Skip to content

Commit a1f8389

Browse files
committed
fix(claude): retain accepted steer state after cancellation
1 parent bcd4255 commit a1f8389

2 files changed

Lines changed: 47 additions & 29 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,6 +2876,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
28762876
) => Effect.Effect<boolean, ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError>;
28772877
readonly offer?: (
28782878
message: SDKUserMessage,
2879+
onQueued?: () => void,
28792880
) => Effect.Effect<void, ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError>;
28802881
readonly environment?: NodeJS.ProcessEnv;
28812882
readonly subagentLaunchToolUseId?: string;
@@ -3007,12 +3008,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
30073008
processQueues.push(sdkMessages);
30083009
return {
30093010
messages,
3010-
offer: (message) =>
3011+
offer: (message, onQueued) =>
30113012
Effect.sync(() => {
30123013
offeredMessages.push(message);
30133014
}).pipe(
30143015
Effect.andThen(changes.notify),
3015-
Effect.andThen(options?.offer?.(message) ?? Effect.void),
3016+
Effect.andThen(options?.offer?.(message, onQueued) ?? Effect.void),
30163017
),
30173018
setModel: (model) =>
30183019
Effect.sync(() => {
@@ -5364,6 +5365,8 @@ describe("ClaudeAdapterV2 background wake turns", () => {
53645365
const answers = new Map<string, Deferred.Deferred<void>>();
53655366
const failOnce = new Set<string>();
53665367
let rejectOffer = false;
5368+
let cancelOffer = false;
5369+
const accepted = yield* Deferred.make<void>();
53675370
const harness = yield* makeWakeHarnessWithOptions({
53685371
backgroundTasks: (id) =>
53695372
Effect.gen(function* () {
@@ -5384,15 +5387,20 @@ describe("ClaudeAdapterV2 background wake turns", () => {
53845387
if (answer !== undefined) yield* Deferred.await(answer);
53855388
return isRegistered;
53865389
}),
5387-
offer: () =>
5388-
rejectOffer
5389-
? Effect.fail(
5390-
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
5391-
method: "offer",
5392-
cause: new Error("Input rejected."),
5393-
}),
5390+
offer: (_message, onQueued) =>
5391+
cancelOffer
5392+
? Effect.sync(() => onQueued?.()).pipe(
5393+
Effect.andThen(Deferred.succeed(accepted, undefined)),
5394+
Effect.andThen(Effect.never),
53945395
)
5395-
: Effect.void,
5396+
: rejectOffer
5397+
? Effect.fail(
5398+
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
5399+
method: "offer",
5400+
cause: new Error("Input rejected."),
5401+
}),
5402+
)
5403+
: Effect.void,
53965404
});
53975405
const idAllocator = yield* IdAllocator.IdAllocatorV2;
53985406
const attemptId = RunAttemptId.make("steer-command-cohorts");
@@ -5549,6 +5557,20 @@ describe("ClaudeAdapterV2 background wake turns", () => {
55495557
moves.filter((move) => move.id === "throws"),
55505558
2,
55515559
);
5560+
yield* command("cancelled");
5561+
registered.add("cancelled");
5562+
cancelOffer = true;
5563+
const cancelled = yield* steer("Skip the command despite cancelled logging.").pipe(
5564+
Effect.forkChild,
5565+
);
5566+
yield* Deferred.await(accepted);
5567+
yield* Fiber.interrupt(cancelled);
5568+
cancelOffer = false;
5569+
yield* through("cancelled");
5570+
yield* harness.offerAndWait(
5571+
makeResultFrame({ uuid: uuid(), result: "", terminalReason: "aborted_tools" }),
5572+
);
5573+
yield* steer("Continue after the accepted steer.");
55525574
}).pipe(
55535575
Effect.provide(
55545576
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10954,7 +10954,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1095410954
);
1095510955
yield* backgroundForegroundSubagents(existing, backgroundDeadline);
1095610956
// Keep the successful offer and command eligibility ahead of SDK frames.
10957-
const running = yield* sdkMessageLock.withPermits(1)(
10957+
yield* sdkMessageLock.withPermits(1)(
1095810958
Effect.gen(function* () {
1095910959
// The turn can end while the moves are answered or while this
1096010960
// waits for the lock. A steer offered then would start a new
@@ -10969,42 +10969,38 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1096910969
detail: `Claude provider turn ${turnInput.providerTurnId} ended before the steer was sent.`,
1097010970
});
1097110971
}
10972+
let queued = false;
1097210973
yield* Effect.uninterruptibleMask((restore) =>
1097310974
restore(
1097410975
existing.query.offer(userMessage, () => {
10976+
queued = true;
1097510977
pdfReservation.commit();
1097610978
}),
1097710979
).pipe(
10978-
Effect.tap(() =>
10979-
Effect.sync(() => {
10980+
// Queue acceptance survives cancellation during protocol logging.
10981+
Effect.onExit((exit) =>
10982+
Effect.gen(function* () {
10983+
if (!queued && !Exit.isSuccess(exit)) return;
1098010984
pdfReservation.commit();
10981-
}),
10982-
),
10983-
Effect.tap(() =>
10984-
Ref.update(steeredTurns, (current) => {
10985-
const next = new Set(current);
10986-
next.add(turnInput.providerTurnId);
10987-
return next;
10988-
}),
10989-
),
10990-
// Eligibility survives older abort results and overlapping steers.
10991-
Effect.tap(() =>
10992-
Effect.sync(() => {
10985+
yield* Ref.update(steeredTurns, (current) => {
10986+
const next = new Set(current);
10987+
next.add(turnInput.providerTurnId);
10988+
return next;
10989+
});
1099310990
for (const [toolUseId, state] of currentTurn.rootCommands) {
1099410991
if (state === "foreground") {
1099510992
currentTurn.rootCommands.set(toolUseId, "steered");
1099610993
}
1099710994
}
10995+
for (const toolUseId of currentTurn.rootCommands.keys()) {
10996+
yield* backgroundForSteer(currentTurn, existing.query, toolUseId);
10997+
}
1099810998
}),
1099910999
),
1100011000
),
1100111001
);
11002-
return [...currentTurn.rootCommands.keys()];
1100311002
}),
1100411003
);
11005-
for (const toolUseId of running) {
11006-
yield* backgroundForSteer(currentTurn, existing.query, toolUseId);
11007-
}
1100811004
},
1100911005
(effect, turnInput) =>
1101011006
effect.pipe(

0 commit comments

Comments
 (0)