Session storage
How Carmel Agent stores conversations and recovers runs with Pi Durable. This page is for contributors; for backups, see Backups and upgrades.
Carmel Agent uses Pi 1.6.0 and @earendil-works/pi-durable for execution and conversation storage. Pi Durable's native Node SQLite backend replaces pi-session-backend-sqlite-node and the harness formerly shipped inside pi-agent-core. Carmel Agent's metadata, authentication, issues, and schedules remain in the existing Drizzle database.
Questions and session credentials use native run termination and idempotent submissions, with request lifecycle and encrypted credential storage owned by Carmel Agent.
Storage and backups
CARMEL_PI_SESSION_DIR selects the native storage directory, defaulting to data/pi-sessions.sqlite.durable/. It contains one SQLite file per Carmel Agent session, named with a hash of the session ID. Separate native sessions keep tool registries, credentials, checkpoints, and entry lookups scoped to a single Carmel Agent conversation.
Back up the metadata database and the whole session directory with the server stopped. The runtime opens Durable storage directly; historical importers and metadata migrations have been retired after verification of the deployed data. Retired database files can be kept separately as archives, but the app never reads them.
Saved message and image-link IDs remain addressable through a session-scoped Durable entry index. New entries use native durable:<id> identifiers. The active conversation document stores the selected branch independently of public message IDs.
Forks and edits use append-only conversation branches. A non-truncating edit creates fresh IDs for the rewritten suffix and retains the original branch. Cross-session forks use a consistent SQLite snapshot, including WAL contents, and require all Durable work to be idle so executable checkpoints cannot be copied into another session.
Message references
The chat composer accepts files through the attachment picker, drag and drop, or paste. Images use native image input. Other files are uploaded unchanged to uploads/<unique-id>/<filename> inside the agent's workspace, then inserted into the draft as @path references. Each upload gets its own directory so repeated filenames cannot overwrite workspace files. Uploading workspace files requires the agent's read and write permissions and also works with text-only models. A message can include up to ten attachments and references; each uploaded file is limited to 20 MiB. Removing a mention removes its reference but keeps the uploaded file in the workspace, where it can be managed through the file browser.
The prompt includes file paths, and the model uses filesystem tools to read their contents. Carmel Agent does not automatically extract or inline uploaded documents. Plain text, Markdown, and CSV are readable through the native read tool; PDF and Office files require an appropriate tool or converter. Saved references preserve paths rather than snapshots of file contents, so later reads see the file's current contents.
Type @ in the composer or use its mention button to choose workspace files, folders, or another conversation. Folder entries open for browsing; “Mention folder” selects the current folder. File and folder mentions insert their paths inline at the cursor, leaving the caret after the path so typing can continue. Paths with spaces are quoted. Removing an inline mention also removes its attached reference. Conversation references appear as removable chips. Saved references have clickable links. Up to ten references can accompany a message, including one with no text.
The server validates file paths against the agent's readable workspace and conversation references against the sender's ownership. Files and folders pass their paths to the agent's filesystem tools. Conversation references read a recent page from the selected native Durable branch, excluding thinking, tools, and nested reference context. Excerpts have a 6,000-character limit per conversation and a 24,000-character combined limit; they do not copy a complete session or subscribe to future changes.
Durable's input entry stores a carmel.referenced-input with display text and structured reference metadata separately from model-facing source context. Reloads, edits, and forks preserve that snapshot. Retrying a referenced message resolves its selected sources again, checking current access and reading current history. No new metadata database or cross-session execution is involved.
Recovery and observation
Pi owns generation checkpoints, tool intent, results, retry policy, compaction, steering submissions, and live progress. Carmel Agent watches the native conversation view and forwards pi.live operations through the shared protocol. Chord's immutable delta application restores streamed messages and tool progress without custom text, thinking, or nested-tool reducers. ConversationTranscript fills missing pages when a slow observer falls behind the native transcript window; committed entry IDs prevent duplicate messages and tool-budget counts.
After an unexpected process exit, requesting a run without new input resumes the selected conversation's checkpoint after installing current credentials and permitted tools. It does not rewind and resubmit the user message. A new prompt first settles pending work and then submits its new input. Read-only requests never start scheduling. A deliberate stop uses Durable's abort protocol and settles pending work rather than leaving it resumable.
Connection snapshots report pending Durable work separately from the current process's active run. After a restart, the chat offers Resume and Stop for that saved work. Stop also works without a live run ID or provider credentials. Retry and transcript edits remain guarded until the checkpoint settles.
Tools default to replay: "unsafe". Pi records intent before executing them; after interruption it returns an error result instead of repeating a possible external effect. Guarded read/grep/find/ls and the built-in skill loader explicitly opt into safe replay. Native Durable Codemode is replay-safe: Pi journals delivered results and reattaches each nested call by its stable key. Completed effects are reused; interrupted nested calls follow their own replay policy. A failed unsafe call can be caught by the script, while an interrupted safe read may run again. Calls admitted by the old unsafe Codemode implementation retain that original policy and settle as interrupted.
Native features adopted
- Native SQLite storage, conversations, checkpoints, replay policy, committed live documents, and queued submissions.
- Native read/write/edit/bash tools, including batched edits, bounded output, efficient text reads, diagnostics, and shell output spill files. Native image reads use the same guarded environment, with lazy Photon processing in a worker for conversion and resizing to the selected model's limits.
- Tools register native
ToolRegistrationdefinitions and return nativeToolExecutionResultvalues. Bash exposes retained output, truncation, spill path, and exit code to programs. Schema tools return their structured value even on an error when a value is present; scripts must check fields such asexitCode. Other failed calls reject with their diagnostics. Tools without explicit output schemas return text to Codemode scripts; MCP structured content remains in native result details unless the server declares an output schema. Image values exposed to scripts must be part of a declared structured output, which scripts can pass toimage(). - OpenRouter's per-key model lists are cached under a hash of the credential and configured endpoint. Shared legacy OpenRouter rows are ignored by model runtimes, and each OpenRouter configuration refreshes separately. Other providers retain their shared catalog cache.
- Native named prompt sections and atomic model/thinking/tool configuration. Already-named sessions skip title history reads; new titles use a bounded sample from native entry pages.
- Native skill catalog formatting.
- Native Durable Codemode, nested tool tasks, validated
// @options:source headers, native tool discovery, and the bounded prompt catalog. Scripts can check"name" in tools, discover permitted tools withsearchTools()/describeTool(), and show images withimage(block). Script options can lower Carmel Agent's output and deadline limits, but cannot raise them. - Native threshold, background, and overflow compaction. Small model windows get scaled reserve/tail settings: Durable's
enabledflag controls overflow recovery too, so the old threshold-only disable workaround is removed. - Pi's
azureprovider identifier.
Pi 1.6.0 still has no Durable grep/find/ls implementations. Those three guarded Agent-loop tools retain a small bridge into native ToolRegistration; Carmel Agent's generic tool/result compatibility layers are gone.
Workspace authorization, container execution, credential selection, nested-call admission limits, and issue review/cron policy remain Carmel Agent responsibilities. Pi's host filesystem resource loaders bypass the guarded environment, so Carmel Agent retains guarded discovery. Prompt argument helpers are still not public exports (parseCommandArgs and substituteArgs live only in the package's internal dist/core/prompt-templates module), so their positional/default/slice semantics remain in the adapter, including the 1.3.0 rule that "" and '' are real arguments and the 1.5.1 quoting fixes. Native Durable task primitives could underpin future application workflows, but do not replace cron scheduling or issue review policy by themselves.
Tasks, subtasks, and background hosts
Home exposes create_task, read_task, list_tasks, and task queue/update controls. A task's bounded brief and review lifecycle are Carmel Agent application policy; its model execution uses Pi Durable. Task conversations can delegate read-only analysis or web research with start_subtask, inspect it with list_subtasks, steer it with send_subtask_update, join it with wait_subtask, or stop it with stop_subtask.
Subtasks are native child conversations in the parent's Pi database, with native task ownership, persisted checkpoints, isolated model context, and the same agent's readable workspace. They receive file reads, knowledge retrieval, private session retrieval, skill loading, and, when the agent has web access permission, web search and fetch. They cannot edit, execute commands, drive the interactive browser, send notifications, create tasks, or delegate again. Each session permits four active subtasks with five-minute execution deadlines. Existing delayed background work remains recoverable.
Foreground subtasks return their answer to the calling tool. Background subtasks use native background ownership and persisted sleep/report tasks. The parent task must collect every background result before reporting completion. Stopping a parent task aborts its native background descendants. Subtasks appear inside the task detail view; legacy helper links redirect there. Ordinary working conversations can also use subtasks; stopping their foreground reply preserves already-started background work.
The server retains the Pi lease and execution resources while background work remains. After a clean shutdown or crash, it reopens registered hosts with current credentials, visible-agent access, and permitted read and network tools before resuming native tasks. An unfinished parent turn keeps recovery paused until the user resumes or stops it. The subtasks section shows host failures and offers Resume for recoverable work. Tasks that exceed Pi's restart limit now end with a visible failure; they are no longer parked or retried through a host adapter.
A subtask stores its result in native state and its own transcript, rather than pasting a second report into the parent conversation. For task-owned subtasks, only the consolidated parent result reaches Home. Standalone session subtasks publish one concise, deduplicated notice with a link, without triggering another agent summary. Existing native task/document names and helper HTTP endpoints remain compatible with saved work; the user-facing feature is Tasks. Schedules remain separate from task review.
Durable Codemode
Carmel Agent registers Pi's native Codemode implementation and prompt section. Carmel Agent supplies its guarded execution environment, permitted native tool registry, sequential scheduling, admission limits, and browser handoff. Pi owns argument repair/validation, replay intent, nested results, output limits, script journals, cancellation of owned work, and script recovery. Nested results do not become transcript messages.
The default limits remain 60 seconds, 64 MiB of VM memory, and 250 nested calls per script. Limits are memoized in the native tool task and admitted calls use native nested-call documents; reattaching an existing nested task does not consume run admission again. The deadline includes time spent interrupted. Native store() and load() keep JSON values per conversation, commit changes only on successful scripts, and fork as of the selected entry.
Native output includes elapsed time, text markers, console grouping, and spill/image paths. The chat renders native pi.live.nestedTools slots, including arguments, bounded output, diagnostics, and completion summaries. Saved Codemode results render Pi's native details.calls. Aborted parents may have no saved call details; cancellation is visible in live state. Nested tool control hints do not terminate the model run; direct model calls retain native termination behavior.
const result = await tools.bash({ command: "npm test" });
text({ exitCode: result.exitCode, output: result.output });
store("lastTestExit", result.exitCode);Removed compatibility layers
The previous AgentHarness/AgentLane facade, generic prompt-dispatch port, filtered event bus, old message-event reconstruction, fake queue watch, and HTTP delta batching timer are retired. Runs call native Conversation.configure, submit, abort, and waitForIdle. Steering and issue delivery read native Submission.status() and InboxDoc; stopping queued input uses abortSubmission.
Native live_update.ops now cross the shared protocol into the client. Pi owns progress batching, and HTTP supplies authentication, sequence numbers, replay bounds, and a terminal application result. Deploy server and client together: this replaces the previous message-update and tool-lifecycle event protocol. Connection snapshots include the native live-state base needed to replay subsequent deltas.
PiRun handles the application-specific boundary between committed input settlement and delivered HTTP observations. It waits for the idle conversation view before finalization and uses native transcript paging to recover missing committed entries. The small SessionLog port remains for Carmel Agent's public branch IDs and retry restoration; its implementation calls native storage operations directly.
Compaction errors come from native pi.live.compactionFailure, with one warning per failed task. Recovery is announced when a native compaction summary entry is placed, so a declined compaction cannot claim success. Native settings select only the current run's extensions.
The remaining bridges protect application behavior: guarded workspace/resource reads, container execution, per-user credentials and personalized catalog caches, resolved model overrides, stable saved entry/image links, and issue/cron policy. Pi 1.4.0 accepts a Models getter, so the session installs fresh per-run credentials on a shared lease without a forwarding proxy. Prompt argument helpers are still not public exports, and grep/find/ls have no Durable equivalents.
Further 1.2.0 adoption
Pi's optional models.* Codemode helpers remain disabled. Exposing classifiers and image generation needs application policy for which models and paid operations scripts may invoke; scripts currently reach the agent's configured tools.
Deferred tools and native tool_search could reduce large MCP catalogs in the prompt. Adoption needs to reconcile Carmel Agent's permission and allowlist filtering with Durable's separate model-visible and program-callable tools, including discovery after reconnect.
The optional bounded ExecutionEnv.listFiles can support future tree listing after implementing Carmel Agent's path and mount authorization.
OpenRouter regional catalog filtering needs its configured endpoint passed into Pi's provider configuration before refresh; the cache scope alone does not configure the upstream request. Synchronous model serializers still use the shared catalog lookup and should eventually read the matching personalized catalog too.
The upgrade also brings MCP structured results that may be arrays, primitives, or null, stricter misplaced Codemode options, and provider retry/timeout fixes. Carmel Agent already passes structured MCP values through without an object-only restriction. Pi's terminal clipboard, themes, and CLI extension-start changes do not apply to Carmel Agent's web client and Durable run path. See the 1.2.0 release, Durable changelog, and MCP changelog.
Prompt cache stability
Carmel Agent does not inject a clock into system instructions or user messages, matching Pi coding-agent/TUI's default prompt. For relative scheduling, the model checks time with bash when available or asks the user. Native message timestamps remain storage metadata.
Named sections reduce redundant storage writes. They preserve the request prefix when the selected model supports mid-conversation system changes; otherwise Pi collapses section updates into the leading system prompt. Keeping a changing clock out of the prompt protects conversation-history caching in both cases. MCP tools are sorted by name within each configured server, keeping native declarations and Codemode's embedded catalog stable when a reconnect returns the same tools in another order.
Pi Durable is experimental. Versions are pinned to 1.6.0, including @earendil-works/chord, which Pi Durable depends on at that exact version. The storage contracts, stable entry IDs, restart replay policies, and streaming/reconnect behavior are covered by integration tests. Storage errors now fail and close the native session; pending waits reject instead of retrying operations on it. Existing task and live-document versions migrate through Pi's native definitions. See the Pi Durable announcement and upstream package documentation.
Pi 1.6.0
Back up the session directory before deployment. Pi 1.6 writes pi.tool version 4 and pi.generation version 3, which Pi 1.5 blocks as task_too_old. Live tool slots may now report sleeping or waiting for parked calls. A parked request call is hidden in the chat while its request card is open; other parked calls show as running.
Chord 1.5.1 made its v2 runtime the package root, so Context, JsonValue, and WatchHandle come from @earendil-works/chord/context, /json, and /watch. Code that calls a tool's execute() directly handles the new ToolPark return.
Adopted:
- MCP servers are Codemode namespaces. Each connected server's tools come from their own extension with
namespace: { name: mcpNamespace(id), description, instructions }; the description is the first line of the server'sinitializeinstructions (mcpServerSummary), anddescribeNamespace()returns the full, redacted instructions, which Carmel Agent previously dropped. Tool names are unchanged, so saved transcripts and cached prefixes stay valid. normalizeMcpInputSchemareplaces Carmel Agent's object-schema coercion of MCP input schemas.- The
fit-imagesextension fits images in any tool result the model gets (MCP, Codemodeimage()) to the model's limits, sharingread's lazy Photon worker. - Grep context lines now come from ripgrep (1.5.1), so the guarded
GrepOperations.readFilewas removed. The search path is still authorized and remapped before rg starts. - The prompt argument adapter follows 1.5.1's parsing: apostrophes inside words stay literal,
\"escapes the open quote, and an unclosed quote is text. - User requests park their tool call on a signal instead of ending the run, and the response becomes the call's result.
PiRunends Carmel Agent's run once only parked work is left (Harness.wake()inactive with a sleeping slot); a continuation delivers the signal and resumes. - Browser handoff, collaboration access, and session-secret redaction are one
carmel.guardsextension of per-toolwraps, so they apply to whichever same-named tool wins, for model calls and Codemode's nested calls alike. Wrappers return their base's park unchanged. - Attached chat images are fitted with
fitImagesbefore submission, sharingread's Photon worker. The notes on what changed reach the model as hidden input context, like reference excerpts.
Not adopted:
createMcpToolName(mcp__<server>__<tool>): namespaces no longer depend on name prefixes, and renaming would change every saved MCP tool call.limitMcpContent: Durable already bounds returned tool output (outputLimits, 50 KB head by default), so a second MCP-only cap adds nothing.validateMcpServerConfigand@earendil-works/pi-mcp/oauth: Carmel Agent's server shape, secret expansion, and container transport differ from themcpServersfile format, and Carmel Agent has no MCP OAuth flow yet.- Per-conversation
configure({ compaction })andonAbort: "keep": each Carmel Agent session already has its own harness settings, and collaboration replies are delivered only between runs.
Pi 1.5.0
Tool schemas use @earendil-works/pi-schema. Subtask tools use its typed builders; Zod-based and MCP tools pass JSON Schema directly without TypeBox casts. Validation and argument coercion come from Pi, including for plain JSON Schema tools.
Live progress is visible immediately through conversation watches, while Pi batches its storage writes for up to one second by default. A crash may lose that last second of partial text or running tool output; settled messages and results remain durable. Commit observers are reserved for persisted changes.
Back up the session directory before deployment. Pi 1.5 writes newer pi.live, generation, and tool-task versions that Pi 1.4 cannot drive. Interrupted model requests now count against native retry policy, and restart-limited tasks settle with explicit errors. Carmel Agent uses the renamed filesystem watch change types and tagged commit-observer changes.
Native turn limits count tool rounds; Carmel Agent's existing guard counts individual tool calls, including nested Codemode calls, so its budget remains application policy. Guarded resource loading, script limits, and cron/review policy also retain their application adapters. See the Durable 1.5.0 changelog.
Pi 1.4.0
Opening a session SQLite file migrates Pi's task table to schema version 2. Back up the session directory before deployment: a migrated file cannot be reopened by Pi 1.3.0. Carmel Agent's two session documents declare checkpointWhen: () => false, preserving their delta storage behavior under the new required API.
Native HarnessOptions.models and settings getters replace Carmel Agent's model forwarding proxy. Pi's ClientIdentity now supplies provider attribution and User-Agent headers to both Durable turns and direct model calls, replacing Carmel Agent's local header map and stream wrapper. PI_TELEMETRY=0 still disables attribution. Pi's compaction.modelOverrides can set thresholds per model, but Carmel Agent already supplies scaled thresholds for the selected model at each run. Guarded resource loading, prompt argument parsing, and cron policy still need their application adapters.
The new Durable sleeping-task status and five-restart crash-loop limit apply to Pi's internal tasks. Carmel Agent does not call the removed TaskRuntime.sleep() API. See the Durable 1.4.0 changelog.
Pi 1.3.0
The 1.3.0 bump is small on top of the 1.2.0 adoption above. These behaviors arrive through the packages Carmel Agent already calls, with no extra adapter:
- Model switching drops thinking blocks from earlier assistant messages produced by a different model, instead of sending them as text.
- OpenAI GPT-6 thinking-level changes with an API key keep the prompt cache (
compat.supportsMidConvoEffort). - Malformed final tool-call arguments fail validation with
argumentsErrorandarguments: {}, instead of running on the JSON parsed before the error. The chat shows that error, including the raw text, in the tool input. - Provider retries cover
Unable to forward upstream streamandThe service is temporarily unavailable. Gemini 4 sendsthinkingLevel. Xiaomi MiMo exposes onlyoffandhigh. Claude Haiku 5.5 and Opus 5 sendthinking: {type: "disabled"}foroff. Kimi Code uses Chat Completions athttps://api.kimi.com/coding/v1; amodels.jsonbase URL forkimi-codingmust include/v1. Carmel Agent has no built-in Kimi override. - After compaction, the system baseline leads the model context when the kept history includes an assistant message.
toBase64encodes images in 8 KiB chunks. Carmel Agent does not share a SQLite file with host tables, so the new storageprefixoption is unused. Chord's host-runtime rewrite is also unused: Carmel Agent only usesContext,JsonValue,BACKGROUND_CONTEXT, andwithAbortSignal.
CodemodeSandbox.close() now returns after requesting worker shutdown, not after the worker exits. Each Carmel Agent script waits on onWorkerStarted until that worker exits, so an aborted script cannot leave a thread behind. Pi's CLI also caps live workers at two across its sessions. That cap is not copied: concurrent Carmel Agent sessions each run their own script, and waiting for the worker this call started already bounds leaks to in-flight scripts. Node 24 is the runtime; the Bun hang this change avoids does not apply.
Not adopted, because 1.3.0 does not replace the custom code:
- Durable grep, find, and ls still do not exist. The guarded Agent-loop bridge stays.
ExecutionEnv.listFilesis an optional tree listing, not those tools, and Pi's host resource loaders still bypass the guarded environment. - Prompt helpers remain internal. The adapter keeps their semantics, including empty quoted arguments.
- Deferred tools and
@earendil-works/pi-durable/tool-searchstill need Carmel Agent's permission and allowlist filtering reconciled with Durable's separate model-visible and program-callable tools. models.classify()andmodels.generateImages()stay off. Scripts may call the agent's configured tools; exposing paid classifier and image calls needs a separate policy.- OpenRouter regional catalog filtering still needs the configured endpoint passed into Pi's provider configuration before refresh. The cache scope alone does not set that request.
/share,sessionShareDestination, resumed-session--tools, themes, and terminal clipboard are CLI features. Carmel Agent's web client and Durable run path do not use them.
See the 1.3.0 release.
