Plan: the gadhs blocks become pi sections - no forced system prompt (#729)

Closes: #729. Relates to: #726 (the cache cost this finishes), #725 (the review), ADR-004 (pi-guidance owns the composition seam). Branch: fix/prompt-sections. Status: Done (2026-11-09) — U1 measured on Vertex Opus 5.5 (read 7,537 / write 62 patched, 0 / 7,579 collapsed); U2-U5 on fix/prompt-sections; U4 with one flag (erratum)

Context

packages/pi-modes/session-wiring.ts onBeforeAgentStart composes the gadhs blocks into event.systemPrompt and returns { systemPrompt }. pi stores that as forceSystemPrompt (runner.js emitBeforeAgentStart) and projects it as the one leading system message (agent-session.js _installAgentForcedPromptProjection), while the transcript keeps recording the structured sections. Settled from source on #729: (a) every system-prompt rebuild is a full cache miss - 73 of 76 in canopy’s week, $259; (b) a later handler’s sections never reach the provider - @gotgenes/pi-permission-system writes a helper’s <tools> and <rules> into sections precisely because pi writes neither under a custom prompt, so every helper of this distribution has run with no tools or rules section at all; (c) the transcript’s system row is not what the model saw.

pi’s own path, read in the installed pi 1.5.0 (node_modules/@earendil-works/pi-coding-agent/dist/core/system-prompt.js buildSystemPromptSections; its docs/extensions.md § before_agent_start): mutate event.systemPromptOptions.sections - keys match /^[a-z][a-z0-9_-]*$/, each is rendered after <cwd> as <name>\ncontent\n</name> in insertion order, and an empty string is left out (if (content) promptSections[name] = content). pi diffs the sections against the transcript’s current system row each turn and appends one system message patching the changed ones. A model whose compat.supportsMidConvoSystemMessages is true receives that patch in place and keeps its cached prefix (pi-ai/dist/api/anthropic-messages.js resolveTranscript); any other gets the replayed prompt, one miss per change, as today. getAnthropicCompat there defaults the flag to false; pi-ai’s own Claude catalogue (dist/providers/data/anthropic.json) sets it true on every Claude, and pi-vertex’s asAnthropicMessagesModel sets only forceAdaptiveThinking and supportsTemperature.

Design

D1. Sections, not a string. pi-guidance gains guidanceSections(blocks: GuidanceBlocks): Record<string, string>: the blocks present, in today’s precedence order - gadhs-guidance (first line scope: global), then one gadhs-guidance-language-<id> per language (scope: language:<id>), then gadhs-mode (first line mode: <id>), then gadhs-guidance-project (scope: project) - each value the block’s text, fenced by fenceTags, under its scope line. A block that is absent has no key. composeSystemPrompt and stripGadhsBlocks are deleted with their tests (pre-1.0: callers move; no alias); GuidanceBlocks, fenceTags and the rest stay.

D2. The handler mutates and returns nothing. onBeforeAgentStart writes D1’s sections into event.systemPromptOptions.sections in D1’s order, then gadhs-memory (the lines of memoryPromptBlock in memory-wiring.ts, which wraps memory.ts renderMemoryBlock, with the <gadhs-memory> tags removed). For each of the fixed keys (gadhs-guidance, gadhs-mode, gadhs-guidance-project, gadhs-memory) and each language key seen this session, a block that is absent this turn is written as "": pi leaves an empty section out of the prompt, and the diff patches a stale one away (a language profile the checkout gained or lost, the project block when trust changes, the memory block when emptied). The handler returns undefined always; its event parameter is { systemPromptOptions: { sections: Record<string, string> } } and the old { systemPrompt } shape is not accepted.

D3. The model keeps its prefix on Vertex. asAnthropicMessagesModel adds supportsMidConvoSystemMessages: true, supportsMidConvoToolChanges: true and supportsMidConvoEffort: true to the compat it injects for every anthropic entry, adaptive and budget alike - the flags pi-ai’s own Claude catalogue carries. So a budget entry, which carries no compat today, carries these three: the #357 case in test/unit.test.ts that asserts compat undefined for claude-haiku-4-5 asserts the three flags instead. U1 probes the wire before U4 lands; Vertex refusing the mid-conversation form is the fallback in Risks.

D4. The words follow. mode-controller.ts’s mode-change message and the guidance say "the gadhs-mode section" where they said block; `modes.adoc == Guidance injection (today "The system prompt is composed per turn, most specific last") describes the sections, the patch and what a helper now receives; public-surface.adoc’s pi-guidance row and ADR-004’s seam sentence name `guidanceSections.

D5. What does not change. The texts of every block; detectLanguages, languageGuidance, readProjectGuidance, packagedGlobalGuidance; the memory block’s content; the judge, whose mode comes from the controller’s modeId(); repeat-guard.ts:106-108 and subagent-wiring.ts:819, which read the agent name from ctx.getSystemPrompt() - pi renders the sections into that text, so the marker they look for is still there; security.adoc, since the mode block’s content is unchanged.

Units

U1 - the probe (fix/prompt-sections, no code)

A scratch script against Vertex Opus 5.5 through pi-ai’s streamAnthropic with a hand-built model carrying D3’s flags (the api field set, per AGENTS.md): request one with a system prompt and one user turn; request two with the same, a later { role: "system" } message carrying one changed section, and the next user turn. Observed and recorded on #729: whether Vertex accepts the second request, and its cacheRead/cacheWrite against request one’s prompt size. Decides U4; U2, U3 and U5 proceed either way.

U2 - pi-guidance: guidanceSections (packages/pi-guidance/payload.ts, index.ts)

Tests (test/payload.test.ts): unit - each block lands under its key with its scope line, in D1’s order; an absent block has no key; a literal <gadhs- in a project text is fenced; property (fast-check) - for any blocks, every key starts with gadhs-, every value is the fenced text under one scope line, and no two blocks share a key. The compose/strip tests go with their functions.

U3 - pi-modes: the handler (packages/pi-modes/session-wiring.ts, memory-wiring.ts, mode-controller.ts)

Tests (test/session-wiring.test.ts, test/wiring-lifecycle.test.ts, test/config.test.ts): unit - the handler returns undefined and writes global, the languages, mode, project (trusted only) and memory in D1’s order; a second run with only the mode changed differs in gadhs-mode alone; an absent memory or project writes ""; an operator’s global guidance replaces the packaged text (today’s case, on the new path); contract - the handler’s event parameter is assignable from pi 1.5.0’s BeforeAgentStartEvent (a compile-time check against @earendil-works/pi-coding-agent’s exported type, so a renamed field fails typecheck here); integration - in `wiring-lifecycle.test.ts, whose load() installs only pi-modes, a stand-in second before_agent_start handler registered after pi-modes writes sections.tools and sections.rules the way the permission system does, and the test asserts both survive in the event’s options and that no handler returned systemPrompt (the property pi’s chaining needs; the real pair is read live in U1’s session, from a helper’s request in the diagnostics); regression - the #443 case (a literal <gadhs- block in a project file survives, fenced) on the new path, and a session resumed from a transcript recorded under the old forced path renders the new sections once.

U4 - pi-vertex: the compat flags (packages/pi-vertex/anthropic.ts)

Only if U1 passes. Tests (test/unit.test.ts): unit - the three flags on an adaptive and a budget entry; supportsTemperature: false still only where #357 sets it; the model object handed in is not mutated (today’s property).

U5 - docs and the release note

modes.adoc == Guidance injection (the sections, the patch, what a helper now receives); public-surface.adoc’s pi-guidance row; one amendment line in ADR-004; `CHANGELOG.adoc under Unreleased with the $259 and the helpers' missing sections; close #729 with the merge SHA and U1’s numbers.

Erratum (2026-11-10, #746): supportsMidConvoEffort is set after all, on Claude Fable 5.2 alone and after its own probe: that model refuses a thinking block whose prefix changed, and the flag’s wire is what drops it instead.

Erratum (2026-11-09, #744): the risk row below came true on Sonnet 4.6 and Haiku 4.5 (400 role 'system' is not supported on this model), found by the release rehearsal; the flag is now per catalogue entry (midConvoSystem), set where a probe of every entry saw the message accepted, off elsewhere.

Erratum (2026-11-09): D3’s three flags became one. supportsMidConvoToolChanges and supportsMidConvoEffort change the tool and thinking wire (tool_addition blocks under a beta, an effort marker per message); U1 ran with thinking off and no tools, so it measured neither, and the contract suite showed the effort change at once. pi-vertex sets supportsMidConvoSystemMessages alone, the flag U1 measured; the other two wait for a probe of their own.

Risks

Risk Mode Mitigation

Vertex rejects the mid-conversation system message (a form Anthropic’s direct API has and Vertex may not)

Fail-closed: the request errors

U1 probes before U4; on refusal U4 is dropped, pi collapses the patch into the leading prompt as today (one miss per change), and D2’s other gains stand.

A section key collides with pi’s own or the permission system’s (tools, rules, project_context, mcp_servers)

Silent: ours would replace theirs in place

Every key is prefixed gadhs-; U2’s property test asserts the prefix; U3’s integration test asserts tools/rules are the later handler’s.

A consumer still calls composeSystemPrompt

Fail-closed: a type error at validate, before anything ships

The repository’s only callers are pi-modes/session-wiring.ts, pi-modes/test/config.test.ts and pi-guidance’s own tests; public-surface.adoc moves the name; no alias.

The mode section sits before the project block, as today, but now also before pi’s mcp_servers and a helper’s tools/rules, so a model weighs it less

Silent

Position stops mattering for caching (the patch is appended as a later message); the section’s first line names the mode, and the mode-change message still points at it. Watched through the judge-field notes, not blocked.

A resumed session replays old sections and the new keys both

Silent: duplicated guidance

pi diffs against the transcript’s current sections by key; the old forced text was never a section, so nothing of it is replayed. U3’s regression case resumes a transcript recorded under the old path.

Delivery

One branch, one MR, U2-U5 as separate commits; validate green before push; CHANGELOG under Unreleased; modes.adoc the canonical page; close #729 with the merge SHA and U1’s measured numbers.

Edit this page · latest