|
| 1 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 5 | +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; |
| 6 | +import { afterEach, describe, expect, it } from "vitest"; |
| 7 | +import { createMinerMcpServer } from "../../packages/gittensory-miner/bin/gittensory-miner-mcp.js"; |
| 8 | +import { initGovernorLedger } from "../../packages/gittensory-miner/lib/governor-ledger.js"; |
| 9 | + |
| 10 | +// gittensory_miner_get_governor_decisions (#5159). Driven against a REAL temp governor ledger (not a fake) so the |
| 11 | +// redaction assertion exercises the actual explicit-named-column SQL — it must fail if a future edit widens the |
| 12 | +// SELECT to include payload_json. |
| 13 | + |
| 14 | +type Content = { content: Array<{ type: string; text?: string }> }; |
| 15 | +type GovernorLedgerHandle = ReturnType<typeof initGovernorLedger>; |
| 16 | + |
| 17 | +const roots: string[] = []; |
| 18 | +function tempGovernorLedger(): GovernorLedgerHandle { |
| 19 | + const root = mkdtempSync(join(tmpdir(), "gittensory-miner-mcp-governor-")); |
| 20 | + roots.push(root); |
| 21 | + return initGovernorLedger(join(root, "governor-ledger.sqlite3")); |
| 22 | +} |
| 23 | +afterEach(() => { |
| 24 | + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); |
| 25 | +}); |
| 26 | + |
| 27 | +function toolText(result: Content): string { |
| 28 | + const first = result.content[0]; |
| 29 | + if (!first || first.type !== "text" || typeof first.text !== "string") { |
| 30 | + throw new Error("expected a single text content block"); |
| 31 | + } |
| 32 | + return first.text; |
| 33 | +} |
| 34 | + |
| 35 | +async function callGovernorDecisions( |
| 36 | + ledger: GovernorLedgerHandle, |
| 37 | + args: Record<string, unknown> = {}, |
| 38 | +): Promise<unknown> { |
| 39 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 40 | + const client = new Client({ name: "miner-mcp-governor-test", version: "0.0.0" }); |
| 41 | + await Promise.all([ |
| 42 | + createMinerMcpServer({ initGovernorLedger: () => ledger }).connect(serverTransport), |
| 43 | + client.connect(clientTransport), |
| 44 | + ]); |
| 45 | + const result = (await client.callTool({ |
| 46 | + name: "gittensory_miner_get_governor_decisions", |
| 47 | + arguments: args, |
| 48 | + })) as Content; |
| 49 | + return JSON.parse(toolText(result)); |
| 50 | +} |
| 51 | + |
| 52 | +describe("gittensory_miner_get_governor_decisions (#5159)", () => { |
| 53 | + it("projects the decision columns and NEVER leaks payload / reputation / budget (redaction by construction)", async () => { |
| 54 | + const ledger = tempGovernorLedger(); |
| 55 | + ledger.appendGovernorEvent({ |
| 56 | + eventType: "denied", |
| 57 | + repoFullName: "acme/api", |
| 58 | + actionClass: "write", |
| 59 | + decision: "block", |
| 60 | + reason: "house rule violation", |
| 61 | + // Sensitive state that #5134 is expanding into payload_json — must never surface through this read tool. |
| 62 | + payload: { reputation: 0.2, self_plagiarism: true, budget: { remaining: 0 }, note: "secretish" }, |
| 63 | + }); |
| 64 | + |
| 65 | + const decisions = (await callGovernorDecisions(ledger)) as Array<Record<string, unknown>>; |
| 66 | + expect(decisions).toHaveLength(1); |
| 67 | + expect(decisions[0]).toEqual({ |
| 68 | + id: expect.any(Number), |
| 69 | + ts: expect.any(String), |
| 70 | + eventType: "denied", |
| 71 | + repoFullName: "acme/api", |
| 72 | + actionClass: "write", |
| 73 | + decision: "block", |
| 74 | + reason: "house rule violation", |
| 75 | + }); |
| 76 | + for (const forbidden of ["payload", "payload_json", "reputation", "self_plagiarism", "selfPlagiarism", "budget"]) { |
| 77 | + expect(decisions[0]).not.toHaveProperty(forbidden); |
| 78 | + } |
| 79 | + // Belt-and-suspenders: the sensitive payload keys/values never appear anywhere in the serialized response. |
| 80 | + // (Only tokens that cannot legitimately occur in a projected column — "budget" is skipped because it may |
| 81 | + // appear in a decision `reason`; the not.toHaveProperty checks above already guard the payload key itself.) |
| 82 | + const serialized = JSON.stringify(decisions); |
| 83 | + for (const forbidden of ["reputation", "self_plagiarism", "secretish"]) { |
| 84 | + expect(serialized).not.toContain(forbidden); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + it("filters by repoFullName", async () => { |
| 89 | + const ledger = tempGovernorLedger(); |
| 90 | + for (const repo of ["acme/api", "acme/web"]) { |
| 91 | + ledger.appendGovernorEvent({ |
| 92 | + eventType: "allowed", |
| 93 | + repoFullName: repo, |
| 94 | + actionClass: "analyze", |
| 95 | + decision: "allow", |
| 96 | + reason: "within budget", |
| 97 | + }); |
| 98 | + } |
| 99 | + const decisions = (await callGovernorDecisions(ledger, { repoFullName: "acme/web" })) as Array<{ |
| 100 | + repoFullName: string; |
| 101 | + }>; |
| 102 | + expect(decisions.map((decision) => decision.repoFullName)).toEqual(["acme/web"]); |
| 103 | + }); |
| 104 | + |
| 105 | + it("returns an empty array when nothing matches", async () => { |
| 106 | + const ledger = tempGovernorLedger(); |
| 107 | + expect(await callGovernorDecisions(ledger, { repoFullName: "none/here" })).toEqual([]); |
| 108 | + }); |
| 109 | +}); |
0 commit comments