|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { createPosixTools } from "@intx/tools-posix"; |
| 6 | +import { mapSearchBudgetOutcome } from "./shell-guard-plugin.js"; |
| 7 | +import { BUDGET_EXPIRED } from "../util/budget-race.js"; |
| 8 | +import { |
| 9 | + formatUnboundedSearchMessage, |
| 10 | + isUnboundedRootSearch, |
| 11 | + TIMEOUT_PREFIX, |
| 12 | +} from "./tool-time-budget.js"; |
| 13 | +import { ripgrepPlugin } from "./ripgrep-plugin.js"; |
| 14 | +import type { RgChild, SpawnRg } from "./rg-run.js"; |
| 15 | + |
| 16 | +/** |
| 17 | + * CL-9469: search_files must fail closed on unbounded root walks instead of |
| 18 | + * timing out. A workspace-root search with a recursive glob refuses up front |
| 19 | + * with scope guidance; bounded searches still return hits. |
| 20 | + */ |
| 21 | + |
| 22 | +async function withFixture( |
| 23 | + run: (paths: { cwd: string; sub: string }) => Promise<void>, |
| 24 | +): Promise<void> { |
| 25 | + const cwd = await mkdtemp(join(tmpdir(), "cl9469-search-scope-")); |
| 26 | + const sub = join(cwd, "sub"); |
| 27 | + try { |
| 28 | + await mkdir(join(sub, "nested"), { recursive: true }); |
| 29 | + await mkdir(join(cwd, "node_modules"), { recursive: true }); |
| 30 | + await writeFile(join(cwd, "top.ts"), "export const top = 1;\n"); |
| 31 | + await writeFile(join(sub, "keep.ts"), "export const keep = 1;\n"); |
| 32 | + await writeFile(join(sub, "nested", "deep.ts"), "export const deep = 1;\n"); |
| 33 | + await writeFile( |
| 34 | + join(cwd, "node_modules", "skip.ts"), |
| 35 | + "export const s = 1;\n", |
| 36 | + ); |
| 37 | + await run({ cwd, sub }); |
| 38 | + } finally { |
| 39 | + await rm(cwd, { recursive: true, force: true }); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function throwingSpawn(calls: string[][]): SpawnRg { |
| 44 | + return (args) => { |
| 45 | + calls.push(args); |
| 46 | + throw new Error("search must not walk: rg must not spawn"); |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +// rg absent: the ENOENT error event is exactly what runRg treats as |
| 51 | +// "rg not installed" ({ kind: "unavailable" } -> fallback walker leg). |
| 52 | +function rgMissingSpawn(): SpawnRg { |
| 53 | + return () => ({ |
| 54 | + pid: undefined, |
| 55 | + stdout: { on: () => undefined }, |
| 56 | + stderr: { on: () => undefined }, |
| 57 | + on: ((event: string, listener: (arg: never) => void) => { |
| 58 | + if (event === "error") { |
| 59 | + const err = Object.assign(new Error("spawn rg ENOENT"), { |
| 60 | + code: "ENOENT", |
| 61 | + }); |
| 62 | + queueMicrotask(() => listener(err as never)); |
| 63 | + } |
| 64 | + }) as RgChild["on"], |
| 65 | + kill: () => undefined, |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +describe("CL-9469 search_files fails closed on unbounded root walks", () => { |
| 70 | + test("root recursive glob is refused with scope guidance, not a walk", async () => { |
| 71 | + await withFixture(async ({ cwd }) => { |
| 72 | + const spawnCalls: string[][] = []; |
| 73 | + const tools = createPosixTools({ |
| 74 | + cwd, |
| 75 | + plugins: [ripgrepPlugin(cwd, {}, throwingSpawn(spawnCalls))], |
| 76 | + }); |
| 77 | + const started = Date.now(); |
| 78 | + const result = await tools.run( |
| 79 | + { id: "1", name: "search_files", arguments: { pattern: "**/*.ts" } }, |
| 80 | + new AbortController().signal, |
| 81 | + ); |
| 82 | + expect(Date.now() - started).toBeLessThan(5_000); |
| 83 | + expect(spawnCalls.length).toBe(0); |
| 84 | + expect(result.isError).toBe(true); |
| 85 | + expect(String(result.content)).toContain("narrow `path`"); |
| 86 | + expect(String(result.content)).toContain("glob"); |
| 87 | + expect(String(result.content)).toContain("not the same as"); |
| 88 | + expect(String(result.content)).not.toContain( |
| 89 | + "[timed out before completing]", |
| 90 | + ); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + test("explicit root path with recursive glob is refused the same way", async () => { |
| 95 | + await withFixture(async ({ cwd }) => { |
| 96 | + const spawnCalls: string[][] = []; |
| 97 | + const tools = createPosixTools({ |
| 98 | + cwd, |
| 99 | + plugins: [ripgrepPlugin(cwd, {}, throwingSpawn(spawnCalls))], |
| 100 | + }); |
| 101 | + for (const path of [".", cwd]) { |
| 102 | + const result = await tools.run( |
| 103 | + { |
| 104 | + id: "1", |
| 105 | + name: "search_files", |
| 106 | + arguments: { pattern: "**/*.ts", path }, |
| 107 | + }, |
| 108 | + new AbortController().signal, |
| 109 | + ); |
| 110 | + expect(spawnCalls.length).toBe(0); |
| 111 | + expect(result.isError).toBe(true); |
| 112 | + expect(String(result.content)).toContain("narrow `path`"); |
| 113 | + } |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + test("bare star at the root is refused", async () => { |
| 118 | + await withFixture(async ({ cwd }) => { |
| 119 | + const spawnCalls: string[][] = []; |
| 120 | + const tools = createPosixTools({ |
| 121 | + cwd, |
| 122 | + plugins: [ripgrepPlugin(cwd, {}, throwingSpawn(spawnCalls))], |
| 123 | + }); |
| 124 | + const result = await tools.run( |
| 125 | + { id: "1", name: "search_files", arguments: { pattern: "*" } }, |
| 126 | + new AbortController().signal, |
| 127 | + ); |
| 128 | + expect(spawnCalls.length).toBe(0); |
| 129 | + expect(result.isError).toBe(true); |
| 130 | + expect(String(result.content)).toContain("narrow `path`"); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + test("scoped subdirectory recursive glob still returns hits", async () => { |
| 135 | + await withFixture(async ({ cwd, sub }) => { |
| 136 | + for (const spawn of [undefined, rgMissingSpawn()] as const) { |
| 137 | + const tools = createPosixTools({ |
| 138 | + cwd, |
| 139 | + plugins: [ripgrepPlugin(cwd, {}, spawn)], |
| 140 | + }); |
| 141 | + const result = await tools.run( |
| 142 | + { |
| 143 | + id: "1", |
| 144 | + name: "search_files", |
| 145 | + arguments: { pattern: "**/*.ts", path: sub }, |
| 146 | + }, |
| 147 | + new AbortController().signal, |
| 148 | + ); |
| 149 | + expect(result.isError !== true).toBe(true); |
| 150 | + expect(String(result.content)).toContain("keep.ts"); |
| 151 | + expect(String(result.content)).toContain("deep.ts"); |
| 152 | + } |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + test("tight glob at the root still returns hits", async () => { |
| 157 | + await withFixture(async ({ cwd }) => { |
| 158 | + for (const spawn of [undefined, rgMissingSpawn()] as const) { |
| 159 | + const tools = createPosixTools({ |
| 160 | + cwd, |
| 161 | + plugins: [ripgrepPlugin(cwd, {}, spawn)], |
| 162 | + }); |
| 163 | + const result = await tools.run( |
| 164 | + { |
| 165 | + id: "1", |
| 166 | + name: "search_files", |
| 167 | + arguments: { pattern: "*.ts" }, |
| 168 | + }, |
| 169 | + new AbortController().signal, |
| 170 | + ); |
| 171 | + expect(result.isError !== true).toBe(true); |
| 172 | + expect(String(result.content)).toContain("top.ts"); |
| 173 | + } |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
| 177 | + |
| 178 | +describe("CL-9469 unbounded-root predicate", () => { |
| 179 | + const cwd = "/repo/root"; |
| 180 | + test("omitted, empty, dot, and root paths count as the workspace root", () => { |
| 181 | + for (const path of [undefined, "", ".", "./", "/repo/root"]) { |
| 182 | + expect(isUnboundedRootSearch({ path, pattern: "**/*.ts", cwd })).toBe( |
| 183 | + true, |
| 184 | + ); |
| 185 | + } |
| 186 | + }); |
| 187 | + test("subdirectories and non-filesystem targets are not the root", () => { |
| 188 | + for (const path of [ |
| 189 | + "sub", |
| 190 | + "sub/nested", |
| 191 | + "/elsewhere", |
| 192 | + "archive:///x", |
| 193 | + "tool-output:///y", |
| 194 | + ]) { |
| 195 | + expect(isUnboundedRootSearch({ path, pattern: "**/*.ts", cwd })).toBe( |
| 196 | + false, |
| 197 | + ); |
| 198 | + } |
| 199 | + }); |
| 200 | + test("tight globs stay allowed at the root", () => { |
| 201 | + for (const pattern of ["*.ts", "*config*", "README.md", "*.{ts,tsx}"]) { |
| 202 | + expect(isUnboundedRootSearch({ path: undefined, pattern, cwd })).toBe( |
| 203 | + false, |
| 204 | + ); |
| 205 | + } |
| 206 | + }); |
| 207 | + test("bare star counts as unbounded", () => { |
| 208 | + expect(isUnboundedRootSearch({ path: undefined, pattern: "*", cwd })).toBe( |
| 209 | + true, |
| 210 | + ); |
| 211 | + }); |
| 212 | + test("refusal message carries scope guidance and never reads as a timeout", () => { |
| 213 | + const message = formatUnboundedSearchMessage("search_files", "**/*.ts"); |
| 214 | + expect(message).toContain("narrow `path`"); |
| 215 | + expect(message).toContain("glob"); |
| 216 | + expect(message).toContain("not the same as"); |
| 217 | + expect(message).not.toContain(TIMEOUT_PREFIX); |
| 218 | + // Shell-guard maps /abort/i errors to timeouts when its budget fires; |
| 219 | + // refusal must flow through untouched. |
| 220 | + expect(message).not.toMatch(/abort/i); |
| 221 | + }); |
| 222 | +}); |
| 223 | + |
| 224 | +describe("CL-9469 timeout path is never empty success", () => { |
| 225 | + test("budget expiry is an explicit timeout error, not empty results", () => { |
| 226 | + const result = mapSearchBudgetOutcome("1", "search_files", BUDGET_EXPIRED, { |
| 227 | + budgetAborted: true, |
| 228 | + parentAborted: false, |
| 229 | + }); |
| 230 | + expect(result.isError).toBe(true); |
| 231 | + expect(String(result.content)).toContain(TIMEOUT_PREFIX); |
| 232 | + expect(String(result.content)).toContain("narrow `path`"); |
| 233 | + expect(String(result.content)).toContain("not the same as"); |
| 234 | + }); |
| 235 | + test("parent abort during the race reads as aborted, not a timeout", () => { |
| 236 | + const result = mapSearchBudgetOutcome("1", "grep", BUDGET_EXPIRED, { |
| 237 | + budgetAborted: true, |
| 238 | + parentAborted: true, |
| 239 | + }); |
| 240 | + expect(result.isError).toBe(true); |
| 241 | + expect(String(result.content)).toContain("aborted"); |
| 242 | + expect(String(result.content)).not.toContain(TIMEOUT_PREFIX); |
| 243 | + }); |
| 244 | + test("timeout error from an inner plugin passes through as an error", () => { |
| 245 | + const inner = { |
| 246 | + callId: "1", |
| 247 | + content: `grep ${TIMEOUT_PREFIX} — narrow down`, |
| 248 | + isError: true, |
| 249 | + } as const; |
| 250 | + const result = mapSearchBudgetOutcome("1", "grep", inner, { |
| 251 | + budgetAborted: true, |
| 252 | + parentAborted: false, |
| 253 | + }); |
| 254 | + expect(result).toBe(inner); |
| 255 | + expect(result.isError).toBe(true); |
| 256 | + }); |
| 257 | + test("generic abort torn down by the budget becomes a timeout error", () => { |
| 258 | + const result = mapSearchBudgetOutcome( |
| 259 | + "1", |
| 260 | + "search_files", |
| 261 | + { callId: "1", content: "aborted", isError: true }, |
| 262 | + { budgetAborted: true, parentAborted: false }, |
| 263 | + ); |
| 264 | + expect(result.isError).toBe(true); |
| 265 | + expect(String(result.content)).toContain(TIMEOUT_PREFIX); |
| 266 | + }); |
| 267 | + test("genuine empty success passes through untouched", () => { |
| 268 | + const empty = { |
| 269 | + callId: "1", |
| 270 | + content: 'no files matching "*.zzz-nope"', |
| 271 | + } as const; |
| 272 | + const result = mapSearchBudgetOutcome("1", "search_files", empty, { |
| 273 | + budgetAborted: false, |
| 274 | + parentAborted: false, |
| 275 | + }); |
| 276 | + expect(result).toBe(empty); |
| 277 | + expect(result.isError !== true).toBe(true); |
| 278 | + expect(String(result.content)).not.toContain(TIMEOUT_PREFIX); |
| 279 | + }); |
| 280 | +}); |
0 commit comments