Plan: helper prompts state the role first; the shared paragraphs live in one block

Closes: #896. Relates to: #884 (the audit), #895, #939. Branch: refactor/helper-prompt-order. Status: Done (2026-11-27) — all eight gates passed on 156 lanes; result on #896

Context

Ruled by the operator (2026-11-22): role first, rules next, output contract last; the four paragraphs kept in one shared block; measured on the battery before merging. Where the block lives and the measurement were agreed in conversation (2026-11-27). What the tree says today, by reading:

  • packages/pi-agents/agents/Explore.md, Research.md and general-purpose.md each open with the same four paragraphs: batching ("Independent reads, greps, finds and counts"), the yield instruction with the result’s shape, the error form ("When the work could not be done"), and the turn budget ("When you are told your turn budget is nearly spent"). The role sentence follows them. The copies differ only in the shape each shows and the fields the budget paragraph names; general-purpose’s batching paragraph also carries two rules of its own.

  • Research’s rule "Quotations must be MECHANICALLY verified" sits after "Terse, structured output".

  • pi-agents rewrites each file as it seeds it: seedAgents (packages/pi-agents/index.ts) reads the packaged file, renderAgent sets its model: and max_turns: lines, and placeAgent writes it under the agent directory, which is the only place pi-subagents reads.

  • The packaged files' other readers: the battery’s task loader (resolvePrompt in tools/model-battery/lib/task.mjs, for a prompt.agent task), and tests: packages/pi-agents/test/contract-drift.test.ts; seeding.test.ts, model-override.test.ts and turns-override.test.ts, which hold a seeded file byte-identical to its packaged one (through readFileSync of the packaged directory, or turns-override.test.ts’s `packagedText); and tools/model-battery/test/explore.test.mjs, which reads Explore.md raw in two tests (its body "ends by calling yield"; no battery source copies a line of it). packages/pi-modes/test/subagent-gate.test.ts reads the files' frontmatter only and lists *.md, so it is unaffected.

  • The battery measures two of the three: explore runs Explore, and coder-helper (#895) runs general-purpose. Research needs the web and has no task.

Design

The shared block

packages/pi-agents/agents/shared/finish.md, no frontmatter, exactly this:

Independent reads, greps, finds and counts: batch them in one `codemode`
script (`Promise.allSettled`) and filter the output there, instead of one
call per turn. One read per turn is what spent whole turn budgets on
nothing; a script that reads twelve files is one turn.

Finish by calling the `yield` tool exactly once. `result.data` is an OBJECT
of exactly the shape shown above - the field names are validated, and prose
in place of the object is rejected.

When the work could not be done, call `yield` with `result.error` (a string
saying why) instead of `data`. The parent reads what you yield, not what you
say — prose is not delivered. If yield reports schema problems, fix every one
it lists and call it again.

When you are told your turn budget is nearly spent, stop and call `yield`
at once with what you have, and name every part you did not reach in the
field your result has for it. A partial result the parent can read is
worth more than a run that ends with nothing.

Each agent file’s body ends with one marker line, <!-- gadhs:shared finish -→, where the block goes. agents/ is already in the package’s files, so the block ships with it; the seeding lists only *.md directly under agents/, so the subdirectory is no agent.

The splice

New packages/pi-agents/shared-block.ts (added to files in packages/pi-agents/package.json), in erasable TypeScript only, since the battery imports it under plain node:

  • SHARED_BLOCK_MARKER, the marker line’s text.

  • readSharedBlock(packagedDir): the trimmed text of shared/finish.md under it, or undefined when the file is absent.

  • withSharedBlock(packaged, block): the text with its marker line replaced by the block. No marker: the text unchanged. A marker with no block, or more than one marker: throws a SharedBlockError naming the marker.

  • packagedAgentText(packagedDir, file): the file of that name under the directory ("Explore.md", with its extension), read and spliced.

seedAgents reads the block once and renders each file as renderAgent(withSharedBlock(text, block), override). A file that throws is reported as that file’s error and not written, as an unrenderable file is today. The battery’s resolvePrompt parses packagedAgentText(AGENTS_DIR, ${p.agent}.md). Nothing else changes: a file whose managed marker was deleted is still never touched, and so keeps the text it has.

Erratum (2026-11-27): a block file that exists and cannot be read is reported once by the seed (shared block unreadable: …), beside each marked file’s own error; the plan named only the missing file. Pinned in extension.test.ts.

The three bodies

Paragraphs are named by their opening words and keep their text unless a change is given.

File Body, in order

Explore.md

1. "You are a codebase exploration specialist", with "If the task requires modifying anything, stop and say so instead." moved up to end that paragraph. 2. "Be terse." 3. "Numbers: use the count tool". 4. New line "Your result is an object of this shape:", then the shape, its uncertain placeholder now <what you could not determine, or did not reach>. 5. "The fields, in order of importance:" and its list. 6. The marker.

Research.md

1. "You research a question against primary sources". 2. "Rules:" and its five bullets, then the quotations bullet as the sixth. 3. "Terse, structured output." 4. "Your result is an object of this shape:", then the shape, its unverified placeholder now <a claim you could not check, or a question you did not reach, and whose it is>. 5. The marker.

general-purpose.md

1. "You are a general-purpose helper". 2. A paragraph of the two rules that leave the batching paragraph: "Numbers come from the count tool, never a hand tally. Read files with read and change them with edit, not through the shell: a shell read is judged on every call, and a shell edit skips the authoring guard." 3. "Your result is an object of this shape:", then the shape. 4. "`summary` is at most 4,000 characters", ending with two new sentences: "A result over a limit is refused. What you did not reach goes under not_done." 5. The marker.

Each file’s own four paragraphs (batching, "Finish by calling", "When the work could not be done", "When you are told your turn budget") are deleted. Frontmatter is unchanged.

The run, and the rule that reads it

The run is the operator’s. Two arms each, the old prompt and the new, interleaved run by run by a scratch script that swaps the one agent file (main’s copy has no marker, so it renders as it is) and restores it at exit, as #891 and #895 did.

Helper Command per arm Lanes, both arms About

Explore

battery run --task explore --model vertex-gemini/gemini-4-hydrogen --runs 2, stores build/battery-896-explore-base and -new

24 x 2 x 2 = 96

$10

general-purpose

battery run --task coder-helper --model vertex-anthropic/claude-opus-5-5 --runs 5, stores build/battery-896-gp-base and -new

6 x 5 x 2 = 60

$15

Research is not measured: it carries the same block as the two that are, and its own text is only reordered.

The decision rule, fixed here. The change merges when all eight hold:

# Gate

E-a

Explore: pathRecallMin (the lower of the two repositories' mean path recall) under the new prompt is at least the old prompt’s minus 0.02

E-b

Explore: lanes ending in an accepted yield with no waiver are at least the old prompt’s count minus 1

E-c

Explore: yield refusals (the sum of yieldRetries) are at most the old prompt’s plus 2

E-d

Explore: the median cost a lane is at most 1.25 times the old prompt’s

G-a

general-purpose: lanes solved are at least the old prompt’s count minus 1

G-b

general-purpose: at least 29 of the 30 lanes end in an accepted yield with no waiver

G-c

general-purpose: the median cost a lane is at most 1.25 times the old prompt’s

G-d

general-purpose: no check reported passed is one its transcript shows was not run or did not pass, read lane by lane as for #895

A dry run and a prediction go on #896 before the run. If a gate fails the result goes on #896 and the operator decides; nothing merges.

Scope

  1. The change, unmerged, on refactor/helper-prompt-order: shared-block.ts, shared/finish.md, the three agent files, seedAgents, the battery’s resolvePrompt, the consumer-sim row, the tests, the docs and the CHANGELOG entry. Validate and consumer-sim run locally; not pushed for merge.

  2. The dry run, the prediction, the operator’s go, the run, and the result on #896 by the rule.

  3. The MR (Closes #896), only when all eight gates hold.

Not in this plan: giving the battery’s explore task the codemode tool (#939). Both arms run without it, as every stored Explore record did.

Tests

Category Test Pins

contract

new packages/pi-agents/test/shared-block.test.ts: withSharedBlock replaces the marker line with the block; returns a text with no marker unchanged; throws for a marker with no block and for two markers; readSharedBlock is undefined for a directory without the file

the splice, and that a prompt holding a raw marker cannot come out of it

contract

contract-drift.test.ts: its parse reads packagedAgentText, so every existing per-agent check (the yield and error phrases, the wrap-up, batching, each shape’s fields and types, general-purpose’s bounds) holds on the spliced text

nothing a helper was told is lost in the move

contract

same file: each packaged file’s own text holds the marker once, as its last line, and none of "Independent reads, greps", "Finish by calling the yield tool", "When the work could not be done", "When you are told your turn budget"; shared/finish.md holds all four

the four paragraphs live in one block

contract

same file: in each spliced body the first paragraph is the role ("You are" or "You research"), "Your result is an object of this shape" comes after it and before "Independent reads, greps", and the block’s last line is the body’s last; in Research, "Quotations must be MECHANICALLY verified" comes before "Terse, structured output"

role first, rules next, output contract last

contract

seeding.test.ts, model-override.test.ts and turns-override.test.ts: every comparison of a seeded file with its packaged one compares against packagedAgentText (the model or turn-cap line aside, as now; turns-override.test.ts’s `packagedText becomes it), and a seeded file holds no marker; seeding.test.ts’s list of packaged agents is the `*.md files

what a box receives is the whole prompt

regression

extension.test.ts: over a temporary packaged directory, an agent file with the marker and no shared/finish.md is one reported error naming the marker and is not written, while a file with no marker beside it is written

a prompt with a raw marker is never seeded

contract

tools/model-battery/test/run.test.mjs: loadTask("explore").prompt.text and loadTask("coder-helper").prompt.text hold "Finish by calling the yield tool exactly once" and no marker. In "the coder-helper arm takes the general-purpose helper’s own tools", both bodies are read through packagedAgentText: Explore’s sha is sha256 of its spliced body, and coder-helper’s is sha256 of "append\n" and general-purpose’s spliced body

the battery measures the prompt a box runs

contract

tools/model-battery/test/explore.test.mjs: its two raw reads of Explore.md become packagedAgentText: "yields its thinking level, its output schema and a body that ends by calling yield" then holds on the spliced body, and "no battery source contains a line of its body" covers the block’s lines too. Its listing of the packaged agents already filters *.md

the battery’s tests judge the prompt the battery sends

regression

tools/consumer-sim.mjs, "agency agents are seeded on a fresh install": each seeded file also holds "Finish by calling the yield tool exactly once" and no marker

the published bundle carries the block

packages/pi/test/shipped-imports.test.ts, unchanged, fails if shared-block.ts is missing from files. Mutants, each to fail its test: seedAgents not splicing; one block paragraph copied back into an agent file; withSharedBlock returning the text when the block is missing. Coverage bars hold (pi-agents 100 % lines).

Risks

Risk Mode Mitigation

A helper runs under a prompt with a raw marker and no mechanics

fail-closed

withSharedBlock throws; the seeding reports the file and does not write it; the consumer-sim row checks the bundle’s seeded files

The new order costs Explore recall or its yield, or general-purpose its work

loud

the eight gates, against the old prompt on the same cases and model

Research is changed and not measured

silent

it carries the block the two measured helpers carry; only its own paragraphs move; stated with the result

A reader takes the packaged file for the whole prompt

silent

the seeding, the battery, and the tests that read a body (contract-drift.test.ts, explore.test.mjs) read through packagedAgentText; the battery test requires the block in the prompt it sends

A file a user owns keeps the old order

silent

by design: an unmanaged file is never touched; the CHANGELOG says so

The swap script leaves main’s agent file in the working tree

loud

restored at exit; git status is checked before any commit

Cost above the projection

fail-closed

the battery refuses over its budget before any call; the dry run is on #896 before the go

Delivery

  • Canonical page: docs/modules/ROOT/pages/packages.adoc, the @gadhs/pi-agents row: the shared block and where it is spliced. packages/pi-agents/README.md: the same, in one sentence.

  • CHANGELOG.adoc == Unreleased: a Changed entry.

  • This plan committed as docs/modules/ROOT/pages/plans/helper-prompt-order.adoc, listed in docs/modules/ROOT/nav.adoc; Status In progress at start, Done at the merge or at the operator’s decision.

  • Validate and consumer-sim before the push; the MR merged with squash off; #896 is closed by it, with the merge SHA on the issue. pi-agents takes a minor bump at the next release.

Edit this page · latest