Skip to content

Commit 0d793e1

Browse files
committed
fix(server): filter local commands from resume handoffs
1 parent 3c1e0d9 commit 0d793e1

3 files changed

Lines changed: 52 additions & 30 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ it("does not commit running state when inherited background routing cannot be re
7676
nodes: [{ id: rootNodeId, checkpointScopeId }],
7777
attempts: [{ id: attemptId }],
7878
providerThreads: [{ id: providerThreadId, providerSessionId }],
79+
providerTurns: [],
7980
messages: [{ id: messageId, text: "Continue", attachments: [] }],
8081
checkpointScopes: [{ id: checkpointScopeId }],
8182
contextHandoffs: [],
@@ -843,7 +844,10 @@ for (const previousMessages of [[], ["/compact", " /COMPACT "]]) {
843844
expect(harness.open).not.toHaveBeenCalled();
844845
expect(harness.tryHandlePromptCommand).not.toHaveBeenCalled();
845846
expect(harness.startRootRun).not.toHaveBeenCalled();
846-
expect(harness.projection().runs.at(-1)).toMatchObject({ status: "failed", startedAt: null });
847+
expect(harness.projection().runs.at(-1)).toMatchObject({
848+
status: "failed",
849+
startedAt: null,
850+
});
847851
expect(harness.projection().turnItems).toMatchObject([
848852
{
849853
type: "error",

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,31 @@ export const layer: Layer.Layer<
280280
cause: `Run ${runId} is missing its execution projection state.`,
281281
});
282282
}
283+
const deliveredAttemptIds = new Set(
284+
projection.providerTurns.map((turn) => turn.runAttemptId),
285+
);
286+
const readHandoffHistory = Effect.fn(function* (runIds?: ReadonlyArray<RunId>) {
287+
const items = yield* projectionStore.getTurnStartHistory(input.threadId, runIds);
288+
const maintenanceMessageIds = new Set(
289+
items.flatMap((item) =>
290+
item.type === "user_message" &&
291+
item.attachments.length === 0 &&
292+
["/compact", "/logout"].includes(item.text.trim().toLowerCase())
293+
? [item.messageId]
294+
: [],
295+
),
296+
);
297+
const excludedRunIds = new Set(
298+
projection.runs
299+
.filter(
300+
(source) =>
301+
maintenanceMessageIds.has(source.userMessageId) &&
302+
!deliveredAttemptIds.has(source.activeAttemptId),
303+
)
304+
.map((source) => source.id),
305+
);
306+
return items.filter((item) => item.runId === null || !excludedRunIds.has(item.runId));
307+
});
283308
// Settles a run that never reached the provider: one signal turn item plus
284309
// terminal run, attempt and root node, written only while the run is still
285310
// the current starting attempt.
@@ -722,7 +747,7 @@ export const layer: Layer.Layer<
722747
coveredRunOrdinals: { from: 1, to: Math.max(1, run.ordinal - 1) },
723748
strategy: "full_thread_summary",
724749
runs: projection.runs,
725-
items: (yield* projectionStore.getTurnStartHistory(input.threadId)).filter(
750+
items: (yield* readHandoffHistory()).filter(
726751
(item) =>
727752
item.runId === null ||
728753
projection.runs.some(
@@ -1021,9 +1046,6 @@ export const layer: Layer.Layer<
10211046
...deliveredItemIds,
10221047
...settledHandoffs.flatMap((handoff) => handoff.delivery?.omittedItemIds ?? []),
10231048
]);
1024-
const deliveredAttemptIds = new Set(
1025-
projection.providerTurns.map((turn) => turn.runAttemptId),
1026-
);
10271049
const acceptedAttempts = projection.attempts.filter(
10281050
(source) =>
10291051
source.providerThreadId === providerThread.id && deliveredAttemptIds.has(source.id),
@@ -1105,33 +1127,14 @@ export const layer: Layer.Layer<
11051127
(source.status === "failed" || source.status === "interrupted") &&
11061128
!deliveredAttemptIds.has(source.activeAttemptId),
11071129
);
1108-
const undeliveredItems =
1130+
const missedItems =
11091131
undeliveredRuns.length === 0
11101132
? []
1111-
: yield* projectionStore.getTurnStartHistory(
1112-
input.threadId,
1113-
undeliveredRuns.map((source) => source.id),
1133+
: (yield* readHandoffHistory(undeliveredRuns.map((source) => source.id))).filter(
1134+
(item) => !coveredItemIds.has(item.id) && historicalMessage(item) !== null,
11141135
);
1115-
const maintenanceMessageIds = new Set(
1116-
undeliveredItems.flatMap((item) =>
1117-
item.type === "user_message" &&
1118-
item.attachments.length === 0 &&
1119-
["/compact", "/logout"].includes(item.text.trim().toLowerCase())
1120-
? [item.messageId]
1121-
: [],
1122-
),
1123-
);
1124-
const missedRuns = undeliveredRuns.filter(
1125-
(source) => !maintenanceMessageIds.has(source.userMessageId),
1126-
);
1127-
const missedRunIds = new Set(missedRuns.map((source) => source.id));
1128-
const missedItems = undeliveredItems.filter(
1129-
(item) =>
1130-
item.runId !== null &&
1131-
missedRunIds.has(item.runId) &&
1132-
!coveredItemIds.has(item.id) &&
1133-
historicalMessage(item) !== null,
1134-
);
1136+
const missedRunIds = new Set(missedItems.map((item) => item.runId));
1137+
const missedRuns = undeliveredRuns.filter((source) => missedRunIds.has(source.id));
11351138
const startWithHandoffs = (
11361139
turnInput: Parameters<typeof session.startTurn>[0],
11371140
compact = false,

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,9 @@ describe("orchestration v2 provider switching", () => {
471471
responseByRunOrdinal: {},
472472
capturedTurns,
473473
...(scenario.includes("retry") || scenario.includes("unsent") ? { failStartOnce } : {}),
474-
...(replaceNative ? { failResumeOnce, nativeThreadGeneration: generation } : {}),
474+
...(replaceNative || scenario === "compact-fallback"
475+
? { failResumeOnce, nativeThreadGeneration: generation }
476+
: {}),
475477
...(scenario.includes("reported-capacity")
476478
? { initialContextUsage: { usedTokens: 999_999, maxTokens: 1_000_000 } }
477479
: {}),
@@ -740,6 +742,19 @@ describe("orchestration v2 provider switching", () => {
740742
],
741743
});
742744
}
745+
if (scenario === "compact-fallback") {
746+
const target = rejected.providerThreads.find(
747+
(thread) => thread.id === compactRun.providerThreadId,
748+
)!;
749+
yield* orchestrator.dispatch({
750+
type: "provider-session.detach",
751+
commandId: CommandId.make("detach-after-rejected-compact"),
752+
threadId,
753+
providerSessionId: target.providerSessionId!,
754+
});
755+
yield* worker.drain();
756+
yield* Ref.set(failResumeOnce, true);
757+
}
743758
current = "Continue after compact";
744759
yield* dispatch(targetOrdinal + 1, current, targetSelection);
745760
yield* wait(targetOrdinal + 1);

0 commit comments

Comments
 (0)