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