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.mdandgeneral-purpose.mdeach 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,renderAgentsets itsmodel:andmax_turns:lines, andplaceAgentwrites it under the agent directory, which is the only place pi-subagents reads. -
The packaged files' other readers: the battery’s task loader (
resolvePromptintools/model-battery/lib/task.mjs, for aprompt.agenttask), and tests:packages/pi-agents/test/contract-drift.test.ts;seeding.test.ts,model-override.test.tsandturns-override.test.ts, which hold a seeded file byte-identical to its packaged one (throughreadFileSyncof the packaged directory, orturns-override.test.ts’s `packagedText); andtools/model-battery/test/explore.test.mjs, which readsExplore.mdraw in two tests (its body "ends by calling yield"; no battery source copies a line of it).packages/pi-modes/test/subagent-gate.test.tsreads the files' frontmatter only and lists*.md, so it is unaffected. -
The battery measures two of the three:
exploreruns Explore, andcoder-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 ofshared/finish.mdunder it, orundefinedwhen 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 aSharedBlockErrornaming 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, . Nothing else changes: a file whose managed marker was deleted is still never touched, and so keeps the text it has.${p.agent}.md)
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 |
|---|---|
|
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 |
|
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 |
|
1. "You are a general-purpose helper". 2. A paragraph of the two rules that leave the batching paragraph: "Numbers come from the |
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 |
|
24 x 2 x 2 = 96 |
$10 |
general-purpose |
|
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: |
E-b |
Explore: lanes ending in an accepted |
E-c |
Explore: |
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 |
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 |
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
-
The change, unmerged, on
refactor/helper-prompt-order:shared-block.ts,shared/finish.md, the three agent files,seedAgents, the battery’sresolvePrompt, the consumer-sim row, the tests, the docs and the CHANGELOG entry. Validate and consumer-sim run locally; not pushed for merge. -
The dry run, the prediction, the operator’s go, the run, and the result on #896 by the rule.
-
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 |
the splice, and that a prompt holding a raw marker cannot come out of it |
contract |
|
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 |
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 |
|
what a box receives is the whole prompt |
regression |
|
a prompt with a raw marker is never seeded |
contract |
|
the battery measures the prompt a box runs |
contract |
|
the battery’s tests judge the prompt the battery sends |
regression |
|
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 |
|
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 ( |
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; |
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-agentsrow: 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 indocs/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.