Skip to content

Commit 4b4f15a

Browse files
committed
fix(server): a Claude hook that drops a prompt now shows its reason
1 parent 9c16146 commit 4b4f15a

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3686,6 +3686,78 @@ describe("ClaudeAdapterV2 background wake turns", () => {
36863686
),
36873687
);
36883688

3689+
it.effect("shows a hook-dropped Claude prompt once and skips info-level lines", () =>
3690+
Effect.gen(function* () {
3691+
const harness = yield* makeWakeHarness;
3692+
const now = yield* DateTime.now;
3693+
yield* harness.runtime.startTurn(
3694+
makeClaudeTestTurnInput({
3695+
threadId: harness.threadId,
3696+
providerThread: harness.providerThread,
3697+
now,
3698+
attemptId: RunAttemptId.make("claude-hook-dropped-prompt-attempt"),
3699+
text: "Deploy to production",
3700+
attachments: [],
3701+
}),
3702+
);
3703+
const notice = "Prompt dropped by a hook: Deploys are frozen until Monday.";
3704+
const uuid = "00000000-0000-4000-8000-000000000305";
3705+
yield* Queue.offer(
3706+
harness.sdkMessages,
3707+
claudeSdkFrame({
3708+
type: "system",
3709+
subtype: "informational",
3710+
content: "Started in the background: agent-1",
3711+
level: "info",
3712+
session_id: WAKE_NATIVE_SESSION,
3713+
uuid: "00000000-0000-4000-8000-000000000307",
3714+
}),
3715+
);
3716+
yield* Queue.offer(
3717+
harness.sdkMessages,
3718+
claudeSdkFrame({
3719+
type: "system",
3720+
subtype: "informational",
3721+
content: notice,
3722+
level: "warning",
3723+
prevent_continuation: true,
3724+
session_id: WAKE_NATIVE_SESSION,
3725+
uuid,
3726+
}),
3727+
);
3728+
yield* Queue.offer(
3729+
harness.sdkMessages,
3730+
makeResultFrame({
3731+
uuid: "00000000-0000-4000-8000-000000000306",
3732+
result: notice,
3733+
numTurns: 0,
3734+
terminalReason: null,
3735+
}),
3736+
);
3737+
yield* Queue.take(harness.terminalReceipts);
3738+
const notices = harness.events.flatMap((event) =>
3739+
event.type === "turn_item.updated" && event.turnItem.type === "system_notice"
3740+
? [event.turnItem]
3741+
: [],
3742+
);
3743+
assert.lengthOf(notices, 1);
3744+
assert.equal(notices[0]?.message, notice);
3745+
assert.equal(notices[0]?.status, "completed");
3746+
assert.equal(notices[0]?.nativeItemRef?.nativeId, uuid);
3747+
assert.equal(harness.terminalEvents()[0]?.status, "completed");
3748+
assert.isFalse(
3749+
harness.events.some(
3750+
(event) => event.type === "message.updated" && event.message.role === "assistant",
3751+
),
3752+
);
3753+
}).pipe(
3754+
Effect.scoped,
3755+
Effect.provide(
3756+
Layer.mergeAll(NodeServices.layer, IdAllocator.layer, McpProviderSessions.layer),
3757+
),
3758+
),
3759+
);
3760+
36893761
it.effect(
36903762
"runs native compaction and keeps its context watermark separate from billed usage",
36913763
() =>

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,6 +2971,8 @@ interface ActiveClaudeTurnContext {
29712971
fallbackText: string;
29722972
fallbackNativeItemId: string;
29732973
emittedNativeItemIds: Set<string>;
2974+
// The CLI repeats a notice that ends a prompt as the turn's result text.
2975+
noticeTexts: Set<string>;
29742976
};
29752977
readonly reasoning: {
29762978
messageId: string | null;
@@ -6770,9 +6772,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
67706772
return;
67716773
}
67726774

6773-
if (message.type === "system" && message.subtype === "model_refusal_fallback") {
6775+
if (
6776+
message.type === "system" &&
6777+
(message.subtype === "model_refusal_fallback" ||
6778+
(message.subtype === "informational" && message.level !== "info"))
6779+
) {
67746780
const now = yield* DateTime.now;
67756781
const nativeItemId = message.uuid;
6782+
context.assistant.noticeTexts.add(message.content);
67766783
yield* emitProviderEvent({
67776784
type: "turn_item.updated",
67786785
driver: CLAUDE_PROVIDER,
@@ -7451,7 +7458,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
74517458
context.assistant.emittedNativeItemIds.size === 0 &&
74527459
context.assistant.fallbackText.length === 0 &&
74537460
resultText !== null &&
7454-
resultText.text.length > 0
7461+
resultText.text.length > 0 &&
7462+
!context.assistant.noticeTexts.has(resultText.text)
74557463
) {
74567464
context.assistant.fallbackText = resultText.text;
74577465
context.assistant.fallbackNativeItemId = resultText.nativeItemId;
@@ -8309,6 +8317,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
83098317
fallbackText: "",
83108318
fallbackNativeItemId: `assistant:${turnInput.runId}`,
83118319
emittedNativeItemIds: new Set(),
8320+
noticeTexts: new Set(),
83128321
},
83138322
reasoning: {
83148323
messageId: null,

0 commit comments

Comments
 (0)