Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import {
3 EventId,
4 MessageId,
5 ProjectId,
6 ProviderInstanceId,
7 RunId,
8 ThreadId,
9 type OrchestrationV2AppThread,
10 type OrchestrationV2Run,
11} from "@t3tools/contracts";
12import * as DateTime from "effect/DateTime";
13import * as Effect from "effect/Effect";
14import * as Layer from "effect/Layer";
15import * as SqlClient from "effect/unstable/sql/SqlClient";
16import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
17import {
18 ProjectionStoreV2,
19 ProjectionStoreThreadNotFoundError,
20 layer,
21 layerMemory,
22} from "./ProjectionStore.ts";
23
24const databaseLayer = Layer.mergeAll(
25 SqlitePersistenceMemory,
26 layer.pipe(Layer.provide(SqlitePersistenceMemory)),
27);
28
29it.effect.each(["sqlite", "memory"] as const)(
30 "checks queue eligibility and delivery ownership without unrelated history in %s",
31 (storage) =>
32 Effect.gen(function* () {
33 const store = yield* ProjectionStoreV2;
34 const now = yield* DateTime.now;
35 const threadId = ThreadId.make("thread:completion-reads");
36 const runId = RunId.make("run:completion-reads");
37 const messageId = MessageId.make("message:completion-reads");
38 const instanceId = ProviderInstanceId.make("codex");
39 const thread: OrchestrationV2AppThread = {
40 id: threadId,
41 projectId: ProjectId.make("project:completion-reads"),
42 title: "Completion controls",
43 providerInstanceId: instanceId,
44 modelSelection: { instanceId, model: "test" },
45 runtimeMode: "full-access",
46 interactionMode: "default",
47 branch: null,
48 worktreePath: null,
49 activeProviderThreadId: null,
50 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
51 forkedFrom: null,
52 createdBy: "user",
53 creationSource: "web",
54 createdAt: now,
55 updatedAt: now,
56 archivedAt: null,
57 settledOverride: null,
58 settledAt: null,
59 lastVisitedAt: null,
60 deletedAt: null,
61 };
62 const run: OrchestrationV2Run = {
63 id: runId,
64 threadId,
65 ordinal: 1,
66 providerInstanceId: instanceId,
67 modelSelection: thread.modelSelection,
68 providerThreadId: null,
69 userMessageId: messageId,
70 rootNodeId: null,
71 activeAttemptId: null,
72 status: "queued",
73 requestedAt: now,
74 startedAt: null,
75 completedAt: null,
76 checkpointId: null,
77 contextHandoffId: null,
78 };
79 const putThread = (value: OrchestrationV2AppThread) =>
80 store.apply({
81 id: EventId.make(`event:thread:${value.id}`),
82 type: "thread.created",
83 threadId: value.id,
84 occurredAt: now,
85 payload: value,
86 });
87 const putRun = (value: OrchestrationV2Run) =>
88 store.apply({
89 id: EventId.make(`event:run:${value.id}:${value.status}`),
90 type: "run.updated",
91 threadId,
92 runId: value.id,
93 occurredAt: now,
94 payload: value,
95 });
96 yield* putThread(thread);
97 yield* putRun(run);
98 const message = {
99 id: messageId,
100 threadId,
101 runId,
102 nodeId: null,
103 role: "user" as const,
104 text: "Continue",
105 attachments: [],
106 streaming: false,
107 createdBy: "agent" as const,
108 creationSource: "server" as const,
109 createdAt: now,
110 updatedAt: now,
111 delegatedCompletion: { parentRunId: RunId.make("parent-run"), generation: 1, taskIds: [] },
112 };
113 yield* store.apply({
114 id: EventId.make("event:input"),
115 type: "message.updated",
116 threadId,
117 runId,
118 occurredAt: now,
119 payload: message,
120 });
121 if (storage === "sqlite") {
122 const sql = yield* SqlClient.SqlClient;
123 yield* sql`INSERT INTO orchestration_v2_projection_messages
124 (message_id, thread_id, run_id, node_id, role, streaming, created_at, updated_at, payload_json)
125 VALUES ('obsolete-history', ${threadId}, NULL, NULL, 'assistant', 0, ${DateTime.formatIso(now)}, ${DateTime.formatIso(now)}, '{"obsolete":true}')`;
126 assert.equal((yield* Effect.exit(store.getThreadProjection(threadId)))._tag, "Failure");
127 }
128 assert.deepEqual(yield* store.getRunMessage(threadId, runId), message);
129 assert.isTrue(yield* store.canStartQueuedRun(threadId));
130 yield* putRun({ ...run, queueHeld: true });
131 assert.isFalse(yield* store.canStartQueuedRun(threadId));
132 yield* putRun(run);
133 const blocker = { ...run, id: RunId.make("blocker"), ordinal: 2 };
134 for (const status of ["preparing", "starting", "running", "waiting"] as const) {
135 yield* putRun({ ...blocker, status });
136 assert.isFalse(yield* store.canStartQueuedRun(threadId));
137 }
138 yield* putRun({ ...blocker, status: "completed" });
139 assert.isTrue(yield* store.canStartQueuedRun(threadId));
140 for (const barrier of ["archivedAt", "deletedAt"] as const) {
141 yield* putThread({ ...thread, [barrier]: now });
142 assert.isFalse(yield* store.canStartQueuedRun(threadId));
143 }
144 yield* putThread(thread);
145 yield* putRun({ ...run, status: "completed" });
146 assert.isFalse(yield* store.canStartQueuedRun(threadId));
147 const otherId = ThreadId.make("thread:other");
148 yield* putThread({ ...thread, id: otherId });
149 assert.isUndefined(yield* store.getRunMessage(otherId, runId));
150 assert.isUndefined(yield* store.getRunMessage(threadId, RunId.make("missing-run")));
151 const missing = ThreadId.make("missing-thread");
152 assert.instanceOf(
153 yield* store.canStartQueuedRun(missing).pipe(Effect.flip),
154 ProjectionStoreThreadNotFoundError,
155 );
156 assert.instanceOf(
157 yield* store.getRunMessage(missing, runId).pipe(Effect.flip),
158 ProjectionStoreThreadNotFoundError,
159 );
160 }).pipe(
161 Effect.provide(
162 storage === "sqlite" ? databaseLayer : Layer.merge(SqlitePersistenceMemory, layerMemory),
163 ),
164 ),
165);