omp packages

pi-attach0.3.10

Subagents for Pi you can attach to — live transcripts, steering and resume, drawn by Pi's own TUI.

shahidshabbir-se Published 3 hours ago MIT ~2.5K 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-attach

Real Pi subagents, right inside your terminal. Delegate work, switch into an agent's own Pi session, and follow its progress without losing the Agent List. Isolate writing tasks on their own git branches when you need to.

pi-attach demo: delegate a task, attach to it, steer it, and read the delivery

pi install npm:pi-attach

Then ask Pi to delegate or type @ and choose an agent:

@explore map the files in src and explain how they fit together

Why another subagents package

pi-attach treats a Subagent as a Pi session you can walk into. Each interactive child runs its own Pi TUI. Attach to see its live screen in the main area while the Agent List stays below; switch agents without stopping their work.

  • Delegate in parallel. Watch live status, elapsed time and tokens; nested work appears in the tree.
  • Navigate and steer. Use ↑/↓ and Enter to select a session, then type into the child. Select main and press Enter to return.
  • Find finished work. /agents reopens live and completed tasks; deliveries show the result and any worktree branch.
  • Choose an agent. Type @ for bundled and user-defined agents. The parent writes the delegated prompt from your request.
  • Control the environment. Opt into worktrees, select model roles and set per-agent limits, skills and MCP connections.

The Agent List has focus when a child opens. Esc first leaves the list; with a running child focused, Esc stops it. ↓ on the child's empty editor returns to the list on main, and ↑ on main goes back to the child's input. x stops another selected live Task, but on the row already on screen it is typed into that child instead. Ctrl-] returns to main without stopping it. If the inline mirror is unavailable, pi-attach uses a full-screen handoff; if PTY support is unavailable, it falls back to the RPC transcript view.

Writing agents can also work in isolated git worktrees, each on its own branch. The delivery reports the changes and a merge command, so you can review and integrate the work when you're ready. Worktrees isolate the working directory, not absolute-path writes; they are not a sandbox.

No tmux or extra panes. PTY support and mouse behavior depend on your terminal and platform; the RPC fallback keeps keyboard navigation available.

pi-attach
Transport one real interactive pi per Task on a PTY; RPC fallback when PTY is unavailable
Navigation the child's real TUI fills the main area; the Agent List keeps focus, ↑/↓ select, Enter switches, Esc blurs then stops a focused child
Steering type in the child's view; steer or queue
Resume --session <file> on the finished child
Sessions child sessions are real Pi sessions in the Parent's session directory, nested under it in /resume; open via the Agent List to keep the agent's launch limits
Rendering the child's own Pi screen inline (full-screen fallback), or Pi's components reconstructed from RPC events
Worktrees Isolated checkout and branch per writing Task; delivery includes changes and a merge command
Limits maxTurns, effort, skills, mcpServers, hooks, memory, context per agent; reasoning per Task

Tools

The parent model gets one tool:

task({ agent: "explore", name?: "List ADR files", prompt: "…", model?: "fast", isolate?: true })
task({ resume: "sa-…", prompt: "Continue from your findings and deliver the missing file." })

name labels the Task in the Agent List and its delivery. model asks for a declared role such as fast; leave it out unless the Task needs something different. isolate: true runs the Task in its own git worktree.

Concurrency is capped (default 4) and the rest queue. x in the Agent List stops a Task. Closing Pi stops its children.

If a finished Task has a saved session, its owner can resume it with the same handle and a follow-up prompt. Resume keeps the saved session and launch options. Close any open viewer of that session first.

Agents

Two definitions ship inside the package:

  • explore — read-only search and reading (read, grep, find, ls, bash)
  • general-purpose — full tool access for multi-step work

Drop ~/.pi/agent/agents/<name>.md to override one by name, or to add your own. The current roster, with each agent's role and model, is part of the task tool description, so the parent knows what it can delegate to.

Type @ in the editor to pick one of these agents. Choosing @explore sends your whole message to the parent, which delegates to that agent and writes the subagent's prompt itself. File mentions still work the same way.

Children inherit your installed extensions by default, so guardrail extensions still ask you before doing something risky — the dialog names the agent that triggered it. An agent can opt out with extensions: none.

Worktrees

Tasks run in the Parent's directory by default. Request a separate checkout with isolate: true, or set worktree: true in an agent definition. Worktree isolation requires a repository with at least one commit.

Ran in worktree pi-attach/write-hello-b8bxzy (/home/you/.pi/agent/worktrees/sa-b8bxzy).
Changes: 1 file changed, 1 insertion(+)
Merge with: git merge pi-attach/write-hello-b8bxzy

The checkout lives under Pi's agent directory (~/.pi/agent/worktrees/), so no project tree is ever touched, and worktree.dir moves it; the branch is kept, so nothing is lost. It is a working directory, not a sandbox: a Task that is handed an absolute path can still write there. worktree.cleanup decides whether a directory survives a Task — when-clean keeps one that has changes, always commits the leftovers to the branch and reclaims it. A Parent that dies leaves its worktrees behind, so starting Pi in a repository reaps the orphans, committing their edits to the branch first. worktree.enabled: false turns the whole feature off.

Limits

An agent file can bound how a Task runs, and every limit is checked before the child starts:

---
name: reviewer
tools: read, grep, bash
worktree: true
maxTurns: 30
effort: high
skills: [skills/review]
mcpServers: [sentry]
memory: [notes/project.md]
context: none
---

agentLimits in config overrides the file; a bad level or a missing path fails the Task, naming the field, rather than silently doing nothing.

mcpServers is connection-level: the child gets a mirrored agent directory whose mcp.json holds only the servers the agent named, with auth, settings and extensions symlinked through, so unwanted servers never connect.

Nesting

nesting.maxDepth (default 2) allows one level of nested Subagents and withholds task at the limit. A child's Tasks appear as nested rows under it:

❯ general-purpose  Nested check (+1)        running · 21s
  └ ● Explore  List files                   running · 15s

A child that delegated is held open until its follow-up turn carries the nested result, so the answer that comes back is the child's real answer, not its first "delegated" line.

Nested rows are selectable under the PTY transport. The Parent does not own the nested PTY, so it asks the child that does to open the row; the nested Task then fills the same composed screen, with the Parent's list still below. The child's own Agent List is hidden while it is composed, so the tree appears once. x on a nested row stops that nested Task through its owner, not the owning child; Esc from a focused nested screen does the same. The full-screen handoff keeps the child's own list, because the Parent list is not visible there. With RPC the nested rows stay informative.

A finished nested Task stays in the list while the child that spawned it is still running, and Enter on it asks for a fresh instruction. The instruction is sent to that child, not to the root: it continues the nested Task's own session with the same model, reasoning level, depth and worktree, and its answer arrives in the child's session. The owner refuses a nested Task that is still running, has no saved session, is open in a real Pi TUI viewer, or whose previous process has not exited, so a session never has two writers. Once the child finishes, its nested rows are gone and those sessions are reachable only through Pi's own /resume. See ADR 0016.

Models

Model choice is layered, Claude-style:

  1. forceModel — one model for every Subagent
  2. the model the parent passes to task — a declared role or an exact provider/model-id
  3. agentModels[<agent>]
  4. model: in the agent file
  5. agentModels["*"]
  6. otherwise inherit the parent's model

inherit is a keyword at any layer and stops the search. task.model is strict: an unknown role, a bare id, a malformed id, or an unavailable model fails the Task before launch instead of falling back to the next layer. Role requests keep forceModel's historical precedence, but an explicit exact provider/model-id that conflicts with forceModel is an error, so a specific choice is never silently replaced. Models from your config or an agent file are validated the same way, naming the layer.

{
  "models": { "fast": "provider/cheap-model", "deep": "provider/strong-model" },
  "agentModels": { "explore": "fast", "general-purpose": "deep" },
  "budget": { "maxTokensPerTask": 200000 }
}

budget.maxTokensPerTask stops a runaway Subagent and delivers what it produced, with the reason.

Configuration

Optional. pi-attach reads the first of:

  1. $PI_ATTACH_CONFIG
  2. ~/.pi/agent/pi-attach.json
  3. config.json inside the package (development)

For example, create ~/.pi/agent/pi-attach.json:

{
  "transport": "pty",
  "concurrency": 4,
  "maxRows": 8,
  "messageMode": "steer",
  "models": {
    "fast": "provider/fast-model",
    "balance": "provider/balanced-model"
  },
  "agentModels": {
    "explore": "fast",
    "general-purpose": "balance"
  },
  "agentLimits": {
    "explore": { "effort": "low", "maxTurns": 20 },
    "general-purpose": { "effort": "medium", "maxTurns": 40 }
  },
  "budget": { "maxTokensPerTask": 200000 },
  "worktree": { "enabled": true, "cleanup": "when-clean" },
  "nesting": { "maxDepth": 2 }
}

Replace the example model IDs with models available in Pi. All keys are optional; omitted settings use the defaults below. Effort is normally configured per agent; task can override it per Task with reasoning (off, minimal, low, medium, high, xhigh, max). The level is validated against the selected model before the child starts, so Pi never silently clamps it; if the Task would inherit the Parent's model, that model is recorded so the resume uses the same one.

Key Default Meaning
key ctrl+shift+a Shortcut that focuses the Agent List
concurrency 4 Running Tasks at once; the rest queue
maxRows 8 Agent List rows before +N more
messageMode steer queue delivers a message after the current work
animation { frames: ["●"], intervalMs: 1000 } One frame means a static glyph
glyphs ◌ ✓ ✗ ■ Per-state glyphs, for icon fonts
models {} Named model roles
agentModels {} Role or model per agent; "*" is the default
forceModel unset Role or id forced onto every Subagent; role requests are forced, a conflicting per-Task exact model is rejected
budget.maxTokensPerTask unset Stop a Task that spends more than this
agentLimits {} Per-agent limits that beat the agent file; "*" is the default
worktree.enabled true Kill switch for worktree isolation
worktree.cleanup when-clean when-clean, always (commit leftovers, reclaim) or never
worktree.dir ~/.pi/agent/worktrees Where checkouts are created
nesting.maxDepth 2 How deep Subagents may delegate; 1 withholds task from Subagents
transport pty pty runs a real interactive child; rpc reconstructs its events in Pi
handoff true With transport: "pty", Enter attaches to the child (inline, or a full-screen fallback)

transport: "pty" is the default. It needs the optional node-pty, @xterm/headless and @xterm/addon-serialize packages. It starts each Subagent as a real pi TUI on a pseudo-terminal; Enter opens that child's screen in the same full conversation area the main agent uses, with the Agent List still below it. The list keeps focus, ↑/↓ move its selection, and Ctrl-] returns while the child keeps running. Esc blurs the list first; a second Esc on the focused live child stops it, unless the child is showing a dialog or esc to interrupt. ↓ on the child's empty editor returns to the list on main, ↑ on main goes back to the child's input, and x on the row already on screen is typed into that child rather than stopping it. If the mirror cannot expose a screen, the same child is shown through a full-screen handoff. If the packages are missing, or Pi is not in its interactive TUI, Tasks run over RPC instead, with the child's events reconstructed as a Pi transcript. transport: "rpc" forces that fallback and needs no native module. The inline renderer covers text, colours and cursor position; it does not reproduce images, OSC 8 links, OSC 52 clipboard writes, terminal title/bell side effects, IME cursor placement or a child's alternate-screen scrollback.

A finished PTY Task's Enter instead reopens its saved session as a fresh pi --session <file> TUI, normally inline. That viewer is a view, not a second Task: it is closed before Pi resumes, and the original Task can then be resumed normally. If the original child is still shutting down, the view waits for it rather than opening a second writer on the same session file.

Keys

Key Where Effect
↓ on an empty editor Agent List Focus the list (main from a child editor)
↑ ↓ Agent List Move the selection; ↑ on main leaves the list for the editor
Enter Agent List View the selected Task, or main to return
x Agent List Stop another selected live Task; on the row already on screen, type x into that child instead
Esc Agent List Leave the list; a live child keeps running
Enter Subagent view Steer, queue, or resume with the text
↑ ↓ Inline child view Move the Agent List selection; Enter switches, Esc blurs the list
Esc Inline child view Stop the focused live child; a finished viewer closes. Dialogs and esc to interrupt keep the key
Ctrl-] PTY view Return to Pi; a live child keeps running, a finished viewer closes
Esc Subagent view Stop a focused live child; return to the parent otherwise
ctrl+o everywhere Expand tool output, prompts and results

Documentation

Development

npm install
npm test                              # unit and boundary tests
npm run test:tui                      # drives real Pi in a PTY: list focus, Esc blur/stop, main+Enter
PI_SUBAGENTS_LIVE=1 node --test test/live-rpc.test.ts   # one real child, costs a model call

Known limits

  • The view swap depends on Pi's internal layout. It validates before touching anything, restores exactly what it replaced, and refuses with a warning otherwise — that code lives in one module and is covered by the real-TUI probe.
  • PTY views only exist in Pi's interactive TUI; RPC and print parents use the reconstructed transcript.
  • The inline child screen is an emulated viewport. Images, OSC 8 links, OSC 52 clipboard writes, terminal title/bell side effects, IME cursor placement and a child's alternate-screen scrollback are not reproduced; when the mirror cannot expose cells, Attach falls back to a full-screen handoff rather than failing.
  • The footer always describes the parent session; a child's usage is printed inside its own transcript.
  • Mouse support (click, hover, wheel) is implemented and hit-tested, but has not been observed end to end in this environment.

License

MIT