Purpose
Enable the agent to restore state across requests, process restarts, and multi-user scenarios. After eachcall() ends, two outputs are automatically persisted on parallel tracks:
- StateModule snapshot (
Memory,ToolExecutionContext, and other serializable state) — defaults toWorkspaceSession - Conversation JSONL (LLM context + full history) — goes through
SessionTree, triggered byMemoryFlushManager.offloadMessages
Trigger Points
Key Logic
Dual-Track Storage Layout
context/:WorkspaceSessionextendsJsonSession, base atagents/<agentId>/context/; eachStateModuleis stored perSessionKey → {key}.jsonin the sessionId subdirectory.sessions/:SessionTreeorganizes a JSONL file as aid/parentIdtree; the paired<sessionId>.log.jsonlis never compacted, used for auditing andsession_search.
How RuntimeContext Aligns the Two Tracks
HarnessAgent.bindRuntimeContext does several things:
- Fill defaults: if
sessionis null, use thedefaultSessionfrom build time (defaults toWorkspaceSession(workspace, agentId)); ifsessionKeyis null, trySimpleSessionKey.of(sessionId)→SimpleSessionKey.of(agentName)in order. - Distribute to hooks:
workspaceContextHook,memoryFlushHook,sessionPersistenceHook,compactionHookall sync to this ctx — they can readsessionIdduring offload / saveTo. - Link
userIdRef: anAtomicReference<String>is updated touserId; the defaultNamespaceFactory → List.of(userId)uses this as a path prefix, enabling transparent multi-tenant isolation. - Pre-load state: if both
sessionandsessionKeyare present, callsdelegate.loadIfExiststo overwrite current Memory. Does nothing if not found.
Default vs Custom Session
Multi-User Isolation at Two Levels
- Session level:
sessionIddetermines thatcontext/<sessionId>/andsessions/<sessionId>.jsonlare independent. - File level:
userId+NamespaceFactorydetermines the file operation path prefix (the defaultLocalFilesystemWithShellreadsuserIdRef).
Session Index
AfterMemoryFlushManager.offloadMessages completes, WorkspaceManager.updateSessionIndex(agentId, sessionId, summary) merges a write into sessions/sessions.json. In another turn, the agent can use the session_list tool to see “what conversations this agent has had historically”.
Related Pages
- Tool —
session_search/session_list/session_historyparameters - Memory — when
offloadMessagesis called, and how it feeds back intomemory_search - Filesystem —
userIdRef+NamespaceFactorymulti-tenant path isolation - Architecture —
SessionPersistenceHookposition inPostCallEvent/ErrorEvent