@effect-agent/storage-cloudflare 0.1.0-beta.12 → 0.1.0-beta.121

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 (74) hide show
  1. package/dist/DoMemoryStore.d.mts +29 -0
  2. package/dist/DoMemoryStore.mjs +55 -0
  3. package/dist/DoMemoryStore.mjs.map +1 -0
  4. package/dist/DoMessageDeliveryStore.d.mts +17 -0
  5. package/dist/DoMessageDeliveryStore.mjs +32 -0
  6. package/dist/DoMessageDeliveryStore.mjs.map +1 -0
  7. package/dist/DoScheduleStore.d.mts +36 -0
  8. package/dist/DoScheduleStore.mjs +388 -0
  9. package/dist/DoScheduleStore.mjs.map +1 -0
  10. package/dist/DoStorageConfig.d.mts +45 -0
  11. package/dist/DoStorageConfig.mjs +52 -0
  12. package/dist/DoStorageConfig.mjs.map +1 -0
  13. package/dist/DoStorageError-DjpuLHmX.d.mts +101 -0
  14. package/dist/DoStorageError.d.mts +2 -0
  15. package/dist/DoStorageError.mjs +167 -0
  16. package/dist/DoStorageError.mjs.map +1 -0
  17. package/dist/DoStorageFailpoint.d.mts +17 -0
  18. package/dist/DoStorageFailpoint.mjs +14 -0
  19. package/dist/DoStorageFailpoint.mjs.map +1 -0
  20. package/dist/DoStorageFailpointTesting.d.mts +33 -0
  21. package/dist/DoStorageFailpointTesting.mjs +35 -0
  22. package/dist/DoStorageFailpointTesting.mjs.map +1 -0
  23. package/dist/DoStorageVersion-DJwZzqel.d.mts +12 -0
  24. package/dist/DoStorageVersion.d.mts +2 -0
  25. package/dist/DoStorageVersion.mjs +8 -0
  26. package/dist/DoStorageVersion.mjs.map +1 -0
  27. package/dist/DoSubmissionLedger.d.mts +23 -0
  28. package/dist/DoSubmissionLedger.mjs +1939 -0
  29. package/dist/DoSubmissionLedger.mjs.map +1 -0
  30. package/dist/DoSubscriptionStore.d.mts +25 -0
  31. package/dist/DoSubscriptionStore.mjs +183 -0
  32. package/dist/DoSubscriptionStore.mjs.map +1 -0
  33. package/dist/DoThreadStore.d.mts +59 -0
  34. package/dist/DoThreadStore.mjs +499 -0
  35. package/dist/DoThreadStore.mjs.map +1 -0
  36. package/dist/MemoryProtocol.d.mts +38160 -0
  37. package/dist/MemoryProtocol.mjs +219 -0
  38. package/dist/MemoryProtocol.mjs.map +1 -0
  39. package/dist/PortProtocol.d.mts +5521 -0
  40. package/dist/PortProtocol.mjs +206 -0
  41. package/dist/PortProtocol.mjs.map +1 -0
  42. package/dist/PortRouting.d.mts +86 -0
  43. package/dist/PortRouting.mjs +461 -0
  44. package/dist/PortRouting.mjs.map +1 -0
  45. package/dist/do-journal-D65Iuln6.mjs +1119 -0
  46. package/dist/do-journal-D65Iuln6.mjs.map +1 -0
  47. package/dist/index.d.mts +14 -1525
  48. package/dist/index.mjs +14 -3695
  49. package/dist/migrations-Br6QqMIf.mjs +306 -0
  50. package/dist/migrations-Br6QqMIf.mjs.map +1 -0
  51. package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
  52. package/package.json +1 -45
  53. package/src/DoMemoryStore.ts +56 -0
  54. package/src/DoMessageDeliveryStore.ts +53 -0
  55. package/src/DoScheduleStore.ts +622 -0
  56. package/src/{errors.ts → DoStorageError.ts} +13 -3
  57. package/src/DoStorageFailpoint.ts +20 -0
  58. package/src/{do-storage-failpoint.ts → DoStorageFailpointTesting.ts} +5 -18
  59. package/src/DoStorageVersion.ts +2 -0
  60. package/src/{do-ledger.ts → DoSubmissionLedger.ts} +1119 -262
  61. package/src/DoSubscriptionStore.ts +329 -0
  62. package/src/DoThreadStore.ts +1064 -0
  63. package/src/MemoryProtocol.ts +351 -0
  64. package/src/{port-protocol.ts → PortProtocol.ts} +103 -38
  65. package/src/{routing.ts → PortRouting.ts} +440 -264
  66. package/src/index.ts +13 -32
  67. package/src/internal/do-journal.ts +1789 -0
  68. package/src/internal/message-delivery-schema.ts +24 -0
  69. package/src/{migrations.ts → internal/migrations.ts} +72 -35
  70. package/src/internal/recovery-checkpoint-schema.ts +17 -0
  71. package/dist/index.mjs.map +0 -1
  72. package/src/do-conversation-store.ts +0 -850
  73. package/src/do-journal.ts +0 -1078
  74. /package/src/{do-storage-config.ts → DoStorageConfig.ts} +0 -0
@@ -0,0 +1,1119 @@
1
+ import { DoAppendConflict, DoCheckpointConflict, DoFenceRejected, DoStorageCompatibilityError, DoStorageCorruptionError, DoStorageError, DoValueBoundExceeded } from "./DoStorageError.mjs";
2
+ import { a as createRecoveryCheckpointTable, i as doMigrations, n as createNonterminalIndex, o as createMessageDeliveryTables, r as createWorkerStops } from "./migrations-Br6QqMIf.mjs";
3
+ import { Effect, Schema, Stream } from "effect";
4
+ import { createMessageDeliveryPendingIndex } from "effect-agent/sql-message-delivery-store";
5
+ import * as SqlClient from "effect/unstable/sql/SqlClient";
6
+ import { SqliteMigrator } from "@effect/sql-sqlite-do";
7
+ import { EMPTY_TAIL_DIGEST } from "effect-agent/digest";
8
+ import { CanonicalRecord, CanonicalSequence, ProducerEpoch } from "effect-agent/records";
9
+ import { checkV2ThreadLayout } from "effect-agent/sql-storage-v2-upgrade";
10
+ import { createNativeReadIndexes, indexCanonicalRecord, seedNativeReadIndexes } from "effect-agent/sql-thread-native-reads";
11
+ import { CheckpointRejected, FenceRejected, MAX_THREAD_EXPORT_RECORDS, ThreadNotMaterialized, ThreadStoreDiagnostic } from "effect-agent/thread-store";
12
+ import { SqlError } from "effect/unstable/sql/SqlError";
13
+ //#region src/internal/do-journal.ts
14
+ /**
15
+ * Static schema ceiling for stored text columns. Writes are bounded in BYTES by the
16
+ * configured `maxStoredValueBytes` (always ≤ 2,000,000); UTF-8 byte length is never smaller
17
+ * than UTF-16 string length, so any value that passed the byte bound also passes this
18
+ * decode-side character ceiling.
19
+ */
20
+ const BoundedStoredText = Schema.String.check(Schema.isMaxLength(2e6));
21
+ const BoundedIdentifier = Schema.NonEmptyString.check(Schema.isMaxLength(1024));
22
+ const MAX_RECORDS_PER_THREAD = MAX_THREAD_EXPORT_RECORDS;
23
+ const ZERO_SEQUENCE = Schema.decodeSync(CanonicalSequence)(0);
24
+ const MAX_IDENTIFIER_LENGTH = 1024;
25
+ const MAX_READ_PAGE_JSON_BYTES = 4194304;
26
+ const isSqlError = Schema.is(SqlError);
27
+ const storedTextBytes = (value) => new TextEncoder().encode(value).byteLength;
28
+ const chunked = (values, size) => {
29
+ const chunks = [];
30
+ for (let index = 0; index < values.length; index += size) chunks.push(values.slice(index, index + size));
31
+ return chunks;
32
+ };
33
+ var DoMetaRow = class extends Schema.Class("DoMetaRow")({ value: Schema.NonEmptyString.check(Schema.isMaxLength(128)) }) {};
34
+ var DoNameRow = class extends Schema.Class("DoNameRow")({ name: BoundedIdentifier }) {};
35
+ var ThreadRow = class extends Schema.Class("ThreadRow")({
36
+ thread_id: BoundedIdentifier,
37
+ created_at: Schema.NonEmptyString.check(Schema.isMaxLength(128)),
38
+ producer_epoch: ProducerEpoch,
39
+ tail_digest: BoundedStoredText,
40
+ tail_sequence: CanonicalSequence
41
+ }) {};
42
+ var BatchRow = class extends Schema.Class("BatchRow")({
43
+ batch_digest: BoundedStoredText,
44
+ batch_id: BoundedIdentifier,
45
+ batch_json: BoundedStoredText,
46
+ thread_id: BoundedIdentifier,
47
+ first_sequence: CanonicalSequence,
48
+ last_sequence: CanonicalSequence,
49
+ tail_digest: BoundedStoredText
50
+ }) {};
51
+ var RecordRow = class extends Schema.Class("RecordRow")({
52
+ batch_id: BoundedIdentifier,
53
+ thread_id: BoundedIdentifier,
54
+ record_id: BoundedIdentifier,
55
+ record_json: BoundedStoredText,
56
+ sequence: CanonicalSequence
57
+ }) {};
58
+ const ReadPlanRow = Schema.Struct({
59
+ sequence: CanonicalSequence,
60
+ record_json_bytes: Schema.Natural.check(Schema.isLessThanOrEqualTo(MAX_READ_PAGE_JSON_BYTES))
61
+ });
62
+ var CheckpointRow = class extends Schema.Class("CheckpointRow")({
63
+ checkpoint_json: BoundedStoredText,
64
+ thread_id: BoundedIdentifier,
65
+ tail_digest: BoundedStoredText,
66
+ through_sequence: CanonicalSequence
67
+ }) {};
68
+ var RawRecord = class extends Schema.Class("@effect-agent/storage-cloudflare/RawRecord")({
69
+ recordId: BoundedIdentifier,
70
+ recordJson: BoundedStoredText
71
+ }) {};
72
+ var RawAppendRequest = class extends Schema.Class("@effect-agent/storage-cloudflare/RawAppendRequest")({
73
+ batchDigest: BoundedStoredText,
74
+ batchId: BoundedIdentifier,
75
+ batchJson: BoundedStoredText,
76
+ threadId: BoundedIdentifier,
77
+ expectedTailDigest: BoundedStoredText,
78
+ expectedTailSequence: CanonicalSequence,
79
+ producerEpoch: ProducerEpoch,
80
+ records: Schema.NonEmptyArray(RawRecord).check(Schema.isMaxLength(256)),
81
+ tailDigest: BoundedStoredText
82
+ }) {};
83
+ var RawAppendResult = class extends Schema.Class("@effect-agent/storage-cloudflare/RawAppendResult")({
84
+ firstSequence: CanonicalSequence,
85
+ lastSequence: CanonicalSequence,
86
+ replayed: Schema.Boolean,
87
+ tailDigest: BoundedStoredText
88
+ }) {};
89
+ var RawReadRequest = class extends Schema.Class("@effect-agent/storage-cloudflare/RawReadRequest")({
90
+ threadId: BoundedIdentifier,
91
+ fromSequenceExclusive: CanonicalSequence,
92
+ limit: Schema.Int.check(Schema.isGreaterThan(0), Schema.isLessThanOrEqualTo(1024))
93
+ }) {};
94
+ var RawCheckpoint = class extends Schema.Class("@effect-agent/storage-cloudflare/RawCheckpoint")({
95
+ checkpointJson: BoundedStoredText,
96
+ threadId: BoundedIdentifier,
97
+ tailDigest: BoundedStoredText,
98
+ throughSequence: CanonicalSequence
99
+ }) {};
100
+ var RawThreadExport = class extends Schema.Class("@effect-agent/storage-cloudflare/RawThreadExport")({
101
+ thread: ThreadRow,
102
+ records: Schema.Array(RecordRow)
103
+ }) {};
104
+ const noFailpoint = () => Effect.void;
105
+ const storageError = (operation) => (error) => DoStorageError.make({
106
+ cause: error,
107
+ operation,
108
+ message: error.message,
109
+ diagnostic: ThreadStoreDiagnostic.make({
110
+ causeTag: error._tag,
111
+ operation,
112
+ issueTag: error.reason._tag
113
+ })
114
+ });
115
+ /** Decode raw Durable Object SQLite rows against a Schema, reporting failures as typed corruption. */
116
+ const decodeRows = Effect.fn((schema, table, rowKey, rows, decoder = table) => Schema.decodeUnknownEffect(schema)(rows).pipe(Effect.mapError((error) => DoStorageCorruptionError.make({
117
+ table,
118
+ rowKey,
119
+ message: "Stored rows do not satisfy the storage schema",
120
+ diagnostic: ThreadStoreDiagnostic.make({
121
+ causeTag: error._tag,
122
+ operation: "decode storage rows",
123
+ decoder,
124
+ issueTag: error.issue._tag
125
+ })
126
+ })), Effect.tapError((error) => Effect.annotateCurrentSpan({
127
+ "storage.failure.operation": error.diagnostic?.operation,
128
+ "storage.failure.cause": error.diagnostic?.causeTag,
129
+ "storage.failure.decoder": decoder,
130
+ "storage.failure.issue": error.diagnostic?.issueTag
131
+ }))));
132
+ /** Decode exactly one raw row against a Schema, reporting failures as typed corruption. */
133
+ const decodeSingleRow = Effect.fn((schema, table, rowKey, rows) => decodeRows(schema, table, rowKey, rows).pipe(Effect.flatMap((decoded) => decoded.length === 1 ? Effect.succeed(decoded[0]) : Effect.fail(DoStorageCorruptionError.make({
134
+ table,
135
+ rowKey,
136
+ message: `Expected exactly one row but found ${decoded.length}.`
137
+ })))));
138
+ /** Column inventory of the supported v3 predecessor, independent of physical column order. */
139
+ const predecessorColumns = {
140
+ effect_agent_threads: [
141
+ "thread_id",
142
+ "created_at",
143
+ "tail_sequence",
144
+ "tail_digest",
145
+ "producer_epoch"
146
+ ],
147
+ effect_agent_canonical_batches: [
148
+ "thread_id",
149
+ "batch_id",
150
+ "first_sequence",
151
+ "last_sequence",
152
+ "batch_digest",
153
+ "tail_digest",
154
+ "batch_json"
155
+ ],
156
+ effect_agent_canonical_records: [
157
+ "thread_id",
158
+ "sequence",
159
+ "record_id",
160
+ "batch_id",
161
+ "record_json"
162
+ ],
163
+ effect_agent_checkpoints: [
164
+ "thread_id",
165
+ "through_sequence",
166
+ "tail_digest",
167
+ "checkpoint_json"
168
+ ],
169
+ effect_agent_submissions: [
170
+ "submission_id",
171
+ "thread_id",
172
+ "queue_sequence",
173
+ "principal",
174
+ "idempotency_key",
175
+ "agent_id",
176
+ "agent_digests_json",
177
+ "deployment_id",
178
+ "input_json",
179
+ "input_digest",
180
+ "receipt_id",
181
+ "state",
182
+ "settled_outcome",
183
+ "created_at",
184
+ "ready_at",
185
+ "input_applied_record_id",
186
+ "input_applied_sequence",
187
+ "joined_host_submission_id",
188
+ "suspended_reason_json",
189
+ "suspended_at",
190
+ "unknown_reason",
191
+ "unknown_tool_call_ids_json",
192
+ "parent_submission_id",
193
+ "parent_tool_call_id",
194
+ "admission_group",
195
+ "admission_fence_json"
196
+ ],
197
+ effect_agent_submission_ownership: [
198
+ "submission_id",
199
+ "attempt_id",
200
+ "ownership_token",
201
+ "producer_epoch",
202
+ "owner_producer_id",
203
+ "lease_expires_at"
204
+ ],
205
+ effect_agent_attempts: [
206
+ "attempt_id",
207
+ "submission_id",
208
+ "thread_id",
209
+ "owner_producer_id",
210
+ "producer_epoch",
211
+ "claimed_at"
212
+ ],
213
+ effect_agent_settlement_reservations: [
214
+ "submission_id",
215
+ "settlement_id",
216
+ "outcome",
217
+ "record_id",
218
+ "record_json",
219
+ "record_digest",
220
+ "reserved_at",
221
+ "finalized_at"
222
+ ],
223
+ effect_agent_abort_intents: [
224
+ "submission_id",
225
+ "author",
226
+ "reason",
227
+ "requested_at",
228
+ "canonical_record_id"
229
+ ],
230
+ effect_agent_approval_decisions: [
231
+ "submission_id",
232
+ "tool_call_id",
233
+ "decision",
234
+ "resolver",
235
+ "reason",
236
+ "decided_at"
237
+ ],
238
+ effect_agent_unknown_resolutions: [
239
+ "submission_id",
240
+ "tool_call_id",
241
+ "author",
242
+ "reason",
243
+ "resolution_json",
244
+ "resolved_at"
245
+ ],
246
+ effect_agent_child_reservations: [
247
+ "reservation_id",
248
+ "parent_submission_id",
249
+ "parent_tool_call_id",
250
+ "child_submission_id",
251
+ "status",
252
+ "allocation_json",
253
+ "allocation_digest",
254
+ "accounting_json",
255
+ "reserved_at",
256
+ "release_began_at",
257
+ "released_at"
258
+ ],
259
+ effect_agent_meta: ["key", "value"],
260
+ effect_agent_child_settlements: [
261
+ "parent_submission_id",
262
+ "child_submission_id",
263
+ "child_outcome",
264
+ "recorded_at"
265
+ ]
266
+ };
267
+ const checkPredecessorLayout = Effect.fn("DoJournal.checkPredecessorLayout")(function* (version) {
268
+ const sql = yield* SqlClient.SqlClient;
269
+ const expectedColumns = {
270
+ ...version === 3 ? predecessorColumns : {
271
+ ...predecessorColumns,
272
+ effect_agent_submissions: [
273
+ ...predecessorColumns.effect_agent_submissions,
274
+ "worker_admission_json",
275
+ "message_admission_json"
276
+ ],
277
+ effect_agent_message_deliveries: [
278
+ "owner_thread_id",
279
+ "message_id",
280
+ "version",
281
+ "state",
282
+ "deadline_at_millis",
283
+ "record_json"
284
+ ]
285
+ },
286
+ ...version === 7 ? { effect_agent_canonical_records: [...predecessorColumns.effect_agent_canonical_records, "outstanding"] } : {},
287
+ ...version >= 5 ? { effect_agent_recovery_checkpoints: [
288
+ "thread_id",
289
+ "through_sequence",
290
+ "tail_digest",
291
+ "checkpoint_json"
292
+ ] } : {}
293
+ };
294
+ for (const [table, expected] of Object.entries(expectedColumns)) {
295
+ const columns = yield* decodeRows(Schema.Array(Schema.Struct({ name: BoundedIdentifier })), table, "schema", yield* sql.unsafe(`PRAGMA table_info(${table})`));
296
+ const names = new Set(expected);
297
+ if (columns.length !== names.size || columns.some((column) => !names.has(column.name))) return yield* DoStorageCompatibilityError.make({
298
+ actualVersion: version,
299
+ supportedVersion: 9,
300
+ message: `The v${version} ${table} columns do not match the supported predecessor; no upgrade was committed.`
301
+ });
302
+ }
303
+ });
304
+ const REQUIRED_TABLES = [
305
+ "effect_agent_abort_intents",
306
+ "effect_agent_approval_decisions",
307
+ "effect_agent_attempts",
308
+ "effect_agent_canonical_batches",
309
+ "effect_agent_canonical_records",
310
+ "effect_agent_checkpoints",
311
+ "effect_agent_child_reservations",
312
+ "effect_agent_child_settlements",
313
+ "effect_agent_threads",
314
+ "effect_agent_meta",
315
+ "effect_agent_settlement_reservations",
316
+ "effect_agent_submission_ownership",
317
+ "effect_agent_submissions",
318
+ "effect_agent_unknown_resolutions"
319
+ ];
320
+ /**
321
+ * Supported-predecessor or fresh storage gate (DEPLOY-008) over `effect_agent_meta` instead of
322
+ * `PRAGMA user_version` (unverified on Durable Object SQL storage; a meta table is portable
323
+ * regardless). No WAL check (Durable Object storage owns durability and confirms writes
324
+ * through output gates) and no busy timeout (a Durable Object has exactly one writer): the
325
+ * Node machinery those served has no DC analogue and is deliberately absent.
326
+ */
327
+ const ensureCurrentStorage = Effect.fn("DoJournal.ensureCurrentStorage")(function* (sql, failpoint = noFailpoint, maxStoredValueBytes) {
328
+ const verifyWorkerPredecessor = Effect.fnUntraced(function* (workerContract) {
329
+ const requiredRows = yield* sql`
330
+ SELECT name
331
+ FROM sqlite_master
332
+ WHERE (type = 'table'
333
+ AND name IN ${sql.in([
334
+ ...REQUIRED_TABLES,
335
+ "effect_agent_message_deliveries",
336
+ "effect_agent_recovery_checkpoints"
337
+ ])}
338
+ ) OR (type = 'index' AND name IN ('effect_agent_submissions_nonterminal', 'effect_agent_records_subtree', 'effect_agent_message_deliveries_pending', 'effect_agent_records_outstanding', 'effect_agent_records_call', 'effect_agent_records_run_input', 'effect_agent_records_worker_input'))
339
+ OR (${workerContract ? 1 : 0} = 1 AND name IN ('effect_agent_worker_stops', 'effect_agent_worker_starts', 'effect_agent_worker_pending', 'effect_agent_worker_execution'))
340
+ ORDER BY name
341
+ `.pipe(Effect.mapError(storageError("verify storage tables")));
342
+ if ((yield* decodeRows(Schema.Array(DoNameRow), "sqlite_master", "required_tables", requiredRows)).length !== REQUIRED_TABLES.length + 9 + (workerContract ? 4 : 0)) return yield* DoStorageCompatibilityError.make({
343
+ actualVersion: 9,
344
+ supportedVersion: 9,
345
+ message: "The Durable Object claims the current format but is missing required tables or its nonterminal index. Retain the original store for inspection."
346
+ });
347
+ });
348
+ const metaTableRows = yield* sql`
349
+ SELECT name
350
+ FROM sqlite_master
351
+ WHERE type = 'table'
352
+ AND name = 'effect_agent_meta'
353
+ `.pipe(Effect.mapError(storageError("read storage version table")));
354
+ if ((yield* decodeRows(Schema.Array(DoNameRow), "sqlite_master", "effect_agent_meta", metaTableRows)).length === 0) {
355
+ const existingRows = yield* sql`
356
+ SELECT name
357
+ FROM sqlite_master
358
+ WHERE type = 'table'
359
+ AND name LIKE 'effect_agent_%'
360
+ ORDER BY name
361
+ `.pipe(Effect.mapError(storageError("inspect unversioned storage")));
362
+ if ((yield* decodeRows(Schema.Array(DoNameRow), "sqlite_master", "effect_agent_%", existingRows)).length > 0) return yield* DoStorageCompatibilityError.make({
363
+ actualVersion: 0,
364
+ supportedVersion: 9,
365
+ message: "The Durable Object contains unversioned Effect Agent tables. Refusing to mutate ambiguous stored data; retain it for inspection with its original writer."
366
+ });
367
+ yield* SqliteMigrator.run({
368
+ loader: doMigrations,
369
+ table: "effect_sql_migrations_agent_threads"
370
+ }).pipe(Effect.provideService(SqlClient.SqlClient, sql), Effect.mapError((error) => DoStorageError.make({
371
+ cause: error,
372
+ operation: "initialize current storage",
373
+ message: error.message
374
+ })));
375
+ } else {
376
+ const versionRows = yield* sql`
377
+ SELECT value
378
+ FROM effect_agent_meta
379
+ WHERE key = 'storage_version'
380
+ `.pipe(Effect.mapError(storageError("read storage version")));
381
+ const version = yield* decodeSingleRow(Schema.Array(DoMetaRow), "effect_agent_meta", "storage_version", versionRows);
382
+ if (version.value === "2" || version.value === "3" || version.value === "4" || version.value === "5") yield* sql.withTransaction(Effect.gen(function* () {
383
+ const current = yield* sql`SELECT value FROM effect_agent_meta WHERE key='storage_version'`;
384
+ if (current.length === 1 && current[0].value === String(9)) return;
385
+ if (current.length !== 1 || current[0].value !== "2" && current[0].value !== "3" && current[0].value !== "4" && current[0].value !== "5") return yield* DoStorageCompatibilityError.make({
386
+ actualVersion: -1,
387
+ supportedVersion: 9,
388
+ message: "Storage version changed while acquiring the upgrade transaction."
389
+ });
390
+ if ((yield* decodeRows(Schema.Array(DoNameRow), "sqlite_master", "required_tables", yield* sql`SELECT name FROM sqlite_master WHERE type='table' AND name IN ${sql.in([...REQUIRED_TABLES])}`)).length !== REQUIRED_TABLES.length) return yield* DoStorageCompatibilityError.make({
391
+ actualVersion: Number(current[0].value),
392
+ supportedVersion: 9,
393
+ message: "The predecessor store is missing required tables. Retain the original store for inspection; no upgrade was committed."
394
+ });
395
+ if ((yield* sql`SELECT name FROM sqlite_master WHERE type='table' AND name='effect_agent_recovery_checkpoints'`).length !== (current[0].value === "5" ? 1 : 0)) return yield* DoStorageCompatibilityError.make({
396
+ actualVersion: Number(current[0].value),
397
+ supportedVersion: 9,
398
+ message: "The predecessor recovery checkpoint storage does not match its version; refusing ambiguous data without mutation."
399
+ });
400
+ if ((yield* sql`SELECT name FROM sqlite_master WHERE name='effect_agent_submissions_nonterminal'`).length !== 0) return yield* DoStorageCompatibilityError.make({
401
+ actualVersion: Number(current[0].value),
402
+ supportedVersion: 9,
403
+ message: "The predecessor already contains the nonterminal index; refusing ambiguous storage without mutation."
404
+ });
405
+ if (current[0].value === "2") {
406
+ yield* checkV2ThreadLayout();
407
+ for (const statement of [
408
+ sql`ALTER TABLE effect_agent_submissions ADD COLUMN admission_group TEXT`,
409
+ sql`ALTER TABLE effect_agent_submissions ADD COLUMN admission_fence_json TEXT`,
410
+ sql`CREATE INDEX effect_agent_submissions_group ON effect_agent_submissions (thread_id, admission_group, state)`
411
+ ]) {
412
+ yield* failpoint("upgrade:before-mutation");
413
+ yield* statement;
414
+ yield* failpoint("upgrade:after-mutation");
415
+ }
416
+ }
417
+ if (current[0].value === "3") yield* checkPredecessorLayout(3);
418
+ if (current[0].value === "4") yield* checkPredecessorLayout(4);
419
+ if (current[0].value === "5") yield* checkPredecessorLayout(5);
420
+ if (current[0].value === "2" || current[0].value === "3") {
421
+ yield* failpoint("upgrade:before-mutation");
422
+ yield* sql`ALTER TABLE effect_agent_submissions ADD COLUMN worker_admission_json TEXT`;
423
+ yield* failpoint("upgrade:after-mutation");
424
+ yield* failpoint("upgrade:before-mutation");
425
+ yield* sql`ALTER TABLE effect_agent_submissions ADD COLUMN message_admission_json TEXT`;
426
+ yield* failpoint("upgrade:after-mutation");
427
+ yield* failpoint("upgrade:before-mutation");
428
+ yield* createMessageDeliveryTables;
429
+ yield* failpoint("upgrade:after-mutation");
430
+ }
431
+ if (current[0].value !== "5") {
432
+ yield* failpoint("upgrade:before-mutation");
433
+ yield* createRecoveryCheckpointTable;
434
+ yield* failpoint("upgrade:after-mutation");
435
+ }
436
+ yield* failpoint("upgrade:before-mutation");
437
+ yield* createNonterminalIndex;
438
+ yield* failpoint("upgrade:after-mutation");
439
+ yield* failpoint("upgrade:before-version");
440
+ yield* createNativeReadIndexes;
441
+ yield* createMessageDeliveryPendingIndex;
442
+ yield* seedNativeReadIndexes.pipe(Effect.catchTag("ThreadStoreError", (error) => DoStorageCorruptionError.make({
443
+ table: "effect_agent_canonical_records",
444
+ rowKey: "upgrade",
445
+ message: error.message
446
+ })));
447
+ yield* createWorkerStops;
448
+ yield* sql`UPDATE effect_agent_meta SET value='9' WHERE key='storage_version'`;
449
+ yield* failpoint("upgrade:after-version");
450
+ })).pipe(Effect.catchTag("DoStorageFailpointError", (error) => DoStorageError.make({
451
+ cause: error,
452
+ operation: "upgrade storage",
453
+ message: error.message
454
+ })), Effect.catchTag("StorageUpgradeError", (error) => DoStorageCorruptionError.make({
455
+ table: error.table,
456
+ rowKey: error.rowKey,
457
+ message: error.message
458
+ })), Effect.catchTag("SchemaError", (error) => DoStorageCorruptionError.make({
459
+ table: "effect_agent_canonical_records",
460
+ rowKey: "upgrade",
461
+ message: error.message
462
+ })), Effect.catchTag("SqlError", storageError("upgrade supported thread storage")));
463
+ else if (version.value === "6") yield* sql.withTransaction(Effect.gen(function* () {
464
+ const current = yield* sql`SELECT value FROM effect_agent_meta WHERE key = 'storage_version'`;
465
+ if (current[0]?.value === "9") return;
466
+ if (current[0]?.value !== "6") return yield* DoStorageCompatibilityError.make({
467
+ actualVersion: -1,
468
+ supportedVersion: 9,
469
+ message: "Storage version changed during native index upgrade"
470
+ });
471
+ yield* checkPredecessorLayout(5);
472
+ if ((yield* sql`SELECT name FROM sqlite_master WHERE type = 'index' AND name = 'effect_agent_submissions_nonterminal'`).length !== 1) return yield* DoStorageCompatibilityError.make({
473
+ actualVersion: 6,
474
+ supportedVersion: 9,
475
+ message: "Predecessor storage is missing its required nonterminal index"
476
+ });
477
+ yield* failpoint("upgrade:before-mutation");
478
+ yield* createNativeReadIndexes;
479
+ yield* createMessageDeliveryPendingIndex;
480
+ yield* seedNativeReadIndexes.pipe(Effect.catchTag("ThreadStoreError", (error) => DoStorageCorruptionError.make({
481
+ table: "effect_agent_canonical_records",
482
+ rowKey: "upgrade",
483
+ message: error.message
484
+ })));
485
+ yield* failpoint("upgrade:after-mutation");
486
+ yield* failpoint("upgrade:before-version");
487
+ yield* createWorkerStops;
488
+ yield* sql`UPDATE effect_agent_meta SET value='9' WHERE key='storage_version'`;
489
+ yield* failpoint("upgrade:after-version");
490
+ })).pipe(Effect.mapError((error) => DoStorageError.make({
491
+ operation: "upgrade native indexes",
492
+ message: error.message,
493
+ cause: error
494
+ })));
495
+ else if (version.value === "7") yield* sql.withTransaction(Effect.gen(function* () {
496
+ const current = yield* sql`SELECT value FROM effect_agent_meta WHERE key = 'storage_version'`;
497
+ if (current[0]?.value === "9") return;
498
+ if (current[0]?.value !== "7") return yield* DoStorageCompatibilityError.make({
499
+ actualVersion: -1,
500
+ supportedVersion: 9,
501
+ message: "Storage version changed during worker stop upgrade"
502
+ });
503
+ yield* checkPredecessorLayout(7);
504
+ yield* verifyWorkerPredecessor(false);
505
+ yield* failpoint("upgrade:before-mutation");
506
+ yield* createWorkerStops;
507
+ yield* failpoint("upgrade:after-mutation");
508
+ yield* failpoint("upgrade:before-version");
509
+ yield* sql`UPDATE effect_agent_meta SET value='9' WHERE key='storage_version'`;
510
+ yield* failpoint("upgrade:after-version");
511
+ })).pipe(Effect.mapError((cause) => DoStorageError.make({
512
+ operation: "upgrade worker stop",
513
+ message: "Worker stop upgrade failed",
514
+ cause
515
+ })));
516
+ else if (version.value === "8") yield* sql.withTransaction(Effect.gen(function* () {
517
+ const current = yield* sql`SELECT value FROM effect_agent_meta WHERE key = 'storage_version'`;
518
+ if (current[0]?.value === "9") return;
519
+ if (current[0]?.value !== "8") return yield* DoStorageCompatibilityError.make({
520
+ actualVersion: -1,
521
+ supportedVersion: 9,
522
+ message: "Storage version changed during assignment seal upgrade"
523
+ });
524
+ yield* checkPredecessorLayout(7);
525
+ yield* verifyWorkerPredecessor(true);
526
+ const columns = yield* sql`PRAGMA table_info(effect_agent_worker_stops)`;
527
+ yield* Schema.decodeUnknownEffect(Schema.Tuple([Schema.Struct({
528
+ cid: Schema.Literal(0),
529
+ name: Schema.Literal("thread_id"),
530
+ type: Schema.Literal("TEXT"),
531
+ notnull: Schema.Literal(1),
532
+ dflt_value: Schema.Null,
533
+ pk: Schema.Literal(1)
534
+ })]))(columns).pipe(Effect.mapError(() => DoStorageCompatibilityError.make({
535
+ actualVersion: 8,
536
+ supportedVersion: 9,
537
+ message: "Unsupported worker seal layout; no upgrade was committed"
538
+ })));
539
+ yield* failpoint("upgrade:before-mutation");
540
+ yield* sql`ALTER TABLE effect_agent_worker_stops ADD COLUMN terminal TEXT`;
541
+ yield* failpoint("upgrade:after-mutation");
542
+ yield* failpoint("upgrade:before-version");
543
+ yield* sql`UPDATE effect_agent_meta SET value='9' WHERE key='storage_version'`;
544
+ yield* failpoint("upgrade:after-version");
545
+ })).pipe(Effect.mapError((cause) => DoStorageError.make({
546
+ operation: "upgrade assignment seals",
547
+ message: "Assignment seal upgrade failed",
548
+ cause
549
+ })));
550
+ else if (version.value !== String(9)) {
551
+ const actualVersion = Number.parseInt(version.value, 10);
552
+ return yield* DoStorageCompatibilityError.make({
553
+ actualVersion: Number.isSafeInteger(actualVersion) ? actualVersion : -1,
554
+ supportedVersion: 9,
555
+ message: `The Durable Object uses unsupported storage version ${version.value}; this build supports exactly version 9. Only supported v2, v3, v4, v5, v6, v7 and v8 can be upgraded automatically. Keep the original store and use a compatible library version.`
556
+ });
557
+ }
558
+ }
559
+ yield* verifyWorkerPredecessor(true);
560
+ return makeJournal(sql, failpoint, maxStoredValueBytes);
561
+ });
562
+ const makeJournal = (sql, failpoint, maxStoredValueBytes) => {
563
+ /** Typed pre-write refusal for any single value over the configured byte bound. */
564
+ const checkValueBound = (operation, value) => {
565
+ const actualBytes = storedTextBytes(value);
566
+ return actualBytes > maxStoredValueBytes ? Effect.fail(DoValueBoundExceeded.make({
567
+ actualBytes,
568
+ maxBytes: maxStoredValueBytes,
569
+ operation
570
+ })) : Effect.void;
571
+ };
572
+ /**
573
+ * Runs one journal write transaction on the Durable Object storage-backed
574
+ * `withTransaction` (`ctx.storage.transaction()` under the hood). Within one Durable
575
+ * Object there is exactly ONE writer, so the Node `BEGIN IMMEDIATE` + busy-retry +
576
+ * `SqliteWriteContention` machinery has no analogue here and is deliberately absent.
577
+ * Ownership-token and epoch checks still run INSIDE the transaction, so fencing atomicity
578
+ * (DUR-006) is preserved identically.
579
+ *
580
+ * Journal write transactions are always top level: the Durable Object client rejects
581
+ * nested transactions, so new journal operations must not wrap this helper inside another
582
+ * transaction.
583
+ */
584
+ const withWriteTransaction = (operation) => (effect) => sql.withTransaction(effect).pipe(Effect.mapError((error) => isSqlError(error) ? storageError(operation)(error) : error), Effect.withSpan("DoJournal.withWriteTransaction", { attributes: { operation } }));
585
+ const materialize = Effect.fn("DoJournal.materialize")(function* (threadId, createdAt, emptyTailDigest, producerEpoch) {
586
+ if (threadId.length > MAX_IDENTIFIER_LENGTH) return yield* DoStorageError.make({
587
+ operation: "materialize thread",
588
+ message: "Thread identity exceeds the Durable Object storage bounds."
589
+ });
590
+ yield* checkValueBound("materialize thread", emptyTailDigest);
591
+ yield* withWriteTransaction("materialize transaction")(Effect.gen(function* () {
592
+ const existingRows = yield* sql`
593
+ SELECT
594
+ thread_id,
595
+ created_at,
596
+ tail_sequence,
597
+ tail_digest,
598
+ producer_epoch
599
+ FROM effect_agent_threads
600
+ WHERE thread_id = ${threadId}
601
+ `.pipe(Effect.mapError(storageError("read materialized thread")));
602
+ const existing = yield* decodeRows(Schema.Array(ThreadRow), "effect_agent_threads", threadId, existingRows);
603
+ if (existing.length > 1) return yield* DoStorageCorruptionError.make({
604
+ table: "effect_agent_threads",
605
+ rowKey: threadId,
606
+ message: "A thread primary key returned more than one row."
607
+ });
608
+ if (existing.length === 0) {
609
+ yield* sql`
610
+ INSERT INTO effect_agent_threads (
611
+ thread_id,
612
+ created_at,
613
+ tail_sequence,
614
+ tail_digest,
615
+ producer_epoch
616
+ ) VALUES (
617
+ ${threadId},
618
+ ${createdAt},
619
+ 0,
620
+ ${emptyTailDigest},
621
+ ${producerEpoch}
622
+ )
623
+ `.pipe(Effect.mapError(storageError("materialize thread")));
624
+ return;
625
+ }
626
+ if (producerEpoch < existing[0].producer_epoch) return yield* DoFenceRejected.make({
627
+ producerEpoch,
628
+ actualEpoch: existing[0].producer_epoch,
629
+ message: `Producer epoch ${producerEpoch} is stale; current epoch is ${existing[0].producer_epoch}.`
630
+ });
631
+ if (producerEpoch > existing[0].producer_epoch) yield* sql`
632
+ UPDATE effect_agent_threads
633
+ SET producer_epoch = ${producerEpoch}
634
+ WHERE thread_id = ${threadId}
635
+ `.pipe(Effect.mapError(storageError("advance materialization epoch")));
636
+ }));
637
+ });
638
+ const getThread = Effect.fn("DoJournal.getThread")(function* (threadId) {
639
+ const rows = yield* sql`
640
+ SELECT
641
+ thread_id,
642
+ created_at,
643
+ tail_sequence,
644
+ tail_digest,
645
+ producer_epoch
646
+ FROM effect_agent_threads
647
+ WHERE thread_id = ${threadId}
648
+ `.pipe(Effect.mapError(storageError("read thread")));
649
+ return yield* decodeRows(Schema.Array(ThreadRow), "effect_agent_threads", threadId, rows);
650
+ });
651
+ const append = Effect.fn("DoJournal.append")(function* (request) {
652
+ if (request.threadId.length > MAX_IDENTIFIER_LENGTH || request.batchId.length > MAX_IDENTIFIER_LENGTH || request.records.some((record) => record.recordId.length > MAX_IDENTIFIER_LENGTH)) return yield* DoStorageError.make({
653
+ operation: "append canonical batch",
654
+ message: "Canonical identifiers exceed the Durable Object storage bounds."
655
+ });
656
+ yield* checkValueBound("append canonical batch", request.batchJson);
657
+ yield* checkValueBound("append canonical batch", request.batchDigest);
658
+ yield* checkValueBound("append canonical batch", request.tailDigest);
659
+ yield* Effect.forEach(request.records, (record) => checkValueBound("append canonical record", record.recordJson), { discard: true });
660
+ return yield* withWriteTransaction("append transaction")(Effect.gen(function* () {
661
+ const recordIds = request.records.map((record) => record.recordId);
662
+ if (new Set(recordIds).size !== recordIds.length) return yield* DoAppendConflict.make({
663
+ message: `Batch ${request.batchId} contains duplicate canonical record IDs.`,
664
+ reason: "record-identity"
665
+ });
666
+ const threadRows = yield* sql`
667
+ SELECT
668
+ thread_id,
669
+ created_at,
670
+ tail_sequence,
671
+ tail_digest,
672
+ producer_epoch
673
+ FROM effect_agent_threads
674
+ WHERE thread_id = ${request.threadId}
675
+ `.pipe(Effect.mapError(storageError("read append tail")));
676
+ const thread = yield* decodeSingleRow(Schema.Array(ThreadRow), "effect_agent_threads", request.threadId, threadRows);
677
+ if (request.producerEpoch !== thread.producer_epoch) return yield* DoFenceRejected.make({
678
+ producerEpoch: request.producerEpoch,
679
+ actualEpoch: thread.producer_epoch,
680
+ message: `Producer epoch ${request.producerEpoch} is not the current epoch ${thread.producer_epoch}.`
681
+ });
682
+ const batchRows = yield* sql`
683
+ SELECT
684
+ thread_id,
685
+ batch_id,
686
+ first_sequence,
687
+ last_sequence,
688
+ batch_digest,
689
+ tail_digest,
690
+ batch_json
691
+ FROM effect_agent_canonical_batches
692
+ WHERE thread_id = ${request.threadId}
693
+ AND batch_id = ${request.batchId}
694
+ `.pipe(Effect.mapError(storageError("read idempotent batch")));
695
+ const batches = yield* decodeRows(Schema.Array(BatchRow), "effect_agent_canonical_batches", `${request.threadId}/${request.batchId}`, batchRows);
696
+ if (batches.length > 1) return yield* DoStorageCorruptionError.make({
697
+ table: "effect_agent_canonical_batches",
698
+ rowKey: `${request.threadId}/${request.batchId}`,
699
+ message: "A canonical batch primary key returned more than one row."
700
+ });
701
+ if (batches.length === 1) {
702
+ const existing = batches[0];
703
+ if (existing.batch_digest !== request.batchDigest) return yield* DoAppendConflict.make({
704
+ message: `Batch ${request.batchId} already exists with different canonical content.`,
705
+ reason: "batch-digest"
706
+ });
707
+ return RawAppendResult.make({
708
+ firstSequence: existing.first_sequence,
709
+ lastSequence: existing.last_sequence,
710
+ replayed: true,
711
+ tailDigest: existing.tail_digest
712
+ });
713
+ }
714
+ if (request.expectedTailSequence !== thread.tail_sequence || request.expectedTailDigest !== thread.tail_digest) return yield* DoAppendConflict.make({
715
+ message: `Expected tail ${request.expectedTailSequence}/${request.expectedTailDigest} but found ${thread.tail_sequence}/${thread.tail_digest}.`,
716
+ reason: "tail",
717
+ actualTailSequence: thread.tail_sequence,
718
+ actualTailDigest: thread.tail_digest
719
+ });
720
+ if (thread.tail_sequence + request.records.length > MAX_RECORDS_PER_THREAD) return yield* DoStorageError.make({
721
+ operation: "append canonical batch",
722
+ message: `Thread record limit ${MAX_RECORDS_PER_THREAD} would be exceeded.`
723
+ });
724
+ const existingRecords = [];
725
+ for (const chunk of chunked(recordIds, 90)) {
726
+ const existingRecordRows = yield* sql`
727
+ SELECT
728
+ thread_id,
729
+ sequence,
730
+ record_id,
731
+ batch_id,
732
+ record_json
733
+ FROM effect_agent_canonical_records
734
+ WHERE thread_id = ${request.threadId}
735
+ AND record_id IN ${sql.in([...chunk])}
736
+ ORDER BY sequence
737
+ `.pipe(Effect.mapError(storageError("check canonical record identities")));
738
+ existingRecords.push(...yield* decodeRows(Schema.Array(RecordRow), "effect_agent_canonical_records", `${request.threadId}/record_ids`, existingRecordRows));
739
+ }
740
+ if (existingRecords.length > 0) return yield* DoAppendConflict.make({
741
+ message: `Canonical record ID ${existingRecords[0].record_id} already exists.`,
742
+ reason: "record-identity"
743
+ });
744
+ const firstSequence = yield* Schema.decodeEffect(CanonicalSequence)(thread.tail_sequence + 1).pipe(Effect.mapError((error) => DoStorageError.make({
745
+ cause: error,
746
+ operation: "append canonical batch",
747
+ message: error.message
748
+ })));
749
+ const lastSequence = yield* Schema.decodeEffect(CanonicalSequence)(firstSequence + request.records.length - 1).pipe(Effect.mapError((error) => DoStorageError.make({
750
+ cause: error,
751
+ operation: "append canonical batch",
752
+ message: error.message
753
+ })));
754
+ yield* sql`
755
+ INSERT INTO effect_agent_canonical_batches (
756
+ thread_id,
757
+ batch_id,
758
+ first_sequence,
759
+ last_sequence,
760
+ batch_digest,
761
+ tail_digest,
762
+ batch_json
763
+ ) VALUES (
764
+ ${request.threadId},
765
+ ${request.batchId},
766
+ ${firstSequence},
767
+ ${lastSequence},
768
+ ${request.batchDigest},
769
+ ${request.tailDigest},
770
+ ${request.batchJson}
771
+ )
772
+ `.pipe(Effect.mapError(storageError("insert canonical batch")));
773
+ yield* failpoint("append:after-batch-insert");
774
+ yield* Effect.forEach(request.records, (record, index) => Effect.gen(function* () {
775
+ yield* sql`
776
+ INSERT INTO effect_agent_canonical_records (
777
+ thread_id,
778
+ sequence,
779
+ record_id,
780
+ batch_id,
781
+ record_json
782
+ ) VALUES (
783
+ ${request.threadId},
784
+ ${firstSequence + index},
785
+ ${record.recordId},
786
+ ${request.batchId},
787
+ ${record.recordJson}
788
+ )
789
+ `.pipe(Effect.mapError(storageError("insert canonical record")));
790
+ const canonical = yield* Schema.decodeEffect(Schema.fromJsonString(CanonicalRecord))(record.recordJson).pipe(Effect.mapError((error) => DoStorageCorruptionError.make({
791
+ table: "effect_agent_canonical_records",
792
+ rowKey: record.recordId,
793
+ message: error.message
794
+ })));
795
+ yield* indexCanonicalRecord(request.threadId, canonical).pipe(Effect.provideService(SqlClient.SqlClient, sql), Effect.mapError(storageError("index canonical record")));
796
+ yield* failpoint("append:after-record-insert");
797
+ }), { discard: true });
798
+ yield* sql`
799
+ UPDATE effect_agent_threads
800
+ SET
801
+ tail_sequence = ${lastSequence},
802
+ tail_digest = ${request.tailDigest},
803
+ producer_epoch = ${request.producerEpoch}
804
+ WHERE thread_id = ${request.threadId}
805
+ `.pipe(Effect.mapError(storageError("advance thread tail")));
806
+ yield* failpoint("append:after-tail-update");
807
+ return RawAppendResult.make({
808
+ firstSequence,
809
+ lastSequence,
810
+ replayed: false,
811
+ tailDigest: request.tailDigest
812
+ });
813
+ }));
814
+ });
815
+ const read = Effect.fnUntraced(function* (request) {
816
+ const planRows = yield* sql`
817
+ SELECT
818
+ sequence,
819
+ length(CAST(record_json AS BLOB)) AS record_json_bytes
820
+ FROM effect_agent_canonical_records
821
+ WHERE thread_id = ${request.threadId}
822
+ AND sequence > ${request.fromSequenceExclusive}
823
+ ORDER BY sequence
824
+ LIMIT ${request.limit}
825
+ `.pipe(Effect.mapError(storageError("read canonical records")));
826
+ const plan = yield* decodeRows(Schema.Array(ReadPlanRow), "effect_agent_canonical_records", `${request.threadId}>${request.fromSequenceExclusive}`, planRows, "ReadPlanRow");
827
+ const mismatch = () => DoStorageCorruptionError.make({
828
+ table: "effect_agent_canonical_records",
829
+ rowKey: `${request.threadId}>${request.fromSequenceExclusive}`,
830
+ message: "Canonical read membership, sequence, or payload size changed from its read plan."
831
+ });
832
+ if (plan.length > request.limit) return yield* mismatch();
833
+ const pages = [];
834
+ let page;
835
+ let pageBytes = 0;
836
+ let previousSequence = request.fromSequenceExclusive;
837
+ for (const row of plan) {
838
+ if (row.sequence !== previousSequence + 1) return yield* mismatch();
839
+ previousSequence = row.sequence;
840
+ if (page === void 0 || pageBytes + row.record_json_bytes > MAX_READ_PAGE_JSON_BYTES) {
841
+ page = [row];
842
+ pages.push(page);
843
+ pageBytes = row.record_json_bytes;
844
+ } else {
845
+ page.push(row);
846
+ pageBytes += row.record_json_bytes;
847
+ }
848
+ }
849
+ const readPage = Effect.fn("DoJournal.readPage")(function* (page) {
850
+ const rows = yield* sql`
851
+ SELECT thread_id, sequence, record_id, batch_id, record_json
852
+ FROM effect_agent_canonical_records
853
+ WHERE thread_id = ${request.threadId}
854
+ AND sequence >= ${page[0].sequence}
855
+ AND sequence <= ${page[page.length - 1].sequence}
856
+ ORDER BY sequence
857
+ `.pipe(Effect.mapError(storageError("read canonical records")));
858
+ const decoded = yield* decodeRows(Schema.Array(RecordRow), "effect_agent_canonical_records", `${request.threadId}/${page[0].sequence}`, rows, "RecordRow");
859
+ if (decoded.length !== page.length || decoded.some((row, index) => row.thread_id !== request.threadId || row.sequence !== page[index].sequence || storedTextBytes(row.record_json) !== page[index].record_json_bytes)) return yield* mismatch();
860
+ return decoded;
861
+ });
862
+ return {
863
+ count: plan.length,
864
+ records: Stream.fromIterable(pages).pipe(Stream.flatMap((page) => Stream.fromIterableEffect(readPage(page))))
865
+ };
866
+ });
867
+ const exportThread = Effect.fn("DoJournal.exportThread")(function* (threadId) {
868
+ return yield* sql.withTransaction(Effect.gen(function* () {
869
+ const threadRows = yield* sql`
870
+ SELECT
871
+ thread_id,
872
+ created_at,
873
+ tail_sequence,
874
+ tail_digest,
875
+ producer_epoch
876
+ FROM effect_agent_threads
877
+ WHERE thread_id = ${threadId}
878
+ `.pipe(Effect.mapError(storageError("export thread")));
879
+ const thread = yield* decodeSingleRow(Schema.Array(ThreadRow), "effect_agent_threads", threadId, threadRows);
880
+ yield* failpoint("export:after-thread-read");
881
+ if (thread.tail_sequence > MAX_RECORDS_PER_THREAD) return yield* DoStorageError.make({
882
+ operation: "export thread",
883
+ message: "The thread exceeds the current export record limit."
884
+ });
885
+ const records = [];
886
+ let afterSequence = ZERO_SEQUENCE;
887
+ while (afterSequence < thread.tail_sequence) {
888
+ const limit = Math.min(1024, thread.tail_sequence - afterSequence);
889
+ const request = RawReadRequest.make({
890
+ threadId,
891
+ fromSequenceExclusive: afterSequence,
892
+ limit
893
+ });
894
+ const plan = yield* read(request);
895
+ const page = yield* Stream.runCollect(plan.records);
896
+ if (page.length !== limit || page.some((record, index) => record.sequence !== afterSequence + index + 1)) return yield* DoStorageCorruptionError.make({
897
+ table: "effect_agent_canonical_records",
898
+ rowKey: threadId,
899
+ message: "The exported canonical prefix is not contiguous through its captured tail."
900
+ });
901
+ records.push(...page);
902
+ afterSequence = page[page.length - 1].sequence;
903
+ }
904
+ if ((yield* sql`SELECT sequence FROM effect_agent_canonical_records WHERE thread_id=${threadId} AND sequence > ${thread.tail_sequence} LIMIT 1`.pipe(Effect.mapError(storageError("verify export tail")))).length !== 0) return yield* DoStorageCorruptionError.make({
905
+ table: "effect_agent_canonical_records",
906
+ rowKey: threadId,
907
+ message: "Canonical records exist beyond the captured thread tail."
908
+ });
909
+ return RawThreadExport.make({
910
+ thread,
911
+ records
912
+ });
913
+ })).pipe(Effect.catchTag("SqlError", (error) => Effect.fail(storageError("export transaction")(error))));
914
+ });
915
+ const saveCheckpoint = Effect.fn("DoJournal.saveCheckpoint")(function* (checkpoint) {
916
+ if (checkpoint.threadId.length > MAX_IDENTIFIER_LENGTH) return yield* DoStorageError.make({
917
+ operation: "save checkpoint",
918
+ message: "Checkpoint identity exceeds the Durable Object storage bounds."
919
+ });
920
+ yield* checkValueBound("save checkpoint", checkpoint.checkpointJson);
921
+ yield* withWriteTransaction("checkpoint transaction")(Effect.gen(function* () {
922
+ const threadRows = yield* sql`
923
+ SELECT
924
+ thread_id,
925
+ created_at,
926
+ tail_sequence,
927
+ tail_digest,
928
+ producer_epoch
929
+ FROM effect_agent_threads
930
+ WHERE thread_id = ${checkpoint.threadId}
931
+ `.pipe(Effect.mapError(storageError("read checkpoint tail")));
932
+ const thread = yield* decodeSingleRow(Schema.Array(ThreadRow), "effect_agent_threads", checkpoint.threadId, threadRows);
933
+ if (checkpoint.throughSequence > thread.tail_sequence) return yield* DoCheckpointConflict.make({ message: `Checkpoint sequence ${checkpoint.throughSequence} is after canonical tail ${thread.tail_sequence}.` });
934
+ const checkpointRows = yield* sql`
935
+ SELECT
936
+ thread_id,
937
+ through_sequence,
938
+ tail_digest,
939
+ checkpoint_json
940
+ FROM effect_agent_checkpoints
941
+ WHERE thread_id = ${checkpoint.threadId}
942
+ AND through_sequence = ${checkpoint.throughSequence}
943
+ `.pipe(Effect.mapError(storageError("read idempotent checkpoint")));
944
+ const existing = yield* decodeRows(Schema.Array(CheckpointRow), "effect_agent_checkpoints", `${checkpoint.threadId}/${checkpoint.throughSequence}`, checkpointRows);
945
+ if (existing.length > 1) return yield* DoStorageCorruptionError.make({
946
+ table: "effect_agent_checkpoints",
947
+ rowKey: `${checkpoint.threadId}/${checkpoint.throughSequence}`,
948
+ message: "A checkpoint primary key returned more than one row."
949
+ });
950
+ if (existing.length === 1) {
951
+ if (existing[0].tail_digest !== checkpoint.tailDigest || existing[0].checkpoint_json !== checkpoint.checkpointJson) return yield* DoCheckpointConflict.make({ message: "A different checkpoint already exists at this canonical sequence." });
952
+ return;
953
+ }
954
+ yield* sql`
955
+ INSERT INTO effect_agent_checkpoints (
956
+ thread_id,
957
+ through_sequence,
958
+ tail_digest,
959
+ checkpoint_json
960
+ ) VALUES (
961
+ ${checkpoint.threadId},
962
+ ${checkpoint.throughSequence},
963
+ ${checkpoint.tailDigest},
964
+ ${checkpoint.checkpointJson}
965
+ )
966
+ `.pipe(Effect.mapError(storageError("insert checkpoint")));
967
+ }));
968
+ });
969
+ const saveRecoveryCheckpoint = Effect.fn("DoJournal.saveRecoveryCheckpoint")(function* (request, checkpointJson) {
970
+ const { checkpoint } = request;
971
+ if (checkpoint.threadId.length > MAX_IDENTIFIER_LENGTH) return yield* DoStorageError.make({
972
+ operation: "save recovery checkpoint",
973
+ message: "Checkpoint identity exceeds the Durable Object storage bounds."
974
+ });
975
+ yield* checkValueBound("save recovery checkpoint", checkpointJson);
976
+ yield* failpoint("save-recovery-checkpoint:before");
977
+ yield* withWriteTransaction("recovery checkpoint transaction")(Effect.gen(function* () {
978
+ const thread = (yield* getThread(checkpoint.threadId))[0];
979
+ if (thread === void 0) return yield* ThreadNotMaterialized.make({ threadId: checkpoint.threadId });
980
+ if (request.producerEpoch !== thread.producer_epoch) return yield* FenceRejected.make({
981
+ threadId: checkpoint.threadId,
982
+ actualEpoch: thread.producer_epoch,
983
+ attemptedEpoch: request.producerEpoch
984
+ });
985
+ if (checkpoint.throughSequence > thread.tail_sequence) return yield* CheckpointRejected.make({
986
+ threadId: checkpoint.threadId,
987
+ reason: "ahead-of-tail"
988
+ });
989
+ const digests = checkpoint.throughSequence === 0 ? [EMPTY_TAIL_DIGEST] : yield* getTailDigestAt(checkpoint.threadId, checkpoint.throughSequence);
990
+ if (digests.length !== 1 || digests[0] !== checkpoint.tailDigest) return yield* CheckpointRejected.make({
991
+ threadId: checkpoint.threadId,
992
+ reason: "digest-mismatch"
993
+ });
994
+ yield* sql`
995
+ INSERT INTO effect_agent_recovery_checkpoints (thread_id, through_sequence, tail_digest, checkpoint_json)
996
+ VALUES (${checkpoint.threadId}, ${checkpoint.throughSequence}, ${checkpoint.tailDigest}, ${checkpointJson})
997
+ ON CONFLICT (thread_id) DO UPDATE SET
998
+ through_sequence = excluded.through_sequence,
999
+ tail_digest = excluded.tail_digest,
1000
+ checkpoint_json = excluded.checkpoint_json
1001
+ WHERE excluded.through_sequence >= effect_agent_recovery_checkpoints.through_sequence
1002
+ `.pipe(Effect.mapError(storageError("save recovery checkpoint")));
1003
+ }));
1004
+ yield* failpoint("save-recovery-checkpoint:after");
1005
+ });
1006
+ const loadRecoveryCheckpoint = Effect.fn("DoJournal.loadRecoveryCheckpoint")(function* (threadId) {
1007
+ const rows = yield* sql`
1008
+ SELECT thread_id, through_sequence, tail_digest, checkpoint_json
1009
+ FROM effect_agent_recovery_checkpoints
1010
+ WHERE thread_id = ${threadId}
1011
+ `.pipe(Effect.mapError(storageError("load recovery checkpoint")));
1012
+ return yield* decodeRows(Schema.Array(CheckpointRow), "effect_agent_recovery_checkpoints", threadId, rows);
1013
+ });
1014
+ const loadCheckpoint = Effect.fn("DoJournal.loadCheckpoint")(function* (threadId, atOrBeforeSequence) {
1015
+ const rows = yield* sql`
1016
+ SELECT
1017
+ thread_id,
1018
+ through_sequence,
1019
+ tail_digest,
1020
+ checkpoint_json
1021
+ FROM effect_agent_checkpoints
1022
+ WHERE thread_id = ${threadId}
1023
+ AND through_sequence <= ${atOrBeforeSequence}
1024
+ ORDER BY through_sequence DESC
1025
+ LIMIT 1
1026
+ `.pipe(Effect.mapError(storageError("load checkpoint")));
1027
+ return yield* decodeRows(Schema.Array(CheckpointRow), "effect_agent_checkpoints", `${threadId}<=${atOrBeforeSequence}`, rows);
1028
+ });
1029
+ const getTailDigestAt = Effect.fn("DoJournal.getTailDigestAt")(function* (threadId, sequence) {
1030
+ if (sequence === 0) {
1031
+ const threads = yield* getThread(threadId);
1032
+ return threads.length === 0 ? [] : [threads[0].tail_sequence === 0 ? threads[0].tail_digest : void 0].filter((value) => value !== void 0);
1033
+ }
1034
+ const rows = yield* sql`
1035
+ SELECT
1036
+ thread_id,
1037
+ batch_id,
1038
+ first_sequence,
1039
+ last_sequence,
1040
+ batch_digest,
1041
+ tail_digest,
1042
+ batch_json
1043
+ FROM effect_agent_canonical_batches
1044
+ WHERE thread_id = ${threadId}
1045
+ AND last_sequence = ${sequence}
1046
+ `.pipe(Effect.mapError(storageError("read canonical digest at sequence")));
1047
+ return (yield* decodeRows(Schema.Array(BatchRow), "effect_agent_canonical_batches", `${threadId}/${sequence}`, rows)).map((batch) => batch.tail_digest);
1048
+ });
1049
+ return {
1050
+ append,
1051
+ checkValueBound,
1052
+ exportThread,
1053
+ getThread,
1054
+ getTailDigestAt,
1055
+ loadCheckpoint,
1056
+ loadRecoveryCheckpoint,
1057
+ saveRecoveryCheckpoint,
1058
+ materialize,
1059
+ read,
1060
+ saveCheckpoint,
1061
+ scanStoredPayloads: Effect.fn("DoJournal.scanStoredPayloads")(function* () {
1062
+ return yield* sql.withTransaction(Effect.gen(function* () {
1063
+ const threads = yield* sql`
1064
+ SELECT
1065
+ thread_id,
1066
+ created_at,
1067
+ tail_sequence,
1068
+ tail_digest,
1069
+ producer_epoch
1070
+ FROM effect_agent_threads
1071
+ ORDER BY thread_id
1072
+ `.pipe(Effect.mapError(storageError("scan threads")));
1073
+ const batches = yield* sql`
1074
+ SELECT
1075
+ thread_id,
1076
+ batch_id,
1077
+ first_sequence,
1078
+ last_sequence,
1079
+ batch_digest,
1080
+ tail_digest,
1081
+ batch_json
1082
+ FROM effect_agent_canonical_batches
1083
+ ORDER BY thread_id, first_sequence
1084
+ `.pipe(Effect.mapError(storageError("scan canonical batches")));
1085
+ const records = yield* sql`
1086
+ SELECT
1087
+ thread_id,
1088
+ sequence,
1089
+ record_id,
1090
+ batch_id,
1091
+ record_json
1092
+ FROM effect_agent_canonical_records
1093
+ ORDER BY thread_id, sequence
1094
+ `.pipe(Effect.mapError(storageError("scan canonical records")));
1095
+ const checkpoints = yield* sql`
1096
+ SELECT
1097
+ thread_id,
1098
+ through_sequence,
1099
+ tail_digest,
1100
+ checkpoint_json
1101
+ FROM effect_agent_checkpoints
1102
+ ORDER BY thread_id, through_sequence
1103
+ `.pipe(Effect.mapError(storageError("scan checkpoints")));
1104
+ return {
1105
+ threads: yield* decodeRows(Schema.Array(ThreadRow), "effect_agent_threads", "startup_scan", threads),
1106
+ batches: yield* decodeRows(Schema.Array(BatchRow), "effect_agent_canonical_batches", "startup_scan", batches),
1107
+ records: yield* decodeRows(Schema.Array(RecordRow), "effect_agent_canonical_records", "startup_scan", records),
1108
+ checkpoints: yield* decodeRows(Schema.Array(CheckpointRow), "effect_agent_checkpoints", "startup_scan", checkpoints)
1109
+ };
1110
+ })).pipe(Effect.catchTag("SqlError", (error) => Effect.fail(storageError("startup scan transaction")(error))));
1111
+ }),
1112
+ withWriteTransaction
1113
+ };
1114
+ };
1115
+ const initializeDoJournal = ensureCurrentStorage;
1116
+ //#endregion
1117
+ export { initializeDoJournal as a, decodeRows as i, RawCheckpoint as n, RawReadRequest as r, RawAppendRequest as t };
1118
+
1119
+ //# sourceMappingURL=do-journal-D65Iuln6.mjs.map