Files
kandev/apps/backend/internal/orchestrator/messagequeue/repository.go
T
José Almeida 7de788ceed 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
2026-07-23 09:03:56 +01:00

103 lines
5.2 KiB
Go

package messagequeue
import "context"
// Repository abstracts persistent storage of queued messages and pending moves.
// Operations on queued messages are atomic per session.
type Repository interface {
// Insert appends a new entry at the tail of the session's FIFO queue.
// Returns ErrQueueFull if maxPerSession > 0 and the count would exceed it.
Insert(ctx context.Context, msg *QueuedMessage, maxPerSession int) error
// AppendOrInsertTail concatenates content onto the tail entry when the tail
// exists AND its QueuedBy matches the supplied queuedBy. Otherwise inserts a
// new entry. Returns the resulting entry and whether the call appended (true)
// or inserted (false). Honors maxPerSession when inserting.
AppendOrInsertTail(ctx context.Context, sessionID, taskID, content, model, queuedBy string, planMode bool, attachments []MessageAttachment, metadata map[string]interface{}, maxPerSession int) (*QueuedMessage, bool, error)
// InsertOrReplaceByCoalesceKey replaces an existing queued entry with the
// same session, queued_by, and metadata coalesce key. If no matching entry
// exists, it inserts a new tail entry when allowInsert is true. Returns
// ErrEntryNotFound when allowInsert is false and no matching entry exists.
InsertOrReplaceByCoalesceKey(ctx context.Context, msg *QueuedMessage, coalesceKey string, maxPerSession int, allowInsert bool) (*QueuedMessage, bool, error)
// ListBySession returns all entries for a session ordered by position ascending.
ListBySession(ctx context.Context, sessionID string) ([]QueuedMessage, error)
// CountBySession returns the number of entries for a session.
CountBySession(ctx context.Context, sessionID string) (int, error)
// TakeHead atomically returns and deletes the lowest-position entry for the
// session. Returns nil, nil if the queue is empty.
TakeHead(ctx context.Context, sessionID string) (*QueuedMessage, error)
// TakeByID atomically returns and deletes the entry identified by entryID
// for sessionID, regardless of its FIFO position. Unlike DeleteByID, it has
// no queued_by="agent" guard: the caller is internal orchestrator code
// (InterruptForPeerMessage) dispatching the specific entry that triggered
// its own interrupt, not a user-driven "remove queued message" request.
// Returns nil, nil if no entry matches (already taken or never existed).
TakeByID(ctx context.Context, sessionID, entryID string) (*QueuedMessage, error)
// UpdateContent replaces the content/attachments of an entry. The session
// scope (`AND session_id = ?`) is mandatory so a caller can't update an
// entry by guessing its UUID across sessions. If queuedBy is non-empty the
// update only succeeds when the entry's queued_by also matches. Returns
// 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
// entry IDs, so without this guard a hostile agent could prune another
// task's queue. Agent-authored entries (`queued_by="agent"`) are immutable
// from this path and return ErrEntryNotFound.
DeleteByID(ctx context.Context, sessionID, entryID string) error
// DeleteAllBySession removes every entry for a session. Returns the count of
// rows removed.
DeleteAllBySession(ctx context.Context, sessionID string) (int, error)
// TransferSession moves all entries (and any pending move) from oldSessionID
// to newSessionID. Used on workflow session switches.
TransferSession(ctx context.Context, oldSessionID, newSessionID string) error
// ReplaceSession replaces a session's queued entries and pending move with
// the supplied snapshot, preserving queued-message identity fields.
ReplaceSession(ctx context.Context, sessionID string, entries []QueuedMessage, pendingMove *PendingMove) error
// SetPendingMove upserts the deferred workflow move for a session.
SetPendingMove(ctx context.Context, sessionID string, move *PendingMove) error
// GetPendingMove returns the deferred move for a session without removing it.
// Returns nil, nil if absent.
GetPendingMove(ctx context.Context, sessionID string) (*PendingMove, error)
// TakePendingMove returns and removes the deferred move for a session.
// 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
}