omp packages

pi-agent-handoff0.1.1

Agent-initiated session handoff for the Pi coding agent: the agent writes a self-contained brief, /new-equivalent session swap happens on its own, and the brief kicks off the replacement session.

xuqianhui Published 7 days ago MIT ~545 downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

pi-agent-handoff

npm ci license

Agent-initiated session handoff for the Pi coding agent: the agent writes a self-contained brief, a /new-equivalent session swap happens by itself, and the brief kicks off the replacement session.

pi install npm:pi-agent-handoff

Then the agent can call session_handoff (or you can type /handoff-new <brief>).


Why

Long autonomous loops accumulate context until reasoning degrades. /compact keeps the same session and summarizes history; sometimes the cleaner move is to start over with a brief — the work is scattered, or the next chunk wants a clean context to fan out fresh subagents.

/new is a human command. Tools get an ExtensionToolContext, which has no newSession, and Pi refuses to replace a session while the agent is running. Pi's own examples/extensions/handoff.ts generates a brief and then stops in the editor for a human to submit.

This package closes that loop: the agent decides, the swap is automatic, and the new session starts working immediately.

What it does

Tool session_handoff({ brief, goal? })

  1. Validates the brief (self-contained by size, TUI mode, one handoff per run, 15 s debounce) and persists it to state/agent-handoff/briefs/.
  2. Ends the run (terminate: true).
  3. On agent_settled the extension dispatches its own /handoff-new command with pi.sendUserMessage(..., { expandPromptTemplates: true }).
  4. The command calls ctx.newSession({ parentSession, withSession }); inside withSession it sends the brief to the replacement context, so the new session immediately runs one turn with the brief as its first user message.
  5. Lineage lands in the new session header (parentSession), so --resume can walk back.

Command /handoff-new [brief text] — human entry point; no argument reuses the brief the model just wrote.

Skill agent-handoff — when to hand off, how to write a brief that survives the swap, and what the swap drops.

Model-visible discovery: the tool carries a promptSnippet and guidelines, and the package ships the skill, so agents in any project find it without being told.

Configuration

$PI_CODING_AGENT_DIR/agent-handoff.json (default ~/.pi/agent/agent-handoff.json), or point PI_AGENT_HANDOFF_CONFIG at a file. /reload picks up changes; a restart is not needed.

{
  "switchPolicy": "ask",
  "switchPromptTimeoutMs": 180000,
  "switchTimeoutAnswer": "take",
  "minBriefChars": 40
}
Field Default Meaning
switchPolicy ask ask keeps the pi-subagents select for human switches; take / leave / stay answer it without a prompt. Handoffs themselves always travel as take.
switchPromptTimeoutMs 180000 Timeout for that select in TUI too (0 = wait forever, the pre-patch behavior).
switchTimeoutAnswer take What a timed-out select means.
minBriefChars 40 Lower bound for a brief; the upper bound is 32 KiB.

The pi-subagents bridge

If a session switch has outstanding background subagent work, pi-subagents asks what to do with it ("Stay here" / "Switch and take the work" / "Switch and leave the work here"). Only RPC mode gets a timeout, so in TUI an unattended switch — exactly what a handoff is — blocks forever.

An upstream option is proposed and implemented in nicobailon/pi-subagents#3382 / #3383 (config sessionSwitch.policy / promptTimeoutMs / timeoutAnswer, defaults unchanged). Until it ships, this package ships an idempotent patcher:

pi-agent-handoff-patch            # apply (no-op when already patched)
pi-agent-handoff-patch --check    # 0 = patched, 1 = unpatched, 2 = pi-subagents not installed
pi-agent-handoff-patch --revert   # restore the backup

It patches the installed pi-subagents bundle so the guard honors PI_SUBAGENTS_SWITCH_ANSWER, PI_SUBAGENTS_SWITCH_POLICY, PI_SUBAGENTS_SWITCH_PROMPT_TIMEOUT_MS and PI_SUBAGENTS_SWITCH_TIMEOUT_ANSWER, which this extension keeps in sync with agent-handoff.json. Backups are kept under state/agent-handoff/pi-subagents-backup/.

pi install npm:pi-subagents rewrites node_modules and therefore wipes the patch. Re-run pi-agent-handoff-patch afterwards; the extension warns at session start when it sees an unpatched guard. Once upstream ships the option, the patcher becomes unnecessary.

Security and limits

  • Session replacement only happens in interactive (TUI) sessions; print and RPC modes get a clear error.
  • Subagent child processes never register the tool: a child must never replace its parent's session.
  • Briefs are written to disk — treat state/agent-handoff/briefs/ as sensitive (they contain paths, and whatever the model decided to quote).
  • A handoff drops this process's scheduled continuations and foreground subagent bindings. Background async subagents follow the work when the bridge above allows it.
  • The tool cannot be undone: the previous session is not resumed automatically (it stays on disk and in --resume).

Development

npm install
npm test          # fake-host unit tests, no Pi process needed
npm run check     # the extension module imports cleanly
npm run pack:dry
npm run validate  # all three

Layout: extensions/pi-agent-handoff.ts (tool + command + config bridge), bin/pi-agent-handoff-patch (pi-subagents bridge), skills/agent-handoff/ (model-facing guidance), test/ (fake-host harness).

License

MIT