Skip to content

Commit aad7de3

Browse files
committed
fix(server): resume fallback keeps the latest turn when an older handoff is pending
1 parent 5307c23 commit aad7de3

2 files changed

Lines changed: 127 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,14 @@ export const layer: Layer.Layer<
724724
),
725725
createdAt,
726726
});
727-
effectiveHandoffs = [handoff, ...effectiveHandoffs];
727+
// Delivery ranks later handoffs as newer history. Pending handoffs from
728+
// earlier runs predate this summary; handoffs for this run (provider-switch
729+
// delta, merge-back) are newer and must stay last.
730+
effectiveHandoffs = [
731+
...effectiveHandoffs.filter((candidate) => candidate.targetRunId !== run.id),
732+
handoff,
733+
...effectiveHandoffs.filter((candidate) => candidate.targetRunId === run.id),
734+
];
728735
yield* eventSink.write({
729736
events: [
730737
{

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

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,125 @@ describe("orchestration v2 provider switching", () => {
11911191
),
11921192
);
11931193

1194+
it.live("keeps the latest turn when a resume fallback also delivers an older handoff", () =>
1195+
Effect.scoped(
1196+
Effect.gen(function* () {
1197+
const cwd = yield* checkpointWorkspace("resume-fallback-handoff-order");
1198+
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1199+
const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
1200+
const oldResult = `old result ${"o".repeat(2_500)}`;
1201+
const latestResult = `latest result ${"l".repeat(2_500)}`;
1202+
const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
1203+
makeTestAdapter({
1204+
instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1205+
driver: CLAUDE_DRIVER,
1206+
capabilities: ClaudeProviderCapabilitiesV2,
1207+
modelSelection: CLAUDE_MODEL_SELECTION,
1208+
responseByRunOrdinal: { 1: oldResult, 3: latestResult },
1209+
capturedTurns,
1210+
injectedHistory,
1211+
failResume: true,
1212+
failedRunOrdinals: new Set([2]),
1213+
nativeThreadGeneration: yield* Ref.make(0),
1214+
}),
1215+
]);
1216+
yield* Effect.gen(function* () {
1217+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1218+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1219+
const runAfterDetach = Effect.fn("runAfterDetach")(function* (
1220+
ordinal: number,
1221+
text: string,
1222+
) {
1223+
const providerSessionId = (yield* orchestrator.getThreadProjection(threadId))
1224+
.providerThreads[0]?.providerSessionId;
1225+
if (providerSessionId != null) {
1226+
yield* orchestrator.dispatch({
1227+
type: "provider-session.detach",
1228+
commandId: CommandId.make(`handoff-order:detach:${ordinal}`),
1229+
threadId,
1230+
providerSessionId,
1231+
});
1232+
yield* worker.drain();
1233+
}
1234+
yield* orchestrator.dispatch({
1235+
type: "message.dispatch",
1236+
commandId: CommandId.make(`handoff-order:${ordinal}`),
1237+
threadId,
1238+
messageId: MessageId.make(`handoff-order:${ordinal}`),
1239+
createdBy: "user",
1240+
creationSource: "web",
1241+
text,
1242+
attachments: [],
1243+
modelSelection: CLAUDE_MODEL_SELECTION,
1244+
dispatchMode: { type: "start_immediately" },
1245+
});
1246+
yield* orchestrator.streamStoredEvents.pipe(
1247+
Stream.filter(
1248+
({ event }) =>
1249+
event.type === "run.updated" &&
1250+
event.payload.ordinal === ordinal &&
1251+
(event.payload.status === "completed" || event.payload.status === "failed"),
1252+
),
1253+
Stream.runHead,
1254+
);
1255+
yield* worker.drain();
1256+
});
1257+
yield* orchestrator.dispatch({
1258+
type: "thread.create",
1259+
commandId: CommandId.make("handoff-order:create"),
1260+
threadId,
1261+
projectId,
1262+
createdBy: "user",
1263+
creationSource: "web",
1264+
title: "Resume fallback handoff order",
1265+
modelSelection: CLAUDE_MODEL_SELECTION,
1266+
runtimeMode: "full-access",
1267+
interactionMode: "default",
1268+
branch: null,
1269+
worktreePath: null,
1270+
});
1271+
yield* runAfterDetach(1, `Original brief ${"b".repeat(9_000)}`);
1272+
yield* runAfterDetach(2, "Request 2");
1273+
yield* runAfterDetach(3, "Request 3");
1274+
const historyBeforeRun4 = (yield* Ref.get(injectedHistory)).length;
1275+
yield* runAfterDetach(4, "Request 4");
1276+
1277+
const projection = yield* orchestrator.getThreadProjection(threadId);
1278+
assert.deepEqual(
1279+
projection.runs.map((run) => run.status),
1280+
["completed", "failed", "completed", "completed"],
1281+
);
1282+
const run4NativeThreadId = (yield* Ref.get(capturedTurns)).at(-1)?.nativeThreadId;
1283+
assert.equal(
1284+
projection.contextHandoffs.find(
1285+
(handoff) => handoff.targetRunId === projection.runs[1]?.id,
1286+
)?.delivery?.nativeThreadId,
1287+
run4NativeThreadId,
1288+
);
1289+
const run4History = yield* encodeJson(
1290+
(yield* Ref.get(injectedHistory)).slice(historyBeforeRun4),
1291+
);
1292+
assert.include(run4History, "Original brief");
1293+
assert.include(run4History, latestResult);
1294+
}).pipe(
1295+
Effect.provide(
1296+
ProviderReplayHarness.layerWithRegistry(
1297+
{
1298+
name: "resume-fallback-handoff-order",
1299+
runtimePolicyOverride: {
1300+
cwd,
1301+
approvalPolicy: "never",
1302+
sandboxPolicy: { type: "readOnly" },
1303+
},
1304+
},
1305+
layerRegistry,
1306+
),
1307+
),
1308+
);
1309+
}),
1310+
),
1311+
);
1312+
11941313
it.live("keeps the native session after turns refused before reaching the provider", () =>
11951314
Effect.scoped(
11961315
Effect.gen(function* () {

0 commit comments

Comments
 (0)