diff options
| author | Jesse Vincent <jesse@fsck.com> | 2026-10-22 20:52:53 +0000 |
|---|---|---|
| committer | Jesse Vincent <jesse@fsck.com> | 2026-10-22 22:56:05 +0000 |
| commit | 5802204a180e576f81a26e6c3dd2bd0b5ea675c6 (patch) | |
| tree | 674f35dffd47040abf25d4ddd6193d163d339fa1 | |
| parent | b58deb3b948d4c305feeac122700100419ee2777 (diff) | |
fix(pi): inject the bootstrap on every prompt, not just the first (#2083)fix/2083-pi-bootstrap-every-turn
The pi extension cleared injectBootstrap on agent_end, assuming it marked
the end of a session. pi emits agent_end at the end of every agent run,
which is once per user prompt, so from the second prompt on no provider
request carried the using-superpowers bootstrap.
The messages a context handler returns are sent to the provider but are
not written to session history, so the bootstrap is never in
event.messages on a later call and the existing marker check cannot see
an earlier injection. The bootstrap therefore has to be injected into
every provider request. Removing the agent_end disarm does exactly that,
one copy per request at a stable position at the start of the messages.
Observed against pi 0.85.1 with a local stub OpenAI-compatible provider,
one session of three prompts (the second makes a tool call): before, only
request 1 of 4 carried the bootstrap; after, all 4 carry exactly one copy.
The test that asserted the disarm as intended behaviour is replaced by
one that drives pi's per-prompt lifecycle events across several prompts
and provider requests. The porting guide described the agent_end reset
as the pattern to copy and now describes the failure instead.
Claude-Session: https://claude.ai/code/session_01SHANcr4A71nzuU9et4Fnba
| -rw-r--r-- | .pi/extensions/superpowers.ts | 7 | ||||
| -rw-r--r-- | docs/porting-to-a-new-harness.md | 15 | ||||
| -rw-r--r-- | tests/pi/test-pi-extension.mjs | 49 |
3 files changed, 54 insertions, 17 deletions
diff --git a/.pi/extensions/superpowers.ts b/.pi/extensions/superpowers.ts index 2e948e9..3b38adf 100644 --- a/.pi/extensions/superpowers.ts +++ b/.pi/extensions/superpowers.ts @@ -24,10 +24,9 @@ export default function superpowersPiExtension(pi: ExtensionAPI) { injectBootstrap = true; }); - pi.on("agent_end", async () => { - injectBootstrap = false; - }); - + // Messages returned from a context handler are sent to the provider but never + // written to session history, so the bootstrap has to be injected into every + // provider request, on every prompt, for the whole session. pi.on("context", async (event) => { if (!injectBootstrap) return; if (event.messages.some(messageContainsBootstrap)) return; diff --git a/docs/porting-to-a-new-harness.md b/docs/porting-to-a-new-harness.md index 4964ff0..15cc84c 100644 --- a/docs/porting-to-a-new-harness.md +++ b/docs/porting-to-a-new-harness.md @@ -428,10 +428,9 @@ messages break some models (#894). Three things you must replicate: harness-specific constant.) Cache the bootstrap content at module level so you're not re-reading and re-parsing `SKILL.md` on every call (#1202). - **Compaction.** If the harness compacts/summarizes history, re-inject - afterward. pi sets an `injectBootstrap` flag on `session_start` and - `session_compact`, clears it on `agent_end`, and inserts the message *after* - any leading compaction-summary messages. OpenCode relies on its per-step - re-injection plus the dedup guard. + afterward. pi re-injects on every `context` call and inserts the message + *after* any leading compaction-summary messages. OpenCode relies on its + per-step re-injection plus the dedup guard. - **Message-object shape is per-harness — discover yours, don't copy a literal.** The two references use *incompatible* shapes: pi builds `{ role, content: [{ type, text }], timestamp }`; OpenCode manipulates @@ -827,9 +826,11 @@ Use this as the live index; when in doubt, read the files, not this table. (Cursor). Use what your harness exports; the script re-derives the root itself. - **System-message injection.** Shape B injects a *user* message on purpose (#750, #894). Don't "fix" it to a system message. -- **Per-step vs per-turn callbacks.** OpenCode fires every step (per-call dedup - guard); pi fires per turn (lifecycle flag + `agent_end` reset). Copying one - harness's dedup strategy onto the other's callback frequency breaks injection. +- **Disarming injection after the first prompt.** If your harness's transform + output is not saved to session history (pi's `context` result is not), the + bootstrap must be injected into every provider request. A flag cleared on a + per-prompt event such as pi's `agent_end` drops the bootstrap after the first + prompt (#2083). - **Message-object shape is per-harness.** Shape B. pi and OpenCode use incompatible shapes; discover yours, don't copy a reference's object literal. - **Hunting for a skill-registration API that doesn't exist.** A harness with no diff --git a/tests/pi/test-pi-extension.mjs b/tests/pi/test-pi-extension.mjs index c79f710..65d65e0 100644 --- a/tests/pi/test-pi-extension.mjs +++ b/tests/pi/test-pi-extension.mjs @@ -34,6 +34,14 @@ function firstHandler(handlers, event) { return eventHandlers[0]; } +// Fire an event the way pi does: every registered handler runs, and an event +// the extension does not subscribe to is a no-op. +async function emit(handlers, event, payload) { + for (const handler of handlers.get(event) ?? []) { + await handler({ type: event, ...payload }, {}); + } +} + function textOf(message) { if (typeof message.content === 'string') return message.content; return message.content @@ -55,17 +63,16 @@ test('extension leaves package skill discovery to Pi and registers lifecycle hoo const { handlers } = await loadExtension(); assert.equal((handlers.get('resources_discover') ?? []).length, 0, 'extension must not bypass package skill filters'); - for (const event of ['session_start', 'session_compact', 'context', 'agent_end']) { + for (const event of ['session_start', 'session_compact', 'context']) { assert.equal((handlers.get(event) ?? []).length, 1, `missing ${event} handler`); } assert.equal((handlers.get('session_before_compact') ?? []).length, 0); }); -test('startup context injects the bootstrap as one user message until agent_end', async () => { +test('startup context injects the bootstrap as one user message', async () => { const { handlers } = await loadExtension(); const sessionStart = firstHandler(handlers, 'session_start'); const context = firstHandler(handlers, 'context'); - const agentEnd = firstHandler(handlers, 'agent_end'); await sessionStart({ type: 'session_start', reason: 'startup' }, {}); @@ -86,10 +93,40 @@ test('startup context injects the bootstrap as one user message until agent_end' const alreadyInjected = await context({ type: 'context', messages: result.messages }, {}); assert.equal(alreadyInjected, undefined, 'bootstrap should not duplicate when already present'); +}); + +// pi emits agent_start/agent_end around every user prompt, not once per +// session, and the messages a context handler returns are never written back +// to session history. So every provider request of every prompt needs the +// bootstrap injected again (#2083). +test('every provider request of every prompt in a session carries the bootstrap', async () => { + const { handlers } = await loadExtension(); + const context = firstHandler(handlers, 'context'); + + await emit(handlers, 'session_start', { reason: 'startup' }); - await agentEnd({ type: 'agent_end', messages: [] }, {}); - const afterEnd = await context({ type: 'context', messages: originalMessages }, {}); - assert.equal(afterEnd, undefined, 'startup bootstrap should clear after agent_end'); + const history = []; + const prompts = [ + { text: 'first prompt', providerRequests: 1 }, + { text: 'second prompt with a tool call', providerRequests: 2 }, + { text: 'third prompt', providerRequests: 1 }, + ]; + for (const [promptIndex, prompt] of prompts.entries()) { + history.push({ role: 'user', content: [{ type: 'text', text: prompt.text }], timestamp: promptIndex }); + await emit(handlers, 'agent_start', {}); + for (let request = 1; request <= prompt.providerRequests; request += 1) { + await emit(handlers, 'turn_start', {}); + const result = await context({ type: 'context', messages: structuredClone(history) }, {}); + const label = `prompt ${promptIndex + 1}, provider request ${request}`; + assert.ok(result, `${label}: bootstrap missing`); + assert.equal(result.messages.length, history.length + 1, `${label}: expected exactly one bootstrap message`); + assert.match(textOf(result.messages[0]), /You have superpowers/, `${label}: bootstrap not first`); + history.push({ role: 'assistant', content: [{ type: 'text', text: 'ok' }], timestamp: promptIndex }); + await emit(handlers, 'turn_end', {}); + } + await emit(handlers, 'agent_end', { messages: [] }); + await emit(handlers, 'agent_settled', {}); + } }); test('session_compact injects bootstrap after compaction summaries, not before compaction', async () => { |
