Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): managed incremental sync prices its delta, not the whole tree, on a stale chunker stamp (#6858 ask 5)

garrytanf19bd21471de
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cd5f70ba45..9c1821f449 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ identifiers and attribution are available in the pre-removal Git revision
| An autopilot (or any) `sync` job whose managed run is blocked, stalled or capped | `completed`, with the parked result | `dead`; the error names the stop and the run's `next` command, and `jobs get` keeps `outcome`, `drain` and `next` in the job's progress |
| A managed sync run blocked on a page | `sync_failures` warned and failed only after 72 h | new doctor check `managed_sync_blocked`: warns at once and fails after an hour, with `gbrain sync status --source <id> --json` as its fix |
| get_brain_health with a source blocked over an hour | `ok` | `attention`, with a `sync_blocked` count band (no source names) |
+| A managed incremental `gbrain sync` on a large repository whose chunker stamp predates the current chunker | read and token-counted every file before the first write (131 s and 1.4 GB on a 308k-file, 2.7 GB test repo), priced a `chunker_drift` ceiling and could auto-defer cheap embeds | prices only the changed files, as the run embeds them (first write on a blocked 52-page retry went from 58 s to 5 s in the same test); `--full` and classic sources are unchanged |
| A managed sync import of an oversized, quarantined or flagged page | rewrote the canonical file with the gate's marker (a file publication with a recovery record, every time the body changed) | stores the marker in the database only and leaves the file as committed; a file whose marker is missing or stale is not file/database drift |
### What you need to do
@@ -31,7 +32,7 @@ Nothing. Markers already written into files stay where they are (nothing rewrite
### Still open on #6858
-Preparation cost that scales with the repository instead of the diff (ask 5) and a supported low-latency mode (ask 6) are tracked separately.
+A supported low-latency mode (ask 6) is a separate proposal. The repository-scaling preparation cost of ask 5 came from the embed cost estimate above; the remaining per-run work that grows with the repository (git discovery, the identity freeze) measured about 3 s at 308k pages.
## [0.60.269.0] - 2026-10-16
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 1ad8ff558d..b05fa6e40e 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -729,10 +729,11 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText }
{ since: '0.60.269.0', text: 'A managed sync no longer forks a second page for a file whose page an older gbrain imported under a slug the current slug fold spells differently (#6727: punctuation, spaces, accents, emoji, full-width characters): a file with no recorded origin and no page at its slug imports into the one live page without a recorded file that folds to it, keeping that page\'s id, links and history. When several such pages match, the file is held `legacy_slug_conflict` (needs a person: keep one, remove the others, then sync); with `sync.holds=fail` the run stops `page_identity_changed`. Pages already forked by older syncs are not merged.' },
{ since: '0.60.269.0', text: 'A managed sync\'s final checkpoint validation reads at most one committed receipt per unfinished receipt, so a catch-up of tens of thousands of entries no longer times out at its last step (#6735). A checkpoint that still hits `checkpoint_validation_timeout` is re-validated by the next sync of the source (once per run, after the stop is recorded) instead of rediscovering the whole manifest.' },
{ since: '0.60.269.0', text: 'Doctor no longer warns that a managed sync cursor has been idle "over an hour" right after the cursor was discovered (#6763): idle time counts from the cursor\'s last advance, or from its discovery when it has not advanced yet.' },
- // #6858 (GBRA-121; Garry: "take care of fixing" #6858 asks 1-4). `since` is a placeholder equal to the branch's VERSION; the integrator sets it to the merge-slot stamp by hand.
+ // #6858 (GBRA-121; Garry: "take care of fixing" #6858 asks 1-5; ask 5 contained per GBRA-108). `since` is a placeholder equal to the branch's VERSION; the integrator sets it to the merge-slot stamp by hand.
{ since: '0.60.269.0', text: 'A durable `sync` job (autopilot freshness included) whose managed run stops on something only a person clears (a run blocked on a page, a stalled drain holder, a write cap) now ends `dead` instead of `completed`: its error names the stop and the run\'s `next` command, and `gbrain jobs get <id> --json` keeps `outcome`, `drain` and `next` in the job\'s progress. New doctor check `managed_sync_blocked` warns as soon as a source\'s run is blocked and fails after an hour (fix: `gbrain sync status --source <id> --json`), and `get_brain_health` gains a `sync_blocked` count band that turns its status to `attention` past the same hour.' },
{ since: '0.60.269.0', text: 'A managed sync import whose stuck publication `gbrain repair stuck-recovery --keep disk` abandoned is retried by the next plain sync after the rest of its run (imported when the kept file is committed, held `worktree_dirty` when it is an uncommitted edit) instead of parking the run `blocked_by_failures`; the `--keep disk` result now names both follow-ups (`sources reconcile … --preview --auto-additive`, then `gbrain sync --source <id> --no-pull`), or the plain sync for a sync import.' },
{ since: '0.60.269.0', text: 'Managed sync no longer rewrites a canonical file only to add or refresh the content gate\'s markers (`embed_skip`, `content_flag`, `quarantine`): they are stored in the database and the file stays as committed, so an oversized page agents keep appending to is no longer republished on every import. A file whose marker is missing or carries another `assessed_at` is not file/database drift. Markers already in files are left alone.' },
+ { since: '0.60.269.0', text: 'On a managed brain an incremental `gbrain sync` (without `--no-embed`) no longer reads every file in the repository to price its embeddings when the source\'s chunker stamp is older than the current chunker: a managed incremental sync imports only the changed files and keeps its stamp, so the cost gate now prices the delta it will actually embed. A stale stamp used to add minutes and gigabytes of memory before the first write on a large repository, show a `chunker_drift` ceiling and auto-defer embeds that were cheap. `--full` and classic sources still price the whole tree.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/sync-cost-gate.ts b/src/core/sync-cost-gate.ts
index 7131381f1e..d271affd98 100644
--- a/src/core/sync-cost-gate.ts
+++ b/src/core/sync-cost-gate.ts
@@ -38,6 +38,7 @@ import {
type EmbedBackfillWorkerSurface,
} from './minions/embed-backfill-admission.ts';
import { git, hasOriginRemote, isDetachedHead, unique } from './sync-git.ts';
+import { readManagedMode } from './persistence/managed-mode.ts';
/**
* Walk ONE source's working tree and sum tokens for every syncable file.
@@ -202,6 +203,9 @@ export interface InlineEstimate {
* 2. chunker drift (stored !== current) → full-tree CEILING (a drift forces
* performFullSync → full re-chunk → full re-embed; a delta would
* underestimate by the whole corpus). kind: ceiling_chunker_drift
+ * #6858: skipped when `chunkerDriftWalks` is false. A managed incremental
+ * sync imports only the delta and keeps the stamp (sync-prepare.ts), so
+ * the drift never walks the tree there and the delta rung prices it.
* A drift in a language's grammar revision alone (`GRAMMAR_REVISIONS`,
* CHUNKER_VERSION unchanged) prices only that language's files: the
* walk re-imports nothing else. kind: ceiling_grammar_drift
@@ -228,7 +232,7 @@ export function estimateInlineNewTokens(
chunker_version: string | null;
}>,
currentChunkerVersion: string,
- opts: { forceFullTree?: boolean } = {},
+ opts: { forceFullTree?: boolean; chunkerDriftWalks?: boolean } = {},
): InlineEstimate {
let tokens = 0;
let changedSources = 0;
@@ -262,7 +266,7 @@ export function estimateInlineNewTokens(
// Rung 2: chunker drift forces a full re-chunk → full re-embed. CEILING.
// A drift in grammar revisions alone re-chunks only those languages' files:
// the walk compares hashes and every other file's hash is unchanged.
- if (src.chunker_version !== currentChunkerVersion) {
+ if (opts.chunkerDriftWalks !== false && src.chunker_version !== currentChunkerVersion) {
const languages = grammarOnlyDrift(src.chunker_version, currentChunkerVersion);
if (languages) {
tokens += estimateLanguageTokens(localPath, strategy, languages).tokens;
@@ -546,6 +550,7 @@ export async function runInlineCostGate(
// ── Inline path ───────────────────────────────────────────────
const inline = estimateInlineNewTokens(sources, chunkerStamp(), {
forceFullTree: ctx.includeGitignored === true,
+ chunkerDriftWalks: full === true || !(await readManagedMode(engine).catch(() => false)),
});
// D7A: `--full` runs `performFullSync` → `runEmbedCore({stale:true})`, which
// sweeps the pre-existing stale backlog INLINE on top of the delta. Price it.
diff --git a/test/sync-cost-estimate.test.ts b/test/sync-cost-estimate.test.ts
index ac67d907b1..080d6cc898 100644
--- a/test/sync-cost-estimate.test.ts
+++ b/test/sync-cost-estimate.test.ts
@@ -54,6 +54,19 @@ describe('estimateInlineNewTokens — ladder', () => {
expect(r.changedSources).toBe(1);
});
+ test('#6858: a managed incremental sync keeps its stamp, so a stale stamp prices the delta, never the tree', () => {
+ writeFileSync(join(repo, 'topics/a.md'), 'an unchanged body that the delta must not price '.repeat(50));
+ const base = commitAll('base');
+ writeFileSync(join(repo, 'topics/b.md'), 'one new note');
+ const head = commitAll('one change');
+ const stale = [src({ last_commit: base, chunker_version: 'STALE-0' })];
+ const managed = estimateInlineNewTokens(stale, CURRENT, { chunkerDriftWalks: false });
+ expect(managed.estimateKind).toBe('delta');
+ expect(managed.ceilingReasons).toEqual([]);
+ expect(managed.tokens).toBe(estimateInlineNewTokens([src({ last_commit: base, chunker_version: CURRENT })], CURRENT).tokens);
+ expect(estimateInlineNewTokens([src({ last_commit: head, chunker_version: 'STALE-0' })], CURRENT, { chunkerDriftWalks: false }).estimateKind).toBe('unchanged');
+ });
+
test('[D2A headline] HEAD==last_commit + current chunker + DIRTY tree → 0 (unchanged)', () => {
writeFileSync(join(repo, 'topics/a.md'), 'body');
const head = commitAll('base');