Skip to content

PostgresStmAdapter

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:65

Postgres-backed short-term memory adapter.

Stores STM records as rows in the shared memories table with type='short-term' and expiresAt set — the schema has carried both columns (plus @@index([expiresAt])) since the baseline migration, so no migration is needed. This replaces the Redis-backed MemoryStmService: STM survives restarts, and both tiers live in one queryable store.

Semantics intentionally mirror the existing adapters:

  • UUID ids (parity with the Redis service; memoryIdSchema accepts them).
  • accessCount and the stored full-TTL window (ttl) live in the metadata JSON, the same convention MemoryLtmService uses for accessCount/importance.
  • Expiry is enforced by filtering on read (expiresAt > now()); expired rows are opportunistically deleted on touch and bulk-removed by sweepExpired() (scheduled by the mcp-server’s StmSweepService).
  • update() preserves expiresAt unless a new ttl is explicitly provided (the WP2-T3/D4 preserve-by-default behavior).
  • expectedVersion updates are a true compare-and-set: the version guard is part of the UPDATE’s WHERE clause, closing the read-compare-set race the Redis implementation documented as deferred.
  • Embeddings are generated best-effort and stored on the row; STM rows are NOT upserted into the vector index (parity with both prior adapters — promotion to LTM is what indexes a memory for semantic recall).

new PostgresStmAdapter(prisma, embeddingsService?): PostgresStmAdapter

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:69

PrismaService

EmbeddingsService

PostgresStmAdapter

clear(userId, organizationId?): Promise<number>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:379

Delete every personal (or org-scoped, when organizationId is given) short-term memory for a user. Returns the number of removed rows.

string

string

Promise<number>


count(userId, options?): Promise<number>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:367

Count short-term memories for a user with optional tag/scope filtering.

string

string

string

string[]

Promise<number>


create(input): Promise<StmMemory>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:79

Create a new short-term memory.

string = contentSchema

Record<string, unknown> = metadataSchema

string = ...

string = ...

string[] = ...

number = ttlSchema

string = userIdSchema

Promise<StmMemory>


delete(userId, memoryId, organizationId?, scope?): Promise<void>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:242

Delete a short-term memory. A scope mismatch is treated as not-found so a caller bound to one namespace cannot delete another’s memory.

string

string

string

string

Promise<void>


extendTtl(userId, memoryId, additionalSeconds, organizationId?): Promise<StmMemory>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:351

Extend a memory’s TTL by additionalSeconds (new total = remaining + additional, validated against the TTL bounds).

string

string

number

string

Promise<StmMemory>


findById(userId, memoryId, organizationId?, scope?): Promise<StmMemory>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:121

Retrieve a short-term memory by ID. Bumps accessCount (best-effort) so the consolidation policy can identify frequently-read memories.

string

string

string

string

Promise<StmMemory>


findCandidates(threshold, userId?): Promise<StmMemory[]>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:396

Find short-term memories with accessCount >= threshold for a user (or globally when userId is omitted). Used by the consolidation job to identify promotion candidates.

number

string

Promise<StmMemory[]>


getTtl(userId, memoryId, organizationId?): Promise<number>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:329

Get the remaining TTL (in seconds) for a memory. Missing and expired memories both surface as not-found (parity with Redis TTL=-2).

string

string

string

Promise<number>


list(userId, options?): Promise<PaginatedResult<StmMemory>>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:285

List short-term memories for a user with cursor pagination and filtering. The cursor contract matches the prior adapters: '0' is the start sentinel; endCursor is '0' when there are no further pages.

string

Partial<ListStmOptionsData> = {}

Promise<PaginatedResult<StmMemory>>


promote(userId, memoryId, organizationId?): Promise<StmMemory>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:423

Promotion hand-off: return the source memory so callers can chain into the LTM service, which performs the durable transfer.

string

string

string

Promise<StmMemory>


sweepExpired(): Promise<number>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:432

Bulk-delete expired STM rows. Correctness never depends on this (every read filters on expiresAt); it is hygiene that keeps the table small. Scheduled by the mcp-server’s StmSweepService.

Promise<number>


update(userId, memoryId, input, organizationId?, scope?): Promise<StmMemory>

Defined in: packages/memory-stm/src/adapters/postgres-stm.adapter.ts:157

Update a short-term memory. Preserves expiresAt unless ttl is explicitly provided. When expectedVersion is set the write is a true compare-and-set on the version column.

string

string

string = ...

number = ...

Optimistic-concurrency guard (WP2 T4). When set, the update fails with StmVersionConflictError unless it matches the stored version. Optional so legacy callers keep last-write-wins.

Record<string, unknown> = metadataSchema

string[] = tagsSchema

number = ttlSchema

string

string

Promise<StmMemory>