Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AgentSessionImporter.test.ts

790 lines30.3 KB

1import { expect, it } from "@effect/vitest";
2import {
3 type AgentSessionSource,
4 type ServerSettingsError,
5 EventId,
6 ProjectId,
7 ProviderDriverKind,
8 ProviderInstanceId,
9 MessageId,
10 RunId,
11 ThreadId,
12 type ApplicationStoredEvent,
13 type OrchestrationV2AppThread,
14 type OrchestrationV2DomainEvent,
15 type OrchestrationV2ProviderThread,
16} from "@t3tools/contracts";
17import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
18import * as KeyedLock from "@t3tools/shared/KeyedLock";
19import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator";
20import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
21import * as Context from "effect/Context";
22import * as DateTime from "effect/DateTime";
23import * as Deferred from "effect/Deferred";
24import * as Effect from "effect/Effect";
25import * as Fiber from "effect/Fiber";
26import * as Layer from "effect/Layer";
27import * as Option from "effect/Option";
28import * as Stream from "effect/Stream";
29import * as SqlClient from "effect/sql/SqlClient";
30import * as TestClock from "effect/testing/TestClock";
31​
32import * as EventSink from "../orchestration-v2/EventSink.ts";
33import * as EventStore from "../orchestration-v2/EventStore.ts";
34import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
35import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
36import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts";
37import * as ThreadCommandExecutor from "../orchestration-v2/ThreadCommandExecutor.ts";
38import {
39 CLAUDE_MODEL_SELECTION,
40 CODEX_MODEL_SELECTION,
41} from "../orchestration-v2/testkit/fixtures/shared.ts";
42import * as ProviderSessionRuntime from "../persistence/ProviderSessionRuntime.ts";
43import * as SqlitePersistence from "../persistence/Sqlite.ts";
44import * as ServerSettings from "../serverSettings.ts";
45import * as AgentSessionImporter from "./AgentSessionImporter.ts";
46import * as AgentSessionScanner from "./AgentSessionScanner.ts";
47import * as ProjectService from "./ProjectService.ts";
48​
49const projectId = ProjectId.make("agent-session-import-project");
50const timestamp = "2026-09-01T10:00:00.000Z";
51const now = DateTime.makeUnsafe(timestamp);
52const stores = Layer.mergeAll(EventStore.layer, ProjectionStore.layer).pipe(
53 Layer.provideMerge(SqlitePersistence.layerMemory),
54);
55const persistence = Layer.mergeAll(
56 stores,
57 EventSink.layer.pipe(Layer.provide(stores)),
58 ProviderSessionRuntime.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)),
59 IdAllocator.layer,
60);
61​
62function candidate(
63 modelSelection: typeof CODEX_MODEL_SELECTION,
64 providerSessionId: string,
65 provider: AgentSessionSource = "codex",
66) {
67 const providerInstanceId = modelSelection.instanceId;
68 return {
69 _tag: "Importable" as const,
70 source: {
71 provider,
72 providerInstanceId,
73 providerSessionId,
74 filePath: `/tmp/${providerInstanceId}/${providerSessionId}.jsonl`,
75 size: 100,
76 mtimeMs: 2,
77 device: 3,
78 inode: 4,
79 birthtimeMs: 1,
80 },
81 thread: {
82 source: provider,
83 providerInstanceId,
84 providerSessionId,
85 title: "Imported thread",
86 model: modelSelection.model,
87 createdAt: timestamp,
88 updatedAt: timestamp,
89 omittedMessageCount: 0,
90 messages: [
91 { role: "user" as const, text: "Fix it", createdAt: timestamp },
92 { role: "assistant" as const, text: "Fixed", createdAt: timestamp },
93 ],
94 },
95 } satisfies AgentSessionScanner.AgentSessionRecentThread;
97​
98const PI_MODEL_SELECTION = { instanceId: ProviderInstanceId.make("pi"), model: "default" };
99​
100// Pi threads are owned by their session file path, not the session UUID.
101const nativeId = ({ source }: ReturnType<typeof candidate>) =>
102 source.provider === "pi" ? source.filePath : source.providerSessionId;
103​
104function importId(source: ReturnType<typeof candidate>) {
105 return ThreadId.make(
106 `import:${source.source.providerInstanceId}:${source.source.providerSessionId}`,
107 );
109​
110const seedOwner = Effect.fn(function* (
111 source: ReturnType<typeof candidate>,
112 lifecycle: "active" | "archived" | "deleted" = "active",
113) {
114 const sink = yield* EventSink.EventSinkV2;
115 const ids = yield* IdAllocator.IdAllocatorV2;
116 const { providerInstanceId, providerSessionId, provider } = source.source;
117 const nativeThreadId = nativeId(source);
118 const threadId = ThreadId.make(`owner:${providerInstanceId}:${providerSessionId}`);
119 const driver = ProviderDriverKind.make(provider);
120 const providerThread: OrchestrationV2ProviderThread = {
121 id: ids.derive.providerThread({ driver, nativeThreadId }),
122 driver,
123 providerInstanceId,
124 providerSessionId: null,
125 appThreadId: threadId,
126 ownerNodeId: null,
127 nativeThreadRef: { driver, nativeId: nativeThreadId, strength: "strong" },
128 nativeConversationHeadRef: null,
129 status: "idle",
130 firstRunOrdinal: 1,
131 lastRunOrdinal: 1,
132 handoffIds: [],
133 forkedFrom: null,
134 createdAt: now,
135 updatedAt: now,
136 };
137 const thread: OrchestrationV2AppThread = {
138 createdBy: "user",
139 creationSource: "web",
140 id: threadId,
141 projectId,
142 title: "Native thread",
143 providerInstanceId,
144 modelSelection: { instanceId: providerInstanceId, model: source.thread.model },
145 runtimeMode: "full-access",
146 interactionMode: "default",
147 branch: null,
148 worktreePath: null,
149 activeProviderThreadId: providerThread.id,
150 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
151 forkedFrom: null,
152 createdAt: now,
153 updatedAt: now,
154 archivedAt: lifecycle === "archived" ? now : null,
155 deletedAt: lifecycle === "deleted" ? now : null,
156 settledOverride: null,
157 settledAt: null,
158 lastVisitedAt: null,
159 };
160 yield* sink.write({
161 events: [
162 {
163 id: EventId.make(`${threadId}:created`),
164 type: "thread.created",
165 threadId,
166 occurredAt: now,
167 payload: thread,
168 },
169 {
170 id: EventId.make(`${threadId}:provider`),
171 type: "provider-thread.updated",
172 threadId,
173 occurredAt: now,
174 payload: providerThread,
175 },
176 ],
177 });
178 return threadId;
179});
180​
181const makeImporter = Effect.fn(function* (
182 candidates: ReadonlyArray<ReturnType<typeof candidate>>,
183 beforeTransaction: Effect.Effect<void> = Effect.void,
184 getAdapter?: ProviderAdapterRegistry.ProviderAdapterRegistryV2Shape["get"],
185 threadCommands?: KeyedLock.KeyedLock<ThreadId>,
186 settings: Layer.Layer<
187 ServerSettings.ServerSettingsService,
188 ServerSettingsError
189 > = ServerSettings.layerTest(),
190) {
191 const sql = yield* SqlClient.SqlClient;
192 const events = yield* EventStore.EventStoreV2;
193 const projections = yield* ProjectionStore.ProjectionStoreV2;
194 const published: Array<ApplicationStoredEvent> = [];
195 const withTransaction: typeof sql.withTransaction = (effect) =>
196 beforeTransaction.pipe(Effect.andThen(sql.withTransaction(effect)));
197 const importSql = new Proxy(sql, {
198 get: (target, key, receiver) =>
199 key === "withTransaction" ? withTransaction : Reflect.get(target, key, receiver),
200 });
201 const sinkLayer = Layer.fresh(EventSink.layer).pipe(
202 Layer.provide(
203 Layer.mergeAll(
204 Layer.succeed(SqlClient.SqlClient, importSql),
205 Layer.succeed(ProjectionStore.ProjectionStoreV2, projections),
206 Layer.succeed(EventStore.EventStoreV2, {
207 ...events,
208 publishCommitted: (batch) =>
209 events.publishCommitted(batch).pipe(
210 Effect.tap(() =>
211 Effect.sync(() => {
212 published.push(...batch);
213 }),
214 ),
215 ),
216 }),
217 ),
218 ),
219 );
220 const context = yield* Layer.build(
221 AgentSessionImporter.layer.pipe(
222 Layer.provide(
223 Layer.mergeAll(
224 sinkLayer,
225 settings,
226 Layer.succeed(IdAllocator.IdAllocatorV2, yield* IdAllocator.IdAllocatorV2),
227 Layer.succeed(
228 ProviderSessionRuntime.ProviderSessionRuntimeRepository,
229 yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository,
230 ),
231 Layer.mock(AgentSessionScanner.AgentSessionScanner)({
232 recentThreads: () => Stream.fromIterable(candidates),
233 }),
234 Layer.mock(ProjectService.ProjectService)({
235 getById: () =>
236 Effect.succeedSome({
237 id: projectId,
238 title: "Project",
239 workspaceRoot: "/workspace/project",
240 defaultModelSelection: null,
241 scripts: [],
242 createdAt: timestamp,
243 updatedAt: timestamp,
244 deletedAt: null,
245 }),
246 }),
247 Layer.mock(Orchestrator.OrchestratorV2)({
248 getThreadRecords: (threadId, fields) =>
249 projections
250 .getThreadRecords(threadId, fields)
251 .pipe(
252 Effect.mapError(
253 (cause) => new Orchestrator.OrchestratorProjectionError({ threadId, cause }),
254 ),
255 ),
256 }),
257 Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)(
258 getAdapter === undefined ? {} : { get: getAdapter },
259 ),
260 Layer.succeed(
261 ThreadCommandExecutor.ThreadCommandExecutor,
262 threadCommands ?? (yield* KeyedLock.make<ThreadId>()),
263 ),
264 ),
265 ),
266 ),
267 );
268 const importer = Context.get(context, AgentSessionImporter.AgentSessionImporter);
269 return {
270 run: importer.importRecentAgentThreads({ projectId }),
271 importNativeHistory: importer.importNativeHistory,
272 published,
273 };
274});
275​
276it.effect(
277 "ignores owned sessions, including archived and deleted threads, but imports other instances and unowned sessions",
278 () =>
279 Effect.gen(function* () {
280 const projections = yield* ProjectionStore.ProjectionStoreV2;
281 const events = yield* EventStore.EventStoreV2;
282 const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
283 const active = candidate(CODEX_MODEL_SELECTION, "active");
284 const archived = candidate(
285 CLAUDE_MODEL_SELECTION,
286 "020e7f00-dce6-406b-8ec6-ed90498945d6",
287 "claudeAgent",
288 );
289 const deleted = candidate(CODEX_MODEL_SELECTION, "deleted");
290 const ownedPi = candidate(PI_MODEL_SELECTION, "owned-pi", "pi");
291 const owners = [
292 yield* seedOwner(active),
293 yield* seedOwner(archived, "archived"),
294 yield* seedOwner(deleted, "deleted"),
295 yield* seedOwner(ownedPi),
296 ];
297 const before = yield* Effect.forEach(owners, (id) =>
298 projections.getThreadRecords(id, ["providerThreads"]),
299 );
300 const otherInstance = candidate(
301 { ...CLAUDE_MODEL_SELECTION, instanceId: ProviderInstanceId.make("claude-other") },
302 archived.source.providerSessionId,
303 "claudeAgent",
304 );
305 const unowned = candidate(CODEX_MODEL_SELECTION, "unowned");
306 unowned.thread.omittedMessageCount = 1;
307 const unownedPi = candidate(PI_MODEL_SELECTION, "unowned-pi", "pi");
308 const importer = yield* makeImporter([
309 active,
310 archived,
311 deleted,
312 ownedPi,
313 otherInstance,
314 unowned,
315 unownedPi,
316 ]);
317​
318 expect(yield* importer.run).toEqual({ importedCount: 3, skippedCount: 0 });
319 for (const source of [active, archived, deleted, ownedPi]) {
320 const threadId = importId(source);
321 expect(yield* Effect.option(projections.getThread(threadId))).toEqual(Option.none());
322 expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual([]);
323 expect(yield* runtimes.getByThreadId({ threadId })).toEqual(Option.none());
324 }
325 expect(
326 yield* Effect.forEach(owners, (id) =>
327 projections.getThreadRecords(id, ["providerThreads"]),
328 ),
329 ).toEqual(before);
330 expect(
331 new Set(
332 importer.published.flatMap((stored) =>
333 "event" in stored ? [stored.event.threadId] : [],
334 ),
335 ),
336 ).toEqual(new Set([importId(otherInstance), importId(unowned), importId(unownedPi)]));
337 for (const source of [otherInstance, unowned, unownedPi]) {
338 const threadId = importId(source);
339 const records = yield* projections.getThreadRecords(threadId, [
340 "messages",
341 "turnItems",
342 "providerThreads",
343 ]);
344 expect(records.messages.map((message) => message.text)).toEqual(["Fix it", "Fixed"]);
345 const notices = records.turnItems.filter((item) => item.type === "system_notice");
346 if (source === unowned) {
347 expect(records.turnItems[0]).toBe(notices[0]);
348 expect(notices).toMatchObject([
349 {
350 ordinal: 1,
351 runId: null,
352 message: expect.stringContaining("1 earlier message was not imported"),
353 },
354 ]);
355 } else {
356 expect(notices).toEqual([]);
357 }
358 expect(records.thread.historyOrigin).toBe("v1_import");
359 expect(records.thread.activeProviderThreadId).toBe(records.providerThreads[0]?.id);
360 expect(records.providerThreads[0]?.nativeThreadRef?.nativeId).toBe(nativeId(source));
361 expect(Option.getOrThrow(yield* runtimes.getByThreadId({ threadId }))).toMatchObject({
362 providerInstanceId: source.source.providerInstanceId,
363 resumeCursor:
364 source.source.provider === "claudeAgent"
365 ? { threadId, resume: source.source.providerSessionId }
366 : { threadId: nativeId(source) },
367 runtimePayload: { importedTranscripts: [source.source] },
368 });
369 }
370 }).pipe(Effect.scoped, Effect.provide(persistence)),
371);
372​
373it.effect("rechecks ownership after the import reaches the commit transaction", () =>
374 Effect.gen(function* () {
375 const enteringTransaction = yield* Deferred.make<void>();
376 const continueTransaction = yield* Deferred.make<void>();
377 const source = candidate(CODEX_MODEL_SELECTION, "claimed-during-import");
378 const importer = yield* makeImporter(
379 [source],
380 Deferred.succeed(enteringTransaction, undefined).pipe(
381 Effect.andThen(Deferred.await(continueTransaction)),
382 ),
383 );
384 const pending = yield* Effect.forkScoped(importer.run);
385 yield* Deferred.await(enteringTransaction);
386 // Commit ownership after all pre-transaction reads, before the import acquires SQLite.
387 const ownerId = yield* seedOwner(source);
388 yield* Deferred.succeed(continueTransaction, undefined);
389 expect(yield* Fiber.join(pending)).toEqual({ importedCount: 0, skippedCount: 0 });
390 expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0 });
391 const projections = yield* ProjectionStore.ProjectionStoreV2;
392 const events = yield* EventStore.EventStoreV2;
393 const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
394 expect(yield* Effect.option(projections.getThread(importId(source)))).toEqual(Option.none());
395 expect(yield* events.read({ threadId: importId(source) }).pipe(Stream.runCollect)).toEqual([]);
396 expect(yield* runtimes.list()).toEqual([]);
397 expect(importer.published).toEqual([]);
398 expect(
399 (yield* projections.getThreadRecords(ownerId, ["providerThreads"])).providerThreads[0]
400 ?.appThreadId,
401 ).toBe(ownerId);
402 }).pipe(Effect.scoped, Effect.provide(persistence)),
403);
404​
405it.effect(
406 "retries failed commits and runtime persistence without replaying committed history",
407 () =>
408 Effect.gen(function* () {
409 const sql = yield* SqlClient.SqlClient;
410 const events = yield* EventStore.EventStoreV2;
411 const projections = yield* ProjectionStore.ProjectionStoreV2;
412 const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
413 const source = candidate(CODEX_MODEL_SELECTION, "retry");
414 const threadId = importId(source);
415 const importer = yield* makeImporter([source]);
416 yield* sql`CREATE TEMP TRIGGER fail_import BEFORE INSERT ON orchestration_events WHEN NEW.event_type = 'message.updated' BEGIN SELECT RAISE(ABORT, 'test publication failure'); END`;
417 expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1 });
418 expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual([]);
419 expect(yield* Effect.option(projections.getThread(threadId))).toEqual(Option.none());
420 expect(yield* runtimes.list()).toEqual([]);
421 expect(importer.published).toEqual([]);
422 yield* sql`DROP TRIGGER fail_import`;
423​
424 yield* sql`CREATE TEMP TRIGGER fail_runtime BEFORE INSERT ON provider_session_runtime BEGIN SELECT RAISE(ABORT, 'test runtime failure'); END`;
425 expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 1 });
426 const committed = yield* events.read({ threadId }).pipe(Stream.runCollect);
427 expect(committed).toHaveLength(6);
428 expect(importer.published).toHaveLength(6);
429 expect(yield* runtimes.list()).toEqual([]);
430 yield* sql`DROP TRIGGER fail_runtime`;
431​
432 expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
433 expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
434 expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual(committed);
435 expect(importer.published).toHaveLength(6);
436 expect(
437 (yield* projections.getThreadRecords(threadId, ["messages"])).messages.map(
438 (message) => message.text,
439 ),
440 ).toEqual(["Fix it", "Fixed"]);
441 expect(Option.getOrThrow(yield* runtimes.getByThreadId({ threadId }))).toMatchObject({
442 providerInstanceId: source.source.providerInstanceId,
443 resumeCursor: { threadId: source.source.providerSessionId },
444 runtimePayload: { importedTranscripts: [source.source] },
445 });
446 }).pipe(Effect.scoped, Effect.provide(persistence)),
447);
448​
449it.effect(
450 "copies native session history into the imported thread once, retrying a failed read",
451 () =>
452 Effect.gen(function* () {
453 const projections = yield* ProjectionStore.ProjectionStoreV2;
454 const source = candidate(CODEX_MODEL_SELECTION, "native-history");
455 const threadId = yield* seedOwner(source);
456 const resolved: Array<ProviderInstanceId> = [];
457 const opened: Array<ProviderAdapter.ProviderAdapterV2OpenSessionInput> = [];
458 let closed = false;
459 let failRead = true;
460 const replayed = (role: "user" | "assistant", text: string) => ({
461 createdBy: role === "user" ? ("user" as const) : ("agent" as const),
462 creationSource: "provider" as const,
463 id: MessageId.make(`replayed:${text}`),
464 threadId,
465 runId: null,
466 nodeId: null,
467 role,
468 text,
469 attachments: [],
470 streaming: false,
471 createdAt: now,
472 updatedAt: now,
473 });
474 const adapter = {
475 openSession: (input: ProviderAdapter.ProviderAdapterV2OpenSessionInput) =>
476 Effect.acquireRelease(
477 Effect.sync(() => {
478 opened.push(input);
479 return {
480 readThreadSnapshot: ({ providerThread }) =>
481 failRead
482 ? Effect.fail(
483 new ProviderAdapter.ProviderAdapterReadThreadSnapshotError({
484 driver: providerThread.driver,
485 providerThreadId: providerThread.id,
486 }),
487 )
488 : Effect.succeed({
489 providerThread,
490 providerTurns: [],
491 runtimeRequests: [],
492 reasoningMessageIds: [MessageId.make("replayed:Check the build log")],
493 messages: [
494 replayed("user", "Fix the build"),
495 replayed("assistant", "Check the build log"),
496 replayed("assistant", "Fixed it"),
497 replayed("assistant", " "),
498 ],
499 }),
500 } as Pick<ProviderAdapter.ProviderAdapterV2SessionRuntime, "readThreadSnapshot">;
501 }),
502 () =>
503 Effect.sync(() => {
504 closed = true;
505 }),
506 ),
507 } as unknown as ProviderAdapter.ProviderAdapterV2["Service"];
508 const importer = yield* makeImporter([], Effect.void, (instanceId) =>
509 Effect.sync(() => {
510 resolved.push(instanceId);
511 return adapter;
512 }),
513 );
514​
515 const importHistory = importer.importNativeHistory({
516 threadId,
517 workspaceRoot: "/workspace/project",
518 });
519​
520 expect((yield* Effect.flip(importHistory))._tag).toBe(
521 "ProviderAdapterReadThreadSnapshotError",
522 );
523 expect(closed).toBe(true);
524 expect(
525 (yield* projections.getThreadRecords(threadId, [])).thread.historyOrigin,
526 ).toBeUndefined();
527​
528 failRead = false;
529 expect(yield* importHistory).toBe(3);
530 expect(resolved).toEqual([
531 CODEX_MODEL_SELECTION.instanceId,
532 CODEX_MODEL_SELECTION.instanceId,
533 ]);
534 expect(opened[1]).toMatchObject({
535 threadId,
536 initialNativeThreadId: source.source.providerSessionId,
537 runtimePolicy: { cwd: "/workspace/project" },
538 });
539 // Forks keep run-less history only for this origin.
540 expect((yield* projections.getThreadRecords(threadId, [])).thread.historyOrigin).toBe(
541 "v1_import",
542 );
543 expect(yield* importHistory).toBe(0);
544 expect(opened).toHaveLength(2);
545 const page = yield* projections.getTimelinePage(threadId, { view: "activity", limit: 10 });
546 expect(
547 page.items.map(({ item }) => [
548 item.type,
549 item.runId,
550 "text" in item ? item.text : undefined,
551 ]),
552 ).toEqual([
553 ["user_message", null, "Fix the build"],
554 ["reasoning", null, "Check the build log"],
555 ["assistant_message", null, "Fixed it"],
556 ]);
557 }).pipe(Effect.scoped, Effect.provide(persistence)),
558);
559​
560it.effect("releases the ACP import lock when a native session never answers", () =>
561 Effect.gen(function* () {
562 const projections = yield* ProjectionStore.ProjectionStoreV2;
563 const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
564 const threadId = yield* seedOwner(candidate(CODEX_MODEL_SELECTION, "native-history-hang"));
565 const opened = yield* Deferred.make<void>();
566 let closed = false;
567 // An agent that only resumes sessions and never answers its first session/resume.
568 const adapter = {
569 openSession: () =>
570 Effect.suspend(() => {
571 if (!closed) {
572 return Effect.addFinalizer(() =>
573 Effect.sync(() => {
574 closed = true;
575 }),
576 ).pipe(
577 Effect.andThen(Deferred.succeed(opened, undefined)),
578 Effect.andThen(Effect.never),
579 );
580 }
581 return Effect.succeed({
582 readThreadSnapshot: ({ providerThread }) =>
583 Effect.succeed({
584 providerThread,
585 providerTurns: [],
586 runtimeRequests: [],
587 messages: [
588 {
589 createdBy: "user",
590 creationSource: "provider",
591 id: MessageId.make("replayed:hang"),
592 threadId,
593 runId: null,
594 nodeId: null,
595 role: "user",
596 text: "Fix the build",
597 attachments: [],
598 streaming: false,
599 createdAt: now,
600 updatedAt: now,
601 },
602 ],
603 }),
604 } as Pick<ProviderAdapter.ProviderAdapterV2SessionRuntime, "readThreadSnapshot">);
605 }),
606 } as unknown as ProviderAdapter.ProviderAdapterV2["Service"];
607 const importer = yield* makeImporter([], Effect.void, () => Effect.succeed(adapter));
608 const importHistory = coordinator.withSessionMutation(
609 importer.importNativeHistory({ threadId, workspaceRoot: "/workspace/project" }),
610 );
611​
612 const importing = yield* importHistory.pipe(Effect.flip, Effect.forkChild);
613 yield* Deferred.await(opened);
614 // Any other ACP import or delete waits for the same lock.
615 const next = yield* coordinator
616 .withSessionMutation(Effect.succeed("next"))
617 .pipe(Effect.forkChild);
618 yield* TestClock.adjust("2 minutes");
619​
620 expect((yield* Fiber.join(importing))._tag).toBe("TimeoutError");
621 expect(closed).toBe(true);
622 expect(yield* Fiber.join(next)).toBe("next");
623 expect(
624 (yield* projections.getThreadRecords(threadId, [])).thread.historyOrigin,
625 ).toBeUndefined();
626 expect(yield* importHistory).toBe(1);
627 expect((yield* projections.getThreadRecords(threadId, [])).thread.historyOrigin).toBe(
628 "v1_import",
629 );
630 }).pipe(
631 Effect.scoped,
632 Effect.provide(Layer.merge(persistence, AcpRegistryRuntimeCoordinator.layer)),
633 ),
634);
635​
636/**
637 * Runs a thread command that read the thread before the history load ended and
638 * writes once the importer is about to commit, as an unserialized write would.
639 */
640const importDuringCommand = Effect.fn(function* (
641 name: string,
642 command: (thread: OrchestrationV2AppThread) => ReadonlyArray<OrchestrationV2DomainEvent>,
643) {
644 const projections = yield* ProjectionStore.ProjectionStoreV2;
645 const sink = yield* EventSink.EventSinkV2;
646 const threadId = yield* seedOwner(candidate(CODEX_MODEL_SELECTION, name));
647 const lock = yield* KeyedLock.make<ThreadId>();
648 const loaded = yield* Deferred.make<void>();
649 const importerCommitting = yield* Deferred.make<void>();
650 const commandWritten = yield* Deferred.make<void>();
651 const committing = Deferred.succeed(importerCommitting, undefined);
652 const adapter = {
653 openSession: () =>
654 Effect.succeed({
655 readThreadSnapshot: ({ providerThread }) =>
656 Deferred.await(loaded).pipe(
657 Effect.as({
658 providerThread,
659 providerTurns: [],
660 runtimeRequests: [],
661 messages: [
662 {
663 createdBy: "user" as const,
664 creationSource: "provider" as const,
665 id: MessageId.make(`replayed:${name}`),
666 threadId,
667 runId: null,
668 nodeId: null,
669 role: "user" as const,
670 text: "Fix the build",
671 attachments: [],
672 streaming: false,
673 createdAt: now,
674 updatedAt: now,
675 },
676 ],
677 }),
678 ),
679 } as Pick<ProviderAdapter.ProviderAdapterV2SessionRuntime, "readThreadSnapshot">),
680 } as unknown as ProviderAdapter.ProviderAdapterV2["Service"];
681 const importer = yield* makeImporter(
682 [],
683 committing.pipe(Effect.andThen(Deferred.await(commandWritten))),
684 () => Effect.succeed(adapter),
685 {
686 ...lock,
687 withLock: (key, effect) => committing.pipe(Effect.andThen(lock.withLock(key, effect))),
688 },
689 );
690 const commanding = yield* Effect.forkScoped(
691 lock.withLock(
692 threadId,
693 Effect.gen(function* () {
694 const { thread } = yield* projections.getThreadRecords(threadId, []);
695 yield* Deferred.succeed(loaded, undefined);
696 yield* Deferred.await(importerCommitting);
697 yield* sink.write({ events: command(thread) });
698 yield* Deferred.succeed(commandWritten, undefined);
699 }),
700 ),
701 );
702 const imported = yield* importer.importNativeHistory({
703 threadId,
704 workspaceRoot: "/workspace/project",
705 });
706 yield* Fiber.join(commanding);
707 const records = yield* projections.getThreadRecords(threadId, ["runs"]);
708 const page = yield* projections.getTimelinePage(threadId, { view: "messages", limit: 10 });
709 return { imported, thread: records.thread, runs: records.runs, items: page.items };
710});
711​
712it.effect("keeps a thread edit made while the native history loads", () =>
713 Effect.gen(function* () {
714 const result = yield* importDuringCommand("renamed-during-load", (thread) => [
715 {
716 id: EventId.make(`${thread.id}:renamed`),
717 type: "thread.metadata-updated",
718 threadId: thread.id,
719 occurredAt: now,
720 payload: { ...thread, title: "Renamed" },
721 },
722 ]);
723 expect(result.imported).toBe(1);
724 expect(result.thread.title).toBe("Renamed");
725 expect(result.thread.historyOrigin).toBe("v1_import");
726 expect(result.items).toHaveLength(1);
727 }).pipe(Effect.scoped, Effect.provide(persistence)),
728);
729​
730it.effect("skips the native history when a run starts while it loads", () =>
731 Effect.gen(function* () {
732 const result = yield* importDuringCommand("run-during-load", (thread) => [
733 {
734 id: EventId.make(`${thread.id}:run`),
735 type: "run.updated",
736 threadId: thread.id,
737 occurredAt: now,
738 payload: {
739 id: RunId.make(`${thread.id}:run`),
740 threadId: thread.id,
741 ordinal: 1,
742 providerInstanceId: thread.providerInstanceId,
743 modelSelection: thread.modelSelection,
744 providerThreadId: null,
745 userMessageId: MessageId.make(`${thread.id}:prompt`),
746 rootNodeId: null,
747 activeAttemptId: null,
748 status: "queued",
749 requestedAt: now,
750 startedAt: null,
751 completedAt: null,
752 checkpointId: null,
753 contextHandoffId: null,
754 },
755 },
756 ]);
757 expect(result.imported).toBe(0);
758 expect(result.runs).toHaveLength(1);
759 expect(result.thread.historyOrigin).toBeUndefined();
760 expect(result.items).toEqual([]);
761 }).pipe(Effect.scoped, Effect.provide(persistence)),
762);
763​
764it.effect("imports under a policy ceiling in the project's default, kept under the ceiling", () =>
765 Effect.gen(function* () {
766 const projections = yield* ProjectionStore.ProjectionStoreV2;
767 const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
768 const source = candidate(CODEX_MODEL_SELECTION, "ceiling");
769 const threadId = importId(source);
770 const importer = yield* makeImporter(
771 [source],
772 Effect.void,
773 undefined,
774 undefined,
775 ServerSettings.layerTest(
776 { defaultRuntimeMode: "full-access" },
777 {
778 organization: "Example Corp",
779 lockedSettings: [],
780 limits: { maxRuntimeMode: "auto-accept-edits" },
781 },
782 ),
783 );
784 expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 });
785 expect((yield* projections.getThread(threadId)).runtimeMode).toBe("auto-accept-edits");
786 expect(Option.getOrThrow(yield* runtimes.getByThreadId({ threadId })).runtimeMode).toBe(
787 "auto-accept-edits",
788 );
789 }).pipe(Effect.scoped, Effect.provide(persistence)),
790);