Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(enrich): give thinking models output headroom in the write-up cap (#8697)
garrytanb65d68110f99
diff --git a/src/commands/enrich.ts b/src/commands/enrich.tsindex bd03e11ed4..44b5d9b964 100644--- a/src/commands/enrich.ts+++ b/src/commands/enrich.ts@@ -37,7 +37,7 @@ import { maintenancePreflight, publishMaintenancePage, type MaintenanceAuthorityimport { preserveCanonicalFences } from '../core/cycle/concept-publication.ts';import type { WriteReceipt } from '../core/persistence/types.ts';import type { OperationContext } from '../core/operations.ts';-import { configureGatewayIfUninitialized, isAvailable, chat, getChatModel, withBudgetTracker } from '../core/ai/gateway.ts';+import { configureGatewayIfUninitialized, isAvailable, chat, getChatModel, withBudgetTracker, isThinkingModel, THINKING_MODEL_MAX_OUTPUT_TOKENS } from '../core/ai/gateway.ts';import { BudgetTracker, BudgetExhausted, loadPricingOverrides, type BudgetReason, type NoPricingGuidance } from '../core/budget/budget-tracker.ts';import { noPricingSteps } from '../core/budget/no-pricing.ts';import { CapFlagError, mergeCapFlag, parseCapFlag, type CapFlag } from '../core/budget/cap-flag.ts';@@ -231,12 +231,12 @@ function completedKey(sourceId: string, slug: string): string {// Default synthesis via the gateway.// ----------------------------------------------------------------------------const defaultSynthesize: SynthesizeFn = async ({ system, user, model, abortSignal }) => {+export const defaultSynthesize: SynthesizeFn = async ({ system, user, model, abortSignal }) => {const res = await chat({model,system,messages: [{ role: 'user', content: user }],- maxTokens: 2048,+ maxTokens: isThinkingModel(model) ? THINKING_MODEL_MAX_OUTPUT_TOKENS : 2048,abortSignal,cacheSystem: true,});diff --git a/test/enrich/thinking-cap-7564.test.ts b/test/enrich/thinking-cap-7564.test.tsnew file mode 100644index 0000000000..93f9575bdf--- /dev/null+++ b/test/enrich/thinking-cap-7564.test.ts@@ -0,0 +1,37 @@+/**+ * #7564 / #8699 — enrich's write-up call must leave a thinking-by-default+ * model room to reason. A flat 2048-token cap is spent on reasoning by+ * Claude 5 (and other thinking recipes), so the write-up came back cut off+ * or, behind claude-cli, failed outright. Non-thinking models keep 2048.+ * Hermetic: the generateText seam captures the SDK args; fake keys only.+ */+import { describe, test, expect, afterEach } from 'bun:test';+import { configureGateway, resetGateway, __setGenerateTextTransportForTests, THINKING_MODEL_MAX_OUTPUT_TOKENS } from '../../src/core/ai/gateway.ts';+import { defaultSynthesize } from '../../src/commands/enrich.ts';++afterEach(() => {+ resetGateway();+ __setGenerateTextTransportForTests(null);+});++async function capturedCap(model: string): Promise<number> {+ configureGateway({ env: { ANTHROPIC_API_KEY: 'sk-fake', OPENAI_API_KEY: 'sk-fake' } });+ let captured: any;+ __setGenerateTextTransportForTests(async (args: any) => {+ captured = args;+ return { content: [{ type: 'text', text: 'write-up' }], finishReason: 'stop', usage: { inputTokens: 5, outputTokens: 1 } } as any;+ });+ const text = await defaultSynthesize({ system: 's', user: 'u', model });+ expect(text).toBe('write-up');+ return captured.maxOutputTokens;+}++describe('enrich write-up output cap (#7564, #8699)', () => {+ test('a Claude 5 model gets the thinking headroom', async () => {+ expect(await capturedCap('anthropic:claude-sonnet-5-5')).toBe(THINKING_MODEL_MAX_OUTPUT_TOKENS);+ });++ test('a non-thinking model keeps the 2048 write-up cap', async () => {+ expect(await capturedCap('openai:gpt-4o')).toBe(2048);+ });+});