@secondlayer/shared 15.2.1 → 15.3.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 (84) hide show
  1. package/dist/src/archive/range-digest.d.ts +61 -9
  2. package/dist/src/archive/semantic-digest-builder.d.ts +61 -9
  3. package/dist/src/coverage/index.d.ts +64 -9
  4. package/dist/src/coverage/index.js.map +2 -2
  5. package/dist/src/db/index.d.ts +63 -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 +61 -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 +108 -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 +69 -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 +77 -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 +69 -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 +61 -9
  24. package/dist/src/db/queries/subgraphs.d.ts +122 -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 +61 -9
  28. package/dist/src/db/queries/webhook-replays.d.ts +73 -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 +96 -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 +62 -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 +11 -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 +98 -19
  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 +62 -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 +61 -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/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,8 @@ 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;
1234
1279
  created_at: Generated<Date>;
1235
1280
  }
1236
1281
  type WebhookReplayStatus = "scanning" | "running" | "done" | "failed";
@@ -1280,6 +1325,13 @@ interface TriggerEvaluatorStateTable {
1280
1325
  /** Watermark for the async sBTC settlement-confirmed webhook scan; null until
1281
1326
  * the evaluator fast-forwards it on first run. */
1282
1327
  last_settlement_scan_at: ColumnType<Date | null, Date | null | undefined, Date | null>;
1328
+ /** `sbtc_settlements.seq` watermark. Null until the first settlement row is
1329
+ * scanned; `last_settlement_scan_at` is the forward-only floor until then. */
1330
+ last_settlement_scan_seq: ColumnType<bigint | null, bigint | number | null | undefined, bigint | number | null>;
1331
+ /** Counts reorg rewinds. The evaluator checks it under the row lock in the
1332
+ * transaction that emits a batch and advances the cursor, so a reorg that
1333
+ * lands mid-tick rolls the batch back. */
1334
+ reorg_generation: ColumnType<bigint, bigint | number | undefined, bigint | number>;
1283
1335
  /** Streams cursor (`<block_height>:<event_index>`) for the Bitcoin (Runes)
1284
1336
  * chain-trigger evaluator (plan 060) — a separate clock from
1285
1337
  * `last_processed_block` (Stacks). Null until the evaluator fast-forwards
@@ -197,6 +197,21 @@ function tlsOptions(url) {
197
197
  // src/db/index.ts
198
198
  import { sql as sql2 } from "kysely";
199
199
 
200
+ // src/db/types.ts
201
+ var ABI_STATUSES = [
202
+ "pending",
203
+ "fetched",
204
+ "unparseable",
205
+ "failed"
206
+ ];
207
+ var OBSERVER_JOURNAL_STATUSES = [
208
+ "received",
209
+ "processed",
210
+ "failed",
211
+ "retried",
212
+ "poisoned",
213
+ "skipped"
214
+ ];
200
215
  // src/db/source-read-columns.ts
201
216
  var SOURCE_READ_COLUMNS = {
202
217
  blocks: [
@@ -815,6 +830,7 @@ var TABLE_TO_DB = {
815
830
  archive_fetches: "target",
816
831
  usage_ledger: "target",
817
832
  account_balance_alerts: "target",
833
+ hosted_stack_state: "target",
818
834
  webhook_incident_alerts: "target",
819
835
  sentinel_accounts: "target",
820
836
  waitlists: "target",
@@ -1122,5 +1138,5 @@ export {
1122
1138
  readChainReorgsSince
1123
1139
  };
1124
1140
 
1125
- //# debugId=00DF878ABBDF7B5B64756E2164756E21
1141
+ //# debugId=153F9B784D1E2E2A64756E2164756E21
1126
1142
  //# sourceMappingURL=chain-reorgs.js.map