pi-subflow0.2.0
Dynamic multi-subagent workflows and delegation for pi: typed asks, named phases, holes, replayable runs, and a terminal monitor
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.
Reports 0
No reports yet.
Readme
pi-subflow
Subagents and workflows for pi: hand a task to a subagent with no script to write, or write a short TypeScript workflow that gives several subagents their tasks in phases, and follow all of it in the terminal.
Workflow Fix the failing checkout tests · 3m 12s · phases 3/5 · asks 11/14 · round 2
✓ Survey every changed file → ✓ Implement the fix → ✓ Check that the tests still pass → ◉ Fix the remaining failures ↺ → ○ Write the final report
◉ fixer · bash npm test -- coupon · 1m 04s ◉ reviewer · read src/checkout.ts · 42s +2: writer, scout
commands 3/3 · ↑1.2M ↓86k · $0.42 of $2.00 ctrl+o expand · /workflow
Those are the lines the panel folds back to. The one run shows its live steps from the start — a line per waiting question,
hole, live step, and queued step, with its newest log lines under them — and ctrl+o, the key pi already uses to expand a tool
result, folds it to the glance above: one key expands what the user looks at, here and in the transcript. pi keeps that key for
itself, so the extension reads the state instead of registering it, and /subflow widget expanded on|off sets the panel on its
own. A press made while no panel shows folds pi's output and leaves the form alone. While a run works the glyphs of its live
steps pulse through ◉ ◍ ◎ ◍, and the line that names a run opens with Workflow and holds still; a step that is paused, or
waits for an answer, a hole, or the provider, does not pulse. Each step reads as its name, then what it does now — the command
or tool call it runs, or the tail of what it thinks or writes between calls — then its elapsed time, and in the expanded form
its tokens; the status gives way before they do. With several runs the panel keeps one compact block each, whatever the key
says, and the monitor is where a run is opened.
The sample is illustrative. pi has no subagents of its own: pi-subflow starts them as pi child
processes, one per ask, each with its own conversation that it keeps from ask to ask. Every run
shows in a live panel above the editor, can be followed and steered in /subflow, and ends as a
card in the transcript.
Install
Requires Node >= 22.19.0 and pi >= 1.6.0.
From npm:
pi install npm:pi-subflow
From source:
pi install git:github.com/ningh123/pi-subflow
Restart pi (or run /reload), then ask for a workflow or a delegation in a normal sentence, for
example:
Review every file changed on this branch, one subagent per file, then fix what they find and run the tests.
Quick start
| Goal | How |
|---|---|
| Hand one or more tasks to subagents, no script | the model calls workflow_delegate |
| Run a multi-step plan with subagents | the model calls workflow with a script; you approve the plan first |
| Make a workflow from a sentence | /workflow <goal text>; /workflow new <name> writes a scaffold to edit |
| State a goal that has to be delivered | /goal <goal text>: the run plans the goal, carries it out, and verifies the criteria; you approve the plan first |
| Follow runs, steer or pause a subagent, stop or resume a run | /subflow opens the monitor; every subcommand is listed under Commands |
| Fold and expand the live panel of the one run | ctrl+o, the key that expands a tool result, or /subflow widget expanded on|off |
| See and author agent definitions | /agent list, /agent show <name>, /agent create <name>, /agent edit <name> |
| Check that everything works | /subflow doctor |
| Turn the live panel off | /subflow widget off |
The model has five tools in a normal turn:
workflow: a run from a TypeScript scriptworkflow_run: look at a run, wait for it, stop, pause, and resume itworkflow_steer: guide one subagent while it worksworkflow_reply: answer a subagent's question or fill a holeworkflow_delegate: hand out tasks with no script
/agent create and /agent edit also make a sixth tool, workflow_agent, available while an authoring is open. It
drafts, validates, and test-runs a definition; only you can save it.
A new workflow never starts without your approval: the terminal shows every phase, the commands it
will run, the model, the tools, and the budgets. Enter starts the run only from the end of the
summary: a press before that jumps to the end, a press in the script view returns to the summary,
and Escape rejects. A delegation runs like any other tool call. In a headless or CI session where no
one can approve a run, set subflow.autoApprove to true or the environment variable
PI_SUBFLOW_AUTO_APPROVE=1 to have new runs approved automatically.
Commands
/subflow manages the runs of a workspace; it never creates a workflow. Its subcommands:
| Subcommand | What it does |
|---|---|
/subflow or /subflow monitor |
open the monitor, or a summary where there is none |
/subflow list |
every run of this workspace, newest first |
/subflow status [<id>] |
the limits of the session, and every run or one in detail |
/subflow stop <id> |
stop a run |
/subflow resume <id> |
continue a paused run, or replay a stopped one |
/subflow pause <id> |
hold a run: nothing new starts until it is resumed |
/subflow discard <id> |
give up a stopped run for good, after you confirm it |
/subflow steer <id> <message...> |
guide the one subagent of a run while it works |
/subflow save <id> <name> |
store the script of a run as a saved workflow |
/subflow run <name> [<args>] |
start a saved workflow by name, with its approval first; args is one JSON object. A run needs a live session: one started in print mode (pi -p) stops when that process exits |
/subflow widget on|off |
show the live panel, or remove it |
/subflow widget expanded on|off |
open the panel of the one run, a line per live item, or fold it back to the glance |
/subflow doctor |
check that a run can work here |
/subflow help |
this usage |
/workflow makes a workflow; it never manages runs. Its subcommands:
| Subcommand | What it does |
|---|---|
/workflow <goal text> |
ask the model of the session to author a workflow for this goal with the workflow tool, which asks for your approval before anything runs |
/workflow new <name> |
write a scaffold of a saved workflow to edit; start it with /subflow run <name> |
/workflow list |
the saved workflows of the workspace, and the drafts of recent runs |
/workflow help |
this usage |
/goal is goal mode: state a goal, and a run is synthesized for it, with no script to write and no workflow to author. A
goal is capped at 4,000 characters, and longer text is refused before anything runs. A
planner turns the goal into tasks and acceptance criteria that anyone can check, a worker carries the tasks out one after
another, and a verifier checks every criterion against the workspace. While criteria fail, the run fixes what failed and
checks again, at most subflow.goalMaxRounds rounds of checks (3 by default). You approve the plan before anything runs,
as for every run. When every criterion holds, the run completes and keeps its verdict table as the primary goal-report
artifact; when some still fail after the last round, it ends with the error AcceptanceFailed, which names them — it
never passes in silence.
/agent lists, shows, and authors the agent definitions of the workspace:
| Subcommand | What it does |
|---|---|
/agent list |
every definition, and every file that cannot be used |
/agent show <name> |
one definition as it resolves in this session |
/agent create <name> [--scope project|user] |
open an authoring that the model drafts in |
/agent edit <name> |
open an authoring over an existing definition |
/agent remove <name> |
move the file to .trash, after you confirm it |
/agent cancel |
end an open authoring, writing nothing |
/agent disable <name> |
keep a definition from spawning |
/agent enable <name> |
let a disabled definition spawn again |
A workflow
A workflow is the body of an async function in TypeScript. It decides which subagents to start, what to tell them, and what to do with their answers.
phase("Review the change");
interface Finding {
/** Where, as `path:line`. */
where: string;
/** What is wrong, and why it matters. */
issue: string;
}
const paths = await git.changedFiles();
const findings = (
await Promise.all(
paths.map((path) =>
agent(`reviewer-${path}`, "You review one file and report only what you see.").ask<Finding[]>(`Review ${path}.`),
),
)
).flat();
phase("Fix what the review found");
const fixer = agent("fixer", "You change as little as possible and run the tests after every change.");
for (const finding of findings) await fixer.ask(`Fix ${finding.where}: ${finding.issue}`);
phase("Check that the tests still pass");
const tests = await world.run("npm", ["test"]);
if (tests.exitCode !== 0) throw new Error(tests.stderr.slice(-2000));
return { reviewed: paths.length, fixed: findings.length };
phase("…")names a step of the plan. The names are what you approve and what the panel shows.agent(name, persona)is a subagent;ask<T>(…)is one task with a typed answer, checked against a schema the compiler derives fromT.world.run,files, andgitrun commands and read the workspace. Nothing there writes: writing is a subagent's job.- A run is recorded as it goes. A stopped run resumes without repeating what finished, and a run
you change (
amend) keeps the finished work of the one it replaces.
The package ships an authoring guide for the model as a skill, so you describe the goal and the
model writes the script. skill/pi-subflow/SKILL.md is the reference for every rule.
A delegation
A delegation needs no script. One task or several, in parallel or one after the other:
workflow_delegate { action: "run", tasks: [
{ agent: "reviewer", task: "Review the diff of src/checkout.ts and report defects." },
{ name: "writer", task: "Update the changelog.", worktree: true, timeout_ms: 600000 },
] }
A task can name an agent definition (agent), give a persona, MCP, a git worktree of its
own, a working directory (cwd), a time limit for the whole task, retries included (timeout_ms),
a report file (output), a schema for a structured answer (output_schema), and a label. A task
cannot name a model: a subagent runs on the model of the session, or the one its agent definition
names. By
default the subagent starts from an empty conversation; context: "fork" starts it from a copy of
the conversation that issued the task. A subagent that is unsure asks, up to three times per ask;
the question reaches the main agent, which answers it with workflow_reply.
Agent definitions
An agent definition is a Markdown file: frontmatter, then the system prompt. Put it in
.pi/agents/<name>.md of the project (read when the project is trusted) or in
agents/<name>.md of the agent directory (~/.pi/agent by default).
---
version: 1
name: reviewer
description: Reviews a diff for defects and reports them as path:line
whenToUse: Before a branch is merged
tools: read, grep, bash
model: provider/model
thinking: high
mcp: false
maxToolCalls: 0
---
You review diffs for defects. You read the whole change before you judge it, and you report
every finding as `path:line: what is wrong and why`. You never edit a file.
A name written out in a script, such as agent("reviewer"), refers to that file; the run does not
start until it exists. A persona written in the script needs no file. /agent create <name> and
/agent edit <name> open an authoring in which the model drafts the file and you review, edit,
test-run, and save it; /agent remove, /agent disable, and /agent enable manage it afterwards.
docs/agent-creation-proposal.md is the authoring reference.
Settings
Settings live under subflow in pi's settings file, merged over defaults. These are the ones you are
most likely to change; the complete list and defaults are in settings.ts:
| Key | Default | Meaning |
|---|---|---|
maxConcurrentSubagentsPerRun |
5 |
Subagent tasks one run may run at once; the session setting caps it |
maxConcurrentSubagentsPerSession |
5 |
Subagent tasks every run of the session may run at once together: the hard cap |
childTools |
read, bash, edit, write, grep, find, ls |
The tools of a subagent; powershell is added on Windows |
childExcludeTools |
none | Tools a subagent never gets |
childMcp |
false |
Connect the configured MCP servers for subagents |
childExtensions |
"none" |
"user" also loads discovered extensions in subagents |
subagentModel |
the session model | Default model of a subagent, as provider/model |
widget |
true |
Show the live panel above the editor |
autoApprove |
false |
Approve new runs where no user can be asked; PI_SUBFLOW_AUTO_APPROVE=1 is the same |
stallMinutes |
10 |
Flag a subagent silent for this long |
goalMaxRounds |
3 |
Rounds of verify/fix checks in a goal run; a whole number from 1 to 8 |
resumePolicy |
"ask" |
ask, auto, or discard interrupted runs at session start |
confirmDelegate |
false |
Ask before a delegation starts |
retentionDays |
14 |
Delete finished runs after this many days |
storageDir |
empty (means <agentDir>/subflow, normally ~/.pi/agent/subflow) |
Where runs are stored |
A subagent starts from an empty conversation, never sees yours, has no MCP servers unless asked
for, and loads no discovered extension unless childExtensions is "user".
A subagent runs on the model of the session unless the script persona or an agent definition file
names one, and the subagentModel setting is the only global override. No tool takes a model
parameter — not workflow, not a task of workflow_delegate — on purpose: the model that calls a
tool cannot name a subagent model at call time, so no run quietly spends your balance on a model
you did not choose.
For other extensions
pi-subflow publishes what it does on pi's event bus (pi.events) and answers requests on the same
bus, in process, with no socket and no HTTP endpoint:
| Channel | Carries |
|---|---|
pi-subflow:ready |
The session and its capabilities, after startup and after /reload |
pi-subflow:run |
A run started, changed phase, or settled |
pi-subflow:child |
An ask started, changed activity, or finished |
pi-subflow:usage |
Usage of an ask, after each assistant turn |
pi-subflow:waiting |
A run waits on the main agent |
pi-subflow:jobs-changed |
The active set, a phase, or a step graph changed; an invalidation hint that carries only version and sessionId |
pi-subflow:rpc:v1:ready, pi-subflow:rpc:v1:request, pi-subflow:rpc:v1:reply:<id> |
ping, status, cost, spawn, update, steer, stop, pause, resume, discard |
Every payload carries version: 1. A new run asked for over RPC needs the same approval as one
started by the model. The channel names, payload types, and RPC methods are declared in
integration/events.ts and integration/rpc.ts.
How it works
- A script is compiled with the TypeScript compiler against a small facade, checked by rules that keep it replayable, and run in a QuickJS sandbox with no access to the file system or the network. Everything it does goes through the host, which records it.
- An ask starts a
pichild in RPC mode with the tools you allow, or continues the session of a named subagent. Its answer is validated, recorded, and handed to the script. - Runs live under
~/.pi/agent/subflow/projects/<project>/, saved workflows under.pi/subflows/of a project orsubflows/of the agent directory.
/subflow doctor checks that the pi child, the sandbox, the compiler, git, the storage
directory, and the stored runs of the project all work, warns about settings it could not read and
agent definitions it cannot use, and names the fix for whatever does not.
Known limits
- Tested on macOS and Linux. Ending a timed-out command together with the processes it started is
written for Windows (
taskkill /T) but has not been run there. - A fresh subagent (
context: "fresh", the default) cannot see the conversation that started it, by design: write what it needs into the task.context: "fork"starts it from a copy instead. - The session's
maxConcurrentSubagentsPerSessionsetting caps how many subagent tasks run at once, 5 by default; a run'smaxConcurrentSubagentsPerRuncan lower it. Raising the session setting raises the hard cap.
Documentation
- docs/agent-creation-proposal.md: the reference for authoring agent definitions
- skill/pi-subflow/SKILL.md: the authoring guide the model reads when it writes a workflow
- The behavior of each module is documented in its JSDoc and pinned by the tests in
__tests__/; the terminal mockups live with the display test in__tests__/fixtures/display-mockups.md.
Releasing
Pushing a vX.Y.Z tag runs the Publish workflow: it checks the tag against package.json, publishes to npm
with provenance, then cuts the GitHub Release. No npm token is stored: the registry verifies the workflow's
OIDC identity against the trusted publisher configured for the package.
License
MIT