@qmediat.io/gemini-code-context-mcp 1.24.0 → 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 +90 -1
- package/README.md +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 +142 -1615
- 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 +29 -178
- package/dist/tools/ask.tool.js.map +1 -1
- package/dist/tools/code.tool.js +9 -20
- package/dist/tools/code.tool.js.map +1 -1
- package/dist/tools/shared/abort-timeout.d.ts +43 -5
- package/dist/tools/shared/abort-timeout.d.ts.map +1 -1
- package/dist/tools/shared/abort-timeout.js +111 -102
- package/dist/tools/shared/abort-timeout.js.map +1 -1
- package/dist/tools/shared/call-ledger.d.ts +27 -12
- package/dist/tools/shared/call-ledger.d.ts.map +1 -1
- package/dist/tools/shared/call-ledger.js +57 -39
- package/dist/tools/shared/call-ledger.js.map +1 -1
- package/dist/tools/shared/continuation.d.ts +1 -1
- package/dist/tools/shared/continuation.d.ts.map +1 -1
- 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 +11 -6
- package/dist/tools/shared/settle-call.d.ts.map +1 -1
- package/dist/tools/shared/settle-call.js +14 -10
- package/dist/tools/shared/settle-call.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/package.json +1 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `ask_agentic` reads from a model response, the same way for a loop iteration and for the forced-finalization
|
|
3
|
+
* pass: the token counts (0 where the response reported none) and the thought summary.
|
|
4
|
+
*/
|
|
5
|
+
/** The longest thought summary a result carries. */
|
|
6
|
+
const THINKING_SUMMARY_MAX_CHARS = 1200;
|
|
7
|
+
const countOf = (value) => (typeof value === 'number' ? value : 0);
|
|
8
|
+
/** A response's token counts, each 0 when its `usageMetadata` does not carry it as a number. */
|
|
9
|
+
export function usageCounts(usage) {
|
|
10
|
+
return {
|
|
11
|
+
promptTokenCount: countOf(usage?.promptTokenCount),
|
|
12
|
+
candidatesTokenCount: countOf(usage?.candidatesTokenCount),
|
|
13
|
+
thoughtsTokenCount: countOf(usage?.thoughtsTokenCount),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** The thought parts' text joined by newlines and cut to 1200 characters, or `null` when there is none. */
|
|
17
|
+
export function thinkingSummaryOf(parts) {
|
|
18
|
+
const thoughtTexts = parts
|
|
19
|
+
.filter((p) => p.thought === true && typeof p.text === 'string')
|
|
20
|
+
.map((p) => p.text);
|
|
21
|
+
return thoughtTexts.length > 0
|
|
22
|
+
? thoughtTexts.join('\n').slice(0, THINKING_SUMMARY_MAX_CHARS)
|
|
23
|
+
: null;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../../../src/tools/agentic/response.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,oDAAoD;AACpD,MAAM,0BAA0B,GAAG,IAAI,CAAC;AAExC,MAAM,OAAO,GAAG,CAAC,KAAyB,EAAU,EAAE,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAE/F,gGAAgG;AAChG,MAAM,UAAU,WAAW,CAAC,KAA+C;IACzE,OAAO;QACL,gBAAgB,EAAE,OAAO,CAAC,KAAK,EAAE,gBAAgB,CAAC;QAClD,oBAAoB,EAAE,OAAO,CAAC,KAAK,EAAE,oBAAoB,CAAC;QAC1D,kBAAkB,EAAE,OAAO,CAAC,KAAK,EAAE,kBAAkB,CAAC;KACvD,CAAC;AACJ,CAAC;AAED,2GAA2G;AAC3G,MAAM,UAAU,iBAAiB,CAAC,KAAa;IAC7C,MAAM,YAAY,GAAG,KAAK;SACvB,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,KAAK,IAAI,IAAI,OAAO,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC;SAC/D,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAc,CAAC,CAAC;IAChC,OAAO,YAAY,CAAC,MAAM,GAAG,CAAC;QAC5B,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,0BAA0B,CAAC;QAC9D,CAAC,CAAC,IAAI,CAAC;AACX,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The results `ask_agentic` returns once the loop has started: the answer (organic or forced), and every error that
|
|
3
|
+
* ends the loop. `apiCalls` is the authoritative total `generateContent` count on every one of them.
|
|
4
|
+
*/
|
|
5
|
+
import { type TextToolResult } from '../registry.js';
|
|
6
|
+
import type { BilledFields } from '../shared/call-ledger.js';
|
|
7
|
+
import type { NoProgress } from './no-progress.js';
|
|
8
|
+
import type { PassTimeout } from './pass-clock.js';
|
|
9
|
+
import type { RescueOutcome } from './rescue.js';
|
|
10
|
+
import type { AgenticRun, KnobNames, LoopState } from './run.js';
|
|
11
|
+
/** Organic final-text path: no forced-finalization pass ran, so total `generateContent` calls equals loop iterations. */
|
|
12
|
+
export declare function organicAnswer(run: AgenticRun, state: LoopState, finalText: string, thinkingSummary: string | null): TextToolResult;
|
|
13
|
+
/**
|
|
14
|
+
* The daily budget refused an iteration's reservation. BUDGET_REJECT is pre-dispatch — `iterations`
|
|
15
|
+
* was incremented at the top of the loop body, but this iteration's `generateContent` never fired.
|
|
16
|
+
* Subtract 1 to match the schema contract that `apiCalls` is the AUTHORITATIVE total
|
|
17
|
+
* generateContent count. `Math.max(0, ...)` guards the first-iteration-rejection edge (iter 1 +
|
|
18
|
+
* reject → 0 calls).
|
|
19
|
+
*/
|
|
20
|
+
export declare function budgetRejected(run: AgenticRun, state: LoopState, spentMicros: number, estimateUsd: number): TextToolResult;
|
|
21
|
+
/** How an iteration timed out: what fired, the knobs, and how far the pass had got. */
|
|
22
|
+
export interface IterationTimeout {
|
|
23
|
+
timeout: PassTimeout;
|
|
24
|
+
/** The configured knobs (`null` when unset), reported whichever fired. */
|
|
25
|
+
timeoutMs: number | null;
|
|
26
|
+
stallMs: number | null;
|
|
27
|
+
/** The knob names the message gives: the caller's. */
|
|
28
|
+
knobNames: KnobNames;
|
|
29
|
+
dispatched: boolean;
|
|
30
|
+
/** The iteration's billed fields when its model call had completed and it was booked; `null` when it was not. */
|
|
31
|
+
billed: BilledFields | null;
|
|
32
|
+
/** The work of its tools phase still running after the grace; 0 outside the tools phase. */
|
|
33
|
+
toolCallsRunning: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* What a timeout message tells the caller to change: the knob that fired, the default of the phase and its knob, or —
|
|
37
|
+
* for an `external` timeout — that none of this call's bounds fired.
|
|
38
|
+
*/
|
|
39
|
+
export declare function timeoutHint(timeout: PassTimeout, names: KnobNames): string;
|
|
40
|
+
/**
|
|
41
|
+
* The whole agentic call fails on an iteration timeout — the failed iteration's function-call results never came back,
|
|
42
|
+
* so the conversation is structurally incomplete.
|
|
43
|
+
*
|
|
44
|
+
* `timeoutMs` / `stallMs` report the configured knobs (as `ask` / `code` do); `timeoutKind` says which kind fired
|
|
45
|
+
* (`stall` for a stream's gap only), `timeoutPhase` where, `timeoutSource` which bound. `dispatched` false means the
|
|
46
|
+
* bound fired in the TPM wait and this iteration never reached the API: `apiCalls` leaves it out. An iteration booked
|
|
47
|
+
* before it timed out (the bound fired in its tools phase) reports its billed fields and its message says so; the abort
|
|
48
|
+
* note is for an iteration whose call was cancelled (`billed: null`). A TIMEOUT is retryable unless tool calls of the
|
|
49
|
+
* iteration still run (`toolCallsRunning`): a retry would stack file-system work on the same thread pool.
|
|
50
|
+
*/
|
|
51
|
+
export declare function iterationTimedOut(iterations: number, t: IterationTimeout): TextToolResult;
|
|
52
|
+
/** The budget guard fires AFTER the iteration's generateContent completed — the call IS counted in apiCalls. */
|
|
53
|
+
export declare function inputBudgetExceeded(run: AgenticRun, state: LoopState): TextToolResult;
|
|
54
|
+
/**
|
|
55
|
+
* R2 message enrichment (v1.16.1 — /6step PARTIAL TP): some MCP consumers (notably Claude Code's
|
|
56
|
+
* tool-result rendering) collapse isError responses to the message string only and don't surface
|
|
57
|
+
* `structuredContent` — wire format includes both per CallToolResultSchema (independent optional
|
|
58
|
+
* fields) but rendering is consumer-side. Operator-actionable fields (iterations, filesRead) are
|
|
59
|
+
* mirrored into the message string so they're visible regardless of consumer rendering. Dedupe fires
|
|
60
|
+
* AFTER the iteration's generateContent completed — the call IS counted in apiCalls.
|
|
61
|
+
*/
|
|
62
|
+
export declare function noProgressDetected(state: LoopState, stuck: NoProgress): TextToolResult;
|
|
63
|
+
/**
|
|
64
|
+
* The end of a loop that exhausted `maxIterations`: the forced answer when the finalization pass
|
|
65
|
+
* produced text, else `AGENTIC_MAX_ITERATIONS`.
|
|
66
|
+
*
|
|
67
|
+
* Token totals are after the finalization pass. When the pass succeeded at the API level its tokens
|
|
68
|
+
* ARE billed even if its text came back empty, so both results report the post-pass totals.
|
|
69
|
+
* `apiCalls` counts the pass from its explicit `attempted` flag (set IMMEDIATELY BEFORE the dispatch)
|
|
70
|
+
* rather than from its usage — timeouts and pre-response network failures leave usageMetadata empty
|
|
71
|
+
* even though the request was sent. Skipped paths
|
|
72
|
+
* (daily-budget reject) leave `attempted` false so they report `apiCalls = iterations`. Operators can
|
|
73
|
+
* branch on `apiCalls > iterations` to detect the forced pass without parsing `convergenceForced`.
|
|
74
|
+
*/
|
|
75
|
+
export declare function maxIterationsResult(run: AgenticRun, state: LoopState, rescue: RescueOutcome): TextToolResult;
|
|
76
|
+
//# sourceMappingURL=results.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"results.d.ts","sourceRoot":"","sources":["../../../src/tools/agentic/results.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,KAAK,cAAc,EAA2B,MAAM,gBAAgB,CAAC;AAE9E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0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E,SAAS,GAAG,cAAc,CAcrF;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,UAAU,GAAG,cAAc,CAiBtF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,UAAU,EACf,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE,aAAa,GACpB,cAAc,CAYhB"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The results `ask_agentic` returns once the loop has started: the answer (organic or forced), and every error that
|
|
3
|
+
* ends the loop. `apiCalls` is the authoritative total `generateContent` count on every one of them.
|
|
4
|
+
*/
|
|
5
|
+
import { errorResult, textResult } from '../registry.js';
|
|
6
|
+
import { ABORTED_REQUEST_NOTE } from '../shared/abort-timeout.js';
|
|
7
|
+
/** An answer's structured fields; `overBudget` says whether its input tokens passed `maxTotalInputTokens`. */
|
|
8
|
+
function answerFields(run, state, totals, source) {
|
|
9
|
+
const { resolved } = run;
|
|
10
|
+
return {
|
|
11
|
+
resolvedModel: resolved.resolved,
|
|
12
|
+
requestedModel: resolved.requested,
|
|
13
|
+
fallbackApplied: resolved.fallbackApplied,
|
|
14
|
+
modelCategory: resolved.category,
|
|
15
|
+
contextWindow: resolved.inputTokenLimit,
|
|
16
|
+
iterations: state.iterations,
|
|
17
|
+
apiCalls: totals.apiCalls,
|
|
18
|
+
cumulativeInputTokens: totals.inputTokens,
|
|
19
|
+
cumulativeOutputTokens: totals.outputTokens,
|
|
20
|
+
cumulativeThinkingTokens: totals.thinkingTokens,
|
|
21
|
+
filesRead: state.filesReadSet.size,
|
|
22
|
+
filesReadList: [...state.filesReadSet].slice(0, 40),
|
|
23
|
+
durationMs: Date.now() - run.started,
|
|
24
|
+
thinkingSummary: totals.thinkingSummary,
|
|
25
|
+
...(source === 'forced' ? { convergenceForced: true } : {}),
|
|
26
|
+
overBudget: totals.inputTokens > run.limits.maxTotalInputTokens,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Organic final-text path: no forced-finalization pass ran, so total `generateContent` calls equals loop iterations. */
|
|
30
|
+
export function organicAnswer(run, state, finalText, thinkingSummary) {
|
|
31
|
+
return textResult(finalText, answerFields(run, state, {
|
|
32
|
+
apiCalls: state.iterations,
|
|
33
|
+
inputTokens: state.cumulativeInputTokens,
|
|
34
|
+
outputTokens: state.cumulativeOutputTokens,
|
|
35
|
+
thinkingTokens: state.cumulativeThinkingTokens,
|
|
36
|
+
thinkingSummary,
|
|
37
|
+
}, 'organic'));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The daily budget refused an iteration's reservation. BUDGET_REJECT is pre-dispatch — `iterations`
|
|
41
|
+
* was incremented at the top of the loop body, but this iteration's `generateContent` never fired.
|
|
42
|
+
* Subtract 1 to match the schema contract that `apiCalls` is the AUTHORITATIVE total
|
|
43
|
+
* generateContent count. `Math.max(0, ...)` guards the first-iteration-rejection edge (iter 1 +
|
|
44
|
+
* reject → 0 calls).
|
|
45
|
+
*/
|
|
46
|
+
export function budgetRejected(run, state, spentMicros, estimateUsd) {
|
|
47
|
+
const spentUsd = spentMicros / 1_000_000;
|
|
48
|
+
const { dailyBudgetUsd } = run.ctx.config;
|
|
49
|
+
return errorResult(`Daily budget cap would be exceeded: spent $${spentUsd.toFixed(4)} + estimate $${estimateUsd.toFixed(4)} > cap $${dailyBudgetUsd.toFixed(2)}. Retry after UTC midnight, or raise GEMINI_DAILY_BUDGET_USD.`, {
|
|
50
|
+
errorCode: 'BUDGET_REJECT',
|
|
51
|
+
retryable: false,
|
|
52
|
+
iterations: state.iterations,
|
|
53
|
+
apiCalls: Math.max(0, state.iterations - 1),
|
|
54
|
+
cumulativeInputTokens: state.cumulativeInputTokens,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/** What the message of an iteration booked before it timed out says of its cost. */
|
|
58
|
+
const BOOKED_ITERATION_NOTE = "The iteration's model call had completed: it is booked at the token and cost fields this result reports.";
|
|
59
|
+
/** The default a bound reports when no knob bounded its phase, by phase (`phase-defaults.ts`); the TPM wait has none. */
|
|
60
|
+
const DEFAULT_HINTS = {
|
|
61
|
+
opening: (n, ms) => `No chunk arrived within the ${ms} ms default for a request's opening. Set \`${n.totalMs}\` per call or \`${n.totalEnv}\` to bound each iteration yourself.`,
|
|
62
|
+
stream: (n, ms) => `The stream sent nothing for the ${ms} ms default. Set \`${n.stallMs}\` per call or \`${n.stallEnv}\` to bound a stream's silence yourself.`,
|
|
63
|
+
tools: (n, ms) => `The iteration's tool calls made no progress for the ${ms} ms default. Set \`${n.totalMs}\` per call or \`${n.totalEnv}\` to bound each iteration yourself.`,
|
|
64
|
+
finalization: (n, ms) => `A request of the forced-finalization pass ran past the ${ms} ms default. Set \`${n.totalMs}\` per call or \`${n.totalEnv}\` to bound it yourself.`,
|
|
65
|
+
};
|
|
66
|
+
/** The knob that raises a bound a caller set. */
|
|
67
|
+
const KNOB_HINTS = {
|
|
68
|
+
iterationTimeoutMs: (n) => `Increase \`${n.totalMs}\` per call or set \`${n.totalEnv}\` higher.`,
|
|
69
|
+
stallMs: (n) => `Increase \`${n.stallMs}\` per call or set \`${n.stallEnv}\` higher. Stall fires when the stream goes silent — usually a dead socket or a Gemini server-side hiccup.`,
|
|
70
|
+
};
|
|
71
|
+
/** An `external` timeout names no bound of this call: it quotes the error's own message. */
|
|
72
|
+
const externalHint = (timeout) => `No bound of this call fired: the request ended with a timeout of its own ("${timeout.message.slice(0, 200)}").`;
|
|
73
|
+
/**
|
|
74
|
+
* What a timeout message tells the caller to change: the knob that fired, the default of the phase and its knob, or —
|
|
75
|
+
* for an `external` timeout — that none of this call's bounds fired.
|
|
76
|
+
*/
|
|
77
|
+
export function timeoutHint(timeout, names) {
|
|
78
|
+
if (timeout.source === 'external')
|
|
79
|
+
return externalHint(timeout);
|
|
80
|
+
const hint = timeout.source === 'default' ? DEFAULT_HINTS[timeout.phase] : KNOB_HINTS[timeout.source];
|
|
81
|
+
return hint(names, timeout.limitMs);
|
|
82
|
+
}
|
|
83
|
+
/** "timed out (kind) after N ms", the limit left out for an `external` timeout: no bound of this call set one. */
|
|
84
|
+
function timedOutAfter(timeout) {
|
|
85
|
+
const after = timeout.limitMs === null ? '' : ` after ${timeout.limitMs}ms`;
|
|
86
|
+
return `timed out (${timeout.kind})${after}`;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The whole agentic call fails on an iteration timeout — the failed iteration's function-call results never came back,
|
|
90
|
+
* so the conversation is structurally incomplete.
|
|
91
|
+
*
|
|
92
|
+
* `timeoutMs` / `stallMs` report the configured knobs (as `ask` / `code` do); `timeoutKind` says which kind fired
|
|
93
|
+
* (`stall` for a stream's gap only), `timeoutPhase` where, `timeoutSource` which bound. `dispatched` false means the
|
|
94
|
+
* bound fired in the TPM wait and this iteration never reached the API: `apiCalls` leaves it out. An iteration booked
|
|
95
|
+
* before it timed out (the bound fired in its tools phase) reports its billed fields and its message says so; the abort
|
|
96
|
+
* note is for an iteration whose call was cancelled (`billed: null`). A TIMEOUT is retryable unless tool calls of the
|
|
97
|
+
* iteration still run (`toolCallsRunning`): a retry would stack file-system work on the same thread pool.
|
|
98
|
+
*/
|
|
99
|
+
export function iterationTimedOut(iterations, t) {
|
|
100
|
+
const { timeout } = t;
|
|
101
|
+
const costNote = t.billed === null ? ABORTED_REQUEST_NOTE : BOOKED_ITERATION_NOTE;
|
|
102
|
+
return errorResult(`ask_agentic: iteration ${iterations} ${timedOutAfter(timeout)}. ${timeoutHint(timeout, t.knobNames)} ${costNote}`, {
|
|
103
|
+
errorCode: 'TIMEOUT',
|
|
104
|
+
timeoutMs: t.timeoutMs,
|
|
105
|
+
stallMs: t.stallMs,
|
|
106
|
+
timeoutKind: timeout.kind,
|
|
107
|
+
timeoutPhase: timeout.phase,
|
|
108
|
+
timeoutSource: timeout.source,
|
|
109
|
+
iteration: iterations,
|
|
110
|
+
iterations,
|
|
111
|
+
apiCalls: t.dispatched ? iterations : Math.max(0, iterations - 1),
|
|
112
|
+
retryable: t.toolCallsRunning === 0,
|
|
113
|
+
...(t.toolCallsRunning > 0 ? { toolCallsRunning: t.toolCallsRunning } : {}),
|
|
114
|
+
...(t.billed ?? {}),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/** The budget guard fires AFTER the iteration's generateContent completed — the call IS counted in apiCalls. */
|
|
118
|
+
export function inputBudgetExceeded(run, state) {
|
|
119
|
+
const { iterations, cumulativeInputTokens } = state;
|
|
120
|
+
const { maxTotalInputTokens } = run.limits;
|
|
121
|
+
return errorResult(`ask_agentic: cumulative input tokens (${cumulativeInputTokens.toLocaleString()}) exceeded budget (${maxTotalInputTokens.toLocaleString()}) after ${iterations} iterations. Increase maxTotalInputTokens, narrow your prompt, or lower thinkingLevel.`, {
|
|
122
|
+
errorCode: 'UNKNOWN',
|
|
123
|
+
retryable: false,
|
|
124
|
+
subReason: 'AGENTIC_INPUT_BUDGET_EXCEEDED',
|
|
125
|
+
iterations,
|
|
126
|
+
apiCalls: iterations,
|
|
127
|
+
cumulativeInputTokens,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* R2 message enrichment (v1.16.1 — /6step PARTIAL TP): some MCP consumers (notably Claude Code's
|
|
132
|
+
* tool-result rendering) collapse isError responses to the message string only and don't surface
|
|
133
|
+
* `structuredContent` — wire format includes both per CallToolResultSchema (independent optional
|
|
134
|
+
* fields) but rendering is consumer-side. Operator-actionable fields (iterations, filesRead) are
|
|
135
|
+
* mirrored into the message string so they're visible regardless of consumer rendering. Dedupe fires
|
|
136
|
+
* AFTER the iteration's generateContent completed — the call IS counted in apiCalls.
|
|
137
|
+
*/
|
|
138
|
+
export function noProgressDetected(state, stuck) {
|
|
139
|
+
const { iterations } = state;
|
|
140
|
+
const filesRead = state.filesReadSet.size;
|
|
141
|
+
const sig = stuck.signature;
|
|
142
|
+
return errorResult(`ask_agentic: no-progress loop detected — call '${sig.slice(0, 200)}' was repeated ${stuck.count} times without new file reads between repeats (after ${iterations} iter${iterations === 1 ? '' : 's'}, ${filesRead} file${filesRead === 1 ? '' : 's'} read). Returning partial state.`, {
|
|
143
|
+
errorCode: 'UNKNOWN',
|
|
144
|
+
retryable: false,
|
|
145
|
+
subReason: 'AGENTIC_NO_PROGRESS',
|
|
146
|
+
iterations,
|
|
147
|
+
apiCalls: iterations,
|
|
148
|
+
repeatedSignature: sig.slice(0, 500),
|
|
149
|
+
cumulativeInputTokens: state.cumulativeInputTokens,
|
|
150
|
+
filesRead,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The end of a loop that exhausted `maxIterations`: the forced answer when the finalization pass
|
|
155
|
+
* produced text, else `AGENTIC_MAX_ITERATIONS`.
|
|
156
|
+
*
|
|
157
|
+
* Token totals are after the finalization pass. When the pass succeeded at the API level its tokens
|
|
158
|
+
* ARE billed even if its text came back empty, so both results report the post-pass totals.
|
|
159
|
+
* `apiCalls` counts the pass from its explicit `attempted` flag (set IMMEDIATELY BEFORE the dispatch)
|
|
160
|
+
* rather than from its usage — timeouts and pre-response network failures leave usageMetadata empty
|
|
161
|
+
* even though the request was sent. Skipped paths
|
|
162
|
+
* (daily-budget reject) leave `attempted` false so they report `apiCalls = iterations`. Operators can
|
|
163
|
+
* branch on `apiCalls > iterations` to detect the forced pass without parsing `convergenceForced`.
|
|
164
|
+
*/
|
|
165
|
+
export function maxIterationsResult(run, state, rescue) {
|
|
166
|
+
const totals = {
|
|
167
|
+
apiCalls: state.iterations + (rescue.attempted ? 1 : 0),
|
|
168
|
+
inputTokens: state.cumulativeInputTokens + rescue.usage.promptTokenCount,
|
|
169
|
+
outputTokens: state.cumulativeOutputTokens + rescue.usage.candidatesTokenCount,
|
|
170
|
+
thinkingTokens: state.cumulativeThinkingTokens + rescue.usage.thoughtsTokenCount,
|
|
171
|
+
thinkingSummary: rescue.thinkingSummary,
|
|
172
|
+
};
|
|
173
|
+
if (rescue.text.length > 0) {
|
|
174
|
+
return textResult(rescue.text, answerFields(run, state, totals, 'forced'));
|
|
175
|
+
}
|
|
176
|
+
return rescueFailed(run, state, rescue, totals);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The message of a forced pass that produced no answer: skipped for the daily budget, or ran and failed — a timeout then
|
|
180
|
+
* says what fired and the knob that raises it, as a loop iteration's does.
|
|
181
|
+
*/
|
|
182
|
+
function rescueFailureMessage(run, rescue) {
|
|
183
|
+
const head = `ask_agentic: reached maxIterations (${run.limits.maxIterations}) without a final answer`;
|
|
184
|
+
if (rescue.skipReason === 'daily-budget') {
|
|
185
|
+
return `${head}; forced-finalization pass skipped because daily budget cap reached. Wait for daily reset (UTC midnight) or raise GEMINI_DAILY_BUDGET_USD.`;
|
|
186
|
+
}
|
|
187
|
+
const remedy = 'Increase maxIterations or narrow your prompt.';
|
|
188
|
+
if (rescue.timeout === null)
|
|
189
|
+
return `${head}. ${remedy}`;
|
|
190
|
+
const { timeout } = rescue;
|
|
191
|
+
return `${head}; the forced-finalization pass ${timedOutAfter(timeout)}. ${timeoutHint(timeout, run.knobNames)} ${remedy}`;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* v1.14.2 Fix 5: the error message branches on whether the rescue was skipped due to daily-budget
|
|
195
|
+
* exhaustion vs ran-and-failed (empty text / network error / timeout). The generic "Increase
|
|
196
|
+
* maxIterations" message would lie to operators on the daily-budget skip path — the actual
|
|
197
|
+
* remediation is to wait for daily reset or raise GEMINI_DAILY_BUDGET_USD. `finalizationSkipReason`
|
|
198
|
+
* is present only when set.
|
|
199
|
+
*
|
|
200
|
+
* P4 (v1.15.0): `rescueErrorCode` is the caller-visible rescue failure-class, present when the rescue
|
|
201
|
+
* ran-but-failed; absent when it was skipped (daily budget). Wrappers can branch retry behaviour on
|
|
202
|
+
* this without log scraping. P8 (v1.15.0): thinking tokens are part of the failure-path cumulative
|
|
203
|
+
* telemetry too, so a rescue-attempted-but-failed call does not under-report its billing footprint.
|
|
204
|
+
* A pass booked before it failed (an empty text, a response that could not be read) reports its billed
|
|
205
|
+
* fields, as a failed loop iteration does. A pass that timed out reports `timeoutPhase` (`tpm-wait` | `finalization`)
|
|
206
|
+
* and `timeoutSource`, read from its clock.
|
|
207
|
+
*/
|
|
208
|
+
function rescueFailed(run, state, rescue, totals) {
|
|
209
|
+
return errorResult(rescueFailureMessage(run, rescue), {
|
|
210
|
+
errorCode: 'UNKNOWN',
|
|
211
|
+
retryable: false,
|
|
212
|
+
subReason: 'AGENTIC_MAX_ITERATIONS',
|
|
213
|
+
...(rescue.skipReason !== null ? { finalizationSkipReason: rescue.skipReason } : {}),
|
|
214
|
+
...(rescue.errorCode !== null ? { rescueErrorCode: rescue.errorCode } : {}),
|
|
215
|
+
...(rescue.timeout !== null
|
|
216
|
+
? { timeoutPhase: rescue.timeout.phase, timeoutSource: rescue.timeout.source }
|
|
217
|
+
: {}),
|
|
218
|
+
...(rescue.errorMessage !== null ? { rescueErrorMessage: rescue.errorMessage } : {}),
|
|
219
|
+
iterations: state.iterations,
|
|
220
|
+
apiCalls: totals.apiCalls,
|
|
221
|
+
cumulativeInputTokens: totals.inputTokens,
|
|
222
|
+
cumulativeOutputTokens: totals.outputTokens,
|
|
223
|
+
cumulativeThinkingTokens: totals.thinkingTokens,
|
|
224
|
+
filesRead: state.filesReadSet.size,
|
|
225
|
+
...(rescue.billed ?? {}),
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=results.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"results.js","sourceRoot":"","sources":["../../../src/tools/agentic/results.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAuB,WAAW,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC9E,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAmBlE,8GAA8G;AAC9G,SAAS,YAAY,CACnB,GAAe,EACf,KAAgB,E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3
|
+
* limits, the request config, the per-pass reservation inputs) and what the loop accumulates (`LoopState`).
|
|
4
|
+
*/
|
|
5
|
+
import type { Content, GenerateContentConfig } from '@google/genai';
|
|
6
|
+
import type { resolveModel } from '../../gemini/models.js';
|
|
7
|
+
import type { MatchConfig } from '../../indexer/globs.js';
|
|
8
|
+
import type { ProgressEmitter } from '../../utils/progress.js';
|
|
9
|
+
import type { ToolContext } from '../registry.js';
|
|
10
|
+
import type { BilledFields } from '../shared/call-ledger.js';
|
|
11
|
+
import type { AskAgenticInput } from './input.js';
|
|
12
|
+
import type { SignatureCounts } from './no-progress.js';
|
|
13
|
+
import type { AgenticKnobs } from './pass-clock.js';
|
|
14
|
+
import type { UsageCounts } from './response.js';
|
|
15
|
+
export type ResolvedModel = Awaited<ReturnType<typeof resolveModel>>;
|
|
16
|
+
export declare const DEFAULT_MAX_ITERATIONS = 20;
|
|
17
|
+
export declare const DEFAULT_MAX_TOTAL_INPUT_TOKENS = 1000000;
|
|
18
|
+
export declare const DEFAULT_MAX_FILES_READ = 40;
|
|
19
|
+
/**
|
|
20
|
+
* Conservative per-iteration input estimate for the pre-iteration
|
|
21
|
+
* budget reservation. An agentic iter typically sends a few KB of
|
|
22
|
+
* prompt + accumulated tool responses + model thinking + short output.
|
|
23
|
+
* We over-estimate on purpose so `reserveBudget` atomicity catches
|
|
24
|
+
* overspend early rather than silently racing past the cap. PR #24
|
|
25
|
+
* review by Grok (P0: budget bypass on agentic path).
|
|
26
|
+
*/
|
|
27
|
+
export declare const PER_ITERATION_INPUT_TOKENS = 50000;
|
|
28
|
+
/** The env var that bounds an iteration (and the finalization pass) when the call sets no `iterationTimeoutMs`. */
|
|
29
|
+
export declare const ITERATION_TIMEOUT_ENV = "GEMINI_CODE_CONTEXT_AGENTIC_ITERATION_TIMEOUT_MS";
|
|
30
|
+
/** The env var that bounds a stream's gaps when the call sets no `stallMs`. */
|
|
31
|
+
export declare const AGENTIC_STALL_ENV = "GEMINI_CODE_CONTEXT_AGENTIC_STALL_MS";
|
|
32
|
+
/** The names a timeout message gives the two knobs and their env vars: the caller's — `ask_agentic`'s or `ask`'s. */
|
|
33
|
+
export interface KnobNames {
|
|
34
|
+
readonly totalMs: string;
|
|
35
|
+
readonly totalEnv: string;
|
|
36
|
+
readonly stallMs: string;
|
|
37
|
+
readonly stallEnv: string;
|
|
38
|
+
}
|
|
39
|
+
/** The knobs of a direct `ask_agentic` call. */
|
|
40
|
+
export declare const AGENTIC_KNOB_NAMES: KnobNames;
|
|
41
|
+
/** Where a knob that is set came from: the call's own field, or its env var. */
|
|
42
|
+
export type KnobOrigin = 'call' | 'env';
|
|
43
|
+
/** The call's knobs as resolved, with the origin of each one (`null` when it is unset). */
|
|
44
|
+
export interface ResolvedKnobs extends AgenticKnobs {
|
|
45
|
+
readonly iterationFrom: KnobOrigin | null;
|
|
46
|
+
readonly stallFrom: KnobOrigin | null;
|
|
47
|
+
}
|
|
48
|
+
/** The call's knobs, resolved once: per call, else the env var, clamped as every timeout knob is. */
|
|
49
|
+
export declare function resolveAgenticKnobs(input: AskAgenticInput): ResolvedKnobs;
|
|
50
|
+
/**
|
|
51
|
+
* The knob names a call's timeout messages give: the caller's, except that a knob its env var set is named by that
|
|
52
|
+
* env var — the agentic one. On `ask`'s fallback, `ask` forwards its own knobs per call; where it set none of a kind,
|
|
53
|
+
* the agentic env var of that kind bounds the loop, and the message names it, never an `ask` variable that set nothing.
|
|
54
|
+
*/
|
|
55
|
+
export declare function messageKnobNames(names: KnobNames, knobs: ResolvedKnobs): KnobNames;
|
|
56
|
+
export interface RunLimits {
|
|
57
|
+
maxIterations: number;
|
|
58
|
+
maxTotalInputTokens: number;
|
|
59
|
+
maxFilesRead: number;
|
|
60
|
+
}
|
|
61
|
+
/** What every pass's reservation is priced from, beside the resolved model. */
|
|
62
|
+
export interface PassBudget {
|
|
63
|
+
/** Output (and thinking) tokens a pass reserves for. */
|
|
64
|
+
outputTokens: number;
|
|
65
|
+
/** The longest an iteration (or the finalization pass) can run: the window each reservation covers. */
|
|
66
|
+
coverMs: number | null;
|
|
67
|
+
dailyBudgetEnforced: boolean;
|
|
68
|
+
tpmEnforced: boolean;
|
|
69
|
+
}
|
|
70
|
+
/** One call, fixed once the loop starts. */
|
|
71
|
+
export interface AgenticRun {
|
|
72
|
+
input: AskAgenticInput;
|
|
73
|
+
ctx: ToolContext;
|
|
74
|
+
emitter: ProgressEmitter;
|
|
75
|
+
resolved: ResolvedModel;
|
|
76
|
+
/** Canonical (symlink-resolved) workspace root every tool call resolves against. */
|
|
77
|
+
workspaceRoot: string;
|
|
78
|
+
/** When the call started (`Date.now()`), for `durationMs`. */
|
|
79
|
+
started: number;
|
|
80
|
+
limits: RunLimits;
|
|
81
|
+
/** The user-glob filter, threaded through every executor invocation in the loop. */
|
|
82
|
+
matchConfig: MatchConfig;
|
|
83
|
+
baseConfig: GenerateContentConfig;
|
|
84
|
+
budget: PassBudget;
|
|
85
|
+
/** The knobs every pass's clock is built from, with their origins. */
|
|
86
|
+
knobs: ResolvedKnobs;
|
|
87
|
+
/** The knob names a timeout message gives (`messageKnobNames`): `ask`'s when the call is its fallback. */
|
|
88
|
+
knobNames: KnobNames;
|
|
89
|
+
}
|
|
90
|
+
/** What the loop accumulates across iterations. */
|
|
91
|
+
export interface LoopState {
|
|
92
|
+
conversation: Content[];
|
|
93
|
+
signatureCounts: SignatureCounts;
|
|
94
|
+
filesReadSet: Set<string>;
|
|
95
|
+
cumulativeInputTokens: number;
|
|
96
|
+
cumulativeOutputTokens: number;
|
|
97
|
+
cumulativeThinkingTokens: number;
|
|
98
|
+
/** P5 (v1.15.0): approximate token count of the tool-response Parts
|
|
99
|
+
* appended to `conversation` after the most recent iteration's
|
|
100
|
+
* `generateContent` returned. Folded into the rescue's
|
|
101
|
+
* `finalizationEstimate` so its TPM/budget reservation covers the data
|
|
102
|
+
* the rescue actually replays. Pre-P5 the estimate omitted this delta;
|
|
103
|
+
* with the parallel tool-dispatch (default 10 in v1.15.1+, was 3
|
|
104
|
+
* pre-bump) reading large files, the rescue could under-reserve by
|
|
105
|
+
* 100k+ tokens and 429 mid-flight. */
|
|
106
|
+
lastToolResponseTokens: number;
|
|
107
|
+
iterations: number;
|
|
108
|
+
/** The most recent iteration's actual `promptTokenCount` (size of the
|
|
109
|
+
* conversation as sent in the LAST loop iter). Used by the v1.14.2
|
|
110
|
+
* forced-finalization rescue's reservation estimate — the rescue replays
|
|
111
|
+
* the same accumulated history, so its real prompt size ≈ this value, not
|
|
112
|
+
* the static `PER_ITERATION_INPUT_TOKENS = 50_000` that under-reserves by
|
|
113
|
+
* 4–6× on a 20-iter loop with file reads. Stays 0 if the loop never
|
|
114
|
+
* produced a usage record (defensive — falls back to the static estimate
|
|
115
|
+
* at the use site). */
|
|
116
|
+
lastIterationPromptTokens: number;
|
|
117
|
+
/** The billed fields of the iteration that was booked and then failed with an error the loop does not shape;
|
|
118
|
+
* the outer catch puts them in its error result. `null` while no such failure happened. */
|
|
119
|
+
failedPassBilled: BilledFields | null;
|
|
120
|
+
}
|
|
121
|
+
export declare function runLimitsOf(input: AskAgenticInput): RunLimits;
|
|
122
|
+
/** The config of every loop request. */
|
|
123
|
+
export declare function baseConfigOf(input: AskAgenticInput): GenerateContentConfig;
|
|
124
|
+
export declare function passBudgetOf(input: AskAgenticInput, ctx: ToolContext, knobs: AgenticKnobs): PassBudget;
|
|
125
|
+
/** The tokens one pass is reserved for: its input estimate, and `budget.outputTokens` of output and of thinking. */
|
|
126
|
+
export declare function passReservedUsage(run: AgenticRun, inputTokens: number): UsageCounts;
|
|
127
|
+
/** One pass's reservation for the resolved model, at the rates in force from now until its timeout. */
|
|
128
|
+
export declare function passReservationUsd(run: AgenticRun, inputTokens: number): number;
|
|
129
|
+
export declare function newLoopState(prompt: string): LoopState;
|
|
130
|
+
//# sourceMappingURL=run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../../src/tools/agentic/run.ts"],"names":[],"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S,EAAE,SAAS,CAAC;CACtB;AAED,mDAAmD;AACnD,MAAM,WAAW,SAAS;IACxB,YAAY,EAAE,OAAO,EAAE,CAAC;IACxB,eAAe,EAAE,eAAe,CAAC;IACjC,YAAY,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,sBAAsB,EAAE,MAAM,CAAC;IAC/B,wBAAwB,EAAE,MAAM,CAAC;IACjC;;;;;;;0CAOsC;IACtC,sBAAsB,EAAE,MAAM,CAAC;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB;;;;;;;2BAOuB;IACvB,yBAAyB,EAAE,MAAM,CAAC;IAClC;+FAC2F;IAC3F,gBAAgB,EAAE,YAAY,GAAG,IAAI,CAAC;CACvC;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,eAAe,GAAG,SAAS,CAM7D;AAeD,wCAAwC;AACxC,wBAAgB,YAAY,CAAC,KAAK,EAAE,eAAe,GAAG,qBAAqB,CAU1E;AAED,wBAAgB,YAAY,CAC1B,KAAK,EAAE,eAAe,EACtB,GAAG,EAAE,WAAW,EAChB,KAAK,EAAE,YAAY,GAClB,UAAU,CAOZ;AAED,oHAAoH;AACpH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,GAAG,WAAW,CAMnF;AAED,uGAAuG;AACvG,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAY/E;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAatD"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed state of one `ask_agentic` call: what is fixed when the loop starts (`AgenticRun` — the model, the
|
|
3
|
+
* limits, the request config, the per-pass reservation inputs) and what the loop accumulates (`LoopState`).
|
|
4
|
+
*/
|
|
5
|
+
import { estimateReservationUsd } from '../../utils/cost-estimator.js';
|
|
6
|
+
import { resolveStallTimeoutMs, resolveTotalTimeoutMs } from '../shared/abort-timeout.js';
|
|
7
|
+
import { SYSTEM_INSTRUCTION_AGENTIC, buildFunctionDeclarations } from './instructions.js';
|
|
8
|
+
export const DEFAULT_MAX_ITERATIONS = 20;
|
|
9
|
+
export const DEFAULT_MAX_TOTAL_INPUT_TOKENS = 1_000_000;
|
|
10
|
+
export const DEFAULT_MAX_FILES_READ = 40;
|
|
11
|
+
/**
|
|
12
|
+
* Conservative per-iteration input estimate for the pre-iteration
|
|
13
|
+
* budget reservation. An agentic iter typically sends a few KB of
|
|
14
|
+
* prompt + accumulated tool responses + model thinking + short output.
|
|
15
|
+
* We over-estimate on purpose so `reserveBudget` atomicity catches
|
|
16
|
+
* overspend early rather than silently racing past the cap. PR #24
|
|
17
|
+
* review by Grok (P0: budget bypass on agentic path).
|
|
18
|
+
*/
|
|
19
|
+
export const PER_ITERATION_INPUT_TOKENS = 50_000;
|
|
20
|
+
/** The env var that bounds an iteration (and the finalization pass) when the call sets no `iterationTimeoutMs`. */
|
|
21
|
+
export const ITERATION_TIMEOUT_ENV = 'GEMINI_CODE_CONTEXT_AGENTIC_ITERATION_TIMEOUT_MS';
|
|
22
|
+
/** The env var that bounds a stream's gaps when the call sets no `stallMs`. */
|
|
23
|
+
export const AGENTIC_STALL_ENV = 'GEMINI_CODE_CONTEXT_AGENTIC_STALL_MS';
|
|
24
|
+
/** The knobs of a direct `ask_agentic` call. */
|
|
25
|
+
export const AGENTIC_KNOB_NAMES = Object.freeze({
|
|
26
|
+
totalMs: 'iterationTimeoutMs',
|
|
27
|
+
totalEnv: ITERATION_TIMEOUT_ENV,
|
|
28
|
+
stallMs: 'stallMs',
|
|
29
|
+
stallEnv: AGENTIC_STALL_ENV,
|
|
30
|
+
});
|
|
31
|
+
const originOf = (ms, perCall) => {
|
|
32
|
+
if (ms === null)
|
|
33
|
+
return null;
|
|
34
|
+
return perCall !== undefined ? 'call' : 'env';
|
|
35
|
+
};
|
|
36
|
+
/** The call's knobs, resolved once: per call, else the env var, clamped as every timeout knob is. */
|
|
37
|
+
export function resolveAgenticKnobs(input) {
|
|
38
|
+
const iterationMs = resolveTotalTimeoutMs(input.iterationTimeoutMs, ITERATION_TIMEOUT_ENV);
|
|
39
|
+
const stallMs = resolveStallTimeoutMs(input.stallMs, AGENTIC_STALL_ENV);
|
|
40
|
+
return {
|
|
41
|
+
iterationMs,
|
|
42
|
+
stallMs,
|
|
43
|
+
iterationFrom: originOf(iterationMs, input.iterationTimeoutMs),
|
|
44
|
+
stallFrom: originOf(stallMs, input.stallMs),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The knob names a call's timeout messages give: the caller's, except that a knob its env var set is named by that
|
|
49
|
+
* env var — the agentic one. On `ask`'s fallback, `ask` forwards its own knobs per call; where it set none of a kind,
|
|
50
|
+
* the agentic env var of that kind bounds the loop, and the message names it, never an `ask` variable that set nothing.
|
|
51
|
+
*/
|
|
52
|
+
export function messageKnobNames(names, knobs) {
|
|
53
|
+
return {
|
|
54
|
+
...names,
|
|
55
|
+
...(knobs.iterationFrom === 'env' ? { totalEnv: ITERATION_TIMEOUT_ENV } : {}),
|
|
56
|
+
...(knobs.stallFrom === 'env' ? { stallEnv: AGENTIC_STALL_ENV } : {}),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function runLimitsOf(input) {
|
|
60
|
+
return {
|
|
61
|
+
maxIterations: input.maxIterations ?? DEFAULT_MAX_ITERATIONS,
|
|
62
|
+
maxTotalInputTokens: input.maxTotalInputTokens ?? DEFAULT_MAX_TOTAL_INPUT_TOKENS,
|
|
63
|
+
maxFilesRead: input.maxFilesRead ?? DEFAULT_MAX_FILES_READ,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function thinkingConfigOf(input) {
|
|
67
|
+
if (input.thinkingLevel !== undefined) {
|
|
68
|
+
return {
|
|
69
|
+
thinkingLevel: input.thinkingLevel,
|
|
70
|
+
includeThoughts: true,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (input.thinkingBudget !== undefined) {
|
|
74
|
+
return { thinkingBudget: input.thinkingBudget, includeThoughts: true };
|
|
75
|
+
}
|
|
76
|
+
return { includeThoughts: true };
|
|
77
|
+
}
|
|
78
|
+
/** The config of every loop request. */
|
|
79
|
+
export function baseConfigOf(input) {
|
|
80
|
+
return {
|
|
81
|
+
systemInstruction: SYSTEM_INSTRUCTION_AGENTIC,
|
|
82
|
+
tools: [{ functionDeclarations: buildFunctionDeclarations() }],
|
|
83
|
+
thinkingConfig: thinkingConfigOf(input),
|
|
84
|
+
...(input.maxOutputTokens !== undefined ? { maxOutputTokens: input.maxOutputTokens } : {}),
|
|
85
|
+
// One request per model call: a response that stops with `CONTINUATION` is never continued here (long decode
|
|
86
|
+
// is `ask` / `code` only, `shared/continuation.ts`).
|
|
87
|
+
automaticContinuation: false,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export function passBudgetOf(input, ctx, knobs) {
|
|
91
|
+
return {
|
|
92
|
+
outputTokens: typeof input.maxOutputTokens === 'number' ? input.maxOutputTokens : 4096,
|
|
93
|
+
coverMs: knobs.iterationMs,
|
|
94
|
+
dailyBudgetEnforced: Number.isFinite(ctx.config.dailyBudgetUsd),
|
|
95
|
+
tpmEnforced: ctx.config.tpmThrottleLimit > 0,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** The tokens one pass is reserved for: its input estimate, and `budget.outputTokens` of output and of thinking. */
|
|
99
|
+
export function passReservedUsage(run, inputTokens) {
|
|
100
|
+
return {
|
|
101
|
+
promptTokenCount: inputTokens,
|
|
102
|
+
candidatesTokenCount: run.budget.outputTokens,
|
|
103
|
+
thoughtsTokenCount: run.budget.outputTokens,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** One pass's reservation for the resolved model, at the rates in force from now until its timeout. */
|
|
107
|
+
export function passReservationUsd(run, inputTokens) {
|
|
108
|
+
const reserved = passReservedUsage(run, inputTokens);
|
|
109
|
+
return estimateReservationUsd({
|
|
110
|
+
model: run.resolved.resolved,
|
|
111
|
+
uncachedInputTokens: reserved.promptTokenCount,
|
|
112
|
+
cachedInputTokens: 0,
|
|
113
|
+
outputTokens: reserved.candidatesTokenCount,
|
|
114
|
+
thinkingTokens: reserved.thoughtsTokenCount,
|
|
115
|
+
}, run.budget.coverMs);
|
|
116
|
+
}
|
|
117
|
+
export function newLoopState(prompt) {
|
|
118
|
+
return {
|
|
119
|
+
conversation: [{ role: 'user', parts: [{ text: prompt }] }],
|
|
120
|
+
signatureCounts: new Map(),
|
|
121
|
+
filesReadSet: new Set(),
|
|
122
|
+
cumulativeInputTokens: 0,
|
|
123
|
+
cumulativeOutputTokens: 0,
|
|
124
|
+
cumulativeThinkingTokens: 0,
|
|
125
|
+
lastToolResponseTokens: 0,
|
|
126
|
+
iterations: 0,
|
|
127
|
+
lastIterationPromptTokens: 0,
|
|
128
|
+
failedPassBilled: null,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.js","sourceRoot":"","sources":["../../../src/tools/agentic/run.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AAGvE,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAG1F,OAAO,EAAE,0BAA0B,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAO1F,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,CAAC;AACzC,MAAM,CAAC,MAAM,8BAA8B,GAAG,SAAS,CAAC;AACxD,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,CAAC;AAEzC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAEjD,mHAAmH;AACn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