diff --git a/apps/server/src/orchestration-v2/Orchestrator.migration.test.ts b/apps/server/src/orchestration-v2/Orchestrator.migration.test.ts index 706f8427ed..065181f8aa 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.migration.test.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.migration.test.ts @@ -1,6 +1,7 @@ import { assert, it } from "@effect/vitest"; +import { ContextHandoffId } from "@t3tools/contracts"; -import { shouldPrepareLegacyImportHandoff } from "./Orchestrator.ts"; +import { appendContextHandoffId, shouldPrepareLegacyImportHandoff } from "./Orchestrator.ts"; it("reissues imported context until the provider thread is materialized", () => { assert.isTrue( @@ -32,3 +33,17 @@ it("reissues imported context until the provider thread is materialized", () => }), ); }); + +it("records a reissued legacy handoff on an existing provider thread", () => { + const existingHandoffId = ContextHandoffId.make("handoff:legacy-import:existing"); + const retryHandoffId = ContextHandoffId.make("handoff:legacy-import:retry"); + + assert.deepEqual(appendContextHandoffId([existingHandoffId], retryHandoffId), [ + existingHandoffId, + retryHandoffId, + ]); + assert.deepEqual(appendContextHandoffId([existingHandoffId], existingHandoffId), [ + existingHandoffId, + ]); + assert.deepEqual(appendContextHandoffId([existingHandoffId], null), [existingHandoffId]); +}); diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 16068de0b2..ea49891f41 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -459,6 +459,13 @@ export function shouldPrepareLegacyImportHandoff(input: { ); } +export function appendContextHandoffId( + handoffIds: OrchestrationV2ProviderThread["handoffIds"], + handoffId: OrchestrationV2ContextHandoff["id"] | null, +): OrchestrationV2ProviderThread["handoffIds"] { + return handoffId === null ? handoffIds : Array.from(new Set([...handoffIds, handoffId])); +} + function rootProviderThreadsForProvider( projection: OrchestrationV2ThreadProjection, providerInstanceId: ModelSelection["instanceId"], @@ -2626,6 +2633,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ...activeProviderThread, providerSessionId, lastRunOrdinal: ordinal, + handoffIds: appendContextHandoffId( + activeProviderThread.handoffIds, + legacyImportHandoff?.id ?? null, + ), updatedAt: now, }; const attemptId = idAllocator.derive.runAttempt({ runId, attemptOrdinal: 1 });