db84b52c0c
The catch block at search.ts:480-485 tried to convert native regex-build failures into clean `ToolError`s but checked for the prefix `"regex parse error"` (lowercase). The native crate at `crates/pi-natives/src/grep.rs` actually emits `"Regex error: "` (capital R, no "parse"). The branch was unreachable: invalid patterns like `a[` leaked out as raw `Error` with a stack trace instead of being wrapped in a structured `ToolError` for the agent to feed back on. Match against `/^regex(?: parse)? error/i` so both the actual native prefix and any hypothetical `regex parse error: ...` variant are caught. Rewrite the leading prefix to `Invalid regex: ` so the agent immediately sees the failure mode. Test: new `test/tools/search-invalid-regex.test.ts` asserts the pattern `a[` rejects with an `instanceof ToolError` whose message matches `/regex/i`. Fails on current main (raw `Error` escapes); passes with the fix.
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
import * as fs from "node:fs/promises";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
|
|
import { SearchTool, type ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
|
|
import { ToolError } from "@oh-my-pi/pi-coding-agent/tools/tool-errors";
|
|
|
|
function createTestSession(cwd: string, overrides: Partial<ToolSession> = {}): ToolSession {
|
|
return {
|
|
cwd,
|
|
hasUI: false,
|
|
getSessionFile: () => null,
|
|
getSessionSpawns: () => "*",
|
|
settings: Settings.isolated(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("search tool invalid regex handling", () => {
|
|
let cwd: string;
|
|
|
|
beforeEach(async () => {
|
|
cwd = await fs.mkdtemp(path.join(os.tmpdir(), "pi-search-invalid-regex-"));
|
|
await Bun.write(path.join(cwd, "sample.txt"), "hello world\n");
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await fs.rm(cwd, { recursive: true, force: true });
|
|
});
|
|
|
|
it("wraps invalid regex pattern errors in a ToolError", async () => {
|
|
const tool = new SearchTool(createTestSession(cwd));
|
|
|
|
let caught: unknown;
|
|
try {
|
|
await tool.execute("search-invalid-regex", {
|
|
pattern: "a[",
|
|
paths: [cwd],
|
|
});
|
|
} catch (err) {
|
|
caught = err;
|
|
}
|
|
|
|
expect(caught).toBeInstanceOf(ToolError);
|
|
expect((caught as Error).message).toMatch(/regex/i);
|
|
});
|
|
});
|