Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(propose_takes): scale extractor call timeout with its output cap (#5771)

garrytan52a84671fd4e
diff --git a/src/core/cycle/propose-takes.ts b/src/core/cycle/propose-takes.ts
index e1ca215296..d0fa1e773e 100644
--- a/src/core/cycle/propose-takes.ts
+++ b/src/core/cycle/propose-takes.ts
@@ -351,8 +351,11 @@ export function extractExistingTakesForDedup(pageBody: string): Array<{
return rows;
}
-/** Per-call wall-clock timeout for the extractor LLM call. */
+/** Per-call wall-clock timeout for the extractor LLM call at the base cap. */
const EXTRACTOR_CALL_TIMEOUT_MS = 90_000;
+/** Ceiling for the scaled timeout: the gateway's own chat default
+ * (GBRAIN_AI_CHAT_TIMEOUT_MS, 300s) bounds the call regardless. */
+const EXTRACTOR_CALL_TIMEOUT_MAX_MS = 300_000;
/**
* #3763 — output caps for the extractor call. A stopReason 'length' response
@@ -373,6 +376,20 @@ const EXTRACTOR_CALL_TIMEOUT_MS = 90_000;
export const PROPOSE_TAKES_MAX_TOKENS = 2048;
export const PROPOSE_TAKES_RETRY_MAX_TOKENS = 4096;
+/**
+ * Wall-clock timeout for one extractor call, scaled with its output cap
+ * (#5771). A flat 90s sized for the 2048-token base call starved the
+ * truncation retry: at a configured retry_max_tokens of 12000 a dense page's
+ * retry needs well over 90s to generate, so it timed out every cycle and the
+ * page was re-billed (base call + aborted retry) forever with no tombstone.
+ * 90s per PROPOSE_TAKES_MAX_TOKENS of output, floored at 90s, capped at
+ * EXTRACTOR_CALL_TIMEOUT_MAX_MS.
+ */
+function extractorCallTimeoutMs(maxTokens: number): number {
+ const scaled = Math.ceil((EXTRACTOR_CALL_TIMEOUT_MS * maxTokens) / PROPOSE_TAKES_MAX_TOKENS);
+ return Math.min(EXTRACTOR_CALL_TIMEOUT_MAX_MS, Math.max(EXTRACTOR_CALL_TIMEOUT_MS, scaled));
+}
+
/**
* #3763 — halt streak for a dead extractor lane. When EVERY extractor call in
* the run has failed (zero successes) and the failure count reaches this
@@ -416,11 +433,13 @@ export async function defaultExtractor(
// Bound each call so one stalled provider socket can't pin the phase for the
// full gateway default (GBRAIN_AI_CHAT_TIMEOUT_MS, 300s) x pageLimit. The
// caller already catches per-page errors, logs a warning, and continues.
+ // The bound scales with maxTokens so an escalated or configured larger cap
+ // gets time to generate what it allows.
const call = (maxTokens: number) => gatewayChat({
messages: [{ role: 'user', content: prompt }],
...(input.modelHint ? { model: input.modelHint } : {}),
maxTokens,
- abortSignal: AbortSignal.timeout(EXTRACTOR_CALL_TIMEOUT_MS),
+ abortSignal: AbortSignal.timeout(extractorCallTimeoutMs(maxTokens)),
});
let result = await call(baseMaxTokens);
diff --git a/test/propose-takes-truncation.test.ts b/test/propose-takes-truncation.test.ts
index 16e3074270..1011422cd9 100644
--- a/test/propose-takes-truncation.test.ts
+++ b/test/propose-takes-truncation.test.ts
@@ -129,6 +129,48 @@ describe('defaultExtractor truncation retry (#3763)', () => {
});
});
+// ─── per-call timeout scales with the output cap (#5771) ────────────
+
+describe('defaultExtractor per-call timeout scales with maxTokens (#5771)', () => {
+ const input = {
+ pagePath: 'companies/acme-example',
+ pageBody: 'I bet Acme doubles ARR by Q4. They ship weekly.',
+ existingTakes: [],
+ };
+
+ async function timeoutsFor(extra: { maxTokens?: number; retryMaxTokens?: number }): Promise<number[]> {
+ const timeouts: number[] = [];
+ const realTimeout = AbortSignal.timeout;
+ AbortSignal.timeout = ((ms: number) => {
+ timeouts.push(ms);
+ return realTimeout.call(AbortSignal, ms);
+ }) as typeof AbortSignal.timeout;
+ let calls = 0;
+ __setChatTransportForTests(async () => {
+ calls++;
+ return calls === 1 ? chatResult('[{"claim_text":"Acme dou', 'length') : chatResult(GOOD_JSON, 'end');
+ });
+ try {
+ await defaultExtractor({ ...input, ...extra });
+ } finally {
+ AbortSignal.timeout = realTimeout;
+ }
+ return timeouts;
+ }
+
+ test('base call keeps 90s; the default 4096 retry gets 180s', async () => {
+ expect(await timeoutsFor({})).toEqual([90_000, 180_000]);
+ });
+
+ test('a large configured retry cap is bounded at the 300s gateway ceiling, not 90s', async () => {
+ expect(await timeoutsFor({ retryMaxTokens: 12_000 })).toEqual([90_000, 300_000]);
+ });
+
+ test('small caps never drop below the 90s floor', async () => {
+ expect(await timeoutsFor({ maxTokens: 256, retryMaxTokens: 256 })).toEqual([90_000, 90_000]);
+ });
+});
+
// ─── phase-level dead-lane halt ─────────────────────────────────────
interface CapturedSql { sql: string; params: unknown[] }