Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sync): keep #6609 untrack-and-ignore contract; classic change limited to fail-closed history and caps
garrytan8ca67d0e6f04
diff --git a/src/commands/sync/deletes.ts b/src/commands/sync/deletes.tsindex 486288357e..bfb9deff8e 100644--- a/src/commands/sync/deletes.ts+++ b/src/commands/sync/deletes.ts@@ -3,8 +3,6 @@* un-syncable-modified sweep that runs before the checkpoint opens, and the* batched removed-file drain. Both SOFT-delete (#4587).*/-import { existsSync } from 'fs';-import { join } from 'path';import { softDeleteSyncPages } from '../../core/company-brain/profile.ts';import { serr, slog } from '../../core/console-prefix.ts';import { DELETE_BATCH_SIZE } from '../../core/engine-constants.ts';@@ -100,17 +98,11 @@ export async function sweepUnsyncableModified(/** The removed-file drain. Returns a partial result when the run aborts mid-drain. */export async function runDeletesPhase(run: SyncRun,- plan: Pick<SyncPlan, 'opts' | 'filtered' | 'lastCommit' | 'pin' | 'syncImportRoot'>,+ plan: Pick<SyncPlan, 'opts' | 'filtered' | 'lastCommit' | 'pin'>,progress: SyncProgress,): Promise<SyncResult | undefined> {const { engine, completed, pagesAffected, deletedSlugs, failedFiles } = run;const { opts, filtered } = plan;- // #7130: Git reports a deletion for `git rm --cached`, but the file is still on disk and only untracked now. Keep its page.- const stillOnDisk = filtered.deleted.filter(p => existsSync(join(plan.syncImportRoot, p)));- const keptOnDisk = new Set(stillOnDisk);- if (stillOnDisk.length > 0) {- serr(`\n Kept ${stillOnDisk.length} page(s) whose file Git stopped tracking but is still on disk (e.g. ${stillOnDisk[0]}); not deleting. Commit the file again to keep it in Git, or delete the file to delete the page.`);- }// v0.41.19.0 (T2/D6/D7/D16/D18 via /plan-eng-review + codex outside-voice):// batched delete loop. Replaces the per-file N+1 that PR #1538 originally// batched on Postgres only. See plan file:@@ -161,7 +153,7 @@ export async function runDeletesPhase(// v0.42.x (#1794): resume-filter the delete set so a resumed run skips paths// already drained in a prior run (deletes are idempotent, but skipping avoids// re-resolving + re-deleting tens of thousands of already-gone pages).- const deletesToDo = resumeFilter(filtered.deleted.filter(p => !keptOnDisk.has(p)), [...completed]);+ const deletesToDo = resumeFilter(filtered.deleted, [...completed]);if (deletesToDo.length > 0) {progress.start('sync.deletes', deletesToDo.length);if (opts.sourceId) {diff --git a/src/commands/sync/full.ts b/src/commands/sync/full.tsindex b00d245dca..c08bb7bf54 100644--- a/src/commands/sync/full.ts+++ b/src/commands/sync/full.ts@@ -2,9 +2,7 @@import { assertSourceFilesystemActive } from '../../core/minions/source-filesystem.ts';import { currentJobSignal } from '../../core/minions/submission-authority.ts';import { currentCompanyBrainSync, softDeleteSyncPages } from '../../core/company-brain/profile.ts';-import { existsSync } from 'fs';-import { join, relative } from 'path';-import { gitIgnoresUntracked } from '../../core/persistence/sync-origin-evidence.ts';+import { relative } from 'path';import type { BrainEngine } from '../../core/engine.ts';import { DELETE_BATCH_SIZE } from '../../core/engine-constants.ts';import { collectSyncableFiles } from '../import.ts';@@ -480,17 +478,6 @@ async function reconcileFullSyncDeletes(if (sp && !everCommitted.has(sp.replace(/\\/g, '/'))) dbOnlySlugs.push(slug);else deletableSlugs.push(slug);}- // #7130: `git rm --cached` plus a .gitignore entry leaves the file on disk; its history says committed, but nothing was removed.- const ignoredOnDisk = new Set<string>();- for (const slug of deletableSlugs) {- const sp = pathBySlug.get(slug);- const abs = sp ? join(slugRoot ?? syncScopeRoot, sp) : null;- if (abs && existsSync(abs) && await gitIgnoresUntracked(gitContextRoot, relative(gitContextRoot, abs))) ignoredOnDisk.add(slug);- }- if (ignoredOnDisk.size > 0) {- serr(`\n Kept ${ignoredOnDisk.size} page(s) whose file .gitignore now excludes but is still on disk; not deleting. Import them with gbrain sync --source ${sid} --full --include-gitignored, or delete the file to delete the page.`);- deletableSlugs = deletableSlugs.filter(slug => !ignoredOnDisk.has(slug));- }} else if (!company) {// #7130: Git could not list the history (a dangling ref fails `git log --all`), so no stale page is proven// committed; deleting them would treat a never-committed page's absence as a deletion. Keep them all.diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.tsindex 50545d61d8..c25c97bd47 100644--- a/src/core/behavior-change-notice.ts+++ b/src/core/behavior-change-notice.ts@@ -1022,7 +1022,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;// #7130 (sync deleting pages whose file Git never committed; autopilot purge hard-deleting them). `since` is restamped at the merge slot.{ since: '0.60.326.0', text: 'A managed sync no longer deletes a live page whose recorded file is missing when Git never committed that file (#7130): a page whose file `.gitignore` excludes, or that no commit of the pinned history or the pass\'s baseline holds, is held `file_drift` (side `deleted`, state `file_gone`, `meta.reason` `ignored` or `never_committed`) instead, in a normal pass, under `--full` and in the dry run. A file that was committed and then removed still deletes its page. When Git cannot answer (a failed or timed-out read), the page is held. `gbrain repair file-drift --prefer file` still applies the deletion after review, and committing the file again clears the hold. A managed put_page, capture or other write-through whose file would land under a path `.gitignore` excludes now writes no file and records no file for the page: the page is database-only, so no later sync can read the missing file as a deletion.' },{ since: '0.60.326.0', text: 'Sync now caps how many pages one run deletes (#7130): new settings `sync.max_deletes` (default 500) and `sync.max_delete_ratio` (default 0.01, applied above 50 deletes), each with a per-source override `<key>.source.<id>`, on top of the 50% valve. A managed pass over a cap withholds its deletes through the existing `deletes_withheld` record (it names the cap that tripped), the dry run lists them, and `gbrain sync status` asks the user (`ask_user`) before anything raises the cap; a classic full reconcile and a working-tree sync (on a detached HEAD too) skip the deletes with a warning naming the cap. Adds and edits still import. `GBRAIN_ALLOW_MASS_RECONCILE=1` lets a deletion through as before, and `gbrain config set` refuses a cap that is not a whole number of 0 or more (ratio: a number from 0 to 1).' },- { since: '0.60.326.0', text: 'Classic sync no longer deletes pages it cannot prove were removed (#7130): `gbrain sync --full` keeps every stale page, with a warning, when Git cannot list the history that shows their files were committed (a dangling ref fails `git log --all`; before, it skipped the never-committed check and deleted them all), and a page whose file Git stopped tracking (`git rm --cached`) but which is still on disk is kept, in an incremental sync and under `--full` (where its file must also be ignored). A real `git rm` deletes as before.' },+ { since: '0.60.326.0', text: 'Classic `gbrain sync --full` no longer deletes pages it cannot prove were removed (#7130): when Git cannot list the history that shows their files were committed (a dangling ref fails `git log --all`), it keeps every stale page with a warning, where before it skipped the never-committed check and deleted them all. With readable history it deletes as before.' },{ since: '0.60.326.0', text: 'The autopilot purge phase and the `purge` job no longer hard-delete a page that a managed sync soft-deleted unless another live page of the same source has identical content (#7130). Each sync deletion now leaves a durable record (op_checkpoints `sync-delete-tombstone`; a tombstone from before this release is recognized by its uncompacted `managed_sync_delete` receipt, and one whose receipt was compacted is not). Held pages are reported as `sync_delete_unreviewed` in the phase\'s `purged_pages_blocked` and counted in `held`, and the phase reports warn. `gbrain pages purge-deleted` still purges them after consent: its dry run reports `sync_unreviewed` and its consent text names how many of the set are sync deletions with no live twin and that a purged page can only come back from a backup. The purge phase\'s recovery window (pages and orphan clone temp dirs), and the `purge` job\'s default, now follow `purge.retention_hours` (default 72, minimum 24) instead of a fixed 72 hours.' },{ since: '0.60.326.0', text: 'New doctor check `uncommitted_sync_deletions` and explicit-only repair kind `gbrain repair uncommitted-deletions --source <id>` (#7130): on each managed source\'s owner host, doctor warns about soft-deleted pages a sync removed although Git never held their recorded file (`.gitignore` excludes it, or no commit holds it and it is not on the disk), bounded to 20,000 tombstones per source and 2,000 Git evidence reads per run. The repair previews each page (slug, recorded path, deletion time, evidence, whether a sync deleted it) with a hash; `--apply --expect <hash> --yes` restores exactly that set, each page live and database-only (detached: it forgets its recorded file and no file is written), so the next sync keeps it. Pages another writer deleted join only with `--include-ambiguous`. It refuses off the owner host (`not_owner_host`) and a changed plan (`preview_changed`); without `--yes` a non-interactive run exits 3 and changes nothing. Pages already purged cannot be restored by this command; only a backup (pg_dump, or `gbrain backup create`) can bring them back. It never runs on upgrade or automatically.' },];diff --git a/test/sync-classic-uncommitted-7130.test.ts b/test/sync-classic-uncommitted-7130.test.tsindex b26eedc993..16f8f3ac2a 100644--- a/test/sync-classic-uncommitted-7130.test.ts+++ b/test/sync-classic-uncommitted-7130.test.ts@@ -3,8 +3,6 @@* On a real PGLite brain, this protects:* - `sync --full` keeps every stale page when Git cannot list the history that proves a file was committed (a dangling* ref fails `git log --all`); it used to skip the #2426 never-committed gate and delete them all;- * - a file Git stopped tracking (`git rm --cached` plus a .gitignore entry) but that is still on disk keeps its page,- * in an incremental sync and under `--full`; a real `git rm` still deletes;* - the absolute caps (`sync.max_deletes`, per source too) withhold a classic full reconcile and a working-tree sync* below the 50% valve, and `GBRAIN_ALLOW_MASS_RECONCILE=1` still lets the deletion through.*/@@ -59,19 +57,6 @@ describe('#7130 classic sync: a missing file is deletion evidence only when Gitexpect(await live()).toEqual(['memories/lost', 'notes/a', 'notes/b']);}, 120_000);- test('git rm --cached plus .gitignore keeps the page while its file is on disk, incremental and --full', async () => {- seed(['a', 'kept', 'gone']);- expect(['first_sync', 'synced']).toContain((await sync()).status);- git(repoPath, 'rm', '-q', '--cached', 'notes/kept.md');- git(repoPath, 'rm', '-q', 'notes/gone.md');- writeFileSync(join(repoPath, '.gitignore'), 'notes/kept.md\n');- commit(repoPath, 'untrack kept, remove gone');- await sync();- expect(await live()).toEqual(['notes/a', 'notes/kept']);- await sync({ full: true });- expect(await live()).toEqual(['notes/a', 'notes/kept']);- }, 120_000);-test('sync.max_deletes withholds a classic full reconcile below the 50% valve; the env valve lets it through', async () => {seed(Array.from({ length: 40 }, (_, i) => `n${String(i).padStart(2, '0')}`));expect(['first_sync', 'synced']).toContain((await sync({ full: true })).status);