Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(loops_extract): escalate maxTokens once on truncation (#5077)

time-attack64647f5c3e69
diff --git a/src/core/google/loops-extract.ts b/src/core/google/loops-extract.ts
index 94dba40b6f..db1b99b2c6 100644
--- a/src/core/google/loops-extract.ts
+++ b/src/core/google/loops-extract.ts
@@ -50,6 +50,15 @@ export const LOOPS_EXTRACT_MAX_PER_SWEEP = 50;
export const LOOPS_EXTRACT_ENQUEUE_CEILING = 500;
/** Only threads whose newest message is within this window get extracted. */
export const LOOPS_EXTRACT_WINDOW_DAYS = 30;
+/**
+ * Judge output caps (#3763 parity with propose_takes). A dense thread can
+ * extract more loops than the base cap carries, and thinking models spend
+ * reasoning tokens inside it. A retry at the same cap truncates identically,
+ * so a `length` stop retries once at the escalated cap; a second truncation
+ * fails the job with the ceiling named.
+ */
+const LOOPS_EXTRACT_MAX_TOKENS = 2048;
+const LOOPS_EXTRACT_RETRY_MAX_TOKENS = 8192;
/** Gmail categories that are bulk by construction. */
const BULK_CATEGORY_LABELS = ['CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS'];
@@ -348,16 +357,19 @@ export async function runLoopsExtract(
let text: string;
try {
- const res = await chat({
- system: JUDGE_SYSTEM,
- messages: [
- {
- role: 'user',
- content: `<thread subject=${JSON.stringify(page.title ?? '')} account_owner="me">\n${content}\n</thread>\n\nExtract the open loops.`,
- },
- ],
- maxTokens: 2000,
- });
+ const call = (maxTokens: number) =>
+ chat({
+ system: JUDGE_SYSTEM,
+ messages: [
+ {
+ role: 'user',
+ content: `<thread subject=${JSON.stringify(page.title ?? '')} account_owner="me">\n${content}\n</thread>\n\nExtract the open loops.`,
+ },
+ ],
+ maxTokens,
+ });
+ let res = await call(LOOPS_EXTRACT_MAX_TOKENS);
+ if (res.stopReason === 'length') res = await call(LOOPS_EXTRACT_RETRY_MAX_TOKENS);
if (res.stopReason === 'refusal' || res.stopReason === 'content_filter') {
return { ...empty, reason: 'refused' };
}
@@ -369,7 +381,9 @@ export async function runLoopsExtract(
if (res.stopReason === 'length') {
throw new LoopsExtractRetryableError(
'truncated',
- 'loops_extract: model output truncated (stopReason=length) — retryable',
+ `loops_extract: model output truncated (stopReason=length) at the ${LOOPS_EXTRACT_RETRY_MAX_TOKENS}-token ` +
+ `ceiling after one escalation from ${LOOPS_EXTRACT_MAX_TOKENS}; the thread extracts more loops than the ` +
+ 'ceiling carries — retryable',
);
}
text = res.text;
diff --git a/test/loops-extract-run.serial.test.ts b/test/loops-extract-run.serial.test.ts
index 2679be4ecb..75ba675601 100644
--- a/test/loops-extract-run.serial.test.ts
+++ b/test/loops-extract-run.serial.test.ts
@@ -394,6 +394,63 @@ describe('runLoopsExtract', () => {
expect(await countLoops()).toBe(0); // none of the failure paths wrote anything
});
+ test('truncation escalates maxTokens once (2048 → 8192) and the escalated answer lands', async () => {
+ // A retry at the same cap truncates identically, so without the
+ // escalation a dense thread is dead after max_attempts with the revision
+ // never extracted.
+ const caps: number[] = [];
+ chatImpl = async () => {
+ caps.push(lastChatReq?.maxTokens ?? -1);
+ return caps.length === 1
+ ? { text: 'partial…', stopReason: 'length' }
+ : { text: '{"commitments":[],"decisions_pending":[]}', stopReason: 'end' };
+ };
+
+ const r = await runLoopsExtract(engine, { slug: EMAIL_SLUG, sourceId: SRC });
+
+ expect(caps).toEqual([2048, 8192]);
+ expect(r.status).toBe('extracted');
+ expect(await countLoops()).toBe(0);
+ });
+
+ test('still truncated at the 8192 ceiling → THROWS naming the ceiling, after exactly two calls', async () => {
+ const caps: number[] = [];
+ chatImpl = async () => {
+ caps.push(lastChatReq?.maxTokens ?? -1);
+ return { text: 'partial…', stopReason: 'length' };
+ };
+
+ const err = await runLoopsExtract(engine, { slug: EMAIL_SLUG, sourceId: SRC }).catch((e: unknown) => e);
+
+ expect(err).toBeInstanceOf(Error);
+ expect((err as { reason?: string }).reason).toBe('truncated');
+ expect((err as Error).message).toContain('at the 8192-token ceiling after one escalation from 2048');
+ expect(caps).toEqual([2048, 8192]);
+ expect(await countLoops()).toBe(0);
+ });
+
+ test('a non-length provider error is not escalated: one call, error propagates', async () => {
+ const caps: number[] = [];
+ chatImpl = async () => {
+ caps.push(lastChatReq?.maxTokens ?? -1);
+ throw new Error('synthetic provider 500');
+ };
+
+ await expect(runLoopsExtract(engine, { slug: EMAIL_SLUG, sourceId: SRC })).rejects.toThrow(
+ 'synthetic provider 500',
+ );
+ expect(caps).toEqual([2048]);
+
+ caps.length = 0;
+ chatImpl = async () => {
+ caps.push(lastChatReq?.maxTokens ?? -1);
+ return { text: '', stopReason: 'refusal' };
+ };
+ const r = await runLoopsExtract(engine, { slug: EMAIL_SLUG, sourceId: SRC });
+ expect(r.reason).toBe('refused');
+ expect(caps).toEqual([2048]);
+ });
+
test('parse failure (garbage response) THROWS the all-or-nothing barrier, ZERO open_loops rows', async () => {
chatImpl = async () => ({
text: 'I found some commitments but here they are in prose, not JSON.',