repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(rtk): gate find rewrites on path support

7efe605a498f5f28b0ce982616b285a10f0df4a3

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (4)

extensions/rtk/README.md

Modified
37 37 ## UI
39 -The bash tool widget is the indicator.
39 +The bash tool widget is the command indicator.
40 40 When RTK applies, Pi shows the rewritten `rtk ...` command.
41 +On load, Pi warns whether `find` rewrites are disabled or the compatibility guard can be removed.
42 43 ## Behavior
46 47 1. probes `rtk --version` on extension load
47 48 2. warns once and registers nothing if `rtk` is missing or too old
48 -3. ignores non-bash tools
49 -4. ignores empty, already-`rtk`, and disabled commands
50 -5. calls `rtk rewrite <command>` with a 2s timeout
51 -6. applies exit `0` and `3` rewrites
52 -7. locally maps missing `journalctl`, `systemctl status`, `hyprctl clients`, and `ps` filters to `rtk summary`
53 -8. leaves other exit `1`, exit `2`, timeouts, errors, and empty output untouched
49 +3. probes `rtk find -path` against a temporary nested marker and warns with the result
50 +4. ignores non-bash tools
51 +5. ignores empty, already-`rtk`, and disabled commands
52 +6. calls `rtk rewrite <command>` with a 2s timeout
53 +7. applies exit `0` and `3` rewrites
54 +8. rejects every `rtk find` rewrite while the path probe fails
55 +9. locally maps missing `journalctl`, `systemctl status`, `hyprctl clients`, and `ps` filters to `rtk summary`
56 +10. leaves other exit `1`, exit `2`, timeouts, errors, and empty output untouched
55 58 No prompt injection.
56 59 No status widget.

extensions/rtk/__tests__/harness.test.ts

Modified
18 18 "const [, , sub, command] = process.argv;",
19 19 "if (sub === '--version') { console.log('rtk 0.37.2'); process.exit(0); }",
20 20 "if (sub === 'rewrite' && command === 'git status') { console.log('rtk git status'); process.exit(0); }",
21 + "if (sub === 'rewrite' && command === 'find . -name AGENTS.md') { console.log('rtk find . -name AGENTS.md'); process.exit(3); }",
21 22 "process.exit(1);",
23 24 ].join("\n");
77 78 await t?.session.extensionRunner.emitToolCall(event as never);
78 79 expect(event.input.command).toBe("rtk summary ps aux");
82 + it("rejects find rewrites when the capability probe fails", async () => {
83 + const event = {
84 + type: "tool_call",
85 + toolName: "bash",
86 + toolCallId: "3",
87 + input: { command: "find . -name AGENTS.md" },
88 + };
89 + await t?.session.extensionRunner.emitToolCall(event as never);
90 + expect(event.input.command).toBe("find . -name AGENTS.md");
91 + });

extensions/rtk/__tests__/rtk.test.ts

Modified
1 1 import { describe, expect, test, vi } from "vitest";
2 2 import rtkExtension, { __test } from "../index.ts";
4 -const { localFallbackRewrite, parseRtkVersion, shouldSkipCommand } = __test;
4 +const {
5 + isFindRewrite,
6 + localFallbackRewrite,
7 + parseRtkVersion,
8 + shouldSkipCommand,
9 +} = __test;
6 11 type Handler = (event: any, ctx: any) => unknown | Promise<unknown>;
53 58 expect(shouldSkipCommand("rtk git status")).toBe(true);
54 59 expect(shouldSkipCommand("env RTK_DISABLED=1 git status")).toBe(true);
55 60 expect(shouldSkipCommand("git status")).toBe(false);
61 + expect(isFindRewrite("rtk find . -name AGENTS.md")).toBe(true);
62 + expect(isFindRewrite("rtk git status && rtk find . -name AGENTS.md")).toBe(
63 + true,
64 + );
65 + expect(isFindRewrite("rtk git status")).toBe(false);
58 68 test("maps local noisy-output misses only", () => {
115 125 test("registers only tool_call when rtk is available", async () => {
116 126 const fx = await loadWithRewrite(1);
117 127 expect(fx.pi.on).toHaveBeenCalledWith("tool_call", expect.any(Function));
118 - expect(fx.notes).toEqual([]);
128 + expect(fx.notes).toEqual([
129 + ["rtk find rewrites disabled: -path semantics unsupported", "warning"],
130 + ]);
121 133 test("rewrites via rtk and falls back only on no-match", async () => {
140 152 expect(await emit(fx as never, "ps aux")).toBe("rtk summary ps aux");
155 + test("rejects find rewrites when rtk find lacks path semantics", async () => {
156 + const fx = fakePi(async (_command, args) => {
157 + if (args[0] === "--version") return { code: 0, stdout: "rtk 0.42.4" };
158 + if (args[0] === "find") {
159 + return {
160 + code: 0,
161 + stdout: "0 for '*nested/rtk-find-path-capability-marker'",
162 + };
164 + return { code: 3, stdout: "rtk find . -name AGENTS.md" };
165 + });
166 + await rtkExtension(fx.pi as never, fx.ctx as never);
167 + expect(await emit(fx as never, "find . -name AGENTS.md")).toBe(
168 + "find . -name AGENTS.md",
169 + );
170 + expect(fx.notes).toEqual([
171 + ["rtk find rewrites disabled: -path semantics unsupported", "warning"],
172 + ]);
173 + });
175 + test("allows find rewrites when the path capability probe passes", async () => {
176 + const fx = fakePi(async (_command, args) => {
177 + if (args[0] === "--version") return { code: 0, stdout: "rtk 0.42.4" };
178 + if (args[0] === "find") {
179 + return { code: 0, stdout: "nested/rtk-find-path-capability-marker" };
181 + return { code: 3, stdout: "rtk find . -name AGENTS.md" };
182 + });
183 + await rtkExtension(fx.pi as never, fx.ctx as never);
184 + expect(await emit(fx as never, "find . -name AGENTS.md")).toBe(
185 + "rtk find . -name AGENTS.md",
186 + );
187 + expect(fx.notes).toEqual([
188 + ["rtk find -path works: remove the compatibility guard", "warning"],
189 + ]);
190 + });
143 192 test("leaves non-bash and skipped commands untouched", async () => {
144 193 const fx = await loadWithRewrite(0, "rtk git status\n");
145 194 expect(await emit(fx, "git status", "read")).toBe("git status");

extensions/rtk/index.ts

Modified
1 1 import { constants } from "node:fs";
2 -import { access } from "node:fs/promises";
2 +import { access, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3 +import { tmpdir } from "node:os";
3 4 import { delimiter, join } from "node:path";
4 5 import type {
5 6 ExecOptions,
48 49 return /[|;<>`]|\$\(|&&|\|\|/u.test(command);
52 +export function isFindRewrite(command: string): boolean {
53 + return command === "rtk find" || command.includes("rtk find ");
51 56 export function localFallbackRewrite(command: string): string | undefined {
52 57 const trimmed = command.trim();
53 58 if (hasShellOperators(trimmed)) return undefined;
161 +async function supportsFindPath(pi: ExtensionAPI): Promise<boolean> {
162 + const marker = "rtk-find-path-capability-marker";
163 + let root: string | undefined;
164 + try {
165 + root = await mkdtemp(join(tmpdir(), "pi-rtk-find-"));
166 + const nested = join(root, "nested");
167 + await mkdir(nested);
168 + await writeFile(join(nested, marker), "");
169 + const result = await execRtk(
170 + pi,
171 + ["find", root, "-path", `*nested/${marker}`],
172 + { timeout: TIMEOUT_MS },
173 + );
174 + const output = result.stdout ?? "";
175 + return (
176 + result.code === 0 &&
177 + !result.killed &&
178 + output.includes(marker) &&
179 + !/^\s*0 for /u.test(output)
180 + );
181 + } catch {
182 + return false;
183 + } finally {
184 + if (root) await rm(root, { force: true, recursive: true }).catch(() => {});
188 +function notifyUser(
189 + pi: ExtensionAPI,
190 + ctx: ExtensionContext | undefined,
191 + message: string,
192 +): boolean {
193 + const notify = ctx?.hasUI
194 + ? ctx.ui.notify.bind(ctx.ui)
195 + : (pi as { ui?: { notify?: (message: string, level: "warning") => void } })
196 + .ui?.notify;
197 + if (!notify) return false;
198 + notify(message, "warning");
199 + return true;
156 202 function warnUnavailable(
157 203 status: RtkStatus,
158 204 pi: ExtensionAPI,
159 205 ctx?: ExtensionContext,
160 206 ): boolean {
161 207 if (status.ok) return false;
162 - const notify = ctx?.hasUI
163 - ? ctx.ui.notify.bind(ctx.ui)
164 - : (pi as { ui?: { notify?: (message: string, level: "warning") => void } })
165 - .ui?.notify;
166 - if (!notify) return false;
167 208 const suffix =
168 209 status.reason === "old"
169 210 ? `too old (${status.version || "unknown"}); need >= 0.23.0`
170 211 : "not found on PATH";
171 - notify(`rtk disabled: ${suffix}`, "warning");
172 - return true;
212 + return notifyUser(pi, ctx, `rtk disabled: ${suffix}`);
175 215 export const __test = {
216 + isFindRewrite,
176 217 localFallbackRewrite,
177 218 parseRtkVersion,
178 219 shouldSkipCommand,
190 231 return;
234 + const findPathSupported = await supportsFindPath(pi);
235 + notifyUser(
236 + pi,
237 + ctx,
238 + findPathSupported
239 + ? "rtk find -path works: remove the compatibility guard"
240 + : "rtk find rewrites disabled: -path semantics unsupported",
241 + );
193 242 pi.on("tool_call", async (event, ctx) => {
194 243 if (!isToolCallEventType("bash", event)) return;
195 244 const command = event.input.command;
201 250 : outcome.kind === "fallback"
202 251 ? localFallbackRewrite(command)
203 252 : undefined;
204 - if (rewritten) event.input.command = rewritten;
253 + if (rewritten && (findPathSupported || !isFindRewrite(rewritten)))
254 + event.input.command = rewritten;