Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(cycle): bound a managed extract pass that has no changed-slug list by the stale-drain budget
garrytana568dfc90719
diff --git a/src/commands/extract.ts b/src/commands/extract.tsindex 7aec18dde9..f238a05b0e 100644--- a/src/commands/extract.ts+++ b/src/commands/extract.ts@@ -724,6 +724,8 @@ export interface ExtractOpts {* `autopilot.incremental_extract_include_frontmatter` (default off).*/includeFrontmatter?: boolean;+ /** GBRA-133: on a managed brain, stop the stale-page pass after this long (the cycle's drain budget); unset runs to the end. */+ timeBudgetMs?: number;}/**@@ -762,7 +764,7 @@ export async function runExtractCore(engine: BrainEngine, opts: ExtractOpts): Prif (!dryRun && opts.mode === 'all' && opts.slugs?.length !== 0 && await managedPersistenceEnabled(engine)) {const { extractManagedStaleLinks } = await import('../core/persistence/links-maintenance.ts');const progress = createProgress(cliOptsToProgressOptions(getCliOptions())); progress.start('extract.links_fs', opts.slugs?.length);- const r = await extractManagedStaleLinks(engine, { sourceId: opts.sourceId, slugs: opts.slugs, signal: opts.signal, maxPages: opts.slugs?.length });+ const r = await extractManagedStaleLinks(engine, { sourceId: opts.sourceId, slugs: opts.slugs, signal: opts.signal, maxPages: opts.slugs?.length, timeBudgetMs: opts.timeBudgetMs });progress.finish(); return { links_created: r.created, timeline_entries_created: r.timeline, pages_processed: r.pages };}// #5904: a timeline-only pass on a managed brain publishes each page's canonical timeline through the coordinator.diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.tsindex 6cdfa373ca..0537ad6a6a 100644--- a/src/core/behavior-change-notice.ts+++ b/src/core/behavior-change-notice.ts@@ -1216,6 +1216,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;{ since: '0.60.424.0', text: 'An effect that still finds its worktree busy retries as `writer_busy` with a backoff that grows per consecutive busy retry (250 ms doubling to 60 s) instead of every 250 ms, and it never parks on contention. While it waits its data records `busy_retries`, `busy_since` and `busy_at`; they are dropped when it completes.' },{ since: '0.60.424.0', text: 'doctor `parked_effects` and `gbrain sources writer status` now report `stalled_catchups`: per source, effects that have waited on a busy canonical worktree for over 10 minutes, with when the wait began and the read-only writer status command that shows who holds the worktree. doctor warns "Catch-up not progressing" for them even when nothing is parked.' },{ since: '0.60.426.0', text: 'With `--job-isolation process`, `--max-rss` now caps each job child too: the worker measures the child\'s process tree (RssAnon + RssShmem) every 5 seconds, and a child past the cap gets SIGTERM, then SIGKILL after the 25-second grace. Its attempt fails with `ChildRssLimitError`, so a job that grows the same way on every retry ends at its `max_attempts`. Before, the cap read only the worker\'s own memory, and a runaway child grew until the host or container limit restarted every process.' },+ { since: 'TBD', text: 'On a managed brain, a cycle extract phase with no changed-page list from sync (sync left out of the phases, as for a source with sync disabled, or a sync that failed) now stops its stale-page pass after the cycle\'s 3-minute stale-drain budget instead of running over every stale page of the source. The rest stays stale and drains across later cycles, and the phase reports `stale_backlog`. `gbrain extract` and `gbrain extract --stale --catch-up` still run to the end.' },];/** The newest disclosed change's release: the notice id moves only when a release adds rows. */diff --git a/src/core/cycle.ts b/src/core/cycle.tsindex dbded5130e..0ed2250d52 100644--- a/src/core/cycle.ts+++ b/src/core/cycle.ts@@ -1412,6 +1412,8 @@ async function runPhaseExtract(signal,sourceId,includeFrontmatter, // honored on the incremental (slugs) path only+ // GBRA-133: with no slug list a managed brain's pass covers every stale page of the source; bound it like the drain below.+ timeBudgetMs: changedSlugs === undefined ? CYCLE_STALE_DRAIN_BUDGET_MS : undefined,}).catch((error: unknown) => { if (!(error instanceof ExtractBatchWriteError)) throw error; lostWrites = `; ${error.message}`; return null; });const linksCreated = result?.links_created ?? 0;const timelineCreated = result?.timeline_entries_created ?? 0;diff --git a/test/cycle-extract-managed-budget-gbra133.test.ts b/test/cycle-extract-managed-budget-gbra133.test.tsnew file mode 100644index 0000000000..4097cf4d92--- /dev/null+++ b/test/cycle-extract-managed-budget-gbra133.test.ts@@ -0,0 +1,42 @@+/**+ * GBRA-133: a managed brain's cycle extract with no changed-slug list (sync left out of the phases, or sync failed)+ * ran its targeted pass over every stale page of the source with no deadline, so a source left wholly stale by a+ * full re-import outran the cycle job's timeout every cycle while the budgeted drain behind it never ran.+ * The targeted pass now honours the cycle's stale-drain budget.+ */+import { expect, spyOn, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import type { OperationContext } from '../src/core/ops/contract.ts';+import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { CYCLE_STALE_DRAIN_BUDGET_MS, runCycle } from '../src/core/cycle.ts';+import { managedBrain, settleManagedGitEffects } from './helpers/managed-brain.ts';++const PAGES = 6;+const put = (ctx: OperationContext, slug: string, body: string) => submitPageMutation(ctx, { operation: 'put_page',+ params: { slug, request_id: randomUUID(), content: `---\ntype: concept\ntitle: "${slug}"\n---\n\n${body}\n` } });++test('managed cycle extract without changed slugs stops its targeted pass at the stale-drain budget', async () => {+ await managedBrain(async ({ engine, ctx, root }) => {+ for (let i = 0; i < PAGES; i++) await put(ctx, `notes/n${i}`, `See [[notes/n${(i + 1) % PAGES}]].`);+ await settleManagedGitEffects(engine);+ await disposePersistenceConsumer(engine);+ await engine.executeRaw("UPDATE pages SET links_extracted_at = NULL WHERE source_id = 'default'");+ // The first page read moves the clock past the budget, as a page late in a long pass would.+ let offset = 0;+ const realNow = Date.now.bind(Date);+ const clock = spyOn(Date, 'now').mockImplementation(() => realNow() + offset);+ const read = engine.readPageSnapshot;+ const snapshot = spyOn(engine, 'readPageSnapshot').mockImplementation(function (this: typeof engine, ...args: Parameters<typeof engine.readPageSnapshot>) {+ offset ||= CYCLE_STALE_DRAIN_BUDGET_MS + 60_000;+ return read.apply(this, args);+ });+ try {+ const report = await runCycle(engine, { brainDir: root, sourceId: 'default', phases: ['extract'] });+ const details = report.phases.find(phase => phase.phase === 'extract')?.details ?? {};+ expect(details.incremental).toBe(false);+ expect(details.pages_processed as number).toBeLessThan(PAGES);+ expect((details.pages_processed as number) + (details.stale_pages_drained as number)).toBe(PAGES);+ } finally { clock.mockRestore(); snapshot.mockRestore(); }+ });+}, 120_000);