Skip to main content

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:
Location: packages/coding-agent/src/core/session-manager.ts:27

Entry Types

Sessions contain multiple entry types:
Location: packages/coding-agent/src/core/session-manager.ts:136

Tree Structure

Sessions use id and parentId to form a tree:
The leaf pointer tracks the current position. Appending creates a child of the current leaf.

Branching

Branching moves the leaf pointer without modifying history:
Result:
Location: packages/coding-agent/src/core/session-manager.ts:1111

Session Lifecycle

Creating Sessions

Location: packages/coding-agent/src/core/session-manager.ts:1246

Session Directories

By default, sessions are stored under ~/.pi/agent/sessions/:
Each directory is encoded from the working directory path.

Appending Entries

Location: 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:
  1. Emit the summary as a compactionSummary message
  2. Emit kept messages (from firstKeptEntryId up to the compaction)
  3. Emit messages after the compaction
Location: 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:
Default settings:
  • reserveTokens: 16384 (reserve for new conversation)
  • keepRecentTokens: 20000 (keep recent messages)
Location: packages/coding-agent/src/core/compaction/compaction.ts:114

Compaction Algorithm

  1. Find cut point: Walk backwards from newest, accumulate message sizes until reaching keepRecentTokens
  2. Generate summary: Use LLM to create structured summary:
  1. Create compaction entry: Append to session with summary and firstKeptEntryId
Location: packages/coding-agent/src/core/compaction/compaction.ts:705

Manual Compaction

Extension-Provided Compaction

Extensions can override compaction:
Location: packages/coding-agent/src/core/extensions/types.ts:414

Session Navigation

Tree Traversal

Location: packages/coding-agent/src/core/session-manager.ts:1062

Labels

Label entries for navigation:
Labels appear in the tree:
Location: packages/coding-agent/src/core/session-manager.ts:995

Branch Summarization

When branching, optionally summarize the abandoned path:
This creates a BranchSummaryEntry that:
  • Captures context from the abandoned path
  • Gets injected as a branchSummary message in the new path
  • Allows the LLM to understand what was tried before
Location: packages/coding-agent/src/core/compaction/branch-summarization.ts

Session Metadata

Display Names

Set user-friendly names for sessions:
Display names appear in session selectors and lists.

Session Info

Query session metadata:
Location: packages/coding-agent/src/core/session-manager.ts:164

Session Forking

Fork sessions between projects:
Forking:
  • Copies all entries from the source session
  • Updates the cwd to the target directory
  • Stores parentSession reference to the source
  • Generates a new session ID
Location: packages/coding-agent/src/core/session-manager.ts:1292

Best Practices

Label entries before major changes:
Name sessions based on the task:
To try different approaches, branch from the decision point:
Capture why you’re changing direction:

Next Steps

Architecture

Understand the system architecture

Extensions

Build custom extensions