Skip to content

Commit 289592d

Browse files
author
Capxul Agent
committed
refactor(server): model pending user-input cancellation as an Effect
Addresses review feedback: the teardown now sequences the settle in the Effect it is already running in, instead of fire-and-forgetting through the runFork bridge that exists for the AbortSignal listener boundary.
1 parent 82e2435 commit 289592d

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ interface PendingApproval {
158158
interface PendingUserInput {
159159
readonly questions: ReadonlyArray<UserInputQuestion>;
160160
readonly answers: Deferred.Deferred<ProviderUserInputAnswers>;
161-
/** Unparks the waiting handler as cancelled. Session teardown must call it. */
162-
readonly cancel: () => void;
161+
/** Unparks the waiting handler as cancelled. Session teardown must run it. */
162+
readonly cancel: Effect.Effect<void>;
163163
}
164164

165165
interface ToolInFlight {
@@ -3059,7 +3059,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
30593059
// Same reason as the approvals above: a request nobody can answer any more
30603060
// must not stay open, or the thread can never be settled.
30613061
for (const pending of [...context.pendingUserInputs.values()]) {
3062-
pending.cancel();
3062+
yield* pending.cancel;
30633063
}
30643064

30653065
if (context.turnState) {
@@ -3240,10 +3240,20 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
32403240

32413241
const answersDeferred = yield* Deferred.make<ProviderUserInputAnswers>();
32423242
let aborted = false;
3243+
const settleAsAborted = Effect.suspend(() => {
3244+
if (!pendingUserInputs.has(requestId)) {
3245+
return Effect.void;
3246+
}
3247+
aborted = true;
3248+
pendingUserInputs.delete(requestId);
3249+
return Deferred.succeed(answersDeferred, {} as ProviderUserInputAnswers).pipe(
3250+
Effect.ignore,
3251+
);
3252+
});
32433253
const pendingInput: PendingUserInput = {
32443254
questions,
32453255
answers: answersDeferred,
3246-
cancel: () => onAbort(),
3256+
cancel: settleAsAborted,
32473257
};
32483258

32493259
// Emit user-input.requested so the UI can present the questions.
@@ -3278,12 +3288,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
32783288

32793289
// Handle abort (e.g. turn interrupted while waiting for user input).
32803290
const onAbort = () => {
3281-
if (!pendingUserInputs.has(requestId)) {
3282-
return;
3283-
}
3284-
aborted = true;
3285-
pendingUserInputs.delete(requestId);
3286-
runFork(Deferred.succeed(answersDeferred, {} as ProviderUserInputAnswers));
3291+
runFork(settleAsAborted);
32873292
};
32883293
callbackOptions.signal.addEventListener("abort", onAbort, {
32893294
once: true,

0 commit comments

Comments
 (0)