|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + CommandId, |
| 4 | + EventId, |
| 5 | + ProjectId, |
| 6 | + ProviderInstanceId, |
| 7 | + RunId, |
| 8 | + ThreadId, |
| 9 | + type OrchestrationV2AppThread, |
| 10 | +} from "@t3tools/contracts"; |
| 11 | +import * as DateTime from "effect/DateTime"; |
| 12 | +import * as Effect from "effect/Effect"; |
| 13 | +import * as Layer from "effect/Layer"; |
| 14 | + |
| 15 | +import * as Sqlite from "../persistence/Sqlite.ts"; |
| 16 | +import * as EventSink from "./EventSink.ts"; |
| 17 | +import * as EventStore from "./EventStore.ts"; |
| 18 | +import * as ProjectionStore from "./ProjectionStore.ts"; |
| 19 | + |
| 20 | +const layer = EventSink.layer.pipe( |
| 21 | + Layer.provideMerge(Layer.merge(EventStore.layer, ProjectionStore.layer)), |
| 22 | + Layer.provide(Sqlite.layerMemory), |
| 23 | +); |
| 24 | + |
| 25 | +it.effect.each([false, true])( |
| 26 | + "checks fork source lifetime inside the commit transaction, deleted before commit=%s", |
| 27 | + (deleteFirst) => |
| 28 | + Effect.gen(function* () { |
| 29 | + const sink = yield* EventSink.EventSinkV2; |
| 30 | + const projections = yield* ProjectionStore.ProjectionStoreV2; |
| 31 | + const now = yield* DateTime.now; |
| 32 | + const sourceId = ThreadId.make("fork-source"); |
| 33 | + const targetId = ThreadId.make("fork-target"); |
| 34 | + const providerInstanceId = ProviderInstanceId.make("codex"); |
| 35 | + const source: OrchestrationV2AppThread = { |
| 36 | + id: sourceId, |
| 37 | + projectId: ProjectId.make("project"), |
| 38 | + title: "Source", |
| 39 | + createdBy: "user", |
| 40 | + creationSource: "web", |
| 41 | + providerInstanceId, |
| 42 | + modelSelection: { instanceId: providerInstanceId, model: "gpt-5" }, |
| 43 | + runtimeMode: "full-access", |
| 44 | + interactionMode: "default", |
| 45 | + branch: null, |
| 46 | + worktreePath: null, |
| 47 | + activeProviderThreadId: null, |
| 48 | + lineage: { rootThreadId: sourceId, parentThreadId: null, relationshipToParent: null }, |
| 49 | + forkedFrom: null, |
| 50 | + createdAt: now, |
| 51 | + updatedAt: now, |
| 52 | + archivedAt: null, |
| 53 | + deletedAt: null, |
| 54 | + settledOverride: null, |
| 55 | + settledAt: null, |
| 56 | + snoozedUntil: null, |
| 57 | + snoozedAt: null, |
| 58 | + lastVisitedAt: null, |
| 59 | + }; |
| 60 | + yield* sink.write({ |
| 61 | + events: [ |
| 62 | + { |
| 63 | + id: EventId.make("source-created"), |
| 64 | + type: "thread.created", |
| 65 | + threadId: sourceId, |
| 66 | + occurredAt: now, |
| 67 | + payload: source, |
| 68 | + }, |
| 69 | + ], |
| 70 | + }); |
| 71 | + const plannedFork = { |
| 72 | + commandId: CommandId.make("fork"), |
| 73 | + commandType: "thread.fork", |
| 74 | + threadId: targetId, |
| 75 | + acceptedAt: now, |
| 76 | + effects: [], |
| 77 | + events: [ |
| 78 | + { |
| 79 | + id: EventId.make("fork-created"), |
| 80 | + type: "thread.created" as const, |
| 81 | + threadId: targetId, |
| 82 | + occurredAt: now, |
| 83 | + payload: { |
| 84 | + ...source, |
| 85 | + id: targetId, |
| 86 | + forkedFrom: { type: "run" as const, threadId: sourceId, runId: RunId.make("run") }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }; |
| 91 | + const deleteSource = sink.write({ |
| 92 | + events: [ |
| 93 | + { |
| 94 | + id: EventId.make("source-deleted"), |
| 95 | + type: "thread.deleted", |
| 96 | + threadId: sourceId, |
| 97 | + occurredAt: now, |
| 98 | + payload: { ...source, deletedAt: now }, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }); |
| 102 | + if (deleteFirst) { |
| 103 | + yield* deleteSource; |
| 104 | + const error = yield* sink.commitCommand(plannedFork).pipe(Effect.flip); |
| 105 | + assert.instanceOf(error, EventSink.EventSinkWriteError); |
| 106 | + const missing = yield* projections.getThread(targetId).pipe(Effect.flip); |
| 107 | + assert.instanceOf(missing, ProjectionStore.ProjectionStoreThreadNotFoundError); |
| 108 | + } else { |
| 109 | + assert.isTrue((yield* sink.commitCommand(plannedFork)).committed); |
| 110 | + yield* deleteSource; |
| 111 | + // A receipt replay must still return the fork that committed before deletion. |
| 112 | + assert.isFalse((yield* sink.commitCommand(plannedFork)).committed); |
| 113 | + assert.deepEqual( |
| 114 | + (yield* projections.getThread(targetId)).forkedFrom, |
| 115 | + plannedFork.events[0]!.payload.forkedFrom, |
| 116 | + ); |
| 117 | + } |
| 118 | + }).pipe(Effect.provide(layer)), |
| 119 | +); |
0 commit comments