@secondlayer/shared 15.2.1 → 15.3.1

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 (85) hide show
  1. package/dist/src/archive/range-digest.d.ts +67 -9
  2. package/dist/src/archive/semantic-digest-builder.d.ts +67 -9
  3. package/dist/src/coverage/index.d.ts +70 -9
  4. package/dist/src/coverage/index.js.map +2 -2
  5. package/dist/src/db/index.d.ts +69 -10
  6. package/dist/src/db/index.js +19 -1
  7. package/dist/src/db/index.js.map +5 -4
  8. package/dist/src/db/queries/chain-reorgs.d.ts +67 -9
  9. package/dist/src/db/queries/chain-reorgs.js +17 -1
  10. package/dist/src/db/queries/chain-reorgs.js.map +5 -4
  11. package/dist/src/db/queries/contracts.d.ts +114 -28
  12. package/dist/src/db/queries/contracts.js +115 -15
  13. package/dist/src/db/queries/contracts.js.map +7 -5
  14. package/dist/src/db/queries/instance.d.ts +75 -10
  15. package/dist/src/db/queries/instance.js +147 -3
  16. package/dist/src/db/queries/instance.js.map +6 -4
  17. package/dist/src/db/queries/integrity.d.ts +83 -10
  18. package/dist/src/db/queries/integrity.js +236 -2
  19. package/dist/src/db/queries/integrity.js.map +7 -4
  20. package/dist/src/db/queries/subgraph-gaps.d.ts +75 -12
  21. package/dist/src/db/queries/subgraph-gaps.js +7 -3
  22. package/dist/src/db/queries/subgraph-gaps.js.map +3 -3
  23. package/dist/src/db/queries/subgraph-operations.d.ts +67 -9
  24. package/dist/src/db/queries/subgraphs.d.ts +128 -42
  25. package/dist/src/db/queries/subgraphs.js +85 -27
  26. package/dist/src/db/queries/subgraphs.js.map +7 -6
  27. package/dist/src/db/queries/sync-scope.d.ts +67 -9
  28. package/dist/src/db/queries/webhook-replays.d.ts +79 -13
  29. package/dist/src/db/queries/webhook-replays.js +468 -19
  30. package/dist/src/db/queries/webhook-replays.js.map +9 -4
  31. package/dist/src/db/queries/webhooks.d.ts +102 -21
  32. package/dist/src/db/queries/webhooks.js +72 -17
  33. package/dist/src/db/queries/webhooks.js.map +6 -5
  34. package/dist/src/db/schema.d.ts +68 -10
  35. package/dist/src/db/schema.js +20 -1
  36. package/dist/src/db/schema.js.map +4 -3
  37. package/dist/src/db/transition.d.ts +46 -0
  38. package/dist/src/db/transition.js +38 -0
  39. package/dist/src/db/transition.js.map +10 -0
  40. package/dist/src/email/index.d.ts +16 -1
  41. package/dist/src/email/index.js +186 -130
  42. package/dist/src/email/index.js.map +5 -4
  43. package/dist/src/errors.d.ts +7 -1
  44. package/dist/src/errors.js +12 -1
  45. package/dist/src/errors.js.map +3 -3
  46. package/dist/src/index-http.d.ts +13 -3
  47. package/dist/src/index-http.js +3 -3
  48. package/dist/src/index-http.js.map +3 -3
  49. package/dist/src/index.d.ts +109 -20
  50. package/dist/src/index.js +41 -8
  51. package/dist/src/index.js.map +8 -7
  52. package/dist/src/insights/index.d.ts +68 -9
  53. package/dist/src/insights/index.js +55 -5
  54. package/dist/src/insights/index.js.map +7 -6
  55. package/dist/src/node/local-client.d.ts +67 -9
  56. package/dist/src/queue/listener.js +17 -1
  57. package/dist/src/queue/listener.js.map +5 -4
  58. package/dist/src/runtime/index.d.ts +0 -4
  59. package/dist/src/runtime/index.js +54 -4
  60. package/dist/src/runtime/index.js.map +6 -5
  61. package/dist/src/schemas/index.d.ts +36 -10
  62. package/dist/src/schemas/index.js +12 -8
  63. package/dist/src/schemas/index.js.map +4 -4
  64. package/dist/src/schemas/subgraphs.d.ts +22 -9
  65. package/dist/src/schemas/subgraphs.js +10 -6
  66. package/dist/src/schemas/subgraphs.js.map +3 -3
  67. package/dist/src/schemas/webhooks.d.ts +15 -2
  68. package/dist/src/schemas/webhooks.js +2 -2
  69. package/dist/src/schemas/webhooks.js.map +3 -3
  70. package/dist/src/subgraphs/spec.d.ts +5 -1
  71. package/dist/src/webhooks/health.d.ts +27 -0
  72. package/dist/src/webhooks/health.js +84 -0
  73. package/dist/src/webhooks/health.js.map +11 -0
  74. package/migrations/0184_status_checks.ts +92 -0
  75. package/migrations/0185_reorg_generation_settlement_seq.ts +89 -0
  76. package/migrations/0186_api_keys_one_active_tenant_per_name.ts +43 -0
  77. package/migrations/0187_accounts_email_lower_unique.ts +27 -0
  78. package/migrations/0188_webhook_reasons.ts +124 -0
  79. package/migrations/0189_subgraph_lifecycle.ts +83 -0
  80. package/migrations/0190_usage_ledger_debited_usd_micros.ts +59 -0
  81. package/migrations/0191_indexer_state_machines.ts +83 -0
  82. package/migrations/0192_hosted_stack_state.ts +37 -0
  83. package/migrations/0193_accounts_ghost_has_no_email.ts +34 -0
  84. package/migrations/0194_webhook_outbox_block_hash.ts +46 -0
  85. package/package.json +9 -1
@@ -40,6 +40,12 @@ interface SubgraphVerification {
40
40
  /** What stays unproven even at this level, e.g. tx attribution of writes. */
41
41
  unproven: string[];
42
42
  }
43
+ /** Every state `subgraphs.status` can hold; mirrored by the
44
+ * `subgraphs_status_check` CHECK (migration 0189). */
45
+ declare const SUBGRAPH_STATES: readonly ["active", "reindexing", "error"];
46
+ type SubgraphStatus = (typeof SUBGRAPH_STATES)[number];
47
+ /** Why a webhook is paused: the customer did it, or the circuit breaker did. */
48
+ type WebhookPauseReason = "user" | "circuit";
43
49
  interface BlocksTable {
44
50
  height: number;
45
51
  hash: string;
@@ -179,7 +185,7 @@ interface SubgraphsTable {
179
185
  id: Generated<string>;
180
186
  name: string;
181
187
  version: Generated<string>;
182
- status: Generated<string>;
188
+ status: Generated<SubgraphStatus>;
183
189
  definition: Record<string, unknown>;
184
190
  schema_hash: string;
185
191
  handler_path: string;
@@ -196,8 +202,10 @@ interface SubgraphsTable {
196
202
  handler_code: string | null;
197
203
  source_code: string | null;
198
204
  project_id: string | null;
199
- /** Paid (wallet-ghost) deploys expire unless renewed or claimed; NULL = no expiry. */
200
- expires_at: Date | null;
205
+ /** Set while the account's metered reads are refused (spend cap, credits).
206
+ * Both columns are set or both null; `last_error` stays for real errors. */
207
+ billing_paused_at: Date | null;
208
+ billing_paused_code: string | null;
201
209
  /** (event type, contract) probe pairs persisted at deploy for weight classification. */
202
210
  sparse_probe_targets: unknown | null;
203
211
  /** sha256 over everything that shapes rows (schema, handler bundle,
@@ -211,6 +219,9 @@ interface SubgraphsTable {
211
219
  created_at: Generated<Date>;
212
220
  updated_at: Generated<Date>;
213
221
  }
222
+ /** Mirrored by the CHECK on `contracts.abi_status`. */
223
+ declare const ABI_STATUSES: readonly ["pending", "fetched", "unparseable", "failed"];
224
+ type AbiStatus = (typeof ABI_STATUSES)[number];
214
225
  interface ContractsTable {
215
226
  contract_id: string;
216
227
  deployer: string;
@@ -219,17 +230,23 @@ interface ContractsTable {
219
230
  abi: unknown | null;
220
231
  declared_traits: Generated<string[]>;
221
232
  inferred_standards: Generated<string[]>;
222
- abi_status: Generated<string>;
233
+ abi_status: Generated<AbiStatus>;
223
234
  abi_fetched_at: Date | null;
235
+ /** Failed fetches so far; `failed` re-polls until the registry's attempt cap. */
236
+ abi_attempts: Generated<number>;
237
+ /** When a `failed` row is next due; NULL once attempts are exhausted. */
238
+ abi_next_attempt_at: Date | null;
224
239
  created_at: Generated<Date>;
225
240
  }
241
+ /** The only reasons the runtime records; mirrored by a CHECK on `subgraph_gaps.reason`. */
242
+ type GapReason = "block_missing" | "processing_error";
226
243
  interface SubgraphGapsTable {
227
244
  id: Generated<string>;
228
245
  subgraph_id: string;
229
246
  subgraph_name: string;
230
247
  gap_start: number;
231
248
  gap_end: number;
232
- reason: string;
249
+ reason: GapReason;
233
250
  detected_at: Generated<Date>;
234
251
  resolved_at: Date | null;
235
252
  }
@@ -275,12 +292,13 @@ interface SubgraphOperationsTable {
275
292
  * (those checkpoint on subgraphs.last_processed_block). */
276
293
  cursor_block: string | number | bigint | null;
277
294
  }
295
+ type ApiKeyStatus = "active" | "revoked";
278
296
  interface ApiKeysTable {
279
297
  id: Generated<string>;
280
298
  key_hash: string;
281
299
  key_prefix: string;
282
300
  name: string | null;
283
- status: Generated<string>;
301
+ status: Generated<ApiKeyStatus>;
284
302
  rate_limit: Generated<number>;
285
303
  ip_address: string;
286
304
  account_id: string;
@@ -641,6 +659,10 @@ interface SbtcSettlementsTable {
641
659
  last_checked_at: Generated<Date>;
642
660
  /** Set once, when `settlement_confirmed` first flips true. */
643
661
  confirmed_at: Date | null;
662
+ /** Monotonic stamp from `sbtc_settlements_seq`, retaken each time the sweep
663
+ * becomes confirmed. The webhook scan's watermark. BIGINT: a string at
664
+ * runtime. */
665
+ seq: Generated<number | string>;
644
666
  updated_at: Generated<Date>;
645
667
  }
646
668
  type BnsNameEventTopic = "new-name" | "transfer-name" | "renew-name" | "burn-name" | "new-airdrop";
@@ -760,7 +782,9 @@ interface PendingForkBlocksTable {
760
782
  payload: ColumnType<unknown, unknown, unknown>;
761
783
  received_at: Generated<Date>;
762
784
  }
763
- type ObserverJournalStatus = "received" | "processed" | "failed" | "retried";
785
+ /** Mirrored by the CHECK on `observer_journal.status`. */
786
+ declare const OBSERVER_JOURNAL_STATUSES: readonly ["received", "processed", "failed", "retried", "poisoned", "skipped"];
787
+ type ObserverJournalStatus = (typeof OBSERVER_JOURNAL_STATUSES)[number];
764
788
  /**
765
789
  * Exact observer callback receipt. `raw_body` is immutable evidence; the
766
790
  * processing columns are updated only after the current derived-state write
@@ -795,7 +819,7 @@ interface StageRegistryTable {
795
819
  enabled: Generated<boolean>;
796
820
  created_at: Generated<Date>;
797
821
  }
798
- /** One versioned sweep of a stage. */
822
+ /** One versioned sweep of a stage. Nothing writes this table yet; kept because the receipts and failures tables reference it. */
799
823
  interface StageRunsTable {
800
824
  id: Generated<string>;
801
825
  stage_id: string;
@@ -929,6 +953,7 @@ interface Database {
929
953
  archive_fetches: ArchiveFetchesTable;
930
954
  usage_ledger: UsageLedgerTable;
931
955
  account_balance_alerts: AccountBalanceAlertsTable;
956
+ hosted_stack_state: HostedStackStateTable;
932
957
  webhook_incident_alerts: WebhookIncidentAlertsTable;
933
958
  cli_device_logins: CliDeviceLoginsTable;
934
959
  oauth_clients: OauthClientsTable;
@@ -1012,7 +1037,11 @@ interface UsageLedgerTable {
1012
1037
  * floor to observe past. */
1013
1038
  observed_quantity: string | number | null;
1014
1039
  usd_micros: string | number | bigint;
1040
+ /** True when the whole price was taken; derived from `debited_usd_micros`. */
1015
1041
  debited: Generated<boolean>;
1042
+ /** Amount actually taken from the balance, `0..usd_micros` (bounded the
1043
+ * other way for a negative top-up/grant row). Unpaid = price - this. */
1044
+ debited_usd_micros: Generated<string | number | bigint>;
1016
1045
  source: string;
1017
1046
  /** API key behind a metered read; null for non-read units and older rows. */
1018
1047
  api_key_id: string | null;
@@ -1030,6 +1059,14 @@ interface AccountBalanceAlertsTable {
1030
1059
  sent_2d_at: Date | null;
1031
1060
  sent_stopped_at: Date | null;
1032
1061
  }
1062
+ /** The workload host's report of one account's hosted stack. Written only by
1063
+ * `POST /internal/stack-state`; `reported_at` is the host's observation time. */
1064
+ interface HostedStackStateTable {
1065
+ account_id: string;
1066
+ state: "provisioning" | "running" | "stopped" | "destroyed";
1067
+ stop_reason: "credits" | "cap" | "idle" | null;
1068
+ reported_at: Date;
1069
+ }
1033
1070
  /** Debounce state for the webhook-down email: one row per incident, open
1034
1071
  * while `resolved_at` is null. No preference columns by design. */
1035
1072
  interface WebhookIncidentAlertsTable {
@@ -1166,7 +1203,7 @@ interface SentinelAccountsTable {
1166
1203
  linked_at: Generated<Date>;
1167
1204
  via: "created" | "consent";
1168
1205
  }
1169
- type WebhookStatus = "active" | "paused" | "error";
1206
+ type WebhookStatus = "active" | "paused";
1170
1207
  /** Webhooks react to raw chain events matched directly off the Index/Streams
1171
1208
  * clock. `chain` is the only kind since migration 0159 removed subgraph-row
1172
1209
  * webhooks; the column stays as the discriminator. */
@@ -1192,6 +1229,8 @@ interface WebhooksTable {
1192
1229
  concurrency: Generated<number>;
1193
1230
  circuit_failures: Generated<number>;
1194
1231
  circuit_opened_at: Date | null;
1232
+ /** Why a paused webhook is paused. Set exactly while `status` is `paused`. */
1233
+ pause_reason: WebhookPauseReason | null;
1195
1234
  last_delivery_at: Date | null;
1196
1235
  last_success_at: Date | null;
1197
1236
  last_error: string | null;
@@ -1202,6 +1241,10 @@ interface WebhooksTable {
1202
1241
  updated_at: Generated<Date>;
1203
1242
  }
1204
1243
  type OutboxStatus = "pending" | "delivered" | "dead";
1244
+ /** Why a row is dead: retries ran out, or a reorg removed its event. An
1245
+ * orphaned row stays dead; requeueing it would resend an event the chain no
1246
+ * longer contains. */
1247
+ type DeadReason = "exhausted" | "orphaned";
1205
1248
  interface WebhookOutboxTable {
1206
1249
  id: Generated<string>;
1207
1250
  webhook_id: string;
@@ -1231,6 +1274,14 @@ interface WebhookOutboxTable {
1231
1274
  * into an orphaned fork. Null otherwise — delivery failures track their
1232
1275
  * history on `webhooks.last_error`, not per-row. */
1233
1276
  last_error: string | null;
1277
+ /** Set exactly while `status` is `dead`. */
1278
+ dead_reason: DeadReason | null;
1279
+ /** Hash of the block a chain apply row was emitted for. Null on rows
1280
+ * written before the column existed (the payload carries it too). */
1281
+ block_hash: string | null;
1282
+ /** Set when a reorg rollback recalled this delivered apply row, so a
1283
+ * later rollback for the same reorg skips it. */
1284
+ rolled_back_at: Date | null;
1234
1285
  created_at: Generated<Date>;
1235
1286
  }
1236
1287
  type WebhookReplayStatus = "scanning" | "running" | "done" | "failed";
@@ -1280,6 +1331,13 @@ interface TriggerEvaluatorStateTable {
1280
1331
  /** Watermark for the async sBTC settlement-confirmed webhook scan; null until
1281
1332
  * the evaluator fast-forwards it on first run. */
1282
1333
  last_settlement_scan_at: ColumnType<Date | null, Date | null | undefined, Date | null>;
1334
+ /** `sbtc_settlements.seq` watermark. Null until the first settlement row is
1335
+ * scanned; `last_settlement_scan_at` is the forward-only floor until then. */
1336
+ last_settlement_scan_seq: ColumnType<bigint | null, bigint | number | null | undefined, bigint | number | null>;
1337
+ /** Counts reorg rewinds. The evaluator checks it under the row lock in the
1338
+ * transaction that emits a batch and advances the cursor, so a reorg that
1339
+ * lands mid-tick rolls the batch back. */
1340
+ reorg_generation: ColumnType<bigint, bigint | number | undefined, bigint | number>;
1283
1341
  /** Streams cursor (`<block_height>:<event_index>`) for the Bitcoin (Runes)
1284
1342
  * chain-trigger evaluator (plan 060) — a separate clock from
1285
1343
  * `last_processed_block` (Stacks). Null until the evaluator fast-forwards
@@ -40,6 +40,12 @@ interface SubgraphVerification {
40
40
  /** What stays unproven even at this level, e.g. tx attribution of writes. */
41
41
  unproven: string[];
42
42
  }
43
+ /** Every state `subgraphs.status` can hold; mirrored by the
44
+ * `subgraphs_status_check` CHECK (migration 0189). */
45
+ declare const SUBGRAPH_STATES: readonly ["active", "reindexing", "error"];
46
+ type SubgraphStatus = (typeof SUBGRAPH_STATES)[number];
47
+ /** Why a webhook is paused: the customer did it, or the circuit breaker did. */
48
+ type WebhookPauseReason = "user" | "circuit";
43
49
  interface BlocksTable {
44
50
  height: number;
45
51
  hash: string;
@@ -179,7 +185,7 @@ interface SubgraphsTable {
179
185
  id: Generated<string>;
180
186
  name: string;
181
187
  version: Generated<string>;
182
- status: Generated<string>;
188
+ status: Generated<SubgraphStatus>;
183
189
  definition: Record<string, unknown>;
184
190
  schema_hash: string;
185
191
  handler_path: string;
@@ -196,8 +202,10 @@ interface SubgraphsTable {
196
202
  handler_code: string | null;
197
203
  source_code: string | null;
198
204
  project_id: string | null;
199
- /** Paid (wallet-ghost) deploys expire unless renewed or claimed; NULL = no expiry. */
200
- expires_at: Date | null;
205
+ /** Set while the account's metered reads are refused (spend cap, credits).
206
+ * Both columns are set or both null; `last_error` stays for real errors. */
207
+ billing_paused_at: Date | null;
208
+ billing_paused_code: string | null;
201
209
  /** (event type, contract) probe pairs persisted at deploy for weight classification. */
202
210
  sparse_probe_targets: unknown | null;
203
211
  /** sha256 over everything that shapes rows (schema, handler bundle,
@@ -211,6 +219,9 @@ interface SubgraphsTable {
211
219
  created_at: Generated<Date>;
212
220
  updated_at: Generated<Date>;
213
221
  }
222
+ /** Mirrored by the CHECK on `contracts.abi_status`. */
223
+ declare const ABI_STATUSES: readonly ["pending", "fetched", "unparseable", "failed"];
224
+ type AbiStatus = (typeof ABI_STATUSES)[number];
214
225
  interface ContractsTable {
215
226
  contract_id: string;
216
227
  deployer: string;
@@ -219,17 +230,23 @@ interface ContractsTable {
219
230
  abi: unknown | null;
220
231
  declared_traits: Generated<string[]>;
221
232
  inferred_standards: Generated<string[]>;
222
- abi_status: Generated<string>;
233
+ abi_status: Generated<AbiStatus>;
223
234
  abi_fetched_at: Date | null;
235
+ /** Failed fetches so far; `failed` re-polls until the registry's attempt cap. */
236
+ abi_attempts: Generated<number>;
237
+ /** When a `failed` row is next due; NULL once attempts are exhausted. */
238
+ abi_next_attempt_at: Date | null;
224
239
  created_at: Generated<Date>;
225
240
  }
241
+ /** The only reasons the runtime records; mirrored by a CHECK on `subgraph_gaps.reason`. */
242
+ type GapReason = "block_missing" | "processing_error";
226
243
  interface SubgraphGapsTable {
227
244
  id: Generated<string>;
228
245
  subgraph_id: string;
229
246
  subgraph_name: string;
230
247
  gap_start: number;
231
248
  gap_end: number;
232
- reason: string;
249
+ reason: GapReason;
233
250
  detected_at: Generated<Date>;
234
251
  resolved_at: Date | null;
235
252
  }
@@ -275,12 +292,13 @@ interface SubgraphOperationsTable {
275
292
  * (those checkpoint on subgraphs.last_processed_block). */
276
293
  cursor_block: string | number | bigint | null;
277
294
  }
295
+ type ApiKeyStatus = "active" | "revoked";
278
296
  interface ApiKeysTable {
279
297
  id: Generated<string>;
280
298
  key_hash: string;
281
299
  key_prefix: string;
282
300
  name: string | null;
283
- status: Generated<string>;
301
+ status: Generated<ApiKeyStatus>;
284
302
  rate_limit: Generated<number>;
285
303
  ip_address: string;
286
304
  account_id: string;
@@ -641,6 +659,10 @@ interface SbtcSettlementsTable {
641
659
  last_checked_at: Generated<Date>;
642
660
  /** Set once, when `settlement_confirmed` first flips true. */
643
661
  confirmed_at: Date | null;
662
+ /** Monotonic stamp from `sbtc_settlements_seq`, retaken each time the sweep
663
+ * becomes confirmed. The webhook scan's watermark. BIGINT: a string at
664
+ * runtime. */
665
+ seq: Generated<number | string>;
644
666
  updated_at: Generated<Date>;
645
667
  }
646
668
  type BnsNameEventTopic = "new-name" | "transfer-name" | "renew-name" | "burn-name" | "new-airdrop";
@@ -760,7 +782,9 @@ interface PendingForkBlocksTable {
760
782
  payload: ColumnType<unknown, unknown, unknown>;
761
783
  received_at: Generated<Date>;
762
784
  }
763
- type ObserverJournalStatus = "received" | "processed" | "failed" | "retried";
785
+ /** Mirrored by the CHECK on `observer_journal.status`. */
786
+ declare const OBSERVER_JOURNAL_STATUSES: readonly ["received", "processed", "failed", "retried", "poisoned", "skipped"];
787
+ type ObserverJournalStatus = (typeof OBSERVER_JOURNAL_STATUSES)[number];
764
788
  /**
765
789
  * Exact observer callback receipt. `raw_body` is immutable evidence; the
766
790
  * processing columns are updated only after the current derived-state write
@@ -795,7 +819,7 @@ interface StageRegistryTable {
795
819
  enabled: Generated<boolean>;
796
820
  created_at: Generated<Date>;
797
821
  }
798
- /** One versioned sweep of a stage. */
822
+ /** One versioned sweep of a stage. Nothing writes this table yet; kept because the receipts and failures tables reference it. */
799
823
  interface StageRunsTable {
800
824
  id: Generated<string>;
801
825
  stage_id: string;
@@ -929,6 +953,7 @@ interface Database {
929
953
  archive_fetches: ArchiveFetchesTable;
930
954
  usage_ledger: UsageLedgerTable;
931
955
  account_balance_alerts: AccountBalanceAlertsTable;
956
+ hosted_stack_state: HostedStackStateTable;
932
957
  webhook_incident_alerts: WebhookIncidentAlertsTable;
933
958
  cli_device_logins: CliDeviceLoginsTable;
934
959
  oauth_clients: OauthClientsTable;
@@ -1012,7 +1037,11 @@ interface UsageLedgerTable {
1012
1037
  * floor to observe past. */
1013
1038
  observed_quantity: string | number | null;
1014
1039
  usd_micros: string | number | bigint;
1040
+ /** True when the whole price was taken; derived from `debited_usd_micros`. */
1015
1041
  debited: Generated<boolean>;
1042
+ /** Amount actually taken from the balance, `0..usd_micros` (bounded the
1043
+ * other way for a negative top-up/grant row). Unpaid = price - this. */
1044
+ debited_usd_micros: Generated<string | number | bigint>;
1016
1045
  source: string;
1017
1046
  /** API key behind a metered read; null for non-read units and older rows. */
1018
1047
  api_key_id: string | null;
@@ -1030,6 +1059,14 @@ interface AccountBalanceAlertsTable {
1030
1059
  sent_2d_at: Date | null;
1031
1060
  sent_stopped_at: Date | null;
1032
1061
  }
1062
+ /** The workload host's report of one account's hosted stack. Written only by
1063
+ * `POST /internal/stack-state`; `reported_at` is the host's observation time. */
1064
+ interface HostedStackStateTable {
1065
+ account_id: string;
1066
+ state: "provisioning" | "running" | "stopped" | "destroyed";
1067
+ stop_reason: "credits" | "cap" | "idle" | null;
1068
+ reported_at: Date;
1069
+ }
1033
1070
  /** Debounce state for the webhook-down email: one row per incident, open
1034
1071
  * while `resolved_at` is null. No preference columns by design. */
1035
1072
  interface WebhookIncidentAlertsTable {
@@ -1166,7 +1203,7 @@ interface SentinelAccountsTable {
1166
1203
  linked_at: Generated<Date>;
1167
1204
  via: "created" | "consent";
1168
1205
  }
1169
- type WebhookStatus = "active" | "paused" | "error";
1206
+ type WebhookStatus = "active" | "paused";
1170
1207
  /** Webhooks react to raw chain events matched directly off the Index/Streams
1171
1208
  * clock. `chain` is the only kind since migration 0159 removed subgraph-row
1172
1209
  * webhooks; the column stays as the discriminator. */
@@ -1192,6 +1229,8 @@ interface WebhooksTable {
1192
1229
  concurrency: Generated<number>;
1193
1230
  circuit_failures: Generated<number>;
1194
1231
  circuit_opened_at: Date | null;
1232
+ /** Why a paused webhook is paused. Set exactly while `status` is `paused`. */
1233
+ pause_reason: WebhookPauseReason | null;
1195
1234
  last_delivery_at: Date | null;
1196
1235
  last_success_at: Date | null;
1197
1236
  last_error: string | null;
@@ -1202,6 +1241,10 @@ interface WebhooksTable {
1202
1241
  updated_at: Generated<Date>;
1203
1242
  }
1204
1243
  type OutboxStatus = "pending" | "delivered" | "dead";
1244
+ /** Why a row is dead: retries ran out, or a reorg removed its event. An
1245
+ * orphaned row stays dead; requeueing it would resend an event the chain no
1246
+ * longer contains. */
1247
+ type DeadReason = "exhausted" | "orphaned";
1205
1248
  interface WebhookOutboxTable {
1206
1249
  id: Generated<string>;
1207
1250
  webhook_id: string;
@@ -1231,6 +1274,14 @@ interface WebhookOutboxTable {
1231
1274
  * into an orphaned fork. Null otherwise — delivery failures track their
1232
1275
  * history on `webhooks.last_error`, not per-row. */
1233
1276
  last_error: string | null;
1277
+ /** Set exactly while `status` is `dead`. */
1278
+ dead_reason: DeadReason | null;
1279
+ /** Hash of the block a chain apply row was emitted for. Null on rows
1280
+ * written before the column existed (the payload carries it too). */
1281
+ block_hash: string | null;
1282
+ /** Set when a reorg rollback recalled this delivered apply row, so a
1283
+ * later rollback for the same reorg skips it. */
1284
+ rolled_back_at: Date | null;
1234
1285
  created_at: Generated<Date>;
1235
1286
  }
1236
1287
  type WebhookReplayStatus = "scanning" | "running" | "done" | "failed";
@@ -1280,6 +1331,13 @@ interface TriggerEvaluatorStateTable {
1280
1331
  /** Watermark for the async sBTC settlement-confirmed webhook scan; null until
1281
1332
  * the evaluator fast-forwards it on first run. */
1282
1333
  last_settlement_scan_at: ColumnType<Date | null, Date | null | undefined, Date | null>;
1334
+ /** `sbtc_settlements.seq` watermark. Null until the first settlement row is
1335
+ * scanned; `last_settlement_scan_at` is the forward-only floor until then. */
1336
+ last_settlement_scan_seq: ColumnType<bigint | null, bigint | number | null | undefined, bigint | number | null>;
1337
+ /** Counts reorg rewinds. The evaluator checks it under the row lock in the
1338
+ * transaction that emits a batch and advances the cursor, so a reorg that
1339
+ * lands mid-tick rolls the batch back. */
1340
+ reorg_generation: ColumnType<bigint, bigint | number | undefined, bigint | number>;
1283
1341
  /** Streams cursor (`<block_height>:<event_index>`) for the Bitcoin (Runes)
1284
1342
  * chain-trigger evaluator (plan 060) — a separate clock from
1285
1343
  * `last_processed_block` (Stacks). Null until the evaluator fast-forwards
@@ -357,6 +357,12 @@ interface SubgraphVerification {
357
357
  /** What stays unproven even at this level, e.g. tx attribution of writes. */
358
358
  unproven: string[];
359
359
  }
360
+ /** Every state `subgraphs.status` can hold; mirrored by the
361
+ * `subgraphs_status_check` CHECK (migration 0189). */
362
+ declare const SUBGRAPH_STATES: readonly ["active", "reindexing", "error"];
363
+ type SubgraphStatus = (typeof SUBGRAPH_STATES)[number];
364
+ /** Why a webhook is paused: the customer did it, or the circuit breaker did. */
365
+ type WebhookPauseReason = "user" | "circuit";
360
366
  interface BlocksTable {
361
367
  height: number;
362
368
  hash: string;
@@ -496,7 +502,7 @@ interface SubgraphsTable {
496
502
  id: Generated<string>;
497
503
  name: string;
498
504
  version: Generated<string>;
499
- status: Generated<string>;
505
+ status: Generated<SubgraphStatus>;
500
506
  definition: Record<string, unknown>;
501
507
  schema_hash: string;
502
508
  handler_path: string;
@@ -513,8 +519,10 @@ interface SubgraphsTable {
513
519
  handler_code: string | null;
514
520
  source_code: string | null;
515
521
  project_id: string | null;
516
- /** Paid (wallet-ghost) deploys expire unless renewed or claimed; NULL = no expiry. */
517
- expires_at: Date | null;
522
+ /** Set while the account's metered reads are refused (spend cap, credits).
523
+ * Both columns are set or both null; `last_error` stays for real errors. */
524
+ billing_paused_at: Date | null;
525
+ billing_paused_code: string | null;
518
526
  /** (event type, contract) probe pairs persisted at deploy for weight classification. */
519
527
  sparse_probe_targets: unknown | null;
520
528
  /** sha256 over everything that shapes rows (schema, handler bundle,
@@ -528,6 +536,9 @@ interface SubgraphsTable {
528
536
  created_at: Generated<Date>;
529
537
  updated_at: Generated<Date>;
530
538
  }
539
+ /** Mirrored by the CHECK on `contracts.abi_status`. */
540
+ declare const ABI_STATUSES: readonly ["pending", "fetched", "unparseable", "failed"];
541
+ type AbiStatus = (typeof ABI_STATUSES)[number];
531
542
  interface ContractsTable {
532
543
  contract_id: string;
533
544
  deployer: string;
@@ -536,17 +547,23 @@ interface ContractsTable {
536
547
  abi: unknown | null;
537
548
  declared_traits: Generated<string[]>;
538
549
  inferred_standards: Generated<string[]>;
539
- abi_status: Generated<string>;
550
+ abi_status: Generated<AbiStatus>;
540
551
  abi_fetched_at: Date | null;
552
+ /** Failed fetches so far; `failed` re-polls until the registry's attempt cap. */
553
+ abi_attempts: Generated<number>;
554
+ /** When a `failed` row is next due; NULL once attempts are exhausted. */
555
+ abi_next_attempt_at: Date | null;
541
556
  created_at: Generated<Date>;
542
557
  }
558
+ /** The only reasons the runtime records; mirrored by a CHECK on `subgraph_gaps.reason`. */
559
+ type GapReason = "block_missing" | "processing_error";
543
560
  interface SubgraphGapsTable {
544
561
  id: Generated<string>;
545
562
  subgraph_id: string;
546
563
  subgraph_name: string;
547
564
  gap_start: number;
548
565
  gap_end: number;
549
- reason: string;
566
+ reason: GapReason;
550
567
  detected_at: Generated<Date>;
551
568
  resolved_at: Date | null;
552
569
  }
@@ -592,12 +609,13 @@ interface SubgraphOperationsTable {
592
609
  * (those checkpoint on subgraphs.last_processed_block). */
593
610
  cursor_block: string | number | bigint | null;
594
611
  }
612
+ type ApiKeyStatus = "active" | "revoked";
595
613
  interface ApiKeysTable {
596
614
  id: Generated<string>;
597
615
  key_hash: string;
598
616
  key_prefix: string;
599
617
  name: string | null;
600
- status: Generated<string>;
618
+ status: Generated<ApiKeyStatus>;
601
619
  rate_limit: Generated<number>;
602
620
  ip_address: string;
603
621
  account_id: string;
@@ -958,6 +976,10 @@ interface SbtcSettlementsTable {
958
976
  last_checked_at: Generated<Date>;
959
977
  /** Set once, when `settlement_confirmed` first flips true. */
960
978
  confirmed_at: Date | null;
979
+ /** Monotonic stamp from `sbtc_settlements_seq`, retaken each time the sweep
980
+ * becomes confirmed. The webhook scan's watermark. BIGINT: a string at
981
+ * runtime. */
982
+ seq: Generated<number | string>;
961
983
  updated_at: Generated<Date>;
962
984
  }
963
985
  type BnsNameEventTopic = "new-name" | "transfer-name" | "renew-name" | "burn-name" | "new-airdrop";
@@ -1077,7 +1099,9 @@ interface PendingForkBlocksTable {
1077
1099
  payload: ColumnType<unknown, unknown, unknown>;
1078
1100
  received_at: Generated<Date>;
1079
1101
  }
1080
- type ObserverJournalStatus = "received" | "processed" | "failed" | "retried";
1102
+ /** Mirrored by the CHECK on `observer_journal.status`. */
1103
+ declare const OBSERVER_JOURNAL_STATUSES: readonly ["received", "processed", "failed", "retried", "poisoned", "skipped"];
1104
+ type ObserverJournalStatus = (typeof OBSERVER_JOURNAL_STATUSES)[number];
1081
1105
  /**
1082
1106
  * Exact observer callback receipt. `raw_body` is immutable evidence; the
1083
1107
  * processing columns are updated only after the current derived-state write
@@ -1112,7 +1136,7 @@ interface StageRegistryTable {
1112
1136
  enabled: Generated<boolean>;
1113
1137
  created_at: Generated<Date>;
1114
1138
  }
1115
- /** One versioned sweep of a stage. */
1139
+ /** One versioned sweep of a stage. Nothing writes this table yet; kept because the receipts and failures tables reference it. */
1116
1140
  interface StageRunsTable {
1117
1141
  id: Generated<string>;
1118
1142
  stage_id: string;
@@ -1246,6 +1270,7 @@ interface Database {
1246
1270
  archive_fetches: ArchiveFetchesTable;
1247
1271
  usage_ledger: UsageLedgerTable;
1248
1272
  account_balance_alerts: AccountBalanceAlertsTable;
1273
+ hosted_stack_state: HostedStackStateTable;
1249
1274
  webhook_incident_alerts: WebhookIncidentAlertsTable;
1250
1275
  cli_device_logins: CliDeviceLoginsTable;
1251
1276
  oauth_clients: OauthClientsTable;
@@ -1329,7 +1354,11 @@ interface UsageLedgerTable {
1329
1354
  * floor to observe past. */
1330
1355
  observed_quantity: string | number | null;
1331
1356
  usd_micros: string | number | bigint;
1357
+ /** True when the whole price was taken; derived from `debited_usd_micros`. */
1332
1358
  debited: Generated<boolean>;
1359
+ /** Amount actually taken from the balance, `0..usd_micros` (bounded the
1360
+ * other way for a negative top-up/grant row). Unpaid = price - this. */
1361
+ debited_usd_micros: Generated<string | number | bigint>;
1333
1362
  source: string;
1334
1363
  /** API key behind a metered read; null for non-read units and older rows. */
1335
1364
  api_key_id: string | null;
@@ -1347,6 +1376,14 @@ interface AccountBalanceAlertsTable {
1347
1376
  sent_2d_at: Date | null;
1348
1377
  sent_stopped_at: Date | null;
1349
1378
  }
1379
+ /** The workload host's report of one account's hosted stack. Written only by
1380
+ * `POST /internal/stack-state`; `reported_at` is the host's observation time. */
1381
+ interface HostedStackStateTable {
1382
+ account_id: string;
1383
+ state: "provisioning" | "running" | "stopped" | "destroyed";
1384
+ stop_reason: "credits" | "cap" | "idle" | null;
1385
+ reported_at: Date;
1386
+ }
1350
1387
  /** Debounce state for the webhook-down email: one row per incident, open
1351
1388
  * while `resolved_at` is null. No preference columns by design. */
1352
1389
  interface WebhookIncidentAlertsTable {
@@ -1483,7 +1520,7 @@ interface SentinelAccountsTable {
1483
1520
  linked_at: Generated<Date>;
1484
1521
  via: "created" | "consent";
1485
1522
  }
1486
- type WebhookStatus = "active" | "paused" | "error";
1523
+ type WebhookStatus = "active" | "paused";
1487
1524
  /** Webhooks react to raw chain events matched directly off the Index/Streams
1488
1525
  * clock. `chain` is the only kind since migration 0159 removed subgraph-row
1489
1526
  * webhooks; the column stays as the discriminator. */
@@ -1509,6 +1546,8 @@ interface WebhooksTable {
1509
1546
  concurrency: Generated<number>;
1510
1547
  circuit_failures: Generated<number>;
1511
1548
  circuit_opened_at: Date | null;
1549
+ /** Why a paused webhook is paused. Set exactly while `status` is `paused`. */
1550
+ pause_reason: WebhookPauseReason | null;
1512
1551
  last_delivery_at: Date | null;
1513
1552
  last_success_at: Date | null;
1514
1553
  last_error: string | null;
@@ -1519,6 +1558,10 @@ interface WebhooksTable {
1519
1558
  updated_at: Generated<Date>;
1520
1559
  }
1521
1560
  type OutboxStatus = "pending" | "delivered" | "dead";
1561
+ /** Why a row is dead: retries ran out, or a reorg removed its event. An
1562
+ * orphaned row stays dead; requeueing it would resend an event the chain no
1563
+ * longer contains. */
1564
+ type DeadReason = "exhausted" | "orphaned";
1522
1565
  interface WebhookOutboxTable {
1523
1566
  id: Generated<string>;
1524
1567
  webhook_id: string;
@@ -1548,6 +1591,14 @@ interface WebhookOutboxTable {
1548
1591
  * into an orphaned fork. Null otherwise — delivery failures track their
1549
1592
  * history on `webhooks.last_error`, not per-row. */
1550
1593
  last_error: string | null;
1594
+ /** Set exactly while `status` is `dead`. */
1595
+ dead_reason: DeadReason | null;
1596
+ /** Hash of the block a chain apply row was emitted for. Null on rows
1597
+ * written before the column existed (the payload carries it too). */
1598
+ block_hash: string | null;
1599
+ /** Set when a reorg rollback recalled this delivered apply row, so a
1600
+ * later rollback for the same reorg skips it. */
1601
+ rolled_back_at: Date | null;
1551
1602
  created_at: Generated<Date>;
1552
1603
  }
1553
1604
  type WebhookReplayStatus = "scanning" | "running" | "done" | "failed";
@@ -1597,6 +1648,13 @@ interface TriggerEvaluatorStateTable {
1597
1648
  /** Watermark for the async sBTC settlement-confirmed webhook scan; null until
1598
1649
  * the evaluator fast-forwards it on first run. */
1599
1650
  last_settlement_scan_at: ColumnType<Date | null, Date | null | undefined, Date | null>;
1651
+ /** `sbtc_settlements.seq` watermark. Null until the first settlement row is
1652
+ * scanned; `last_settlement_scan_at` is the forward-only floor until then. */
1653
+ last_settlement_scan_seq: ColumnType<bigint | null, bigint | number | null | undefined, bigint | number | null>;
1654
+ /** Counts reorg rewinds. The evaluator checks it under the row lock in the
1655
+ * transaction that emits a batch and advances the cursor, so a reorg that
1656
+ * lands mid-tick rolls the batch back. */
1657
+ reorg_generation: ColumnType<bigint, bigint | number | undefined, bigint | number>;
1600
1658
  /** Streams cursor (`<block_height>:<event_index>`) for the Bitcoin (Runes)
1601
1659
  * chain-trigger evaluator (plan 060) — a separate clock from
1602
1660
  * `last_processed_block` (Stacks). Null until the evaluator fast-forwards
@@ -1764,6 +1822,9 @@ declare function applyDecoderReceipts(state: RunnerState, receipts: readonly Dec
1764
1822
  * Persist output + checkpoint + receipts + failure in one transaction.
1765
1823
  * Runner validation is the caller's job (`applyDecoderReceipts`) so a
1766
1824
  * rejected ack never opens a transaction.
1825
+ *
1826
+ * No production caller (decoders use the batch form); kept as the exported
1827
+ * single-decoder entry point and covered by decoder-adapter-crash.test.ts.
1767
1828
  */
1768
1829
  declare function commitDecoderAdapter(db: Kysely<Database>, input: DecoderAdapterCommit): Promise<void>;
1769
1830
  /**