Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(#6712 PR 4b): a kept file never classifies as a delete (design §2.3 row 4); working_tree_kept skips Git-provenance pages; 5459 row 2 per row 4
garrytan02eda842c1a7
diff --git a/src/core/persistence/sync-classify.ts b/src/core/persistence/sync-classify.tsindex 115d026645..fb1d84f32b 100644--- a/src/core/persistence/sync-classify.ts+++ b/src/core/persistence/sync-classify.ts@@ -28,7 +28,7 @@ export interface SyncPathState {tree: string | null;/** `L(p)`: committed mode `T[p]`; working mode the physical bytes' blob id (CRLF-folded / LFS-resolved to `T[p]` when clean); null = absent. */level: string | null;- /** `workingTreeFilePresent`; committed mode, `L = absent`: false for a page whose import provenance is the Git blob (`keptWorkingTreePages`). */+ /** `workingTreeFilePresent`. */present: boolean;/** Committed mode, `p ∈ T`: the candidate dirtiness; null when the pass did not probe it (only paths settled without it). */dirty: SyncDirtiness | null;diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.tsindex 2ad00e4551..0bb83b4f9f 100644--- a/src/core/persistence/sync-pass-run.ts+++ b/src/core/persistence/sync-pass-run.ts@@ -462,7 +462,7 @@ export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Pr};const identityDowngrades: DowngradedRename[] = [];const fileRefusals = new Map<string, SyncFileRefusal>();- const keptCount = () => lastLevel?.paths.filter(entry => entry.result.cls === 'kept').length ?? 0;+ const keptCount = () => lastLevel?.paths.filter(entry => entry.result.cls === 'kept' && !entry.gitProvenance).length ?? 0;const result = (status: SyncResult['status'], reason?: SyncResult['reason']): SyncResult => ({ status, fromCommit: remote ? null : context.source.last_commit, toCommit: remote ? '' : head,added: counts.added, modified: counts.modified, deleted: counts.deleted, renamed: counts.renamed, chunksCreated: counts.chunks, embedded: 0, pagesAffected: [],filesImported: sliceBase.pages, bankedFiles: sliceBase.pages, waived: { imports: counts.waivedImports, deletes: counts.waivedDeletes }, prescreened: 0,diff --git a/src/core/persistence/sync-pass.ts b/src/core/persistence/sync-pass.tsindex 9bd831e6ad..da8f3bb171 100644--- a/src/core/persistence/sync-pass.ts+++ b/src/core/persistence/sync-pass.ts@@ -74,7 +74,9 @@ export interface ShadowPath { path: string; slug: string | null; pageId: number/** A page-less path unchanged since the baseline whose page a committed request deleted: the bootstrap writes it a `legacy` marker (`legacy_pageless`). */legacyPageless?: true;/** #6517 (`selectOriginClaimant`): several live pages, or no live page and several tombstones, record this origin. */- ambiguousOrigin?: { kind: 'several_live' | 'several_tombstones'; slugs: string[] } }+ ambiguousOrigin?: { kind: 'several_live' | 'several_tombstones'; slugs: string[] };+ /** The page's import record names the pinned Git blob as its bytes (`working: false`, or a blob id on a record from before the stamp). */+ gitProvenance?: true }export interface ShadowLevel {sourceId: string; incarnation: string; root: string; gitRoot: string; scope: string; slugMode: 'git-root' | 'source-root';@@ -439,16 +441,13 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st}const fp = (path: string) => syncImportParserFor(sourceRelative(path), pack?.identity ?? null);- const stateOf = async (path: string, dirty: SyncPathState['dirty'], probe: { dp: string; rawOid: string | null; rawSha256: string | null } | undefined): Promise<{ state: SyncPathState; row?: PageRow; legacyPageless?: true }> => {+ const stateOf = async (path: string, dirty: SyncPathState['dirty'], probe: { dp: string; rawOid: string | null; rawSha256: string | null } | undefined): Promise<{ state: SyncPathState; row?: PageRow; legacyPageless?: true; gitProvenance?: true }> => {const committed = tree.get(path) ?? null;const row = byOrigin.get(path);const level = levelOf(path, probe);// Presence decides only rows 4-6 (a level of absent); a committed path in T is not read here (Dp covers it).const record = row?.record ?? null;- // #5459 (`keptWorkingTreePages`): a page whose import provenance is the pinned Git blob follows Git, so a lingering- // untracked copy of its file does not keep it.- const fromGit = record?.working === false || (record?.working === undefined && !!record?.blob_oid);- const presentNow = mode === 'working' ? level !== null : level === null ? !fromGit && readPresent(root, path, scope) : true;+ const presentNow = mode === 'working' ? level !== null : level === null ? readPresent(root, path, scope) : true;const recordBlobOids = recordOids(record);const attested = row && (row.arm1 || row.attested_hash) ? { hash: row.attested_hash ?? null, arm1: row.arm1 === true } : null;const hold = holdByGitPath.get(path);@@ -472,7 +471,9 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: stkeepDb: !mirror && keepDbPrefixes.length > 0 && !!row && isKeepDbSlug(row.slug, keepDbPrefixes),openWork: !!row && open.pageIds.has(Number(row.id)) || open.origins.has(originOf(path)),};- return { state, ...(row ? { row } : {}), ...(legacyPageless ? { legacyPageless } : {}) };+ // #5459: `working_tree_kept` counts the kept pages whose import provenance is the working tree or unknown, never the Git blob.+ const gitProvenance = record?.working === false || (record?.working === undefined && !!record?.blob_oid);+ return { state, ...(row ? { row } : {}), ...(legacyPageless ? { legacyPageless } : {}), ...(gitProvenance ? { gitProvenance } : {}) };};// Bootstrap (the incarnation's first pass; for a read-only level, no pass row yet): page-less paths at their baseline blob@@ -499,7 +500,7 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: stconst settledBeforeDirtiness = new Set(['held', 'spelling', 'untracked', 'kept', 'delete', 'delete_drift', 'gone', 'deleted_same', 'deleted_edited', 'legacy_same', 'matched']);for (const path of [...domain].sort()) {const probe = probes.get(path);- const { state, row, legacyPageless } = await stateOf(path, mode === 'committed' && tree.has(path) ? 'clean' : null, probe);+ const { state, row, legacyPageless, gitProvenance } = await stateOf(path, mode === 'committed' && tree.has(path) ? 'clean' : null, probe);const result = classifySyncPath(state);// Rows 1-10 precede row 11 and `matched` settles either way, so only the rest are Dp candidates (design v3.2 §2.1).const staged = mode === 'committed' && tree.has(path) && (index.get(path)?.oid !== tree.get(path)!.oid || (index.get(path)?.stage ?? 0) !== 0);@@ -507,7 +508,7 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: stconst claimed = claimants.get(path) ?? [];const { ambiguous } = claimed.length > 1 ? selectOriginClaimant(claimed.map(page => ({ ...page, deleted_at: page.deleted ? true : null }))) : { ambiguous: null };const ambiguousOrigin = ambiguous ? { kind: ambiguous, slugs: claimed.filter(page => ambiguous === 'several_tombstones' || !page.deleted).map(page => page.slug) } : undefined;- paths.push({ path, slug: row?.slug ?? null, pageId: row ? Number(row.id) : null, revision: row?.revision ?? null, state, result, ...(legacyPageless ? { legacyPageless } : {}), ...(ambiguousOrigin ? { ambiguousOrigin } : {}) });+ paths.push({ path, slug: row?.slug ?? null, pageId: row ? Number(row.id) : null, revision: row?.revision ?? null, state, result, ...(legacyPageless ? { legacyPageless } : {}), ...(ambiguousOrigin ? { ambiguousOrigin } : {}), ...(gitProvenance ? { gitProvenance } : {}) });}timings.classify = t();if (candidates.length) {diff --git a/test/sync-full-untracked-import-5459.test.ts b/test/sync-full-untracked-import-5459.test.tsindex f30ae49ce5..a928020853 100644--- a/test/sync-full-untracked-import-5459.test.ts+++ b/test/sync-full-untracked-import-5459.test.ts@@ -116,7 +116,7 @@ describe('--full tree walk keeps pages imported from untracked working-tree file}}), 300_000);- test('an explicit tracked Git deletion is applied even when a copy of the file lingers untracked (Git is the truth for tracked paths)', () => withEnv({ GBRAIN_HOME: home, GBRAIN_SYNC_FAILURES_DIR: home }, async () => {+ test('a Git-imported page whose file left Git but lingers untracked is kept, not counted as a working-tree import', () => withEnv({ GBRAIN_HOME: home, GBRAIN_SYNC_FAILURES_DIR: home }, async () => {for (const engine of engines) {const { r, id, live, page, incarnation, full } = await managedSource(engine);r.write('tracked'); r.commit('add tracked');@@ -126,10 +126,17 @@ describe('--full tree walk keeps pages imported from untracked working-tree filer.git('rm', '-q', '--cached', 'notes/tracked.md'); r.commitStaged('stop tracking');// The bytes are still on disk, untracked.expect(r.git('status', '--porcelain')).toContain('?? notes/tracked.md');+ // #6712 PR 4b (design v3.2 §2.3 row 4, `kept`): a live page whose file left T but is still on disk is kept in committed+ // mode, whatever its provenance; only a page imported from the working tree (or of unknown provenance) is counted.const result = await full();+ expect(await live()).toEqual(['notes/base', 'notes/tracked']);+ expect((await page('notes/tracked'))!.deleted_at).toBeNull();+ expect(result.working_tree_kept).toBeUndefined();+ // Removing the lingering copy as well is the deletion: nothing keeps the page any more.+ r.remove('tracked');+ await full();expect(await live()).toEqual(['notes/base']);expect((await page('notes/tracked'))!.deleted_at).not.toBeNull();- expect(result.working_tree_kept).toBeUndefined();expect(await readSyncImportProvenance(engine, id, incarnation, tracked.id)).toMatchObject({ working: false });}}), 300_000);