Sessions
One-line summary
The session system manages conversation persistence as append-only JSONL transcript files, with a layered store for metadata, maintenance, and multi-agent coordination.
Responsibilities
- Persist conversation transcripts as append-only JSONL files (
~/.openclaw/agents/<id>/sessions/<sessionId>.jsonl) - Maintain a session store with metadata, model overrides, delivery context, and labels
- Perform automated maintenance: prune stale entries, cap entry count, rotate large files, enforce disk budgets
- Parse and classify session keys (direct/group/channel, cron, subagent, ACP, threaded)
- Track input provenance (external user vs inter-session vs internal system)
Architecture diagram
Key source files
Data flow
Inbound (writing)
Outbound (reading)
Maintenance (background)
Token optimization impact
The session system is the single largest token consumer (60-80% of input tokens):
Key insight for optimization
The JSONL transcript format means every past tool result is re-sent on every LLM call until compaction. A single web_fetch or read tool result of 5,000 tokens stays in history for all subsequent calls. With 10 such tool calls, that's 50,000 tokens of stale tool results per call.
Session store maintenance vs token optimization
The store maintenance (pruning, capping, rotating) operates on session metadata, not transcript content. It controls how many sessions exist on disk, not how large each session's transcript grows. Transcript size is controlled by the compaction system in src/agents/compaction.ts.
How it connects to other modules
-
Depends on:
@mariozechner/pi-coding-agent—SessionManager,CURRENT_SESSION_VERSIONconfig/— maintenance settings, pathsagents/session-write-lock.ts— file locking for concurrent accesschannels/chat-type.ts— chat type normalizationrouting/session-key.ts— key normalization
-
Depended by:
agents/pi-embedded-runner/— loads transcript for LLM callsauto-reply/— session entry creation, model/level overridesgateway/— session listing API for Control UIcron/— session targeting for scheduled jobsmemory/— session-aware memory indexing
Session key format
Detection helpers:
isCronRunSessionKey()— matchescron:run:patternisSubagentSessionKey()— matchessubagent:or checks depthisAcpSessionKey()— matches ACP protocol sessionsgetSubagentDepth()— returns nesting level
My blind spots
- Exact JSONL format and how Pi SDK reads/writes transcript lines
-
disk-budget.tsenforcement strategy — how it decides which sessions to evict - File locking implementation in
agents/session-write-lock.ts— race conditions? - How
rotateSessionFile()works — does it archive or truncate? - Whether Gateway API supports pagination for session listing (relevant for UI performance)
-
session-utils.fs.ts— filesystem operations and archive utilities
Related contributions
- None yet
Change frequency
store.ts: High — maintenance logic, normalization, and concurrent access handling evolve frequentlysession-utils.ts: High — gateway API features (filtering, multi-agent, display) expand with UI needssession-key-utils.ts: Medium — new session types added as features ship (ACP, threaded)transcript.ts: Low — append-only logic is stablesend-policy.ts: Low — policy rules are rarely modified