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
Modified39 -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.
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.
extensions/rtk/__tests__/harness.test.ts
Modified18 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);",
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 ()
=> {
87 + input: { command:
"find . -name AGENTS.md" },
89 + await t?.session.extensionRunner.
emitToolCall(
event as never);
90 + expect(
event.
input.command).
toBe(
"find . -name AGENTS.md");
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;
6 + localFallbackRewrite,
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(
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"],
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") {
161 + stdout:
"0 for '*nested/rtk-find-path-capability-marker'",
164 + return { code:
3, stdout:
"rtk find . -name AGENTS.md" };
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",
170 + expect(fx.notes).
toEqual([
171 + [
"rtk find rewrites disabled: -path semantics unsupported",
"warning"],
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" };
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",
187 + expect(fx.notes).
toEqual([
188 + [
"rtk find -path works: remove the compatibility guard",
"warning"],
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";
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;
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(
171 + [
"find",
root,
"-path",
`*nested/${marker}`],
172 + { timeout: TIMEOUT_MS },
174 + const output
= result.stdout
?? "";
176 + result.
code === 0 && 178 + output.
includes(marker)
&& 179 + !/^\s*0 for /u.
test(output)
184 + if (
root)
await rm(
root, { force:
true, recursive:
true }).
catch(()
=> {});
188 +function notifyUser(
190 + ctx
: ExtensionContext | undefined,
193 + const notify
= ctx?.hasUI
194 + ? ctx.ui.notify.
bind(ctx.ui)
195 + : (pi
as { ui
?: {
notify?: (message
: string, level
: "warning")
=> void } })
197 + if (
!notify)
return false;
198 + notify(message,
"warning");
156 202 function warnUnavailable(
157 203 status
: RtkStatus,
158 204 pi
: ExtensionAPI,
159 205 ctx
?: ExtensionContext,
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 } })
166 - if (
!notify)
return false;
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");
212 + return notifyUser(pi, ctx,
`rtk disabled: ${suffix}`);
175 215 export const __test
= {
176 217 localFallbackRewrite,
178 219 shouldSkipCommand,
234 + const findPathSupported
= await supportsFindPath(pi);
239 + ? "rtk find -path works: remove the compatibility guard" 240 + : "rtk find rewrites disabled: -path semantics unsupported",
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)
204 - if (rewritten)
event.
input.command
= rewritten;
253 + if (rewritten
&& (findPathSupported
|| !isFindRewrite(rewritten)))
254 + event.
input.command
= rewritten;