test(leaks): cover provider redaction
c76522ab0f23157b084910078287b7a4e1df3324
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 1bb29cc21212
bugabingas pi extensions
c76522ab0f23157b084910078287b7a4e1df3324
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+107 / −10 text lines
+13 / −1 text lines
import { createTestSession, type TestSession } from "@marcfargas/pi-test-harness";import { createTestSession, says, when, type TestSession } from "@marcfargas/pi-test-harness"; it("redacts during a mocked agent run", async () => { t = await createTestSession({ extensions: [extPath()], mockTools: {} }); const value = "sk-or-v1-" + "9".repeat(64); await t.run(when(`Decide if this is sensitive: ${value}`, [says("done")])); const command = t.session.extensionRunner.extensions[0].commands.get("leaks"); await command.handler("status", t.session.extensionRunner.createContext()); const notices = t.events.uiCallsFor("notify").map((c) => String(c.args[0])); expect(notices.some((m) => m.includes("mappings: 1"))).toBe(true); });new file mode 100644+86 / −0 text lines
import { describe, expect, it } from "bun:test";import fs from "node:fs";import os from "node:os";import path from "node:path";import { AuthStorage, createAgentSession, DefaultResourceLoader, ModelRegistry, SessionManager, SettingsManager,} from "@earendil-works/pi-coding-agent";import { getModel } from "@earendil-works/pi-ai";const real = process.env.LEAKS_REAL === "1";const d = real ? describe : describe.skip;d("real MiniMax integration", () => { it("redacts provider-bound token before a real MiniMax request", async () => { const temp = fs.mkdtempSync(path.join(os.tmpdir(), "leaks-real-")); const cwd = path.join(temp, "cwd"); const agentDir = path.join(temp, "agent"); fs.mkdirSync(cwd, { recursive: true }); fs.mkdirSync(agentDir, { recursive: true }); const leaksPath = path.resolve(import.meta.dir, "../../index.ts"); const settingsManager = SettingsManager.inMemory({ defaultProvider: "minimax", defaultModel: "MiniMax-M2.7-highspeed", defaultThinkingLevel: "off", leaks: { enabled: true, secretFiles: [] }, }); const loader = new DefaultResourceLoader({ cwd, agentDir, settingsManager, noExtensions: true, noSkills: true, noPromptTemplates: true, noThemes: true, noContextFiles: true, additionalExtensionPaths: [leaksPath], }); await loader.reload(); const authStorage = AuthStorage.create(); const modelRegistry = ModelRegistry.create(authStorage); const model = getModel("minimax", "MiniMax-M2.7-highspeed"); const assistantTexts: string[] = []; const { session, extensionsResult } = await createAgentSession({ cwd, agentDir, authStorage, modelRegistry, model, thinkingLevel: "off", sessionManager: SessionManager.inMemory(), settingsManager, resourceLoader: loader, noTools: "all", }); try { expect(extensionsResult.extensions.map((e) => e.path)).toEqual([leaksPath]); session.subscribe((event) => { if (event.type !== "message_end") return; const msg = event.message; if (msg.role !== "assistant") return; assistantTexts.push(JSON.stringify(msg.content)); }); const fake = "sk-or-v1-" + "1".repeat(64); await session.prompt(`Look only at the bracket content. Does it start with sk-or-v1-? Answer exactly YES or NO. [${fake}]`); await session.agent.waitForIdle(); const answer = assistantTexts.join("\n").toUpperCase(); expect(answer).toContain("NO"); expect(answer).not.toContain("YES"); } finally { session.dispose(); fs.rmSync(temp, { recursive: true, force: true }); } }, 120000);});+5 / −3 text lines
+3 / −6 text lines
const BUILTIN_RULES: Array<Omit<CompiledRule, "re" | "priority"> & { pattern: string; flags?: string }> = [ { id: "openai", pattern: String.raw`\bsk-(?:(?:proj|svcacct|service|admin)-[A-Za-z0-9_-]+|[A-Za-z0-9]+)T3BlbkFJ[A-Za-z0-9_-]+\b` }, if (rule.generic && isGenericReject(captured)) continue; if (rule.generic && isGenericReject(captured, full)) continue;function isGenericReject(s: string): boolean {function isGenericReject(s: string, context = ""): boolean { if (/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(v)) return true;