Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

docs(key-files): keep core-utilities-1 under its 60KB cap after the wave 12 merge

time-attack7cbbb2c35f6e
diff --git a/docs/architecture/key-files/core-utilities-1.md b/docs/architecture/key-files/core-utilities-1.md
index a82590e07c..2637bae08f 100644
--- a/docs/architecture/key-files/core-utilities-1.md
+++ b/docs/architecture/key-files/core-utilities-1.md
@@ -61,7 +61,7 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL
- `src/core/preferences.ts` — preferences.json + the migration ledger (`migrations/completed.jsonl` append/read helpers; `appendCompletedMigration`, `loadCompletedMigrations`). Path resolution delegates to `config.ts:gbrainPath()`, so GBRAIN_HOME follows the ONE canonical convention: it is a PARENT dir and `.gbrain` is appended (`GBRAIN_HOME=/tmp/x` → `/tmp/x/.gbrain/migrations/...`). `copyForwardLegacyFile` migrates a legacy layout that placed these files directly under `$GBRAIN_HOME` once per file on first touch: atomic (temp + linkSync, EEXIST = concurrent winner), JSON-validated for prefs, chmod 0600, copy-not-move for binary rollback, once-per-process warning; a valid-but-uncopyable legacy file (read-only home) is READ IN PLACE so a transient failure can't drop a `minion_mode` opt-out or migration history. One-shot by design — mixed-version writers diverging post-snapshot is an accepted, documented limitation.
-- `src/core/reindex-consent.ts` — W4.5 paid-consent gate for `reindex --markdown`: `estimateReindexSpend` (pages after `--limit`, embedding estimate from the pending pages' text under the `countPending` predicate; null when the model has no price) and `requireReindexConsent` (null when `embedWouldSpend` is false; else `requireConsent` with effect `paid`, page count and estimate in `user_message`/`est_usd`, `--yes` command and `--dry-run` preview; `interactive: false` never prompts; a user cap below the estimate throws `cost_cap_exceeded`). Called by `runReindex` (CLI, `upgrade`, the `reindex` job handler) and `jobs submit reindex`, which stores the authorization on the job (`jobSpendAuthorization`); the search-mode switch prompt stores its TTY yes the same way. The handler (`minions/handlers/reindex.ts`) runs authorized only with `job.spend.record` and turns the consent refusal into a terminal `confirmation_required` error. Tests: `test/reindex-consent.test.ts`.
+- `src/core/reindex-consent.ts` — W4.5 paid-consent gate for `reindex --markdown`: `estimateReindexSpend` (pages after `--limit`, estimate over pending pages' text via `countPending`; null when unpriced) and `requireReindexConsent` (null when `embedWouldSpend` is false; else `requireConsent` with effect `paid`, page count and estimate in `user_message`/`est_usd`, `--yes` command and `--dry-run` preview; `interactive: false` never prompts; a user cap below the estimate throws `cost_cap_exceeded`). Called by `runReindex` (CLI, `upgrade`, job handler) and `jobs submit reindex` (stores `jobSpendAuthorization`; so does the search-mode switch prompt's TTY yes). `minions/handlers/reindex.ts` runs paid work only with `job.spend.record`; a refusal is a terminal `confirmation_required`. Tests: `test/reindex-consent.test.ts`.
- `src/core/remediation-checkpoint.ts` — `doctor --remediate` checkpoint at `~/.gbrain/remediation/<plan_hash>.json`. `plan_hash = sha256(JSON.stringify(sorted recommendation ids)).slice(0,16)`. Schema-versioned, atomic `.tmp + rename`. `gbrain doctor --remediate --resume <plan_hash>` (no arg picks newest matching) loads it and skips completed steps. Mismatched plan_hash refuses with a paste-ready message. Cleared on clean completion. Optional resume-contract fields written by the doctor CLI: `brain_id` (a resume against another brain refuses), `max_usd` (reused by `--resume` without `--max-usd`), `include_repairs`, cumulative `spent_usd` and the original step `manifest` (`job_ids`, `repair_kinds`); a checkpoint without them keeps the legacy exact-plan-hash resume. Pinned by 13 unit cases plus `test/recovery-layer.test.ts`.
- `src/core/remediation/` — the doctor remediation library (`computeRemediationPlan`, `runRemediation`, shared by doctor, onboard and MCP `run_onboard`). `repairs.ts`: `planRepairSteps` previews every registered repair kind brain-wide and returns PROTECTED steps (`requires_user_agreement`, exact `command`, `paid`, `est_usd_cost`), independent of the score target; a `spends: 'llm'` kind's step is `paid` and adds `llm_usd` (its paid-model estimate) to `est_usd_cost`, which is null when either part is unpriced; `runRepairSteps` applies them in-process for a trusted local caller only, refusing a paid step whose estimate exceeds the remaining cap before it starts and continuing with free steps; `effect` kinds reserve their embedding estimate against the cap up front (the consumer embeds outside the tracker), and an `inline` step is marked `budget_exhausted` when the tracker fired even if a callee swallowed the throw. A `spends: 'llm'` step never reserves its model estimate: it runs under the caller's tracker (never a nested `stepBudget` tracker) with what is left of the cap as `maxLlmUsd`, then `charge`s the part of its actual `cost.llm_usd` that `spentUsd()` did not already rise by during the step, so model spend counts once; a step its allowance stopped is `budget_exhausted`. `planRepairSteps`, `runRepairSteps` and `runRemediation`'s `repairs.registry` take stub specs for tests. The plan hash includes the brain id so brains on one host never share a checkpoint file. `run.ts`: `opts.repairs` (doctor CLI only) plans the repair steps and runs them when `include` is set, even when the target is unreachable (job steps are skipped then); a remote `include` refuses `permission_denied`. The cap is cumulative (`spent_usd` from the checkpoint plus this run's tracker, reservations and settled model spend); a refused paid step ends the run budget-exhausted and saves the checkpoint. Callers that omit `repairs` keep the job-only behavior. `manual-only.ts`: `isManualOnlyStep` (by job name: `unify-types`, `extract-takes-from-pages`; fail-closed without `protected`) is the one predicate the onboard renderer's `manual_only` label, `runRemediation` and autopilot's targeted dispatch share; the runner never submits such a step, initially or after a recheck, keeps its cost out of the `--max-usd` estimate and returns `manual_only_skipped` entries (`code`, `why`, rendered `fix`: `gbrain jobs submit <job> [--params <json>] --follow`, actor `user`, `consent: ['paid']` when paid; `queued_jobs` names rows of that job still waiting or active). Plan and run preview through `planRepairStepsReport` (the strict `planRepairSteps` still propagates the first error), so a kind whose preview throws is reported as `repair_preview_failures` (code `timeout` for SQLSTATE 57014, naming `GBRAIN_STATEMENT_TIMEOUT`, else `preview_failed`; message redacted, then capped at 300 characters; `fix` is the rendered read-only `gbrain repair <kind>` preview) and the other kinds still plan and run. Each executed job step whose job moves a tracked metric (`impactMetricForJob`: embed / embed-catch-up: `countStaleChunks`; extract: `findOrphanPages` under the orphan-reporting policy, equal to get_health's orphan_pages without running `getHealth()`; extract-ner / extract-timeline-from-meetings: the onboard coverage checks' `visibleEntityCoverageSql`; extract-takes-from-pages: takes) records it before submit and once the job is terminal as one `migration_impact_log` row (`src/core/onboard/impact-capture.ts` `openStepImpact` / `closeStepImpact`, best-effort: a failed capture records null, a failed write only logs, and neither changes the step result), which `gbrain onboard --history` reads; dry runs, steps that throw before their job is terminal and unmapped jobs record nothing.