|
| 1 | +import path from "node:path"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { createQaBusState } from "./bus-state.js"; |
| 4 | +import { readQaScenarioById } from "./scenario-catalog.js"; |
| 5 | +import { runLoadedScenarioFlow } from "./scenario-flow-runner.test-support.js"; |
| 6 | + |
| 7 | +type RedactionAgentPrompt = { |
| 8 | + sessionKey: string; |
| 9 | + message: string; |
| 10 | + transcriptToolName?: string; |
| 11 | + requireSuccessfulTranscriptToolResult?: boolean; |
| 12 | +}; |
| 13 | + |
| 14 | +const redactionScenarioIds = [ |
| 15 | + "secret-redaction-tool-logs", |
| 16 | + "personal-redaction-no-secret-leak", |
| 17 | +] as const; |
| 18 | + |
| 19 | +const redactionProviderModes = ["mock-openai", "live-frontier"] as const; |
| 20 | + |
| 21 | +const redactionScenarioCases = redactionScenarioIds.flatMap((scenarioId) => |
| 22 | + redactionProviderModes.map((providerMode) => ({ scenarioId, providerMode })), |
| 23 | +); |
| 24 | + |
| 25 | +async function runSecretRedactionScenario( |
| 26 | + scenarioId: (typeof redactionScenarioIds)[number], |
| 27 | + params: { |
| 28 | + providerMode?: (typeof redactionProviderModes)[number]; |
| 29 | + seedPriorInbound?: boolean; |
| 30 | + transcriptEvidence?: "fixture" | "unrelated-file" | "uncorrelated-result" | "missing-secret"; |
| 31 | + } = {}, |
| 32 | +) { |
| 33 | + const scenario = readQaScenarioById(scenarioId); |
| 34 | + const config = scenario.execution.config ?? {}; |
| 35 | + const { fakeSecret, fileName, safeMarker } = config; |
| 36 | + if ( |
| 37 | + typeof fakeSecret !== "string" || |
| 38 | + typeof fileName !== "string" || |
| 39 | + typeof safeMarker !== "string" |
| 40 | + ) { |
| 41 | + throw new Error("secret redaction scenario must declare its fake fixture and safe marker"); |
| 42 | + } |
| 43 | + |
| 44 | + const workspaceDir = "/qa-redaction-workspace"; |
| 45 | + const fixturePath = path.join(workspaceDir, fileName); |
| 46 | + const unrelatedFilePath = path.join(workspaceDir, "README.md"); |
| 47 | + const workspaceFiles = new Map<string, string>([ |
| 48 | + [unrelatedFilePath, "Unrelated workspace documentation without credential material.\n"], |
| 49 | + ]); |
| 50 | + const harnessReadPaths: string[] = []; |
| 51 | + const agentToolReads: Array<{ path: string; contents: string }> = []; |
| 52 | + const outboundWaitCursors: number[] = []; |
| 53 | + const gatewayHistoryRequests: Array<{ |
| 54 | + method: string; |
| 55 | + params: { sessionKey: string; limit: number; maxChars: number }; |
| 56 | + }> = []; |
| 57 | + const persistedMessages: Array<Record<string, unknown>> = []; |
| 58 | + const state = createQaBusState(); |
| 59 | + let agentPrompt: RedactionAgentPrompt | undefined; |
| 60 | + const providerMode = params.providerMode ?? "mock-openai"; |
| 61 | + |
| 62 | + const readWorkspaceFixture = (filePath: string) => { |
| 63 | + const contents = workspaceFiles.get(filePath); |
| 64 | + if (contents === undefined) { |
| 65 | + throw new Error(`missing workspace fixture: ${filePath}`); |
| 66 | + } |
| 67 | + return contents; |
| 68 | + }; |
| 69 | + |
| 70 | + const result = await runLoadedScenarioFlow(scenario.id, { |
| 71 | + state, |
| 72 | + api: { |
| 73 | + env: { |
| 74 | + providerMode, |
| 75 | + gateway: { |
| 76 | + workspaceDir, |
| 77 | + call: async ( |
| 78 | + method: string, |
| 79 | + historyParams: { sessionKey: string; limit: number; maxChars: number }, |
| 80 | + ) => { |
| 81 | + if (method !== "chat.history") { |
| 82 | + throw new Error(`unexpected gateway method: ${method}`); |
| 83 | + } |
| 84 | + gatewayHistoryRequests.push({ method, params: historyParams }); |
| 85 | + return { messages: persistedMessages }; |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + fs: { |
| 90 | + writeFile: async (filePath: string, contents: string) => { |
| 91 | + workspaceFiles.set(filePath, contents); |
| 92 | + }, |
| 93 | + readFile: async (filePath: string) => { |
| 94 | + harnessReadPaths.push(filePath); |
| 95 | + if (params.seedPriorInbound) { |
| 96 | + state.addInboundMessage({ |
| 97 | + accountId: "qa-channel", |
| 98 | + conversation: { id: "qa-operator", kind: "direct" }, |
| 99 | + senderId: "qa-driver", |
| 100 | + text: "earlier inbound fixture preparation", |
| 101 | + }); |
| 102 | + } |
| 103 | + return readWorkspaceFixture(filePath); |
| 104 | + }, |
| 105 | + }, |
| 106 | + path, |
| 107 | + runAgentPrompt: async (_env: unknown, prompt: RedactionAgentPrompt) => { |
| 108 | + agentPrompt = prompt; |
| 109 | + if ( |
| 110 | + prompt.message.includes(fileName) && |
| 111 | + prompt.transcriptToolName === "read" && |
| 112 | + prompt.requireSuccessfulTranscriptToolResult === true |
| 113 | + ) { |
| 114 | + const toolReadPath = |
| 115 | + params.transcriptEvidence === "unrelated-file" ? unrelatedFilePath : fixturePath; |
| 116 | + const toolReadContents = readWorkspaceFixture(toolReadPath); |
| 117 | + agentToolReads.push({ path: toolReadPath, contents: toolReadContents }); |
| 118 | + |
| 119 | + const callId = `qa-redaction-${providerMode}-read`; |
| 120 | + persistedMessages.push( |
| 121 | + { |
| 122 | + role: "assistant", |
| 123 | + content: [ |
| 124 | + providerMode === "mock-openai" |
| 125 | + ? { |
| 126 | + type: "toolCall", |
| 127 | + id: callId, |
| 128 | + name: "read", |
| 129 | + arguments: { path: path.basename(toolReadPath) }, |
| 130 | + } |
| 131 | + : { |
| 132 | + type: "tool_use", |
| 133 | + id: callId, |
| 134 | + name: "read", |
| 135 | + input: { path: toolReadPath }, |
| 136 | + }, |
| 137 | + ], |
| 138 | + }, |
| 139 | + { |
| 140 | + role: "toolResult", |
| 141 | + toolCallId: |
| 142 | + params.transcriptEvidence === "uncorrelated-result" |
| 143 | + ? `${callId}-different` |
| 144 | + : callId, |
| 145 | + toolName: "read", |
| 146 | + isError: false, |
| 147 | + content: [ |
| 148 | + { |
| 149 | + type: providerMode === "mock-openai" ? "text" : "output_text", |
| 150 | + text: |
| 151 | + params.transcriptEvidence === "missing-secret" |
| 152 | + ? "Read completed without returning credential material." |
| 153 | + : toolReadContents, |
| 154 | + }, |
| 155 | + ], |
| 156 | + }, |
| 157 | + { |
| 158 | + role: "assistant", |
| 159 | + content: [{ type: "text", text: safeMarker }], |
| 160 | + }, |
| 161 | + ); |
| 162 | + } |
| 163 | + state.addOutboundMessage({ |
| 164 | + accountId: "qa-channel", |
| 165 | + to: "dm:qa-operator", |
| 166 | + text: safeMarker, |
| 167 | + }); |
| 168 | + }, |
| 169 | + waitForOutboundMessage: async ( |
| 170 | + currentState: ReturnType<typeof createQaBusState>, |
| 171 | + predicate: (message: unknown) => boolean, |
| 172 | + _timeoutMs: number, |
| 173 | + options?: { sinceIndex?: number }, |
| 174 | + ) => { |
| 175 | + const sinceIndex = options?.sinceIndex ?? 0; |
| 176 | + outboundWaitCursors.push(sinceIndex); |
| 177 | + const match = currentState |
| 178 | + .getSnapshot() |
| 179 | + .messages.filter((message) => message.direction === "outbound") |
| 180 | + .slice(sinceIndex) |
| 181 | + .find((message) => predicate(message)); |
| 182 | + if (!match) { |
| 183 | + throw new Error(`no outbound reply after outbound cursor ${sinceIndex}`); |
| 184 | + } |
| 185 | + return match; |
| 186 | + }, |
| 187 | + }, |
| 188 | + }); |
| 189 | + |
| 190 | + return { |
| 191 | + agentPrompt, |
| 192 | + agentToolReads, |
| 193 | + fakeSecret, |
| 194 | + fileName, |
| 195 | + fixturePath, |
| 196 | + gatewayHistoryRequests, |
| 197 | + harnessReadPaths, |
| 198 | + outboundWaitCursors, |
| 199 | + result, |
| 200 | + state, |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +describe("secret redaction scenario proof", () => { |
| 205 | + it.each(redactionScenarioIds)( |
| 206 | + "requires %s to successfully read the fake secret before proving safe delivery", |
| 207 | + async (scenarioId) => { |
| 208 | + const proof = await runSecretRedactionScenario(scenarioId); |
| 209 | + |
| 210 | + expect(proof.result.status).toBe("pass"); |
| 211 | + expect(proof.harnessReadPaths).toEqual([proof.fixturePath]); |
| 212 | + expect(proof.agentPrompt).toMatchObject({ |
| 213 | + transcriptToolName: "read", |
| 214 | + requireSuccessfulTranscriptToolResult: true, |
| 215 | + }); |
| 216 | + expect(proof.agentPrompt?.message).toContain(proof.fileName); |
| 217 | + expect(proof.agentToolReads).toEqual([ |
| 218 | + { path: proof.fixturePath, contents: expect.stringContaining(proof.fakeSecret) }, |
| 219 | + ]); |
| 220 | + }, |
| 221 | + ); |
| 222 | + |
| 223 | + it.each(redactionScenarioCases)( |
| 224 | + "requires $scenarioId to verify the correlated fixture read from $providerMode chat history", |
| 225 | + async ({ scenarioId, providerMode }) => { |
| 226 | + const proof = await runSecretRedactionScenario(scenarioId, { providerMode }); |
| 227 | + |
| 228 | + expect(proof.result.status).toBe("pass"); |
| 229 | + expect(proof.gatewayHistoryRequests).toEqual([ |
| 230 | + { |
| 231 | + method: "chat.history", |
| 232 | + params: { |
| 233 | + sessionKey: proof.agentPrompt?.sessionKey, |
| 234 | + limit: 100, |
| 235 | + maxChars: 131072, |
| 236 | + }, |
| 237 | + }, |
| 238 | + ]); |
| 239 | + expect(proof.agentToolReads).toHaveLength(1); |
| 240 | + expect(proof.agentToolReads[0]?.path).toBe(proof.fixturePath); |
| 241 | + expect(proof.agentToolReads[0]?.contents.includes(proof.fakeSecret)).toBe(true); |
| 242 | + expect(JSON.stringify(proof.result)).not.toContain(proof.fakeSecret); |
| 243 | + expect( |
| 244 | + proof.state |
| 245 | + .getSnapshot() |
| 246 | + .messages.filter((message) => message.direction === "outbound") |
| 247 | + .some((message) => message.text.includes(proof.fakeSecret)), |
| 248 | + ).toBe(false); |
| 249 | + }, |
| 250 | + ); |
| 251 | + |
| 252 | + it.each(redactionScenarioCases)( |
| 253 | + "rejects a successful unrelated read in $scenarioId under $providerMode", |
| 254 | + async ({ scenarioId, providerMode }) => { |
| 255 | + await expect( |
| 256 | + runSecretRedactionScenario(scenarioId, { |
| 257 | + providerMode, |
| 258 | + transcriptEvidence: "unrelated-file", |
| 259 | + }), |
| 260 | + ).rejects.toThrow("successful persisted read did not target the fake secret fixture"); |
| 261 | + }, |
| 262 | + ); |
| 263 | + |
| 264 | + it.each(redactionScenarioCases)( |
| 265 | + "rejects an uncorrelated fixture result in $scenarioId under $providerMode", |
| 266 | + async ({ scenarioId, providerMode }) => { |
| 267 | + await expect( |
| 268 | + runSecretRedactionScenario(scenarioId, { |
| 269 | + providerMode, |
| 270 | + transcriptEvidence: "uncorrelated-result", |
| 271 | + }), |
| 272 | + ).rejects.toThrow("successful persisted read did not target the fake secret fixture"); |
| 273 | + }, |
| 274 | + ); |
| 275 | + |
| 276 | + it.each(redactionScenarioCases)( |
| 277 | + "rejects a fixture result without secret material in $scenarioId under $providerMode", |
| 278 | + async ({ scenarioId, providerMode }) => { |
| 279 | + await expect( |
| 280 | + runSecretRedactionScenario(scenarioId, { |
| 281 | + providerMode, |
| 282 | + transcriptEvidence: "missing-secret", |
| 283 | + }), |
| 284 | + ).rejects.toThrow("successful persisted read did not target the fake secret fixture"); |
| 285 | + }, |
| 286 | + ); |
| 287 | + |
| 288 | + it.each(redactionScenarioIds)( |
| 289 | + "%s uses an outbound-only cursor when earlier inbound messages remain on the QA bus", |
| 290 | + async (scenarioId) => { |
| 291 | + const proof = await runSecretRedactionScenario(scenarioId, { seedPriorInbound: true }); |
| 292 | + |
| 293 | + expect(proof.result.status).toBe("pass"); |
| 294 | + expect(proof.outboundWaitCursors).toEqual([0]); |
| 295 | + }, |
| 296 | + ); |
| 297 | +}); |
0 commit comments