Skip to content

Commit 8dd73ca

Browse files
committed
fix(server): queue background notifications during active tools
- Preserve Claude's native tool cancellation metadata and avoid interrupting active tools for automatic deliveries - Queue scheduled prompts for their bound thread
1 parent 484459f commit 8dd73ca

8 files changed

Lines changed: 581 additions & 4 deletions

File tree

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,20 @@ describe("ClaudeAdapterV2 context usage", () => {
867867
});
868868

869869
describe("ClaudeAdapterV2 session permissions", () => {
870+
it("keeps explicit user refusals classified as user_reject", () => {
871+
const result = ClaudeAdapterV2.permissionResultFromDecision({
872+
toolName: "Bash",
873+
decision: "decline",
874+
toolInput: { command: "make" },
875+
toolUseID: "denied-build",
876+
});
877+
assert.equal(result.behavior, "deny");
878+
if (result.behavior !== "deny") return;
879+
assert.equal(result.decisionClassification, "user_reject");
880+
assert.equal(result.message, "User declined tool execution.");
881+
assert.equal(result.interrupt, undefined);
882+
});
883+
870884
it("forces suggested permission updates to session scope", () => {
871885
const result = ClaudeAdapterV2.permissionResultFromDecision({
872886
toolName: "Bash",
@@ -2300,6 +2314,77 @@ describe("ClaudeAdapterV2 background wake turns", () => {
23002314
}).pipe(Effect.scoped, Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
23012315
);
23022316

2317+
it.effect.each(["cancelled", "denied", "permission_denied", undefined])(
2318+
"preserves native tool non-execution metadata %s without inferring a denial from text",
2319+
(kind) =>
2320+
Effect.scoped(
2321+
Effect.gen(function* () {
2322+
const harness = yield* makeWakeHarness;
2323+
yield* harness.runtime.startTurn(
2324+
makeClaudeTestTurnInput({
2325+
threadId: harness.threadId,
2326+
providerThread: harness.providerThread,
2327+
now: yield* DateTime.now,
2328+
attemptId: RunAttemptId.make("attempt-tool-non-execution"),
2329+
text: "Run the tool.",
2330+
attachments: [],
2331+
}),
2332+
);
2333+
// Same error text can describe a cancellation or a real refusal.
2334+
// Each result must use its own metadata, even in a multi-result frame.
2335+
yield* harness.offerAndWait(
2336+
claudeSdkFrame({
2337+
type: "user",
2338+
uuid: "tool-non-execution",
2339+
session_id: WAKE_NATIVE_SESSION,
2340+
parent_tool_use_id: null,
2341+
message: {
2342+
role: "user",
2343+
content: [
2344+
{
2345+
type: "tool_result",
2346+
tool_use_id: "tool-error",
2347+
is_error: true,
2348+
content: "STOP and wait for the user.",
2349+
},
2350+
{ type: "tool_result", tool_use_id: "tool-ok", is_error: false, content: "OK" },
2351+
],
2352+
},
2353+
...(kind === undefined
2354+
? {}
2355+
: {
2356+
tool_result_meta: [
2357+
{ id: "tool-error", non_execution_kind: kind },
2358+
{ id: "tool-ok", non_execution_kind: null },
2359+
],
2360+
}),
2361+
}),
2362+
);
2363+
yield* Queue.offer(
2364+
harness.sdkMessages,
2365+
makeResultFrame({ uuid: "result-non-execution", result: "Done" }),
2366+
);
2367+
yield* Queue.take(harness.terminalReceipts);
2368+
const items = harness.events.flatMap((event) =>
2369+
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
2370+
? [event.turnItem]
2371+
: [],
2372+
);
2373+
const failed = items.findLast((item) => item.nativeItemRef?.nativeId === "tool-error")!;
2374+
assert.equal(failed.status, kind === "cancelled" ? "cancelled" : "failed");
2375+
assert.equal(failed.toolNonExecutionKind, kind);
2376+
const ok = items.findLast((item) => item.nativeItemRef?.nativeId === "tool-ok")!;
2377+
assert.equal(ok.status, "completed");
2378+
assert.equal(ok.toolNonExecutionKind, undefined);
2379+
const node = harness.events.findLast(
2380+
(event) =>
2381+
event.type === "node.updated" && event.node.nativeItemRef?.nativeId === "tool-error",
2382+
);
2383+
assert.equal(node?.type === "node.updated" ? node.node.status : undefined, failed.status);
2384+
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
2385+
),
2386+
);
2387+
23032388
it.effect.each(
23042389
(["aborted_tools", "aborted_streaming"] as const).flatMap((terminalReason) =>
23052390
[true, false].map((steered) => ({ terminalReason, steered })),

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export const ClaudeProviderCapabilitiesV2 = {
198198
emitsTurnCompleted: true,
199199
supportsInterrupt: true,
200200
supportsActiveSteering: true,
201+
activeSteeringInterruptsTools: true,
201202
supportsSteeringByInterruptRestart: false,
202203
supportsQueuedMessages: true,
203204
terminalStatusQuality: "strong",
@@ -2192,6 +2193,24 @@ function claudeToolResultEntriesFromMessage(message: SDKMessage): ReadonlyArray<
21922193
];
21932194
}
21942195

2196+
// These wire fields are not yet declared by the SDK's SDKUserMessage type.
2197+
const ClaudeToolResultMetadata = Schema.Struct({
2198+
tool_result_meta: Schema.Array(
2199+
Schema.Struct({
2200+
id: Schema.String,
2201+
non_execution_kind: Schema.optional(Schema.NullOr(Schema.String)),
2202+
}),
2203+
),
2204+
});
2205+
const isClaudeToolResultMetadata = Schema.is(ClaudeToolResultMetadata);
2206+
2207+
function claudeToolNonExecutionKind(message: SDKMessage, toolUseId: string) {
2208+
return isClaudeToolResultMetadata(message)
2209+
? (message.tool_result_meta.find((meta) => meta.id === toolUseId)?.non_execution_kind ??
2210+
undefined)
2211+
: undefined;
2212+
}
2213+
21952214
function parentToolUseIdFromSdkMessage(message: SDKMessage): string | null {
21962215
return message.type === "assistant" || message.type === "user"
21972216
? message.parent_tool_use_id
@@ -3734,6 +3753,7 @@ export function makeClaudeAdapterV2(
37343753
readonly startedAt: DateTime.Utc;
37353754
readonly updatedAt: DateTime.Utc;
37363755
readonly presentation: ClaudeToolPresentation | undefined;
3756+
readonly toolNonExecutionKind?: string;
37373757
}) => {
37383758
const completedAt = input.status === "running" ? null : input.updatedAt;
37393759
const nodeId = idAllocator.derive.nodeFromProviderItem({
@@ -3781,6 +3801,9 @@ export function makeClaudeAdapterV2(
37813801
})
37823802
: undefined;
37833803
const itemBase = {
3804+
...(input.toolNonExecutionKind === undefined
3805+
? {}
3806+
: { toolNonExecutionKind: input.toolNonExecutionKind }),
37843807
id: turnItemId,
37853808
threadId: input.threadId,
37863809
runId: input.runId,
@@ -6102,6 +6125,10 @@ export function makeClaudeAdapterV2(
61026125
parentToolUseId,
61036126
}));
61046127
const completedAt = yield* DateTime.now;
6128+
const toolNonExecutionKind = claudeToolNonExecutionKind(
6129+
message,
6130+
toolResult.tool_use_id,
6131+
);
61056132
const artifacts = buildToolCallArtifacts({
61066133
context,
61076134
nativeItemId: toolCall.nativeItemId,
@@ -6114,7 +6141,13 @@ export function makeClaudeAdapterV2(
61146141
parentNodeId: toolCall.parentNodeId,
61156142
ordinal: toolCall.ordinal,
61166143
output,
6117-
status: isClaudeToolResultError(toolResult) ? "failed" : "completed",
6144+
status:
6145+
toolNonExecutionKind === "cancelled"
6146+
? "cancelled"
6147+
: isClaudeToolResultError(toolResult)
6148+
? "failed"
6149+
: "completed",
6150+
...(toolNonExecutionKind === undefined ? {} : { toolNonExecutionKind }),
61186151
startedAt: toolCall.startedAt,
61196152
updatedAt: completedAt,
61206153
presentation: toolCall.presentation,

0 commit comments

Comments
 (0)