Skip to content

Commit 7ef4d99

Browse files
fix(server): Pi turns start while an extension run is streaming
The turn-start prompt was sent without streamingBehavior. When a Pi extension continuation was already streaming, Pi rejected it with "Agent is already processing" and the turn failed. Send it with streamingBehavior: "steer", as the steer path already does. Pi ignores the field when idle, so recorded replay frames only gain the field. Fixes #15221
1 parent 1826fb5 commit 7ef4d99

11 files changed

Lines changed: 54 additions & 19 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/PiAdapterV2.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,37 @@ describe("PiAdapterV2", () => {
15971597
}).pipe(Effect.scoped, Effect.provide(testLayer)),
15981598
);
15991599

1600+
it.effect("starts a turn while an extension run is already streaming", () =>
1601+
Effect.gen(function* () {
1602+
const fake = yield* makeFakePi;
1603+
const { runtime, takeEvent } = yield* openRuntime(fake);
1604+
const providerThread = yield* runtime.ensureThread({
1605+
threadId: THREAD_ID,
1606+
modelSelection: modelSelection("default"),
1607+
runtimePolicy,
1608+
});
1609+
yield* startTurn(runtime, providerThread);
1610+
const prompt = yield* fake.takeRequest("prompt");
1611+
// An extension continuation resumed with the session and is streaming.
1612+
// Pi rejects a prompt without streamingBehavior while it streams.
1613+
yield* fake.emit({ type: "agent_start" });
1614+
yield* fake.emit(
1615+
prompt["streamingBehavior"] === undefined
1616+
? {
1617+
type: "response",
1618+
command: "prompt",
1619+
success: false,
1620+
error:
1621+
"Agent is already processing. Specify streamingBehavior ('steer' or 'followUp') to queue the message.",
1622+
}
1623+
: { type: "response", command: "prompt", success: true },
1624+
);
1625+
yield* fake.emit({ type: "agent_settled" });
1626+
const terminal = yield* takeEvent((event) => event.type === "turn.terminal");
1627+
assert.isTrue(terminal.type === "turn.terminal" && terminal.status === "completed");
1628+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1629+
);
1630+
16001631
it.effect("shows compaction progress and completes the same activity row", () =>
16011632
Effect.gen(function* () {
16021633
const fake = yield* makeFakePi;

‎apps/server/src/orchestration-v2/Adapters/PiAdapterV2.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,9 +2355,13 @@ export function makePiAdapterV2(
23552355
kind: "turn_start",
23562356
});
23572357
} else if (payload !== null) {
2358+
// An extension can already be streaming when the turn starts.
2359+
// Pi rejects a bare prompt then; steer joins that run, and an
2360+
// idle Pi ignores streamingBehavior.
23582361
yield* connection.send({
23592362
type: "prompt",
23602363
message: payload.message,
2364+
streamingBehavior: "steer",
23612365
...(payload.images.length === 0 ? {} : { images: payload.images }),
23622366
});
23632367
pendingPromptResponses.push({

‎apps/server/src/orchestration-v2/testkit/fixtures/message_steering/pi_output.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function assertPiMessageSteeringOutput(
3636
assert.deepEqual(
3737
prompts.map((frame) => [frameField(frame, "message"), frameField(frame, "streamingBehavior")]),
3838
[
39-
[MESSAGE_STEERING_INITIAL_PROMPT, undefined],
39+
[MESSAGE_STEERING_INITIAL_PROMPT, "steer"],
4040
[MESSAGE_STEERING_STEER_PROMPT, "steer"],
4141
],
4242
);

‎apps/server/src/orchestration-v2/testkit/fixtures/message_steering/pi_transcript.ndjson‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{"type":"expect_outbound","label":"set_session_name","frame":{"type":"set_session_name","name":"Replay fixture: message_steering","id":"t3-5"}}
1515
{"type":"emit_inbound","label":"session_info_changed","frame":{"type":"session_info_changed","name":"Replay fixture: message_steering"}}
1616
{"type":"emit_inbound","label":"response:set_session_name","frame":{"id":"t3-5","type":"response","command":"set_session_name","success":true}}
17-
{"type":"expect_outbound","label":"prompt","frame":{"type":"prompt","message":"Respond with exactly: steering fixture initial response"}}
17+
{"type":"expect_outbound","label":"prompt","frame":{"type":"prompt","message":"Respond with exactly: steering fixture initial response","streamingBehavior":"steer"}}
1818
{"type":"emit_inbound","label":"response:prompt","frame":{"type":"response","command":"prompt","success":true,"data":{"disposition":"started"}}}
1919
{"type":"emit_inbound","label":"agent_start","frame":{"type":"agent_start"}}
2020
{"type":"emit_inbound","label":"turn_start","frame":{"type":"turn_start"}}

‎apps/server/src/orchestration-v2/testkit/fixtures/multi_turn/pi_transcript.ndjson‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{"type":"expect_outbound","label":"set_session_name","frame":{"type":"set_session_name","name":"Replay fixture: multi_turn","id":"t3-5"}}
1515
{"type":"emit_inbound","label":"session_info_changed","frame":{"type":"session_info_changed","name":"Replay fixture: multi_turn"}}
1616
{"type":"emit_inbound","label":"response:set_session_name","frame":{"id":"t3-5","type":"response","command":"set_session_name","success":true}}
17-
{"type":"expect_outbound","label":"prompt","frame":{"type":"prompt","message":"Respond with exactly: first fixture turn complete"}}
17+
{"type":"expect_outbound","label":"prompt","frame":{"type":"prompt","message":"Respond with exactly: first fixture turn complete","streamingBehavior":"steer"}}
1818
{"type":"emit_inbound","label":"response:prompt","frame":{"type":"response","command":"prompt","success":true,"data":{"disposition":"started"}}}
1919
{"type":"emit_inbound","label":"agent_start","frame":{"type":"agent_start"}}
2020
{"type":"emit_inbound","label":"turn_start","frame":{"type":"turn_start"}}
@@ -43,7 +43,7 @@
4343
{"type":"emit_inbound","label":"response:get_entries","frame":{"id":"t3-7","type":"response","command":"get_entries","success":true,"data":{"entries":[{"type":"model_change","id":"5e37f535","parentId":"f3bce6ef","timestamp":"2026-01-01T00:00:00.437Z","provider":"openrouter","modelId":"deepseek/deepseek-v4-flash"},{"type":"session_info","id":"16d089a9","parentId":"5e37f535","timestamp":"2026-01-01T00:00:00.440Z","name":"Replay fixture: multi_turn"},{"type":"message","id":"ea6f5949","parentId":"16d089a9","timestamp":"2026-01-01T00:00:00.448Z","message":{"role":"system","content":"","sections":{"preamble":"<system-prompt-section>","tools":"<system-prompt-section>","rules":"<system-prompt-section>","docs":"<system-prompt-section>","cwd":"<system-prompt-section>"},"timestamp":1790898591750,"toolsAdded":[{"name":"read","description":"Read the contents of a file. Supports text files and images (jpg, png, gif, webp, bmp). Images are sent as attachments. For text files, output is truncated to 2000 lines or 50KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete.","parameters":{"type":"object","required":["path"],"properties":{"path":{"type":"string","description":"Path to the file to read (relative or absolute)"},"offset":{"type":"number","description":"Line number to start reading from (1-indexed)"},"limit":{"type":"number","description":"Maximum number of lines to read"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"bash","description":"Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last 2000 lines or 50KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.","parameters":{"type":"object","required":["command"],"properties":{"command":{"type":"string","description":"Shell command to execute"},"timeout":{"type":"number","description":"Timeout in seconds (optional, no default timeout)"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"edit","description":"Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.","parameters":{"type":"object","required":["path","edits"],"properties":{"path":{"type":"string","description":"Path to the file to edit (relative or absolute)"},"edits":{"type":"array","items":{"type":"object","required":["oldText","newText"],"properties":{"oldText":{"type":"string","description":"Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call."},"newText":{"type":"string","description":"Replacement text for this targeted edit."}}},"description":"One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead."}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"write","description":"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.","parameters":{"type":"object","required":["path","content"],"properties":{"path":{"type":"string","description":"Path to the file to write (relative or absolute)"},"content":{"type":"string","description":"Content to write to the file"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}}]}},{"type":"message","id":"ff895990","parentId":"ea6f5949","timestamp":"2026-01-01T00:00:00.448Z","message":{"role":"user","content":[{"type":"text","text":"Respond with exactly: first fixture turn complete"}],"timestamp":1767225600444}},{"type":"message","id":"5c0d3865","parentId":"ff895990","timestamp":"2026-01-01T00:00:01.723Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"The user wants me to respond with exactly: \"first fixture turn complete\"","thinkingSignature":"<signature>"},{"type":"text","text":"first fixture turn complete"}],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":1787,"output":21,"cacheRead":0,"cacheWrite":0,"reasoning":18,"totalTokens":1808,"cost":{"input":0.00007480382,"output":0.0000017581199999999999,"cacheRead":0,"cacheWrite":0,"total":0.00007656194}},"stopReason":"stop","timestamp":1767225600465,"responseId":"gen-1790898592-RtbvUBJEB0AHaUCPMqrg","rawStopReason":"stop","thinkingLevel":"high"}}],"leafId":"5c0d3865"}}}
4444
{"type":"expect_outbound","label":"get_session_stats","frame":{"type":"get_session_stats","id":"t3-8"}}
4545
{"type":"emit_inbound","label":"response:get_session_stats","frame":{"id":"t3-8","type":"response","command":"get_session_stats","success":true,"data":{"sessionFile":"/pi-sessions/session-1.jsonl","sessionId":"00000000-0000-4000-8000-000000000002","userMessages":1,"assistantMessages":1,"toolCalls":0,"toolResults":0,"totalMessages":3,"tokens":{"input":1787,"output":21,"cacheRead":0,"cacheWrite":0,"total":1808},"cost":0.00007656194,"contextUsage":{"tokens":1808,"contextWindow":1048576,"percent":0.17242431640625}}}}
46-
{"type":"expect_outbound","label":"prompt","frame":{"type":"prompt","message":"Respond with exactly: second fixture turn complete"}}
46+
{"type":"expect_outbound","label":"prompt","frame":{"type":"prompt","message":"Respond with exactly: second fixture turn complete","streamingBehavior":"steer"}}
4747
{"type":"emit_inbound","label":"response:prompt","frame":{"type":"response","command":"prompt","success":true,"data":{"disposition":"started"}}}
4848
{"type":"emit_inbound","label":"agent_start","frame":{"type":"agent_start"}}
4949
{"type":"emit_inbound","label":"turn_start","frame":{"type":"turn_start"}}

0 commit comments

Comments
 (0)