Skip to content

Commit 1beb035

Browse files
authored
fix(server): queue background notifications during active tools (pingdotgg#15892)
1 parent a1d9d72 commit 1beb035

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
@@ -871,6 +871,20 @@ describe("ClaudeAdapterV2 context usage", () => {
871871
});
872872

873873
describe("ClaudeAdapterV2 session permissions", () => {
874+
it("keeps explicit user refusals classified as user_reject", () => {
875+
const result = ClaudeAdapterV2.permissionResultFromDecision({
876+
toolName: "Bash",
877+
decision: "decline",
878+
toolInput: { command: "make" },
879+
toolUseID: "denied-build",
880+
});
881+
assert.equal(result.behavior, "deny");
882+
if (result.behavior !== "deny") return;
883+
assert.equal(result.decisionClassification, "user_reject");
884+
assert.equal(result.message, "User declined tool execution.");
885+
assert.equal(result.interrupt, undefined);
886+
});
887+
874888
it("forces suggested permission updates to session scope", () => {
875889
const result = ClaudeAdapterV2.permissionResultFromDecision({
876890
toolName: "Bash",
@@ -2480,6 +2494,77 @@ describe("ClaudeAdapterV2 background wake turns", () => {
24802494
}).pipe(Effect.scoped, Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
24812495
);
24822496

2497+
it.effect.each(["cancelled", "denied", "permission_denied", undefined])(
2498+
"preserves native tool non-execution metadata %s without inferring a denial from text",
2499+
(kind) =>
2500+
Effect.scoped(
2501+
Effect.gen(function* () {
2502+
const harness = yield* makeWakeHarness;
2503+
yield* harness.runtime.startTurn(
2504+
makeClaudeTestTurnInput({
2505+
threadId: harness.threadId,
2506+
providerThread: harness.providerThread,
2507+
now: yield* DateTime.now,
2508+
attemptId: RunAttemptId.make("attempt-tool-non-execution"),
2509+
text: "Run the tool.",
2510+
attachments: [],
2511+
}),
2512+
);
2513+
// Same error text can describe a cancellation or a real refusal.
2514+
// Each result must use its own metadata, even in a multi-result frame.
2515+
yield* harness.offerAndWait(
2516+
claudeSdkFrame({
2517+
type: "user",
2518+
uuid: "tool-non-execution",
2519+
session_id: WAKE_NATIVE_SESSION,
2520+
parent_tool_use_id: null,
2521+
message: {
2522+
role: "user",
2523+
content: [
2524+
{
2525+
type: "tool_result",
2526+
tool_use_id: "tool-error",
2527+
is_error: true,
2528+
content: "STOP and wait for the user.",
2529+
},
2530+
{ type: "tool_result", tool_use_id: "tool-ok", is_error: false, content: "OK" },
2531+
],
2532+
},
2533+
...(kind === undefined
2534+
? {}
2535+
: {
2536+
tool_result_meta: [
2537+
{ id: "tool-error", non_execution_kind: kind },
2538+
{ id: "tool-ok", non_execution_kind: null },
2539+
],
2540+
}),
2541+
}),
2542+
);
2543+
yield* Queue.offer(
2544+
harness.sdkMessages,
2545+
makeResultFrame({ uuid: "result-non-execution", result: "Done" }),
2546+
);
2547+
yield* Queue.take(harness.terminalReceipts);
2548+
const items = harness.events.flatMap((event) =>
2549+
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
2550+
? [event.turnItem]
2551+
: [],
2552+
);
2553+
const failed = items.findLast((item) => item.nativeItemRef?.nativeId === "tool-error")!;
2554+
assert.equal(failed.status, kind === "cancelled" ? "cancelled" : "failed");
2555+
assert.equal(failed.toolNonExecutionKind, kind);
2556+
const ok = items.findLast((item) => item.nativeItemRef?.nativeId === "tool-ok")!;
2557+
assert.equal(ok.status, "completed");
2558+
assert.equal(ok.toolNonExecutionKind, undefined);
2559+
const node = harness.events.findLast(
2560+
(event) =>
2561+
event.type === "node.updated" && event.node.nativeItemRef?.nativeId === "tool-error",
2562+
);
2563+
assert.equal(node?.type === "node.updated" ? node.node.status : undefined, failed.status);
2564+
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
2565+
),
2566+
);
2567+
24832568
it.effect.each(
24842569
(["aborted_tools", "aborted_streaming"] as const).flatMap((terminalReason) =>
24852570
[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",
@@ -2163,6 +2164,24 @@ function claudeToolResultEntriesFromMessage(message: SDKMessage): ReadonlyArray<
21632164
];
21642165
}
21652166

2167+
// These wire fields are not yet declared by the SDK's SDKUserMessage type.
2168+
const ClaudeToolResultMetadata = Schema.Struct({
2169+
tool_result_meta: Schema.Array(
2170+
Schema.Struct({
2171+
id: Schema.String,
2172+
non_execution_kind: Schema.optional(Schema.NullOr(Schema.String)),
2173+
}),
2174+
),
2175+
});
2176+
const isClaudeToolResultMetadata = Schema.is(ClaudeToolResultMetadata);
2177+
2178+
function claudeToolNonExecutionKind(message: SDKMessage, toolUseId: string) {
2179+
return isClaudeToolResultMetadata(message)
2180+
? (message.tool_result_meta.find((meta) => meta.id === toolUseId)?.non_execution_kind ??
2181+
undefined)
2182+
: undefined;
2183+
}
2184+
21662185
function parentToolUseIdFromSdkMessage(message: SDKMessage): string | null {
21672186
return message.type === "assistant" || message.type === "user"
21682187
? message.parent_tool_use_id
@@ -3705,6 +3724,7 @@ export function makeClaudeAdapterV2(
37053724
readonly startedAt: DateTime.Utc;
37063725
readonly updatedAt: DateTime.Utc;
37073726
readonly presentation: ClaudeToolPresentation | undefined;
3727+
readonly toolNonExecutionKind?: string;
37083728
}) => {
37093729
const completedAt = input.status === "running" ? null : input.updatedAt;
37103730
const nodeId = idAllocator.derive.nodeFromProviderItem({
@@ -3752,6 +3772,9 @@ export function makeClaudeAdapterV2(
37523772
})
37533773
: undefined;
37543774
const itemBase = {
3775+
...(input.toolNonExecutionKind === undefined
3776+
? {}
3777+
: { toolNonExecutionKind: input.toolNonExecutionKind }),
37553778
id: turnItemId,
37563779
threadId: input.threadId,
37573780
runId: input.runId,
@@ -6080,6 +6103,10 @@ export function makeClaudeAdapterV2(
60806103
parentToolUseId,
60816104
}));
60826105
const completedAt = yield* DateTime.now;
6106+
const toolNonExecutionKind = claudeToolNonExecutionKind(
6107+
message,
6108+
toolResult.tool_use_id,
6109+
);
60836110
const artifacts = buildToolCallArtifacts({
60846111
context,
60856112
nativeItemId: toolCall.nativeItemId,
@@ -6092,7 +6119,13 @@ export function makeClaudeAdapterV2(
60926119
parentNodeId: toolCall.parentNodeId,
60936120
ordinal: toolCall.ordinal,
60946121
output,
6095-
status: isClaudeToolResultError(toolResult) ? "failed" : "completed",
6122+
status:
6123+
toolNonExecutionKind === "cancelled"
6124+
? "cancelled"
6125+
: isClaudeToolResultError(toolResult)
6126+
? "failed"
6127+
: "completed",
6128+
...(toolNonExecutionKind === undefined ? {} : { toolNonExecutionKind }),
60966129
startedAt: toolCall.startedAt,
60976130
updatedAt: completedAt,
60986131
presentation: toolCall.presentation,

0 commit comments

Comments
 (0)