org.llm4s.agent.harness.memory

Members list

Type members

Classlikes

@Experimental
final case class MemoryConsolidated(groups: Int, removed: Int, evicted: Int)

groups groups were consolidated: removed memories replaced by one summary each, evicted evicted.

groups groups were consolidated: removed memories replaced by one summary each, evicted evicted.

Attributes

Source
MemoryEvents.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
@Experimental
final case class MemoryConsolidation

When SemanticMemoryMiddleware consolidates: after a turn, at most once per interval for each tenant (in this process), the manager's consolidateMemories over memories older than olderThan, in groups of at least minCount. The interval keeps a turn from reading the tenant's old memories every time (#3040).

When SemanticMemoryMiddleware consolidates: after a turn, at most once per interval for each tenant (in this process), the manager's consolidateMemories over memories older than olderThan, in groups of at least minCount. The interval keeps a turn from reading the tenant's old memories every time (#3040).

Attributes

Companion
object
Source
SemanticMemoryMiddleware.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
@Experimental

Attributes

Companion
class
Source
SemanticMemoryMiddleware.scala
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
@Experimental
object MemoryEvents

The durable events of SemanticMemoryMiddleware, each an org.llm4s.agent.graph.EventType to match a StreamEvent with. They carry counts and names only, never memory content or an error message (rule 9).

The durable events of SemanticMemoryMiddleware, each an org.llm4s.agent.graph.EventType to match a StreamEvent with. They carry counts and names only, never memory content or an error message (rule 9).

Attributes

Source
MemoryEvents.scala
Supertypes
class Object
trait Matchable
class Any
Self type
@Experimental
final case class MemoryRecall

What SemanticMemoryMiddleware recalls before a turn: at most maxRecallTokens (estimated at four characters a token, as the agent's prompt-section budget is), of the memories filter accepts, and scope accepts for the run.

What SemanticMemoryMiddleware recalls before a turn: at most maxRecallTokens (estimated at four characters a token, as the agent's prompt-section budget is), of the memories filter accepts, and scope accepts for the run.

excludeCurrentThread (the default) leaves out what the run's own thread recorded, which is already in its message history (#3040). scope computes a filter from the run; MemoryRecall.byPrincipal recalls only what runs of the same principal recorded.

Attributes

Companion
object
Source
SemanticMemoryMiddleware.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
@Experimental
object MemoryRecall

Attributes

Companion
class
Source
SemanticMemoryMiddleware.scala
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
@Experimental
final case class MemoryRecorded(added: Int, evicted: Int)

The turn's recording stored added memories, and evicted evicted to keep the store within its bounds.

The turn's recording stored added memories, and evicted evicted to keep the store within its bounds.

Attributes

Source
MemoryEvents.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
@Experimental
final case class MemoryRecording

How SemanticMemoryMiddleware records a turn: each memory's content cut to maxRecordChars code points.

How SemanticMemoryMiddleware records a turn: each memory's content cut to maxRecordChars code points.

Attributes

Companion
object
Source
SemanticMemoryMiddleware.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
@Experimental

Attributes

Companion
class
Source
SemanticMemoryMiddleware.scala
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
@Experimental
final case class MemorySkipped(operation: String, error: String)

operation (memory.recall, memory.record, memory.consolidate or memory.resolve) failed with an error of type error (its class's simple name), and was skipped.

operation (memory.recall, memory.record, memory.consolidate or memory.resolve) failed with an error of type error (its class's simple name), and was skipped.

Attributes

Source
MemoryEvents.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
@Experimental
final class SemanticMemoryMiddleware(stores: Option[TenantId] => Result[MemoryStore], manager: (MemoryStore, LLMClient) => MemoryManager, client: LLMClient, val recall: Option[MemoryRecall], val record: Option[MemoryRecording], val consolidate: Option[MemoryConsolidation], val bounds: MemoryBounds, clock: Clock, val id: MiddlewareId, val maxTenants: Int, release: MemoryStore => Unit) extends AgentMiddleware, AutoCloseable

Long-term semantic memory for an agent (design §4.31): before each turn it recalls what earlier threads of the same tenant recorded and shows it to the model; after the turn it records the exchange, and, when consolidate is set, replaces groups of old memories with a summary.

Long-term semantic memory for an agent (design §4.31): before each turn it recalls what earlier threads of the same tenant recorded and shows it to the model; after the turn it records the exchange, and, when consolidate is set, replaces groups of old memories with a summary.

  • Tenant scope. stores is asked once per tenant (RunContext.config.tenantId) for that tenant's store; the store is held in one cell per tenant, whose writes are serialised. Every thread of a tenant shares it; no two tenants may share a store (refused): not one instance, nor two instances with one storageIdentity - one SQLite file, however its path is spelled, or one Postgres table and tenant (#3032). A store that names no identity is told apart by instance only, so a resolver that opens one shared backend twice must scope it to the tenant itself. A failed resolution is asked again on the next turn.
  • The manager is built for each hook call by manager(view, context.accounted(client)), over a view of the tenant's store whose writes are the middleware's effects, so every model call it makes is counted in the thread's usage.
  • Recall (beforeAgent) runs getRelevantContext on the turn's input, cuts it to maxRecallTokens * 4 characters and stores it in SemanticMemoryMiddleware.RecalledKey; every model step of the turn shows it as the prompt section semantic-memory. A resolution or configuration failure refuses the input before any model call; a failed search is skipped, announced by MemoryEvents.Skipped.
  • Recalled text is data. Memories are earlier users' input and model answers, shared by every thread of the tenant, so the section fences them as llm4s-prompt's Untrusted text under a heading that says they are never instructions (#3033): escaped, so no memory can close the fence, and, from llm4s-memory's managers, one line per memory, so none can forge a heading. Fencing reduces prompt injection; it does not prevent it.
  • Recording (afterAgent) records the turn's query and answer through the manager as one replayable effect (memory.record, C4): on a org.llm4s.agent.memory.TransactionalMemoryStore a replay after a crash returns the recorded outcome and never writes twice; on any other store a replay of a write that may have run is refused. Each memory is cut to maxRecordChars.
  • Consolidation (afterAgent, after recording) runs the manager's consolidateMemories on memories older than olderThan; each group it replaces is one effect (memory.consolidate, keyed by the group's ids). It needs a transactional store: on any other, the input is refused. A summary carries its sources' writer, and one longer than maxRecordChars is refused, never cut, so its sources are kept (#3035).
  • Bounds. Every memory the middleware writes carries SemanticMemoryMiddleware.WriterKey set to its id, and bounds counts only those: every write evicts, in its own transaction, the middleware's own memories of lowest importance, oldest first, beyond bounds, so what it records stays bounded whether or not consolidation is on. A memory it did not write - knowledge an application loaded, facts it stored - is never counted or evicted (#3034).
  • Failures after the answer are skipped and announced by MemoryEvents.Skipped: the answer is never retracted because memory could not be written. A crash is replayed by recover from C4's records.

It runs outside guardrail middleware, so that a blocked answer is never recorded: afterAgent runs in reverse stack order, and a guardrail's block stops the others. In one agent's stack that is arranged, wherever the two were registered (runsBefore: every GuardrailMiddleware and GuardrailReviewMiddleware, if there is one). Across a handoff family it cannot be arranged, so build() refuses it on a handoff target when the root agent has one (placement), since the root's afterAgent runs after every agent's own. An effect that applied stays applied: a Block restores thread state, not the store.

The key is registered only by an agent family with a SemanticMemoryMiddleware: adding one to an existing agent changes its graph's structure.

Value parameters

bounds

the most of the middleware's own memories each tenant's store keeps; any owner it has is replaced by the middleware's (SemanticMemoryMiddleware.WriterKey, id)

client

the client the manager calls a model with; every call is accounted to the run

clock

where consolidation reads "now" from

consolidate

when to consolidate old memories after a turn; None never

id

this middleware's id

manager

builds the manager over a store and a client, e.g. LLMMemoryManager(config, _, _)

maxTenants

how many tenants' stores are held open at once (#3040): past it, the least recently used one that no turn is using is released (release) and resolved again on its tenant's next turn. Only a durable store (a SQLite file, a Postgres table) is evicted; one whose memories die with it, such as an InMemoryStore or an in-memory SQLite database, is kept

recall

what to recall before a turn; None recalls nothing

record

how to record a turn; None records nothing

release

what releasing a tenant's store does, when it is evicted or the middleware is closed; by default it closes a store that is AutoCloseable (the SQLite and Postgres stores), which the resolver made for it Resources: close releases every tenant's store, and refuses later turns.

stores

the store of each tenant (None for runs without one): a store scoped to that tenant

Attributes

Companion
object
Source
SemanticMemoryMiddleware.scala
Supertypes
class Object
trait Matchable
class Any
@Experimental

Attributes

Companion
class
Source
SemanticMemoryMiddleware.scala
Supertypes
class Object
trait Matchable
class Any
Self type