@secondlayer/shared 11.20.0 → 11.22.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 (43) hide show
  1. package/dist/src/archive/range-digest.d.ts +65 -0
  2. package/dist/src/archive/semantic-digest-builder.d.ts +65 -0
  3. package/dist/src/classify/index.d.ts +83 -0
  4. package/dist/src/classify/index.js +464 -0
  5. package/dist/src/classify/index.js.map +18 -0
  6. package/dist/src/coverage/index.d.ts +65 -0
  7. package/dist/src/db/index.d.ts +72 -1
  8. package/dist/src/db/index.js +3 -1
  9. package/dist/src/db/index.js.map +3 -3
  10. package/dist/src/db/queries/chain-reorgs.d.ts +65 -0
  11. package/dist/src/db/queries/chain-reorgs.js +3 -1
  12. package/dist/src/db/queries/chain-reorgs.js.map +3 -3
  13. package/dist/src/db/queries/contracts.d.ts +65 -0
  14. package/dist/src/db/queries/instance.d.ts +65 -0
  15. package/dist/src/db/queries/integrity.d.ts +65 -0
  16. package/dist/src/db/queries/subgraph-gaps.d.ts +65 -0
  17. package/dist/src/db/queries/subgraph-operations.d.ts +65 -0
  18. package/dist/src/db/queries/subgraphs.d.ts +69 -0
  19. package/dist/src/db/queries/subgraphs.js +10 -2
  20. package/dist/src/db/queries/subgraphs.js.map +4 -4
  21. package/dist/src/db/queries/sync-scope.d.ts +65 -0
  22. package/dist/src/db/queries/webhooks.d.ts +65 -0
  23. package/dist/src/db/schema.d.ts +70 -1
  24. package/dist/src/error-envelope.d.ts +48 -0
  25. package/dist/src/error-envelope.js +76 -0
  26. package/dist/src/error-envelope.js.map +10 -0
  27. package/dist/src/index.d.ts +288 -208
  28. package/dist/src/index.js +6 -1
  29. package/dist/src/index.js.map +5 -5
  30. package/dist/src/node/local-client.d.ts +65 -0
  31. package/dist/src/queue/listener.js +3 -1
  32. package/dist/src/queue/listener.js.map +3 -3
  33. package/dist/src/schemas/index.d.ts +30 -1
  34. package/dist/src/schemas/index.js.map +2 -2
  35. package/dist/src/schemas/subgraphs.d.ts +30 -1
  36. package/dist/src/schemas/subgraphs.js.map +2 -2
  37. package/dist/src/subgraphs/spec.d.ts +27 -0
  38. package/dist/src/subgraphs/spec.js +4 -1
  39. package/dist/src/subgraphs/spec.js.map +3 -3
  40. package/migrations/0155_api_failed_requests.ts +37 -0
  41. package/migrations/0156_feedback_tickets.ts +47 -0
  42. package/migrations/0157_subgraph_pin_verification.ts +35 -0
  43. package/package.json +17 -1
@@ -19,6 +19,27 @@ type FailureClass = (typeof FAILURE_CLASSES)[number];
19
19
  declare const RETRY_STATES: readonly ["open", "retrying", "halted", "resolved"];
20
20
  type RetryState = (typeof RETRY_STATES)[number];
21
21
  type BootstrapSource = "archive" | "genesis" | "import";
22
+ /**
23
+ * How far a subgraph's rows can be checked without trusting the server,
24
+ * derived from its sources and a scan of its handler. Never configured.
25
+ *
26
+ * - `state`: every input is a named state write, provable from block headers.
27
+ * Handlers run in the deterministic realm. A deployed `state` subgraph is
28
+ * verifiable: one with any reason is refused at deploy.
29
+ * - `events`: an input (events, prints, tx results, `ctx.client`) needs
30
+ * re-execution proofs, which are not served yet.
31
+ * - `none`: a source can never be proven as written (trait scope, tip-first
32
+ * backfill).
33
+ */
34
+ interface SubgraphVerification {
35
+ level: "state" | "events" | "none";
36
+ /** What stands between the subgraph and checkable rows, e.g.
37
+ * `print_event source "swaps" needs event proofs` or
38
+ * `subgraph.ts:41:12 Date: wall-clock time differs per run`. */
39
+ reasons: string[];
40
+ /** What stays unproven even at this level, e.g. tx attribution of writes. */
41
+ unproven: string[];
42
+ }
22
43
  interface BlocksTable {
23
44
  height: number;
24
45
  hash: string;
@@ -179,6 +200,11 @@ interface SubgraphsTable {
179
200
  expires_at: Date | null;
180
201
  /** (event type, contract) probe pairs persisted at deploy for weight classification. */
181
202
  sparse_probe_targets: unknown | null;
203
+ /** sha256 over everything that shapes rows (schema, handler bundle,
204
+ * startBlock, network, runtime). NULL for unbundled or older deploys. */
205
+ pin: string | null;
206
+ /** Level derived at deploy. NULL (older deploys) = not verifiable. */
207
+ verification: SubgraphVerification | null;
182
208
  created_at: Generated<Date>;
183
209
  updated_at: Generated<Date>;
184
210
  }
@@ -847,6 +873,8 @@ interface Database {
847
873
  sentinel_accounts: SentinelAccountsTable;
848
874
  waitlists: WaitlistsTable;
849
875
  waitlist_signups: WaitlistSignupsTable;
876
+ api_failed_requests: ApiFailedRequestsTable;
877
+ feedback_tickets: FeedbackTicketsTable;
850
878
  }
851
879
  interface ServiceHeartbeatsTable {
852
880
  name: string;
@@ -1055,6 +1083,43 @@ interface WaitlistSignupsTable {
1055
1083
  created_at: Generated<Date>;
1056
1084
  }
1057
1085
  /**
1086
+ * 24h hosted record of a failed API request, keyed by `request_id` and
1087
+ * scoped to the account that made it. `query` is redacted before insert.
1088
+ */
1089
+ interface ApiFailedRequestsTable {
1090
+ request_id: string;
1091
+ account_id: string;
1092
+ method: string;
1093
+ path: string;
1094
+ status: number;
1095
+ code: string;
1096
+ message: Generated<string>;
1097
+ query: Record<string, unknown>;
1098
+ origin: string | null;
1099
+ created_at: Generated<Date>;
1100
+ }
1101
+ /**
1102
+ * Hosted problem report. `attempted` is the server's own copy of the failed
1103
+ * request (never caller-supplied); `status` starts `new` until classified.
1104
+ */
1105
+ interface FeedbackTicketsTable {
1106
+ id: Generated<string>;
1107
+ account_id: string;
1108
+ idempotency_key: string | null;
1109
+ request_id: string | null;
1110
+ intent: string;
1111
+ expected: Record<string, unknown> | null;
1112
+ kind_hint: string | null;
1113
+ evidence: Record<string, unknown> | null;
1114
+ attempted: Record<string, unknown> | null;
1115
+ origin: string;
1116
+ status: Generated<"new" | "classified">;
1117
+ route: string | null;
1118
+ classification: Record<string, unknown> | null;
1119
+ classified_at: Date | null;
1120
+ created_at: Generated<Date>;
1121
+ }
1122
+ /**
1058
1123
  * Cheap, SQL-computed digests over a height range — the basis of a verify that
1059
1124
  * runs in seconds instead of hours.
1060
1125
  *
@@ -19,6 +19,27 @@ type FailureClass = (typeof FAILURE_CLASSES)[number];
19
19
  declare const RETRY_STATES: readonly ["open", "retrying", "halted", "resolved"];
20
20
  type RetryState = (typeof RETRY_STATES)[number];
21
21
  type BootstrapSource = "archive" | "genesis" | "import";
22
+ /**
23
+ * How far a subgraph's rows can be checked without trusting the server,
24
+ * derived from its sources and a scan of its handler. Never configured.
25
+ *
26
+ * - `state`: every input is a named state write, provable from block headers.
27
+ * Handlers run in the deterministic realm. A deployed `state` subgraph is
28
+ * verifiable: one with any reason is refused at deploy.
29
+ * - `events`: an input (events, prints, tx results, `ctx.client`) needs
30
+ * re-execution proofs, which are not served yet.
31
+ * - `none`: a source can never be proven as written (trait scope, tip-first
32
+ * backfill).
33
+ */
34
+ interface SubgraphVerification {
35
+ level: "state" | "events" | "none";
36
+ /** What stands between the subgraph and checkable rows, e.g.
37
+ * `print_event source "swaps" needs event proofs` or
38
+ * `subgraph.ts:41:12 Date: wall-clock time differs per run`. */
39
+ reasons: string[];
40
+ /** What stays unproven even at this level, e.g. tx attribution of writes. */
41
+ unproven: string[];
42
+ }
22
43
  interface BlocksTable {
23
44
  height: number;
24
45
  hash: string;
@@ -179,6 +200,11 @@ interface SubgraphsTable {
179
200
  expires_at: Date | null;
180
201
  /** (event type, contract) probe pairs persisted at deploy for weight classification. */
181
202
  sparse_probe_targets: unknown | null;
203
+ /** sha256 over everything that shapes rows (schema, handler bundle,
204
+ * startBlock, network, runtime). NULL for unbundled or older deploys. */
205
+ pin: string | null;
206
+ /** Level derived at deploy. NULL (older deploys) = not verifiable. */
207
+ verification: SubgraphVerification | null;
182
208
  created_at: Generated<Date>;
183
209
  updated_at: Generated<Date>;
184
210
  }
@@ -847,6 +873,8 @@ interface Database {
847
873
  sentinel_accounts: SentinelAccountsTable;
848
874
  waitlists: WaitlistsTable;
849
875
  waitlist_signups: WaitlistSignupsTable;
876
+ api_failed_requests: ApiFailedRequestsTable;
877
+ feedback_tickets: FeedbackTicketsTable;
850
878
  }
851
879
  interface ServiceHeartbeatsTable {
852
880
  name: string;
@@ -1055,6 +1083,43 @@ interface WaitlistSignupsTable {
1055
1083
  created_at: Generated<Date>;
1056
1084
  }
1057
1085
  /**
1086
+ * 24h hosted record of a failed API request, keyed by `request_id` and
1087
+ * scoped to the account that made it. `query` is redacted before insert.
1088
+ */
1089
+ interface ApiFailedRequestsTable {
1090
+ request_id: string;
1091
+ account_id: string;
1092
+ method: string;
1093
+ path: string;
1094
+ status: number;
1095
+ code: string;
1096
+ message: Generated<string>;
1097
+ query: Record<string, unknown>;
1098
+ origin: string | null;
1099
+ created_at: Generated<Date>;
1100
+ }
1101
+ /**
1102
+ * Hosted problem report. `attempted` is the server's own copy of the failed
1103
+ * request (never caller-supplied); `status` starts `new` until classified.
1104
+ */
1105
+ interface FeedbackTicketsTable {
1106
+ id: Generated<string>;
1107
+ account_id: string;
1108
+ idempotency_key: string | null;
1109
+ request_id: string | null;
1110
+ intent: string;
1111
+ expected: Record<string, unknown> | null;
1112
+ kind_hint: string | null;
1113
+ evidence: Record<string, unknown> | null;
1114
+ attempted: Record<string, unknown> | null;
1115
+ origin: string;
1116
+ status: Generated<"new" | "classified">;
1117
+ route: string | null;
1118
+ classification: Record<string, unknown> | null;
1119
+ classified_at: Date | null;
1120
+ created_at: Generated<Date>;
1121
+ }
1122
+ /**
1058
1123
  * v1 canonical byte encoding for chain identity — the shape that lets
1059
1124
  * independently-run runtimes agree on the same block.
1060
1125
  *
@@ -0,0 +1,83 @@
1
+ type ClassifierName = "jev" | "kev" | "clef" | "rules";
2
+ type Question = {
3
+ type: "choice"
4
+ instructions: string
5
+ criteria: Record<string, string>
6
+ } | {
7
+ type: "score"
8
+ instructions: string
9
+ criteria: readonly string[]
10
+ } | {
11
+ type: "boolean"
12
+ instructions: string
13
+ criteria: {
14
+ true: string
15
+ false: string
16
+ }
17
+ };
18
+ type Answer = {
19
+ type: "choice"
20
+ choice: string
21
+ probabilities: Record<string, number>
22
+ confidence: number
23
+ } | {
24
+ type: "score"
25
+ score: number
26
+ probabilities: Record<string, number>
27
+ confidence: number
28
+ } | {
29
+ type: "boolean"
30
+ probability: number
31
+ confidence: number
32
+ } | {
33
+ type: "refusal"
34
+ };
35
+ type Answers<Q extends Record<string, Question>> = { [K in keyof Q] : Answer };
36
+ type ClassifyResult<Q extends Record<string, Question>> = {
37
+ provider: ClassifierName
38
+ /** response.modelId when the backend reports one. */
39
+ modelId: string
40
+ answers: Answers<Q>
41
+ };
42
+ /** Deterministic answers for the offline provider. Return a value per question
43
+ * it can decide; omitted questions come back as `{ type: "refusal" }`. */
44
+ type RuleValue = string | number | boolean;
45
+ type RulesFn<Q extends Record<string, Question>> = (state: string | Record<string, unknown>) => { [K in keyof Q]? : RuleValue } | null;
46
+ declare class ClassifierInputError extends Error {
47
+ constructor(message: string);
48
+ }
49
+ /**
50
+ * One classifier call over jev, kev, clef or offline rules.
51
+ * Returns null on any provider failure (fail-open: callers decide what null
52
+ * means). Only ClassifierInputError, a bad question definition, escapes.
53
+ * Never logs state or response bodies.
54
+ */
55
+ declare function classify<const Q extends Record<string, Question>>(input: {
56
+ state: string | Record<string, unknown>
57
+ questions: Q
58
+ rules?: RulesFn<Q>
59
+ /** Overrides resolveProvider(); used by classifier-compare. */
60
+ provider?: ClassifierName
61
+ /** Overrides CLASSIFIER_MODEL / the provider default. */
62
+ modelId?: string
63
+ /** Default 5000. */
64
+ timeoutMs?: number
65
+ env?: Record<string, string | undefined>
66
+ /** Tests only (kev/clef). */
67
+ fetchImpl?: typeof fetch
68
+ }): Promise<ClassifyResult<Q> | null>;
69
+ type Env = Record<string, string | undefined>;
70
+ /** CLASSIFIER picks the backend; unset means jev when the gateway key is set,
71
+ * else offline rules. A backend missing credentials falls back to rules. */
72
+ declare function resolveProvider(env?: Env): {
73
+ name: ClassifierName
74
+ modelId: string
75
+ };
76
+ /** (p_max - 1/K) / (1 - 1/K). One option gives 1. */
77
+ declare function choiceConfidence(probabilities: Record<string, number>): number;
78
+ /** max(0, 1 - E|level - mode| / D); D = mean distance of a uniform
79
+ * distribution over the levels from the middle level. */
80
+ declare function scoreConfidence(probabilities: Record<string, number>, levels: number): number;
81
+ /** Host-defined: |2p - 1|. Not comparable with choice/score confidence. */
82
+ declare function booleanConfidence(p: number): number;
83
+ export { Answer, Answers, ClassifierInputError, ClassifierName, ClassifyResult, Question, RuleValue, RulesFn, booleanConfidence, choiceConfidence, classify, resolveProvider, scoreConfidence };