@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
@@ -1,4 +1,6 @@
1
1
  import { Kysely } from "kysely";
2
+ /** Why a webhook is paused: the customer did it, or the circuit breaker did. */
3
+ type WebhookPauseReason = "user" | "circuit";
2
4
  /** Live state of one stored replay. */
3
5
  interface ReplayProgress {
4
6
  replayId: string;
@@ -60,6 +62,10 @@ interface SubgraphVerification {
60
62
  /** What stays unproven even at this level, e.g. tx attribution of writes. */
61
63
  unproven: string[];
62
64
  }
65
+ /** Every state `subgraphs.status` can hold; mirrored by the
66
+ * `subgraphs_status_check` CHECK (migration 0189). */
67
+ declare const SUBGRAPH_STATES: readonly ["active", "reindexing", "error"];
68
+ type SubgraphStatus = (typeof SUBGRAPH_STATES)[number];
63
69
  interface BlocksTable {
64
70
  height: number;
65
71
  hash: string;
@@ -199,7 +205,7 @@ interface SubgraphsTable {
199
205
  id: Generated<string>;
200
206
  name: string;
201
207
  version: Generated<string>;
202
- status: Generated<string>;
208
+ status: Generated<SubgraphStatus>;
203
209
  definition: Record<string, unknown>;
204
210
  schema_hash: string;
205
211
  handler_path: string;
@@ -216,8 +222,10 @@ interface SubgraphsTable {
216
222
  handler_code: string | null;
217
223
  source_code: string | null;
218
224
  project_id: string | null;
219
- /** Paid (wallet-ghost) deploys expire unless renewed or claimed; NULL = no expiry. */
220
- expires_at: Date | null;
225
+ /** Set while the account's metered reads are refused (spend cap, credits).
226
+ * Both columns are set or both null; `last_error` stays for real errors. */
227
+ billing_paused_at: Date | null;
228
+ billing_paused_code: string | null;
221
229
  /** (event type, contract) probe pairs persisted at deploy for weight classification. */
222
230
  sparse_probe_targets: unknown | null;
223
231
  /** sha256 over everything that shapes rows (schema, handler bundle,
@@ -231,6 +239,9 @@ interface SubgraphsTable {
231
239
  created_at: Generated<Date>;
232
240
  updated_at: Generated<Date>;
233
241
  }
242
+ /** Mirrored by the CHECK on `contracts.abi_status`. */
243
+ declare const ABI_STATUSES: readonly ["pending", "fetched", "unparseable", "failed"];
244
+ type AbiStatus = (typeof ABI_STATUSES)[number];
234
245
  interface ContractsTable {
235
246
  contract_id: string;
236
247
  deployer: string;
@@ -239,17 +250,23 @@ interface ContractsTable {
239
250
  abi: unknown | null;
240
251
  declared_traits: Generated<string[]>;
241
252
  inferred_standards: Generated<string[]>;
242
- abi_status: Generated<string>;
253
+ abi_status: Generated<AbiStatus>;
243
254
  abi_fetched_at: Date | null;
255
+ /** Failed fetches so far; `failed` re-polls until the registry's attempt cap. */
256
+ abi_attempts: Generated<number>;
257
+ /** When a `failed` row is next due; NULL once attempts are exhausted. */
258
+ abi_next_attempt_at: Date | null;
244
259
  created_at: Generated<Date>;
245
260
  }
261
+ /** The only reasons the runtime records; mirrored by a CHECK on `subgraph_gaps.reason`. */
262
+ type GapReason = "block_missing" | "processing_error";
246
263
  interface SubgraphGapsTable {
247
264
  id: Generated<string>;
248
265
  subgraph_id: string;
249
266
  subgraph_name: string;
250
267
  gap_start: number;
251
268
  gap_end: number;
252
- reason: string;
269
+ reason: GapReason;
253
270
  detected_at: Generated<Date>;
254
271
  resolved_at: Date | null;
255
272
  }
@@ -295,12 +312,13 @@ interface SubgraphOperationsTable {
295
312
  * (those checkpoint on subgraphs.last_processed_block). */
296
313
  cursor_block: string | number | bigint | null;
297
314
  }
315
+ type ApiKeyStatus = "active" | "revoked";
298
316
  interface ApiKeysTable {
299
317
  id: Generated<string>;
300
318
  key_hash: string;
301
319
  key_prefix: string;
302
320
  name: string | null;
303
- status: Generated<string>;
321
+ status: Generated<ApiKeyStatus>;
304
322
  rate_limit: Generated<number>;
305
323
  ip_address: string;
306
324
  account_id: string;
@@ -661,6 +679,10 @@ interface SbtcSettlementsTable {
661
679
  last_checked_at: Generated<Date>;
662
680
  /** Set once, when `settlement_confirmed` first flips true. */
663
681
  confirmed_at: Date | null;
682
+ /** Monotonic stamp from `sbtc_settlements_seq`, retaken each time the sweep
683
+ * becomes confirmed. The webhook scan's watermark. BIGINT: a string at
684
+ * runtime. */
685
+ seq: Generated<number | string>;
664
686
  updated_at: Generated<Date>;
665
687
  }
666
688
  type BnsNameEventTopic = "new-name" | "transfer-name" | "renew-name" | "burn-name" | "new-airdrop";
@@ -780,7 +802,9 @@ interface PendingForkBlocksTable {
780
802
  payload: ColumnType<unknown, unknown, unknown>;
781
803
  received_at: Generated<Date>;
782
804
  }
783
- type ObserverJournalStatus = "received" | "processed" | "failed" | "retried";
805
+ /** Mirrored by the CHECK on `observer_journal.status`. */
806
+ declare const OBSERVER_JOURNAL_STATUSES: readonly ["received", "processed", "failed", "retried", "poisoned", "skipped"];
807
+ type ObserverJournalStatus = (typeof OBSERVER_JOURNAL_STATUSES)[number];
784
808
  /**
785
809
  * Exact observer callback receipt. `raw_body` is immutable evidence; the
786
810
  * processing columns are updated only after the current derived-state write
@@ -815,7 +839,7 @@ interface StageRegistryTable {
815
839
  enabled: Generated<boolean>;
816
840
  created_at: Generated<Date>;
817
841
  }
818
- /** One versioned sweep of a stage. */
842
+ /** One versioned sweep of a stage. Nothing writes this table yet; kept because the receipts and failures tables reference it. */
819
843
  interface StageRunsTable {
820
844
  id: Generated<string>;
821
845
  stage_id: string;
@@ -949,6 +973,7 @@ interface Database {
949
973
  archive_fetches: ArchiveFetchesTable;
950
974
  usage_ledger: UsageLedgerTable;
951
975
  account_balance_alerts: AccountBalanceAlertsTable;
976
+ hosted_stack_state: HostedStackStateTable;
952
977
  webhook_incident_alerts: WebhookIncidentAlertsTable;
953
978
  cli_device_logins: CliDeviceLoginsTable;
954
979
  oauth_clients: OauthClientsTable;
@@ -1032,7 +1057,11 @@ interface UsageLedgerTable {
1032
1057
  * floor to observe past. */
1033
1058
  observed_quantity: string | number | null;
1034
1059
  usd_micros: string | number | bigint;
1060
+ /** True when the whole price was taken; derived from `debited_usd_micros`. */
1035
1061
  debited: Generated<boolean>;
1062
+ /** Amount actually taken from the balance, `0..usd_micros` (bounded the
1063
+ * other way for a negative top-up/grant row). Unpaid = price - this. */
1064
+ debited_usd_micros: Generated<string | number | bigint>;
1036
1065
  source: string;
1037
1066
  /** API key behind a metered read; null for non-read units and older rows. */
1038
1067
  api_key_id: string | null;
@@ -1050,6 +1079,14 @@ interface AccountBalanceAlertsTable {
1050
1079
  sent_2d_at: Date | null;
1051
1080
  sent_stopped_at: Date | null;
1052
1081
  }
1082
+ /** The workload host's report of one account's hosted stack. Written only by
1083
+ * `POST /internal/stack-state`; `reported_at` is the host's observation time. */
1084
+ interface HostedStackStateTable {
1085
+ account_id: string;
1086
+ state: "provisioning" | "running" | "stopped" | "destroyed";
1087
+ stop_reason: "credits" | "cap" | "idle" | null;
1088
+ reported_at: Date;
1089
+ }
1053
1090
  /** Debounce state for the webhook-down email: one row per incident, open
1054
1091
  * while `resolved_at` is null. No preference columns by design. */
1055
1092
  interface WebhookIncidentAlertsTable {
@@ -1186,7 +1223,7 @@ interface SentinelAccountsTable {
1186
1223
  linked_at: Generated<Date>;
1187
1224
  via: "created" | "consent";
1188
1225
  }
1189
- type WebhookStatus2 = "active" | "paused" | "error";
1226
+ type WebhookStatus2 = "active" | "paused";
1190
1227
  /** Webhooks react to raw chain events matched directly off the Index/Streams
1191
1228
  * clock. `chain` is the only kind since migration 0159 removed subgraph-row
1192
1229
  * webhooks; the column stays as the discriminator. */
@@ -1212,6 +1249,8 @@ interface WebhooksTable {
1212
1249
  concurrency: Generated<number>;
1213
1250
  circuit_failures: Generated<number>;
1214
1251
  circuit_opened_at: Date | null;
1252
+ /** Why a paused webhook is paused. Set exactly while `status` is `paused`. */
1253
+ pause_reason: WebhookPauseReason | null;
1215
1254
  last_delivery_at: Date | null;
1216
1255
  last_success_at: Date | null;
1217
1256
  last_error: string | null;
@@ -1222,6 +1261,10 @@ interface WebhooksTable {
1222
1261
  updated_at: Generated<Date>;
1223
1262
  }
1224
1263
  type OutboxStatus = "pending" | "delivered" | "dead";
1264
+ /** Why a row is dead: retries ran out, or a reorg removed its event. An
1265
+ * orphaned row stays dead; requeueing it would resend an event the chain no
1266
+ * longer contains. */
1267
+ type DeadReason = "exhausted" | "orphaned";
1225
1268
  interface WebhookOutboxTable {
1226
1269
  id: Generated<string>;
1227
1270
  webhook_id: string;
@@ -1251,6 +1294,14 @@ interface WebhookOutboxTable {
1251
1294
  * into an orphaned fork. Null otherwise — delivery failures track their
1252
1295
  * history on `webhooks.last_error`, not per-row. */
1253
1296
  last_error: string | null;
1297
+ /** Set exactly while `status` is `dead`. */
1298
+ dead_reason: DeadReason | null;
1299
+ /** Hash of the block a chain apply row was emitted for. Null on rows
1300
+ * written before the column existed (the payload carries it too). */
1301
+ block_hash: string | null;
1302
+ /** Set when a reorg rollback recalled this delivered apply row, so a
1303
+ * later rollback for the same reorg skips it. */
1304
+ rolled_back_at: Date | null;
1254
1305
  created_at: Generated<Date>;
1255
1306
  }
1256
1307
  type WebhookReplayStatus = "scanning" | "running" | "done" | "failed";
@@ -1301,6 +1352,13 @@ interface TriggerEvaluatorStateTable {
1301
1352
  /** Watermark for the async sBTC settlement-confirmed webhook scan; null until
1302
1353
  * the evaluator fast-forwards it on first run. */
1303
1354
  last_settlement_scan_at: ColumnType<Date | null, Date | null | undefined, Date | null>;
1355
+ /** `sbtc_settlements.seq` watermark. Null until the first settlement row is
1356
+ * scanned; `last_settlement_scan_at` is the forward-only floor until then. */
1357
+ last_settlement_scan_seq: ColumnType<bigint | null, bigint | number | null | undefined, bigint | number | null>;
1358
+ /** Counts reorg rewinds. The evaluator checks it under the row lock in the
1359
+ * transaction that emits a batch and advances the cursor, so a reorg that
1360
+ * lands mid-tick rolls the batch back. */
1361
+ reorg_generation: ColumnType<bigint, bigint | number | undefined, bigint | number>;
1304
1362
  /** Streams cursor (`<block_height>:<event_index>`) for the Bitcoin (Runes)
1305
1363
  * chain-trigger evaluator (plan 060) — a separate clock from
1306
1364
  * `last_processed_block` (Stacks). Null until the evaluator fast-forwards
@@ -1437,17 +1495,24 @@ interface FeedbackTicketsTable {
1437
1495
  declare class ReplayInProgressError extends Error {
1438
1496
  constructor();
1439
1497
  }
1498
+ /** Fail every `scanning` replay older than `STALE_SCAN_MINUTES` (its process
1499
+ * died) and stamp `finished_at` so retention can delete it. Scoped to one
1500
+ * webhook when `webhookId` is given; otherwise sweeps them all. */
1501
+ declare function reapStaleScans(db: Kysely<Database>, webhookId?: string): Promise<void>;
1440
1502
  declare function beginReplay(db: Kysely<Database>, input: {
1441
1503
  id: string
1442
1504
  webhookId: string
1443
1505
  fromBlock: number
1444
1506
  toBlock: number
1445
1507
  }): Promise<void>;
1508
+ /** Both scan writes only move a replay that is still `scanning`: a replay the
1509
+ * sweep already finalized, or a newer run that restarted the same id, is not
1510
+ * overwritten by a late scan. Returns false when nothing was updated. */
1446
1511
  declare function finishScan(db: Kysely<Database>, id: string, counts: {
1447
1512
  scanned: number
1448
1513
  enqueued: number
1449
- }): Promise<void>;
1450
- declare function failScan(db: Kysely<Database>, id: string, error: string): Promise<void>;
1514
+ }): Promise<boolean>;
1515
+ declare function failScan(db: Kysely<Database>, id: string, error: string): Promise<boolean>;
1451
1516
  /** Outbox rows of this replay still waiting to be delivered or to give up. */
1452
1517
  declare function pendingCount(db: Kysely<Database>, id: string): Promise<number>;
1453
1518
  /** Outbox rows of this replay that gave up. Finalize only; never on a poll. */
@@ -1462,8 +1527,9 @@ declare function listReplays(db: Kysely<Database>, input: {
1462
1527
  }): Promise<ReplayProgress[]>;
1463
1528
  /** Newest replays of one webhook, finalizing any that have drained. */
1464
1529
  declare function listWebhookReplays(db: Kysely<Database>, webhookId: string, limit: number): Promise<ReplayProgress[]>;
1465
- /** Retention backstop: finalize every running replay that has drained. */
1530
+ /** Retention backstop: fail stale scans, then finalize every running replay
1531
+ * that has drained. */
1466
1532
  declare function finalizeReplays(db: Kysely<Database>): Promise<void>;
1467
1533
  declare function deadSince(db: Kysely<Database>, webhookId: string, since: Date | null): Promise<number>;
1468
1534
  declare function knownEventsInRange(db: Kysely<Database>, webhookId: string, from: number, to: number): Promise<number>;
1469
- export { ReplayInProgressError, beginReplay, deadCount, deadSince, failScan, finalizeReplays, finishScan, knownEventsInRange, listReplays, listWebhookReplays, pendingCount, progressOf, readReplay };
1535
+ export { ReplayInProgressError, beginReplay, deadCount, deadSince, failScan, finalizeReplays, finishScan, knownEventsInRange, listReplays, listWebhookReplays, pendingCount, progressOf, readReplay, reapStaleScans };