diff --git a/src/agents/pi-tools.before-tool-call.test.ts b/src/agents/pi-tools.before-tool-call.test.ts index df5d9d3a2c..3348c3e334 100644 --- a/src/agents/pi-tools.before-tool-call.test.ts +++ b/src/agents/pi-tools.before-tool-call.test.ts @@ -109,6 +109,18 @@ describe("before_tool_call loop detection behavior", () => { } } } + + function createGenericReadRepeatFixture() { + const execute = vi.fn().mockResolvedValue({ + content: [{ type: "text", text: "same output" }], + details: { ok: true }, + }); + return { + tool: createWrappedTool("read", execute), + params: { path: "/tmp/file" }, + }; + } + it("blocks known poll loops when no progress repeats", async () => { const execute = vi.fn().mockResolvedValue({ content: [{ type: "text", text: "(no new output)\n\nProcess still running." }], @@ -160,12 +172,7 @@ describe("before_tool_call loop detection behavior", () => { }); it("keeps generic repeated calls warn-only below global breaker", async () => { - const execute = vi.fn().mockResolvedValue({ - content: [{ type: "text", text: "same output" }], - details: { ok: true }, - }); - const tool = createWrappedTool("read", execute); - const params = { path: "/tmp/file" }; + const { tool, params } = createGenericReadRepeatFixture(); for (let i = 0; i < CRITICAL_THRESHOLD + 5; i += 1) { await expect(tool.execute(`read-${i}`, params, undefined, undefined)).resolves.toBeDefined(); @@ -173,12 +180,7 @@ describe("before_tool_call loop detection behavior", () => { }); it("blocks generic repeated no-progress calls at global breaker threshold", async () => { - const execute = vi.fn().mockResolvedValue({ - content: [{ type: "text", text: "same output" }], - details: { ok: true }, - }); - const tool = createWrappedTool("read", execute); - const params = { path: "/tmp/file" }; + const { tool, params } = createGenericReadRepeatFixture(); for (let i = 0; i < GLOBAL_CIRCUIT_BREAKER_THRESHOLD; i += 1) { await expect(tool.execute(`read-${i}`, params, undefined, undefined)).resolves.toBeDefined(); @@ -192,12 +194,7 @@ describe("before_tool_call loop detection behavior", () => { it("coalesces repeated generic warning events into threshold buckets", async () => { await withToolLoopEvents( async (emitted) => { - const execute = vi.fn().mockResolvedValue({ - content: [{ type: "text", text: "same output" }], - details: { ok: true }, - }); - const tool = createWrappedTool("read", execute); - const params = { path: "/tmp/file" }; + const { tool, params } = createGenericReadRepeatFixture(); for (let i = 0; i < 21; i += 1) { await tool.execute(`read-bucket-${i}`, params, undefined, undefined);