diff --git a/plugin/scripts/_capture.mjs b/plugin/scripts/_capture.mjs index 1068696f48..2799eaf7da 100644 --- a/plugin/scripts/_capture.mjs +++ b/plugin/scripts/_capture.mjs @@ -282,7 +282,29 @@ function resolveDataDir(options = {}) { } //#endregion //#region src/functions/privacy.ts -const PRIVATE_TAG_RE = /[\s\S]*?<\/private>/gi; +const PRIVATE_TAG_TOKEN_RE = /)[^>]*>|<\/private\s*>/gi; +function redactPrivateBlocks(input) { + const tokens = new RegExp(PRIVATE_TAG_TOKEN_RE.source, PRIVATE_TAG_TOKEN_RE.flags); + let out = ""; + let cursor = 0; + let depth = 0; + for (let match = tokens.exec(input); match; match = tokens.exec(input)) { + const closing = match[0].startsWith(" 0) return `${out}[REDACTED]`; + return out + input.slice(cursor); +} const SECRET_PATTERN_SOURCES = [ /(?:api[_-]?key|secret|token|password|credential|auth)[\s]*[=:]\s*["']?[A-Za-z0-9_\-/.+]{20,}["']?/gi, /Bearer\s+[A-Za-z0-9._\-+/=]{20,}/gi, @@ -302,7 +324,7 @@ const SECRET_PATTERN_SOURCES = [ const PRIVATE_KEY_BLOCK_RE = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g; const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; function stripPrivateData(input) { - let result = input.replace(PRIVATE_TAG_RE, "[REDACTED]"); + let result = redactPrivateBlocks(input); result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { diff --git a/plugin/scripts/subagent-stop.mjs b/plugin/scripts/subagent-stop.mjs index 77bd223db3..a01bff6cd0 100755 --- a/plugin/scripts/subagent-stop.mjs +++ b/plugin/scripts/subagent-stop.mjs @@ -2,7 +2,7 @@ import { execSync } from "node:child_process"; import { realpathSync } from "node:fs"; import { basename, dirname, resolve } from "node:path"; -import { captureObservation, isDrainChild, runDrainChild, withEventId } from "./_capture.mjs"; +import { captureObservation, isDrainChild, runDrainChild, stripPrivateData, withEventId } from "./_capture.mjs"; //#region src/hooks/_capture-filter.ts function truncateText(value, max) { if (value.length <= max) return value; @@ -68,9 +68,11 @@ async function main() { if (!data || typeof data !== "object") return; if (isSdkChildContext(data)) return; const sessionId = data.session_id || data.sessionId || data.conversation_id || "unknown"; - const agentId = data.agent_id || data.agentName; - const agentType = data.agent_type || data.agentDisplayName || data.agentName; - const lastMsg = typeof data.last_assistant_message === "string" ? truncateText(data.last_assistant_message, 4e3) : ""; + const agentId = data.agent_id || data.agentId || data.agentName; + const agentType = data.agent_type || data.agentType || data.agentDisplayName || data.agentName; + const reply = data.last_assistant_message ?? data.response; + const redactedReply = typeof reply === "string" ? stripPrivateData(reply) : ""; + const lastMsg = truncateText(redactedReply, 4e3); const cwd = hookCwd(data) || process.cwd(); const body = { hookType: "subagent_stop", @@ -86,8 +88,9 @@ async function main() { }; captureObservation(withEventId(body, {}, { ...body.data, + last_message: redactedReply, stop_hook_active: data.stop_hook_active === true - }, { stable: Boolean(data.agent_id) }), OBSERVE_TIMEOUT_MS); + }, { stable: Boolean(data.agent_id || data.agentId) }), OBSERVE_TIMEOUT_MS); setTimeout(() => process.exit(0), EXIT_CAP_MS).unref(); } main().catch(() => process.exit(0)); diff --git a/src/functions/privacy.ts b/src/functions/privacy.ts index d20cb79254..e4f4a13d68 100644 --- a/src/functions/privacy.ts +++ b/src/functions/privacy.ts @@ -1,6 +1,31 @@ import type { IIIClient } from "iii-sdk"; -const PRIVATE_TAG_RE = /[\s\S]*?<\/private>/gi; +const PRIVATE_TAG_TOKEN_RE = /)[^>]*>|<\/private\s*>/gi; + +function redactPrivateBlocks(input: string): string { + const tokens = new RegExp(PRIVATE_TAG_TOKEN_RE.source, PRIVATE_TAG_TOKEN_RE.flags); + let out = ""; + let cursor = 0; + let depth = 0; + for (let match = tokens.exec(input); match; match = tokens.exec(input)) { + const closing = match[0].startsWith(" 0) return `${out}[REDACTED]`; + return out + input.slice(cursor); +} const SECRET_PATTERN_SOURCES = [ /(?:api[_-]?key|secret|token|password|credential|auth)[\s]*[=:]\s*["']?[A-Za-z0-9_\-/.+]{20,}["']?/gi, @@ -26,7 +51,7 @@ const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; export function stripPrivateData(input: string): string { - let result = input.replace(PRIVATE_TAG_RE, "[REDACTED]"); + let result = redactPrivateBlocks(input); result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { diff --git a/src/hooks/stop.ts b/src/hooks/stop.ts index 60d23a3ac6..5ecf93a794 100644 --- a/src/hooks/stop.ts +++ b/src/hooks/stop.ts @@ -16,8 +16,6 @@ const EXIT_CAP_MS = 3500; const END_CAP_MS = 1500; const REPLY_MAX_CHARS = 4000; -// stripPrivateData only matches a closed block, so the whole reply -// has to be redacted before anything shortens it. function boundReply(reply: string): { text: string; bytes?: number } { const safe = stripPrivateData(reply); if (safe.length <= REPLY_MAX_CHARS) return { text: safe }; diff --git a/src/hooks/subagent-stop.ts b/src/hooks/subagent-stop.ts index f2e7d248e5..ac70ba6b6b 100644 --- a/src/hooks/subagent-stop.ts +++ b/src/hooks/subagent-stop.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node import { truncateText } from "./_capture-filter.js"; import { resolveProject, hookCwd } from "./_project.js"; -import { captureObservation, isDrainChild, runDrainChild, withEventId } from "./_capture.js"; +import { captureObservation, isDrainChild, runDrainChild, stripPrivateData, withEventId } from "./_capture.js"; function isSdkChildContext(payload: unknown): boolean { if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true; @@ -30,12 +30,11 @@ async function main() { if (isSdkChildContext(data)) return; const sessionId = ((data.session_id || data.sessionId || data.conversation_id) as string) || "unknown"; - const agentId = data.agent_id || data.agentName; - const agentType = data.agent_type || data.agentDisplayName || data.agentName; - const lastMsg = - typeof data.last_assistant_message === "string" - ? truncateText(data.last_assistant_message, 4000) - : ""; + const agentId = data.agent_id || data.agentId || data.agentName; + const agentType = data.agent_type || data.agentType || data.agentDisplayName || data.agentName; + const reply = data.last_assistant_message ?? data.response; + const redactedReply = typeof reply === "string" ? stripPrivateData(reply) : ""; + const lastMsg = truncateText(redactedReply, 4000); const cwd = hookCwd(data) || process.cwd(); @@ -55,8 +54,8 @@ async function main() { withEventId( body, {}, - { ...body.data, stop_hook_active: data.stop_hook_active === true }, - { stable: Boolean(data.agent_id) }, + { ...body.data, last_message: redactedReply, stop_hook_active: data.stop_hook_active === true }, + { stable: Boolean(data.agent_id || data.agentId) }, ), OBSERVE_TIMEOUT_MS, ); diff --git a/test/copilot-plugin.test.ts b/test/copilot-plugin.test.ts index 3b1295b48d..3d223f7379 100644 --- a/test/copilot-plugin.test.ts +++ b/test/copilot-plugin.test.ts @@ -389,6 +389,56 @@ describe("Copilot hook scripts", () => { }); }); + it("subagent-stop accepts Copilot camelCase subagent payload", async () => { + const payload = { + sessionId: "copilot-session", + cwd: "C:\\repo", + agentId: "agent-7f3", + agentType: "general-purpose", + agentName: "explore", + agentDisplayName: "Explore", + response: "Found 3 call sites", + stopReason: "end_turn", + }; + const first = await runHook("scripts/subagent-stop.mjs", payload); + const again = await runHook("scripts/subagent-stop.mjs", payload); + const other = await runHook("scripts/subagent-stop.mjs", { ...payload, agentId: "agent-9c1" }); + + expect(first.requests[0]?.path).toBe("/agentmemory/observe"); + expect(first.requests[0]?.body).toMatchObject({ + hookType: "subagent_stop", + sessionId: "copilot-session", + data: { agent_id: "agent-7f3", agent_type: "general-purpose", last_message: "Found 3 call sites" }, + }); + expect(again.requests[0]?.body.eventId).toBe(first.requests[0]?.body.eventId); + expect(other.requests[0]?.body.eventId).not.toBe(first.requests[0]?.body.eventId); + }); + + it("subagent-stop keeps distinct long replies from one agent as separate events", async () => { + const base = { sessionId: "copilot-session", cwd: "C:\\repo", agentId: "agent-7f3" }; + const draft = await runHook("scripts/subagent-stop.mjs", { ...base, response: "A".repeat(4000) + "draft" }); + const done = await runHook("scripts/subagent-stop.mjs", { ...base, response: "A".repeat(4000) + "finished" }); + const retry = await runHook("scripts/subagent-stop.mjs", { ...base, response: "A".repeat(4000) + "finished" }); + + expect(draft.requests[0]?.body.eventId).not.toBe(done.requests[0]?.body.eventId); + expect(retry.requests[0]?.body.eventId).toBe(done.requests[0]?.body.eventId); + expect((done.requests[0]?.body.data as { last_message: string }).last_message).toHaveLength(4000); + }); + + it("subagent-stop redacts a private block that crosses the reply limit", async () => { + const result = await runHook("scripts/subagent-stop.mjs", { + sessionId: "copilot-session", + cwd: "C:\\repo", + agentId: "agent-7f3", + response: "A".repeat(3980) + "TOPSECRET" + "B".repeat(100), + }); + + const sent = (result.requests[0]?.body.data as { last_message: string }).last_message; + expect(sent).not.toContain("TOPSECRET"); + expect(sent).toContain("[REDACTED]"); + expect(sent).toHaveLength(4000); + }); + it("notification accepts Copilot camelCase notificationType", async () => { const result = await runHook("scripts/notification.mjs", { sessionId: "copilot-session", diff --git a/test/privacy.test.ts b/test/privacy.test.ts index 8277045b02..2e5cc1cc42 100644 --- a/test/privacy.test.ts +++ b/test/privacy.test.ts @@ -8,6 +8,21 @@ describe("stripPrivateData", () => { ); }); + it("strips an unclosed private tag through the end of the text", () => { + expect(stripPrivateData("keep secret to the end")).toBe("keep [REDACTED]"); + }); + + it("strips a nested private block as one block", () => { + expect(stripPrivateData("a outer inner outer-secret b")).toBe("a [REDACTED] b"); + expect(stripPrivateData("a b x c")).toBe("a b [REDACTED] c"); + }); + + it("strips a private tag that carries attributes", () => { + expect(stripPrivateData('a secret b')).toBe("a [REDACTED] b"); + expect(stripPrivateData("a value b")).toBe("a value b"); + expect(stripPrivateData("a value b")).toBe("a value b"); + }); + it("strips private tags case-insensitive", () => { expect(stripPrivateData("data")).toBe("[REDACTED]"); });