Skip to content

Commit c56aaad

Browse files
committed
fix(server): imported Claude Code sessions resume on their first follow-up
An imported Claude Code session is written as a provider thread with a strong native ref, no conversation head and no provider turns. `openQuery` only resumed when one of those existed or this session instance had already opened the native thread, so the first follow-up reopened the session with a fixed `sessionId` and the CLI rejected it: "Session ID ... is already in use". `resumeThread` is how the orchestrator re-attaches a provider thread that already carries a native id. When the thread has never been bound by a provider session of ours (`providerSessionId === null`, the shape both import paths write), record its native id as adopted and let `openQuery` resume it. Threads this adapter minted keep their provider session id, so a retry after a failed first create still creates the session.
1 parent 00eb8f6 commit c56aaad

2 files changed

Lines changed: 87 additions & 4 deletions

File tree

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ describe("ClaudeAdapterV2 native fork", () => {
17831783
});
17841784

17851785
describe("ClaudeAdapterV2 native session identity", () => {
1786-
const openTurnWithOrdinal = (providerTurnOrdinal: number) =>
1786+
const openTurnWithOrdinal = (providerTurnOrdinal: number, attach?: "imported" | "minted") =>
17871787
Effect.scoped(
17881788
Effect.gen(function* () {
17891789
const fileSystem = yield* FileSystem.FileSystem;
@@ -1827,12 +1827,37 @@ describe("ClaudeAdapterV2 native session identity", () => {
18271827
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
18281828
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
18291829
});
1830-
const providerThread = yield* runtime.ensureThread({
1830+
const now = yield* DateTime.now;
1831+
const ensured = yield* runtime.ensureThread({
18311832
threadId,
18321833
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
18331834
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
18341835
});
1835-
const now = yield* DateTime.now;
1836+
let providerThread = ensured;
1837+
if (attach !== undefined) {
1838+
// Imports write a provider thread no session of ours has bound:
1839+
// a strong native ref, no conversation head and no provider turns.
1840+
// The orchestrator hands such a thread to resumeThread before the
1841+
// first follow-up, as it does for any thread with a native ref.
1842+
providerThread = yield* runtime.resumeThread({
1843+
providerThread:
1844+
attach === "imported"
1845+
? {
1846+
...ensured,
1847+
providerSessionId: null,
1848+
nativeThreadRef: {
1849+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
1850+
nativeId: "imported-native-session",
1851+
strength: "strong",
1852+
},
1853+
nativeConversationHeadRef: null,
1854+
}
1855+
: ensured,
1856+
threadId,
1857+
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
1858+
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
1859+
});
1860+
}
18361861
yield* runtime.startTurn(
18371862
makeClaudeTestTurnInput({
18381863
threadId,
@@ -1867,6 +1892,30 @@ describe("ClaudeAdapterV2 native session identity", () => {
18671892
assert.equal(openedQueries[0]?.options.sessionId, undefined);
18681893
}),
18691894
);
1895+
1896+
it.effect("resumes an imported native thread on its first provider turn", () =>
1897+
Effect.gen(function* () {
1898+
// #15243: the imported session already exists in Claude Code, so
1899+
// reopening it with a fixed session id fails with "Session ID ... is
1900+
// already in use" even though no provider turn has been persisted yet.
1901+
const openedQueries = yield* openTurnWithOrdinal(1, "imported");
1902+
assert.equal(openedQueries.length, 1);
1903+
assert.equal(openedQueries[0]?.options.resume, "imported-native-session");
1904+
assert.equal(openedQueries[0]?.options.sessionId, undefined);
1905+
}),
1906+
);
1907+
1908+
it.effect("still creates a minted native session that resumeThread re-attached", () =>
1909+
Effect.gen(function* () {
1910+
// A thread this adapter minted keeps its provider session id. When its
1911+
// first create failed, the retry passes through resumeThread too and
1912+
// must create the session rather than resume one that never existed.
1913+
const openedQueries = yield* openTurnWithOrdinal(1, "minted");
1914+
assert.equal(openedQueries.length, 1);
1915+
assert.equal(openedQueries[0]?.options.sessionId, "native-session-identity");
1916+
assert.equal(openedQueries[0]?.options.resume, undefined);
1917+
}),
1918+
);
18701919
});
18711920

18721921
const encodeJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2987,6 +2987,11 @@ export function makeClaudeAdapterV2(
29872987
const steeredTurns = yield* Ref.make(new Set<OrchestrationV2ProviderTurn["id"]>());
29882988
const queryContext = yield* Ref.make<ClaudeLiveQueryContext | null>(null);
29892989
const openedNativeThreads = yield* Ref.make(new Set<string>());
2990+
// Native threads resumeThread adopted while no provider session of
2991+
// ours had bound them. An imported Claude Code session arrives that
2992+
// way: a strong native ref, no conversation head and no provider turn,
2993+
// yet the native session already exists on disk.
2994+
const adoptedNativeThreads = yield* Ref.make(new Set<string>());
29902995
const latestPlanByKind = yield* Ref.make(new Map<string, OrchestrationV2PlanArtifact>());
29912996
const planIdsByNativeItem = yield* Ref.make(
29922997
new Map<string, OrchestrationV2PlanArtifact["id"]>(),
@@ -6932,8 +6937,17 @@ export function makeClaudeAdapterV2(
69326937
// it; reopening with a fixed session id makes the CLI fail fast with
69336938
// "Session ID ... is already in use".
69346939
const hasPersistedProviderTurn = turnInput.providerTurnOrdinal > 1;
6940+
// An imported session has no provider turn yet either, but
6941+
// resumeThread adopted it from outside this adapter, so its native
6942+
// session exists just the same.
6943+
const adoptedExistingNativeThread = (yield* Ref.get(adoptedNativeThreads)).has(
6944+
nativeThreadId,
6945+
);
69356946
const shouldResume =
6936-
resumeSessionAt !== undefined || openedWithResume || hasPersistedProviderTurn;
6947+
resumeSessionAt !== undefined ||
6948+
openedWithResume ||
6949+
hasPersistedProviderTurn ||
6950+
adoptedExistingNativeThread;
69376951
const queryOptions = makeClaudeQueryOptions({
69386952
modelSelection: turnInput.modelSelection,
69396953
nativeThreadId,
@@ -7489,6 +7503,26 @@ export function makeClaudeAdapterV2(
74897503
),
74907504
resumeThread: Effect.fn("ClaudeAdapterV2.resumeThread")(
74917505
function* (threadInput: { readonly providerThread: OrchestrationV2ProviderThread }) {
7506+
// A provider thread no session of ours has bound was adopted from
7507+
// outside this adapter (an imported Claude Code session), so its
7508+
// native session already exists and the first follow-up has to
7509+
// resume it. A thread this adapter minted keeps its provider
7510+
// session id, so a retry after a failed create still creates.
7511+
const adoptedNativeThreadId = threadInput.providerThread.nativeThreadRef?.nativeId;
7512+
if (
7513+
threadInput.providerThread.providerSessionId === null &&
7514+
adoptedNativeThreadId !== undefined &&
7515+
adoptedNativeThreadId !== null
7516+
) {
7517+
yield* Ref.update(adoptedNativeThreads, (current) => {
7518+
if (current.has(adoptedNativeThreadId)) {
7519+
return current;
7520+
}
7521+
const updated = new Set(current);
7522+
updated.add(adoptedNativeThreadId);
7523+
return updated;
7524+
});
7525+
}
74927526
const updatedAt = yield* DateTime.now;
74937527
return {
74947528
...threadInput.providerThread,

0 commit comments

Comments
 (0)