|
| 1 | +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | +import { assert, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + EnvironmentId, |
| 5 | + ProjectId, |
| 6 | + ProviderInstanceId, |
| 7 | + ThreadId, |
| 8 | + type OrchestrationV2ThreadProjection, |
| 9 | +} from "@t3tools/contracts"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | +import * as Layer from "effect/Layer"; |
| 12 | +import * as Ref from "effect/Ref"; |
| 13 | + |
| 14 | +import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 15 | +import * as ServerSettings from "../serverSettings.ts"; |
| 16 | +import * as CommandReceiptStore from "./CommandReceiptStore.ts"; |
| 17 | +import * as EventSink from "./EventSink.ts"; |
| 18 | +import * as EventStore from "./EventStore.ts"; |
| 19 | +import * as Orchestrator from "./Orchestrator.ts"; |
| 20 | +import * as ProjectionStore from "./ProjectionStore.ts"; |
| 21 | +import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; |
| 22 | +import * as ThreadImportService from "./ThreadImportService.ts"; |
| 23 | +import * as ThreadManagement from "./ThreadManagementService.ts"; |
| 24 | +import { type CapturedTurn, makeCapturingCodexAdapter } from "./testkit/CapturingCodexAdapter.ts"; |
| 25 | +import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts"; |
| 26 | +import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace"; |
| 27 | + |
| 28 | +// A thread moved here from another environment arrives with its conversation. |
| 29 | +// Its first run here gets that history as a context handoff, so the agent |
| 30 | +// picks up where it left off, and a retried import changes nothing. |
| 31 | + |
| 32 | +const modelSelection = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }; |
| 33 | +const source = { |
| 34 | + environmentId: EnvironmentId.make("environment:laptop"), |
| 35 | + threadId: ThreadId.make("thread:on-the-laptop"), |
| 36 | + handoffId: "handoff-1", |
| 37 | +}; |
| 38 | +const history = [ |
| 39 | + { |
| 40 | + role: "user" as const, |
| 41 | + text: "Find why the login test flakes.", |
| 42 | + createdAt: "2026-10-06T10:00:00.000Z", |
| 43 | + }, |
| 44 | + { |
| 45 | + role: "assistant" as const, |
| 46 | + text: "EARLIER_FINDING: the session cookie races the redirect.", |
| 47 | + createdAt: "2026-10-06T10:01:00.000Z", |
| 48 | + }, |
| 49 | +]; |
| 50 | +const continuation = "Carry on from where you stopped on the laptop."; |
| 51 | + |
| 52 | +const layerFor = (workspace: string, captured: Ref.Ref<ReadonlyArray<CapturedTurn>>) => { |
| 53 | + const layerDatabase = SqlitePersistence.layerMemory; |
| 54 | + const layerStores = Layer.mergeAll( |
| 55 | + layerDatabase, |
| 56 | + EventStore.layer.pipe(Layer.provideMerge(layerDatabase)), |
| 57 | + ProjectionStore.layer.pipe(Layer.provideMerge(layerDatabase)), |
| 58 | + ); |
| 59 | + const layerOrchestrator = ProviderReplayHarness.layerWithRegistry( |
| 60 | + { |
| 61 | + name: "thread-import", |
| 62 | + runtimePolicyOverride: { |
| 63 | + cwd: workspace, |
| 64 | + approvalPolicy: "never", |
| 65 | + sandboxPolicy: { type: "readOnly", access: { type: "fullAccess" }, networkAccess: false }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + ProviderAdapterRegistry.layerSingle( |
| 69 | + makeCapturingCodexAdapter(captured, { response: "Picked it up.", modelSelection }), |
| 70 | + ), |
| 71 | + { databaseLayer: layerDatabase }, |
| 72 | + ); |
| 73 | + const layerThreads = ThreadManagement.layer.pipe( |
| 74 | + Layer.provide(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))), |
| 75 | + Layer.provide(ServerSettings.layerTest()), |
| 76 | + Layer.provideMerge(layerOrchestrator), |
| 77 | + ); |
| 78 | + return ThreadImportService.layer.pipe( |
| 79 | + Layer.provideMerge(layerThreads), |
| 80 | + Layer.provideMerge(EventSink.layer.pipe(Layer.provide(layerStores))), |
| 81 | + Layer.provideMerge(layerStores), |
| 82 | + Layer.provide(NodeCrypto.layer), |
| 83 | + ); |
| 84 | +}; |
| 85 | + |
| 86 | +const waitForIdle = (threadId: ThreadId) => |
| 87 | + Effect.gen(function* () { |
| 88 | + const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 89 | + for (let attempt = 0; attempt < 1_000; attempt += 1) { |
| 90 | + const projection: OrchestrationV2ThreadProjection = |
| 91 | + yield* orchestrator.getThreadProjection(threadId); |
| 92 | + if ( |
| 93 | + projection.runs.length > 0 && |
| 94 | + projection.runs.every( |
| 95 | + (run) => !["queued", "starting", "running", "waiting"].includes(run.status), |
| 96 | + ) |
| 97 | + ) { |
| 98 | + return projection; |
| 99 | + } |
| 100 | + yield* Effect.sleep("5 millis"); |
| 101 | + } |
| 102 | + return yield* Effect.die(new Error("The imported thread never went idle.")); |
| 103 | + }); |
| 104 | + |
| 105 | +it.live( |
| 106 | + "an imported thread's first run continues with its history, and re-importing is a no-op", |
| 107 | + () => |
| 108 | + Effect.scoped( |
| 109 | + Effect.gen(function* () { |
| 110 | + const workspace = yield* checkpointWorkspace("thread-import"); |
| 111 | + const captured = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]); |
| 112 | + yield* Effect.gen(function* () { |
| 113 | + const imports = yield* ThreadImportService.ThreadImportService; |
| 114 | + const request = { |
| 115 | + source, |
| 116 | + projectId: ProjectId.make("project:box"), |
| 117 | + title: "Flaky login test", |
| 118 | + modelSelection, |
| 119 | + runtimeMode: "full-access" as const, |
| 120 | + interactionMode: "default" as const, |
| 121 | + linkOrigin: { sessionId: "session-laptop", label: "T3 Code · Laptop" }, |
| 122 | + messages: history, |
| 123 | + continuationPrompt: continuation, |
| 124 | + }; |
| 125 | + const first = yield* imports.importThread(request); |
| 126 | + assert.isTrue(first.created); |
| 127 | + const projection = yield* waitForIdle(first.threadId); |
| 128 | + |
| 129 | + // The history is there, before the first run, and marked as imported. |
| 130 | + assert.equal(projection.thread.historyOrigin, "v1_import"); |
| 131 | + assert.deepEqual(projection.thread.linkOrigin, request.linkOrigin); |
| 132 | + const runless = projection.messages.filter((message) => message.runId === null); |
| 133 | + assert.deepEqual( |
| 134 | + runless.map((message) => [message.role, message.text]), |
| 135 | + history.map((message) => [message.role, message.text]), |
| 136 | + ); |
| 137 | + // The first run got the history as context, then the continuation. |
| 138 | + assert.equal(projection.contextHandoffs.length, 1); |
| 139 | + const turns = yield* Ref.get(captured); |
| 140 | + assert.equal(turns.length, 1); |
| 141 | + assert.include(turns[0]!.text, "Context handoff (manual_context):"); |
| 142 | + assert.include(turns[0]!.text, "EARLIER_FINDING"); |
| 143 | + assert.include(turns[0]!.text, `User message:\n${continuation}`); |
| 144 | + |
| 145 | + // Retrying the same import creates nothing and runs nothing new. |
| 146 | + const retried = yield* imports.importThread(request); |
| 147 | + assert.deepEqual(retried, { ...first, created: false }); |
| 148 | + const after = yield* Orchestrator.OrchestratorV2.pipe( |
| 149 | + Effect.flatMap((orchestrator) => orchestrator.getThreadProjection(first.threadId)), |
| 150 | + ); |
| 151 | + assert.equal(after.messages.length, projection.messages.length); |
| 152 | + assert.equal(after.runs.length, 1); |
| 153 | + assert.equal((yield* Ref.get(captured)).length, 1); |
| 154 | + |
| 155 | + // A different handoff of the same source thread is a new thread. |
| 156 | + const again = yield* imports.importThread({ |
| 157 | + ...request, |
| 158 | + source: { ...source, handoffId: "handoff-2" }, |
| 159 | + continuationPrompt: undefined, |
| 160 | + }); |
| 161 | + assert.notEqual(again.threadId, first.threadId); |
| 162 | + assert.isNull(again.runId); |
| 163 | + }).pipe(Effect.provide(layerFor(workspace, captured))); |
| 164 | + }), |
| 165 | + ), |
| 166 | +); |
0 commit comments