Skip to main content

Module memory_layer

Module memory_layer 

Source
Expand description

Long-term-memory wiring for crate::AgentLoop.

Two pieces, designed to be installed together:

  • MemoryGuide — at session start, calls Memory::recall with the current task description and pushes the top-K matches into ctx.guides as plain text. The model sees a “Relevant prior context” section in its system prompt before the very first model call.

  • MemoryWriter — captures every assistant text turn (via PostModel) and persists the last one as a MemoryEntry when the run finishes (TaskCompleted). This turns “this conversation produced an answer” into “future sessions can recall the answer”.

Both share an Arc<dyn Memory> so a single backend serves recall + write. The trait is async; the writer hook uses tokio::spawn to commit without blocking the loop.

§Wiring

let mem: Arc<dyn Memory> = Arc::new(FileMemory::open("~/.harness/mem.jsonl")?);
let loop_ = AgentLoop::new(model)
    .with_guide(Arc::new(MemoryGuide::new(mem.clone()).with_top_k(5)))
    .with_hook(Arc::new(MemoryWriter::new(mem)));

Structs§

MemoryGuide
Guide that recalls relevant prior memories and injects them into ctx.guides as a Block::Text for the model to see.
MemorySynthesizer
Smarter alternative to MemoryWriter — distil the session’s assistant turns into 1..=max_facts atomic durable facts using a cheap “synthesizer” model, instead of persisting the verbatim final answer.
MemoryWriter
Hook that writes the final assistant text of every successful run back into long-term memory.