fix(pi-plugin): support pi-web multi-session and RPC hosts

Merged
EL
3mo ago
+3273-516

Summary

This PR adapts the Pi plugin for pi-web, where multiple Pi sessions share one persistent Node.js process and commands run through Pi’s RPC mode.

It addresses shared-process session isolation, duplicate startup work, unsafe subagent CLI detection, RPC command feedback, and /ctx-dream failures before the first model turn.

Changes

Session isolation

  • Replace the process-global child-session flag with an AsyncLocalStorage<boolean> marker.
  • Update the marker through the public subagents:child:session-created and subagents:child:disposed events.
  • Suppress initialization only for in-process child subagents.
  • Allow independent sessions in the same pi-web process to initialize normally.
  • Remove child lifecycle listeners during session_shutdown to prevent stale handlers after reloads.

Process-wide startup maintenance

  • Claim deferred startup maintenance once per process.
  • Schedule the legacy-memory identity rekey introduced by database migration v22 only once per process.
  • Schedule the session-to-project mapping backfill only once per process.
  • Load session history only after acquiring the durable backfill lease, avoiding redundant JSONL scans.

Safer subagent CLI detection

  • Reuse process.argv[1] only when it identifies a supported Pi CLI; otherwise use the packaged executable, bundled CLI, or PATH fallback as appropriate.
  • This prevents embedded hosts such as Next.js from accidentally launching another web server when creating a subagent.

RPC command presentation

  • Preserve model-invisible command entries while presenting them through the RPC UI.
  • Show short progress updates through ctx.ui.notify.
  • Show formatted results in modal dialogs through ctx.ui.custom.
  • Apply this behavior to /ctx-status, /ctx-embed, /ctx-recomp, /ctx-session-upgrade, and /ctx-dream.

Dreamer registration

  • Track Dreamer registration ownership per Pi extension instance.
  • Prevent one session’s shutdown from deregistering a same-project sibling.
  • Transfer the active registration to a remaining worktree owner when necessary.
  • Synchronize the current project’s Dreamer registration immediately before a manual /ctx-dream run.
  • This allows /ctx-dream to work before the first before_agent_start event.

Verification

  • bun run --cwd packages/pi-plugin build
  • bun run --cwd packages/pi-plugin test — 804 passed, 0 failed
  • bun test packages/plugin/src/features/magic-context/session-project-backfill.test.ts — 9 passed, 0 failed
  • git diff --check

Regression coverage includes:

  • independent same-process sessions versus child-session suppression
  • lifecycle listener cleanup during shutdown
  • process-wide startup maintenance claimed through full runtime initialization
  • lazy backfill lease gating without session-history reads
  • owner-aware Dreamer registration, shutdown isolation, and ownership transfer
  • single-timer Dreamer handoff when remaining owners repeat a worktree directory
  • embedded-host Pi CLI discrimination
  • RPC notification and modal-dialog routing
  • pre-execution /ctx-dream registration synchronization

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Supports packages/pi-plugin in pi-web multi-session and RPC hosts. Old behavior suppressed all same-process inits and drained all jobs on shutdown; new behavior suppresses only in-process child subagents, drains only the shutting-down session's historian/recomp and the active owner's Dreamer jobs (cancels after ~5s), routes command output through the live RPC UI, and isolates the shared Dreamer lifecycle across plugin instances and worktrees.

New Features

  • Scopes child-session detection to lifecycle AsyncLocalStorage, unregisters lifecycle listeners on session_shutdown, and keeps agent_end synchronous.
  • Captures a session snapshot at command start, threads cancellation through recomp and memory migration, blocks late UI writes, and aborts long drains safely.
  • Shows short progress via notifications and detailed results in dialogs, with a safe fallback to notifications; applied to /ctx-status, /ctx-embed, /ctx-recomp, /ctx-session-upgrade, /ctx-wrapup, /ctx-flush, and /ctx-dream.
  • Claims process-wide startup tasks once and defers session-history reads until the backfill lease is acquired.
  • Shares Dreamer registration per project with an active owner requirement for manual runs, transfers ownership on exit, rejects stale/ownerless requests, prevents stale runs across worktrees, and stops shared timers when idle.
  • Tracks and cancels in-flight recomp/upgrade runs per session; waits only the matching session during shutdown and aborts remaining work after a short grace period.
  • Resolves the subagent CLI safely in embedded hosts by reusing process.argv[1] only when the host is the Pi CLI, preferring packaged/bundled binaries otherwise, and never spawning with a shell.

Greptile Summary

The PR adapts the Pi integration for multiple sessions sharing one RPC host process.

  • Isolates in-process child initialization while allowing independent sessions to initialize.
  • Coordinates process-wide startup maintenance and session-scoped background-job shutdown.
  • Adds owner-aware Dreamer registration, manual execution, and worktree handoff.
  • Routes command status through live RPC notifications and dialogs.
  • Safely resolves the Pi CLI when running inside embedded hosts.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

FilenameOverview
packages/pi-plugin/src/index.tsIntroduces child-context isolation, process-wide startup claims, per-session lifecycle cleanup, and owner-aware Dreamer wiring.
packages/pi-plugin/src/dreamer/index.tsAdds process-shared project registrations with explicit owners, safe timer replacement, owner-bound manual runs, and scoped in-flight drainage.
packages/plugin/src/plugin/dream-timer.tsMakes shared timer registration cleanup identity-aware so stale cleanup cannot remove a replacement registration.
packages/pi-plugin/src/pi-recomp-runner.tsTracks detached recomp and upgrade work per session with cancellation and guarded cleanup.
packages/pi-plugin/src/commands/pi-command-utils.tsPreserves model-invisible command entries while presenting status through live RPC notifications and modal dialogs.
packages/pi-plugin/src/subagent-runner.tsHardens executable resolution so embedded hosts do not accidentally relaunch their own server entrypoint.
packages/pi-plugin/src/commands/ctx-recomp.tsCaptures session state before detached execution, propagates cancellation, and defers marker and context refresh effects safely.
packages/pi-plugin/src/commands/ctx-session-upgrade.tsApplies the same snapshot and cancellation lifecycle to recompilation and memory migration during session upgrades.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  P[Persistent Pi RPC host] --> S1[Session A extension]
  P --> S2[Session B extension]
  P --> C[In-process child session]
  C --> G[AsyncLocalStorage child guard]
  G -->|Child context| N[Suppress full initialization]
  S1 --> M[Shared process maintenance]
  S2 --> M
  M --> O[Run startup work once]
  S1 --> D[Shared Dreamer project registration]
  S2 --> D
  D --> T[Active owner timer]
  S1 --> J1[Session-scoped detached jobs]
  S2 --> J2[Session-scoped detached jobs]
  J1 --> U1[Session A RPC UI]
  J2 --> U2[Session B RPC UI]

Reviews (10): Last reviewed commit: "Merge branch 'master' into fix/pi-web-co..." | Re-trigger Greptile

Context used (5)