Files
can1357 de21c0ac45 feat(tools)!: replaced xd:// devices with bash tool commands
- Mounted discoverable tools as bare bash commands with schema-derived --help, recorded on the bash result as details.toolCommands.
- Turned resolve, reject, propose and report_issue into tools that run as commands, or top-level without bash.
- Moved tool doc topics to help:// and renamed the tools.xdev* settings to tools.command*.
- Saved decoded terminal images to temp files and linked them in bash output.
2026-11-21 08:25:12 +01:00

58 KiB

Session Storage and Entry Model

This document is the source of truth for how coding-agent sessions are represented, persisted, migrated, and reconstructed at runtime.

Scope

Covers:

  • Session JSONL format and versioning
  • Entry taxonomy and tree semantics (id/parentId + leaf pointer)
  • Migration/compatibility behavior when loading old or malformed files
  • Context reconstruction (buildSessionContext)
  • Persistence guarantees, failure behavior, truncation/blob externalization
  • Storage abstractions (FileSessionStorage, MemorySessionStorage) and related utilities

Does not cover /tree UI rendering behavior beyond semantics that affect session data.

Implementation Files

On-Disk Layout

Default file-session location:

~/.omp/agent/sessions/<encoded-cwd>/<timestamp>_<sessionId>.jsonl

<encoded-cwd> is derived from the canonicalized cwd (so symlink aliases share a bucket): -<relative> for directories under home, -tmp-<relative> for directories under the temp root, and --<encoded-absolute>-- for anything else, with path separators replaced by -.

On access, buckets written by the short-lived hashed scheme (<scope>-<project-basename>-<sha256(canonical-cwd)>, used in 17.2.5-17.2.8 and reverted in 17.2.9 by #7397) are migrated back into the path-encoded names best-effort, along with older --<home-encoded>-*-- spellings of home-relative buckets.

Blob store location:

~/.omp/agent/blobs/<sha256>

Terminal breadcrumb files are written under:

~/.omp/agent/terminal-sessions/<terminal-id>

Breadcrumb content begins with cwd and session file path. Optional extra lines are fresh and cwdstat <device> <inode>. A fresh breadcrumb preserves an initially created, lazy session whose JSONL file does not exist yet, preventing continueRecent() from reopening the previous session. Explicit newSession() boundaries materialize their header before returning, except the lazy successor created by drop (/delete). The directory identity permits automatic re-rooting after a same-filesystem project rename; a missing cwd alone is not move evidence. Writes are synchronous, ordered, and best-effort.

File Format

Session files are JSONL: one JSON object per line. Current files physically begin with a fixed-width, 256-byte type: "title" slot (including its newline), followed by the session header and then SessionEntry values. Legacy files may begin directly with the header. Loaders strip the physical slot and fold its current title/source into the logical header.

  • The logical first entry is always the session header (type: "session").
  • Remaining logical entries are SessionEntry values.
  • Ordinary appends extend the tree; branch navigation moves an in-memory pointer (leafId). Targeted rewrite/discard helpers can change existing records.

Header (SessionHeader)

{
	"type": "session",
	"version": 3,
	"id": "019c625b-b900-7000-8000-000000000001",
	"timestamp": "2026-02-16T10:20:30.000Z",
	"cwd": "/work/pi",
	"title": "optional session title",
	"titleSource": "auto",
	"additionalDirectories": ["/work/shared"],
	"previousSessionFiles": ["/old/location/session.jsonl"],
	"providerPromptCacheKey": "optional inherited cache identity",
	"taskPromptCacheKey": "optional local-only Task sibling family key",
	"parentSession": "optional lineage marker"
}

Notes:

  • New session ids are UUIDv7 strings; readers also accept older string ids.

  • additionalDirectories records normalized, deduplicated workspace roots beyond cwd.

  • previousSessionFiles records prior absolute locations after successful moves.

  • providerPromptCacheKey carries an inherited provider prompt-cache identity for eligible full forks.

  • taskPromptCacheKey is a local-only Task sibling family key (<parent cache key>:<agent name>), omitted from shares and HTML exports.

    • The executor derives it once the child session exists, regardless of the child's initial model. An empty parent cache lineage derives nothing, so those children keep their own session identity.
    • Until a request uses it, the key lives in a session-scoped, non-persisted stash. The first primary request on a model that opts into shared-prompt-cache writes it to the header, so fork and reopen keep it.
    • Each request decides consumption against its own model: an explicit session key wins; otherwise the family key applies only on opted-in models.
    • The stash does not survive a fork, move, branch, new-session reset, or switch to a different session; a rolled-back move or switch restores it.
  • parentSession is an opaque lineage string. Current code writes either a session id or a session path depending on flow (fork, forkFrom, createBranchedSession, or explicit newSession({ parentSession })). Treat it as metadata, not a typed foreign key.

  • titleSource is auto or user; automatic renames cannot overwrite a user title.

Entry Base (SessionEntryBase)

All non-header entries include:

{
	"type": "...",
	"id": "8-char-id",
	"parentId": "previous-or-branch-parent",
	"timestamp": "2026-02-16T10:20:30.000Z"
}

parentId can be null for a root entry (first append, or after resetLeaf()). Generated entry ids are normally eight hexadecimal characters, with a full Snowflake id as the collision-exhaustion fallback; consumers must not assume a fixed width.

Entry Taxonomy

SessionEntry is the union of:

  • message
  • model_usage
  • thinking_level_change
  • model_change
  • service_tier_change
  • compaction
  • branch_summary
  • reset_boundary
  • custom
  • custom_message
  • label
  • title_change
  • ttsr_injection
  • credential_pin
  • session_init
  • mode_change

message

Stores an AgentMessage directly.

{
	"type": "message",
	"id": "a1b2c3d4",
	"parentId": null,
	"timestamp": "2026-02-16T10:21:00.000Z",
	"message": {
		"role": "assistant",
		"api": "anthropic-messages",
		"provider": "anthropic",
		"model": "claude-sonnet-4-5",
		"content": [{ "type": "text", "text": "Done." }],
		"usage": {
			"input": 100,
			"output": 20,
			"cacheRead": 0,
			"cacheWrite": 0,
			"totalTokens": 120,
			"cost": {
				"input": 0,
				"output": 0,
				"cacheRead": 0,
				"cacheWrite": 0,
				"total": 0
			}
		},
		"stopReason": "stop",
		"timestamp": 1760000000000
	}
}

The persisted message.role discriminant is camelCase, not the snake_case used by the LLM wire format or extension hook names. Ordinary conversation records use these roles under type: "message":

Persisted message.role Owner package Notes
user pi-ai User/tool-feedback turn.
developer pi-ai Developer-role instruction turn.
assistant pi-ai Model turn; tool calls live in its content as { "type": "toolCall" } blocks.
toolResult pi-ai Result of one tool call — not tool_result. Carries toolCallId/toolName.
bashExecution pi-tui Standalone !-bash run.
pythonExecution pi-tui Standalone Python run.
hookMessage pi-tui Legacy hook-injected message, retained for migration; new code uses custom.
fileMention pi-tui Inlined @file mention contents.

Branch and compaction summary roles are synthesized from dedicated top-level entries during session-context reconstruction. Extension messages sent through pi.sendMessage likewise persist as custom_message entries and reconstruct as custom:

Persisted entry type Reconstructed role
branch_summary branchSummary
compaction compactionSummary
custom_message custom

Internal callers can append a custom message directly, so readers must discriminate on entry.type rather than infer the persisted shape from the reconstructed role.

toolCall is a content-block type inside an assistant message's content array, not a message role. An extension keying off message.role that matches snake_case constants (tool_result, tool_call) or lowercases the role before comparing will silently skip toolResult (and every other camelCase role) — no error is raised. Match the camelCase values above verbatim. The base roles are Message in packages/ai/src/types.ts; the rest are merged into CustomAgentMessages (packages/agent/src/compaction/messages.ts, packages/tui/src/chat/messages.ts). Coding-agent re-exports the custom types and converts them for provider context in packages/coding-agent/src/session/messages.ts.

model_usage

Records model calls outside the conversation transcript. Fields are purpose, optional model role, api, provider, model, usage, stopReason, and optional errorMessage. These records contribute usage accounting, not model messages or transcript turns. appendModelUsage() attaches the record to the initiating session/branch and rejects stale session ownership.

Optional fields describe the call without copying the static prompt, which is identified by purpose.

  • duration / ttft: request wall time and time to first token, in ms.
  • judgment: a native judge query (TypeSafe jev and other judgment APIs). state is the sha256 of the canonical state, which is states.id in the judgment answer cache (~/.omp/cache/judgment-cache.db): that database holds the state text once and, under oracle(state, model, name), each question's rendered instructions, criteria, and full answer. answers keeps the headline of each answer the request produced, by question id: a noul's probability of yes, [label, confidence] for a choice, [score, confidence] for a score, [top, bottom, left, right, confidence] for a box. cached counts questions the cache answered instead (not billed, not in answers). A judgment answered wholly from the cache writes no entry; a failed request has empty answers.
  • completion: a text side channel (title generation, recaps, commit messages, memory extraction, …). prompt is the dynamic user-turn text the model was sent; it is absent when the request read the conversation at the entry's parentId (title-fork, recap, btw). reply is the raw reply before parsing, or the tool arguments as JSON when the call answered through a forced tool call. Either side is left out, never truncated, when it exceeds 4,000 characters (a diff, a transcript). Calls whose result the session already stores (compaction:<method>, branch-summary, web-search, image-gen, image-describe) journal usage and timing only. Compaction purposes name the method that made the request: compaction:remote (provider-native compaction, including OpenAI /responses/compact and V2 streaming), compaction:soft (local summary requests), compaction:handoff (the handoff document); an advisor compacting its own history journals compaction:advisor.

An on-device model call that produced output is journaled with zero usage, so its count and duration are still visible.

{
	"type": "model_usage",
	"id": "4e1e5bcf",
	"parentId": "40ec204f",
	"timestamp": "2026-11-18T10:16:19.964Z",
	"purpose": "find",
	"role": "typesafe",
	"api": "typesafe",
	"provider": "typesafe",
	"model": "jev-1.14.0",
	"usage": { "input": 824, "output": 87, "cacheRead": 0, "cacheWrite": 0, "totalTokens": 911, "cost": { "input": 0.0000346, "output": 0, "cacheRead": 0, "cacheWrite": 0, "total": 0.0000346 } },
	"stopReason": "stop",
	"duration": 212,
	"judgment": {
		"state": "9f2c1a7b3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8",
		"answers": { "p00": 0.46, "p01": 0.02, "kind": ["helper", 0.41] },
		"cached": 1
	}
}
{
	"type": "model_usage",
	"purpose": "title",
	"duration": 640,
	"ttft": 410,
	"completion": {
		"prompt": "<user>\nfix the share bar\n</user>",
		"reply": "<title>Fix stats share bar</title>"
	}
}

model_change

{
	"type": "model_change",
	"id": "b1c2d3e4",
	"parentId": "a1b2c3d4",
	"timestamp": "2026-02-16T10:21:30.000Z",
	"model": "openai/gpt-4o",
	"role": "default"
}

role is optional; missing is treated as default in context reconstruction. resolvedModelIsFallback optionally marks a retry-fallback transition. The reserved role fallback is ephemeral: model restoration prefers the configured default instead of restoring that role's temporary model.

service_tier_change

{
	"type": "service_tier_change",
	"id": "c1d2e3f4",
	"parentId": "b1c2d3e4",
	"timestamp": "2026-02-16T10:21:45.000Z",
	"serviceTier": { "openai": "priority", "google": "flex" }
}

serviceTier is a per-family map keyed by openai/anthropic/google (each value auto/default/flex/scale/priority/ultrafast), or null when no tier is active. Legacy entries that stored a single string ("flex", "openai-only", "claude-only", …) are coerced to this map during context reconstruction; loading alone does not rewrite their payload.

thinking_level_change

{
	"type": "thinking_level_change",
	"id": "c1d2e3f4",
	"parentId": "b1c2d3e4",
	"timestamp": "2026-02-16T10:22:00.000Z",
	"thinkingLevel": "high"
}

configured may additionally preserve the selector the user chose ("auto" or a concrete level). Readers of older entries fall back to thinkingLevel. Both fields can be null; a null/missing resolved level reconstructs as "off".

compaction

{
	"type": "compaction",
	"id": "d1e2f3a4",
	"parentId": "c1d2e3f4",
	"timestamp": "2026-02-16T10:23:00.000Z",
	"summary": "Conversation summary",
	"shortSummary": "Short recap",
	"firstKeptEntryId": "a1b2c3d4",
	"tokensBefore": 42000,
	"details": { "readFiles": ["src/a.ts"] },
	"preserveData": { "hookState": true },
	"fromExtension": false
}

Optional compaction metadata includes tokensAfter, method, providerReplayThroughEntryId, and warning. preserveData can carry native OpenAI/Anthropic replay state or a snapcompact archive; it is not just extension state. providerReplayThroughEntryId marks the last entry represented by native replacement history so later entries still replay normally.

branch_summary

{
	"type": "branch_summary",
	"id": "e1f2a3b4",
	"parentId": "a1b2c3d4",
	"timestamp": "2026-02-16T10:24:00.000Z",
	"fromId": "a1b2c3d4",
	"summary": "Summary of abandoned path",
	"details": { "note": "optional" },
	"fromExtension": true
}

If branching from root (branchFromId === null), fromId is the literal string "root".

reset_boundary

A payload-free marker appended by /clear. The collapsed live transcript and rebuilt model context begin after the latest applicable boundary; full-history transcript export still retains entries before it.

custom

Opaque, non-LLM records owned by core subsystems or extensions. buildSessionContext does not directly turn them into model messages, but subsystem-specific replay code can consume customType values to restore runtime state or diagnose an interrupted turn.

{
	"type": "custom",
	"id": "f1a2b3c4",
	"parentId": "e1f2a3b4",
	"timestamp": "2026-02-16T10:25:00.000Z",
	"customType": "com.example.my-extension.state",
	"data": { "state": 1 }
}

Current core-owned values include:

customType data schema Writer and consumer
tool_execution_start { toolCallId: string, toolName: string, startedAt: string, args?: { command?: string, path?: string }, intent?: string } AgentSession writes a marker immediately before a tool implementation starts. Exit diagnostics combine it with assistant tool calls and tool results to reconstruct calls left pending. Argument summaries are truncated projections; older full argument objects are accepted on read.
session_exit { reason: string, kind: "normal" | "signal" | "fatal" | "process_exit", recordedAt: string, pendingToolCalls?: Array<{ toolCallId?: string, toolName: string, args?: unknown, intent?: string, assistantTimestamp?: number, startedAt?: string }> } Normal disposal and postmortem teardown record the exit when the session has assistant history or pending tool calls. The writer immediately calls flushSync() so a subsequent process can inspect the last durable turn; a flush failure is logged. Resume diagnostics consume the latest valid record.
tool_approval_requested { toolCallId: string, parentToolCallId?: string, toolName: string, approvalMode: string, reason?: string, requestedAt: string } The extension tool wrapper writes it to disk before the approval prompt opens, whether or not an extension listens to the matching event, creating the session file if there is no assistant message yet. The call is still waiting until a tool_approval_resolved or a tool result for its toolCallId follows, or, when an eval cell or kernel-defined tool dispatched it, a tool result for its parentToolCallId (fork and resume repair close an interrupted assistant call with a tool result, but never the nested calls an eval made), so another process reading the file can tell a session blocked on the user from one still working.
tool_approval_resolved { toolCallId: string, toolName: string, approved: boolean, reason?: string, resolvedAt: string } Ends that wait: approved, denied (reason: "denied by user"), aborted, or refused because no interactive UI is available.
user_todo_edit { phases: TodoPhase[] } SDK/UI todo editing persists the complete phase snapshot. Todo restoration scans backward for the latest snapshot (or a successful todo tool result) and restores its phases.
vibe-session-lifecycle Version-1 event with { version: 1, id, ownerId, parentSessionId, action, ... }; spawn adds cli, agent, childSessionFile, and createdAt; turn events add turn; tombstone events add reason. Vibe runtime persists and replays child spawn, turn-started/settled, tombstone, and tombstone-revoked transitions to recover owned child sessions and in-flight state. Invalid or out-of-scope events are ignored.
autoresearch-control { mode: "on" | "off" | "clear", goal?: string } The built-in autoresearch command writes mode/goal changes, and experiment-limit shutdown writes mode: "off". reconstructControlState() replays valid records on resume to restore whether autoresearch is active and its goal; clear removes the goal. See Autoresearch.
system-prompt-digest Hexadecimal string: a hash of the base system prompt blocks (before a per-turn hook override) AgentSession appends it just before a primary assistant reply with provider output whose prompt differs from the branch's latest record, and carries it onto a rewind branch with the reparented sibling reply. Before a resumed session's first model call, a prefix-bound tool-list rebuild commits only if it reproduces that prompt; without a record it freezes. Memory recall injected into the base prompt is part of the hash, so such sessions stay frozen.

On resume, a valid latest session_exit after a non-terminal conversation tail causes AgentSession/SDK initialization to append a synthetic assistant message with stopReason: "aborted" and rebuild the display/agent context. A normal exit only triggers that transition when it recorded pending tool calls; abnormal exit kinds can trigger it without that list. This prevents the restored transcript from presenting an interrupted turn as still live.

The strings in the table are reserved for their core consumers. Extensions MUST NOT use them. Use a namespaced identifier such as a reverse-domain or package-qualified name for extension records; a collision can cause core replay logic to interpret extension data as lifecycle state. Unknown namespaced values remain opaque to core session-context reconstruction.

custom_message

Extension-provided message that participates in LLM context unless excludeFromContext is true (then it is only drawn and saved, like a !! bash row). content can be a string or text/image content blocks, and attribution records whether the user or agent initiated it.

{
	"type": "custom_message",
	"id": "a2b3c4d5",
	"parentId": "f1a2b3c4",
	"timestamp": "2026-02-16T10:26:00.000Z",
	"customType": "my-extension",
	"content": "Injected context",
	"display": true,
	"details": { "debug": false },
	"attribution": "agent"
}

label

{
	"type": "label",
	"id": "b2c3d4e5",
	"parentId": "a2b3c4d5",
	"timestamp": "2026-02-16T10:27:00.000Z",
	"targetId": "a1b2c3d4",
	"label": "checkpoint"
}

label: undefined clears a label for targetId.

title_change

Append-only audit entry for a session rename. It records title, source (auto or user), and optionally previousTitle and trigger. The current title is also updated in the fixed-width title slot so listing does not require a full-file rewrite.

/rename <title> sets an explicit title. /rename without a title generates one from recent conversation using the title-generation candidates (tiny, commit, smol, then eligible current-model fallbacks), keeping the current title's card (<icon> <CODE>: ) if it has one. A title still without a card, typed or generated, gets one from the same candidates (src/prompts/system/title-card.md) unless title.icons is boring; a failed card leaves the title plain, and an interrupt drops only the card from a typed title. Both are user-requested renames (source: "user"), so later automatic titling cannot replace them. Empty conversation or failed generation leaves the current title unchanged. A session switch or newer rename while generation runs discards the stale result. Local tiny-model failures never fall back to an online provider.

ttsr_injection

{
	"type": "ttsr_injection",
	"id": "c2d3e4f5",
	"parentId": "b2c3d4e5",
	"timestamp": "2026-02-16T10:28:00.000Z",
	"injectedRules": ["ruleA", "ruleB"]
}

credential_pin

Records the provider and a pseudonymous SHA-256 account/scope hash used to re-pin resumed OAuth traffic to the serving account and preserve account-scoped prompt-cache reuse. It does not store the raw account identity; exported hashes remain linkable and are not anonymous.

session_init

{
	"type": "session_init",
	"id": "d2e3f4a5",
	"parentId": "c2d3e4f5",
	"timestamp": "2026-02-16T10:29:00.000Z",
	"systemPrompt": ["...", "..."],
	"task": "...",
	"tools": ["read", "edit"],
	"outputSchema": { "type": "object" },
	"outputSchemaMode": "strict",
	"restrictToolNames": true,
	"spawns": "*",
	"readSummarize": false
}

The latest session_init is also the cold-subagent revival contract. Optional fields include agent, modelRole, resolvedModel, perCallModel (boolean), perCallModelIdentity, perCallThinkingLevel, retryFallback, readOnly, advisor, and compactionThreshold (thresholdPercent/thresholdTokens). For perCallModel: true, cold revival requires both perCallModelIdentity (the exact provider/model-id[@upstream], without an effort suffix) and perCallThinkingLevel (the configured effort selector, including auto, or null for unset). Legacy per-call records missing either field refuse revival rather than guessing from the historical-display resolvedModel. These fields are preserved in anonymized exports under the same flag, model-identity and effort rules as other session metadata. isolated: true marks an isolation-worktree child that cannot be cold-revived. mountedTools names the entries of tools presented as tool commands rather than top-level tools; cold revival mounts them again, so the revived session sends the same tool list and tool inventory. Files without it revive every name in tools top-level. Anonymized exports treat it like tools. systemPrompt holds the base prompt blocks a model call was built from (never a per-turn before_agent_start override), and revival replays them unchanged. A session appends a newer session_init when a model call runs on a different base prompt or work-pool yield items (workPoolYieldItems, restored on revival). Older files store one joined string, which revives as a single block. extractSessionInit() and read-only peekSessionInit() expose this contract.

mode_change

{
	"type": "mode_change",
	"id": "e2f3a4b5",
	"parentId": "d2e3f4a5",
	"timestamp": "2026-02-16T10:30:00.000Z",
	"mode": "plan",
	"data": { "planFilePath": "local://PLAN.md" }
}

Versioning and Migration

Current session version: 3.

v1 -> v2

Applied when header version is missing or < 2:

  • Adds id and parentId to each non-header entry.
  • Reconstructs a linear parent chain using file order.
  • Migrates compaction field firstKeptEntryIndex -> firstKeptEntryId when present.
  • Sets header version = 2.

v2 -> v3

Applied when header version < 3:

  • For message entries: rewrites legacy message.role === "hookMessage" to "custom".
  • Sets header version = 3.

Migration Trigger and Persistence

  • Migrations run during session load (setSessionFile).
  • If any migration ran, the in-memory representation is marked for a full rewrite rather than rewritten immediately.
  • The next persistence operation performs the full rewrite before incremental appends continue.

Load and Compatibility Behavior

loadEntriesFromFile(path) behavior:

  • Missing file (ENOENT) -> returns [], unless throwIfMissing: true requests an error.
  • Current files at least 8 MiB use a streaming JSONL loader; smaller or non-file storage uses a full text read.
  • Malformed records are skipped and counted by the lenient JSONL parser.
  • The optional fixed-width title slot is removed and folded into the header.
  • If the first logical entry is not a valid session header (type !== "session" or missing string id) -> returns [].

SessionManager.setSessionFile() behavior:

  • Missing or genuinely empty files initialize a new session at that exact path and materialize its header immediately. SessionManager.open(..., { throwIfMissing: true }) instead rejects missing/empty input.
  • Non-empty data without a valid leading session header is rejected without modifying the file. An array-only loadEntriesFromFile() result of [] therefore does not distinguish empty from corrupt input; the manager uses loadSessionFile() diagnostics.
  • Valid files are loaded, migrated if needed, blob refs resolved, then indexed. Migrations, skipped malformed records, and loaded OpenAI replay sanitization mark the next persistence operation for a full rewrite.
  • A recorded cwd is adopted only when it is enterable. Otherwise runtime cwd stays at the launch/current directory while the transcript remains in its original location; workspace-root edits stay runtime-only until relocation.

Tree and Leaf Semantics

The underlying model is append-only tree + mutable leaf pointer:

  • Ordinary append methods create one new entry whose parentId is current leafId, then advance the leaf.
  • appendMessageToBranch() appends to an explicit parent without moving the active leaf. appendModelUsage() likewise retains a successor branch's leaf when the initiating parent is no longer active.
  • branch(entryId) moves only leafId; existing entries remain unchanged.
  • resetLeaf() sets leafId = null; next append creates a new root entry (parentId: null).
  • branchWithSummary() sets leaf to branch target and appends a branch_summary entry.

getEntries() returns all non-header entries in insertion order. There is no separate persisted leaf field: loading rebuilds the leaf from the last physical entry. Pointer-only branch()/resetLeaf() changes therefore need a subsequent append to survive reload. discardEntryDurably() appends a metadata branch marker and rewrites the journal to make a discarded path durable.

createBranchedSession(leafId, { copyArtifacts? }) creates a new identity containing only the selected root-to-leaf path. It drops old label records and recreates the resolved labels for retained entries. Unlike a full fork, it does not inherit the provider prompt-cache key. With copyArtifacts (used by AgentSession.fork(entryId)), the artifacts directory is copied in the background and the new artifact manager waits for the copy before allocating ids or resolving spills (local://N.log), as for a move to a sibling file.

Context Reconstruction (buildSessionContext)

buildSessionContext(entries, leafId?, byId?, options?) resolves what is sent to the model. options.transcript: true instead builds a display transcript. Full transcript mode preserves compactions inline; collapseCompactedHistory renders only the current compacted tail, and keepDanglingToolCalls preserves still-running tool calls during a mid-turn UI rebuild.

Algorithm:

  1. Determine leaf:
    • leafId === null -> return empty context.
    • explicit leafId -> use that entry if found.
    • otherwise fallback to last entry.
  2. Walk parentId to root, stopping on a repeated id to bound corrupt cycles, then reverse to root->leaf.
  3. Derive runtime state across the path:
    • resolved and configured thinking selectors from latest thinking_level_change
    • service tier from latest service_tier_change
    • model map from model_change entries (role ?? "default"); assistant-message inference is legacy fallback only until an explicit default is seen
    • deduplicated injectedTtsrRules
    • mode/modeData from latest mode_change (default mode "none")
  4. Choose the emission boundary:
    • a later reset_boundary hides everything through that boundary from model context and collapsed live transcript
    • otherwise the latest compaction emits its summary plus kept/post-compaction messages (provider-native replacement history may supply the kept model context)
    • full transcript export retains pre-reset history and renders compactions chronologically
  5. Convert message, custom_message, and branch_summary entries into messages. Other entry types only affect replay state or metadata.
  6. Remove dangling tool calls from replay (unless explicitly retained for a mid-turn transcript), neutralizing protected reasoning metadata on rewritten turns; drop unsafe aborted/error assistant turns and their paired tool results from model context.

Persistence Guarantees and Failure Model

Persist vs in-memory

  • SessionManager.create/open/continueRecent/forkFrom -> persistent mode (persist = true).
  • SessionManager.inMemory -> non-persistent mode (persist = false) with MemorySessionStorage.

Write pipeline

Ordinary completed appends update memory and local file storage synchronously once the lazy file-creation gate has been crossed. There is no fsync, so successful local writes protect against software crashes, not power loss. Indexed Redis/SQL backends update their local view immediately but publish remotely in an ordered async queue; flush()/backend drain is required to confirm those writes. Title changes and atomic batches have their own awaited persistence paths. Streaming partial text is not persisted until the completed message is appended.

  • A new ordinary session remains memory-only until it contains an assistant message or a caller invokes ensureOnDisk().
  • Before that gate, entries remain in memory; crossing it writes the full title slot, header, and accumulated entries.
  • Explicit newSession() (including /new) calls ensureOnDisk() before returning, preserving an empty session boundary across terminals. The /delete successor (drop) is the exception: it stays lazy, because the fresh terminal breadcrumb already pins this terminal to it and a failed deletion leaves the predecessor as a legitimate resume target; a zero-turn quit must not leave an empty file.
  • Afterwards, entries append incrementally.
  • Saving an editor draft forces a discoverable header and stores draft.txt with a marker; if the draft disappears while only startup metadata remains, close removes that draft-only session. Explicit ensureOnDisk() sessions remain resumable.
  • Concurrent completed appends supersede an in-flight atomic rewrite with an authoritative full-body rewrite so stale publication cannot clobber them.
  • A queued indexed full rewrite stays non-current until confirmation. When a fenced rewrite lands after a successor supersedes it, the manager validates that durable body and records its byte size before retrying the newer transcript.

Durability operations

  • flush() drains async disk/storage queues and the open writer (no fsync); flushSync() drains synchronously supported work or rewrites a non-current file. It cannot confirm queued remote publication; those backends still require awaited flush()/drain.
  • Atomic full rewrites use storage writeTextAtomic with a commit guard and expected byte-size precondition; file storage stages then renames over the target, including an EPERM-safe move-aside fallback.
  • Local appends and publication share a cross-process publish lock. A changed byte size raises SessionWriteConflictError; lock contention raises SessionLockError without publishing the staged rewrite. This is not a content-hash comparison and cannot protect against non-cooperating external writers.
  • FileSessionStorage holds a process-owned OS lease on each session a process writes, keyed by the session id rather than the file's path, so every process that reaches one journal (through a symlink, a hard link, or after a move) meets the same lease. The lease is pi-utils tryAcquireFileLock on <session-owners>/<session-id>, where <session-owners> is ~/.omp/run/session-owners (XDG: $XDG_STATE_HOME/omp/run/session-owners), shared across profiles: on Windows a named mutex, which creates no file; on Linux an open-file-description (F_OFD_SETLK) lock and on macOS and other Unix a flock, both on a sidecar in that directory. On Linux a lease whose abstract-socket name an older omp build still binds also counts as held. Only write paths claim it, so the first process to write a session owns it; opening a session to inspect it (omp share, --export, render) never does. Managers in one process share the lease; it is released on close or a session switch, and the kernel drops it when its process exits. Processes with different home or state directories do not meet in this lease. A plain copy of a session file keeps its id, so a process writing the copy while the original's writer is live moves to a sibling; a collab guest's replica takes its own id (parentSession is the host's) so it never contends with the host.
  • A process never writes a session whose lease another live process holds: its first write moves the session to a fresh sibling <timestamp>_<new-session-id>.jsonl in the same directory, publishes the whole in-memory transcript there once, and continues appending incrementally. The owner's file is left untouched. Like fork, the sibling gets a new session id with parentSession pointing at the old one and keeps the provider prompt-cache key, so resume-by-id, the title index, and the picker never see two files for one id. The artifacts directory is copied in the background without overwriting files the moved session has already written; its artifact manager waits for the copy before allocating ids or resolving spills, and flush()/close() await it. local:// and agent:// reads that bypass the artifact manager can miss pre-move files until the copy finishes.
  • moveTo() refuses, before anything moves or any directory is created, to move a session another live process writes or to replace a destination file whose session (by its header id) another live process writes.
  • Another program can check ownership without claiming: isSessionLeaseHeld(sessionId), omp session lease --json -- <id>... ({"held": [...], "unknown": [...]}), and RPC get_session_lease observe the lease without taking it (an F_OFD_GETLK query plus a connect probe of the older abstract-socket name on Linux, F_GETLK on other Unix, a \BaseNamedObjects directory listing on Windows), so they never make a concurrent claim fork the session.
  • Under bun test, the lease directory is <os temp>/omp-test-session-owners-<uid> instead of the real state directory. It is exported as PI_TEST_SESSION_OWNERS_DIR when session-storage loads; a test that spawns an omp process which writes sessions must pass env: process.env (Bun's default is the launch environment), or run under ci-test-ts, whose children inherit PI_TEST_RUNTIME=1 and derive the same directory.
  • On the file and memory backends, a SessionWriteConflictError on durable bytes during a full rewrite means a writer without the lease (an older omp, an external tool) changed the file. The manager reads it back, keeps the entries it lacks as a side branch (the active leaf stays its own), and retries against the size it read, up to three times per rewrite. If that writer changes the file inside every retry, the session leaves the file to it and moves to a sibling (reason: "contested") rather than re-serializing the transcript on every later write. A deleted file is recreated; a file that no longer reads as this session is left untouched and the session moves to a sibling. Indexed backends keep reporting the conflict, since there it can be the manager racing its own unconfirmed publish.
  • appendEntriesAtomically() groups a synchronous callback's appends into one atomic publication. Failure rolls back staged entries and repairs retained concurrent work.
  • Rewrites serve renames, entry rewrites, migrations/sanitization, move/fork, and recovery. Session-title changes normally update the fixed-width title slot and append a title_change audit entry instead of rewriting the body.

Error behavior

  • Ordinary append failures are latched and logged once with session-file context rather than thrown into the turn loop. Later appends may retry the complete in-memory journal; flush()/flushSync() and close surface unresolved failures.
  • onPersistenceNotice reports a session moving to a sibling file as a SessionPersistenceNotice (reason, from, to); it latches nothing and never reaches onPersistenceError. Every notice raised so far is replayed to each new subscriber, like a latched failure. Interactive mode shows it as a warning, print mode on stderr, RPC as a warning notice frame, each with home-relative paths.
  • Atomic batch and recovery paths attempt authoritative repair. If publication may have happened and repair cannot be proven durable, SessionPersistenceIndeterminateError fails closed with the original and recovery errors.
  • Writer close propagates the first meaningful error. Final disposal seals the manager, making late appends/rewrites no-ops, then releases retained entries so a disposed manager cannot overwrite a revived transcript.

Data Size Controls and Blob Externalization

Before persisting entries:

  • Strings over 500,000 characters are truncated with "[Session persistence truncated large content]", except signed/encrypted provider blocks, signature fields, validated Anthropic native web/tool-search history blocks, Anthropic server-compaction replay carriers, and Cursor server records (cursorHistory payloads), which must remain byte-exact for replay.
  • Transient jsonlEvents is removed.
  • If an object has both string content and numeric lineCount, line count is recomputed after truncation.
  • Image data URLs in image_url fields are always content-addressed in the blob store and replaced with blob:sha256:<hash>, regardless of length. Base64 payloads at least 1,024 characters are externalized in image content blocks, images[], snapcompact frames[], and image-generation results.
  • Redundant OpenAI Responses thinkingSignature copies are omitted when the authoritative reasoning item already exists in providerPayload.
  • Spilled MCP tool results omit duplicate details.structuredContent when the rendered structured output already lives in their truncation artifact.

These projections leave the live entries unchanged. On load, ordinary persisted image references are resolved back to inline payloads. Snapcompact frames stay lazy until context reconstruction selects them. Archives with frames truncated by older persistence code fall back to their retained text or undamaged frames.

Storage Abstractions

SessionStorage owns filesystem-like operations used by SessionManager: synchronous directory/existence/write/stat/list operations; async read, sliced read, write, guarded atomic write, rename, unlink, artifact-aware deletion, title update, writer creation, and backend drain. Optional capabilities include synchronous reads, confirmed remote writes, assistant-turn scans, file locking, session ownership claims, and conditional artifact-aware deletion.

Implementations and adapters:

  • FileSessionStorage: real local files
  • MemorySessionStorage: map/chunk-backed in-memory storage for non-persistent sessions and tests
  • IndexedSessionStorage: shared local index plus ordered remote publication used by Redis/SQL-backed storage

SessionStorageWriter exposes append, optional appendSync, flush, optional flushSync, isOpen, close, and getError.

Manual storage maintenance

omp gc previews maintenance by default; --apply is required to sweep unreferenced blobs, archive eligible cold sessions, checkpoint database WALs, or prune stale state. Storage maintenance is separate from model-context compaction.

The sessions phase also removes transcript rewrite temps (.<name>.jsonl.<snowflake>.tmp, and the same pattern beside registered --session files) once they are past the 5-minute write grace. A live rewrite renames its temp into place within milliseconds, so an older one was left by a killed or crashed writer.

The stale-state phase is opt-in: it runs only with --stale or when gc.stale is enabled (default off), so an unqualified omp gc --apply never deletes reports or replicas. It removes custom-session-files markers and terminal breadcrumbs whose session file no longer exists once they are a day old (a lazy session's marker names a transcript that is written only on its first turn). A breadcrumb recorded as a fresh /new boundary is always kept: --continue honors it before its transcript exists, and it is rewritten when the session materializes or replaced by the terminal's next session. For the default agent dir — or a custom agent dir named agent, whose parent is treated as the config root — it also expires debug report bundles (reports/*.tar.gz) and collab guest replicas (collab/*.jsonl plus their artifact directories and custom-session marker) that are both outside the newest gc.staleRetainNewest (default 20) and older than gc.staleRetainDays (default 30). A replica that a terminal breadcrumb points at, or that a running guest holds open, is kept. Stale state is pruned before the blob sweep, so blobs referenced only by an expired replica are swept in the same run.

The compress phase is opt-in as well: --compress or gc.compress (default off). It stores every file of a session (the transcript and its artifact tree) with the filesystem's transparent compression once the transcript has been idle for gc.compressAfterDays (default 7, --compress-after-days); files written more recently are skipped. Session ownership leases are taken only for sessions with files left to compress: a session is skipped while a running process holds the lease of its transcript or of any transcript nested in its artifact tree (subagent sessions, recognized by their session header whatever their file name), and those leases are held while its files are rewritten. Unlike archiving, a session with an interrupted last turn qualifies, since it stays resumable. Paths, bytes, and mtimes are unchanged and no history or stats rows are deleted, so compressed sessions stay listed, resumable, and searchable. A regular file with a single link that spans more than one filesystem block qualifies; already-compressed formats (.gz, .zst, images) are skipped. The volume holding the sessions root decides the method:

  • APFS (macOS): detected by matching the directory's device against apfs entries in the mount table. ditto --hfsCompression writes compressed copies, with the original mtime to the nanosecond, into a fresh .omp-gc-compress-XXXXXX scratch directory beside the originals that holds an owner marker. A copy replaces its original only when its size and SHA-256 match and it allocates less space; immediately before the rename, the original's inode, size, and mtime must still match their values at selection and it must still have a single link, and a transcript's check and rename run under the session publish lock that serializes appends. A scratch directory left by a killed run is removed by the next --compress --apply run, under the sessions root and the archive root; a directory with that name and no owner marker is removed only when empty. Stats sync and the dashboard watcher skip .omp-gc-compress-* directories, so they never ingest a scratch copy. The replaced file is a new inode, so the next stats sync re-reads each compressed transcript once from the start; it replaces the session's rows rather than duplicating them. Appending to a compressed file stores it uncompressed again, which a later run picks up.
  • NTFS (Windows): detected with GetVolumeInformationW, which must report NTFS with per-file compression support. compact /c sets LZNT1 compression on each file in place, keeping its inode and timestamps; later appends are stored compressed too. WOF (compact /exe) is not used because a write inflates the whole file.

Other filesystems report the phase as unsupported.

The strip phase is opt-in too: --strip or gc.strip (default off). Providers return state only they can read back, which omp persists so a resumed turn replays natively: signed and encrypted reasoning, the verbatim wire response kept for prompt caching, request controls, uploaded-file references. Once a transcript has been idle for gc.stripAfterDays (default 30, --strip-after-days), past every provider's cache and response retention, the phase rewrites it without that state: incremental OpenAI Responses histories, Cursor and Gemini native histories, requestControls, inputTransformations, opaque thinkingSignatures, itemIds, redactedThinking blocks, tool-call thoughtSignatures and leftover streaming state, and image providerFile references. Each turn then reads like one recorded by another model, which every provider already replays (reasoning as plain text, or not at all). The latest replay turn keeps everything: its run of assistant responses joined by tool results, which a provider replays as one assistant message and validates as a unit, since a resumed tool loop must replay every one of those responses' reasoning unmodified. State with no copy elsewhere also stays: reasoning source markers (reasoning_content, including the one a chat-completions reasoning_details envelope carries, which the envelope is reduced to), text signatures (OpenAI message phase), Anthropic message controls and native compaction blocks, OpenAI snapshot histories, and compaction entries (an OpenAI remote compaction's stored summary is only a placeholder for its encrypted history). It covers each session's transcript and the subagent transcripts in its artifact tree, under the same ownership-lease rules as compression, and archived .jsonl.gz sessions, which are recompressed. Lines without replay data keep their bytes, the title slot included, and the transcript keeps its mtime, so session ordering is unchanged; the stats database reparses a rewritten transcript once. The phase runs before archiving and compression so both store the stripped transcripts.

To compress without running the command, enable gc.autoCompress (default off). Once an interactive session has painted its first frame, omp starts a detached omp gc --compress --apply at low CPU priority when the last background run is at least a day old and the sessions directory supports compression. Support is checked by compressing a throwaway probe file in the sessions directory, so a volume whose compressor stores files uncompressed (macOS 27's ditto --hfsCompression, #16244) is not copied and hashed every day; when the check fails, gc-auto-compress.log records why and the next check is a day later. Launches claim the run under an OS lock keyed on the log and recheck the interval while holding it, so simultaneous launches start one child. The child outlives the launching session, writes its summary to gc-auto-compress.log in the agent dir, and that file's mtime marks the last run; a spawn that fails removes the log so the next launch retries. The child takes gc.lock like any other gc run, so a run that finds gc already running exits with that error in the log and is retried a day later. A file that compresses but does not shrink stays a candidate and is copied again on later runs. Print, RPC, and ACP launches never start it.

Journal payload I/O is streamed during blob-reference scans, archive history/stats reconciliation, gzip creation, and rollback. Active .jsonl, recoverable .jsonl.*.bak, and archived .jsonl.gz records all participate in reference discovery, including references in malformed JSON text. Compressed scans drain and validate the complete stream before their results can authorize deletion. Archives retain the original JSONL bytes when decompressed (less any replay data the strip phase removed), and artifact trees keep their existing layout.

Streaming avoids materializing a whole journal; it does not make GC constant-memory. A single long record, reference sets, and history/stats identity collections still consume memory. Original files and temporary compressed output also coexist during archive publication.

Session Discovery Utilities

Discovery helpers live in session-listing.ts; SessionManager exposes project-scoped wrappers:

  • getRecentSessions(sessionDir, limit?) -> lightweight welcome metadata, default limit 4
  • findMostRecentSession(sessionDir) -> newest by mtime
  • findMostRecentNonEmptySession(sessionDir) -> newest resumable content, used by continueRecent
  • listSessions(sessionDir, storage) / SessionManager.list(...) -> project scope with lifecycle status
  • listSessionsReadOnly(...) -> same metadata without backup recovery
  • listAllSessions(storage) / SessionManager.listAll() -> project buckets under the managed sessions root, not arbitrary custom directories
  • SessionManager.listForPicker(...) / listAllForPicker() -> pinned-first lists with untitled, prompt-less zero-turn stubs removed; pinned stubs remain selectable
  • resolveResumableSession(...) -> local lookup then optional global fallback

getRecentSessions() sorts file stats and first looks up titles in history.db's session_titles index; unindexed files fall back to content scanning and backfill their title. findMostRecentSession() reads a 4 KiB prefix. Status-enabled full/non-empty scans read that prefix plus a bounded 32 KiB tail. Scans are stat-keyed and cached; large full lists use bounded parallel workers. Normal per-directory scans also recover the newest orphaned EPERM backup when its primary JSONL is missing.

SessionManager.list()/listAll() sort pinned sessions first; raw listing helpers sort newest first. Empty filtering is picker/continue-specific, not applied to id lookup or maintenance. Resume matching is case-insensitive and accepts session id prefixes, full filename prefixes, or the id suffix after the timestamp. Explicit sessionDir disables global fallback unless allowGlobalFallback: true is requested.

HistoryStorage (history-storage.ts) is a separate SQLite subsystem for prompt recall/search, not session replay.

  • DB: ~/.omp/agent/history.db
  • Table: history(id, prompt, created_at, cwd, session_id, use_count), with unique prompt
  • FTS5 index: history_fts; new prompts are indexed by an insert trigger
  • Normalizes line endings and surrounding/trailing whitespace, then deduplicates prompts across the database
  • Resubmission updates the latest timestamp/cwd/session provenance and increments use_count
  • add() writes synchronously before returning its resolved promise; failures are logged rather than thrown
  • The same DB also contains independent session_titles and append-only session_recaps tables. Idle recaps do not enter the session JSONL or model context.

Use session files for conversation graph/state replay; use HistoryStorage for prompt history UX.