mongo

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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 New

func New(ctx context.Context, uri, dbName string) (*Store, error)

New creates a new MongoDB store from a connection URI and database name.

func (*Store) BeginRunAttempt

func (s *Store) BeginRunAttempt(ctx context.Context, id string, generation int64) (bool, error)

func (*Store) Close

func (s *Store) Close() error

Close disconnects the client.

func (*Store) CompleteRun

func (s *Store) CompleteRun(ctx context.Context, c *state.RunCompletion) (state.CompletionResult, error)

func (*Store) CountActiveRuns

func (s *Store) CountActiveRuns(ctx context.Context, agentID string) (int, error)

func (*Store) CountAgents

func (s *Store) CountAgents(ctx context.Context) (int, error)

func (*Store) CountRunsByStatus

func (s *Store) CountRunsByStatus(ctx context.Context) (map[string]int, error)

func (*Store) CountRunsCreatedSince

func (s *Store) CountRunsCreatedSince(ctx context.Context, since time.Time, agentID string) (int, error)

func (*Store) CountThreadsByStatus

func (s *Store) CountThreadsByStatus(ctx context.Context) (map[string]int, error)

func (*Store) CreateCachedRun

func (s *Store) CreateCachedRun(ctx context.Context, run *models.Run, caps *state.RunAdmissionCaps) error

func (*Store) CreateRun

func (s *Store) CreateRun(ctx context.Context, run *models.Run) error

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 (s *Store) CreateThread(ctx context.Context, thread *models.Thread) error

func (*Store) DeleteBoundOpaqueCheckpoint

func (s *Store) DeleteBoundOpaqueCheckpoint(ctx context.Context, binding state.RunCheckpointBinding, checkpointID string) error

func (*Store) DeleteCronSchedule

func (s *Store) DeleteCronSchedule(ctx context.Context, name string) error

func (*Store) DeleteItem

func (s *Store) DeleteItem(ctx context.Context, namespace []string, key string) error

func (*Store) DeleteOpaqueCheckpoint

func (s *Store) DeleteOpaqueCheckpoint(ctx context.Context, threadID, checkpointID string) error

func (*Store) DeleteRegistryEntry

func (s *Store) DeleteRegistryEntry(ctx context.Context, name string) error

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) DeleteRun

func (s *Store) DeleteRun(ctx context.Context, runID string) error

func (*Store) DeleteThread

func (s *Store) DeleteThread(ctx context.Context, threadID string) error

func (*Store) Downgrade

func (s *Store) Downgrade(ctx context.Context) error

Downgrade rolls back the most recently applied migration.

func (*Store) GetAgent

func (s *Store) GetAgent(ctx context.Context, agentID string) (*models.Agent, error)

func (*Store) GetAgentSchema

func (s *Store) GetAgentSchema(ctx context.Context, agentID string) (*models.AgentSchema, error)

func (*Store) GetAgentVersion

func (s *Store) GetAgentVersion(ctx context.Context, agentID string, version int) (*models.AgentVersion, error)

func (*Store) GetCachedRunResult

func (s *Store) GetCachedRunResult(ctx context.Context, cacheKey string) (*models.CachedRunResult, error)

func (*Store) GetItem

func (s *Store) GetItem(ctx context.Context, namespace []string, key string, refreshTTL bool) (*models.StoreItem, error)

func (*Store) GetLastCronFireTime

func (s *Store) GetLastCronFireTime(ctx context.Context, scheduleName string) (time.Time, bool, error)

func (*Store) GetLatestCheckpoint

func (s *Store) GetLatestCheckpoint(ctx context.Context, threadID string) (*models.ThreadState, error)

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 (s *Store) GetOpaqueCheckpoint(ctx context.Context, threadID, checkpointID string) (*models.OpaqueCheckpoint, error)

func (*Store) GetRegistryEntry

func (s *Store) GetRegistryEntry(ctx context.Context, name string) (*models.RegistryEntry, error)

func (*Store) GetRegistryEntryVersion

func (s *Store) GetRegistryEntryVersion(ctx context.Context, name string, version int) (*models.RegistryEntryVersion, error)

func (*Store) GetRun

func (s *Store) GetRun(ctx context.Context, runID string) (*models.Run, error)

func (*Store) GetThread

func (s *Store) GetThread(ctx context.Context, threadID string) (*models.Thread, error)

func (*Store) GetWebhookDeadLetter

func (s *Store) GetWebhookDeadLetter(ctx context.Context, id string) (*models.WebhookDeadLetter, error)

func (*Store) Init

func (s *Store) Init(ctx context.Context) error

Init applies pending numbered schema migrations (indexes + backfills).

func (*Store) ListActiveRunsCreatedBefore

func (s *Store) ListActiveRunsCreatedBefore(ctx context.Context, before time.Time, limit int) ([]*models.Run, error)

func (*Store) ListAgentVersions

func (s *Store) ListAgentVersions(ctx context.Context, agentID string) ([]*models.AgentVersion, error)

func (*Store) ListCheckpoints

func (s *Store) ListCheckpoints(ctx context.Context, threadID string, limit int, before string) ([]*models.ThreadState, error)

func (*Store) ListCronSchedules

func (s *Store) ListCronSchedules(ctx context.Context) ([]*models.CronSchedule, error)

func (*Store) ListNamespaces

func (s *Store) ListNamespaces(ctx context.Context, req *models.StoreListNamespacesRequest) ([][]string, error)

func (*Store) ListOpaqueCheckpoints

func (s *Store) ListOpaqueCheckpoints(ctx context.Context, threadID string, limit int) ([]models.OpaqueCheckpointMeta, error)

func (*Store) ListRegistryEntryVersions

func (s *Store) ListRegistryEntryVersions(ctx context.Context, name string) ([]*models.RegistryEntryVersion, error)

func (*Store) ListWebhookDeadLetters

func (s *Store) ListWebhookDeadLetters(ctx context.Context, limit int) ([]*models.WebhookDeadLetter, error)

func (*Store) Ping

func (s *Store) Ping(ctx context.Context) error

Ping verifies the client can still reach the Mongo deployment right now.

func (*Store) PruneCheckpoints

func (s *Store) PruneCheckpoints(ctx context.Context, keepLast int) (int64, error)

func (*Store) PruneCronClaims

func (s *Store) PruneCronClaims(ctx context.Context, olderThan time.Time) (int64, error)

func (*Store) PruneExpiredStoreItems

func (s *Store) PruneExpiredStoreItems(ctx context.Context) (int64, error)

func (*Store) PruneOpaqueCheckpoints

func (s *Store) PruneOpaqueCheckpoints(ctx context.Context, keepLast int) (int64, error)

func (*Store) PruneRuns

func (s *Store) PruneRuns(ctx context.Context, olderThan time.Time) (int64, error)

func (*Store) PruneTerminalHookClaims

func (s *Store) PruneTerminalHookClaims(ctx context.Context, olderThan time.Time) (int64, error)

func (*Store) PruneWebhookDeadLetters

func (s *Store) PruneWebhookDeadLetters(ctx context.Context, olderThan time.Time) (int64, error)

func (*Store) PublishRegistryEntry

func (s *Store) PublishRegistryEntry(ctx context.Context, entry *models.RegistryEntry) error

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 (s *Store) PutBoundOpaqueCheckpoint(ctx context.Context, binding state.RunCheckpointBinding, checkpointID string, data []byte, framework string, ifMatch *int64) (int64, error)

func (*Store) PutItem

func (s *Store) PutItem(ctx context.Context, item *models.StoreItem) error

func (*Store) PutOpaqueCheckpoint

func (s *Store) PutOpaqueCheckpoint(ctx context.Context, threadID, checkpointID string, data []byte, framework string, ifMatch *int64) (int64, error)

func (*Store) ReleaseCronClaim

func (s *Store) ReleaseCronClaim(ctx context.Context, scheduleName string, fireTime time.Time) error

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 (s *Store) SaveCachedRunResult(ctx context.Context, result *models.CachedRunResult) error

func (*Store) SaveCheckpoint

func (s *Store) SaveCheckpoint(ctx context.Context, threadID string, ts *models.ThreadState) error

func (*Store) SaveWebhookDeadLetter

func (s *Store) SaveWebhookDeadLetter(ctx context.Context, dl *models.WebhookDeadLetter) error

func (*Store) SearchAgents

func (s *Store) SearchAgents(ctx context.Context, req *models.AgentSearchRequest) ([]*models.Agent, error)

func (*Store) SearchItems

func (s *Store) SearchItems(ctx context.Context, req *models.StoreSearchRequest) ([]*models.StoreItem, error)

func (*Store) SearchRegistryEntries

func (s *Store) SearchRegistryEntries(ctx context.Context, req *models.RegistrySearchRequest) ([]*models.RegistryEntry, error)

func (*Store) SearchRuns

func (s *Store) SearchRuns(ctx context.Context, req *models.RunSearchRequest) ([]*models.Run, error)

func (*Store) SearchThreads

func (s *Store) SearchThreads(ctx context.Context, req *models.ThreadSearchRequest) ([]*models.Thread, error)

func (*Store) SetThreadStatus

func (s *Store) SetThreadStatus(ctx context.Context, threadID string, status models.ThreadStatus) error

func (*Store) TruncateAll

func (s *Store) TruncateAll(ctx context.Context) error

TruncateAll removes all documents from all collections. For testing only.

func (*Store) TryClaimCronFire

func (s *Store) TryClaimCronFire(ctx context.Context, scheduleName string, fireTime time.Time) (bool, error)

func (*Store) TryClaimTerminalHook

func (s *Store) TryClaimTerminalHook(ctx context.Context, runID string) (bool, error)

func (*Store) TryClaimThread

func (s *Store) TryClaimThread(ctx context.Context, threadID string) (bool, error)

func (*Store) TryMarkRunTimeout

func (s *Store) TryMarkRunTimeout(ctx context.Context, runID string, errMsg string) (bool, error)

func (*Store) UpdateRunStatus

func (s *Store) UpdateRunStatus(ctx context.Context, runID string, status models.RunStatus, output []byte, errMsg string) error

func (*Store) UpdateThread

func (s *Store) UpdateThread(ctx context.Context, threadID string, patch *models.ThreadPatch) (*models.Thread, error)

func (*Store) UpsertAgent

func (s *Store) UpsertAgent(ctx context.Context, agent *models.Agent) error

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.

func (*Store) UpsertAgentSchema

func (s *Store) UpsertAgentSchema(ctx context.Context, schema *models.AgentSchema) error

func (*Store) UpsertCronSchedule

func (s *Store) UpsertCronSchedule(ctx context.Context, sched *models.CronSchedule) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL