Overview
Pi sessions are append-only conversation trees stored as JSONL files. Sessions support:- Persistent conversation history
- Non-destructive branching (try different approaches)
- Context compaction (summarize old context when approaching limits)
- Session forking (copy from another project)
Session Structure
JSONL Format
Each line is a JSON object representing an entry:packages/coding-agent/src/core/session-manager.ts:27
Entry Types
Sessions contain multiple entry types:packages/coding-agent/src/core/session-manager.ts:136
Tree Structure
Sessions useid and parentId to form a tree:
Branching
Branching moves the leaf pointer without modifying history:packages/coding-agent/src/core/session-manager.ts:1111
Session Lifecycle
Creating Sessions
packages/coding-agent/src/core/session-manager.ts:1246
Session Directories
By default, sessions are stored under~/.pi/agent/sessions/:
Appending Entries
packages/coding-agent/src/core/session-manager.ts:818
Context Building
The session manager builds the LLM context by walking from the leaf to the root:Compaction Handling
When a compaction entry is encountered:- Emit the summary as a
compactionSummarymessage - Emit kept messages (from
firstKeptEntryIdup to the compaction) - Emit messages after the compaction
packages/coding-agent/src/core/session-manager.ts:306
Compaction
Compaction summarizes old context to make room for new conversations.When Compaction Triggers
Compaction triggers automatically when:reserveTokens: 16384 (reserve for new conversation)keepRecentTokens: 20000 (keep recent messages)
packages/coding-agent/src/core/compaction/compaction.ts:114
Compaction Algorithm
-
Find cut point: Walk backwards from newest, accumulate message sizes until reaching
keepRecentTokens - Generate summary: Use LLM to create structured summary:
- Create compaction entry: Append to session with summary and
firstKeptEntryId
packages/coding-agent/src/core/compaction/compaction.ts:705
Manual Compaction
Extension-Provided Compaction
Extensions can override compaction:packages/coding-agent/src/core/extensions/types.ts:414
Session Navigation
Tree Traversal
packages/coding-agent/src/core/session-manager.ts:1062
Labels
Label entries for navigation:packages/coding-agent/src/core/session-manager.ts:995
Branch Summarization
When branching, optionally summarize the abandoned path:BranchSummaryEntry that:
- Captures context from the abandoned path
- Gets injected as a
branchSummarymessage in the new path - Allows the LLM to understand what was tried before
packages/coding-agent/src/core/compaction/branch-summarization.ts
Session Metadata
Display Names
Set user-friendly names for sessions:Session Info
Query session metadata:packages/coding-agent/src/core/session-manager.ts:164
Session Forking
Fork sessions between projects:- Copies all entries from the source session
- Updates the
cwdto the target directory - Stores
parentSessionreference to the source - Generates a new session ID
packages/coding-agent/src/core/session-manager.ts:1292
Best Practices
Use labels for important points
Use labels for important points
Label entries before major changes:
Set display names early
Set display names early
Name sessions based on the task:
Branch instead of editing prompts
Branch instead of editing prompts
To try different approaches, branch from the decision point:
Summarize when branching
Summarize when branching
Capture why you’re changing direction:
Next Steps
Architecture
Understand the system architecture
Extensions
Build custom extensions