Skip to content

Commit 54333b1

Browse files
committed
fix(server): ignore legacy attempts once a native session is replaced
Attempts without a native id predate native identity, so they can only belong to the native thread the provider thread started with. Count them only while that thread is unchanged and no attempt has recorded a native id yet; otherwise the fresh-session fallback still resumed the new id.
1 parent 5d73e3a commit 54333b1

2 files changed

Lines changed: 128 additions & 99 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,19 @@ export const layer: Layer.Layer<
12321232
.filter((turn) => turn.providerThreadId === providerThread.id)
12331233
.map((turn) => turn.ordinal),
12341234
) + 1,
1235-
nativeThreadHasTurns: nativeInputRunIds.size > 0 || legacyInputRunIds.size > 0,
1235+
// Legacy attempts predate native identity, so they can only have run on
1236+
// the native thread this provider thread was created with: one that was
1237+
// not just replaced, and with no attempt yet recorded under any native id.
1238+
nativeThreadHasTurns:
1239+
nativeInputRunIds.size > 0 ||
1240+
(legacyInputRunIds.size > 0 &&
1241+
sameNativeThread &&
1242+
!projection.attempts.some(
1243+
(source) =>
1244+
source.providerThreadId === providerThread.id &&
1245+
source.id !== attempt.id &&
1246+
source.nativeThreadId !== undefined,
1247+
)),
12361248
shouldStartProviderTurn: runControls.shouldStartProviderTurn,
12371249
shouldFinalizeRun: runControls.shouldFinalizeRun,
12381250
hasUnpairedRunInterruptRequest: runControls.hasUnpairedRunInterruptRequest,

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

Lines changed: 115 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,109 +1059,126 @@ describe("orchestration v2 provider switching", () => {
10591059
),
10601060
);
10611061

1062-
it.live("starts a replacement native thread as new after the resume fallback", () =>
1063-
Effect.scoped(
1064-
Effect.gen(function* () {
1065-
const cwd = yield* checkpointWorkspace("resume-fallback-native-turns");
1066-
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1067-
const failResumeOnce = yield* Ref.make(false);
1068-
const generation = yield* Ref.make(0);
1069-
const registry = ProviderAdapterRegistry.makeLayer([
1070-
makeTestAdapter({
1071-
instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1072-
driver: CLAUDE_DRIVER,
1073-
capabilities: ClaudeProviderCapabilitiesV2,
1074-
modelSelection: CLAUDE_MODEL_SELECTION,
1075-
responseByRunOrdinal: {},
1076-
capturedTurns,
1077-
failResumeOnce,
1078-
nativeThreadGeneration: generation,
1079-
}),
1080-
]);
1081-
yield* Effect.gen(function* () {
1082-
const orchestrator = yield* Orchestrator.OrchestratorV2;
1083-
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1084-
const runToCompletion = (ordinal: number) =>
1085-
Effect.gen(function* () {
1086-
yield* orchestrator.dispatch({
1087-
type: "message.dispatch",
1088-
commandId: CommandId.make(`resume-fallback:${ordinal}`),
1089-
threadId,
1090-
messageId: MessageId.make(`resume-fallback:${ordinal}`),
1091-
createdBy: "user",
1092-
creationSource: "web",
1093-
text: `Request ${ordinal}`,
1094-
attachments: [],
1095-
modelSelection: CLAUDE_MODEL_SELECTION,
1096-
dispatchMode: { type: "start_immediately" },
1062+
it.live.each(["native", "legacy"] as const)(
1063+
"starts a replacement native thread as new after the resume fallback with %s attempts",
1064+
(attempts) =>
1065+
Effect.scoped(
1066+
Effect.gen(function* () {
1067+
const cwd = yield* checkpointWorkspace("resume-fallback-native-turns");
1068+
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1069+
const failResumeOnce = yield* Ref.make(false);
1070+
const generation = yield* Ref.make(0);
1071+
const registry = ProviderAdapterRegistry.makeLayer([
1072+
makeTestAdapter({
1073+
instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1074+
driver: CLAUDE_DRIVER,
1075+
capabilities: ClaudeProviderCapabilitiesV2,
1076+
modelSelection: CLAUDE_MODEL_SELECTION,
1077+
responseByRunOrdinal: {},
1078+
capturedTurns,
1079+
failResumeOnce,
1080+
nativeThreadGeneration: generation,
1081+
}),
1082+
]);
1083+
yield* Effect.gen(function* () {
1084+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1085+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1086+
const runToCompletion = (ordinal: number) =>
1087+
Effect.gen(function* () {
1088+
yield* orchestrator.dispatch({
1089+
type: "message.dispatch",
1090+
commandId: CommandId.make(`resume-fallback:${ordinal}`),
1091+
threadId,
1092+
messageId: MessageId.make(`resume-fallback:${ordinal}`),
1093+
createdBy: "user",
1094+
creationSource: "web",
1095+
text: `Request ${ordinal}`,
1096+
attachments: [],
1097+
modelSelection: CLAUDE_MODEL_SELECTION,
1098+
dispatchMode: { type: "start_immediately" },
1099+
});
1100+
yield* orchestrator.streamStoredEvents.pipe(
1101+
Stream.filter(
1102+
({ event }) =>
1103+
event.type === "run.updated" &&
1104+
event.payload.ordinal === ordinal &&
1105+
(event.payload.status === "completed" || event.payload.status === "failed"),
1106+
),
1107+
Stream.runHead,
1108+
);
1109+
yield* worker.drain();
10971110
});
1098-
yield* orchestrator.streamStoredEvents.pipe(
1099-
Stream.filter(
1100-
({ event }) =>
1101-
event.type === "run.updated" &&
1102-
event.payload.ordinal === ordinal &&
1103-
(event.payload.status === "completed" || event.payload.status === "failed"),
1104-
),
1105-
Stream.runHead,
1106-
);
1107-
yield* worker.drain();
1111+
yield* orchestrator.dispatch({
1112+
type: "thread.create",
1113+
commandId: CommandId.make("resume-fallback:create"),
1114+
threadId,
1115+
projectId,
1116+
createdBy: "user",
1117+
creationSource: "web",
1118+
title: "Resume fallback",
1119+
modelSelection: CLAUDE_MODEL_SELECTION,
1120+
runtimeMode: "full-access",
1121+
interactionMode: "default",
1122+
branch: null,
1123+
worktreePath: null,
11081124
});
1109-
yield* orchestrator.dispatch({
1110-
type: "thread.create",
1111-
commandId: CommandId.make("resume-fallback:create"),
1112-
threadId,
1113-
projectId,
1114-
createdBy: "user",
1115-
creationSource: "web",
1116-
title: "Resume fallback",
1117-
modelSelection: CLAUDE_MODEL_SELECTION,
1118-
runtimeMode: "full-access",
1119-
interactionMode: "default",
1120-
branch: null,
1121-
worktreePath: null,
1122-
});
1123-
yield* runToCompletion(1);
1124-
// A detached session resumes on the next turn, as after a restart.
1125-
const providerThread = (yield* orchestrator.getThreadProjection(threadId))
1126-
.providerThreads[0]!;
1127-
yield* orchestrator.dispatch({
1128-
type: "provider-session.detach",
1129-
commandId: CommandId.make("resume-fallback:detach"),
1130-
threadId,
1131-
providerSessionId: providerThread.providerSessionId!,
1132-
});
1133-
yield* worker.drain();
1134-
yield* Ref.set(failResumeOnce, true);
1135-
yield* runToCompletion(2);
1136-
yield* runToCompletion(3);
1125+
yield* runToCompletion(1);
1126+
// A detached session resumes on the next turn, as after a restart.
1127+
const providerThread = (yield* orchestrator.getThreadProjection(threadId))
1128+
.providerThreads[0]!;
1129+
yield* orchestrator.dispatch({
1130+
type: "provider-session.detach",
1131+
commandId: CommandId.make("resume-fallback:detach"),
1132+
threadId,
1133+
providerSessionId: providerThread.providerSessionId!,
1134+
});
1135+
yield* worker.drain();
1136+
if (attempts === "legacy") {
1137+
// Attempts persisted before native identity existed carry no native id.
1138+
const existing = yield* orchestrator.getThreadProjection(threadId);
1139+
yield* (yield* EventSink.EventSinkV2).write({
1140+
events: existing.attempts.map(
1141+
({ nativeThreadId: _nativeThreadId, ...legacy }, index) => ({
1142+
id: EventId.make(`resume-fallback-legacy-attempt:${index}`),
1143+
type: "run-attempt.updated" as const,
1144+
threadId,
1145+
occurredAt: existing.thread.createdAt,
1146+
payload: legacy,
1147+
}),
1148+
),
1149+
});
1150+
}
1151+
yield* Ref.set(failResumeOnce, true);
1152+
yield* runToCompletion(2);
1153+
yield* runToCompletion(3);
11371154

1138-
const turns = yield* Ref.get(capturedTurns);
1139-
assert.equal(turns.length, 3);
1140-
assert.equal(yield* Ref.get(generation), 2);
1141-
// The fallback binds a new native thread to a provider thread that
1142-
// already has a turn, so the provider must not resume it.
1143-
assert.notEqual(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId);
1144-
assert.isAbove(turns[1]?.providerTurnOrdinal ?? 0, 1);
1145-
assert.isFalse(turns[1]?.nativeThreadHasTurns);
1146-
assert.equal(turns[2]?.nativeThreadId, turns[1]?.nativeThreadId);
1147-
assert.isTrue(turns[2]?.nativeThreadHasTurns);
1148-
}).pipe(
1149-
Effect.provide(
1150-
makeOrchestratorV2ReplayLayerWithRegistry(
1151-
{
1152-
name: "resume-fallback-native-turns",
1153-
runtimePolicyOverride: {
1154-
cwd,
1155-
approvalPolicy: "never",
1156-
sandboxPolicy: { type: "readOnly" },
1155+
const turns = yield* Ref.get(capturedTurns);
1156+
assert.equal(turns.length, 3);
1157+
assert.equal(yield* Ref.get(generation), 2);
1158+
// The fallback binds a new native thread to a provider thread that
1159+
// already has a turn, so the provider must not resume it.
1160+
assert.notEqual(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId);
1161+
assert.isAbove(turns[1]?.providerTurnOrdinal ?? 0, 1);
1162+
assert.isFalse(turns[1]?.nativeThreadHasTurns);
1163+
assert.equal(turns[2]?.nativeThreadId, turns[1]?.nativeThreadId);
1164+
assert.isTrue(turns[2]?.nativeThreadHasTurns);
1165+
}).pipe(
1166+
Effect.provide(
1167+
makeOrchestratorV2ReplayLayerWithRegistry(
1168+
{
1169+
name: "resume-fallback-native-turns",
1170+
runtimePolicyOverride: {
1171+
cwd,
1172+
approvalPolicy: "never",
1173+
sandboxPolicy: { type: "readOnly" },
1174+
},
11571175
},
1158-
},
1159-
registry,
1176+
registry,
1177+
),
11601178
),
1161-
),
1162-
);
1163-
}),
1164-
),
1179+
);
1180+
}),
1181+
),
11651182
);
11661183

11671184
it.live.each(

0 commit comments

Comments
 (0)