Pure file moves. No behavior change. inference.ts retains createInferenceRunner public surface; new files are internal to services/inference/. - budget.ts: resolveToolBudget - sentinels.ts: detectDoomLoop (re-exported through inference.ts), isCapHitSentinel, isDoomLoopSentinel, isAnySentinel - xml-parser.ts: parseXmlToolCall, partialXmlOpenerStart First of four refactor batches preparing inference.ts for the v1.13 AI SDK migration. inference.ts goes from 1780 LoC to ~1620. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import type { Message, ToolCall } from '../../types/api.js';
|
|
|
|
// v1.11.6: doom-loop guard. When the model calls the same tool with the
|
|
// same arguments DOOM_LOOP_THRESHOLD times in a row within one user-message
|
|
// turn, abort the recursion and run the same wrap-up summary path as the
|
|
// cap-hit case. Ported from opencode (DOOM_LOOP_THRESHOLD in
|
|
// session/processor.ts). Threshold of 3 is the smallest value that doesn't
|
|
// false-positive on a model that retries once after a transient error.
|
|
export const DOOM_LOOP_THRESHOLD = 3;
|
|
|
|
// Returns the name + args of the looping tool when the LAST
|
|
// DOOM_LOOP_THRESHOLD entries in `recentToolCalls` are identical (same name
|
|
// AND deep-equal args via JSON.stringify). Returns null otherwise.
|
|
// Pure; exported for unit-test access.
|
|
export function detectDoomLoop(
|
|
recentToolCalls: ToolCall[],
|
|
): { name: string; args: Record<string, unknown> } | null {
|
|
if (recentToolCalls.length < DOOM_LOOP_THRESHOLD) return null;
|
|
const last = recentToolCalls.slice(-DOOM_LOOP_THRESHOLD);
|
|
const ref = last[0]!;
|
|
const refArgs = JSON.stringify(ref.args);
|
|
for (let i = 1; i < last.length; i++) {
|
|
const tc = last[i]!;
|
|
if (tc.name !== ref.name) return null;
|
|
if (JSON.stringify(tc.args) !== refArgs) return null;
|
|
}
|
|
return { name: ref.name, args: ref.args };
|
|
}
|
|
|
|
export function isCapHitSentinel(m: Message): boolean {
|
|
return (
|
|
m.role === 'system' &&
|
|
m.metadata !== null &&
|
|
typeof m.metadata === 'object' &&
|
|
(m.metadata as { kind?: unknown }).kind === 'cap_hit'
|
|
);
|
|
}
|
|
|
|
// v1.11.6: parallel predicate. Same UI-only semantics as cap-hit sentinels —
|
|
// never sent to the LLM (filtered by buildMessagesPayload through the
|
|
// isAnySentinel check below).
|
|
export function isDoomLoopSentinel(m: Message): boolean {
|
|
return (
|
|
m.role === 'system' &&
|
|
m.metadata !== null &&
|
|
typeof m.metadata === 'object' &&
|
|
(m.metadata as { kind?: unknown }).kind === 'doom_loop'
|
|
);
|
|
}
|
|
|
|
export function isAnySentinel(m: Message): boolean {
|
|
return isCapHitSentinel(m) || isDoomLoopSentinel(m);
|
|
}
|