Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Merge remote-tracking branch 'origin/capy/w9-lane-b' into capy/fix-wave-9

time-attackd25457788ea6
diff --cc src/core/ai/gateway.ts
index 2b570bd017,c5073a3be4..c36381d0b7
--- a/src/core/ai/gateway.ts
+++ b/src/core/ai/gateway.ts
@@@ -34,9 -34,10 +34,10 @@@ import { z } from 'zod'
import { DEFAULT_CHARS_PER_TOKEN, DEFAULT_SAFETY_FACTOR, embedRequestMaxInputTokens, planEmbedRequests, rerankRequestMaxInputTokens, truncateEmbedInputs } from './embed-batch-plan.ts';
export { splitByTokenBudget, capBatchItems, NO_BATCH_CAP_SUB_BATCH_ITEMS } from './embed-batch-plan.ts';
-import { BudgetTracker, type BudgetKind } from '../budget/budget-tracker.ts';
+import { BudgetTracker, type BudgetReservation } from '../budget/budget-tracker.ts';
import { failedCallUsage, recordOnTracker } from './budget-record.ts';
import { chatWithFallback, normalizeChatFallbackChain } from './chat-fallback.ts';
+ import { applyThinkingOff, thinkingOffMaxOutputTokens } from './thinking-off.ts';
import type {
AIGatewayConfig,
EmbedMultimodalOpts,
@@@ -3442,27 -3464,22 +3456,27 @@@ export async function chat(opts: ChatOp
}
}
const estimatedInputTokens = estimateChatInputTokens(opts);
- const maxOutputTokens = opts.maxTokens ?? defaultMaxOutputTokens(modelStrEarly);
+ const maxOutputTokens = thinkingOffMaxOutputTokens(modelStrEarly, opts.maxTokens ?? defaultMaxOutputTokens(modelStrEarly), opts.thinking === 'off' && isThinkingModel(modelStrEarly), THINKING_MODEL_MAX_OUTPUT_TOKENS);
- const chatRecord = { requestedModelId: modelStrEarly, purpose: opts.purpose, label: 'gateway.chat' };
-
- // TX5: reserve BEFORE the provider call. Throws BudgetExhausted on cost,
- // runtime, or no_pricing (when cap is set). Pre-resolution model id is
- // fine here — resolveChatProvider would map aliases the same way for the
- // cost lookup. record() below uses the real result.model.
- if (tracker) {
- tracker.reserve({
- modelId: modelStrEarly,
- estimatedInputTokens,
- maxOutputTokens,
- kind: 'chat' as BudgetKind,
- label: 'gateway.chat',
- });
+
+ // TX5: reserve BEFORE the provider call (BudgetExhausted on cost, runtime,
+ // or no_pricing under a user cap) with the pre-resolution model id. record()
+ // settles the reservation by id; the finally frees an attempt that never
+ // billed (provider resolution failed, an invocation policy refused it).
+ const reservation = tracker?.reserve({ modelId: modelStrEarly, estimatedInputTokens, maxOutputTokens, kind: 'chat', label: 'gateway.chat' });
+ const chatRecord = { requestedModelId: modelStrEarly, purpose: opts.purpose, label: 'gateway.chat', reservation };
+ try {
+ return await chatAdmitted(opts, { tracker, modelStrEarly, estimatedInputTokens, maxOutputTokens, chatRecord });
+ } finally {
+ tracker?.release(reservation);
}
+}
+
+/** One admitted chat attempt: the provider call plus its budget record. */
+async function chatAdmitted(opts: ChatOpts, admitted: {
+ tracker: BudgetTracker | null; modelStrEarly: string; estimatedInputTokens: number; maxOutputTokens: number;
+ chatRecord: { requestedModelId: string; purpose?: string; label: string; reservation?: BudgetReservation };
+}): Promise<ChatResult> {
+ const { tracker, modelStrEarly, estimatedInputTokens, maxOutputTokens, chatRecord } = admitted;
// Test seam: when a test transport is installed, route through it without
// touching provider resolution, AI SDK, or any network. See