@yieldstar/sqlite-runtime 0.6.0 → 0.7.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.
@@ -0,0 +1,988 @@
1
+ // src/sqlite-runtime.ts
2
+ import { Runtime } from "@yieldstar/core";
3
+
4
+ // src/sqlite-effect.ts
5
+ import {
6
+ createEffectState,
7
+ effectNotFound,
8
+ matchEffectBegin,
9
+ prepareEffectBegin,
10
+ prepareEffectSettle,
11
+ prepareEffectWatch,
12
+ settleEffect,
13
+ toEffectRecord
14
+ } from "@yieldstar/core";
15
+
16
+ // src/dao/effect-waiters-dao.ts
17
+ class EffectWaitersDao {
18
+ db;
19
+ constructor(db) {
20
+ this.db = db;
21
+ this.setupDb();
22
+ }
23
+ setupDb() {
24
+ this.db.run(`
25
+ CREATE TABLE IF NOT EXISTS effect_waiters (
26
+ effect_id TEXT NOT NULL,
27
+ execution_id TEXT NOT NULL,
28
+ step_key TEXT NOT NULL,
29
+ PRIMARY KEY (effect_id, execution_id, step_key)
30
+ );
31
+ CREATE INDEX IF NOT EXISTS idx_effect_waiters_step
32
+ ON effect_waiters(execution_id, step_key);
33
+ `);
34
+ }
35
+ upsertWaiter(waiter) {
36
+ this.db.query(`INSERT INTO effect_waiters (effect_id, execution_id, step_key)
37
+ VALUES ($effectId, $executionId, $stepKey)
38
+ ON CONFLICT(effect_id, execution_id, step_key) DO NOTHING`).run({
39
+ $effectId: waiter.effectId,
40
+ $executionId: waiter.executionId,
41
+ $stepKey: waiter.stepKey
42
+ });
43
+ }
44
+ getWaitingExecutionIds(effectId) {
45
+ return this.db.query(`SELECT DISTINCT execution_id FROM effect_waiters WHERE effect_id = $effectId`).all({ $effectId: effectId }).map((row) => row.execution_id);
46
+ }
47
+ deleteStepWaiters(executionId, stepKey) {
48
+ this.db.query(`DELETE FROM effect_waiters WHERE execution_id = $executionId AND step_key = $stepKey`).run({ $executionId: executionId, $stepKey: stepKey });
49
+ }
50
+ }
51
+
52
+ // src/dao/effects-dao.ts
53
+ class EffectsDao {
54
+ db;
55
+ constructor(db) {
56
+ this.db = db;
57
+ this.setupDb();
58
+ }
59
+ setupDb() {
60
+ this.db.run(`
61
+ CREATE TABLE IF NOT EXISTS effects (
62
+ effect_id TEXT PRIMARY KEY,
63
+ operation TEXT NOT NULL,
64
+ input TEXT NOT NULL,
65
+ status TEXT NOT NULL,
66
+ result TEXT
67
+ );
68
+ CREATE TABLE IF NOT EXISTS effect_conflicts (
69
+ effect_id TEXT NOT NULL,
70
+ result TEXT NOT NULL,
71
+ conflicting_result TEXT NOT NULL,
72
+ reported_at INTEGER NOT NULL,
73
+ PRIMARY KEY (effect_id, conflicting_result)
74
+ );
75
+ `);
76
+ }
77
+ getState(effectId) {
78
+ const row = this.db.query(`SELECT * FROM effects WHERE effect_id = $effectId`).get({ $effectId: effectId });
79
+ if (!row)
80
+ return;
81
+ const conflicts = this.db.query(`SELECT result, conflicting_result, reported_at FROM effect_conflicts
82
+ WHERE effect_id = $effectId
83
+ ORDER BY reported_at, rowid`).all({ $effectId: effectId });
84
+ const state = {
85
+ effectId: row.effect_id,
86
+ operation: row.operation,
87
+ input: row.input,
88
+ status: row.status,
89
+ conflicts: conflicts.map((conflict) => ({
90
+ result: conflict.result,
91
+ conflictingResult: conflict.conflicting_result,
92
+ reportedAt: conflict.reported_at
93
+ }))
94
+ };
95
+ if (row.result !== null)
96
+ state.result = row.result;
97
+ return state;
98
+ }
99
+ insertState(state) {
100
+ this.db.query(`INSERT INTO effects (effect_id, operation, input, status, result)
101
+ VALUES ($effectId, $operation, $input, $status, $result)`).run({
102
+ $effectId: state.effectId,
103
+ $operation: state.operation,
104
+ $input: state.input,
105
+ $status: state.status,
106
+ $result: state.result ?? null
107
+ });
108
+ }
109
+ updateState(before, after) {
110
+ if (before.status !== after.status || before.result !== after.result) {
111
+ this.db.query(`UPDATE effects SET status = $status, result = $result
112
+ WHERE effect_id = $effectId`).run({
113
+ $effectId: after.effectId,
114
+ $status: after.status,
115
+ $result: after.result ?? null
116
+ });
117
+ }
118
+ for (const conflict of after.conflicts.slice(before.conflicts.length)) {
119
+ this.insertConflict(after.effectId, conflict);
120
+ }
121
+ }
122
+ insertConflict(effectId, conflict) {
123
+ this.db.query(`INSERT INTO effect_conflicts (effect_id, result, conflicting_result, reported_at)
124
+ VALUES ($effectId, $result, $conflictingResult, $reportedAt)`).run({
125
+ $effectId: effectId,
126
+ $result: conflict.result,
127
+ $conflictingResult: conflict.conflictingResult,
128
+ $reportedAt: conflict.reportedAt
129
+ });
130
+ }
131
+ }
132
+
133
+ // src/sqlite-execution.ts
134
+ import {
135
+ acknowledgeExecution,
136
+ claimExecution,
137
+ createExecutionState,
138
+ createStartedExecution,
139
+ executionIdConflict,
140
+ executionNotFound,
141
+ isExecutionClaimable,
142
+ matchStartRequest,
143
+ prepareExecutionAdmission,
144
+ prepareExecutionStart,
145
+ releaseExecutionLease,
146
+ renewExecutionLease,
147
+ requestExecutionWake,
148
+ toExecutionRecord
149
+ } from "@yieldstar/core";
150
+
151
+ // src/dao/executions-dao.ts
152
+ import { nextClaimAt } from "@yieldstar/core";
153
+ class ExecutionsDao {
154
+ db;
155
+ constructor(db) {
156
+ this.db = db;
157
+ this.setupDb();
158
+ }
159
+ setupDb() {
160
+ this.db.run(`
161
+ CREATE TABLE IF NOT EXISTS executions (
162
+ execution_id TEXT PRIMARY KEY,
163
+ workflow_id TEXT NOT NULL,
164
+ params TEXT NOT NULL,
165
+ context TEXT NOT NULL,
166
+ status TEXT NOT NULL,
167
+ outcome TEXT,
168
+ admitted_at INTEGER NOT NULL,
169
+ updated_at INTEGER NOT NULL,
170
+ due_at INTEGER,
171
+ claimed_due_at INTEGER,
172
+ lease_token INTEGER NOT NULL DEFAULT 0,
173
+ lease_expires_at INTEGER,
174
+ unacknowledged_delivery_count INTEGER NOT NULL DEFAULT 0,
175
+ redeliver_at INTEGER,
176
+ next_claim_at INTEGER,
177
+ parent_execution_id TEXT,
178
+ parent_step_key TEXT
179
+ );
180
+ CREATE INDEX IF NOT EXISTS idx_executions_next_claim_at
181
+ ON executions(next_claim_at) WHERE next_claim_at IS NOT NULL;
182
+ `);
183
+ }
184
+ getState(executionId) {
185
+ const row = this.db.query(`SELECT * FROM executions WHERE execution_id = $executionId`).get({ $executionId: executionId });
186
+ return row ? toState(row) : undefined;
187
+ }
188
+ getClaimableIds(now, limit, executionIds) {
189
+ return this.db.query(`SELECT execution_id FROM executions
190
+ WHERE next_claim_at IS NOT NULL AND next_claim_at <= $now
191
+ AND ($executionIds IS NULL
192
+ OR execution_id IN (SELECT value FROM json_each($executionIds)))
193
+ ORDER BY next_claim_at, execution_id
194
+ LIMIT $limit`).all({
195
+ $now: now,
196
+ $limit: limit,
197
+ $executionIds: executionIds ? JSON.stringify(executionIds) : null
198
+ }).map((row) => row.execution_id);
199
+ }
200
+ insertState(state) {
201
+ const result = this.db.query(`INSERT INTO executions (
202
+ execution_id, workflow_id, params, context, status, outcome,
203
+ admitted_at, updated_at, due_at, claimed_due_at, lease_token,
204
+ lease_expires_at, unacknowledged_delivery_count, redeliver_at,
205
+ next_claim_at, parent_execution_id, parent_step_key
206
+ ) VALUES (
207
+ $executionId, $workflowId, $params, $context, $status, $outcome,
208
+ $admittedAt, $updatedAt, $dueAt, $claimedDueAt, $leaseToken,
209
+ $leaseExpiresAt, $unacknowledgedDeliveryCount, $redeliverAt,
210
+ $nextClaimAt, $parentExecutionId, $parentStepKey
211
+ )
212
+ ON CONFLICT(execution_id) DO NOTHING`).run({
213
+ ...toBindings(state),
214
+ $parentExecutionId: state.parent?.executionId ?? null,
215
+ $parentStepKey: state.parent?.stepKey ?? null
216
+ });
217
+ return Number(result.changes) === 1;
218
+ }
219
+ updateState(state) {
220
+ this.db.query(`UPDATE executions SET
221
+ workflow_id = $workflowId,
222
+ params = $params,
223
+ context = $context,
224
+ status = $status,
225
+ outcome = $outcome,
226
+ admitted_at = $admittedAt,
227
+ updated_at = $updatedAt,
228
+ due_at = $dueAt,
229
+ claimed_due_at = $claimedDueAt,
230
+ lease_token = $leaseToken,
231
+ lease_expires_at = $leaseExpiresAt,
232
+ unacknowledged_delivery_count = $unacknowledgedDeliveryCount,
233
+ redeliver_at = $redeliverAt,
234
+ next_claim_at = $nextClaimAt
235
+ WHERE execution_id = $executionId`).run(toBindings(state));
236
+ }
237
+ }
238
+ function toState(row) {
239
+ return {
240
+ executionId: row.execution_id,
241
+ workflowId: row.workflow_id,
242
+ params: row.params,
243
+ context: row.context,
244
+ status: row.status,
245
+ outcome: row.outcome,
246
+ admittedAt: Number(row.admitted_at),
247
+ updatedAt: Number(row.updated_at),
248
+ dueAt: toNumberOrNull(row.due_at),
249
+ claimedDueAt: toNumberOrNull(row.claimed_due_at),
250
+ leaseToken: Number(row.lease_token),
251
+ leaseExpiresAt: toNumberOrNull(row.lease_expires_at),
252
+ unacknowledgedDeliveryCount: Number(row.unacknowledged_delivery_count),
253
+ redeliverAt: toNumberOrNull(row.redeliver_at),
254
+ parent: row.parent_execution_id !== null && row.parent_step_key !== null ? { executionId: row.parent_execution_id, stepKey: row.parent_step_key } : null
255
+ };
256
+ }
257
+ function toBindings(state) {
258
+ return {
259
+ $executionId: state.executionId,
260
+ $workflowId: state.workflowId,
261
+ $params: state.params,
262
+ $context: state.context,
263
+ $status: state.status,
264
+ $outcome: state.outcome,
265
+ $admittedAt: state.admittedAt,
266
+ $updatedAt: state.updatedAt,
267
+ $dueAt: state.dueAt,
268
+ $claimedDueAt: state.claimedDueAt,
269
+ $leaseToken: state.leaseToken,
270
+ $leaseExpiresAt: state.leaseExpiresAt,
271
+ $unacknowledgedDeliveryCount: state.unacknowledgedDeliveryCount,
272
+ $redeliverAt: state.redeliverAt,
273
+ $nextClaimAt: nextClaimAt(state)
274
+ };
275
+ }
276
+ function toNumberOrNull(value) {
277
+ return value === null ? null : Number(value);
278
+ }
279
+
280
+ // src/dao/start-requests-dao.ts
281
+ class StartRequestsDao {
282
+ db;
283
+ constructor(db) {
284
+ this.db = db;
285
+ this.setupDb();
286
+ }
287
+ setupDb() {
288
+ this.db.run(`
289
+ CREATE TABLE IF NOT EXISTS start_requests (
290
+ caller_execution_id TEXT NOT NULL,
291
+ step_key TEXT NOT NULL,
292
+ workflow_id TEXT NOT NULL,
293
+ params TEXT,
294
+ execution_id TEXT NOT NULL,
295
+ PRIMARY KEY (caller_execution_id, step_key)
296
+ );
297
+ `);
298
+ }
299
+ get(callerExecutionId, stepKey) {
300
+ const row = this.db.query(`SELECT * FROM start_requests
301
+ WHERE caller_execution_id = $callerExecutionId AND step_key = $stepKey`).get({ $callerExecutionId: callerExecutionId, $stepKey: stepKey });
302
+ return row ? {
303
+ callerExecutionId: row.caller_execution_id,
304
+ stepKey: row.step_key,
305
+ workflowId: row.workflow_id,
306
+ params: row.params,
307
+ executionId: row.execution_id
308
+ } : undefined;
309
+ }
310
+ insert(request) {
311
+ this.db.query(`INSERT INTO start_requests
312
+ (caller_execution_id, step_key, workflow_id, params, execution_id)
313
+ VALUES
314
+ ($callerExecutionId, $stepKey, $workflowId, $params, $executionId)`).run({
315
+ $callerExecutionId: request.callerExecutionId,
316
+ $stepKey: request.stepKey,
317
+ $workflowId: request.workflowId,
318
+ $params: request.params,
319
+ $executionId: request.executionId
320
+ });
321
+ }
322
+ }
323
+
324
+ // src/sqlite-transaction.ts
325
+ function runTransaction(db, fn) {
326
+ db.run("BEGIN IMMEDIATE");
327
+ try {
328
+ const result = fn();
329
+ db.run("COMMIT");
330
+ return result;
331
+ } catch (err) {
332
+ db.run("ROLLBACK");
333
+ throw err;
334
+ }
335
+ }
336
+
337
+ // src/sqlite-execution.ts
338
+ class SqliteExecutionClient2 {
339
+ db;
340
+ executionsDao;
341
+ startRequestsDao;
342
+ clock;
343
+ constructor(db, options = {}) {
344
+ this.db = db;
345
+ this.executionsDao = new ExecutionsDao(db);
346
+ this.startRequestsDao = new StartRequestsDao(db);
347
+ this.clock = options.clock ?? (() => Date.now());
348
+ }
349
+ async admit(request) {
350
+ const prepared = prepareExecutionAdmission(request);
351
+ if (!prepared.ok) {
352
+ return {
353
+ status: "rejected",
354
+ code: prepared.code,
355
+ message: prepared.message
356
+ };
357
+ }
358
+ return runTransaction(this.db, () => {
359
+ const state = createExecutionState({
360
+ executionId: prepared.executionId,
361
+ workflowId: request.workflowId,
362
+ encoded: prepared.encoded,
363
+ now: this.clock()
364
+ });
365
+ if (!this.executionsDao.insertState(state)) {
366
+ return executionIdConflict(prepared.executionId);
367
+ }
368
+ return { status: "accepted", executionId: prepared.executionId };
369
+ });
370
+ }
371
+ async start(request) {
372
+ const prepared = prepareExecutionStart(request);
373
+ if (!prepared.ok)
374
+ return prepared.result;
375
+ const { start } = prepared;
376
+ return runTransaction(this.db, () => {
377
+ const recorded = this.startRequestsDao.get(start.callerExecutionId, start.stepKey);
378
+ if (recorded)
379
+ return matchStartRequest(recorded, start);
380
+ const caller = this.executionsDao.getState(start.callerExecutionId);
381
+ if (!caller) {
382
+ throw new Error(`Cannot start a workflow from execution "${start.callerExecutionId}": it does not exist`);
383
+ }
384
+ let created = createStartedExecution({ start, caller, now: this.clock() });
385
+ while (!this.executionsDao.insertState(created.target)) {
386
+ created = createStartedExecution({ start, caller, now: this.clock() });
387
+ }
388
+ this.startRequestsDao.insert(created.startRequest);
389
+ return {
390
+ status: "accepted",
391
+ executionId: created.target.executionId
392
+ };
393
+ });
394
+ }
395
+ async get(executionId) {
396
+ const state = this.executionsDao.getState(executionId);
397
+ return state ? toExecutionRecord(state, this.clock()) : undefined;
398
+ }
399
+ async requestWake(request) {
400
+ return runTransaction(this.db, () => {
401
+ const state = this.executionsDao.getState(request.executionId);
402
+ if (!state)
403
+ return executionNotFound(request.executionId);
404
+ const next = requestExecutionWake(state, request.wakeAt, this.clock());
405
+ if (!next)
406
+ return { status: "ignored" };
407
+ this.executionsDao.updateState(next);
408
+ return { status: "requested" };
409
+ });
410
+ }
411
+ async claimDue(request) {
412
+ const executionIds = request.filter?.executionIds;
413
+ if (request.limit <= 0 || executionIds?.length === 0)
414
+ return [];
415
+ return runTransaction(this.db, () => {
416
+ const now = this.clock();
417
+ const leases = [];
418
+ for (const executionId of this.executionsDao.getClaimableIds(now, request.limit, executionIds)) {
419
+ const state = this.executionsDao.getState(executionId);
420
+ if (!state || !isExecutionClaimable(state, now))
421
+ continue;
422
+ const claimed = claimExecution(state, {
423
+ now,
424
+ duration: request.duration
425
+ });
426
+ this.executionsDao.updateState(claimed.state);
427
+ leases.push(claimed.lease);
428
+ }
429
+ return leases;
430
+ });
431
+ }
432
+ async renew(request) {
433
+ return this.transition(request.lease, (state) => renewExecutionLease(state, {
434
+ lease: request.lease,
435
+ duration: request.duration,
436
+ now: this.clock()
437
+ }));
438
+ }
439
+ async release(request) {
440
+ return this.transition(request.lease, (state) => releaseExecutionLease(state, { lease: request.lease, now: this.clock() }));
441
+ }
442
+ async acknowledge(request) {
443
+ return this.transition(request.lease, (state) => acknowledgeExecution(state, {
444
+ lease: request.lease,
445
+ outcome: request.outcome,
446
+ now: this.clock()
447
+ }));
448
+ }
449
+ transition(lease, fn) {
450
+ return Promise.resolve(runTransaction(this.db, () => {
451
+ const state = this.executionsDao.getState(lease.executionId);
452
+ if (!state)
453
+ return { status: "lost" };
454
+ const next = fn(state);
455
+ if (next.state !== state)
456
+ this.executionsDao.updateState(next.state);
457
+ return next.result;
458
+ }));
459
+ }
460
+ wakeInTransaction(executionId, wakeAt = this.clock()) {
461
+ const state = this.executionsDao.getState(executionId);
462
+ if (!state)
463
+ return;
464
+ const next = requestExecutionWake(state, wakeAt, this.clock());
465
+ if (next)
466
+ this.executionsDao.updateState(next);
467
+ }
468
+ }
469
+
470
+ // src/sqlite-effect.ts
471
+ class SqliteEffectClient2 {
472
+ db;
473
+ executions;
474
+ effectsDao;
475
+ effectWaitersDao;
476
+ clock;
477
+ constructor(params) {
478
+ this.db = params.db;
479
+ this.clock = params.clock ?? (() => Date.now());
480
+ this.executions = params.executions ?? new SqliteExecutionClient2(params.db, { clock: this.clock });
481
+ this.effectsDao = new EffectsDao(params.db);
482
+ this.effectWaitersDao = new EffectWaitersDao(params.db);
483
+ }
484
+ async begin(request) {
485
+ const begin = prepareEffectBegin(request);
486
+ return runTransaction(this.db, () => {
487
+ const state = this.effectsDao.getState(begin.effectId);
488
+ if (state)
489
+ return matchEffectBegin(state, begin);
490
+ this.effectsDao.insertState(createEffectState(begin));
491
+ return { status: "new" };
492
+ });
493
+ }
494
+ async settle(request) {
495
+ const settle = prepareEffectSettle(request);
496
+ return runTransaction(this.db, () => {
497
+ const state = this.effectsDao.getState(settle.effectId);
498
+ if (!state)
499
+ throw effectNotFound(settle.effectId);
500
+ const settled = settleEffect(state, settle, this.clock());
501
+ if (settled.state !== state)
502
+ this.effectsDao.updateState(state, settled.state);
503
+ if (settled.wake) {
504
+ for (const executionId of this.effectWaitersDao.getWaitingExecutionIds(settle.effectId)) {
505
+ this.executions.wakeInTransaction(executionId);
506
+ }
507
+ }
508
+ return settled.result;
509
+ });
510
+ }
511
+ async watch(request) {
512
+ const watch = prepareEffectWatch(request);
513
+ runTransaction(this.db, () => {
514
+ const state = this.effectsDao.getState(watch.effectId);
515
+ if (!state)
516
+ throw effectNotFound(watch.effectId);
517
+ this.effectWaitersDao.upsertWaiter(watch);
518
+ if (state.status === "settled") {
519
+ this.executions.wakeInTransaction(watch.executionId);
520
+ }
521
+ });
522
+ }
523
+ async getEffect(effectId) {
524
+ const state = this.effectsDao.getState(effectId);
525
+ return state ? toEffectRecord(state) : undefined;
526
+ }
527
+ }
528
+
529
+ // src/sqlite-heap.ts
530
+ import {
531
+ isLeaseCurrent
532
+ } from "@yieldstar/core";
533
+
534
+ // src/dao/step-response-dao.ts
535
+ class StepResponsesDao {
536
+ db;
537
+ constructor(db) {
538
+ this.db = db;
539
+ this.setupDb();
540
+ }
541
+ setupDb() {
542
+ this.db.run(`
543
+ CREATE TABLE IF NOT EXISTS step_responses (
544
+ execution_id TEXT NOT NULL,
545
+ step_key TEXT NOT NULL,
546
+ step_attempt INTEGER NOT NULL,
547
+ step_done BOOL NOT NULL,
548
+ step_response TEXT NOT NULL,
549
+ PRIMARY KEY (execution_id, step_key, step_attempt)
550
+ );
551
+ CREATE INDEX IF NOT EXISTS idx_execution_step_attempt
552
+ ON step_responses(execution_id, step_key, step_attempt DESC);
553
+ `);
554
+ }
555
+ getAllSteps() {
556
+ const query = this.db.query(`SELECT * FROM step_responses`);
557
+ return query.all();
558
+ }
559
+ deleteAll() {
560
+ const query = this.db.query(`DELETE FROM step_responses`);
561
+ query.run();
562
+ }
563
+ getLatestStepResponse(executionId, stepKey) {
564
+ const query = this.db.query(`SELECT * FROM step_responses
565
+ WHERE execution_id = $executionId
566
+ AND step_key = $stepKey
567
+ ORDER BY step_attempt DESC LIMIT 1`);
568
+ const result = query.get({
569
+ $executionId: executionId,
570
+ $stepKey: stepKey
571
+ });
572
+ return result;
573
+ }
574
+ getStepResponse(executionId, stepKey, stepAttempt) {
575
+ return this.db.query(`SELECT * FROM step_responses
576
+ WHERE execution_id = $executionId
577
+ AND step_key = $stepKey
578
+ AND step_attempt = $stepAttempt`).get({
579
+ $executionId: executionId,
580
+ $stepKey: stepKey,
581
+ $stepAttempt: stepAttempt
582
+ });
583
+ }
584
+ insertStepResponse(executionId, stepKey, stepAttempt, stepDone, stepResponseJson) {
585
+ const query = this.db.query(`INSERT INTO step_responses (execution_id, step_key, step_attempt, step_done, step_response)
586
+ VALUES ($executionId, $stepKey, $stepAttempt, $stepDone, $stepResponse)`);
587
+ query.run({
588
+ $executionId: executionId,
589
+ $stepKey: stepKey,
590
+ $stepAttempt: stepAttempt,
591
+ $stepDone: stepDone,
592
+ $stepResponse: stepResponseJson
593
+ });
594
+ }
595
+ }
596
+
597
+ // src/dao/store-waiters-dao.ts
598
+ class StoreWaitersDao {
599
+ db;
600
+ constructor(db) {
601
+ this.db = db;
602
+ this.setupDb();
603
+ }
604
+ setupDb() {
605
+ this.db.run(`
606
+ CREATE TABLE IF NOT EXISTS store_waiters (
607
+ workflow_id TEXT NOT NULL,
608
+ execution_id TEXT NOT NULL,
609
+ step_key TEXT NOT NULL,
610
+ store_name TEXT NOT NULL,
611
+ store_id TEXT NOT NULL,
612
+ since_version INTEGER NOT NULL,
613
+ read_paths TEXT NOT NULL,
614
+ PRIMARY KEY (store_name, store_id, execution_id, step_key)
615
+ );
616
+ CREATE INDEX IF NOT EXISTS idx_store_waiters_step
617
+ ON store_waiters(execution_id, step_key);
618
+ `);
619
+ }
620
+ upsertWaiter(waiter) {
621
+ this.db.query(`INSERT INTO store_waiters
622
+ (workflow_id, execution_id, step_key, store_name, store_id, since_version, read_paths)
623
+ VALUES
624
+ ($workflowId, $executionId, $stepKey, $storeName, $storeId, $sinceVersion, $readPaths)
625
+ ON CONFLICT(store_name, store_id, execution_id, step_key)
626
+ DO UPDATE SET
627
+ workflow_id = excluded.workflow_id,
628
+ since_version = excluded.since_version,
629
+ read_paths = excluded.read_paths`).run({
630
+ $workflowId: waiter.workflowId,
631
+ $executionId: waiter.executionId,
632
+ $stepKey: waiter.stepKey,
633
+ $storeName: waiter.storeName,
634
+ $storeId: waiter.storeId,
635
+ $sinceVersion: waiter.sinceVersion,
636
+ $readPaths: JSON.stringify(waiter.readPaths)
637
+ });
638
+ }
639
+ getStoreWaiters(storeName, storeId) {
640
+ return this.db.query(`SELECT * FROM store_waiters
641
+ WHERE store_name = $storeName AND store_id = $storeId`).all({ $storeName: storeName, $storeId: storeId });
642
+ }
643
+ deleteStoreWaiters(storeName, storeId) {
644
+ this.db.query(`DELETE FROM store_waiters WHERE store_name = $storeName AND store_id = $storeId`).run({ $storeName: storeName, $storeId: storeId });
645
+ }
646
+ deleteStepWaiters(executionId, stepKey) {
647
+ this.db.query(`DELETE FROM store_waiters WHERE execution_id = $executionId AND step_key = $stepKey`).run({ $executionId: executionId, $stepKey: stepKey });
648
+ }
649
+ }
650
+
651
+ // src/sqlite-heap.ts
652
+ class SqliteHeapClient2 {
653
+ db;
654
+ executionsDao;
655
+ stepResponsesDao;
656
+ storeWaitersDao;
657
+ effectWaitersDao;
658
+ constructor(db) {
659
+ this.db = db;
660
+ this.executionsDao = new ExecutionsDao(db);
661
+ this.stepResponsesDao = new StepResponsesDao(db);
662
+ this.storeWaitersDao = new StoreWaitersDao(db);
663
+ this.effectWaitersDao = new EffectWaitersDao(db);
664
+ }
665
+ async getAllSteps() {
666
+ return this.stepResponsesDao.getAllSteps();
667
+ }
668
+ async deleteAll() {
669
+ this.stepResponsesDao.deleteAll();
670
+ }
671
+ async readStep(params) {
672
+ const result = this.stepResponsesDao.getLatestStepResponse(params.executionId, params.stepKey);
673
+ if (!result) {
674
+ return null;
675
+ }
676
+ return {
677
+ stepResponseJson: result.step_response,
678
+ meta: {
679
+ attempt: result.step_attempt,
680
+ done: Boolean(result.step_done)
681
+ }
682
+ };
683
+ }
684
+ async writeStep(params) {
685
+ const { lease, stepKey, stepAttempt, done, stepResponseJson } = params;
686
+ const { executionId } = lease;
687
+ return runTransaction(this.db, () => {
688
+ const state = this.executionsDao.getState(executionId);
689
+ if (!state || !isLeaseCurrent(state, lease)) {
690
+ return { status: "lost" };
691
+ }
692
+ const existing = this.stepResponsesDao.getStepResponse(executionId, stepKey, stepAttempt);
693
+ if (existing) {
694
+ return {
695
+ stepResponseJson: existing.step_response,
696
+ meta: { attempt: existing.step_attempt, done: Boolean(existing.step_done) }
697
+ };
698
+ }
699
+ this.stepResponsesDao.insertStepResponse(executionId, stepKey, stepAttempt, done, stepResponseJson);
700
+ if (done) {
701
+ this.storeWaitersDao.deleteStepWaiters(executionId, stepKey);
702
+ this.effectWaitersDao.deleteStepWaiters(executionId, stepKey);
703
+ }
704
+ return { stepResponseJson, meta: { attempt: stepAttempt, done } };
705
+ });
706
+ }
707
+ }
708
+
709
+ // src/sqlite-store.ts
710
+ import { v7 as uuidv7 } from "uuid";
711
+ import {
712
+ cloneStoreState,
713
+ CasStoreClient,
714
+ decodeStoreValue,
715
+ encodeStoreValue,
716
+ storePathsIntersect
717
+ } from "@yieldstar/core";
718
+ class SqliteStoreClient2 extends CasStoreClient {
719
+ db;
720
+ executions;
721
+ storeWaitersDao;
722
+ constructor(params) {
723
+ super();
724
+ this.db = params.db;
725
+ this.executions = params.executions ?? new SqliteExecutionClient2(params.db);
726
+ this.setupDb();
727
+ this.storeWaitersDao = new StoreWaitersDao(params.db);
728
+ }
729
+ createInstanceId() {
730
+ return uuidv7();
731
+ }
732
+ async readStore(params) {
733
+ const row = this.getStoreRow(params.definition.name, params.id);
734
+ return row ? toSnapshot(row) : undefined;
735
+ }
736
+ async insertStore(params) {
737
+ return runTransaction(this.db, () => {
738
+ const row = this.getStoreRow(params.definition.name, params.id);
739
+ if (row)
740
+ return toSnapshot(row);
741
+ this.insertStoreRow({
742
+ storeName: params.definition.name,
743
+ storeId: params.id,
744
+ instanceId: params.snapshot.instanceId,
745
+ version: params.snapshot.version,
746
+ state: params.snapshot.state
747
+ });
748
+ return cloneStoreState(params.snapshot);
749
+ });
750
+ }
751
+ async getAppliedStoreStep(params) {
752
+ const row = this.getAppliedStepRow({
753
+ storeName: params.definition.name,
754
+ storeId: params.id,
755
+ stepId: params.stepId
756
+ });
757
+ return row ? { result: decodeStoreValue(row.result) } : undefined;
758
+ }
759
+ async commitStoreMutation(mutation) {
760
+ return runTransaction(this.db, () => {
761
+ if (mutation.stepId) {
762
+ const applied = this.getAppliedStepRow({
763
+ storeName: mutation.definition.name,
764
+ storeId: mutation.id,
765
+ stepId: mutation.stepId
766
+ });
767
+ if (applied) {
768
+ return {
769
+ status: "already-applied",
770
+ result: decodeStoreValue(applied.result)
771
+ };
772
+ }
773
+ }
774
+ const row = this.getStoreRow(mutation.definition.name, mutation.id);
775
+ if (!row && !mutation.expected.created)
776
+ return { status: "missing" };
777
+ if (row && (mutation.expected.created || row.instance_id !== mutation.expected.instanceId || row.version !== mutation.expected.version)) {
778
+ return { status: "conflict", snapshot: toSnapshot(row) };
779
+ }
780
+ const version = mutation.expected.version + 1;
781
+ if (mutation.expected.created) {
782
+ this.insertStoreRow({
783
+ storeName: mutation.definition.name,
784
+ storeId: mutation.id,
785
+ instanceId: mutation.expected.instanceId,
786
+ version,
787
+ state: mutation.nextState
788
+ });
789
+ } else {
790
+ const changed = this.db.query(`UPDATE stores
791
+ SET version = $version, state = $state
792
+ WHERE instance_id = $instanceId AND version = $expectedVersion`).run({
793
+ $instanceId: mutation.expected.instanceId,
794
+ $expectedVersion: mutation.expected.version,
795
+ $version: version,
796
+ $state: encodeStoreValue(mutation.nextState)
797
+ });
798
+ if (Number(changed.changes) !== 1) {
799
+ throw new Error("Store changed during its mutation transaction");
800
+ }
801
+ }
802
+ if (mutation.stepId) {
803
+ this.insertAppliedStepRow({
804
+ storeName: mutation.definition.name,
805
+ storeId: mutation.id,
806
+ stepId: mutation.stepId,
807
+ result: encodeStoreValue(mutation.result)
808
+ });
809
+ }
810
+ const executionIds = this.selectMatchingExecutionIds({
811
+ storeName: mutation.definition.name,
812
+ storeId: mutation.id,
813
+ version,
814
+ changedPaths: mutation.changedPaths
815
+ });
816
+ for (const executionId of executionIds) {
817
+ this.executions.wakeInTransaction(executionId);
818
+ }
819
+ return { status: "committed" };
820
+ });
821
+ }
822
+ async listStores(definition) {
823
+ const rows = this.db.query(`SELECT store_id FROM stores
824
+ WHERE store_name = $storeName
825
+ ORDER BY store_id ASC`).all({ $storeName: definition.name });
826
+ return rows.map((row) => row.store_id);
827
+ }
828
+ async deleteStore(params) {
829
+ runTransaction(this.db, () => {
830
+ this.db.query(`DELETE FROM stores WHERE store_name = $storeName AND store_id = $storeId`).run({
831
+ $storeName: params.definition.name,
832
+ $storeId: params.id
833
+ });
834
+ this.storeWaitersDao.deleteStoreWaiters(params.definition.name, params.id);
835
+ });
836
+ }
837
+ async commitStoreDeletion(deletion) {
838
+ return runTransaction(this.db, () => {
839
+ if (deletion.stepId) {
840
+ const applied = this.getAppliedStepRow({
841
+ storeName: deletion.definition.name,
842
+ storeId: deletion.id,
843
+ stepId: deletion.stepId
844
+ });
845
+ if (applied) {
846
+ return {
847
+ status: "already-applied",
848
+ result: decodeStoreValue(applied.result)
849
+ };
850
+ }
851
+ }
852
+ const row = this.getStoreRow(deletion.definition.name, deletion.id);
853
+ if (!row)
854
+ return { status: "missing" };
855
+ if (row.instance_id !== deletion.expected.instanceId || row.version !== deletion.expected.version) {
856
+ return { status: "conflict", snapshot: toSnapshot(row) };
857
+ }
858
+ this.storeWaitersDao.deleteStoreWaiters(deletion.definition.name, deletion.id);
859
+ this.db.query(`DELETE FROM stores WHERE instance_id = $instanceId`).run({ $instanceId: row.instance_id });
860
+ if (deletion.stepId) {
861
+ this.insertAppliedStepRow({
862
+ storeName: deletion.definition.name,
863
+ storeId: deletion.id,
864
+ stepId: deletion.stepId,
865
+ result: encodeStoreValue(deletion.result)
866
+ });
867
+ }
868
+ return { status: "committed" };
869
+ });
870
+ }
871
+ async registerWaiter(waiter) {
872
+ runTransaction(this.db, () => {
873
+ this.storeWaitersDao.upsertWaiter(waiter);
874
+ const row = this.getStoreRow(waiter.storeName, waiter.storeId);
875
+ if (!row || row.instance_id !== waiter.instanceId || row.version > waiter.sinceVersion) {
876
+ this.executions.wakeInTransaction(waiter.executionId);
877
+ }
878
+ });
879
+ }
880
+ setupDb() {
881
+ this.createStoresTable();
882
+ this.db.run(`
883
+ CREATE TABLE IF NOT EXISTS store_applied_steps (
884
+ store_name TEXT NOT NULL,
885
+ store_id TEXT NOT NULL,
886
+ execution_id TEXT NOT NULL,
887
+ step_key TEXT NOT NULL,
888
+ result TEXT NOT NULL,
889
+ PRIMARY KEY (store_name, store_id, execution_id, step_key)
890
+ );
891
+ `);
892
+ }
893
+ createStoresTable() {
894
+ this.db.run(`
895
+ CREATE TABLE IF NOT EXISTS stores (
896
+ instance_id TEXT PRIMARY KEY,
897
+ store_name TEXT NOT NULL,
898
+ store_id TEXT NOT NULL,
899
+ version INTEGER NOT NULL,
900
+ state TEXT NOT NULL,
901
+ UNIQUE (store_name, store_id)
902
+ )
903
+ `);
904
+ }
905
+ getAppliedStepRow(params) {
906
+ return this.db.query(`SELECT result FROM store_applied_steps
907
+ WHERE store_name = $storeName
908
+ AND store_id = $storeId
909
+ AND execution_id = $executionId
910
+ AND step_key = $stepKey`).get({
911
+ $storeName: params.storeName,
912
+ $storeId: params.storeId,
913
+ $executionId: params.stepId.executionId,
914
+ $stepKey: params.stepId.stepKey
915
+ });
916
+ }
917
+ insertAppliedStepRow(params) {
918
+ this.db.query(`INSERT INTO store_applied_steps
919
+ (store_name, store_id, execution_id, step_key, result)
920
+ VALUES
921
+ ($storeName, $storeId, $executionId, $stepKey, $result)`).run({
922
+ $storeName: params.storeName,
923
+ $storeId: params.storeId,
924
+ $executionId: params.stepId.executionId,
925
+ $stepKey: params.stepId.stepKey,
926
+ $result: params.result
927
+ });
928
+ }
929
+ getStoreRow(storeName, storeId) {
930
+ return this.db.query(`SELECT instance_id, state, version FROM stores
931
+ WHERE store_name = $storeName AND store_id = $storeId`).get({
932
+ $storeName: storeName,
933
+ $storeId: storeId
934
+ });
935
+ }
936
+ selectMatchingExecutionIds(params) {
937
+ const executionIds = new Set;
938
+ const rows = this.storeWaitersDao.getStoreWaiters(params.storeName, params.storeId);
939
+ for (const row of rows) {
940
+ if (row.since_version >= params.version)
941
+ continue;
942
+ const readPaths = JSON.parse(row.read_paths);
943
+ if (!storePathsIntersect(readPaths, params.changedPaths))
944
+ continue;
945
+ executionIds.add(row.execution_id);
946
+ }
947
+ return executionIds;
948
+ }
949
+ insertStoreRow(params) {
950
+ this.db.query(`INSERT INTO stores (instance_id, store_name, store_id, version, state)
951
+ VALUES ($instanceId, $storeName, $storeId, $version, $state)`).run({
952
+ $instanceId: params.instanceId,
953
+ $storeName: params.storeName,
954
+ $storeId: params.storeId,
955
+ $version: params.version,
956
+ $state: encodeStoreValue(params.state)
957
+ });
958
+ }
959
+ }
960
+ function toSnapshot(row) {
961
+ return {
962
+ state: decodeStoreValue(row.state),
963
+ instanceId: row.instance_id,
964
+ version: row.version
965
+ };
966
+ }
967
+
968
+ // src/sqlite-runtime.ts
969
+ class SqliteRuntime2 extends Runtime {
970
+ clock;
971
+ executions;
972
+ effects;
973
+ heap;
974
+ stores;
975
+ constructor(db, options = {}) {
976
+ super();
977
+ this.clock = options.clock ?? (() => Date.now());
978
+ this.executions = new SqliteExecutionClient2(db, { clock: this.clock });
979
+ this.heap = new SqliteHeapClient2(db);
980
+ this.stores = new SqliteStoreClient2({ db, executions: this.executions });
981
+ this.effects = new SqliteEffectClient2({
982
+ db,
983
+ executions: this.executions,
984
+ clock: this.clock
985
+ });
986
+ }
987
+ }
988
+ export { SqliteExecutionClient2, SqliteEffectClient2, SqliteHeapClient2, SqliteStoreClient2, SqliteRuntime2 };