feat: add cross-integration entity references (#1862)
* feat: add cross-integration entity references * fix(web): anchor entity reference menu to composer * fix: keep Kandev tasks under @ mentions * fix: address entity reference review findings * test(e2e): use option role for prompt menu * test(e2e): update passthrough mention roles * fix: preserve queued references and paginate Azure PRs
This commit is contained in:
@@ -46,6 +46,11 @@ type Repository interface {
|
||||
// ErrEntryNotFound when no row matches all guards.
|
||||
UpdateContent(ctx context.Context, sessionID, entryID, content string, attachments []MessageAttachment, queuedBy string) error
|
||||
|
||||
// UpdateContentAndMetadata atomically replaces content/attachments and
|
||||
// applies metadata key updates. A nil update value removes that key while
|
||||
// unrelated metadata remains unchanged.
|
||||
UpdateContentAndMetadata(ctx context.Context, sessionID, entryID, content string, attachments []MessageAttachment, metadataUpdates map[string]interface{}, queuedBy string) error
|
||||
|
||||
// DeleteByID removes a single entry. The session scope (`AND session_id = ?`)
|
||||
// is mandatory so a caller can't delete an entry by guessing its UUID across
|
||||
// sessions — the queue_full MCP payload deliberately discloses sibling-task
|
||||
@@ -77,3 +82,21 @@ type Repository interface {
|
||||
// Returns nil, nil if absent.
|
||||
TakePendingMove(ctx context.Context, sessionID string) (*PendingMove, error)
|
||||
}
|
||||
|
||||
func applyMetadataUpdates(current, updates map[string]interface{}) map[string]interface{} {
|
||||
merged := make(map[string]interface{}, len(current)+len(updates))
|
||||
for key, value := range current {
|
||||
merged[key] = value
|
||||
}
|
||||
for key, value := range updates {
|
||||
if value == nil {
|
||||
delete(merged, key)
|
||||
continue
|
||||
}
|
||||
merged[key] = value
|
||||
}
|
||||
if len(merged) == 0 {
|
||||
return nil
|
||||
}
|
||||
return merged
|
||||
}
|
||||
|
||||
@@ -179,7 +179,11 @@ func (r *memoryRepository) TakeByID(_ context.Context, sessionID, entryID string
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *memoryRepository) UpdateContent(_ context.Context, sessionID, entryID, content string, attachments []MessageAttachment, queuedBy string) error {
|
||||
func (r *memoryRepository) UpdateContent(ctx context.Context, sessionID, entryID, content string, attachments []MessageAttachment, queuedBy string) error {
|
||||
return r.UpdateContentAndMetadata(ctx, sessionID, entryID, content, attachments, nil, queuedBy)
|
||||
}
|
||||
|
||||
func (r *memoryRepository) UpdateContentAndMetadata(_ context.Context, sessionID, entryID, content string, attachments []MessageAttachment, metadataUpdates map[string]interface{}, queuedBy string) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
list, ok := r.entries[sessionID]
|
||||
@@ -195,6 +199,7 @@ func (r *memoryRepository) UpdateContent(_ context.Context, sessionID, entryID,
|
||||
}
|
||||
m.Content = content
|
||||
m.Attachments = attachments
|
||||
m.Metadata = applyMetadataUpdates(m.Metadata, metadataUpdates)
|
||||
return nil
|
||||
}
|
||||
return ErrEntryNotFound
|
||||
|
||||
@@ -431,19 +431,55 @@ func (r *sqliteRepository) TakeByID(ctx context.Context, sessionID, entryID stri
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) UpdateContent(ctx context.Context, sessionID, entryID, content string, attachments []MessageAttachment, queuedBy string) error {
|
||||
return r.UpdateContentAndMetadata(ctx, sessionID, entryID, content, attachments, nil, queuedBy)
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) UpdateContentAndMetadata(ctx context.Context, sessionID, entryID, content string, attachments []MessageAttachment, metadataUpdates map[string]interface{}, queuedBy string) error {
|
||||
attachmentsJSON, err := marshalAttachments(attachments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var query string
|
||||
args := []interface{}{content, attachmentsJSON, entryID, sessionID}
|
||||
tx, err := r.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin update queued tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var selectQuery string
|
||||
selectArgs := []interface{}{entryID, sessionID}
|
||||
if queuedBy != "" {
|
||||
query = `UPDATE queued_messages SET content = ?, attachments_json = ? WHERE id = ? AND session_id = ? AND queued_by = ?`
|
||||
selectQuery = `SELECT metadata_json FROM queued_messages WHERE id = ? AND session_id = ? AND queued_by = ?`
|
||||
selectArgs = append(selectArgs, queuedBy)
|
||||
} else {
|
||||
selectQuery = `SELECT metadata_json FROM queued_messages WHERE id = ? AND session_id = ?`
|
||||
}
|
||||
var metadataJSON string
|
||||
if err := tx.GetContext(ctx, &metadataJSON, r.db.Rebind(selectQuery), selectArgs...); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ErrEntryNotFound
|
||||
}
|
||||
return fmt.Errorf("read queued metadata: %w", err)
|
||||
}
|
||||
var metadata map[string]interface{}
|
||||
if metadataJSON != "" && metadataJSON != "{}" {
|
||||
if err := json.Unmarshal([]byte(metadataJSON), &metadata); err != nil {
|
||||
return fmt.Errorf("unmarshal queued metadata: %w", err)
|
||||
}
|
||||
}
|
||||
metadataJSON, err = marshalMetadata(applyMetadataUpdates(metadata, metadataUpdates))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var updateQuery string
|
||||
args := []interface{}{content, attachmentsJSON, metadataJSON, entryID, sessionID}
|
||||
if queuedBy != "" {
|
||||
updateQuery = `UPDATE queued_messages SET content = ?, attachments_json = ?, metadata_json = ? WHERE id = ? AND session_id = ? AND queued_by = ?`
|
||||
args = append(args, queuedBy)
|
||||
} else {
|
||||
query = `UPDATE queued_messages SET content = ?, attachments_json = ? WHERE id = ? AND session_id = ?`
|
||||
updateQuery = `UPDATE queued_messages SET content = ?, attachments_json = ?, metadata_json = ? WHERE id = ? AND session_id = ?`
|
||||
}
|
||||
res, err := r.db.ExecContext(ctx, r.db.Rebind(query), args...)
|
||||
res, err := tx.ExecContext(ctx, r.db.Rebind(updateQuery), args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update queued: %w", err)
|
||||
}
|
||||
@@ -454,7 +490,7 @@ func (r *sqliteRepository) UpdateContent(ctx context.Context, sessionID, entryID
|
||||
if n == 0 {
|
||||
return ErrEntryNotFound
|
||||
}
|
||||
return nil
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (r *sqliteRepository) DeleteByID(ctx context.Context, sessionID, entryID string) error {
|
||||
|
||||
@@ -187,6 +187,69 @@ func TestSQLiteRepository_UpdateContent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteRepository_UpdateContentAndMetadataPreservesUnrelatedKeys(t *testing.T) {
|
||||
repo := newTestSQLiteRepo(t)
|
||||
ctx := context.Background()
|
||||
msg := &QueuedMessage{
|
||||
SessionID: "s1",
|
||||
TaskID: "t1",
|
||||
Content: "original",
|
||||
QueuedBy: "user-1",
|
||||
Metadata: map[string]interface{}{
|
||||
"entity_references": []interface{}{"old"},
|
||||
"origin": "inter-task",
|
||||
},
|
||||
}
|
||||
if err := repo.Insert(ctx, msg, 0); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
if err := repo.UpdateContentAndMetadata(
|
||||
ctx, "s1", msg.ID, "edited", nil,
|
||||
map[string]interface{}{"entity_references": []interface{}{"new"}},
|
||||
"user-1",
|
||||
); err != nil {
|
||||
t.Fatalf("update content and metadata: %v", err)
|
||||
}
|
||||
|
||||
entries, err := repo.ListBySession(ctx, "s1")
|
||||
if err != nil {
|
||||
t.Fatalf("list: %v", err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("expected one entry, got %d", len(entries))
|
||||
}
|
||||
if entries[0].Content != "edited" {
|
||||
t.Fatalf("content = %q, want edited", entries[0].Content)
|
||||
}
|
||||
if entries[0].Metadata["origin"] != "inter-task" {
|
||||
t.Fatalf("unrelated metadata lost: %+v", entries[0].Metadata)
|
||||
}
|
||||
wantReferences := []interface{}{"new"}
|
||||
gotReferences, ok := entries[0].Metadata["entity_references"].([]interface{})
|
||||
if !ok || len(gotReferences) != len(wantReferences) || gotReferences[0] != wantReferences[0] {
|
||||
t.Fatalf("entity references = %#v, want %#v", entries[0].Metadata["entity_references"], wantReferences)
|
||||
}
|
||||
|
||||
if err := repo.UpdateContentAndMetadata(
|
||||
ctx, "s1", msg.ID, "edited again", nil,
|
||||
map[string]interface{}{"entity_references": nil},
|
||||
"user-1",
|
||||
); err != nil {
|
||||
t.Fatalf("clear references: %v", err)
|
||||
}
|
||||
entries, err = repo.ListBySession(ctx, "s1")
|
||||
if err != nil {
|
||||
t.Fatalf("list after clear: %v", err)
|
||||
}
|
||||
if entries[0].Metadata["origin"] != "inter-task" {
|
||||
t.Fatalf("unrelated metadata lost while clearing: %+v", entries[0].Metadata)
|
||||
}
|
||||
if _, exists := entries[0].Metadata["entity_references"]; exists {
|
||||
t.Fatalf("stale references survived clear: %+v", entries[0].Metadata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteRepository_ReplaceCoalescedDetectsMissingRow(t *testing.T) {
|
||||
repo := newTestSQLiteRepo(t).(*sqliteRepository)
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -54,7 +54,7 @@ func (s *Service) QueueMessage(ctx context.Context, sessionID, taskID, content,
|
||||
// is propagated to the resulting Message row when the queued message is
|
||||
// drained (e.g. sender_task_id for messages sent via message_task_kandev).
|
||||
func (s *Service) QueueMessageWithMetadata(ctx context.Context, sessionID, taskID, content, model, userID string, planMode bool, attachments []MessageAttachment, metadata map[string]interface{}) (*QueuedMessage, error) {
|
||||
metadataCopy := copyMessageMetadata(metadata, 0)
|
||||
metadataCopy := copyMessageMetadata(metadata, false)
|
||||
msg := &QueuedMessage{
|
||||
SessionID: sessionID,
|
||||
TaskID: taskID,
|
||||
@@ -87,7 +87,7 @@ func (s *Service) QueueMessageWithMetadata(ctx context.Context, sessionID, taskI
|
||||
// inserts a new tail entry if allowInsert is true; otherwise ErrEntryNotFound is
|
||||
// returned. The returned bool is true when an existing entry was replaced.
|
||||
func (s *Service) QueueMessageWithCoalesceKey(ctx context.Context, sessionID, taskID, content, model, userID string, planMode bool, attachments []MessageAttachment, metadata map[string]interface{}, coalesceKey string, allowInsert bool) (*QueuedMessage, bool, error) {
|
||||
metadataCopy := copyMessageMetadata(metadata, 1)
|
||||
metadataCopy := copyMessageMetadata(metadata, true)
|
||||
metadataCopy[MetadataCoalesceKey] = coalesceKey
|
||||
msg := &QueuedMessage{
|
||||
SessionID: sessionID,
|
||||
@@ -119,11 +119,11 @@ func (s *Service) QueueMessageWithCoalesceKey(ctx context.Context, sessionID, ta
|
||||
return queued, replaced, nil
|
||||
}
|
||||
|
||||
func copyMessageMetadata(metadata map[string]interface{}, extraCapacity int) map[string]interface{} {
|
||||
if len(metadata) == 0 && extraCapacity == 0 {
|
||||
func copyMessageMetadata(metadata map[string]interface{}, ensureWritable bool) map[string]interface{} {
|
||||
if len(metadata) == 0 && !ensureWritable {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]interface{}, len(metadata)+extraCapacity)
|
||||
out := make(map[string]interface{}, len(metadata))
|
||||
for key, value := range metadata {
|
||||
out[key] = value
|
||||
}
|
||||
@@ -200,7 +200,13 @@ func (s *Service) TakeQueuedEntry(ctx context.Context, sessionID, entryID string
|
||||
// was already drained, the session doesn't own it, or the queuedBy guard
|
||||
// rejects the caller.
|
||||
func (s *Service) UpdateMessage(ctx context.Context, sessionID, entryID, content string, attachments []MessageAttachment, queuedBy string) error {
|
||||
if err := s.repo.UpdateContent(ctx, sessionID, entryID, content, attachments, queuedBy); err != nil {
|
||||
return s.UpdateMessageWithMetadata(ctx, sessionID, entryID, content, attachments, nil, queuedBy)
|
||||
}
|
||||
|
||||
// UpdateMessageWithMetadata atomically edits queue content and applies
|
||||
// metadata replacements while retaining unrelated metadata keys.
|
||||
func (s *Service) UpdateMessageWithMetadata(ctx context.Context, sessionID, entryID, content string, attachments []MessageAttachment, metadataUpdates map[string]interface{}, queuedBy string) error {
|
||||
if err := s.repo.UpdateContentAndMetadata(ctx, sessionID, entryID, content, attachments, metadataUpdates, queuedBy); err != nil {
|
||||
return err
|
||||
}
|
||||
s.logger.Info("queued entry updated",
|
||||
|
||||
@@ -368,6 +368,61 @@ func TestUpdateMessage(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestMemoryRepository_UpdateContentAndMetadataPreservesUnrelatedKeys(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := NewMemoryRepository()
|
||||
msg := &QueuedMessage{
|
||||
SessionID: "s",
|
||||
TaskID: "t",
|
||||
Content: "original",
|
||||
QueuedBy: "user-1",
|
||||
Metadata: map[string]interface{}{
|
||||
"entity_references": []interface{}{"old"},
|
||||
"origin": "inter-task",
|
||||
},
|
||||
}
|
||||
require.NoError(t, repo.Insert(ctx, msg, 0))
|
||||
|
||||
require.NoError(t, repo.UpdateContentAndMetadata(
|
||||
ctx, "s", msg.ID, "edited", nil,
|
||||
map[string]interface{}{"entity_references": []interface{}{"new"}},
|
||||
"user-1",
|
||||
))
|
||||
|
||||
entries, err := repo.ListBySession(ctx, "s")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, entries, 1)
|
||||
assert.Equal(t, "edited", entries[0].Content)
|
||||
assert.Equal(t, "inter-task", entries[0].Metadata["origin"])
|
||||
assert.Equal(t, []interface{}{"new"}, entries[0].Metadata["entity_references"])
|
||||
}
|
||||
|
||||
func TestUpdateMessageWithMetadataClearsEntityReferences(t *testing.T) {
|
||||
svc := setupService(t)
|
||||
ctx := context.Background()
|
||||
msg, err := svc.QueueMessageWithMetadata(
|
||||
ctx, "s", "t", "original", "", "user-1", false, nil,
|
||||
map[string]interface{}{
|
||||
"entity_references": []interface{}{"old"},
|
||||
"origin": "inter-task",
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, svc.UpdateMessageWithMetadata(
|
||||
ctx, "s", msg.ID, "edited", nil,
|
||||
map[string]interface{}{"entity_references": nil},
|
||||
"user-1",
|
||||
))
|
||||
|
||||
entries := svc.GetStatus(ctx, "s").Entries
|
||||
require.Len(t, entries, 1)
|
||||
assert.Equal(t, "edited", entries[0].Content)
|
||||
assert.Equal(t, "inter-task", entries[0].Metadata["origin"])
|
||||
_, exists := entries[0].Metadata["entity_references"]
|
||||
assert.False(t, exists, "empty replacement must remove stale references")
|
||||
}
|
||||
|
||||
func TestRemoveEntry(t *testing.T) {
|
||||
t.Run("removes the targeted entry", func(t *testing.T) {
|
||||
svc := setupService(t)
|
||||
|
||||
@@ -20,9 +20,13 @@ const (
|
||||
QueuedByWorkflow = "workflow"
|
||||
)
|
||||
|
||||
// MetadataCoalesceKey identifies queued entries that should be replaced rather
|
||||
// than appended when a newer pending message supersedes an older one.
|
||||
const MetadataCoalesceKey = "coalesce_key"
|
||||
const (
|
||||
// MetadataCoalesceKey identifies queued entries that should be replaced
|
||||
// rather than appended when newer pending content supersedes older content.
|
||||
MetadataCoalesceKey = "coalesce_key"
|
||||
// MetadataEntityReferences stores normalized composer references.
|
||||
MetadataEntityReferences = "entity_references"
|
||||
)
|
||||
|
||||
// QueueFullErrorCode is the well-known WS / MCP error code surfaced when an
|
||||
// insert would exceed the per-session cap. Shared between the user-side WS
|
||||
|
||||
Reference in New Issue
Block a user