@tenetkit/pg 0.31.0 → 0.33.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.
@@ -1,16 +1,17 @@
1
- export const SCHEMA_VERSION = 7;
2
- export const SCHEMA_META_TABLE = "baton_schema_meta";
3
- export const MIGRATIONS_TABLE = "baton_sql_migrations";
4
- export const NOTIFY_CHANNEL = "baton_run_events";
1
+ export const SCHEMA_VERSION = 1;
2
+ export const MIGRATION_NAME = "tenetkit_runtime";
3
+ export const SCHEMA_META_TABLE = "tenetkit_schema_meta";
4
+ export const MIGRATIONS_TABLE = "tenetkit_sql_migrations";
5
+ export const NOTIFY_CHANNEL = "tenetkit_run_events";
5
6
  export const SCHEMA_STATEMENTS = [
6
- `CREATE TABLE IF NOT EXISTS baton_schema_meta (
7
+ `CREATE TABLE IF NOT EXISTS tenetkit_schema_meta (
7
8
  id INTEGER PRIMARY KEY CHECK (id = 1),
8
9
  version INTEGER NOT NULL,
9
10
  checksum TEXT NOT NULL,
10
11
  dirty BOOLEAN NOT NULL DEFAULT FALSE,
11
12
  applied_at TIMESTAMPTZ NOT NULL
12
13
  )`,
13
- `CREATE TABLE IF NOT EXISTS baton_lanes (
14
+ `CREATE TABLE IF NOT EXISTS tenetkit_lanes (
14
15
  address TEXT NOT NULL,
15
16
  session_id TEXT NOT NULL,
16
17
  accepted_sequence BIGINT NOT NULL,
@@ -18,7 +19,7 @@ export const SCHEMA_STATEMENTS = [
18
19
  head_run_id TEXT,
19
20
  PRIMARY KEY (address, session_id)
20
21
  )`,
21
- `CREATE TABLE IF NOT EXISTS baton_runs (
22
+ `CREATE TABLE IF NOT EXISTS tenetkit_runs (
22
23
  run_id TEXT PRIMARY KEY,
23
24
  status TEXT NOT NULL,
24
25
  address TEXT NOT NULL,
@@ -54,15 +55,15 @@ export const SCHEMA_STATEMENTS = [
54
55
  updated_at TIMESTAMPTZ NOT NULL,
55
56
  UNIQUE (address, session_id, idempotency_key)
56
57
  )`,
57
- `CREATE TABLE IF NOT EXISTS baton_run_events (
58
- run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
58
+ `CREATE TABLE IF NOT EXISTS tenetkit_run_events (
59
+ run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
59
60
  sequence INTEGER NOT NULL,
60
61
  event_id TEXT NOT NULL UNIQUE,
61
62
  event_json TEXT NOT NULL,
62
63
  PRIMARY KEY (run_id, sequence)
63
64
  )`,
64
- `CREATE TABLE IF NOT EXISTS baton_run_operations (
65
- run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
65
+ `CREATE TABLE IF NOT EXISTS tenetkit_run_operations (
66
+ run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
66
67
  operation_id TEXT NOT NULL,
67
68
  operation_key TEXT NOT NULL,
68
69
  kind TEXT NOT NULL,
@@ -82,8 +83,8 @@ export const SCHEMA_STATEMENTS = [
82
83
  PRIMARY KEY (run_id, operation_id),
83
84
  UNIQUE (run_id, operation_key)
84
85
  )`,
85
- `CREATE TABLE IF NOT EXISTS baton_run_waits (
86
- run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
86
+ `CREATE TABLE IF NOT EXISTS tenetkit_run_waits (
87
+ run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
87
88
  wait_id TEXT NOT NULL,
88
89
  reason TEXT NOT NULL,
89
90
  status TEXT NOT NULL,
@@ -95,9 +96,9 @@ export const SCHEMA_STATEMENTS = [
95
96
  closed_at TIMESTAMPTZ,
96
97
  PRIMARY KEY (run_id, wait_id)
97
98
  )`,
98
- `CREATE TABLE IF NOT EXISTS baton_run_links (
99
- parent_run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
100
- child_run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
99
+ `CREATE TABLE IF NOT EXISTS tenetkit_run_links (
100
+ parent_run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
101
+ child_run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
101
102
  invocation_id TEXT NOT NULL,
102
103
  readiness TEXT NOT NULL,
103
104
  terminal_event_id TEXT,
@@ -106,10 +107,10 @@ export const SCHEMA_STATEMENTS = [
106
107
  PRIMARY KEY (parent_run_id, child_run_id),
107
108
  UNIQUE (child_run_id)
108
109
  )`,
109
- `CREATE INDEX IF NOT EXISTS baton_run_links_readiness_idx ON baton_run_links(parent_run_id, readiness, created_at, child_run_id)`,
110
- `CREATE TABLE IF NOT EXISTS baton_run_steering (
110
+ `CREATE INDEX IF NOT EXISTS tenetkit_run_links_readiness_idx ON tenetkit_run_links(parent_run_id, readiness, created_at, child_run_id)`,
111
+ `CREATE TABLE IF NOT EXISTS tenetkit_run_steering (
111
112
  entry_id TEXT PRIMARY KEY,
112
- run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
113
+ run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
113
114
  sequence BIGINT NOT NULL,
114
115
  idempotency_key TEXT NOT NULL,
115
116
  digest TEXT NOT NULL,
@@ -119,16 +120,16 @@ export const SCHEMA_STATEMENTS = [
119
120
  UNIQUE (run_id, sequence),
120
121
  UNIQUE (run_id, idempotency_key)
121
122
  )`,
122
- `CREATE INDEX IF NOT EXISTS baton_run_steering_pending_idx
123
- ON baton_run_steering(run_id, sequence) WHERE consumed_operation_id IS NULL AND discarded_reason IS NULL`,
124
- `CREATE TABLE IF NOT EXISTS baton_agent_names (
123
+ `CREATE INDEX IF NOT EXISTS tenetkit_run_steering_pending_idx
124
+ ON tenetkit_run_steering(run_id, sequence) WHERE consumed_operation_id IS NULL AND discarded_reason IS NULL`,
125
+ `CREATE TABLE IF NOT EXISTS tenetkit_agent_names (
125
126
  scope TEXT NOT NULL,
126
127
  name TEXT NOT NULL,
127
- run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
128
+ run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
128
129
  PRIMARY KEY (scope, name)
129
130
  )`,
130
- `CREATE INDEX IF NOT EXISTS baton_agent_names_run_idx ON baton_agent_names(run_id)`,
131
- `CREATE TABLE IF NOT EXISTS baton_messages (
131
+ `CREATE INDEX IF NOT EXISTS tenetkit_agent_names_run_idx ON tenetkit_agent_names(run_id)`,
132
+ `CREATE TABLE IF NOT EXISTS tenetkit_messages (
132
133
  entry_id TEXT PRIMARY KEY,
133
134
  target_session_id TEXT NOT NULL,
134
135
  sequence BIGINT NOT NULL,
@@ -150,17 +151,17 @@ export const SCHEMA_STATEMENTS = [
150
151
  UNIQUE (target_session_id, message_id, idempotency_key),
151
152
  UNIQUE (target_session_id, sequence)
152
153
  )`,
153
- `CREATE INDEX IF NOT EXISTS baton_messages_pending_idx
154
- ON baton_messages(target_session_id, sequence) WHERE delivered_run_id IS NULL`,
155
- `CREATE INDEX IF NOT EXISTS baton_runs_claim_idx
156
- ON baton_runs(status, lease_expires_at)
154
+ `CREATE INDEX IF NOT EXISTS tenetkit_messages_pending_idx
155
+ ON tenetkit_messages(target_session_id, sequence) WHERE delivered_run_id IS NULL`,
156
+ `CREATE INDEX IF NOT EXISTS tenetkit_runs_claim_idx
157
+ ON tenetkit_runs(status, lease_expires_at)
157
158
  WHERE status IN ('queued', 'running', 'waiting', 'needs-resolution', 'cancelling')`,
158
- `CREATE INDEX IF NOT EXISTS baton_lanes_head_idx ON baton_lanes(head_run_id)`,
159
- `CREATE INDEX IF NOT EXISTS baton_run_operations_status_idx ON baton_run_operations(status)`,
160
- `CREATE INDEX IF NOT EXISTS baton_run_waits_due_idx ON baton_run_waits(status, due_at)`,
161
- `CREATE TABLE IF NOT EXISTS baton_fan_outs (
159
+ `CREATE INDEX IF NOT EXISTS tenetkit_lanes_head_idx ON tenetkit_lanes(head_run_id)`,
160
+ `CREATE INDEX IF NOT EXISTS tenetkit_run_operations_status_idx ON tenetkit_run_operations(status)`,
161
+ `CREATE INDEX IF NOT EXISTS tenetkit_run_waits_due_idx ON tenetkit_run_waits(status, due_at)`,
162
+ `CREATE TABLE IF NOT EXISTS tenetkit_fan_outs (
162
163
  fan_out_id TEXT PRIMARY KEY,
163
- parent_run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
164
+ parent_run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
164
165
  idempotency_key TEXT NOT NULL,
165
166
  input_digest TEXT NOT NULL,
166
167
  join_json TEXT NOT NULL,
@@ -171,15 +172,15 @@ export const SCHEMA_STATEMENTS = [
171
172
  updated_at TIMESTAMPTZ NOT NULL,
172
173
  UNIQUE (parent_run_id, idempotency_key)
173
174
  )`,
174
- `CREATE TABLE IF NOT EXISTS baton_fan_out_members (
175
- fan_out_id TEXT NOT NULL REFERENCES baton_fan_outs(fan_out_id),
175
+ `CREATE TABLE IF NOT EXISTS tenetkit_fan_out_members (
176
+ fan_out_id TEXT NOT NULL REFERENCES tenetkit_fan_outs(fan_out_id),
176
177
  ordinal INTEGER NOT NULL,
177
178
  member_key TEXT NOT NULL,
178
179
  selection TEXT NOT NULL,
179
180
  display_label TEXT,
180
181
  prompt_json TEXT NOT NULL,
181
182
  origin_json TEXT,
182
- child_run_id TEXT NOT NULL UNIQUE REFERENCES baton_runs(run_id),
183
+ child_run_id TEXT NOT NULL UNIQUE REFERENCES tenetkit_runs(run_id),
183
184
  depth INTEGER NOT NULL,
184
185
  status TEXT NOT NULL,
185
186
  terminal_event_id TEXT,
@@ -187,24 +188,24 @@ export const SCHEMA_STATEMENTS = [
187
188
  PRIMARY KEY (fan_out_id, ordinal),
188
189
  UNIQUE (fan_out_id, member_key)
189
190
  )`,
190
- `CREATE INDEX IF NOT EXISTS baton_fan_out_members_status_idx ON baton_fan_out_members(fan_out_id, status, ordinal)`,
191
- `CREATE TABLE IF NOT EXISTS baton_tree_roots (
192
- root_run_id TEXT PRIMARY KEY REFERENCES baton_runs(run_id),
191
+ `CREATE INDEX IF NOT EXISTS tenetkit_fan_out_members_status_idx ON tenetkit_fan_out_members(fan_out_id, status, ordinal)`,
192
+ `CREATE TABLE IF NOT EXISTS tenetkit_tree_roots (
193
+ root_run_id TEXT PRIMARY KEY REFERENCES tenetkit_runs(run_id),
193
194
  earliest_position BIGINT NOT NULL DEFAULT 0,
194
195
  last_position BIGINT NOT NULL DEFAULT -1
195
196
  )`,
196
- `CREATE TABLE IF NOT EXISTS baton_tree_event_index (
197
- root_run_id TEXT NOT NULL REFERENCES baton_tree_roots(root_run_id),
197
+ `CREATE TABLE IF NOT EXISTS tenetkit_tree_event_index (
198
+ root_run_id TEXT NOT NULL REFERENCES tenetkit_tree_roots(root_run_id),
198
199
  position BIGINT NOT NULL,
199
200
  run_id TEXT NOT NULL,
200
201
  run_sequence INTEGER NOT NULL,
201
- event_id TEXT NOT NULL UNIQUE REFERENCES baton_run_events(event_id),
202
+ event_id TEXT NOT NULL UNIQUE REFERENCES tenetkit_run_events(event_id),
202
203
  PRIMARY KEY (root_run_id, position),
203
204
  UNIQUE (run_id, run_sequence),
204
- FOREIGN KEY (run_id, run_sequence) REFERENCES baton_run_events(run_id, sequence)
205
+ FOREIGN KEY (run_id, run_sequence) REFERENCES tenetkit_run_events(run_id, sequence)
205
206
  )`,
206
- `CREATE TABLE IF NOT EXISTS baton_program_runs (
207
- run_id TEXT PRIMARY KEY REFERENCES baton_runs(run_id),
207
+ `CREATE TABLE IF NOT EXISTS tenetkit_program_runs (
208
+ run_id TEXT PRIMARY KEY REFERENCES tenetkit_runs(run_id),
208
209
  program_pin TEXT NOT NULL,
209
210
  budget_json TEXT NOT NULL,
210
211
  deadline_millis BIGINT NOT NULL,
@@ -214,8 +215,8 @@ export const SCHEMA_STATEMENTS = [
214
215
  log_bytes BIGINT NOT NULL DEFAULT 0,
215
216
  active_slots BIGINT NOT NULL DEFAULT 0
216
217
  )`,
217
- `CREATE TABLE IF NOT EXISTS baton_program_operations (
218
- run_id TEXT NOT NULL REFERENCES baton_program_runs(run_id),
218
+ `CREATE TABLE IF NOT EXISTS tenetkit_program_operations (
219
+ run_id TEXT NOT NULL REFERENCES tenetkit_program_runs(run_id),
219
220
  operation_name TEXT NOT NULL,
220
221
  kind TEXT NOT NULL,
221
222
  capability TEXT NOT NULL,
@@ -232,27 +233,27 @@ export const SCHEMA_STATEMENTS = [
232
233
  resolution_json TEXT,
233
234
  PRIMARY KEY (run_id, operation_name)
234
235
  )`,
235
- `CREATE TABLE IF NOT EXISTS baton_executable_registrations (
236
+ `CREATE TABLE IF NOT EXISTS tenetkit_executable_registrations (
236
237
  pin TEXT PRIMARY KEY,
237
238
  codec TEXT NOT NULL,
238
239
  version TEXT NOT NULL,
239
240
  payload_json TEXT NOT NULL,
240
241
  registration_digest TEXT NOT NULL
241
242
  )`,
242
- `CREATE TABLE IF NOT EXISTS baton_run_registrations (
243
- run_id TEXT NOT NULL REFERENCES baton_runs(run_id),
244
- pin TEXT NOT NULL REFERENCES baton_executable_registrations(pin),
243
+ `CREATE TABLE IF NOT EXISTS tenetkit_run_registrations (
244
+ run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
245
+ pin TEXT NOT NULL REFERENCES tenetkit_executable_registrations(pin),
245
246
  PRIMARY KEY (run_id, pin)
246
247
  )`,
247
- `CREATE INDEX IF NOT EXISTS baton_run_registrations_pin_idx ON baton_run_registrations(pin)`,
248
- `CREATE TABLE IF NOT EXISTS baton_sessions (
248
+ `CREATE INDEX IF NOT EXISTS tenetkit_run_registrations_pin_idx ON tenetkit_run_registrations(pin)`,
249
+ `CREATE TABLE IF NOT EXISTS tenetkit_sessions (
249
250
  session_id TEXT PRIMARY KEY,
250
251
  leaf_id TEXT,
251
252
  next_seq BIGINT NOT NULL DEFAULT 0,
252
253
  owner_token TEXT,
253
254
  updated_at TIMESTAMPTZ NOT NULL
254
255
  )`,
255
- `CREATE TABLE IF NOT EXISTS baton_session_entries (
256
+ `CREATE TABLE IF NOT EXISTS tenetkit_session_entries (
256
257
  session_id TEXT NOT NULL,
257
258
  entry_id TEXT NOT NULL,
258
259
  parent_id TEXT,
@@ -262,9 +263,34 @@ export const SCHEMA_STATEMENTS = [
262
263
  created_at TIMESTAMPTZ NOT NULL,
263
264
  PRIMARY KEY (session_id, entry_id)
264
265
  )`,
265
- `CREATE UNIQUE INDEX IF NOT EXISTS baton_session_entries_seq_idx ON baton_session_entries(session_id, seq)`,
266
- `CREATE INDEX IF NOT EXISTS baton_session_entries_parent_idx ON baton_session_entries(session_id, parent_id)`,
266
+ `CREATE UNIQUE INDEX IF NOT EXISTS tenetkit_session_entries_seq_idx ON tenetkit_session_entries(session_id, seq)`,
267
+ `CREATE INDEX IF NOT EXISTS tenetkit_session_entries_parent_idx ON tenetkit_session_entries(session_id, parent_id)`,
268
+ `CREATE TABLE IF NOT EXISTS tenetkit_external_child_placements (
269
+ placement_id TEXT PRIMARY KEY,
270
+ parent_run_id TEXT NOT NULL REFERENCES tenetkit_runs(run_id),
271
+ partition TEXT NOT NULL,
272
+ external_run_id TEXT NOT NULL,
273
+ invocation_id TEXT NOT NULL,
274
+ request_digest TEXT NOT NULL,
275
+ executable_digest TEXT NOT NULL,
276
+ wait_id TEXT,
277
+ suspension_identity TEXT,
278
+ acknowledged BOOLEAN NOT NULL DEFAULT FALSE,
279
+ cancel_requested BOOLEAN NOT NULL DEFAULT FALSE,
280
+ settlement_id TEXT,
281
+ outcome_json TEXT,
282
+ outcome_event_id TEXT,
283
+ created_at TIMESTAMPTZ NOT NULL,
284
+ settled_at TIMESTAMPTZ,
285
+ UNIQUE (partition, external_run_id),
286
+ UNIQUE (parent_run_id, invocation_id),
287
+ CHECK ((settlement_id IS NULL AND outcome_json IS NULL AND outcome_event_id IS NULL AND settled_at IS NULL)
288
+ OR (settlement_id IS NOT NULL AND outcome_json IS NOT NULL AND outcome_event_id IS NOT NULL AND settled_at IS NOT NULL))
289
+ )`,
290
+ `CREATE INDEX IF NOT EXISTS tenetkit_external_child_placements_parent_idx
291
+ ON tenetkit_external_child_placements(parent_run_id, settlement_id, created_at)`,
267
292
  ];
293
+ export const SCHEMA_TABLES = SCHEMA_STATEMENTS.flatMap((statement) => statement.match(/^CREATE TABLE IF NOT EXISTS (\w+)/)?.slice(1, 2) ?? []);
268
294
  export const schemaChecksum = () => {
269
295
  const hasher = new Bun.CryptoHasher("sha256");
270
296
  hasher.update(SCHEMA_STATEMENTS.join("\n"));
@@ -6,12 +6,12 @@ const { encodePayload, entryPayloadEquivalence, pathFromRows, requireActive, sto
6
6
  const lockSession = (sessionId) => Effect.gen(function* () {
7
7
  const sql = yield* SqlClient.SqlClient;
8
8
  yield* sql `
9
- INSERT INTO baton_sessions (session_id, leaf_id, next_seq, owner_token, updated_at)
9
+ INSERT INTO tenetkit_sessions (session_id, leaf_id, next_seq, owner_token, updated_at)
10
10
  VALUES (${sessionId}, NULL, 0, NULL, NOW())
11
11
  ON CONFLICT (session_id) DO NOTHING
12
12
  `;
13
13
  const rows = yield* sql `
14
- SELECT leaf_id, next_seq, owner_token FROM baton_sessions
14
+ SELECT leaf_id, next_seq, owner_token FROM tenetkit_sessions
15
15
  WHERE session_id = ${sessionId}
16
16
  FOR UPDATE
17
17
  `;
@@ -21,14 +21,14 @@ const lockSession = (sessionId) => Effect.gen(function* () {
21
21
  const loadEntries = (sessionId) => Effect.gen(function* () {
22
22
  const sql = yield* SqlClient.SqlClient;
23
23
  return yield* sql `
24
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
24
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
25
25
  WHERE session_id = ${sessionId} ORDER BY seq
26
26
  `;
27
27
  });
28
28
  const insertEntry = (input) => Effect.gen(function* () {
29
29
  const sql = yield* SqlClient.SqlClient;
30
30
  yield* sql `
31
- INSERT INTO baton_session_entries (session_id, entry_id, parent_id, seq, tag, payload_json, created_at)
31
+ INSERT INTO tenetkit_session_entries (session_id, entry_id, parent_id, seq, tag, payload_json, created_at)
32
32
  VALUES (${input.sessionId}, ${input.id}, ${input.parentId}, ${input.seq}, ${input.tag},
33
33
  ${encodePayload(input.payload)}, NOW())
34
34
  `;
@@ -37,13 +37,13 @@ const advanceSession = (input) => Effect.gen(function* () {
37
37
  const sql = yield* SqlClient.SqlClient;
38
38
  if (input.ownerToken === undefined) {
39
39
  yield* sql `
40
- UPDATE baton_sessions SET leaf_id = ${input.leafId}, next_seq = ${input.nextSeq}, updated_at = NOW()
40
+ UPDATE tenetkit_sessions SET leaf_id = ${input.leafId}, next_seq = ${input.nextSeq}, updated_at = NOW()
41
41
  WHERE session_id = ${input.sessionId}
42
42
  `;
43
43
  }
44
44
  else {
45
45
  yield* sql `
46
- UPDATE baton_sessions SET leaf_id = ${input.leafId}, next_seq = ${input.nextSeq},
46
+ UPDATE tenetkit_sessions SET leaf_id = ${input.leafId}, next_seq = ${input.nextSeq},
47
47
  owner_token = ${input.ownerToken}, updated_at = NOW()
48
48
  WHERE session_id = ${input.sessionId}
49
49
  `;
@@ -21,7 +21,7 @@ export const appendCompletedSessionEntry = (input) => Effect.gen(function* () {
21
21
  const sql = yield* SqlClient.SqlClient;
22
22
  const session = yield* lockSession(input.sessionId);
23
23
  const rows = yield* sql `
24
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
24
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
25
25
  WHERE session_id = ${input.sessionId} AND entry_id = ${input.entryId}
26
26
  `;
27
27
  const existing = rows[0];
@@ -65,7 +65,7 @@ export const verifyCompletedSessionEntry = (input) => Effect.gen(function* () {
65
65
  const sql = yield* SqlClient.SqlClient;
66
66
  const session = yield* lockSession(input.sessionId);
67
67
  const rows = yield* sql `
68
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
68
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
69
69
  WHERE session_id = ${input.sessionId} AND entry_id = ${input.entryId}
70
70
  `;
71
71
  const existing = rows[0];
@@ -86,7 +86,7 @@ export const appendInterruptedSessionEntry = (input) => Effect.gen(function* ()
86
86
  const sql = yield* SqlClient.SqlClient;
87
87
  const session = yield* lockSession(input.sessionId);
88
88
  const rows = yield* sql `
89
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
89
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
90
90
  WHERE session_id = ${input.sessionId} AND entry_id = ${input.entryId}
91
91
  `;
92
92
  const existing = rows[0];
@@ -129,7 +129,7 @@ export const verifyInterruptedSessionEntry = (input) => Effect.gen(function* ()
129
129
  const sql = yield* SqlClient.SqlClient;
130
130
  const session = yield* lockSession(input.sessionId);
131
131
  const rows = yield* sql `
132
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
132
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
133
133
  WHERE session_id = ${input.sessionId} AND entry_id = ${input.entryId}
134
134
  `;
135
135
  const existing = rows[0];
@@ -150,7 +150,7 @@ export const appendHandoffSessionEntry = (input) => Effect.gen(function* () {
150
150
  const sql = yield* SqlClient.SqlClient;
151
151
  const session = yield* lockSession(input.sessionId);
152
152
  const rows = yield* sql `
153
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
153
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
154
154
  WHERE session_id = ${input.sessionId} AND entry_id = ${input.entryId}
155
155
  `;
156
156
  const existing = rows[0];
@@ -193,7 +193,7 @@ export const verifyHandoffSessionEntry = (input) => Effect.gen(function* () {
193
193
  const sql = yield* SqlClient.SqlClient;
194
194
  const session = yield* lockSession(input.sessionId);
195
195
  const rows = yield* sql `
196
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
196
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
197
197
  WHERE session_id = ${input.sessionId} AND entry_id = ${input.entryId}
198
198
  `;
199
199
  const existing = rows[0];
@@ -221,7 +221,7 @@ export const makePostgresSessionStore = (options) => {
221
221
  const session = yield* lockSession(sessionId);
222
222
  if (appendOptions?.id !== undefined) {
223
223
  const rows = yield* sql `
224
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
224
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
225
225
  WHERE session_id = ${sessionId} AND entry_id = ${appendOptions.id}
226
226
  `;
227
227
  const existing = rows[0];
@@ -249,7 +249,7 @@ export const makePostgresSessionStore = (options) => {
249
249
  if (appendOptions?.id === undefined) {
250
250
  while (true) {
251
251
  const collision = yield* sql `
252
- SELECT entry_id FROM baton_session_entries
252
+ SELECT entry_id FROM tenetkit_session_entries
253
253
  WHERE session_id = ${sessionId} AND entry_id = ${String(generatedSequence)}
254
254
  `;
255
255
  if (collision[0] === undefined)
@@ -278,7 +278,7 @@ export const makePostgresSessionStore = (options) => {
278
278
  const sql = yield* SqlClient.SqlClient;
279
279
  const session = yield* lockSession(sessionId);
280
280
  const rows = yield* sql `
281
- SELECT entry_id, parent_id, seq, tag, payload_json FROM baton_session_entries
281
+ SELECT entry_id, parent_id, seq, tag, payload_json FROM tenetkit_session_entries
282
282
  WHERE session_id = ${sessionId} AND entry_id = ${prepared.id}
283
283
  `;
284
284
  const existing = rows[0];
@@ -343,7 +343,7 @@ export const makePostgresSessionStore = (options) => {
343
343
  let sequence = Number(session.next_seq);
344
344
  while (true) {
345
345
  const collision = yield* sql `
346
- SELECT entry_id FROM baton_session_entries
346
+ SELECT entry_id FROM tenetkit_session_entries
347
347
  WHERE session_id = ${sessionId} AND entry_id = ${String(sequence)}
348
348
  `;
349
349
  if (collision[0] === undefined)
@@ -358,7 +358,7 @@ export const makePostgresSessionStore = (options) => {
358
358
  path: (leaf) => runNoTxn(Effect.gen(function* () {
359
359
  const sql = yield* SqlClient.SqlClient;
360
360
  const sessions = yield* sql `
361
- SELECT leaf_id, next_seq, owner_token FROM baton_sessions WHERE session_id = ${sessionId}
361
+ SELECT leaf_id, next_seq, owner_token FROM tenetkit_sessions WHERE session_id = ${sessionId}
362
362
  `;
363
363
  return { target: leaf ?? sessions[0]?.leaf_id ?? null, rows: yield* loadEntries(sessionId) };
364
364
  }).pipe(Effect.flatMap(({ rows, target }) => {
@@ -370,17 +370,17 @@ export const makePostgresSessionStore = (options) => {
370
370
  yield* lockSession(sessionId);
371
371
  if (id !== null) {
372
372
  const rows = yield* sql `
373
- SELECT entry_id FROM baton_session_entries WHERE session_id = ${sessionId} AND entry_id = ${id}
373
+ SELECT entry_id FROM tenetkit_session_entries WHERE session_id = ${sessionId} AND entry_id = ${id}
374
374
  `;
375
375
  if (rows[0] === undefined)
376
376
  return yield* storeError(`Session entry ${id} does not exist`);
377
377
  }
378
- yield* sql `UPDATE baton_sessions SET leaf_id = ${id}, updated_at = NOW() WHERE session_id = ${sessionId}`;
378
+ yield* sql `UPDATE tenetkit_sessions SET leaf_id = ${id}, updated_at = NOW() WHERE session_id = ${sessionId}`;
379
379
  })).pipe(mapReadError),
380
380
  leaf: Effect.orDie(runNoTxn(Effect.gen(function* () {
381
381
  const sql = yield* SqlClient.SqlClient;
382
382
  const rows = yield* sql `
383
- SELECT leaf_id, next_seq, owner_token FROM baton_sessions WHERE session_id = ${sessionId}
383
+ SELECT leaf_id, next_seq, owner_token FROM tenetkit_sessions WHERE session_id = ${sessionId}
384
384
  `;
385
385
  return rows[0]?.leaf_id ?? null;
386
386
  }))),
@@ -28,7 +28,7 @@ export const admitSend = Function.dual(4, (hub, addressBindings, nextId, input)
28
28
  const treePolicy = yield* normalizeTreePolicy(input.treePolicy);
29
29
  const digest = rootDigest(input.message, treePolicy);
30
30
  const existing = yield* sql `
31
- SELECT * FROM baton_runs
31
+ SELECT * FROM tenetkit_runs
32
32
  WHERE address = ${input.message.to}
33
33
  AND session_id = ${input.message.sessionId}
34
34
  AND idempotency_key = ${input.message.idempotencyKey}
@@ -55,7 +55,7 @@ export const admitSend = Function.dual(4, (hub, addressBindings, nextId, input)
55
55
  };
56
56
  }
57
57
  if (input.runId !== undefined) {
58
- const byId = yield* sql `SELECT * FROM baton_runs WHERE run_id = ${input.runId}`;
58
+ const byId = yield* sql `SELECT * FROM tenetkit_runs WHERE run_id = ${input.runId}`;
59
59
  if (byId[0] !== undefined)
60
60
  return yield* RunIdConflict.make({ runId: input.runId, existingRunId: byId[0].run_id });
61
61
  }
@@ -7,11 +7,11 @@ import { cancelOwnedFanOuts } from "./store-fan-out.js";
7
7
  import { reconcileProgramCancellation } from "tenetkit/runtime/driver/sql/store-program";
8
8
  export const deferCancelledFanOutParent = Function.dual(2, (sql, runId) => Effect.gen(function* () {
9
9
  const running = yield* sql `
10
- SELECT fan_out_id FROM baton_fan_outs WHERE parent_run_id = ${runId} AND status = 'running' LIMIT 1
10
+ SELECT fan_out_id FROM tenetkit_fan_outs WHERE parent_run_id = ${runId} AND status = 'running' LIMIT 1
11
11
  `;
12
12
  if (running.length === 0 && !(yield* hasUnsettledChild(runId)))
13
13
  return false;
14
- yield* sql `UPDATE baton_runs SET owner_worker_id = NULL, lease_expires_at = NULL WHERE run_id = ${runId}`;
14
+ yield* sql `UPDATE tenetkit_runs SET owner_worker_id = NULL, lease_expires_at = NULL WHERE run_id = ${runId}`;
15
15
  return true;
16
16
  }));
17
17
  export const makeCancelRun = (input) => {
@@ -27,12 +27,12 @@ export const makeCancelRun = (input) => {
27
27
  if (!terminal)
28
28
  yield* reconcileProgramCancellation(runId, reason ?? current.cancelReason);
29
29
  yield* input.sql `
30
- UPDATE baton_run_waits SET status = 'cancelled', closed_at = NOW()
30
+ UPDATE tenetkit_run_waits SET status = 'cancelled', closed_at = NOW()
31
31
  WHERE run_id = ${runId} AND status = 'open'
32
32
  `;
33
33
  const linked = yield* input.sql `
34
- SELECT l.child_run_id FROM baton_run_links l
35
- LEFT JOIN baton_fan_out_members m ON m.child_run_id = l.child_run_id
34
+ SELECT l.child_run_id FROM tenetkit_run_links l
35
+ LEFT JOIN tenetkit_fan_out_members m ON m.child_run_id = l.child_run_id
36
36
  WHERE l.parent_run_id = ${runId} AND m.child_run_id IS NULL
37
37
  ORDER BY l.child_run_id ASC
38
38
  `;
@@ -58,7 +58,7 @@ export const makeCancelRun = (input) => {
58
58
  if (isTerminal(current.status))
59
59
  return;
60
60
  const running = yield* input.sql `
61
- SELECT fan_out_id FROM baton_fan_outs WHERE parent_run_id = ${runId} AND status = 'running' LIMIT 1
61
+ SELECT fan_out_id FROM tenetkit_fan_outs WHERE parent_run_id = ${runId} AND status = 'running' LIMIT 1
62
62
  `;
63
63
  if (running.length > 0)
64
64
  return;
@@ -3,14 +3,14 @@ import { admitFanOut, inspectFanOut } from "tenetkit/runtime/driver/sql/store-fa
3
3
  import { NOTIFY_CHANNEL } from "./schema.js";
4
4
  export const fanOutStoreMethods = (input) => ({
5
5
  admitFanOut: (fanOut) => input
6
- .run(input.sql `SELECT run_id FROM baton_runs WHERE run_id = ${fanOut.parentRunId} FOR UPDATE`.pipe(Effect.andThen(input.sql `SELECT pg_advisory_xact_lock(hashtext(${`fanout:${fanOut.parentRunId}:${fanOut.idempotencyKey}`}))`), Effect.andThen(admitFanOut(input.hub, fanOut))))
6
+ .run(input.sql `SELECT run_id FROM tenetkit_runs WHERE run_id = ${fanOut.parentRunId} FOR UPDATE`.pipe(Effect.andThen(input.sql `SELECT pg_advisory_xact_lock(hashtext(${`fanout:${fanOut.parentRunId}:${fanOut.idempotencyKey}`}))`), Effect.andThen(admitFanOut(input.hub, fanOut))))
7
7
  .pipe(Effect.tap((receipt) => input.runNoTxn(Effect.forEach([receipt.parentRunId, ...receipt.childRunIds], (runId) => input.pg.notify(NOTIFY_CHANNEL, runId), { discard: true })))),
8
8
  inspectFanOut: (fanOutId) => input.runNoTxn(inspectFanOut(fanOutId)),
9
9
  });
10
10
  export const cancelOwnedFanOuts = Function.dual(2, (sql, parentRunId) => Effect.gen(function* () {
11
11
  const owned = yield* sql `
12
- SELECT m.child_run_id FROM baton_fan_outs f
13
- JOIN baton_fan_out_members m ON m.fan_out_id = f.fan_out_id
12
+ SELECT m.child_run_id FROM tenetkit_fan_outs f
13
+ JOIN tenetkit_fan_out_members m ON m.fan_out_id = f.fan_out_id
14
14
  WHERE f.parent_run_id = ${parentRunId} AND f.status = 'running' ORDER BY m.ordinal ASC
15
15
  `;
16
16
  return owned.map((row) => row.child_run_id);
@@ -11,12 +11,12 @@ import { sameInterruptedModelOutcome, sameInterruptedModelResponse, validateInte
11
11
  import { appendCompletedSessionEntry, appendInterruptedSessionEntry, verifyCompletedSessionEntry, verifyInterruptedSessionEntry, } from "./session-store.js";
12
12
  export const postgresModelResponseOperations = (input) => {
13
13
  const { sql, hub, run, requireRun, requireClaim } = input;
14
- const fenced = (claim, effect) => run(sql `SELECT run_id FROM baton_runs WHERE run_id = ${claim.runId} FOR UPDATE`.pipe(Effect.andThen(requireClaim(claim)), Effect.andThen(effect)));
14
+ const fenced = (claim, effect) => run(sql `SELECT run_id FROM tenetkit_runs WHERE run_id = ${claim.runId} FOR UPDATE`.pipe(Effect.andThen(requireClaim(claim)), Effect.andThen(effect)));
15
15
  return {
16
16
  commitInterruptedModelResponse: (op) => fenced(op, Effect.gen(function* () {
17
17
  const loaded = yield* requireRun(op.runId);
18
18
  const rows = yield* sql `
19
- SELECT * FROM baton_run_operations
19
+ SELECT * FROM tenetkit_run_operations
20
20
  WHERE run_id = ${op.runId} AND operation_id = ${op.operationId}
21
21
  FOR UPDATE
22
22
  `;
@@ -58,20 +58,20 @@ export const postgresModelResponseOperations = (input) => {
58
58
  }
59
59
  yield* appendInterruptedSessionEntry(sessionEntry).pipe(Effect.mapError((error) => RuntimeUnavailable.make({ message: error.message })));
60
60
  yield* sql `
61
- UPDATE baton_run_operations
61
+ UPDATE tenetkit_run_operations
62
62
  SET status = 'failed', error_json = ${encodeJsonValue(op.outcome.error)}, finished_at = NOW()
63
63
  WHERE run_id = ${op.runId} AND operation_id = ${op.operationId} AND status = 'running'
64
64
  `;
65
65
  yield* appendEvent(hub, yield* requireRun(op.runId), validated.event);
66
66
  const completed = yield* sql `
67
- SELECT * FROM baton_run_operations WHERE run_id = ${op.runId} AND operation_id = ${op.operationId}
67
+ SELECT * FROM tenetkit_run_operations WHERE run_id = ${op.runId} AND operation_id = ${op.operationId}
68
68
  `;
69
69
  return toOperationRecord(completed[0]);
70
70
  })),
71
71
  commitModelResponse: (op) => fenced(op, Effect.gen(function* () {
72
72
  const loaded = yield* requireRun(op.runId);
73
73
  const existing = yield* sql `
74
- SELECT * FROM baton_run_operations
74
+ SELECT * FROM tenetkit_run_operations
75
75
  WHERE run_id = ${op.runId} AND operation_id = ${op.operationId}
76
76
  FOR UPDATE
77
77
  `;
@@ -116,7 +116,7 @@ export const postgresModelResponseOperations = (input) => {
116
116
  }
117
117
  for (const entryId of new Set(op.steeringEntryIds ?? [])) {
118
118
  const rows = yield* sql `
119
- SELECT consumed_operation_id FROM baton_run_steering
119
+ SELECT consumed_operation_id FROM tenetkit_run_steering
120
120
  WHERE run_id = ${op.runId} AND entry_id = ${entryId}
121
121
  `;
122
122
  if (rows[0]?.consumed_operation_id !== op.operationId) {
@@ -131,13 +131,13 @@ export const postgresModelResponseOperations = (input) => {
131
131
  });
132
132
  yield* appendCompletedSessionEntry(sessionEntry).pipe(Effect.mapError((error) => RuntimeUnavailable.make({ message: error.message })));
133
133
  yield* sql `
134
- UPDATE baton_run_operations
134
+ UPDATE tenetkit_run_operations
135
135
  SET status = 'succeeded', result_json = ${encodeJsonValue(validated.reference)}, finished_at = NOW()
136
136
  WHERE run_id = ${op.runId} AND operation_id = ${op.operationId}
137
137
  AND status IN ('requested', 'running')
138
138
  `;
139
139
  yield* sql `
140
- UPDATE baton_runs SET
140
+ UPDATE tenetkit_runs SET
141
141
  driver_checkpoint_json = COALESCE(${op.checkpoint === undefined ? null : encodeJson(ExecutionCheckpoint, op.checkpoint)}, driver_checkpoint_json),
142
142
  executable_ref_json = ${encodeExecutableRef(executableRef)},
143
143
  continuation_json = CASE WHEN ${op.continuation === undefined ? 0 : 1} = 1
@@ -148,7 +148,7 @@ export const postgresModelResponseOperations = (input) => {
148
148
  `;
149
149
  yield* appendEvent(hub, yield* requireRun(op.runId), validated.event);
150
150
  const rows = yield* sql `
151
- SELECT * FROM baton_run_operations WHERE run_id = ${op.runId} AND operation_id = ${op.operationId}
151
+ SELECT * FROM tenetkit_run_operations WHERE run_id = ${op.runId} AND operation_id = ${op.operationId}
152
152
  `;
153
153
  return toOperationRecord(rows[0]);
154
154
  })),