@qmediat.io/gemini-code-context-mcp 1.4.0 → 1.5.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +89 -0
  2. package/README.md +36 -3
  3. package/dist/config.d.ts +23 -0
  4. package/dist/config.d.ts.map +1 -1
  5. package/dist/config.js +9 -0
  6. package/dist/config.js.map +1 -1
  7. package/dist/indexer/globs.d.ts +106 -0
  8. package/dist/indexer/globs.d.ts.map +1 -1
  9. package/dist/indexer/globs.js +151 -11
  10. package/dist/indexer/globs.js.map +1 -1
  11. package/dist/tools/agentic/sandbox.d.ts +55 -0
  12. package/dist/tools/agentic/sandbox.d.ts.map +1 -0
  13. package/dist/tools/agentic/sandbox.js +272 -0
  14. package/dist/tools/agentic/sandbox.js.map +1 -0
  15. package/dist/tools/agentic/workspace-tools.d.ts +104 -0
  16. package/dist/tools/agentic/workspace-tools.d.ts.map +1 -0
  17. package/dist/tools/agentic/workspace-tools.js +518 -0
  18. package/dist/tools/agentic/workspace-tools.js.map +1 -0
  19. package/dist/tools/ask-agentic.tool.d.ts +85 -0
  20. package/dist/tools/ask-agentic.tool.d.ts.map +1 -0
  21. package/dist/tools/ask-agentic.tool.js +727 -0
  22. package/dist/tools/ask-agentic.tool.js.map +1 -0
  23. package/dist/tools/ask.tool.d.ts +12 -12
  24. package/dist/tools/ask.tool.d.ts.map +1 -1
  25. package/dist/tools/ask.tool.js +522 -477
  26. package/dist/tools/ask.tool.js.map +1 -1
  27. package/dist/tools/code.tool.d.ts +8 -8
  28. package/dist/tools/code.tool.d.ts.map +1 -1
  29. package/dist/tools/code.tool.js +457 -423
  30. package/dist/tools/code.tool.js.map +1 -1
  31. package/dist/tools/index.d.ts.map +1 -1
  32. package/dist/tools/index.js +2 -0
  33. package/dist/tools/index.js.map +1 -1
  34. package/dist/tools/registry.d.ts +1 -1
  35. package/dist/tools/registry.d.ts.map +1 -1
  36. package/dist/tools/registry.js +8 -2
  37. package/dist/tools/registry.js.map +1 -1
  38. package/package.json +2 -2
@@ -75,8 +75,14 @@ export const codeInputSchema = z
75
75
  .min(1)
76
76
  .optional()
77
77
  .describe("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). Values larger than the resolved model's limit are clamped. 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."),
78
- includeGlobs: z.array(z.string()).optional(),
79
- excludeGlobs: z.array(z.string()).optional(),
78
+ includeGlobs: z
79
+ .array(z.string())
80
+ .optional()
81
+ .describe('Additional file extensions or filenames to include.'),
82
+ excludeGlobs: z
83
+ .array(z.string())
84
+ .optional()
85
+ .describe('Additional patterns to exclude. Supports three shapes: (1) directory names or path prefixes (`node_modules`, `src/vendor`, `./dist/`, `.vercel/`), (2) literal filenames exact-match, including bare dot-prefixed names (`pr27-diff.txt`, `foo.bar.baz`, `.env`, `.map`, `.tsbuildinfo`), (3) extension globs that match via endsWith (`*.tsbuildinfo`, `*.map`). Bare dot-prefixed names like `.env` are treated as exact filename literals — write `*.env` for extension semantics. Paths are POSIX-normalised (backslashes → `/`, leading `./` and trailing `/` stripped). Case-insensitive. No mid-string `*` / `**` / `?` — split into dir + extension patterns if needed.'),
80
86
  })
81
87
  .refine((data) => !(data.thinkingBudget !== undefined && data.thinkingLevel !== undefined), {
82
88
  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).',
@@ -125,461 +131,489 @@ export const codeTool = {
125
131
  async execute(input, ctx) {
126
132
  const started = Date.now();
127
133
  const workspaceRoot = resolve(input.workspace ?? process.cwd());
128
- const modelRequest = input.model ?? 'latest-pro-thinking';
129
- const expectEdits = input.expectEdits ?? true;
130
- const codeExecution = input.codeExecution ?? false;
131
- // Schema `.refine()` guarantees `thinkingBudget` and `thinkingLevel` are
132
- // never both set. `usingThinkingLevel` is a single source of truth for
133
- // the "is the caller driving reasoning via the discrete-tier knob?"
134
- // predicate — used by the cost estimate, the emitter message, and the
135
- // thinkingConfig shape below.
136
- const usingThinkingLevel = input.thinkingLevel !== undefined;
137
- // `code` keeps its pre-existing default of 16_384 for the `thinkingBudget`
138
- // path (strong reasoning for coding out of the box — coding tasks
139
- // genuinely benefit from it and callers rarely want to disable thinking).
140
- // When the caller uses `thinkingLevel` instead, `thinkingBudget` stays
141
- // undefined and we take the level branch below.
142
- const thinkingBudget = usingThinkingLevel ? undefined : (input.thinkingBudget ?? 16_384);
143
- let reservationId = null;
144
- // `-1` signals "no reservation held". Mirror of ask.tool.ts.
145
- let throttleReservationId = -1;
146
- // Canonical resolved-model string for retry-hint seeding (T22a).
147
- // See ask.tool.ts for rationale.
148
- let resolvedModelKey = null;
149
- const emitter = createProgressEmitter(ctx.server, ctx.progressToken);
134
+ return executeCodeBody(input, ctx, workspaceRoot, started);
135
+ },
136
+ };
137
+ async function executeCodeBody(input, ctx, workspaceRoot, started) {
138
+ const modelRequest = input.model ?? 'latest-pro-thinking';
139
+ const expectEdits = input.expectEdits ?? true;
140
+ const codeExecution = input.codeExecution ?? false;
141
+ // Schema `.refine()` guarantees `thinkingBudget` and `thinkingLevel` are
142
+ // never both set. `usingThinkingLevel` is a single source of truth for
143
+ // the "is the caller driving reasoning via the discrete-tier knob?"
144
+ // predicate — used by the cost estimate, the emitter message, and the
145
+ // thinkingConfig shape below.
146
+ const usingThinkingLevel = input.thinkingLevel !== undefined;
147
+ // `code` keeps its pre-existing default of 16_384 for the `thinkingBudget`
148
+ // path (strong reasoning for coding out of the box — coding tasks
149
+ // genuinely benefit from it and callers rarely want to disable thinking).
150
+ // When the caller uses `thinkingLevel` instead, `thinkingBudget` stays
151
+ // undefined and we take the level branch below.
152
+ const thinkingBudget = usingThinkingLevel ? undefined : (input.thinkingBudget ?? 16_384);
153
+ let reservationId = null;
154
+ // `-1` signals "no reservation held". Mirror of ask.tool.ts.
155
+ let throttleReservationId = -1;
156
+ // Canonical resolved-model string for retry-hint seeding (T22a).
157
+ // See ask.tool.ts for rationale.
158
+ let resolvedModelKey = null;
159
+ const emitter = createProgressEmitter(ctx.server, ctx.progressToken);
160
+ try {
161
+ // Workspace validation inside try → reported as a regular tool error
162
+ // (errorResult, "code failed: …") rather than an unhandled exception
163
+ // surfaced by the server-level handler.
150
164
  try {
151
- // Workspace validation inside try → reported as a regular tool error
152
- // (errorResult, "code failed: …") rather than an unhandled exception
153
- // surfaced by the server-level handler.
154
- try {
155
- validateWorkspacePath(workspaceRoot);
165
+ validateWorkspacePath(workspaceRoot);
166
+ }
167
+ catch (err) {
168
+ if (err instanceof WorkspaceValidationError) {
169
+ return errorResult(`code: ${err.message}`);
156
170
  }
157
- catch (err) {
158
- if (err instanceof WorkspaceValidationError) {
159
- return errorResult(`code: ${err.message}`);
160
- }
161
- throw err;
171
+ throw err;
172
+ }
173
+ emitter.emit(`resolving model '${modelRequest}'…`);
174
+ // `code` is strictly text-reasoning — coding tasks genuinely benefit
175
+ // from reasoning tokens, and dispatching to fast/lite tiers would
176
+ // degrade output quality without meaningful cost savings. Crucially:
177
+ // this category gate prevents image-gen / audio-gen / agent models
178
+ // (which may share `pro` tokens with text models in Google's registry)
179
+ // from reaching `generateContent` with a code-review prompt — the
180
+ // primary motivation for the v1.4.0 taxonomy work.
181
+ const resolved = await resolveModel(modelRequest, ctx.client, {
182
+ requiredCategory: ['text-reasoning'],
183
+ });
184
+ resolvedModelKey = resolved.resolved;
185
+ emitter.emit(`scanning workspace ${workspaceRoot}…`);
186
+ const scan = await scanWorkspace(workspaceRoot, {
187
+ ...(input.includeGlobs !== undefined ? { includeGlobs: input.includeGlobs } : {}),
188
+ ...(input.excludeGlobs !== undefined ? { excludeGlobs: input.excludeGlobs } : {}),
189
+ maxFiles: ctx.config.maxFilesPerWorkspace,
190
+ maxFileSizeBytes: ctx.config.maxFileSizeBytes,
191
+ });
192
+ // Output-cap strategy (v1.4.0) — see ask.tool.ts for full rationale
193
+ // of the three-layer precedence. Summary: input.maxOutputTokens
194
+ // (per-call) > ctx.config.forceMaxOutputTokens (MCP-host env) >
195
+ // auto (omit from wire; Gemini uses model-default). Code review
196
+ // commonly produces long OLD/NEW diff blocks, so operators doing
197
+ // heavy review work set `GEMINI_CODE_CONTEXT_FORCE_MAX_OUTPUT=true`
198
+ // to pin every call at the model's full 65,536-token capacity.
199
+ const CODE_MAX_OUTPUT_TOKENS_FALLBACK = 65_536;
200
+ const modelOutputLimit = typeof resolved.outputTokenLimit === 'number' && resolved.outputTokenLimit > 0
201
+ ? resolved.outputTokenLimit
202
+ : CODE_MAX_OUTPUT_TOKENS_FALLBACK;
203
+ const wireMaxOutputTokens = input.maxOutputTokens !== undefined
204
+ ? Math.min(input.maxOutputTokens, modelOutputLimit)
205
+ : ctx.config.forceMaxOutputTokens
206
+ ? modelOutputLimit
207
+ : undefined;
208
+ // Effective cap for thinking-budget clamp and budget reservation.
209
+ // When auto (neither override set), Gemini's model-default equals
210
+ // modelOutputLimit per Google docs, so using the limit as worst-case
211
+ // keeps the daily $ cap a true upper bound.
212
+ const effectiveOutputCap = wireMaxOutputTokens ?? modelOutputLimit;
213
+ // Gemini requires `thinkingBudget < maxOutputTokens` (the thinking pool
214
+ // is carved out of the candidate-output allowance). The hard cap above
215
+ // may clamp maxOutputTokens as low as the model's `outputTokenLimit`
216
+ // (e.g. 8_192 on Flash-lite). Clamp the effective thinkingBudget so
217
+ // the generateContent call never 400s on this invariant; reserve at
218
+ // least 1_024 tokens for the actual completion.
219
+ //
220
+ // Only meaningful on the thinkingBudget path — when the caller uses
221
+ // `thinkingLevel` instead, `thinkingBudget` is `undefined` and this
222
+ // value is ignored by the thinkingConfig builder below.
223
+ //
224
+ // Early-reject when the caller's per-call `maxOutputTokens` can't fit
225
+ // an explicit positive `thinkingBudget` + 1024-token answer reserve.
226
+ // Silently clamping to 0 would make Gemini 3 Pro 400 on a
227
+ // "thinking disabled" error and obscure the real mismatch (PR #22
228
+ // round-3 review finding #C).
229
+ if (input.thinkingBudget !== undefined &&
230
+ input.thinkingBudget > 0 &&
231
+ input.maxOutputTokens !== undefined &&
232
+ effectiveOutputCap < input.thinkingBudget + 1024) {
233
+ return errorResult(`code: thinkingBudget (${input.thinkingBudget}) + 1024-token answer reserve exceeds maxOutputTokens (${effectiveOutputCap}). Raise \`maxOutputTokens\` to at least ${input.thinkingBudget + 1024}, or lower \`thinkingBudget\`.`);
234
+ }
235
+ const effectiveThinkingBudget = thinkingBudget !== undefined && thinkingBudget > 0
236
+ ? Math.max(0, Math.min(thinkingBudget, effectiveOutputCap - 1024))
237
+ : 0;
238
+ // Cost-estimate thinking-token reserve — tier-aware when the caller
239
+ // uses `thinkingLevel`, exact-on-the-budget when they use
240
+ // `thinkingBudget`. Mirror of `ask.tool.ts` logic; see
241
+ // `THINKING_LEVEL_RESERVE` in `shared/thinking.ts` for the rationale
242
+ // behind the per-tier numbers and why HIGH falls through to the
243
+ // dynamic cap.
244
+ //
245
+ // Defensive clamp: tier reserves (MINIMAL=512, LOW=2048, MEDIUM=4096)
246
+ // are fixed constants, but `maxOutputTokens` can be clamped down to a
247
+ // resolved model's `outputTokenLimit`. A future small-cap model (say
248
+ // `outputTokenLimit: 4000`) would let MEDIUM's 4096 exceed the
249
+ // available headroom and over-estimate the budget reservation. Clamp
250
+ // every tier's reserve against the dynamic headroom so the upper
251
+ // bound stays coherent across model rollouts (PR #17 self-review F2).
252
+ const thinkingHeadroom = Math.max(0, effectiveOutputCap - 1024);
253
+ const thinkingTokensForEstimate = input.thinkingLevel !== undefined
254
+ ? Math.min(THINKING_LEVEL_RESERVE[input.thinkingLevel] ?? thinkingHeadroom, thinkingHeadroom)
255
+ : effectiveThinkingBudget;
256
+ // Shared input-token fingerprint for budget + TPM throttle (see
257
+ // ask.tool.ts for the rationale — same `Math.ceil(bytes/4)` tokenisation
258
+ // used by `estimatePreCallCostUsd`).
259
+ const workspaceBytes = scan.files.reduce((sum, f) => sum + f.size, 0);
260
+ const estimatedInputTokens = Math.ceil(workspaceBytes / 4) + Math.ceil(input.task.length / 4);
261
+ // v1.5.0 preflight — mirror of ask.tool.ts guard. See there for rationale.
262
+ const contextWindow = resolved.inputTokenLimit;
263
+ if (typeof contextWindow === 'number' && contextWindow > 0) {
264
+ const threshold = Math.floor(contextWindow * ctx.config.workspaceGuardRatio);
265
+ if (estimatedInputTokens > threshold) {
266
+ const pctDisplay = Math.round(ctx.config.workspaceGuardRatio * 100);
267
+ return errorResult(`Workspace too large for eager \`code\`: ~${estimatedInputTokens.toLocaleString()} input tokens exceeds ${threshold.toLocaleString()} (${pctDisplay}% of ${resolved.resolved}'s ${contextWindow.toLocaleString()} context window). Options: (a) pass \`excludeGlobs\` to filter large/generated files — supports \`*.ext\` patterns, filenames, and directory paths, (b) narrow with \`includeGlobs\`, (c) switch to a larger-context model, (d) split the workspace into subdirectories, or (e) use \`ask_agentic\` for Q&A-style analysis without uploading the repo (\`code\` itself still requires the eager path for its OLD/NEW edit format).`, {
268
+ errorCode: 'WORKSPACE_TOO_LARGE',
269
+ retryable: false,
270
+ estimatedInputTokens,
271
+ contextWindowTokens: contextWindow,
272
+ thresholdTokens: threshold,
273
+ guardRatio: ctx.config.workspaceGuardRatio,
274
+ resolvedModel: resolved.resolved,
275
+ filesIndexed: scan.files.length,
276
+ });
162
277
  }
163
- emitter.emit(`resolving model '${modelRequest}'…`);
164
- // `code` is strictly text-reasoning — coding tasks genuinely benefit
165
- // from reasoning tokens, and dispatching to fast/lite tiers would
166
- // degrade output quality without meaningful cost savings. Crucially:
167
- // this category gate prevents image-gen / audio-gen / agent models
168
- // (which may share `pro` tokens with text models in Google's registry)
169
- // from reaching `generateContent` with a code-review prompt — the
170
- // primary motivation for the v1.4.0 taxonomy work.
171
- const resolved = await resolveModel(modelRequest, ctx.client, {
172
- requiredCategory: ['text-reasoning'],
278
+ }
279
+ else {
280
+ logger.warn(`code: resolved model ${resolved.resolved} has no advertised inputTokenLimit — workspace size guard skipped. Request may fail downstream if the workspace exceeds the model's context window.`);
281
+ }
282
+ // Atomic budget reservation — see ask.tool.ts for the full rationale.
283
+ // Estimate includes the thinking budget (billed as output tokens on Pro)
284
+ // PLUS the candidate output cap.
285
+ if (Number.isFinite(ctx.config.dailyBudgetUsd)) {
286
+ const estimateUsd = estimatePreCallCostUsd({
287
+ model: resolved.resolved,
288
+ workspaceBytes,
289
+ promptChars: input.task.length,
290
+ expectedOutputTokens: effectiveOutputCap,
291
+ thinkingTokens: thinkingTokensForEstimate,
173
292
  });
174
- resolvedModelKey = resolved.resolved;
175
- emitter.emit(`scanning workspace ${workspaceRoot}…`);
176
- const scan = await scanWorkspace(workspaceRoot, {
177
- ...(input.includeGlobs !== undefined ? { includeGlobs: input.includeGlobs } : {}),
178
- ...(input.excludeGlobs !== undefined ? { excludeGlobs: input.excludeGlobs } : {}),
179
- maxFiles: ctx.config.maxFilesPerWorkspace,
180
- maxFileSizeBytes: ctx.config.maxFileSizeBytes,
293
+ const reserve = ctx.manifest.reserveBudget({
294
+ workspaceRoot,
295
+ toolName: 'code',
296
+ model: resolved.resolved,
297
+ estimatedCostMicros: toMicrosUsd(estimateUsd),
298
+ dailyBudgetMicros: toMicrosUsd(ctx.config.dailyBudgetUsd),
299
+ nowMs: Date.now(),
181
300
  });
182
- // Output-cap strategy (v1.4.0) — see ask.tool.ts for full rationale
183
- // of the three-layer precedence. Summary: input.maxOutputTokens
184
- // (per-call) > ctx.config.forceMaxOutputTokens (MCP-host env) >
185
- // auto (omit from wire; Gemini uses model-default). Code review
186
- // commonly produces long OLD/NEW diff blocks, so operators doing
187
- // heavy review work set `GEMINI_CODE_CONTEXT_FORCE_MAX_OUTPUT=true`
188
- // to pin every call at the model's full 65,536-token capacity.
189
- const CODE_MAX_OUTPUT_TOKENS_FALLBACK = 65_536;
190
- const modelOutputLimit = typeof resolved.outputTokenLimit === 'number' && resolved.outputTokenLimit > 0
191
- ? resolved.outputTokenLimit
192
- : CODE_MAX_OUTPUT_TOKENS_FALLBACK;
193
- const wireMaxOutputTokens = input.maxOutputTokens !== undefined
194
- ? Math.min(input.maxOutputTokens, modelOutputLimit)
195
- : ctx.config.forceMaxOutputTokens
196
- ? modelOutputLimit
197
- : undefined;
198
- // Effective cap for thinking-budget clamp and budget reservation.
199
- // When auto (neither override set), Gemini's model-default equals
200
- // modelOutputLimit per Google docs, so using the limit as worst-case
201
- // keeps the daily $ cap a true upper bound.
202
- const effectiveOutputCap = wireMaxOutputTokens ?? modelOutputLimit;
203
- // Gemini requires `thinkingBudget < maxOutputTokens` (the thinking pool
204
- // is carved out of the candidate-output allowance). The hard cap above
205
- // may clamp maxOutputTokens as low as the model's `outputTokenLimit`
206
- // (e.g. 8_192 on Flash-lite). Clamp the effective thinkingBudget so
207
- // the generateContent call never 400s on this invariant; reserve at
208
- // least 1_024 tokens for the actual completion.
209
- //
210
- // Only meaningful on the thinkingBudget path — when the caller uses
211
- // `thinkingLevel` instead, `thinkingBudget` is `undefined` and this
212
- // value is ignored by the thinkingConfig builder below.
213
- //
214
- // Early-reject when the caller's per-call `maxOutputTokens` can't fit
215
- // an explicit positive `thinkingBudget` + 1024-token answer reserve.
216
- // Silently clamping to 0 would make Gemini 3 Pro 400 on a
217
- // "thinking disabled" error and obscure the real mismatch (PR #22
218
- // round-3 review finding #C).
219
- if (input.thinkingBudget !== undefined &&
220
- input.thinkingBudget > 0 &&
221
- input.maxOutputTokens !== undefined &&
222
- effectiveOutputCap < input.thinkingBudget + 1024) {
223
- return errorResult(`code: thinkingBudget (${input.thinkingBudget}) + 1024-token answer reserve exceeds maxOutputTokens (${effectiveOutputCap}). Raise \`maxOutputTokens\` to at least ${input.thinkingBudget + 1024}, or lower \`thinkingBudget\`.`);
301
+ if ('rejected' in reserve) {
302
+ const spentUsd = reserve.spentMicros / 1_000_000;
303
+ return errorResult(`Daily budget cap would be exceeded: spent $${spentUsd.toFixed(4)} + estimate $${estimateUsd.toFixed(4)} > cap $${ctx.config.dailyBudgetUsd.toFixed(2)}. Retry after UTC midnight, or raise \`GEMINI_DAILY_BUDGET_USD\`.`, { errorCode: 'BUDGET_REJECT', retryable: false });
224
304
  }
225
- const effectiveThinkingBudget = thinkingBudget !== undefined && thinkingBudget > 0
226
- ? Math.max(0, Math.min(thinkingBudget, effectiveOutputCap - 1024))
227
- : 0;
228
- // Cost-estimate thinking-token reserve — tier-aware when the caller
229
- // uses `thinkingLevel`, exact-on-the-budget when they use
230
- // `thinkingBudget`. Mirror of `ask.tool.ts` logic; see
231
- // `THINKING_LEVEL_RESERVE` in `shared/thinking.ts` for the rationale
232
- // behind the per-tier numbers and why HIGH falls through to the
233
- // dynamic cap.
234
- //
235
- // Defensive clamp: tier reserves (MINIMAL=512, LOW=2048, MEDIUM=4096)
236
- // are fixed constants, but `maxOutputTokens` can be clamped down to a
237
- // resolved model's `outputTokenLimit`. A future small-cap model (say
238
- // `outputTokenLimit: 4000`) would let MEDIUM's 4096 exceed the
239
- // available headroom and over-estimate the budget reservation. Clamp
240
- // every tier's reserve against the dynamic headroom so the upper
241
- // bound stays coherent across model rollouts (PR #17 self-review F2).
242
- const thinkingHeadroom = Math.max(0, effectiveOutputCap - 1024);
243
- const thinkingTokensForEstimate = input.thinkingLevel !== undefined
244
- ? Math.min(THINKING_LEVEL_RESERVE[input.thinkingLevel] ?? thinkingHeadroom, thinkingHeadroom)
245
- : effectiveThinkingBudget;
246
- // Shared input-token fingerprint for budget + TPM throttle (see
247
- // ask.tool.ts for the rationale — same `Math.ceil(bytes/4)` tokenisation
248
- // used by `estimatePreCallCostUsd`).
249
- const workspaceBytes = scan.files.reduce((sum, f) => sum + f.size, 0);
250
- const estimatedInputTokens = Math.ceil(workspaceBytes / 4) + Math.ceil(input.task.length / 4);
251
- // Atomic budget reservation — see ask.tool.ts for the full rationale.
252
- // Estimate includes the thinking budget (billed as output tokens on Pro)
253
- // PLUS the candidate output cap.
254
- if (Number.isFinite(ctx.config.dailyBudgetUsd)) {
255
- const estimateUsd = estimatePreCallCostUsd({
256
- model: resolved.resolved,
257
- workspaceBytes,
258
- promptChars: input.task.length,
259
- expectedOutputTokens: effectiveOutputCap,
260
- thinkingTokens: thinkingTokensForEstimate,
261
- });
262
- const reserve = ctx.manifest.reserveBudget({
263
- workspaceRoot,
264
- toolName: 'code',
265
- model: resolved.resolved,
266
- estimatedCostMicros: toMicrosUsd(estimateUsd),
267
- dailyBudgetMicros: toMicrosUsd(ctx.config.dailyBudgetUsd),
268
- nowMs: Date.now(),
269
- });
270
- if ('rejected' in reserve) {
271
- const spentUsd = reserve.spentMicros / 1_000_000;
272
- return errorResult(`Daily budget cap would be exceeded: spent $${spentUsd.toFixed(4)} + estimate $${estimateUsd.toFixed(4)} > cap $${ctx.config.dailyBudgetUsd.toFixed(2)}. Retry after UTC midnight, or raise \`GEMINI_DAILY_BUDGET_USD\`.`);
273
- }
274
- reservationId = reserve.id;
305
+ reservationId = reserve.id;
306
+ }
307
+ const systemPromptHash = createHash('sha256')
308
+ .update(SYSTEM_INSTRUCTION_CODE)
309
+ .digest('hex')
310
+ .slice(0, 16);
311
+ const ctxPrep = await prepareContext({
312
+ client: ctx.client,
313
+ manifest: ctx.manifest,
314
+ scan,
315
+ model: resolved,
316
+ systemPromptHash,
317
+ systemInstruction: SYSTEM_INSTRUCTION_CODE,
318
+ ttlSeconds: ctx.config.cacheTtlSeconds,
319
+ cacheMinTokens: ctx.config.cacheMinTokens,
320
+ emitter,
321
+ // codeExecution requires `tools:[{codeExecution:{}}]` on generateContent,
322
+ // which Gemini rejects together with cachedContent. Force inline path
323
+ // when codeExecution is requested so we have actual inline file parts
324
+ // to embed alongside the prompt.
325
+ allowCaching: scan.files.length > 0 && !codeExecution,
326
+ });
327
+ // TPM throttle — placed AFTER `prepareContext`, immediately before
328
+ // `generateContent`, so the reservation's `tsMs` accurately reflects
329
+ // when tokens hit Gemini's quota counter. See ask.tool.ts for the
330
+ // full rationale and the cold-cache-timing concern it fixes.
331
+ //
332
+ // Extracted into a helper so the stale-cache retry branch can
333
+ // cancel-and-re-reserve with an accurate tsMs rather than reusing a
334
+ // stale reservation.
335
+ const reserveForDispatch = async () => {
336
+ if (ctx.config.tpmThrottleLimit <= 0)
337
+ return;
338
+ const reservation = ctx.throttle.reserve(resolved.resolved, estimatedInputTokens);
339
+ throttleReservationId = reservation.releaseId;
340
+ if (reservation.delayMs > 0) {
341
+ emitter.emit(`throttle: waiting ${Math.ceil(reservation.delayMs / 1000)}s for TPM window…`);
342
+ await new Promise((resolveSleep) => setTimeout(resolveSleep, reservation.delayMs));
275
343
  }
276
- const systemPromptHash = createHash('sha256')
277
- .update(SYSTEM_INSTRUCTION_CODE)
278
- .digest('hex')
279
- .slice(0, 16);
280
- const ctxPrep = await prepareContext({
281
- client: ctx.client,
282
- manifest: ctx.manifest,
283
- scan,
284
- model: resolved,
285
- systemPromptHash,
286
- systemInstruction: SYSTEM_INSTRUCTION_CODE,
287
- ttlSeconds: ctx.config.cacheTtlSeconds,
288
- cacheMinTokens: ctx.config.cacheMinTokens,
289
- emitter,
290
- // codeExecution requires `tools:[{codeExecution:{}}]` on generateContent,
291
- // which Gemini rejects together with cachedContent. Force inline path
292
- // when codeExecution is requested so we have actual inline file parts
293
- // to embed alongside the prompt.
294
- allowCaching: scan.files.length > 0 && !codeExecution,
295
- });
296
- // TPM throttle — placed AFTER `prepareContext`, immediately before
297
- // `generateContent`, so the reservation's `tsMs` accurately reflects
298
- // when tokens hit Gemini's quota counter. See ask.tool.ts for the
299
- // full rationale and the cold-cache-timing concern it fixes.
300
- //
301
- // Extracted into a helper so the stale-cache retry branch can
302
- // cancel-and-re-reserve with an accurate tsMs rather than reusing a
303
- // stale reservation.
304
- const reserveForDispatch = async () => {
305
- if (ctx.config.tpmThrottleLimit <= 0)
306
- return;
307
- const reservation = ctx.throttle.reserve(resolved.resolved, estimatedInputTokens);
308
- throttleReservationId = reservation.releaseId;
309
- if (reservation.delayMs > 0) {
310
- emitter.emit(`throttle: waiting ${Math.ceil(reservation.delayMs / 1000)}s for TPM window…`);
311
- await new Promise((resolveSleep) => setTimeout(resolveSleep, reservation.delayMs));
312
- }
313
- };
314
- await reserveForDispatch();
315
- const userPrompt = expectEdits
316
- ? `${input.task}\n\nRespond with your rationale and OLD/NEW diff blocks per the system instruction.`
317
- : input.task;
318
- const thinkingDescription = usingThinkingLevel
319
- ? `thinking-level=${input.thinkingLevel}`
320
- : `thinking=${effectiveThinkingBudget}`;
321
- emitter.emit(codeExecution
322
- ? `generating with ${thinkingDescription} + codeExecution…`
323
- : `generating with ${thinkingDescription}…`);
324
- // Two mutually-exclusive reasoning-control paths (enforced by schema
325
- // `.refine()`):
326
- // a) `thinkingLevel` set → cast to the SDK enum and pass through.
327
- // Google's recommended path on Gemini 3. Rejected by Gemini 2.5.
328
- // Cast rather than runtime enum-object lookup so a future SDK
329
- // rename surfaces as a Gemini 400, not a silent `undefined`.
330
- // b) `thinkingBudget` path (default 16_384, clamped above) → pass
331
- // `thinkingBudget` as before.
332
- // `includeThoughts: true` is unconditional so `thinkingSummary`
333
- // extraction below always has material to find.
334
- const thinkingConfig = usingThinkingLevel
335
- ? {
336
- thinkingLevel: input.thinkingLevel,
337
- includeThoughts: true,
338
- }
339
- : { thinkingBudget: effectiveThinkingBudget, includeThoughts: true };
340
- // Gemini rejects generateContent({cachedContent, system_instruction|tools|tool_config})
341
- // with 400. System instruction is baked into the cache at build time; tools
342
- // (codeExecution) cannot be combined with a cache built without them. When
343
- // codeExecution is requested AND a cache is active, we can't use both — in that
344
- // case we bypass the cache so the user's explicit codeExecution request wins.
345
- const canUseCache = (cacheId) => cacheId !== null && !codeExecution;
346
- if (ctxPrep.cacheId && codeExecution) {
347
- logger.warn('code({ codeExecution: true }) is incompatible with an active cache; bypassing cache for this call.');
344
+ };
345
+ await reserveForDispatch();
346
+ const userPrompt = expectEdits
347
+ ? `${input.task}\n\nRespond with your rationale and OLD/NEW diff blocks per the system instruction.`
348
+ : input.task;
349
+ const thinkingDescription = usingThinkingLevel
350
+ ? `thinking-level=${input.thinkingLevel}`
351
+ : `thinking=${effectiveThinkingBudget}`;
352
+ emitter.emit(codeExecution
353
+ ? `generating with ${thinkingDescription} + codeExecution…`
354
+ : `generating with ${thinkingDescription}…`);
355
+ // Two mutually-exclusive reasoning-control paths (enforced by schema
356
+ // `.refine()`):
357
+ // a) `thinkingLevel` set → cast to the SDK enum and pass through.
358
+ // Google's recommended path on Gemini 3. Rejected by Gemini 2.5.
359
+ // Cast rather than runtime enum-object lookup so a future SDK
360
+ // rename surfaces as a Gemini 400, not a silent `undefined`.
361
+ // b) `thinkingBudget` path (default 16_384, clamped above) → pass
362
+ // `thinkingBudget` as before.
363
+ // `includeThoughts: true` is unconditional so `thinkingSummary`
364
+ // extraction below always has material to find.
365
+ const thinkingConfig = usingThinkingLevel
366
+ ? {
367
+ thinkingLevel: input.thinkingLevel,
368
+ includeThoughts: true,
348
369
  }
349
- const buildConfig = (cacheId) => {
350
- const maxOutputField = wireMaxOutputTokens !== undefined ? { maxOutputTokens: wireMaxOutputTokens } : {};
351
- if (canUseCache(cacheId) && cacheId) {
352
- // With cache: system_instruction is baked in; thinkingConfig remains OK.
353
- // `maxOutputTokens` only emitted when the caller set it explicitly
354
- // or `GEMINI_CODE_CONTEXT_FORCE_MAX_OUTPUT=true` — otherwise omit
355
- // and Gemini uses its model-default.
356
- return {
357
- cachedContent: cacheId,
358
- thinkingConfig,
359
- ...maxOutputField,
360
- };
361
- }
362
- // Without cache (or cache bypassed for codeExecution): pass full config.
370
+ : { thinkingBudget: effectiveThinkingBudget, includeThoughts: true };
371
+ // Gemini rejects generateContent({cachedContent, system_instruction|tools|tool_config})
372
+ // with 400. System instruction is baked into the cache at build time; tools
373
+ // (codeExecution) cannot be combined with a cache built without them. When
374
+ // codeExecution is requested AND a cache is active, we can't use both — in that
375
+ // case we bypass the cache so the user's explicit codeExecution request wins.
376
+ const canUseCache = (cacheId) => cacheId !== null && !codeExecution;
377
+ if (ctxPrep.cacheId && codeExecution) {
378
+ logger.warn('code({ codeExecution: true }) is incompatible with an active cache; bypassing cache for this call.');
379
+ }
380
+ const buildConfig = (cacheId) => {
381
+ const maxOutputField = wireMaxOutputTokens !== undefined ? { maxOutputTokens: wireMaxOutputTokens } : {};
382
+ if (canUseCache(cacheId) && cacheId) {
383
+ // With cache: system_instruction is baked in; thinkingConfig remains OK.
384
+ // `maxOutputTokens` only emitted when the caller set it explicitly
385
+ // or `GEMINI_CODE_CONTEXT_FORCE_MAX_OUTPUT=true` — otherwise omit
386
+ // and Gemini uses its model-default.
363
387
  return {
364
- systemInstruction: SYSTEM_INSTRUCTION_CODE,
388
+ cachedContent: cacheId,
365
389
  thinkingConfig,
366
390
  ...maxOutputField,
367
- ...(codeExecution ? { tools: [{ codeExecution: {} }] } : {}),
368
391
  };
369
- };
370
- const buildContents = (cacheId, inline) => canUseCache(cacheId)
371
- ? userPrompt
372
- : [...inline, { role: 'user', parts: [{ text: userPrompt }] }];
373
- let activePrep = ctxPrep;
374
- let retriedOnStaleCache = false;
375
- let response;
376
- try {
377
- response = await ctx.client.models.generateContent({
378
- model: resolved.resolved,
379
- contents: buildContents(activePrep.cacheId, activePrep.inlineContents),
380
- config: buildConfig(activePrep.cacheId),
381
- });
382
- }
383
- catch (err) {
384
- if (activePrep.cacheId && isStaleCacheError(err)) {
385
- logger.warn(`Gemini rejected cached content ${activePrep.cacheId}; invalidating and retrying once.`);
386
- try {
387
- // Reset cache pointer only — preserve `files` rows so the rebuild
388
- // reuses uploaded files via content-hash dedup instead of double-
389
- // uploading. The Gemini-side cache is already dead.
390
- markCacheStale({ manifest: ctx.manifest, workspaceRoot });
391
- const rebuilt = await prepareContext({
392
- client: ctx.client,
393
- manifest: ctx.manifest,
394
- scan,
395
- model: resolved,
396
- systemPromptHash,
397
- systemInstruction: SYSTEM_INSTRUCTION_CODE,
398
- ttlSeconds: ctx.config.cacheTtlSeconds,
399
- cacheMinTokens: ctx.config.cacheMinTokens,
400
- emitter,
401
- allowCaching: scan.files.length > 0 && !codeExecution,
402
- });
403
- // Cancel stale reservation (tsMs was stamped before first
404
- // dispatch, now seconds old after rebuild) and re-reserve so
405
- // the retry's tsMs matches actual dispatch time. Mirror of
406
- // ask.tool.ts — see rationale there.
407
- if (throttleReservationId !== -1) {
408
- ctx.throttle.cancel(throttleReservationId);
409
- throttleReservationId = -1;
410
- }
411
- await reserveForDispatch();
412
- response = await ctx.client.models.generateContent({
413
- model: resolved.resolved,
414
- contents: buildContents(rebuilt.cacheId, rebuilt.inlineContents),
415
- config: buildConfig(rebuilt.cacheId),
416
- });
417
- activePrep = rebuilt;
418
- retriedOnStaleCache = true;
419
- }
420
- catch (retryErr) {
421
- throw new Error(`code retry after stale cache failed: ${retryErr instanceof Error ? retryErr.message : String(retryErr)}`, { cause: err });
422
- }
423
- }
424
- else {
425
- throw err;
426
- }
427
392
  }
428
- if (activePrep.cacheId) {
429
- ctx.ttlWatcher.markHot(workspaceRoot, activePrep.cacheId, ctx.config.cacheTtlSeconds);
430
- }
431
- const text = response.text ?? '';
432
- const edits = expectEdits ? parseEdits(text) : [];
433
- const codeBlocks = parseCodeBlocks(text);
434
- // Extract code_execution tool artifacts + Gemini's thinking summary if present.
435
- const executedCode = [];
436
- const executionOutput = [];
437
- const thoughtTexts = [];
438
- const candidates = response.candidates ?? [];
439
- for (const cand of candidates) {
440
- const parts = cand.content?.parts ?? [];
441
- for (const part of parts) {
442
- if (part.executableCode?.code)
443
- executedCode.push(part.executableCode.code);
444
- if (part.codeExecutionResult?.output)
445
- executionOutput.push(part.codeExecutionResult.output);
446
- // `thinkingConfig: { includeThoughts: true }` returns thinking as parts
447
- // flagged with `thought: true`. Cap the summary to avoid overwhelming the MCP response.
448
- if (part.thought === true && typeof part.text === 'string') {
449
- thoughtTexts.push(part.text);
450
- }
451
- }
452
- }
453
- const thinkingSummary = thoughtTexts.length > 0 ? thoughtTexts.join('\n').slice(0, 1200) : null;
454
- const usage = response.usageMetadata;
455
- const cached = typeof usage?.cachedContentTokenCount === 'number' ? usage.cachedContentTokenCount : 0;
456
- const inputTotal = typeof usage?.promptTokenCount === 'number' ? usage.promptTokenCount : 0;
457
- const uncached = Math.max(0, inputTotal - cached);
458
- const output = typeof usage?.candidatesTokenCount === 'number' ? usage.candidatesTokenCount : 0;
459
- const thinking = typeof usage?.thoughtsTokenCount === 'number' ? usage.thoughtsTokenCount : 0;
460
- // Finalise TPM throttle reservation with actual input tokens
461
- // (cached + uncached — both count toward Gemini's per-minute quota).
462
- if (throttleReservationId !== -1) {
463
- ctx.throttle.release(throttleReservationId, inputTotal);
464
- throttleReservationId = -1;
465
- }
466
- const cost = estimateCostUsd({
393
+ // Without cache (or cache bypassed for codeExecution): pass full config.
394
+ return {
395
+ systemInstruction: SYSTEM_INSTRUCTION_CODE,
396
+ thinkingConfig,
397
+ ...maxOutputField,
398
+ ...(codeExecution ? { tools: [{ codeExecution: {} }] } : {}),
399
+ };
400
+ };
401
+ const buildContents = (cacheId, inline) => canUseCache(cacheId)
402
+ ? userPrompt
403
+ : [...inline, { role: 'user', parts: [{ text: userPrompt }] }];
404
+ let activePrep = ctxPrep;
405
+ let retriedOnStaleCache = false;
406
+ let response;
407
+ try {
408
+ response = await ctx.client.models.generateContent({
467
409
  model: resolved.resolved,
468
- uncachedInputTokens: uncached,
469
- cachedInputTokens: cached,
470
- outputTokens: output,
471
- thinkingTokens: thinking,
410
+ contents: buildContents(activePrep.cacheId, activePrep.inlineContents),
411
+ config: buildConfig(activePrep.cacheId),
472
412
  });
473
- const costMicros = toMicrosUsd(cost);
474
- const durationMs = Date.now() - started;
475
- if (reservationId !== null) {
476
- // Defensive: if `finalize` throws (disk full / lock contention), keep
477
- // the reservation row so we don't lose all record of this billable
478
- // call. Drop `reservationId` either way so the outer catch doesn't
479
- // touch it.
413
+ }
414
+ catch (err) {
415
+ if (activePrep.cacheId && isStaleCacheError(err)) {
416
+ logger.warn(`Gemini rejected cached content ${activePrep.cacheId}; invalidating and retrying once.`);
480
417
  try {
481
- ctx.manifest.finalizeBudgetReservation(reservationId, {
482
- cachedTokens: cached,
483
- uncachedTokens: uncached,
484
- costUsdMicro: costMicros,
485
- durationMs,
418
+ // Reset cache pointer only — preserve `files` rows so the rebuild
419
+ // reuses uploaded files via content-hash dedup instead of double-
420
+ // uploading. The Gemini-side cache is already dead.
421
+ markCacheStale({ manifest: ctx.manifest, workspaceRoot });
422
+ const rebuilt = await prepareContext({
423
+ client: ctx.client,
424
+ manifest: ctx.manifest,
425
+ scan,
426
+ model: resolved,
427
+ systemPromptHash,
428
+ systemInstruction: SYSTEM_INSTRUCTION_CODE,
429
+ ttlSeconds: ctx.config.cacheTtlSeconds,
430
+ cacheMinTokens: ctx.config.cacheMinTokens,
431
+ emitter,
432
+ allowCaching: scan.files.length > 0 && !codeExecution,
433
+ });
434
+ // Cancel stale reservation (tsMs was stamped before first
435
+ // dispatch, now seconds old after rebuild) and re-reserve so
436
+ // the retry's tsMs matches actual dispatch time. Mirror of
437
+ // ask.tool.ts — see rationale there.
438
+ if (throttleReservationId !== -1) {
439
+ ctx.throttle.cancel(throttleReservationId);
440
+ throttleReservationId = -1;
441
+ }
442
+ await reserveForDispatch();
443
+ response = await ctx.client.models.generateContent({
444
+ model: resolved.resolved,
445
+ contents: buildContents(rebuilt.cacheId, rebuilt.inlineContents),
446
+ config: buildConfig(rebuilt.cacheId),
486
447
  });
448
+ activePrep = rebuilt;
449
+ retriedOnStaleCache = true;
487
450
  }
488
- catch (finalizeErr) {
489
- logger.error(`code: finalizeBudgetReservation failed for id=${reservationId}; reservation row keeps the estimate (${costMicros} micros). Error: ${String(finalizeErr)}`);
451
+ catch (retryErr) {
452
+ throw new Error(`code retry after stale cache failed: ${retryErr instanceof Error ? retryErr.message : String(retryErr)}`, { cause: err });
490
453
  }
491
- reservationId = null;
492
454
  }
493
455
  else {
494
- ctx.manifest.insertUsageMetric({
495
- workspaceRoot,
496
- toolName: 'code',
497
- model: resolved.resolved,
456
+ throw err;
457
+ }
458
+ }
459
+ if (activePrep.cacheId) {
460
+ ctx.ttlWatcher.markHot(workspaceRoot, activePrep.cacheId, ctx.config.cacheTtlSeconds);
461
+ }
462
+ const text = response.text ?? '';
463
+ const edits = expectEdits ? parseEdits(text) : [];
464
+ const codeBlocks = parseCodeBlocks(text);
465
+ // Extract code_execution tool artifacts + Gemini's thinking summary if present.
466
+ const executedCode = [];
467
+ const executionOutput = [];
468
+ const thoughtTexts = [];
469
+ const candidates = response.candidates ?? [];
470
+ for (const cand of candidates) {
471
+ const parts = cand.content?.parts ?? [];
472
+ for (const part of parts) {
473
+ if (part.executableCode?.code)
474
+ executedCode.push(part.executableCode.code);
475
+ if (part.codeExecutionResult?.output)
476
+ executionOutput.push(part.codeExecutionResult.output);
477
+ // `thinkingConfig: { includeThoughts: true }` returns thinking as parts
478
+ // flagged with `thought: true`. Cap the summary to avoid overwhelming the MCP response.
479
+ if (part.thought === true && typeof part.text === 'string') {
480
+ thoughtTexts.push(part.text);
481
+ }
482
+ }
483
+ }
484
+ const thinkingSummary = thoughtTexts.length > 0 ? thoughtTexts.join('\n').slice(0, 1200) : null;
485
+ const usage = response.usageMetadata;
486
+ const cached = typeof usage?.cachedContentTokenCount === 'number' ? usage.cachedContentTokenCount : 0;
487
+ const inputTotal = typeof usage?.promptTokenCount === 'number' ? usage.promptTokenCount : 0;
488
+ const uncached = Math.max(0, inputTotal - cached);
489
+ const output = typeof usage?.candidatesTokenCount === 'number' ? usage.candidatesTokenCount : 0;
490
+ const thinking = typeof usage?.thoughtsTokenCount === 'number' ? usage.thoughtsTokenCount : 0;
491
+ // Finalise TPM throttle reservation with actual input tokens
492
+ // (cached + uncached — both count toward Gemini's per-minute quota).
493
+ if (throttleReservationId !== -1) {
494
+ ctx.throttle.release(throttleReservationId, inputTotal);
495
+ throttleReservationId = -1;
496
+ }
497
+ const cost = estimateCostUsd({
498
+ model: resolved.resolved,
499
+ uncachedInputTokens: uncached,
500
+ cachedInputTokens: cached,
501
+ outputTokens: output,
502
+ thinkingTokens: thinking,
503
+ });
504
+ const costMicros = toMicrosUsd(cost);
505
+ const durationMs = Date.now() - started;
506
+ if (reservationId !== null) {
507
+ // Defensive: if `finalize` throws (disk full / lock contention), keep
508
+ // the reservation row so we don't lose all record of this billable
509
+ // call. Drop `reservationId` either way so the outer catch doesn't
510
+ // touch it.
511
+ try {
512
+ ctx.manifest.finalizeBudgetReservation(reservationId, {
498
513
  cachedTokens: cached,
499
514
  uncachedTokens: uncached,
500
515
  costUsdMicro: costMicros,
501
516
  durationMs,
502
- occurredAt: Date.now(),
503
517
  });
504
518
  }
505
- if (scan.truncated) {
506
- logger.warn(`workspace ${workspaceRoot} contains more files than GEMINI_CODE_CONTEXT_MAX_FILES (${ctx.config.maxFilesPerWorkspace}); the tail was dropped before indexing.`);
519
+ catch (finalizeErr) {
520
+ logger.error(`code: finalizeBudgetReservation failed for id=${reservationId}; reservation row keeps the estimate (${costMicros} micros). Error: ${String(finalizeErr)}`);
507
521
  }
508
- const structured = {
509
- resolvedModel: resolved.resolved,
510
- requestedModel: resolved.requested,
511
- modelCategory: resolved.category,
512
- modelCostTier: resolved.capabilities.costTier,
513
- contextWindow: resolved.inputTokenLimit,
514
- // `thinkingBudget` echoes the clamped value actually sent on the
515
- // wire; it's only meaningful on the budget path. On the
516
- // `thinkingLevel` path nothing is sent for `thinkingBudget`, so we
517
- // emit `null` (not `0`) — `0` is the wire sentinel for "thinking
518
- // disabled" and reporting it here for a level-path call would lie
519
- // to downstream audit / dashboard consumers (PR #17 self-review F1,
520
- // 3-reviewer consensus: GPT + Copilot + Self). Matches `ask.tool.ts`.
521
- thinkingBudget: usingThinkingLevel ? null : effectiveThinkingBudget,
522
- thinkingLevel: input.thinkingLevel ?? null,
523
- codeExecutionUsed: codeExecution,
524
- cacheHit: activePrep.reused,
525
- cacheRebuilt: activePrep.rebuilt || retriedOnStaleCache,
526
- retriedOnStaleCache,
522
+ reservationId = null;
523
+ }
524
+ else {
525
+ ctx.manifest.insertUsageMetric({
526
+ workspaceRoot,
527
+ toolName: 'code',
528
+ model: resolved.resolved,
527
529
  cachedTokens: cached,
528
530
  uncachedTokens: uncached,
529
- thinkingTokens: thinking,
530
- outputTokens: output,
531
- costEstimateUsd: Math.round(cost * 10000) / 10000,
531
+ costUsdMicro: costMicros,
532
532
  durationMs,
533
- edits: edits.map((e) => ({
534
- file: e.file,
535
- oldPreview: e.old.slice(0, 120),
536
- newPreview: e.new.slice(0, 120),
537
- })),
538
- editCount: edits.length,
539
- codeBlocks: codeBlocks.map((b) => ({ lang: b.lang, length: b.content.length })),
540
- workspaceTruncated: scan.truncated,
541
- maxFilesCap: ctx.config.maxFilesPerWorkspace,
542
- filesIndexed: scan.files.length,
543
- filesUploadFailed: activePrep.uploaded.failedCount,
544
- ...(activePrep.uploaded.failedCount > 0
545
- ? { uploadFailures: activePrep.uploaded.failures.slice(0, 5) }
546
- : {}),
547
- inlineOnly: activePrep.inlineOnly,
548
- ...(thinkingSummary ? { thinkingSummary } : {}),
549
- ...(executedCode.length > 0 ? { executedCode } : {}),
550
- ...(executionOutput.length > 0 ? { executionOutput } : {}),
551
- };
552
- return textResult(text, structured);
533
+ occurredAt: Date.now(),
534
+ });
553
535
  }
554
- catch (err) {
555
- logger.error(`code failed: ${String(err)}`);
556
- // T22a + v1.3.2 — seed the throttle's retry-hint from Gemini 429
557
- // bodies, gated on `isGemini429` (ApiError instance + status===429)
558
- // to prevent hint-poisoning. Mirror of ask.tool.ts — see there for
559
- // full rationale.
560
- const retryDelayMs = isGemini429(err) ? parseRetryDelayMs(err.message) : null;
561
- if (retryDelayMs !== null && resolvedModelKey !== null) {
562
- ctx.throttle.recordRetryHint(resolvedModelKey, retryDelayMs);
563
- }
564
- if (reservationId !== null) {
565
- try {
566
- ctx.manifest.cancelBudgetReservation(reservationId);
567
- }
568
- catch (cancelErr) {
569
- logger.error(`code: cancelBudgetReservation failed for id=${reservationId}; reservation row keeps the estimate. Error: ${String(cancelErr)}`);
570
- }
571
- reservationId = null;
536
+ if (scan.truncated) {
537
+ logger.warn(`workspace ${workspaceRoot} contains more files than GEMINI_CODE_CONTEXT_MAX_FILES (${ctx.config.maxFilesPerWorkspace}); the tail was dropped before indexing.`);
538
+ }
539
+ const structured = {
540
+ resolvedModel: resolved.resolved,
541
+ requestedModel: resolved.requested,
542
+ modelCategory: resolved.category,
543
+ modelCostTier: resolved.capabilities.costTier,
544
+ contextWindow: resolved.inputTokenLimit,
545
+ // `thinkingBudget` echoes the clamped value actually sent on the
546
+ // wire; it's only meaningful on the budget path. On the
547
+ // `thinkingLevel` path nothing is sent for `thinkingBudget`, so we
548
+ // emit `null` (not `0`) — `0` is the wire sentinel for "thinking
549
+ // disabled" and reporting it here for a level-path call would lie
550
+ // to downstream audit / dashboard consumers (PR #17 self-review F1,
551
+ // 3-reviewer consensus: GPT + Copilot + Self). Matches `ask.tool.ts`.
552
+ thinkingBudget: usingThinkingLevel ? null : effectiveThinkingBudget,
553
+ thinkingLevel: input.thinkingLevel ?? null,
554
+ codeExecutionUsed: codeExecution,
555
+ cacheHit: activePrep.reused,
556
+ cacheRebuilt: activePrep.rebuilt || retriedOnStaleCache,
557
+ retriedOnStaleCache,
558
+ cachedTokens: cached,
559
+ uncachedTokens: uncached,
560
+ thinkingTokens: thinking,
561
+ outputTokens: output,
562
+ costEstimateUsd: Math.round(cost * 10000) / 10000,
563
+ durationMs,
564
+ edits: edits.map((e) => ({
565
+ file: e.file,
566
+ oldPreview: e.old.slice(0, 120),
567
+ newPreview: e.new.slice(0, 120),
568
+ })),
569
+ editCount: edits.length,
570
+ codeBlocks: codeBlocks.map((b) => ({ lang: b.lang, length: b.content.length })),
571
+ workspaceTruncated: scan.truncated,
572
+ maxFilesCap: ctx.config.maxFilesPerWorkspace,
573
+ filesIndexed: scan.files.length,
574
+ filesUploadFailed: activePrep.uploaded.failedCount,
575
+ ...(activePrep.uploaded.failedCount > 0
576
+ ? { uploadFailures: activePrep.uploaded.failures.slice(0, 5) }
577
+ : {}),
578
+ inlineOnly: activePrep.inlineOnly,
579
+ ...(thinkingSummary ? { thinkingSummary } : {}),
580
+ ...(executedCode.length > 0 ? { executedCode } : {}),
581
+ ...(executionOutput.length > 0 ? { executionOutput } : {}),
582
+ };
583
+ return textResult(text, structured);
584
+ }
585
+ catch (err) {
586
+ logger.error(`code failed: ${String(err)}`);
587
+ // T22a + v1.3.2 — seed the throttle's retry-hint from Gemini 429
588
+ // bodies, gated on `isGemini429` (ApiError instance + status===429)
589
+ // to prevent hint-poisoning. Mirror of ask.tool.ts — see there for
590
+ // full rationale.
591
+ const retryDelayMs = isGemini429(err) ? parseRetryDelayMs(err.message) : null;
592
+ if (retryDelayMs !== null && resolvedModelKey !== null) {
593
+ ctx.throttle.recordRetryHint(resolvedModelKey, retryDelayMs);
594
+ }
595
+ if (reservationId !== null) {
596
+ try {
597
+ ctx.manifest.cancelBudgetReservation(reservationId);
572
598
  }
573
- // Drop TPM reservation on failure — mirror of ask.tool.ts rationale.
574
- if (throttleReservationId !== -1) {
575
- ctx.throttle.cancel(throttleReservationId);
576
- throttleReservationId = -1;
599
+ catch (cancelErr) {
600
+ logger.error(`code: cancelBudgetReservation failed for id=${reservationId}; reservation row keeps the estimate. Error: ${String(cancelErr)}`);
577
601
  }
578
- return errorResult(`code failed: ${err instanceof Error ? err.message : String(err)}`);
602
+ reservationId = null;
579
603
  }
580
- finally {
581
- emitter.stop();
604
+ // Drop TPM reservation on failure — mirror of ask.tool.ts rationale.
605
+ if (throttleReservationId !== -1) {
606
+ ctx.throttle.cancel(throttleReservationId);
607
+ throttleReservationId = -1;
582
608
  }
583
- },
584
- };
609
+ const httpStatus = err.status;
610
+ return errorResult(`code failed: ${err instanceof Error ? err.message : String(err)}`, {
611
+ errorCode: 'UNKNOWN',
612
+ ...(httpStatus !== undefined ? { httpStatus } : {}),
613
+ });
614
+ }
615
+ finally {
616
+ emitter.stop();
617
+ }
618
+ }
585
619
  //# sourceMappingURL=code.tool.js.map