Workflows in code
A workflow is a JavaScript program that operates the agent harness for
you. The workflow.* API gives code many of the same controls
you use as an operator: run model turns, change posture, execute commands,
manage context, and decide when to stop.
Control the harness
The workflow automation layer gives your code access to the harness,
not just to the model. It can carry out operations you would otherwise
perform in a Pi session, inspect their results, and use those results to
decide what happens next. For example, workflow.new() starts
a new session just as entering /new does.
Run work
workflow.run(prompt, options?)runs a prompt or skill, optionally returning structured output.workflow.classify(request)asks a classifier model typed questions about JSON state.workflow.shell(command, options?)runs a shell command and returns its result.workflow.readonly(enabled)turns the readonly posture on or off.
Inspect the session
workflow.state()returns a digest of the current session.workflow.entries({ after?, limit? }?)reads compact conversation entries.
Manage memory and context
workflow.notebookIndex()lists notebook pages and their previews.workflow.notebookRead(name)reads a notebook page.workflow.notebookWrite({ name, content })writes a notebook page.workflow.notebookTopic()reads the active notebook topic.workflow.handoff(direction)hands off to a fresh context while retaining the notebook.workflow.new()starts a new session and clears the notebook.
Control the run
workflow.budget()reports run cost and remaining cost and time targets.workflow.checkpoint(metadata)marks a safe stop and supplies partial-result metadata.workflow.exit(metadata)stops the workflow and records its result.
Example: implement a spec
This example implements a spec: it finds gaps in the code, plans and applies fixes, and runs tests. We chose this “spec printing” job because turning a spec into working software is the most basic job of a software factory.
Design the workflow
Tests must check observable behavior, not implementation details: a full rewrite should pass the same suite without users noticing a difference. This is a requirement for the suite, not a guarantee the workflow can make.
First, the agent detects the test command and returns it as structured output. The workflow runs it directly as a baseline, then repeats a loop: analyze the spec and code, plan fixes, implement them, and run tests. The analysis and plan stay in the conversation for the next step. The loop runs up to its iteration limit; it does not parse the analysis to decide whether the spec is complete.
A failed test or the iteration limit stops the workflow for human review. Automatic test repair could weaken the suite or add slop. Checkpoints also let the workflow's TUI apply cost and time targets; these are soft limits, so work can continue until the next checkpoint. A cancel is not a soft stop: it aborts the active turn at once. Passing tests and model-reported findings do not prove the spec is met.
The workflow
Skills and saved prompts can set posture and model routing in their
frontmatter. For example, an analysis skill at
.pi/skills/analyze-spec/SKILL.md could use a configured
review Model Group:
---
name: analyze-spec
description: Compare a specification with the code and report gaps.
readonly: true
model-group: review
---
Read the supplied spec and compare it with the code. Report concrete gaps.
If you cannot read the spec, say so.
The workflow below also uses two project prompt templates: /detect-tests
finds the full test command (or returns an empty command), and
/plan-spec-fixes plans from the analysis. Implementation needs
no template: the workflow hands the plan to a successor context. Configure
the templates' frontmatter for readonly detection and planning. The planning
template must leave files unchanged when the spec cannot be read or there is
no actionable plan.
Save this file as .pi/workflows/implement-spec.js. The agent
selects a test command and the workflow executes it without asking for
approval. Run it only in a trusted repository where you accept agent
edits and shell commands. Verify that the selected command runs the
appropriate suite.
return defineWorkflow((workflow) => {
// The run argument names the spec. A missing argument is a caller mistake,
// so the workflow exits before it spends any model or shell work.
const MAX_ROUNDS = 3;
const spec = load("intent").trim();
if (!spec) return workflow.exit({ reason: "spec-path-missing" });
// Detection answers through the output tool, so the schema turns the answer
// into a value the workflow can trust. Its frontmatter keeps the turn readonly.
const detection = workflow.run(
"/detect-tests",
{
output: {
schema: {
type: "object",
additionalProperties: false,
required: ["command"],
properties: { command: { type: "string" } },
},
},
},
);
const command = detection.output.command.trim();
// No command means no round can be verified, so stop before editing anything.
if (!command) return workflow.exit({ reason: "test-command-not-found" });
// Tests may write artifacts, so drop the detection turn's readonly posture.
// A red baseline stops for a human before any round spends budget on fixes.
workflow.readonly(false);
const baseline = workflow.shell(command);
if (!baseline.ok) {
return workflow.exit({
reason: "baseline-tests-failing",
command,
output: baseline.output,
});
}
// A checkpoint is the only safe exit point: a pending cost or time stop lands
// here and salvages this phase with the resolved command.
workflow.checkpoint({ phase: "baseline-passed", command });
// Each round starts from a fresh session so earlier rounds do not bias the
// analysis. Cost stays cumulative, and the loop bound keeps spending finite.
for (let round = 1; round <= MAX_ROUNDS; round++) {
// A fresh context keeps this round blind to the previous round's verdicts
// and repairs, so the analysis starts unbiased.
workflow.new();
// The skill's frontmatter selects a random model from the review group. The
// analysis stays in this fresh context for the plan turn to read.
workflow.run("/skill:analyze-spec", { input: spec });
// Salvage point: a soft target that lands here reports the inspected phase.
workflow.checkpoint({ phase: "inspected", round });
// Planning frontmatter keeps this turn readonly: it writes the plan to the
// conversation, never to the tree, so a bad plan cannot touch files.
workflow.run("/plan-spec-fixes");
// Handoff keeps the context small: the plan runs in a successor context
// instead of piling onto the analysis. Execution needs writes, so drop the
// readonly posture the planning turn left behind.
workflow.readonly(false);
workflow.handoff("apply the planned fixes");
// The handoff settles before this call, so the edits are on disk. Run the
// same suite directly: a model-written test could weaken the suite.
const tests = workflow.shell(command);
if (!tests.ok) {
return workflow.exit({
reason: "tests-failing",
round,
command,
output: tests.output,
});
}
// Salvage point: a soft target that lands here reports the tested phase.
workflow.checkpoint({ phase: "tested", round, command });
}
// Stop at the cap and leave the result for human review. The workflow never
// claims the spec is met on its own.
return workflow.exit({ reason: "iteration-limit", rounds: MAX_ROUNDS });
}); Run the example
Install pi-schematic using the setup guide, configure the skill and prompt templates, and start pi in the repository. Pass the path to an existing spec that the agent can read:
/workflow implement-spec specs/checkout.md
The argument is available through load("intent"). When the
workflow stops, inspect the exit reason and changes. Shell output is
visible to you, but is not automatically sent to the model.
For the full API, target behavior, and limitations, see the workflow author contract.