#1 Persistent memory for AI coding agents based on real-world benchmarks
TypeScript agentmemoryagentsaiclaudeclaudecodecodexcopilotcursorgenaiharnesshermesmemoryompopenclawopencodepi
fix(hooks): stop collapsing distinct subagent, task and notification events (#1513)
Dmitrii Zhukov72e126676ba3
diff --git a/plugin/scripts/notification.mjs b/plugin/scripts/notification.mjsindex 423b9965..9148e40b 100755--- a/plugin/scripts/notification.mjs+++ b/plugin/scripts/notification.mjs@@ -69,7 +69,8 @@ async function main() {].find((v) => typeof v === "string" && v.length > 0);const sessionId = typeof rawSessionId === "string" ? rawSessionId : "unknown";const cwd = hookCwd(data) || process.cwd();- captureObservation(withEventId({+ const promptId = [data.prompt_id, data.promptId].find((v) => typeof v === "string" && v.length > 0);+ const body = {hookType: "notification",sessionId,project: resolveProject(cwd),@@ -80,7 +81,12 @@ async function main() {title: data.title,message: data.message}- }, data), OBSERVE_TIMEOUT_MS);+ };+ captureObservation(withEventId(body, {}, {+ ...body.data,+ prompt_id: promptId,+ agent_id: data.agent_id+ }, { stable: promptId !== void 0 }), OBSERVE_TIMEOUT_MS);setTimeout(() => process.exit(0), EXIT_CAP_MS).unref();}main().catch(() => process.exit(0));diff --git a/plugin/scripts/subagent-start.mjs b/plugin/scripts/subagent-start.mjsindex ebb174b7..90eb4508 100755--- a/plugin/scripts/subagent-start.mjs+++ b/plugin/scripts/subagent-start.mjs@@ -74,7 +74,7 @@ async function main() {agent_id: agentId,agent_type: agentType}- }, data), OBSERVE_TIMEOUT_MS);+ }, {}, void 0, { stable: Boolean(data.agent_id) }), OBSERVE_TIMEOUT_MS);setTimeout(() => process.exit(0), EXIT_CAP_MS).unref();}main().catch(() => process.exit(0));diff --git a/plugin/scripts/subagent-stop.mjs b/plugin/scripts/subagent-stop.mjsindex f84008f7..b67517f9 100755--- a/plugin/scripts/subagent-stop.mjs+++ b/plugin/scripts/subagent-stop.mjs@@ -65,7 +65,7 @@ async function main() {const agentType = data.agent_type || data.agentDisplayName || data.agentName;const lastMsg = typeof data.last_assistant_message === "string" ? data.last_assistant_message.slice(0, 4e3) : "";const cwd = hookCwd(data) || process.cwd();- captureObservation(withEventId({+ const body = {hookType: "subagent_stop",sessionId,project: resolveProject(cwd),@@ -76,7 +76,11 @@ async function main() {agent_type: agentType,last_message: lastMsg}- }, data), OBSERVE_TIMEOUT_MS);+ };+ captureObservation(withEventId(body, {}, {+ ...body.data,+ stop_hook_active: data.stop_hook_active === true+ }, { stable: Boolean(data.agent_id) }), OBSERVE_TIMEOUT_MS);setTimeout(() => process.exit(0), EXIT_CAP_MS).unref();}main().catch(() => process.exit(0));diff --git a/plugin/scripts/task-completed.mjs b/plugin/scripts/task-completed.mjsindex 5567f4e9..2b4aab2c 100755--- a/plugin/scripts/task-completed.mjs+++ b/plugin/scripts/task-completed.mjs@@ -75,7 +75,7 @@ async function main() {teammate_name: data.teammate_name,team_name: data.team_name}- }, data), OBSERVE_TIMEOUT_MS);+ }, {}, void 0, { stable: Boolean(data.task_id) }), OBSERVE_TIMEOUT_MS);setTimeout(() => process.exit(0), EXIT_CAP_MS).unref();}main().catch(() => process.exit(0));diff --git a/src/hooks/notification.ts b/src/hooks/notification.tsindex 694675c8..9ae23a20 100644--- a/src/hooks/notification.ts+++ b/src/hooks/notification.ts@@ -37,21 +37,25 @@ async function main() {const cwd = hookCwd(data) || process.cwd();+ const promptId = [data.prompt_id, data.promptId].find((v) => typeof v === "string" && v.length > 0);+ const body = {+ hookType: "notification",+ sessionId,+ project: resolveProject(cwd),+ cwd,+ timestamp: new Date().toISOString(),+ data: {+ notification_type: notificationType,+ title: data.title,+ message: data.message,+ },+ };void captureObservation(withEventId(- {- hookType: "notification",- sessionId,- project: resolveProject(cwd),- cwd,- timestamp: new Date().toISOString(),- data: {- notification_type: notificationType,- title: data.title,- message: data.message,- },- },- data,+ body,+ {},+ { ...body.data, prompt_id: promptId, agent_id: data.agent_id },+ { stable: promptId !== undefined },),OBSERVE_TIMEOUT_MS,);diff --git a/src/hooks/subagent-start.ts b/src/hooks/subagent-start.tsindex 3f037d9c..9a2dd610 100644--- a/src/hooks/subagent-start.ts+++ b/src/hooks/subagent-start.ts@@ -49,7 +49,9 @@ async function main() {agent_type: agentType,},},- data,+ {},+ undefined,+ { stable: Boolean(data.agent_id) },),OBSERVE_TIMEOUT_MS,);diff --git a/src/hooks/subagent-stop.ts b/src/hooks/subagent-stop.tsindex a201abe6..181f7bf1 100644--- a/src/hooks/subagent-stop.ts+++ b/src/hooks/subagent-stop.ts@@ -38,21 +38,24 @@ async function main() {const cwd = hookCwd(data) || process.cwd();+ const body = {+ hookType: "subagent_stop",+ sessionId,+ project: resolveProject(cwd),+ cwd,+ timestamp: new Date().toISOString(),+ data: {+ agent_id: agentId,+ agent_type: agentType,+ last_message: lastMsg,+ },+ };void captureObservation(withEventId(- {- hookType: "subagent_stop",- sessionId,- project: resolveProject(cwd),- cwd,- timestamp: new Date().toISOString(),- data: {- agent_id: agentId,- agent_type: agentType,- last_message: lastMsg,- },- },- data,+ body,+ {},+ { ...body.data, stop_hook_active: data.stop_hook_active === true },+ { stable: Boolean(data.agent_id) },),OBSERVE_TIMEOUT_MS,);diff --git a/src/hooks/task-completed.ts b/src/hooks/task-completed.tsindex 3d0bd396..25550c4b 100644--- a/src/hooks/task-completed.ts+++ b/src/hooks/task-completed.ts@@ -50,7 +50,9 @@ async function main() {team_name: data.team_name,},},- data,+ {},+ undefined,+ { stable: Boolean(data.task_id) },),OBSERVE_TIMEOUT_MS,);diff --git a/test/hook-event-ids.test.ts b/test/hook-event-ids.test.tsnew file mode 100644index 00000000..5e3cde9c--- /dev/null+++ b/test/hook-event-ids.test.ts@@ -0,0 +1,210 @@+import { describe, it, expect, vi, beforeAll, afterAll } from "vitest";+import { spawn } from "node:child_process";+import { createServer, type Server } from "node:http";+import { mkdtempSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { mockKV, mockSdk } from "./helpers/mocks.js";+import { KV } from "../src/state/schema.js";++vi.mock("../src/logger.js", () => ({+ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },+}));++const HOOKS_DIR = join(import.meta.dirname, "..", "plugin", "scripts");+const SESSION = "ses_hook_ids";++const turn = {+ session_id: SESSION,+ prompt_id: "6f1c2a4e-0b7d-4c22-9e83-5a4b3c2d1e0f",+ transcript_path: `/home/u/.claude/projects/p/${SESSION}.jsonl`,+ cwd: "/work/hook-ids",+};++function subagentStop(agentId: string, message: string, extra: Record<string, unknown> = {}) {+ return {+ ...turn,+ hook_event_name: "SubagentStop",+ stop_hook_active: false,+ agent_id: agentId,+ agent_type: "Explore",+ agent_transcript_path: `/home/u/.claude/projects/p/${SESSION}/subagents/agent-${agentId}.jsonl`,+ last_assistant_message: message,+ ...extra,+ };+}++function subagentStart(agentId: string) {+ return { ...turn, hook_event_name: "SubagentStart", agent_id: agentId, agent_type: "Explore" };+}++function taskCompleted(taskId: string, subject: string) {+ return { ...turn, hook_event_name: "TaskCompleted", task_id: taskId, task_subject: subject };+}++function permissionPrompt(message: string) {+ return { ...turn, hook_event_name: "Notification", notification_type: "permission_prompt", title: "Permission needed", message };+}++describe("hook event ids for distinct events under one prompt", () => {+ let server: Server;+ let received: Array<Record<string, unknown>>;+ let env: Record<string, string>;++ beforeAll(async () => {+ received = [];+ server = createServer((req, res) => {+ let raw = "";+ req.on("data", (c) => (raw += c));+ req.on("end", () => {+ try {+ received.push(JSON.parse(raw));+ } catch {}+ res.writeHead(201, { "content-type": "application/json" });+ res.end(JSON.stringify({ status: "accepted" }));+ });+ });+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", () => resolve()));+ const addr = server.address();+ const port = typeof addr === "object" && addr ? addr.port : 0;+ const home = mkdtempSync(join(tmpdir(), "am-hook-ids-"));+ env = {+ PATH: process.env["PATH"] ?? "",+ HOME: home,+ AGENTMEMORY_URL: `http://127.0.0.1:${port}`,+ AGENTMEMORY_CAPTURE_SPOOL_DIR: join(home, "spool"),+ };+ });++ afterAll(async () => {+ await new Promise<void>((resolve) => server.close(() => resolve()));+ });++ async function send(script: string, payload: Record<string, unknown>): Promise<Record<string, unknown>> {+ const before = received.length;+ await new Promise<void>((resolve, reject) => {+ const child = spawn(process.execPath, [join(HOOKS_DIR, script)], { env, stdio: ["pipe", "ignore", "ignore"] });+ child.on("error", reject);+ child.on("close", () => resolve());+ child.stdin.end(JSON.stringify(payload));+ });+ expect(received).toHaveLength(before + 1);+ return received[received.length - 1]!;+ }++ it("gives two subagents that stop in the same prompt different ids", async () => {+ const a = await send("subagent-stop.mjs", subagentStop("a1c4e7f0b2d5a8c13", "Done"));+ const b = await send("subagent-stop.mjs", subagentStop("b9d2f5a8c1e4b7d06", "Done"));+ expect(a.eventId).not.toBe(b.eventId);+ });++ it("gives two subagents that start in the same prompt different ids", async () => {+ const a = await send("subagent-start.mjs", subagentStart("a1c4e7f0b2d5a8c13"));+ const b = await send("subagent-start.mjs", subagentStart("b9d2f5a8c1e4b7d06"));+ expect(a.eventId).not.toBe(b.eventId);+ });++ it("gives two tasks completed in the same prompt different ids", async () => {+ const a = await send("task-completed.mjs", taskCompleted("task-001", "Add the endpoint"));+ const b = await send("task-completed.mjs", taskCompleted("task-002", "Add the endpoint"));+ expect(a.eventId).not.toBe(b.eventId);+ });++ it("gives two permission prompts in the same prompt different ids", async () => {+ const a = await send("notification.mjs", permissionPrompt("Claude needs your permission to use Bash"));+ const b = await send("notification.mjs", permissionPrompt("Claude needs your permission to use Edit"));+ expect(a.eventId).not.toBe(b.eventId);+ });++ it("gives the same permission prompt from two subagents different ids", async () => {+ const message = "Claude needs your permission to use Bash";+ const a = await send("notification.mjs", { ...permissionPrompt(message), agent_id: "a1c4e7f0b2d5a8c13" });+ const b = await send("notification.mjs", { ...permissionPrompt(message), agent_id: "b9d2f5a8c1e4b7d06" });+ expect(a.eventId).not.toBe(b.eventId);+ });++ it("gives the same permission prompt in two prompts different ids", async () => {+ const message = "Claude needs your permission to use Bash";+ const a = await send("notification.mjs", permissionPrompt(message));+ const b = await send("notification.mjs", { ...permissionPrompt(message), prompt_id: "9d3e5b7a-1c2f-4e6a-8b0d-2f4a6c8e0b1d" });+ expect(a.eventId).not.toBe(b.eventId);+ });++ it("keeps permission prompts apart when the host sends no prompt id", async () => {+ const { prompt_id: _omit, ...payload } = permissionPrompt("Claude needs your permission to use Bash");+ const a = await send("notification.mjs", payload);+ const b = await send("notification.mjs", payload);+ expect(a.eventId).not.toBe(b.eventId);+ });++ it("keeps one id when the same subagent, task or permission event is delivered twice", async () => {+ for (const [script, payload] of [+ ["subagent-stop.mjs", subagentStop("c3e6a9d2f5b8c1e47", "Done")],+ ["subagent-start.mjs", subagentStart("c3e6a9d2f5b8c1e47")],+ ["task-completed.mjs", taskCompleted("task-003", "Write the migration")],+ ["notification.mjs", permissionPrompt("Claude needs your permission to use Write")],+ ] as const) {+ const first = await send(script, payload);+ const again = await send(script, payload);+ expect(again.eventId).toBe(first.eventId);+ }+ });++ it("keeps subagents without a host agent id apart even when their reports match", async () => {+ const payload = { sessionId: SESSION, cwd: turn.cwd, agentName: "explore", last_assistant_message: "Done" };+ const a = await send("subagent-stop.mjs", payload);+ const b = await send("subagent-stop.mjs", payload);+ expect(a.eventId).not.toBe(b.eventId);+ });++ it("keeps subagent starts and tasks without a host id apart", async () => {+ for (const [script, payload] of [+ ["subagent-start.mjs", { sessionId: SESSION, cwd: turn.cwd, agentName: "explore" }],+ ["task-completed.mjs", { sessionId: SESSION, cwd: turn.cwd, task_subject: "Write the migration" }],+ ] as const) {+ const a = await send(script, payload);+ const b = await send(script, payload);+ expect(a.eventId, script).not.toBe(b.eventId);+ }+ });++ it("gives a subagent that stops again after a blocking stop hook a new id", async () => {+ const first = await send("subagent-stop.mjs", subagentStop("d4f7b0e3a6c9d2f58", "Draft ready"));+ const final = await send(+ "subagent-stop.mjs",+ subagentStop("d4f7b0e3a6c9d2f58", "Draft ready, tests added", { stop_hook_active: true }),+ );+ expect(final.eventId).not.toBe(first.eventId);+ });++ it("gives a subagent that stops again with the same reply after a blocking stop hook a new id", async () => {+ const first = await send("subagent-stop.mjs", subagentStop("a7c0e3b6d9f2a5c81", "Draft ready"));+ const again = await send(+ "subagent-stop.mjs",+ subagentStop("a7c0e3b6d9f2a5c81", "Draft ready", { stop_hook_active: true }),+ );+ expect(again.eventId).not.toBe(first.eventId);+ });++ it("stores both subagent reports when the server dedups by event id", async () => {+ const a = await send("subagent-stop.mjs", subagentStop("e5a8c1f4b7d0e3a69", "Report A"));+ const b = await send("subagent-stop.mjs", subagentStop("f6b9d2a5c8e1f4b70", "Report B"));+ const retry = await send("subagent-stop.mjs", subagentStop("e5a8c1f4b7d0e3a69", "Report A"));++ vi.resetModules();+ const kv = mockKV();+ const { registerObserveFunction } = await import("../src/functions/observe.js");+ const { registerCaptureFunctions } = await import("../src/functions/capture.js");+ const { DedupMap } = await import("../src/functions/dedup.js");+ const sdk = mockSdk({ looseTrigger: true });+ registerObserveFunction(sdk as never, kv as never, new DedupMap());+ registerCaptureFunctions(sdk as never, kv as never, {});+ const capture = ({ eventId, ...payload }: Record<string, unknown>) =>+ sdk.trigger("mem::capture", { payload, eventId }) as Promise<Record<string, unknown>>;++ expect(await capture(a)).toMatchObject({ status: "accepted" });+ expect(await capture(b)).toMatchObject({ status: "accepted" });+ expect(await capture(retry)).toMatchObject({ status: "duplicate" });+ expect(kv.store.get(KV.observations(SESSION))?.size).toBe(2);+ });+});