Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

persistence: scan before a conflict and keep --scan-secrets in every managed publish fix

garrytaneb515a8452db
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 52a3c59b23..6a5a9e3229 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,9 +17,9 @@ identifiers and attribution are available in the pre-removal Git revision
### Itemized changes
- `src/core/persistence/effect-git.ts`: `pushGitRoot` reads the remote branch tip with `git ls-remote`, scans `<tip>..<HEAD sha>` and pushes that sha only as a fast-forward of the tip, leased on it; a tip that is not an ancestor, or a remote that moves before the push lands (`stale info`), goes to the keep-checkout publish, which now runs with `--scan-secrets`. The in-process scan gets 10 s; a range it cannot finish goes to the keep-checkout publish. A remote without the branch gets HEAD's whole history scanned and the branch created with a lease that it does not exist; one too large to scan in process holds with `publish_scan_unreadable` naming `git ls-remote --heads`. A refusal from either path holds the effects as `publish_secret_found` with the user's fix.
-- `src/core/git/commit-push.ts`: a `scanSecrets` publish scans `<base>..<candidate>` before every push and leases the push on that base, so each merge with a newly fetched remote tip is scanned again.
-- `src/core/git/range-secret-scan.ts`: `rangeSecretRefusal`, the one refusal both callers throw (`publish_scan_unreadable` for a scan that could not read the range, `publish_secret_found` otherwise; a gate-file change says the user pushes it). `scanRange` marks a scan that ran past its budget (`timed_out`) and scans a whole history when the base is empty. It skips commits the commit ledger records as page writes (`effect`, `door`, `hosted_repo`); `gbrain git commit-push` commits, adopted outside edits and commits the ledger no longer holds are still scanned, and an unreadable ledger means everything is scanned. A Git effect now makes its commit by plumbing (`commit-tree` over its staged paths, then `update-ref HEAD <new> <old>`, which refuses when HEAD moved), so the ledger records a sha gbrain created before any ref names it. The scan skips an effect commit only by that sha, recorded `verified` when every staged file holds the bytes whose sha256 the effect recorded (the page walk passes the hash it checked too). An amend, reset or reword of it is a different commit and is scanned, a page file changed before it was staged leaves it unverified, and records written before this check existed are scanned. Its time budget is also checked while the diff is parsed.
-- `src/commands/doctor/checks/git-convergence.ts`: the agent push fixes for a stranded managed checkout and after committing its modified pages run `gbrain git commit-push --push-only --keep-checkout --scan-secrets`, so a script's commit is scanned there too.
+- `src/core/git/commit-push.ts`: a `scanSecrets` publish scans `<base>..<candidate>` before every push and leases the push on that base, so each merge with a newly fetched remote tip is scanned again. It also scans `<remote tip>..<candidate>` before merging with a newly fetched tip, so a remote rewound past a pushed secret that also changed a file this machine changed refuses `publish_secret_found` rather than `integrate_conflict`, and a conflict's resolve command keeps `--scan-secrets`.
+- `src/core/git/range-secret-scan.ts`: `rangeSecretRefusal`, the one refusal both callers throw (`publish_scan_unreadable` for a scan that could not read the range, `publish_secret_found` otherwise; a gate-file change says the user pushes it). `scanRange` marks a scan that ran past its budget (`timed_out`) and scans a whole history when the base is empty. It skips commits the commit ledger records as page writes (`effect`, `door`, `hosted_repo`); `gbrain git commit-push` commits, adopted outside edits and commits the ledger no longer holds are still scanned, and an unreadable ledger means everything is scanned. A Git effect now makes its commit by plumbing (`commit-tree` over its staged paths, then `update-ref HEAD <new> <old>`, which refuses when HEAD moved, then `git maintenance run --auto --detach` as `git commit` does, without `--detach` on git before 2.47), so the ledger records a sha gbrain created before any ref names it. The scan skips an effect commit only by that sha, recorded `verified` when every staged file holds the bytes whose sha256 the effect recorded (the page walk passes the hash it checked too). An amend, reset or reword of it is a different commit and is scanned, a page file changed before it was staged leaves it unverified, and records written before this check existed are scanned. Its time budget is also checked while the diff is parsed.
+- `src/commands/doctor/checks/git-convergence.ts`, `src/core/persistence/worktree-refresh.ts`, `src/core/persistence/git-push-holds.ts`: every publish fix they name for a managed checkout (stranded, after committing its modified pages, diverged, `refresh_diverged`, and a held Git effect's conflict resolve) runs `gbrain git commit-push --push-only --keep-checkout --scan-secrets`, so a script's commit is scanned there too.
- `src/core/persistence/git-push-holds.ts`: `publish_secret_found` and `publish_scan_unreadable` are held push codes, so doctor, `gbrain sync status` and `retry-effects --parked-pushes` cover them, with an ask-the-user fix (the refused commits, or `git fsck`).
- Docs: `docs/guides/shared-checkout-git.md` (`publish_secret_found` and `publish_scan_unreadable` rows), `docs/guides/live-sync.md`.
- Tests: `test/effect-git-secret-gate.test.ts` (an ahead checkout, a diverged one, a remote rewound past a pushed secret, a `.gitattributes` change and a remote without the branch whose history holds a secret each refuse with nothing pushed; a clean ahead checkout pushes, a clean history creates a missing branch, and a scan past the in-process budget still scans in the child; through the effect worker the page write's effect holds, sync status names the fix, and an allowlisted false positive releases it, parked or not; a page write whose body holds a credential-shaped value still backs up; a script commit, amend or reword racing the effect commit, a page file changed after the effect checked it, and an unverified ledger record each hold with nothing pushed; a source-scan walk's page with a credential-shaped body still backs up; a repaired unreadable commit releases its hold); `test/persistence-git-removal-push-7089.test.ts` and `test/persistence-git-subdir-root-7091.test.ts` (doctor's push fixes scan); `test/guarded-publish.test.ts` (a remote rewound past a pushed secret is not published again); `test/git-literal-pathspec-hooks-7759.test.ts` publishes its probe `.gitattributes` commit first, since the effect's push now refuses one; `test/cli-flag-equals.test.ts` lists `range-secret-scan.ts` as a git child argv reader.
diff --git a/docs/guides/shared-checkout-git.md b/docs/guides/shared-checkout-git.md
index 985ac01a71..5c2167b96e 100644
--- a/docs/guides/shared-checkout-git.md
+++ b/docs/guides/shared-checkout-git.md
@@ -290,7 +290,7 @@ commits come in. Four rules keep that checkout able to move:
`git log --format='%h %an %ad %s' @{upstream}..HEAD` lists the stray
commits. Fix that writer first, then, after the user agrees, publish the
stray commits merged with the upstream
- (`gbrain git commit-push --push-only --keep-checkout --path <checkout>`)
+ (`gbrain git commit-push --push-only --keep-checkout --scan-secrets --path <checkout>`)
and retry the refresh, as the [refusal](write-refusals.md#refresh_diverged)
says. Never force the checkout. When local commits there are expected
(automations committing alongside a sibling checkout that pushes the same
@@ -328,6 +328,9 @@ untouched and nothing is pushed. The error names the files, the local and
remote commits (in its `detail`), and a question to relay to the user. Until it
is resolved, every later commit-push from this checkout hits the same
conflict, even for other files, because the unpushed commit is still waiting.
+A conflict reported by a scanned publish (a Git effect's push, a refresh
+publish) comes only after the secret scan of what the local commits add to the
+remote's current tip passed, and its resolve command keeps `--scan-secrets`.
One kind of conflict settles itself on push. When the remote's version of a
file is one this checkout already had at that path and later replaced with
diff --git a/docs/guides/troubleshooting.md b/docs/guides/troubleshooting.md
index bbf41baa20..b3f311b09b 100644
--- a/docs/guides/troubleshooting.md
+++ b/docs/guides/troubleshooting.md
@@ -31,7 +31,7 @@
| A command exited 3 (`confirmation_required`) | relay `user_message`; run `fix.command` only after the user agrees | agent, after the user agrees | the payload's `effects` | the fix's `verify` |
| A scheduled job, workspace script or hook finds nothing in the brain (every lookup empty), or its log shows `no_brain` or `GBRAIN_DB_ACCESS no_url`, while the same command works by hand | the job runs without the brain's config: call gbrain through one wrapper that sets `GBRAIN_HOME` and loads the brain's env file, and stop discarding its stderr ([the wrapper](cron-schedule.md#calling-gbrain-unattended)); `gbrain doctor --only cron_gbrain_home --json` names crontab lines that skip it | agent | none | the job's log shows results, and `gbrain engine status --json` run through the wrapper names the expected `db_url_source` |
| `gbrain sources refresh` refuses `refresh_not_owner` from cron or a script but works by hand | the caller resolves another gbrain home, so it is another host; the refusal names the identity file, `HOME` and `GBRAIN_HOME` it read. Set the owner's `GBRAIN_HOME` in the wrapper (not `HOME`, which also swaps git credentials); never copy or delete a `host.json` | agent | none | `gbrain sources refresh <id> --dry-run` through the wrapper |
-| `gbrain sources refresh` refuses `refresh_diverged` on every run | something commits into the managed checkout outside gbrain, usually a script that writes files and runs `git commit` without pushing. `git log @{upstream}..HEAD` names the commits; move that writer to `gbrain git commit-push -- <paths>`, then publish the stray commits merged with the upstream (`gbrain git commit-push --push-only --keep-checkout --path <checkout>`) and retry, as the [refusal](write-refusals.md#refresh_diverged) says; never force ([scripts and the managed checkout](shared-checkout-git.md#scripts-and-the-managed-checkout)). With the worktree's Git durability on, each refresh publishes them itself unless `sources.refresh_publish_diverged` is `false` (the refusal names a durability that is not on, or a value that looks like a secret in the local commits) ([live sync](live-sync.md)) | agent; reconciling after the user agrees | `destructive` when reconciling drops local commits | `gbrain sources refresh <id> --dry-run` |
+| `gbrain sources refresh` refuses `refresh_diverged` on every run | something commits into the managed checkout outside gbrain, usually a script that writes files and runs `git commit` without pushing. `git log @{upstream}..HEAD` names the commits; move that writer to `gbrain git commit-push -- <paths>`, then publish the stray commits merged with the upstream (`gbrain git commit-push --push-only --keep-checkout --scan-secrets --path <checkout>`) and retry, as the [refusal](write-refusals.md#refresh_diverged) says; never force ([scripts and the managed checkout](shared-checkout-git.md#scripts-and-the-managed-checkout)). With the worktree's Git durability on, each refresh publishes them itself unless `sources.refresh_publish_diverged` is `false` (the refusal names a durability that is not on, or a value that looks like a secret in the local commits) ([live sync](live-sync.md)) | agent; reconciling after the user agrees | `destructive` when reconciling drops local commits | `gbrain sources refresh <id> --dry-run` |
| A `--json` caller fails to parse gbrain's output | something other than gbrain wrote to stdout, usually a wrapper or a hook it runs; send that output to a log file or stderr (gbrain's own notices already go to stderr) | agent | none | the same call piped to `jq .` parses |
| gbrain calls time out from a script or language runtime but finish by hand, or each search takes several seconds | a cold CLI start takes 3–6 s on a pooled Postgres brain: set caller timeouts of at least 10 s in both the shell and the runtime (a runtime exec timeout can override the shell's), and turn on the [warm CLI socket](cli-socket.md). If warm searches stay slow, measure the round trip to the database; one in another region dominates search time | agent; moving the database after the user agrees | none | `GBRAIN_DEBUG=1 gbrain search <query>` prints a `[cli-socket] warm:` line |
| After a database change some processes still use the old server, or a URL variable silently keeps its old value | a shell-sourced env file has an unquoted `&` (the assignment ends there), or a process kept the environment it started with. Quote every value, keep Postgres URLs to `?sslmode=require` (driver options as variables, such as `GBRAIN_PREPARE=false`), update every URL variable the tooling reads, restart every gbrain process, and prove the switch from the database side ([switching a database](../../INSTALL_FOR_AGENTS.md#switching-a-database)). Never paste `env` output into a transcript while debugging | agent, after the user agrees | none | a row written after the restart appears on the new server |
diff --git a/docs/guides/write-refusals.md b/docs/guides/write-refusals.md
index cc48ea8298..e373b202c8 100644
--- a/docs/guides/write-refusals.md
+++ b/docs/guides/write-refusals.md
@@ -564,7 +564,7 @@ this anchor (`--json` puts them in one object on stdout); the command exits 1.
| <a id="refresh_no_upstream"></a>`refresh_no_upstream` | `refresh_no_upstream` | The checkout's branch tracks no upstream, HEAD is detached, or the checkout is not a Git checkout at all (gbrain never initializes Git on its own), so there is nothing to fast-forward to. | `git -C <checkout> branch --set-upstream-to <remote>/<branch>` (the fix fills in the real values), then retry. A checkout that is not Git has nothing to refresh; making it a clone with an upstream is the user's call. |
| <a id="fetch_failed"></a>`fetch_failed` | `fetch_failed` | `git fetch` failed or did not finish within `sources.refresh_fetch_timeout_ms` (default 120000; `GBRAIN_REFRESH_FETCH_TIMEOUT_MS`; `--fetch-timeout-ms` wins over both). Nothing changed and no refresh record was written. | Retry. For a slow remote, raise the bound with `--fetch-timeout-ms <ms>` or `gbrain config set sources.refresh_fetch_timeout_ms <ms>`. Credentials are the user's: ask them to fix authentication rather than changing the remote. |
| <a id="refresh_git_unavailable"></a>`git_unavailable` | `git_unavailable` | A git command that reads the checkout's branch or remote (`symbolic-ref`, `remote`, `config --get branch.<branch>.remote` or `.merge`) failed or did not finish, so the checkout's state is unknown. The message names the command and git's first error line. A detached HEAD or an unset key is not this refusal. Nothing changed and no refresh record was written. | Run `git -C <checkout> status` to see why git cannot read the checkout, repair what it reports, then retry `gbrain sources refresh <source>`. |
-| <a id="refresh_diverged"></a>`refresh_diverged` | `refresh_diverged` | The checkout has commits its upstream lacks, so a fast-forward is impossible. Nothing changed. | Ask the user, then publish the local commits merged with the upstream: `gbrain git commit-push --push-only --keep-checkout --path <checkout>` merges in memory, pushes the merge, refuses on a conflicting file and never moves the checkout (the refusal's fix fills in the real path and counts the local commits). Then retry `gbrain sources refresh <source>`, which fast-forwards to the merge. `gbrain sources reclone <source>` replaces the checkout instead and drops the local commits. With `sources.refresh_publish_diverged` unset on a worktree whose Git durability is on, or set to `true`, the refresh runs that publish itself, in a child process that skips the pre-push hook, and then fast-forwards; that child first scans what the local commits add (messages, added lines, merge resolutions) and refuses [`publish_secret_found`](shared-checkout-git.md#publish_secret_found) naming the file, line and rule (never the value) when one looks like a secret, and unset with durability not on refuses naming `gbrain sources writer git-durability <source> --enable`; a conflicting file then refuses `integrate_conflict` (with `gbrain.quarantine` on, the remote's version is published instead, this machine's is kept under `refs/gbrain/quarantine/`, and the refresh says so), and an uncommitted edit to a file the upstream changed refuses `refresh_dirty`, both with nothing pushed. The setting never publishes over a rewritten upstream: when the checkout holds a commit the upstream held before but no longer contains (a force-push), or gbrain has no record of the upstream before the fetch, the refresh still refuses `refresh_diverged` and the user decides. |
+| <a id="refresh_diverged"></a>`refresh_diverged` | `refresh_diverged` | The checkout has commits its upstream lacks, so a fast-forward is impossible. Nothing changed. | Ask the user, then publish the local commits merged with the upstream: `gbrain git commit-push --push-only --keep-checkout --scan-secrets --path <checkout>` scans what the local commits add, merges in memory, pushes the merge, refuses on a conflicting file and never moves the checkout (the refusal's fix fills in the real path and counts the local commits). Then retry `gbrain sources refresh <source>`, which fast-forwards to the merge. `gbrain sources reclone <source>` replaces the checkout instead and drops the local commits. With `sources.refresh_publish_diverged` unset on a worktree whose Git durability is on, or set to `true`, the refresh runs that publish itself, in a child process that skips the pre-push hook, and then fast-forwards; that child first scans what the local commits add (messages, added lines, merge resolutions) and refuses [`publish_secret_found`](shared-checkout-git.md#publish_secret_found) naming the file, line and rule (never the value) when one looks like a secret, and unset with durability not on refuses naming `gbrain sources writer git-durability <source> --enable`; a conflicting file then refuses `integrate_conflict` (with `gbrain.quarantine` on, the remote's version is published instead, this machine's is kept under `refs/gbrain/quarantine/`, and the refresh says so), and an uncommitted edit to a file the upstream changed refuses `refresh_dirty`, both with nothing pushed. The setting never publishes over a rewritten upstream: when the checkout holds a commit the upstream held before but no longer contains (a force-push), or gbrain has no record of the upstream before the fetch, the refresh still refuses `refresh_diverged` and the user decides. |
| <a id="refresh_dirty"></a>`refresh_dirty` | `refresh_dirty` | Uncommitted or untracked files overlap the incoming upstream changes (the first 20 are named), or `git merge --ff-only` refused for the same reason. HEAD did not move. The merge runs under the checkout's commit lock, so a `gbrain git commit-push` in progress is waited out (#8117); a lock held past its wait refuses as `commit_lock_busy`, and another git process holding `.git/index.lock` refuses as `git_index_locked` after brief retries. Both are retryable. Uncommitted files outside the incoming changes do not refuse; they are kept and listed as `preserved_uncommitted`. | Commit or discard the named paths, then retry. Check `gbrain sources writer status <source> --json` first: a pending Git effect may be about to commit a gbrain-published file. |
| <a id="sync_in_progress"></a>`sync_in_progress` | `sync_in_progress` | A managed sync of one of the checkout's sources has an unfinished cursor. Moving the checkout now would strand it, and the refresh never waits on a sync. `gbrain repair frontmatter --apply` refuses the same way while an unfinished cursor still names a selected file. | Run the printed `gbrain sync --source <source> --no-pull ...` resume command (it keeps the cursor's options), then retry (for a repair, preview again and approve the new hash). |
| <a id="refresh_in_progress"></a>`refresh_in_progress` | `refresh_in_progress` | Another refresh of the same checkout is active and its process is still running (the message names its holder's pid and host when the active refresh row is already readable), or a command that changes which sources share the checkout (`sources add`, `remove`, `archive`, `restore`, `reclone`) ran while one was active. A refresh whose process died is not a reason to refuse: the next refresh on the owner host finishes it first (`--dry-run` does not, so it still refuses while the orphan stands, and neither does a refresh that cannot read the checkout's HEAD: it leaves the orphan as it is and refuses until git can read HEAD again; writer status then names `git -C <checkout> status` instead of the next refresh). A refresh whose holder is recorded on another host, or whose lock file this host cannot open, is never taken over either: writer status shows its `liveness` as `unknown` (its `next` names the host to run on), and `gbrain sources refresh <source> --resume` on the owner host finishes it once that host's refresh is known to have stopped. Pending publication or topology recovery on the checkout refuses `refresh_recovery_required` first. An interrupted refresh that needs a reset decision refuses `refresh_recovery_required` instead. | Wait for the running refresh, or check it with `gbrain sources writer status <source>` (`liveness`, `holder`). `gbrain sources refresh <source> --resume` finishes an orphaned one now; then retry the source command with the same request ID. |
diff --git a/src/commands/doctor/checks/git-convergence.ts b/src/commands/doctor/checks/git-convergence.ts
index bf2babaf39..a21e55a334 100644
--- a/src/commands/doctor/checks/git-convergence.ts
+++ b/src/commands/doctor/checks/git-convergence.ts
@@ -219,7 +219,7 @@ export async function gitConvergenceCheck(engine: BrainEngine, now = Date.now())
if (diverged) {
return { name: 'git_convergence', status, details,
message: `Git checkouts have not converged with their upstream: ${lines.join(' | ')}. ${diverged.root} is a managed checkout that diverged from its upstream, so gbrain sources refresh refuses (refresh_diverged) until its local commits are published merged with the upstream; details.roots lists each one (sha, author, date, paths). ${remedy}; this compares with the last fetched upstream and does not fetch.${boundNote}`,
- fix: { ask_user: true, argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', diverged.root, '--json'], consent: [], actor: 'user', requires_exclusive: false,
+ fix: { ask_user: true, argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--scan-secrets', '--path', diverged.root, '--json'], consent: [], actor: 'user', requires_exclusive: false,
why: `${diverged.root} holds ${diverged.ahead} commit(s) its upstream lacks while the upstream moved on; this merges the upstream into HEAD in memory, refuses on a conflict, pushes the merge and never moves the checkout, after which gbrain sources refresh fast-forwards. It publishes those commits, so the user reviews them first.`,
user_message: `${diverged.root} has ${diverged.ahead} local commit(s) that were never pushed (newest ${diverged.unpushed_commits?.[0]?.sha.slice(0, 7) ?? 'unknown'} by ${diverged.unpushed_commits?.[0]?.author ?? 'unknown'}) while its remote moved on. Push them, merged with the remote, so gbrain can bring the checkout up to date?`,
verify: doctorVerify('git_convergence') } };
diff --git a/src/core/git/commit-push.ts b/src/core/git/commit-push.ts
index 6e0922c174..3b65379028 100644
--- a/src/core/git/commit-push.ts
+++ b/src/core/git/commit-push.ts
@@ -141,6 +141,7 @@ interface Ctx {
mergeRef: string;
trackingRef: string;
keepCheckout?: boolean;
+ scanSecrets?: boolean;
}
interface GitRun { code: number; stdout: string; stderr: string }
@@ -834,13 +835,13 @@ function conflictError(ctx: Ctx, ours: string, theirs: string, merged: MergeTree
const compare = `Compare the two versions (this machine's: git show ${local}:<path>; the remote's: git show ${remote}:<path>)`;
const fix = merged.structural.length
? `${preface}The local commit ${local} stays local and the checkout is unchanged. ${merged.structural.map(shellWord).join(', ')} ${merged.structural.length === 1 ? 'is' : 'are'} a file/directory or rename conflict that a per-file choice cannot resolve. ${compare}, then move or remove the local path in a new commit (gbrain git commit-push -m "<msg>" -- <paths>) and rerun gbrain git commit-push --push-only. See docs/guides/shared-checkout-git.md#conflicts.`
- : `${preface}The local commit ${local} stays local and the checkout is unchanged. ${compare}, then run gbrain git commit-push --push-only${ctx.keepCheckout ? ' --keep-checkout' : ''} --expect-remote ${remote} ${paths.map(p => `--resolve ${shellWord(p)}=ours|theirs`).join(' ')} (ours = this machine's version, theirs = the remote's, or =file:<path> with a merged copy). Until then every push from this checkout reports this conflict. See docs/guides/shared-checkout-git.md#conflicts.`;
+ : `${preface}The local commit ${local} stays local and the checkout is unchanged. ${compare}, then run gbrain git commit-push --push-only${ctx.keepCheckout ? ' --keep-checkout' : ''}${ctx.scanSecrets ? ' --scan-secrets' : ''} --expect-remote ${remote} ${paths.map(p => `--resolve ${shellWord(p)}=ours|theirs`).join(' ')} (ours = this machine's version, theirs = the remote's, or =file:<path> with a merged copy). Until then every push from this checkout reports this conflict. See docs/guides/shared-checkout-git.md#conflicts.`;
return opError('integrate_conflict', `Local and remote commits both changed ${paths.length} file(s) on ${ctx.branch}; nothing was merged.`, fix,
{ detail: JSON.stringify({ local: ours, remote: theirs, paths, structural: merged.structural, ...(merged.superseded?.length ? { superseded: merged.superseded } : {}) }),
fix: { ask_user: true, consent: [], actor: 'user', requires_exclusive: false,
why: 'Choosing which version of a file wins discards the other side; ask the user before resolving.',
user_message: `Both this machine and the remote changed ${paths.join(', ')}. Which version should win, or should they be merged by hand?`,
- ...(merged.structural.length ? {} : { argv: ['gbrain', 'git', 'commit-push', '--push-only', ...(ctx.keepCheckout ? ['--keep-checkout'] : []), '--path', ctx.root, '--expect-remote', theirs, ...paths.flatMap(p => ['--resolve', `${p}=<ours|theirs>`])] }),
+ ...(merged.structural.length ? {} : { argv: ['gbrain', 'git', 'commit-push', '--push-only', ...(ctx.keepCheckout ? ['--keep-checkout'] : []), ...(ctx.scanSecrets ? ['--scan-secrets'] : []), '--path', ctx.root, '--expect-remote', theirs, ...paths.flatMap(p => ['--resolve', `${p}=<ours|theirs>`])] }),
docs: 'docs/guides/shared-checkout-git.md#conflicts' } });
}
@@ -970,6 +971,7 @@ function publish(ctx: Ctx, start: string, resolutions: Map<string, Resolution> |
continue;
}
}
+ if (scanSecrets) assertNoSecretsInRange({ ...ctx, trackingRef: remoteTip }, candidate);
const raw = integrate(ctx.root, candidate, remoteTip);
const kept = supersededPaths(ctx.root, candidate, remoteTip, raw, resolutions);
const merged = kept.length
@@ -1179,7 +1181,7 @@ export function commitAndPush(opts: CommitPushOptions): CommitPushResult {
throw opError('commit_paths_invalid', 'A commit message is required.', 'Pass -m "<message>".');
}
const upstream = resolveCtx(root);
- const ctx = { ...withBranch(upstream, opts.branch), ...(opts.keepCheckout ? { keepCheckout: true } : {}) };
+ const ctx = { ...withBranch(upstream, opts.branch), ...(opts.keepCheckout ? { keepCheckout: true } : {}), ...(opts.scanSecrets ? { scanSecrets: true } : {}) };
const elsewhere = ctx.mergeRef !== upstream.mergeRef;
assertNoOperationInProgress(root);
diff --git a/src/core/persistence/effect-git.ts b/src/core/persistence/effect-git.ts
index aff18f1314..a0f0b6e38f 100644
--- a/src/core/persistence/effect-git.ts
+++ b/src/core/persistence/effect-git.ts
@@ -442,7 +442,8 @@ async function effectCommit(session: GitSession, root: string, prefix: string, s
...(supersedes.length ? { supersedes } : {}), ...(adopted.length ? { adopted } : {}) });
const moved = await git(session, ['update-ref', '-m', `commit: ${paragraphs[0]!.split('\n')[0]}`, 'HEAD', created, head ?? '']);
if (moved.code === 0) {
- await git(session, ['maintenance', 'run', '--auto', '--quiet']).catch(() => undefined);
+ const detached = await git(session, ['maintenance', 'run', '--auto', '--quiet', '--detach']).catch(() => null);
+ if (detached?.code === 129) await git(session, ['maintenance', 'run', '--auto', '--quiet']).catch(() => undefined);
return created;
}
if (attempt) return null;
diff --git a/src/core/persistence/git-push-holds.ts b/src/core/persistence/git-push-holds.ts
index a13326979d..bdc8b92c4f 100644
--- a/src/core/persistence/git-push-holds.ts
+++ b/src/core/persistence/git-push-holds.ts
@@ -62,7 +62,7 @@ function gitPushFix(hold: Omit<GitPushHold, 'fix'>): Action {
const held = hold.retrying + hold.parked;
if (hold.conflict && hold.root) return {
ask_user: true, consent: [], actor: 'user', requires_exclusive: false,
- argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', hold.root, '--expect-remote', hold.conflict.remote,
+ argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--scan-secrets', '--path', hold.root, '--expect-remote', hold.conflict.remote,
...hold.conflict.paths.flatMap(path => ['--resolve', `${path}=<ours|theirs>`])],
why: 'Choosing which version of a file wins discards the other side; ask the user before resolving.',
user_message: `This machine and the remote both changed ${hold.conflict.paths.join(', ')} in ${hold.root}, so ${held} Git backup(s) of page writes in source ${hold.source_id} are held (nothing is lost; the database has every write). Which version should win: this machine's (ours) or the remote's (theirs)? Afterwards run gbrain sources writer retry-effects ${hold.source_id} --parked-pushes, then gbrain sources refresh ${hold.source_id}.`,
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 460e4be72c..d3f3086365 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -563,7 +563,7 @@ async function publishInChild(root: string, sourceId: string, checkedUpstream: s
async function divergedRefusal(git: Git, root: string, sourceId: string, upstreamRef: string, oldHead: string, targetHead: string, rewrite?: string, hint?: string): Promise<OperationError> {
const count = Number((await git(['-C', root, 'rev-list', '--count', `${targetHead}..${oldHead}`])).stdout.trim()) || 0;
const local = count ? `${count} local commit(s)` : 'the local commits';
- const argv = ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', root, '--json'];
+ const argv = ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--scan-secrets', '--path', root, '--json'];
const error = refusal('refresh_diverged',
`The checkout HEAD ${oldHead} is not an ancestor of ${upstreamRef} (${targetHead}); a fast-forward is impossible and nothing changed.${rewrite ? ` ${rewrite} ${REFRESH_PUBLISH_DIVERGED_KEY} does not publish it on its own.` : ''}`,
`Publish ${local} merged with ${upstreamRef}: ${argv.map(q).join(' ')} (it merges in memory, refuses on a conflict and never moves the checkout), then retry gbrain sources refresh ${sourceId}. Ask the user first, because it pushes those commits (git -C ${q(root)} log --stat ${upstreamRef}..HEAD lists them). gbrain sources reclone ${sourceId} replaces the checkout instead and drops them.${hint ?? (rewrite ? '' : ` To let every refresh publish a diverged checkout this way on its own, the user can run gbrain config set ${REFRESH_PUBLISH_DIVERGED_KEY} true.`)}`);
diff --git a/test/effect-git-secret-gate.test.ts b/test/effect-git-secret-gate.test.ts
index f7a13a74a2..ead7cd4354 100644
--- a/test/effect-git-secret-gate.test.ts
+++ b/test/effect-git-secret-gate.test.ts
@@ -14,7 +14,7 @@
*/
import { afterEach, describe, expect, test } from 'bun:test';
import { randomUUID } from 'node:crypto';
-import { chmodSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
+import { chmodSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import type { BrainEngine } from '../src/core/engine.ts';
@@ -128,6 +128,32 @@ describe('#8896 pushGitRoot scans the commits it publishes', () => {
expect(remoteTip(c)).toBe(base);
}, 120_000);
+ test('a remote rewound past a published secret that also moved the same file refuses on the secret, never on a conflict whose resolve would publish it', async () => {
+ const c = checkout();
+ const base = remoteTip(c);
+ scriptCommit(c.root, 'notes/deploy.md', `Deploy token ${token}\n`);
+ git(c.root, 'push', '-q');
+ git(c.remote, 'update-ref', 'refs/heads/main', base);
+ const theirs = moveRemote(c, 'notes/page.md');
+ scriptCommit(c.root, 'notes/page.md', 'This machine\'s version\n');
+ const error = await withEnv(childEnv(c), () => refusal(pushGitRoot(c.root)));
+ expect(error.code).toBe('publish_secret_found');
+ expect(error.message).toContain('notes/deploy.md:1 (');
+ expect(remoteTip(c)).toBe(theirs);
+ }, 120_000);
+
+ test('a conflict the scanned publish reports hands back a resolve that still scans', async () => {
+ const c = checkout();
+ const theirs = moveRemote(c, 'notes/page.md');
+ scriptCommit(c.root, 'notes/page.md', 'This machine\'s version\n');
+ const error = await withEnv(childEnv(c), () => refusal(pushGitRoot(c.root)));
+ expect(error.code).toBe('integrate_conflict');
+ expect(error.fix?.argv).toEqual(['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--scan-secrets', '--path', c.root,
+ '--expect-remote', theirs, '--resolve', 'notes/page.md=<ours|theirs>']);
+ expect(error.suggestion).toContain('--keep-checkout --scan-secrets --expect-remote');
+ expect(remoteTip(c)).toBe(theirs);
+ }, 120_000);
+
test('a remote without the branch (a first push): a clean history creates it', async () => {
const c = checkout();
scriptCommit(c.root, 'notes/page.md', 'A clean page\n');
@@ -476,4 +502,38 @@ describe('#8896 the Git effect commit is made by plumbing', () => {
expect(outcome.get('notes/a.md')).toBeInstanceOf(Error);
expect(git(root, 'rev-parse', 'HEAD').trim()).toBe(before);
}, 60_000);
+
+ /** A git that logs each `maintenance` call's arguments and, with `refuseDetach`, rejects --detach the way git before 2.47 does. */
+ function maintenanceGit(home: string, refuseDetach: boolean): { PATH: string; calls: () => string[] } {
+ const path = process.env.PATH ?? '', bin = join(home, 'maint-bin'), log = join(home, 'maintenance.log');
+ mkdirSync(bin);
+ const refuse = refuseDetach ? `; case " $* " in *" --detach "*) echo 'error: unknown option detach' >&2; exit 129;; esac` : '';
+ writeFileSync(join(bin, 'git'), ['#!/bin/sh',
+ `case " $* " in *" maintenance "*) echo "$*" >> '${log}'${refuse};; esac`,
+ `PATH='${path}' exec '${Bun.which('git')!}' "$@"`, ''].join('\n'), { mode: 0o755 });
+ const calls = () => { try { return readFileSync(log, 'utf8').trim().split('\n').map(line => line.slice(line.indexOf('maintenance'))); } catch { return []; } };
+ return { PATH: `${bin}:${path}`, calls };
+ }
+
+ test('the effect commit runs auto maintenance detached, so a repack never runs under the writer lock', async () => {
+ const home = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-8896-'))); dirs.push(home);
+ const root = join(home, 'brain');
+ repo(root);
+ writeFileSync(join(root, 'notes', 'a.md'), 'After\n');
+ const shim = maintenanceGit(home, false);
+ const outcome = await withEnv({ PATH: shim.PATH, GBRAIN_HOME: join(home, 'gbrain-home') }, () => commitGitTargets(root, ['notes/a.md']));
+ expect(outcome.get('notes/a.md')).toEqual({ git: 'committed' });
+ expect(shim.calls()).toEqual(['maintenance run --auto --quiet --detach']);
+ }, 60_000);
+
+ test('on a git without maintenance --detach the effect commit still runs auto maintenance', async () => {
+ const home = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-8896-'))); dirs.push(home);
+ const root = join(home, 'brain');
+ repo(root);
+ writeFileSync(join(root, 'notes', 'a.md'), 'After\n');
+ const shim = maintenanceGit(home, true);
+ const outcome = await withEnv({ PATH: shim.PATH, GBRAIN_HOME: join(home, 'gbrain-home') }, () => commitGitTargets(root, ['notes/a.md']));
+ expect(outcome.get('notes/a.md')).toEqual({ git: 'committed' });
+ expect(shim.calls()).toEqual(['maintenance run --auto --quiet --detach', 'maintenance run --auto --quiet']);
+ }, 60_000);
});
diff --git a/test/pathological/h2-git-effect-moved-remote.test.ts b/test/pathological/h2-git-effect-moved-remote.test.ts
index dfd8b82322..5753be95eb 100644
--- a/test/pathological/h2-git-effect-moved-remote.test.ts
+++ b/test/pathological/h2-git-effect-moved-remote.test.ts
@@ -174,7 +174,7 @@ for (const { backend, databaseUrl } of pathologicalArms()) {
const hold = status.git_push_holds[0]!;
expect(hold).toMatchObject({ retrying: 1, parked: 0, codes: ['integrate_conflict'], conflict: { paths: ['notes/acme-conflict.md'], remote: theirs } });
expect(hold.fix).toMatchObject({ ask_user: true, actor: 'user',
- argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', b.root, '--expect-remote', theirs, '--resolve', 'notes/acme-conflict.md=<ours|theirs>'] });
+ argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--scan-secrets', '--path', b.root, '--expect-remote', theirs, '--resolve', 'notes/acme-conflict.md=<ours|theirs>'] });
expect(hold.fix.user_message).toContain('Which version should win');
expect(status.next).toEqual(hold.fix);
expect((await checkParkedEffects(b.engine, [b.sourceId])).status).toBe('warn');
diff --git a/test/persistence-worktree-refresh.test.ts b/test/persistence-worktree-refresh.test.ts
index 31718516aa..75507e5c78 100644
--- a/test/persistence-worktree-refresh.test.ts
+++ b/test/persistence-worktree-refresh.test.ts
@@ -215,7 +215,7 @@ test('4b. Q1: the diverged refusal names the keep-checkout publish; running it,
const local = f.head();
const error = await refusedWith(refreshWorktree(f.engine, f.alpha), 'refresh_diverged') as { suggestion?: string; fix?: { argv: string[]; ask_user?: boolean; actor?: string } };
expect(error.suggestion).toContain('1 local commit(s)');
- expect(error.fix).toMatchObject({ argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', f.root, '--json'], ask_user: true, actor: 'user' });
+ expect(error.fix).toMatchObject({ argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--scan-secrets', '--path', f.root, '--json'], ask_user: true, actor: 'user' });
git(f.root, 'config', 'user.name', 'Example'); git(f.root, 'config', 'user.email', '[email protected]');
expect(commitAndPush({ repo: f.root, pushOnly: true, keepCheckout: true })).toMatchObject({ outcome: 'pushed', integrated: 1, checkout: 'unchanged' });
expect(f.head()).toBe(local);
@@ -238,7 +238,7 @@ test('4e. #7685: sources refresh --json emits the diverged refusal\'s structured
try {
const refusal = JSON.parse(out.join('\n'));
expect(refusal).toMatchObject({ status: 'refused', code: 'refresh_diverged', contract_version: 1,
- fix: { argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', f.root, '--json'], next: 'ask_user', actor: 'user' } });
+ fix: { argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--scan-secrets', '--path', f.root, '--json'], next: 'ask_user', actor: 'user' } });
expect(refusal.fix.user_message).toContain(f.alpha);
expect(refusal.suggestion).toContain('1 local commit(s)');
expect(currentExitCode()).toBe(1);
@@ -279,7 +279,7 @@ test('4d. Q1: writer status and doctor name each commit a diverged managed check
const root = (check?.details as { roots: Array<Record<string, any>> }).roots.find(r => r.root === f.root)!;
expect(root).toMatchObject({ managed: true, diverged: true, ahead: 1, behind: 1, porcelain: { untracked: 1 } });
expect(root.unpushed_commits[0]).toMatchObject({ sha, author, paths: ['beta/local.md'] });
- expect(check?.fix).toMatchObject({ ask_user: true, actor: 'user', argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--path', f.root, '--json'] });
+ expect(check?.fix).toMatchObject({ ask_user: true, actor: 'user', argv: ['gbrain', 'git', 'commit-push', '--push-only', '--keep-checkout', '--scan-secrets', '--path', f.root, '--json'] });
}), 120_000);
test('5. dirt overlapping the incoming diff refuses naming the path; unrelated dirt is preserved and listed', () => each(async f => {