Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,20 @@ describe("ClaudeAdapterV2 context usage", () => {
});

describe("ClaudeAdapterV2 session permissions", () => {
it("keeps explicit user refusals classified as user_reject", () => {
const result = ClaudeAdapterV2.permissionResultFromDecision({
toolName: "Bash",
decision: "decline",
toolInput: { command: "make" },
toolUseID: "denied-build",
});
assert.equal(result.behavior, "deny");
if (result.behavior !== "deny") return;
assert.equal(result.decisionClassification, "user_reject");
assert.equal(result.message, "User declined tool execution.");
assert.equal(result.interrupt, undefined);
});

it("forces suggested permission updates to session scope", () => {
const result = ClaudeAdapterV2.permissionResultFromDecision({
toolName: "Bash",
Expand Down Expand Up @@ -2480,6 +2494,77 @@ describe("ClaudeAdapterV2 background wake turns", () => {
}).pipe(Effect.scoped, Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
);

it.effect.each(["cancelled", "denied", "permission_denied", undefined])(
"preserves native tool non-execution metadata %s without inferring a denial from text",
(kind) =>
Effect.scoped(
Effect.gen(function* () {
const harness = yield* makeWakeHarness;
yield* harness.runtime.startTurn(
makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId: RunAttemptId.make("attempt-tool-non-execution"),
text: "Run the tool.",
attachments: [],
}),
);
// Same error text can describe a cancellation or a real refusal.
// Each result must use its own metadata, even in a multi-result frame.
yield* harness.offerAndWait(
claudeSdkFrame({
type: "user",
uuid: "tool-non-execution",
session_id: WAKE_NATIVE_SESSION,
parent_tool_use_id: null,
message: {
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "tool-error",
is_error: true,
content: "STOP and wait for the user.",
},
{ type: "tool_result", tool_use_id: "tool-ok", is_error: false, content: "OK" },
],
},
...(kind === undefined
? {}
: {
tool_result_meta: [
{ id: "tool-error", non_execution_kind: kind },
{ id: "tool-ok", non_execution_kind: null },
],
}),
}),
);
yield* Queue.offer(
harness.sdkMessages,
makeResultFrame({ uuid: "result-non-execution", result: "Done" }),
);
yield* Queue.take(harness.terminalReceipts);
const items = harness.events.flatMap((event) =>
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
? [event.turnItem]
: [],
);
const failed = items.findLast((item) => item.nativeItemRef?.nativeId === "tool-error")!;
assert.equal(failed.status, kind === "cancelled" ? "cancelled" : "failed");
assert.equal(failed.toolNonExecutionKind, kind);
const ok = items.findLast((item) => item.nativeItemRef?.nativeId === "tool-ok")!;
assert.equal(ok.status, "completed");
assert.equal(ok.toolNonExecutionKind, undefined);
const node = harness.events.findLast(
(event) =>
event.type === "node.updated" && event.node.nativeItemRef?.nativeId === "tool-error",
);
assert.equal(node?.type === "node.updated" ? node.node.status : undefined, failed.status);
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
),
);

it.effect.each(
(["aborted_tools", "aborted_streaming"] as const).flatMap((terminalReason) =>
[true, false].map((steered) => ({ terminalReason, steered })),
Expand Down
35 changes: 34 additions & 1 deletion apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export const ClaudeProviderCapabilitiesV2 = {
emitsTurnCompleted: true,
supportsInterrupt: true,
supportsActiveSteering: true,
activeSteeringInterruptsTools: true,
supportsSteeringByInterruptRestart: false,
supportsQueuedMessages: true,
terminalStatusQuality: "strong",
Expand Down Expand Up @@ -2163,6 +2164,24 @@ function claudeToolResultEntriesFromMessage(message: SDKMessage): ReadonlyArray<
];
}

// These wire fields are not yet declared by the SDK's SDKUserMessage type.
const ClaudeToolResultMetadata = Schema.Struct({
tool_result_meta: Schema.Array(
Schema.Struct({
id: Schema.String,
non_execution_kind: Schema.optional(Schema.NullOr(Schema.String)),
}),
),
});
const isClaudeToolResultMetadata = Schema.is(ClaudeToolResultMetadata);

function claudeToolNonExecutionKind(message: SDKMessage, toolUseId: string) {
return isClaudeToolResultMetadata(message)
? (message.tool_result_meta.find((meta) => meta.id === toolUseId)?.non_execution_kind ??
undefined)
: undefined;
}

function parentToolUseIdFromSdkMessage(message: SDKMessage): string | null {
return message.type === "assistant" || message.type === "user"
? message.parent_tool_use_id
Expand Down Expand Up @@ -3705,6 +3724,7 @@ export function makeClaudeAdapterV2(
readonly startedAt: DateTime.Utc;
readonly updatedAt: DateTime.Utc;
readonly presentation: ClaudeToolPresentation | undefined;
readonly toolNonExecutionKind?: string;
}) => {
const completedAt = input.status === "running" ? null : input.updatedAt;
const nodeId = idAllocator.derive.nodeFromProviderItem({
Expand Down Expand Up @@ -3752,6 +3772,9 @@ export function makeClaudeAdapterV2(
})
: undefined;
const itemBase = {
...(input.toolNonExecutionKind === undefined
? {}
: { toolNonExecutionKind: input.toolNonExecutionKind }),
id: turnItemId,
threadId: input.threadId,
runId: input.runId,
Expand Down Expand Up @@ -6080,6 +6103,10 @@ export function makeClaudeAdapterV2(
parentToolUseId,
}));
const completedAt = yield* DateTime.now;
const toolNonExecutionKind = claudeToolNonExecutionKind(
message,
toolResult.tool_use_id,
);
const artifacts = buildToolCallArtifacts({
context,
nativeItemId: toolCall.nativeItemId,
Expand All @@ -6092,7 +6119,13 @@ export function makeClaudeAdapterV2(
parentNodeId: toolCall.parentNodeId,
ordinal: toolCall.ordinal,
output,
status: isClaudeToolResultError(toolResult) ? "failed" : "completed",
status:
toolNonExecutionKind === "cancelled"
? "cancelled"
: isClaudeToolResultError(toolResult)
? "failed"
: "completed",
...(toolNonExecutionKind === undefined ? {} : { toolNonExecutionKind }),
startedAt: toolCall.startedAt,
updatedAt: completedAt,
presentation: toolCall.presentation,
Expand Down
Loading
Loading