Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(patterns): a child stopped at its own timeout records timeout so the next plan halves (#6296)
time-attackc7c4025dcb77
diff --git a/docs/guides/dream-patterns.md b/docs/guides/dream-patterns.mdindex 6fed736285..f56b705973 100644--- a/docs/guides/dream-patterns.md+++ b/docs/guides/dream-patterns.md@@ -37,7 +37,9 @@ An in-cycle run sizes itself from that record:- With history, it estimates milliseconds per reflection with a 1.25× marginand submits the newest reflections that fit in the remaining budget. Atimed-out run counts as a lower bound on cost, so the next run is also at- most half its size. A completed run after shrinking restores the estimate.+ most half its size. A child the job queue stopped at its own timeout+ (`timeout exceeded` or `wall-clock timeout exceeded`) counts as timed out+ too. A completed run after shrinking restores the estimate.- With no history (or after a failed child), it submits a conservative firstbatch: 25 reflections, or `min_evidence` if larger.- When fewer than `min_evidence` reflections fit, the run is skipped with@@ -51,8 +53,13 @@ A direct `gbrain dream --phase patterns` has no cycle deadline: it keeps itsfull size (up to 100 reflections) and records `last_run` like an in-cyclerun. It is the skip's `fix`; it is a paid run, so ask the user first.-The linear cost model has not been measured on a real paid run yet.+The linear cost model has not been measured on a real paid run yet. It scales+cost with the number of reflections submitted, but a child also reads the+existing pattern pages, so on a brain with many of them the cost falls more+slowly than the reflection count and halving may take several cycles to fit.## Changelog- Fix wave 11: budget sizing from `dream.patterns.last_run` (#6177).+- Fix wave 13: a child stopped at its own timeout records `timeout`, so the+ next plan halves (#6296).diff --git a/src/core/cycle/patterns-plan.ts b/src/core/cycle/patterns-plan.tsindex 448f098ff9..7261f60d71 100644--- a/src/core/cycle/patterns-plan.ts+++ b/src/core/cycle/patterns-plan.ts@@ -75,9 +75,15 @@ export async function readPatternsLastRun(engine: BrainEngine): Promise<Patterns}}-/** Record a child's cost; any end other than completed or timeout is `failed`. */-export async function recordPatternsLastRun(engine: BrainEngine, run: { duration_ms: number; reflections: number; outcome: string }): Promise<void> {- const outcome: PatternsLastRun['outcome'] = run.outcome === 'completed' || run.outcome === 'timeout' ? run.outcome : 'failed';+/**+ * Record a child's cost; any end other than completed or timeout is `failed`.+ * #6296: a child dead-lettered at its own timeout_ms (`timeout exceeded`,+ * `wall-clock timeout exceeded`) is a timeout, so the next plan halves.+ */+export async function recordPatternsLastRun(engine: BrainEngine,+ run: { duration_ms: number; reflections: number; outcome: string; error_text?: string | null }): Promise<void> {+ const timedOut = run.outcome === 'timeout' || (run.outcome === 'dead' && /^(wall-clock )?timeout exceeded$/.test(run.error_text ?? ''));+ const outcome: PatternsLastRun['outcome'] = timedOut ? 'timeout' : run.outcome === 'completed' ? 'completed' : 'failed';const record: PatternsLastRun = { duration_ms: run.duration_ms, reflections: run.reflections, at: new Date().toISOString(), outcome, budget_skips: 0 };await engine.setConfig(PATTERNS_LAST_RUN_KEY, JSON.stringify(record)).catch(() => undefined);}diff --git a/src/core/cycle/patterns.ts b/src/core/cycle/patterns.tsindex 5593570039..aec9544449 100644--- a/src/core/cycle/patterns.ts+++ b/src/core/cycle/patterns.ts@@ -352,6 +352,7 @@ async function runPatternsPass(engine: BrainEngine, opts: PatternsPhaseOpts, con);let outcome: MinionJobStatus | 'timeout';+ let errorText: string | null = null;try {const final = await waitForCompletionRenewing(queue, job.id, {timeoutMs: budgets.waitTimeoutMs,@@ -363,6 +364,7 @@ async function runPatternsPass(engine: BrainEngine, opts: PatternsPhaseOpts, con// throwing — unwind before treating it as an outcome.throwIfAborted(opts.signal, '[dream] patterns completion wait');outcome = final.status;+ errorText = final.error_text ?? null;} catch (e) {if (e instanceof TimeoutError) {outcome = 'timeout';@@ -378,7 +380,7 @@ async function runPatternsPass(engine: BrainEngine, opts: PatternsPhaseOpts, con}}- await recordPatternsLastRun(engine, { duration_ms: Date.now() - submittedAt, reflections: submitted.length, outcome }); // #6177: every child, timed out or failed too+ await recordPatternsLastRun(engine, { duration_ms: Date.now() - submittedAt, reflections: submitted.length, outcome, error_text: errorText }); // #6177: every child, timed out or failed tooif (outcome === 'completed' && submitOpts.idempotency_key) await clearPatternsSourceDeaths(engine, opts.sourceId ?? 'default'); // #6236: only consecutive deaths tripif (opts.yieldDuringPhase) {diff --git a/test/cycle-patterns-plan.test.ts b/test/cycle-patterns-plan.test.tsindex c8bec4bbfd..c4bc3dddd7 100644--- a/test/cycle-patterns-plan.test.ts+++ b/test/cycle-patterns-plan.test.ts@@ -20,7 +20,7 @@ import { join } from 'node:path';import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { resetPgliteState } from './helpers/reset-pglite.ts';import { runPhasePatterns } from '../src/core/cycle/patterns.ts';-import { PATTERNS_LAST_RUN_KEY, planPatternsRun, type PatternsLastRun } from '../src/core/cycle/patterns-plan.ts';+import { PATTERNS_LAST_RUN_KEY, planPatternsRun, readPatternsLastRun, recordPatternsLastRun, type PatternsLastRun } from '../src/core/cycle/patterns-plan.ts';import { withEnv } from './helpers/with-env.ts';import { __setChatTransportForTests, resetGateway } from '../src/core/ai/gateway.ts';@@ -111,6 +111,18 @@ describe('runPhasePatterns budget sizing (#6177)', () => {} finally { rmSync(brainDir, { recursive: true, force: true }); }});+ test('a child dead-lettered at its own timeout records timeout, so the next plan halves (#6296)', async () => {+ for (const error_text of ['timeout exceeded', 'wall-clock timeout exceeded']) {+ await recordPatternsLastRun(engine, { duration_ms: 20 * MIN, reflections: 40, outcome: 'dead', error_text });+ const last = await readPatternsLastRun(engine);+ expect(last?.outcome).toBe('timeout');+ const plan = planPatternsRun({ budgetMs: 28 * MIN, lastRun: last, reflections: 100, minEvidence: 3, nowMs: Date.now() });+ expect(plan.n).toBeLessThanOrEqual(20);+ }+ await recordPatternsLastRun(engine, { duration_ms: 20 * MIN, reflections: 40, outcome: 'dead', error_text: 'provider down' });+ expect((await readPatternsLastRun(engine))?.outcome).toBe('failed');+ });+test('a child that ends without completing still records last_run', async () => {const brainDir = mkdtempSync(join(tmpdir(), 'gbrain-patterns-plan-'));try {