Skip to content

Commit ab1de49

Browse files
committed
fix(server): replace Claude sessions that never saved a message
1 parent 348f217 commit ab1de49

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,67 @@ describe("ClaudeAdapterV2 executable path", () => {
12871287
);
12881288
});
12891289

1290+
describe("ClaudeAdapterV2 resumeThread", () => {
1291+
it.effect("fails for a transcript without messages so a fresh session replaces it", () =>
1292+
Effect.scoped(
1293+
Effect.gen(function* () {
1294+
const fileSystem = yield* FileSystem.FileSystem;
1295+
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
1296+
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
1297+
settings: DEFAULT_CLAUDE_SETTINGS,
1298+
environment: {},
1299+
attachmentsDir: yield* fileSystem.makeTempDirectoryScoped({
1300+
prefix: "t3-claude-empty-session-",
1301+
}),
1302+
fileSystem,
1303+
path: yield* Path.Path,
1304+
crypto: yield* Crypto.Crypto,
1305+
idAllocator: yield* IdAllocator.IdAllocatorV2,
1306+
queryRunner: {
1307+
allocateSessionId: Effect.succeed("native-thread-empty"),
1308+
open: () => Effect.die("unused"),
1309+
forkSession: () => Effect.die("unused"),
1310+
subagentLaunchToolUseId: () => Effect.succeed(null),
1311+
sessionIsEmpty: (input) => Effect.succeed(input.sessionId === "native-thread-empty"),
1312+
assertComplete: Effect.void,
1313+
},
1314+
});
1315+
const threadId = ThreadId.make("thread-claude-empty-session");
1316+
const runtime = yield* adapter.openSession({
1317+
threadId,
1318+
providerSessionId: ProviderSessionId.make("provider-session-claude-empty-session"),
1319+
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
1320+
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
1321+
});
1322+
const providerThread = yield* runtime.ensureThread({
1323+
threadId,
1324+
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
1325+
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
1326+
});
1327+
1328+
const error = yield* Effect.flip(runtime.resumeThread({ providerThread }));
1329+
assert.equal(error._tag, "ProviderAdapterResumeThreadError");
1330+
1331+
const saved = yield* runtime.resumeThread({
1332+
providerThread: {
1333+
...providerThread,
1334+
nativeThreadRef: {
1335+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
1336+
nativeId: "native-thread-saved",
1337+
strength: "strong",
1338+
},
1339+
},
1340+
});
1341+
assert.equal(saved.nativeThreadRef?.nativeId, "native-thread-saved");
1342+
}),
1343+
).pipe(
1344+
Effect.provide(
1345+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
1346+
),
1347+
),
1348+
);
1349+
});
1350+
12901351
describe("ClaudeAdapterV2 resume compaction", () => {
12911352
it.effect("resolves and cancels the SDK resume dialog through structured runtime input", () =>
12921353
Effect.scoped(

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
type EffortLevel,
2222
type ForkSessionOptions,
2323
type ForkSessionResult,
24+
getSessionInfo,
25+
getSessionMessages,
2426
getSubagentMessages,
2527
query,
2628
type Options as ClaudeQueryOptions,
@@ -464,6 +466,10 @@ export interface ClaudeAgentSdkQueryRunnerShape {
464466
readonly subagentLaunchToolUseId: (
465467
input: ClaudeAgentSdkSubagentLookupInput,
466468
) => Effect.Effect<string | null, ClaudeAgentSdkQueryRunnerError>;
469+
/** Whether the CLI's session storage holds a transcript with no message to resume. */
470+
readonly sessionIsEmpty?: (
471+
input: ClaudeAgentSdkSessionLookupInput,
472+
) => Effect.Effect<boolean, ClaudeAgentSdkQueryRunnerError>;
467473
readonly assertComplete: Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
468474
}
469475

@@ -479,6 +485,12 @@ export interface ClaudeAgentSdkSessionForkInput {
479485
readonly providerSessionId: OrchestrationV2ProviderSession["id"];
480486
}
481487

488+
export interface ClaudeAgentSdkSessionLookupInput {
489+
readonly sessionId: string;
490+
/** The instance's `CLAUDE_CONFIG_DIR`, where the CLI keeps its sessions. */
491+
readonly configDir: string | undefined;
492+
}
493+
482494
export interface ClaudeAgentSdkSubagentLookupInput {
483495
readonly sessionId: string;
484496
readonly agentId: string;
@@ -713,6 +725,7 @@ export const layerQueryRunner: Layer.Layer<
713725
const { native: nativeEventLogger } = yield* ProviderEventLoggers.ProviderEventLoggers;
714726
const agentScope = yield* AgentScope;
715727
const platform = yield* HostProcess.Platform;
728+
const hostEnvironment = yield* HostProcess.Environment;
716729

717730
return ClaudeAgentSdkQueryRunner.of({
718731
allocateSessionId: crypto
@@ -1019,6 +1032,26 @@ export const layerQueryRunner: Layer.Layer<
10191032
return toolUseId;
10201033
},
10211034
),
1035+
sessionIsEmpty: Effect.fn("ClaudeAgentSdkQueryRunner.sessionIsEmpty")(function* (
1036+
input: ClaudeAgentSdkSessionLookupInput,
1037+
) {
1038+
// The SDK reads sessions from this process's config dir only.
1039+
if (input.configDir !== hostEnvironment.CLAUDE_CONFIG_DIR) {
1040+
return false;
1041+
}
1042+
const info = yield* Effect.tryPromise({
1043+
try: () => getSessionInfo(input.sessionId),
1044+
catch: (cause) => queryRunnerError(cause, "getSessionInfo"),
1045+
});
1046+
if (info === undefined) {
1047+
return false;
1048+
}
1049+
const messages = yield* Effect.tryPromise({
1050+
try: () => getSessionMessages(input.sessionId, { limit: 1 }),
1051+
catch: (cause) => queryRunnerError(cause, "getSessionMessages"),
1052+
});
1053+
return messages.length === 0;
1054+
}),
10221055
assertComplete: Effect.void,
10231056
});
10241057
}),
@@ -9182,6 +9215,30 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
91829215
),
91839216
resumeThread: Effect.fn("ClaudeAdapterV2.resumeThread")(
91849217
function* (threadInput: { readonly providerThread: OrchestrationV2ProviderThread }) {
9218+
// A CLI stopped before it saved the first prompt can leave a
9219+
// transcript with no messages. Claude can neither resume that id
9220+
// nor create it again, so fail and let the caller bind a fresh
9221+
// native session with a context handoff.
9222+
const nativeThreadId = threadInput.providerThread.nativeThreadRef?.nativeId;
9223+
if (
9224+
nativeThreadId !== undefined &&
9225+
nativeThreadId !== null &&
9226+
queryRunner.sessionIsEmpty !== undefined &&
9227+
(yield* Ref.get(queryContext))?.nativeThreadId !== nativeThreadId &&
9228+
!(yield* Ref.get(openedNativeThreads)).has(nativeThreadId) &&
9229+
// An unreadable store leaves the resume to the CLI, as before.
9230+
(yield* queryRunner
9231+
.sessionIsEmpty({
9232+
sessionId: nativeThreadId,
9233+
configDir: adapterOptions.environment.CLAUDE_CONFIG_DIR,
9234+
})
9235+
.pipe(Effect.orElseSucceed(() => false)))
9236+
) {
9237+
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
9238+
driver: CLAUDE_PROVIDER,
9239+
detail: `Claude session ${nativeThreadId} has no conversation to resume.`,
9240+
});
9241+
}
91859242
const updatedAt = yield* DateTime.now;
91869243
return {
91879244
...threadInput.providerThread,

0 commit comments

Comments
 (0)