Skip to content

Commit 73f6520

Browse files
shivamhwpclaude
andcommitted
fix(server): keep a settled root's held text from reopening it
When a root ended mid-paragraph while child work kept ingestion open, the idle flush later wrote the root's held tail as a streaming snapshot after the run had settled. The flush now skips root-thread text once the root run is finalized; child text still flushes. Tests: the filter's omnibus case becomes a sequence table covering unfinished fences, lists and tables, appends after an idle delivery, completion at the idle deadline, fast paragraph bursts, two messages, empty and whitespace text, rewrites and turn mode. The queued-final ordering test folds into the idle-paragraph scenario, and the interrupted child asserts it stops streaming. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 28c3000 commit 73f6520

3 files changed

Lines changed: 231 additions & 109 deletions

File tree

‎apps/server/src/orchestration-v2/RunExecutionService.test.ts‎

Lines changed: 58 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4023,27 +4023,41 @@ it.effect("delivers a held paragraph after the provider goes idle with its promp
40234023
},
40244024
] as ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>;
40254025

4026+
const blockerStarted = yield* Deferred.make<void>();
4027+
const releaseBlocker = yield* Deferred.make<void>();
4028+
const blocker = streamedMessageEvent({
4029+
id: `message:${key}:blocker`,
4030+
threadId: ids.threadId,
4031+
runId: ids.runId,
4032+
role: "assistant",
4033+
text: "Other",
4034+
streaming: false,
4035+
});
40264036
const scenario = yield* runBackgroundItemScenario(key, () => Stream.fromQueue(providerEvents), {
40274037
batched: true,
40284038
onIngest: (event, write) =>
4029-
event.type === "message.updated" || event.type === "turn_item.updated"
4030-
? Effect.gen(function* () {
4031-
const message = event.type === "message.updated" ? event.message : event.turnItem;
4032-
const text = "text" in message ? message.text : "";
4033-
const streaming = "streaming" in message && message.streaming;
4034-
yield* Ref.update(delivered, (current) => [
4035-
...current,
4036-
`${event.type}:${streaming ? "streaming" : "final"}:${JSON.stringify(text)}${
4037-
write.inSharedCommit ? " in commit" : ""
4038-
}`,
4039-
]);
4040-
if (event.type === "turn_item.updated") {
4041-
yield* Deferred.succeed(paragraphDelivered, undefined);
4042-
if (streaming && text === "First.\n\nWaiting on the build")
4043-
yield* Deferred.succeed(idleDelivered, undefined);
4044-
}
4045-
})
4046-
: Effect.void,
4039+
event.type === "message.updated" && event.message.text === "Other"
4040+
? Deferred.succeed(blockerStarted, undefined).pipe(
4041+
Effect.andThen(Deferred.await(releaseBlocker)),
4042+
)
4043+
: event.type === "message.updated" || event.type === "turn_item.updated"
4044+
? Effect.gen(function* () {
4045+
const message = event.type === "message.updated" ? event.message : event.turnItem;
4046+
const text = "text" in message ? message.text : "";
4047+
const streaming = "streaming" in message && message.streaming;
4048+
yield* Ref.update(delivered, (current) => [
4049+
...current,
4050+
`${event.type}:${streaming ? "streaming" : "final"}:${JSON.stringify(text)}${
4051+
write.inSharedCommit ? " in commit" : ""
4052+
}`,
4053+
]);
4054+
if (event.type === "turn_item.updated") {
4055+
yield* Deferred.succeed(paragraphDelivered, undefined);
4056+
if (streaming && text === "First.\n\nWaiting on the build")
4057+
yield* Deferred.succeed(idleDelivered, undefined);
4058+
}
4059+
})
4060+
: Effect.void,
40474061
}).pipe(Effect.forkChild);
40484062

40494063
yield* Queue.offerAll(providerEvents, assistantText("First.\n\nWaiting on the build", true));
@@ -4053,12 +4067,18 @@ it.effect("delivers a held paragraph after the provider goes idle with its promp
40534067
yield* Deferred.await(idleDelivered);
40544068
yield* Queue.offerAll(providerEvents, [
40554069
...assistantText("First.\n\nWaiting on the build to", true),
4070+
blocker,
40564071
...assistantText("First.\n\nWaiting on the build to finish.", false),
4057-
rootTerminalEvent(ids, "completed"),
40584072
]);
4073+
yield* Deferred.await(blockerStarted);
4074+
// The appended text comes due while its final snapshots wait behind the blocked write.
4075+
yield* TestClock.adjust("1 second");
4076+
yield* Deferred.succeed(releaseBlocker, undefined);
4077+
yield* Queue.offer(providerEvents, rootTerminalEvent(ids, "completed"));
40594078
yield* Fiber.join(scenario);
40604079

40614080
// Snapshots share the batch commit; the idle write commits alone, before it counts as shown.
4081+
// The final snapshots win over the stale append that came due behind them.
40624082
assert.deepEqual(yield* Ref.get(delivered), [
40634083
`message.updated:streaming:"First.\\n\\n" in commit`,
40644084
`turn_item.updated:streaming:"First.\\n\\n" in commit`,
@@ -4159,6 +4179,7 @@ it.effect("settles a child message only an idle flush wrote when the root is int
41594179
assert.equal(settled.length, 1);
41604180
assert.equal(settled[0]?.status, "interrupted");
41614181
assert.equal("text" in settled[0]! ? settled[0].text : null, heldChildText);
4182+
assert.isFalse("streaming" in settled[0]! && settled[0].streaming);
41624183
}),
41634184
);
41644185

@@ -4173,17 +4194,32 @@ it.effect.each([
41734194
const subagentIngested = yield* Deferred.make<void>();
41744195
const rootFinalized = yield* Deferred.make<void>();
41754196
const flushed = yield* Deferred.make<void>();
4197+
const rootWrites = yield* Ref.make<ReadonlyArray<string>>([]);
41764198
const scenario = yield* runBackgroundItemScenario(key, () => Stream.fromQueue(events), {
41774199
onIngest: (event) =>
41784200
event.type === "subagent.updated"
41794201
? Deferred.succeed(subagentIngested, undefined)
41804202
: isHeldChildSnapshot(event)
41814203
? Deferred.succeed(flushed, undefined)
4182-
: Effect.void,
4204+
: event.type === "message.updated" && event.message.threadId === ids.threadId
4205+
? Ref.update(rootWrites, (current) => [...current, event.message.text])
4206+
: Effect.void,
41834207
onFinalized: () => Deferred.succeed(rootFinalized, undefined),
41844208
}).pipe(Effect.forkChild);
41854209

41864210
yield* holdChildSnapshot(ids, events, subagentIngested);
4211+
// The root ends mid-paragraph: its tail is held, and the run settles without it.
4212+
yield* Queue.offer(
4213+
events,
4214+
streamedMessageEvent({
4215+
id: `message:${key}:root`,
4216+
threadId: ids.threadId,
4217+
runId: ids.runId,
4218+
role: "assistant",
4219+
text: "Root.\n\nhalf",
4220+
streaming: true,
4221+
}),
4222+
);
41874223
const terminal = rootTerminalEvent(ids, terminalStatus);
41884224
yield* Queue.offer(
41894225
events,
@@ -4202,6 +4238,8 @@ it.effect.each([
42024238
"turn_item:running",
42034239
"subagent:completed",
42044240
]);
4241+
// A streaming write after the run settled would reopen the root's message.
4242+
assert.deepEqual(yield* Ref.get(rootWrites), ["Root.\n\n"]);
42054243
}),
42064244
);
42074245

@@ -4298,57 +4336,3 @@ it.effect("drops a child's held text once the child is handed to another run", (
42984336
assert.deepEqual(yield* Ref.get(staleWrites), []);
42994337
}),
43004338
);
4301-
4302-
it.effect("flushes idle text only after the snapshots queued ahead of its wake", () =>
4303-
Effect.gen(function* () {
4304-
const key = "idle-flush-queued-snapshot";
4305-
const ids = backgroundScenarioIds(key);
4306-
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event, Cause.Done>();
4307-
const paragraphWritten = yield* Deferred.make<void>();
4308-
const blockerStarted = yield* Deferred.make<void>();
4309-
const releaseBlocker = yield* Deferred.make<void>();
4310-
const writes = yield* Ref.make<ReadonlyArray<string>>([]);
4311-
const message = (id: string, text: string, streaming: boolean) =>
4312-
streamedMessageEvent({
4313-
id: `message:${key}:${id}`,
4314-
threadId: ids.threadId,
4315-
runId: ids.runId,
4316-
role: "assistant",
4317-
text,
4318-
streaming,
4319-
});
4320-
const scenario = yield* runBackgroundItemScenario(key, () => Stream.fromQueue(events), {
4321-
batched: true,
4322-
onIngest: (event) => {
4323-
if (event.type !== "message.updated") return Effect.void;
4324-
if (event.message.text === "Other")
4325-
return Deferred.succeed(blockerStarted, undefined).pipe(
4326-
Effect.andThen(Deferred.await(releaseBlocker)),
4327-
);
4328-
const { streaming, text } = event.message;
4329-
return Ref.update(writes, (current) => [
4330-
...current,
4331-
`${streaming ? "streaming" : "final"}:${text}`,
4332-
]).pipe(Effect.andThen(Deferred.succeed(paragraphWritten, undefined)));
4333-
},
4334-
}).pipe(Effect.forkChild);
4335-
4336-
yield* Queue.offer(events, message("text", "First.\n\nWaiting on the build", true));
4337-
yield* Deferred.await(paragraphWritten);
4338-
yield* Queue.offerAll(events, [
4339-
message("blocker", "Other", false),
4340-
message("text", "First.\n\nWaiting on the build to finish.", false),
4341-
]);
4342-
yield* Deferred.await(blockerStarted);
4343-
// The held text comes due while its final snapshot waits behind the blocked write.
4344-
yield* TestClock.adjust("1 second");
4345-
yield* Deferred.succeed(releaseBlocker, undefined);
4346-
yield* Queue.offer(events, rootTerminalEvent(ids, "completed"));
4347-
yield* Fiber.join(scenario);
4348-
4349-
assert.deepEqual(yield* Ref.get(writes), [
4350-
"streaming:First.\n\n",
4351-
"final:First.\n\nWaiting on the build to finish.",
4352-
]);
4353-
}),
4354-
);

‎apps/server/src/orchestration-v2/RunExecutionService.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ function isRunOwnedSubagentTerminalStatus(
176176
return status === "interrupted" || status === "failed" || status === "cancelled";
177177
}
178178

179+
function streamedThreadId(event: ProviderAdapter.ProviderAdapterV2Event): ThreadId | null {
180+
if (event.type === "message.updated") return event.message.threadId;
181+
if (event.type === "turn_item.updated") return event.turnItem.threadId;
182+
return null;
183+
}
184+
179185
function childTurnsContinue(terminal: ProviderTerminalEvent): boolean {
180186
return terminal.status === "failed" && terminal.childTurnsContinue === true;
181187
}
@@ -1506,7 +1512,10 @@ export const layer: Layer.Layer<
15061512
// A wake due by now has fired; a later deadline needs a new one.
15071513
if (idleFlushWake !== undefined && idleFlushWake.at <= now) idleFlushWake = undefined;
15081514
if (idleFlushClosed) return;
1515+
const rootSettled = yield* Ref.get(rootRunFinalized);
15091516
for (const event of assistantStreaming.takeIdleFlushes(now)) {
1517+
// Settling the root run closed its messages; only children may still stream.
1518+
if (rootSettled && streamedThreadId(event) === input.run.threadId) continue;
15101519
// The held text's child may have been handed to another run since it arrived.
15111520
if (!routeProviderEvent(event, routeIdentity, yield* Ref.get(eventRouting))[0])
15121521
continue;

0 commit comments

Comments
 (0)