@sorbetai/runtime 0.1.0-alpha.16 → 0.1.0-alpha.17

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.
package/dist/node.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as UnitReader, At as JournalWrite, Ct as JournalPage, Et as JournalStore, G as UnitFact, O as RuntimeObservationPort, Ot as JournalSubscription, Tt as JournalReadOptions, V as OpenedUnitStore, X as UnitOpenOptions, Y as UnitInspectionPage, at as JournalRecoveryFact, bt as JournalFact, ht as JournalAppendOptions, kt as JournalSubscriptionOptions, tt as UnitStore } from "./contracts-CFmBr0LI.js";
1
+ import { $ as UnitReader, At as JournalWrite, Ct as JournalPage, Et as JournalStore, G as UnitFact, O as RuntimeObservationPort, Ot as JournalSubscription, Tt as JournalReadOptions, V as OpenedUnitStore, X as UnitOpenOptions, Y as UnitInspectionPage, at as JournalRecoveryFact, bt as JournalFact, ht as JournalAppendOptions, kt as JournalSubscriptionOptions, tt as UnitStore } from "./contracts-D0cczmsb.js";
2
2
  import * as s from "@earendil-works/pi-schema";
3
3
  import "@sorbetai/schema";
4
4
  import "@sorbetai/context";
@@ -139,9 +139,15 @@ declare class JsonlUnitStore implements UnitStore {
139
139
  readonly signal?: AbortSignal;
140
140
  readonly unitKinds?: readonly UnitFact['kind'][];
141
141
  }): AsyncGenerator<UnitInspectionPage>;
142
- /** Complete lifecycle scan without diagnostic budgets or historical content hydration. */
142
+ /**
143
+ * Complete lifecycle scan without diagnostic budgets or historical content hydration.
144
+ * Omit `sessionId` to fold every Session in the Unit in one pass. `fromBytes` resumes from a
145
+ * prior scan's committed `endOffset`; the prefix is trusted (append-only) so its global
146
+ * sequence check is skipped, while per-Session sequence still flows through the projection.
147
+ */
143
148
  readRecovery(options: {
144
- readonly sessionId: string;
149
+ readonly sessionId?: string;
150
+ readonly fromBytes?: number;
145
151
  readonly signal?: AbortSignal;
146
152
  }): AsyncGenerator<UnitInspectionPage>;
147
153
  open(options?: UnitOpenOptions): Promise<OpenedUnitStore>;
package/dist/node.js CHANGED
@@ -1127,9 +1127,9 @@ async function decodeUnitInspectionFrame(input, artifacts, transactions, session
1127
1127
  }
1128
1128
  const fact = envelope === void 0 ? raw : await decodeJournalEnvelope(envelope, artifacts);
1129
1129
  unitFacts.push(UnitFactValidator.check(fact) ? fact : decodeWith(UnitFactValidator, fact));
1130
- } else if (sessionId !== void 0) {
1130
+ } else if (sessionId !== void 0 || diagnostic === false) {
1131
1131
  if (!SessionHeaderValidator.check(raw)) throw new UnitStoreError("unit_corrupt", "Unit fact has no Session identity");
1132
- if (raw.sessionId !== sessionId) continue;
1132
+ if (sessionId !== void 0 && raw.sessionId !== sessionId) continue;
1133
1133
  const fact = decodeJournalRecoveryFact(raw);
1134
1134
  sessionFacts.push(fact);
1135
1135
  }
@@ -1239,13 +1239,17 @@ function failFrameDecode(cause) {
1239
1239
  * and 64 KiB chunks left the open path mostly waiting on them.
1240
1240
  */
1241
1241
  const SCAN_CHUNK_BYTES = 4 * 1024 * 1024;
1242
- /** Pins the observed file length. The final unterminated bytes are evidence, never a commit. */
1243
- async function* scanUnitFrames(file, signal) {
1242
+ /**
1243
+ * Pins the observed file length. The final unterminated bytes are evidence, never a commit.
1244
+ * `from` starts the scan at a committed frame boundary (a prior scan's `completeBytes`), so a
1245
+ * caller that already folded the prefix only parses the appended tail.
1246
+ */
1247
+ async function* scanUnitFrames(file, signal, from = 0) {
1244
1248
  const { size } = await file.stat();
1245
1249
  let pending = [];
1246
1250
  let pendingLength = 0;
1247
- let position = 0;
1248
- let offset = 0;
1251
+ let position = from;
1252
+ let offset = from;
1249
1253
  while (position < size) {
1250
1254
  signal?.throwIfAborted();
1251
1255
  const buffer = Buffer.allocUnsafe(Math.min(SCAN_CHUNK_BYTES, size - position));
@@ -1773,7 +1777,12 @@ var JsonlUnitStore = class {
1773
1777
  inspect(options = {}) {
1774
1778
  return this.#readLifecycle(options, true);
1775
1779
  }
1776
- /** Complete lifecycle scan without diagnostic budgets or historical content hydration. */
1780
+ /**
1781
+ * Complete lifecycle scan without diagnostic budgets or historical content hydration.
1782
+ * Omit `sessionId` to fold every Session in the Unit in one pass. `fromBytes` resumes from a
1783
+ * prior scan's committed `endOffset`; the prefix is trusted (append-only) so its global
1784
+ * sequence check is skipped, while per-Session sequence still flows through the projection.
1785
+ */
1777
1786
  readRecovery(options) {
1778
1787
  return this.#readLifecycle(options, false);
1779
1788
  }
@@ -1783,37 +1792,54 @@ var JsonlUnitStore = class {
1783
1792
  const path = await canonicalJournalPath(this.#options.filePath);
1784
1793
  const { source: file } = await openUnitScanSource(path, "r");
1785
1794
  const transactions = new FileUnitTransactions(path);
1795
+ const fromBytes = diagnostic ? void 0 : options.fromBytes;
1796
+ const tailScan = fromBytes !== void 0 && fromBytes > 0;
1786
1797
  let sequence = 1;
1787
1798
  let sessionSequence = 1;
1799
+ const sessionSequences = /* @__PURE__ */ new Map();
1788
1800
  let frames = 0;
1789
1801
  let skipped = false;
1802
+ let adoptSequence = tailScan;
1803
+ let endOffset = fromBytes ?? 0;
1790
1804
  try {
1791
- for await (const raw of scanUnitFrames(file, options.signal)) {
1805
+ for await (const raw of scanUnitFrames(file, options.signal, fromBytes ?? 0)) {
1792
1806
  if (!raw.complete) {
1793
1807
  yield {
1794
1808
  unitFacts: [],
1795
1809
  sessionFacts: [],
1796
- incompleteTail: true
1810
+ incompleteTail: true,
1811
+ endOffset
1797
1812
  };
1798
1813
  return;
1799
1814
  }
1815
+ endOffset = raw.offset + raw.bytes.length + 1;
1800
1816
  frames++;
1801
1817
  if (needle !== void 0 && !raw.bytes.subarray(0, 64).includes(ReferenceFrameMarker) && !raw.bytes.includes(needle)) {
1802
1818
  skipped = true;
1803
1819
  continue;
1804
1820
  }
1805
1821
  const frame = await decodeUnitInspectionFrame(raw.bytes, this.#options.artifactStore, transactions, options.sessionId, diagnostic, unitKinds === void 0 ? void 0 : new Set(unitKinds));
1806
- if (frame.unitId !== this.#options.unitId || (skipped ? frame.firstSequence < sequence : frame.firstSequence !== sequence)) throw new UnitStoreError("unit_corrupt", "Unit inspection identity or ordering differs");
1822
+ if (frame.unitId !== this.#options.unitId) throw new UnitStoreError("unit_corrupt", "Unit inspection identity or ordering differs");
1823
+ if (adoptSequence) {
1824
+ sequence = frame.firstSequence;
1825
+ adoptSequence = false;
1826
+ } else if (skipped ? frame.firstSequence < sequence : frame.firstSequence !== sequence) throw new UnitStoreError("unit_corrupt", "Unit inspection identity or ordering differs");
1807
1827
  skipped = false;
1808
1828
  sequence = frame.firstSequence + frame.count;
1809
- for (const fact of frame.sessionFacts) if (fact.sequence !== sessionSequence++ || fact.cursor !== fact.id) throw new UnitStoreError("unit_corrupt", "Session inspection ordering differs");
1829
+ if (!tailScan) for (const fact of frame.sessionFacts) {
1830
+ const next = options.sessionId === void 0 ? sessionSequences.get(fact.sessionId) ?? 1 : sessionSequence;
1831
+ if (fact.sequence !== next || fact.cursor !== fact.id) throw new UnitStoreError("unit_corrupt", "Session inspection ordering differs");
1832
+ if (options.sessionId === void 0) sessionSequences.set(fact.sessionId, next + 1);
1833
+ else sessionSequence = next + 1;
1834
+ }
1810
1835
  yield {
1811
1836
  unitFacts: frame.unitFacts,
1812
1837
  sessionFacts: frame.sessionFacts,
1813
- incompleteTail: false
1838
+ incompleteTail: false,
1839
+ endOffset
1814
1840
  };
1815
1841
  }
1816
- if (frames === 0) throw new UnitStoreError("unit_corrupt", "Published unit has no committed catalog");
1842
+ if (frames === 0 && fromBytes === void 0) throw new UnitStoreError("unit_corrupt", "Published unit has no committed catalog");
1817
1843
  } finally {
1818
1844
  await file.close();
1819
1845
  }