|
5 | 5 | ThreadId, |
6 | 6 | type OrchestrationV2DomainEvent, |
7 | 7 | } from "@t3tools/contracts"; |
| 8 | +import * as DateTime from "effect/DateTime"; |
8 | 9 | import * as Effect from "effect/Effect"; |
9 | 10 | import * as Layer from "effect/Layer"; |
10 | 11 | import * as Option from "effect/Option"; |
@@ -146,3 +147,134 @@ it.effect("imports messages once and preserves the provider native resume bindin |
146 | 147 | expect(recorded).toHaveLength(2); |
147 | 148 | }).pipe(Effect.provide(testLayer)); |
148 | 149 | }); |
| 150 | + |
| 151 | +it.effect("stamps the import origin on provider threads imported before it existed", () => { |
| 152 | + const originProjectId = ProjectId.make("project-import-origin"); |
| 153 | + const originInstanceId = ProviderInstanceId.make("codex"); |
| 154 | + const originSessionId = "native-codex-thread-origin"; |
| 155 | + const originThreadId = ThreadId.make(`import:${originInstanceId}:${originSessionId}`); |
| 156 | + const writes: Array<ReadonlyArray<OrchestrationV2DomainEvent>> = []; |
| 157 | + let imported = false; |
| 158 | + let stamped = false; |
| 159 | + const legacyProviderThread = { |
| 160 | + id: `provider-thread:${originSessionId}`, |
| 161 | + driver: "codex", |
| 162 | + providerInstanceId: originInstanceId, |
| 163 | + providerSessionId: "provider-session-1", |
| 164 | + appThreadId: originThreadId, |
| 165 | + ownerNodeId: null, |
| 166 | + nativeThreadRef: { driver: "codex", nativeId: originSessionId, strength: "strong" }, |
| 167 | + nativeConversationHeadRef: null, |
| 168 | + status: "idle", |
| 169 | + firstRunOrdinal: null, |
| 170 | + lastRunOrdinal: null, |
| 171 | + handoffIds: [], |
| 172 | + forkedFrom: null, |
| 173 | + createdAt: DateTime.makeUnsafe("2026-09-01T10:00:00.000Z"), |
| 174 | + updatedAt: DateTime.makeUnsafe("2026-09-01T10:01:00.000Z"), |
| 175 | + }; |
| 176 | + const scanner = AgentSessionScanner.AgentSessionScanner.of({ |
| 177 | + scan: Effect.die("unused"), |
| 178 | + recentThreads: () => |
| 179 | + Stream.succeed({ |
| 180 | + _tag: "Importable", |
| 181 | + source: { |
| 182 | + provider: "codex", |
| 183 | + providerInstanceId: originInstanceId, |
| 184 | + providerSessionId: originSessionId, |
| 185 | + filePath: "/tmp/native-codex-thread-origin.jsonl", |
| 186 | + size: 100, |
| 187 | + mtimeMs: 2, |
| 188 | + device: 3, |
| 189 | + inode: 4, |
| 190 | + birthtimeMs: 1, |
| 191 | + }, |
| 192 | + thread: { |
| 193 | + source: "codex", |
| 194 | + providerInstanceId: originInstanceId, |
| 195 | + providerSessionId: originSessionId, |
| 196 | + title: "Imported thread", |
| 197 | + model: "gpt-5.4", |
| 198 | + createdAt: "2026-09-01T10:00:00.000Z", |
| 199 | + updatedAt: "2026-09-01T10:01:00.000Z", |
| 200 | + messages: [{ role: "user", text: "Fix it", createdAt: "2026-09-01T10:00:00.000Z" }], |
| 201 | + }, |
| 202 | + }), |
| 203 | + }); |
| 204 | + const testLayer = AgentSessionImporter.layer.pipe( |
| 205 | + Layer.provide( |
| 206 | + Layer.mergeAll( |
| 207 | + Layer.succeed(AgentSessionScanner.AgentSessionScanner, scanner), |
| 208 | + Layer.mock(ProjectService.ProjectService)({ |
| 209 | + getById: () => |
| 210 | + Effect.succeed( |
| 211 | + Option.some({ id: originProjectId, workspaceRoot: "/workspace/project" } as never), |
| 212 | + ), |
| 213 | + }), |
| 214 | + Layer.mock(Orchestrator.OrchestratorV2)({ |
| 215 | + // A thread imported before the origin marker existed: its provider |
| 216 | + // thread row carries no nativeMetadata until a later import stamps it. |
| 217 | + getThreadRecords: () => |
| 218 | + imported |
| 219 | + ? Effect.succeed({ |
| 220 | + thread: { |
| 221 | + id: originThreadId, |
| 222 | + projectId: originProjectId, |
| 223 | + historyOrigin: "v1_import", |
| 224 | + }, |
| 225 | + providerThreads: [ |
| 226 | + stamped |
| 227 | + ? { |
| 228 | + ...legacyProviderThread, |
| 229 | + nativeMetadata: { nativeThreadOrigin: "imported" }, |
| 230 | + } |
| 231 | + : legacyProviderThread, |
| 232 | + ], |
| 233 | + } as never) |
| 234 | + : Effect.fail( |
| 235 | + new Orchestrator.OrchestratorProjectionError({ threadId: originThreadId }), |
| 236 | + ), |
| 237 | + }), |
| 238 | + Layer.mock(EventSink.EventSinkV2)({ |
| 239 | + write: (input) => |
| 240 | + Effect.sync(() => { |
| 241 | + writes.push(input.events); |
| 242 | + imported = true; |
| 243 | + return []; |
| 244 | + }), |
| 245 | + }), |
| 246 | + Layer.mock(ProviderSessionRuntime.ProviderSessionRuntimeRepository)({ |
| 247 | + list: () => Effect.succeed([]), |
| 248 | + upsert: () => Effect.void, |
| 249 | + recordImportedTranscript: () => Effect.void, |
| 250 | + }), |
| 251 | + IdAllocator.layer, |
| 252 | + ), |
| 253 | + ), |
| 254 | + ); |
| 255 | + |
| 256 | + return Effect.gen(function* () { |
| 257 | + const importer = yield* AgentSessionImporter.AgentSessionImporter; |
| 258 | + yield* importer.importRecentAgentThreads({ projectId: originProjectId }); |
| 259 | + expect(writes).toHaveLength(1); |
| 260 | + |
| 261 | + // The second pass finds the thread already imported and stamps its row. |
| 262 | + yield* importer.importRecentAgentThreads({ projectId: originProjectId }); |
| 263 | + expect(writes).toHaveLength(2); |
| 264 | + expect(writes[1]?.map((event) => event.type)).toEqual(["provider-thread.updated"]); |
| 265 | + const stampedEvent = writes[1]?.[0]; |
| 266 | + expect(stampedEvent?.id).toBe( |
| 267 | + `agent-session-import:v2:provider-thread:${legacyProviderThread.id}:imported-origin`, |
| 268 | + ); |
| 269 | + expect(stampedEvent?.payload).toMatchObject({ |
| 270 | + id: legacyProviderThread.id, |
| 271 | + nativeThreadRef: legacyProviderThread.nativeThreadRef, |
| 272 | + nativeMetadata: { nativeThreadOrigin: "imported" }, |
| 273 | + }); |
| 274 | + |
| 275 | + // A row that already carries the origin is left alone. |
| 276 | + stamped = true; |
| 277 | + yield* importer.importRecentAgentThreads({ projectId: originProjectId }); |
| 278 | + expect(writes).toHaveLength(2); |
| 279 | + }).pipe(Effect.provide(testLayer)); |
| 280 | +}); |
0 commit comments