Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Wave B 1b-i: resumable SHA-256, streamed content_hash, shared hash/frontmatter helpers (WIP)
garrytan08c88428ef77
diff --git a/src/core/data-frontmatter.ts b/src/core/data-frontmatter.tsindex 5fc4b5421c..6e661f2092 100644--- a/src/core/data-frontmatter.ts+++ b/src/core/data-frontmatter.ts@@ -42,14 +42,22 @@ export class FrontmatterLanguageError extends Error {}}-/** Parse data only. There is deliberately no pluggable engine or evaluator. */-export function parseDataFrontmatter(input: string): DataFrontmatter {+/**+ * Where parseDataFrontmatter finds the block: the input without its BOM and,+ * for a fenced input, the language, the block text and the body after the+ * closing fence (`content` null when the fence never closes). Throws+ * FrontmatterLanguageError for a language selector other than YAML or JSON.+ */+export function scanDataFrontmatter(input: string): {+ original: string;+ fenced: { language: string; block: string; content: string | null } | null;+} {const original = input.replace(/^\uFEFF/, '');// Preserve ordinary body whitespace; lift leading blank lines only for a fence.const lifted = original.replace(/^(?:[\t ]*\r?\n)+(?=---)/, '');const opening = /^---([^\r\n]*)(?:\r?\n|$)/.exec(lifted);if (!opening || opening[1]!.startsWith('-') || opening[1]!.trimEnd().endsWith('---')) {- return { data: {}, content: original, hasFrontmatter: false };+ return { original, fenced: null };}const language = opening[1]!.trim().toLowerCase();if (language !== '' && language !== 'yaml' && language !== 'yml' && language !== 'json') {@@ -59,7 +67,21 @@ export function parseDataFrontmatter(input: string): DataFrontmatter {const closing = /^---[\t ]*(?:\r?\n|$)/m.exec(rest);// Match the established parser's missing-close behavior: parse the remaining// block and let the higher-level markdown validator report MISSING_CLOSE.- const block = closing ? rest.slice(0, closing.index) : rest;+ return {+ original,+ fenced: {+ language,+ block: closing ? rest.slice(0, closing.index) : rest,+ content: closing ? rest.slice(closing.index + closing[0].length) : null,+ },+ };+}++/** Parse data only. There is deliberately no pluggable engine or evaluator. */+export function parseDataFrontmatter(input: string): DataFrontmatter {+ const { original, fenced } = scanDataFrontmatter(input);+ if (!fenced) return { data: {}, content: original, hasFrontmatter: false };+ const { language, block } = fenced;let value: unknown;try {value = block.trim() === '' ? {} : language === 'json' ? JSON.parse(block) : load(block, { schema: FRONTMATTER_SCHEMA });@@ -74,7 +96,7 @@ export function parseDataFrontmatter(input: string): DataFrontmatter {}return {data: (value ?? {}) as Record<string, unknown>,- content: closing ? rest.slice(closing.index + closing[0].length) : '',+ content: fenced.content ?? '',hasFrontmatter: true,};}diff --git a/src/core/fence-scan.ts b/src/core/fence-scan.tsindex e195b72390..c9d2661bf6 100644--- a/src/core/fence-scan.ts+++ b/src/core/fence-scan.ts@@ -385,10 +385,10 @@ export function findLineOutsideFencedCode(return walk.opener || walk.mispaired ? firstQuoted : firstOutside;}-interface FenceWalk { opener: string | null; mispaired: boolean }+export interface FenceWalk { opener: string | null; mispaired: boolean }/** Advance the fence pairing by one line (a trailing CR is its line ending). */-function stepFenceWalk(walk: FenceWalk, line: string): void {+export function stepFenceWalk(walk: FenceWalk, line: string): void {// A fence line starts with indent, ` or ~; every other line is prose to it.const c0 = line.charCodeAt(0);if (c0 !== 32 && c0 !== 96 && c0 !== 126) return;diff --git a/src/core/page-body/content-hash-stream.ts b/src/core/page-body/content-hash-stream.tsnew file mode 100644index 0000000000..88894ae941--- /dev/null+++ b/src/core/page-body/content-hash-stream.ts@@ -0,0 +1,106 @@+/**+ * `content_hash` as a resumable stream (Wave B, spec 1.4). `contentHash`+ * (utils.ts) is sha256 over+ *+ * JSON.stringify({title, type, compiled_truth, timeline || '', frontmatter*, tags*})+ *+ * This module feeds the same bytes piece by piece:+ *+ * '{"title":' J(title) ',"type":' J(type) ',"compiled_truth":"' E(truth 1) E(truth 2) ...+ * '","timeline":"' E(timeline 1) ... '","frontmatter":' J(frontmatter*) ',"tags":' J(tags*) '}'+ *+ * with J = JSON.stringify and E(s) = JSON.stringify(s).slice(1, -1). E is exact+ * for any split on a code-point boundary (segments always end on one), so the+ * digest depends only on the logical page, never on where segments were cut.+ *+ * A manifest row stores `exportState()` after its own E(...) bytes+ * (`hash_state`). Resume rules:+ * - append to the last region: resume from the last row's state, push the appended text;+ * - append to the truth while the timeline is non-empty: resume from the last truth row,+ * push the appended truth, then re-push every timeline row;+ * - frontmatter or tags change: resume from the last row and `finish` with the new values;+ * - title or type change: the prefix changed, start over with `begin`.+ */+import type { PageInput } from '../types.ts';+import { contentHashFrontmatterAndTags } from '../utils.ts';+import { ResumableSha256 } from './sha256-resumable.ts';++type Phase = 'truth' | 'timeline';+const PHASE_BYTE: Record<Phase, number> = { truth: 0, timeline: 1 };++/** Largest `ContentHashStream.exportState()` result in bytes (phase byte plus SHA-256 midstate). */+export const CONTENT_HASH_STATE_MAX_BYTES = 104;++/** Envelope member text: `"name":` + J(value), or '' when JSON.stringify omits the member. */+function member(name: string, value: unknown): string {+ const json = JSON.stringify(value);+ return json === undefined ? '' : `"${name}":${json}`;+}++export class ContentHashStream {+ private constructor(private readonly sha: ResumableSha256, private phase: Phase) {}++ /** Start a page: feeds the envelope through the opening quote of `compiled_truth`. */+ static begin(page: { title: unknown; type: unknown }): ContentHashStream {+ const stream = new ContentHashStream(new ResumableSha256(), 'truth');+ const head = [member('title', page.title), member('type', page.type)].filter(Boolean);+ head.push('"compiled_truth":"');+ stream.feed(`{${head.join(',')}`);+ return stream;+ }++ static fromState(state: Uint8Array): ContentHashStream {+ const phase = state[0] === PHASE_BYTE.timeline ? 'timeline' : state[0] === PHASE_BYTE.truth ? 'truth' : null;+ if (!phase) throw new Error(`content hash state has an unknown phase byte ${state[0]}`);+ return new ContentHashStream(ResumableSha256.fromState(state.subarray(1)), phase);+ }++ /** Append compiled_truth text (a whole number of code points). */+ pushTruth(text: string): this {+ if (this.phase !== 'truth') throw new Error('content hash stream: truth text after the timeline began');+ this.feed(JSON.stringify(text).slice(1, -1));+ return this;+ }++ /** Close compiled_truth and open the timeline string. Idempotent. */+ beginTimeline(): this {+ if (this.phase === 'truth') {+ this.feed('","timeline":"');+ this.phase = 'timeline';+ }+ return this;+ }++ /** Append timeline text (a whole number of code points); opens the timeline first if needed. */+ pushTimeline(text: string): this {+ this.beginTimeline();+ this.feed(JSON.stringify(text).slice(1, -1));+ return this;+ }++ /** The midstate after everything pushed so far (at most CONTENT_HASH_STATE_MAX_BYTES). */+ exportState(): Uint8Array {+ const sha = this.sha.exportState();+ const out = new Uint8Array(1 + sha.length);+ out[0] = PHASE_BYTE[this.phase];+ out.set(sha, 1);+ return out;+ }++ /** Hex content_hash with this frontmatter and tags. The stream itself is left unchanged. */+ finish(meta: Pick<PageInput, 'frontmatter' | 'tags'>): string {+ const tail = ContentHashStream.fromState(this.exportState()).beginTimeline();+ const { frontmatter, tags } = contentHashFrontmatterAndTags(meta);+ tail.feed(`",${member('frontmatter', frontmatter)},${member('tags', tags)}}`);+ return tail.sha.digest();+ }++ private feed(text: string): void {+ this.sha.update(Buffer.from(text, 'utf8'));+ }+}++/** `contentHash(page)` computed through the stream, one push per region (tests and small callers). */+export function streamingContentHash(page: PageInput): string {+ return ContentHashStream.begin(page).pushTruth(page.compiled_truth).pushTimeline(page.timeline || '').finish(page);+}diff --git a/src/core/page-body/sha256-resumable.ts b/src/core/page-body/sha256-resumable.tsnew file mode 100644index 0000000000..b492c09f71--- /dev/null+++ b/src/core/page-body/sha256-resumable.ts@@ -0,0 +1,130 @@+/**+ * Pure-TS SHA-256 whose midstate can be saved and resumed (Wave B, spec 1.4).+ * Node and Bun hash natively but do not serialize a midstate (`Hash.copy()`+ * forks in memory only), and every segmented manifest row stores the+ * content_hash stream's state after its own bytes so an append re-hashes only+ * the appended bytes. The digest equals node:crypto's on every input.+ *+ * State layout (`exportState`, at most 103 bytes): the eight 32-bit hash words+ * big-endian (32 bytes), the total byte count as a big-endian u64 (8 bytes),+ * then the buffered partial block (`total % 64` bytes).+ */++const K = new Int32Array([+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,+]);+const IV = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];+const STATE_HEADER_BYTES = 40;++/** Largest `exportState()` result: header plus a 63-byte partial block. */+export const SHA256_STATE_MAX_BYTES = STATE_HEADER_BYTES + 63;++export class ResumableSha256 {+ private readonly h = new Int32Array(IV);+ private readonly buf = new Uint8Array(64);+ private readonly bufView = new DataView(this.buf.buffer);+ private readonly w = new Int32Array(64);+ private total = 0;++ update(bytes: Uint8Array): this {+ const len = bytes.length;+ let i = 0;+ let used = this.total % 64;+ this.total += len;+ if (used > 0) {+ const take = Math.min(64 - used, len);+ this.buf.set(bytes.subarray(0, take), used);+ used += take;+ i = take;+ if (used < 64) return this;+ compress(this.h, this.w, this.bufView, 0);+ }+ if (i + 64 <= len) {+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);+ for (; i + 64 <= len; i += 64) compress(this.h, this.w, view, i);+ }+ if (i < len) this.buf.set(bytes.subarray(i), 0);+ return this;+ }++ /** Hex digest of everything fed so far; the hasher stays usable (it pads a copy). */+ digest(): string {+ const copy = ResumableSha256.fromState(this.exportState());+ const used = copy.total % 64;+ const buf = copy.buf;+ buf[used] = 0x80;+ buf.fill(0, used + 1);+ if (used >= 56) {+ compress(copy.h, copy.w, copy.bufView, 0);+ buf.fill(0);+ }+ const hi = Math.floor(copy.total / 2 ** 29);+ const lo = (copy.total * 8) >>> 0;+ for (let k = 0; k < 4; k++) {+ buf[59 - k] = (hi >>> (8 * k)) & 0xff;+ buf[63 - k] = (lo >>> (8 * k)) & 0xff;+ }+ compress(copy.h, copy.w, copy.bufView, 0);+ let hex = '';+ for (let k = 0; k < 8; k++) hex += (copy.h[k]! >>> 0).toString(16).padStart(8, '0');+ return hex;+ }++ exportState(): Uint8Array {+ const used = this.total % 64;+ const out = new Uint8Array(STATE_HEADER_BYTES + used);+ const view = new DataView(out.buffer);+ for (let k = 0; k < 8; k++) view.setInt32(k * 4, this.h[k]!);+ view.setUint32(32, Math.floor(this.total / 2 ** 32));+ view.setUint32(36, this.total >>> 0);+ out.set(this.buf.subarray(0, used), STATE_HEADER_BYTES);+ return out;+ }++ static fromState(state: Uint8Array): ResumableSha256 {+ if (state.length < STATE_HEADER_BYTES) throw new Error(`SHA-256 state too short (${state.length} bytes)`);+ const view = new DataView(state.buffer, state.byteOffset, state.byteLength);+ const total = view.getUint32(32) * 2 ** 32 + view.getUint32(36);+ if (state.length !== STATE_HEADER_BYTES + (total % 64)) {+ throw new Error(`SHA-256 state length ${state.length} does not match its byte count ${total}`);+ }+ const out = new ResumableSha256();+ for (let k = 0; k < 8; k++) out.h[k] = view.getInt32(k * 4);+ out.total = total;+ out.buf.set(state.subarray(STATE_HEADER_BYTES), 0);+ return out;+ }+}++/**+ * One 64-byte block. Words load through DataView.getInt32 (faster than four+ * byte loads on Bun); the padding is written by hand in `digest` so this call+ * site only ever sees DataViews over input or the block buffer.+ */+function compress(h: Int32Array, w: Int32Array, view: DataView, o: number): void {+ for (let t = 0; t < 16; t++, o += 4) w[t] = view.getInt32(o);+ for (let t = 16; t < 64; t++) {+ const x = w[t - 15]!;+ const y = w[t - 2]!;+ const s0 = ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);+ const s1 = ((y >>> 17) | (y << 15)) ^ ((y >>> 19) | (y << 13)) ^ (y >>> 10);+ w[t] = (w[t - 16]! + s0 + w[t - 7]! + s1) | 0;+ }+ let a = h[0]!, b = h[1]!, c = h[2]!, d = h[3]!, e = h[4]!, f = h[5]!, g = h[6]!, hh = h[7]!;+ for (let t = 0; t < 64; t++) {+ const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));+ const t1 = (hh + S1 + (g ^ (e & (f ^ g))) + K[t]! + w[t]!) | 0;+ const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));+ const t2 = (S0 + ((a & b) | (c & (a | b)))) | 0;+ hh = g; g = f; f = e; e = (d + t1) | 0; d = c; c = b; b = a; a = (t1 + t2) | 0;+ }+ h[0] = (h[0]! + a) | 0; h[1] = (h[1]! + b) | 0; h[2] = (h[2]! + c) | 0; h[3] = (h[3]! + d) | 0;+ h[4] = (h[4]! + e) | 0; h[5] = (h[5]! + f) | 0; h[6] = (h[6]! + g) | 0; h[7] = (h[7]! + hh) | 0;+}diff --git a/src/core/utils.ts b/src/core/utils.tsindex 4dd5edddc2..c71da7464b 100644--- a/src/core/utils.ts+++ b/src/core/utils.ts@@ -117,23 +117,36 @@ export const HASH_EPHEMERAL_FRONTMATTER_KEYS: readonly string[] = [* callers usually leave them inside — both now hash identically).*/export function contentHash(page: PageInput): string {- const fm: Record<string, unknown> = { ...(page.frontmatter || {}) };- for (const k of HASH_EPHEMERAL_FRONTMATTER_KEYS) delete fm[k];- const rawTags = page.tags ?? fm.tags;- delete fm.tags;- const tags = Array.isArray(rawTags) ? rawTags.map(t => String(t)).sort() : [];+ const { frontmatter, tags } = contentHashFrontmatterAndTags(page);return createHash('sha256').update(JSON.stringify({title: page.title,type: page.type,compiled_truth: page.compiled_truth,timeline: page.timeline || '',- frontmatter: fm,+ frontmatter,tags,})).digest('hex');}+/**+ * The `frontmatter*` and `tags*` members of the content hash envelope: a copy+ * of the frontmatter without HASH_EPHEMERAL_FRONTMATTER_KEYS and `tags`, and+ * `page.tags ?? frontmatter.tags` as sorted strings. Shared with the streamed+ * form (page-body/content-hash-stream.ts) so both hash identical bytes.+ */+export function contentHashFrontmatterAndTags(page: Pick<PageInput, 'frontmatter' | 'tags'>): {+ frontmatter: Record<string, unknown>;+ tags: string[];+} {+ const fm: Record<string, unknown> = { ...(page.frontmatter || {}) };+ for (const k of HASH_EPHEMERAL_FRONTMATTER_KEYS) delete fm[k];+ const rawTags = page.tags ?? fm.tags;+ delete fm.tags;+ return { frontmatter: fm, tags: Array.isArray(rawTags) ? rawTags.map(t => String(t)).sort() : [] };+}+/*** The pre-#3694 putPage-side formula (no ephemeral strip, no tags array).* Kept ONLY so the importer can recognize a DB row written by the old