@secondlayer/shared 18.0.2 → 19.0.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 (43) hide show
  1. package/dist/src/archive/range-digest.d.ts +10 -0
  2. package/dist/src/archive/semantic-digest-builder.d.ts +10 -0
  3. package/dist/src/coverage/index.d.ts +11 -341
  4. package/dist/src/coverage/index.js +2 -751
  5. package/dist/src/coverage/index.js.map +4 -13
  6. package/dist/src/db/index.d.ts +12 -1
  7. package/dist/src/db/index.js +2 -1
  8. package/dist/src/db/index.js.map +4 -4
  9. package/dist/src/db/queries/api-keys.d.ts +38 -0
  10. package/dist/src/db/queries/api-keys.js +89 -0
  11. package/dist/src/db/queries/api-keys.js.map +11 -0
  12. package/dist/src/db/queries/chain-reorgs.d.ts +10 -0
  13. package/dist/src/db/queries/chain-reorgs.js +2 -1
  14. package/dist/src/db/queries/chain-reorgs.js.map +4 -4
  15. package/dist/src/db/queries/contracts.d.ts +10 -0
  16. package/dist/src/db/queries/contracts.js.map +1 -1
  17. package/dist/src/db/queries/instance.d.ts +10 -0
  18. package/dist/src/db/queries/integrity.d.ts +10 -0
  19. package/dist/src/db/queries/subgraph-gaps.d.ts +10 -0
  20. package/dist/src/db/queries/subgraph-operations.d.ts +10 -0
  21. package/dist/src/db/queries/subgraphs.d.ts +10 -0
  22. package/dist/src/db/queries/sync-scope.d.ts +10 -0
  23. package/dist/src/db/queries/webhook-replays.d.ts +10 -0
  24. package/dist/src/db/queries/webhooks.d.ts +10 -0
  25. package/dist/src/db/schema.d.ts +11 -1
  26. package/dist/src/db/schema.js.map +1 -1
  27. package/dist/src/index.d.ts +12 -1
  28. package/dist/src/index.js +2 -1
  29. package/dist/src/index.js.map +4 -4
  30. package/dist/src/insights/index.d.ts +10 -0
  31. package/dist/src/leader.js +2 -1
  32. package/dist/src/leader.js.map +4 -4
  33. package/dist/src/node/local-client.d.ts +10 -0
  34. package/dist/src/queue/listener.js +2 -1
  35. package/dist/src/queue/listener.js.map +4 -4
  36. package/dist/src/runtime/index.d.ts +1 -120
  37. package/dist/src/runtime/index.js +1 -310
  38. package/dist/src/runtime/index.js.map +3 -7
  39. package/dist/src/streams-rows/index.d.ts +0 -12
  40. package/dist/src/streams-rows/index.js +2 -3
  41. package/dist/src/streams-rows/index.js.map +8 -8
  42. package/migrations/0217_usage_monthly_totals.ts +41 -0
  43. package/package.json +6 -2
@@ -67,15 +67,6 @@ function isNonNegativeInt(value) {
67
67
  function rangeIsOrdered(from, to) {
68
68
  return isNonNegativeInt(from) && isNonNegativeInt(to) && from <= to;
69
69
  }
70
- function receiptRetentionHolds(row) {
71
- if (!isNonNegativeInt(row.block_height))
72
- return false;
73
- if (!isNonNegativeInt(row.input_count))
74
- return false;
75
- if (row.compacted_at !== null && !row.finalized)
76
- return false;
77
- return true;
78
- }
79
70
  function failureRangeHolds(row) {
80
71
  if (row.from_height !== null && !isNonNegativeInt(row.from_height)) {
81
72
  return false;
@@ -97,9 +88,6 @@ function failureRetentionHolds(row) {
97
88
  return row.resolved_at !== null;
98
89
  return true;
99
90
  }
100
- function segmentsOverlap(a, b) {
101
- return a.from_height <= b.to_height && b.from_height <= a.to_height;
102
- }
103
91
  function defaultRetainUntil(createdAt) {
104
92
  return new Date(createdAt.getTime() + FAILURE_RETENTION_DAYS * 24 * 60 * 60 * 1000);
105
93
  }
@@ -543,392 +531,6 @@ function evaluateCoverage(input) {
543
531
  cycles
544
532
  };
545
533
  }
546
- // src/coverage/runner.ts
547
- var RUNNER_STATUSES = [
548
- "pending",
549
- "running",
550
- "complete",
551
- "failed",
552
- "halted"
553
- ];
554
- var RUNNER_EVENT_TYPES = [
555
- "start",
556
- "ack",
557
- "fail",
558
- "retry",
559
- "halt",
560
- "resume",
561
- "reorg",
562
- "version",
563
- "finish"
564
- ];
565
- var DEFAULT_MAX_RETRIES = 3;
566
- var RUNNER_TRANSITION_TABLE = [
567
- { from: "pending", event: "start", to: "running" },
568
- { from: "pending", event: "ack", to: "reject" },
569
- { from: "pending", event: "fail", to: "reject" },
570
- { from: "pending", event: "retry", to: "reject" },
571
- { from: "pending", event: "halt", to: "reject" },
572
- { from: "pending", event: "resume", to: "reject" },
573
- { from: "pending", event: "reorg", to: "reject" },
574
- { from: "pending", event: "version", to: "pending" },
575
- { from: "pending", event: "finish", to: "reject" },
576
- { from: "running", event: "start", to: "reject" },
577
- { from: "running", event: "ack", to: "running" },
578
- { from: "running", event: "fail", to: "failed" },
579
- { from: "running", event: "retry", to: "reject" },
580
- { from: "running", event: "halt", to: "halted" },
581
- { from: "running", event: "resume", to: "running" },
582
- { from: "running", event: "reorg", to: "running" },
583
- { from: "running", event: "version", to: "halted" },
584
- { from: "running", event: "finish", to: "complete" },
585
- { from: "complete", event: "start", to: "reject" },
586
- { from: "complete", event: "ack", to: "reject" },
587
- { from: "complete", event: "fail", to: "reject" },
588
- { from: "complete", event: "retry", to: "reject" },
589
- { from: "complete", event: "halt", to: "reject" },
590
- { from: "complete", event: "resume", to: "reject" },
591
- { from: "complete", event: "reorg", to: "running" },
592
- { from: "complete", event: "version", to: "complete" },
593
- { from: "complete", event: "finish", to: "complete" },
594
- { from: "failed", event: "start", to: "reject" },
595
- { from: "failed", event: "ack", to: "reject" },
596
- { from: "failed", event: "fail", to: "reject" },
597
- { from: "failed", event: "retry", to: "running" },
598
- { from: "failed", event: "halt", to: "halted" },
599
- { from: "failed", event: "resume", to: "running" },
600
- { from: "failed", event: "reorg", to: "failed" },
601
- { from: "failed", event: "version", to: "halted" },
602
- { from: "failed", event: "finish", to: "reject" },
603
- { from: "halted", event: "start", to: "reject" },
604
- { from: "halted", event: "ack", to: "reject" },
605
- { from: "halted", event: "fail", to: "reject" },
606
- { from: "halted", event: "retry", to: "running" },
607
- { from: "halted", event: "halt", to: "halted" },
608
- { from: "halted", event: "resume", to: "running" },
609
- { from: "halted", event: "reorg", to: "halted" },
610
- { from: "halted", event: "version", to: "halted" },
611
- { from: "halted", event: "finish", to: "reject" }
612
- ];
613
- var RULE_INDEX = new Map(RUNNER_TRANSITION_TABLE.map((rule) => [`${rule.from}:${rule.event}`, rule]));
614
- function lookupTransition(from, event) {
615
- const rule = RULE_INDEX.get(`${from}:${event}`);
616
- if (!rule) {
617
- throw new Error(`missing transition ${from} + ${event}`);
618
- }
619
- return rule;
620
- }
621
- function versionsEqual(a, b) {
622
- return a.code_hash === b.code_hash && a.config_hash === b.config_hash && a.handler_hash === b.handler_hash;
623
- }
624
- function expectedAckHeight(state) {
625
- return state.complete_through === null ? state.start_height : state.complete_through + 1;
626
- }
627
- function createRunnerState(init) {
628
- return {
629
- stage_id: init.stage_id,
630
- status: "pending",
631
- version: init.version,
632
- start_height: init.start_height,
633
- target_height: init.target_height ?? null,
634
- complete_through: null,
635
- last_acked_hash: null,
636
- retry_count: 0,
637
- open_failure: null
638
- };
639
- }
640
- function ok(state, effects = []) {
641
- return { ok: true, state, effects };
642
- }
643
- function reject(state, reason) {
644
- return { ok: false, reason, state };
645
- }
646
- function clone(state, patch) {
647
- return { ...state, ...patch };
648
- }
649
- function rewind(state, forkPoint) {
650
- const from = Math.max(forkPoint, state.start_height);
651
- const through = state.complete_through;
652
- if (through === null || through < from) {
653
- return { next: state, effects: [] };
654
- }
655
- const completeThrough = from <= state.start_height ? null : from - 1;
656
- return {
657
- next: clone(state, {
658
- complete_through: completeThrough,
659
- last_acked_hash: null
660
- }),
661
- effects: [
662
- {
663
- type: "invalidate_receipts",
664
- range: { from_height: from, to_height: through }
665
- }
666
- ]
667
- };
668
- }
669
- function applyRetry(state, maxRetries) {
670
- if (state.retry_count >= maxRetries) {
671
- const failure = state.open_failure ? { ...state.open_failure, retry_state: "halted" } : null;
672
- return ok(clone(state, { status: "halted", open_failure: failure }));
673
- }
674
- const failure = state.open_failure ? { ...state.open_failure, retry_state: "retrying" } : null;
675
- return ok(clone(state, {
676
- status: "running",
677
- retry_count: state.retry_count + 1,
678
- open_failure: failure
679
- }));
680
- }
681
- function applyRunnerEvent(state, event, opts) {
682
- const maxRetries = opts?.maxRetries ?? DEFAULT_MAX_RETRIES;
683
- const rule = lookupTransition(state.status, event.type);
684
- if (rule.to === "reject") {
685
- return reject(state, `${state.status} cannot ${event.type}`);
686
- }
687
- const nextStatus = rule.to;
688
- switch (event.type) {
689
- case "start":
690
- return ok(clone(state, { status: "running" }));
691
- case "ack": {
692
- if (event.height === state.complete_through) {
693
- if (event.hash === state.last_acked_hash)
694
- return ok(state);
695
- return reject(state, `ack at ${event.height} has a different hash; reorg first`);
696
- }
697
- const expected = expectedAckHeight(state);
698
- if (event.height !== expected) {
699
- return reject(state, `ack at ${event.height} skips ${expected}; acknowledgements must be ordered`);
700
- }
701
- const effects = [];
702
- const failure = state.open_failure;
703
- const coversFailure = failure && failure.from_height !== null && event.height >= failure.from_height && (failure.to_height === null || event.height <= failure.to_height);
704
- if (coversFailure)
705
- effects.push({ type: "resolve_failure" });
706
- return ok(clone(state, {
707
- status: "running",
708
- complete_through: event.height,
709
- last_acked_hash: event.hash,
710
- open_failure: coversFailure ? null : failure
711
- }), effects);
712
- }
713
- case "fail": {
714
- const failure = {
715
- unit_kind: event.unit_kind,
716
- class: event.class,
717
- retry_state: "open",
718
- from_height: event.from_height,
719
- to_height: event.to_height,
720
- error: event.error
721
- };
722
- return ok(clone(state, { status: "failed", open_failure: failure }), [
723
- { type: "open_failure", failure }
724
- ]);
725
- }
726
- case "retry":
727
- return applyRetry(state, maxRetries);
728
- case "halt": {
729
- const failure = state.open_failure ? { ...state.open_failure, retry_state: "halted" } : state.open_failure;
730
- return ok(clone(state, { status: "halted", open_failure: failure }));
731
- }
732
- case "resume":
733
- if (state.status === "running")
734
- return ok(state);
735
- if (state.open_failure)
736
- return applyRetry(state, maxRetries);
737
- return ok(clone(state, { status: "running" }));
738
- case "reorg": {
739
- if (event.fork_point < state.start_height) {
740
- return reject(state, `reorg fork ${event.fork_point} is below start ${state.start_height}`);
741
- }
742
- const { next, effects } = rewind(state, event.fork_point);
743
- if (effects.length === 0)
744
- return ok(state);
745
- return ok(clone(next, { status: nextStatus }), effects);
746
- }
747
- case "version": {
748
- if (versionsEqual(state.version, event.version))
749
- return ok(state);
750
- if (state.status === "pending") {
751
- return ok(clone(state, { version: event.version }));
752
- }
753
- const sealed = state.status === "complete" ? "complete" : "halted";
754
- return ok(clone(state, { status: sealed }), [
755
- { type: "spawn_run", version: event.version }
756
- ]);
757
- }
758
- case "finish": {
759
- if (state.status === "complete")
760
- return ok(state);
761
- if (state.target_height === null) {
762
- return reject(state, "cannot finish a follow-the-chain run");
763
- }
764
- if (state.complete_through === null || state.complete_through < state.target_height) {
765
- return reject(state, `cannot finish: complete_through ${state.complete_through} is short of target ${state.target_height}`);
766
- }
767
- return ok(clone(state, { status: "complete" }));
768
- }
769
- }
770
- }
771
- // src/coverage/decoder-clock.ts
772
- import { createHash } from "node:crypto";
773
- function inputDigest(cursors) {
774
- if (cursors.length === 0) {
775
- return createHash("sha256").update(`no-match
776
- `).digest("hex");
777
- }
778
- const hash = createHash("sha256");
779
- for (const cursor of cursors)
780
- hash.update(`${cursor}
781
- `);
782
- return hash.digest("hex");
783
- }
784
- function cursorIsAfterBlock(cursor, height) {
785
- return compareStreamsCursor(cursor, blockEndCursor(height)) >= 0;
786
- }
787
- function cursorIsBeforeBlock(cursor, height) {
788
- return cursor.block_height < height;
789
- }
790
- function parseCursor(value, label) {
791
- try {
792
- return { ok: true, cursor: decodeStreamsCursor(value) };
793
- } catch {
794
- return { ok: false, reason: `${label} is not a valid streams cursor` };
795
- }
796
- }
797
- function planDecoderReceipts(input) {
798
- const to = parseCursor(input.to_cursor, "to_cursor");
799
- if (!to.ok)
800
- return { ok: false, reason: to.reason, receipts: [] };
801
- let from = null;
802
- if (input.from_cursor !== null) {
803
- const parsed = parseCursor(input.from_cursor, "from_cursor");
804
- if (!parsed.ok)
805
- return { ok: false, reason: parsed.reason, receipts: [] };
806
- from = parsed.cursor;
807
- if (compareStreamsCursor(from, to.cursor) >= 0) {
808
- return {
809
- ok: false,
810
- reason: "to_cursor must be after from_cursor",
811
- receipts: []
812
- };
813
- }
814
- }
815
- const startFull = from === null ? input.start_height : from.block_height + 1;
816
- const lastFull = isEmptyRangeCursor(to.cursor) ? to.cursor.block_height : to.cursor.block_height - 1;
817
- if (lastFull < startFull) {
818
- return { ok: true, receipts: [], through_cursor: input.to_cursor };
819
- }
820
- if (startFull < input.start_height) {
821
- return {
822
- ok: false,
823
- reason: `window starts at ${startFull}, below start_height ${input.start_height}`,
824
- receipts: []
825
- };
826
- }
827
- const byHeight = new Map;
828
- let prevHeight = null;
829
- for (const block of input.blocks) {
830
- if (prevHeight !== null && block.height !== prevHeight + 1) {
831
- return {
832
- ok: false,
833
- reason: `blocks are not contiguous (${prevHeight} → ${block.height})`,
834
- receipts: []
835
- };
836
- }
837
- if (byHeight.has(block.height)) {
838
- return {
839
- ok: false,
840
- reason: `duplicate canonical height ${block.height}`,
841
- receipts: []
842
- };
843
- }
844
- byHeight.set(block.height, block);
845
- prevHeight = block.height;
846
- }
847
- for (let height = startFull;height <= lastFull; height++) {
848
- if (!byHeight.has(height)) {
849
- return {
850
- ok: false,
851
- reason: `missing canonical block ${height}`,
852
- receipts: []
853
- };
854
- }
855
- }
856
- const decoded = [];
857
- let previous = from;
858
- for (const event of input.events) {
859
- const parsed = parseCursor(event.cursor, "event.cursor");
860
- if (!parsed.ok)
861
- return { ok: false, reason: parsed.reason, receipts: [] };
862
- if (previous && compareStreamsCursor(parsed.cursor, previous) <= 0) {
863
- return {
864
- ok: false,
865
- reason: `event cursor ${event.cursor} is not after ${encodeStreamsCursor(previous)}`,
866
- receipts: []
867
- };
868
- }
869
- if (compareStreamsCursor(parsed.cursor, to.cursor) >= 0) {
870
- return {
871
- ok: false,
872
- reason: `event cursor ${event.cursor} is not before to_cursor`,
873
- receipts: []
874
- };
875
- }
876
- if (from && compareStreamsCursor(parsed.cursor, from) <= 0) {
877
- return {
878
- ok: false,
879
- reason: `event cursor ${event.cursor} is not after from_cursor`,
880
- receipts: []
881
- };
882
- }
883
- const block = byHeight.get(parsed.cursor.block_height);
884
- if (!block) {
885
- return {
886
- ok: false,
887
- reason: `event ${event.cursor} has no canonical block`,
888
- receipts: []
889
- };
890
- }
891
- if (block.hash !== event.block_hash) {
892
- return {
893
- ok: false,
894
- reason: `event ${event.cursor} hash ${event.block_hash} does not match canonical ${block.hash}`,
895
- receipts: []
896
- };
897
- }
898
- decoded.push({
899
- cursor: parsed.cursor,
900
- encoded: event.cursor,
901
- block_hash: event.block_hash,
902
- matched: event.matched
903
- });
904
- previous = parsed.cursor;
905
- }
906
- const receipts = [];
907
- for (let height = startFull;height <= lastFull; height++) {
908
- const block = byHeight.get(height);
909
- if (!block) {
910
- return {
911
- ok: false,
912
- reason: `missing canonical block ${height}`,
913
- receipts
914
- };
915
- }
916
- const inBlock = decoded.filter((e) => e.cursor.block_height === height);
917
- const matched = inBlock.filter((e) => e.matched);
918
- const last = inBlock[inBlock.length - 1];
919
- const through = last ? last.encoded : encodeStreamsCursor(blockEndCursor(height));
920
- receipts.push({
921
- height,
922
- hash: block.hash,
923
- input_count: matched.length,
924
- input_cursors: matched.map((e) => e.encoded),
925
- input_digest: inputDigest(matched.map((e) => e.encoded)),
926
- through_cursor: through,
927
- no_match: matched.length === 0
928
- });
929
- }
930
- return { ok: true, receipts, through_cursor: input.to_cursor };
931
- }
932
534
  // src/coverage/adapter.ts
933
535
  import { sql } from "kysely";
934
536
  var DECODER_COMMIT_STEPS = [
@@ -1051,319 +653,6 @@ async function commitDecoderAdapterBatch(db, inputs) {
1051
653
  }
1052
654
  });
1053
655
  }
1054
- // src/coverage/sealer.ts
1055
- import { createHash as createHash2 } from "node:crypto";
1056
- function rangeDigestOf(parts) {
1057
- const hash = createHash2("sha256");
1058
- for (const part of parts)
1059
- hash.update(`${part}
1060
- `);
1061
- return hash.digest("hex");
1062
- }
1063
- function sealFinalizedRange(receipts, range) {
1064
- const inRange = receipts.filter((r) => r.height >= range.from_height && r.height <= range.to_height).sort((a, b) => a.height - b.height);
1065
- if (inRange.length === 0) {
1066
- return { ok: false, reason: "no receipts in range" };
1067
- }
1068
- let expected = range.from_height;
1069
- for (const receipt of inRange) {
1070
- if (!receipt.finalized) {
1071
- return { ok: false, reason: `unfinalized receipt at ${receipt.height}` };
1072
- }
1073
- if (receipt.height !== expected) {
1074
- return {
1075
- ok: false,
1076
- reason: `gap at ${expected} before ${receipt.height}`
1077
- };
1078
- }
1079
- expected += 1;
1080
- }
1081
- if (inRange[inRange.length - 1]?.height !== range.to_height) {
1082
- return { ok: false, reason: "range is not fully covered" };
1083
- }
1084
- return {
1085
- ok: true,
1086
- compacted: inRange.length,
1087
- segment: {
1088
- from_height: range.from_height,
1089
- to_height: range.to_height,
1090
- chain_digest: rangeDigestOf(inRange.map((r) => `${r.height}:${r.hash}`)),
1091
- input_digest: rangeDigestOf(inRange.map((r) => r.input_digest)),
1092
- output_digest: rangeDigestOf(inRange.map((r) => r.effect_digest))
1093
- }
1094
- };
1095
- }
1096
- function segmentsSurviveReorg(segments, fork) {
1097
- return segments.filter((s) => s.to_height < fork);
1098
- }
1099
- function receiptsAfterReorg(receipts, fork) {
1100
- return receipts.filter((r) => r.height < fork);
1101
- }
1102
- // src/coverage/effect-manifest.ts
1103
- import { createHash as createHash3 } from "node:crypto";
1104
- function canonicalizeMutations(ops) {
1105
- return ops.map((op) => ({
1106
- op: op.op,
1107
- table: op.table,
1108
- key: sortRecord(op.key),
1109
- value: op.value ? sortRecord(op.value) : undefined
1110
- }));
1111
- }
1112
- function sortRecord(record) {
1113
- const out = {};
1114
- for (const key of Object.keys(record).sort())
1115
- out[key] = record[key] ?? null;
1116
- return out;
1117
- }
1118
- function hashEffectManifest(ops) {
1119
- const canonical = canonicalizeMutations(ops);
1120
- const hash = createHash3("sha256");
1121
- hash.update(JSON.stringify(canonical));
1122
- return hash.digest("hex");
1123
- }
1124
- function manifestsEqual(a, b) {
1125
- return hashEffectManifest(a) === hashEffectManifest(b);
1126
- }
1127
- // src/coverage/subgraph-adapter.ts
1128
- var SUBGRAPH_COMMIT_STEPS = [
1129
- "effects",
1130
- "rows",
1131
- "journal",
1132
- "cursor",
1133
- "receipt",
1134
- "failure"
1135
- ];
1136
-
1137
- class SubgraphAdapterCrash extends Error {
1138
- step;
1139
- name = "SubgraphAdapterCrash";
1140
- constructor(step) {
1141
- super(`subgraph adapter crash after ${step}`);
1142
- this.step = step;
1143
- }
1144
- }
1145
- async function runSubgraphCommitSteps(steps, crashAfter) {
1146
- for (const step of SUBGRAPH_COMMIT_STEPS) {
1147
- await steps[step]();
1148
- if (crashAfter === step)
1149
- throw new SubgraphAdapterCrash(step);
1150
- }
1151
- }
1152
- async function commitSubgraphAdapter(input, wrap = (fn) => fn()) {
1153
- await wrap(() => runSubgraphCommitSteps({
1154
- effects: input.writeEffects,
1155
- rows: input.writeRows,
1156
- journal: input.writeJournal,
1157
- cursor: input.writeCursor,
1158
- receipt: input.writeReceipts,
1159
- failure: input.writeFailure
1160
- }, input.crashAfter));
1161
- }
1162
- // src/coverage/sparse.ts
1163
- import { createHash as createHash4 } from "node:crypto";
1164
- function skipReceipt(skip, chainHash) {
1165
- const empty = hashEffectManifest([]);
1166
- return {
1167
- height: skip.from_height,
1168
- hash: chainHash,
1169
- effect_digest: empty,
1170
- row_digest: empty
1171
- };
1172
- }
1173
- function sequentialDigests(heights) {
1174
- const ordered = [...heights].sort((a, b) => a.height - b.height);
1175
- return {
1176
- chain: rangeDigestOf(ordered.map((h) => `${h.height}:${h.hash}`)),
1177
- effect: rangeDigestOf(ordered.map((h) => h.effect_digest)),
1178
- row: rangeDigestOf(ordered.map((h) => h.row_digest))
1179
- };
1180
- }
1181
- function sparseDigests(executed, skips) {
1182
- const skipped = skips.flatMap(({ skip, hash }) => {
1183
- const out = [];
1184
- for (let h = skip.from_height;h <= skip.to_height; h++) {
1185
- out.push(skipReceipt({ ...skip, from_height: h, to_height: h }, hash));
1186
- }
1187
- return out;
1188
- });
1189
- return sequentialDigests([...executed, ...skipped]);
1190
- }
1191
- function proofsAgree(sequential, sparse) {
1192
- return sequential.chain === sparse.chain && sequential.effect === sparse.effect && sequential.row === sparse.row;
1193
- }
1194
- // src/coverage/deep-verify.ts
1195
- import { createHash as createHash5 } from "node:crypto";
1196
- function finalRowDigest(rows) {
1197
- const ordered = [...rows].sort((a, b) => a.table.localeCompare(b.table) || a.key.localeCompare(b.key));
1198
- const hash = createHash5("sha256");
1199
- for (const row of ordered) {
1200
- hash.update(`${row.table} ${row.key} ${row.value}
1201
- `);
1202
- }
1203
- return hash.digest("hex");
1204
- }
1205
- function applyMutations(rows, ops) {
1206
- const map = new Map(rows.map((r) => [`${r.table}:${r.key}`, { ...r }]));
1207
- for (const op of ops) {
1208
- const id = String(op.key.id ?? JSON.stringify(op.key));
1209
- const k = `${op.table}:${id}`;
1210
- if (op.op === "delete") {
1211
- map.delete(k);
1212
- continue;
1213
- }
1214
- map.set(k, {
1215
- table: op.table,
1216
- key: id,
1217
- value: JSON.stringify(op.value ?? {})
1218
- });
1219
- }
1220
- return [...map.values()];
1221
- }
1222
- function deepVerify(opts) {
1223
- const scratch = applyMutations([], opts.replay);
1224
- const liveDigest = finalRowDigest(opts.live);
1225
- const scratchDigest = finalRowDigest(scratch);
1226
- let found = false;
1227
- if (opts.seed) {
1228
- const tainted = applyMutations(opts.live, [opts.seed]);
1229
- found = finalRowDigest(tainted) !== liveDigest;
1230
- }
1231
- return {
1232
- ok: liveDigest === scratchDigest,
1233
- live_digest: liveDigest,
1234
- scratch_digest: scratchDigest,
1235
- found_mutation: found
1236
- };
1237
- }
1238
- // src/coverage/queue.ts
1239
- var QUEUE_STAGES = [
1240
- "decision",
1241
- "outbox",
1242
- "delivery",
1243
- "rollback",
1244
- "dlq",
1245
- "replay"
1246
- ];
1247
- function emptyQueueState() {
1248
- return {
1249
- stage: "decision",
1250
- accepted: 0,
1251
- decided: 0,
1252
- enqueued: 0,
1253
- delivered: 0,
1254
- dead: 0,
1255
- fence_cursor: null,
1256
- last_cursor: null,
1257
- dedupe: new Set
1258
- };
1259
- }
1260
- function applyQueueEvent(state, event, dedupeKey) {
1261
- if (dedupeKey && state.dedupe.has(dedupeKey)) {
1262
- return { ok: true, state };
1263
- }
1264
- const next = {
1265
- ...state,
1266
- dedupe: new Set(state.dedupe)
1267
- };
1268
- if (dedupeKey)
1269
- next.dedupe.add(dedupeKey);
1270
- switch (event.type) {
1271
- case "accept":
1272
- next.accepted += 1;
1273
- next.stage = "decision";
1274
- return { ok: true, state: next };
1275
- case "decide":
1276
- if (next.decided >= next.accepted) {
1277
- return { ok: false, reason: "decide without accept", state };
1278
- }
1279
- next.decided += 1;
1280
- next.stage = event.matched ? "outbox" : "decision";
1281
- return { ok: true, state: next };
1282
- case "enqueue":
1283
- if (next.enqueued >= next.decided) {
1284
- return { ok: false, reason: "enqueue without decide", state };
1285
- }
1286
- next.enqueued += 1;
1287
- next.stage = "outbox";
1288
- return { ok: true, state: next };
1289
- case "deliver":
1290
- if (next.delivered + next.dead >= next.enqueued) {
1291
- return { ok: false, reason: "deliver without enqueue", state };
1292
- }
1293
- next.delivered += 1;
1294
- next.stage = "delivery";
1295
- return { ok: true, state: next };
1296
- case "fail":
1297
- next.stage = "delivery";
1298
- return { ok: true, state: next };
1299
- case "dead":
1300
- if (next.delivered + next.dead >= next.enqueued) {
1301
- return { ok: false, reason: "dead without enqueue", state };
1302
- }
1303
- next.dead += 1;
1304
- next.stage = "dlq";
1305
- return { ok: true, state: next };
1306
- case "requeue":
1307
- if (next.dead < 1)
1308
- return { ok: false, reason: "requeue empty dlq", state };
1309
- next.dead -= 1;
1310
- next.enqueued += 1;
1311
- next.stage = "outbox";
1312
- return { ok: true, state: next };
1313
- case "rollback":
1314
- next.stage = "rollback";
1315
- return { ok: true, state: next };
1316
- case "replay":
1317
- next.stage = "replay";
1318
- next.last_cursor = event.cursor;
1319
- next.fence_cursor = event.cursor;
1320
- return { ok: true, state: next };
1321
- }
1322
- }
1323
- function queueCaughtUp(state) {
1324
- return state.accepted === state.decided && state.delivered + state.dead === state.enqueued;
1325
- }
1326
- // src/coverage/repair.ts
1327
- var SAFE_REPAIR_MODES = [
1328
- "range_safe",
1329
- "full_reindex",
1330
- "archive_replay"
1331
- ];
1332
- function planRepair(input) {
1333
- if (input.range.from_height > input.range.to_height) {
1334
- return {
1335
- ...input,
1336
- safe: false,
1337
- reason: "inverted range"
1338
- };
1339
- }
1340
- if (!SAFE_REPAIR_MODES.includes(input.mode)) {
1341
- return {
1342
- ...input,
1343
- safe: false,
1344
- reason: `mode ${input.mode} is not registered`
1345
- };
1346
- }
1347
- if (input.mode === "range_safe" && !input.range_safe_proven) {
1348
- return {
1349
- ...input,
1350
- safe: false,
1351
- reason: "range_safe requires a proven range"
1352
- };
1353
- }
1354
- if (input.defect === "source_gap" && input.mode !== "archive_replay") {
1355
- return {
1356
- ...input,
1357
- safe: false,
1358
- reason: "raw/source gaps require archive_replay"
1359
- };
1360
- }
1361
- return {
1362
- ...input,
1363
- safe: true,
1364
- reason: `dry-run ${input.mode} for ${input.defect}`
1365
- };
1366
- }
1367
656
  // src/archive/datasets.ts
1368
657
  var ARCHIVE_DATASETS = [
1369
658
  "blocks",
@@ -1475,48 +764,26 @@ export {
1475
764
  COVERAGE_REPORT_SCHEMA_VERSION,
1476
765
  COVERAGE_STATES,
1477
766
  DECODER_COMMIT_STEPS,
1478
- DEFAULT_MAX_RETRIES,
1479
767
  DecoderAdapterCrash,
1480
768
  FAILURE_CLASSES,
1481
769
  FAILURE_RETENTION_DAYS,
1482
770
  FAILURE_UNITS,
1483
771
  NATIVE_CLOCKS,
1484
- QUEUE_STAGES,
1485
772
  REPAIR_MODES,
1486
773
  RETRY_STATES,
1487
- RUNNER_EVENT_TYPES,
1488
- RUNNER_STATUSES,
1489
- RUNNER_TRANSITION_TABLE,
1490
774
  RUN_STATUSES,
1491
- SAFE_REPAIR_MODES,
1492
775
  STAGE_KINDS,
1493
- SUBGRAPH_COMMIT_STEPS,
1494
- SubgraphAdapterCrash,
1495
776
  VERIFY_EXIT,
1496
- applyMutations,
1497
- applyQueueEvent,
1498
- applyRunnerEvent,
1499
- canonicalizeMutations,
1500
777
  commitDecoderAdapter,
1501
778
  commitDecoderAdapterBatch,
1502
- commitSubgraphAdapter,
1503
779
  contiguousThrough,
1504
- createRunnerState,
1505
780
  cursorHeight,
1506
- cursorIsAfterBlock,
1507
- cursorIsBeforeBlock,
1508
781
  datasetMatchesTarget,
1509
- deepVerify,
1510
782
  defaultRetainUntil,
1511
- emptyQueueState,
1512
783
  evaluateCoverage,
1513
- expectedAckHeight,
1514
784
  failureRangeHolds,
1515
785
  failureRetentionHolds,
1516
- finalRowDigest,
1517
786
  findRangeGaps,
1518
- hashEffectManifest,
1519
- inputDigest,
1520
787
  isFailureClass,
1521
788
  isFailureUnit,
1522
789
  isNativeClock,
@@ -1525,30 +792,14 @@ export {
1525
792
  isRetryState,
1526
793
  isRunStatus,
1527
794
  isStageKind,
1528
- lookupTransition,
1529
- manifestsEqual,
1530
795
  mergeRanges,
1531
796
  parseVerifyTarget,
1532
- planDecoderReceipts,
1533
- planRepair,
1534
- proofsAgree,
1535
- queueCaughtUp,
1536
- rangeDigestOf,
1537
797
  rangeIsOrdered,
1538
- receiptRetentionHolds,
1539
- receiptsAfterReorg,
1540
798
  reportVerify,
1541
799
  rewindDecoderCheckpoint,
1542
800
  runDecoderCommitSteps,
1543
- runSubgraphCommitSteps,
1544
- sealFinalizedRange,
1545
- segmentsOverlap,
1546
- segmentsSurviveReorg,
1547
- sequentialDigests,
1548
- sparseDigests,
1549
- topoSort,
1550
- versionsEqual
801
+ topoSort
1551
802
  };
1552
803
 
1553
- //# debugId=79523CE79087EE7864756E2164756E21
804
+ //# debugId=2F5050ACBA49472D64756E2164756E21
1554
805
  //# sourceMappingURL=index.js.map