2281 lines · 78.1 KB · TypeScript
RawDownload
import {  CheckpointRef,  CommandId,  CorrelationId,  EventId,  MessageId,  ProjectId,  ThreadId,  TurnId,  ProviderInstanceId,} from "@t3tools/contracts";import * as NodeServices from "@effect/platform-node/NodeServices";import { assert, it } from "@effect/vitest";import { Effect, FileSystem, Layer, Path } from "effect";import * as SqlClient from "effect/unstable/sql/SqlClient"; import { OrchestrationCommandReceiptRepositoryLive } from "../../persistence/Layers/OrchestrationCommandReceipts.ts";import { OrchestrationEventStoreLive } from "../../persistence/Layers/OrchestrationEventStore.ts";import {  makeSqlitePersistenceLive,  SqlitePersistenceMemory,} from "../../persistence/Layers/Sqlite.ts";import { OrchestrationEventStore } from "../../persistence/Services/OrchestrationEventStore.ts";import { RepositoryIdentityResolverLive } from "../../project/Layers/RepositoryIdentityResolver.ts";import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";import {  ORCHESTRATION_PROJECTOR_NAMES,  OrchestrationProjectionPipelineLive,} from "./ProjectionPipeline.ts";import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts";import { ServerConfig } from "../../config.ts"; const makeProjectionPipelinePrefixedTestLayer = (prefix: string) =>  OrchestrationProjectionPipelineLive.pipe(    Layer.provideMerge(OrchestrationEventStoreLive),    Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix })),    Layer.provideMerge(SqlitePersistenceMemory),    Layer.provideMerge(NodeServices.layer),  ); const exists = (filePath: string) =>  Effect.gen(function* () {    const fileSystem = yield* FileSystem.FileSystem;    const fileInfo = yield* Effect.result(fileSystem.stat(filePath));    return fileInfo._tag === "Success";  }); const BaseTestLayer = makeProjectionPipelinePrefixedTestLayer("t3-projection-pipeline-test-"); it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {  it.effect("bootstraps all projection states and writes projection rows", () =>    Effect.gen(function* () {      const projectionPipeline = yield* OrchestrationProjectionPipeline;      const eventStore = yield* OrchestrationEventStore;      const sql = yield* SqlClient.SqlClient;      const now = new Date().toISOString();       yield* eventStore.append({        type: "project.created",        eventId: EventId.make("evt-1"),        aggregateKind: "project",        aggregateId: ProjectId.make("project-1"),        occurredAt: now,        commandId: CommandId.make("cmd-1"),        causationEventId: null,        correlationId: CommandId.make("cmd-1"),        metadata: {},        payload: {          projectId: ProjectId.make("project-1"),          title: "Project 1",          workspaceRoot: "/tmp/project-1",          defaultModelSelection: null,          scripts: [],          createdAt: now,          updatedAt: now,        },      });       yield* eventStore.append({        type: "thread.created",        eventId: EventId.make("evt-2"),        aggregateKind: "thread",        aggregateId: ThreadId.make("thread-1"),        occurredAt: now,        commandId: CommandId.make("cmd-2"),        causationEventId: null,        correlationId: CommandId.make("cmd-2"),        metadata: {},        payload: {          threadId: ThreadId.make("thread-1"),          projectId: ProjectId.make("project-1"),          title: "Thread 1",          modelSelection: {            instanceId: ProviderInstanceId.make("codex"),            model: "gpt-5-codex",          },          runtimeMode: "full-access",          branch: null,          worktreePath: null,          createdAt: now,          updatedAt: now,        },      });       yield* eventStore.append({        type: "thread.message-sent",        eventId: EventId.make("evt-3"),        aggregateKind: "thread",        aggregateId: ThreadId.make("thread-1"),        occurredAt: now,        commandId: CommandId.make("cmd-3"),        causationEventId: null,        correlationId: CommandId.make("cmd-3"),        metadata: {},        payload: {          threadId: ThreadId.make("thread-1"),          messageId: MessageId.make("message-1"),          role: "assistant",          text: "hello",          turnId: null,          streaming: false,          createdAt: now,          updatedAt: now,        },      });       yield* projectionPipeline.bootstrap;       const projectRows = yield* sql<{        readonly projectId: string;        readonly title: string;        readonly scriptsJson: string;      }>`        SELECT          project_id AS "projectId",          title,          scripts_json AS "scriptsJson"        FROM projection_projects      `;      assert.deepEqual(projectRows, [        { projectId: "project-1", title: "Project 1", scriptsJson: "[]" },      ]);       const messageRows = yield* sql<{        readonly messageId: string;        readonly text: string;      }>`        SELECT          message_id AS "messageId",          text        FROM projection_thread_messages      `;      assert.deepEqual(messageRows, [{ messageId: "message-1", text: "hello" }]);       const stateRows = yield* sql<{        readonly projector: string;        readonly lastAppliedSequence: number;      }>`        SELECT          projector,          last_applied_sequence AS "lastAppliedSequence"        FROM projection_state        ORDER BY projector ASC      `;      assert.equal(stateRows.length, Object.keys(ORCHESTRATION_PROJECTOR_NAMES).length);      for (const row of stateRows) {        assert.equal(row.lastAppliedSequence, 3);      }    }),  );}); it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-base-")))(  "OrchestrationProjectionPipeline",  (it) => {    it.effect("stores message attachment references without mutating payloads", () =>      Effect.gen(function* () {        const projectionPipeline = yield* OrchestrationProjectionPipeline;        const eventStore = yield* OrchestrationEventStore;        const sql = yield* SqlClient.SqlClient;        const now = new Date().toISOString();         yield* eventStore.append({          type: "thread.message-sent",          eventId: EventId.make("evt-attachments"),          aggregateKind: "thread",          aggregateId: ThreadId.make("thread-attachments"),          occurredAt: now,          commandId: CommandId.make("cmd-attachments"),          causationEventId: null,          correlationId: CommandId.make("cmd-attachments"),          metadata: {},          payload: {            threadId: ThreadId.make("thread-attachments"),            messageId: MessageId.make("message-attachments"),            role: "user",            text: "Inspect this",            attachments: [              {                type: "image",                id: "thread-attachments-att-1",                name: "example.png",                mimeType: "image/png",                sizeBytes: 5,              },            ],            turnId: null,            streaming: false,            createdAt: now,            updatedAt: now,          },        });         yield* projectionPipeline.bootstrap;         const rows = yield* sql<{          readonly attachmentsJson: string | null;        }>`            SELECT              attachments_json AS "attachmentsJson"            FROM projection_thread_messages            WHERE message_id = 'message-attachments'          `;        assert.equal(rows.length, 1);        assert.deepEqual(JSON.parse(rows[0]?.attachmentsJson ?? "null"), [          {            type: "image",            id: "thread-attachments-att-1",            name: "example.png",            mimeType: "image/png",            sizeBytes: 5,          },        ]);      }),    );  },); it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-projection-attachments-safe-")))(  "OrchestrationProjectionPipeline",  (it) => {    it.effect("preserves mixed image attachment metadata as-is", () =>      Effect.gen(function* () {        const projectionPipeline = yield* OrchestrationProjectionPipeline;        const eventStore = yield* OrchestrationEventStore;        const sql = yield* SqlClient.SqlClient;        const now = new Date().toISOString();         yield* eventStore.append({          type: "thread.message-sent",          eventId: EventId.make("evt-attachments-safe"),          aggregateKind: "thread",          aggregateId: ThreadId.make("thread-attachments-safe"),          occurredAt: now,          commandId: CommandId.make("cmd-attachments-safe"),          causationEventId: null,          correlationId: CommandId.make("cmd-attachments-safe"),          metadata: {},          payload: {            threadId: ThreadId.make("thread-attachments-safe"),            messageId: MessageId.make("message-attachments-safe"),            role: "user",            text: "Inspect this",            attachments: [              {                type: "image",                id: "thread-attachments-safe-att-1",                name: "untrusted.exe",                mimeType: "image/x-unknown",                sizeBytes: 5,              },              {                type: "image",                id: "thread-attachments-safe-att-2",                name: "not-image.png",                mimeType: "image/png",                sizeBytes: 5,              },            ],            turnId: null,            streaming: false,            createdAt: now,            updatedAt: now,          },        });         yield* projectionPipeline.bootstrap;         const rows = yield* sql<{          readonly attachmentsJson: string | null;        }>`            SELECT              attachments_json AS "attachmentsJson"            FROM projection_thread_messages            WHERE message_id = 'message-attachments-safe'          `;        assert.equal(rows.length, 1);        assert.deepEqual(JSON.parse(rows[0]?.attachmentsJson ?? "null"), [          {            type: "image",            id: "thread-attachments-safe-att-1",            name: "untrusted.exe",            mimeType: "image/x-unknown",            sizeBytes: 5,          },          {            type: "image",            id: "thread-attachments-safe-att-2",            name: "not-image.png",            mimeType: "image/png",            sizeBytes: 5,          },        ]);      }),    );  },); it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {  it.effect(    "passes explicit empty attachment arrays through the projection pipeline to clear attachments",    () =>      Effect.gen(function* () {        const projectionPipeline = yield* OrchestrationProjectionPipeline;        const eventStore = yield* OrchestrationEventStore;        const sql = yield* SqlClient.SqlClient;        const now = new Date().toISOString();        const later = new Date(Date.now() + 1_000).toISOString();         yield* eventStore.append({          type: "project.created",          eventId: EventId.make("evt-clear-attachments-1"),          aggregateKind: "project",          aggregateId: ProjectId.make("project-clear-attachments"),          occurredAt: now,          commandId: CommandId.make("cmd-clear-attachments-1"),          causationEventId: null,          correlationId: CommandId.make("cmd-clear-attachments-1"),          metadata: {},          payload: {            projectId: ProjectId.make("project-clear-attachments"),            title: "Project Clear Attachments",            workspaceRoot: "/tmp/project-clear-attachments",            defaultModelSelection: null,            scripts: [],            createdAt: now,            updatedAt: now,          },        });         yield* eventStore.append({          type: "thread.created",          eventId: EventId.make("evt-clear-attachments-2"),          aggregateKind: "thread",          aggregateId: ThreadId.make("thread-clear-attachments"),          occurredAt: now,          commandId: CommandId.make("cmd-clear-attachments-2"),          causationEventId: null,          correlationId: CommandId.make("cmd-clear-attachments-2"),          metadata: {},          payload: {            threadId: ThreadId.make("thread-clear-attachments"),            projectId: ProjectId.make("project-clear-attachments"),            title: "Thread Clear Attachments",            modelSelection: {              instanceId: ProviderInstanceId.make("codex"),              model: "gpt-5-codex",            },            runtimeMode: "full-access",            branch: null,            worktreePath: null,            createdAt: now,            updatedAt: now,          },        });         yield* eventStore.append({          type: "thread.message-sent",          eventId: EventId.make("evt-clear-attachments-3"),          aggregateKind: "thread",          aggregateId: ThreadId.make("thread-clear-attachments"),          occurredAt: now,          commandId: CommandId.make("cmd-clear-attachments-3"),          causationEventId: null,          correlationId: CommandId.make("cmd-clear-attachments-3"),          metadata: {},          payload: {            threadId: ThreadId.make("thread-clear-attachments"),            messageId: MessageId.make("message-clear-attachments"),            role: "user",            text: "Has attachments",            attachments: [              {                type: "image",                id: "thread-clear-attachments-att-1",                name: "clear.png",                mimeType: "image/png",                sizeBytes: 5,              },            ],            turnId: null,            streaming: false,            createdAt: now,            updatedAt: now,          },        });         yield* eventStore.append({          type: "thread.message-sent",          eventId: EventId.make("evt-clear-attachments-4"),          aggregateKind: "thread",          aggregateId: ThreadId.make("thread-clear-attachments"),          occurredAt: later,          commandId: CommandId.make("cmd-clear-attachments-4"),          causationEventId: null,          correlationId: CommandId.make("cmd-clear-attachments-4"),          metadata: {},          payload: {            threadId: ThreadId.make("thread-clear-attachments"),            messageId: MessageId.make("message-clear-attachments"),            role: "user",            text: "",            attachments: [],            turnId: null,            streaming: false,            createdAt: now,            updatedAt: later,          },        });         yield* projectionPipeline.bootstrap;         const rows = yield* sql<{          readonly attachmentsJson: string | null;        }>`          SELECT            attachments_json AS "attachmentsJson"          FROM projection_thread_messages          WHERE message_id = 'message-clear-attachments'        `;        assert.equal(rows.length, 1);        assert.deepEqual(JSON.parse(rows[0]?.attachmentsJson ?? "null"), []);      }),  );}); it.layer(  Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-projection-attachments-overwrite-")),)("OrchestrationProjectionPipeline", (it) => {  it.effect("overwrites stored attachment references when a message updates attachments", () =>    Effect.gen(function* () {      const projectionPipeline = yield* OrchestrationProjectionPipeline;      const eventStore = yield* OrchestrationEventStore;      const sql = yield* SqlClient.SqlClient;      const now = new Date().toISOString();      const later = new Date(Date.now() + 1_000).toISOString();       yield* eventStore.append({        type: "project.created",        eventId: EventId.make("evt-overwrite-1"),        aggregateKind: "project",        aggregateId: ProjectId.make("project-overwrite"),        occurredAt: now,        commandId: CommandId.make("cmd-overwrite-1"),        causationEventId: null,        correlationId: CommandId.make("cmd-overwrite-1"),        metadata: {},        payload: {          projectId: ProjectId.make("project-overwrite"),          title: "Project Overwrite",          workspaceRoot: "/tmp/project-overwrite",          defaultModelSelection: null,          scripts: [],          createdAt: now,          updatedAt: now,        },      });       yield* eventStore.append({        type: "thread.created",        eventId: EventId.make("evt-overwrite-2"),        aggregateKind: "thread",        aggregateId: ThreadId.make("thread-overwrite"),        occurredAt: now,        commandId: CommandId.make("cmd-overwrite-2"),        causationEventId: null,        correlationId: CommandId.make("cmd-overwrite-2"),        metadata: {},        payload: {          threadId: ThreadId.make("thread-overwrite"),          projectId: ProjectId.make("project-overwrite"),          title: "Thread Overwrite",          modelSelection: {            instanceId: ProviderInstanceId.make("codex"),            model: "gpt-5-codex",          },          runtimeMode: "full-access",          branch: null,

Lines 1–500 of 2281.