Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: pre-landing review fixes

garrytana43297e60c44
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d7cc84b6d2..d92f884578 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,7 @@ identifiers and attribution are available in the pre-removal Git revision
## [Unreleased]
-**A diverged managed checkout now publishes its local commits on refresh by default when Git durability is on, after a secret scan of those commits.** Commits a script made with plain `git commit` in a managed checkout used to strand there whenever the remote moved: every `gbrain sources refresh` refused `refresh_diverged` and asked the user, unless `sources.refresh_publish_diverged` was set to `true`. With the setting unset, a worktree whose Git durability is on (`gbrain sources writer git-durability <source> --enable`, or the legacy hook) now publishes them itself: the local commits merged with the upstream in memory, never a rebase or force-push, then the fast-forward. The publish child scans, under its publish lock and for the exact commits it pushes, what they add: commit messages, added lines (including a value a later commit removed) and merge-commit conflict resolutions. A value that looks like a secret, or added text too large to scan, refuses the new `publish_secret_found` naming the file, line, rule and commit, never the value, with nothing pushed. The rewritten-upstream refusal and the `--expect-remote` bound are unchanged. `false` turns it off; `true` still publishes with durability off.
+**A diverged managed checkout now publishes its local commits on refresh by default when Git durability is on, after a secret scan of those commits.** Commits a script made with plain `git commit` in a managed checkout used to strand there whenever the remote moved: every `gbrain sources refresh` refused `refresh_diverged` and asked the user, unless `sources.refresh_publish_diverged` was set to `true`. With the setting unset, a worktree whose Git durability is on (`gbrain sources writer git-durability <source> --enable`, or the legacy hook) now publishes them itself: the local commits merged with the upstream in memory, never a rebase or force-push, then the fast-forward. The publish child scans, under its publish lock and for the exact commits it pushes, what they add: commit messages, added lines (including a value a later commit removed) and merge-commit conflict resolutions. A value that looks like a secret, added text too large to scan, or a commit that changes `.gbrain-scan-allow` or a `.gitattributes` refuses the new `publish_secret_found` naming the file, line, rule and commit, never the value, with nothing pushed. The rewritten-upstream refusal and the `--expect-remote` bound are unchanged. `false` turns it off; `true` still publishes with durability off.
### Itemized changes
diff --git a/docs/guides/shared-checkout-git.md b/docs/guides/shared-checkout-git.md
index d04f0b7b6a..6f9e273c0e 100644
--- a/docs/guides/shared-checkout-git.md
+++ b/docs/guides/shared-checkout-git.md
@@ -185,7 +185,7 @@ Follow the error's `fix.next` as the
| `git_index_stale` | 1 | An `index.lock` older than 10 minutes blocks the checkout. Check that no git command is running there, tell the user, and remove the named lock file only with their agreement. |
| `git_operation_in_progress` | 1 | A rebase, merge, cherry-pick or revert is unfinished in the checkout. gbrain never aborts it for you. Follow [stuck rebase](#stuck-rebase). |
| `git_detached_head` | 1 | The checkout is on a detached HEAD or an unborn branch. Check out the branch the remote tracks (`git switch <branch>`), or make the first commit, then rerun. |
-| <a id="publish_secret_found"></a>`publish_secret_found` | 1 | Only with `--scan-secrets` (the publish `gbrain sources refresh` runs on its own): a commit about to be pushed adds a value that looks like a secret, in its message, an added line (even one a later commit removed) or a merge resolution, or adds text too large to scan; nothing was pushed. The message names each file, line, rule and commit, never the value. Ask the user: remove the value from those unpushed commits, or add a false positive's fingerprint to `.gbrain-scan-allow`, then retry. Never publish them with a command that skips the scan. |
+| <a id="publish_secret_found"></a>`publish_secret_found` | 1 | Only with `--scan-secrets` (the publish `gbrain sources refresh` runs on its own): a commit about to be pushed adds a value that looks like a secret, in its message, an added line (even one a later commit removed) or a merge resolution, adds text too large to scan, or changes `.gbrain-scan-allow` or a `.gitattributes` (files that decide what the scan sees); nothing was pushed. The message names each file, line, rule and commit, never the value. Ask the user: remove the value from those unpushed commits, or add a false positive's fingerprint to `.gbrain-scan-allow`, then retry. Never publish them with a command that skips the scan. |
| `integrate_conflict` | 1 | Local and remote commits changed the same file; nothing was merged and the checkout is unchanged. Stop, relay the question to the user, then [resolve](#conflicts). |
| `push_unconfirmed` | 4 | The commit is durable locally but no push destination confirmed it (network, credentials, branch protection, a remote that kept moving, or another push that held the push lock for the whole wait). Fix what the message names, then run `gbrain git commit-push --push-only`. Never force-push. |
| `writer_coordinator_required` | 1 | This is a managed worktree. Stop using git for pages; save them through `put_page` and the other gbrain write tools. |
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index d067fb30b6..3b8a4ac17c 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1488,7 +1488,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
{ since: '0.60.617.0', id: 'url_source_origin', text: 'New doctor check `url_source_origin` (warn) lists URL sources in the default clone directory with no record of who added them, which is every URL source added before that record existed: gbrain cannot tell whether a remote caller or the local operator added each one, so their reclones use the clone settings of a local add. `gbrain sources record-origin <id>... --remote|--local` records the answer the user gives, as a preview until `--apply`, on the brain host only. `--remote` gives the source the clone settings a remote add gets (git-lfs downloads and hooks off on every later reclone) and refuses a checkout that tracks git-lfs files or cannot be inspected. Nothing changes on upgrade.' },
{ since: '0.60.618.0', id: 'refresh_replan_own_commits_8828', text: 'A `gbrain sources refresh` whose checkout HEAD moved while it waited for writes admitted before its fence no longer refuses `refresh_source_changed` when every commit since its start is one of gbrain\'s own (in the commit ledger) and the upstream branch holds them: it takes HEAD as its start and fast-forwards to the upstream. A HEAD move that includes a commit gbrain did not make, or gbrain\'s commits the upstream does not hold yet, still refuses as before.' },
{ since: '0.60.619.0', id: 'managed_commit_push_unpublished_gst1923', text: 'On a managed checkout, `gbrain git commit-push -m <msg> -- <paths>` now reports how many commits the upstream lacks as last fetched (`unpublished` in `--json`, `null` when there is no upstream) and says nothing was pushed, instead of answering `nothing to commit; <remote>/<branch> at -` and logging `[push] ok-already-on-remote` for a commit that already existed. It writes no push-log line on a managed checkout. It still never pushes there; `gbrain git commit-push --push-only --keep-checkout --path <checkout>` publishes.' },
- { since: '0.60.616.0', id: 'refresh_publish_diverged_default_gbra386', text: 'A diverged managed checkout now publishes its local commits on `gbrain sources refresh` by default when the worktree\'s Git durability is on: `sources.refresh_publish_diverged` unset now means on there (it used to mean off), merged with the upstream in memory, never rebased or forced. Every automatic publish first scans what the local commits add (messages, added lines, merge resolutions) and refuses the new `publish_secret_found` naming the file, line and rule, never the value, when one looks like a secret; `gbrain git commit-push --push-only --scan-secrets` runs the same gate. Unset with durability not on still refuses, naming `gbrain sources writer git-durability <source> --enable`; `false` turns it off.' },
+ { since: '0.60.616.0', id: 'refresh_publish_diverged_default_gbra386', text: 'A diverged managed checkout now publishes its local commits on `gbrain sources refresh` by default when the worktree\'s Git durability is on: `sources.refresh_publish_diverged` unset now means on there (it used to mean off), merged with the upstream in memory, never rebased or forced. The refresh publish first scans what the local commits add (messages, added lines, merge resolutions) and refuses the new `publish_secret_found` naming the file, line and rule, never the value, when one looks like a secret; `gbrain git commit-push --push-only --scan-secrets` runs the same gate. Unset with durability not on still refuses, naming `gbrain sources writer git-durability <source> --enable`; `false` turns it off.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/git/commit-push.ts b/src/core/git/commit-push.ts
index 35b50ac703..f93ee83f32 100644
--- a/src/core/git/commit-push.ts
+++ b/src/core/git/commit-push.ts
@@ -1033,8 +1033,9 @@ function assertLocalCommitsInsideRoots(ctx: Ctx, target: string, roots: string[]
function assertNoSecretsInRange(ctx: Ctx, target: string): void {
const log = git(ctx.root, rangeScanArgs(ctx.trackingRef, target), { timeoutMs: 300_000, maxBuffer: 1024 * 1024 * 1024 });
const scan = log.code === 0 ? findingsInLog(ctx.root, log.stdout) : null;
- if (scan && !scan.findings.length && !scan.unscanned.length) return;
- const named = scan ? [...scan.findings.map(f => `${f.path}:${f.line} (${f.pattern}, ${f.commit.slice(0, 7)})`), ...scan.unscanned.map(u => `${u.path} (too large to scan, ${u.commit.slice(0, 7)})`)] : [];
+ if (scan && !scan.findings.length && !scan.unscanned.length && !scan.gate_changes.length) return;
+ const named = scan ? [...scan.findings.map(f => `${f.path}:${f.line} (${f.pattern}, ${f.commit.slice(0, 7)})`), ...scan.unscanned.map(u => `${u.path} (too large to scan, ${u.commit.slice(0, 7)})`),
+ ...scan.gate_changes.map(g => `${g.path} (changes what the scan sees, ${g.commit.slice(0, 7)})`)] : [];
throw opError('publish_secret_found', scan
? `${named.length} item(s) in the local commits ${ctx.trackingRef}..${target.slice(0, 12)} look like secrets or could not be scanned: ${named.slice(0, 10).join(', ')}${named.length > 10 ? ', ...' : ''}. Nothing was pushed.`
: `git could not list the local commits ${ctx.trackingRef}..${target.slice(0, 12)} to scan them for secrets (${log.stderr.trim().slice(0, 200) || `exit ${log.code}`}). Nothing was pushed.`,
diff --git a/src/core/git/range-secret-scan.ts b/src/core/git/range-secret-scan.ts
index 879f533495..c4c286dcd4 100644
--- a/src/core/git/range-secret-scan.ts
+++ b/src/core/git/range-secret-scan.ts
@@ -5,17 +5,19 @@
* commit removed is still in the pushed history), and for a merge commit the lines it adds that neither parent has
* (a conflict resolution). The workspace allowlist (`.gbrain-scan-allow`: path globs and fingerprints) applies.
* Findings carry the path, line, rule, fingerprint and commit, never the matched value. Binary changes carry no lines;
- * a file whose added text is over the scan cap cannot be scanned and is reported as such, so callers refuse.
+ * a file whose added text is over the scan cap cannot be scanned and is reported as such, so callers refuse. A commit
+ * in the range that changes a file deciding what the scan sees (`.gbrain-scan-allow`, a `.gitattributes`) is reported
+ * too, so callers refuse rather than let the published commits widen their own gate.
*/
-import { loadWorkspaceAllowlist, pathAllowlisted } from '../secret-scan-allowlist.ts';
+import { loadWorkspaceAllowlist, pathAllowlisted, SCAN_ALLOW_FILENAME } from '../secret-scan-allowlist.ts';
import { scanText, SCAN_MAX_FILE_BYTES } from '../secret-scan.ts';
export interface RangeSecretFinding { path: string; commit: string; line: number; pattern: string; fingerprint: string }
-export interface RangeScan { findings: RangeSecretFinding[]; unscanned: Array<{ path: string; commit: string }> }
+export interface RangeScan { findings: RangeSecretFinding[]; unscanned: Array<{ path: string; commit: string }>; gate_changes: Array<{ path: string; commit: string }> }
/** The `git log` arguments whose output findingsInLog reads. */
export function rangeScanArgs(base: string, head: string): string[] {
- return ['-c', 'core.quotePath=false', 'log', '--no-renames', '--no-ext-diff', '--no-textconv', '--no-color', '--diff-merges=dense-combined',
+ return ['-c', 'core.quotePath=false', '-c', 'core.attributesFile=/dev/null', 'log', '--no-renames', '--no-ext-diff', '--no-textconv', '--no-color', '--diff-merges=dense-combined',
'--unified=0', '-p', '--format=commit %H%n%B%n%x00', `${base}..${head}`, '--'];
}
@@ -24,8 +26,8 @@ export const MESSAGE_PATH = '(commit message)';
export function findingsInLog(root: string, log: string): RangeScan {
const allowlist = loadWorkspaceAllowlist(root);
- const scan: RangeScan = { findings: [], unscanned: [] };
- let commit = '', path: string | null = null, next = 0, combined = false, inMessage = false;
+ const scan: RangeScan = { findings: [], unscanned: [], gate_changes: [] };
+ let commit = '', path: string | null = null, next = 0, combined = false, inMessage = false, header = false;
let added: string[] = [], at: number[] = [];
const flush = () => {
if (path && added.length && (path === MESSAGE_PATH || !pathAllowlisted(path, allowlist))) {
@@ -42,10 +44,19 @@ export function findingsInLog(root: string, log: string): RangeScan {
continue;
}
if (line.startsWith('commit ') && /^commit [0-9a-f]{40,64}$/.test(line)) { flush(); commit = line.slice(7); path = MESSAGE_PATH; inMessage = true; continue; }
- if (line.startsWith('diff --git ') || line.startsWith('diff --cc ') || line.startsWith('diff --combined ')) { flush(); path = null; combined = !line.startsWith('diff --git '); continue; }
- if (line.startsWith('+++ ')) { path = line === '+++ /dev/null' ? null : line.slice(4).replace(/^"?b\//, '').replace(/"$/, ''); continue; }
+ if (line.startsWith('diff --git ') || line.startsWith('diff --cc ') || line.startsWith('diff --combined ')) {
+ flush(); path = null; header = true; combined = !line.startsWith('diff --git ');
+ const named = (combined ? line.replace(/^diff --(?:cc|combined) /, '') : line.replace(/^diff --git "?a\/.*? "?b\//, '')).replace(/"$/, '');
+ if (named === SCAN_ALLOW_FILENAME || named === '.gitattributes' || named.endsWith('/.gitattributes')) scan.gate_changes.push({ path: named, commit });
+ continue;
+ }
const hunk = /^@@@? -\d+(?:,\d+)? (?:-\d+(?:,\d+)? )?\+(\d+)(?:,\d+)? @@@?/.exec(line);
- if (hunk) { next = Number(hunk[1]); continue; }
+ if (hunk) { header = false; next = Number(hunk[1]); continue; }
+ if (header) {
+ if (!line.startsWith('+++ ')) continue;
+ path = line === '+++ /dev/null' ? null : line.slice(4).replace(/^"?b\//, '').replace(/"$/, '');
+ continue;
+ }
if (!path) continue;
if (combined) {
const prefix = line.slice(0, 2);
diff --git a/test/guarded-publish.test.ts b/test/guarded-publish.test.ts
index 8a10d77f29..5ffa3e74a8 100644
--- a/test/guarded-publish.test.ts
+++ b/test/guarded-publish.test.ts
@@ -3,8 +3,10 @@
* `gbrain sources refresh` runs in its child): it scans, under the publish lock, what the commits it is about to push add.
* Protects: a value that looks like a secret is found in an added line even when a later commit removed it, in a
* commit message, and in a merge commit's conflict resolution, named by path, line, rule and commit but never by
- * value, and nothing is pushed; a fingerprint in .gbrain-scan-allow and a binary file are skipped; a clean range
- * publishes; without scanSecrets nothing changes.
+ * value, and nothing is pushed; an added line whose text starts with `++` (printed `+++`, like a file header) is
+ * still scanned; added text over the scan cap, and a commit in the range that changes .gbrain-scan-allow or a
+ * .gitattributes, refuse; a fingerprint in the working tree's .gbrain-scan-allow and a binary file are skipped; a clean
+ * range publishes; without scanSecrets nothing changes.
* Seams: none; real repositories and a bare remote.
*/
import { afterAll, expect, test } from 'bun:test';
@@ -13,6 +15,7 @@ import { mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { commitAndPush } from '../src/core/git/commit-push.ts';
+import { SCAN_MAX_FILE_BYTES } from '../src/core/secret-scan.ts';
const dir = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-guarded-publish-')));
afterAll(() => rmSync(dir, { recursive: true, force: true }));
@@ -89,3 +92,39 @@ test('an allowlisted fingerprint and a binary file are skipped and the clean ran
commit(plain.root, 'notes.md', `Deploy token ${token}\n`);
expect(publish(plain.root, false)).toMatchObject({ outcome: 'pushed' });
});
+
+
+test('an added line whose text starts with ++ is scanned, not read as a file header', () => {
+ const r = repo('plusplus');
+ commit(r.root, 'notes.md', `intro\n++ ${token}\nafter\n`);
+ const error = refused(() => publish(r.root));
+ expect(error.code).toBe('publish_secret_found');
+ expect(error.message).toContain('notes.md:2 (');
+ expect(remoteTip(r)).toBe(r.base);
+});
+
+test('added text over the scan cap refuses as unscanned', () => {
+ const r = repo('big');
+ commit(r.root, 'big.md', `${'x'.repeat(SCAN_MAX_FILE_BYTES + 10)}\n`);
+ const error = refused(() => publish(r.root));
+ expect(error.code).toBe('publish_secret_found');
+ expect(error.message).toContain('big.md (too large to scan, ');
+ expect(remoteTip(r)).toBe(r.base);
+});
+
+test('an unpublished commit that widens .gbrain-scan-allow or turns a file binary with .gitattributes refuses', () => {
+ const r = repo('gate');
+ commit(r.root, '.gbrain-scan-allow', '**\n');
+ commit(r.root, 'notes.md', `Deploy token ${token}\n`);
+ const allow = refused(() => publish(r.root));
+ expect(allow.code).toBe('publish_secret_found');
+ expect(allow.message).toContain('.gbrain-scan-allow (changes what the scan sees, ');
+ expect(remoteTip(r)).toBe(r.base);
+
+ const attrs = repo('attrs');
+ commit(attrs.root, '.gitattributes', '* -diff\n');
+ commit(attrs.root, 'notes.md', `Deploy token ${token}\n`);
+ const hidden = refused(() => publish(attrs.root));
+ expect(hidden.message).toContain('.gitattributes (changes what the scan sees, ');
+ expect(remoteTip(attrs)).toBe(attrs.base);
+});