Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sync): code/image imports record the full episode (#8047); plain sync status skips unchanged abandoned-edit holds (#7339)
garrytanc71b345db608
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bebd4b391d..6e5966c444 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,20 @@ credits are retained; no result has been reassigned to another provider. Originaidentifiers and attribution are available in the pre-removal Git revision`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).+## [Unreleased]++**A first `gbrain sync --full` on a code-strategy source now settles in one pass, and a plain `gbrain sync status` no longer issues one database statement per abandoned uncommitted edit (#8047, #7339).**++A code or image import recorded its provenance without the open `--full` episode, so every round of the pass saw those files as still owed and imported them again. A fresh code source reported twice its file count, stopped `not_converged` three passes in a row and then held every file `settle_mismatch`. Code and image imports now record the episode the way Markdown imports do, and the pass converges in its first round.++`sync status` re-reads each `worktree_dirty` hold that is due to escalate, to see whether someone is still editing the file. It read every such hold's row even when the file's bytes still matched the hash the hold already recorded. On a source with thousands of abandoned edits that was thousands of round trips per status call, about minutes over a remote pooler. It now reads a row only when the bytes changed. With 40 such holds, a plain status went from 54 statements to 15, the same count as with one hold; an edit whose bytes changed is still noticed and stops counting as abandoned.++### Itemized changes++- `src/core/persistence/sync-prepare.ts`: `syncFileProvenance` (code and image imports) records `full_episode` (#8047). The same line is in contributor PR #8051; this fix was derived independently from the #8047 reproduction.+- `src/core/persistence/sync-status.ts`: `observeDirtyEditors` skips `noteHoldEdit` when the working-tree hash equals the hold's recorded `working_hash` (#7339).+- New tests: `test/sync-full-code-episode-8047.test.ts`, `test/sync-status-hold-reads-7339.test.ts`.+## [0.60.529.0] - 2026-10-31**A pull request that adds a file to the PGLite snapshot inputs no longer gets stopped at gstack's pregate secrets check by its own workflow cache keys (#8107).**diff --git a/src/core/persistence/sync-prepare.ts b/src/core/persistence/sync-prepare.tsindex 2b5f5b9e40..3432369bd5 100644--- a/src/core/persistence/sync-prepare.ts+++ b/src/core/persistence/sync-prepare.ts@@ -475,7 +475,7 @@ function syncFileProvenance(row: WriteRequest, p: SyncIntent, root: string, impoif (!final || final.page.deleted_at != null) return;await recordSyncImportProvenance(tx, { source_id: row.source_id, incarnation: row.source_incarnation, page_id: Number(final.page.id), origin: p.sourcePath!,raw_sha256: sha256(imported), ...(p.blobOid ? { blob_oid: p.blobOid } : {}), working: p.working === true, gbrain_version: VERSION, revision: final.revision,- parser: syncImportParserFor(p.sourcePath!, null), ...syncDiskProvenance(syncDiskBytes(root, p.path!, imported, p.rawHash), p.target) });+ ...(p.fullEpisode ? { full_episode: p.fullEpisode } : {}), parser: syncImportParserFor(p.sourcePath!, null), ...syncDiskProvenance(syncDiskBytes(root, p.path!, imported, p.rawHash), p.target) });};}diff --git a/src/core/persistence/sync-status.ts b/src/core/persistence/sync-status.tsindex 32390f5955..3bf3e22084 100644--- a/src/core/persistence/sync-status.ts+++ b/src/core/persistence/sync-status.ts@@ -662,8 +662,8 @@ function workingHashOf(root: string, path: string): { workingHash?: string } {/*** #6712 PR 2 (L4): before a `worktree_dirty` hold is called abandoned, its uncommitted bytes are read from the checkout* now; bytes that changed since the hold last looked are a live editor (`edited_at` refreshed, no attempt counted), so a- * hold escalates only when nobody touched the edit for a day. Holds that would not escalate are not read. Status, unblock- * and the soak sweep all look through this.+ * hold escalates only when nobody touched the edit for a day. Holds that would not escalate are not read, and a hold whose+ * bytes match the hash it already records costs no statement (#7339). Status, unblock and the soak sweep all look through this.*/export async function observeDirtyEditors(engine: BrainEngine, sourceId: string, incarnation: string, holds: GitHoldRecord[], now = Date.now()): Promise<GitHoldRecord[]> {const due = holds.filter(hold => hold.code === 'worktree_dirty' && classifySyncFault({ code: hold.code, ...holdEscalation(hold, now) }).needs_human);@@ -673,7 +673,7 @@ export async function observeDirtyEditors(engine: BrainEngine, sourceId: string,const fresh = new Map<string, GitHoldRecord>();for (const hold of due) {const { workingHash } = workingHashOf(context.root, hold.path);- const updated = workingHash === undefined ? null : await noteHoldEdit(engine, { sourceId, incarnation, path: hold.path, workingHash });+ const updated = workingHash === undefined || workingHash === hold.meta.working_hash ? null : await noteHoldEdit(engine, { sourceId, incarnation, path: hold.path, workingHash });if (updated) fresh.set(hold.path, updated);}return holds.map(hold => fresh.get(hold.path) ?? hold);diff --git a/test/sync-full-code-episode-8047.test.ts b/test/sync-full-code-episode-8047.test.tsnew file mode 100644index 0000000000..94bf109cd8--- /dev/null+++ b/test/sync-full-code-episode-8047.test.ts@@ -0,0 +1,33 @@+import { expect, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { mkdirSync, writeFileSync } from 'node:fs';+import { dirname, join } from 'node:path';+import { runSources } from '../src/commands/sources.ts';+import { performManagedSync } from '../src/core/persistence/sync-run.ts';+import { managedBrain } from './helpers/managed-brain.ts';++const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8' }).trim();+const quiet = async <T>(run: () => Promise<T>): Promise<T> => {+ const log = console.log; console.log = () => {};+ try { return await run(); } finally { console.log = log; }+};+const files: Array<[string, string]> = [['src/alpha-example.ts', 'export const alpha = 1;\n'], ['src/beta-example.py', 'beta = 2\n'], ['lib/gamma-example.go', 'package lib\n']];++// Value: protects=a first `sync --full` on a code-strategy source settles in one pass, each code file imported once (#8047);+// fails_when=a code import's provenance stops recording the open full episode, so every round re-imports the same files and+// the pass ends partial / not_converged (then settle_mismatch holds after three passes); why_new=the full-episode tests+// cover Markdown imports only, whose provenance records the episode on its own path; seam=none; no_claim=image imports+// (same provenance helper, not exercised here) or a Postgres brain (PGLite only)+test('#8047: sync --full on a fresh code-strategy source converges in one pass', () => managedBrain(async ({ engine }) => {+ await quiet(() => runSources(engine, ['set-strategy', 'default', 'code']));+ const result = await quiet(() => performManagedSync(engine, { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true, full: true }));+ expect({ status: result.status, reason: result.reason ?? null, imported: result.filesImported, outcome: result.pass?.outcome, remaining: result.pass?.remaining })+ .toEqual({ status: 'first_sync', reason: null, imported: files.length, outcome: 'synced', remaining: 0 });+ const again = await quiet(() => performManagedSync(engine, { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true }));+ expect({ status: again.status, outcome: again.pass?.outcome ?? null }).toMatchObject({ status: 'up_to_date' });+}, { setup: ({ root }) => {+ git(root, 'init', '-q');+ for (const [path, text] of files) { mkdirSync(join(root, dirname(path)), { recursive: true }); writeFileSync(join(root, path), text); }+ git(root, 'add', '.');+ git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-qm', 'code files');+} }), 180_000);diff --git a/test/sync-status-hold-reads-7339.test.ts b/test/sync-status-hold-reads-7339.test.tsnew file mode 100644index 0000000000..1b5227d39c--- /dev/null+++ b/test/sync-status-hold-reads-7339.test.ts@@ -0,0 +1,69 @@+import { expect, test } from 'bun:test';+import { createHash } from 'node:crypto';+import { execFileSync } from 'node:child_process';+import { mkdirSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import { HOLD_ESCALATE_MS } from '../src/core/persistence/sync-fault-class.ts';+import { GIT_HOLD_OP, writeGitHold } from '../src/core/persistence/sync-holds.ts';+import { performManagedSync } from '../src/core/persistence/sync-run.ts';+import { readSyncStatus } from '../src/core/persistence/sync-status.ts';+import { managedBrain } from './helpers/managed-brain.ts';++const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8' }).trim();+const sha = (text: string) => createHash('sha256').update(text).digest('hex');+const path = (i: number) => `held/page-${i}.md`;++/** The statements the engine's top-level executeRaw sends while `fn` runs. */+async function countStatements<T>(engine: BrainEngine, fn: () => Promise<T>): Promise<{ n: number; value: T }> {+ const target = engine as unknown as Record<string, (...args: unknown[]) => Promise<unknown>>;+ const original = target.executeRaw!;+ let n = 0;+ target.executeRaw = function (this: unknown, ...args: unknown[]) { n++; return original.apply(this, args); };+ try { const value = await fn(); return { n, value }; } finally { delete target.executeRaw; }+}++/** Abandoned uncommitted edits: worktree_dirty holds escalated past the attempts and a day, each recording its file's current bytes. */+async function abandonedHolds(engine: BrainEngine, root: string, from: number, to: number): Promise<void> {+ const [source] = await engine.executeRaw<{ incarnation: string }>("SELECT incarnation::text AS incarnation FROM sources WHERE id='default'");+ await engine.transaction(async tx => {+ for (let i = from; i < to; i++) {+ writeFileSync(join(root, path(i)), `uncommitted edit ${i}\n`);+ await writeGitHold(tx, { source_id: 'default', incarnation: source!.incarnation, path: path(i), source_path: path(i), slug: `held/page-${i}`, page_id: null,+ code: 'worktree_dirty', message: 'uncommitted edit', upstream_version: null, observed_at: new Date().toISOString(), run_id: null, mode: 'managed',+ meta: { working_hash: sha(`uncommitted edit ${i}\n`) } as never });+ }+ });+ const old = new Date(Date.now() - 2 * HOLD_ESCALATE_MS).toISOString();+ await engine.executeRaw(`UPDATE op_checkpoints SET completed_keys=jsonb_build_array(jsonb_set(completed_keys->0,'{meta}',+ (completed_keys->0->'meta') || jsonb_build_object('attempts',3,'first_held_at',$2::text,'edited_at',$2::text))) WHERE op=$1`, [GIT_HOLD_OP, old]);+}++// Value: protects=a plain `sync status` costs a fixed number of statements however many abandoned uncommitted edits a source+// holds, and still notices an edit whose bytes changed (#7339: about 11k holds took minutes over a remote pooler, one+// statement per hold); fails_when=the live-editor check re-reads every escalated worktree_dirty hold's row even when its+// recorded working hash already matches the file, or stops refreshing a hold whose bytes changed; why_new=the L4 tests pin+// the escalation rule and unblock's attempts, never the status read's statement count; seam=none (the executeRaw counter+// wraps the real engine); no_claim=wall time on a remote Postgres, or the --detail reads+test('#7339: plain sync status reads abandoned worktree_dirty holds in a fixed number of statements, and still refreshes a changed edit', () => managedBrain(async ({ engine, root }) => {+ const log = console.log; console.log = () => {};+ try { await performManagedSync(engine, { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true }); } finally { console.log = log; }+ mkdirSync(join(root, 'held'));+ await abandonedHolds(engine, root, 0, 1);+ await readSyncStatus(engine, 'default', { detail: false });+ const one = await countStatements(engine, () => readSyncStatus(engine, 'default', { detail: false }));+ await abandonedHolds(engine, root, 1, 40);+ const many = await countStatements(engine, () => readSyncStatus(engine, 'default', { detail: false }));+ expect(many.value.holds.filter(hold => hold.needs_human)).toHaveLength(40);+ expect(many.n).toBe(one.n);++ writeFileSync(join(root, path(7)), 'a person is editing this again\n');+ const edited = await readSyncStatus(engine, 'default', { detail: false });+ expect(edited.holds.find(hold => hold.path === path(7))?.needs_human).toBe(false);+ expect(edited.holds.filter(hold => hold.needs_human)).toHaveLength(39);+}, { setup: ({ root }) => {+ git(root, 'init', '-q');+ writeFileSync(join(root, 'readme-example.md'), '# Example\n');+ git(root, 'add', '.');+ git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-qm', 'seed');+} }), 180_000);