Skip to content

Commit af2c2ca

Browse files
committed
Merge remote-tracking branch 'origin/main' into t3/inline-open-button
2 parents 83e086f + 694076d commit af2c2ca

2 files changed

Lines changed: 223 additions & 12 deletions

File tree

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

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,6 +4958,154 @@ describe("ClaudeAdapterV2 background wake turns", () => {
49584958
),
49594959
);
49604960

4961+
// regression: #15708 Stop must not silently drop a steer Claude queued behind a foreground task
4962+
it.effect.each([
4963+
{
4964+
readBy: "none",
4965+
echoes: true,
4966+
title: "warns that a steer Claude never read before Stop was not delivered",
4967+
},
4968+
{
4969+
readBy: "none",
4970+
echoes: false,
4971+
title: "does not warn about a steer on a CLI that never echoes prompt uuids",
4972+
},
4973+
{
4974+
readBy: "assistant",
4975+
echoes: true,
4976+
title: "does not warn about a steer Claude read before Stop",
4977+
},
4978+
] as const)("$title", ({ readBy, echoes }) =>
4979+
Effect.gen(function* () {
4980+
const harness = yield* makeWakeHarnessWithOptions({
4981+
close: (sdkMessages) => Queue.shutdown(sdkMessages),
4982+
freshQueueOnReopen: true,
4983+
});
4984+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
4985+
const firstInput = makeClaudeTestTurnInput({
4986+
threadId: harness.threadId,
4987+
providerThread: harness.providerThread,
4988+
now: yield* DateTime.now,
4989+
attemptId: RunAttemptId.make("attempt-claude-unread-steer-1"),
4990+
text: "Run the slow curl in the foreground.",
4991+
attachments: [],
4992+
});
4993+
yield* harness.runtime.startTurn(firstInput);
4994+
const providerTurnId = idAllocator.derive.providerTurn({
4995+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
4996+
nativeTurnId: `turn:${firstInput.attemptId}`,
4997+
});
4998+
yield* harness.offerAndWait(
4999+
claudeSdkFrame({
5000+
type: "assistant",
5001+
uuid: "00000000-0000-4000-8000-000000015701",
5002+
session_id: WAKE_NATIVE_SESSION,
5003+
parent_tool_use_id: null,
5004+
...(echoes ? { user_message_uuids: [harness.promptUuid(0)] } : {}),
5005+
message: {
5006+
id: "msg_unread_steer_bash",
5007+
type: "message",
5008+
role: "assistant",
5009+
model: "claude-sonnet-4-6",
5010+
content: [
5011+
{
5012+
type: "tool_use",
5013+
id: "toolu-unread-steer-bash",
5014+
name: "Bash",
5015+
input: { command: "curl http://127.0.0.1:9/hang" },
5016+
},
5017+
],
5018+
},
5019+
}),
5020+
);
5021+
// Past task_started the CLI queues a "now" steer until the call returns.
5022+
yield* harness.offerAndWait(
5023+
claudeSdkFrame({
5024+
type: "system",
5025+
subtype: "task_started",
5026+
task_id: "task-unread-steer-bash",
5027+
tool_use_id: "toolu-unread-steer-bash",
5028+
description: "Slow curl",
5029+
is_backgrounded: false,
5030+
task_type: "local_bash",
5031+
uuid: "00000000-0000-4000-8000-000000015702",
5032+
session_id: WAKE_NATIVE_SESSION,
5033+
}),
5034+
);
5035+
const steerText = "Codeword: PELICAN. Remember it, I will ask for it later.";
5036+
yield* harness.runtime.steerTurn({
5037+
threadId: harness.threadId,
5038+
runId: firstInput.runId,
5039+
providerThread: harness.providerThread,
5040+
providerTurnId,
5041+
message: {
5042+
createdBy: "user",
5043+
creationSource: "web",
5044+
messageId: MessageId.make("unread-steer"),
5045+
text: steerText,
5046+
attachments: [],
5047+
},
5048+
});
5049+
const steerUuid = harness.offeredMessages.at(-1)?.uuid;
5050+
assert.isDefined(steerUuid);
5051+
if (readBy === "assistant") {
5052+
yield* harness.offerAndWait(
5053+
claudeSdkFrame({
5054+
type: "assistant",
5055+
uuid: "00000000-0000-4000-8000-000000015703",
5056+
session_id: WAKE_NATIVE_SESSION,
5057+
parent_tool_use_id: null,
5058+
user_message_uuids: [steerUuid],
5059+
message: {
5060+
id: "msg_unread_steer_reply",
5061+
type: "message",
5062+
role: "assistant",
5063+
model: "claude-sonnet-4-6",
5064+
content: [{ type: "text", text: "Noted." }],
5065+
},
5066+
}),
5067+
);
5068+
}
5069+
const noticesBefore = harness.events.length;
5070+
yield* harness.runtime.interruptTurn({
5071+
providerThread: harness.providerThread,
5072+
providerTurnId,
5073+
});
5074+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "interrupted");
5075+
5076+
const notices = harness.events
5077+
.slice(noticesBefore)
5078+
.flatMap((event) =>
5079+
event.type === "turn_item.updated" && event.turnItem.type === "system_notice"
5080+
? [event.turnItem.message]
5081+
: [],
5082+
);
5083+
const undelivered = notices.find((message) =>
5084+
/not (have )?(been )?delivered|never (reached|delivered)/i.test(message ?? ""),
5085+
);
5086+
if (readBy !== "none" || !echoes) {
5087+
assert.isUndefined(undelivered);
5088+
return;
5089+
}
5090+
assert.isDefined(
5091+
undelivered,
5092+
`expected an undelivered-steer notice, got ${JSON.stringify(notices)}`,
5093+
);
5094+
assert.match(undelivered ?? "", /resend|send it again|re-send/i);
5095+
assert.include(undelivered, "PELICAN");
5096+
}).pipe(
5097+
Effect.scoped,
5098+
Effect.provide(
5099+
Layer.mergeAll(
5100+
IdAllocator.layer,
5101+
McpProviderSessions.layer,
5102+
NodeServices.layer,
5103+
NodeCrypto.layer,
5104+
),
5105+
),
5106+
),
5107+
);
5108+
49615109
// regression: #23948 Claude holds a steer behind a running foreground tool
49625110
// call and goes silent, so each steer stays pending until Claude starts it.
49635111
it.effect("keeps a steer pending until Claude starts it", () =>

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

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,6 +4453,9 @@ interface ActiveClaudeTurnContext {
44534453
compactionFailure: { readonly message: string | undefined } | null;
44544454
// Steers end segments whose usage is excluded from the final result; bill them too, oldest first.
44554455
readonly steeredSegmentUsage: Array<SDKResultMessage["usage"]>;
4456+
// Set as finalization starts: a steer admitted later would stay pending
4457+
// forever, so it fails and the orchestrator follows up instead.
4458+
steersClosed: boolean;
44564459
// Ledger growth of the steered segments; undefined once any segment's growth is unknown.
44574460
steeredLedgerUsage: ClaudeLedgerUsage | undefined;
44584461
readonly rootModels: Set<string>;
@@ -4465,8 +4468,16 @@ interface ActiveClaudeTurnContext {
44654468
gatedFramesBeforeEcho: number;
44664469
readonly heldRootFrames: Array<SDKMessage>;
44674470
// Steers Claude has not started or echoed yet, by prompt uuid. Claude holds
4468-
// a steer behind a running foreground tool call without saying so.
4469-
readonly pendingSteers: Map<string, MessageId>;
4471+
// a steer behind a running foreground tool call without saying so, and Stop
4472+
// drops it unread.
4473+
readonly pendingSteers: Map<
4474+
string,
4475+
{
4476+
readonly messageId: MessageId;
4477+
readonly text: string;
4478+
readonly liveQuery: ClaudeLiveQueryContext;
4479+
}
4480+
>;
44704481
// Claude may settle a turn with a result that echoes only a steer's uuid.
44714482
readonly steerUuids: Set<string>;
44724483
// Claude can run another model than requested, such as when an organization disallows it.
@@ -8124,10 +8135,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
81248135
? [lifecycle.uuid]
81258136
: [];
81268137
for (const uuid of accepted) {
8127-
const messageId = context.pendingSteers.get(uuid);
8128-
if (messageId === undefined) continue;
8138+
const steer = context.pendingSteers.get(uuid);
8139+
if (steer === undefined) continue;
81298140
context.pendingSteers.delete(uuid);
8130-
yield* emitSteerStatus(context, messageId, "completed");
8141+
yield* emitSteerStatus(context, steer.messageId, "completed");
81318142
}
81328143
});
81338144

@@ -8142,16 +8153,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
81428153
readonly threadDisposition?: "reusable" | "broken";
81438154
readonly result?: SDKResultMessage;
81448155
}) {
8156+
input.context.steersClosed = true;
81458157
yield* reasoningDeltas.flushTurn(input.context.nativeTurnId);
81468158
// A turn that completes has taken in every steer; one that stops drops them unread.
8147-
for (const messageId of input.context.pendingSteers.values()) {
8159+
const droppedSteers = [...input.context.pendingSteers];
8160+
input.context.pendingSteers.clear();
8161+
for (const [, { messageId }] of droppedSteers) {
81488162
yield* emitSteerStatus(
81498163
input.context,
81508164
messageId,
81518165
input.status === "completed" ? "completed" : "cancelled",
81528166
);
81538167
}
8154-
input.context.pendingSteers.clear();
81558168
if (input.context.compaction?.status === "running") {
81568169
const reason = input.result?.subtype === "success" ? input.result.result.trim() : "";
81578170
yield* emitProviderEvent({
@@ -8289,6 +8302,49 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
82898302
});
82908303
}
82918304

8305+
if (input.status === "interrupted") {
8306+
for (const [uuid, { text, liveQuery }] of droppedSteers) {
8307+
// Only a CLI proven to echo prompt uuids can leave a steer unconfirmed.
8308+
if (
8309+
liveQuery.promptEchoMode !== "early" &&
8310+
liveQuery.promptEchoMode !== "result_only"
8311+
) {
8312+
continue;
8313+
}
8314+
const nativeItemId = `unread-steer:${uuid}`;
8315+
const notice = `Claude did not confirm reading your steer before Stop. It may not have been delivered. Resend it if needed: "${text}"`;
8316+
yield* emitProviderEvent({
8317+
type: "turn_item.updated",
8318+
driver: CLAUDE_PROVIDER,
8319+
turnItem: {
8320+
id: idAllocator.derive.turnItemFromProviderItem({
8321+
driver: CLAUDE_PROVIDER,
8322+
nativeItemId,
8323+
}),
8324+
threadId: input.context.input.threadId,
8325+
runId: input.context.input.runId,
8326+
nodeId: input.context.input.rootNodeId,
8327+
providerThreadId: input.context.input.providerThread.id,
8328+
providerTurnId: input.context.providerTurnId,
8329+
nativeItemRef: {
8330+
driver: CLAUDE_PROVIDER,
8331+
nativeId: nativeItemId,
8332+
strength: "weak",
8333+
},
8334+
parentItemId: null,
8335+
ordinal: yield* resolveItemOrdinal(input.context, nativeItemId),
8336+
type: "system_notice",
8337+
status: "completed",
8338+
title: "Steer delivery not confirmed",
8339+
message: notice,
8340+
startedAt: input.completedAt,
8341+
completedAt: input.completedAt,
8342+
updatedAt: input.completedAt,
8343+
},
8344+
});
8345+
}
8346+
}
8347+
82928348
const threadDisposition = input.threadDisposition ?? "reusable";
82938349
const terminalEvent: ProviderAdapter.ProviderAdapterV2Event =
82948350
input.status === "failed"
@@ -11538,6 +11594,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1153811594
latestAssistantRateLimit: null,
1153911595
compactionFailure: null,
1154011596
steeredSegmentUsage: [],
11597+
steersClosed: false,
1154111598
steeredLedgerUsage: { inputTokens: 0, outputTokens: 0 },
1154211599
rootModels: new Set(),
1154311600
subagentsByTaskId: new Map(),
@@ -11978,22 +12035,28 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1197812035
// the steer fails and the orchestrator starts the follow-up turn.
1197912036
if (
1198012037
(yield* Ref.get(activeTurn))?.providerTurnId !== turnInput.providerTurnId ||
11981-
(yield* Ref.get(queryContext)) !== existing
12038+
(yield* Ref.get(queryContext)) !== existing ||
12039+
// No yield between this check and the set below.
12040+
currentTurn.steersClosed
1198212041
) {
1198312042
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
1198412043
driver: CLAUDE_PROVIDER,
1198512044
detail: `Claude provider turn ${turnInput.providerTurnId} ended before the steer was sent.`,
1198612045
});
1198712046
}
12047+
// Register before offering: Claude can start the steer and end the turn before offer returns.
12048+
const messageId = turnInput.message.messageId;
12049+
currentTurn.steerUuids.add(steerUuid);
12050+
currentTurn.pendingSteers.set(steerUuid, {
12051+
messageId,
12052+
text: turnInput.message.text,
12053+
liveQuery: existing,
12054+
});
1198812055
yield* Ref.update(steeredTurns, (current) => {
1198912056
const next = new Set(current);
1199012057
next.add(turnInput.providerTurnId);
1199112058
return next;
1199212059
});
11993-
// Register before offering: Claude can start the steer and end the turn before offer returns.
11994-
const messageId = turnInput.message.messageId;
11995-
currentTurn.steerUuids.add(steerUuid);
11996-
currentTurn.pendingSteers.set(steerUuid, messageId);
1199712060
yield* emitSteerStatus(currentTurn, messageId, "pending");
1199812061
yield* Effect.uninterruptibleMask((restore) =>
1199912062
restore(

0 commit comments

Comments
 (0)