Skip to content

Commit fbe35c0

Browse files
committed
feat: add tiered tool-repetition detection (soft warning + hard stop)
Replace the single-threshold ToolRepetitionDetector with a two-tier system: - Soft block (default 2nd identical call): the tool is not executed and the model receives an error asking it to justify the repeat. The user is not involved and the counter keeps incrementing toward the hard limit. - Hard stop: execution stops and the user is asked for guidance (previous behavior).
1 parent ecab519 commit fbe35c0

63 files changed

Lines changed: 1903 additions & 384 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// npx vitest run src/__tests__/tool-repetition-limits.test.ts
2+
3+
import { normalizeToolRepetitionSoftLimit } from "../provider-settings.js"
4+
5+
describe("normalizeToolRepetitionSoftLimit", () => {
6+
it("keeps a soft limit that is already below the hard limit", () => {
7+
expect(normalizeToolRepetitionSoftLimit(2, 5)).toBe(2)
8+
})
9+
10+
it("clamps a soft limit equal to the hard limit down to hardLimit - 1", () => {
11+
expect(normalizeToolRepetitionSoftLimit(5, 5)).toBe(4)
12+
})
13+
14+
it("clamps a soft limit above the hard limit down to hardLimit - 1", () => {
15+
expect(normalizeToolRepetitionSoftLimit(8, 3)).toBe(2)
16+
})
17+
18+
it("allows soft limit 0 regardless of hard limit", () => {
19+
expect(normalizeToolRepetitionSoftLimit(0, 5)).toBe(0)
20+
})
21+
22+
it("clamps soft to 0 when the hard limit is 1", () => {
23+
expect(normalizeToolRepetitionSoftLimit(3, 1)).toBe(0)
24+
})
25+
26+
it("disables the soft tier when the hard limit is 0", () => {
27+
expect(normalizeToolRepetitionSoftLimit(9, 0)).toBe(0)
28+
expect(normalizeToolRepetitionSoftLimit(2, 0)).toBe(0)
29+
expect(normalizeToolRepetitionSoftLimit(2, -1)).toBe(0)
30+
})
31+
32+
it("disables the soft tier when the hard limit is effectively unreachable", () => {
33+
expect(normalizeToolRepetitionSoftLimit(2, Number.MAX_SAFE_INTEGER)).toBe(0)
34+
expect(normalizeToolRepetitionSoftLimit(2, Number.POSITIVE_INFINITY)).toBe(0)
35+
})
36+
37+
it("clamps negative soft values to 0", () => {
38+
expect(normalizeToolRepetitionSoftLimit(-3, 5)).toBe(0)
39+
expect(normalizeToolRepetitionSoftLimit(-3, 0)).toBe(0)
40+
})
41+
42+
it("never returns a negative value for a fractional hard limit", () => {
43+
expect(normalizeToolRepetitionSoftLimit(3, 0.5)).toBe(0)
44+
expect(normalizeToolRepetitionSoftLimit(0, 0.5)).toBe(0)
45+
})
46+
47+
it("always stays strictly below an enabled hard limit", () => {
48+
for (let hard = 1; hard <= 20; hard++) {
49+
for (let requested = 0; requested <= 30; requested++) {
50+
const result = normalizeToolRepetitionSoftLimit(requested, hard)
51+
expect(result).toBeGreaterThanOrEqual(0)
52+
expect(result).toBeLessThan(hard)
53+
}
54+
}
55+
})
56+
})

‎packages/types/src/provider-settings.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ import {
5555

5656
export const DEFAULT_CONSECUTIVE_MISTAKE_LIMIT = 3
5757

58+
/**
59+
* Number of identical consecutive tool calls allowed before the tool is
60+
* soft-blocked. When reached, the tool is not executed and the model is
61+
* asked to justify why it needs to repeat the call. Set to 0 to disable.
62+
*/
63+
export const DEFAULT_TOOL_REPETITION_SOFT_LIMIT = 2
64+
65+
/**
66+
* Shared by `ToolRepetitionDetector`, `Task` and the settings UI.
67+
* Clamps the soft limit to `[0, hardLimit - 1]` so it fires before the hard
68+
* stop. A hard limit of 0 disables the whole mechanism, so soft becomes 0 too.
69+
*
70+
* An effectively unreachable hard limit (`Number.MAX_SAFE_INTEGER` or larger,
71+
* e.g. tasks created through the extension API) also disables the soft tier:
72+
* without a reachable hard stop, soft blocking would reject every identical
73+
* call from then on with no escalation to the user.
74+
*/
75+
export function normalizeToolRepetitionSoftLimit(softLimit: number, hardLimit: number): number {
76+
if (hardLimit <= 0 || hardLimit >= Number.MAX_SAFE_INTEGER) {
77+
return 0
78+
}
79+
80+
return Math.max(0, Math.min(softLimit, hardLimit - 1))
81+
}
82+
5883
/**
5984
* DynamicProvider
6085
*

‎packages/types/src/provider-settings/common.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@ export const API_PROVIDER_FIELD = "apiProvider"
77
export const SETTINGS_SHAPE_FIELD = "settingsShape"
88
export const API_MODEL_ID_FIELD = "apiModelId"
99

10+
// Treat a persisted `null` as an unset value. Previously persisted or imported
11+
// profiles may store `null` for these fields; converting `null` to `undefined`
12+
// before validation keeps the inferred type as `NonNull<T> | undefined` (so
13+
// consumers don't need to handle `null`) while ensuring the whole profile is not
14+
// dropped during per-profile `safeParse` in ProviderSettingsManager.load().
15+
//
16+
// The argument is a nullish schema (accepts `null` and `undefined`). At runtime we
17+
// map `null` to `undefined` before validation; the return type strips `null` from
18+
// the output because it can never survive the preprocessing step.
19+
const nullableOptional = <Schema extends z.ZodTypeAny>(schema: Schema) =>
20+
z.preprocess((value) => (value === null ? undefined : value), schema) as unknown as z.ZodType<
21+
Exclude<z.infer<Schema>, null>,
22+
z.ZodTypeDef,
23+
z.input<Schema>
24+
>
25+
1026
export const baseProviderSettingsShape = {
1127
includeMaxTokens: z.boolean().optional(),
1228
todoListEnabled: z.boolean().optional(),
1329
modelTemperature: z.number().nullish(),
1430
rateLimitSeconds: z.number().optional(),
15-
consecutiveMistakeLimit: z.number().min(0).optional(),
31+
consecutiveMistakeLimit: nullableOptional(z.number().min(0).nullish()),
32+
toolRepetitionSoftLimit: nullableOptional(z.number().min(0).nullish()),
1633
enableReasoningEffort: z.boolean().optional(),
1734
reasoningEffort: reasoningEffortSettingSchema.optional(),
1835
modelMaxTokens: z.number().optional(),

‎packages/types/src/task.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface CreateTaskOptions {
8787
taskId?: string
8888
enableCheckpoints?: boolean
8989
consecutiveMistakeLimit?: number
90+
toolRepetitionSoftLimit?: number
9091
experiments?: Record<string, boolean>
9192
initialTodos?: TodoItem[]
9293
/** Initial status for the task's history item (e.g., "active" for child tasks) */

‎src/__tests__/single-open-invariant.spec.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,5 +419,14 @@ describe("Single-open-task invariant", () => {
419419
expect(taskId).toBe("ipc-1")
420420
expect(removeClineFromStack).toHaveBeenCalledTimes(1)
421421
expect(createTask).toHaveBeenCalled()
422+
// API tasks have no reachable hard stop, so the soft repetition tier
423+
// must be disabled rather than soft-blocking forever.
424+
expect(createTask).toHaveBeenCalledWith(
425+
"hello",
426+
undefined,
427+
undefined,
428+
expect.objectContaining({ consecutiveMistakeLimit: Number.MAX_SAFE_INTEGER, toolRepetitionSoftLimit: 0 }),
429+
{},
430+
)
422431
})
423432
})

‎src/core/assistant-message/__tests__/presentAssistantMessage-custom-tool.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
7474
recordToolUsage: vi.fn(),
7575
recordToolError: vi.fn(),
7676
toolRepetitionDetector: {
77-
check: vi.fn().mockReturnValue({ allowExecution: true }),
77+
check: vi.fn().mockReturnValue({ action: "allow" }),
7878
},
7979
providerRef: {
8080
deref: () => ({

‎src/core/assistant-message/__tests__/presentAssistantMessage-images.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe("presentAssistantMessage - Image Handling in Native Tool Calling", () =
4848
},
4949
recordToolUsage: vi.fn(),
5050
toolRepetitionDetector: {
51-
check: vi.fn().mockReturnValue({ allowExecution: true }),
51+
check: vi.fn().mockReturnValue({ action: "allow" }),
5252
},
5353
providerRef: {
5454
deref: () => ({

0 commit comments

Comments
 (0)