Documentation
¶
Overview ¶
Package mongo implements the state.Store interface using MongoDB. This is the project's non-SQL exemplar backend -- proof that state.Store is genuinely implementable against a document store, not just SQL databases, and a template for community-contributed backends.
Design notes (see internal/state/postgres for the reference SQL implementation this mirrors field-for-field):
- Collections map 1:1 to Postgres tables. Unique indexes (created in Init) enforce the same composite-key semantics Postgres's PRIMARY KEY/UNIQUE INDEX pairs do (e.g. agents on (tenant_id, agent_id), threads on thread_id alone -- matching Postgres's schema exactly, including which tables scope uniqueness by tenant and which don't).
- JSON fields stay native, queryable BSON documents. Application values use exact int64/Decimal128 conversion and return json.Number; values outside that native range fail before writes. Control metadata keeps the older normalizeBSON behavior so its established Go scalar types remain unchanged. Existing stored doubles retain their represented value; precision lost by a previous binary cannot be recovered.
- Namespace arrays (key-value store) reuse the exact same \x1F delimited string encoding Postgres/SQLite use (nsToString/ stringToNs/nsPrefixPattern), so prefix search is a regex anchored at "^", not a Mongo-specific array-prefix trick -- guarantees identical observable behavior to the SQL backends for the same conformance suite.
- UpsertAgent, PublishRegistryEntry, and DeleteRegistryEntry each run inside a real multi-document Mongo transaction (see withTransaction's doc comment) -- the connected Mongo must be a replica set (even a single-node one) for this, which docker-compose.test.yml/CI now provide. Every other multi-step write in this package (DeleteThread's cascade, etc.) still isn't transactional; those three were the ones with a documented, previously-deferred atomicity gap vs Postgres/SQLite's SQL transactions specifically because a replica set was the missing prerequisite, not a decision that the others don't matter.
Index ¶
- type Store
- func (s *Store) BeginRunAttempt(ctx context.Context, id string, generation int64) (bool, error)
- func (s *Store) Close() error
- func (s *Store) CompleteRun(ctx context.Context, c *state.RunCompletion) (state.CompletionResult, error)
- func (s *Store) CountActiveRuns(ctx context.Context, agentID string) (int, error)
- func (s *Store) CountAgents(ctx context.Context) (int, error)
- func (s *Store) CountRunsByStatus(ctx context.Context) (map[string]int, error)
- func (s *Store) CountRunsCreatedSince(ctx context.Context, since time.Time, agentID string) (int, error)
- func (s *Store) CountThreadsByStatus(ctx context.Context) (map[string]int, error)
- func (s *Store) CreateCachedRun(ctx context.Context, run *models.Run, caps *state.RunAdmissionCaps) error
- func (s *Store) CreateRun(ctx context.Context, run *models.Run) error
- func (s *Store) CreateRunAdmitted(ctx context.Context, run *models.Run, caps *state.RunAdmissionCaps) error
- func (s *Store) CreateThread(ctx context.Context, thread *models.Thread) error
- func (s *Store) DeleteBoundOpaqueCheckpoint(ctx context.Context, binding state.RunCheckpointBinding, checkpointID string) error
- func (s *Store) DeleteCronSchedule(ctx context.Context, name string) error
- func (s *Store) DeleteItem(ctx context.Context, namespace []string, key string) error
- func (s *Store) DeleteOpaqueCheckpoint(ctx context.Context, threadID, checkpointID string) error
- func (s *Store) DeleteRegistryEntry(ctx context.Context, name string) error
- func (s *Store) DeleteRun(ctx context.Context, runID string) error
- func (s *Store) DeleteThread(ctx context.Context, threadID string) error
- func (s *Store) Downgrade(ctx context.Context) error
- func (s *Store) GetAgent(ctx context.Context, agentID string) (*models.Agent, error)
- func (s *Store) GetAgentSchema(ctx context.Context, agentID string) (*models.AgentSchema, error)
- func (s *Store) GetAgentVersion(ctx context.Context, agentID string, version int) (*models.AgentVersion, error)
- func (s *Store) GetCachedRunResult(ctx context.Context, cacheKey string) (*models.CachedRunResult, error)
- func (s *Store) GetItem(ctx context.Context, namespace []string, key string, refreshTTL bool) (*models.StoreItem, error)
- func (s *Store) GetLastCronFireTime(ctx context.Context, scheduleName string) (time.Time, bool, error)
- func (s *Store) GetLatestCheckpoint(ctx context.Context, threadID string) (*models.ThreadState, error)
- func (s *Store) GetLatestOpaqueCheckpoint(ctx context.Context, threadID, namespace string) (*models.OpaqueCheckpoint, error)
- func (s *Store) GetOpaqueCheckpoint(ctx context.Context, threadID, checkpointID string) (*models.OpaqueCheckpoint, error)
- func (s *Store) GetRegistryEntry(ctx context.Context, name string) (*models.RegistryEntry, error)
- func (s *Store) GetRegistryEntryVersion(ctx context.Context, name string, version int) (*models.RegistryEntryVersion, error)
- func (s *Store) GetRun(ctx context.Context, runID string) (*models.Run, error)
- func (s *Store) GetThread(ctx context.Context, threadID string) (*models.Thread, error)
- func (s *Store) GetWebhookDeadLetter(ctx context.Context, id string) (*models.WebhookDeadLetter, error)
- func (s *Store) Init(ctx context.Context) error
- func (s *Store) ListActiveRunsCreatedBefore(ctx context.Context, before time.Time, limit int) ([]*models.Run, error)
- func (s *Store) ListAgentVersions(ctx context.Context, agentID string) ([]*models.AgentVersion, error)
- func (s *Store) ListCheckpoints(ctx context.Context, threadID string, limit int, before string) ([]*models.ThreadState, error)
- func (s *Store) ListCronSchedules(ctx context.Context) ([]*models.CronSchedule, error)
- func (s *Store) ListNamespaces(ctx context.Context, req *models.StoreListNamespacesRequest) ([][]string, error)
- func (s *Store) ListOpaqueCheckpoints(ctx context.Context, threadID string, limit int) ([]models.OpaqueCheckpointMeta, error)
- func (s *Store) ListRegistryEntryVersions(ctx context.Context, name string) ([]*models.RegistryEntryVersion, error)
- func (s *Store) ListWebhookDeadLetters(ctx context.Context, limit int) ([]*models.WebhookDeadLetter, error)
- func (s *Store) Ping(ctx context.Context) error
- func (s *Store) PruneCheckpoints(ctx context.Context, keepLast int) (int64, error)
- func (s *Store) PruneCronClaims(ctx context.Context, olderThan time.Time) (int64, error)
- func (s *Store) PruneExpiredStoreItems(ctx context.Context) (int64, error)
- func (s *Store) PruneOpaqueCheckpoints(ctx context.Context, keepLast int) (int64, error)
- func (s *Store) PruneRuns(ctx context.Context, olderThan time.Time) (int64, error)
- func (s *Store) PruneTerminalHookClaims(ctx context.Context, olderThan time.Time) (int64, error)
- func (s *Store) PruneWebhookDeadLetters(ctx context.Context, olderThan time.Time) (int64, error)
- func (s *Store) PublishRegistryEntry(ctx context.Context, entry *models.RegistryEntry) error
- func (s *Store) PutBoundOpaqueCheckpoint(ctx context.Context, binding state.RunCheckpointBinding, checkpointID string, ...) (int64, error)
- func (s *Store) PutItem(ctx context.Context, item *models.StoreItem) error
- func (s *Store) PutOpaqueCheckpoint(ctx context.Context, threadID, checkpointID string, data []byte, ...) (int64, error)
- func (s *Store) ReleaseCronClaim(ctx context.Context, scheduleName string, fireTime time.Time) error
- func (s *Store) ReleaseThreadIfNoOtherActive(ctx context.Context, threadID, excludeRunID string, status models.ThreadStatus) (bool, error)
- func (s *Store) SaveCachedRunResult(ctx context.Context, result *models.CachedRunResult) error
- func (s *Store) SaveCheckpoint(ctx context.Context, threadID string, ts *models.ThreadState) error
- func (s *Store) SaveWebhookDeadLetter(ctx context.Context, dl *models.WebhookDeadLetter) error
- func (s *Store) SearchAgents(ctx context.Context, req *models.AgentSearchRequest) ([]*models.Agent, error)
- func (s *Store) SearchItems(ctx context.Context, req *models.StoreSearchRequest) ([]*models.StoreItem, error)
- func (s *Store) SearchRegistryEntries(ctx context.Context, req *models.RegistrySearchRequest) ([]*models.RegistryEntry, error)
- func (s *Store) SearchRuns(ctx context.Context, req *models.RunSearchRequest) ([]*models.Run, error)
- func (s *Store) SearchThreads(ctx context.Context, req *models.ThreadSearchRequest) ([]*models.Thread, error)
- func (s *Store) SetThreadStatus(ctx context.Context, threadID string, status models.ThreadStatus) error
- func (s *Store) TruncateAll(ctx context.Context) error
- func (s *Store) TryClaimCronFire(ctx context.Context, scheduleName string, fireTime time.Time) (bool, error)
- func (s *Store) TryClaimTerminalHook(ctx context.Context, runID string) (bool, error)
- func (s *Store) TryClaimThread(ctx context.Context, threadID string) (bool, error)
- func (s *Store) TryMarkRunTimeout(ctx context.Context, runID string, errMsg string) (bool, error)
- func (s *Store) UpdateRunStatus(ctx context.Context, runID string, status models.RunStatus, output []byte, ...) error
- func (s *Store) UpdateThread(ctx context.Context, threadID string, patch *models.ThreadPatch) (*models.Thread, error)
- func (s *Store) UpsertAgent(ctx context.Context, agent *models.Agent) error
- func (s *Store) UpsertAgentSchema(ctx context.Context, schema *models.AgentSchema) error
- func (s *Store) UpsertCronSchedule(ctx context.Context, sched *models.CronSchedule) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store implements state.Store with a MongoDB database.
func (*Store) BeginRunAttempt ¶
func (*Store) CompleteRun ¶
func (s *Store) CompleteRun(ctx context.Context, c *state.RunCompletion) (state.CompletionResult, error)
func (*Store) CountActiveRuns ¶
func (*Store) CountRunsByStatus ¶
func (*Store) CountRunsCreatedSince ¶
func (*Store) CountThreadsByStatus ¶
func (*Store) CreateCachedRun ¶
func (*Store) CreateRunAdmitted ¶
func (s *Store) CreateRunAdmitted(ctx context.Context, run *models.Run, caps *state.RunAdmissionCaps) error
CreateRunAdmitted serializes via a lock document write inside a multi-document transaction, then COUNT + INSERT in that same txn so concurrent creators for the same scope cannot all observe a stale zero.
func (*Store) CreateThread ¶
func (*Store) DeleteBoundOpaqueCheckpoint ¶
func (*Store) DeleteCronSchedule ¶
func (*Store) DeleteItem ¶
func (*Store) DeleteOpaqueCheckpoint ¶
func (*Store) DeleteRegistryEntry ¶
DeleteRegistryEntry also removes the entry's own version history -- see the Postgres backend's identical fix for the full rationale (delete-then-republish otherwise resurrects stale pre-delete version snapshots: the unique index on registry_entry_versions would reject the new v1's InsertOne as a duplicate of an orphaned pre-delete row -- wrong here since it's stale, unrelated content from before the delete, not a genuine same-content race PublishRegistryEntry's version-bump logic would correctly treat as benign).
Runs inside a real Mongo transaction so the two deletes commit or roll back together -- a crash between them can no longer leave the entry gone but its version history still present (same class of gap PublishRegistryEntry's non-atomicity used to have, now closed the same way). Returning ErrNotFound from inside the transaction (no entry to delete) is a safe, un-retried abort -- see withTransaction's doc comment: a plain application error, unlike a real write conflict, doesn't carry the "TransientTransactionError" label that would make WithTransaction retry it, and no writes had happened yet anyway.
func (*Store) DeleteThread ¶
func (*Store) GetAgentSchema ¶
func (*Store) GetAgentVersion ¶
func (*Store) GetCachedRunResult ¶
func (*Store) GetLastCronFireTime ¶
func (*Store) GetLatestCheckpoint ¶
func (*Store) GetLatestOpaqueCheckpoint ¶
func (s *Store) GetLatestOpaqueCheckpoint(ctx context.Context, threadID, namespace string) (*models.OpaqueCheckpoint, error)
GetLatestOpaqueCheckpoint returns the newest opaque blob for threadID. namespace "" = root graph keys (checkpoint_id with no "\x1f"); otherwise keys prefixed with namespace+"\x1f" (LangGraph folds checkpoint_ns into the opaque path key this way).
func (*Store) GetOpaqueCheckpoint ¶
func (*Store) GetRegistryEntry ¶
func (*Store) GetRegistryEntryVersion ¶
func (*Store) GetWebhookDeadLetter ¶
func (*Store) ListActiveRunsCreatedBefore ¶
func (*Store) ListAgentVersions ¶
func (*Store) ListCheckpoints ¶
func (*Store) ListCronSchedules ¶
func (*Store) ListNamespaces ¶
func (*Store) ListOpaqueCheckpoints ¶
func (*Store) ListRegistryEntryVersions ¶
func (*Store) ListWebhookDeadLetters ¶
func (*Store) PruneCheckpoints ¶
func (*Store) PruneCronClaims ¶
func (*Store) PruneExpiredStoreItems ¶
func (*Store) PruneOpaqueCheckpoints ¶
func (*Store) PruneTerminalHookClaims ¶
func (*Store) PruneWebhookDeadLetters ¶
func (*Store) PublishRegistryEntry ¶
PublishRegistryEntry runs entirely inside a real Mongo transaction -- same rationale as UpsertAgent's identical shape, see its doc comment and withTransaction's for the full explanation of why no duplicate- key handling is needed here either.
func (*Store) PutBoundOpaqueCheckpoint ¶
func (*Store) PutOpaqueCheckpoint ¶
func (*Store) ReleaseCronClaim ¶
func (*Store) ReleaseThreadIfNoOtherActive ¶
func (s *Store) ReleaseThreadIfNoOtherActive(ctx context.Context, threadID, excludeRunID string, status models.ThreadStatus) (bool, error)
ReleaseThreadIfNoOtherActive checks for other active runs then conditionally updates the thread inside one transaction -- the Mongo equivalent of SQL's UPDATE ... WHERE NOT EXISTS.
func (*Store) SaveCachedRunResult ¶
func (*Store) SaveCheckpoint ¶
func (*Store) SaveWebhookDeadLetter ¶
func (*Store) SearchAgents ¶
func (*Store) SearchItems ¶
func (*Store) SearchRegistryEntries ¶
func (s *Store) SearchRegistryEntries(ctx context.Context, req *models.RegistrySearchRequest) ([]*models.RegistryEntry, error)
func (*Store) SearchRuns ¶
func (*Store) SearchThreads ¶
func (*Store) SetThreadStatus ¶
func (*Store) TruncateAll ¶
TruncateAll removes all documents from all collections. For testing only.
func (*Store) TryClaimCronFire ¶
func (*Store) TryClaimTerminalHook ¶
func (*Store) TryClaimThread ¶
func (*Store) TryMarkRunTimeout ¶
func (*Store) UpdateRunStatus ¶
func (*Store) UpdateThread ¶
func (*Store) UpsertAgent ¶
UpsertAgent runs entirely inside a real Mongo transaction (see withTransaction's doc comment) -- the "agents" update and the conditional "agent_versions" insert either both land or neither does, and a concurrent different-content race for the same (tenant_id, agent_id) is serialized by the transaction machinery itself rather than by any duplicate-key handling in this function.