Skip to content

Commit ef58cd9

Browse files
fix(server): exclude mixed native-thread runs from handoff history
Runs accepted on multiple native threads cannot be assigned to a single transcript using only the run ID. Exclude those runs from handoff history membership checks. Add focused coverage for the mixed-thread case. Implemented With: Codex via GPT-6 Luna on OpenAI through T3 Code
1 parent 0058b4e commit ef58cd9

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ it("excludes rolled-back runs from native-thread history", () => {
7272
expect(nativeInputRunIds).toEqual(new Set([keptRunId]));
7373
});
7474

75+
it("does not treat runs accepted on multiple native threads as native history", () => {
76+
const providerThreadId = ProviderThreadId.make("provider_thread_mixed_native_history");
77+
const mixedRunId = RunId.make("run_mixed_native_history");
78+
const singleThreadRunId = RunId.make("run_single_native_history");
79+
const attempts = [
80+
{ runId: mixedRunId, providerThreadId, nativeThreadId: "current-native-thread" },
81+
{ runId: mixedRunId, providerThreadId, nativeThreadId: "older-native-thread" },
82+
{ runId: singleThreadRunId, providerThreadId, nativeThreadId: "current-native-thread" },
83+
];
84+
85+
expect(ProviderTurnStart.mixedNativeThreadRunIdsForThread(attempts, providerThreadId)).toEqual(
86+
new Set([mixedRunId]),
87+
);
88+
});
89+
7590
it("only treats provider transcript items as native history", () => {
7691
expect(ProviderTurnStart.isNativeTranscriptHistoryKind("user_message")).toBe(true);
7792
expect(ProviderTurnStart.isNativeTranscriptHistoryKind("command_execution")).toBe(true);

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ export const nativeInputRunIdsForThread = (input: {
9494
);
9595
};
9696

97+
export const mixedNativeThreadRunIdsForThread = (
98+
acceptedAttempts: ReadonlyArray<
99+
Pick<OrchestrationV2RunAttempt, "runId" | "providerThreadId" | "nativeThreadId">
100+
>,
101+
providerThreadId: OrchestrationV2ProviderThread["id"],
102+
) => {
103+
const nativeThreadIdsByRunId = new Map<RunId, Set<string>>();
104+
for (const attempt of acceptedAttempts) {
105+
if (attempt.providerThreadId !== providerThreadId || attempt.nativeThreadId === undefined)
106+
continue;
107+
const nativeThreadIds = nativeThreadIdsByRunId.get(attempt.runId) ?? new Set<string>();
108+
nativeThreadIds.add(attempt.nativeThreadId);
109+
nativeThreadIdsByRunId.set(attempt.runId, nativeThreadIds);
110+
}
111+
return new Set(
112+
[...nativeThreadIdsByRunId]
113+
.filter(([, nativeThreadIds]) => nativeThreadIds.size > 1)
114+
.map(([runId]) => runId),
115+
);
116+
};
117+
97118
export const isNativeTranscriptHistoryKind = (kind: string) =>
98119
kind === "user_message" || kind === "assistant_message" || kind === "command_execution";
99120

@@ -1105,6 +1126,10 @@ export const layer: Layer.Layer<
11051126
providerThreadId: providerThread.id,
11061127
nativeThreadId: runningProviderThread.nativeThreadRef?.nativeId,
11071128
});
1129+
const mixedNativeThreadRunIds = mixedNativeThreadRunIdsForThread(
1130+
acceptedAttempts,
1131+
providerThread.id,
1132+
);
11081133
const legacyInputRunIds = new Set(
11091134
acceptedAttempts
11101135
.filter((source) => source.nativeThreadId === undefined)
@@ -1181,7 +1206,8 @@ export const layer: Layer.Layer<
11811206
item.providerThreadId === providerThread.id &&
11821207
item.runId !== null &&
11831208
isNativeTranscriptHistoryKind(item.kind ?? item.type ?? "") &&
1184-
nativeInputRunIds.has(item.runId);
1209+
nativeInputRunIds.has(item.runId) &&
1210+
!mixedNativeThreadRunIds.has(item.runId);
11851211
const handoffIsAlreadyInNativeThread = (
11861212
handoff: OrchestrationV2ContextHandoff,
11871213
sourceItemsById: ReadonlyMap<string, OrchestrationV2TurnItem>,

0 commit comments

Comments
 (0)