Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(notifier): a completed run settles a freshness marker once the checkout has nothing left to import (#7941)

garrytanf278aee4aa8e
diff --git a/src/core/notifier/handoff.ts b/src/core/notifier/handoff.ts
index bdd035353e..9685ce9332 100644
--- a/src/core/notifier/handoff.ts
+++ b/src/core/notifier/handoff.ts
@@ -16,7 +16,8 @@ export async function readMarkersForRun(engine: BrainEngine, sourceId: string):
/** After a completed run: clears what it satisfied; queues one follow-up job when something is still due. Returns that job. */
export async function settleRunMarkers(engine: BrainEngine, sourceId: string, read: WantedMarker[], run: { pulled: boolean; full: boolean }, opts: { queue?: boolean } = {}): Promise<{ job_id: number | null; markers: number } | null> {
try {
- const remaining = await settleMarkers(engine, sourceId, read, run);
+ const head = await (await import('./nudge.ts')).checkoutHead(engine, sourceId).catch(() => null);
+ const remaining = await settleMarkers(engine, sourceId, read, { ...run, checkoutHead: head });
if (!remaining.length) return null;
if (opts.queue === false || engine.kind === 'pglite') {
(await import('./resident.ts')).nudgeResidentNotifier();
diff --git a/src/core/notifier/nudge.ts b/src/core/notifier/nudge.ts
index 642086eb65..9f1bfbd35e 100644
--- a/src/core/notifier/nudge.ts
+++ b/src/core/notifier/nudge.ts
@@ -27,6 +27,12 @@ export async function sourceSyncShape(engine: BrainEngine, sourceId: string): Pr
return { exists: true, managed, root: (managed ? row.root : null) ?? row.local_path };
}
+/** The source checkout's HEAD now, or null when it has none or git can't read it (a settle then stays exact). */
+export async function checkoutHead(engine: BrainEngine, sourceId: string): Promise<string | null> {
+ const { root } = await sourceSyncShape(engine, sourceId);
+ return root ? revParseHead(root).catch(() => null) : null;
+}
+
/** Records the marker a trigger asks for; resolves the intent and, for freshness, the checkout's HEAD. */
export async function recordTriggerMarker(engine: BrainEngine, req: { sourceId: string; intent?: MarkerIntent; wantedHead?: string | null; requestedBy: MarkerRequester; jobId?: number | null }):
Promise<{ marker: WantedMarker; managed: boolean } | null> {
diff --git a/src/core/notifier/resident.ts b/src/core/notifier/resident.ts
index 8bd17815ee..5fb9e78e59 100644
--- a/src/core/notifier/resident.ts
+++ b/src/core/notifier/resident.ts
@@ -49,7 +49,8 @@ export function sqlNotifierStore(engine: BrainEngine): NotifierStore {
liveJobs: async (ids) => new Set((await engine.executeRaw<{ id: unknown }>(
"SELECT id FROM minion_jobs WHERE id = ANY($1::bigint[]) AND status IN ('waiting','active','delayed','waiting-children','paused')", [ids])).map(r => Number(r.id))),
forward: async (sourceId, head) => { await upsertMarker(engine, { sourceId, intent: 'freshness', wantedHead: head, requestedBy: 'notifier' }); },
- settleMarkers: (sourceId, read, run) => settleMarkers(engine, sourceId, read, run),
+ settleMarkers: async (sourceId, read, run) => settleMarkers(engine, sourceId, read,
+ { ...run, checkoutHead: await (await import('./nudge.ts')).checkoutHead(engine, sourceId).catch(() => null) }),
readStates: (ids) => readNotifierSources(engine, ids),
writeState: (sourceId, patch) => writeState(engine, sourceId, patch),
writeCoverage: (sourceId, c) => writeCoverage(engine, sourceId, c),
diff --git a/src/core/notifier/store.ts b/src/core/notifier/store.ts
index f4e3ea3818..00fcc45ad3 100644
--- a/src/core/notifier/store.ts
+++ b/src/core/notifier/store.ts
@@ -158,10 +158,12 @@ export async function clearMarker(engine: Exec, sourceId: string, intent: Marker
/**
* After a completed run: clears every marker of the source the run satisfied, generation-checked against `read` (the
* rows the drainer read before it started), and returns the markers that are still due afterwards.
- * - freshness clears when last_commit equals its wanted_head (or the marker named no head);
+ * - freshness clears when last_commit equals its wanted_head (or the marker named no head), or when the drainer saw
+ * the checkout's HEAD equal last_commit after the run (`checkoutHead`): nothing is left to import, whatever head
+ * the marker named (#7941: a head the source had passed kept every later run queuing a follow-up);
* - pull clears when the run pulled (`pulled`), full when the run was a full walk (`full`).
*/
-export async function settleMarkers(engine: Exec, sourceId: string, read: WantedMarker[], run: { pulled: boolean; full: boolean }): Promise<WantedMarker[]> {
+export async function settleMarkers(engine: Exec, sourceId: string, read: WantedMarker[], run: { pulled: boolean; full: boolean; checkoutHead?: string | null }): Promise<WantedMarker[]> {
for (const m of read) {
if (m.source_id !== sourceId) continue;
if (m.intent === 'pull' && !run.pulled) continue;
@@ -169,7 +171,8 @@ export async function settleMarkers(engine: Exec, sourceId: string, read: Wanted
if (m.intent === 'freshness') {
await engine.executeRaw(
`DELETE FROM sync_wanted_markers m USING sources s WHERE m.source_id = s.id AND m.source_id = $1 AND m.intent = 'freshness'
- AND m.generation = $2 AND (m.wanted_head IS NULL OR m.wanted_head IS NOT DISTINCT FROM s.last_commit)`, [sourceId, m.generation]);
+ AND m.generation = $2 AND (m.wanted_head IS NULL OR m.wanted_head IS NOT DISTINCT FROM s.last_commit
+ OR ($3::text IS NOT NULL AND s.last_commit = $3::text))`, [sourceId, m.generation, run.checkoutHead ?? null]);
continue;
}
await clearMarker(engine, sourceId, m.intent, m.generation);
diff --git a/test/notifier-passed-freshness-marker-7941.test.ts b/test/notifier-passed-freshness-marker-7941.test.ts
new file mode 100644
index 0000000000..aec3222bdc
--- /dev/null
+++ b/test/notifier-passed-freshness-marker-7941.test.ts
@@ -0,0 +1,112 @@
+/**
+ * #7941: a freshness marker whose wanted_head the source has already passed (an ancestor of last_commit, recorded by
+ * a run that found the source busy before more commits landed) stayed due forever. Settlement was exact equality, so
+ * every completed sync settled nothing and the sync handler queued one more priority -10 follow-up, ahead of every
+ * default-priority job, with no end. A completed run now also settles a marker it read when the source's checkout has
+ * nothing left to import (its HEAD equals last_commit after the run): no ancestry assumption, and a marker re-recorded
+ * during the run (new generation) or a checkout with commits still to import keeps its follow-up.
+ *
+ * Real git, real store, real sync. PGLite always, Postgres when DATABASE_URL is set.
+ */
+import { describe, expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { performSync } from '../src/commands/sync.ts';
+import { readDueMarkers, readMarkers, settleMarkers, upsertMarker } from '../src/core/notifier/store.ts';
+import { settleRunMarkers } from '../src/core/notifier/handoff.ts';
+import { sqlNotifierStore } from '../src/core/notifier/resident.ts';
+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
+import { isolatedSharedSkillsEngine } from './helpers/shared-skills-engine.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+const SOURCE = 'source-a';
+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
+function commit(root: string, file: string, body: string): string {
+ mkdirSync(join(root, 'notes'), { recursive: true });
+ writeFileSync(join(root, file), body);
+ git(root, 'add', '-A');
+ git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-qm', `add ${file}`);
+ return git(root, 'rev-parse', 'HEAD');
+}
+const page = (n: number) => `---\ntitle: Note ${n}\n---\nAlice Example wrote note ${n} for the acme-example ledger.\n`;
+const lastCommit = async (engine: BrainEngine) => (await engine.executeRaw<{ last_commit: string | null }>('SELECT last_commit FROM sources WHERE id=$1', [SOURCE]))[0]?.last_commit ?? null;
+const sync = (engine: BrainEngine, root: string) => performSync(engine, { sourceId: SOURCE, repoPath: root, noPull: true, noEmbed: true });
+
+const arms: Array<[string, string | undefined]> = [['pglite', undefined], ...(process.env.DATABASE_URL ? [['postgres', process.env.DATABASE_URL] as [string, string]] : [])];
+
+async function brain(databaseUrl: string | undefined, run: (b: { engine: BrainEngine; root: string; stale: string }) => Promise<void>) {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-7941-'));
+ try {
+ await withEnv({ GBRAIN_HOME: home, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined, GBRAIN_NOTIFIER: undefined }, async () => {
+ const { engine, close } = await isolatedSharedSkillsEngine(databaseUrl);
+ try {
+ const root = join(home, SOURCE); mkdirSync(root); git(root, 'init', '-q');
+ const stale = commit(root, 'notes/one.md', page(1));
+ await engine.executeRaw('INSERT INTO sources(id,name,local_path,config) VALUES($1,$1,$2,$3)', [SOURCE, root, '{}']);
+ await sync(engine, root);
+ await engine.setConfig('sync.on_commit', 'false');
+ await run({ engine, root, stale });
+ } finally { await disposePersistenceConsumer(engine); await close(); }
+ });
+ } finally { rmSync(home, { recursive: true, force: true }); }
+}
+
+/** The #7941 state: a freshness marker at `stale`, then more commits synced, so last_commit is a descendant of it. */
+async function passedMarker(engine: BrainEngine, root: string, stale: string) {
+ const marker = await upsertMarker(engine, { sourceId: SOURCE, intent: 'freshness', wantedHead: stale, requestedBy: 'lock_busy' });
+ commit(root, 'notes/two.md', page(2));
+ const head = commit(root, 'notes/three.md', page(3));
+ await sync(engine, root);
+ expect(await lastCommit(engine)).toBe(head);
+ expect((await readDueMarkers(engine, [SOURCE])).map(m => m.wanted_head)).toEqual([stale]);
+ return marker;
+}
+
+describe.each(arms)('#7941 a freshness marker the source already passed (%s)', (_arm, databaseUrl) => {
+ test('the sync handler path: a completed run settles it and queues no follow-up', async () => {
+ await brain(databaseUrl, async ({ engine, root, stale }) => {
+ const marker = await passedMarker(engine, root, stale);
+ expect(await settleRunMarkers(engine, SOURCE, [marker], { pulled: false, full: false })).toBeNull();
+ expect(await readMarkers(engine, [SOURCE])).toEqual([]);
+ expect((await engine.executeRaw("SELECT id FROM minion_jobs WHERE name='sync'")).length).toBe(0);
+ });
+ }, 120_000);
+
+ test('the resident notifier path: its store settles it', async () => {
+ await brain(databaseUrl, async ({ engine, root, stale }) => {
+ const marker = await passedMarker(engine, root, stale);
+ expect(await sqlNotifierStore(engine).settleMarkers(SOURCE, [marker], { pulled: false, full: false })).toEqual([]);
+ expect(await readMarkers(engine, [SOURCE])).toEqual([]);
+ });
+ }, 120_000);
+
+ test('commits still to import keep the marker due and the follow-up queued', async () => {
+ await brain(databaseUrl, async ({ engine, root, stale }) => {
+ const marker = await passedMarker(engine, root, stale);
+ commit(root, 'notes/four.md', page(4));
+ const followUp = await settleRunMarkers(engine, SOURCE, [marker], { pulled: false, full: false });
+ expect(followUp?.markers).toBe(1);
+ expect((await readMarkers(engine, [SOURCE])).map(m => m.wanted_head)).toEqual([stale]);
+ });
+ }, 120_000);
+
+ test('a marker re-recorded while the run was in flight (new generation) survives the settle', async () => {
+ await brain(databaseUrl, async ({ engine, root, stale }) => {
+ const read = await passedMarker(engine, root, stale);
+ await upsertMarker(engine, { sourceId: SOURCE, intent: 'freshness', wantedHead: stale, requestedBy: 'lock_busy' });
+ const remaining = await settleMarkers(engine, SOURCE, [read], { pulled: false, full: false, checkoutHead: await lastCommit(engine) });
+ expect(remaining.map(m => [m.generation, m.wanted_head])).toEqual([[read.generation + 1, stale]]);
+ });
+ }, 120_000);
+
+ test('without a checkout head the store keeps exact settlement (no ancestry assumption)', async () => {
+ await brain(databaseUrl, async ({ engine, root, stale }) => {
+ const marker = await passedMarker(engine, root, stale);
+ expect((await settleMarkers(engine, SOURCE, [marker], { pulled: false, full: false })).map(m => m.wanted_head)).toEqual([stale]);
+ expect((await settleMarkers(engine, SOURCE, [marker], { pulled: false, full: false, checkoutHead: stale })).map(m => m.wanted_head)).toEqual([stale]);
+ });
+ }, 120_000);
+});