@qmediat.io/gemini-code-context-mcp 1.23.1 → 1.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +161 -1
- package/README.md +1 -1
- package/dist/cache/ttl-watcher.d.ts +29 -0
- package/dist/cache/ttl-watcher.d.ts.map +1 -1
- package/dist/cache/ttl-watcher.js +104 -43
- package/dist/cache/ttl-watcher.js.map +1 -1
- package/dist/gemini/client.d.ts +7 -0
- package/dist/gemini/client.d.ts.map +1 -1
- package/dist/gemini/client.js +8 -1
- package/dist/gemini/client.js.map +1 -1
- package/dist/gemini/long-decode.d.ts +153 -0
- package/dist/gemini/long-decode.d.ts.map +1 -0
- package/dist/gemini/long-decode.js +178 -0
- package/dist/gemini/long-decode.js.map +1 -0
- package/dist/gemini/model-registry.d.ts +2 -0
- package/dist/gemini/model-registry.d.ts.map +1 -1
- package/dist/gemini/model-registry.js +5 -2
- package/dist/gemini/model-registry.js.map +1 -1
- package/dist/gemini/models.d.ts.map +1 -1
- package/dist/gemini/models.js +1 -0
- package/dist/gemini/models.js.map +1 -1
- package/dist/gemini/retry.d.ts +8 -0
- package/dist/gemini/retry.d.ts.map +1 -1
- package/dist/gemini/retry.js +33 -1
- package/dist/gemini/retry.js.map +1 -1
- package/dist/tools/agentic/dispatch.d.ts +68 -0
- package/dist/tools/agentic/dispatch.d.ts.map +1 -0
- package/dist/tools/agentic/dispatch.js +167 -0
- package/dist/tools/agentic/dispatch.js.map +1 -0
- package/dist/tools/agentic/input.d.ts +28 -0
- package/dist/tools/agentic/input.d.ts.map +1 -0
- package/dist/tools/agentic/input.js +66 -0
- package/dist/tools/agentic/input.js.map +1 -0
- package/dist/tools/agentic/instructions.d.ts +46 -0
- package/dist/tools/agentic/instructions.d.ts.map +1 -0
- package/dist/tools/agentic/instructions.js +170 -0
- package/dist/tools/agentic/instructions.js.map +1 -0
- package/dist/tools/agentic/iteration.d.ts +81 -0
- package/dist/tools/agentic/iteration.d.ts.map +1 -0
- package/dist/tools/agentic/iteration.js +317 -0
- package/dist/tools/agentic/iteration.js.map +1 -0
- package/dist/tools/agentic/ledger.d.ts +64 -0
- package/dist/tools/agentic/ledger.d.ts.map +1 -0
- package/dist/tools/agentic/ledger.js +110 -0
- package/dist/tools/agentic/ledger.js.map +1 -0
- package/dist/tools/agentic/loop.d.ts +10 -0
- package/dist/tools/agentic/loop.d.ts.map +1 -0
- package/dist/tools/agentic/loop.js +179 -0
- package/dist/tools/agentic/loop.js.map +1 -0
- package/dist/tools/agentic/no-progress.d.ts +90 -0
- package/dist/tools/agentic/no-progress.d.ts.map +1 -0
- package/dist/tools/agentic/no-progress.js +107 -0
- package/dist/tools/agentic/no-progress.js.map +1 -0
- package/dist/tools/agentic/pass-clock.d.ts +85 -0
- package/dist/tools/agentic/pass-clock.d.ts.map +1 -0
- package/dist/tools/agentic/pass-clock.js +162 -0
- package/dist/tools/agentic/pass-clock.js.map +1 -0
- package/dist/tools/agentic/phase-defaults.d.ts +29 -0
- package/dist/tools/agentic/phase-defaults.d.ts.map +1 -0
- package/dist/tools/agentic/phase-defaults.js +23 -0
- package/dist/tools/agentic/phase-defaults.js.map +1 -0
- package/dist/tools/agentic/rescue.d.ts +69 -0
- package/dist/tools/agentic/rescue.d.ts.map +1 -0
- package/dist/tools/agentic/rescue.js +203 -0
- package/dist/tools/agentic/rescue.js.map +1 -0
- package/dist/tools/agentic/response.d.ts +16 -0
- package/dist/tools/agentic/response.d.ts.map +1 -0
- package/dist/tools/agentic/response.js +25 -0
- package/dist/tools/agentic/response.js.map +1 -0
- package/dist/tools/agentic/results.d.ts +76 -0
- package/dist/tools/agentic/results.d.ts.map +1 -0
- package/dist/tools/agentic/results.js +228 -0
- package/dist/tools/agentic/results.js.map +1 -0
- package/dist/tools/agentic/run.d.ts +130 -0
- package/dist/tools/agentic/run.d.ts.map +1 -0
- package/dist/tools/agentic/run.js +131 -0
- package/dist/tools/agentic/run.js.map +1 -0
- package/dist/tools/agentic/sandbox.d.ts +13 -2
- package/dist/tools/agentic/sandbox.d.ts.map +1 -1
- package/dist/tools/agentic/sandbox.js +127 -111
- package/dist/tools/agentic/sandbox.js.map +1 -1
- package/dist/tools/agentic/workspace-tools.d.ts +30 -27
- package/dist/tools/agentic/workspace-tools.d.ts.map +1 -1
- package/dist/tools/agentic/workspace-tools.js +324 -385
- package/dist/tools/agentic/workspace-tools.js.map +1 -1
- package/dist/tools/ask-agentic-fallback.d.ts +33 -0
- package/dist/tools/ask-agentic-fallback.d.ts.map +1 -0
- package/dist/tools/ask-agentic-fallback.js +113 -0
- package/dist/tools/ask-agentic-fallback.js.map +1 -0
- package/dist/tools/ask-agentic.tool.d.ts +11 -44
- package/dist/tools/ask-agentic.tool.d.ts.map +1 -1
- package/dist/tools/ask-agentic.tool.js +143 -1618
- package/dist/tools/ask-agentic.tool.js.map +1 -1
- package/dist/tools/ask.tool.d.ts.map +1 -1
- package/dist/tools/ask.tool.js +195 -555
- package/dist/tools/ask.tool.js.map +1 -1
- package/dist/tools/code.tool.d.ts +6 -0
- package/dist/tools/code.tool.d.ts.map +1 -1
- package/dist/tools/code.tool.js +155 -306
- package/dist/tools/code.tool.js.map +1 -1
- package/dist/tools/shared/abort-timeout.d.ts +48 -4
- package/dist/tools/shared/abort-timeout.d.ts.map +1 -1
- package/dist/tools/shared/abort-timeout.js +116 -95
- package/dist/tools/shared/abort-timeout.js.map +1 -1
- package/dist/tools/shared/call-ledger.d.ts +88 -0
- package/dist/tools/shared/call-ledger.d.ts.map +1 -0
- package/dist/tools/shared/call-ledger.js +133 -0
- package/dist/tools/shared/call-ledger.js.map +1 -0
- package/dist/tools/shared/context-decode.d.ts +77 -0
- package/dist/tools/shared/context-decode.d.ts.map +1 -0
- package/dist/tools/shared/context-decode.js +125 -0
- package/dist/tools/shared/context-decode.js.map +1 -0
- package/dist/tools/shared/continuation.d.ts +124 -0
- package/dist/tools/shared/continuation.d.ts.map +1 -0
- package/dist/tools/shared/continuation.js +302 -0
- package/dist/tools/shared/continuation.js.map +1 -0
- package/dist/tools/shared/dispatch-throttle.d.ts +43 -0
- package/dist/tools/shared/dispatch-throttle.d.ts.map +1 -0
- package/dist/tools/shared/dispatch-throttle.js +65 -0
- package/dist/tools/shared/dispatch-throttle.js.map +1 -0
- package/dist/tools/shared/question-fields.d.ts +10 -0
- package/dist/tools/shared/question-fields.d.ts.map +1 -0
- package/dist/tools/shared/question-fields.js +13 -0
- package/dist/tools/shared/question-fields.js.map +1 -0
- package/dist/tools/shared/settle-call.d.ts +68 -0
- package/dist/tools/shared/settle-call.d.ts.map +1 -0
- package/dist/tools/shared/settle-call.js +59 -0
- package/dist/tools/shared/settle-call.js.map +1 -0
- package/dist/tools/shared/stream-collector.d.ts +7 -0
- package/dist/tools/shared/stream-collector.d.ts.map +1 -1
- package/dist/tools/shared/stream-collector.js +5 -1
- package/dist/tools/shared/stream-collector.js.map +1 -1
- package/dist/tools/shared/thinking.d.ts +41 -11
- package/dist/tools/shared/thinking.d.ts.map +1 -1
- package/dist/tools/shared/thinking.js +62 -9
- package/dist/tools/shared/thinking.js.map +1 -1
- package/dist/tools/shared/throttle.d.ts +7 -0
- package/dist/tools/shared/throttle.d.ts.map +1 -1
- package/dist/tools/shared/throttle.js +18 -0
- package/dist/tools/shared/throttle.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/cost-estimator.d.ts +17 -0
- package/dist/utils/cost-estimator.d.ts.map +1 -1
- package/dist/utils/cost-estimator.js +15 -2
- package/dist/utils/cost-estimator.js.map +1 -1
- package/package.json +4 -3
package/dist/tools/code.tool.js
CHANGED
|
@@ -10,24 +10,26 @@
|
|
|
10
10
|
import { createHash } from 'node:crypto';
|
|
11
11
|
import { resolve } from 'node:path';
|
|
12
12
|
import { z } from 'zod';
|
|
13
|
-
import {
|
|
13
|
+
import { prepareContext } from '../cache/cache-manager.js';
|
|
14
14
|
import { CACHING_MODE_FALLBACK_DESCRIPTION, cachingModeFor, cachingRan, } from '../cache/caching-mode.js';
|
|
15
|
+
import { capThatRuns, preflightModeFor, resolveOutputCap, thinkingBudgetRefusal, } from '../gemini/long-decode.js';
|
|
15
16
|
import { ModelCategoryMismatchError } from '../gemini/model-taxonomy.js';
|
|
16
17
|
import { resolveModel } from '../gemini/models.js';
|
|
17
|
-
import { abortableSleep, withNetworkRetry } from '../gemini/retry.js';
|
|
18
18
|
import { countForPreflight } from '../gemini/token-counter.js';
|
|
19
19
|
import { buildScanMemo, scanWorkspace } from '../indexer/workspace-scanner.js';
|
|
20
20
|
import { WorkspaceValidationError, validateWorkspacePath, } from '../indexer/workspace-validation.js';
|
|
21
|
-
import {
|
|
21
|
+
import { estimatePreCallCostUsd, toMicrosUsd } from '../utils/cost-estimator.js';
|
|
22
22
|
import { logger, safeForLog } from '../utils/logger.js';
|
|
23
23
|
import { createProgressEmitter } from '../utils/progress.js';
|
|
24
24
|
import { errorResult, textResult } from './registry.js';
|
|
25
|
-
import {
|
|
25
|
+
import { isTimeoutAbort, timedOutCall, toolTimeoutController } from './shared/abort-timeout.js';
|
|
26
|
+
import { decodeOnContext } from './shared/context-decode.js';
|
|
27
|
+
import { CompletedRequests, LONG_DECODE_CAP_DESCRIPTION } from './shared/continuation.js';
|
|
28
|
+
import { DispatchThrottle } from './shared/dispatch-throttle.js';
|
|
26
29
|
import { SERVICE_TIER_DESCRIPTION, ServiceTierError, networkAttempts, resolveServiceTier, serviceTierConfig, statusOf, tierErrorMeta, } from './shared/service-tier.js';
|
|
27
|
-
import {
|
|
30
|
+
import { settleAnswer, settleFailure } from './shared/settle-call.js';
|
|
28
31
|
import { SYSTEM_INSTRUCTION_SAFETY_EAGER } from './shared/system-instruction-safety.js';
|
|
29
|
-
import { THINKING_LEVEL_FIELD,
|
|
30
|
-
import { isGemini429, parseRetryDelayMs } from './shared/throttle.js';
|
|
32
|
+
import { THINKING_LEVEL_FIELD, thinkingBudgetUnder, thinkingReserve } from './shared/thinking.js';
|
|
31
33
|
/**
|
|
32
34
|
* `code` system instruction. Prepends the shared `SYSTEM_INSTRUCTION_SAFETY_EAGER`
|
|
33
35
|
* data-vs-instruction firewall (added in v1.15.2 — pre-v1.15.2 `code` had no
|
|
@@ -92,7 +94,10 @@ export const codeInputSchema = z
|
|
|
92
94
|
.int()
|
|
93
95
|
.min(1)
|
|
94
96
|
.optional()
|
|
95
|
-
.describe(
|
|
97
|
+
.describe([
|
|
98
|
+
"Per-call opt-in cap on response length (tokens). OMIT for auto — Gemini uses its model-default cap (per Google docs, equal to the model's advertised `outputTokenLimit`: 65,536 for Gemini 3.x / 2.5 Pro-tier; see ai.google.dev/gemini-api/docs/models/gemini-2.5-pro). Pass a smaller value to bound a specific call (e.g. `maxOutputTokens: 16384` for a tight code-review summary). On any other model a value larger than the resolved model's limit is clamped to it. Operators who want EVERY call at full model capacity set `GEMINI_CODE_CONTEXT_FORCE_MAX_OUTPUT=true` at MCP-host level — that env override is still beaten by this per-call field.",
|
|
99
|
+
LONG_DECODE_CAP_DESCRIPTION,
|
|
100
|
+
].join(' ')),
|
|
96
101
|
includeGlobs: z
|
|
97
102
|
.array(z.string())
|
|
98
103
|
.optional()
|
|
@@ -107,7 +112,7 @@ export const codeInputSchema = z
|
|
|
107
112
|
.min(1_000)
|
|
108
113
|
.max(1_800_000)
|
|
109
114
|
.optional()
|
|
110
|
-
.describe('Per-call wall-clock TOTAL timeout in ms (1s–30min). The cost ceiling — aborts the in-flight `generateContent` request even if it is actively streaming.
|
|
115
|
+
.describe('Per-call wall-clock TOTAL timeout in ms (1s–30min). The cost ceiling — aborts the in-flight `generateContent` request even if it is actively streaming. A request aborted after its stream opened is booked 0; whether Google bills it is not measured (docs/cost-model.md). When omitted, falls back to env var `GEMINI_CODE_CONTEXT_CODE_TIMEOUT_MS`, then to disabled. v1.12.0 RECOMMENDATION: prefer `stallMs` for liveness — `timeoutMs` is the cost cap, `stallMs` is the heartbeat-aware liveness watchdog that does NOT fire while the model is actively thinking. Both can be set simultaneously. Returns `errorCode: "TIMEOUT"` with `timeoutKind: "total"` on abort.'),
|
|
111
116
|
stallMs: z
|
|
112
117
|
.number()
|
|
113
118
|
.int()
|
|
@@ -140,6 +145,14 @@ export const codeInputSchema = z
|
|
|
140
145
|
message: "`thinkingLevel: 'XHIGH'` is not usable with `code`: only Gemini 4 Hydrogen takes it, a fast-tier model, and `code` runs reasoning-tier models, which answer it with a 400. Use `HIGH` here, or `ask` with `model: 'gemini-4-hydrogen'`.",
|
|
141
146
|
path: ['thinkingLevel'],
|
|
142
147
|
});
|
|
148
|
+
/** Leads the text result of an answer with edits (`expectEdits`) cut at the output cap: its edit list is incomplete. */
|
|
149
|
+
export const TRUNCATED_EDITS_NOTE = '[truncated] The answer stopped at the output cap before the model finished it (see `finishReason`): the edit list below is cut — do not apply it as complete. Raise `maxOutputTokens` or lower the thinking, and ask again.';
|
|
150
|
+
/** Leads the text result of a prose answer (`expectEdits: false`) cut at the output cap. */
|
|
151
|
+
export const TRUNCATED_ANSWER_NOTE = '[truncated] The answer stopped at the output cap before the model finished it (see `finishReason`): the answer below is cut. Raise `maxOutputTokens` or lower the thinking, and ask again.';
|
|
152
|
+
/** The note that leads a truncated answer: about its edit list when edits were asked for, else about the prose. */
|
|
153
|
+
export function truncatedNote(expectEdits) {
|
|
154
|
+
return expectEdits ? TRUNCATED_EDITS_NOTE : TRUNCATED_ANSWER_NOTE;
|
|
155
|
+
}
|
|
143
156
|
const EDIT_REGEX = /\*\*FILE: (.+?)\*\*\s*\n```[^\n]*\n(?:OLD:\s*\n([\s\S]*?)\n)?NEW:\s*\n([\s\S]*?)\n```/g;
|
|
144
157
|
/** @internal — exported for testing only. Not part of the public API surface. */
|
|
145
158
|
export function parseEdits(text) {
|
|
@@ -234,24 +247,26 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
234
247
|
// undefined and we take the level branch below.
|
|
235
248
|
const thinkingBudget = usingThinkingLevel ? undefined : (input.thinkingBudget ?? 16_384);
|
|
236
249
|
let reservationId = null;
|
|
237
|
-
// `-1` signals "no reservation held". Mirror of ask.tool.ts.
|
|
238
|
-
let throttleReservationId = -1;
|
|
239
250
|
// Canonical resolved-model string for retry-hint seeding (T22a).
|
|
240
251
|
// See ask.tool.ts for rationale.
|
|
241
252
|
let resolvedModelKey = null;
|
|
242
|
-
//
|
|
243
|
-
|
|
253
|
+
// every request that completed is billed: the ledger settles from these, on an answer and on a failure alike
|
|
254
|
+
const completed = new CompletedRequests();
|
|
255
|
+
// the running decode's booking and whether a stream opened (a later failure is then billed work, not re-sent)
|
|
256
|
+
const tracking = { ledgerEntry: null, streamOpened: false };
|
|
244
257
|
let modelAudit = {};
|
|
245
258
|
const emitter = createProgressEmitter(ctx.server, ctx.progressToken);
|
|
246
259
|
// T19 + Phase 4 — composite controller (wall-clock + stall watchdog).
|
|
247
260
|
// See ask.tool.ts for rationale; same shape here.
|
|
248
|
-
const timeoutController =
|
|
249
|
-
...(input.timeoutMs !== undefined ? { totalMs: input.timeoutMs } : {}),
|
|
250
|
-
totalEnvVar: 'GEMINI_CODE_CONTEXT_CODE_TIMEOUT_MS',
|
|
251
|
-
...(input.stallMs !== undefined ? { stallMs: input.stallMs } : {}),
|
|
252
|
-
stallEnvVar: 'GEMINI_CODE_CONTEXT_CODE_STALL_MS',
|
|
253
|
-
});
|
|
261
|
+
const timeoutController = toolTimeoutController('code', input);
|
|
254
262
|
const abortSignal = timeoutController.signal;
|
|
263
|
+
// Each request of the decode holds its own TPM reservation; a failure drops the one held.
|
|
264
|
+
const dispatch = new DispatchThrottle({
|
|
265
|
+
throttle: ctx.throttle,
|
|
266
|
+
enabled: ctx.config.tpmThrottleLimit > 0,
|
|
267
|
+
signal: abortSignal,
|
|
268
|
+
emit: (message) => emitter.emit(message),
|
|
269
|
+
});
|
|
255
270
|
try {
|
|
256
271
|
// Workspace validation inside try → reported as a regular tool error
|
|
257
272
|
// (errorResult, "code failed: …") rather than an unhandled exception
|
|
@@ -320,62 +335,20 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
320
335
|
// heavy review work set `GEMINI_CODE_CONTEXT_FORCE_MAX_OUTPUT=true`
|
|
321
336
|
// to pin every call at the model's full 65,536-token capacity.
|
|
322
337
|
const CODE_MAX_OUTPUT_TOKENS_FALLBACK = 65_536;
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
:
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
//
|
|
333
|
-
//
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
// is carved out of the candidate-output allowance). The hard cap above
|
|
338
|
-
// may clamp maxOutputTokens as low as the model's `outputTokenLimit`
|
|
339
|
-
// (e.g. 8_192 on Flash-lite). Clamp the effective thinkingBudget so
|
|
340
|
-
// the generateContent call never 400s on this invariant; reserve at
|
|
341
|
-
// least 1_024 tokens for the actual completion.
|
|
342
|
-
//
|
|
343
|
-
// Only meaningful on the thinkingBudget path — when the caller uses
|
|
344
|
-
// `thinkingLevel` instead, `thinkingBudget` is `undefined` and this
|
|
345
|
-
// value is ignored by the thinkingConfig builder below.
|
|
346
|
-
//
|
|
347
|
-
// Early-reject when the caller's per-call `maxOutputTokens` can't fit
|
|
348
|
-
// an explicit positive `thinkingBudget` + 1024-token answer reserve.
|
|
349
|
-
// Silently clamping to 0 would make Gemini 3 Pro 400 on a
|
|
350
|
-
// "thinking disabled" error and obscure the real mismatch (PR #22
|
|
351
|
-
// round-3 review finding #C).
|
|
352
|
-
if (input.thinkingBudget !== undefined &&
|
|
353
|
-
input.thinkingBudget > 0 &&
|
|
354
|
-
input.maxOutputTokens !== undefined &&
|
|
355
|
-
effectiveOutputCap < input.thinkingBudget + 1024) {
|
|
356
|
-
return errorResult(`code: thinkingBudget (${input.thinkingBudget}) + 1024-token answer reserve exceeds maxOutputTokens (${effectiveOutputCap}). Raise \`maxOutputTokens\` to at least ${input.thinkingBudget + 1024}, or lower \`thinkingBudget\`.`);
|
|
357
|
-
}
|
|
358
|
-
const effectiveThinkingBudget = thinkingBudget !== undefined && thinkingBudget > 0
|
|
359
|
-
? Math.max(0, Math.min(thinkingBudget, effectiveOutputCap - 1024))
|
|
360
|
-
: 0;
|
|
361
|
-
// Cost-estimate thinking-token reserve — tier-aware when the caller
|
|
362
|
-
// uses `thinkingLevel`, exact-on-the-budget when they use
|
|
363
|
-
// `thinkingBudget`. Mirror of `ask.tool.ts` logic; see
|
|
364
|
-
// `THINKING_LEVEL_RESERVE` in `shared/thinking.ts` for the rationale
|
|
365
|
-
// behind the per-tier numbers and why HIGH falls through to the
|
|
366
|
-
// dynamic cap.
|
|
367
|
-
//
|
|
368
|
-
// Defensive clamp: tier reserves (MINIMAL=512, LOW=2048, MEDIUM=4096)
|
|
369
|
-
// are fixed constants, but `maxOutputTokens` can be clamped down to a
|
|
370
|
-
// resolved model's `outputTokenLimit`. A future small-cap model (say
|
|
371
|
-
// `outputTokenLimit: 4000`) would let MEDIUM's 4096 exceed the
|
|
372
|
-
// available headroom and over-estimate the budget reservation. Clamp
|
|
373
|
-
// every tier's reserve against the dynamic headroom so the upper
|
|
374
|
-
// bound stays coherent across model rollouts (PR #17 self-review F2).
|
|
375
|
-
const thinkingHeadroom = Math.max(0, effectiveOutputCap - 1024);
|
|
376
|
-
const thinkingTokensForEstimate = input.thinkingLevel !== undefined
|
|
377
|
-
? Math.min(THINKING_LEVEL_RESERVE[input.thinkingLevel] ?? thinkingHeadroom, thinkingHeadroom)
|
|
378
|
-
: effectiveThinkingBudget;
|
|
338
|
+
// The caller's `thinkingBudget` is checked against `outputCap` (the wire cap, else the model's per-request limit);
|
|
339
|
+
// the clamp and the reservation use the cap that runs (`runCap`, after the input window's cut).
|
|
340
|
+
const outputCap = resolveOutputCap({
|
|
341
|
+
requested: input.maxOutputTokens,
|
|
342
|
+
forceMax: ctx.config.forceMaxOutputTokens,
|
|
343
|
+
outputTokenLimit: resolved.outputTokenLimit,
|
|
344
|
+
longDecodeTokenLimit: resolved.longDecodeTokenLimit,
|
|
345
|
+
fallbackLimit: CODE_MAX_OUTPUT_TOKENS_FALLBACK,
|
|
346
|
+
});
|
|
347
|
+
// A positive `thinkingBudget` that leaves no answer reserve under the caller's own cap is refused
|
|
348
|
+
// (`thinkingBudgetRefusal`); below, the budget on the wire is clamped under the cap that runs.
|
|
349
|
+
const budgetRefusal = thinkingBudgetRefusal(input.thinkingBudget, input.maxOutputTokens, outputCap.effective);
|
|
350
|
+
if (budgetRefusal !== null)
|
|
351
|
+
return errorResult(`code: ${budgetRefusal}`);
|
|
379
352
|
// Shared input-token fingerprint for budget + TPM throttle (see
|
|
380
353
|
// ask.tool.ts for the rationale — same `Math.ceil(bytes/4)` tokenisation
|
|
381
354
|
// used by `estimatePreCallCostUsd`). The PREFLIGHT against
|
|
@@ -389,6 +362,8 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
389
362
|
// success-path metadata can surface preflight provenance.
|
|
390
363
|
let preflight;
|
|
391
364
|
const contextWindow = resolved.inputTokenLimit;
|
|
365
|
+
// A decode that may take more than one request takes the exact count: its prompt is billed per request.
|
|
366
|
+
const preflightMode = preflightModeFor(input.preflightMode, outputCap);
|
|
392
367
|
if (typeof contextWindow === 'number' && contextWindow > 0) {
|
|
393
368
|
preflight = await countForPreflight(ctx.client, {
|
|
394
369
|
files: scan.files,
|
|
@@ -396,7 +371,7 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
396
371
|
model: resolved.resolved,
|
|
397
372
|
// `filesHash` is post-glob-filter — no separate globsHash needed.
|
|
398
373
|
filesHash: scan.filesHash,
|
|
399
|
-
...(
|
|
374
|
+
...(preflightMode !== undefined ? { preflightMode } : {}),
|
|
400
375
|
inputTokenLimit: contextWindow,
|
|
401
376
|
// See ask.tool.ts for AbortSignal contract — abort propagates as
|
|
402
377
|
// `AbortError` to outer catch (mapped to `errorCode: 'TIMEOUT'`).
|
|
@@ -423,6 +398,20 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
423
398
|
else {
|
|
424
399
|
logger.warn(`code: resolved model ${safeForLog(resolved.resolved)} has no advertised inputTokenLimit — workspace size guard skipped. Request may fail downstream if the workspace exceeds the model's context window.`);
|
|
425
400
|
}
|
|
401
|
+
const { cap: runCap, cut: inputWindowCut } = capThatRuns({
|
|
402
|
+
tool: 'code',
|
|
403
|
+
cap: outputCap,
|
|
404
|
+
promptTokens: preflight?.effectiveTokens,
|
|
405
|
+
inputTokenLimit: contextWindow,
|
|
406
|
+
emit: (message) => emitter.emit(message),
|
|
407
|
+
});
|
|
408
|
+
// The budget on the wire: clamped below the cap that runs, after the input window's cut — a cap cut to one
|
|
409
|
+
// request goes on the wire as its cut value, and `thinkingBudget < maxOutputTokens` holds there too.
|
|
410
|
+
const effectiveThinkingBudget = thinkingBudget !== undefined && thinkingBudget > 0
|
|
411
|
+
? thinkingBudgetUnder(runCap, thinkingBudget)
|
|
412
|
+
: 0;
|
|
413
|
+
// The thinking the budget reservation counts, from the cap that runs.
|
|
414
|
+
const thinkingTokensForEstimate = thinkingReserve(input.thinkingLevel, effectiveThinkingBudget, runCap);
|
|
426
415
|
// Atomic budget reservation — see ask.tool.ts for the full rationale.
|
|
427
416
|
// Estimate includes the thinking budget (billed as output tokens on Pro)
|
|
428
417
|
// PLUS the candidate output cap.
|
|
@@ -432,7 +421,11 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
432
421
|
serviceTier,
|
|
433
422
|
workspaceBytes,
|
|
434
423
|
promptChars: input.task.length,
|
|
435
|
-
expectedOutputTokens:
|
|
424
|
+
expectedOutputTokens: runCap.effective,
|
|
425
|
+
// A cap above the per-request limit (long decode) adds the input every continuation request bills again.
|
|
426
|
+
perRequestOutputTokens: runCap.perRequest,
|
|
427
|
+
// The preflight's count, when it ran: the reservation takes the larger of it and the bytes/4 heuristic.
|
|
428
|
+
countedInputTokens: preflight?.effectiveTokens,
|
|
436
429
|
thinkingTokens: thinkingTokensForEstimate,
|
|
437
430
|
coverMs: timeoutController.timeoutMs,
|
|
438
431
|
});
|
|
@@ -461,7 +454,8 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
461
454
|
// codeExecution-forced-inline path stay distinguishable.
|
|
462
455
|
const caching = cachingModeFor(input.cachingMode ?? ctx.config.cachingMode, resolved, emitter);
|
|
463
456
|
const requestedCachingMode = caching.mode;
|
|
464
|
-
|
|
457
|
+
// The context of the call: built here, and again by the stale-cache retry on a fresh cache.
|
|
458
|
+
const prepare = () => prepareContext({
|
|
465
459
|
client: ctx.client,
|
|
466
460
|
manifest: ctx.manifest,
|
|
467
461
|
scan,
|
|
@@ -481,26 +475,7 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
481
475
|
// v1.13.0 — opt-in implicit caching path (skip caches.create).
|
|
482
476
|
cachingMode: requestedCachingMode,
|
|
483
477
|
});
|
|
484
|
-
|
|
485
|
-
// `generateContent`, so the reservation's `tsMs` accurately reflects
|
|
486
|
-
// when tokens hit Gemini's quota counter. See ask.tool.ts for the
|
|
487
|
-
// full rationale and the cold-cache-timing concern it fixes.
|
|
488
|
-
//
|
|
489
|
-
// Extracted into a helper so the stale-cache retry branch can
|
|
490
|
-
// cancel-and-re-reserve with an accurate tsMs rather than reusing a
|
|
491
|
-
// stale reservation.
|
|
492
|
-
const reserveForDispatch = async () => {
|
|
493
|
-
if (ctx.config.tpmThrottleLimit <= 0)
|
|
494
|
-
return;
|
|
495
|
-
const reservation = ctx.throttle.reserve(resolved.resolved, estimatedInputTokens);
|
|
496
|
-
throttleReservationId = reservation.releaseId;
|
|
497
|
-
if (reservation.delayMs > 0) {
|
|
498
|
-
emitter.emit(`throttle: waiting ${Math.ceil(reservation.delayMs / 1000)}s for TPM window…`);
|
|
499
|
-
// Abortable — wall-clock timeout (T19) must beat throttle delay.
|
|
500
|
-
await abortableSleep(reservation.delayMs, abortSignal);
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
await reserveForDispatch();
|
|
478
|
+
const ctxPrep = await prepare();
|
|
504
479
|
const userPrompt = expectEdits
|
|
505
480
|
? `${input.task}\n\nRespond with your rationale and OLD/NEW diff blocks per the system instruction.`
|
|
506
481
|
: input.task;
|
|
@@ -537,7 +512,7 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
537
512
|
}
|
|
538
513
|
const tierField = serviceTierConfig(serviceTier);
|
|
539
514
|
const buildConfig = (cacheId) => {
|
|
540
|
-
const maxOutputField =
|
|
515
|
+
const maxOutputField = runCap.wire !== undefined ? { maxOutputTokens: runCap.wire } : {};
|
|
541
516
|
if (canUseCache(cacheId) && cacheId) {
|
|
542
517
|
// With cache: system_instruction is baked in; thinkingConfig remains OK.
|
|
543
518
|
// `maxOutputTokens` only emitted when the caller set it explicitly
|
|
@@ -562,119 +537,32 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
562
537
|
const buildContents = (cacheId, inline) => canUseCache(cacheId)
|
|
563
538
|
? userPrompt
|
|
564
539
|
: [...inline, { role: 'user', parts: [{ text: userPrompt }] }];
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
//
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
emitter.emit(`thinking: ${trimmed}…`);
|
|
592
|
-
},
|
|
593
|
-
// v1.12.0 — stall watchdog reset on every chunk.
|
|
594
|
-
onChunkReceived: () => timeoutController.recordChunk(),
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
catch (err) {
|
|
598
|
-
if (activePrep.cacheId && isStaleCacheError(err)) {
|
|
599
|
-
logger.warn(`Gemini rejected cached content ${activePrep.cacheId}; invalidating and retrying once.`);
|
|
600
|
-
try {
|
|
601
|
-
// Reset cache pointer only — preserve `files` rows so the rebuild
|
|
602
|
-
// reuses uploaded files via content-hash dedup instead of double-
|
|
603
|
-
// uploading. The Gemini-side cache is already dead.
|
|
604
|
-
markCacheStale({ manifest: ctx.manifest, workspaceRoot });
|
|
605
|
-
const rebuilt = await prepareContext({
|
|
606
|
-
client: ctx.client,
|
|
607
|
-
manifest: ctx.manifest,
|
|
608
|
-
scan,
|
|
609
|
-
model: resolved,
|
|
610
|
-
systemPromptHash,
|
|
611
|
-
systemInstruction: SYSTEM_INSTRUCTION_CODE,
|
|
612
|
-
ttlSeconds: ctx.config.cacheTtlSeconds,
|
|
613
|
-
cacheMinTokens: ctx.config.cacheMinTokens,
|
|
614
|
-
emitter,
|
|
615
|
-
allowCaching: scan.files.length > 0 && !codeExecution,
|
|
616
|
-
// v1.12.1 — propagate signal into stale-cache rebuild too.
|
|
617
|
-
signal: abortSignal,
|
|
618
|
-
// v1.13.0 — keep cachingMode consistent across happy + retry paths.
|
|
619
|
-
cachingMode: requestedCachingMode,
|
|
620
|
-
});
|
|
621
|
-
// Cancel stale reservation (tsMs was stamped before first
|
|
622
|
-
// dispatch, now seconds old after rebuild) and re-reserve so
|
|
623
|
-
// the retry's tsMs matches actual dispatch time. Mirror of
|
|
624
|
-
// ask.tool.ts — see rationale there.
|
|
625
|
-
if (throttleReservationId !== -1) {
|
|
626
|
-
ctx.throttle.cancel(throttleReservationId);
|
|
627
|
-
throttleReservationId = -1;
|
|
628
|
-
}
|
|
629
|
-
await reserveForDispatch();
|
|
630
|
-
// The stale-cache retry itself can hit a transient network failure;
|
|
631
|
-
// wrap it too so the rebuild isn't wasted on a one-off blip.
|
|
632
|
-
// Stale-cache retry — discard partial, open fresh stream. Same
|
|
633
|
-
// retry-OPENING-only contract as the happy path above.
|
|
634
|
-
const retryStream = await withNetworkRetry(() => ctx.client.models.generateContentStream({
|
|
635
|
-
model: resolved.resolved,
|
|
636
|
-
contents: buildContents(rebuilt.cacheId, rebuilt.inlineContents),
|
|
637
|
-
config: { ...buildConfig(rebuilt.cacheId), abortSignal },
|
|
638
|
-
}), {
|
|
639
|
-
attempts: networkAttempts(serviceTier),
|
|
640
|
-
signal: abortSignal,
|
|
641
|
-
onRetry: (attempt, retryErr) => {
|
|
642
|
-
logger.warn(`code (stale-cache retry): retrying generateContent after transient network failure (attempt ${attempt}): ${retryErr instanceof Error ? retryErr.message : String(retryErr)}`);
|
|
643
|
-
},
|
|
644
|
-
});
|
|
645
|
-
streamOpened = true;
|
|
646
|
-
response = await collectStream(retryStream, {
|
|
647
|
-
signal: abortSignal,
|
|
648
|
-
onThoughtChunk: (text) => {
|
|
649
|
-
const trimmed = text.trim().slice(0, 80);
|
|
650
|
-
if (trimmed.length > 0)
|
|
651
|
-
emitter.emit(`thinking: ${trimmed}…`);
|
|
652
|
-
},
|
|
653
|
-
// v1.12.0 — stall watchdog reset on retry stream.
|
|
654
|
-
onChunkReceived: () => timeoutController.recordChunk(),
|
|
655
|
-
});
|
|
656
|
-
activePrep = rebuilt;
|
|
657
|
-
retriedOnStaleCache = true;
|
|
658
|
-
}
|
|
659
|
-
catch (retryErr) {
|
|
660
|
-
// Re-throw timeout directly so the outer catch's `isTimeoutAbort`
|
|
661
|
-
// sees it. Wrapping with `cause: err` (the original stale-cache
|
|
662
|
-
// error) would mask the timeout; outer catch would map to UNKNOWN
|
|
663
|
-
// instead of TIMEOUT.
|
|
664
|
-
if (isTimeoutAbort(retryErr))
|
|
665
|
-
throw retryErr;
|
|
666
|
-
if (statusOf(retryErr) !== undefined)
|
|
667
|
-
throw retryErr; // an HTTP error on the retry is the error itself
|
|
668
|
-
throw new Error(`code retry after stale cache failed: ${retryErr instanceof Error ? retryErr.message : String(retryErr)}`, { cause: err });
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
else {
|
|
672
|
-
throw err;
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
if (activePrep.cacheId) {
|
|
676
|
-
ctx.ttlWatcher.markHot(workspaceRoot, activePrep.cacheId, ctx.config.cacheTtlSeconds);
|
|
677
|
-
}
|
|
540
|
+
// The decode and its one stale-cache retry — the shared path of `ask` and `code` (see ask.tool.ts).
|
|
541
|
+
const decoded = await decodeOnContext({
|
|
542
|
+
client: ctx.client,
|
|
543
|
+
booking: { workspaceRoot, toolName: 'code', model: resolved.resolved, serviceTier },
|
|
544
|
+
// `cachingRan`: a codeExecution call is forced inline and never builds a cache.
|
|
545
|
+
cachingMode: (prep) => cachingRan(prep, caching).mode,
|
|
546
|
+
request: (prep) => ({
|
|
547
|
+
contents: buildContents(prep.cacheId, prep.inlineContents),
|
|
548
|
+
config: buildConfig(prep.cacheId),
|
|
549
|
+
}),
|
|
550
|
+
cache: {
|
|
551
|
+
ttlWatcher: ctx.ttlWatcher,
|
|
552
|
+
manifest: ctx.manifest,
|
|
553
|
+
ttlSeconds: ctx.config.cacheTtlSeconds,
|
|
554
|
+
prepare,
|
|
555
|
+
},
|
|
556
|
+
openAttempts: networkAttempts(serviceTier), // a flex request is never re-sent
|
|
557
|
+
limits: { maxRequests: runCap.maxRequests, cap: runCap.effective },
|
|
558
|
+
timeout: timeoutController,
|
|
559
|
+
emit: (message) => emitter.emit(message),
|
|
560
|
+
completed,
|
|
561
|
+
tracking,
|
|
562
|
+
throttle: dispatch,
|
|
563
|
+
inputTokens: { estimated: estimatedInputTokens, counted: preflight?.effectiveTokens },
|
|
564
|
+
}, ctxPrep);
|
|
565
|
+
const { response, prep: activePrep, retriedOnStaleCache } = decoded;
|
|
678
566
|
const text = response.text;
|
|
679
567
|
const edits = expectEdits ? parseEdits(text) : [];
|
|
680
568
|
const codeBlocks = parseCodeBlocks(text);
|
|
@@ -696,70 +584,40 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
696
584
|
}
|
|
697
585
|
}
|
|
698
586
|
const thinkingSummary = response.thoughtsSummary;
|
|
699
|
-
const usage = response.usageMetadata;
|
|
700
|
-
const cached = typeof usage?.cachedContentTokenCount === 'number' ? usage.cachedContentTokenCount : 0;
|
|
701
|
-
const inputTotal = typeof usage?.promptTokenCount === 'number' ? usage.promptTokenCount : 0;
|
|
702
|
-
const uncached = Math.max(0, inputTotal - cached);
|
|
703
|
-
const output = typeof usage?.candidatesTokenCount === 'number' ? usage.candidatesTokenCount : 0;
|
|
704
|
-
const thinking = typeof usage?.thoughtsTokenCount === 'number' ? usage.thoughtsTokenCount : 0;
|
|
705
|
-
// Finalise TPM throttle reservation with actual input tokens
|
|
706
|
-
// (cached + uncached — both count toward Gemini's per-minute quota).
|
|
707
|
-
if (throttleReservationId !== -1) {
|
|
708
|
-
ctx.throttle.release(throttleReservationId, inputTotal);
|
|
709
|
-
throttleReservationId = -1;
|
|
710
|
-
}
|
|
711
|
-
const cost = estimateCostUsd({
|
|
712
|
-
model: resolved.resolved,
|
|
713
|
-
serviceTier,
|
|
714
|
-
uncachedInputTokens: uncached,
|
|
715
|
-
cachedInputTokens: cached,
|
|
716
|
-
outputTokens: output,
|
|
717
|
-
thinkingTokens: thinking,
|
|
718
|
-
});
|
|
719
|
-
const costMicros = toMicrosUsd(cost);
|
|
720
587
|
// Telemetry and the response record the mode that ran, not the one asked for
|
|
721
588
|
// (`cachingRan`: a codeExecution call is forced inline and never builds a cache).
|
|
722
589
|
const ran = cachingRan(activePrep, caching);
|
|
723
590
|
const effectiveCachingMode = ran.mode;
|
|
724
591
|
const durationMs = Date.now() - started;
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
costUsdMicro: costMicros,
|
|
735
|
-
durationMs,
|
|
736
|
-
cachingMode: effectiveCachingMode,
|
|
737
|
-
cachedContentTokenCount: cached,
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
catch (finalizeErr) {
|
|
741
|
-
logger.error(`code: finalizeBudgetReservation failed for id=${reservationId}; reservation row keeps the estimate (${costMicros} micros). Error: ${String(finalizeErr)}`);
|
|
742
|
-
}
|
|
743
|
-
reservationId = null;
|
|
744
|
-
}
|
|
745
|
-
else {
|
|
746
|
-
ctx.manifest.insertUsageMetric({
|
|
592
|
+
// Settled here, once: the outer catch then neither releases the reservation nor books the call again. What the
|
|
593
|
+
// API billed is every request that completed, over a long decode and a stale-cache retry alike.
|
|
594
|
+
const settledReservation = reservationId;
|
|
595
|
+
reservationId = null;
|
|
596
|
+
tracking.ledgerEntry = null;
|
|
597
|
+
const settled = settleAnswer({
|
|
598
|
+
manifest: ctx.manifest,
|
|
599
|
+
reservationId: settledReservation,
|
|
600
|
+
entry: {
|
|
747
601
|
workspaceRoot,
|
|
748
602
|
toolName: 'code',
|
|
749
603
|
model: resolved.resolved,
|
|
750
|
-
|
|
751
|
-
uncachedTokens: uncached,
|
|
752
|
-
costUsdMicro: costMicros,
|
|
753
|
-
durationMs,
|
|
754
|
-
occurredAt: Date.now(),
|
|
604
|
+
serviceTier,
|
|
755
605
|
cachingMode: effectiveCachingMode,
|
|
756
|
-
|
|
606
|
+
},
|
|
607
|
+
completed,
|
|
608
|
+
response,
|
|
609
|
+
cap: runCap,
|
|
610
|
+
cut: inputWindowCut,
|
|
611
|
+
durationMs,
|
|
612
|
+
});
|
|
613
|
+
const finish = settled.finish;
|
|
614
|
+
// Settled and billed above; an empty answer at the cap is a failure the caller can act on, never a success.
|
|
615
|
+
if (settled.outputCapStop !== null) {
|
|
616
|
+
return errorResult(`code: ${settled.outputCapStop.message}`, {
|
|
617
|
+
...modelAudit,
|
|
618
|
+
...settled.outputCapStop.fields,
|
|
757
619
|
});
|
|
758
620
|
}
|
|
759
|
-
// Settled and billed above; an empty answer at the cap is a failure the caller can act on, never a success.
|
|
760
|
-
const starved = emptyAtOutputCap(response, thinking);
|
|
761
|
-
if (starved !== null)
|
|
762
|
-
return errorResult(`code: ${starved}`);
|
|
763
621
|
if (scan.truncated) {
|
|
764
622
|
logger.warn(`workspace ${workspaceRoot} contains more files than GEMINI_CODE_CONTEXT_MAX_FILES (${ctx.config.maxFilesPerWorkspace}); the tail was dropped before indexing.`);
|
|
765
623
|
}
|
|
@@ -782,11 +640,11 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
782
640
|
cacheHit: activePrep.reused,
|
|
783
641
|
cacheRebuilt: activePrep.rebuilt || retriedOnStaleCache,
|
|
784
642
|
retriedOnStaleCache,
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
643
|
+
...settled.usage,
|
|
644
|
+
...settled.cutFields,
|
|
645
|
+
// `truncated`: the answer stopped at the output cap (MAX_TOKENS, or a long decode at its limits) — it is cut
|
|
646
|
+
finishReason: finish.finishReason,
|
|
647
|
+
truncated: finish.truncated,
|
|
790
648
|
durationMs,
|
|
791
649
|
edits: edits.map((e) => ({
|
|
792
650
|
file: e.file,
|
|
@@ -821,57 +679,48 @@ async function executeCodeBody(input, ctx, workspaceRoot, started) {
|
|
|
821
679
|
...(executedCode.length > 0 ? { executedCode } : {}),
|
|
822
680
|
...(executionOutput.length > 0 ? { executionOutput } : {}),
|
|
823
681
|
};
|
|
824
|
-
return textResult(text, structured);
|
|
682
|
+
return textResult(finish.truncated ? `${truncatedNote(expectEdits)}\n\n${text}` : text, structured);
|
|
825
683
|
}
|
|
826
684
|
catch (err) {
|
|
827
685
|
logger.error(`code failed: ${safeForLog(err)}`);
|
|
828
|
-
//
|
|
829
|
-
//
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
// Drop TPM reservation on failure — mirror of ask.tool.ts rationale.
|
|
846
|
-
if (throttleReservationId !== -1) {
|
|
847
|
-
ctx.throttle.cancel(throttleReservationId);
|
|
848
|
-
throttleReservationId = -1;
|
|
849
|
-
}
|
|
686
|
+
// A 429's retry hint seeds the throttle; the ledger keeps what the completed requests billed and releases the rest
|
|
687
|
+
// (all of it when none completed).
|
|
688
|
+
const failedUsage = settleFailure({
|
|
689
|
+
manifest: ctx.manifest,
|
|
690
|
+
failed: {
|
|
691
|
+
toolName: 'code',
|
|
692
|
+
reservationId,
|
|
693
|
+
entry: tracking.ledgerEntry,
|
|
694
|
+
completed,
|
|
695
|
+
durationMs: Date.now() - started,
|
|
696
|
+
},
|
|
697
|
+
err,
|
|
698
|
+
model: resolvedModelKey,
|
|
699
|
+
throttle: ctx.throttle,
|
|
700
|
+
dispatch,
|
|
701
|
+
});
|
|
702
|
+
reservationId = null;
|
|
850
703
|
// v1.12.0 — `timeoutKind` distinguishes wall-clock cap from stall watchdog.
|
|
851
704
|
if (isTimeoutAbort(err)) {
|
|
852
|
-
const
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
const limitMs = kind === 'stall' ? stallMs : ms;
|
|
856
|
-
emitter.emit(`code: aborted after ${limitMs ?? '?'}ms ${kind ?? 'timeout'}`);
|
|
857
|
-
const message = kind === 'stall'
|
|
858
|
-
? `code timed out — no chunk received for ${stallMs ?? '?'}ms (stall watchdog). Increase \`stallMs\` per call or set \`GEMINI_CODE_CONTEXT_CODE_STALL_MS\` higher; default disabled. Stall watchdog is heartbeat-aware — it does NOT fire while the model is streaming chunks. A fired stall is usually a dead socket or a server-side hang. Note: AbortSignal is client-only — Gemini still finishes server-side and bills for completed work.`
|
|
859
|
-
: `code timed out after ${ms ?? '?'}ms (total wall-clock). Increase \`timeoutMs\` per call or set \`GEMINI_CODE_CONTEXT_CODE_TIMEOUT_MS\` higher; default disabled. Consider \`stallMs\` instead — a heartbeat-aware watchdog that does NOT fire while the model is actively thinking. Note: AbortSignal is client-only — Gemini still finishes server-side and bills for completed work.`;
|
|
860
|
-
return errorResult(message, {
|
|
705
|
+
const timedOut = timedOutCall('code', err, timeoutController);
|
|
706
|
+
emitter.emit(`code: aborted after ${timedOut.limitMs ?? '?'}ms ${timedOut.kind}`);
|
|
707
|
+
return errorResult(timedOut.message, {
|
|
861
708
|
errorCode: 'TIMEOUT',
|
|
862
|
-
timeoutKind: kind
|
|
863
|
-
timeoutMs:
|
|
864
|
-
stallMs,
|
|
709
|
+
timeoutKind: timedOut.kind,
|
|
710
|
+
timeoutMs: timeoutController.timeoutMs,
|
|
711
|
+
stallMs: timeoutController.stallMs,
|
|
865
712
|
retryable: true,
|
|
866
713
|
...modelAudit,
|
|
714
|
+
...failedUsage,
|
|
867
715
|
});
|
|
868
716
|
}
|
|
869
717
|
const httpStatus = statusOf(err);
|
|
870
718
|
return errorResult(`code failed: ${err instanceof Error ? err.message : String(err)}`, {
|
|
871
|
-
...tierErrorMeta(err, streamOpened),
|
|
719
|
+
...tierErrorMeta(err, tracking.streamOpened),
|
|
872
720
|
...(httpStatus !== undefined ? { httpStatus } : {}),
|
|
873
721
|
serviceTier: input.serviceTier ?? ctx.config.serviceTier, // before the model is resolved: the requested tier
|
|
874
722
|
...modelAudit,
|
|
723
|
+
...failedUsage,
|
|
875
724
|
});
|
|
876
725
|
}
|
|
877
726
|
finally {
|