|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + CommandId, |
| 5 | + EventId, |
| 6 | + ProjectId, |
| 7 | + ProviderInstanceId, |
| 8 | + ThreadId, |
| 9 | + type OrchestrationEvent, |
| 10 | +} from "@t3tools/contracts"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | +import * as FileSystem from "effect/FileSystem"; |
| 13 | +import * as Layer from "effect/Layer"; |
| 14 | +import * as Path from "effect/Path"; |
| 15 | + |
| 16 | +import { ServerConfig } from "../../config.ts"; |
| 17 | +import { OrchestrationEventStoreLive } from "../../persistence/Layers/OrchestrationEventStore.ts"; |
| 18 | +import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts"; |
| 19 | +import { OrchestrationEventStore } from "../../persistence/Services/OrchestrationEventStore.ts"; |
| 20 | +import { ProjectionStateRepository } from "../../persistence/Services/ProjectionState.ts"; |
| 21 | +import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts"; |
| 22 | +import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts"; |
| 23 | + |
| 24 | +const TestLayer = OrchestrationProjectionPipelineLive.pipe( |
| 25 | + Layer.provideMerge(OrchestrationEventStoreLive), |
| 26 | + Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "t3-projection-cleanup-" })), |
| 27 | + Layer.provideMerge(SqlitePersistenceMemory), |
| 28 | + Layer.provideMerge(NodeServices.layer), |
| 29 | +); |
| 30 | + |
| 31 | +const CLEANUP_PROJECTOR = "projection.attachment-cleanup"; |
| 32 | + |
| 33 | +const exists = (filePath: string) => |
| 34 | + Effect.gen(function* () { |
| 35 | + const fileSystem = yield* FileSystem.FileSystem; |
| 36 | + return (yield* Effect.result(fileSystem.stat(filePath)))._tag === "Success"; |
| 37 | + }); |
| 38 | + |
| 39 | +it.layer(TestLayer)("OrchestrationProjectionPipeline attachment cleanup bootstrap", (it) => { |
| 40 | + it.effect( |
| 41 | + "advances the cleanup cursor to the latest event, dedupes reverts, and holds on failure", |
| 42 | + () => |
| 43 | + Effect.gen(function* () { |
| 44 | + const projectionPipeline = yield* OrchestrationProjectionPipeline; |
| 45 | + const eventStore = yield* OrchestrationEventStore; |
| 46 | + const projectionState = yield* ProjectionStateRepository; |
| 47 | + const fileSystem = yield* FileSystem.FileSystem; |
| 48 | + const path = yield* Path.Path; |
| 49 | + const { attachmentsDir } = yield* ServerConfig; |
| 50 | + const now = "2026-01-01T00:00:00.000Z"; |
| 51 | + const projectId = ProjectId.make("project-cleanup"); |
| 52 | + const revertedThreadId = ThreadId.make("thread-cleanup-revert"); |
| 53 | + const deletedThreadId = ThreadId.make("thread-cleanup-delete"); |
| 54 | + const revertedAttachmentPath = path.join( |
| 55 | + attachmentsDir, |
| 56 | + "thread-cleanup-revert-00000000-0000-4000-8000-000000000001.png", |
| 57 | + ); |
| 58 | + const deletedAttachmentPath = path.join( |
| 59 | + attachmentsDir, |
| 60 | + "thread-cleanup-delete-00000000-0000-4000-8000-000000000001.png", |
| 61 | + ); |
| 62 | + const cleanupCursor = projectionState |
| 63 | + .listAll() |
| 64 | + .pipe( |
| 65 | + Effect.map( |
| 66 | + (states) => |
| 67 | + states.find((state) => state.projector === CLEANUP_PROJECTOR)?.lastAppliedSequence, |
| 68 | + ), |
| 69 | + ); |
| 70 | + |
| 71 | + let eventOrdinal = 0; |
| 72 | + const append = ( |
| 73 | + event: Pick<OrchestrationEvent, "type" | "aggregateKind" | "aggregateId" | "payload">, |
| 74 | + ) => { |
| 75 | + eventOrdinal += 1; |
| 76 | + return eventStore.append({ |
| 77 | + ...event, |
| 78 | + eventId: EventId.make(`evt-cleanup-${eventOrdinal}`), |
| 79 | + occurredAt: now, |
| 80 | + commandId: CommandId.make(`cmd-cleanup-${eventOrdinal}`), |
| 81 | + causationEventId: null, |
| 82 | + correlationId: CommandId.make(`cmd-cleanup-${eventOrdinal}`), |
| 83 | + metadata: {}, |
| 84 | + } as Omit<OrchestrationEvent, "sequence">); |
| 85 | + }; |
| 86 | + const appendThreadCreated = (threadId: ThreadId) => |
| 87 | + append({ |
| 88 | + type: "thread.created", |
| 89 | + aggregateKind: "thread", |
| 90 | + aggregateId: threadId, |
| 91 | + payload: { |
| 92 | + threadId, |
| 93 | + projectId, |
| 94 | + title: "Cleanup thread", |
| 95 | + modelSelection: { |
| 96 | + instanceId: ProviderInstanceId.make("codex"), |
| 97 | + model: "gpt-5-codex", |
| 98 | + }, |
| 99 | + runtimeMode: "full-access", |
| 100 | + branch: null, |
| 101 | + worktreePath: null, |
| 102 | + createdAt: now, |
| 103 | + updatedAt: now, |
| 104 | + }, |
| 105 | + }); |
| 106 | + const appendReverted = (turnCount: number) => |
| 107 | + append({ |
| 108 | + type: "thread.reverted", |
| 109 | + aggregateKind: "thread", |
| 110 | + aggregateId: revertedThreadId, |
| 111 | + payload: { threadId: revertedThreadId, turnCount }, |
| 112 | + }); |
| 113 | + |
| 114 | + yield* append({ |
| 115 | + type: "project.created", |
| 116 | + aggregateKind: "project", |
| 117 | + aggregateId: projectId, |
| 118 | + payload: { |
| 119 | + projectId, |
| 120 | + title: "Cleanup project", |
| 121 | + workspaceRoot: "/tmp/project-cleanup", |
| 122 | + defaultModelSelection: null, |
| 123 | + scripts: [], |
| 124 | + createdAt: now, |
| 125 | + updatedAt: now, |
| 126 | + }, |
| 127 | + }); |
| 128 | + yield* appendThreadCreated(revertedThreadId); |
| 129 | + yield* appendThreadCreated(deletedThreadId); |
| 130 | + yield* fileSystem.makeDirectory(attachmentsDir, { recursive: true }); |
| 131 | + yield* fileSystem.writeFileString(revertedAttachmentPath, "stale"); |
| 132 | + // Two reverts for one thread dedupe into a single prune. |
| 133 | + yield* appendReverted(2); |
| 134 | + yield* appendReverted(1); |
| 135 | + // The latest event is neither a revert nor a delete, yet the cursor must land on it. |
| 136 | + const latest = yield* append({ |
| 137 | + type: "thread.archived", |
| 138 | + aggregateKind: "thread", |
| 139 | + aggregateId: deletedThreadId, |
| 140 | + payload: { threadId: deletedThreadId, archivedAt: now, updatedAt: now }, |
| 141 | + }); |
| 142 | + |
| 143 | + yield* projectionPipeline.bootstrap; |
| 144 | + assert.isFalse(yield* exists(revertedAttachmentPath)); |
| 145 | + assert.equal(yield* cleanupCursor, latest.sequence); |
| 146 | + |
| 147 | + // A cleanup that cannot remove its files leaves the cursor behind for the next bootstrap. |
| 148 | + yield* fileSystem.makeDirectory(deletedAttachmentPath); |
| 149 | + yield* fileSystem.writeFileString(path.join(deletedAttachmentPath, "keep.txt"), "keep"); |
| 150 | + yield* append({ |
| 151 | + type: "thread.deleted", |
| 152 | + aggregateKind: "thread", |
| 153 | + aggregateId: deletedThreadId, |
| 154 | + payload: { threadId: deletedThreadId, deletedAt: now }, |
| 155 | + }); |
| 156 | + yield* projectionPipeline.bootstrap; |
| 157 | + assert.isTrue(yield* exists(deletedAttachmentPath)); |
| 158 | + assert.equal(yield* cleanupCursor, latest.sequence); |
| 159 | + |
| 160 | + yield* fileSystem.remove(deletedAttachmentPath, { recursive: true }); |
| 161 | + yield* fileSystem.writeFileString(deletedAttachmentPath, "retry"); |
| 162 | + yield* projectionPipeline.bootstrap; |
| 163 | + assert.isFalse(yield* exists(deletedAttachmentPath)); |
| 164 | + assert.equal(yield* cleanupCursor, latest.sequence + 1); |
| 165 | + }), |
| 166 | + ); |
| 167 | +}); |
0 commit comments