Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): classic full reconcile fails closed, keeps untracked-on-disk files, absolute caps on classic lanes (#7130)

garrytan784cba659217
diff --git a/src/commands/sync/deletes.ts b/src/commands/sync/deletes.ts
index bfb9deff8e..486288357e 100644
--- a/src/commands/sync/deletes.ts
+++ b/src/commands/sync/deletes.ts
@@ -3,6 +3,8 @@
* 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';
@@ -98,11 +100,17 @@ 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'>,
+ plan: Pick<SyncPlan, 'opts' | 'filtered' | 'lastCommit' | 'pin' | 'syncImportRoot'>,
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:
@@ -153,7 +161,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, [...completed]);
+ const deletesToDo = resumeFilter(filtered.deleted.filter(p => !keptOnDisk.has(p)), [...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.ts
index 328b7321b0..b00d245dca 100644
--- a/src/commands/sync/full.ts
+++ b/src/commands/sync/full.ts
@@ -2,7 +2,9 @@
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 { relative } from 'path';
+import { existsSync } from 'fs';
+import { join, relative } from 'path';
+import { gitIgnoresUntracked } from '../../core/persistence/sync-origin-evidence.ts';
import type { BrainEngine } from '../../core/engine.ts';
import { DELETE_BATCH_SIZE } from '../../core/engine-constants.ts';
import { collectSyncableFiles } from '../import.ts';
@@ -40,6 +42,9 @@ import {
planReconcileDeletes,
listEverCommittedPaths,
massReconcileAllowed,
+ readSyncDeleteCaps,
+ syncDeleteTrip,
+ syncDeleteTripText,
} from '../../core/sync-reconcile.ts';
import type { SyncOpts, SyncResult } from '../sync.ts';
import { heldRenameOrigins, holdRefusedImport, moveHeldRenames, noteScreenedImport, settleFullSyncHolds, wouldHoldFields, type LegacyHolds } from './holds.ts';
@@ -475,6 +480,30 @@ 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.
+ serr(`\n WARNING: kept ${plan.staleSlugs.length} stale page(s) of source '${sid}': Git could not list the history that proves their files were committed and removed (run git -C ${gitContextRoot} log --all to see why). No pages were deleted.`);
+ deletableSlugs = [];
+ }
+ // #7130: the absolute caps (sync.max_deletes, sync.max_delete_ratio) brake a full reconcile below the 50% valve too.
+ const caps = await readSyncDeleteCaps(engine, sid);
+ const trip = syncDeleteTrip(deletableSlugs.length, plan.reconcilableCount, caps);
+ if (trip && !massReconcileAllowed()) {
+ serr(`\n WARNING: refusing to reconcile-delete ${deletableSlugs.length} of ${plan.reconcilableCount} file-backed page(s) for source '${sid}' (${syncDeleteTripText(trip, caps)}). ` +
+ `No pages were deleted. Check the checkout first; if the removal is intended, raise the cap (gbrain config set ${trip === 'max_deletes' ? 'sync.max_deletes' : 'sync.max_delete_ratio'}.source.${sid} <value>) or re-run with GBRAIN_ALLOW_MASS_RECONCILE=1.`);
+ deletableSlugs = [];
}
if (dbOnlySlugs.length > 0) {
let reExported = 0;
diff --git a/src/commands/sync/preflight.ts b/src/commands/sync/preflight.ts
index 2c231369e2..12171ea87a 100644
--- a/src/commands/sync/preflight.ts
+++ b/src/commands/sync/preflight.ts
@@ -34,7 +34,7 @@ import {
unique,
} from '../../core/sync-git.ts';
import { buildPartialResult } from '../../core/sync-lock.ts';
-import { composeAbortSignals, massReconcileAllowed, MASS_RECONCILE_RATIO } from '../../core/sync-reconcile.ts';
+import { composeAbortSignals, massReconcileAllowed, MASS_RECONCILE_RATIO, readSyncDeleteCaps, syncDeleteTrip, syncDeleteTripText } from '../../core/sync-reconcile.ts';
import {
DEFAULT_SOURCE_ID,
resolveSlugForPath,
@@ -1112,7 +1112,7 @@ async function computeFilteredDelta(
// ratio + same env escape hatch. Deletes are skipped loudly; adds and
// modifies still import, and committing the deletions (or
// GBRAIN_ALLOW_MASS_RECONCILE=1) re-enables them.
- if (importWorkingTree && !detachedHead && filtered.deleted.length >= 10 && !massReconcileAllowed()) {
+ if (importWorkingTree && filtered.deleted.length > 0 && !massReconcileAllowed()) {
try {
const rows = await engine.executeRaw<{ count: number }>(
opts.sourceId
@@ -1121,7 +1121,15 @@ async function computeFilteredDelta(
opts.sourceId ? [opts.sourceId] : [],
);
const pageCount = Number(rows[0]?.count ?? 0);
- if (pageCount > 0 && filtered.deleted.length > pageCount * MASS_RECONCILE_RATIO) {
+ // #7130: the absolute caps (sync.max_deletes, sync.max_delete_ratio) apply on a detached HEAD too.
+ const sid = opts.sourceId ?? DEFAULT_SOURCE_ID;
+ const caps = await readSyncDeleteCaps(engine, sid);
+ const trip = syncDeleteTrip(filtered.deleted.length, pageCount, caps);
+ if (trip) {
+ serr(`\n WARNING: refusing to delete ${filtered.deleted.length} page(s) from a working-tree sync (${syncDeleteTripText(trip, caps)}). ` +
+ `Commit the deletions and sync again, raise the cap (gbrain config set ${trip === 'max_deletes' ? 'sync.max_deletes' : 'sync.max_delete_ratio'}.source.${sid} <value>), or re-run with GBRAIN_ALLOW_MASS_RECONCILE=1. Adds/modifies still import.\n`);
+ filtered.deleted = [];
+ } else if (!detachedHead && filtered.deleted.length >= 10 && pageCount > 0 && filtered.deleted.length > pageCount * MASS_RECONCILE_RATIO) {
serr(
`\n WARNING: refusing to delete ${filtered.deleted.length} page(s) from a working-tree ` +
`sync (> ${Math.round(MASS_RECONCILE_RATIO * 100)}% of ${pageCount} page(s)). An uncommitted ` +
diff --git a/src/core/persistence/sync-origin-evidence.ts b/src/core/persistence/sync-origin-evidence.ts
index 6f6f8ad89a..a9455f534f 100644
--- a/src/core/persistence/sync-origin-evidence.ts
+++ b/src/core/persistence/sync-origin-evidence.ts
@@ -8,7 +8,7 @@
* Both reads are bounded Git plumbing over the candidate paths alone. A read that fails or times out answers `unknown`
* for every path it covered, which the classifier treats like a missing commit (fail closed: hold, never delete).
*/
-import { execFile, spawn } from 'node:child_process';
+import { execFile, spawn } from '../spawn.ts';
import { promisify } from 'node:util';
import { gitChildEnv } from '../git-env.ts';
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index 15d79af331..a3ff64838b 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -422,6 +422,7 @@ test/sync-slug-root-mode-6694.test.ts
test/sync-staged-catchup.test.ts
test/sync-status-failure-detail-6513.test.ts
test/sync-unblock-repair-error-gbra130.test.ts
+test/sync-uncommitted-origin-7130.test.ts
test/sync-unreadable-file-gbra130.test.ts
test/sync-waiver-budget.test.ts
test/sync-waiver-run-imports.test.ts