From 16ddcb6577419628a81b288e5516c6af64443f6f Mon Sep 17 00:00:00 2001 From: mothieras Date: Thu, 5 Nov 2026 13:31:09 +0800 Subject: [PATCH 1/2] fix(cursor): map subagent stop type and summary fields Signed-off-by: mothieras --- plugin/scripts/cursor/run-detached.mjs | 4 + plugin/scripts/cursor/run-hook.mjs | 4 + src/hooks/cursor/delegate.ts | 4 + test/cursor-adapter.test.ts | 101 +++++++++++++++++++++++++ 4 files changed, 113 insertions(+) diff --git a/plugin/scripts/cursor/run-detached.mjs b/plugin/scripts/cursor/run-detached.mjs index 25515f55e7..e51ad559e8 100755 --- a/plugin/scripts/cursor/run-detached.mjs +++ b/plugin/scripts/cursor/run-detached.mjs @@ -716,6 +716,10 @@ function delegateHook(hookKey, data, options = {}) { return 0; } const { project, payload } = enrichPayload(data); + if (hookKey === "subagentStop") { + payload.agent_type ??= data.subagent_type; + payload.last_assistant_message ??= data.summary; + } const child = spawnSync(process.execPath, [scriptPath], { input: JSON.stringify(payload), env: { diff --git a/plugin/scripts/cursor/run-hook.mjs b/plugin/scripts/cursor/run-hook.mjs index cad1c0492b..71979b7901 100755 --- a/plugin/scripts/cursor/run-hook.mjs +++ b/plugin/scripts/cursor/run-hook.mjs @@ -658,6 +658,10 @@ function delegateHook(hookKey, data, options = {}) { return 0; } const { project, payload } = enrichPayload(data); + if (hookKey === "subagentStop") { + payload.agent_type ??= data.subagent_type; + payload.last_assistant_message ??= data.summary; + } const child = spawnSync(process.execPath, [scriptPath], { input: JSON.stringify(payload), env: { diff --git a/src/hooks/cursor/delegate.ts b/src/hooks/cursor/delegate.ts index c07020d6c5..83f85367c1 100644 --- a/src/hooks/cursor/delegate.ts +++ b/src/hooks/cursor/delegate.ts @@ -85,6 +85,10 @@ export function delegateHook( } const { project, payload } = enrichPayload(data); + if (hookKey === "subagentStop") { + payload.agent_type ??= data.subagent_type; + payload.last_assistant_message ??= data.summary; + } const child = spawnSync(process.execPath, [scriptPath], { input: JSON.stringify(payload), diff --git a/test/cursor-adapter.test.ts b/test/cursor-adapter.test.ts index f8c7517f16..de17c8dd0f 100644 --- a/test/cursor-adapter.test.ts +++ b/test/cursor-adapter.test.ts @@ -239,6 +239,107 @@ describe("built shims against a local server", () => { rmSync(workspace, { recursive: true, force: true }); }, 30000); + it.each([ + { + name: "Cursor summary and subagent type", + fields: { subagent_type: "explore", summary: "Found authenticateUser in auth.ts" }, + expected: { agent_type: "explore", last_message: "Found authenticateUser in auth.ts" }, + }, + { + name: "canonical fields when both forms are present", + fields: { + subagent_type: "explore", + summary: "Cursor summary", + agent_type: "Explore", + last_assistant_message: "Canonical reply", + }, + expected: { agent_type: "Explore", last_message: "Canonical reply" }, + }, + { + name: "an explicitly empty canonical reply", + fields: { subagent_type: "explore", summary: "Cursor summary", last_assistant_message: "" }, + expected: { agent_type: "explore", last_message: "" }, + }, + { + name: "a missing summary", + fields: { subagent_type: "explore" }, + expected: { agent_type: "explore", last_message: "" }, + }, + { + name: "a non-string summary", + fields: { subagent_type: "explore", summary: { text: "Not a string" } }, + expected: { agent_type: "explore", last_message: "" }, + }, + ])("subagentStop preserves $name through the built adapter", async ({ fields, expected }) => { + const workspace = mkdtempSync(join(tmpdir(), "am-cursor-subagent-")).replace(/\\/g, "/"); + const sessionId = uniqueId("shim-subagent"); + try { + const { code, stderr } = await runShim( + "run-hook.mjs", + ["subagentStop"], + { conversation_id: sessionId, workspace_roots: [workspace], status: "completed", ...fields }, + { + ...hookEnv(), + HOME: join(workspace, "home"), + USERPROFILE: join(workspace, "home"), + AGENTMEMORY_CAPTURE_SPOOL_DIR: join(workspace, "spool"), + }, + ); + expect(code).toBe(0); + expect(stderr).toBe(""); + const hits = server.captures.filter((c) => + (c.body as { sessionId?: string })?.sessionId === sessionId, + ); + expect(hits).toHaveLength(1); + expect(hits[0].url).toBe("/agentmemory/observe"); + expect(hits[0].body).toMatchObject({ + hookType: "subagent_stop", + sessionId, + project: basename(workspace), + cwd: workspace, + data: expected, + }); + } finally { + rmSync(workspace, { recursive: true, force: true }); + } + }, 15000); + + it("subagentStop redacts the Cursor summary before truncating it", async () => { + const workspace = mkdtempSync(join(tmpdir(), "am-cursor-subagent-")).replace(/\\/g, "/"); + const sessionId = uniqueId("shim-private-summary"); + try { + const { code } = await runShim( + "run-hook.mjs", + ["subagentStop"], + { + conversation_id: sessionId, + workspace_roots: [workspace], + subagent_type: "explore", + summary: "A".repeat(3980) + "fixture-private-value" + "x".repeat(100) + "" + "B".repeat(100), + }, + { + ...hookEnv(), + HOME: join(workspace, "home"), + USERPROFILE: join(workspace, "home"), + AGENTMEMORY_CAPTURE_SPOOL_DIR: join(workspace, "spool"), + }, + ); + expect(code).toBe(0); + const hit = server.captures.find((c) => + (c.body as { sessionId?: string })?.sessionId === sessionId, + ); + expect(hit).toBeDefined(); + const body = hit!.body as { data: { agent_type: string; last_message: string } }; + expect(body.data.agent_type).toBe("explore"); + expect(body.data.last_message).toHaveLength(4000); + expect(body.data.last_message.startsWith("A".repeat(3980))).toBe(true); + expect(body.data.last_message).not.toContain("fixture-private-value"); + expect(body.data.last_message).not.toContain(""); + } finally { + rmSync(workspace, { recursive: true, force: true }); + } + }, 15000); + it("run-detached delivers a large payload through its background worker", async () => { const workspace = mkdtempSync(join(tmpdir(), "am-cursor-ws-")); const sessionId = uniqueId("shim-stop"); From 1943730c99d43b379a4cc2f4dbe9d25fc01a4514 Mon Sep 17 00:00:00 2001 From: mothieras Date: Thu, 5 Nov 2026 13:37:30 +0800 Subject: [PATCH 2/2] fix(cursor): preserve existing subagent field aliases Signed-off-by: mothieras --- plugin/scripts/cursor/run-detached.mjs | 4 ++-- plugin/scripts/cursor/run-hook.mjs | 4 ++-- src/hooks/cursor/delegate.ts | 4 ++-- test/cursor-adapter.test.ts | 30 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/plugin/scripts/cursor/run-detached.mjs b/plugin/scripts/cursor/run-detached.mjs index e51ad559e8..0acb8da80e 100755 --- a/plugin/scripts/cursor/run-detached.mjs +++ b/plugin/scripts/cursor/run-detached.mjs @@ -717,8 +717,8 @@ function delegateHook(hookKey, data, options = {}) { } const { project, payload } = enrichPayload(data); if (hookKey === "subagentStop") { - payload.agent_type ??= data.subagent_type; - payload.last_assistant_message ??= data.summary; + payload.agent_type ||= data.agentType || data.agentDisplayName || data.agentName || data.subagent_type; + payload.last_assistant_message ??= data.response ?? data.summary; } const child = spawnSync(process.execPath, [scriptPath], { input: JSON.stringify(payload), diff --git a/plugin/scripts/cursor/run-hook.mjs b/plugin/scripts/cursor/run-hook.mjs index 71979b7901..92c72dd00c 100755 --- a/plugin/scripts/cursor/run-hook.mjs +++ b/plugin/scripts/cursor/run-hook.mjs @@ -659,8 +659,8 @@ function delegateHook(hookKey, data, options = {}) { } const { project, payload } = enrichPayload(data); if (hookKey === "subagentStop") { - payload.agent_type ??= data.subagent_type; - payload.last_assistant_message ??= data.summary; + payload.agent_type ||= data.agentType || data.agentDisplayName || data.agentName || data.subagent_type; + payload.last_assistant_message ??= data.response ?? data.summary; } const child = spawnSync(process.execPath, [scriptPath], { input: JSON.stringify(payload), diff --git a/src/hooks/cursor/delegate.ts b/src/hooks/cursor/delegate.ts index 83f85367c1..8cb6ea5a28 100644 --- a/src/hooks/cursor/delegate.ts +++ b/src/hooks/cursor/delegate.ts @@ -86,8 +86,8 @@ export function delegateHook( const { project, payload } = enrichPayload(data); if (hookKey === "subagentStop") { - payload.agent_type ??= data.subagent_type; - payload.last_assistant_message ??= data.summary; + payload.agent_type ||= data.agentType || data.agentDisplayName || data.agentName || data.subagent_type; + payload.last_assistant_message ??= data.response ?? data.summary; } const child = spawnSync(process.execPath, [scriptPath], { diff --git a/test/cursor-adapter.test.ts b/test/cursor-adapter.test.ts index de17c8dd0f..c1b2145465 100644 --- a/test/cursor-adapter.test.ts +++ b/test/cursor-adapter.test.ts @@ -255,6 +255,36 @@ describe("built shims against a local server", () => { }, expected: { agent_type: "Explore", last_message: "Canonical reply" }, }, + { + name: "existing response and agentType aliases", + fields: { + subagent_type: "explore", + summary: "Cursor summary", + agentType: "Explore", + response: "Existing reply", + }, + expected: { agent_type: "Explore", last_message: "Existing reply" }, + }, + { + name: "an existing response alongside a non-string summary", + fields: { subagent_type: "explore", summary: { text: "Preview" }, response: "Existing reply" }, + expected: { agent_type: "explore", last_message: "Existing reply" }, + }, + { + name: "an explicitly empty response alias", + fields: { subagent_type: "explore", summary: "Cursor summary", response: "" }, + expected: { agent_type: "explore", last_message: "" }, + }, + { + name: "the agentDisplayName fallback after empty type fields", + fields: { subagent_type: "explore", agent_type: "", agentType: "", agentDisplayName: "Explorer" }, + expected: { agent_type: "Explorer", last_message: "" }, + }, + { + name: "the agentName type fallback", + fields: { subagent_type: "explore", agentName: "named-agent" }, + expected: { agent_type: "named-agent", last_message: "" }, + }, { name: "an explicitly empty canonical reply", fields: { subagent_type: "explore", summary: "Cursor summary", last_assistant_message: "" },