Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(q1): gate findings: emits order, cron check only on gbrain crontabs, gitChildEnv, const name

garrytand77b2a37c2ad
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a8f7ecab93..9bca00b699 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,7 +28,7 @@ identifiers and attribution are available in the pre-removal Git revision
- A managed commit is not on the remote until the owner's next Git publish. `gbrain sources writer status --json` (`checkouts`) shows it as ahead until then.
- `--pending`, `--branch`, `--resolve`, `--expect-remote` and `--allow-outside-source-root` are refused in a managed worktree (`commit_paths_invalid`), and `--push-only` without `--keep-checkout` still refuses `writer_coordinator_required`.
-- doctor now runs `crontab -l` once (read-only, 5 s). A host without a crontab gets no `cron_gbrain_home` result.
+- doctor now runs `crontab -l` once (read-only, 5 s). A host whose crontab never mentions gbrain gets no `cron_gbrain_home` result.
- Nothing is rewritten on upgrade, and there is no schema migration.
### For contributors
diff --git a/src/commands/doctor/checks/cron-gbrain-home.ts b/src/commands/doctor/checks/cron-gbrain-home.ts
index 7115946201..8db9b04229 100644
--- a/src/commands/doctor/checks/cron-gbrain-home.ts
+++ b/src/commands/doctor/checks/cron-gbrain-home.ts
@@ -4,7 +4,7 @@
* a checkout, or the `gbrain` binary while this brain is under a non-default GBRAIN_HOME, reads and writes
* `~/.gbrain` instead: a second, silent brain state (locks, queue, config) beside the real one.
*
- * Read-only and bounded: `crontab -l` (5 s; no crontab, no check), plus at most 20 scripts the lines name (64 KiB
+ * Read-only and bounded: `crontab -l` (5 s; no check unless the crontab mentions gbrain or src/cli.ts), plus at most 20 scripts the lines name (64 KiB
* each). A line or script counts as setting the home when it assigns GBRAIN_HOME (on the line, in the crontab's
* environment lines, or anywhere in the script). Warns, never fails; the user edits the crontab.
*/
@@ -18,7 +18,7 @@ import { doctorVerify } from '../check-fix.ts';
const SCRIPTS_MAX = 20;
const SCRIPT_BYTES_MAX = 64 * 1024;
const DIRECT_CLI = /(?:^|[\s;&|(=])(?:\S*\/)?src\/cli\.ts\b/;
-const GBRAIN_BINARY = /(?:^|[\s;&|(\/])gbrain\s+[a-z]/;
+const BINARY_CALL = /(?:^|[\s;&|(\/])gbrain\s+[a-z]/;
const SETS_HOME = /(?:^|[\s;&|(])(?:export\s+)?GBRAIN_HOME=/;
export interface CronGbrainHomeFinding { where: string; command: string; reason: 'direct_cli' | 'default_home' }
@@ -55,7 +55,7 @@ function scriptOf(command: string): string | null {
function classify(command: string, homeSet: boolean, customHome: boolean): CronGbrainHomeFinding['reason'] | null {
if (homeSet || SETS_HOME.test(command)) return null;
if (DIRECT_CLI.test(command)) return 'direct_cli';
- return customHome && GBRAIN_BINARY.test(command) ? 'default_home' : null;
+ return customHome && BINARY_CALL.test(command) ? 'default_home' : null;
}
/** Pure: the findings for one crontab text. `customHome` is this process's GBRAIN_HOME when it is set. */
@@ -84,7 +84,7 @@ export function cronGbrainHomeFindings(crontab: string, customHome: string | und
}
export function cronGbrainHomeCheck(crontab: string | null, customHome: string | undefined, read?: (path: string) => string | null): Check | null {
- if (crontab === null) return null;
+ if (crontab === null || !/gbrain|src\/cli\.ts/.test(crontab)) return null;
const findings = cronGbrainHomeFindings(crontab, customHome, read);
if (!findings.length) return { name: 'cron_gbrain_home', status: 'ok', message: 'No cron line or script it runs calls gbrain without its GBRAIN_HOME.', details: { findings } };
const home = customHome ? `GBRAIN_HOME=${customHome}` : 'GBRAIN_HOME set to the home this brain uses';
diff --git a/src/commands/doctor/checks/local-audits.ts b/src/commands/doctor/checks/local-audits.ts
index 994e363460..4db3c1aa2b 100644
--- a/src/commands/doctor/checks/local-audits.ts
+++ b/src/commands/doctor/checks/local-audits.ts
@@ -515,7 +515,7 @@ async function runHomeDirInWorktree(ctx: DoctorContext): Promise<Check[]> {
export const homeDirInWorktreeEntry: DoctorEntry = {
name: 'home_dir_in_worktree',
- emits: ['home_dir_in_worktree', 'npm_squat', 'pglite_leftovers', 'graduation_interrupted', 'cron_gbrain_home'],
+ emits: ['home_dir_in_worktree', 'npm_squat', 'pglite_leftovers', 'cron_gbrain_home', 'graduation_interrupted'],
run: runHomeDirInWorktree,
};
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 1463ffcd85..b3b9f13ec1 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1228,7 +1228,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
{ since: 'TBD', text: 'The `refresh_diverged` refusal of `gbrain sources refresh` now counts the checkout\'s local commits and its fix is `gbrain git commit-push --push-only --keep-checkout --path <root> --json` (actor user, ask_user): it merges the upstream in memory, refuses on a conflict, pushes the merge and never moves the checkout, after which the refresh fast-forwards. It used to suggest reconciling in a separate clone or recloning, which drops the local commits.' },
{ since: 'TBD', text: '`gbrain sources writer status --json` has a new `checkouts` block: each canonical checkout on this host against its last fetched upstream (no fetch), with branch, upstream, ahead, behind, `diverged`, each commit the upstream lacks (sha, author, date, subject, paths; at most 20) and porcelain counts (staged, modified, untracked, conflicted). doctor `git_convergence` adds the same commits and counts for a managed canonical checkout, and now warns at once when one has diverged from its upstream (it blocks `gbrain sources refresh`), with the keep-checkout publish as an ask-user fix.' },
{ since: 'TBD', text: 'New `gbrain sources set-sync <id> <on|off>` sets `config.syncEnabled`, which includes or excludes a source from automatic sync (`sync --all`, autopilot\'s freshness dispatcher and the full-cycle fan-out; an explicit `gbrain sync --source <id>` runs either way). `--dry-run` prints the current state and the apply command, which carries `--expect <current>`; an apply whose expectation no longer holds refuses `preview_changed` and changes nothing. Setting it used to need a raw write to `sources.config`.' },
- { since: 'TBD', text: 'New doctor check `cron_gbrain_home` reads `crontab -l` (read-only, 5 s; no crontab, no check) and the scripts its lines run (at most 20, 64 KiB each) and warns on each command that runs `src/cli.ts` directly without GBRAIN_HOME, or the `gbrain` binary without GBRAIN_HOME while this brain runs under one: cron passes no GBRAIN_HOME, so those commands act on `~/.gbrain` instead of this brain. A GBRAIN_HOME assignment on the line, in the crontab\'s environment lines or in the script counts. The fix asks the user to edit the crontab.' },
+ { since: 'TBD', text: 'New doctor check `cron_gbrain_home` reads `crontab -l` (read-only, 5 s; no result unless the crontab mentions gbrain or src/cli.ts) and the scripts its lines run (at most 20, 64 KiB each) and warns on each command that runs `src/cli.ts` directly without GBRAIN_HOME, or the `gbrain` binary without GBRAIN_HOME while this brain runs under one: cron passes no GBRAIN_HOME, so those commands act on `~/.gbrain` instead of this brain. A GBRAIN_HOME assignment on the line, in the crontab\'s environment lines or in the script counts. The fix asks the user to edit the crontab.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/persistence/checkout-state.ts b/src/core/persistence/checkout-state.ts
index 2506636637..9cfb0773b9 100644
--- a/src/core/persistence/checkout-state.ts
+++ b/src/core/persistence/checkout-state.ts
@@ -8,6 +8,7 @@
* Every git call is time-bounded and read-only; the comparison is with the last fetched upstream.
*/
import { execFileSync } from '../spawn.ts';
+import { gitChildEnv } from '../git-env.ts';
const GIT_TIMEOUT_MS = 5_000;
export const CHECKOUT_COMMITS_MAX = 20;
@@ -21,7 +22,7 @@ export interface CheckoutState {
}
function git(root: string, args: string[]): string | null {
- try { return execFileSync('git', ['-C', root, ...args], { stdio: ['ignore', 'pipe', 'ignore'], timeout: GIT_TIMEOUT_MS }).toString(); }
+ try { return execFileSync('git', ['-C', root, ...args], { stdio: ['ignore', 'pipe', 'ignore'], timeout: GIT_TIMEOUT_MS, env: gitChildEnv() }).toString(); }
catch { return null; }
}
diff --git a/test/doctor-cron-gbrain-home.test.ts b/test/doctor-cron-gbrain-home.test.ts
index 64a118290c..555ea414c1 100644
--- a/test/doctor-cron-gbrain-home.test.ts
+++ b/test/doctor-cron-gbrain-home.test.ts
@@ -33,8 +33,9 @@ test('the gbrain binary without GBRAIN_HOME is flagged only while this brain run
expect(cronGbrainHomeFindings(`GBRAIN_HOME=/srv/brain-a\n${crontab}0 * * * * /opt/example/run-sync.sh\n`, '/srv/brain-a', read)).toEqual([]);
});
-test('the check: no crontab emits nothing, a clean one is ok, findings warn with a user fix naming the home', () => {
+test('the check: no crontab, or one that never mentions gbrain, emits nothing; a clean one is ok; findings warn with a user fix naming the home', () => {
expect(cronGbrainHomeCheck(null, undefined, read)).toBeNull();
+ expect(cronGbrainHomeCheck('0 3 * * * /usr/bin/backup --nightly\n', '/srv/brain-a', read)).toBeNull();
expect(cronGbrainHomeCheck('0 * * * * gbrain sync --all\n', undefined, read)).toMatchObject({ name: 'cron_gbrain_home', status: 'ok' });
const check = cronGbrainHomeCheck('0 * * * * gbrain sync --all\n', '/srv/brain-a', read)!;
expect(check).toMatchObject({ name: 'cron_gbrain_home', status: 'warn', details: { gbrain_home: '/srv/brain-a' },