Skip to content

Commit c7618f6

Browse files
fix(server): a steer that cuts off a streaming Claude reply marks it interrupted (pingdotgg#34597)
1 parent c2ff84a commit c7618f6

3 files changed

Lines changed: 89 additions & 14 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6861,6 +6861,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {
68616861
});
68626862
assert.equal(harness.offeredMessages[1]?.priority, "now");
68636863
}
6864+
const partialText = "The audit covers the `integ";
6865+
if (!queryExits)
6866+
yield* Queue.offer(
6867+
harness.sdkMessages,
6868+
makeAssistantTextFrame({
6869+
uuid: "00000000-0000-4000-8000-000000000900",
6870+
text: partialText,
6871+
}),
6872+
);
68646873
yield* harness.offerAndWait(
68656874
makeResultFrame({
68666875
uuid: "00000000-0000-4000-8000-000000000901",
@@ -6897,6 +6906,19 @@ describe("ClaudeAdapterV2 background wake turns", () => {
68976906
terminal.status,
68986907
queryExits ? "failed" : steered ? "completed" : "interrupted",
68996908
);
6909+
if (steered && !queryExits && terminalReason === "aborted_streaming") {
6910+
// The steer cut this reply off mid-stream; it must not read as a finished answer.
6911+
const partial = harness.events.findLast(
6912+
(event) =>
6913+
event.type === "turn_item.updated" &&
6914+
event.turnItem.type === "assistant_message" &&
6915+
event.turnItem.text === partialText,
6916+
);
6917+
assert.equal(
6918+
partial?.type === "turn_item.updated" ? partial.turnItem.status : undefined,
6919+
"interrupted",
6920+
);
6921+
}
69006922
if (steered) {
69016923
if (!queryExits)
69026924
assert.isTrue(

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

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,6 +4271,7 @@ function buildAssistantArtifacts(input: {
42714271
readonly nativeItemId: string;
42724272
readonly text: string;
42734273
readonly ordinal: number;
4274+
readonly status?: "completed" | "interrupted";
42744275
readonly startedAt: DateTime.Utc;
42754276
readonly completedAt: DateTime.Utc;
42764277
}): {
@@ -4304,7 +4305,7 @@ function buildAssistantArtifacts(input: {
43044305
parentNodeId: input.turnInput.rootNodeId,
43054306
rootNodeId: input.turnInput.rootNodeId,
43064307
kind: "assistant_message",
4307-
status: "completed",
4308+
status: input.status ?? "completed",
43084309
countsForRun: false,
43094310
providerThreadId: input.turnInput.providerThread.id,
43104311
providerTurnId: input.providerTurnId,
@@ -4338,7 +4339,7 @@ function buildAssistantArtifacts(input: {
43384339
nativeItemRef,
43394340
parentItemId: null,
43404341
ordinal: input.ordinal,
4341-
status: "completed",
4342+
status: input.status ?? "completed",
43424343
title: null,
43434344
startedAt: input.startedAt,
43444345
completedAt: input.completedAt,
@@ -4408,6 +4409,12 @@ interface ActiveClaudeTurnContext {
44084409
fallbackText: string;
44094410
fallbackNativeItemId: string;
44104411
emittedNativeItemIds: Set<string>;
4412+
// The root reply the latest assistant frame carried; a steer's aborted_streaming cuts it off.
4413+
lastReply: {
4414+
readonly nativeItemId: string;
4415+
readonly text: string;
4416+
readonly startedAt: DateTime.Utc;
4417+
} | null;
44114418
// The CLI repeats a notice that ends a prompt as the turn's result text.
44124419
noticeTexts: Set<string>;
44134420
};
@@ -8518,16 +8525,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
85188525
});
85198526
});
85208527

8521-
const emitAssistantTextArtifacts = Effect.fnUntraced(function* (input: {
8528+
const publishAssistantArtifacts = Effect.fnUntraced(function* (input: {
85228529
readonly context: ActiveClaudeTurnContext;
85238530
readonly nativeItemId: string;
85248531
readonly text: string;
8532+
readonly status: "completed" | "interrupted";
8533+
readonly startedAt: DateTime.Utc;
8534+
readonly completedAt: DateTime.Utc;
85258535
}) {
8526-
if (input.context.assistant.emittedNativeItemIds.has(input.nativeItemId)) {
8527-
return;
8528-
}
8529-
input.context.assistant.emittedNativeItemIds.add(input.nativeItemId);
8530-
const now = yield* DateTime.now;
85318536
const ordinal = yield* resolveItemOrdinal(input.context, input.nativeItemId);
85328537
const artifacts = buildAssistantArtifacts({
85338538
idAllocator,
@@ -8536,8 +8541,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
85368541
nativeItemId: input.nativeItemId,
85378542
text: input.text,
85388543
ordinal,
8539-
startedAt: now,
8540-
completedAt: now,
8544+
status: input.status,
8545+
startedAt: input.startedAt,
8546+
completedAt: input.completedAt,
85418547
});
85428548
yield* Effect.all(
85438549
[
@@ -8561,6 +8567,25 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
85618567
);
85628568
});
85638569

8570+
const emitAssistantTextArtifacts = Effect.fnUntraced(function* (input: {
8571+
readonly context: ActiveClaudeTurnContext;
8572+
readonly nativeItemId: string;
8573+
readonly text: string;
8574+
}) {
8575+
if (input.context.assistant.emittedNativeItemIds.has(input.nativeItemId)) {
8576+
return undefined;
8577+
}
8578+
input.context.assistant.emittedNativeItemIds.add(input.nativeItemId);
8579+
const now = yield* DateTime.now;
8580+
yield* publishAssistantArtifacts({
8581+
...input,
8582+
status: "completed",
8583+
startedAt: now,
8584+
completedAt: now,
8585+
});
8586+
return now;
8587+
});
8588+
85648589
const endSubagentsOfClosedQuery = Effect.fnUntraced(function* (input: {
85658590
readonly closedQuery: ClaudeLiveQueryContext;
85668591
readonly context: ActiveClaudeTurnContext;
@@ -10189,6 +10214,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1018910214
}
1019010215
}
1019110216
const assistantText = assistantTextFromSdkMessage(message);
10217+
if (message.type === "assistant" && assistantParentToolUseId === null) {
10218+
context.assistant.lastReply = null;
10219+
}
1019210220
if (
1019310221
assistantText !== null &&
1019410222
assistantText.text.length > 0 &&
@@ -10237,11 +10265,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1023710265
return;
1023810266
}
1023910267
if (assistantText !== null && assistantText.text.length > 0) {
10240-
yield* emitAssistantTextArtifacts({
10268+
const startedAt = yield* emitAssistantTextArtifacts({
1024110269
context,
1024210270
nativeItemId: assistantText.nativeItemId,
1024310271
text: assistantText.text,
1024410272
});
10273+
if (startedAt !== undefined) {
10274+
context.assistant.lastReply = { ...assistantText, startedAt };
10275+
}
1024510276
return;
1024610277
}
1024710278

@@ -10314,6 +10345,16 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1031410345
const interrupted = (yield* Ref.get(interruptedTurns)).has(context.providerTurnId);
1031510346
const wasSteered = (yield* Ref.get(steeredTurns)).has(context.providerTurnId);
1031610347
if (!interrupted && wasSteered && isClaudeActiveSteeringAbortResult(message)) {
10348+
const cutOffReply = context.assistant.lastReply;
10349+
context.assistant.lastReply = null;
10350+
if (message.terminal_reason === "aborted_streaming" && cutOffReply !== null) {
10351+
yield* publishAssistantArtifacts({
10352+
context,
10353+
...cutOffReply,
10354+
status: "interrupted",
10355+
completedAt,
10356+
});
10357+
}
1031710358
context.steeredSegmentUsage.push(message.usage);
1031810359
context.steeredLedgerUsage = addClaudeLedgerUsage(
1031910360
context.steeredLedgerUsage,
@@ -11578,6 +11619,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1157811619
fallbackText: "",
1157911620
fallbackNativeItemId: `assistant:${turnInput.runId}`,
1158011621
emittedNativeItemIds: new Set(),
11622+
lastReply: null,
1158111623
noticeTexts: new Set(),
1158211624
},
1158311625
reasoning: {

‎apps/web/src/components/chat/TimelineAssistantRows.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ export function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind
7070
onOpenTurnDiff={ctx.onOpenTurnDiff}
7171
/>
7272
) : null}
73+
{!row.showAssistantMeta && row.projectedItem?.item.status === "interrupted" ? (
74+
<div className="mt-1.5 flex">
75+
<AssistantStatusBadge status="interrupted" />
76+
</div>
77+
) : null}
7378
{row.showAssistantMeta ? (
7479
<AssistantMessageMeta
7580
className="mt-1.5"
@@ -178,9 +183,7 @@ function AssistantMessageMeta({
178183
)}
179184
>
180185
{projectedItem && projectedItem.item.status !== "completed" ? (
181-
<span className="rounded-full border border-border/70 px-1.5 py-0.5 font-mono text-3xs text-muted-foreground">
182-
{projectedItem.item.status}
183-
</span>
186+
<AssistantStatusBadge status={projectedItem.item.status} />
184187
) : null}
185188
<AssistantCopyButton
186189
message={message}
@@ -204,6 +207,14 @@ function AssistantMessageMeta({
204207
);
205208
}
206209

210+
function AssistantStatusBadge({ status }: { status: string }) {
211+
return (
212+
<span className="rounded-full border border-border/70 px-1.5 py-0.5 font-mono text-3xs text-muted-foreground">
213+
{status}
214+
</span>
215+
);
216+
}
217+
207218
function AssistantCopyButton({
208219
message,
209220
showCopyButton,

0 commit comments

Comments
 (0)