@assemblyline-agents/postgres 0.1.0

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 (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/dist/adapter-checkpoints.d.ts +6 -0
  4. package/dist/adapter-checkpoints.d.ts.map +1 -0
  5. package/dist/adapter-checkpoints.js +110 -0
  6. package/dist/adapter-checkpoints.js.map +1 -0
  7. package/dist/adapter-conversation-turns.d.ts +20 -0
  8. package/dist/adapter-conversation-turns.d.ts.map +1 -0
  9. package/dist/adapter-conversation-turns.js +216 -0
  10. package/dist/adapter-conversation-turns.js.map +1 -0
  11. package/dist/adapter-conversations.d.ts +46 -0
  12. package/dist/adapter-conversations.d.ts.map +1 -0
  13. package/dist/adapter-conversations.js +129 -0
  14. package/dist/adapter-conversations.js.map +1 -0
  15. package/dist/adapter-delivery.d.ts +13 -0
  16. package/dist/adapter-delivery.d.ts.map +1 -0
  17. package/dist/adapter-delivery.js +157 -0
  18. package/dist/adapter-delivery.js.map +1 -0
  19. package/dist/adapter-grants.d.ts +11 -0
  20. package/dist/adapter-grants.d.ts.map +1 -0
  21. package/dist/adapter-grants.js +125 -0
  22. package/dist/adapter-grants.js.map +1 -0
  23. package/dist/adapter-memory.d.ts +21 -0
  24. package/dist/adapter-memory.d.ts.map +1 -0
  25. package/dist/adapter-memory.js +181 -0
  26. package/dist/adapter-memory.js.map +1 -0
  27. package/dist/adapter-misc.d.ts +61 -0
  28. package/dist/adapter-misc.d.ts.map +1 -0
  29. package/dist/adapter-misc.js +127 -0
  30. package/dist/adapter-misc.js.map +1 -0
  31. package/dist/adapter-runs.d.ts +20 -0
  32. package/dist/adapter-runs.d.ts.map +1 -0
  33. package/dist/adapter-runs.js +193 -0
  34. package/dist/adapter-runs.js.map +1 -0
  35. package/dist/adapter-sandbox.d.ts +27 -0
  36. package/dist/adapter-sandbox.d.ts.map +1 -0
  37. package/dist/adapter-sandbox.js +267 -0
  38. package/dist/adapter-sandbox.js.map +1 -0
  39. package/dist/adapter-settings.d.ts +8 -0
  40. package/dist/adapter-settings.d.ts.map +1 -0
  41. package/dist/adapter-settings.js +38 -0
  42. package/dist/adapter-settings.js.map +1 -0
  43. package/dist/adapter-usage.d.ts +7 -0
  44. package/dist/adapter-usage.d.ts.map +1 -0
  45. package/dist/adapter-usage.js +243 -0
  46. package/dist/adapter-usage.js.map +1 -0
  47. package/dist/client-types.d.ts +6 -0
  48. package/dist/client-types.d.ts.map +1 -0
  49. package/dist/client-types.js +2 -0
  50. package/dist/client-types.js.map +1 -0
  51. package/dist/index.d.ts +208 -0
  52. package/dist/index.d.ts.map +1 -0
  53. package/dist/index.js +424 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/migrations.d.ts +35 -0
  56. package/dist/migrations.d.ts.map +1 -0
  57. package/dist/migrations.js +694 -0
  58. package/dist/migrations.js.map +1 -0
  59. package/dist/presets.d.ts +19 -0
  60. package/dist/presets.d.ts.map +1 -0
  61. package/dist/presets.js +31 -0
  62. package/dist/presets.js.map +1 -0
  63. package/dist/provider.d.ts +12 -0
  64. package/dist/provider.d.ts.map +1 -0
  65. package/dist/provider.js +86 -0
  66. package/dist/provider.js.map +1 -0
  67. package/dist/row-mappers.d.ts +24 -0
  68. package/dist/row-mappers.d.ts.map +1 -0
  69. package/dist/row-mappers.js +450 -0
  70. package/dist/row-mappers.js.map +1 -0
  71. package/dist/sql-helpers.d.ts +46 -0
  72. package/dist/sql-helpers.d.ts.map +1 -0
  73. package/dist/sql-helpers.js +244 -0
  74. package/dist/sql-helpers.js.map +1 -0
  75. package/dist/stores.d.ts +40 -0
  76. package/dist/stores.d.ts.map +1 -0
  77. package/dist/stores.js +210 -0
  78. package/dist/stores.js.map +1 -0
  79. package/package.json +49 -0
@@ -0,0 +1,694 @@
1
+ export const POSTGRES_SCHEMA_MIGRATIONS_SQL = `
2
+ create table if not exists openeve_schema_migrations (
3
+ id text primary key,
4
+ checksum text not null,
5
+ description text not null,
6
+ package_version text not null,
7
+ applied_at timestamptz not null
8
+ );
9
+ `;
10
+ export const postgresMigrations = [
11
+ {
12
+ id: "001_openeve_state",
13
+ description: "Core OpenEve runtime state, memory documents, file catalog, sandbox sync worker fields, queues, and idempotency tables.",
14
+ expandContractNotes: "All objects are created with if-not-exists semantics. Existing deployments can expand in place; later cleanup should be additive until all runtimes are upgraded.",
15
+ sql: `
16
+ create table if not exists openeve_runs (
17
+ id text primary key,
18
+ agent_revision text not null,
19
+ status text not null,
20
+ input jsonb not null default '{}'::jsonb,
21
+ context_bundle_id text,
22
+ sandbox_id text,
23
+ sandbox_snapshot_id text,
24
+ memory_revision text,
25
+ created_at timestamptz not null,
26
+ updated_at timestamptz not null
27
+ );
28
+
29
+ create table if not exists openeve_run_events (
30
+ id text primary key,
31
+ run_id text not null references openeve_runs(id),
32
+ sequence integer not null,
33
+ type text not null,
34
+ data jsonb not null default '{}'::jsonb,
35
+ created_at timestamptz not null,
36
+ unique (run_id, sequence)
37
+ );
38
+
39
+ create table if not exists openeve_tool_calls (
40
+ id text primary key,
41
+ run_id text not null references openeve_runs(id),
42
+ tool_name text not null,
43
+ status text not null,
44
+ input jsonb not null,
45
+ result jsonb,
46
+ model_output jsonb,
47
+ error text,
48
+ idempotency_key text not null unique,
49
+ created_at timestamptz not null,
50
+ updated_at timestamptz not null
51
+ );
52
+
53
+ create table if not exists openeve_checkpoints (
54
+ id text primary key,
55
+ run_id text not null references openeve_runs(id),
56
+ name text not null,
57
+ data jsonb not null default '{}'::jsonb,
58
+ created_at timestamptz not null
59
+ );
60
+
61
+ create table if not exists openeve_delivery_obligations (
62
+ id text primary key,
63
+ run_id text not null references openeve_runs(id),
64
+ channel text not null,
65
+ payload jsonb not null,
66
+ idempotency_key text not null unique,
67
+ status text not null,
68
+ created_at timestamptz not null,
69
+ updated_at timestamptz not null
70
+ );
71
+
72
+ create table if not exists openeve_agents (
73
+ id text primary key,
74
+ name text not null,
75
+ manifest jsonb not null,
76
+ created_at timestamptz not null,
77
+ updated_at timestamptz not null
78
+ );
79
+
80
+ create table if not exists openeve_agent_revisions (
81
+ agent_revision text primary key,
82
+ agent_id text references openeve_agents(id),
83
+ manifest jsonb not null,
84
+ source_hashes jsonb not null default '{}'::jsonb,
85
+ created_at timestamptz not null
86
+ );
87
+
88
+ create table if not exists openeve_conversations (
89
+ id text primary key,
90
+ agent_id text references openeve_agents(id),
91
+ channel text not null,
92
+ subject text,
93
+ metadata jsonb not null default '{}'::jsonb,
94
+ created_at timestamptz not null,
95
+ updated_at timestamptz not null
96
+ );
97
+
98
+ create table if not exists openeve_messages (
99
+ id text primary key,
100
+ conversation_id text references openeve_conversations(id),
101
+ run_id text references openeve_runs(id),
102
+ role text not null,
103
+ content jsonb not null,
104
+ created_at timestamptz not null
105
+ );
106
+
107
+ create table if not exists openeve_approval_gates (
108
+ id text primary key,
109
+ run_id text not null references openeve_runs(id),
110
+ tool_call_id text references openeve_tool_calls(id),
111
+ status text not null,
112
+ reason text,
113
+ created_at timestamptz not null,
114
+ updated_at timestamptz not null
115
+ );
116
+
117
+ create table if not exists openeve_schedules (
118
+ id text primary key,
119
+ name text not null,
120
+ cron text not null,
121
+ timezone text,
122
+ metadata jsonb not null default '{}'::jsonb,
123
+ created_at timestamptz not null,
124
+ updated_at timestamptz not null
125
+ );
126
+
127
+ create table if not exists openeve_schedule_runs (
128
+ id text primary key,
129
+ schedule_id text references openeve_schedules(id),
130
+ run_id text references openeve_runs(id),
131
+ idempotency_key text not null unique,
132
+ status text not null,
133
+ run_at timestamptz not null,
134
+ created_at timestamptz not null,
135
+ updated_at timestamptz not null
136
+ );
137
+
138
+ create table if not exists openeve_memory_file_indexes (
139
+ id text primary key,
140
+ run_id text references openeve_runs(id),
141
+ path text not null,
142
+ blob_key text not null,
143
+ sha256 text not null,
144
+ size_bytes integer not null,
145
+ created_at timestamptz not null
146
+ );
147
+
148
+ create unique index if not exists openeve_memory_file_indexes_revision_unique
149
+ on openeve_memory_file_indexes (run_id, path, sha256);
150
+
151
+ create table if not exists openeve_memory_documents (
152
+ id text primary key,
153
+ scope_key text not null unique,
154
+ scope jsonb not null,
155
+ path text not null,
156
+ normalized_path text not null,
157
+ body jsonb not null,
158
+ content_type text not null,
159
+ title text,
160
+ summary text,
161
+ tags jsonb not null default '[]'::jsonb,
162
+ confidence text,
163
+ source_refs jsonb not null default '[]'::jsonb,
164
+ token_estimate integer not null default 0,
165
+ sha256 text not null,
166
+ revision text not null,
167
+ search_vector tsvector generated always as (
168
+ setweight(to_tsvector('simple', coalesce(normalized_path, '')), 'A') ||
169
+ setweight(to_tsvector('simple', coalesce(title, '')), 'A') ||
170
+ setweight(to_tsvector('simple', coalesce(summary, '')), 'B') ||
171
+ setweight(to_tsvector('simple', coalesce(tags::text, '')), 'B') ||
172
+ setweight(to_tsvector('simple', coalesce(body::text, '')), 'C')
173
+ ) stored,
174
+ created_at timestamptz not null,
175
+ updated_at timestamptz not null
176
+ );
177
+
178
+ create index if not exists openeve_memory_documents_scope_path
179
+ on openeve_memory_documents ((scope->>'agentScope'), normalized_path);
180
+
181
+ create index if not exists openeve_memory_documents_scope_user
182
+ on openeve_memory_documents ((scope->>'agentScope'), (scope->>'userId'), updated_at);
183
+
184
+ create index if not exists openeve_memory_documents_scope_conversation
185
+ on openeve_memory_documents ((scope->>'agentScope'), (scope->>'conversationId'), updated_at);
186
+
187
+ create index if not exists openeve_memory_documents_scope_project
188
+ on openeve_memory_documents ((scope->>'agentScope'), (scope->>'projectId'), updated_at);
189
+
190
+ create index if not exists openeve_memory_documents_search_vector
191
+ on openeve_memory_documents using gin (search_vector);
192
+
193
+ create table if not exists openeve_file_catalog_records (
194
+ id text primary key,
195
+ run_id text references openeve_runs(id),
196
+ path text not null,
197
+ kind text not null,
198
+ blob_key text not null,
199
+ sha256 text not null,
200
+ size_bytes integer not null,
201
+ metadata jsonb not null default '{}'::jsonb,
202
+ created_at timestamptz not null
203
+ );
204
+
205
+ create unique index if not exists openeve_file_catalog_records_revision_unique
206
+ on openeve_file_catalog_records (run_id, path, sha256);
207
+
208
+ create table if not exists openeve_sandbox_leases (
209
+ id text primary key,
210
+ run_id text references openeve_runs(id),
211
+ sandbox_id text not null,
212
+ status text not null,
213
+ acquired_at timestamptz not null,
214
+ released_at timestamptz
215
+ );
216
+
217
+ create table if not exists openeve_sandbox_snapshots (
218
+ id text primary key,
219
+ run_id text references openeve_runs(id),
220
+ sandbox_id text not null,
221
+ snapshot_id text not null,
222
+ metadata jsonb not null default '{}'::jsonb,
223
+ created_at timestamptz not null
224
+ );
225
+
226
+ create table if not exists openeve_sandbox_sync_jobs (
227
+ id text primary key,
228
+ run_id text not null references openeve_runs(id),
229
+ sandbox_id text not null,
230
+ status text not null,
231
+ idempotency_key text not null unique,
232
+ reason text not null,
233
+ manifest jsonb not null default '{}'::jsonb,
234
+ error text,
235
+ attempt_count integer not null default 0,
236
+ max_attempts integer not null default 5,
237
+ next_attempt_at timestamptz not null,
238
+ lease_token text,
239
+ lease_expires_at timestamptz,
240
+ completed_at timestamptz,
241
+ failed_at timestamptz,
242
+ last_error text,
243
+ metadata jsonb not null default '{}'::jsonb,
244
+ created_at timestamptz not null,
245
+ updated_at timestamptz not null
246
+ );
247
+
248
+ create index if not exists openeve_sandbox_sync_jobs_due
249
+ on openeve_sandbox_sync_jobs (status, next_attempt_at, created_at);
250
+
251
+ create index if not exists openeve_sandbox_sync_jobs_lease
252
+ on openeve_sandbox_sync_jobs (lease_token, lease_expires_at);
253
+
254
+ create index if not exists openeve_sandbox_sync_jobs_run
255
+ on openeve_sandbox_sync_jobs (run_id, status);
256
+
257
+ create table if not exists openeve_usage_records (
258
+ id text primary key,
259
+ run_id text references openeve_runs(id),
260
+ model text,
261
+ input_tokens integer not null default 0,
262
+ output_tokens integer not null default 0,
263
+ cached_tokens integer not null default 0,
264
+ cost_usd numeric not null default 0,
265
+ metadata jsonb not null default '{}'::jsonb,
266
+ created_at timestamptz not null
267
+ );
268
+
269
+ create table if not exists openeve_run_queue (
270
+ id text primary key,
271
+ run_id text references openeve_runs(id),
272
+ queue text not null,
273
+ status text not null,
274
+ available_at timestamptz not null,
275
+ idempotency_key text not null unique,
276
+ created_at timestamptz not null,
277
+ updated_at timestamptz not null
278
+ );
279
+
280
+ create table if not exists openeve_connection_grants (
281
+ connection_name text not null,
282
+ principal_key text not null,
283
+ principal jsonb not null,
284
+ token text not null,
285
+ refresh_token text,
286
+ token_type text,
287
+ scopes jsonb,
288
+ expires_at bigint,
289
+ created_at timestamptz not null,
290
+ updated_at timestamptz not null,
291
+ primary key (connection_name, principal_key)
292
+ );
293
+
294
+ create table if not exists openeve_connection_authorization_sessions (
295
+ id text primary key,
296
+ state text not null unique,
297
+ connection_name text not null,
298
+ principal_key text not null,
299
+ principal jsonb not null,
300
+ session jsonb not null default '{}'::jsonb,
301
+ callback_url text not null,
302
+ code_verifier text,
303
+ resume jsonb,
304
+ run_id text references openeve_runs(id),
305
+ tool_call_id text references openeve_tool_calls(id),
306
+ tool_name text,
307
+ challenge jsonb not null default '{}'::jsonb,
308
+ continuation jsonb,
309
+ status text not null,
310
+ expires_at bigint not null,
311
+ created_at timestamptz not null,
312
+ updated_at timestamptz not null
313
+ );
314
+
315
+ create table if not exists openeve_idempotency_keys (
316
+ scope text not null,
317
+ key text not null,
318
+ owner_id text,
319
+ created_at timestamptz not null,
320
+ primary key (scope, key)
321
+ );
322
+ `
323
+ },
324
+ {
325
+ id: "002_openeve_self_improvement",
326
+ description: "Durable skills, dynamic schedules, and runtime-provided connection definitions.",
327
+ expandContractNotes: "Adds independent tables only; no contract phase is required.",
328
+ sql: `
329
+ create table if not exists openeve_skills (
330
+ scope text not null,
331
+ name text not null,
332
+ body text not null,
333
+ origin text not null,
334
+ body_sha256 text not null,
335
+ seed_sha256 text,
336
+ pending boolean not null default false,
337
+ updated_at timestamptz not null,
338
+ primary key (scope, name)
339
+ );
340
+
341
+ create table if not exists openeve_dynamic_schedules (
342
+ id text primary key,
343
+ scope text not null,
344
+ prompt text not null,
345
+ channel text not null,
346
+ user_id text,
347
+ conversation_id text,
348
+ target jsonb,
349
+ cron text,
350
+ every_minutes integer,
351
+ timezone text,
352
+ next_run_at timestamptz not null,
353
+ enabled boolean not null default true,
354
+ lease_token text,
355
+ lease_expires_at timestamptz,
356
+ last_run_at timestamptz,
357
+ metadata jsonb,
358
+ created_at timestamptz not null,
359
+ updated_at timestamptz not null
360
+ );
361
+
362
+ create index if not exists openeve_dynamic_schedules_due
363
+ on openeve_dynamic_schedules (enabled, next_run_at);
364
+
365
+ create table if not exists openeve_connection_definitions (
366
+ scope text not null,
367
+ name text not null,
368
+ protocol text not null,
369
+ description text not null,
370
+ definition jsonb not null,
371
+ origin text not null,
372
+ metadata jsonb,
373
+ created_at timestamptz not null,
374
+ updated_at timestamptz not null,
375
+ primary key (scope, name)
376
+ );
377
+ `
378
+ },
379
+ {
380
+ id: "003_openeve_memory_embeddings_pgvector",
381
+ description: "Optional pgvector-backed memory embeddings for semantic search.",
382
+ expandContractNotes: "Requires the vector extension. Keep gated per deployment and backfill embeddings after enabling.",
383
+ optional: true,
384
+ sql: `
385
+ create extension if not exists vector;
386
+
387
+ create table if not exists openeve_memory_embeddings (
388
+ id text primary key,
389
+ scope_key text not null,
390
+ scope jsonb not null,
391
+ path text not null,
392
+ model text not null,
393
+ dimensions integer not null,
394
+ content_sha256 text not null,
395
+ embedding vector,
396
+ stale boolean not null default false,
397
+ metadata jsonb not null default '{}'::jsonb,
398
+ created_at timestamptz not null,
399
+ updated_at timestamptz not null,
400
+ unique (scope_key, model)
401
+ );
402
+
403
+ create index if not exists openeve_memory_embeddings_scope_model
404
+ on openeve_memory_embeddings ((scope->>'agentScope'), model, stale);
405
+
406
+ create index if not exists openeve_memory_embeddings_path_model
407
+ on openeve_memory_embeddings (path, model, content_sha256);
408
+
409
+ create index if not exists openeve_memory_embeddings_vector
410
+ on openeve_memory_embeddings using ivfflat (embedding vector_cosine_ops);
411
+ `
412
+ },
413
+ {
414
+ id: "004_openeve_connection_grant_credentials",
415
+ description: "Adds nullable credential material to connection grants.",
416
+ expandContractNotes: "Additive nullable column only; existing grants remain valid.",
417
+ sql: `
418
+ alter table openeve_connection_grants
419
+ add column if not exists credential jsonb;
420
+ `
421
+ },
422
+ {
423
+ id: "005_openeve_sandbox_sessions",
424
+ description: "Durable sandbox session records for warm reconnect, dirty retention, and async sync diagnostics.",
425
+ expandContractNotes: "Additive table and indexes only; foreground runtime paths use tiny upserts while async workers perform filesystem writeback.",
426
+ sql: `
427
+ create table if not exists openeve_sandbox_sessions (
428
+ id text primary key,
429
+ session_key text not null unique,
430
+ provider text not null,
431
+ sandbox_id text not null,
432
+ run_id text references openeve_runs(id),
433
+ root text,
434
+ state text not null,
435
+ dirty boolean not null default false,
436
+ last_seen_at timestamptz not null,
437
+ last_synced_at timestamptz,
438
+ snapshot_id text,
439
+ sync_job_id text,
440
+ manifest jsonb not null default '{}'::jsonb,
441
+ metadata jsonb not null default '{}'::jsonb,
442
+ created_at timestamptz not null,
443
+ updated_at timestamptz not null
444
+ );
445
+
446
+ create index if not exists openeve_sandbox_sessions_dirty
447
+ on openeve_sandbox_sessions (dirty, updated_at);
448
+
449
+ create index if not exists openeve_sandbox_sessions_provider_state
450
+ on openeve_sandbox_sessions (provider, state, last_seen_at);
451
+ `
452
+ },
453
+ {
454
+ id: "006_openeve_dynamic_schedule_triggers",
455
+ description: "Add trigger lifecycle metadata to dynamic schedules.",
456
+ expandContractNotes: "Additive nullable columns only; older dynamic schedule rows keep using prompt-only dispatch.",
457
+ sql: `
458
+ alter table if exists openeve_dynamic_schedules
459
+ add column if not exists message text,
460
+ add column if not exists project_id text,
461
+ add column if not exists trigger jsonb;
462
+ `
463
+ },
464
+ {
465
+ id: "007_openeve_delivery_queue",
466
+ description: "Durable delivery queue lease/retry columns, due index, and run status sweep index for orphan recovery.",
467
+ expandContractNotes: "Additive columns with defaults and new indexes only. Delivery rows created before this migration are normalized on read (next_attempt_at backfilled from created_at).",
468
+ sql: `
469
+ alter table if exists openeve_delivery_obligations
470
+ add column if not exists attempt_count integer not null default 0,
471
+ add column if not exists max_attempts integer not null default 5,
472
+ add column if not exists next_attempt_at timestamptz,
473
+ add column if not exists lease_token text,
474
+ add column if not exists lease_expires_at timestamptz,
475
+ add column if not exists last_error text,
476
+ add column if not exists sent_at timestamptz,
477
+ add column if not exists failed_at timestamptz;
478
+
479
+ update openeve_delivery_obligations set next_attempt_at = created_at where next_attempt_at is null;
480
+
481
+ create index if not exists openeve_delivery_obligations_due
482
+ on openeve_delivery_obligations (status, next_attempt_at, created_at)
483
+ where status in ('pending', 'sending');
484
+
485
+ create index if not exists openeve_delivery_obligations_lease
486
+ on openeve_delivery_obligations (lease_token, lease_expires_at);
487
+
488
+ create index if not exists openeve_runs_status_updated
489
+ on openeve_runs (status, updated_at);
490
+ `
491
+ },
492
+ {
493
+ id: "008_openeve_checkpoint_retention",
494
+ description: "Indexes for bounded checkpoint retention and operator cleanup.",
495
+ expandContractNotes: "Additive indexes only. Checkpoint pruning remains explicit operator maintenance.",
496
+ sql: `
497
+ create index if not exists openeve_checkpoints_run_name_created
498
+ on openeve_checkpoints (run_id, name, created_at desc);
499
+
500
+ create index if not exists openeve_checkpoints_created
501
+ on openeve_checkpoints (created_at);
502
+ `
503
+ },
504
+ {
505
+ id: "009_openeve_runtime_controls",
506
+ description: "Durable run control intents, scoped runtime settings, and operator audit events.",
507
+ expandContractNotes: "Additive nullable column and new tables only; older runtimes ignore them safely during rolling upgrades.",
508
+ sql: `
509
+ alter table if exists openeve_runs
510
+ add column if not exists control_intent text;
511
+
512
+ create table if not exists openeve_runtime_settings (
513
+ scope text not null,
514
+ key text not null,
515
+ value jsonb not null,
516
+ updated_at timestamptz not null,
517
+ primary key (scope, key)
518
+ );
519
+
520
+ create table if not exists openeve_runtime_control_events (
521
+ id text primary key,
522
+ scope text not null,
523
+ type text not null,
524
+ data jsonb not null default '{}'::jsonb,
525
+ created_at timestamptz not null
526
+ );
527
+
528
+ create index if not exists openeve_runtime_control_events_scope_created
529
+ on openeve_runtime_control_events (scope, created_at);
530
+ `
531
+ },
532
+ {
533
+ id: "010_openeve_direct_conversations",
534
+ description: "Stable agent scoping, titles, archiving, and cursor indexes for direct conversation clients.",
535
+ expandContractNotes: "Additive nullable columns and indexes only. Existing conversations are backfilled from their earliest associated run when possible.",
536
+ sql: `
537
+ alter table if exists openeve_conversations
538
+ add column if not exists agent_scope text,
539
+ add column if not exists agent_revision text,
540
+ add column if not exists title text,
541
+ add column if not exists archived_at timestamptz;
542
+
543
+ update openeve_conversations conversation
544
+ set agent_revision = (
545
+ select run.agent_revision
546
+ from openeve_messages message
547
+ join openeve_runs run on run.id = message.run_id
548
+ where message.conversation_id = conversation.id
549
+ order by message.created_at asc
550
+ limit 1
551
+ )
552
+ where conversation.agent_revision is null;
553
+
554
+ update openeve_conversations
555
+ set agent_scope = coalesce(agent_scope, agent_revision, agent_id)
556
+ where agent_scope is null;
557
+
558
+ create index if not exists openeve_conversations_scope_channel_updated
559
+ on openeve_conversations (agent_scope, channel, updated_at desc, id desc);
560
+
561
+ create index if not exists openeve_messages_conversation_created
562
+ on openeve_messages (conversation_id, created_at desc, id desc);
563
+ `
564
+ },
565
+ {
566
+ id: "011_openeve_usage_accounting",
567
+ description: "Source-backed idempotent usage ledger, exact monetary fields, provenance, and query indexes.",
568
+ expandContractNotes: "Adds observational accounting columns, marks legacy cash unavailable rather than estimating it, and widens token counters. The legacy cost_usd column remains for rolling compatibility but is not authoritative.",
569
+ sql: `
570
+ alter table if exists openeve_usage_records
571
+ alter column input_tokens type bigint,
572
+ alter column output_tokens type bigint,
573
+ alter column cached_tokens type bigint,
574
+ add column if not exists idempotency_key text,
575
+ add column if not exists parent_run_id text,
576
+ add column if not exists agent_id text,
577
+ add column if not exists agent_revision text,
578
+ add column if not exists subagent text,
579
+ add column if not exists turn_id text,
580
+ add column if not exists iteration integer,
581
+ add column if not exists provider text,
582
+ add column if not exists provider_request_id text,
583
+ add column if not exists response_model text,
584
+ add column if not exists billing_mode text not null default 'unknown',
585
+ add column if not exists record_kind text not null default 'transaction',
586
+ add column if not exists cache_write_tokens bigint not null default 0,
587
+ add column if not exists reasoning_tokens bigint not null default 0,
588
+ add column if not exists cost_micros bigint,
589
+ add column if not exists upstream_cost_micros bigint,
590
+ add column if not exists currency text not null default 'USD',
591
+ add column if not exists token_source text not null default 'provider_reported',
592
+ add column if not exists cost_source text not null default 'unavailable',
593
+ add column if not exists provider_receipt jsonb not null default '{}'::jsonb,
594
+ add column if not exists receipt_sha256 text,
595
+ add column if not exists occurred_at timestamptz;
596
+
597
+ update openeve_usage_records
598
+ set idempotency_key = coalesce(idempotency_key, 'legacy:' || id),
599
+ cost_micros = null,
600
+ cost_source = 'unavailable',
601
+ occurred_at = coalesce(occurred_at, created_at)
602
+ where idempotency_key is null or occurred_at is null;
603
+
604
+ update openeve_usage_records
605
+ set input_tokens = 0,
606
+ output_tokens = 0,
607
+ cached_tokens = 0,
608
+ cache_write_tokens = 0,
609
+ reasoning_tokens = 0,
610
+ token_source = 'unavailable'
611
+ where token_source not in ('provider_reported', 'provider_reconciled');
612
+
613
+ update openeve_usage_records
614
+ set cost_micros = null,
615
+ upstream_cost_micros = null,
616
+ cost_source = 'unavailable'
617
+ where cost_source not in ('provider_reported', 'provider_reconciled');
618
+
619
+ alter table if exists openeve_usage_records
620
+ alter column idempotency_key set not null,
621
+ alter column occurred_at set not null;
622
+
623
+ create unique index if not exists openeve_usage_records_idempotency
624
+ on openeve_usage_records (idempotency_key);
625
+
626
+ create index if not exists openeve_usage_records_occurred
627
+ on openeve_usage_records (occurred_at desc, id desc);
628
+
629
+ create index if not exists openeve_usage_records_agent_occurred
630
+ on openeve_usage_records (agent_id, occurred_at desc);
631
+
632
+ create index if not exists openeve_usage_records_run_occurred
633
+ on openeve_usage_records (run_id, occurred_at);
634
+
635
+ create index if not exists openeve_usage_records_provider_request
636
+ on openeve_usage_records (provider, provider_request_id)
637
+ where provider_request_id is not null;
638
+ `
639
+ },
640
+ {
641
+ id: "012_openeve_run_terminal_outcome",
642
+ description: "Queryable terminal reason/error on runs and poison-schedule failure tracking.",
643
+ expandContractNotes: "Additive nullable columns only; older runtimes ignore them safely during rolling upgrades.",
644
+ sql: `
645
+ alter table if exists openeve_runs
646
+ add column if not exists terminal_reason text,
647
+ add column if not exists terminal_error text;
648
+
649
+ alter table if exists openeve_dynamic_schedules
650
+ add column if not exists consecutive_failures integer not null default 0;
651
+ `
652
+ },
653
+ {
654
+ id: "013_openeve_conversation_turn_mailbox",
655
+ description: "Durable per-conversation FIFO ingress mailbox with cross-replica active-turn exclusion.",
656
+ expandContractNotes: "Adds a new mailbox table and indexes only; older runtimes ignore it safely during rolling upgrades.",
657
+ sql: `
658
+ create table if not exists openeve_conversation_turns (
659
+ id text primary key,
660
+ enqueue_sequence bigserial not null,
661
+ agent_scope text not null,
662
+ conversation_id text not null,
663
+ channel text not null,
664
+ event_id text not null,
665
+ payload jsonb not null,
666
+ idempotency_scope text not null,
667
+ idempotency_key text not null,
668
+ status text not null,
669
+ run_id text references openeve_runs(id),
670
+ attempt_count integer not null default 0,
671
+ max_attempts integer not null default 5,
672
+ next_attempt_at timestamptz not null,
673
+ lease_token text,
674
+ lease_expires_at timestamptz,
675
+ last_error text,
676
+ created_at timestamptz not null,
677
+ updated_at timestamptz not null,
678
+ unique (agent_scope, idempotency_scope, idempotency_key)
679
+ );
680
+
681
+ create unique index if not exists openeve_conversation_turns_one_active
682
+ on openeve_conversation_turns (agent_scope, conversation_id)
683
+ where status in ('running', 'blocked');
684
+
685
+ create index if not exists openeve_conversation_turns_ready
686
+ on openeve_conversation_turns (agent_scope, status, next_attempt_at, enqueue_sequence);
687
+
688
+ create index if not exists openeve_conversation_turns_run
689
+ on openeve_conversation_turns (run_id)
690
+ where run_id is not null;
691
+ `
692
+ }
693
+ ];
694
+ //# sourceMappingURL=migrations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrations.js","sourceRoot":"","sources":["../src/migrations.ts"],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