31
} from "@t3tools/contracts";
32
import * as DateTime from "effect/DateTime";
33
import * as Effect from "effect/Effect";
34
+import * as Deferred from "effect/Deferred";
35
+import * as Fiber from "effect/Fiber";
36
import * as Layer from "effect/Layer";
37
import * as Option from "effect/Option";
38
import * as Schema from "effect/Schema";
39
import * as Tracer from "effect/Tracer";
40
import * as SqlClient from "effect/sql/SqlClient";
41
+import * as Statement from "effect/sql/Statement";
42
import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness";
43
44
import * as SqlitePersistence from "../persistence/Sqlite.ts";
54
} from "./threadHistoryPaging.ts";
55
import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
56
57
+import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts";
58
+
59
const layerTest = Layer.mergeAll(
60
ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
61
SqlitePersistence.layerMemory,
231
return threadId;
232
});
233
234
+it.effect("routes full projection reads to readers and retains uncommitted writer state", () =>
235
+ Effect.gen(function* () {
236
+ const store = yield* ProjectionStore.ProjectionStoreV2;
237
+ const sql = yield* SqlClient.SqlClient;
238
+ const parentId = yield* addRolledBackRecoveryCandidate("reader-routing");
239
+ const parent = yield* store.getThreadProjection(parentId);
240
+ const threadId = ThreadId.make("thread:reader-routing:fork");
241
+ yield* store.apply({
242
+ id: EventId.make("event:reader-routing:fork"),
243
+ type: "thread.created",
244
+ threadId,
245
+ occurredAt: parent.thread.createdAt,
246
+ payload: {
247
+ ...parent.thread,
248
+ id: threadId,
249
+ forkedFrom: { type: "run", threadId: parentId, runId: parent.runs[0]!.id },
250
+ },
251
+ });
252
+ const { queries, trace } = traceSqlRouting();
253
+ const projection = yield* store.getThreadProjection(threadId).pipe(trace);
254
+ assert.equal(projection.thread.id, threadId);
255
+ assert.deepEqual(
256
+ projection.visibleTurnItems
257
+ .filter((row) => row.visibility === "inherited")
258
+ .map((row) => row.sourceItemId),
259
+ parent.turnItems.map((item) => item.id),
260
+ );
261
+ assert.isAbove(queries.length, 1);
262
+ assert.deepEqual([...new Set(queries.map((row) => row.connection))], ["reader"]);
263
+ const commitRequested = yield* Deferred.make<void>();
264
+ const writer = yield* Deferred.await(commitRequested).pipe(
265
+ Effect.andThen(
266
+ sql.withTransaction(
267
+ Effect.gen(function* () {
268
+ yield* sql`
269
+ UPDATE orchestration_v2_projection_threads
270
+ SET payload_json = json_set(payload_json, '$.title', 'Committed title')
271
+ WHERE thread_id = ${threadId}
272
+ `;
273
+ yield* sql`
274
+ UPDATE orchestration_v2_projection_turn_items
275
+ SET payload_json = json_set(payload_json, '$.title', 'Committed command')
276
+ WHERE thread_id = ${parentId}
277
+ `;
278
+ }),
279
+ ),
280
+ ),
281
+ Effect.forkChild,
282
+ );
283
+ let commitInjected = false;
284
+ const duringCommit = yield* store.getThreadProjection(threadId).pipe(
285
+ Effect.provideService(Statement.CurrentTransformer, (statement) => {
286
+ const [query] = statement.compile();
287
+ if (!commitInjected && query.includes("FROM orchestration_v2_projection_turn_items")) {
288
+ commitInjected = true;
289
+ return Deferred.succeed(commitRequested, undefined).pipe(
290
+ Effect.andThen(Fiber.join(writer).pipe(Effect.orDie)),
291
+ Effect.as(statement),
292
+ );
293
+ }
294
+ return Effect.succeed(statement);
295
+ }),
296
+ );
297
+ assert.isTrue(commitInjected);
298
+ assert.deepEqual(duringCommit, projection);
299
+ const afterCommit = yield* store.getThreadProjection(threadId);
300
+ assert.equal(afterCommit.thread.title, "Committed title");
301
+ assert.equal(
302
+ afterCommit.visibleTurnItems.find((row) => row.visibility === "inherited")?.item.title,
303
+ "Committed command",
304
+ );
305
+ yield* sql.withTransaction(
306
+ Effect.gen(function* () {
307
+ yield* sql`
308
+ UPDATE orchestration_v2_projection_threads
309
+ SET payload_json = json_set(payload_json, '$.title', 'Uncommitted title')
310
+ WHERE thread_id = ${threadId}
311
+ `;
312
+ assert.equal(
313
+ (yield* store.getThreadProjection(threadId)).thread.title,
314
+ "Uncommitted title",
315
+ );
316
+ }),
317
+ );
318
+ }).pipe(Effect.provide(ProjectionStore.layer.pipe(Layer.provideMerge(layerReaderDatabase)))),
319
+);
320
+
321
it.effect("record reads restrict provider histories to delivery identities", () =>
322
Effect.gen(function* () {
323
const store = yield* ProjectionStore.ProjectionStoreV2;