Skip to main content

Purpose

Enable the agent to restore state across requests, process restarts, and multi-user scenarios. Session persistence is owned by the agent runtime itself: conversation progress is appended to a native session journal — a lossless, hash-chained sequence of structured events. The journal is never compacted or rewritten; compaction only shrinks the in-context window.
  • Native session journal — the complete history: messages, tool calls with full inputs/outputs, and reasoning events, stored as atomic commits
  • Read-time projections — agent.sessionTranscript(ctx) and the session_search / session_list tools project the journal into flat views on demand; no duplicate conversation files are written

Trigger Points

Key Logic

Storage Layout

  • Journal: JournalSessionLog writes ordered, hash-chained commits of structured SessionEvents with full payloads, so tool inputs/outputs and reasoning content are preserved verbatim. Compaction summaries are ordinary events; earlier events are never dropped.
  • Backends: the default LocalFilesystem stores journal objects under the workspace’s .agentscope-runtime/ directory; a SQLite backend is used on Windows, and larger deployments can plug SessionLogStore / AtomicSessionStorage implementations (e.g. a remote store).
  • Reading: agent.sessionTranscript(ctx) projects committed events into a flat Msg view; session_search / session_list query the same journal.

How RuntimeContext Routes the Journal

RuntimeContext decides which journal a call reads and writes:
  1. Key derivation: (agentId, userId, sessionId) maps to the journal directory agents/<agentId>/sessions/<userId>/<sessionId>/.
  2. Isolation: different userId / sessionId values hit different journal directories; there is no shared mutable state between them.
  3. Restore: committed events are replayed for that identity before the turn runs, so state survives restarts.
  4. Reading from application code: agent.sessionTranscript(ctx) (flat view) or agent.sessionLog(ctx) (raw journal) never starts inference.

Default vs Custom Storage

Multi-User Isolation

  • Journal level: userId + sessionId determine the journal directory; sessions of different users live in disjoint namespaces.
  • File level: userId + NamespaceFactory determines the file operation path prefix (the default LocalFilesystemWithShell reads the per-call identity).

Listing Sessions

session_list / session_search read the journal store directly — there is no separate index file to keep in sync. The store discovers sessions by scanning agents/<agentId>/sessions/ for session headers, so anything committed through the runtime is automatically visible to both tools.
  • Tool — session_search / session_list / session_history parameters
  • Memory — how memory flush interacts with compaction and feeds memory_search
  • Filesystem — userIdRef + NamespaceFactory multi-tenant path isolation
  • Session log — journal internals: backends, physical layout, custom stores and exports