Skip to content

Commit 8959c97

Browse files
committed
fix(server): held assistant drafts keep their position when flushed at turn end
1 parent 6524a8d commit 8959c97

6 files changed

Lines changed: 162 additions & 2 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,20 @@ import * as ProviderEventIngestor from "./ProviderEventIngestor.ts";
4949
import * as McpAppModelContext from "../mcpApps/McpAppModelContext.ts";
5050
import * as RunExecutionService from "./RunExecutionService.ts";
5151
import * as RunFinalizationService from "./RunFinalizationService.ts";
52+
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
5253

5354
const driver = ProviderDriverKind.make("codex");
5455

56+
const layerTurnItemPositionsTest = Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({
57+
allocate: () => Effect.succeed(1),
58+
});
59+
5560
const layerRunExecutionTest = RunExecutionService.layer.pipe(
5661
Layer.provide(
5762
Layer.mergeAll(
5863
McpAppModelContext.layerEmpty,
5964
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
65+
layerTurnItemPositionsTest,
6066
Layer.mock(EventSink.EventSinkV2)({}),
6167
IdAllocator.layer,
6268
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
@@ -755,6 +761,7 @@ it.effect("passes the thread's MCP app context to the provider under a safe key"
755761
Layer.mock(CheckpointService.CheckpointServiceV2)({
756762
captureBaseline: () => Effect.void,
757763
}),
764+
layerTurnItemPositionsTest,
758765
Layer.mock(EventSink.EventSinkV2)({}),
759766
IdAllocator.layer,
760767
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
@@ -782,6 +789,7 @@ it.effect("fails the run when its ownership check cannot be read before calling
782789
Layer.mergeAll(
783790
McpAppModelContext.layerEmpty,
784791
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
792+
layerTurnItemPositionsTest,
785793
Layer.mock(EventSink.EventSinkV2)({
786794
writeIfRunCurrent: (input) =>
787795
Ref.update(writes, (current) => [...current, ...input.events]).pipe(
@@ -1065,6 +1073,7 @@ it.effect("starts the provider when checkpoint baseline capture fails", () =>
10651073
}),
10661074
),
10671075
}),
1076+
layerTurnItemPositionsTest,
10681077
Layer.mock(EventSink.EventSinkV2)({
10691078
writeWithEffects: (input) =>
10701079
Ref.update(writes, (current) => [
@@ -1177,6 +1186,7 @@ it.effect.each(["failure", "interruption", "stale-attempt", "start-guard"] as co
11771186
captureBaseline: () =>
11781187
scenario === "start-guard" ? Effect.void : Effect.die("not reached"),
11791188
}),
1189+
layerTurnItemPositionsTest,
11801190
Layer.mock(EventSink.EventSinkV2)({
11811191
writeIfRunCurrent: (input) =>
11821192
Effect.gen(function* () {
@@ -1346,6 +1356,7 @@ it.effect("ingests new member threads of an owned child after the root turn term
13461356
Layer.mergeAll(
13471357
McpAppModelContext.layerEmpty,
13481358
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
1359+
layerTurnItemPositionsTest,
13491360
Layer.mock(EventSink.EventSinkV2)({
13501361
write: () => Effect.succeed([]),
13511362
writeWithEffects: (input) =>
@@ -1575,6 +1586,7 @@ it.effect("ends a nested native subagent and its thread when the root run is int
15751586
Layer.mergeAll(
15761587
McpAppModelContext.layerEmpty,
15771588
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
1589+
layerTurnItemPositionsTest,
15781590
Layer.mock(EventSink.EventSinkV2)({
15791591
write: () => Effect.succeed([]),
15801592
writeWithEffects: (input) =>
@@ -1932,6 +1944,7 @@ it.effect(
19321944
Layer.mock(CheckpointService.CheckpointServiceV2)({
19331945
captureBaseline: () => Effect.void,
19341946
}),
1947+
layerTurnItemPositionsTest,
19351948
Layer.mock(EventSink.EventSinkV2)({
19361949
write: () => Effect.succeed([]),
19371950
writeWithEffects: (input) =>
@@ -2148,6 +2161,7 @@ it.effect("drops late root provider-thread writes from a superseded attempt", ()
21482161
Layer.mergeAll(
21492162
McpAppModelContext.layerEmpty,
21502163
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
2164+
layerTurnItemPositionsTest,
21512165
Layer.mock(EventSink.EventSinkV2)({
21522166
write: () => Effect.succeed([]),
21532167
writeWithEffects: (input) =>
@@ -2355,6 +2369,7 @@ it.effect(
23552369
Layer.mock(CheckpointService.CheckpointServiceV2)({
23562370
captureBaseline: () => Effect.void,
23572371
}),
2372+
layerTurnItemPositionsTest,
23582373
Layer.mock(EventSink.EventSinkV2)({
23592374
write: () => Effect.succeed([]),
23602375
writeWithEffects: (input) =>
@@ -2527,6 +2542,7 @@ it.effect(
25272542
Layer.mock(CheckpointService.CheckpointServiceV2)({
25282543
captureBaseline: () => Effect.void,
25292544
}),
2545+
layerTurnItemPositionsTest,
25302546
Layer.mock(EventSink.EventSinkV2)({
25312547
write: () => Effect.succeed([]),
25322548
writeWithEffects: (input) =>
@@ -2697,6 +2713,7 @@ it.effect(
26972713
Layer.mock(CheckpointService.CheckpointServiceV2)({
26982714
captureBaseline: () => Effect.void,
26992715
}),
2716+
layerTurnItemPositionsTest,
27002717
Layer.mock(EventSink.EventSinkV2)({
27012718
write: () => Effect.succeed([]),
27022719
writeWithEffects: (input) =>
@@ -3138,6 +3155,7 @@ it.effect(
31383155
Layer.mock(CheckpointService.CheckpointServiceV2)({
31393156
captureBaseline: () => Effect.void,
31403157
}),
3158+
layerTurnItemPositionsTest,
31413159
Layer.mock(EventSink.EventSinkV2)({
31423160
write: () => Effect.succeed([]),
31433161
writeWithEffects: (input) =>
@@ -3862,6 +3880,7 @@ function captureRootRunTermination(input: {
38623880
Layer.mergeAll(
38633881
McpAppModelContext.layerEmpty,
38643882
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
3883+
layerTurnItemPositionsTest,
38653884
Layer.mock(EventSink.EventSinkV2)({
38663885
write: (payload) =>
38673886
Effect.gen(function* () {
@@ -4343,6 +4362,7 @@ function runBackgroundItemScenario(
43434362
Layer.mergeAll(
43444363
McpAppModelContext.layerEmpty,
43454364
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
4365+
layerTurnItemPositionsTest,
43464366
Layer.mock(EventSink.EventSinkV2)({
43474367
write: () => Effect.succeed([]),
43484368
writeWithEffects: (input) =>

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
makeProviderFailureTurnItem,
5050
} from "@t3tools/provider-core/server/failure";
5151
import * as RunFinalizationService from "./RunFinalizationService.ts";
52+
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
5253

5354
export interface ProviderEventRoutingState {
5455
readonly ownedThreadIds: ReadonlySet<ThreadId>;
@@ -569,6 +570,7 @@ export const layer: Layer.Layer<
569570
| ProviderEventIngestor.ProviderEventIngestorV2
570571
| ServerSettings.ServerSettingsService
571572
| McpAppModelContext.McpAppModelContext
573+
| TurnItemPositionStore.TurnItemPositionStoreV2
572574
> = Layer.effect(
573575
RunExecutionServiceV2,
574576
Effect.gen(function* () {
@@ -578,6 +580,7 @@ export const layer: Layer.Layer<
578580
const providerEventIngestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
579581
const serverSettings = yield* ServerSettings.ServerSettingsService;
580582
const mcpAppModelContext = yield* McpAppModelContext.McpAppModelContext;
583+
const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
581584
const finalizationObserver = yield* RunFinalizationService.RunFinalizationObserver;
582585

583586
const writeFinalRunEvents = (input: {
@@ -1224,6 +1227,9 @@ export const layer: Layer.Layer<
12241227
return true;
12251228
});
12261229
const filterAssistantEvent = makeAssistantStreamingFilter(responseStreamingMode);
1230+
// Held text writes nothing, so its item would take a position only
1231+
// when finally written, behind every item that began after it.
1232+
const reservedHeldTurnItemIds = new Set<TurnItemId>();
12271233
const providerEventFiber = yield* eventSubscription.events.pipe(
12281234
Stream.filterEffect((event) =>
12291235
Ref.modify(eventRouting, (state) => routeProviderEvent(event, routeIdentity, state)),
@@ -1286,6 +1292,19 @@ export const layer: Layer.Layer<
12861292
// stream on this run's background probe.
12871293
yield* Ref.set(providerThreadOwnerLost, true);
12881294
}
1295+
} else if (
1296+
event.type === "turn_item.updated" &&
1297+
(event.turnItem.type === "assistant_message" ||
1298+
event.turnItem.type === "reasoning") &&
1299+
event.turnItem.streaming &&
1300+
!reservedHeldTurnItemIds.has(event.turnItem.id)
1301+
) {
1302+
yield* turnItemPositions.allocate({
1303+
threadId: event.turnItem.threadId,
1304+
turnItemId: event.turnItem.id,
1305+
runId: event.turnItem.runId,
1306+
});
1307+
reservedHeldTurnItemIds.add(event.turnItem.id);
12891308
}
12901309
if (event.type === "provider_thread.updated") {
12911310
if (event.providerThread.id === input.providerThread.id && storedEventCount > 0) {

‎apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ProviderThreadId,
1111
ProviderTurnId,
1212
ThreadId,
13+
TurnItemId,
1314
type ModelSelection,
1415
type OrchestrationV2DomainEvent,
1516
} from "@t3tools/contracts";
@@ -702,3 +703,120 @@ it.effect("starts a steer that missed the turn on the saved next-turn selection"
702703
}),
703704
),
704705
);
706+
707+
// A draft that never reaches a paragraph boundary is held back while it
708+
// streams. Codex only flushes it at turn end, after the final answer is written,
709+
// so the draft must already own its place from before the steer.
710+
it.effect("keeps a held commentary draft before the steer when turn end flushes it", () =>
711+
Effect.scoped(
712+
Effect.gen(function* () {
713+
const { events, started, layer, startFirstTurn } =
714+
yield* nextTurnSelectionHarness("steering-held-draft");
715+
yield* Effect.gen(function* () {
716+
const orchestrator = yield* Orchestrator.OrchestratorV2;
717+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
718+
const threadId = yield* startFirstTurn;
719+
const first = started[0]!;
720+
const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
721+
const now = yield* DateTime.now;
722+
const assistantItem = (id: string, text: string, streaming: boolean) =>
723+
({
724+
type: "turn_item.updated",
725+
driver,
726+
turnItem: {
727+
id: TurnItemId.make(id),
728+
threadId,
729+
runId: first.runId,
730+
nodeId: first.rootNodeId,
731+
providerThreadId: turn.providerThreadId,
732+
providerTurnId: turn.id,
733+
nativeItemRef: null,
734+
parentItemId: null,
735+
ordinal: 0,
736+
status: streaming ? "running" : "completed",
737+
title: null,
738+
startedAt: now,
739+
completedAt: streaming ? null : now,
740+
updatedAt: now,
741+
type: "assistant_message",
742+
messageId: MessageId.make(`message:${id}`),
743+
text,
744+
streaming,
745+
},
746+
}) satisfies ProviderAdapter.ProviderAdapterV2Event;
747+
const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
748+
orchestrator.streamDomainEvents.pipe(
749+
Stream.filter(predicate),
750+
Stream.take(1),
751+
Stream.runDrain,
752+
Effect.forkScoped,
753+
);
754+
755+
// The cut-off draft starts first; the commentary after it is written,
756+
// so the draft has been seen by the time the steer arrives.
757+
yield* Queue.offer(events, assistantItem("draft", "Let me look at the", true));
758+
const commentaryWritten = yield* watch(
759+
(event) => event.type === "turn-item.updated" && event.payload.id === "commentary",
760+
);
761+
yield* Queue.offer(events, assistantItem("commentary", "Checking the popover.", false));
762+
yield* Fiber.join(commentaryWritten);
763+
764+
yield* orchestrator.dispatch({
765+
type: "message.dispatch",
766+
commandId: CommandId.make("steer"),
767+
threadId,
768+
messageId: MessageId.make("message:steer"),
769+
text: "fix the popover",
770+
attachments: [],
771+
dispatchMode: { type: "steer_active", targetRunId: first.runId },
772+
createdBy: "user",
773+
creationSource: "web",
774+
});
775+
yield* worker.drain();
776+
777+
const settled = yield* watch(
778+
(event) =>
779+
event.type === "run.updated" &&
780+
event.payload.id === first.runId &&
781+
event.payload.status === "waiting",
782+
);
783+
yield* Queue.offer(events, assistantItem("answer", "All fixed.", false));
784+
yield* Queue.offer(events, assistantItem("draft", "Let me look at the", false));
785+
yield* Queue.offer(events, {
786+
type: "provider_turn.updated",
787+
driver,
788+
providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now },
789+
});
790+
yield* Queue.offer(events, {
791+
type: "turn.terminal",
792+
driver,
793+
providerThreadId: turn.providerThreadId,
794+
providerTurnId: turn.id,
795+
runOrdinal: first.runOrdinal,
796+
status: "completed",
797+
failure: null,
798+
threadDisposition: "reusable",
799+
});
800+
yield* Fiber.join(settled);
801+
yield* worker.drain();
802+
803+
const items = (yield* orchestrator.getThreadProjection(threadId)).turnItems
804+
.toSorted((left, right) => left.ordinal - right.ordinal)
805+
.flatMap((item) =>
806+
item.type === "user_message"
807+
? [String(item.messageId)]
808+
: item.type === "assistant_message"
809+
? [String(item.id)]
810+
: [],
811+
);
812+
assert.deepEqual(items, [
813+
"message:first",
814+
"draft",
815+
"commentary",
816+
"message:steer",
817+
"answer",
818+
]);
819+
}).pipe(Effect.provide(layer));
820+
}),
821+
),
822+
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ const layerRunExecutionServiceProvided = RunExecutionService.layer.pipe(
148148
layerEventSinkProvided,
149149
IdAllocator.layer,
150150
layerProviderEventIngestorProvided,
151+
TurnItemPositionStore.layer,
151152
),
152153
),
153154
);

‎apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ export function layerWithRegistry<Error>(
380380
IdAllocator.layer,
381381
layerProviderEventIngestorProvided,
382382
layerServerSettings,
383+
layerStores,
383384
),
384385
),
385386
);

‎apps/server/src/orchestration-v2/testkit/fixtures/opencode2_tool_call/output.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ export function assertOpenCode2ToolCallOutput(
2323
assertSemanticProjectionIntegrity(projection);
2424
assertVisibleTurnItemsMirrorLocalTurnItems(projection);
2525
// Buffered delivery keeps streaming text out of the read model until
26-
// `text.ended`, which OpenCode sends after both tool calls started.
26+
// `text.ended`, which OpenCode sends after both tool calls started. The text
27+
// keeps the position it took when it began, ahead of the tool calls.
2728
assertTurnItemTypeSequence(projection, [
2829
"user_message",
30+
"assistant_message",
2931
"dynamic_tool",
3032
"command_execution",
3133
"assistant_message",
32-
"assistant_message",
3334
"checkpoint",
3435
]);
3536
assertUserMessagesInclude(projection, [OPENCODE2_TOOL_CALL_PROMPT]);

0 commit comments

Comments
 (0)