@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/ask.tool.js
CHANGED
|
@@ -10,24 +10,27 @@ import { createHash } from 'node:crypto';
|
|
|
10
10
|
import { resolve } from 'node:path';
|
|
11
11
|
import { z } from 'zod';
|
|
12
12
|
import { AttachmentError, AttachmentTokensUncountedError, INLINE_REQUEST_LIMIT_BYTES, MAX_ATTACHMENTS, MAX_ATTACHMENTS_TOTAL_BYTES, MAX_ATTACHMENT_BYTES, countAttachmentTokens, inlineRequestBytes, inspectAttachments, readAttachments, } from '../attachments.js';
|
|
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 { resolveModel } from '../gemini/models.js';
|
|
16
|
-
import { abortableSleep, withNetworkRetry } from '../gemini/retry.js';
|
|
17
17
|
import { countForPreflight } from '../gemini/token-counter.js';
|
|
18
18
|
import { buildScanMemo, scanWorkspace } from '../indexer/workspace-scanner.js';
|
|
19
19
|
import { WorkspaceValidationError, validateWorkspacePath, } from '../indexer/workspace-validation.js';
|
|
20
|
-
import {
|
|
20
|
+
import { estimatePreCallCostUsd, toMicrosUsd } from '../utils/cost-estimator.js';
|
|
21
21
|
import { logger, safeForLog } from '../utils/logger.js';
|
|
22
22
|
import { createProgressEmitter } from '../utils/progress.js';
|
|
23
|
-
import {
|
|
23
|
+
import { fallBackToAgentic } from './ask-agentic-fallback.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';
|
|
29
|
+
import { PROMPT_FIELD, WORKSPACE_FIELD } from './shared/question-fields.js';
|
|
26
30
|
import { SERVICE_TIER_DESCRIPTION, ServiceTierError, networkAttempts, resolveServiceTier, serviceTierConfig, statusOf, tierErrorMeta, } from './shared/service-tier.js';
|
|
27
|
-
import {
|
|
31
|
+
import { settleAnswer, settleFailure } from './shared/settle-call.js';
|
|
28
32
|
import { SYSTEM_INSTRUCTION_SAFETY_EAGER } from './shared/system-instruction-safety.js';
|
|
29
|
-
import { THINKING_LEVEL_FIELD,
|
|
30
|
-
import { isGemini429, parseRetryDelayMs } from './shared/throttle.js';
|
|
33
|
+
import { THINKING_LEVEL_FIELD, thinkingBudgetUnder, thinkingReserve } from './shared/thinking.js';
|
|
31
34
|
/**
|
|
32
35
|
* `ask` system instruction. Prepends the shared `SYSTEM_INSTRUCTION_SAFETY_EAGER`
|
|
33
36
|
* data-vs-instruction firewall (added in v1.15.2 — pre-v1.15.2 `ask` had no
|
|
@@ -72,11 +75,8 @@ const THINKING_BUDGET_MODEL_DEFAULT = null;
|
|
|
72
75
|
// publishes per-tier token budgets, one edit propagates to both tools.
|
|
73
76
|
export const askInputSchema = z
|
|
74
77
|
.object({
|
|
75
|
-
prompt:
|
|
76
|
-
workspace:
|
|
77
|
-
.string()
|
|
78
|
-
.optional()
|
|
79
|
-
.describe('Absolute or cwd-relative path to the workspace. Defaults to process.cwd().'),
|
|
78
|
+
prompt: PROMPT_FIELD,
|
|
79
|
+
workspace: WORKSPACE_FIELD,
|
|
80
80
|
model: z
|
|
81
81
|
.string()
|
|
82
82
|
.optional()
|
|
@@ -106,14 +106,17 @@ export const askInputSchema = z
|
|
|
106
106
|
.int()
|
|
107
107
|
.min(1)
|
|
108
108
|
.optional()
|
|
109
|
-
.describe(
|
|
109
|
+
.describe([
|
|
110
|
+
"Per-call opt-in cap on response length (tokens). OMIT for the default 'auto' behaviour — 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 when you want a bounded response (e.g. strict summary length, tighter budget per call). On any other model a value larger than the resolved model's limit is clamped down to it. Budget reservation always uses the effective cap (explicit OR model limit) as worst-case, so `GEMINI_DAILY_BUDGET_USD` bounds what a completed call books (a request aborted mid-stream is booked 0: docs/cost-model.md).",
|
|
111
|
+
LONG_DECODE_CAP_DESCRIPTION,
|
|
112
|
+
].join(' ')),
|
|
110
113
|
timeoutMs: z
|
|
111
114
|
.number()
|
|
112
115
|
.int()
|
|
113
116
|
.min(1_000)
|
|
114
117
|
.max(1_800_000)
|
|
115
118
|
.optional()
|
|
116
|
-
.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.
|
|
119
|
+
.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_ASK_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.'),
|
|
117
120
|
stallMs: z
|
|
118
121
|
.number()
|
|
119
122
|
.int()
|
|
@@ -141,7 +144,7 @@ export const askInputSchema = z
|
|
|
141
144
|
onWorkspaceTooLarge: z
|
|
142
145
|
.enum(['error', 'fallback-to-agentic'])
|
|
143
146
|
.optional()
|
|
144
|
-
.describe("Behaviour when the v1.5.0 preflight detects the workspace exceeds the model's `inputTokenLimit × workspaceGuardRatio` (v1.11.0+). `'error'` (default — preserves v1.5.0 behaviour) returns a structured `errorCode: 'WORKSPACE_TOO_LARGE'` and lets the caller decide how to recover. `'fallback-to-agentic'` opt-in: the server transparently re-routes through `ask_agentic` (sandboxed file-access loop — no eager upload, scales to arbitrarily large repos), wraps the agentic result, and surfaces `fallbackApplied: 'ask_agentic'` in `structuredContent` so callers can audit the swap. **Semantic divergence:** `ask_agentic` runs as a multi-iteration loop, so `timeoutMs` (ask's wall-clock cap) translates to `iterationTimeoutMs` (per-iteration cap
|
|
147
|
+
.describe("Behaviour when the v1.5.0 preflight detects the workspace exceeds the model's `inputTokenLimit × workspaceGuardRatio` (v1.11.0+). `'error'` (default — preserves v1.5.0 behaviour) returns a structured `errorCode: 'WORKSPACE_TOO_LARGE'` and lets the caller decide how to recover. `'fallback-to-agentic'` opt-in: the server transparently re-routes through `ask_agentic` (sandboxed file-access loop — no eager upload, scales to arbitrarily large repos), wraps the agentic result, and surfaces `fallbackApplied: 'ask_agentic'` in `structuredContent` so callers can audit the swap. **Semantic divergence:** `ask_agentic` runs as a multi-iteration loop, so `timeoutMs` (ask's wall-clock cap, per call or `GEMINI_CODE_CONTEXT_ASK_TIMEOUT_MS`) translates to `iterationTimeoutMs` (per-iteration cap; total wall-clock can be N × timeoutMs) and `stallMs` (per call or `GEMINI_CODE_CONTEXT_ASK_STALL_MS`) to the loop's `stallMs` (a stream's silence only — a tool batch longer than `stallMs` answers); a phase neither bounds ends at `ask_agentic`'s default (600 s for a request's opening and a silent stream, 120 s without progress in an iteration's tools). The result reports `iterTimeoutMs`, `iterTimeoutSource: 'timeoutMs'` and `stallMs`, and lifts `errorCode`, `retryable`, `timeoutKind`, `timeoutPhase` and `timeoutSource` of a failed agentic call. Cost shape also changes (multiple smaller calls vs one big eager call). Recommended setting for orchestrators that prioritise getting an answer over a single round-trip; default stays `'error'` so existing callers see no behaviour change. **`code` does NOT support this field** — `code`'s OLD/NEW edit format is load-bearing for Claude's Edit pipeline, and `ask_agentic` returns prose only."),
|
|
145
148
|
})
|
|
146
149
|
.refine((data) => !(data.thinkingBudget !== undefined && data.thinkingLevel !== undefined), {
|
|
147
150
|
message: 'Cannot specify both `thinkingBudget` and `thinkingLevel` — they are mutually exclusive. Gemini rejects the combination with 400. Choose one: `thinkingLevel` (recommended for Gemini 3) or `thinkingBudget` (required on Gemini 2.5).',
|
|
@@ -237,16 +240,15 @@ function attachmentMetadata(prepared) {
|
|
|
237
240
|
}
|
|
238
241
|
async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
239
242
|
let reservationId = null;
|
|
240
|
-
// `-1` signals "no reservation held" (distinct from 0 which is a valid
|
|
241
|
-
// releaseId). Mirrors the pattern used by `reservationId`.
|
|
242
|
-
let throttleReservationId = -1;
|
|
243
243
|
// Canonical resolved-model string, captured once after `resolveModel`
|
|
244
244
|
// so the outer catch can feed retry hints into the same throttle bucket
|
|
245
245
|
// `reserve` used. `model` at the top of execute is the request alias
|
|
246
246
|
// ("latest-pro-thinking") — different key, different bucket.
|
|
247
247
|
let resolvedModelKey = null;
|
|
248
|
-
//
|
|
249
|
-
|
|
248
|
+
// every request that completed is billed: the ledger settles from these, on an answer and on a failure alike
|
|
249
|
+
const completed = new CompletedRequests();
|
|
250
|
+
// the running decode's booking and whether a stream opened (a later failure is then billed work, not re-sent)
|
|
251
|
+
const tracking = { ledgerEntry: null, streamOpened: false };
|
|
250
252
|
// the tier this call runs on once resolved (Vertex may force standard); before that, the requested one is reported
|
|
251
253
|
let serviceTier;
|
|
252
254
|
const reportedTier = () => serviceTier ?? input.serviceTier ?? ctx.config.serviceTier;
|
|
@@ -258,13 +260,15 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
258
260
|
// SDK's `config.abortSignal` — whichever timer fires first aborts both
|
|
259
261
|
// layers cleanly. `recordChunk()` is wired into `collectStream` to reset
|
|
260
262
|
// the stall watchdog on every chunk arrival.
|
|
261
|
-
const timeoutController =
|
|
262
|
-
...(input.timeoutMs !== undefined ? { totalMs: input.timeoutMs } : {}),
|
|
263
|
-
totalEnvVar: 'GEMINI_CODE_CONTEXT_ASK_TIMEOUT_MS',
|
|
264
|
-
...(input.stallMs !== undefined ? { stallMs: input.stallMs } : {}),
|
|
265
|
-
stallEnvVar: 'GEMINI_CODE_CONTEXT_ASK_STALL_MS',
|
|
266
|
-
});
|
|
263
|
+
const timeoutController = toolTimeoutController('ask', input);
|
|
267
264
|
const abortSignal = timeoutController.signal;
|
|
265
|
+
// Each request of the decode holds its own TPM reservation; a failure drops the one held.
|
|
266
|
+
const dispatch = new DispatchThrottle({
|
|
267
|
+
throttle: ctx.throttle,
|
|
268
|
+
enabled: ctx.config.tpmThrottleLimit > 0,
|
|
269
|
+
signal: abortSignal,
|
|
270
|
+
emit: (message) => emitter.emit(message),
|
|
271
|
+
});
|
|
268
272
|
try {
|
|
269
273
|
// Workspace path validation lives INSIDE the try so a
|
|
270
274
|
// `WorkspaceValidationError` is reported as a regular tool error
|
|
@@ -325,97 +329,38 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
325
329
|
manifestMemo: scanMemo,
|
|
326
330
|
forceRescan: input.forceRescan === true || ctx.config.forceRescan === true,
|
|
327
331
|
});
|
|
328
|
-
// Output
|
|
332
|
+
// Output cap, three layers (`resolveOutputCap`):
|
|
329
333
|
//
|
|
330
|
-
// 1. `input.maxOutputTokens` (per-call, strongest) —
|
|
331
|
-
//
|
|
332
|
-
//
|
|
333
|
-
//
|
|
334
|
-
//
|
|
335
|
-
//
|
|
336
|
-
//
|
|
337
|
-
//
|
|
338
|
-
//
|
|
339
|
-
// (documented as the model's advertised `outputTokenLimit`),
|
|
340
|
-
// letting the model size the response to the query's complexity
|
|
341
|
-
// rather than reserving full capacity on short Q&A.
|
|
334
|
+
// 1. `input.maxOutputTokens` (per-call, strongest) — clamped to the
|
|
335
|
+
// model's per-request limit, or on a model with long decode
|
|
336
|
+
// (Gemini 4) to its long-decode total: a cap above the per-request
|
|
337
|
+
// limit continues the decode in further requests.
|
|
338
|
+
// 2. `ctx.config.forceMaxOutputTokens` (env override) — every call
|
|
339
|
+
// sends the model's per-request limit.
|
|
340
|
+
// 3. Default — `maxOutputTokens` is omitted: one request, the model's
|
|
341
|
+
// own default (its advertised `outputTokenLimit`), letting the model
|
|
342
|
+
// size the response to the query rather than reserving full capacity.
|
|
342
343
|
//
|
|
343
|
-
// `
|
|
344
|
-
//
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
:
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
? modelOutputLimit
|
|
353
|
-
: undefined;
|
|
354
|
-
// Effective cap used for thinking-budget clamp and budget reservation.
|
|
355
|
-
// When neither override is set, the model's internal default equals
|
|
356
|
-
// `modelOutputLimit` per Google docs — so using the limit as
|
|
357
|
-
// worst-case keeps `GEMINI_DAILY_BUDGET_USD` a true upper bound.
|
|
358
|
-
const effectiveOutputCap = wireMaxOutputTokens ?? modelOutputLimit;
|
|
344
|
+
// The caller's `thinkingBudget` is checked against `outputCap`; the
|
|
345
|
+
// clamp and the reservation use the cap that runs (`runCap`).
|
|
346
|
+
const outputCap = resolveOutputCap({
|
|
347
|
+
requested: input.maxOutputTokens,
|
|
348
|
+
forceMax: ctx.config.forceMaxOutputTokens,
|
|
349
|
+
outputTokenLimit: resolved.outputTokenLimit,
|
|
350
|
+
longDecodeTokenLimit: resolved.longDecodeTokenLimit,
|
|
351
|
+
fallbackLimit: ASK_MAX_OUTPUT_TOKENS_FALLBACK,
|
|
352
|
+
});
|
|
359
353
|
// Single source of truth for "is the caller driving reasoning via the
|
|
360
354
|
// discrete-tier knob?". Three downstream call sites branch on this
|
|
361
355
|
// (cost estimate, emitter, thinkingConfig build) — extracting to a
|
|
362
356
|
// named const keeps them in lockstep and makes future additions hard
|
|
363
357
|
// to forget.
|
|
364
358
|
const usingThinkingLevel = input.thinkingLevel !== undefined;
|
|
365
|
-
//
|
|
366
|
-
//
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
// rejected) and mislead the caller about the actual cause (PR #22
|
|
371
|
-
// round-3 review finding #C). Minimum answer reserve: 1024 tokens.
|
|
372
|
-
if (input.thinkingBudget !== undefined &&
|
|
373
|
-
input.thinkingBudget > 0 &&
|
|
374
|
-
input.maxOutputTokens !== undefined &&
|
|
375
|
-
effectiveOutputCap < input.thinkingBudget + 1024) {
|
|
376
|
-
return errorResult(`ask: thinkingBudget (${input.thinkingBudget}) + 1024-token answer reserve exceeds maxOutputTokens (${effectiveOutputCap}). Raise \`maxOutputTokens\` to at least ${input.thinkingBudget + 1024}, or lower \`thinkingBudget\`.`);
|
|
377
|
-
}
|
|
378
|
-
// Normalise thinkingBudget:
|
|
379
|
-
// undefined → null (model-default path — `thinkingBudget` OMITTED on wire)
|
|
380
|
-
// -1 → -1 (explicit legacy dynamic; kept for Gemini 2.5 and
|
|
381
|
-
// power-users who want to force the legacy path)
|
|
382
|
-
// 0 → 0 (thinking disabled; Gemini 3 Pro will 400 here)
|
|
383
|
-
// N > 0 → clamp to [0, maxOutputTokens - 1024] — the thinking
|
|
384
|
-
// pool is carved out of the candidate-output allowance,
|
|
385
|
-
// so reserving ≥1024 tokens for the answer prevents 400s
|
|
386
|
-
// when a caller passes N ≥ maxOutputTokens.
|
|
387
|
-
//
|
|
388
|
-
// Schema `.refine()` guarantees `thinkingBudget` and `thinkingLevel`
|
|
389
|
-
// are never both set, so we handle them in two mutually-exclusive
|
|
390
|
-
// branches below.
|
|
391
|
-
const rawThinkingBudget = input.thinkingBudget;
|
|
392
|
-
const effectiveThinkingBudget = rawThinkingBudget === undefined
|
|
393
|
-
? THINKING_BUDGET_MODEL_DEFAULT
|
|
394
|
-
: rawThinkingBudget === -1
|
|
395
|
-
? -1
|
|
396
|
-
: rawThinkingBudget > 0
|
|
397
|
-
? Math.max(0, Math.min(rawThinkingBudget, effectiveOutputCap - 1024))
|
|
398
|
-
: 0;
|
|
399
|
-
// Cost-estimate thinking-token reserve — tier-aware when the caller
|
|
400
|
-
// uses `thinkingLevel`, worst-case when they use `thinkingBudget: -1`
|
|
401
|
-
// or omit both fields (Gemini 3 Pro always spends thinking tokens per
|
|
402
|
-
// Google's docs, so we reserve dynamic-thinking headroom there too).
|
|
403
|
-
// When the caller passes an explicit positive `thinkingBudget`, we
|
|
404
|
-
// reserve only that much. Keeps `GEMINI_DAILY_BUDGET_USD` a TRUE
|
|
405
|
-
// upper bound on completed spend without false-rejecting callers
|
|
406
|
-
// who legitimately use a low tier (see `THINKING_LEVEL_RESERVE`
|
|
407
|
-
// rationale above).
|
|
408
|
-
//
|
|
409
|
-
// We dereference `input.thinkingLevel` directly (rather than via
|
|
410
|
-
// `usingThinkingLevel`) so TypeScript narrows the type inside the
|
|
411
|
-
// ternary — `THINKING_LEVEL_RESERVE[undefined]` would be a compile
|
|
412
|
-
// error. The `?? worst-case` fallback handles the HIGH tier (which
|
|
413
|
-
// maps to `null` in the reserve table to mean "use the dynamic cap").
|
|
414
|
-
const thinkingTokensForEstimate = input.thinkingLevel !== undefined
|
|
415
|
-
? (THINKING_LEVEL_RESERVE[input.thinkingLevel] ?? Math.max(0, effectiveOutputCap - 1024))
|
|
416
|
-
: effectiveThinkingBudget === null || effectiveThinkingBudget === -1
|
|
417
|
-
? Math.max(0, effectiveOutputCap - 1024)
|
|
418
|
-
: effectiveThinkingBudget;
|
|
359
|
+
// A positive `thinkingBudget` that leaves no answer reserve under the caller's own cap is refused
|
|
360
|
+
// (`thinkingBudgetRefusal`); below, the budget on the wire is clamped under the cap that runs.
|
|
361
|
+
const budgetRefusal = thinkingBudgetRefusal(input.thinkingBudget, input.maxOutputTokens, outputCap.effective);
|
|
362
|
+
if (budgetRefusal !== null)
|
|
363
|
+
return errorResult(`ask: ${budgetRefusal}`);
|
|
419
364
|
// Estimated input-token count drives both the daily-budget reservation
|
|
420
365
|
// (dollars) and the TPM throttle reservation (rate). Hoisted here so
|
|
421
366
|
// both consumers share one fingerprint — no drift between what the $
|
|
@@ -454,6 +399,8 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
454
399
|
// model has no advertised `inputTokenLimit`.
|
|
455
400
|
let preflight;
|
|
456
401
|
const contextWindow = resolved.inputTokenLimit;
|
|
402
|
+
// A decode that may take more than one request takes the exact count: its prompt is billed per request.
|
|
403
|
+
const preflightMode = preflightModeFor(input.preflightMode, outputCap);
|
|
457
404
|
if (typeof contextWindow === 'number' && contextWindow > 0) {
|
|
458
405
|
preflight = await countForPreflight(ctx.client, {
|
|
459
406
|
files: scan.files,
|
|
@@ -463,7 +410,7 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
463
410
|
// already encodes user-supplied includeGlobs/excludeGlobs — no
|
|
464
411
|
// separate globsHash axis needed in the cache key.
|
|
465
412
|
filesHash: scan.filesHash,
|
|
466
|
-
...(
|
|
413
|
+
...(preflightMode !== undefined ? { preflightMode } : {}),
|
|
467
414
|
inputTokenLimit: contextWindow,
|
|
468
415
|
extraTokens: attachmentTokens, // weighs on the heuristic-vs-exact decision, added to the count below
|
|
469
416
|
// Thread the user's `timeoutMs` AbortSignal so a hung countTokens
|
|
@@ -484,9 +431,10 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
484
431
|
// 'fallback-to-agentic'`. Default stays `'error'` so pre-v1.11.0
|
|
485
432
|
// callers see identical behaviour. The fallback re-uses this
|
|
486
433
|
// tool's resolved inputs (model / globs / thinking config /
|
|
487
|
-
// workspace) and
|
|
488
|
-
// (per-iteration cap; total wall-clock can be N × that)
|
|
489
|
-
//
|
|
434
|
+
// workspace) and forwards its resolved `timeoutMs` as `iterationTimeoutMs`
|
|
435
|
+
// (per-iteration cap; total wall-clock can be N × that) and `stallMs` as
|
|
436
|
+
// `stallMs` (`ask-agentic-fallback.ts`). Cost + timing shape genuinely
|
|
437
|
+
// changes — opt-in is the right default.
|
|
490
438
|
if (input.onWorkspaceTooLarge === 'fallback-to-agentic' &&
|
|
491
439
|
attachments.attached.length > 0) {
|
|
492
440
|
return errorResult('ask: the workspace is too large for the eager path and `attachments` cannot follow the ask_agentic fallback (it sends no inline parts); drop the attachments or narrow the workspace', {
|
|
@@ -497,152 +445,15 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
497
445
|
});
|
|
498
446
|
}
|
|
499
447
|
if (input.onWorkspaceTooLarge === 'fallback-to-agentic') {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
// recommended liveness watchdog) saw it silently dropped on the
|
|
510
|
-
// fallback path. The agentic loop uses non-streaming
|
|
511
|
-
// `generateContent`, so there's no chunk stream to reset on; we
|
|
512
|
-
// collapse both knobs onto the per-iteration cap and use the
|
|
513
|
-
// TIGHTER of the two as the bound. The CHANGELOG documents the
|
|
514
|
-
// semantic divergence on `timeoutMs` (total → per-iteration).
|
|
515
|
-
//
|
|
516
|
-
// v1.12.2 — `iterTimeoutSource` is surfaced in metadata AND emitted
|
|
517
|
-
// in the warn-log so users can see WHICH knob bound their call
|
|
518
|
-
// (one of: 'timeoutMs', 'stallMs', or 'min(timeoutMs,stallMs)').
|
|
519
|
-
// Pre-v1.12.2 the log only fired when stallMs was set alone; the
|
|
520
|
-
// both-set case had no signal explaining the pick.
|
|
521
|
-
const iterTimeoutPick = (() => {
|
|
522
|
-
const a = input.timeoutMs;
|
|
523
|
-
const b = input.stallMs;
|
|
524
|
-
if (a !== undefined && b !== undefined) {
|
|
525
|
-
const cap = Math.min(a, b);
|
|
526
|
-
return { cap, source: 'min(timeoutMs,stallMs)' };
|
|
527
|
-
}
|
|
528
|
-
if (a !== undefined)
|
|
529
|
-
return { cap: a, source: 'timeoutMs' };
|
|
530
|
-
if (b !== undefined)
|
|
531
|
-
return { cap: b, source: 'stallMs' };
|
|
532
|
-
return { cap: undefined, source: 'none' };
|
|
533
|
-
})();
|
|
534
|
-
const tightestPerIterCap = iterTimeoutPick.cap;
|
|
535
|
-
if (iterTimeoutPick.source !== 'none') {
|
|
536
|
-
logger.warn(`ask: fallback to ask_agentic — \`iterationTimeoutMs\` set to ${tightestPerIterCap}ms (source: ${iterTimeoutPick.source}). The agentic loop uses non-streaming \`generateContent\` so there's no chunk stream to reset on; \`stallMs\` (when set) is treated as a per-iteration wall-clock cap, the same way \`timeoutMs\` is. Total wall-clock for the agentic call can reach \`maxIterations × iterationTimeoutMs\`.`);
|
|
537
|
-
}
|
|
538
|
-
const agenticInput = {
|
|
539
|
-
prompt: input.prompt,
|
|
540
|
-
...(input.workspace !== undefined ? { workspace: input.workspace } : {}),
|
|
541
|
-
...(input.model !== undefined ? { model: input.model } : {}),
|
|
542
|
-
...(input.includeGlobs !== undefined ? { includeGlobs: input.includeGlobs } : {}),
|
|
543
|
-
...(input.excludeGlobs !== undefined ? { excludeGlobs: input.excludeGlobs } : {}),
|
|
544
|
-
...(input.thinkingBudget !== undefined ? { thinkingBudget: input.thinkingBudget } : {}),
|
|
545
|
-
...(input.thinkingLevel !== undefined ? { thinkingLevel: input.thinkingLevel } : {}),
|
|
546
|
-
...(input.maxOutputTokens !== undefined
|
|
547
|
-
? { maxOutputTokens: input.maxOutputTokens }
|
|
548
|
-
: {}),
|
|
549
|
-
// `timeoutMs` (ask wall-clock cap) → `iterationTimeoutMs`
|
|
550
|
-
// (ask_agentic per-iteration cap). Total wall-clock for the
|
|
551
|
-
// agentic call can be `maxIterations × iterationTimeoutMs`.
|
|
552
|
-
// v1.12.1 fix: also collapses `stallMs` onto this cap (using
|
|
553
|
-
// the TIGHTER of the two when both are set), so the fallback
|
|
554
|
-
// path doesn't silently drop the user's stall watchdog.
|
|
555
|
-
...(tightestPerIterCap !== undefined ? { iterationTimeoutMs: tightestPerIterCap } : {}),
|
|
556
|
-
};
|
|
557
|
-
const agenticResult = await askAgenticTool.execute(agenticInput, ctx);
|
|
558
|
-
// Wrap the agentic result so callers can audit the swap.
|
|
559
|
-
// The text content (`content[0].text`) is the agentic prose
|
|
560
|
-
// verbatim — same shape any direct `ask_agentic` caller would
|
|
561
|
-
// see. The structuredContent is enriched with fallback-trail
|
|
562
|
-
// metadata so orchestrators can distinguish a fallback-served
|
|
563
|
-
// response from a direct one.
|
|
564
|
-
//
|
|
565
|
-
// Two load-bearing contracts the wrapper must preserve:
|
|
566
|
-
// 1. `structuredContent.responseText` — the T23 wire-format
|
|
567
|
-
// invariant (`textResult` / `errorResult` always write it
|
|
568
|
-
// LAST; sub-agent orchestrators extract from this key
|
|
569
|
-
// ONLY). We pull it from the agentic result's
|
|
570
|
-
// `structuredContent.responseText` (or the prose text from
|
|
571
|
-
// `content[0].text` as a fallback) and write it LAST so
|
|
572
|
-
// it can't be clobbered.
|
|
573
|
-
// 2. Top-level `errorCode` / `retryable` — orchestrator retry
|
|
574
|
-
// policies switch on these. When the agentic call itself
|
|
575
|
-
// fails (e.g. iteration budget exhausted), the failure
|
|
576
|
-
// code lives in `agenticResult.structuredContent.errorCode`;
|
|
577
|
-
// we lift it to the top level of `wrappedStructured` so
|
|
578
|
-
// consumer policies see it without descending into nested
|
|
579
|
-
// `agenticResult.errorCode` (which they wouldn't know
|
|
580
|
-
// about pre-v1.11.0).
|
|
581
|
-
const agenticStructured = agenticResult.structuredContent ?? {};
|
|
582
|
-
const promptResponseText = typeof agenticStructured.responseText === 'string'
|
|
583
|
-
? agenticStructured.responseText
|
|
584
|
-
: agenticResult.content?.[0]?.type === 'text'
|
|
585
|
-
? agenticResult.content[0].text
|
|
586
|
-
: '';
|
|
587
|
-
const wrappedStructured = {
|
|
588
|
-
fallbackApplied: 'ask_agentic',
|
|
589
|
-
serviceTier: 'standard', // ask_agentic has no tier: the fallback runs standard whatever was asked
|
|
590
|
-
...(serviceTier === 'flex' ? { serviceTierDowngraded: 'flex' } : {}),
|
|
591
|
-
fallbackReason: 'WORKSPACE_TOO_LARGE',
|
|
592
|
-
preflightEstimate: {
|
|
593
|
-
tokens: preflight.effectiveTokens,
|
|
594
|
-
threshold,
|
|
595
|
-
source: preflight.method,
|
|
596
|
-
cacheHit: preflight.cacheHit,
|
|
597
|
-
rawTokens: preflight.rawTokens,
|
|
598
|
-
},
|
|
599
|
-
// v1.12.2 — surface the per-iteration cap selection so callers
|
|
600
|
-
// can audit which knob bound the fallback call. Useful when the
|
|
601
|
-
// user passed both `timeoutMs` and `stallMs` (we use the tighter
|
|
602
|
-
// of the two — `iterTimeoutSource: 'min(timeoutMs,stallMs)'`).
|
|
603
|
-
// Omitted when neither knob was set (`source: 'none'`).
|
|
604
|
-
...(iterTimeoutPick.source !== 'none'
|
|
605
|
-
? {
|
|
606
|
-
iterTimeoutMs: tightestPerIterCap,
|
|
607
|
-
iterTimeoutSource: iterTimeoutPick.source,
|
|
608
|
-
}
|
|
609
|
-
: {}),
|
|
610
|
-
// Lift top-level error metadata from the agentic call's
|
|
611
|
-
// structuredContent so orchestrator policies (e.g. retry on
|
|
612
|
-
// BUDGET_EXHAUSTED, retry on stall but not total) keep working
|
|
613
|
-
// without descending into `.agenticResult.errorCode`. v1.12.1
|
|
614
|
-
// adds `timeoutKind` to the lift set — Phase 4 introduced
|
|
615
|
-
// `timeoutKind` as a top-level field on direct-path TIMEOUT
|
|
616
|
-
// errors; the fallback wrapper now mirrors that on the
|
|
617
|
-
// fallback-served TIMEOUT path so the contract is uniform.
|
|
618
|
-
...(agenticResult.isError && typeof agenticStructured.errorCode === 'string'
|
|
619
|
-
? { errorCode: agenticStructured.errorCode }
|
|
620
|
-
: {}),
|
|
621
|
-
...(agenticResult.isError && typeof agenticStructured.retryable === 'boolean'
|
|
622
|
-
? { retryable: agenticStructured.retryable }
|
|
623
|
-
: {}),
|
|
624
|
-
...(agenticResult.isError &&
|
|
625
|
-
(agenticStructured.timeoutKind === 'total' || agenticStructured.timeoutKind === 'stall')
|
|
626
|
-
? { timeoutKind: agenticStructured.timeoutKind }
|
|
627
|
-
: {}),
|
|
628
|
-
...(Object.keys(agenticStructured).length > 0
|
|
629
|
-
? { agenticResult: agenticStructured }
|
|
630
|
-
: {}),
|
|
631
|
-
// T23 wire-format invariant — `responseText` is written LAST
|
|
632
|
-
// so any caller-supplied / nested key with the same name is
|
|
633
|
-
// intentionally clobbered by the canonical value. Sub-agent
|
|
634
|
-
// orchestrators read the prose from this exact key.
|
|
635
|
-
responseText: promptResponseText,
|
|
636
|
-
};
|
|
637
|
-
// `isError` lives at the ROOT of the MCP CallToolResult per spec
|
|
638
|
-
// (sibling to `structuredContent`, not nested inside it). The
|
|
639
|
-
// spread on the return statement carries the agentic root-level
|
|
640
|
-
// `isError` automatically — no need to mirror it inside
|
|
641
|
-
// `structuredContent`.
|
|
642
|
-
return {
|
|
643
|
-
...agenticResult,
|
|
644
|
-
structuredContent: wrappedStructured,
|
|
645
|
-
};
|
|
448
|
+
return fallBackToAgentic({
|
|
449
|
+
input,
|
|
450
|
+
ctx,
|
|
451
|
+
emitter,
|
|
452
|
+
serviceTier,
|
|
453
|
+
preflight,
|
|
454
|
+
threshold,
|
|
455
|
+
timeout: timeoutController,
|
|
456
|
+
});
|
|
646
457
|
}
|
|
647
458
|
const attachmentsNote = attachments.attached.length > 0
|
|
648
459
|
? ' — it carries no attachments: drop them, or narrow the workspace and ask again'
|
|
@@ -667,6 +478,38 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
667
478
|
else {
|
|
668
479
|
logger.warn(`ask: 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.`);
|
|
669
480
|
}
|
|
481
|
+
const { cap: runCap, cut: inputWindowCut } = capThatRuns({
|
|
482
|
+
tool: 'ask',
|
|
483
|
+
cap: outputCap,
|
|
484
|
+
promptTokens: preflight?.effectiveTokens,
|
|
485
|
+
inputTokenLimit: contextWindow,
|
|
486
|
+
emit: (message) => emitter.emit(message),
|
|
487
|
+
});
|
|
488
|
+
// Normalise thinkingBudget:
|
|
489
|
+
// undefined → null (model-default path — `thinkingBudget` OMITTED on wire)
|
|
490
|
+
// -1 → -1 (explicit legacy dynamic; kept for Gemini 2.5 and
|
|
491
|
+
// power-users who want to force the legacy path)
|
|
492
|
+
// 0 → 0 (thinking disabled; Gemini 3 Pro will 400 here)
|
|
493
|
+
// N > 0 → clamp to [0, cap that runs - 1024] — the thinking
|
|
494
|
+
// pool is carved out of the candidate-output allowance,
|
|
495
|
+
// so reserving ≥1024 tokens for the answer prevents 400s
|
|
496
|
+
// when a caller passes N ≥ maxOutputTokens, and under a
|
|
497
|
+
// cap the input window cut to one request (its value on
|
|
498
|
+
// the wire) the budget stays below it.
|
|
499
|
+
//
|
|
500
|
+
// Schema `.refine()` guarantees `thinkingBudget` and `thinkingLevel`
|
|
501
|
+
// are never both set, so we handle them in two mutually-exclusive
|
|
502
|
+
// branches below.
|
|
503
|
+
const rawThinkingBudget = input.thinkingBudget;
|
|
504
|
+
const effectiveThinkingBudget = rawThinkingBudget === undefined
|
|
505
|
+
? THINKING_BUDGET_MODEL_DEFAULT
|
|
506
|
+
: rawThinkingBudget === -1
|
|
507
|
+
? -1
|
|
508
|
+
: rawThinkingBudget > 0
|
|
509
|
+
? thinkingBudgetUnder(runCap, rawThinkingBudget)
|
|
510
|
+
: 0;
|
|
511
|
+
// The thinking the budget reservation counts, from the cap that runs.
|
|
512
|
+
const thinkingTokensForEstimate = thinkingReserve(input.thinkingLevel, effectiveThinkingBudget, runCap);
|
|
670
513
|
// Atomic budget reservation. Happens AFTER scan (so the estimate is
|
|
671
514
|
// accurate) but BEFORE any billable upload or generateContent call.
|
|
672
515
|
// Concurrent tool calls cannot all pass a pre-check and then
|
|
@@ -683,7 +526,11 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
683
526
|
// OR model's full limit) as `expectedOutputTokens` — a TRUE
|
|
684
527
|
// upper bound, since Gemini's default stops at the model's
|
|
685
528
|
// advertised `outputTokenLimit`.
|
|
686
|
-
expectedOutputTokens:
|
|
529
|
+
expectedOutputTokens: runCap.effective,
|
|
530
|
+
// A cap above the per-request limit (long decode) adds the input every continuation request bills again.
|
|
531
|
+
perRequestOutputTokens: runCap.perRequest,
|
|
532
|
+
// The preflight's count, when it ran: the reservation takes the larger of it and the bytes/4 heuristic.
|
|
533
|
+
countedInputTokens: preflight?.effectiveTokens,
|
|
687
534
|
thinkingTokens: thinkingTokensForEstimate,
|
|
688
535
|
coverMs: timeoutController.timeoutMs,
|
|
689
536
|
});
|
|
@@ -715,7 +562,8 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
715
562
|
// AFTER `prepareContext` returns from `activePrep.inlineOnly` — see FN2 fix.
|
|
716
563
|
const caching = cachingModeFor(input.cachingMode ?? ctx.config.cachingMode, resolved, emitter);
|
|
717
564
|
const requestedCachingMode = caching.mode;
|
|
718
|
-
|
|
565
|
+
// The context of the call: built here, and again by the stale-cache retry on a fresh cache.
|
|
566
|
+
const prepare = () => prepareContext({
|
|
719
567
|
client: ctx.client,
|
|
720
568
|
manifest: ctx.manifest,
|
|
721
569
|
scan,
|
|
@@ -731,33 +579,7 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
731
579
|
// v1.13.0 — opt-in implicit caching path (skip caches.create).
|
|
732
580
|
cachingMode: requestedCachingMode,
|
|
733
581
|
});
|
|
734
|
-
|
|
735
|
-
// immediately before `generateContent`) so the reservation's `tsMs`
|
|
736
|
-
// accurately reflects when tokens hit Gemini's quota counter. An
|
|
737
|
-
// earlier reserve (before `prepareContext`) would stamp `tsMs`
|
|
738
|
-
// minutes before actual dispatch on cold-cache calls; our window
|
|
739
|
-
// would then expire before Gemini's, leaving a gap where we admit
|
|
740
|
-
// concurrent calls that bust Gemini's per-minute quota. Trade-off:
|
|
741
|
-
// two concurrent cold-cache callers will both complete
|
|
742
|
-
// `prepareContext` before one backs off at `reserve` — mostly
|
|
743
|
-
// idempotent via file-hash dedup, minor upload duplication.
|
|
744
|
-
//
|
|
745
|
-
// Extracted into a helper so the stale-cache retry branch can
|
|
746
|
-
// cancel-and-re-reserve with an accurate tsMs rather than reusing a
|
|
747
|
-
// stale reservation stamped before the first (failed) dispatch.
|
|
748
|
-
const reserveForDispatch = async () => {
|
|
749
|
-
if (ctx.config.tpmThrottleLimit <= 0)
|
|
750
|
-
return;
|
|
751
|
-
const reservation = ctx.throttle.reserve(resolved.resolved, estimatedInputTokens);
|
|
752
|
-
throttleReservationId = reservation.releaseId;
|
|
753
|
-
if (reservation.delayMs > 0) {
|
|
754
|
-
emitter.emit(`throttle: waiting ${Math.ceil(reservation.delayMs / 1000)}s for TPM window…`);
|
|
755
|
-
// Abortable sleep — without this a 60s TPM throttle wait would block
|
|
756
|
-
// a 10s wall-clock timeout, defeating T19's whole-dispatch contract.
|
|
757
|
-
await abortableSleep(reservation.delayMs, abortSignal);
|
|
758
|
-
}
|
|
759
|
-
};
|
|
760
|
-
await reserveForDispatch();
|
|
582
|
+
const ctxPrep = await prepare();
|
|
761
583
|
emitter.emit(usingThinkingLevel
|
|
762
584
|
? `generating response (thinking-level=${input.thinkingLevel})…`
|
|
763
585
|
: effectiveThinkingBudget === null
|
|
@@ -790,9 +612,9 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
790
612
|
// `input.maxOutputTokens` — otherwise we omit it and Gemini uses
|
|
791
613
|
// its model-default cap (per docs, = the model's advertised
|
|
792
614
|
// `outputTokenLimit`, currently 65,536 for pro-tier 3.x/2.5). Budget
|
|
793
|
-
// reservation always uses
|
|
794
|
-
// model limit
|
|
795
|
-
//
|
|
615
|
+
// reservation always uses the cap the decode runs under (explicit cap
|
|
616
|
+
// OR model limit, cut by the input window on a long decode) as the
|
|
617
|
+
// worst-case upper bound of what a completed request bills.
|
|
796
618
|
// Cast to `ThinkingLevel` rather than indexing the runtime enum object
|
|
797
619
|
// (`ThinkingLevel[input.thinkingLevel]`). For string enums the value
|
|
798
620
|
// IS the member name, so passing `"HIGH"` as `ThinkingLevel` is
|
|
@@ -810,7 +632,7 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
810
632
|
: { thinkingBudget: effectiveThinkingBudget, includeThoughts: true };
|
|
811
633
|
const tierField = serviceTierConfig(serviceTier);
|
|
812
634
|
const buildConfig = (cacheId) => {
|
|
813
|
-
const maxOutputField =
|
|
635
|
+
const maxOutputField = runCap.wire !== undefined ? { maxOutputTokens: runCap.wire } : {};
|
|
814
636
|
return cacheId
|
|
815
637
|
? { cachedContent: cacheId, thinkingConfig, ...maxOutputField, ...tierField }
|
|
816
638
|
: {
|
|
@@ -830,222 +652,70 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
830
652
|
return attachments.parts.length === 0 ? input.prompt : [userTurn];
|
|
831
653
|
return [...inline, userTurn];
|
|
832
654
|
};
|
|
833
|
-
//
|
|
834
|
-
//
|
|
835
|
-
//
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
signal: abortSignal,
|
|
862
|
-
onThoughtChunk: (text) => {
|
|
863
|
-
// Surface the model's reasoning live as it arrives. Throttled
|
|
864
|
-
// to ~1.5s by collectStream so the MCP host's progress channel
|
|
865
|
-
// doesn't get flooded on long thinking bursts.
|
|
866
|
-
const trimmed = text.trim().slice(0, 80);
|
|
867
|
-
if (trimmed.length > 0)
|
|
868
|
-
emitter.emit(`thinking: ${trimmed}…`);
|
|
869
|
-
},
|
|
870
|
-
// v1.12.0 — reset the stall watchdog on every chunk (text or
|
|
871
|
-
// thought). Both prove the stream is alive. No-op when stall
|
|
872
|
-
// is disabled (env + per-call both unset).
|
|
873
|
-
onChunkReceived: () => timeoutController.recordChunk(),
|
|
874
|
-
});
|
|
875
|
-
}
|
|
876
|
-
catch (err) {
|
|
877
|
-
// Self-heal: if Gemini rejected our cached content (evicted, expired,
|
|
878
|
-
// externally deleted), invalidate locally and retry ONCE with a fresh
|
|
879
|
-
// cache. Prevents users from seeing hard failures they'd otherwise need
|
|
880
|
-
// to clear/reindex manually. Budget: one retry; a second stale-cache
|
|
881
|
-
// error propagates with both errors chained via Error.cause.
|
|
882
|
-
if (activePrep.cacheId && isStaleCacheError(err)) {
|
|
883
|
-
logger.warn(`Gemini rejected cached content ${activePrep.cacheId}; invalidating and retrying once.`);
|
|
884
|
-
try {
|
|
885
|
-
// Reset the cache pointer only — keep `files` rows so the rebuild
|
|
886
|
-
// can reuse uploaded files via content-hash dedup instead of
|
|
887
|
-
// re-uploading. The Gemini-side cache is already dead (that's
|
|
888
|
-
// what triggered this branch), so no `caches.delete` needed.
|
|
889
|
-
markCacheStale({ manifest: ctx.manifest, workspaceRoot });
|
|
890
|
-
const rebuilt = await prepareContext({
|
|
891
|
-
client: ctx.client,
|
|
892
|
-
manifest: ctx.manifest,
|
|
893
|
-
scan,
|
|
894
|
-
model: resolved,
|
|
895
|
-
systemPromptHash,
|
|
896
|
-
systemInstruction: SYSTEM_INSTRUCTION_Q_AND_A,
|
|
897
|
-
ttlSeconds: ctx.config.cacheTtlSeconds,
|
|
898
|
-
cacheMinTokens: ctx.config.cacheMinTokens,
|
|
899
|
-
emitter,
|
|
900
|
-
allowCaching: !input.noCache && scan.files.length > 0,
|
|
901
|
-
// v1.12.1 — propagate signal into stale-cache rebuild too.
|
|
902
|
-
signal: abortSignal,
|
|
903
|
-
// v1.13.0 — keep cachingMode consistent across happy + retry paths.
|
|
904
|
-
cachingMode: requestedCachingMode,
|
|
905
|
-
});
|
|
906
|
-
// Cancel the original throttle reservation — its `tsMs` was
|
|
907
|
-
// stamped at first-dispatch time, which is now seconds in the
|
|
908
|
-
// past (the rebuild took real time). Re-reserve fresh so the
|
|
909
|
-
// retry's tsMs reflects when the retry call actually hits
|
|
910
|
-
// Gemini's quota counter. Without this, our window would expire
|
|
911
|
-
// locally before Gemini's, leaving a gap where concurrent
|
|
912
|
-
// callers bust the per-minute limit (PR #19 round-2 GPT review).
|
|
913
|
-
if (throttleReservationId !== -1) {
|
|
914
|
-
ctx.throttle.cancel(throttleReservationId);
|
|
915
|
-
throttleReservationId = -1;
|
|
916
|
-
}
|
|
917
|
-
await reserveForDispatch();
|
|
918
|
-
// The stale-cache retry itself can hit a transient network failure;
|
|
919
|
-
// wrap it too so the rebuild isn't wasted on a one-off blip.
|
|
920
|
-
// Stale-cache retry: discard partial response, open a brand-new
|
|
921
|
-
// stream with the rebuilt cache. Gemini's stream API doesn't
|
|
922
|
-
// support resume, so this is the only correct semantics.
|
|
923
|
-
// Same retry-OPENING-only contract as the happy path — see above.
|
|
924
|
-
const retryStream = await withNetworkRetry(() => ctx.client.models.generateContentStream({
|
|
925
|
-
model: resolved.resolved,
|
|
926
|
-
contents: buildContents(rebuilt.cacheId, rebuilt.inlineContents),
|
|
927
|
-
config: { ...buildConfig(rebuilt.cacheId), abortSignal },
|
|
928
|
-
}), {
|
|
929
|
-
attempts: networkAttempts(serviceTier),
|
|
930
|
-
signal: abortSignal,
|
|
931
|
-
onRetry: (attempt, retryErr) => {
|
|
932
|
-
logger.warn(`ask (stale-cache retry): retrying generateContent after transient network failure (attempt ${attempt}): ${retryErr instanceof Error ? retryErr.message : String(retryErr)}`);
|
|
933
|
-
},
|
|
934
|
-
});
|
|
935
|
-
streamOpened = true;
|
|
936
|
-
response = await collectStream(retryStream, {
|
|
937
|
-
signal: abortSignal,
|
|
938
|
-
onThoughtChunk: (text) => {
|
|
939
|
-
const trimmed = text.trim().slice(0, 80);
|
|
940
|
-
if (trimmed.length > 0)
|
|
941
|
-
emitter.emit(`thinking: ${trimmed}…`);
|
|
942
|
-
},
|
|
943
|
-
// v1.12.0 — stall watchdog reset on retry stream too.
|
|
944
|
-
onChunkReceived: () => timeoutController.recordChunk(),
|
|
945
|
-
});
|
|
946
|
-
activePrep = rebuilt;
|
|
947
|
-
retriedOnStaleCache = true;
|
|
948
|
-
}
|
|
949
|
-
catch (retryErr) {
|
|
950
|
-
// Re-throw the retry's TimeoutError directly so the outer catch's
|
|
951
|
-
// `isTimeoutAbort` check sees it on the cause chain. Without this,
|
|
952
|
-
// wrapping with `cause: err` (the original stale-cache error)
|
|
953
|
-
// would mask the timeout — outer catch maps to UNKNOWN instead of
|
|
954
|
-
// TIMEOUT, breaking the contract on stale-cache+timeout paths.
|
|
955
|
-
if (isTimeoutAbort(retryErr))
|
|
956
|
-
throw retryErr;
|
|
957
|
-
// Otherwise preserve the ORIGINAL stale-cache error as `cause` so
|
|
958
|
-
// ops can root-cause diagnostics across both the first 404 and any
|
|
959
|
-
// rebuild-time failure.
|
|
960
|
-
// an HTTP error on the retry (a 429/503) IS the error: thrown as the SDK made it, so its status and the
|
|
961
|
-
// throttle's retry hint are read as usual; only a status-less failure is wrapped over the stale 404
|
|
962
|
-
if (statusOf(retryErr) !== undefined)
|
|
963
|
-
throw retryErr;
|
|
964
|
-
throw new Error(`ask retry after stale cache failed: ${retryErr instanceof Error ? retryErr.message : String(retryErr)}`, { cause: err });
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
else {
|
|
968
|
-
throw err;
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
if (activePrep.cacheId) {
|
|
972
|
-
ctx.ttlWatcher.markHot(workspaceRoot, activePrep.cacheId, ctx.config.cacheTtlSeconds);
|
|
973
|
-
}
|
|
655
|
+
// The decode — and, on a stale cache before any request completed, its one retry on a rebuilt cache. The TPM
|
|
656
|
+
// reservation is taken here, after `prepareContext` and immediately before the first request, so its timestamp
|
|
657
|
+
// is when the tokens reach Gemini's quota counter; only the OPENING of a request is retried (a mid-stream
|
|
658
|
+
// failure re-sent would bill the work again).
|
|
659
|
+
const decoded = await decodeOnContext({
|
|
660
|
+
client: ctx.client,
|
|
661
|
+
booking: { workspaceRoot, toolName: 'ask', model: resolved.resolved, serviceTier },
|
|
662
|
+
cachingMode: (prep) => cachingRan(prep, caching).mode,
|
|
663
|
+
request: (prep) => ({
|
|
664
|
+
contents: buildContents(prep.cacheId, prep.inlineContents),
|
|
665
|
+
config: buildConfig(prep.cacheId),
|
|
666
|
+
}),
|
|
667
|
+
cache: {
|
|
668
|
+
ttlWatcher: ctx.ttlWatcher,
|
|
669
|
+
manifest: ctx.manifest,
|
|
670
|
+
ttlSeconds: ctx.config.cacheTtlSeconds,
|
|
671
|
+
prepare,
|
|
672
|
+
},
|
|
673
|
+
openAttempts: networkAttempts(serviceTier), // a flex request is never re-sent
|
|
674
|
+
limits: { maxRequests: runCap.maxRequests, cap: runCap.effective },
|
|
675
|
+
timeout: timeoutController,
|
|
676
|
+
emit: (message) => emitter.emit(message),
|
|
677
|
+
completed,
|
|
678
|
+
tracking,
|
|
679
|
+
throttle: dispatch,
|
|
680
|
+
inputTokens: { estimated: estimatedInputTokens, counted: preflight?.effectiveTokens },
|
|
681
|
+
}, ctxPrep);
|
|
682
|
+
const { response, prep: activePrep, retriedOnStaleCache } = decoded;
|
|
974
683
|
const text = response.text;
|
|
975
684
|
// `collectStream` already aggregated thought-flagged parts and capped
|
|
976
685
|
// the joined summary at 1200 chars (T20). Reuse directly — re-iterating
|
|
977
686
|
// `response.candidates` would risk drift between in-flight thought
|
|
978
687
|
// emit (live progress) and post-call summary.
|
|
979
688
|
const thinkingSummary = response.thoughtsSummary;
|
|
980
|
-
const usage = response.usageMetadata;
|
|
981
|
-
const cached = typeof usage?.cachedContentTokenCount === 'number' ? usage.cachedContentTokenCount : 0;
|
|
982
|
-
const inputTotal = typeof usage?.promptTokenCount === 'number' ? usage.promptTokenCount : 0;
|
|
983
|
-
const uncached = Math.max(0, inputTotal - cached);
|
|
984
|
-
const output = typeof usage?.candidatesTokenCount === 'number' ? usage.candidatesTokenCount : 0;
|
|
985
|
-
const thinking = typeof usage?.thoughtsTokenCount === 'number' ? usage.thoughtsTokenCount : 0;
|
|
986
|
-
// Finalise the throttle reservation with Gemini's actual
|
|
987
|
-
// `promptTokenCount` — includes cached tokens, which DO count against
|
|
988
|
-
// the per-minute quota (empirically confirmed 2026-04-20). If actual
|
|
989
|
-
// < estimate, subsequent reserves see the freed headroom; if >, they
|
|
990
|
-
// back off accordingly.
|
|
991
|
-
if (throttleReservationId !== -1) {
|
|
992
|
-
ctx.throttle.release(throttleReservationId, inputTotal);
|
|
993
|
-
throttleReservationId = -1;
|
|
994
|
-
}
|
|
995
|
-
const cost = estimateCostUsd({
|
|
996
|
-
model: resolved.resolved,
|
|
997
|
-
serviceTier,
|
|
998
|
-
uncachedInputTokens: uncached,
|
|
999
|
-
cachedInputTokens: cached,
|
|
1000
|
-
outputTokens: output,
|
|
1001
|
-
thinkingTokens: thinking,
|
|
1002
|
-
});
|
|
1003
|
-
const costMicros = toMicrosUsd(cost);
|
|
1004
689
|
// Telemetry and the response record the mode that ran, not the one asked for.
|
|
1005
690
|
const ran = cachingRan(activePrep, caching);
|
|
1006
691
|
const effectiveCachingMode = ran.mode;
|
|
1007
692
|
const durationMs = Date.now() - started;
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
ctx.manifest.finalizeBudgetReservation(reservationId, {
|
|
1018
|
-
cachedTokens: cached,
|
|
1019
|
-
uncachedTokens: uncached,
|
|
1020
|
-
costUsdMicro: costMicros,
|
|
1021
|
-
durationMs,
|
|
1022
|
-
cachingMode: effectiveCachingMode,
|
|
1023
|
-
cachedContentTokenCount: cached,
|
|
1024
|
-
});
|
|
1025
|
-
}
|
|
1026
|
-
catch (finalizeErr) {
|
|
1027
|
-
logger.error(`ask: finalizeBudgetReservation failed for id=${reservationId}; reservation row keeps the estimate (${costMicros} micros). Error: ${String(finalizeErr)}`);
|
|
1028
|
-
}
|
|
1029
|
-
reservationId = null;
|
|
1030
|
-
}
|
|
1031
|
-
else {
|
|
1032
|
-
ctx.manifest.insertUsageMetric({
|
|
693
|
+
// Settled here, once: the outer catch then neither releases the reservation nor books the call again. What the
|
|
694
|
+
// API billed is every request that completed, over a long decode and a stale-cache retry alike.
|
|
695
|
+
const settledReservation = reservationId;
|
|
696
|
+
reservationId = null;
|
|
697
|
+
tracking.ledgerEntry = null;
|
|
698
|
+
const settled = settleAnswer({
|
|
699
|
+
manifest: ctx.manifest,
|
|
700
|
+
reservationId: settledReservation,
|
|
701
|
+
entry: {
|
|
1033
702
|
workspaceRoot,
|
|
1034
703
|
toolName: 'ask',
|
|
1035
704
|
model: resolved.resolved,
|
|
1036
|
-
|
|
1037
|
-
uncachedTokens: uncached,
|
|
1038
|
-
costUsdMicro: costMicros,
|
|
1039
|
-
durationMs,
|
|
1040
|
-
occurredAt: Date.now(),
|
|
705
|
+
serviceTier,
|
|
1041
706
|
cachingMode: effectiveCachingMode,
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
707
|
+
},
|
|
708
|
+
completed,
|
|
709
|
+
response,
|
|
710
|
+
cap: runCap,
|
|
711
|
+
cut: inputWindowCut,
|
|
712
|
+
durationMs,
|
|
713
|
+
});
|
|
714
|
+
const finish = settled.finish;
|
|
1045
715
|
// Settled and billed above; an empty answer at the cap is a failure the caller can act on, never a success.
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
716
|
+
if (settled.outputCapStop !== null) {
|
|
717
|
+
return errorResult(`ask: ${settled.outputCapStop.message}`, settled.outputCapStop.fields);
|
|
718
|
+
}
|
|
1049
719
|
if (scan.truncated) {
|
|
1050
720
|
logger.warn(`workspace ${workspaceRoot} contains more files than GEMINI_CODE_CONTEXT_MAX_FILES (${ctx.config.maxFilesPerWorkspace}); the tail was dropped before indexing.`);
|
|
1051
721
|
}
|
|
@@ -1060,11 +730,11 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
1060
730
|
contextWindow: resolved.inputTokenLimit,
|
|
1061
731
|
thinkingBudget: effectiveThinkingBudget,
|
|
1062
732
|
thinkingLevel: input.thinkingLevel ?? null,
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
733
|
+
...settled.usage,
|
|
734
|
+
...settled.cutFields,
|
|
735
|
+
// `truncated`: the answer stopped at the output cap (MAX_TOKENS, or a long decode at its limits) — it is cut
|
|
736
|
+
finishReason: finish.finishReason,
|
|
737
|
+
truncated: finish.truncated,
|
|
1068
738
|
cacheHit: activePrep.reused,
|
|
1069
739
|
cacheRebuilt: activePrep.rebuilt || retriedOnStaleCache,
|
|
1070
740
|
retriedOnStaleCache,
|
|
@@ -1104,77 +774,47 @@ async function executeAskBody(input, ctx, workspaceRoot, model, started) {
|
|
|
1104
774
|
}
|
|
1105
775
|
catch (err) {
|
|
1106
776
|
logger.error(`ask failed: ${safeForLog(err)}`);
|
|
1107
|
-
//
|
|
1108
|
-
//
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
if (retryDelayMs !== null && resolvedModelKey !== null) {
|
|
1125
|
-
ctx.throttle.recordRetryHint(resolvedModelKey, retryDelayMs);
|
|
1126
|
-
}
|
|
1127
|
-
// Release any unconsumed budget reservation so the failed call's
|
|
1128
|
-
// estimate doesn't eat into future headroom for today. Wrap in its
|
|
1129
|
-
// own try/catch — better-sqlite3 can throw SQLITE_BUSY or I/O errors
|
|
1130
|
-
// and we must not let a rollback-time DB failure replace the real
|
|
1131
|
-
// tool error the user is waiting on.
|
|
1132
|
-
if (reservationId !== null) {
|
|
1133
|
-
try {
|
|
1134
|
-
ctx.manifest.cancelBudgetReservation(reservationId);
|
|
1135
|
-
}
|
|
1136
|
-
catch (cancelErr) {
|
|
1137
|
-
logger.error(`ask: cancelBudgetReservation failed for id=${reservationId}; reservation row keeps the estimate. Error: ${String(cancelErr)}`);
|
|
1138
|
-
}
|
|
1139
|
-
reservationId = null;
|
|
1140
|
-
}
|
|
1141
|
-
// Drop the TPM reservation on any failure path. If Gemini actually
|
|
1142
|
-
// consumed quota server-side before failing, the hint path above
|
|
1143
|
-
// (T22a) compensates via `recordRetryHint` before we cancel. For
|
|
1144
|
-
// non-429 errors (validation, transport), we under-count slightly
|
|
1145
|
-
// but bounded — accepted trade-off documented in PR #19 round-2.
|
|
1146
|
-
if (throttleReservationId !== -1) {
|
|
1147
|
-
ctx.throttle.cancel(throttleReservationId);
|
|
1148
|
-
throttleReservationId = -1;
|
|
1149
|
-
}
|
|
777
|
+
// A 429's retry hint seeds the throttle; the ledger keeps what the completed requests billed and releases the rest
|
|
778
|
+
// of the reservation (all of it when none completed); their usage goes into the error result.
|
|
779
|
+
const failedUsage = settleFailure({
|
|
780
|
+
manifest: ctx.manifest,
|
|
781
|
+
failed: {
|
|
782
|
+
toolName: 'ask',
|
|
783
|
+
reservationId,
|
|
784
|
+
entry: tracking.ledgerEntry,
|
|
785
|
+
completed,
|
|
786
|
+
durationMs: Date.now() - started,
|
|
787
|
+
},
|
|
788
|
+
err,
|
|
789
|
+
model: resolvedModelKey,
|
|
790
|
+
throttle: ctx.throttle,
|
|
791
|
+
dispatch,
|
|
792
|
+
});
|
|
793
|
+
reservationId = null;
|
|
1150
794
|
// Timeout-driven abort gets a dedicated errorCode so callers can
|
|
1151
795
|
// distinguish "Gemini was slow" from "schema invalid" / "auth failed".
|
|
1152
796
|
// v1.12.0 — `timeoutKind` ('total' | 'stall') distinguishes the
|
|
1153
797
|
// wall-clock cap from the heartbeat-aware stall watchdog so callers
|
|
1154
798
|
// can apply different policies (e.g. retry on stall but not on total).
|
|
1155
799
|
if (isTimeoutAbort(err)) {
|
|
1156
|
-
const
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
const limitMs = kind === 'stall' ? stallMs : ms;
|
|
1160
|
-
emitter.emit(`ask: aborted after ${limitMs ?? '?'}ms ${kind ?? 'timeout'}`);
|
|
1161
|
-
const message = kind === 'stall'
|
|
1162
|
-
? `ask timed out — no chunk received for ${stallMs ?? '?'}ms (stall watchdog). Increase \`stallMs\` per call or set \`GEMINI_CODE_CONTEXT_ASK_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.`
|
|
1163
|
-
: `ask timed out after ${ms ?? '?'}ms (total wall-clock). Increase \`timeoutMs\` per call or set \`GEMINI_CODE_CONTEXT_ASK_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.`;
|
|
1164
|
-
return errorResult(message, {
|
|
800
|
+
const timedOut = timedOutCall('ask', err, timeoutController);
|
|
801
|
+
emitter.emit(`ask: aborted after ${timedOut.limitMs ?? '?'}ms ${timedOut.kind}`);
|
|
802
|
+
return errorResult(timedOut.message, {
|
|
1165
803
|
errorCode: 'TIMEOUT',
|
|
1166
|
-
timeoutKind: kind
|
|
1167
|
-
timeoutMs:
|
|
1168
|
-
stallMs,
|
|
804
|
+
timeoutKind: timedOut.kind,
|
|
805
|
+
timeoutMs: timeoutController.timeoutMs,
|
|
806
|
+
stallMs: timeoutController.stallMs,
|
|
1169
807
|
retryable: true,
|
|
1170
808
|
serviceTier: reportedTier(),
|
|
809
|
+
...failedUsage,
|
|
1171
810
|
});
|
|
1172
811
|
}
|
|
1173
812
|
const httpStatus = statusOf(err);
|
|
1174
813
|
return errorResult(`ask failed: ${err instanceof Error ? err.message : String(err)}`, {
|
|
1175
|
-
...tierErrorMeta(err, streamOpened),
|
|
814
|
+
...tierErrorMeta(err, tracking.streamOpened),
|
|
1176
815
|
serviceTier: reportedTier(),
|
|
1177
816
|
...(httpStatus !== undefined ? { httpStatus } : {}),
|
|
817
|
+
...failedUsage,
|
|
1178
818
|
});
|
|
1179
819
|
}
|
|
1180
820
|
finally {
|