@yielded/agent-storage-cloudflare 0.1.0-beta.173 → 0.1.0-beta.175

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/DoMemoryStore.mjs +1 -1
  2. package/dist/DoMessageDeliveryStore.mjs +1 -1
  3. package/dist/DoStorageFailpoint.d.mts +3 -1
  4. package/dist/DoStorageFailpoint.mjs +7 -2
  5. package/dist/DoStorageFailpoint.mjs.map +1 -1
  6. package/dist/DoStorageFailpointTesting.mjs +1 -4
  7. package/dist/DoStorageFailpointTesting.mjs.map +1 -1
  8. package/dist/{DoStorageVersion-NKnQU3eR.d.mts → DoStorageVersion-Mc7CXMdN.d.mts} +2 -2
  9. package/dist/DoStorageVersion.d.mts +1 -1
  10. package/dist/DoStorageVersion.mjs +2 -2
  11. package/dist/DoSubmissionLedger.mjs +121 -67
  12. package/dist/DoSubmissionLedger.mjs.map +1 -1
  13. package/dist/DoSubscriptionStore.mjs.map +1 -1
  14. package/dist/{DoThreadStore-CtyahRTW.mjs → DoThreadStore-CgIZQUNR.mjs} +30 -25
  15. package/dist/DoThreadStore-CgIZQUNR.mjs.map +1 -0
  16. package/dist/DoThreadStore.mjs +1 -1
  17. package/dist/PortProtocol.d.mts +12 -0
  18. package/dist/PortRouting.mjs +6 -1
  19. package/dist/PortRouting.mjs.map +1 -1
  20. package/dist/{do-journal-_MdLGTd3.mjs → do-journal-7P3DUBz0.mjs} +140 -65
  21. package/dist/do-journal-7P3DUBz0.mjs.map +1 -0
  22. package/dist/index.d.mts +1 -1
  23. package/dist/index.mjs +1 -1
  24. package/dist/{migrations-C3N9GN6A.mjs → migrations-CAO_akhD.mjs} +45 -22
  25. package/dist/migrations-CAO_akhD.mjs.map +1 -0
  26. package/dist/{owned-state-BHzUi0eb.mjs → owned-state-B4QhezMp.mjs} +8 -6
  27. package/dist/owned-state-B4QhezMp.mjs.map +1 -0
  28. package/package.json +1 -1
  29. package/src/DoStorageFailpoint.ts +4 -0
  30. package/src/DoStorageFailpointTesting.ts +9 -7
  31. package/src/DoSubmissionLedger.ts +271 -150
  32. package/src/DoSubscriptionStore.ts +1 -0
  33. package/src/DoThreadStore.ts +27 -50
  34. package/src/PortRouting.ts +10 -2
  35. package/src/internal/canonical-append.ts +1 -1
  36. package/src/internal/do-journal.ts +987 -800
  37. package/src/internal/migrations.ts +53 -10
  38. package/src/internal/owned-state.ts +18 -7
  39. package/src/internal/prompt-checkpoint.ts +19 -13
  40. package/src/internal/storage-span.ts +22 -13
  41. package/dist/DoThreadStore-CtyahRTW.mjs.map +0 -1
  42. package/dist/do-journal-_MdLGTd3.mjs.map +0 -1
  43. package/dist/migrations-C3N9GN6A.mjs.map +0 -1
  44. package/dist/owned-state-BHzUi0eb.mjs.map +0 -1
@@ -1,10 +1,12 @@
1
+ import { isDefaultDoStorageFailpoint } from "./DoStorageFailpoint.mjs";
1
2
  import { DoAppendConflict, DoCheckpointConflict, DoStorageCorruptionError, DoStorageError, DoValueBoundExceeded } from "./DoStorageError.mjs";
2
- import { n as ensureDoStorageLayout } from "./migrations-C3N9GN6A.mjs";
3
- import { n as ownedRows, r as ownedState } from "./owned-state-BHzUi0eb.mjs";
3
+ import { n as ensureDoStorageLayout } from "./migrations-CAO_akhD.mjs";
4
+ import { n as ownedRows, r as ownedState } from "./owned-state-B4QhezMp.mjs";
4
5
  import { SqlLifecycleRetainer, SqlLifecycleSource, makeSqlLifecyclePublication } from "@yielded/agent-storage-sql/sql-lifecycle-publication";
5
6
  import { SqlStorageOwner } from "@yielded/agent/sql-memory-store";
6
- import { Cause, Clock, Effect, Option, Predicate, Result, Schema, Stream } from "effect";
7
+ import { Cause, Clock, Effect, Option, Predicate, References, Result, Schema, Stream, Tracer } from "effect";
7
8
  import * as SqlClient$1 from "effect/sql/SqlClient";
9
+ import { SqliteClient } from "@effect/sql-sqlite-do";
8
10
  import { makeCanonicalStorage, recordBatchId } from "@yielded/agent-storage-sql/sql-canonical-storage";
9
11
  import { makeSqlExecutions } from "@yielded/agent-storage-sql/sql-journal";
10
12
  import { SqlStorageProgress } from "@yielded/agent-storage-sql/sql-storage-progress";
@@ -12,9 +14,10 @@ import { canonicalRecordJson, makeSqlThreadArchiveRange } from "@yielded/agent-s
12
14
  import { makeSqlThreadWork } from "@yielded/agent-storage-sql/sql-thread-work";
13
15
  import { ThreadId } from "@yielded/agent/identifiers";
14
16
  import { LifecyclePublicationError, LifecyclePublicationFact } from "@yielded/agent/lifecycle-publication";
15
- import { CanonicalRecord, CanonicalSequence } from "@yielded/agent/records";
16
- import { ThreadStoreDiagnostic, ThreadStoreError } from "@yielded/agent/thread-store";
17
- import { SqlError } from "effect/sql/SqlError";
17
+ import { BatchId, CanonicalRecord, CanonicalSequence } from "@yielded/agent/records";
18
+ import { AppendResult, ThreadStoreDiagnostic, ThreadStoreError } from "@yielded/agent/thread-store";
19
+ import { SqlError, classifySqliteError } from "effect/sql/SqlError";
20
+ import { CurrentTransformer } from "effect/sql/Statement";
18
21
  //#region src/internal/storage-span.ts
19
22
  const isAppendContention = (error) => (error._tag === "DoAppendConflict" || error._tag === "AppendConflict") && error.reason === "tail";
20
23
  /** Expected refusals remain typed at the port, but are outcomes of the storage operation. */
@@ -24,7 +27,10 @@ const annotateStorageError = (error) => Effect.annotateCurrentSpan({
24
27
  "error.type": error._tag,
25
28
  ...Predicate.hasProperty(error, "cause") && Predicate.hasProperty(error.cause, "_tag") && typeof error.cause._tag === "string" ? { "error.cause.type": error.cause._tag } : {}
26
29
  });
27
- const withStorageSpan = (name, expected = () => false) => (effect) => storageResult(effect, expected).pipe(Effect.tap((result) => Result.isFailure(result) ? Effect.annotateCurrentSpan({ "storage.outcome": result.failure._tag }) : Effect.void), Effect.tapError(annotateStorageError), Effect.withSpan(name), Effect.flatMap(Effect.fromResult));
30
+ const withStorageSpan = (name, expected = () => false) => {
31
+ const span = Effect.withSpan(name);
32
+ return (effect) => Effect.withFiber((fiber) => !fiber.getRef(References.TracerEnabled) || fiber.getRef(Tracer.Tracer) === Tracer.nativeTracer ? effect : storageResult(effect, expected).pipe(Effect.tap((result) => Result.isFailure(result) ? Effect.annotateCurrentSpan({ "storage.outcome": result.failure._tag }) : Effect.void), Effect.tapError(annotateStorageError), span, Effect.flatMap(Effect.fromResult)));
33
+ };
28
34
  //#endregion
29
35
  //#region src/internal/do-journal.ts
30
36
  /**
@@ -42,6 +48,7 @@ const MAX_RECORD_CACHE_ENTRIES = 4096;
42
48
  /** Durable Object SQL storage allows at most 100 bound parameters per statement. */
43
49
  const MAX_BOUND_PARAMETERS = 100;
44
50
  const isSqlError$1 = Schema.is(SqlError);
51
+ const isNativeClient = Schema.is(Schema.declare((value) => Predicate.hasProperty(value, SqliteClient.TypeId)));
45
52
  const storedTextBytes = (value) => new TextEncoder().encode(value).byteLength;
46
53
  const chunked = (values, size) => {
47
54
  const chunks = [];
@@ -55,8 +62,8 @@ var ThreadRow = class extends Schema.Class("ThreadRow")({
55
62
  }) {};
56
63
  const threadRows = ownedRows(ThreadRow, "effect_agent_threads", (row) => row.thread_id, "thread_id");
57
64
  var RecordRow = class extends Schema.Class("RecordRow")({
58
- batch_id: Schema.fromJsonString(BoundedIdentifier),
59
- thread_id: BoundedIdentifier,
65
+ batch_id: Schema.fromJsonString(BatchId.check(Schema.isMaxLength(MAX_IDENTIFIER_LENGTH))),
66
+ thread_id: ThreadId.check(Schema.isMaxLength(MAX_IDENTIFIER_LENGTH)),
60
67
  record_id: Schema.fromJsonString(BoundedIdentifier),
61
68
  record_json: BoundedStoredText,
62
69
  sequence: CanonicalSequence
@@ -123,11 +130,6 @@ const LifecycleCursorRow = Schema.Struct({
123
130
  });
124
131
  const lifecycleCursorRows = ownedRows(LifecycleCursorRow, "effect_agent_lifecycle_cursors", (row) => row.thread_id, "thread_id");
125
132
  const initializedLifecycleSources = /* @__PURE__ */ new WeakSet();
126
- var RawAppendResult = class extends Schema.Class("@effect-agent/storage-cloudflare/RawAppendResult")({
127
- firstSequence: CanonicalSequence,
128
- lastSequence: CanonicalSequence,
129
- replayed: Schema.Boolean
130
- }) {};
131
133
  var RawReadRequest = class extends Schema.Class("@effect-agent/storage-cloudflare/RawReadRequest")({
132
134
  threadId: BoundedIdentifier,
133
135
  fromSequenceExclusive: CanonicalSequence,
@@ -174,9 +176,9 @@ const decodeSingleRow = (schema, table, rowKey, rows) => decodeRows(schema, tabl
174
176
  }))));
175
177
  /** Open a validated layout and bind this Object's storage owner. */
176
178
  const ensureCurrentStorage = Effect.fnUntraced(function* (sql, failpoint = noFailpoint, maxStoredValueBytes) {
177
- yield* ensureDoStorageLayout().pipe(Effect.provideService(SqlClient$1.SqlClient, sql));
178
179
  const state = yield* ownedState(sql);
179
180
  const owner = (yield* SqlStorageOwner) ?? state;
181
+ yield* ensureDoStorageLayout().pipe(Effect.provideService(SqlStorageOwner, owner), Effect.provideService(SqlClient$1.SqlClient, sql));
180
182
  let journal;
181
183
  const lifecycle = yield* makeSqlLifecyclePublication(void 0, maxStoredValueBytes).pipe(Effect.provideService(SqlStorageOwner, owner), Effect.provideService(SqlLifecycleSource, {
182
184
  beforeRetain: (threadId) => Effect.suspend(() => journal === void 0 ? Effect.fail(LifecyclePublicationError.make({ reason: "unavailable" })) : journal.flushCanonical(threadId)),
@@ -190,9 +192,10 @@ const ensureCurrentStorage = Effect.fnUntraced(function* (sql, failpoint = noFai
190
192
  if (lifecycle !== void 0) yield* journal.initializeLifecycleSource();
191
193
  return journal;
192
194
  });
193
- const makeJournal = (sql, failpoint, maxStoredValueBytes, lifecycle, state) => Effect.gen(function* () {
195
+ const makeJournal = Effect.fnUntraced(function* (sql, failpoint, maxStoredValueBytes, lifecycle, state) {
194
196
  const owner = (yield* SqlStorageOwner) ?? state;
195
197
  const progress = yield* SqlStorageProgress;
198
+ const nativeDb = isNativeClient(sql) && sql.config.transformQueryNames === void 0 && sql.config.transformResultNames === void 0 && isDefaultDoStorageFailpoint(failpoint) ? sql.config.storage?.sql : void 0;
196
199
  const work = yield* makeSqlThreadWork({
197
200
  read: (body) => state.read(body),
198
201
  write: (body) => owner.transaction(body.pipe(Effect.tap(() => state.invalidate))).pipe(Effect.provideService(SqlClient$1.SqlClient, sql), Effect.catchTag("SqlError", (cause) => ThreadStoreError.make({
@@ -252,7 +255,10 @@ const makeJournal = (sql, failpoint, maxStoredValueBytes, lifecycle, state) => E
252
255
  * transaction. Expected outcomes may be captured only when every such refusal precedes
253
256
  * mutation; real faults still roll back. Successful preflight reads never authorize a write.
254
257
  */
255
- const withWriteTransaction = (operation, expected = () => false) => (effect) => state.owner.withMutation(owner.transaction(storageResult(effect, expected))).pipe(Effect.provideService(SqlClient$1.SqlClient, sql), Effect.mapError((error) => isSqlError$1(error) ? storageError(operation)(error) : error), Effect.tapError(annotateStorageError), Effect.withSpan("DoJournal.withWriteTransaction", { attributes: { operation } }), Effect.flatMap(Effect.fromResult));
258
+ const withWriteTransaction = (operation, expected = () => false) => (effect) => Effect.withFiber((fiber) => {
259
+ const body = state.owner.withMutation(owner.transaction(storageResult(effect, expected))).pipe(Effect.provideService(SqlClient$1.SqlClient, sql), Effect.mapError((error) => isSqlError$1(error) ? storageError(operation)(error) : error));
260
+ return (fiber.getRef(References.TracerEnabled) && fiber.getRef(Tracer.Tracer) !== Tracer.nativeTracer ? body.pipe(Effect.tapError(annotateStorageError), Effect.withSpan("DoJournal.withWriteTransaction", { attributes: { operation } })) : body).pipe(Effect.flatMap(Effect.fromResult));
261
+ });
256
262
  const materialize = Effect.fnUntraced(function* (threadId, createdAt) {
257
263
  if (threadId.length > MAX_IDENTIFIER_LENGTH) return yield* DoStorageError.make({
258
264
  operation: "materialize thread",
@@ -315,8 +321,70 @@ const makeJournal = (sql, failpoint, maxStoredValueBytes, lifecycle, state) => E
315
321
  });
316
322
  return request;
317
323
  });
324
+ const appendNativeBody = Effect.fnUntraced(function* (request, first, last, batchInserted) {
325
+ const db = nativeDb;
326
+ if (yield* Effect.withFiber((fiber) => Effect.succeed(fiber.getRef(CurrentTransformer) !== void 0 || fiber.getRef(References.TracerEnabled) && fiber.getRef(Tracer.Tracer) !== Tracer.nativeTracer))) return false;
327
+ if (db === void 0 || request.executionWrites.length !== 1 || request.executionWrites[0]?.update.retire || request.executionWrites[0]?.update.facts.length !== 0) return false;
328
+ const workPlan = work.prepareClosedPair(request.threadId, first, last, request.records.map((record) => record.canonical));
329
+ if (workPlan === void 0) return false;
330
+ const native = yield* Effect.suspend(() => {
331
+ const query = (statement, operation) => Result.try({
332
+ try: () => {
333
+ const [text, parameters] = statement.compile();
334
+ return db.exec(text, ...parameters).toArray();
335
+ },
336
+ catch: (cause) => storageError(operation)(SqlError.make({ reason: classifySqliteError(cause, {
337
+ operation,
338
+ message: "Native canonical statement failed"
339
+ }) }))
340
+ });
341
+ return Effect.fromResult(Result.gen(function* () {
342
+ const captured = physical.cachedRecords(request.threadId, first, request.records);
343
+ if (captured === void 0) return void 0;
344
+ const physicalRecords = yield* captured;
345
+ const rows = yield* query(workPlan.advance, "advance native work frontier");
346
+ if (rows.length === 0) return void 0;
347
+ const closed = yield* workPlan.fold();
348
+ const executionPlans = executions.prepareCommit(request);
349
+ if (!batchInserted) yield* query(sql`INSERT INTO effect_agent_canonical_batches
350
+ (thread_id, batch_id, first_sequence, last_sequence, producer_id_json)
351
+ VALUES (${request.threadId}, ${JSON.stringify(request.batchId)}, ${first}, ${last}, ${request.producerIdJson})`, "insert canonical batch");
352
+ if ((yield* query(sql`INSERT INTO effect_agent_canonical_records ${sql.insert(physicalRecords.map((row) => row.columns))}
353
+ ON CONFLICT(thread_id, record_id) DO NOTHING RETURNING 1`, "insert canonical records")).length !== request.records.length) return yield* Result.fail(DoAppendConflict.make({
354
+ message: "A canonical record ID already exists.",
355
+ reason: "record-identity"
356
+ }));
357
+ const memberships = physicalRecords.flatMap((row) => row.memberships);
358
+ for (const group of chunked(memberships, Math.floor(MAX_BOUND_PARAMETERS / 3))) yield* query(sql`INSERT INTO effect_agent_record_runs ${sql.insert(group)}`, "index canonical Run membership");
359
+ for (const pointer of closed.pointers) yield* query(pointer, "index canonical pointers");
360
+ for (const plan of executionPlans) {
361
+ const changed = yield* query(plan.statement, "commit active execution");
362
+ const accepted = plan.accept(changed);
363
+ if (Result.isFailure(accepted)) return yield* Result.fail(DoAppendConflict.make({
364
+ reason: accepted.failure.reason,
365
+ message: "Active execution conflicts with the captured commit",
366
+ ...accepted.failure.actualTailSequence === void 0 ? {} : { actualTailSequence: accepted.failure.actualTailSequence }
367
+ }));
368
+ }
369
+ for (let index = 0; index < request.records.length; index++) {
370
+ const record = request.records[index];
371
+ recordCache.put(RecordRow.make({
372
+ ...physicalRecords[index].columns,
373
+ thread_id: request.threadId,
374
+ record_id: record.recordId,
375
+ batch_id: request.batchId,
376
+ record_json: record.recordJson,
377
+ sequence: CanonicalSequence.make(first + index)
378
+ }), record.recordBytes);
379
+ }
380
+ return rows;
381
+ }));
382
+ });
383
+ if (native === void 0) return false;
384
+ yield* threads.write(Effect.succeed(native));
385
+ return true;
386
+ });
318
387
  const appendPrepared = Effect.fnUntraced(function* (request) {
319
- const recordIds = request.records.map((record) => record.recordId);
320
388
  const threadRows = yield* getThread(request.threadId);
321
389
  const thread = threadRows.length === 1 ? threadRows[0] : void 0;
322
390
  if (thread === void 0) return yield* DoStorageCorruptionError.make({
@@ -324,19 +392,37 @@ const makeJournal = (sql, failpoint, maxStoredValueBytes, lifecycle, state) => E
324
392
  rowKey: request.threadId,
325
393
  message: `Expected exactly one row but found ${threadRows.length}.`
326
394
  });
327
- const prefix = recordCache.prefix(request.threadId, thread.tail_sequence);
328
- const replay = yield* executionConflict(executions.replay(request));
329
- if (replay !== void 0) return RawAppendResult.make(replay);
330
- const existing = prefix !== void 0 && !prefix.some((row) => row.batch_id === request.batchId) ? void 0 : yield* archives.replay(request.threadId, request.batchId);
331
- if (existing !== void 0) {
332
- if (existing.batch_json !== request.batchJson) return yield* DoAppendConflict.make({
333
- message: `Batch ${request.batchId} already exists with different canonical content.`,
334
- reason: "batch-content"
335
- });
336
- return RawAppendResult.make({
337
- firstSequence: existing.first_sequence,
338
- lastSequence: existing.last_sequence,
339
- replayed: true
395
+ const canInsert = request.records.length > 0 && request.expectedTailSequence === thread.tail_sequence && Number.isSafeInteger(thread.tail_sequence + request.records.length);
396
+ const inserted = canInsert ? yield* sql`INSERT INTO effect_agent_canonical_batches
397
+ (thread_id, batch_id, first_sequence, last_sequence, producer_id_json)
398
+ SELECT ${request.threadId}, ${JSON.stringify(request.batchId)},
399
+ ${thread.tail_sequence + 1}, ${thread.tail_sequence + request.records.length},
400
+ ${request.producerIdJson}
401
+ WHERE NOT EXISTS (
402
+ SELECT 1 FROM effect_agent_executions
403
+ WHERE thread_id=${request.threadId} AND last_commit_id=${request.batchId}
404
+ )
405
+ ON CONFLICT(thread_id, batch_id) DO NOTHING RETURNING 1`.pipe(Effect.mapError(storageError("insert canonical batch"))) : [];
406
+ if (inserted.length === 0) {
407
+ const replay = yield* executionConflict(executions.replay(request));
408
+ if (replay !== void 0) return AppendResult.make(replay);
409
+ const prefix = recordCache.prefix(request.threadId, thread.tail_sequence);
410
+ const existing = prefix !== void 0 && !prefix.some((row) => row.batch_id === request.batchId) ? void 0 : yield* archives.replay(request.threadId, request.batchId);
411
+ if (existing !== void 0) {
412
+ if (existing.batch_json !== request.batchJson) return yield* DoAppendConflict.make({
413
+ message: `Batch ${request.batchId} already exists with different canonical content.`,
414
+ reason: "batch-content"
415
+ });
416
+ return AppendResult.make({
417
+ firstSequence: existing.first_sequence,
418
+ lastSequence: existing.last_sequence,
419
+ replayed: true
420
+ });
421
+ }
422
+ if (canInsert) return yield* DoStorageCorruptionError.make({
423
+ table: "effect_agent_canonical_batches",
424
+ rowKey: request.threadId,
425
+ message: "Batch insertion conflicted without a durable replay receipt."
340
426
  });
341
427
  }
342
428
  yield* executionConflict(executions.rejectSuperseded(request));
@@ -345,42 +431,26 @@ const makeJournal = (sql, failpoint, maxStoredValueBytes, lifecycle, state) => E
345
431
  reason: "tail",
346
432
  actualTailSequence: thread.tail_sequence
347
433
  });
348
- const existingRecords = prefix === void 0 ? [] : prefix.filter((row) => recordIds.includes(row.record_id));
349
- for (const chunk of prefix === void 0 ? chunked(recordIds, 90) : []) {
350
- const existingRecordRows = yield* sql`
351
- SELECT
352
- thread_id,
353
- sequence,
354
- record_id,
355
- ${recordBatchId(sql)} AS batch_id,
356
- ${recordJson} AS record_json
357
- FROM effect_agent_canonical_records
358
- WHERE thread_id = ${request.threadId}
359
- AND record_id IN ${sql.in(chunk.map((id) => JSON.stringify(id)))}
360
- `.pipe(Effect.mapError(storageError("check canonical record identities")));
361
- existingRecords.push(...yield* decodeRows(Schema.Array(RecordRow), "effect_agent_canonical_records", `${request.threadId}/record_ids`, existingRecordRows));
362
- }
363
- if (existingRecords.length > 0) return yield* DoAppendConflict.make({
364
- message: `Canonical record ID ${existingRecords[0].record_id} already exists.`,
365
- reason: "record-identity"
366
- });
367
- const firstSequence = yield* Schema.decodeEffect(CanonicalSequence)(thread.tail_sequence + (request.records.length === 0 ? 0 : 1)).pipe(Effect.mapError((error) => DoStorageError.make({
368
- cause: error,
369
- operation: "append canonical batch",
370
- message: error.message
371
- })));
372
- const lastSequence = yield* Schema.decodeEffect(CanonicalSequence)(thread.tail_sequence + request.records.length).pipe(Effect.mapError((error) => DoStorageError.make({
434
+ const result = yield* Schema.decodeEffect(AppendResult)({
435
+ firstSequence: thread.tail_sequence + (request.records.length === 0 ? 0 : 1),
436
+ lastSequence: thread.tail_sequence + request.records.length,
437
+ replayed: false
438
+ }).pipe(Effect.mapError((error) => DoStorageError.make({
373
439
  cause: error,
374
440
  operation: "append canonical batch",
375
441
  message: error.message
376
442
  })));
443
+ const { firstSequence, lastSequence } = result;
377
444
  yield* executionConflict(executions.validate(request));
378
- const result = RawAppendResult.make({
379
- firstSequence,
380
- lastSequence,
381
- replayed: false
382
- });
383
- if (request.records.length > 0) yield* sql`
445
+ if (yield* appendNativeBody(request, firstSequence, lastSequence, inserted.length > 0)) {
446
+ yield* progress.committed("canonical").pipe(Effect.catchCause((cause) => Effect.failCause(Cause.map(cause, (error) => DoStorageError.make({
447
+ operation: "enroll canonical progress",
448
+ message: error.message,
449
+ cause: error
450
+ })))));
451
+ return result;
452
+ }
453
+ if (request.records.length > 0 && inserted.length === 0) yield* sql`
384
454
  INSERT INTO effect_agent_canonical_batches (
385
455
  thread_id,
386
456
  batch_id,
@@ -403,12 +473,17 @@ const makeJournal = (sql, failpoint, maxStoredValueBytes, lifecycle, state) => E
403
473
  memberships
404
474
  }));
405
475
  for (const group of chunked(records, Math.floor(MAX_BOUND_PARAMETERS / 18))) {
406
- yield* sql`INSERT INTO effect_agent_canonical_records ${sql.insert(group.map(({ row }) => row))}`.pipe(Effect.mapError(storageError("insert canonical records")));
476
+ if ((yield* sql`INSERT INTO effect_agent_canonical_records ${sql.insert(group.map(({ row }) => row))}
477
+ ON CONFLICT(thread_id, record_id) DO NOTHING RETURNING 1`.pipe(Effect.mapError(storageError("insert canonical records")))).length !== group.length) return yield* DoAppendConflict.make({
478
+ message: "A canonical record ID already exists.",
479
+ reason: "record-identity"
480
+ });
407
481
  const memberships = group.flatMap(({ memberships }) => memberships);
408
482
  for (const members of chunked(memberships, Math.floor(MAX_BOUND_PARAMETERS / 3))) yield* sql`INSERT INTO effect_agent_record_runs ${sql.insert(members)}`.pipe(Effect.mapError(storageError("index canonical Run membership")));
409
483
  for (const { row, record } of group) {
410
484
  recordCache.put(RecordRow.make({
411
485
  ...row,
486
+ thread_id: request.threadId,
412
487
  record_id: record.recordId,
413
488
  batch_id: request.batchId,
414
489
  record_json: record.recordJson,
@@ -475,7 +550,7 @@ const makeJournal = (sql, failpoint, maxStoredValueBytes, lifecycle, state) => E
475
550
  actualTailSequence: observed.tail_sequence
476
551
  });
477
552
  }
478
- return yield* withWriteTransaction("append transaction", isAppendContention)(appendPrepared(request));
553
+ return yield* withWriteTransaction("append transaction")(appendPrepared(request));
479
554
  }, withStorageSpan("DoJournal.append", isAppendContention));
480
555
  const read = Effect.fnUntraced(function* (request) {
481
556
  const thread = (yield* getThread(request.threadId))[0];
@@ -755,4 +830,4 @@ const initializeDoJournal = ensureCurrentStorage;
755
830
  //#endregion
756
831
  export { isAppendContention as a, initializeDoJournal as i, RawReadRequest as n, withStorageSpan as o, decodeRows as r, RawCheckpoint as t };
757
832
 
758
- //# sourceMappingURL=do-journal-_MdLGTd3.mjs.map
833
+ //# sourceMappingURL=do-journal-7P3DUBz0.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"do-journal-7P3DUBz0.mjs","names":["isSqlError","SqlClient"],"sources":["../src/internal/storage-span.ts","../src/internal/do-journal.ts"],"sourcesContent":["import { Effect, Predicate, References, Result, Tracer } from \"effect\";\n\ntype Tagged = { readonly _tag: string; readonly reason?: unknown };\n\nexport const isAppendContention = (error: Tagged): boolean =>\n (error._tag === \"DoAppendConflict\" || error._tag === \"AppendConflict\") && error.reason === \"tail\";\n\n/** Expected refusals remain typed at the port, but are outcomes of the storage operation. */\nexport const storageResult = <A, E extends Tagged, R>(\n effect: Effect.Effect<A, E, R>,\n expected: (error: E) => boolean,\n) =>\n effect.pipe(\n Effect.map(Result.succeed),\n Effect.catchIf(expected, (error) => Effect.succeed(Result.fail(error))),\n );\n\n/** Only error discriminants enter telemetry; messages, SQL, identities and payloads do not. */\nexport const annotateStorageError = (error: Tagged) =>\n Effect.annotateCurrentSpan({\n \"error.type\": error._tag,\n ...(Predicate.hasProperty(error, \"cause\") &&\n Predicate.hasProperty(error.cause, \"_tag\") &&\n typeof error.cause._tag === \"string\"\n ? { \"error.cause.type\": error.cause._tag }\n : {}),\n });\n\nexport const withStorageSpan = (\n name: string,\n expected: (error: Tagged) => boolean = () => false,\n) => {\n const span = Effect.withSpan(name);\n\n return <A, E extends Tagged, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>\n Effect.withFiber((fiber) =>\n !fiber.getRef(References.TracerEnabled) || fiber.getRef(Tracer.Tracer) === Tracer.nativeTracer\n ? effect\n : storageResult(effect, expected).pipe(\n Effect.tap((result) =>\n Result.isFailure(result)\n ? Effect.annotateCurrentSpan({ \"storage.outcome\": result.failure._tag })\n : Effect.void,\n ),\n Effect.tapError(annotateStorageError),\n span,\n Effect.flatMap(Effect.fromResult),\n ),\n );\n};\n","import { SqliteClient } from \"@effect/sql-sqlite-do\";\nimport {\n makeCanonicalStorage,\n recordBatchId,\n} from \"@yielded/agent-storage-sql/sql-canonical-storage\";\nimport type { RawAppendRequest } from \"@yielded/agent-storage-sql/sql-journal\";\nimport { makeSqlExecutions } from \"@yielded/agent-storage-sql/sql-journal\";\nimport {\n makeSqlLifecyclePublication,\n type SqlLifecycleRetainMany,\n SqlLifecycleSource,\n SqlLifecycleRetainer,\n} from \"@yielded/agent-storage-sql/sql-lifecycle-publication\";\nimport { SqlStorageProgress } from \"@yielded/agent-storage-sql/sql-storage-progress\";\nimport {\n canonicalRecordJson,\n makeSqlThreadArchiveRange,\n} from \"@yielded/agent-storage-sql/sql-thread-archive-range\";\nimport { makeSqlThreadWork } from \"@yielded/agent-storage-sql/sql-thread-work\";\nimport { ThreadId } from \"@yielded/agent/identifiers\";\nimport {\n LifecyclePublicationFact,\n LifecyclePublicationError,\n} from \"@yielded/agent/lifecycle-publication\";\nimport { BatchId, CanonicalRecord, CanonicalSequence } from \"@yielded/agent/records\";\nimport { SqlStorageOwner } from \"@yielded/agent/sql-memory-store\";\nimport {\n type AppendConflict,\n AppendResult,\n ThreadStoreDiagnostic,\n ThreadStoreError,\n} from \"@yielded/agent/thread-store\";\nimport {\n Cause,\n Clock,\n Effect,\n Option,\n Predicate,\n References,\n Result,\n Schema,\n Stream,\n Tracer,\n} from \"effect\";\nimport * as SqlClient from \"effect/sql/SqlClient\";\nimport { classifySqliteError, SqlError } from \"effect/sql/SqlError\";\nimport { CurrentTransformer, type Statement } from \"effect/sql/Statement\";\n\nimport {\n type DoStorageFailpointError,\n DoAppendConflict,\n DoCheckpointConflict,\n DoStorageCorruptionError,\n DoStorageError,\n DoValueBoundExceeded,\n type DoStorageFailpointLocation,\n} from \"../DoStorageError.ts\";\nimport { isDefaultDoStorageFailpoint } from \"../DoStorageFailpoint.ts\";\nimport { ensureDoStorageLayout } from \"./migrations.ts\";\nimport { ownedState, ownedRows, type OwnedState } from \"./owned-state.ts\";\nimport {\n isAppendContention,\n annotateStorageError,\n storageResult,\n withStorageSpan,\n} from \"./storage-span.ts\";\n\n/**\n * Static schema ceiling for stored text columns. Writes are bounded in BYTES by the\n * configured `maxStoredValueBytes` (always ≤ 2,000,000); UTF-8 byte length is never smaller\n * than UTF-16 string length, so any value that passed the byte bound also passes this\n * decode-side character ceiling.\n */\nconst BoundedStoredText = Schema.String.check(Schema.isMaxLength(2_000_000));\nconst BoundedIdentifier = Schema.NonEmptyString.check(Schema.isMaxLength(1024));\nconst MAX_IDENTIFIER_LENGTH = 1_024;\nconst MAX_READ_PAGE_JSON_BYTES = 4 * 1024 * 1024;\n// A shared isolate budget permits multi-page hydration without multiplying its\n// retained payload by the number of resident Durable Object instances.\nconst MAX_RECORD_CACHE_JSON_BYTES = 8 * 1024 * 1024;\nconst MAX_RECORD_CACHE_ENTRIES = 4_096;\n/** Durable Object SQL storage allows at most 100 bound parameters per statement. */\nconst MAX_BOUND_PARAMETERS = 100;\nconst isSqlError = Schema.is(SqlError);\n\nconst isNativeClient = Schema.is(\n Schema.declare<SqliteClient.SqliteClient>((value): value is SqliteClient.SqliteClient =>\n Predicate.hasProperty(value, SqliteClient.TypeId),\n ),\n);\n\nconst storedTextBytes = (value: string): number => new TextEncoder().encode(value).byteLength;\n\nconst chunked = <A>(values: ReadonlyArray<A>, size: number): Array<ReadonlyArray<A>> => {\n const chunks: Array<ReadonlyArray<A>> = [];\n\n for (let index = 0; index < values.length; index += size) {\n chunks.push(values.slice(index, index + size));\n }\n\n return chunks;\n};\n\nclass ThreadRow extends Schema.Class<ThreadRow>(\"ThreadRow\")({\n thread_id: BoundedIdentifier,\n created_at: Schema.NullOr(Schema.NonEmptyString.check(Schema.isMaxLength(128))),\n tail_sequence: CanonicalSequence,\n}) {}\n\nconst threadRows = ownedRows(\n ThreadRow,\n \"effect_agent_threads\",\n (row) => row.thread_id,\n \"thread_id\",\n);\n\nclass RecordRow extends Schema.Class<RecordRow>(\"RecordRow\")({\n batch_id: Schema.fromJsonString(BatchId.check(Schema.isMaxLength(MAX_IDENTIFIER_LENGTH))),\n thread_id: ThreadId.check(Schema.isMaxLength(MAX_IDENTIFIER_LENGTH)),\n record_id: Schema.fromJsonString(BoundedIdentifier),\n record_json: BoundedStoredText,\n sequence: CanonicalSequence,\n}) {}\n\ninterface RecordCacheEntry {\n readonly row: RecordRow;\n readonly bytes: number;\n readonly evict: () => void;\n}\n\n// Entries retain only their local row map, never a storage owner or incarnation.\nconst isolateRecords = new Map<RecordCacheEntry, true>();\nlet isolateRecordBytes = 0;\n\nconst releaseRecord = (entry: RecordCacheEntry) => {\n if (!isolateRecords.delete(entry)) return;\n isolateRecordBytes -= entry.bytes;\n entry.evict();\n};\n\nconst recordCaches = new WeakMap<OwnedState, ReturnType<typeof makeRecordCache>>();\n\nconst makeRecordCache = () => {\n const records = new Map<string, RecordCacheEntry>();\n const key = (thread: string, sequence: number) => JSON.stringify([thread, sequence]);\n\n return {\n clear: () => {\n for (const entry of records.values()) releaseRecord(entry);\n },\n get: (thread: string, sequence: number) => records.get(key(thread, sequence))?.row,\n prefix: (thread: string, through: number): ReadonlyArray<RecordRow> | undefined => {\n const prefix: Array<RecordRow> = [];\n\n for (let sequence = 1; sequence <= through; sequence++) {\n const row = records.get(key(thread, sequence));\n\n if (row === undefined) return undefined;\n prefix.push(row.row);\n }\n\n return prefix;\n },\n put: (row: RecordRow, capturedBytes?: number) => {\n const id = key(row.thread_id, row.sequence);\n const prior = records.get(id);\n\n if (prior?.row === row) return;\n const size = capturedBytes ?? storedTextBytes(row.record_json);\n\n if (prior !== undefined) releaseRecord(prior);\n\n const entry: RecordCacheEntry = {\n row,\n bytes: size,\n evict: () => records.delete(id),\n };\n\n records.set(id, entry);\n isolateRecords.set(entry, true);\n isolateRecordBytes += size;\n while (\n isolateRecordBytes > MAX_RECORD_CACHE_JSON_BYTES ||\n isolateRecords.size > MAX_RECORD_CACHE_ENTRIES\n ) {\n const oldest = isolateRecords.keys().next().value;\n\n if (oldest === undefined) break;\n releaseRecord(oldest);\n }\n },\n };\n};\n\nconst ReadPlanRow = Schema.Struct({\n sequence: CanonicalSequence,\n record_json_bytes: Schema.Natural.check(Schema.isLessThanOrEqualTo(MAX_READ_PAGE_JSON_BYTES)),\n});\n\ntype ReadPage = [typeof ReadPlanRow.Type, ...Array<typeof ReadPlanRow.Type>];\n\nclass CheckpointRow extends Schema.Class<CheckpointRow>(\"CheckpointRow\")({\n checkpoint_json: BoundedStoredText,\n thread_id: BoundedIdentifier,\n through_sequence: CanonicalSequence,\n}) {}\n\nconst LifecycleCursorRow = Schema.Struct({\n thread_id: BoundedIdentifier,\n through_sequence: CanonicalSequence,\n});\n\nconst lifecycleCursorRows = ownedRows(\n LifecycleCursorRow,\n \"effect_agent_lifecycle_cursors\",\n (row) => row.thread_id,\n \"thread_id\",\n);\n\nconst initializedLifecycleSources = new WeakSet<OwnedState>();\n\nexport type { RawAppendRequest } from \"@yielded/agent-storage-sql/sql-journal\";\n\nexport class RawReadRequest extends Schema.Class<RawReadRequest>(\n \"@effect-agent/storage-cloudflare/RawReadRequest\",\n)({\n threadId: BoundedIdentifier,\n fromSequenceExclusive: CanonicalSequence,\n limit: Schema.Int.check(Schema.isGreaterThan(0), Schema.isLessThanOrEqualTo(1_024)),\n}) {}\n\nexport class RawCheckpoint extends Schema.Class<RawCheckpoint>(\n \"@effect-agent/storage-cloudflare/RawCheckpoint\",\n)({\n checkpointJson: BoundedStoredText,\n threadId: BoundedIdentifier,\n throughSequence: CanonicalSequence,\n}) {}\n\ntype AppendError =\n | ThreadStoreError\n | DoAppendConflict\n | DoStorageCorruptionError\n | DoStorageError\n | DoStorageFailpointError\n | DoValueBoundExceeded;\n\ntype CheckpointError =\n | DoCheckpointConflict\n | DoStorageCorruptionError\n | DoStorageError\n | DoValueBoundExceeded;\n\ntype DoJournalFailpoint = (\n location: DoStorageFailpointLocation,\n) => Effect.Effect<void, DoStorageFailpointError>;\n\nconst noFailpoint: DoJournalFailpoint = () => Effect.void;\n\nconst storageError =\n (operation: string) =>\n (error: SqlError): DoStorageError =>\n DoStorageError.make({\n cause: error,\n operation,\n message: error.message,\n diagnostic: ThreadStoreDiagnostic.make({\n causeTag: error._tag,\n operation,\n issueTag: error.reason._tag,\n }),\n });\n\n/** Decode raw Durable Object SQLite rows against a Schema, reporting failures as typed corruption. */\nexport const decodeRows = <A, I>(\n schema: Schema.Codec<ReadonlyArray<A>, ReadonlyArray<I>>,\n table: string,\n rowKey: string,\n rows: unknown,\n decoder: string = table,\n): Effect.Effect<ReadonlyArray<A>, DoStorageCorruptionError> =>\n Schema.decodeUnknownEffect(schema)(rows).pipe(\n Effect.mapError((error) =>\n DoStorageCorruptionError.make({\n table,\n rowKey,\n message: \"Stored rows do not satisfy the storage schema\",\n diagnostic: ThreadStoreDiagnostic.make({\n causeTag: error._tag,\n operation: \"decode storage rows\",\n decoder,\n issueTag: error.issue._tag,\n }),\n }),\n ),\n Effect.tapError((error) =>\n Effect.annotateCurrentSpan({\n \"storage.failure.operation\": error.diagnostic?.operation,\n \"storage.failure.cause\": error.diagnostic?.causeTag,\n \"storage.failure.decoder\": decoder,\n \"storage.failure.issue\": error.diagnostic?.issueTag,\n }),\n ),\n );\n\n/** Decode exactly one raw row against a Schema, reporting failures as typed corruption. */\nexport const decodeSingleRow = <A, I>(\n schema: Schema.Codec<ReadonlyArray<A>, ReadonlyArray<I>>,\n table: string,\n rowKey: string,\n rows: unknown,\n): Effect.Effect<A, DoStorageCorruptionError> =>\n decodeRows(schema, table, rowKey, rows).pipe(\n Effect.flatMap((decoded) =>\n decoded.length === 1\n ? Effect.succeed(decoded[0])\n : Effect.fail(\n DoStorageCorruptionError.make({\n table,\n rowKey,\n message: `Expected exactly one row but found ${decoded.length}.`,\n }),\n ),\n ),\n );\n\n/** Open a validated layout and bind this Object's storage owner. */\nconst ensureCurrentStorage = Effect.fnUntraced(function* (\n sql: SqlClient.SqlClient,\n failpoint: DoJournalFailpoint = noFailpoint,\n maxStoredValueBytes: number,\n) {\n const state = yield* ownedState(sql);\n const owner = (yield* SqlStorageOwner) ?? state;\n\n yield* ensureDoStorageLayout().pipe(\n Effect.provideService(SqlStorageOwner, owner),\n Effect.provideService(SqlClient.SqlClient, sql),\n );\n\n let journal: DoJournal | undefined;\n\n const lifecycle = yield* makeSqlLifecyclePublication(undefined, maxStoredValueBytes).pipe(\n Effect.provideService(SqlStorageOwner, owner),\n Effect.provideService(SqlLifecycleSource, {\n beforeRetain: (threadId) =>\n Effect.suspend(() =>\n journal === undefined\n ? Effect.fail(LifecyclePublicationError.make({ reason: \"unavailable\" }))\n : journal.flushCanonical(threadId),\n ),\n beforePending: Effect.suspend(() =>\n journal === undefined\n ? Effect.fail(LifecyclePublicationError.make({ reason: \"unavailable\" }))\n : journal.flushPublications(),\n ),\n }),\n Effect.provideService(SqlClient.SqlClient, sql),\n Effect.mapError((cause) =>\n DoStorageError.make({\n operation: \"initialize lifecycle publication\",\n message: \"Native publication storage unavailable\",\n cause,\n }),\n ),\n );\n\n journal = yield* makeJournal(sql, failpoint, maxStoredValueBytes, lifecycle, state).pipe(\n Effect.provideService(SqlStorageOwner, owner),\n );\n if (lifecycle !== undefined) yield* journal.initializeLifecycleSource();\n\n return journal;\n});\n\nconst makeJournal = Effect.fnUntraced(function* (\n sql: SqlClient.SqlClient,\n failpoint: DoJournalFailpoint,\n maxStoredValueBytes: number,\n lifecycle: Effect.Success<ReturnType<typeof makeSqlLifecyclePublication>>,\n state: OwnedState,\n) {\n const owner = (yield* SqlStorageOwner) ?? state;\n const progress = yield* SqlStorageProgress;\n\n const nativeDb =\n isNativeClient(sql) &&\n sql.config.transformQueryNames === undefined &&\n sql.config.transformResultNames === undefined &&\n isDefaultDoStorageFailpoint(failpoint)\n ? sql.config.storage?.sql\n : undefined;\n\n const work = yield* makeSqlThreadWork({\n read: (body) => state.read(body),\n write: (body) =>\n owner.transaction(body.pipe(Effect.tap(() => state.invalidate))).pipe(\n Effect.provideService(SqlClient.SqlClient, sql),\n Effect.catchTag(\"SqlError\", (cause) =>\n ThreadStoreError.make({\n operation: \"rebuild Thread work\",\n message: \"Thread work transaction failed\",\n cause,\n }),\n ),\n ),\n }).pipe(Effect.provideService(SqlClient.SqlClient, sql));\n\n yield* Effect.acquireRelease(\n Effect.sync(() => state.invalidators.add(work.invalidate)),\n () => Effect.sync(() => state.invalidators.delete(work.invalidate)),\n );\n\n const workFailure = (cause: unknown) =>\n DoStorageError.make({\n operation: \"publish Thread work\",\n message: \"Canonical work metadata could not be published\",\n cause,\n });\n\n const executions = yield* makeSqlExecutions().pipe(\n Effect.provideService(SqlClient.SqlClient, sql),\n );\n\n const executionConflict = <A>(effect: Effect.Effect<A, AppendConflict | ThreadStoreError>) =>\n effect.pipe(\n Effect.catchTag(\"AppendConflict\", (error) =>\n Effect.fail(\n DoAppendConflict.make({\n reason: error.reason,\n message: \"Active execution conflicts with the captured commit\",\n ...(error.actualTailSequence === undefined\n ? {}\n : { actualTailSequence: error.actualTailSequence }),\n }),\n ),\n ),\n );\n\n const threads = threadRows(state, sql);\n const cursors = lifecycleCursorRows(state, sql);\n let records = recordCaches.get(state);\n\n if (records === undefined) {\n records = makeRecordCache();\n recordCaches.set(state, records);\n state.invalidators.add(records.clear);\n }\n const recordCache = records;\n\n const archives = yield* makeSqlThreadArchiveRange({\n read: (body) => state.read(body),\n write: (body) =>\n owner.transaction(body.pipe(Effect.tap(() => state.invalidate))).pipe(\n Effect.provideService(SqlClient.SqlClient, sql),\n Effect.catchTag(\"SqlError\", (cause) =>\n ThreadStoreError.make({\n operation: \"archive write\",\n message: \"Archive transaction failed.\",\n cause,\n }),\n ),\n ),\n }).pipe(Effect.provideService(SqlClient.SqlClient, sql));\n\n const recordJson = canonicalRecordJson(sql);\n\n const physical = yield* makeCanonicalStorage().pipe(\n Effect.provideService(SqlClient.SqlClient, sql),\n );\n\n /** Typed pre-write refusal for any single value over the configured byte bound. */\n const checkValueBound = (\n operation: string,\n value: string,\n actualBytes = storedTextBytes(value),\n ): Effect.Effect<void, DoValueBoundExceeded> => {\n return actualBytes > maxStoredValueBytes\n ? Effect.fail(\n DoValueBoundExceeded.make({\n actualBytes,\n maxBytes: maxStoredValueBytes,\n operation,\n }),\n )\n : Effect.void;\n };\n\n /**\n * Runs one journal write transaction on the Durable Object storage-backed\n * `withTransaction` (`ctx.storage.transaction()` under the hood). Within one Durable\n * Object there is exactly ONE writer, so the Node `BEGIN IMMEDIATE` + busy-retry +\n * `SqliteWriteContention` machinery has no analogue here and is deliberately absent.\n * The shared local owner gate covers the transaction and acknowledgement.\n *\n * Journal write transactions are always top level: the Durable Object client rejects\n * nested transactions, so new journal operations must not wrap this helper inside another\n * transaction. Expected outcomes may be captured only when every such refusal precedes\n * mutation; real faults still roll back. Successful preflight reads never authorize a write.\n */\n const withWriteTransaction =\n (operation: string, expected: (error: { readonly _tag: string }) => boolean = () => false) =>\n <A, E extends { readonly _tag: string }>(\n effect: Effect.Effect<A, E>,\n ): Effect.Effect<A, E | DoStorageError> =>\n Effect.withFiber((fiber) => {\n const body = state.owner\n .withMutation(owner.transaction(storageResult(effect, expected)))\n .pipe(\n Effect.provideService(SqlClient.SqlClient, sql),\n Effect.mapError((error) =>\n isSqlError(error) ? storageError(operation)(error) : error,\n ),\n );\n\n const traced =\n fiber.getRef(References.TracerEnabled) &&\n fiber.getRef(Tracer.Tracer) !== Tracer.nativeTracer;\n\n return (\n traced\n ? body.pipe(\n Effect.tapError(annotateStorageError),\n Effect.withSpan(\"DoJournal.withWriteTransaction\", { attributes: { operation } }),\n )\n : body\n ).pipe(Effect.flatMap(Effect.fromResult));\n });\n\n const materialize = Effect.fnUntraced(function* (\n threadId: string,\n createdAt: string,\n ): Effect.fn.Return<void, DoStorageCorruptionError | DoStorageError | DoValueBoundExceeded> {\n if (threadId.length > MAX_IDENTIFIER_LENGTH) {\n return yield* DoStorageError.make({\n operation: \"materialize thread\",\n message: \"Thread identity exceeds the Durable Object storage bounds.\",\n });\n }\n // Refusals and idempotent materialization need no transaction. Recheck before writes.\n const current = yield* getThread(threadId);\n\n if (current.length === 1) {\n return;\n }\n yield* withWriteTransaction(\"materialize transaction\")(\n Effect.gen(function* () {\n const existingRows = yield* getThread(threadId);\n\n const existing = yield* decodeRows(\n Schema.Array(ThreadRow),\n \"effect_agent_threads\",\n threadId,\n existingRows,\n );\n\n if (existing.length > 1) {\n return yield* DoStorageCorruptionError.make({\n table: \"effect_agent_threads\",\n rowKey: threadId,\n message: \"A thread primary key returned more than one row.\",\n });\n }\n if (existing.length === 0) {\n yield* sql`\n INSERT INTO effect_agent_threads (\n thread_id,\n created_at,\n tail_sequence\n ) VALUES (\n ${threadId},\n ${createdAt},\n 0\n ) ON CONFLICT(thread_id) DO UPDATE SET created_at=excluded.created_at\n WHERE effect_agent_threads.created_at IS NULL\n RETURNING *`.pipe(\n threads.write,\n Effect.mapError((error) =>\n error._tag === \"SqlError\" ? storageError(\"materialize thread\")(error) : error,\n ),\n );\n\n if (lifecycle !== undefined)\n yield* sql`INSERT INTO effect_agent_lifecycle_cursors (thread_id, through_sequence)\n VALUES (${threadId}, 0) RETURNING *`.pipe(\n cursors.write,\n Effect.mapError((error) =>\n error._tag === \"SqlError\"\n ? storageError(\"initialize lifecycle source\")(error)\n : error,\n ),\n );\n\n return;\n }\n }),\n );\n }, withStorageSpan(\"DoJournal.materialize\"));\n\n const getThread = Effect.fnUntraced(function* (threadId: string) {\n return (yield* threads\n .by(\"thread_id\", threadId)\n .pipe(\n Effect.mapError((error) =>\n error._tag === \"SqlError\" ? storageError(\"read thread\")(error) : error,\n ),\n )).filter((row) => row.thread_id === threadId && row.created_at !== null);\n });\n\n const hasRecord = Effect.fnUntraced(function* (threadId: string, recordId: string) {\n const thread = (yield* getThread(threadId))[0];\n const prefix = recordCache.prefix(threadId, thread?.tail_sequence ?? 0);\n\n if (prefix !== undefined) return prefix.some((row) => row.record_id === recordId);\n\n const rows = yield* sql`SELECT record_id FROM effect_agent_canonical_records\n WHERE thread_id = ${threadId} AND record_id = ${JSON.stringify(recordId)}`.pipe(\n Effect.mapError(storageError(\"read canonical record identity\")),\n );\n\n return rows.length > 0;\n });\n\n const prepareAppend = Effect.fnUntraced(function* (request: RawAppendRequest) {\n if (\n request.threadId.length > MAX_IDENTIFIER_LENGTH ||\n request.batchId.length > MAX_IDENTIFIER_LENGTH ||\n request.executionWrites.some(\n ({ update }) => update.checkpoint.progress.runId.length > MAX_IDENTIFIER_LENGTH,\n ) ||\n request.records.some((record) => record.recordId.length > MAX_IDENTIFIER_LENGTH)\n ) {\n return yield* DoStorageError.make({\n operation: \"append canonical batch\",\n message: \"Canonical identifiers exceed the Durable Object storage bounds.\",\n });\n }\n // Archives store the full batch as one value, so their ceiling also bounds hot appends.\n // The trusted append SPI pairs captured JSON with its exact UTF-8 width.\n yield* checkValueBound(\"append canonical batch\", request.batchJson, request.batchBytes);\n for (const record of request.records) {\n if (record.recordBytes > maxStoredValueBytes)\n return yield* DoValueBoundExceeded.make({\n actualBytes: record.recordBytes,\n maxBytes: maxStoredValueBytes,\n operation: \"append canonical record\",\n });\n }\n for (const write of request.executionWrites)\n yield* checkValueBound(\"commit active execution\", write.json, write.bytes);\n\n const recordIds: Array<string> = request.records.map((record) => record.recordId);\n\n if (new Set(recordIds).size !== recordIds.length) {\n return yield* DoAppendConflict.make({\n message: `Batch ${request.batchId} contains duplicate canonical record IDs.`,\n reason: \"record-identity\",\n });\n }\n\n return request;\n });\n\n // Replay and progress validation precede this body in the same owner transaction.\n // A miss makes no native change; the ordinary path reuses any tentative batch.\n // After advancing the frontier, failures must roll back rather than fall back.\n const appendNativeBody = Effect.fnUntraced(function* (\n request: RawAppendRequest,\n first: CanonicalSequence,\n last: CanonicalSequence,\n batchInserted: boolean,\n ) {\n const db = nativeDb;\n\n // Effectful statement transformers and exported SQL spans retain the ordinary path.\n const ordinaryStatement = yield* Effect.withFiber((fiber) =>\n Effect.succeed(\n fiber.getRef(CurrentTransformer) !== undefined ||\n (fiber.getRef(References.TracerEnabled) &&\n fiber.getRef(Tracer.Tracer) !== Tracer.nativeTracer),\n ),\n );\n\n if (ordinaryStatement) return false;\n if (\n db === undefined ||\n request.executionWrites.length !== 1 ||\n request.executionWrites[0]?.update.retire ||\n request.executionWrites[0]?.update.facts.length !== 0\n )\n return false;\n\n const workPlan = work.prepareClosedPair(\n request.threadId,\n first,\n last,\n request.records.map((record) => record.canonical),\n );\n\n if (workPlan === undefined) return false;\n\n const native = yield* Effect.suspend(() => {\n const query = (statement: Statement<object>, operation: string) =>\n Result.try({\n try: () => {\n const [text, parameters] = statement.compile();\n\n return db.exec(text, ...parameters).toArray();\n },\n catch: (cause) =>\n storageError(operation)(\n SqlError.make({\n reason: classifySqliteError(cause, {\n operation,\n message: \"Native canonical statement failed\",\n }),\n }),\n ),\n });\n\n return Effect.fromResult(\n Result.gen(function* () {\n const captured = physical.cachedRecords(request.threadId, first, request.records);\n\n if (captured === undefined) return undefined;\n const physicalRecords = yield* captured;\n const rows = yield* query(workPlan.advance, \"advance native work frontier\");\n\n // A zero-row predicate miss has made no change. A successful update\n // must never fall back: every later refusal rolls back this transaction.\n if (rows.length === 0) return undefined;\n const closed = yield* workPlan.fold();\n const executionPlans = executions.prepareCommit(request);\n\n if (!batchInserted)\n yield* query(\n sql`INSERT INTO effect_agent_canonical_batches\n (thread_id, batch_id, first_sequence, last_sequence, producer_id_json)\n VALUES (${request.threadId}, ${JSON.stringify(request.batchId)}, ${first}, ${last}, ${request.producerIdJson})`,\n \"insert canonical batch\",\n );\n\n const inserted = yield* query(\n sql`INSERT INTO effect_agent_canonical_records ${sql.insert(physicalRecords.map((row) => row.columns))}\n ON CONFLICT(thread_id, record_id) DO NOTHING RETURNING 1`,\n \"insert canonical records\",\n );\n\n if (inserted.length !== request.records.length)\n return yield* Result.fail(\n DoAppendConflict.make({\n message: \"A canonical record ID already exists.\",\n reason: \"record-identity\",\n }),\n );\n const memberships = physicalRecords.flatMap((row) => row.memberships);\n\n for (const group of chunked(memberships, Math.floor(MAX_BOUND_PARAMETERS / 3)))\n yield* query(\n sql`INSERT INTO effect_agent_record_runs ${sql.insert(group)}`,\n \"index canonical Run membership\",\n );\n for (const pointer of closed.pointers) yield* query(pointer, \"index canonical pointers\");\n\n for (const plan of executionPlans) {\n const changed = yield* query(plan.statement, \"commit active execution\");\n const accepted = plan.accept(changed);\n\n if (Result.isFailure(accepted))\n return yield* Result.fail(\n DoAppendConflict.make({\n reason: accepted.failure.reason,\n message: \"Active execution conflicts with the captured commit\",\n ...(accepted.failure.actualTailSequence === undefined\n ? {}\n : { actualTailSequence: accepted.failure.actualTailSequence }),\n }),\n );\n }\n for (let index = 0; index < request.records.length; index++) {\n const record = request.records[index]!;\n\n recordCache.put(\n RecordRow.make({\n ...physicalRecords[index]!.columns,\n thread_id: request.threadId,\n record_id: record.recordId,\n batch_id: request.batchId,\n record_json: record.recordJson,\n sequence: CanonicalSequence.make(first + index),\n }),\n record.recordBytes,\n );\n }\n\n return rows;\n }),\n );\n });\n\n if (native === undefined) return false;\n yield* threads.write(Effect.succeed(native));\n\n return true;\n });\n\n const appendPrepared = Effect.fnUntraced(function* (\n request: RawAppendRequest,\n ): Effect.fn.Return<AppendResult, AppendError> {\n const threadRows = yield* getThread(request.threadId);\n\n const thread = threadRows.length === 1 ? threadRows[0] : undefined;\n\n if (thread === undefined)\n return yield* DoStorageCorruptionError.make({\n table: \"effect_agent_threads\",\n rowKey: request.threadId,\n message: `Expected exactly one row but found ${threadRows.length}.`,\n });\n\n // A fresh canonical batch key and absence of an active receipt prove the two\n // negative replay lookups in the same statement that retains the batch header.\n // Keep the read-first path for stale/overflowed coordinates so exact retries\n // still precede sequence and tail refusals.\n const canInsert =\n request.records.length > 0 &&\n request.expectedTailSequence === thread.tail_sequence &&\n Number.isSafeInteger(thread.tail_sequence + request.records.length);\n\n const inserted = canInsert\n ? yield* sql`INSERT INTO effect_agent_canonical_batches\n (thread_id, batch_id, first_sequence, last_sequence, producer_id_json)\n SELECT ${request.threadId}, ${JSON.stringify(request.batchId)},\n ${thread.tail_sequence + 1}, ${thread.tail_sequence + request.records.length},\n ${request.producerIdJson}\n WHERE NOT EXISTS (\n SELECT 1 FROM effect_agent_executions\n WHERE thread_id=${request.threadId} AND last_commit_id=${request.batchId}\n )\n ON CONFLICT(thread_id, batch_id) DO NOTHING RETURNING 1`.pipe(\n Effect.mapError(storageError(\"insert canonical batch\")),\n )\n : [];\n\n if (inserted.length === 0) {\n const replay = yield* executionConflict(executions.replay(request));\n\n if (replay !== undefined) return AppendResult.make(replay);\n\n const prefix = recordCache.prefix(request.threadId, thread.tail_sequence);\n\n const existing =\n prefix !== undefined && !prefix.some((row) => row.batch_id === request.batchId)\n ? undefined\n : yield* archives.replay(request.threadId, request.batchId);\n\n if (existing !== undefined) {\n if (existing.batch_json !== request.batchJson) {\n return yield* DoAppendConflict.make({\n message: `Batch ${request.batchId} already exists with different canonical content.`,\n reason: \"batch-content\",\n });\n }\n\n return AppendResult.make({\n firstSequence: existing.first_sequence,\n lastSequence: existing.last_sequence,\n replayed: true,\n });\n }\n if (canInsert)\n return yield* DoStorageCorruptionError.make({\n table: \"effect_agent_canonical_batches\",\n rowKey: request.threadId,\n message: \"Batch insertion conflicted without a durable replay receipt.\",\n });\n }\n\n yield* executionConflict(executions.rejectSuperseded(request));\n\n if (request.expectedTailSequence !== thread.tail_sequence) {\n return yield* DoAppendConflict.make({\n message:\n `Expected tail ${request.expectedTailSequence} ` + `but found ${thread.tail_sequence}.`,\n reason: \"tail\",\n actualTailSequence: thread.tail_sequence,\n });\n }\n\n const result = yield* Schema.decodeEffect(AppendResult)({\n firstSequence: thread.tail_sequence + (request.records.length === 0 ? 0 : 1),\n lastSequence: thread.tail_sequence + request.records.length,\n replayed: false,\n }).pipe(\n Effect.mapError((error) =>\n DoStorageError.make({\n cause: error,\n operation: \"append canonical batch\",\n message: error.message,\n }),\n ),\n );\n\n const { firstSequence, lastSequence } = result;\n\n yield* executionConflict(executions.validate(request));\n\n if (yield* appendNativeBody(request, firstSequence, lastSequence, inserted.length > 0)) {\n yield* progress.committed(\"canonical\").pipe(\n Effect.catchCause((cause) =>\n Effect.failCause(\n Cause.map(cause, (error) =>\n DoStorageError.make({\n operation: \"enroll canonical progress\",\n message: error.message,\n cause: error,\n }),\n ),\n ),\n ),\n );\n\n return result;\n }\n\n if (request.records.length > 0 && inserted.length === 0) {\n yield* sql`\n INSERT INTO effect_agent_canonical_batches (\n thread_id,\n batch_id,\n first_sequence,\n last_sequence,\n producer_id_json\n ) VALUES (\n ${request.threadId},\n ${JSON.stringify(request.batchId)},\n ${firstSequence},\n ${lastSequence},\n ${request.producerIdJson}\n )\n `.pipe(Effect.mapError(storageError(\"insert canonical batch\")));\n }\n yield* failpoint(\"append:after-batch-insert\");\n\n const physicalRecords = yield* physical.records(\n request.threadId,\n firstSequence,\n request.records,\n );\n\n const records = physicalRecords.map(({ columns, memberships }, index) => ({\n record: request.records[index]!,\n canonical: request.records[index]!.canonical,\n row: columns,\n memberships,\n }));\n\n // Captured scalar locators avoid parsing canonical JSON under the writer.\n // Eighteen bound columns per row stay within workerd's statement limit.\n for (const group of chunked(records, Math.floor(MAX_BOUND_PARAMETERS / 18))) {\n const inserted =\n yield* sql`INSERT INTO effect_agent_canonical_records ${sql.insert(group.map(({ row }) => row))}\n ON CONFLICT(thread_id, record_id) DO NOTHING RETURNING 1`.pipe(\n Effect.mapError(storageError(\"insert canonical records\")),\n );\n\n if (inserted.length !== group.length)\n return yield* DoAppendConflict.make({\n message: \"A canonical record ID already exists.\",\n reason: \"record-identity\",\n });\n\n const memberships = group.flatMap(({ memberships }) => memberships);\n\n for (const members of chunked(memberships, Math.floor(MAX_BOUND_PARAMETERS / 3)))\n yield* sql`INSERT INTO effect_agent_record_runs ${sql.insert(members)}`.pipe(\n Effect.mapError(storageError(\"index canonical Run membership\")),\n );\n for (const { row, record } of group) {\n recordCache.put(\n RecordRow.make({\n ...row,\n thread_id: request.threadId,\n record_id: record.recordId,\n batch_id: request.batchId,\n record_json: record.recordJson,\n sequence: Schema.decodeSync(CanonicalSequence)(row.sequence),\n }),\n record.recordBytes,\n );\n yield* failpoint(\"append:after-record-insert\");\n }\n }\n\n if (request.records.length > 0) {\n const workFields = yield* work\n .apply(\n yield* Schema.decodeEffect(ThreadId)(request.threadId).pipe(Effect.mapError(workFailure)),\n firstSequence,\n request.records.map((record) => record.canonical),\n )\n .pipe(Effect.mapError(workFailure));\n\n yield* sql`UPDATE effect_agent_threads SET ${sql.update({ tail_sequence: lastSequence, ...workFields })}\n WHERE thread_id=${request.threadId} RETURNING *`.pipe(\n threads.write,\n Effect.mapError((error) =>\n error._tag === \"SqlError\" ? storageError(\"advance thread tail\")(error) : error,\n ),\n );\n }\n yield* executionConflict(executions.commit(request, result));\n if (request.executionWrites.length > 0) yield* failpoint(\"append:after-record-insert\");\n if (\n lifecycle !== undefined &&\n request.executionWrites.some(({ update }) =>\n update.facts.some(({ payload }) => payload._tag === \"RunStarted\"),\n )\n )\n yield* flushCanonical(request.threadId).pipe(\n Effect.provideService(SqlLifecycleRetainer, { retainMany: lifecycle.retainMany }),\n Effect.mapError((cause) =>\n DoStorageError.make({\n operation: \"retain lifecycle start prefix\",\n message: \"Lifecycle start intent could not be retained\",\n cause,\n }),\n ),\n );\n for (const { update } of request.executionWrites) {\n for (const fact of update.facts) {\n if (\n lifecycle !== undefined &&\n Schema.is(Schema.toType(LifecyclePublicationFact))(fact.payload)\n )\n yield* lifecycle\n .retain({\n id: JSON.stringify([request.threadId, \"record\", fact.recordId]),\n ownerThreadId: request.threadId,\n canonicalSequence: lastSequence,\n createdAt: fact.createdAt,\n fact: fact.payload,\n })\n .pipe(\n Effect.mapError((cause) =>\n DoStorageError.make({\n operation: \"retain operational lifecycle\",\n message: cause.message,\n cause,\n }),\n ),\n );\n }\n }\n yield* failpoint(\"append:after-tail-update\");\n\n // Retain one start prefix atomically with its canonical proof, before model/tool\n // execution. Later facts stay journal-backed until the settlement publication wave.\n if (\n lifecycle !== undefined &&\n records.some(\n ({ canonical: { payload } }) =>\n payload._tag === \"RunStarted\" || payload._tag === \"SubagentStarted\",\n )\n )\n yield* flushCanonical(request.threadId).pipe(\n Effect.provideService(SqlLifecycleRetainer, { retainMany: lifecycle.retainMany }),\n Effect.mapError((cause) =>\n DoStorageError.make({\n operation: \"retain lifecycle start prefix\",\n message: \"Lifecycle start intent could not be retained\",\n cause,\n }),\n ),\n );\n\n yield* progress.committed(\"canonical\").pipe(\n Effect.catchCause((cause) =>\n Effect.failCause(\n Cause.map(cause, (error) =>\n DoStorageError.make({\n operation: \"enroll canonical progress\",\n message: error.message,\n cause: error,\n }),\n ),\n ),\n ),\n );\n\n return result;\n });\n\n const append = Effect.fnUntraced(\n function* (\n request: RawAppendRequest,\n observed: ThreadRow,\n ): Effect.fn.Return<AppendResult, AppendError> {\n yield* prepareAppend(request);\n // The facade already read this tail. Reject a known stale writer without opening a\n // transaction; a matching tail is still checked atomically below. Replays precede tail\n // conflicts, preserving retry identity even after the log has advanced.\n if (request.records.length > 0 && request.expectedTailSequence !== observed.tail_sequence) {\n const batches = yield* sql`\n SELECT batch_id FROM effect_agent_canonical_batches\n WHERE thread_id = ${request.threadId} AND batch_id = ${JSON.stringify(request.batchId)}\n UNION ALL SELECT last_commit_id FROM effect_agent_executions\n WHERE thread_id = ${request.threadId} AND last_commit_id = ${request.batchId}\n LIMIT 1\n `.pipe(Effect.mapError(storageError(\"preflight append replay\")));\n\n // A possible replay still checks exact content in the transaction.\n if (batches.length === 0) {\n return yield* DoAppendConflict.make({\n reason: \"tail\",\n message: \"The canonical tail has advanced.\",\n actualTailSequence: observed.tail_sequence,\n });\n }\n }\n\n // Receipt arbitration may have tentatively inserted a batch. Even typed\n // contention must roll back before leaving the owner mutation gate.\n return yield* withWriteTransaction(\"append transaction\")(appendPrepared(request));\n },\n withStorageSpan(\"DoJournal.append\", isAppendContention),\n );\n\n const read = Effect.fnUntraced(function* (request: RawReadRequest) {\n const thread = (yield* getThread(request.threadId))[0];\n\n const through = Math.min(\n thread?.tail_sequence ?? 0,\n request.fromSequenceExclusive + request.limit,\n );\n\n const cached: Array<RecordRow> = [];\n\n for (let sequence = request.fromSequenceExclusive + 1; sequence <= through; sequence++) {\n const record = recordCache.get(request.threadId, sequence);\n\n if (record === undefined) break;\n cached.push(record);\n }\n if (cached.length === Math.max(0, through - request.fromSequenceExclusive)) {\n return { count: cached.length, records: Stream.fromIterable(cached) };\n }\n\n // Capture membership without retaining payloads. Append-only sequences keep each later\n // payload query inside this snapshot, even when new records arrive during consumption.\n const planRows = yield* sql<Record<string, unknown>>`\n SELECT\n sequence,\n length(CAST(${recordJson} AS BLOB)) AS record_json_bytes\n FROM effect_agent_canonical_records\n WHERE thread_id = ${request.threadId}\n AND sequence > ${request.fromSequenceExclusive}\n ORDER BY sequence\n LIMIT ${request.limit}\n `.pipe(Effect.mapError(storageError(\"read canonical records\")));\n\n const plan = yield* decodeRows(\n Schema.Array(ReadPlanRow),\n \"effect_agent_canonical_records\",\n `${request.threadId}>${request.fromSequenceExclusive}`,\n planRows,\n \"ReadPlanRow\",\n );\n\n const mismatch = () =>\n DoStorageCorruptionError.make({\n table: \"effect_agent_canonical_records\",\n rowKey: `${request.threadId}>${request.fromSequenceExclusive}`,\n message: \"Canonical read membership, sequence, or payload size changed from its read plan.\",\n });\n\n if (plan.length > request.limit) return yield* mismatch();\n\n const pages: Array<ReadPage> = [];\n let page: ReadPage | undefined;\n let pageBytes = 0;\n let previousSequence = request.fromSequenceExclusive;\n\n for (const row of plan) {\n if (row.sequence !== previousSequence + 1) return yield* mismatch();\n previousSequence = row.sequence;\n if (page === undefined || pageBytes + row.record_json_bytes > MAX_READ_PAGE_JSON_BYTES) {\n page = [row];\n pages.push(page);\n pageBytes = row.record_json_bytes;\n } else {\n page.push(row);\n pageBytes += row.record_json_bytes;\n }\n }\n\n const readPage = Effect.fnUntraced(function* (page: ReadPage) {\n const rows = yield* sql<Record<string, unknown>>`\n SELECT thread_id, sequence, record_id, ${recordBatchId(sql)} AS batch_id, ${recordJson} AS record_json\n FROM effect_agent_canonical_records\n WHERE thread_id = ${request.threadId}\n AND sequence >= ${page[0].sequence}\n AND sequence <= ${page[page.length - 1].sequence}\n ORDER BY sequence\n `.pipe(Effect.mapError(storageError(\"read canonical records\")));\n\n const decoded = yield* decodeRows(\n Schema.Array(RecordRow),\n \"effect_agent_canonical_records\",\n `${request.threadId}/${page[0].sequence}`,\n rows,\n \"RecordRow\",\n );\n\n if (\n decoded.length !== page.length ||\n decoded.some(\n (row, index) =>\n row.thread_id !== request.threadId ||\n row.sequence !== page[index].sequence ||\n storedTextBytes(row.record_json) !== page[index].record_json_bytes,\n )\n ) {\n return yield* mismatch();\n }\n\n for (const row of decoded) recordCache.put(row);\n\n return decoded;\n });\n\n return {\n count: plan.length,\n records: Stream.fromIterable(pages).pipe(\n Stream.flatMap((page) => Stream.fromIterableEffect(state.read(readPage(page)))),\n ),\n };\n });\n\n const saveCheckpoint = Effect.fnUntraced(function* (\n checkpoint: RawCheckpoint,\n ): Effect.fn.Return<void, CheckpointError> {\n if (checkpoint.threadId.length > MAX_IDENTIFIER_LENGTH) {\n return yield* DoStorageError.make({\n operation: \"save checkpoint\",\n message: \"Checkpoint identity exceeds the Durable Object storage bounds.\",\n });\n }\n yield* checkValueBound(\"save checkpoint\", checkpoint.checkpointJson);\n yield* withWriteTransaction(\"checkpoint transaction\")(\n Effect.gen(function* () {\n const threadRows = yield* getThread(checkpoint.threadId);\n\n const thread = yield* decodeSingleRow(\n Schema.Array(ThreadRow),\n \"effect_agent_threads\",\n checkpoint.threadId,\n threadRows,\n );\n\n if (checkpoint.throughSequence > thread.tail_sequence) {\n return yield* DoCheckpointConflict.make({\n reason: \"ahead-of-tail\",\n message:\n `Checkpoint sequence ${checkpoint.throughSequence} is after canonical tail ` +\n `${thread.tail_sequence}.`,\n });\n }\n\n if (!(yield* isBatchBoundary(checkpoint.threadId, checkpoint.throughSequence))) {\n return yield* DoCheckpointConflict.make({\n reason: \"not-batch-boundary\",\n message: \"Checkpoint sequence is not the end of a complete canonical batch.\",\n });\n }\n\n const checkpointRows = yield* sql<Record<string, unknown>>`\n SELECT\n thread_id,\n through_sequence,\n checkpoint_json\n FROM effect_agent_checkpoints\n WHERE thread_id = ${checkpoint.threadId}\n AND through_sequence = ${checkpoint.throughSequence}\n `.pipe(Effect.mapError(storageError(\"read idempotent checkpoint\")));\n\n const existing = yield* decodeRows(\n Schema.Array(CheckpointRow),\n \"effect_agent_checkpoints\",\n `${checkpoint.threadId}/${checkpoint.throughSequence}`,\n checkpointRows,\n );\n\n if (existing.length > 1) {\n return yield* DoStorageCorruptionError.make({\n table: \"effect_agent_checkpoints\",\n rowKey: `${checkpoint.threadId}/${checkpoint.throughSequence}`,\n message: \"A checkpoint primary key returned more than one row.\",\n });\n }\n if (existing.length === 1) {\n if (existing[0].checkpoint_json !== checkpoint.checkpointJson) {\n return yield* DoCheckpointConflict.make({\n reason: \"corrupt\",\n message: \"A different checkpoint already exists at this canonical sequence.\",\n });\n }\n\n return;\n }\n\n yield* sql`\n INSERT INTO effect_agent_checkpoints (\n thread_id,\n through_sequence,\n checkpoint_json\n ) VALUES (\n ${checkpoint.threadId},\n ${checkpoint.throughSequence},\n ${checkpoint.checkpointJson}\n )\n `.pipe(Effect.mapError(storageError(\"insert checkpoint\")));\n }),\n );\n });\n\n const loadCheckpoint = Effect.fnUntraced(function* (\n threadId: string,\n atOrBeforeSequence: CanonicalSequence,\n ) {\n const rows = yield* sql<Record<string, unknown>>`\n SELECT\n thread_id,\n through_sequence,\n checkpoint_json\n FROM effect_agent_checkpoints\n WHERE thread_id = ${threadId}\n AND through_sequence <= ${atOrBeforeSequence}\n ORDER BY through_sequence DESC\n LIMIT 1\n `.pipe(Effect.mapError(storageError(\"load checkpoint\")));\n\n return yield* decodeRows(\n Schema.Array(CheckpointRow),\n \"effect_agent_checkpoints\",\n `${threadId}<=${atOrBeforeSequence}`,\n rows,\n );\n });\n\n const isBatchBoundary = Effect.fnUntraced(function* (\n threadId: string,\n sequence: CanonicalSequence,\n ) {\n if (sequence === 0) return true;\n\n const rows = yield* sql<Record<string, unknown>>`\n SELECT last_sequence FROM effect_agent_canonical_batches\n WHERE thread_id = ${threadId}\n AND batch_id = (\n SELECT ${recordBatchId(sql)} FROM effect_agent_canonical_records\n WHERE thread_id = ${threadId} AND sequence = ${sequence}\n )\n AND last_sequence = ${sequence}\n `.pipe(Effect.mapError(storageError(\"read canonical batch boundary\")));\n\n const batches = yield* decodeRows(\n Schema.Array(Schema.Struct({ last_sequence: CanonicalSequence })),\n \"effect_agent_canonical_batches\",\n `${threadId}/${sequence}`,\n rows,\n );\n\n return batches.length === 1;\n });\n\n const sourceFailure = (cause: unknown) =>\n LifecyclePublicationError.make({ reason: \"unavailable\", cause });\n\n const sourceThreads = Effect.fnUntraced(function* () {\n const all = yield* threads.matching(\n \"lifecycle-owners\",\n (row) => row.created_at !== null,\n sql`SELECT * FROM effect_agent_threads WHERE created_at IS NOT NULL LIMIT 129`,\n 128,\n );\n\n if (all.length <= 128) return all;\n\n // Large custom hosts hydrate only owners whose journal has an unmaterialized suffix.\n return yield* Schema.decodeUnknownEffect(Schema.Array(ThreadRow))(\n yield* sql`SELECT t.* FROM effect_agent_threads t LEFT JOIN effect_agent_lifecycle_cursors c ON c.thread_id=t.thread_id\n WHERE t.created_at IS NOT NULL AND (c.thread_id IS NULL OR t.tail_sequence > c.through_sequence) ORDER BY t.thread_id LIMIT 128`,\n );\n });\n\n const initializeLifecycleSource = Effect.fnUntraced(function* () {\n if (initializedLifecycleSources.has(state)) return;\n yield* state\n .transaction(\n Effect.gen(function* () {\n const existing =\n yield* sql`SELECT name FROM sqlite_master WHERE name='effect_agent_lifecycle_cursors'`;\n\n if (existing.length === 0) {\n yield* sql`CREATE TABLE effect_agent_lifecycle_cursors (\n thread_id TEXT PRIMARY KEY, through_sequence INTEGER NOT NULL\n )`;\n // Existing inline writers already retained their facts. Enabling publication on an\n // old store starts here too; it does not publish historical facts retroactively.\n }\n // New Threads created with publication disabled have no cursor. Never advance\n // existing cursors: they can owe intent from an interrupted Attempt.\n yield* sql`INSERT INTO effect_agent_lifecycle_cursors(thread_id, through_sequence)\n SELECT thread_id, tail_sequence FROM effect_agent_threads WHERE created_at IS NOT NULL\n ON CONFLICT(thread_id) DO NOTHING`;\n yield* sourceThreads();\n }),\n )\n .pipe(\n Effect.provideService(SqlClient.SqlClient, sql),\n Effect.mapError((cause) =>\n DoStorageError.make({\n operation: \"initialize lifecycle source\",\n message: \"Lifecycle source cursor unavailable\",\n cause,\n }),\n ),\n );\n initializedLifecycleSources.add(state);\n });\n\n /** The journal is durable intent; only the bounded publication wave materializes a write set. */\n const flushCanonical = Effect.fnUntraced(function* (threadId: string, maxRecords = Infinity) {\n const { retainMany } = yield* SqlLifecycleRetainer;\n const thread = (yield* getThread(threadId))[0];\n\n if (thread === undefined) return;\n const cursor = (yield* cursors.by(\"thread_id\", threadId))[0];\n\n if (cursor === undefined || cursor.through_sequence > thread.tail_sequence)\n return yield* LifecyclePublicationError.make({ reason: \"corrupt\" });\n let through = cursor.through_sequence;\n const bound = Math.min(thread.tail_sequence, through + maxRecords);\n\n while (through < bound) {\n const page = yield* read(\n RawReadRequest.make({\n threadId,\n fromSequenceExclusive: through,\n limit: Math.min(128, bound - through),\n }),\n );\n\n const facts: Array<Parameters<SqlLifecycleRetainMany>[0][number]> = [];\n let bytes = 0;\n\n yield* Stream.runForEach(page.records, (row) =>\n Effect.gen(function* () {\n const record = yield* Schema.decodeEffect(Schema.fromJsonString(CanonicalRecord))(\n row.record_json,\n );\n\n if (record.recordId !== row.record_id || row.sequence !== through + 1)\n return yield* LifecyclePublicationError.make({ reason: \"corrupt\" });\n if (Schema.is(Schema.toType(LifecyclePublicationFact))(record.payload)) {\n if (\n bytes + storedTextBytes(row.record_json) > MAX_READ_PAGE_JSON_BYTES &&\n facts.length > 0\n ) {\n yield* retainMany(facts);\n facts.length = 0;\n bytes = 0;\n }\n facts.push({\n id: JSON.stringify([threadId, \"record\", record.recordId]),\n ownerThreadId: yield* Schema.decodeEffect(ThreadId)(threadId),\n canonicalSequence: row.sequence,\n createdAt: record.createdAt,\n fact: record.payload,\n });\n bytes += storedTextBytes(row.record_json);\n }\n through = row.sequence;\n }),\n );\n if (page.count === 0) return yield* LifecyclePublicationError.make({ reason: \"corrupt\" });\n yield* retainMany(facts);\n yield* sql`UPDATE effect_agent_lifecycle_cursors SET through_sequence=${through} WHERE thread_id=${threadId} RETURNING *`.pipe(\n cursors.write,\n );\n }\n }, Effect.mapError(sourceFailure));\n\n const flushPublications = Effect.fnUntraced(function* () {\n let owners = 0;\n\n for (const thread of yield* sourceThreads()) {\n const cursor = (yield* cursors.by(\"thread_id\", thread.thread_id))[0];\n\n if (cursor !== undefined && cursor.through_sequence === thread.tail_sequence) continue;\n yield* flushCanonical(thread.thread_id, 1024);\n if (++owners === 4) break;\n }\n }, Effect.mapError(sourceFailure));\n\n const sourcePending = Effect.gen(function* () {\n for (const thread of yield* sourceThreads()) {\n const cursor = (yield* cursors.by(\"thread_id\", thread.thread_id))[0];\n\n if (cursor === undefined || cursor.through_sequence > thread.tail_sequence)\n return yield* LifecyclePublicationError.make({ reason: \"corrupt\" });\n if (cursor.through_sequence < thread.tail_sequence) return true;\n }\n\n return false;\n }).pipe(Effect.mapError(sourceFailure));\n\n const ownedLifecycle =\n lifecycle === undefined\n ? undefined\n : {\n ...lifecycle,\n storage: {\n ...lifecycle.storage,\n retainedPendingDeadline: lifecycle.storage.pendingDeadline,\n pendingDeadline: state.read(\n Effect.gen(function* () {\n const deadline = yield* lifecycle.storage.pendingDeadline;\n\n if (!(yield* sourcePending)) return deadline;\n const now = yield* Clock.currentTimeMillis;\n\n return Option.some(Option.isSome(deadline) ? Math.min(now, deadline.value) : now);\n }),\n ),\n },\n };\n\n return {\n archives: archives.storage,\n archiveRanges: archives,\n work,\n state,\n owner,\n threads,\n lifecycle: ownedLifecycle,\n initializeLifecycleSource,\n flushCanonical,\n flushPublications,\n append,\n prepareAppend,\n appendPrepared,\n checkCommitIntent: (request: RawAppendRequest) =>\n executionConflict(executions.checkCommitIntent(request)),\n readExecution: executions.read,\n checkValueBound,\n getThread,\n hasRecord,\n isBatchBoundary,\n loadCheckpoint,\n materialize,\n read: (request: RawReadRequest) => state.read(read(request)),\n saveCheckpoint,\n withWriteTransaction,\n } as const;\n});\n\nexport type DoJournal = Effect.Success<ReturnType<typeof makeJournal>>;\n\nexport const initializeDoJournal = ensureCurrentStorage;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAIA,MAAa,sBAAsB,WAChC,MAAM,SAAS,sBAAsB,MAAM,SAAS,qBAAqB,MAAM,WAAW;;AAG7F,MAAa,iBACX,QACA,aAEA,OAAO,KACL,OAAO,IAAI,OAAO,OAAO,GACzB,OAAO,QAAQ,WAAW,UAAU,OAAO,QAAQ,OAAO,KAAK,KAAK,CAAC,CAAC,CACxE;;AAGF,MAAa,wBAAwB,UACnC,OAAO,oBAAoB;CACzB,cAAc,MAAM;CACpB,GAAI,UAAU,YAAY,OAAO,OAAO,KACxC,UAAU,YAAY,MAAM,OAAO,MAAM,KACzC,OAAO,MAAM,MAAM,SAAS,WACxB,EAAE,oBAAoB,MAAM,MAAM,KAAK,IACvC,CAAC;AACP,CAAC;AAEH,MAAa,mBACX,MACA,iBAA6C,UAC1C;CACH,MAAM,OAAO,OAAO,SAAS,IAAI;CAEjC,QAAgC,WAC9B,OAAO,WAAW,UAChB,CAAC,MAAM,OAAO,WAAW,aAAa,KAAK,MAAM,OAAO,OAAO,MAAM,MAAM,OAAO,eAC9E,SACA,cAAc,QAAQ,QAAQ,CAAC,CAAC,KAC9B,OAAO,KAAK,WACV,OAAO,UAAU,MAAM,IACnB,OAAO,oBAAoB,EAAE,mB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,IACV,KAAA,IACA;EACE,GAAG;EACH,SAAS;GACP,GAAG,UAAU;GACb,yBAAyB,UAAU,QAAQ;GAC3C,iBAAiB,MAAM,KACrB,OAAO,IAAI,aAAa;IACtB,MAAM,WAAW,OAAO,UAAU,QAAQ;IAE1C,IAAI,EAAE,OAAO,gBAAgB,OAAO;IACpC,MAAM,MAAM,OAAO,MAAM;IAEzB,OAAO,OAAO,KAAK,OAAO,OAAO,QAAQ,IAAI,KAAK,IAAI,KAAK,SAAS,KAAK,IAAI,GAAG;GAClF,CAAC,CACH;EACF;CACF;CAEN,OAAO;EACL,UAAU,SAAS;EACnB,eAAe;EACf;EACA;EACA;EACA;EACA,WAAW;EACX;EACA;EACA;EACA;EACA;EACA;EACA,oBAAoB,YAClB,kBAAkB,WAAW,kBAAkB,OAAO,CAAC;EACzD,eAAe,WAAW;EAC1B;EACA;EACA;EACA;EACA;EACA;EACA,OAAO,YAA4B,MAAM,KAAK,KAAK,OAAO,CAAC;EAC3D;EACA;CACF;AACF,CAAC;AAID,MAAa,sBAAsB"}
package/dist/index.d.mts CHANGED
@@ -4,7 +4,7 @@ import { t as DoStorageConfig_d_exports } from "./DoStorageConfig.mjs";
4
4
  import { t as DoStorageFailpoint_d_exports } from "./DoStorageFailpoint.mjs";
5
5
  import { t as DoMessageDeliveryStore_d_exports } from "./DoMessageDeliveryStore.mjs";
6
6
  import { t as DoScheduleStore_d_exports } from "./DoScheduleStore.mjs";
7
- import { t as DoStorageVersion_d_exports } from "./DoStorageVersion-NKnQU3eR.mjs";
7
+ import { t as DoStorageVersion_d_exports } from "./DoStorageVersion-Mc7CXMdN.mjs";
8
8
  import { r as DoThreadStore_d_exports } from "./DoThreadStore-Bd4fTns3.mjs";
9
9
  import { t as DoSubmissionLedger_d_exports } from "./DoSubmissionLedger.mjs";
10
10
  import { t as DoSubscriptionStore_d_exports } from "./DoSubscriptionStore.mjs";
package/dist/index.mjs CHANGED
@@ -5,7 +5,7 @@ import { t as DoMessageDeliveryStore_exports } from "./DoMessageDeliveryStore.mj
5
5
  import { t as DoMemoryStore_exports } from "./DoMemoryStore.mjs";
6
6
  import { t as DoScheduleStore_exports } from "./DoScheduleStore.mjs";
7
7
  import { t as DoStorageVersion_exports } from "./DoStorageVersion.mjs";
8
- import { t as DoThreadStore_exports } from "./DoThreadStore-CtyahRTW.mjs";
8
+ import { t as DoThreadStore_exports } from "./DoThreadStore-CgIZQUNR.mjs";
9
9
  import { t as DoSubmissionLedger_exports } from "./DoSubmissionLedger.mjs";
10
10
  import { t as DoSubscriptionStore_exports } from "./DoSubscriptionStore.mjs";
11
11
  import { t as MemoryProtocol_exports } from "./MemoryProtocol.mjs";