repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

Deferred RTK probes until background session startup or first bash call; Win32 focused blocking vanished, RTK behavior tests and checks passed

2436ddb388b8a3c6189a3891c97cd71ae8ca8ace

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

SSH signature · not checked

Browse filesCompare with HEAD

Result: {"status":"keep","rtk_tax_ms":-93.808,"elapsed_s":433.6,"win32_start_reload_ms":12788.385,"win32_extension_tax_ms":3934.926,"win32_startup_ms":10434.021,"win32_reload_ms":2354.364,"wsl_start_reload_ms":2210.808,"wsl_extension_tax_ms":1056.048,"wsl_rtk_tax_ms":63.529}

Changed files (2)

extensions/rtk/__tests__/rtk.test.ts

Modified
38 38 ? { code: 0, stdout: "rtk 0.42.4" }
39 39 : { code, stdout },
41 - await rtkExtension(fx.pi as never, fx.ctx as never);
41 + rtkExtension(fx.pi as never);
42 42 return fx;
48 48 toolName = "bash",
50 50 const event = { toolName, input: { command } };
51 - await fx.handlers.tool_call?.[0]?.(event, { signal: AbortSignal.abort() });
51 + await fx.handlers.tool_call?.[0]?.(event, {
52 + ...fx.ctx,
53 + signal: AbortSignal.abort(),
54 + });
52 55 return event.input.command;
102 105 ).toBeUndefined();
105 - test("warns once and registers nothing when rtk is missing", async () => {
108 + test("warns once and leaves commands unchanged when rtk is missing", async () => {
106 109 const fx = fakePi(async () => ({ code: 127, stdout: "" }));
107 - await rtkExtension(fx.pi as never, fx.ctx as never);
108 - expect(fx.handlers.tool_call).toBeUndefined();
109 - expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]);
110 - });
112 - test("warns through pi ui when no load ctx is passed", async () => {
113 - const fx = fakePi(async () => ({ code: 127, stdout: "" }));
114 - await rtkExtension(fx.pi as never);
115 - expect(fx.handlers.tool_call).toBeUndefined();
116 - expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]);
117 - });
119 - test("warns on startup hook when load ui is unavailable", async () => {
120 - const fx = fakePi(async () => ({ code: 127, stdout: "" }));
121 - const pi = { ...fx.pi, ui: undefined };
122 - await rtkExtension(pi as never);
123 - expect(fx.handlers.tool_call).toBeUndefined();
110 + rtkExtension(fx.pi as never);
111 + expect(fx.handlers.tool_call).toHaveLength(1);
124 112 expect(fx.notes).toEqual([]);
125 - fx.handlers.before_agent_start?.[0]?.({}, fx.ctx);
126 - expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]);
127 - fx.handlers.session_start?.[0]?.({}, fx.ctx);
113 + expect(await emit(fx as never, "ps aux")).toBe("ps aux");
128 114 expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]);
131 - test("warns once and registers nothing when rtk is old", async () => {
117 + test("registers without probing during extension load", () => {
118 + const fx = fakePi(async () => ({ code: 127, stdout: "" }));
119 + rtkExtension(fx.pi as never);
120 + expect(fx.pi.exec).not.toHaveBeenCalled();
121 + expect(fx.notes).toEqual([]);
122 + });
124 + test("starts the availability probe after session_start returns", async () => {
125 + const fx = fakePi(async () => ({ code: 127, stdout: "" }));
126 + rtkExtension(fx.pi as never);
127 + const result = fx.handlers.session_start?.[0]?.({}, fx.ctx);
128 + expect(result).toBeUndefined();
129 + expect(fx.pi.exec).not.toHaveBeenCalled();
130 + await vi.waitFor(() => expect(fx.pi.exec).toHaveBeenCalled());
131 + expect(fx.notes).toEqual([["rtk disabled: not found on PATH", "warning"]]);
132 + });
134 + test("warns once and leaves commands unchanged when rtk is old", async () => {
132 135 const fx = fakePi(async () => ({ code: 0, stdout: "rtk 0.22.9" }));
133 - await rtkExtension(fx.pi as never, fx.ctx as never);
134 - expect(fx.handlers.tool_call).toBeUndefined();
136 + rtkExtension(fx.pi as never);
137 + expect(await emit(fx as never, "ps aux")).toBe("ps aux");
135 138 expect(fx.notes).toEqual([
136 139 ["rtk disabled: too old (rtk 0.22.9); need >= 0.23.0", "warning"],
140 143 test("registers only tool_call when rtk is available", async () => {
141 144 const fx = await loadWithRewrite(1);
142 145 expect(fx.pi.on).toHaveBeenCalledWith("tool_call", expect.any(Function));
146 + await emit(fx, "git status");
143 147 expect(fx.notes).toEqual([
144 148 ["rtk find rewrites disabled: -path semantics unsupported", "warning"],

extensions/rtk/index.ts

Modified
204 204 ctx: ExtensionContext | undefined,
205 205 message: string,
206 206 ): boolean {
207 - const notify = ctx?.hasUI
208 - ? ctx.ui.notify.bind(ctx.ui)
209 - : (pi as { ui?: { notify?: (message: string, level: "warning") => void } })
210 - .ui?.notify;
211 - if (!notify) return false;
212 - notify(message, "warning");
213 - return true;
207 + try {
208 + const notify = ctx?.hasUI
209 + ? ctx.ui.notify.bind(ctx.ui)
210 + : (
211 + pi as {
212 + ui?: { notify?: (message: string, level: "warning") => void };
214 + ).ui?.notify;
215 + if (!notify) return false;
216 + notify(message, "warning");
217 + return true;
218 + } catch (error) {
219 + if (
220 + error instanceof Error &&
221 + error.message.includes("ctx is stale after session replacement or reload")
223 + return false;
224 + throw error;
216 228 function warnUnavailable(
234 246 splitCommandPrefix,
237 -export default async function rtk(pi: ExtensionAPI, ctx?: ExtensionContext) {
238 - const status = await rtkStatus(pi);
239 - if (!status.ok) {
240 - let warned = warnUnavailable(status, pi, ctx);
241 - const warnOnce = (_event: unknown, hookCtx: ExtensionContext) => {
242 - if (!warned) warned = warnUnavailable(status, pi, hookCtx);
243 - };
244 - pi.on("session_start", warnOnce);
245 - pi.on("before_agent_start", warnOnce);
246 - return;
247 - }
249 +export default function rtk(pi: ExtensionAPI) {
250 + let generation = 0;
251 + let announced = false;
252 + let initialization:
253 + | {
254 + generation: number;
255 + promise: Promise<{
256 + status: RtkStatus;
257 + findPathSupported: boolean;
258 + }>;
260 + | undefined;
249 - const findPathSupported = await supportsFindPath(pi);
250 - notifyUser(
251 - pi,
252 - ctx,
253 - findPathSupported
254 - ? "rtk find -path works: remove the compatibility guard"
255 - : "rtk find rewrites disabled: -path semantics unsupported",
256 - );
262 + const ensureInitialized = async (ctx: ExtensionContext) => {
263 + if (initialization?.generation !== generation) {
264 + const ticket = generation;
265 + const promise = (async () => {
266 + const status = await rtkStatus(pi);
267 + return {
268 + status,
269 + findPathSupported: status.ok ? await supportsFindPath(pi) : false,
270 + };
271 + })();
272 + initialization = { generation: ticket, promise };
275 + const ticket = initialization.generation;
276 + const support = await initialization.promise;
277 + if (ticket !== generation) return undefined;
278 + if (!announced) {
279 + announced = support.status.ok
280 + ? notifyUser(
281 + pi,
282 + ctx,
283 + support.findPathSupported
284 + ? "rtk find -path works: remove the compatibility guard"
285 + : "rtk find rewrites disabled: -path semantics unsupported",
287 + : warnUnavailable(support.status, pi, ctx);
289 + return support;
290 + };
292 + pi.on("session_start", (_event, ctx) => {
293 + const ticket = ++generation;
294 + announced = false;
295 + initialization = undefined;
296 + const timer = setTimeout(() => {
297 + if (ticket === generation)
298 + void ensureInitialized(ctx).catch((error) =>
299 + console.error("rtk initialization failed:", error),
300 + );
301 + }, 0);
302 + timer.unref?.();
303 + });
304 + pi.on("session_shutdown", () => {
305 + generation++;
306 + announced = false;
307 + initialization = undefined;
308 + });
257 309 pi.on("tool_call", async (event, ctx) => {
258 310 if (!isToolCallEventType("bash", event)) return;
311 + const support = await ensureInitialized(ctx);
312 + if (!support?.status.ok) return;
259 313 const framed = splitCommandPrefix(event.input.command);
260 314 if (shouldSkipCommand(framed.command)) return;
261 315 const outcome = await rewriteCommand(pi, framed.command, ctx.signal);
265 319 : outcome.kind === "fallback"
266 320 ? localFallbackRewrite(framed.command)
267 321 : undefined;
268 - if (rewritten && (findPathSupported || !isFindRewrite(rewritten)))
322 + if (
323 + rewritten &&
324 + (support.findPathSupported || !isFindRewrite(rewritten))
269 326 event.input.command = framed.prefix + rewritten;