Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
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).admitChangeReasoncanonicalises areason(one line, at most 500 code points) and refuseschange_reason_rejected(reason_too_long,reason_contains_secretviascanTextSpans) before admission, so no journal row ever holds a refused reason;page-mutations.tsruns it beforecallerIntent, so the canonical reason joins replay identity, andpage-batch.tsruns it per item (all or nothing).withChangeReasonwraps a page preparer inservice.ts: its apply stampspage_versions.change_reasonon the snapshots that transaction created for the page, and, for a world-visible page whose file is published, appendsgbrain-reason: <path>: <text>toprepared.file.commit(aGitCommitNotewithtrailers[]), which both the door (commitThroughDoor) and the effect commit (commitMessage, one trailer line each,commitMessageArgsswitching to-Fover 8 KiB) read after apply. The outcome'sreason_recorded.gitiscommitted(strict),queued,private,not_file_backed,no_repository(repo-less) ornone;settledReasonRecordedturnsqueuedintosupersededwith areason_not_committednotice on the receipt when the Git effect was superseded. Read side:replacedBecauseSqlinengine-sql/pages.ts(remote readers see a reason only when the successor snapshot is world-visible and notsuccessor_pruned, which the prunes inpage-state/versions.tsset). 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).readReasonTrailersstreams onegit logthroughhardenedGitStreamwithREASON_LOG_OPTIONS(hardened-git.ts:-z --first-parent --diff-merges=first-parent --root --name-only --no-renamesand 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.scanSyncReasonsmaps each repository path to its newest first-parent commit's trailer (canonicalised bycanonicalChangeReason, over-long ones counted as malformed, secrets viascanTextSpansreported by commit and path,reason_purge_tombstonespairs skipped) and counts touches per path;importHistoryForturns that into theVersionHistoryan import passes tocreateVersion(more than one commit, or a failed or capped scan, setshistoryGap), with the deciding commit and path asreasonSource, so the snapshot insert rechecks that pair's tombstone under the page guard (#8635).gitTopLevelreturns null outside a repository, so a repo-less or plain-folder source runs no scan.reasonScanNoticesbuildsreason_scan_failed/reason_scan_capped/reason_trailer_secret.cachedSyncReasonScanshares one scan per (source, root, range) for managed preparations; purge clears it.scanTrailersCarryingis 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.tsscanslast_commit..pinonce per pass when it imports or renames files (not for a company brain),full.tsscans the whole history only when the source already has pages, andimports.ts/renames.ts/runImportpass each file'shistorytoimportFile;withReasonNoticesputs the notices andreason_trailers_malformedon theSyncResult(sync/run.tsrenders them). Managed:sync-prepare.tscallsmanagedFileHistory(claim stepreason_scan) only when the page exists, andperformManagedPassadds the pass's notices throughwithManagedReasonNotices. Content never waits on the scan.src/core/facts/purge-reasons.ts— A7 (purge and reasons).scanPurgeTrailersruns before the purge transaction (no checkout: nothing to scan; a failed read refusesreason_purge_scan_failed,fix.next = wait; over the capreason_purge_scan_capped, ask_user with--reason-scan-limit). Inside the transactionredactGitEffectReasonsrewritescommit_line/commit_trailers/ legacycommit_traileron queued, failed and committed git effects (a running one refusespurge_blocked_pending_recovery) andrecordReasonTombstonesrecheckshead..HEADand inserts text-freereason_purge_tombstonesrows.purge.tsredactPageVersionssets a quotingchange_reasonto[purged];purge-verify.tschecks it and reports pushed commits as thegit_commit_messagesresidual.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 eachreason_purge_tombstonesrow's<commit>^!from the source's checkout (gitTopLevel,readReasonTrailers,trailerTextswithpathForms,canonicalChangeReason) and returns the source'spage_versionswhosechange_reasonequals that text:evidencedwhensource_pathis 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, takessources FOR UPDATEand sets each approved row still of its class to[purged]only while it holds exactly that text, onemaintenanceTransactionper batch (changed_since_previewotherwise). Tests:test/purged-change-reasons-repair-gst2886.test.ts(PGLite and Postgres).