@anatta.io/tva 0.0.0-stage → 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 (176) hide show
  1. package/README.md +50 -2
  2. package/dist/actions/action-consumer.d.ts +45 -0
  3. package/dist/actions/action-consumer.js +90 -0
  4. package/dist/actions/action.d.ts +67 -0
  5. package/dist/actions/action.js +12 -0
  6. package/dist/actions/index.d.ts +3 -0
  7. package/dist/actions/index.js +4 -0
  8. package/dist/actions/run-action.d.ts +31 -0
  9. package/dist/actions/run-action.js +113 -0
  10. package/dist/adapters/cloudflare/bindings.d.ts +10 -0
  11. package/dist/adapters/cloudflare/bindings.js +1 -0
  12. package/dist/adapters/cloudflare/blob-store.d.ts +39 -0
  13. package/dist/adapters/cloudflare/blob-store.js +95 -0
  14. package/dist/adapters/cloudflare/cache.d.ts +14 -0
  15. package/dist/adapters/cloudflare/cache.js +48 -0
  16. package/dist/adapters/cloudflare/faults.d.ts +7 -0
  17. package/dist/adapters/cloudflare/faults.js +39 -0
  18. package/dist/adapters/cloudflare/index.d.ts +15 -0
  19. package/dist/adapters/cloudflare/index.js +16 -0
  20. package/dist/adapters/cloudflare/jobs.d.ts +97 -0
  21. package/dist/adapters/cloudflare/jobs.js +218 -0
  22. package/dist/adapters/cloudflare/journal.d.ts +107 -0
  23. package/dist/adapters/cloudflare/journal.js +305 -0
  24. package/dist/adapters/cloudflare/ledger.d.ts +14 -0
  25. package/dist/adapters/cloudflare/ledger.js +108 -0
  26. package/dist/adapters/cloudflare/logger.d.ts +22 -0
  27. package/dist/adapters/cloudflare/logger.js +49 -0
  28. package/dist/adapters/cloudflare/manifest.d.ts +81 -0
  29. package/dist/adapters/cloudflare/manifest.js +81 -0
  30. package/dist/adapters/cloudflare/queue.d.ts +80 -0
  31. package/dist/adapters/cloudflare/queue.js +177 -0
  32. package/dist/adapters/cloudflare/retention.d.ts +24 -0
  33. package/dist/adapters/cloudflare/retention.js +103 -0
  34. package/dist/adapters/cloudflare/session-storage.d.ts +27 -0
  35. package/dist/adapters/cloudflare/session-storage.js +78 -0
  36. package/dist/adapters/cloudflare/toolkit.d.ts +16 -0
  37. package/dist/adapters/cloudflare/toolkit.js +57 -0
  38. package/dist/adapters/cloudflare/workflow-jobs.d.ts +57 -0
  39. package/dist/adapters/cloudflare/workflow-jobs.js +319 -0
  40. package/dist/adapters/journal-records.d.ts +79 -0
  41. package/dist/adapters/journal-records.js +74 -0
  42. package/dist/adapters/memory/blob-store.d.ts +9 -0
  43. package/dist/adapters/memory/blob-store.js +19 -0
  44. package/dist/adapters/memory/cache.d.ts +13 -0
  45. package/dist/adapters/memory/cache.js +36 -0
  46. package/dist/adapters/memory/clock.d.ts +1 -0
  47. package/dist/adapters/memory/clock.js +4 -0
  48. package/dist/adapters/memory/index.d.ts +11 -0
  49. package/dist/adapters/memory/index.js +11 -0
  50. package/dist/adapters/memory/jobs.d.ts +62 -0
  51. package/dist/adapters/memory/jobs.js +219 -0
  52. package/dist/adapters/memory/journal.d.ts +34 -0
  53. package/dist/adapters/memory/journal.js +135 -0
  54. package/dist/adapters/memory/ledger.d.ts +9 -0
  55. package/dist/adapters/memory/ledger.js +33 -0
  56. package/dist/adapters/memory/logger.d.ts +16 -0
  57. package/dist/adapters/memory/logger.js +21 -0
  58. package/dist/adapters/memory/manifest.d.ts +59 -0
  59. package/dist/adapters/memory/manifest.js +59 -0
  60. package/dist/adapters/memory/queue.d.ts +20 -0
  61. package/dist/adapters/memory/queue.js +102 -0
  62. package/dist/adapters/memory/session-storage.d.ts +20 -0
  63. package/dist/adapters/memory/session-storage.js +33 -0
  64. package/dist/adapters/memory/toolkit.d.ts +8 -0
  65. package/dist/adapters/memory/toolkit.js +31 -0
  66. package/dist/adapters/postgres/cache.d.ts +17 -0
  67. package/dist/adapters/postgres/cache.js +69 -0
  68. package/dist/adapters/postgres/config.d.ts +27 -0
  69. package/dist/adapters/postgres/config.js +45 -0
  70. package/dist/adapters/postgres/faults.d.ts +14 -0
  71. package/dist/adapters/postgres/faults.js +47 -0
  72. package/dist/adapters/postgres/index.d.ts +12 -0
  73. package/dist/adapters/postgres/index.js +12 -0
  74. package/dist/adapters/postgres/journal.d.ts +83 -0
  75. package/dist/adapters/postgres/journal.js +314 -0
  76. package/dist/adapters/postgres/ledger.d.ts +16 -0
  77. package/dist/adapters/postgres/ledger.js +97 -0
  78. package/dist/adapters/postgres/logger.d.ts +19 -0
  79. package/dist/adapters/postgres/logger.js +37 -0
  80. package/dist/adapters/postgres/manifest.d.ts +52 -0
  81. package/dist/adapters/postgres/manifest.js +53 -0
  82. package/dist/adapters/postgres/migrate.d.ts +20 -0
  83. package/dist/adapters/postgres/migrate.js +96 -0
  84. package/dist/adapters/postgres/queue.d.ts +64 -0
  85. package/dist/adapters/postgres/queue.js +333 -0
  86. package/dist/adapters/postgres/retention.d.ts +18 -0
  87. package/dist/adapters/postgres/retention.js +104 -0
  88. package/dist/adapters/postgres/session-storage.d.ts +20 -0
  89. package/dist/adapters/postgres/session-storage.js +71 -0
  90. package/dist/adapters/postgres/toolkit.d.ts +25 -0
  91. package/dist/adapters/postgres/toolkit.js +58 -0
  92. package/dist/adapters/postgres/with-client.d.ts +11 -0
  93. package/dist/adapters/postgres/with-client.js +29 -0
  94. package/dist/adapters/reference-executor.d.ts +171 -0
  95. package/dist/adapters/reference-executor.js +564 -0
  96. package/dist/adapters/retention.d.ts +18 -0
  97. package/dist/adapters/retention.js +13 -0
  98. package/dist/adapters/s3/blob-store.d.ts +35 -0
  99. package/dist/adapters/s3/blob-store.js +92 -0
  100. package/dist/adapters/s3/faults.d.ts +8 -0
  101. package/dist/adapters/s3/faults.js +34 -0
  102. package/dist/adapters/s3/index.d.ts +2 -0
  103. package/dist/adapters/s3/index.js +2 -0
  104. package/dist/adapters/session-envelope.d.ts +21 -0
  105. package/dist/adapters/session-envelope.js +122 -0
  106. package/dist/adapters/system-clock.d.ts +8 -0
  107. package/dist/adapters/system-clock.js +8 -0
  108. package/dist/contracts/blob-store.d.ts +11 -0
  109. package/dist/contracts/blob-store.js +1 -0
  110. package/dist/contracts/cache.d.ts +27 -0
  111. package/dist/contracts/cache.js +1 -0
  112. package/dist/contracts/duration.d.ts +23 -0
  113. package/dist/contracts/duration.js +27 -0
  114. package/dist/contracts/faults.d.ts +28 -0
  115. package/dist/contracts/faults.js +29 -0
  116. package/dist/contracts/identity.d.ts +12 -0
  117. package/dist/contracts/identity.js +1 -0
  118. package/dist/contracts/index.d.ts +14 -0
  119. package/dist/contracts/index.js +15 -0
  120. package/dist/contracts/instant.d.ts +17 -0
  121. package/dist/contracts/instant.js +12 -0
  122. package/dist/contracts/jobs.d.ts +335 -0
  123. package/dist/contracts/jobs.js +1 -0
  124. package/dist/contracts/journal.d.ts +186 -0
  125. package/dist/contracts/journal.js +10 -0
  126. package/dist/contracts/ledger.d.ts +34 -0
  127. package/dist/contracts/ledger.js +1 -0
  128. package/dist/contracts/logger.d.ts +11 -0
  129. package/dist/contracts/logger.js +1 -0
  130. package/dist/contracts/manifest.d.ts +37 -0
  131. package/dist/contracts/manifest.js +30 -0
  132. package/dist/contracts/queue.d.ts +55 -0
  133. package/dist/contracts/queue.js +1 -0
  134. package/dist/contracts/session-storage.d.ts +18 -0
  135. package/dist/contracts/session-storage.js +1 -0
  136. package/dist/contracts/toolkit.d.ts +21 -0
  137. package/dist/contracts/toolkit.js +1 -0
  138. package/dist/schedule/schedule.d.ts +40 -0
  139. package/dist/schedule/schedule.js +135 -0
  140. package/dist/webhooks/create-webhook-handler.d.ts +63 -0
  141. package/dist/webhooks/create-webhook-handler.js +246 -0
  142. package/dist/webhooks/dead-letter-consumer.d.ts +14 -0
  143. package/dist/webhooks/dead-letter-consumer.js +42 -0
  144. package/dist/webhooks/define-registry.d.ts +42 -0
  145. package/dist/webhooks/define-registry.js +56 -0
  146. package/dist/webhooks/once.d.ts +15 -0
  147. package/dist/webhooks/once.js +29 -0
  148. package/dist/webhooks/redact-customer-bodies.d.ts +29 -0
  149. package/dist/webhooks/redact-customer-bodies.js +45 -0
  150. package/dist/webhooks/replay-webhooks.d.ts +39 -0
  151. package/dist/webhooks/replay-webhooks.js +79 -0
  152. package/dist/webhooks/sign-shopify-webhook.d.ts +14 -0
  153. package/dist/webhooks/sign-shopify-webhook.js +31 -0
  154. package/dist/webhooks/webhook-consumer.d.ts +51 -0
  155. package/dist/webhooks/webhook-consumer.js +127 -0
  156. package/migrations/0001_create_ledger_claims.sql +23 -0
  157. package/migrations/0002_create_journal.sql +76 -0
  158. package/migrations/0003_create_shopify_sessions.sql +30 -0
  159. package/migrations/0004_create_journal_steps.sql +34 -0
  160. package/migrations/0007_create_job_tables.sql +71 -0
  161. package/migrations/0008_add_ledger_completed_at.sql +7 -0
  162. package/migrations/0009_create_webhook_receipts.sql +34 -0
  163. package/migrations/0010_index_webhook_deliveries_received_at.sql +2 -0
  164. package/migrations/0011_add_webhook_task_reason_code.sql +4 -0
  165. package/migrations/postgres/0001_create_ledger_claims.sql +13 -0
  166. package/migrations/postgres/0002_create_journal.sql +48 -0
  167. package/migrations/postgres/0003_create_shopify_sessions.sql +12 -0
  168. package/migrations/postgres/0004_create_journal_steps.sql +19 -0
  169. package/migrations/postgres/0005_create_cache_entries.sql +14 -0
  170. package/migrations/postgres/0006_create_queue.sql +42 -0
  171. package/migrations/postgres/0007_create_job_tables.sql +40 -0
  172. package/migrations/postgres/0008_add_ledger_completed_at.sql +3 -0
  173. package/migrations/postgres/0009_create_webhook_receipts.sql +23 -0
  174. package/migrations/postgres/0010_index_webhook_deliveries_received_at.sql +2 -0
  175. package/migrations/postgres/0011_add_webhook_task_reason_code.sql +3 -0
  176. package/package.json +137 -4
@@ -0,0 +1,76 @@
1
+ -- The run journal. Three grains, three tables, and a closed set of named
2
+ -- queries over them — see src/contracts/journal.ts for why the surface has no
3
+ -- method that takes SQL.
4
+ --
5
+ -- Distinct from the ledger: this one is queryable, it is not on the correctness
6
+ -- path, and it may be lossy under pressure without breaking anything.
7
+
8
+ CREATE TABLE journal_runs (
9
+ run_id TEXT PRIMARY KEY,
10
+ job TEXT NOT NULL,
11
+ trigger TEXT NOT NULL,
12
+ mode TEXT NOT NULL,
13
+ started_at INTEGER NOT NULL,
14
+ -- Null until finishRun. A run that never finished is the interesting one
15
+ -- during an incident, so it is a nullable column rather than a missing row.
16
+ ended_at INTEGER,
17
+ -- DeploymentIdentity as a JSON object. It is read whole and never queried
18
+ -- into: the question is always "which deployment processed this record",
19
+ -- never "every record from git sha X". A column per field would be five
20
+ -- columns serving no query.
21
+ identity TEXT NOT NULL
22
+ ) STRICT;
23
+
24
+ -- Serves recentRuns(), which is newest-first and limited.
25
+ CREATE INDEX journal_runs_started_at ON journal_runs (started_at DESC);
26
+
27
+ CREATE TABLE journal_units (
28
+ run_id TEXT NOT NULL,
29
+ unit_key TEXT NOT NULL,
30
+ unit_type TEXT NOT NULL,
31
+ -- When this unit reached its outcome. Every time-windowed query needs it,
32
+ -- and its absence on the in-memory adapter is what the suite's window
33
+ -- assertion found.
34
+ recorded_at INTEGER NOT NULL,
35
+ outcome TEXT NOT NULL,
36
+ reason_code TEXT,
37
+ plan_hash TEXT,
38
+ plan_ref TEXT,
39
+ duration_ms INTEGER NOT NULL,
40
+ -- externalIds as a JSON object: {"shopifyProductId": "gid://…"}. The keys
41
+ -- are the domain's, not ours, so there is no column set that would hold
42
+ -- them, and no query asks about one in isolation.
43
+ external_ids TEXT,
44
+ -- The grain is one record per unit per run. Re-recording a unit within a run
45
+ -- is a correction, not a second row.
46
+ PRIMARY KEY (run_id, unit_key)
47
+ ) STRICT;
48
+
49
+ -- Serves latestForUnit(): the newest record for a key, across runs. The unit is
50
+ -- the grain an operator thinks in — they arrive with an identifier and one
51
+ -- question — so this is the index that has to be there.
52
+ CREATE INDEX journal_units_unit_key ON journal_units (unit_key, recorded_at DESC);
53
+
54
+ -- Serves failuresByReason(since). The window is the whole point of the
55
+ -- parameter; without this index it is a table scan over every unit ever
56
+ -- recorded to answer a question about the last hour.
57
+ CREATE INDEX journal_units_recorded_at ON journal_units (recorded_at);
58
+
59
+ CREATE TABLE journal_calls (
60
+ run_id TEXT NOT NULL,
61
+ unit_key TEXT NOT NULL,
62
+ -- Position within the unit. The contract promises a stable order and the
63
+ -- suite asserts it, and insertion order is not a property a row has unless a
64
+ -- column carries it.
65
+ seq INTEGER NOT NULL,
66
+ system TEXT NOT NULL,
67
+ -- Our name for the operation, never a URL.
68
+ operation TEXT NOT NULL,
69
+ status TEXT NOT NULL,
70
+ http_status INTEGER,
71
+ idempotency_key TEXT,
72
+ duration_ms INTEGER,
73
+ -- Serves unit(runId, unitKey) — its leading columns are exactly the lookup,
74
+ -- and its third orders the result.
75
+ PRIMARY KEY (run_id, unit_key, seq)
76
+ ) STRICT;
@@ -0,0 +1,30 @@
1
+ -- Shopify session storage, encrypted at rest. See src/contracts/session-storage.ts
2
+ -- and docs/decisions/0005.
3
+ --
4
+ -- `id` and `shop` are the only plaintext columns, and that is deliberate: they
5
+ -- are the only two things SessionStorage ever looks up by — loadSession(id)
6
+ -- and findSessionsByShop(shop). Everything else Shopify's Session carries
7
+ -- (accessToken, refreshToken, state, scope, expires, isOnline,
8
+ -- refreshTokenExpires, onlineAccessInfo) is JSON-serialized together and
9
+ -- encrypted as one AES-256-GCM envelope — never a column per field, so there
10
+ -- is no column anywhere in this table an operator could read a token out of.
11
+ --
12
+ -- `version` is the envelope format, not the row's edit history. A future key
13
+ -- rotation needs a reader for "the old format" alongside "the new one," and
14
+ -- that is only possible to write if something on the row already says which
15
+ -- is which. #36 does not build rotation; this column is what makes it
16
+ -- buildable later without a blind migration over every existing row.
17
+ --
18
+ -- `iv` and `ciphertext` are BLOB, not TEXT: AES-GCM output and a random nonce
19
+ -- are bytes, not text, and D1/SQLite's BLOB column is exactly that.
20
+ CREATE TABLE shopify_sessions (
21
+ id TEXT PRIMARY KEY,
22
+ shop TEXT NOT NULL,
23
+ version INTEGER NOT NULL,
24
+ iv BLOB NOT NULL,
25
+ ciphertext BLOB NOT NULL
26
+ ) STRICT;
27
+
28
+ -- Serves findSessionsByShop(shop), the one query against this table that is
29
+ -- not a primary-key lookup.
30
+ CREATE INDEX shopify_sessions_shop ON shopify_sessions (shop);
@@ -0,0 +1,34 @@
1
+ -- The journal's Step grain: one row per attempt of one Job step on one unit.
2
+ -- docs/decisions/0006 says why it exists and what it must never become.
3
+ --
4
+ -- A record, not state. Nothing reads this table to decide what runs next, so
5
+ -- it has no index by Job instance and no column that names one. The reference
6
+ -- executor keeps its step state in its own tables, and Workflows keeps its own.
7
+ -- A row here holds an outcome, never a step result.
8
+
9
+ CREATE TABLE journal_steps (
10
+ -- Write order. Two attempts can end in the same millisecond under a test
11
+ -- clock, and the contract promises a stable order, which insertion order is
12
+ -- not unless a column carries it. An alias for the rowid, so it costs no
13
+ -- extra storage and every index below carries it for free.
14
+ seq INTEGER PRIMARY KEY,
15
+ run_id TEXT NOT NULL,
16
+ unit_key TEXT NOT NULL,
17
+ -- The step's name in its Job definition.
18
+ step TEXT NOT NULL,
19
+ attempt INTEGER NOT NULL,
20
+ outcome TEXT NOT NULL,
21
+ reason_code TEXT,
22
+ duration_ms INTEGER NOT NULL,
23
+ -- When the attempt ended.
24
+ recorded_at INTEGER NOT NULL
25
+ ) STRICT;
26
+
27
+ -- Serves unit(runId, unitKey): its leading columns are exactly the lookup, and
28
+ -- recorded_at then the rowid order the result.
29
+ --
30
+ -- No unique key on (run_id, unit_key, step, attempt). Unlike a unit row, a step
31
+ -- row is never a correction: each one says an attempt ended. A constraint would
32
+ -- turn a duplicate write into either a fault or a silent overwrite, and this
33
+ -- table is append-only so that neither can hide what was recorded.
34
+ CREATE INDEX journal_steps_unit ON journal_steps (run_id, unit_key, recorded_at);
@@ -0,0 +1,71 @@
1
+ -- The reference executor's step state: docs/decisions/0006. Two tables, and
2
+ -- no interface. Nothing outside the executor reads or writes them, and the
3
+ -- Jobs conformance suite tests them through `Jobs`.
4
+ --
5
+ -- 0007, not 0005: 0005 and 0006 are Postgres-only migrations (cache and
6
+ -- queue), and a number means the same change in both dialects (decision
7
+ -- 0007). The Postgres twin is postgres/0007_create_job_tables.sql.
8
+ --
9
+ -- On the correctness path, unlike the journal. A lost or wrong row runs a
10
+ -- completed step again. Every change to job_instances is one conditional
11
+ -- UPDATE on `version`, and every write to job_steps is an insert that keeps
12
+ -- the first result. Removing an ended instance deletes its job_steps rows
13
+ -- first, then its job_instances row, so a crash between the two leaves no
14
+ -- results without an instance. Nothing here relies on a transaction or on D1
15
+ -- batch atomicity.
16
+ --
17
+ -- Times are epoch milliseconds from the injected Clock, as in ledger_claims,
18
+ -- and never `CURRENT_TIMESTAMP`: the suite drives backoff and lease expiry
19
+ -- with a test clock, and a schema-side clock cannot be advanced.
20
+
21
+ CREATE TABLE job_instances (
22
+ -- name + '-' + key(input). The primary key is what makes a second dispatch
23
+ -- of the same id `already-started`.
24
+ instance_id TEXT PRIMARY KEY,
25
+ job TEXT NOT NULL,
26
+ -- The Job input as JSON, unredacted (decision 0006, Consequences).
27
+ input TEXT NOT NULL,
28
+ run_id TEXT NOT NULL,
29
+ unit_key TEXT NOT NULL,
30
+ status TEXT NOT NULL
31
+ CHECK (status IN ('running', 'failing', 'completed', 'failed')),
32
+ -- Not one of decision 0006's columns. Without it the exhaustion handler
33
+ -- cannot be told whether a step was terminal, ran out of retries or
34
+ -- returned too large a result. See InstanceRow in reference-executor.ts.
35
+ cause TEXT
36
+ CHECK (cause IN ('terminal', 'retries-exhausted', 'result-too-large')),
37
+ reason_code TEXT,
38
+ current_step INTEGER NOT NULL,
39
+ attempts INTEGER NOT NULL,
40
+ version INTEGER NOT NULL,
41
+ lease_expires_at INTEGER,
42
+ next_wake_at INTEGER,
43
+ created_at INTEGER NOT NULL,
44
+ ended_at INTEGER,
45
+ -- THE INVARIANT THE SWEEPER DEPENDS ON (decision 0006): an instance that is
46
+ -- running or failing always has a wake-up due, and an ended one never does.
47
+ -- Held by the schema, so a write that breaks it is a fault and not a Job
48
+ -- that nothing ever wakes again.
49
+ CHECK ((status IN ('running', 'failing')) = (next_wake_at IS NOT NULL))
50
+ ) STRICT;
51
+
52
+ -- Serves the sweeper's due() read: status IN ('running', 'failing') AND
53
+ -- next_wake_at <= now. Ended instances have a null next_wake_at, so the scan
54
+ -- stays on the instances that can still be woken.
55
+ CREATE INDEX job_instances_due ON job_instances (status, next_wake_at);
56
+
57
+ -- Serves the sweeper's retention delete: ended_at <= cutoff.
58
+ CREATE INDEX job_instances_ended_at ON job_instances (ended_at);
59
+
60
+ -- One row per completed step. The first result stored for a step is the one
61
+ -- that stays (INSERT ... ON CONFLICT DO NOTHING), so two consumers that both
62
+ -- ran a step cannot both commit a result.
63
+ CREATE TABLE job_steps (
64
+ instance_id TEXT NOT NULL,
65
+ step_index INTEGER NOT NULL,
66
+ -- The step result as JSON, unredacted. At most stepResultMaxBytes, which
67
+ -- the executor enforces before it writes (rule 7).
68
+ result TEXT NOT NULL,
69
+ completed_at INTEGER NOT NULL,
70
+ PRIMARY KEY (instance_id, step_index)
71
+ ) STRICT;
@@ -0,0 +1,7 @@
1
+ -- Claim, act, complete (#130). NULL while a lease is held; set by `complete`,
2
+ -- which also moves `expires_at` out to the long TTL. A claim that overwrites an
3
+ -- ended row clears it again.
4
+ --
5
+ -- `expires_at` keeps one meaning for both states: once it has passed, the row
6
+ -- holds nothing and may be overwritten or swept.
7
+ ALTER TABLE ledger_claims ADD COLUMN completed_at INTEGER;
@@ -0,0 +1,34 @@
1
+ -- Webhook receipts: each delivery the edge accepted, and one task per Action
2
+ -- that must run on it. Journal tables, but REQUIRED writes, unlike the rest of
3
+ -- the journal: dead letters, replay and the sweeper read them
4
+ -- (docs/operator-ledger.md §1, src/contracts/journal.ts).
5
+
6
+ CREATE TABLE webhook_deliveries (
7
+ -- The edge's dedup key, source:shop:topic:webhookId.
8
+ delivery_id TEXT PRIMARY KEY,
9
+ source TEXT NOT NULL,
10
+ topic TEXT NOT NULL,
11
+ shop TEXT NOT NULL,
12
+ webhook_id TEXT NOT NULL,
13
+ -- The raw body's key in the blob store, which replay reads.
14
+ body_key TEXT NOT NULL,
15
+ received_at INTEGER NOT NULL
16
+ ) STRICT;
17
+
18
+ CREATE TABLE webhook_tasks (
19
+ delivery_id TEXT NOT NULL,
20
+ -- The Action's name.
21
+ action TEXT NOT NULL,
22
+ -- received | processing | done | failed | dead | replayed. Moved only by a
23
+ -- conditional update from a state TASK_TRANSITIONS allows.
24
+ state TEXT NOT NULL,
25
+ updated_at INTEGER NOT NULL,
26
+ -- Serves delivery(id): its leading column is the lookup, its second the order.
27
+ PRIMARY KEY (delivery_id, action)
28
+ ) STRICT;
29
+
30
+ -- Serves deadTasks() and stuckTasks(): the state, then the keyset order.
31
+ CREATE INDEX webhook_tasks_state ON webhook_tasks (state, updated_at, delivery_id, action);
32
+
33
+ -- Serves deadTasks({ action }).
34
+ CREATE INDEX webhook_tasks_action_state ON webhook_tasks (action, state, updated_at, delivery_id);
@@ -0,0 +1,2 @@
1
+ -- Serves the retention sweep (#141): deliveries oldest first, past the cutoff.
2
+ CREATE INDEX webhook_deliveries_received_at ON webhook_deliveries (received_at);
@@ -0,0 +1,4 @@
1
+ -- Why a task is dead: the Action's last retryable reason code, `fault` when
2
+ -- its last attempt threw, or `dead-lettered` from the DLQ consumer (#139).
3
+ -- Written by every moveTask, and NULL unless the move gave one.
4
+ ALTER TABLE webhook_tasks ADD COLUMN reason_code TEXT;
@@ -0,0 +1,13 @@
1
+ -- The Postgres twin of ../0001_create_ledger_claims.sql. The comments there
2
+ -- say why each column and index exists. Only the dialect differs here, and the
3
+ -- parity test (src/adapters/postgres/parity.spec.ts) holds the two together.
4
+ --
5
+ -- Epoch milliseconds are BIGINT, not INTEGER: Postgres INTEGER is 32-bit and
6
+ -- overflows at about 25 days of milliseconds. docs/decisions/0007.
7
+ CREATE TABLE ledger_claims (
8
+ key TEXT PRIMARY KEY,
9
+ claimed_at BIGINT NOT NULL,
10
+ expires_at BIGINT NOT NULL
11
+ );
12
+
13
+ CREATE INDEX ledger_claims_expires_at ON ledger_claims (expires_at);
@@ -0,0 +1,48 @@
1
+ -- The Postgres twin of ../0002_create_journal.sql, which says why each column
2
+ -- and index exists.
3
+ --
4
+ -- JSON columns stay TEXT, not JSONB: JSONB reorders keys and drops duplicates,
5
+ -- so a value read back would differ from the SQLite one. docs/decisions/0007.
6
+
7
+ CREATE TABLE journal_runs (
8
+ run_id TEXT PRIMARY KEY,
9
+ job TEXT NOT NULL,
10
+ trigger TEXT NOT NULL,
11
+ mode TEXT NOT NULL,
12
+ started_at BIGINT NOT NULL,
13
+ ended_at BIGINT,
14
+ identity TEXT NOT NULL
15
+ );
16
+
17
+ CREATE INDEX journal_runs_started_at ON journal_runs (started_at DESC);
18
+
19
+ CREATE TABLE journal_units (
20
+ run_id TEXT NOT NULL,
21
+ unit_key TEXT NOT NULL,
22
+ unit_type TEXT NOT NULL,
23
+ recorded_at BIGINT NOT NULL,
24
+ outcome TEXT NOT NULL,
25
+ reason_code TEXT,
26
+ plan_hash TEXT,
27
+ plan_ref TEXT,
28
+ duration_ms BIGINT NOT NULL,
29
+ external_ids TEXT,
30
+ PRIMARY KEY (run_id, unit_key)
31
+ );
32
+
33
+ CREATE INDEX journal_units_unit_key ON journal_units (unit_key, recorded_at DESC);
34
+
35
+ CREATE INDEX journal_units_recorded_at ON journal_units (recorded_at);
36
+
37
+ CREATE TABLE journal_calls (
38
+ run_id TEXT NOT NULL,
39
+ unit_key TEXT NOT NULL,
40
+ seq INTEGER NOT NULL,
41
+ system TEXT NOT NULL,
42
+ operation TEXT NOT NULL,
43
+ status TEXT NOT NULL,
44
+ http_status INTEGER,
45
+ idempotency_key TEXT,
46
+ duration_ms BIGINT,
47
+ PRIMARY KEY (run_id, unit_key, seq)
48
+ );
@@ -0,0 +1,12 @@
1
+ -- The Postgres twin of ../0003_create_shopify_sessions.sql, which says why
2
+ -- only `id` and `shop` are plaintext. The IV and ciphertext are bytes, so
3
+ -- BYTEA where SQLite has BLOB.
4
+ CREATE TABLE shopify_sessions (
5
+ id TEXT PRIMARY KEY,
6
+ shop TEXT NOT NULL,
7
+ version INTEGER NOT NULL,
8
+ iv BYTEA NOT NULL,
9
+ ciphertext BYTEA NOT NULL
10
+ );
11
+
12
+ CREATE INDEX shopify_sessions_shop ON shopify_sessions (shop);
@@ -0,0 +1,19 @@
1
+ -- The Postgres twin of ../0004_create_journal_steps.sql, which says why the
2
+ -- table is append-only and has no unique key beyond `seq`.
3
+ --
4
+ -- SQLite's `seq INTEGER PRIMARY KEY` is the rowid, assigned on insert. An
5
+ -- identity column is the Postgres equivalent, so the same INSERT, which names
6
+ -- no `seq`, works on both.
7
+ CREATE TABLE journal_steps (
8
+ seq BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
9
+ run_id TEXT NOT NULL,
10
+ unit_key TEXT NOT NULL,
11
+ step TEXT NOT NULL,
12
+ attempt INTEGER NOT NULL,
13
+ outcome TEXT NOT NULL,
14
+ reason_code TEXT,
15
+ duration_ms BIGINT NOT NULL,
16
+ recorded_at BIGINT NOT NULL
17
+ );
18
+
19
+ CREATE INDEX journal_steps_unit ON journal_steps (run_id, unit_key, recorded_at);
@@ -0,0 +1,14 @@
1
+ -- The cache, on Postgres only. Cloudflare keeps its cache in KV, so this table
2
+ -- has no SQLite twin, and the parity test lists it as Postgres-only.
3
+ --
4
+ -- `expires_at` is epoch milliseconds from the injected Clock, as everywhere
5
+ -- else in this schema. A row at or past it is a miss (the contract's expiry is
6
+ -- inclusive), and the adapter's writes delete such rows in small batches. The
7
+ -- index serves those deletes.
8
+ CREATE TABLE cache_entries (
9
+ key TEXT PRIMARY KEY,
10
+ value TEXT NOT NULL,
11
+ expires_at BIGINT NOT NULL
12
+ );
13
+
14
+ CREATE INDEX cache_entries_expires_at ON cache_entries (expires_at);
@@ -0,0 +1,42 @@
1
+ -- The queue, on Postgres only. Cloudflare's queue is the platform's own, so
2
+ -- these tables have no SQLite twin, and the parity test lists them as
3
+ -- Postgres-only. #95 deferred pg-boss: this is a library table, read with
4
+ -- FOR UPDATE SKIP LOCKED, and no new dependency.
5
+ --
6
+ -- Times are epoch milliseconds from the injected Clock. `body` and `identity`
7
+ -- are JSON in TEXT, as everywhere else in this schema (decision 0007).
8
+ --
9
+ -- `visible_at` does two jobs. Before a delivery it is when a message may be
10
+ -- delivered, which is how `delaySeconds` works. During a delivery it is when
11
+ -- the consumer's lease runs out: a claim moves it forward by the lease, so a
12
+ -- consumer that stops without acking or retrying gives the message back when
13
+ -- the lease expires, and not before.
14
+ --
15
+ -- `attempt` counts deliveries, and is incremented when a message is claimed.
16
+ -- `queue` names the queue, so one table serves every queue an app has.
17
+ CREATE TABLE queue_messages (
18
+ id TEXT PRIMARY KEY,
19
+ queue TEXT NOT NULL,
20
+ body TEXT NOT NULL,
21
+ identity TEXT NOT NULL,
22
+ enqueued_at BIGINT NOT NULL,
23
+ visible_at BIGINT NOT NULL,
24
+ attempt INTEGER NOT NULL
25
+ );
26
+
27
+ -- Serves the claim: the visible messages of one queue, oldest first.
28
+ CREATE INDEX queue_messages_visible ON queue_messages (queue, visible_at);
29
+
30
+ -- A message that ran out of attempts. It stays here until an operator reads it
31
+ -- and replays it, which moves it back to queue_messages with a fresh count.
32
+ CREATE TABLE queue_dead_letters (
33
+ id TEXT PRIMARY KEY,
34
+ queue TEXT NOT NULL,
35
+ body TEXT NOT NULL,
36
+ identity TEXT NOT NULL,
37
+ enqueued_at BIGINT NOT NULL,
38
+ attempt INTEGER NOT NULL,
39
+ dead_at BIGINT NOT NULL
40
+ );
41
+
42
+ CREATE INDEX queue_dead_letters_dead_at ON queue_dead_letters (queue, dead_at);
@@ -0,0 +1,40 @@
1
+ -- The Postgres twin of ../0007_create_job_tables.sql, which says what each
2
+ -- table and column is for. No Jobs executor runs on Postgres yet. The tables
3
+ -- are here so the two dialects stay in step (decision 0007), and so the
4
+ -- reference executor's Postgres store has its schema when it is built.
5
+ --
6
+ -- Times are epoch milliseconds from the injected Clock, as BIGINT. `input`
7
+ -- and `result` are JSON in TEXT, as everywhere else in this schema (decision
8
+ -- 0007). The CHECKs are SQLite's, unchanged.
9
+ CREATE TABLE job_instances (
10
+ instance_id TEXT PRIMARY KEY,
11
+ job TEXT NOT NULL,
12
+ input TEXT NOT NULL,
13
+ run_id TEXT NOT NULL,
14
+ unit_key TEXT NOT NULL,
15
+ status TEXT NOT NULL
16
+ CHECK (status IN ('running', 'failing', 'completed', 'failed')),
17
+ cause TEXT
18
+ CHECK (cause IN ('terminal', 'retries-exhausted', 'result-too-large')),
19
+ reason_code TEXT,
20
+ current_step INTEGER NOT NULL,
21
+ attempts INTEGER NOT NULL,
22
+ version INTEGER NOT NULL,
23
+ lease_expires_at BIGINT,
24
+ next_wake_at BIGINT,
25
+ created_at BIGINT NOT NULL,
26
+ ended_at BIGINT,
27
+ CHECK ((status IN ('running', 'failing')) = (next_wake_at IS NOT NULL))
28
+ );
29
+
30
+ CREATE INDEX job_instances_due ON job_instances (status, next_wake_at);
31
+
32
+ CREATE INDEX job_instances_ended_at ON job_instances (ended_at);
33
+
34
+ CREATE TABLE job_steps (
35
+ instance_id TEXT NOT NULL,
36
+ step_index INTEGER NOT NULL,
37
+ result TEXT NOT NULL,
38
+ completed_at BIGINT NOT NULL,
39
+ PRIMARY KEY (instance_id, step_index)
40
+ );
@@ -0,0 +1,3 @@
1
+ -- The Postgres twin of ../0008_add_ledger_completed_at.sql, which says what the
2
+ -- column means.
3
+ ALTER TABLE ledger_claims ADD COLUMN completed_at BIGINT;
@@ -0,0 +1,23 @@
1
+ -- The Postgres twin of ../0009_create_webhook_receipts.sql, which says what
2
+ -- each table and index is for.
3
+ CREATE TABLE webhook_deliveries (
4
+ delivery_id TEXT PRIMARY KEY,
5
+ source TEXT NOT NULL,
6
+ topic TEXT NOT NULL,
7
+ shop TEXT NOT NULL,
8
+ webhook_id TEXT NOT NULL,
9
+ body_key TEXT NOT NULL,
10
+ received_at BIGINT NOT NULL
11
+ );
12
+
13
+ CREATE TABLE webhook_tasks (
14
+ delivery_id TEXT NOT NULL,
15
+ action TEXT NOT NULL,
16
+ state TEXT NOT NULL,
17
+ updated_at BIGINT NOT NULL,
18
+ PRIMARY KEY (delivery_id, action)
19
+ );
20
+
21
+ CREATE INDEX webhook_tasks_state ON webhook_tasks (state, updated_at, delivery_id, action);
22
+
23
+ CREATE INDEX webhook_tasks_action_state ON webhook_tasks (action, state, updated_at, delivery_id);
@@ -0,0 +1,2 @@
1
+ -- The Postgres twin of ../0010_index_webhook_deliveries_received_at.sql.
2
+ CREATE INDEX webhook_deliveries_received_at ON webhook_deliveries (received_at);
@@ -0,0 +1,3 @@
1
+ -- The Postgres twin of ../0011_add_webhook_task_reason_code.sql, which says
2
+ -- what the column is for.
3
+ ALTER TABLE webhook_tasks ADD COLUMN reason_code TEXT;
package/package.json CHANGED
@@ -1,6 +1,139 @@
1
1
  {
2
2
  "name": "@anatta.io/tva",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "The platform primitives a Shopify React Router app does not come with",
5
+ "type": "module",
6
+ "main": "./dist/contracts/index.js",
7
+ "types": "./dist/contracts/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "@anatta.io/source": "./src/contracts/index.ts",
11
+ "types": "./dist/contracts/index.d.ts",
12
+ "default": "./dist/contracts/index.js"
13
+ },
14
+ "./adapters/cloudflare": {
15
+ "@anatta.io/source": "./src/adapters/cloudflare/index.ts",
16
+ "types": "./dist/adapters/cloudflare/index.d.ts",
17
+ "default": "./dist/adapters/cloudflare/index.js"
18
+ },
19
+ "./adapters/memory": {
20
+ "@anatta.io/source": "./src/adapters/memory/index.ts",
21
+ "types": "./dist/adapters/memory/index.d.ts",
22
+ "default": "./dist/adapters/memory/index.js"
23
+ },
24
+ "./adapters/postgres": {
25
+ "@anatta.io/source": "./src/adapters/postgres/index.ts",
26
+ "types": "./dist/adapters/postgres/index.d.ts",
27
+ "default": "./dist/adapters/postgres/index.js"
28
+ },
29
+ "./adapters/s3": {
30
+ "@anatta.io/source": "./src/adapters/s3/index.ts",
31
+ "types": "./dist/adapters/s3/index.d.ts",
32
+ "default": "./dist/adapters/s3/index.js"
33
+ },
34
+ "./schedule": {
35
+ "@anatta.io/source": "./src/schedule/schedule.ts",
36
+ "types": "./dist/schedule/schedule.d.ts",
37
+ "default": "./dist/schedule/schedule.js"
38
+ },
39
+ "./webhooks": {
40
+ "@anatta.io/source": "./src/webhooks/sign-shopify-webhook.ts",
41
+ "types": "./dist/webhooks/sign-shopify-webhook.d.ts",
42
+ "default": "./dist/webhooks/sign-shopify-webhook.js"
43
+ },
44
+ "./webhooks/consumer": {
45
+ "@anatta.io/source": "./src/webhooks/webhook-consumer.ts",
46
+ "types": "./dist/webhooks/webhook-consumer.d.ts",
47
+ "default": "./dist/webhooks/webhook-consumer.js"
48
+ },
49
+ "./actions": {
50
+ "@anatta.io/source": "./src/actions/index.ts",
51
+ "types": "./dist/actions/index.d.ts",
52
+ "default": "./dist/actions/index.js"
53
+ },
54
+ "./webhooks/edge": {
55
+ "@anatta.io/source": "./src/webhooks/create-webhook-handler.ts",
56
+ "types": "./dist/webhooks/create-webhook-handler.d.ts",
57
+ "default": "./dist/webhooks/create-webhook-handler.js"
58
+ },
59
+ "./webhooks/once": {
60
+ "@anatta.io/source": "./src/webhooks/once.ts",
61
+ "types": "./dist/webhooks/once.d.ts",
62
+ "default": "./dist/webhooks/once.js"
63
+ },
64
+ "./webhooks/redact": {
65
+ "@anatta.io/source": "./src/webhooks/redact-customer-bodies.ts",
66
+ "types": "./dist/webhooks/redact-customer-bodies.d.ts",
67
+ "default": "./dist/webhooks/redact-customer-bodies.js"
68
+ }
69
+ },
70
+ "files": [
71
+ "dist",
72
+ "migrations",
73
+ "!migrations/**/*.md"
74
+ ],
75
+ "publishConfig": {
76
+ "access": "restricted"
77
+ },
78
+ "workspaces": [
79
+ "examples/cloudflare",
80
+ "examples/cyber-onboarding"
81
+ ],
82
+ "engines": {
83
+ "node": ">=20"
84
+ },
85
+ "devEngines": {
86
+ "runtime": {
87
+ "name": "node",
88
+ "version": ">=22.13.0"
89
+ }
90
+ },
91
+ "scripts": {
92
+ "prepare": "wrangler types",
93
+ "types": "wrangler types",
94
+ "typecheck": "tsc --noEmit",
95
+ "test": "vitest run",
96
+ "test:postgres": "vitest run -c vitest.postgres.config.ts",
97
+ "test:s3": "vitest run -c vitest.s3.config.ts",
98
+ "db:up": "docker compose up -d --wait postgres minio",
99
+ "s3:up": "docker compose up -d --wait minio",
100
+ "lint": "eslint .",
101
+ "format": "prettier --write .",
102
+ "format:check": "prettier --check .",
103
+ "check": "npm run format:check && npm run lint && npm run typecheck && npm run test",
104
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
105
+ "prepublishOnly": "npm run check && npm run build"
106
+ },
107
+ "peerDependencies": {
108
+ "@aws-sdk/client-s3": "^3.0.0",
109
+ "@shopify/shopify-api": "^15.0.0",
110
+ "@shopify/shopify-app-session-storage": "^7.0.0",
111
+ "pg": "^8.0.0"
112
+ },
113
+ "peerDependenciesMeta": {
114
+ "@aws-sdk/client-s3": {
115
+ "optional": true
116
+ },
117
+ "pg": {
118
+ "optional": true
119
+ }
120
+ },
121
+ "devDependencies": {
122
+ "@aws-sdk/client-s3": "^3.1155.0",
123
+ "@cloudflare/vitest-pool-workers": "0.22.0",
124
+ "@shopify/shopify-api": "^15.0.0",
125
+ "@shopify/shopify-app-session-storage": "^7.0.0",
126
+ "@stylistic/eslint-plugin": "^5.10.0",
127
+ "@types/node": "^22.10.2",
128
+ "@types/pg": "^8.23.1",
129
+ "@vitest/eslint-plugin": "^1.6.27",
130
+ "eslint": "^9.17.0",
131
+ "eslint-config-prettier": "^10.1.8",
132
+ "pg": "^8.23.1",
133
+ "prettier": "^3.9.9",
134
+ "typescript": "^5.7.2",
135
+ "typescript-eslint": "^8.18.1",
136
+ "vitest": "^4.1.11",
137
+ "wrangler": "4.124.0"
138
+ }
139
+ }