Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(cycle): start a connector cycle's changed-page window when it lists pages, not at its end stamp
garrytana6d437dc05e9
diff --git a/src/core/cycle.ts b/src/core/cycle.tsindex c152c0f264..897319dc23 100644--- a/src/core/cycle.ts+++ b/src/core/cycle.ts@@ -1972,6 +1972,8 @@ async function runCycleUnscoped(? (opts.sourceId ?? (await resolveSourceForDir(engine, brainDir))): opts.sourceId;const orphansSourceId = opts.forceGlobalOrphans ? undefined : cycleSourceId;+ /** #6803: when this cycle began listing the source's changed pages (connector cycles only). */+ let sourceWindowAt: string | undefined;const progress = createProgress(cliOptsToProgressOptions(getCliOptions()));@@ -2260,9 +2262,12 @@ async function runCycleUnscoped(let syncAttempted = false;let synthesizeWrittenSlugs: string[] | undefined;const sourceChangedSlugs = engine && cycleSourceId && opts.sourceChangedSince !== undefined- ? (await engine.executeRaw<{ slug: string }>(- 'SELECT slug FROM pages WHERE source_id=$1 AND deleted_at IS NULL AND ($2::timestamptz IS NULL OR updated_at>$2::timestamptz) ORDER BY slug',- [cycleSourceId, opts.sourceChangedSince])).map(row => row.slug)+ ? await (async () => {+ sourceWindowAt = (await engine.executeRaw<{ at: string }>('SELECT clock_timestamp()::text AS at'))[0]?.at;+ return (await engine.executeRaw<{ slug: string }>(+ 'SELECT slug FROM pages WHERE source_id=$1 AND deleted_at IS NULL AND ($2::timestamptz IS NULL OR updated_at>$2::timestamptz) ORDER BY slug',+ [cycleSourceId, opts.sourceChangedSince])).map(row => row.slug);+ })(): undefined;if (phases.includes('sync')) {checkAborted(cycleSignal);@@ -3169,6 +3174,8 @@ async function runCycleUnscoped(await engine.updateSourceConfig(opts.sourceId, {last_source_cycle_at: nowIso,last_full_cycle_at: nowIso,+ // #6803: where the next connector cycle's changed-page window starts (read before this cycle listed its pages).+ ...(sourceWindowAt ? { last_source_cycle_window_at: sourceWindowAt } : {}),});} catch (e) {const message = e instanceof Error ? e.message : String(e);diff --git a/src/core/minions/handlers/autopilot-cycle.ts b/src/core/minions/handlers/autopilot-cycle.tsindex 7d5d8ce715..c5fa2ebd40 100644--- a/src/core/minions/handlers/autopilot-cycle.ts+++ b/src/core/minions/handlers/autopilot-cycle.ts@@ -68,7 +68,7 @@ export function makeAutopilotCycleHandler(engine: BrainEngine): MinionHandler {// source's own checkout (the #2227/#2194 mixed-scope fix).const rows = await engine.executeRaw<{ archived: boolean | null; local_path: string | null; kind: string | null; last_cycle: string | null }>(`SELECT archived, local_path, config->>'kind' AS kind,- COALESCE(config->>'last_source_cycle_at', config->>'last_full_cycle_at') AS last_cycle FROM sources WHERE id = $1`,+ COALESCE(config->>'last_source_cycle_window_at', config->>'last_source_cycle_at', config->>'last_full_cycle_at') AS last_cycle FROM sources WHERE id = $1`,[rawSourceId],);if (rows.length === 0) {diff --git a/test/connector-cycle-extract-facts-scope.serial.test.ts b/test/connector-cycle-extract-facts-scope.serial.test.tsindex 0e1f0eb7ab..008626716c 100644--- a/test/connector-cycle-extract-facts-scope.serial.test.ts+++ b/test/connector-cycle-extract-facts-scope.serial.test.ts@@ -53,6 +53,14 @@ describe('#6803 connector cycle extract_facts scope', () => {// The next cycle starts from this cycle's stamp: nothing in the source changed since.const again = await makeAutopilotCycleHandler(engine)({ id: 2, data: { source_id: connector } } as never) as typeof out;expect(again.report.phases.find(p => p.phase === 'recompute_emotional_weight')?.details).toMatchObject({ mode: 'incremental', pages_recomputed: 0 });+ // A page that changed while a cycle ran (after it listed its pages, before its stamp) is picked up by the next one.+ const [stamps] = await engine.executeRaw<{ window_at: string; stamp: string }>(+ "SELECT config->>'last_source_cycle_window_at' AS window_at, config->>'last_source_cycle_at' AS stamp FROM sources WHERE id=$1", [connector]);+ expect(Date.parse(stamps.window_at)).toBeLessThan(Date.parse(stamps.stamp));+ await engine.executeRaw("UPDATE pages SET updated_at=$2::timestamptz + interval '1 millisecond' WHERE source_id=$1 AND slug='emails/example'", [connector, stamps.window_at]);+ expect((await engine.executeRaw<{ before: boolean }>("SELECT updated_at < $2::timestamptz AS before FROM pages WHERE source_id=$1 AND slug='emails/example'", [connector, stamps.stamp]))[0].before).toBe(true);+ const third = await makeAutopilotCycleHandler(engine)({ id: 3, data: { source_id: connector } } as never) as typeof out;+ expect(third.report.phases.find(p => p.phase === 'recompute_emotional_weight')?.details).toMatchObject({ mode: 'incremental', pages_recomputed: 1 });await disposePersistenceConsumer(engine);await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');}