Skip to content

Commit a917d84

Browse files
authored
fix(server): held assistant drafts keep their position when flushed at turn end (pingdotgg#19023)
1 parent ca2afd8 commit a917d84

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)({
@@ -752,6 +758,7 @@ it.effect("passes the thread's MCP app context to the provider under a safe key"
752758
Layer.mock(CheckpointService.CheckpointServiceV2)({
753759
captureBaseline: () => Effect.void,
754760
}),
761+
layerTurnItemPositionsTest,
755762
Layer.mock(EventSink.EventSinkV2)({}),
756763
IdAllocator.layer,
757764
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
@@ -779,6 +786,7 @@ it.effect("fails the run when its ownership check cannot be read before calling
779786
Layer.mergeAll(
780787
McpAppModelContext.layerEmpty,
781788
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
789+
layerTurnItemPositionsTest,
782790
Layer.mock(EventSink.EventSinkV2)({
783791
writeIfRunCurrent: (input) =>
784792
Ref.update(writes, (current) => [...current, ...input.events]).pipe(
@@ -1059,6 +1067,7 @@ it.effect("starts the provider when checkpoint baseline capture fails", () =>
10591067
}),
10601068
),
10611069
}),
1070+
layerTurnItemPositionsTest,
10621071
Layer.mock(EventSink.EventSinkV2)({
10631072
writeWithEffects: (input) =>
10641073
Ref.update(writes, (current) => [
@@ -1171,6 +1180,7 @@ it.effect.each(["failure", "interruption", "stale-attempt", "start-guard"] as co
11711180
captureBaseline: () =>
11721181
scenario === "start-guard" ? Effect.void : Effect.die("not reached"),
11731182
}),
1183+
layerTurnItemPositionsTest,
11741184
Layer.mock(EventSink.EventSinkV2)({
11751185
writeIfRunCurrent: (input) =>
11761186
Effect.gen(function* () {
@@ -1337,6 +1347,7 @@ it.effect("ingests new member threads of an owned child after the root turn term
13371347
Layer.mergeAll(
13381348
McpAppModelContext.layerEmpty,
13391349
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
1350+
layerTurnItemPositionsTest,
13401351
Layer.mock(EventSink.EventSinkV2)({
13411352
write: () => Effect.succeed([]),
13421353
writeWithEffects: (input) =>
@@ -1566,6 +1577,7 @@ it.effect("ends a nested native subagent and its thread when the root run is int
15661577
Layer.mergeAll(
15671578
McpAppModelContext.layerEmpty,
15681579
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
1580+
layerTurnItemPositionsTest,
15691581
Layer.mock(EventSink.EventSinkV2)({
15701582
write: () => Effect.succeed([]),
15711583
writeWithEffects: (input) =>
@@ -1923,6 +1935,7 @@ it.effect(
19231935
Layer.mock(CheckpointService.CheckpointServiceV2)({
19241936
captureBaseline: () => Effect.void,
19251937
}),
1938+
layerTurnItemPositionsTest,
19261939
Layer.mock(EventSink.EventSinkV2)({
19271940
write: () => Effect.succeed([]),
19281941
writeWithEffects: (input) =>
@@ -2136,6 +2149,7 @@ it.effect("drops late root provider-thread writes from a superseded attempt", ()
21362149
Layer.mergeAll(
21372150
McpAppModelContext.layerEmpty,
21382151
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
2152+
layerTurnItemPositionsTest,
21392153
Layer.mock(EventSink.EventSinkV2)({
21402154
write: () => Effect.succeed([]),
21412155
writeWithEffects: (input) =>
@@ -2337,6 +2351,7 @@ it.effect(
23372351
Layer.mock(CheckpointService.CheckpointServiceV2)({
23382352
captureBaseline: () => Effect.void,
23392353
}),
2354+
layerTurnItemPositionsTest,
23402355
Layer.mock(EventSink.EventSinkV2)({
23412356
write: () => Effect.succeed([]),
23422357
writeWithEffects: (input) =>
@@ -2506,6 +2521,7 @@ it.effect(
25062521
Layer.mock(CheckpointService.CheckpointServiceV2)({
25072522
captureBaseline: () => Effect.void,
25082523
}),
2524+
layerTurnItemPositionsTest,
25092525
Layer.mock(EventSink.EventSinkV2)({
25102526
write: () => Effect.succeed([]),
25112527
writeWithEffects: (input) =>
@@ -2668,6 +2684,7 @@ it.effect(
26682684
Layer.mock(CheckpointService.CheckpointServiceV2)({
26692685
captureBaseline: () => Effect.void,
26702686
}),
2687+
layerTurnItemPositionsTest,
26712688
Layer.mock(EventSink.EventSinkV2)({
26722689
write: () => Effect.succeed([]),
26732690
writeWithEffects: (input) =>
@@ -3108,6 +3125,7 @@ it.effect(
31083125
Layer.mock(CheckpointService.CheckpointServiceV2)({
31093126
captureBaseline: () => Effect.void,
31103127
}),
3128+
layerTurnItemPositionsTest,
31113129
Layer.mock(EventSink.EventSinkV2)({
31123130
write: () => Effect.succeed([]),
31133131
writeWithEffects: (input) =>
@@ -3885,6 +3903,7 @@ function captureRootRunTermination(input: {
38853903
forThread: () => input.appContext ?? Effect.succeed([]),
38863904
}),
38873905
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
3906+
layerTurnItemPositionsTest,
38883907
Layer.mock(EventSink.EventSinkV2)({
38893908
write: (payload) =>
38903909
Effect.gen(function* () {
@@ -4370,6 +4389,7 @@ function runBackgroundItemScenario(
43704389
Layer.mergeAll(
43714390
McpAppModelContext.layerEmpty,
43724391
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
4392+
layerTurnItemPositionsTest,
43734393
Layer.mock(EventSink.EventSinkV2)({
43744394
write: () => Effect.succeed([]),
43754395
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>;
@@ -564,6 +565,7 @@ export const layer: Layer.Layer<
564565
| ProviderEventIngestor.ProviderEventIngestorV2
565566
| ServerSettings.ServerSettingsService
566567
| McpAppModelContext.McpAppModelContext
568+
| TurnItemPositionStore.TurnItemPositionStoreV2
567569
> = Layer.effect(
568570
RunExecutionServiceV2,
569571
Effect.gen(function* () {
@@ -573,6 +575,7 @@ export const layer: Layer.Layer<
573575
const providerEventIngestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
574576
const serverSettings = yield* ServerSettings.ServerSettingsService;
575577
const mcpAppModelContext = yield* McpAppModelContext.McpAppModelContext;
578+
const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
576579
const finalizationObserver = yield* RunFinalizationService.RunFinalizationObserver;
577580

578581
const writeFinalRunEvents = (input: {
@@ -1195,6 +1198,9 @@ export const layer: Layer.Layer<
11951198
return true;
11961199
});
11971200
const filterAssistantEvent = makeAssistantStreamingFilter(responseStreamingMode);
1201+
// Held text writes nothing, so its item would take a position only
1202+
// when finally written, behind every item that began after it.
1203+
const reservedHeldTurnItemIds = new Set<TurnItemId>();
11981204
const providerEventFiber = yield* eventSubscription.events.pipe(
11991205
Stream.filterEffect((event) =>
12001206
Ref.modify(eventRouting, (state) => routeProviderEvent(event, routeIdentity, state)),
@@ -1250,6 +1256,19 @@ export const layer: Layer.Layer<
12501256
) {
12511257
yield* Ref.set(providerThreadOwnerLost, true);
12521258
}
1259+
} else if (
1260+
event.type === "turn_item.updated" &&
1261+
(event.turnItem.type === "assistant_message" ||
1262+
event.turnItem.type === "reasoning") &&
1263+
event.turnItem.streaming &&
1264+
!reservedHeldTurnItemIds.has(event.turnItem.id)
1265+
) {
1266+
yield* turnItemPositions.allocate({
1267+
threadId: event.turnItem.threadId,
1268+
turnItemId: event.turnItem.id,
1269+
runId: event.turnItem.runId,
1270+
});
1271+
reservedHeldTurnItemIds.add(event.turnItem.id);
12531272
}
12541273
if (event.type === "provider_thread.updated") {
12551274
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";
@@ -700,3 +701,120 @@ it.effect("starts a steer that missed the turn on the saved next-turn selection"
700701
}),
701702
),
702703
);
704+
705+
// A draft that never reaches a paragraph boundary is held back while it
706+
// streams. Codex only flushes it at turn end, after the final answer is written,
707+
// so the draft must already own its place from before the steer.
708+
it.effect("keeps a held commentary draft before the steer when turn end flushes it", () =>
709+
Effect.scoped(
710+
Effect.gen(function* () {
711+
const { events, started, layer, startFirstTurn } =
712+
yield* nextTurnSelectionHarness("steering-held-draft");
713+
yield* Effect.gen(function* () {
714+
const orchestrator = yield* Orchestrator.OrchestratorV2;
715+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
716+
const threadId = yield* startFirstTurn;
717+
const first = started[0]!;
718+
const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
719+
const now = yield* DateTime.now;
720+
const assistantItem = (id: string, text: string, streaming: boolean) =>
721+
({
722+
type: "turn_item.updated",
723+
driver,
724+
turnItem: {
725+
id: TurnItemId.make(id),
726+
threadId,
727+
runId: first.runId,
728+
nodeId: first.rootNodeId,
729+
providerThreadId: turn.providerThreadId,
730+
providerTurnId: turn.id,
731+
nativeItemRef: null,
732+
parentItemId: null,
733+
ordinal: 0,
734+
status: streaming ? "running" : "completed",
735+
title: null,
736+
startedAt: now,
737+
completedAt: streaming ? null : now,
738+
updatedAt: now,
739+
type: "assistant_message",
740+
messageId: MessageId.make(`message:${id}`),
741+
text,
742+
streaming,
743+
},
744+
}) satisfies ProviderAdapter.ProviderAdapterV2Event;
745+
const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
746+
orchestrator.streamDomainEvents.pipe(
747+
Stream.filter(predicate),
748+
Stream.take(1),
749+
Stream.runDrain,
750+
Effect.forkScoped,
751+
);
752+
753+
// The cut-off draft starts first; the commentary after it is written,
754+
// so the draft has been seen by the time the steer arrives.
755+
yield* Queue.offer(events, assistantItem("draft", "Let me look at the", true));
756+
const commentaryWritten = yield* watch(
757+
(event) => event.type === "turn-item.updated" && event.payload.id === "commentary",
758+
);
759+
yield* Queue.offer(events, assistantItem("commentary", "Checking the popover.", false));
760+
yield* Fiber.join(commentaryWritten);
761+
762+
yield* orchestrator.dispatch({
763+
type: "message.dispatch",
764+
commandId: CommandId.make("steer"),
765+
threadId,
766+
messageId: MessageId.make("message:steer"),
767+
text: "fix the popover",
768+
attachments: [],
769+
dispatchMode: { type: "steer_active", targetRunId: first.runId },
770+
createdBy: "user",
771+
creationSource: "web",
772+
});
773+
yield* worker.drain();
774+
775+
const settled = yield* watch(
776+
(event) =>
777+
event.type === "run.updated" &&
778+
event.payload.id === first.runId &&
779+
event.payload.status === "waiting",
780+
);
781+
yield* Queue.offer(events, assistantItem("answer", "All fixed.", false));
782+
yield* Queue.offer(events, assistantItem("draft", "Let me look at the", false));
783+
yield* Queue.offer(events, {
784+
type: "provider_turn.updated",
785+
driver,
786+
providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now },
787+
});
788+
yield* Queue.offer(events, {
789+
type: "turn.terminal",
790+
driver,
791+
providerThreadId: turn.providerThreadId,
792+
providerTurnId: turn.id,
793+
runOrdinal: first.runOrdinal,
794+
status: "completed",
795+
failure: null,
796+
threadDisposition: "reusable",
797+
});
798+
yield* Fiber.join(settled);
799+
yield* worker.drain();
800+
801+
const items = (yield* orchestrator.getThreadProjection(threadId)).turnItems
802+
.toSorted((left, right) => left.ordinal - right.ordinal)
803+
.flatMap((item) =>
804+
item.type === "user_message"
805+
? [String(item.messageId)]
806+
: item.type === "assistant_message"
807+
? [String(item.id)]
808+
: [],
809+
);
810+
assert.deepEqual(items, [
811+
"message:first",
812+
"draft",
813+
"commentary",
814+
"message:steer",
815+
"answer",
816+
]);
817+
}).pipe(Effect.provide(layer));
818+
}),
819+
),
820+
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ const layerRunExecutionServiceProvided = RunExecutionService.layer.pipe(
155155
layerEventSinkProvided,
156156
IdAllocator.layer,
157157
layerProviderEventIngestorProvided,
158+
TurnItemPositionStore.layer,
158159
),
159160
),
160161
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ export function layerWithRegistry<Error>(
385385
IdAllocator.layer,
386386
layerProviderEventIngestorProvided,
387387
layerServerSettings,
388+
layerStores,
388389
),
389390
),
390391
);

‎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
@@ -22,13 +22,14 @@ export function assertOpenCode2ToolCallOutput(
2222
assertSemanticProjectionIntegrity(projection);
2323
assertVisibleTurnItemsMirrorLocalTurnItems(projection);
2424
// Buffered delivery keeps streaming text out of the read model until
25-
// `text.ended`, which OpenCode sends after both tool calls started.
25+
// `text.ended`, which OpenCode sends after both tool calls started. The text
26+
// keeps the position it took when it began, ahead of the tool calls.
2627
assertTurnItemTypeSequence(projection, [
2728
"user_message",
29+
"assistant_message",
2830
"dynamic_tool",
2931
"command_execution",
3032
"assistant_message",
31-
"assistant_message",
3233
"checkpoint",
3334
]);
3435
assertUserMessagesInclude(projection, [OPENCODE2_TOOL_CALL_PROMPT]);

0 commit comments

Comments
 (0)