pi-ext
bugabingas pi extensions
owned by bugabinga
feat(leaks): add safe status and commands
fca005ba27716b607e140513d909c4ad4c3321d2
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (2)
extensions/leaks/__tests__/ux.test.ts
Addednew file mode 100644 1 +import { afterEach, describe, expect, it }
from "vitest";
2 +import { createTestSession, type TestSession }
from "../../../test/harness";
3 +import path
from "node:path";
6 + return path.
resolve(
import.
meta.dirname,
"../index.ts");
9 +const secret
= "sk-or-v1-" + "e".
repeat(
64);
11 +describe(
"leaks UX and commands", ()
=> {
12 + let t
: TestSession | undefined;
18 + it(
"notifies once for a redacting provider turn and not for clean turns",
async ()
=> {
19 + t
= await createTestSession({ extensions: [
extPath()] });
20 + await t.session.extensionRunner.
emitBeforeProviderRequest({ text: secret });
21 + await t.session.extensionRunner.
emitBeforeProviderRequest({ text:
"ordinary metadata" });
23 + const notices
= t.events.
uiCallsFor(
"notify").
map((call)
=> String(call.args[
0]));
24 + expect(notices.
filter((notice)
=> notice.
includes(
"leaks redacted"))).
toHaveLength(
1);
27 + it(
"status reports required safe fields",
async ()
=> {
28 + t
= await createTestSession({ extensions: [
extPath()] });
29 + await t.session.extensionRunner.
emitBeforeProviderRequest({ text: secret });
30 + const command
= t.session.extensionRunner.extensions[
0].commands.
get(
"leaks");
31 + await command.
handler(
"status", t.session.extensionRunner.
createContext());
33 + const status
= t.events.
uiCallsFor(
"notify").
map((call)
=> String(call.args[
0])).
at(
-1)
?? "";
34 + for (
const field
of [
42 + "last provider redactions:",
43 + "total provider redactions:",
46 + ])
expect(status).
toContain(field);
47 + expect(status).not.
toContain(secret);
50 + it(
"reload test and clear commands are safe",
async ()
=> {
51 + t
= await createTestSession({ extensions: [
extPath()], mockUI: { confirm:
true } });
52 + const command
= t.session.extensionRunner.extensions[
0].commands.
get(
"leaks");
53 + const ctx
= t.session.extensionRunner.
createContext();
55 + await command.
handler(
"test " + secret, ctx);
56 + await command.
handler(
"reload", ctx);
57 + await t.session.extensionRunner.
emitBeforeProviderRequest({ text: secret });
58 + await command.
handler(
"clear", ctx);
59 + await command.
handler(
"status", ctx);
61 + const notices
= t.events.
uiCallsFor(
"notify").
map((call)
=> String(call.args[
0]));
62 + expect(notices.
join(
"\n")).not.
toContain(secret);
63 + expect(notices.
some((notice)
=> notice.
includes(
"leaks reloaded"))).
toBe(
true);
64 + expect(notices.
some((notice)
=> notice.
includes(
"leaks cleared"))).
toBe(
true);
65 + expect(notices.
at(
-1)).
toContain(
"mappings: 0");
68 + it(
"clear respects cancellation",
async ()
=> {
69 + t
= await createTestSession({ extensions: [
extPath()], mockUI: { confirm:
false } });
70 + await t.session.extensionRunner.
emitBeforeProviderRequest({ text: secret });
71 + const command
= t.session.extensionRunner.extensions[
0].commands.
get(
"leaks");
72 + await command.
handler(
"clear", t.session.extensionRunner.
createContext());
73 + await command.
handler(
"status", t.session.extensionRunner.
createContext());
75 + const notices
= t.events.
uiCallsFor(
"notify").
map((call)
=> String(call.args[
0]));
76 + expect(notices.
some((notice)
=> notice.
includes(
"clear cancelled"))).
toBe(
true);
77 + expect(notices.
at(
-1)).
toContain(
"mappings: 1");
extensions/leaks/src/pi.ts
Modified19 19 pi.
on(
"before_provider_request",
async (event, ctx)
=> {
20 20 const result
= engine.
redactDeep(
event.payload,
"provider");
21 - if (ctx.hasUI) ctx.ui.
setStatus(
"leaks",
`leaks on | last ${result.count}`);
22 + ctx.ui.
setStatus(
"leaks",
`leaks on | last ${result.count}`);
23 + if (result.count
> 0) ctx.ui.
notify(
`leaks redacted ${result.count} provider value(s)`,
"info");
22 25 return result.
value;
48 51 if (command
=== "clear") {
53 + const confirmed
= await ctx.ui.
confirm(
"Clear leaks mappings?",
"Old placeholders become unrestorable.");
55 + ctx.ui.
notify(
"clear cancelled",
"info");
50 60 ctx.ui.
notify(
"leaks cleared",
"info");
77 87 `built-in rules: ${stats.builtInRuleCount}`,
78 88 `custom regex: ${stats.customRegexCount}`,
89 + `regex warnings: ${stats.regexWarningCount}`,
79 90 `invalid regex: ${stats.invalidRegexCount}`,
80 91 `learned: ${stats.learnedCount}`,
81 92 `mappings: ${stats.mappingCount}`,
82 93 `last provider redactions: ${stats.lastProviderRedactionCount}`,
83 94 `total provider redactions: ${stats.totalProviderRedactionCount}`,
95 + `recent labels: ${stats.recentLabels.join(", ")}`,
96 + `latest tags: ${stats.latestTags.join(", ")}`,