Skip to content
Merged
26 changes: 24 additions & 2 deletions plugin/scripts/_capture.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,29 @@ function resolveDataDir(options = {}) {
}
//#endregion
//#region src/functions/privacy.ts
const PRIVATE_TAG_RE = /<private>[\s\S]*?<\/private>/gi;
const PRIVATE_TAG_TOKEN_RE = /<private(?=\s|>)[^>]*>|<\/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("</");
if (depth === 0) {
if (closing) continue;
out += input.slice(cursor, match.index);
depth = 1;
} else if (closing) {
depth -= 1;
if (depth === 0) {
out += "[REDACTED]";
cursor = match.index + match[0].length;
}
} else depth += 1;
}
if (depth > 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,
Expand All @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions plugin/scripts/subagent-stop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand All @@ -86,8 +88,9 @@ async function main() {
};
captureObservation(withEventId(body, {}, {
...body.data,
last_message: redactedReply,
Comment thread
rohitg00 marked this conversation as resolved.
Resolved
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));
Expand Down
29 changes: 27 additions & 2 deletions src/functions/privacy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import type { IIIClient } from "iii-sdk";

const PRIVATE_TAG_RE = /<private>[\s\S]*?<\/private>/gi;
const PRIVATE_TAG_TOKEN_RE = /<private(?=\s|>)[^>]*>|<\/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("</");
if (depth === 0) {
if (closing) continue;
out += input.slice(cursor, match.index);
depth = 1;
} else if (closing) {
depth -= 1;
if (depth === 0) {
out += "[REDACTED]";
cursor = match.index + match[0].length;
}
} else {
depth += 1;
}
}
if (depth > 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,
Expand All @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const EXIT_CAP_MS = 3500;
const END_CAP_MS = 1500;
const REPLY_MAX_CHARS = 4000;

// stripPrivateData only matches a closed <private> 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 };
Expand Down
17 changes: 8 additions & 9 deletions src/hooks/subagent-stop.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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) },
Comment thread
rohitg00 marked this conversation as resolved.
Resolved
),
OBSERVE_TIMEOUT_MS,
);
Expand Down
50 changes: 50 additions & 0 deletions test/copilot-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "<private>TOPSECRET</private>" + "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",
Expand Down
15 changes: 15 additions & 0 deletions test/privacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ describe("stripPrivateData", () => {
);
});

it("strips an unclosed private tag through the end of the text", () => {
expect(stripPrivateData("keep <private>secret to the end")).toBe("keep [REDACTED]");
});

it("strips a nested private block as one block", () => {
expect(stripPrivateData("a <private>outer <private>inner</private> outer-secret</private> b")).toBe("a [REDACTED] b");
expect(stripPrivateData("a </private> b <private>x</private> c")).toBe("a </private> b [REDACTED] c");
});

it("strips a private tag that carries attributes", () => {
expect(stripPrivateData('a <private reason="x">secret</private> b')).toBe("a [REDACTED] b");
expect(stripPrivateData("a <privateKey>value</privateKey> b")).toBe("a <privateKey>value</privateKey> b");
expect(stripPrivateData("a <private-notes>value</private-notes> b")).toBe("a <private-notes>value</private-notes> b");
});

it("strips private tags case-insensitive", () => {
expect(stripPrivateData("<Private>data</Private>")).toBe("[REDACTED]");
});
Expand Down
Loading