Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Merge remote-tracking branch 'origin/master' into capy/belief-overhead
garrytand98a90444c05
diff --cc docs/designs/BELIEF_LEDGER.mdindex 50c26e2bcb,8a18b16a83..5f2ae38d15--- a/docs/designs/BELIEF_LEDGER.md+++ b/docs/designs/BELIEF_LEDGER.md@@@ -1547,47 -1504,88 +1549,132 @@@ Garry accepted those answers. Questionand 2000 pages on PGLite). The read now sets `force_generic_plan` forthat one statement, transaction-local, and restores the previous value;the cost stays about 0.6 ms per page at every size.+17. **Projections skip identical rewrites; the post read reuses untouched+ tables.** The fence fact-field update, the take upsert and the take+ resolution update write only rows whose values change, so a prose edit+ rewrites no fact or take row and `takes.updated_at` moves only when the+ take changes. Nothing reads a rewrite that changes no value: write+ attribution and the trust tier already move only on a value change, and+ every `updated_at` cursor and freshness reader is on `pages`. The post+ read compares the transaction's `pg_stat_get_xact_tuples_*` counters for+ facts, takes, links and link transitions with the pre read's and reuses+ the pre read's rows for every table the transaction did not write, while+ the stream pages are the same pages at the same slugs (fact entity pages+ and link endpoints are read fresh). With `track_counts` off it reads+ everything. A missed write would surface as drift in doctor+ `belief_ledger_coverage`, which a forced probe shows.+18. **Fewer statements per batch.** The source read that opens a batch also+ reads the managed mode and switches to generic plans for the pre read;+ each state read restores the plan mode inside its own statement. When+ the post read reused a table's rows, the post side reuses the pre side's+ fact and take subjects (take visibility from the post side) instead of+ keying them again.+19. **Process-local fingerprint cache.** Each stream read hashes claim texts+ (SHA-256, in SQL) and fingerprints only those whose hash the process has+ not fingerprinted before; `gbrain_fact_fingerprint` costs about 27 µs per+ claim on Postgres, the SHA-256 under 1 µs. The cache maps text hash to+ fingerprint only, never claim text, and is bounded (20,000 fingerprints,+ 2,000 streams, oldest out first). A forced probe compares cached and SQL+ fingerprints for random claims with case, Unicode, punctuation and+ whitespace edge cases on both engines, because a divergence would break+ purge matching. One-shot CLI processes start cold and pay the full read.+20. **Declined: a subject snapshot cache on the lifetime row** (the "cheaper+ option" section 9 listed). After items 17 to 19 a typical Postgres edit+ costs about 3.8 ms of a 44–50 ms write. Removing the pre read entirely+ would leave about 2.6–2.8 ms (5.5–6%); a real cache also needs a+ validity check and a write per batch, so about 6–7%, still above the 5%+ target, for a migration plus invalidation on purge, redaction and+ unmanaged writers. Not built.+21. **Keying without re-sorting.** `canonicalJson` sorts an object's keys+ only when they are not already in order and caches the quoted form of+ key names (at most 4,096); fact and take values are built with their+ keys in canonical order; the state hash computes each subject's sort key+ once. The bytes are unchanged, so every stored value, claim and state+ hash stays valid: test/belief-ledger-canonical-json.test.ts holds the+ serializer and the state hash to the original implementation on a+ seeded corpus and every edge case, and broken variants fail it.++ ## 14. W2's first slice as built: page-at-time++ 1. **Surface.** Page-at-time is a parameter, not a new op: `get_page+ {known_as_of}` and `get_versions {known_as_of, diff, source_id}`, full+ surface only, so the served starter schema is unchanged. `known_as_of` is+ recorded time, an ISO 8601 instant with a zone; a bare date refuses+ `clock_mismatch` (`date_for_instant`) instead of being read as midnight,+ because a date is the valid-time clock (`as_of` on `get_links`). The+ catalogue gains `clock_mismatch` and `known_as_of_unproven` (retryable);+ `coverage_from` names `not_recorded`, `backfill_only` or+ `backfill_incomplete`, and backfilled history never answers a+ recorded-time read, because it has source times only. Every answer carries+ `certified` and `uncertified_reason`.+ 2. **Bodies pair by revision, in order.** Several intervals can share a+ revision: a withdrawal's materialization is a view change at the same+ revision. So the closed intervals of one revision pair with that+ revision's versions in id order, newest with newest, and the open head+ reads the live page while it is still at that revision. A body no version+ holds is returned as `body: unavailable` with `body_reason` (`purged`,+ `expired`, `hard_deleted`, `pruned` for a large or segmented page,+ `version_missing`), never taken from a neighbour.+ 3. **Correction to section 10, item 1: a rename also left no preimage.** The+ page revision trigger advances the revision on a slug change, and+ `updateSlug` (the cheap sync rename, the frontmatter slug repair, the held+ file and import moves, managed sync's rename) wrote no version, so after a+ rename no version held the body from before it. `updateSlug` now versions+ the page first, in the same transaction. A publication that changes the+ revision twice in one transaction (a rename then an edit) records the+ pre-transaction revision as `revision_before`, and that revision now has+ its version too. A BEHAVIOR_CHANGES row discloses the extra version.+ 4. **Correction to sections 3 and 10: a soft delete keeps the lifetime.** A+ write to a soft-deleted slug restores the same page row and appends+ `page.restored` on the same lifetime (`ledger.ts`); only a page removed+ outright (purge, retention cleanup) and written again starts a new+ lifetime. W1's code and probes already behaved this way; only the text was+ wrong.+ 5. **Purged lifetimes collapse.** Section 6.5 sets `value` to NULL on every+ event under a purged key, page events included, so a purged lifetime's+ events no longer carry revisions. Its intervals split only where its slug+ or deleted state changed (the event type and row still say), and every+ body is `purged`. A local caller reading an instant before the purge gets+ that interval with no body; a remote caller gets `page_not_found`, because+ the page row is gone and the remote rule fails closed. A probe un-redacting+ the revisions makes the lifetime split again, and `purge_fact` probes show+ no historical body or diff returns the purged claim to either caller.+ 6. **Proof of commit is the same lifetime, and backfill never proves.** An+ unmarked event counts at `T` only when a later event of the same page+ lifetime, not of origin `backfill`, was recorded by `T`. That is+ stricter than `beliefEventVisibility`, which also accepts a later+ backfill row, so a read can refuse `known_as_of_unproven` where the W1+ helper says visible; it never answers where the helper says unproven.+ 7. **The stalled-transaction cell runs on commit marks.** The read depends+ only on marks and recorded times, so the cell deletes and re-adds a mark+ (and stamps one at `recorded_at` as its forced probe) rather than holding+ a transaction past ceiling plus grace; W1's probe 13 holds a real one.+ 8. **The preimage invariant, enforced.** Every publication that moves a+ page's knowledge revision leaves a `page_versions` row of the revision it+ started from (`revision_before`), written in the same transaction before+ the write that advances it. Revisions a transaction only passes through+ (a rename then an edit in one publication) are never a ledger state, since+ events record only the publication's start and end, so they need no+ version. Large and segmented pages are exempt, because their versions are+ pruned on their own policy and page-at-time discloses those bodies as+ `pruned`. Enforcement has two parts:+ - **In tests**, `finalizeBeliefEvents` checks every stream whose+ revision moved and fails the transaction when no version of+ `revision_before` exists (`preimages.ts`, `missingPreimages`), so a+ writer that breaks the rule fails its own tests. It runs only under+ `NODE_ENV=test` (`GBRAIN_BELIEF_PREIMAGE_CHECK=0` turns it off for a+ probe that breaks the rule on purpose): in production it would add one+ indexed lookup per revision-moving publication to the write path the+ W1 overhead work is cutting.+ - **In production**, doctor `belief_ledger_coverage` lists page events+ from `live` or `unmanaged` publications whose `revision_before` no+ version of the live page holds (`preimageGaps`: last 30 days, complete+ under `--tier deep`) and warns, naming the slugs and events. The body+ is not recoverable; the finding names the writer to fix.++ Turning the check on found a second writer that broke the rule:+ `purge_fact` rewrote each affected page body (dropping the purged fence+ row) with no version. It now versions each page before the rewrite, and+ its version redaction pass then redacts that version like every other+ one, so the interval before a purge reads the body as it stood, minus the+ purged claim.