Skip to content

Commit 7e46f87

Browse files
committed
fix(server): record import provenance on the provider thread
The first version keyed the resume decision on `providerSessionId === null` at `resumeThread`, but the orchestrator binds a provider session to the provider thread when it starts the run, before the turn-start service ever reaches the adapter, so the guard could not fire in production. Both import paths now stamp `nativeMetadata.nativeThreadOrigin = "imported"` on the provider thread they write, and `ClaudeAdapterV2.openQuery` reads it from the turn's provider thread next to the existing resume signals. The provenance is persisted with the row, so it survives idle release and a failed first follow-up, and threads this adapter minted carry no origin and keep creating their session.
1 parent c56aaad commit 7e46f87

7 files changed

Lines changed: 28 additions & 42 deletions

File tree

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,22 +1835,23 @@ describe("ClaudeAdapterV2 native session identity", () => {
18351835
});
18361836
let providerThread = ensured;
18371837
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.
1838+
// An import writes a provider thread with a strong native ref, no
1839+
// conversation head, no provider turns and an `imported` origin in
1840+
// its native metadata. By the time the first follow-up starts, the
1841+
// orchestrator has bound a provider session to it and handed it to
1842+
// resumeThread, as it does for any thread with a native ref.
18421843
providerThread = yield* runtime.resumeThread({
18431844
providerThread:
18441845
attach === "imported"
18451846
? {
18461847
...ensured,
1847-
providerSessionId: null,
18481848
nativeThreadRef: {
18491849
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
18501850
nativeId: "imported-native-session",
18511851
strength: "strong",
18521852
},
18531853
nativeConversationHeadRef: null,
1854+
nativeMetadata: { nativeThreadOrigin: "imported" },
18541855
}
18551856
: ensured,
18561857
threadId,
@@ -1907,9 +1908,9 @@ describe("ClaudeAdapterV2 native session identity", () => {
19071908

19081909
it.effect("still creates a minted native session that resumeThread re-attached", () =>
19091910
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.
1911+
// A thread this adapter minted carries no import origin. When its first
1912+
// create failed, the retry passes through resumeThread too and must
1913+
// create the session rather than resume one that never existed.
19131914
const openedQueries = yield* openTurnWithOrdinal(1, "minted");
19141915
assert.equal(openedQueries.length, 1);
19151916
assert.equal(openedQueries[0]?.options.sessionId, "native-session-identity");

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

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,11 +2987,6 @@ 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>());
29952990
const latestPlanByKind = yield* Ref.make(new Map<string, OrchestrationV2PlanArtifact>());
29962991
const planIdsByNativeItem = yield* Ref.make(
29972992
new Map<string, OrchestrationV2PlanArtifact["id"]>(),
@@ -6937,17 +6932,16 @@ export function makeClaudeAdapterV2(
69376932
// it; reopening with a fixed session id makes the CLI fail fast with
69386933
// "Session ID ... is already in use".
69396934
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-
);
6935+
// An imported session has no provider turn yet either, but the
6936+
// import stamped its provider thread as adopted from outside this
6937+
// app, so its native session exists just the same.
6938+
const importedNativeThread =
6939+
turnInput.providerThread.nativeMetadata?.nativeThreadOrigin === "imported";
69466940
const shouldResume =
69476941
resumeSessionAt !== undefined ||
69486942
openedWithResume ||
69496943
hasPersistedProviderTurn ||
6950-
adoptedExistingNativeThread;
6944+
importedNativeThread;
69516945
const queryOptions = makeClaudeQueryOptions({
69526946
modelSelection: turnInput.modelSelection,
69536947
nativeThreadId,
@@ -7503,26 +7497,6 @@ export function makeClaudeAdapterV2(
75037497
),
75047498
resumeThread: Effect.fn("ClaudeAdapterV2.resumeThread")(
75057499
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-
}
75267500
const updatedAt = yield* DateTime.now;
75277501
return {
75287502
...threadInput.providerThread,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2170,7 +2170,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
21702170
handoffIds: [],
21712171
forkedFrom: null,
21722172
contextUsage: null,
2173-
nativeMetadata: command.importedNativeThread.metadata ?? null,
2173+
nativeMetadata: {
2174+
...command.importedNativeThread.metadata,
2175+
nativeThreadOrigin: "imported",
2176+
},
21742177
createdAt: now,
21752178
updatedAt: now,
21762179
},

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ it.effect("creates a strong provider-thread mapping for an imported native sessi
17281728
appThreadId: input.threadId,
17291729
nativeThreadRef: input.importedNativeThread.ref,
17301730
status: "not_loaded",
1731-
nativeMetadata: input.importedNativeThread.metadata,
1731+
nativeMetadata: { ...input.importedNativeThread.metadata, nativeThreadOrigin: "imported" },
17321732
});
17331733
assert.equal(
17341734
launched.projection.thread.activeProviderThreadId,

‎apps/server/src/project/AgentSessionImporter.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ it.effect("imports messages once and preserves the provider native resume bindin
129129
nativeId: providerSessionId,
130130
strength: "strong",
131131
},
132+
nativeMetadata: { nativeThreadOrigin: "imported" },
132133
});
133134
expect(
134135
writes[0]

‎apps/server/src/project/AgentSessionImporter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ const make = Effect.gen(function* () {
328328
handoffIds: [],
329329
forkedFrom: null,
330330
pendingBackgroundTasks: [],
331+
nativeMetadata: { nativeThreadOrigin: "imported" },
331332
createdAt,
332333
updatedAt,
333334
};

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,12 @@ export const OrchestrationV2ProviderThreadNativeMetadata = Schema.Struct({
803803
updatedAt: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
804804
/** Version 2 scopes provider-derived item ids by provider instance. */
805805
itemIdentityVersion: Schema.optional(Schema.Literal(2)),
806+
/**
807+
* The native thread existed before this app bound it: an imported session.
808+
* An adapter whose create and resume paths differ must resume it even
809+
* before any provider turn of ours has been persisted.
810+
*/
811+
nativeThreadOrigin: Schema.optional(Schema.Literal("imported")),
806812
});
807813
export type OrchestrationV2ProviderThreadNativeMetadata =
808814
typeof OrchestrationV2ProviderThreadNativeMetadata.Type;

0 commit comments

Comments
 (0)