@effect-agent/storage-sqlite 0.1.0-beta.6 → 0.1.0-beta.60

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 (59) hide show
  1. package/dist/SqliteActivityStore.d.mts +14 -0
  2. package/dist/SqliteActivityStore.mjs +310 -0
  3. package/dist/SqliteActivityStore.mjs.map +1 -0
  4. package/dist/SqliteMessageDeliveryStore.d.mts +14 -0
  5. package/dist/SqliteMessageDeliveryStore.mjs +24 -0
  6. package/dist/SqliteMessageDeliveryStore.mjs.map +1 -0
  7. package/dist/SqliteScheduleStore.d.mts +14 -0
  8. package/dist/SqliteScheduleStore.mjs +255 -0
  9. package/dist/SqliteScheduleStore.mjs.map +1 -0
  10. package/dist/SqliteStorageConfig.d.mts +34 -0
  11. package/dist/SqliteStorageConfig.mjs +39 -0
  12. package/dist/SqliteStorageConfig.mjs.map +1 -0
  13. package/dist/SqliteStorageError-CzKmwCLl.d.mts +86 -0
  14. package/dist/SqliteStorageError.d.mts +2 -0
  15. package/dist/SqliteStorageError.mjs +148 -0
  16. package/dist/SqliteStorageError.mjs.map +1 -0
  17. package/dist/SqliteStorageFailpoint.d.mts +17 -0
  18. package/dist/SqliteStorageFailpoint.mjs +14 -0
  19. package/dist/SqliteStorageFailpoint.mjs.map +1 -0
  20. package/dist/SqliteStorageFailpointTesting.d.mts +15 -0
  21. package/dist/SqliteStorageFailpointTesting.mjs +19 -0
  22. package/dist/SqliteStorageFailpointTesting.mjs.map +1 -0
  23. package/dist/SqliteStorageVersion-gA7_96xM.d.mts +9 -0
  24. package/dist/SqliteStorageVersion.d.mts +2 -0
  25. package/dist/SqliteStorageVersion.mjs +8 -0
  26. package/dist/SqliteStorageVersion.mjs.map +1 -0
  27. package/dist/SqliteSubmissionLedger.d.mts +23 -0
  28. package/dist/SqliteSubmissionLedger.mjs +1765 -0
  29. package/dist/SqliteSubmissionLedger.mjs.map +1 -0
  30. package/dist/SqliteSubscriptionStore.d.mts +13 -0
  31. package/dist/SqliteSubscriptionStore.mjs +28 -0
  32. package/dist/SqliteSubscriptionStore.mjs.map +1 -0
  33. package/dist/SqliteThreadStore.d.mts +49 -0
  34. package/dist/SqliteThreadStore.mjs +427 -0
  35. package/dist/SqliteThreadStore.mjs.map +1 -0
  36. package/dist/index.d.mts +11 -193
  37. package/dist/index.mjs +11 -3082
  38. package/dist/migrations-dnTR0RKq.mjs +326 -0
  39. package/dist/migrations-dnTR0RKq.mjs.map +1 -0
  40. package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
  41. package/dist/sqlite-journal-lDduEakl.mjs +887 -0
  42. package/dist/sqlite-journal-lDduEakl.mjs.map +1 -0
  43. package/package.json +1 -41
  44. package/src/SqliteActivityStore.ts +556 -0
  45. package/src/SqliteMessageDeliveryStore.ts +42 -0
  46. package/src/SqliteScheduleStore.ts +404 -0
  47. package/src/{sqlite-storage-config.ts → SqliteStorageConfig.ts} +1 -1
  48. package/src/{errors.ts → SqliteStorageError.ts} +8 -3
  49. package/src/SqliteStorageFailpoint.ts +23 -0
  50. package/src/{sqlite-storage-failpoint.ts → SqliteStorageFailpointTesting.ts} +7 -18
  51. package/src/SqliteStorageVersion.ts +2 -0
  52. package/src/{sqlite-ledger.ts → SqliteSubmissionLedger.ts} +753 -118
  53. package/src/SqliteSubscriptionStore.ts +59 -0
  54. package/src/{sqlite-conversation-store.ts → SqliteThreadStore.ts} +372 -302
  55. package/src/index.ts +10 -6
  56. package/src/internal/message-delivery-schema.ts +24 -0
  57. package/src/{migrations.ts → internal/migrations.ts} +145 -79
  58. package/src/{sqlite-journal.ts → internal/sqlite-journal.ts} +534 -222
  59. package/dist/index.mjs.map +0 -1
package/src/index.ts CHANGED
@@ -1,6 +1,10 @@
1
- export * from "./errors.ts";
2
- export * from "./migrations.ts";
3
- export * from "./sqlite-conversation-store.ts";
4
- export * from "./sqlite-ledger.ts";
5
- export * from "./sqlite-storage-config.ts";
6
- export * from "./sqlite-storage-failpoint.ts";
1
+ export * as SqliteMessageDeliveryStore from "./SqliteMessageDeliveryStore.ts";
2
+ export * as SqliteActivityStore from "./SqliteActivityStore.ts";
3
+ export * as SqliteScheduleStore from "./SqliteScheduleStore.ts";
4
+ export * as SqliteStorageConfig from "./SqliteStorageConfig.ts";
5
+ export * as SqliteStorageError from "./SqliteStorageError.ts";
6
+ export * as SqliteStorageFailpoint from "./SqliteStorageFailpoint.ts";
7
+ export * as SqliteStorageVersion from "./SqliteStorageVersion.ts";
8
+ export * as SqliteSubmissionLedger from "./SqliteSubmissionLedger.ts";
9
+ export * as SqliteSubscriptionStore from "./SqliteSubscriptionStore.ts";
10
+ export * as SqliteThreadStore from "./SqliteThreadStore.ts";
@@ -0,0 +1,24 @@
1
+ import { Effect } from "effect";
2
+ import * as SqlClient from "effect/unstable/sql/SqlClient";
3
+
4
+ /** Additive storage owned by this adapter; creation participates in its version transaction. */
5
+ export const createMessageDeliveryTables = Effect.gen(function* () {
6
+ const sql = yield* SqlClient.SqlClient;
7
+
8
+ yield* sql`
9
+ CREATE TABLE effect_agent_message_deliveries (
10
+ owner_thread_id TEXT NOT NULL,
11
+ message_id TEXT NOT NULL,
12
+ version INTEGER NOT NULL,
13
+ state TEXT NOT NULL,
14
+ deadline_at_millis INTEGER,
15
+ record_json TEXT NOT NULL,
16
+ PRIMARY KEY (owner_thread_id, message_id)
17
+ )
18
+ `.withoutTransform;
19
+ yield* sql`
20
+ CREATE INDEX effect_agent_message_deliveries_due
21
+ ON effect_agent_message_deliveries (deadline_at_millis, owner_thread_id, message_id)
22
+ WHERE deadline_at_millis IS NOT NULL
23
+ `.withoutTransform;
24
+ });
@@ -2,15 +2,18 @@ import { SqliteMigrator } from "@effect/sql-sqlite-node";
2
2
  import { Effect } from "effect";
3
3
  import * as SqlClient from "effect/unstable/sql/SqlClient";
4
4
 
5
- export const CurrentSqliteStorageVersion = 4;
5
+ import { createMessageDeliveryTables } from "./message-delivery-schema.ts";
6
6
 
7
+ export const CurrentSqliteStorageVersion = 9;
8
+
9
+ /** Initialize empty storage with the complete current schema. */
7
10
  export const sqliteMigrations = SqliteMigrator.fromRecord({
8
- "1_current_persistent_conversation_foundation": Effect.gen(function* () {
11
+ "1_current_thread_storage": Effect.gen(function* () {
9
12
  const sql = yield* SqlClient.SqlClient;
10
13
 
11
14
  yield* sql`
12
- CREATE TABLE effect_agent_conversations (
13
- conversation_id TEXT PRIMARY KEY NOT NULL,
15
+ CREATE TABLE effect_agent_threads (
16
+ thread_id TEXT PRIMARY KEY NOT NULL,
14
17
  created_at TEXT NOT NULL,
15
18
  tail_sequence INTEGER NOT NULL,
16
19
  tail_digest TEXT NOT NULL,
@@ -20,64 +23,59 @@ export const sqliteMigrations = SqliteMigrator.fromRecord({
20
23
 
21
24
  yield* sql`
22
25
  CREATE TABLE effect_agent_canonical_batches (
23
- conversation_id TEXT NOT NULL,
26
+ thread_id TEXT NOT NULL,
24
27
  batch_id TEXT NOT NULL,
25
28
  first_sequence INTEGER NOT NULL,
26
29
  last_sequence INTEGER NOT NULL,
27
30
  batch_digest TEXT NOT NULL,
28
31
  tail_digest TEXT NOT NULL,
29
32
  batch_json TEXT NOT NULL,
30
- PRIMARY KEY (conversation_id, batch_id),
31
- FOREIGN KEY (conversation_id)
32
- REFERENCES effect_agent_conversations(conversation_id)
33
+ PRIMARY KEY (thread_id, batch_id),
34
+ FOREIGN KEY (thread_id)
35
+ REFERENCES effect_agent_threads(thread_id)
33
36
  ON DELETE RESTRICT
34
37
  )
35
38
  `.withoutTransform;
36
39
 
37
40
  yield* sql`
38
41
  CREATE TABLE effect_agent_canonical_records (
39
- conversation_id TEXT NOT NULL,
42
+ thread_id TEXT NOT NULL,
40
43
  sequence INTEGER NOT NULL,
41
44
  record_id TEXT NOT NULL,
42
45
  batch_id TEXT NOT NULL,
43
46
  record_json TEXT NOT NULL,
44
- PRIMARY KEY (conversation_id, sequence),
45
- UNIQUE (conversation_id, record_id),
46
- FOREIGN KEY (conversation_id, batch_id)
47
- REFERENCES effect_agent_canonical_batches(conversation_id, batch_id)
47
+ PRIMARY KEY (thread_id, sequence),
48
+ UNIQUE (thread_id, record_id),
49
+ FOREIGN KEY (thread_id, batch_id)
50
+ REFERENCES effect_agent_canonical_batches(thread_id, batch_id)
48
51
  ON DELETE RESTRICT
49
52
  )
50
53
  `.withoutTransform;
51
54
 
52
55
  yield* sql`
53
56
  CREATE INDEX effect_agent_canonical_records_batch
54
- ON effect_agent_canonical_records (conversation_id, batch_id, sequence)
57
+ ON effect_agent_canonical_records (thread_id, batch_id, sequence)
55
58
  `.withoutTransform;
56
59
 
57
60
  yield* sql`
58
61
  CREATE TABLE effect_agent_checkpoints (
59
- conversation_id TEXT NOT NULL,
62
+ thread_id TEXT NOT NULL,
60
63
  through_sequence INTEGER NOT NULL,
61
64
  tail_digest TEXT NOT NULL,
62
65
  checkpoint_json TEXT NOT NULL,
63
- PRIMARY KEY (conversation_id, through_sequence),
64
- FOREIGN KEY (conversation_id)
65
- REFERENCES effect_agent_conversations(conversation_id)
66
+ PRIMARY KEY (thread_id, through_sequence),
67
+ FOREIGN KEY (thread_id)
68
+ REFERENCES effect_agent_threads(thread_id)
66
69
  ON DELETE RESTRICT
67
70
  )
68
71
  `.withoutTransform;
69
72
 
70
- yield* sql`PRAGMA user_version = 1`.withoutTransform;
71
- }),
72
- "2_durable_submission_ledger": Effect.gen(function* () {
73
- const sql = yield* SqlClient.SqlClient;
74
-
75
- // Admission rows exist before Conversation materialization (durability §4), so
76
- // conversation_id intentionally carries no foreign key into effect_agent_conversations.
73
+ // Admission rows exist before Thread materialization (durability §4), so
74
+ // thread_id intentionally carries no foreign key into effect_agent_threads.
77
75
  yield* sql`
78
76
  CREATE TABLE effect_agent_submissions (
79
77
  submission_id TEXT PRIMARY KEY NOT NULL,
80
- conversation_id TEXT NOT NULL,
78
+ thread_id TEXT NOT NULL,
81
79
  queue_sequence INTEGER NOT NULL,
82
80
  principal TEXT NOT NULL,
83
81
  idempotency_key TEXT NOT NULL,
@@ -93,11 +91,27 @@ export const sqliteMigrations = SqliteMigrator.fromRecord({
93
91
  ready_at TEXT,
94
92
  input_applied_record_id TEXT,
95
93
  input_applied_sequence INTEGER,
96
- UNIQUE (conversation_id, principal, idempotency_key),
97
- UNIQUE (conversation_id, queue_sequence)
94
+ joined_host_submission_id TEXT,
95
+ suspended_reason_json TEXT,
96
+ suspended_at TEXT,
97
+ unknown_reason TEXT,
98
+ unknown_tool_call_ids_json TEXT,
99
+ parent_submission_id TEXT,
100
+ parent_tool_call_id TEXT,
101
+ admission_group TEXT,
102
+ admission_fence_json TEXT,
103
+ worker_admission_json TEXT,
104
+ message_admission_json TEXT,
105
+ UNIQUE (thread_id, principal, idempotency_key),
106
+ UNIQUE (thread_id, queue_sequence)
98
107
  )
99
108
  `.withoutTransform;
100
109
 
110
+ yield* sql`
111
+ CREATE INDEX effect_agent_submissions_group
112
+ ON effect_agent_submissions (thread_id, admission_group, state)
113
+ `.withoutTransform;
114
+
101
115
  yield* sql`
102
116
  CREATE TABLE effect_agent_submission_ownership (
103
117
  submission_id TEXT PRIMARY KEY NOT NULL,
@@ -116,7 +130,7 @@ export const sqliteMigrations = SqliteMigrator.fromRecord({
116
130
  CREATE TABLE effect_agent_attempts (
117
131
  attempt_id TEXT PRIMARY KEY NOT NULL,
118
132
  submission_id TEXT NOT NULL,
119
- conversation_id TEXT NOT NULL,
133
+ thread_id TEXT NOT NULL,
120
134
  owner_producer_id TEXT NOT NULL,
121
135
  producer_epoch INTEGER NOT NULL,
122
136
  claimed_at TEXT NOT NULL,
@@ -155,42 +169,6 @@ export const sqliteMigrations = SqliteMigrator.fromRecord({
155
169
  )
156
170
  `.withoutTransform;
157
171
 
158
- yield* sql`PRAGMA user_version = 2`.withoutTransform;
159
- }),
160
- "3_durable_tools_and_joined_input": Effect.gen(function* () {
161
- const sql = yield* SqlClient.SqlClient;
162
-
163
- // Joined queued input (plan §2.5, DUR-016): a joining/joined Submission records which host
164
- // Run claimed it. The canonical input marker reuses input_applied_record_id/_sequence
165
- // because the joined input IS the Submission's own canonical `input:{sid}` record.
166
- yield* sql`
167
- ALTER TABLE effect_agent_submissions
168
- ADD COLUMN joined_host_submission_id TEXT
169
- `.withoutTransform;
170
-
171
- // Durable approval suspension (plan §2.6): the reason is the Schema-encoded
172
- // SuspensionReason union so later suspension families stay additive.
173
- yield* sql`
174
- ALTER TABLE effect_agent_submissions
175
- ADD COLUMN suspended_reason_json TEXT
176
- `.withoutTransform;
177
- yield* sql`
178
- ALTER TABLE effect_agent_submissions
179
- ADD COLUMN suspended_at TEXT
180
- `.withoutTransform;
181
-
182
- // Unknown Outcome marking (DUR-009/DUR-017): the marked open Tool Call identities are
183
- // stored so `recordUnknownResolution` can reopen the lane only when every marked call
184
- // has a durable resolution intent.
185
- yield* sql`
186
- ALTER TABLE effect_agent_submissions
187
- ADD COLUMN unknown_reason TEXT
188
- `.withoutTransform;
189
- yield* sql`
190
- ALTER TABLE effect_agent_submissions
191
- ADD COLUMN unknown_tool_call_ids_json TEXT
192
- `.withoutTransform;
193
-
194
172
  yield* sql`
195
173
  CREATE INDEX effect_agent_submissions_joined_host
196
174
  ON effect_agent_submissions (joined_host_submission_id)
@@ -226,21 +204,8 @@ export const sqliteMigrations = SqliteMigrator.fromRecord({
226
204
  )
227
205
  `.withoutTransform;
228
206
 
229
- yield* sql`PRAGMA user_version = 3`.withoutTransform;
230
- }),
231
- "4_durable_subagents": Effect.gen(function* () {
232
- const sql = yield* SqlClient.SqlClient;
233
-
234
207
  // Durable attached children (spec §12, SUB-004): a child Submission records its immutable
235
208
  // parent linkage at admission; the parent-side index serves the recovery attachment view.
236
- yield* sql`
237
- ALTER TABLE effect_agent_submissions
238
- ADD COLUMN parent_submission_id TEXT
239
- `.withoutTransform;
240
- yield* sql`
241
- ALTER TABLE effect_agent_submissions
242
- ADD COLUMN parent_tool_call_id TEXT
243
- `.withoutTransform;
244
209
  yield* sql`
245
210
  CREATE INDEX effect_agent_submissions_parent
246
211
  ON effect_agent_submissions (parent_submission_id)
@@ -270,6 +235,107 @@ export const sqliteMigrations = SqliteMigrator.fromRecord({
270
235
  )
271
236
  `.withoutTransform;
272
237
 
273
- yield* sql`PRAGMA user_version = 4`.withoutTransform;
238
+ // record_json is authoritative. The remaining columns support owner keyset paging and
239
+ // deadline queries without decoding unrelated future schedules.
240
+ yield* sql`
241
+ CREATE TABLE effect_agent_schedules (
242
+ tenant_id TEXT NOT NULL,
243
+ owner_id TEXT NOT NULL,
244
+ schedule_id TEXT NOT NULL,
245
+ deadline_at_millis INTEGER,
246
+ record_json TEXT NOT NULL,
247
+ PRIMARY KEY (tenant_id, owner_id, schedule_id)
248
+ )
249
+ `.withoutTransform;
250
+
251
+ yield* sql`
252
+ CREATE INDEX effect_agent_schedules_deadline
253
+ ON effect_agent_schedules (deadline_at_millis, tenant_id, owner_id, schedule_id)
254
+ WHERE deadline_at_millis IS NOT NULL
255
+ `.withoutTransform;
256
+
257
+ yield* sql`
258
+ CREATE INDEX effect_agent_schedules_owner_deadline
259
+ ON effect_agent_schedules (tenant_id, owner_id, deadline_at_millis, schedule_id)
260
+ WHERE deadline_at_millis IS NOT NULL
261
+ `.withoutTransform;
262
+
263
+ yield* sql`
264
+ CREATE TABLE effect_agent_subscription_sequences (
265
+ tenant_id TEXT NOT NULL,
266
+ source_address TEXT NOT NULL,
267
+ sequence INTEGER NOT NULL,
268
+ event_scan_cursor TEXT NOT NULL,
269
+ delivery_scan_cursor TEXT NOT NULL,
270
+ recovery_scan_cursor INTEGER NOT NULL,
271
+ PRIMARY KEY (tenant_id, source_address)
272
+ )
273
+ `.withoutTransform;
274
+ yield* sql`
275
+ CREATE TABLE effect_agent_subscriptions (
276
+ tenant_id TEXT NOT NULL,
277
+ source_address TEXT NOT NULL,
278
+ owner_id TEXT NOT NULL,
279
+ subscription_id TEXT NOT NULL,
280
+ ordinal INTEGER NOT NULL,
281
+ source_name TEXT NOT NULL,
282
+ source_version TEXT NOT NULL,
283
+ matching_key TEXT NOT NULL,
284
+ state TEXT NOT NULL,
285
+ expires_at_millis INTEGER,
286
+ recovery_at_millis INTEGER,
287
+ recovery_present INTEGER NOT NULL DEFAULT 0,
288
+ record_json TEXT NOT NULL,
289
+ PRIMARY KEY (tenant_id, source_address, owner_id, subscription_id),
290
+ UNIQUE (tenant_id, source_address, ordinal)
291
+ )
292
+ `.withoutTransform;
293
+ yield* sql`CREATE INDEX effect_agent_subscriptions_owner ON effect_agent_subscriptions (tenant_id, source_address, owner_id, ordinal)`
294
+ .withoutTransform;
295
+ yield* sql`CREATE INDEX effect_agent_subscriptions_candidates ON effect_agent_subscriptions (tenant_id, source_address, source_name, source_version, matching_key, ordinal)`
296
+ .withoutTransform;
297
+ yield* sql`CREATE INDEX effect_agent_subscriptions_recovery ON effect_agent_subscriptions (tenant_id, source_address, recovery_at_millis, ordinal) WHERE recovery_at_millis IS NOT NULL`
298
+ .withoutTransform;
299
+ yield* sql`
300
+ CREATE TABLE effect_agent_subscription_events (
301
+ tenant_id TEXT NOT NULL,
302
+ source_address TEXT NOT NULL,
303
+ event_id TEXT NOT NULL,
304
+ source_name TEXT NOT NULL,
305
+ source_version TEXT NOT NULL,
306
+ matching_key TEXT NOT NULL,
307
+ payload_digest TEXT NOT NULL,
308
+ cutoff INTEGER NOT NULL,
309
+ cursor INTEGER NOT NULL,
310
+ routing_complete INTEGER NOT NULL,
311
+ tombstone INTEGER NOT NULL DEFAULT 0,
312
+ next_attempt_at_millis INTEGER NOT NULL,
313
+ record_json TEXT NOT NULL,
314
+ PRIMARY KEY (tenant_id, source_address, event_id)
315
+ )
316
+ `.withoutTransform;
317
+ yield* sql`CREATE INDEX effect_agent_subscription_events_pending ON effect_agent_subscription_events (tenant_id, source_address, routing_complete, next_attempt_at_millis, event_id)`
318
+ .withoutTransform;
319
+ yield* sql`
320
+ CREATE TABLE effect_agent_subscription_deliveries (
321
+ tenant_id TEXT NOT NULL,
322
+ source_address TEXT NOT NULL,
323
+ owner_id TEXT NOT NULL,
324
+ subscription_id TEXT NOT NULL,
325
+ event_id TEXT NOT NULL,
326
+ delivery_key TEXT NOT NULL,
327
+ state TEXT NOT NULL,
328
+ next_attempt_at_millis INTEGER NOT NULL,
329
+ record_json TEXT NOT NULL,
330
+ PRIMARY KEY (tenant_id, source_address, owner_id, subscription_id, event_id),
331
+ UNIQUE (tenant_id, source_address, delivery_key)
332
+ )
333
+ `.withoutTransform;
334
+ yield* sql`CREATE INDEX effect_agent_subscription_deliveries_pending ON effect_agent_subscription_deliveries (tenant_id, source_address, state, next_attempt_at_millis, delivery_key)`
335
+ .withoutTransform;
336
+ yield* sql`CREATE INDEX effect_agent_subscription_deliveries_registration ON effect_agent_subscription_deliveries (tenant_id, source_address, owner_id, subscription_id, delivery_key)`
337
+ .withoutTransform;
338
+ yield* createMessageDeliveryTables;
339
+ yield* sql`PRAGMA user_version = 9`.withoutTransform;
274
340
  }),
275
341
  });