Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
4.2 KB
Raw Blame

Key files: Engines (1/2, continued)

Subsystem index · Previous entries. Read only the entries relevant to your change. Current behavior and load-bearing invariants; history belongs in Git and CHANGELOG.

  • src/core/pglite-legacy-lock.ts — core of gbrain pglite-repair --legacy-lock (C-3): read-only inspectLegacyLock (verdict, plan hash over the legacy dir, advisory this-host-only evidence: kill -0, /proc fd scan, statfs; refuses live_holder/network_filesystem/filesystem_unverified, the last when locality is unknown: neither Linux statfs nor macOS df -P + mount (local flag and a known-local type) confirms a local disk, or a probe failed) and applyLegacyLockRemoval (moves the dir aside with a receipt, backs up the prior ownership marker bytes, writes the kernel-v1 marker; the restore command puts the marker back). Test: test/pathological/c3-pglite-legacy-lock.test.ts.
  • src/core/pglite-repair.ts — WAL-repair orchestrator wrapping the resetWal port with the safety layers that make it runnable automatically from connect(): validateWalRepairTarget (read-only, fail-closed; refuses symlinked dataDir/pg_wal/global/pg_control — lstat follows INTERMEDIATE symlinks, so global/ itself must be checked or surgery would write pg_control through it into a foreign dir; tolerates the in-dir .gbrain-lock), rename-based backup (the ENTIRE pg_wal/ dir + postmaster.pid renamed into a sibling <dataDir>.wal-repair-backup-<ts>/, only the 8KB pg_control copied — zero transient disk cost), restoreWalBackup (overwrite order: control first via atomic tmp+rename, then a pg_wal dir swap with the reset dir set ASIDE inside the backup — nothing is ever deleted during restore; mtime guard refuses when a foreign segment is newer than the backup; a missing/empty backup NEVER reports restored:true), WalRepairError (thrown when resetWal fails AFTER the backup — carries the receipt + the best-effort restore's REAL result so the seam's restored flag and the failed-restored/failed-not-restored message arms never lie), a cooldown sidecar <dataDir>.wal-repair-attempt.json (skip 'recently-failed' inside GBRAIN_PGLITE_WAL_REPAIR_COOLDOWN_SECONDS, default 3600 — bounds the autopilot/supervisor reconnect loops) with episode-scoped backups (attempts within one corruption episode REUSE the episode's first backup — the pre-damage forensic state, honored only when the sidecar's path is a real non-symlink <dataDir>.wal-repair-backup-* sibling since the sidecar is user-writable JSON; retention keeps the newest 3 episodes, never pruning the open episode's), and attemptWalRepairAndRetry — the engine seam that NEVER throws (gates: kill-switch → reaped-lock 'possibly-live-writer' → 10-minute reap-marker quarantine (msSinceLastReap, cross-process) → validation → cooldown; then repair → retry create once → restore-and-record on failure; prints a repair-start stderr line so a timeout-killed attempt is self-explaining). inspectPgliteDataDir is the read-only diagnosis for gbrain doctor + pglite-repair --dry-run. Imports runtime values only from pglite-lock.ts/pglite-resetwal.ts/node:fs — never from pglite-engine.ts (no cycle; the engine statically imports THIS file per the engine-live rule). Pinned by test/pglite-repair.test.ts + test/pglite-wal-repair.serial.test.ts.
  • src/core/pglite-repair-consent.ts + the repair-failed marker in src/core/pglite-repair.ts — when automatic WAL repair fails (or a recent failure put it on cooldown) recordFailedAutoRepair writes the sibling <dataDir>.repair-failed.json; while it exists PGLiteEngine.connect() refuses before acquireLock (zero writes to the data dir) with repairFailedRefusal: the exit-3 consent payload (effects destructive, fix gbrain pglite-repair [--path] --yes --expect <plan_hash> from the same walRepairPlan the command binds, preview --dry-run --json, and the hands-off text HANDS_OFF_BRAIN_FILES). The CLI's exitOnRepairFailed (src/cli/cli-error.ts) exits 3 on it from connectEngine; stdio serve maps it to status reason repair_failed. Cleared by a completed pglite-repair or reinit-pglite. Pinned by test/agent-operator-gate-fixes.{,serial.}test.ts.