@quolu/lattice 0.110.1 → 0.110.2

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/todo-store.mjs +22 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quolu/lattice",
3
- "version": "0.110.1",
3
+ "version": "0.110.2",
4
4
  "description": "Schedulability compiler for multi-agent development: observe real code boundaries, refactor the conflicting seam, recompile the plan for parallel execution",
5
5
  "author": {
6
6
  "name": "Quo / クオ at kitepon.dev",
@@ -235,8 +235,15 @@ function decodeUtf8(bytes, code, reason) {
235
235
  catch { fail(code, reason); }
236
236
  }
237
237
 
238
+ /** w4: artifact bytes this process already accepted at one ref and mode (see `acceptedSegments`); a repeat read only parses. */
239
+ const acceptedArtifacts = new Set();
240
+
238
241
  function parseCanonicalJsonLine(bytes, { code, reason, maxBytes, validate, ref = null, canonical = true }) {
239
242
  const detail = ref === null ? undefined : { ref };
243
+ // `validate` is fixed by what lives at `ref`; without a ref there is nothing to key it by, so nothing is remembered.
244
+ const identity = ref === null ? null : `${ref}:${sha256Bytes(bytes)}`;
245
+ const key = identity === null ? null : `${canonical ? 'strict' : 'read'}:${identity}`;
246
+ const known = key !== null && (acceptedArtifacts.has(key) || (!canonical && acceptedArtifacts.has(`strict:${identity}`)));
240
247
  if (bytes.length === 0 || bytes.length > maxBytes) {
241
248
  fail(code, bytes.length > maxBytes ? 'size_limit_exceeded' : reason, detail);
242
249
  }
@@ -246,6 +253,7 @@ function parseCanonicalJsonLine(bytes, { code, reason, maxBytes, validate, ref =
246
253
  let value;
247
254
  try { value = JSON.parse(body); } catch { fail(code, reason, detail); }
248
255
  if (!text.endsWith('\n')) fail(code, reason, detail);
256
+ if (known) return value;
249
257
  if (canonical) {
250
258
  let expected;
251
259
  try { expected = `${canonicalizeTodoArtifact(value)}\n`; } catch { fail(code, reason, detail); }
@@ -256,6 +264,7 @@ function parseCanonicalJsonLine(bytes, { code, reason, maxBytes, validate, ref =
256
264
  if (newer !== null) failNewer(newer, ref);
257
265
  fail(code, 'schema_invalid', detail);
258
266
  }
267
+ if (key !== null) acceptedArtifacts.add(key);
259
268
  return value;
260
269
  }
261
270
 
@@ -278,6 +287,12 @@ async function readSnapshotArtifact(repoRoot, ref) {
278
287
  });
279
288
  }
280
289
 
290
+ /**
291
+ * w4: segments this process has already accepted, by sha256 of their bytes and the mode. The canonical check and `validateTodoEvent`
292
+ * are pure functions of the bytes, so a repeat read of the same bytes (one command reads the Store about a dozen times) only parses.
293
+ */
294
+ const acceptedSegments = new Set();
295
+
281
296
  function parseJournalSegment(bytes, ref = null, { canonical: checkCanonical = true } = {}) {
282
297
  const detail = ref === null ? undefined : { ref };
283
298
  if (bytes.length === 0 || bytes.length > TODO_LIMITS.journalSegmentBytes) {
@@ -288,9 +303,13 @@ function parseJournalSegment(bytes, ref = null, { canonical: checkCanonical = tr
288
303
  if (!text.endsWith('\n') || text.startsWith('\uFEFF')) fail('STORE_CORRUPT', 'journal_byte_contract', detail);
289
304
  const lines = text.slice(0, -1).split('\n');
290
305
  if (lines.some((line) => line.length === 0)) fail('STORE_CORRUPT', 'journal_truncated_or_empty_line');
291
- return lines.map((line) => {
306
+ const digest = sha256Bytes(bytes);
307
+ const strictKey = `strict:${digest}`;
308
+ const known = acceptedSegments.has(strictKey) || (!checkCanonical && acceptedSegments.has(`read:${digest}`));
309
+ const events = lines.map((line) => {
292
310
  let event;
293
311
  try { event = JSON.parse(line); } catch { fail('STORE_CORRUPT', 'journal_json_invalid'); }
312
+ if (known) return event;
294
313
  let canonical;
295
314
  if (checkCanonical) try { canonical = canonicalizeTodoArtifact(event); } catch { fail('STORE_CORRUPT', 'journal_schema_invalid'); }
296
315
  if (checkCanonical && line !== canonical) fail('STORE_CORRUPT', 'journal_non_canonical_or_duplicate_key');
@@ -301,6 +320,8 @@ function parseJournalSegment(bytes, ref = null, { canonical: checkCanonical = tr
301
320
  }
302
321
  return event;
303
322
  });
323
+ acceptedSegments.add(checkCanonical ? strictKey : `read:${digest}`);
324
+ return events;
304
325
  }
305
326
 
306
327
  /** Sealed segments of one chain, in order, each checked for name, digest, range and link. */