Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
6.2 KB
Raw Blame

Key files: Core Persistence (page history)

Subsystem index · Core Persistence. Read only the entries relevant to your change. Current behavior and load-bearing invariants; history belongs in Git and CHANGELOG. Page-write reasons: admission, the snapshot column, the Git trailer and the remote read projection.

  • src/core/persistence/change-reason.ts — GBRA-217 page-write reasons (page history). admitChangeReason canonicalises a reason (one line, at most 500 code points) and refuses change_reason_rejected (reason_too_long, reason_contains_secret via scanTextSpans) before admission, so no journal row ever holds a refused reason; page-mutations.ts runs it before callerIntent, so the canonical reason joins replay identity, and page-batch.ts runs it per item (all or nothing). withChangeReason wraps a page preparer in service.ts: its apply stamps page_versions.change_reason on the snapshots that transaction created for the page, and, for a world-visible page whose file is published, appends gbrain-reason: <path>: <text> to prepared.file.commit (a GitCommitNote with trailers[]), which both the door (commitThroughDoor) and the effect commit (commitMessage, one trailer line each, commitMessageArgs switching to -F over 8 KiB) read after apply. The outcome's reason_recorded.git is committed (strict), queued, private, not_file_backed, no_repository (repo-less) or none; settledReasonRecorded turns queued into superseded with a reason_not_committed notice on the receipt when the Git effect was superseded. Read side: replacedBecauseSql in engine-sql/pages.ts (remote readers see a reason only when the successor snapshot is world-visible and not successor_pruned, which the prunes in page-state/versions.ts set). Tests: test/page-change-reason-gbra217.test.ts, test/page-change-reason-git-gbra217.test.ts, test/page-change-reason-mcp-gbra217.test.ts.
  • src/core/reason-trailers.ts — GBRA-217 reasons read back from Git (reasons from Git). readReasonTrailers streams one git log through hardenedGitStream with REASON_LOG_OPTIONS (hardened-git.ts: -z --first-parent --diff-merges=first-parent --root --name-only --no-renames and a %x00%H%x00%(trailers:key=gbrain-reason,valueonly,unfold) format; no pathspec, no grep), parses records as they stream and stops at a commit cap. scanSyncReasons maps each repository path to its newest first-parent commit's trailer (canonicalised by canonicalChangeReason, over-long ones counted as malformed, secrets via scanTextSpans reported by commit and path, reason_purge_tombstones pairs skipped) and counts touches per path; importHistoryFor turns that into the VersionHistory an import passes to createVersion (more than one commit, or a failed or capped scan, sets historyGap), with the deciding commit and path as reasonSource, so the snapshot insert rechecks that pair's tombstone under the page guard (#8635). gitTopLevel returns null outside a repository, so a repo-less or plain-folder source runs no scan. reasonScanNotices builds reason_scan_failed / reason_scan_capped / reason_trailer_secret. cachedSyncReasonScan shares one scan per (source, root, range) for managed preparations; purge clears it. scanTrailersCarrying is purge's read (cap 200,000, or the user's --reason-scan-limit). Caps 10,000 (sync) and 200,000 (purge), 120 s timeout. Seams: __setReasonScanCapsForTests, __setReasonGitStreamForTests. Test: test/reason-trailers-gbra217.test.ts (both engines).
  • src/commands/sync/reasons.ts + src/core/persistence/sync-reasons.ts — the sync wiring. Classic: incremental.ts scans last_commit..pin once per pass when it imports or renames files (not for a company brain), full.ts scans the whole history only when the source already has pages, and imports.ts / renames.ts / runImport pass each file's history to importFile; withReasonNotices puts the notices and reason_trailers_malformed on the SyncResult (sync/run.ts renders them). Managed: sync-prepare.ts calls managedFileHistory (claim step reason_scan) only when the page exists, and performManagedPass adds the pass's notices through withManagedReasonNotices. Content never waits on the scan.
  • src/core/facts/purge-reasons.ts — A7 (purge and reasons). scanPurgeTrailers runs before the purge transaction (no checkout: nothing to scan; a failed read refuses reason_purge_scan_failed, fix.next = wait; over the cap reason_purge_scan_capped, ask_user with --reason-scan-limit). Inside the transaction redactGitEffectReasons rewrites commit_line / commit_trailers / legacy commit_trailer on queued, failed and committed git effects (a running one refuses purge_blocked_pending_recovery) and recordReasonTombstones rechecks head..HEAD and inserts text-free reason_purge_tombstones rows. purge.ts redactPageVersions sets a quoting change_reason to [purged]; purge-verify.ts checks it and reports pushed commits as the git_commit_messages residual.
  • src/core/repair/purged-change-reasons.ts — gbrain repair purged-change-reasons (GST-2886, #8635 follow-up), explicit-only, preview-bound, destructive. findLeakedChangeReasons (read-only) re-reads the trailers of each reason_purge_tombstones row's <commit>^! from the source's checkout (gitTopLevel, readReasonTrailers, trailerTexts with pathForms, canonicalChangeReason) and returns the source's page_versions whose change_reason equals that text: evidenced when source_path is a form of the tombstoned path, else, or when the commit gives that path more than one reason, path_mismatch (only with --include-ambiguous); unreadable commits and repo-less sources are residuals. The approved set holds version ids and classes, never the text or a digest of it; the apply reads the candidates from Git again, takes sources FOR UPDATE and sets each approved row still of its class to [purged] only while it holds exactly that text, one maintenanceTransaction per batch (changed_since_preview otherwise). Tests: test/purged-change-reasons-repair-gst2886.test.ts (PGLite and Postgres).