Closes #11060. ## Problem A session stores the working directory (cwd) it was created in. If that directory no longer exists, for example after the project folder was renamed, interactive mode offered only to continue in the current directory. That choice is kept in memory only, so every later resume of the session asked again. ``` ~/proj (session X created) -> mv ~/proj ~/proj2 ~/proj2$ pi --session <X.jsonl> -> "continue in current cwd" -> works ~/proj2$ pi --session <X.jsonl> -> asks again, every time ``` ## Solution The missing-cwd prompt now offers three choices everywhere it appears: - **Continue**: unchanged behavior. Runs the session in the current cwd, not stored, asks again next time. - **Fork**: copies the session into the current cwd as a new session with a new ID and `parentSession` pointing at the original, like `pi --fork` and like `pi --session <id>` already does for sessions from another project. The original file is not modified. The copy has the current cwd in its header, so resuming it does not ask again. - **Cancel**: unchanged. The session file is never rewritten. `docs/sessions.md` states that a session records its cwd and never changes it; Fork keeps that rule. Also fixes a bug on `main`: `/import` of a session whose cwd is missing copied the file into the session directory before the cwd check, so every such import left a stray copy, and Continue then created a second copy with the same ID. ## Behavior changes Example used below: session X was created in `~/proj` (file `sessions/--home-me-proj--/X.jsonl`, header `cwd: ~/proj`). `~/proj` was renamed to `~/proj2`; pi runs from `~/proj2`. ### Strings (verbatim) | Where | Before | After | |---|---|---| | Prompt text, startup | `cwd from session file does not exist`<br>`~/proj`<br><br>`continue in current cwd`<br>`~/proj2` | `Session cwd does not exist: ~/proj` | | Prompt text, TUI (`/resume`, `/import`, `ctx.switchSession`) | `Session cwd not found`<br>`cwd from session file does not exist`<br>`~/proj`<br><br>`continue in current cwd`<br>`~/proj2` | `Session cwd does not exist: ~/proj` | | Options, startup | `Continue`, `Cancel` | `Continue in ~/proj2 (asks again next time)`, `Fork into ~/proj2 (new session ID, original kept)`, `Cancel` | | Options, TUI | `Yes`, `No` | `Continue in ~/proj2 (asks again next time)`, `Fork into ~/proj2 (new session ID, original kept)`, `Cancel` | | Status after `/resume` Continue | `Resumed session in current cwd` | `Resumed session in ~/proj2` | | Status after `/resume` or `/import` Fork | n/a | `Forked into new session <id> in ~/proj2` | | Status after `/resume` Cancel | `Resume cancelled` | `Resume cancelled` (unchanged) | | Status after `/import` Continue | `Session imported from: <path>` | `Session imported from: <path>` (unchanged) | | Status after `/import` Cancel | `Import cancelled` | `Import cancelled` (unchanged) | | Blocked program status (OSC 7501) while the TUI prompt is open | kind `permission`, message `Session cwd not found` | kind `question`, message `Session cwd does not exist: ~/proj` | | Non-interactive error | `Stored session working directory does not exist: ~/proj`<br>`Session file: <path>`<br>`Current working directory: ~/proj2` | unchanged | ### Invocations (startup), stored cwd missing | Command | Before | After | |---|---|---| | `pi --session <path to X>` | Continue: runs X in `~/proj2`, header unchanged, asks again next launch. Cancel: exit 0 | Continue: same as before. Fork: writes copy Y (new ID, `cwd: ~/proj2`, `parentSession: <X path>`) to `--session-dir` / `PI_CODING_AGENT_SESSION_DIR` / `sessionDir` setting, else the default session directory for `~/proj2`, and opens Y; X unchanged. Cancel: exit 0 | | `pi --resume`, picking X | Same as `--session <path>` | Same as `--session <path>` | | `pi --session-id X` or `pi --session X` matching a file in this folder's session directory whose header cwd is missing (after `/import` or a manual move) | Same as `--session <path>` (Continue keeps ID X) | Same as `--session <path>` (Continue keeps ID X, Fork gives a new ID) | | `pi -p ...`, `pi --mode rpc ...` | Error, exit 1 | Unchanged | ### Invocations (startup), stored cwd exists or other cases (all unchanged) | Command | Behavior | |---|---| | `pi --session <path>` / `--resume` for a session whose cwd exists | Opens it in its stored cwd, no prompt | | `pi --session X` (ID) when X is only in another project's session directory | `Session found in different project: ~/proj` / `Fork this session into current directory? [y/N]`; the missing-cwd prompt is not reached | | `pi --fork X` | Forks without a prompt | | `pi -c` | Runs the most recent session of this folder in the launch cwd; header cwd not checked | ### TUI, stored cwd missing | Action | Before | After | |---|---|---| | `/resume` -> pick X, Continue (old: Yes) | Switch to X in `~/proj2`, not stored | Same | | `/resume` -> pick X, Fork | n/a | Copy Y saved in the current session's directory (like `/new`), switch to Y. X unchanged | | `/resume` -> pick X, Cancel (old: No) | `Resume cancelled` | Same | | `/import <file>`, Continue (old: Yes) | Switch to the imported copy in `~/proj2`, not stored. Bug: two copies with the same ID (`s.jsonl`, `s-1.jsonl`) | Same, exactly one copy `s.jsonl` | | `/import <file>`, Fork | n/a | Like `pi --fork <file>`: one copy with a new ID in the current session's directory, switch to it | | `/import <file>`, Cancel (old: No) | `Import cancelled`, but `s.jsonl` already copied (bug) | `Import cancelled`, no copy left | | `/fork`, `/clone` after Continue | Clone or later-message fork runs in the missing `~/proj` | Unchanged (known limitation, not addressed here) | | `/fork`, `/clone` after Fork | n/a | Stay in `~/proj2` (the copy's header has `~/proj2`) | ### TUI, stored cwd exists (all unchanged) | Action | Behavior | |---|---| | `/resume` -> pick a session from `~/other` | No prompt; pi switches into `~/other` | | `/import` of a file whose cwd `~/other` exists | No prompt; copied next to the current session, runs in `~/other` | ### Extensions | Case | Before | After | |---|---|---| | `ctx.switchSession(X)` in interactive mode, cwd missing | Yes/No prompt; Yes switches to X in the current cwd | Three-option prompt. Continue: switches to X, `withSession` gets X. Fork: switches to copy Y, `withSession` gets Y. Cancel: `{ cancelled: true }` | | `ctx.switchSession(X)` in print / RPC mode, cwd missing | Throws `MissingSessionCwdError` | Unchanged | | Events on Continue | `session_before_switch` (`resume`, target X) before the prompt, `session_before_switch` (`resume`, target X) on retry, `session_shutdown`, `session_start` (`resume`) | Unchanged | | Events on Fork | n/a | `session_before_switch` (`resume`, target X) before the prompt, `session_before_switch` (`resume`, target Y), `session_shutdown` (`resume`, target Y), `session_start` (`resume`). Reported as a resume, as `/import` reports its copy. No `session_before_fork` | | Extension cancels `session_before_switch` for Y | n/a | Y is deleted, `{ cancelled: true }` | | Events on `/import` with cwd missing | First `session_before_switch` target `s.jsonl`, retry target `s-1.jsonl` (stray-copy bug) | Both target `s.jsonl` | ### SDK / API No public API changes. `AgentSessionRuntime.switchSession({ cwdOverride })` and `importFromJsonl(path, cwdOverride)` are unchanged; `importFromJsonl` no longer leaves a copy behind when it throws `MissingSessionCwdError`. ## Docs `docs/sessions.md`: the paragraph "Pi does not store that choice, so it asks again the next time you open the session" is replaced by a description of Continue, Fork, and Cancel, including where the fork is stored (startup: like other new sessions for the current directory; `/resume` and `/import`: next to the current session, like `/new`). ## Tests - `test/interactive-mode-resume-missing-cwd.test.ts` (new): `/resume` with Fork (copy location, header, original unchanged, status), Continue (no file written), extension veto (copy removed), Cancel. - `test/suite/agent-session-runtime.test.ts`: import with a missing cwd leaves no copy, and the retry with a cwd override produces no `-1` file. - Manually checked in tmux: startup Continue and Fork, `/resume` Continue and Fork, `/import` Fork.
5.8 KiB
Sessions and Context
Pi saves a conversation as a session. The active branch of that session supplies conversation history for the next model request. Use session commands to continue work, explore another branch, or reduce the amount of history sent to the model.
Continue or switch sessions
Pi saves sessions automatically unless you start it with --no-session.
pi --continue
pi --resume
--continue opens the most recent session for the current directory. --resume opens the session picker. In interactive mode, /resume opens the same picker and /new starts a new session.
Each session records the working directory it was created in and never changes it. Pi runs a session in its working directory, so tools, @path references, project configuration, and context files resolve from there.
| Entry point | Working directory |
|---|---|
--continue |
The current directory |
--resume, /resume, --session, /import |
The session's working directory, which can differ from the current directory |
--fork, or a --session ID from another directory that you choose to fork |
The current directory, in a new session |
If a session's working directory no longer exists, for example after the project folder was renamed, interactive mode asks how to open it:
- Continue runs the session in the current directory. Pi does not store that choice, so it asks again the next time you open the session.
- Fork copies the session into the current directory as a new session with a new ID, like
--fork. The original stays unchanged. At startup the copy is stored like other new sessions for the current directory; from/resumeor/importit is stored next to the current session, like/new. - Cancel leaves the current session open, or exits at startup.
Other modes exit with an error.
Use /name or --name to assign a recognizable session name. Run /session to verify the current session file, ID, message count, token usage, and cost.
The session picker lets you search, rename, and delete sessions. It can also show paths, change sorting, and limit results to named sessions. See Keybindings for its shortcuts.
Choose how to branch
Pi stores entries as a tree, so returning to an earlier point does not erase the branch you leave.
| Action | Result | Use it when |
|---|---|---|
/tree |
Moves within the current session file | Related alternatives should stay together |
/fork |
Creates a new session from an earlier user message | The alternative should become separate work |
/clone |
Copies the active branch into a new session | You want a separate copy of the current state |
In /tree, select a user message to put its text back in the editor. Edit and submit it to create another branch. Selecting an assistant response or another entry continues after that entry with an empty editor.
When you leave a branch, Pi can summarize it and attach that summary to the branch you enter. This preserves relevant work from the abandoned path without including every message from it.
For the persisted tree and entry types, see Session Format.
Manage conversation context
The model receives the active branch, not every branch in the session file. Pi combines that history with the system prompt, discovered context files, available tools, and loaded skill descriptions. How Pi Works describes how those inputs are assembled.
The footer shows current context usage. When the active context approaches the model's limit, Pi normally compacts older history automatically. Compaction adds a summary and keeps recent messages. It does not delete the original session entries.
Run /compact to compact manually. You can add instructions when the summary should preserve a particular topic or decision. Configure automatic compaction and retained history through Settings.
Compaction can fail if the provider is unavailable or cannot accept the summarization request. Correct the provider problem and run /compact again. Disabling automatic compaction does not disable the manual command.
See Compaction Reference for thresholds, retained boundaries, branch-summary behavior, and extension hooks.
Control session storage
By default, Pi stores sessions under ~/.pi/agent/sessions/, grouped by working directory. Use --session-dir, PI_CODING_AGENT_SESSION_DIR, or the sessionDir setting to choose another location. The CLI option has highest precedence.
Use --no-session for an ephemeral run. An ephemeral session cannot be resumed after Pi exits.
Export or share a session
Use /export to write the current session as HTML or JSONL. Use /share to upload it and get a viewer link. By default, Pi uses a Radius artifact when Radius authentication is configured; otherwise, it uses a private GitHub gist. Set sessionShareDestination to gist or radius to force one destination.
Review exported or shared sessions first. They can contain prompts, model responses, tool arguments, command output, file contents, and extension messages.
Report a bug
Run /bug [description] to prepare a private report for the Pi developers. You can include the session transcript, omit it, or ask the current model to summarize the problem. Review any transcript or generated summary because it can contain sensitive conversation data.
The report includes environment and provider configuration without credential values, plus recorded error diagnostics. Upload it through radius.pi.dev or export the same report as a zip to inspect and share yourself. Uploads do not require a login; Radius authentication attributes the report to your account so the developers can follow up. If an upload fails, Pi offers to export the zip.