Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pi Agent Session Management

Pi Agent Session Management

Pi Agent's session management system is one of its most powerful features.

This chapter covers saving, restoring, branching, and exporting sessions in detail.


Session storage mechanism

Each conversation is automatically saved as a session file in JSONL format under ~/.pi/agent/sessions/, organized by project path.

The directory name is derived from the project's absolute path: slashes are replaced with hyphens and wrapped with a -- on each side.

The filename is a concatenation of the creation timestamp and the session UUID.

Each session is organized internally as a tree — meaning you can branch from any historical node without losing earlier conversation content.



~/.pi/agent/sessions/

└── --Users-runoob-projects-runoob-demo--/        # 目录名为项目路径,斜杠已替换为连字符

    ├── 2026-08-31T02-40-11-000Z_019fc2a1-93b4-7c22-8d5e-2b3c4d5e6f7a.jsonl    # 时间戳_UUID.jsonl

    ├── 2026-08-31T04-12-38-000Z_2c8f71ba-4a19-4e63-9d02-7f5a6b8c9d0e.jsonl

    └── 2026-08-31T08-55-02-000Z_5e3d92f4-8b26-47ac-a174-9c0d1e2f3a4b.jsonl

Internal structure of a session file

The first line of a session file is a fixed piece of metadata recording the session format version, session ID, creation time, and working directory.

This metadata has no id or parentId fields and does not participate in the conversation tree.

Starting from the second line, each line is an entry with id and parentId fields; together they form the tree structure.

Entry types include message, compaction, branch_summary, label, custom, and others.

For sessions created by /fork or /clone, the first-line metadata has an extra parentSession field pointing to the original session file.



{"type":"session","version":3,"id":"019fc2a1-93b4-7c22-8d5e-2b3c4d5e6f7a","timestamp":"2026-08-31T02:40:11.000Z","cwd":"/Users/runoob/projects/runoob-demo"}

{"type":"message","id":"m1","parentId":null,"message":{"role":"user","content":[{"type":"text","text":"帮我实现登录功能"}]}}

{"type":"message","id":"m2","parentId":"m1","message":{"role":"assistant","content":[{"type":"text","text":"好的,我用 JWT 来实现"}]}}

The session directory can also be customized; the priority from highest to lowest is: the --session-dir argument, the PI_CODING_AGENT_SESSION_DIR environment variable, and sessionDir in settings.json.


Session options at startup

Control session behavior at startup via command-line arguments:



$ pi -c

Resumed session abc123-def456 (42 messages, model claude-sonnet-4-5)



$ pi -r

Select the session to restore:

│ > abc123  快速审查      3 分钟前

│   def456  重构认证模块  2 小时前

└



$ pi --session ~/.pi/agent/sessions/--Users-runoob-projects-runoob-demo--/2026-08-30T09-15-04-000Z_019fbb5d-7a2e-7f31-b5c4-1a2b3c4d5e6f.jsonl

Resumed session 019fbb5d-7a2e-7f31-b5c4-1a2b3c4d5e6f



$ pi --session abc123

Resumed session abc123-def456



$ pi --fork abc123

Forked to session 9f2e7a01-5c48



$ pi --name "重构认证模块"

Session renamed: 重构认证模块



$ pi --no-session

Temporary session (will not be saved)


Session management commands

Once in interactive mode, the following commands handle the day-to-day management of sessions.

Command Function Use Cases
/resume Browse and restore historical sessions Switch to a previous conversation and continue working
/new Start a new session Start a brand-new task unrelated to history
/name name Set the session name Give the session an easy-to-remember name
/session View session information View the file path, message count, token usage, and cost
/tree Open the session tree browser Jump to any node in the conversation tree
/fork Fork session Create a new session from a previous user message
/clone Clone session Copy the current active branch to a new session file
/export file Export as an HTML or JSONL file Archive locally or share offline
/share Share session Upload as a GitHub Gist to generate a shareable link

/resume session picker

Enter /resume or launch with pi -r to open the session picker for the current project.

Type characters directly in the list to filter sessions; the other keys are as follows:

Operation Key Description
Filter sessions Type characters directly Filter the current project's session list in real time by the entered text
Toggle path display Ctrl+P Show or hide the working directory path of sessions
Toggle sorting Ctrl+S Cycle between sorting methods
Show only named sessions Ctrl+N Filter out sessions that have been given a name
Rename Ctrl+R Change the name of the selected session
Delete Ctrl+D Confirm and execute; if the system has a trash command, move to the trash instead of deleting directly

Session tree (/tree)

This is Pi Agent's most distinctive feature — sessions are stored as a tree, and you can branch from any historical node.

Tree structure

Here is a typical session tree structure:



├─ user: "帮我实现一个登录功能..."

│  └─ assistant: "好的,我来实现..."

│     ├─ user: "用 JWT 方案..."

│     │  └─ assistant: "使用 JWT 实现..."

│     │     └─ user: "测试通过了"  ← 当前活跃分支

│     └─ user: "还是用 Session 方案..."

│        └─ assistant: "使用 Session 实现..."

In this example, you started from the same point and explored two approaches, JWT and Session, with each approach's conversation fully preserved.

Tree browser operations

After opening /tree, use the following keys to move and select in the tree.

Operation Keyboard shortcut Description
Move up and down ↑/↓ Move the cursor among visible entries
Pager ←/→ Page up / page down
Collapse / expand Ctrl+←/Ctrl+→ or Alt+←/Alt+→ Collapse/expand a branch segment or jump
Set a label Shift+L Set a label for the selected node
Toggle timestamp Shift+T Show/hide timestamps of entry labels (labels are set with Shift+L)
Confirm selection Enter Select the current node
Cancellation Escape / Ctrl+C Exit the tree browser
Toggle filter mode Ctrl+O Cycle through filter modes (only effective inside the /tree browser)

Filter modes

Filter modes determine which entries are shown in the tree browser; cycle with Ctrl+O.

Mode Display content
default Default view, with tool call details collapsed
no-tools Hide all tool calls and results
user-only Show only user messages
labeled-only Show only labeled entries
all Show all entries

Selecting a user message fills its text into the edit area, letting you re-edit and resubmit it to create a new branch.

Selecting an AI reply or tool call jumps directly to that position, letting you continue the conversation from there.


Differences between /tree, /fork, and /clone

All three can return you to a historical position; the difference is whether a new session file is produced and how much of the conversation you can see.

Command Output Views Typical use Branch summaries One-line summary
/tree Same session file Full conversation tree Explore alternatives within the same session Optional summary Manage multiple threads of thought together for easy comparison
/fork New session file Show only user messages Start a new session from an earlier prompt None Restart from a historical point, completely independent
/clone New session file Current active branch Copy the current work before continuing None Make a copy before continuing, keeping a way back

Branch summaries

When you jump from one branch to another via /tree, Pi Agent can generate a summary of the abandoned branch for you.

This summary is attached to the new location so you know what the previous branch did:

Mode Behavior Applicable scenarios
Do not generate a summary Jump directly, keeping no information about the abandoned branch The abandoned attempt has no value worth preserving
Default summary Use the default prompt to have AI summarize the abandoned branch Only want a general idea of what was done before
Custom summary Specify the focus, and AI summarizes according to your needs Need to carry over specific conclusions, such as pitfalls you hit

You can control summary behavior in ~/.pi/agent/settings.json:

Example

{

  "branchSummary": {

    "reserveTokens": 16384,

    "skipPrompt": false

  }

}

Setting skipPrompt to true skips the "generate a summary?" question during /tree navigation.

After skipping the question, no summary is generated by default and navigation jumps directly to the target location.


Exporting and sharing sessions

Sessions can be exported to a local file or uploaded directly to generate a shareable link.

Export to HTML

An export produces a single-file page that can be opened directly in a browser, ideal for offline viewing.



# Export to default location

/export



# Export to specified file

/export ~/Desktop/my-session.html

Share as a GitHub Gist

If you don't want to upload a file, use the /share command to generate an online link.



/share

Running it creates a private GitHub Gist and generates a shareable HTML link.

Command-line export

You can export an existing session without entering interactive mode.



$ pi --export session.jsonl output.html

Exported to output.html (128 KB)

If you work on an open-source project and want to publish sessions for research purposes, you can look at the pi-share-hf tool, which can publish sessions to a Hugging Face dataset.