orca icon indicating copy to clipboard operation
orca copied to clipboard

feat(agents): pi session resume support

Open eisen0419 opened this issue 4 months ago • 1 comments

Summary

pi is a recognized TuiAgent with first-class status-extension wiring (main + SSH relay), but it was missing from RESUMABLE_TUI_AGENTS — so pi panes could not survive worktree sleep, app quit-restore, or agent hibernation: the pane came back dead and the conversation was lost.

This PR adds pi to the resumable set, following the nine existing agent precedents:

  • The generated pi status extension captures the current session via ctx.sessionManager.getSessionId() / getSessionFile() on session_start and carries session_id + session_file on subsequent hook posts.
  • The pi hook path ingests them into AgentStatusEntry.providerSession, gated on a persisted session file — ephemeral --no-session runs are deliberately not resumable.
  • RESUMABLE_TUI_AGENTS gains 'pi'; resume argv is ['pi', '--session', <session-file path>]. The path form is cwd-independent and always non-interactive (a bare id can hit pi's interactive cross-project fork prompt).
  • session_file is threaded as transcriptPath, mirroring the Claude/Codex transcript_path convention.

User-visible: pi workers now hibernate and revive with conversation context intact.

Screenshots

No visual change.

Testing

  • [x] pnpm lint — passed (41 reliability gates, localization parity checks)
  • [x] pnpm typecheck — all three tsconfigs, exit 0
  • [x] pnpm test — honest accounting: on our macOS test box the full suite shows load-induced flaky failures in unrelated renderer suites both on this branch and on clean base ef87461bf (different failure sets per run, 15–20s timeouts under load). The one deterministic failure actually caused by this PR — workspace-session-schema.sleeping-agent.test.ts used agent: 'pi' as its malformed-record sentinel, which this PR made valid — is fixed here by switching the fixture to a genuinely non-resumable sentinel. All PR-touched suites plus that schema suite pass deterministically: 170/170.
  • [x] pnpm build — full build passed (renderer, native, macOS computer-use targets)
  • [x] Added/updated tests: extension session-metadata transport, provider-session extraction edges (ephemeral / missing file / empty id), path-form resume argv, listener session_start handling, omp payload identity, hibernation-planner gating.

Additionally verified against the real pi binary (0.80.6) with a mock hook server: real runs emit session_id + session_file matching the on-disk session header; --no-session runs omit session_file; and pi --session <absolute session file> resumes with context intact from a different cwd, non-interactively.

AI Review Report

Multi-agent review pipeline (implementer, independent adversarial reviewer, separate cross-platform/security auditor — different models):

  • Adversarial review of the initial commit: 0 P1, 2 P2, 4 P3. The P2s (ephemeral sessions falsely marked resumable; bare-id resume argv reaching pi's interactive cross-cwd fork prompt) are fixed in the follow-up commit by gating on and resuming via the persisted session file.
  • Cross-platform pass (macOS / Linux / Windows / WSL): the change adds no new platform-specific code. New payload fields ride the existing endpoint-file parser (POSIX endpoint.env and Windows endpoint.cmd variants) and the existing WSL curl.exe fallback verbatim. Resume argv quoting goes through the shared quoteStartupArg (posix single-quote escaping, PowerShell quote doubling, cmd caret-escaping); a leading-dash or metacharacter-bearing path cannot escape quoting on posix/powershell. SSH remotes resume on the capture host, so no cross-host path translation occurs.
  • Noted inherited assumption (pre-existing, not introduced here): SSH connectionId implies a posix shell. Native-Windows pi under the cmd shell is an unexercised combination (likely unreachable if pi is WSL-only on Windows).

Security Audit

session_id / session_file arrive over the existing token-authenticated loopback hook channel from the managed pi process (semi-trusted source). session_id passes normalizeSessionId (length cap, leading-dash rejection, control-char rejection); session_file is control-char-checked and is only ever emitted as the quoted value of --session — no shell-injection vector through the changed paths. No new secrets, no new IPC channels; omp (which shares the extension source generator) is scoped out with a payload-identity test.

Two low-severity hardening notes from our audit, disclosed deliberately rather than patched — happy to add either if you prefer:

  • session_file is not additionally checked to be absolute, existing, or confined to pi's session root before use as resume argv; bounded by pi's own session-load behavior and the loopback token gate.
  • Re-hydration from persisted state re-validates the session id but does not re-run the control-char check on the stored transcript path (the capture path does); backstopped by spawn-time quoting.

Notes

  • Scope honesty: verified via unit/integration tests, typecheck, full build, and a real-pi-binary hook harness including cross-cwd resume. A full in-app hibernate→revive e2e was not run here (no dev-build environment on this machine); the wiring follows the existing nine-agent precedents exactly, and we're glad to run any additional verification maintainers want.
  • pi restores provider/model/thinking from its session file on resume, so the resume argv intentionally passes no model flags.

eisen0419 avatar Jul 15 '26 13:07 eisen0419

Review Change Stack

[!NOTE]

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • [ ] ▶️ Resume reviews
  • [ ] 🔍 Trigger review
📝 Walkthrough

Walkthrough

Pi hook generation now captures session_id and session_file for Pi events while keeping OMP payloads unchanged. Pi session-start handling clears cached turn state. Shared session utilities recognize Pi as resumable, extract transcript paths from session_file, and construct Pi resume arguments when metadata is complete. Metadata-only session updates now flow through server, relay, IPC, persistence, and renderer ownership handling. Tests cover hook context forwarding, payload metadata, event normalization, session extraction, incomplete sessions, hibernation planning, and malformed sleeping-agent records.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.22% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: pi session resume support.
Description check ✅ Passed The description follows the required template and includes summary, screenshots, testing, AI review, security audit, and notes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 15 '26 17:07 coderabbitai[bot]