Skip to content

Commit dfdd41d

Browse files
fix(server): resume long native threads without handoff budget
Continuing a long native conversation could fail before the provider received the new request when its recovery handoff no longer fit. Defer only verified same-thread history, keep the handoff available for later delivery, and exclude rolled-back or app-generated items from the transcript check. Implemented With: Codex via GPT-6 Luna on OpenAI through T3 Code
1 parent 31bfffb commit dfdd41d

3 files changed

Lines changed: 118 additions & 15 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,4 +640,32 @@ describe("handoff delivery", () => {
640640
assert.equal(calls, 0);
641641
}),
642642
);
643+
644+
it.effect("leaves a verified native-thread handoff pending when its wrapper cannot fit", () =>
645+
Effect.gen(function* () {
646+
let injections = 0;
647+
let persists = 0;
648+
const result = yield* deliverContextHandoffs({
649+
handoffs: [handoff],
650+
providerThread,
651+
budget: 0,
652+
deferIfOverBudget: true,
653+
alreadyDeliveredItemIds: new Set(),
654+
inject: () =>
655+
Effect.sync(() => {
656+
injections++;
657+
return true;
658+
}),
659+
persist: () =>
660+
Effect.sync(() => {
661+
persists++;
662+
}),
663+
});
664+
665+
yield* result.delivered;
666+
assert.equal(result.context, "");
667+
assert.equal(injections, 0);
668+
assert.equal(persists, 0);
669+
}),
670+
);
643671
});

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,36 @@ import * as RuntimePolicy from "./RuntimePolicy.ts";
4949

5050
const isDomainEvent = Schema.is(OrchestrationV2DomainEvent);
5151

52+
it("excludes rolled-back runs from native-thread history", () => {
53+
const providerThreadId = ProviderThreadId.make("provider_thread_native_history");
54+
const keptRunId = RunId.make("run_native_history_kept");
55+
const rolledBackRunId = RunId.make("run_native_history_rolled_back");
56+
const acceptedAttempts = [keptRunId, rolledBackRunId].map((runId, index) => ({
57+
id: RunAttemptId.make(`attempt_native_history_${index}`),
58+
runId,
59+
providerThreadId,
60+
nativeThreadId: "native-thread",
61+
}));
62+
const nativeInputRunIds = ProviderTurnStart.nativeInputRunIdsForThread({
63+
acceptedAttempts,
64+
runs: [
65+
{ id: keptRunId, status: "completed" },
66+
{ id: rolledBackRunId, status: "rolled_back" },
67+
] as const,
68+
providerThreadId,
69+
nativeThreadId: "native-thread",
70+
});
71+
72+
expect(nativeInputRunIds).toEqual(new Set([keptRunId]));
73+
});
74+
75+
it("only treats provider transcript items as native history", () => {
76+
expect(ProviderTurnStart.isNativeTranscriptHistoryKind("user_message")).toBe(true);
77+
expect(ProviderTurnStart.isNativeTranscriptHistoryKind("command_execution")).toBe(true);
78+
expect(ProviderTurnStart.isNativeTranscriptHistoryKind("run_interrupt_result")).toBe(false);
79+
expect(ProviderTurnStart.isNativeTranscriptHistoryKind("file_change")).toBe(false);
80+
});
81+
5282
const managedWorktreesDir = "/tmp/provider-turn-start-worktrees";
5383
// fs.realpath applies `..` and `.` before following symlinks.
5484
const normalize = (target: string) => {

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

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,33 @@ export class ProviderTurnStartError extends Schema.TaggedError<ProviderTurnStart
7070

7171
const isProviderTurnStartError = Schema.is(ProviderTurnStartError);
7272

73+
export const nativeInputRunIdsForThread = (input: {
74+
readonly acceptedAttempts: ReadonlyArray<
75+
Pick<OrchestrationV2RunAttempt, "runId" | "providerThreadId" | "nativeThreadId">
76+
>;
77+
readonly runs: ReadonlyArray<Pick<OrchestrationV2Run, "id" | "status">>;
78+
readonly providerThreadId: OrchestrationV2ProviderThread["id"];
79+
readonly nativeThreadId: string | undefined;
80+
}) => {
81+
const rolledBackRunIds = new Set(
82+
input.runs.filter((run) => run.status === "rolled_back").map((run) => run.id),
83+
);
84+
return new Set(
85+
input.acceptedAttempts
86+
.filter(
87+
(attempt) =>
88+
attempt.providerThreadId === input.providerThreadId &&
89+
attempt.nativeThreadId !== undefined &&
90+
attempt.nativeThreadId === input.nativeThreadId &&
91+
!rolledBackRunIds.has(attempt.runId),
92+
)
93+
.map((attempt) => attempt.runId),
94+
);
95+
};
96+
97+
export const isNativeTranscriptHistoryKind = (kind: string) =>
98+
kind === "user_message" || kind === "assistant_message" || kind === "command_execution";
99+
73100
/**
74101
* Claude refuses these before it reads the prompt: replacing a process running background work,
75102
* or launch arguments whose appended system prompt cannot be read.
@@ -1080,15 +1107,12 @@ export const layer: Layer.Layer<
10801107
(source) =>
10811108
source.providerThreadId === providerThread.id && deliveredAttemptIds.has(source.id),
10821109
);
1083-
const nativeInputRunIds = new Set(
1084-
acceptedAttempts
1085-
.filter(
1086-
(source) =>
1087-
source.nativeThreadId !== undefined &&
1088-
source.nativeThreadId === runningProviderThread.nativeThreadRef?.nativeId,
1089-
)
1090-
.map((source) => source.runId),
1091-
);
1110+
const nativeInputRunIds = nativeInputRunIdsForThread({
1111+
acceptedAttempts,
1112+
runs: projection.runs,
1113+
providerThreadId: providerThread.id,
1114+
nativeThreadId: runningProviderThread.nativeThreadRef?.nativeId,
1115+
});
10921116
const legacyInputRunIds = new Set(
10931117
acceptedAttempts
10941118
.filter((source) => source.nativeThreadId === undefined)
@@ -1161,13 +1185,16 @@ export const layer: Layer.Layer<
11611185
// A turn in this native thread is not enough: another provider's handoff
11621186
// may contain work the native transcript has never seen.
11631187
const itemIsInNativeThread = (item: {
1164-
readonly threadId: string;
1165-
readonly providerThreadId: string | null;
1166-
readonly runId: string | null;
1188+
readonly threadId: ThreadId;
1189+
readonly providerThreadId: OrchestrationV2TurnItem["providerThreadId"];
1190+
readonly runId: OrchestrationV2TurnItem["runId"];
1191+
readonly type?: string;
1192+
readonly kind?: string;
11671193
}) =>
11681194
item.threadId === projection.thread.id &&
11691195
item.providerThreadId === providerThread.id &&
11701196
item.runId !== null &&
1197+
isNativeTranscriptHistoryKind(item.kind ?? item.type ?? "") &&
11711198
nativeInputRunIds.has(item.runId);
11721199
const handoffIsAlreadyInNativeThread = (
11731200
handoff: OrchestrationV2ContextHandoff,
@@ -1235,10 +1262,28 @@ export const layer: Layer.Layer<
12351262
nativeThreadHasTurns &&
12361263
missedItems.length === 0 &&
12371264
handoffsToDeliver.length > 0;
1265+
const omittedSourceRunIds = new Set(
1266+
handoffsToDeliver.flatMap((handoff) =>
1267+
(handoff.history?.omittedItems ?? 0) === 0
1268+
? []
1269+
: projection.runs
1270+
.filter(
1271+
(source) =>
1272+
source.providerThreadId !== null &&
1273+
handoff.fromProviderThreadIds.includes(source.providerThreadId) &&
1274+
source.ordinal >= handoff.coveredRunOrdinals.from &&
1275+
source.ordinal <= handoff.coveredRunOrdinals.to,
1276+
)
1277+
.map((source) => source.id),
1278+
),
1279+
);
12381280
const sourceItemsById =
1239-
canDeferHandoff &&
1240-
handoffsToDeliver.some((handoff) => (handoff.history?.omittedItems ?? 0) > 0)
1241-
? new Map((yield* loadTurnStartHistory).map((item) => [item.id, item] as const))
1281+
canDeferHandoff && omittedSourceRunIds.size > 0
1282+
? new Map(
1283+
(yield* projectionStore.getTurnStartHistory(input.threadId, [
1284+
...omittedSourceRunIds,
1285+
])).map((item) => [item.id, item] as const),
1286+
)
12421287
: new Map<string, OrchestrationV2TurnItem>();
12431288
const delivery = yield* deliverContextHandoffs({
12441289
handoffs: handoffsToDeliver,

0 commit comments

Comments
 (0)