Sessions, branches & durability
“Persistence” means two very different things in agent systems: saving a conversation transcript so you can reopen or branch it later, versus saving execution state before every side effect so a crashed worker finishes an in-flight turn without duplicating work. PiSharp provides both as separate, cleanly bounded libraries.
Append-only JSONL trees (PiSharp.Sessions)
Section titled “Append-only JSONL trees (PiSharp.Sessions)”PiSharp.Sessions ports Pi’s v3 JSONL session format verbatim so Pi and PiSharp can open, resume, and branch each other’s files under ~/.pi/agent/sessions.
- Every entry points to a
parentId, forming a tree inside a single append-only file. - Branching (
/treeorSessionBranchPlanner.Fork) moves the active leaf pointer to an earlier entry without deleting abandoned attempts. - Compaction appends a
compactionsummary entry referencing earlier history while leaving the raw entries intact for inspection or branching. - Branch-aware tool state: tools like StatefulTodo reconstruct their state exclusively from
SessionSnapshot.BranchEntriesso switching branches never leaks state across timelines.
What PiSharp.Sessions does not do: if the process dies while a tool call or provider stream is in progress, the interrupted turn is not automatically resumed on restart. For that guarantee, use PiSharp.Durable.
Commit-first execution (PiSharp.Durable)
Section titled “Commit-first execution (PiSharp.Durable)”PiSharp.Durable replaces the in-memory loop with a transactional store (ISession) and task scheduler (IHarness):
- Commit before display: every submission, assistant entry, tool task creation, tool result, compaction, and document patch commits to
IStoragebefore observers see it. - Code stays in the
Registry, data stays inIStorage: conversations store the names of extensions and tools, resolved against your liveRegistryon every run so you can upgrade code and reload extensions cleanly. - Tasks are checkpointed state machines:
pi.generation(model turn),pi.tool(tool call),pi.compaction, and your own custom tasks save their checkpoint after each phase. - Reopening finishes pending work:
Harness.OpenAsyncresets anyrunningtask left by a dead process back topending; callingResume()(orSubmitAsync/WaitAsync) finishes them. - Crash-loop protection: a task cut off by a process crash five times in a row halts instead of crash-looping forever.
- Rewindable documents: extensions attach typed JSON documents to conversations or sessions (
Documents.DefineRewindableConversationDoc). Forking a conversation withDocumentFork.AsOfsnapshots the document’s exact value at the fork entry.
Choosing a storage backend
Section titled “Choosing a storage backend”| Backend | Package | Survives restart? | Use it for |
|---|---|---|---|
MemoryStorage |
PiSharp.Durable |
No | Fast unit tests and ephemeral in-memory demos. |
JsonlStorage |
PiSharp.Durable |
Yes (folder per session) | Human-inspectable append-only durable logs on disk (1.5.2 adds read-only recovery). |
SqliteStorage |
PiSharp.Durable.Sqlite |
Yes (single .sqlite WAL file) |
Recommended default for local durable agents, AgentTeam, and single-node services. |
StoreStorage |
PiSharp.Durable.Store |
Yes (shared Chord v2 store) | Apps already hosting PiSharp.Chord that want Chord tables, plugins and Durable sessions sharing one owner, sequence and archive. |
SQL Server (PiSharp.Durable.MSSQL) |
Source-only (not on NuGet) | Yes | Multi-instance SQL Server experiments built from source. |
What happens to an interrupted tool call?
Section titled “What happens to an interrupted tool call?”When a process crashes after a tool started executing and before its result committed, ToolRegistration.Replay controls recovery:
| Replay setting | Behavior on reopen | Use when |
|---|---|---|
ToolReplay.Unsafe (default) |
Does not rerun the tool. Commits an "interrupted" error result so the model inspects workspace state and decides what to do. |
Shell commands (bash), file edits (write, edit), non-idempotent external APIs. |
ToolReplay.Safe |
Reruns the tool from the beginning (reusing any memoized OnceAsync checkpoints inside it). |
Pure lookups (web_search), idempotent APIs using IdempotencyKey, and orchestration tools (subagent, delegate). |
Durable on Chord (PiSharp.Chord + PiSharp.Durable.Store)
Section titled “Durable on Chord (PiSharp.Chord + PiSharp.Durable.Store)”When your application hosts multiple reactive domains, HTTP routes, or hot-reloaded native plugins alongside agents, PiSharp.Chord + PiSharp.Durable.Store connect them cleanly:
StoreStorage.OpenAsync(store, owner, ctx)mounts a Durable session inside a ChordIStorewithout taking ownership of the store’s lifetime.RegistryReader.Createprojects plugin-contributed extensions from the real Chord host into Durable’s registry, tracking reloads and retirements.ViewBridgebridges typed immutable Durable conversation/task views into Chord JSON members with automatic observer cleanup (Durable.Chord.Examples35–37).
Related guides
Section titled “Related guides”- Crash recovery, retries & replay safety
- Managing long context & tool bloat
- Sessions library SDK · Durable library SDK · Durable on Chord’s store
- 28-ResearchWorkbench (native
SessionBranchPlanner.Fork) & Durable Examples 00–47