Skip to content

Commit 05b5fb2

Browse files
Fail Claude runs on is_error results; harden idle resume with fixtures
Two additions from the session-audit work (#3677), rebased onto this PR's continuation architecture: - The SDK reports API-level failures (401 auth, 529 overloaded) as result subtype "success" with is_error: true. terminalStatusFromResult only checked the subtype, so those runs projected as completed with no failure recorded (observed on 4 real threads in the audit). Terminal status now honors is_error and the failure keeps the API error text and an api_error_<status> code. Replay fixture claude_result_is_error is built from the real 401 chunks (thread 47763f5e run 1). - Resume hardening on top of this PR's providerTurnOrdinal signal: the openedNativeThreads insert now happens only after queryRunner.open succeeds, so a failed create can no longer poison the in-memory state into resuming a session that was never created. Replay fixture claude_idle_resume drives the real 30-minute idle reaper via a new advance_clock fixture step (TestClock) and asserts the reopen uses resume with both runs completing — it reproduces the exact "Session ID ... is already in use" failure this PR fixes. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 6840b8d commit 05b5fb2

10 files changed

Lines changed: 282 additions & 21 deletions

File tree

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,9 @@ function terminalStatusFromResult(
16821682
"completed" | "interrupted" | "failed" | "cancelled"
16831683
> {
16841684
if (message.subtype === "success") {
1685-
return "completed";
1685+
// The SDK reports API-level failures (401 auth, 529 overloaded, …) as
1686+
// subtype "success" with is_error set; the turn produced no real work.
1687+
return message.is_error ? "failed" : "completed";
16861688
}
16871689
const errorText = message.errors.join("\n").toLowerCase();
16881690
if (errorText.includes("interrupt")) {
@@ -1698,6 +1700,28 @@ function isClaudeActiveSteeringAbortResult(message: SDKResultMessage): boolean {
16981700
return message.terminal_reason === "aborted_streaming";
16991701
}
17001702

1703+
function providerFailureFromResult(
1704+
message: SDKResultMessage,
1705+
): OrchestrationV2ProviderFailure | null {
1706+
if (message.subtype !== "success") {
1707+
return makeProviderFailure({
1708+
message: message.errors.join("\n"),
1709+
code: message.subtype,
1710+
class: "provider_error",
1711+
});
1712+
}
1713+
if (!message.is_error) {
1714+
return null;
1715+
}
1716+
const apiErrorStatus = message.api_error_status ?? null;
1717+
return makeProviderFailure({
1718+
message: message.result,
1719+
code: apiErrorStatus === null ? "sdk_result_error" : `api_error_${apiErrorStatus}`,
1720+
class: "provider_error",
1721+
retryable: apiErrorStatus === 429 || apiErrorStatus === 529 ? true : null,
1722+
});
1723+
}
1724+
17011725
function buildAssistantArtifacts(input: {
17021726
readonly idAllocator: IdAllocatorV2Shape;
17031727
readonly turnInput: ProviderAdapterV2TurnInput;
@@ -3177,7 +3201,12 @@ export function makeClaudeAdapterV2(
31773201
return;
31783202
}
31793203

3180-
const resultText = resultTextFromSdkMessage(message);
3204+
// An is_error result's text is the error message; it belongs on the
3205+
// terminal-failure item, not on a synthetic assistant message.
3206+
const resultText =
3207+
message.type === "result" && message.subtype === "success" && message.is_error
3208+
? null
3209+
: resultTextFromSdkMessage(message);
31813210
if (
31823211
context.assistant.emittedNativeItemIds.size === 0 &&
31833212
context.assistant.fallbackText.length === 0 &&
@@ -3200,19 +3229,12 @@ export function makeClaudeAdapterV2(
32003229
next.delete(context.providerTurnId);
32013230
return next;
32023231
});
3232+
const resultFailure = interrupted ? null : providerFailureFromResult(message);
32033233
yield* finalizeActiveTurn({
32043234
context,
32053235
status: interrupted ? "interrupted" : terminalStatusFromResult(message),
32063236
completedAt,
3207-
...(message.subtype === "success" || interrupted
3208-
? {}
3209-
: {
3210-
failure: makeProviderFailure({
3211-
message: message.errors.join("\n"),
3212-
code: message.subtype,
3213-
class: "provider_error",
3214-
}),
3215-
}),
3237+
...(resultFailure === null ? {} : { failure: resultFailure }),
32163238
});
32173239
}
32183240
});
@@ -3343,15 +3365,7 @@ export function makeClaudeAdapterV2(
33433365
yield* existing.query.close.pipe(Effect.ignore);
33443366
}
33453367

3346-
const openedWithResume = yield* Ref.modify(openedNativeThreads, (current) => {
3347-
const hasOpenedThread = current.has(nativeThreadId);
3348-
if (hasOpenedThread) {
3349-
return [true, current];
3350-
}
3351-
const updated = new Set(current);
3352-
updated.add(nativeThreadId);
3353-
return [false, updated];
3354-
});
3368+
const openedWithResume = (yield* Ref.get(openedNativeThreads)).has(nativeThreadId);
33553369
// openedNativeThreads is per session instance and is lost when the
33563370
// provider session is idle-released. A prior persisted provider turn
33573371
// proves the native session already exists, so the query must resume
@@ -3380,6 +3394,17 @@ export function makeClaudeAdapterV2(
33803394
...(shouldInstallClaudePermissionCallback(queryPolicy) ? { canUseTool } : {}),
33813395
}),
33823396
});
3397+
// Marked only after a successful open: a failed create must not
3398+
// leave the runtime believing the native session exists, or the
3399+
// retry would resume a session that was never created.
3400+
yield* Ref.update(openedNativeThreads, (current) => {
3401+
if (current.has(nativeThreadId)) {
3402+
return current;
3403+
}
3404+
const updated = new Set(current);
3405+
updated.add(nativeThreadId);
3406+
return updated;
3407+
});
33833408
const closed = yield* Deferred.make<void, never>();
33843409
const context: ClaudeLiveQueryContext = {
33853410
nativeThreadId,

‎apps/server/src/orchestration-v2/testkit/OrchestratorReplayFixtures.contract.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ describe("orchestrator replay fixture contract", () => {
6969
throw new Error(`${fixture.name}/${provider.driver} must start with thread.create`);
7070
}
7171
assert.equal(firstCommand.threadId, materialized.projectionThreadIds[0]);
72-
assert.equal(materialized.commands.length, fixture.buildInput().steps.length + 1);
72+
// advance_clock only moves the test clock; every other input step
73+
// dispatches a command.
74+
const commandProducingSteps = fixture
75+
.buildInput()
76+
.steps.filter((step) => step.type !== "advance_clock");
77+
assert.equal(materialized.commands.length, commandProducingSteps.length + 1);
7378
assert.isAtLeast(materialized.steps.length, materialized.commands.length);
7479
assert.equal(typeof provider.assertOutput, "function");
7580

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{"type":"transcript_start","provider":"claudeAgent","protocol":"claude-agent-sdk.query","version":"0.2.111","scenario":"claude_idle_resume","metadata":{"prompts":["Respond with exactly: idle resume first turn complete","Respond with exactly: idle resume second turn complete"],"model":"claude-sonnet-4-6","nativeSessionId":"1d1e5e55-1041-4e40-8e40-000000000001","queryMode":"restart","tools":"claude_code","permissionMode":"bypassPermissions","generatedBy":"manual-replay-from-thread-d0fe9018 runs 6-8 (idle-released session must reopen with resume, 2026-07-02T20:46:05Z)"}}
2+
{"type":"expect_outbound","label":"query.open:1","frame":{"type":"query.open","options":{"model":"claude-sonnet-4-6","tools":{"type":"preset","preset":"claude_code"},"permissionMode":"bypassPermissions","allowDangerouslySkipPermissions":true,"sessionId":"1d1e5e55-1041-4e40-8e40-000000000001"}}}
3+
{"type":"expect_outbound","label":"prompt.offer:1","frame":{"type":"prompt.offer","message":{"type":"user","message":{"role":"user","content":"Respond with exactly: idle resume first turn complete"},"parent_tool_use_id":null}}}
4+
{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"hook_started","hook_id":"96353a54-8984-4e77-8060-b1dc6b030cf6","hook_name":"SessionStart:startup","hook_event":"SessionStart","uuid":"ffbfcc28-dd82-4c2a-a99f-58e5af6e393e","session_id":"1d1e5e55-1041-4e40-8e40-000000000001"}}
5+
{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"hook_response","hook_id":"96353a54-8984-4e77-8060-b1dc6b030cf6","hook_name":"SessionStart:startup","hook_event":"SessionStart","output":"","stdout":"","stderr":"","exit_code":0,"outcome":"success","uuid":"bbf29fb9-d4b3-4ab2-b0e3-9d2aa833099c","session_id":"1d1e5e55-1041-4e40-8e40-000000000001"}}
6+
{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"init","agents":[],"apiKeySource":"none","claude_code_version":"2.1.111","cwd":"/tmp/claude-replay-claude_idle_resume","tools":[],"mcp_servers":[],"model":"claude-sonnet-4-6","permissionMode":"bypassPermissions","slash_commands":[],"output_style":"default","skills":[],"plugins":[],"fast_mode_state":"off","uuid":"f468f37d-e0e6-43cf-bcd7-71e34045083e","session_id":"1d1e5e55-1041-4e40-8e40-000000000001"}}
7+
{"type":"emit_inbound","label":"assistant","frame":{"type":"assistant","message":{"model":"claude-sonnet-4-6","id":"msg_01DKpWHtEMhugcX5nJwxKfeM","type":"message","role":"assistant","content":[{"type":"text","text":"idle resume first turn complete"}],"stop_reason":null,"stop_sequence":null,"stop_details":null,"usage":{"input_tokens":3,"cache_creation_input_tokens":0,"cache_read_input_tokens":11792,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":1,"service_tier":"standard","inference_geo":"not_available"},"context_management":null},"parent_tool_use_id":null,"session_id":"1d1e5e55-1041-4e40-8e40-000000000001","uuid":"7e96c9db-0543-4276-b97d-ced7f53b93c4"}}
8+
{"type":"emit_inbound","label":"rate_limit_event","frame":{"type":"rate_limit_event","rate_limit_info":{"status":"allowed"},"uuid":"28620e49-aa10-42fa-a7f9-768c9c79c093","session_id":"1d1e5e55-1041-4e40-8e40-000000000001"}}
9+
{"type":"emit_inbound","label":"result","frame":{"type":"result","subtype":"success","is_error":false,"api_error_status":null,"duration_ms":2809,"duration_api_ms":2523,"num_turns":1,"result":"idle resume first turn complete","stop_reason":"end_turn","session_id":"1d1e5e55-1041-4e40-8e40-000000000001","total_cost_usd":0.0036516,"usage":{"input_tokens":3,"cache_creation_input_tokens":0,"cache_read_input_tokens":11792,"output_tokens":7,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[{"input_tokens":3,"output_tokens":7,"cache_read_input_tokens":11792,"cache_creation_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"type":"message"}],"speed":"standard"},"modelUsage":{"claude-sonnet-4-6":{"inputTokens":3,"outputTokens":7,"cacheReadInputTokens":11792,"cacheCreationInputTokens":0,"webSearchRequests":0,"costUSD":0.0036516,"contextWindow":200000,"maxOutputTokens":32000}},"permission_denials":[],"terminal_reason":"completed","fast_mode_state":"off","uuid":"d88c31ae-a8fd-4ed4-a168-96cfd3b0edd7"}}
10+
{"type":"runtime_exit","status":"success"}
11+
{"type":"expect_outbound","label":"query.open:2","frame":{"type":"query.open","options":{"model":"claude-sonnet-4-6","tools":{"type":"preset","preset":"claude_code"},"permissionMode":"bypassPermissions","allowDangerouslySkipPermissions":true,"resume":"1d1e5e55-1041-4e40-8e40-000000000001"}}}
12+
{"type":"expect_outbound","label":"prompt.offer:2","frame":{"type":"prompt.offer","message":{"type":"user","message":{"role":"user","content":"Respond with exactly: idle resume second turn complete"},"parent_tool_use_id":null}}}
13+
{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"hook_started","hook_id":"4db1e61e-de28-49ee-b7ef-c375670d0aae","hook_name":"SessionStart:resume","hook_event":"SessionStart","uuid":"66e900fe-b02e-41a0-ac08-92260125b13d","session_id":"377e06a0-fe46-420e-bfaa-b4b9fcc8b871"}}
14+
{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"hook_response","hook_id":"4db1e61e-de28-49ee-b7ef-c375670d0aae","hook_name":"SessionStart:resume","hook_event":"SessionStart","output":"","stdout":"","stderr":"","exit_code":0,"outcome":"success","uuid":"03f2754d-daee-4128-b180-c1a39faa03f0","session_id":"377e06a0-fe46-420e-bfaa-b4b9fcc8b871"}}
15+
{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"init","agents":[],"apiKeySource":"none","claude_code_version":"2.1.111","cwd":"/tmp/claude-replay-claude_idle_resume","tools":[],"mcp_servers":[],"model":"claude-sonnet-4-6","permissionMode":"bypassPermissions","slash_commands":[],"output_style":"default","skills":[],"plugins":[],"fast_mode_state":"off","uuid":"5e1d64c9-a2ca-4ad1-ba2c-546ba18d1785","session_id":"1d1e5e55-1041-4e40-8e40-000000000001"}}
16+
{"type":"emit_inbound","label":"assistant","frame":{"type":"assistant","message":{"model":"claude-sonnet-4-6","id":"msg_01CiTVsN2HCfqr6Ge166z8hv","type":"message","role":"assistant","content":[{"type":"text","text":"idle resume second turn complete"}],"stop_reason":null,"stop_sequence":null,"stop_details":null,"usage":{"input_tokens":3,"cache_creation_input_tokens":0,"cache_read_input_tokens":11811,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":7,"service_tier":"standard","inference_geo":"not_available"},"context_management":null},"parent_tool_use_id":null,"session_id":"1d1e5e55-1041-4e40-8e40-000000000001","uuid":"e821cdff-a217-4fb3-b1d0-f89387dbac41"}}
17+
{"type":"emit_inbound","label":"rate_limit_event","frame":{"type":"rate_limit_event","rate_limit_info":{"status":"allowed"},"uuid":"21e2dfd3-a7f5-48d1-a343-ddb1be3198c8","session_id":"1d1e5e55-1041-4e40-8e40-000000000001"}}
18+
{"type":"emit_inbound","label":"result","frame":{"type":"result","subtype":"success","is_error":false,"api_error_status":null,"duration_ms":2103,"duration_api_ms":1644,"num_turns":1,"result":"idle resume second turn complete","stop_reason":"end_turn","session_id":"1d1e5e55-1041-4e40-8e40-000000000001","total_cost_usd":0.0036573,"usage":{"input_tokens":3,"cache_creation_input_tokens":0,"cache_read_input_tokens":11811,"output_tokens":7,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[{"input_tokens":3,"output_tokens":7,"cache_read_input_tokens":11811,"cache_creation_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"type":"message"}],"speed":"standard"},"modelUsage":{"claude-sonnet-4-6":{"inputTokens":3,"outputTokens":7,"cacheReadInputTokens":11811,"cacheCreationInputTokens":0,"webSearchRequests":0,"costUSD":0.0036573,"contextWindow":200000,"maxOutputTokens":32000}},"permission_denials":[],"terminal_reason":"completed","fast_mode_state":"off","uuid":"a876579c-a18c-4237-ba4e-6105b2162bbb"}}
19+
{"type":"runtime_exit","status":"success"}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { OrchestratorFixtureInput } from "../shared.ts";
2+
3+
export const CLAUDE_IDLE_RESUME_PROMPT_1 = "Respond with exactly: idle resume first turn complete";
4+
export const CLAUDE_IDLE_RESUME_PROMPT_2 = "Respond with exactly: idle resume second turn complete";
5+
6+
/**
7+
* Regression for the stale-session first-message failure (threads 47763f5e
8+
* run 10 and d0fe9018 runs 7/8, 2026-07-01/02): the provider session manager
9+
* idle-releases the session runtime between turns, wiping the adapter's
10+
* in-memory openedNativeThreads set. The next turn must reopen the SDK query
11+
* with `resume` (the persisted provider thread proves the native session
12+
* exists) — a create-style `sessionId` open fails against an existing
13+
* session and burned the user's first message after every idle gap.
14+
*/
15+
export function claudeIdleResumeInput(): OrchestratorFixtureInput {
16+
return {
17+
steps: [
18+
{ type: "message", text: CLAUDE_IDLE_RESUME_PROMPT_1 },
19+
// Past ProviderSessionManager's 30-minute idle timeout: the reaper
20+
// releases the live session entry and closes the SDK query.
21+
{ type: "advance_clock", duration: "31 minutes" },
22+
{ type: "message", text: CLAUDE_IDLE_RESUME_PROMPT_2 },
23+
],
24+
};
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { assert } from "@effect/vitest";
2+
import type { ProviderReplayTranscript } from "@t3tools/contracts";
3+
4+
import type { OrchestratorV2ScenarioResult } from "../../OrchestratorScenario.ts";
5+
import {
6+
assertBaseProjection,
7+
assertSemanticProjectionIntegrity,
8+
assertUserMessagesInclude,
9+
projectionFor,
10+
} from "../shared.ts";
11+
import { CLAUDE_IDLE_RESUME_PROMPT_1, CLAUDE_IDLE_RESUME_PROMPT_2 } from "./input.ts";
12+
13+
export function assertClaudeIdleResumeOutput(
14+
result: OrchestratorV2ScenarioResult,
15+
transcript: ProviderReplayTranscript,
16+
) {
17+
// The transcript itself enforces the regression: query.open:2 expects a
18+
// `resume`-style open. A create-style reopen (the bug) fails the replay at
19+
// the boundary before these assertions run. Both runs must complete — the
20+
// pre-fix behavior burned the first post-idle message as a failed run.
21+
assertBaseProjection({
22+
result,
23+
transcript,
24+
runCount: 2,
25+
runStatuses: ["completed", "completed"],
26+
});
27+
28+
const projection = projectionFor(result, transcript.scenario);
29+
assertSemanticProjectionIntegrity(projection);
30+
assertUserMessagesInclude(projection, [CLAUDE_IDLE_RESUME_PROMPT_1, CLAUDE_IDLE_RESUME_PROMPT_2]);
31+
32+
const assistantTexts = projection.turnItems.flatMap((item) =>
33+
item.type === "assistant_message" ? [item.text] : [],
34+
);
35+
assert.deepEqual(assistantTexts, [
36+
"idle resume first turn complete",
37+
"idle resume second turn complete",
38+
]);
39+
}

0 commit comments

Comments
 (0)