From bf2e60a042d09423d46571970ffad2f24031fc7e Mon Sep 17 00:00:00 2001 From: gezerui0124 Date: Wed, 11 Nov 2026 04:17:53 +0800 Subject: [PATCH] fix(opencode): deduplicate replayed capture events Signed-off-by: gezerui0124 --- plugin/opencode/agentmemory-capture.ts | 81 +++++--- test/opencode-capture-dedup.test.ts | 258 +++++++++++++++++++++++++ 2 files changed, 315 insertions(+), 24 deletions(-) create mode 100644 test/opencode-capture-dedup.test.ts diff --git a/plugin/opencode/agentmemory-capture.ts b/plugin/opencode/agentmemory-capture.ts index bd1ecce368..7a75164cf0 100644 --- a/plugin/opencode/agentmemory-capture.ts +++ b/plugin/opencode/agentmemory-capture.ts @@ -1,4 +1,5 @@ import type { Plugin } from "@opencode-ai/plugin"; +import { createHash } from "node:crypto"; import { execFileSync } from "node:child_process"; import { readFileSync, realpathSync } from "node:fs"; import { homedir } from "node:os"; @@ -101,9 +102,11 @@ async function observe( sessionId: string, hookType: string, data: Record, + eventId?: string, ): Promise { const proj = projectFor(sessionId); await post("/observe", { + eventId, hookType, sessionId, project: proj.name, @@ -810,6 +813,15 @@ const v1Hooks: Plugin = async (ctx) => { }; }; +// This plugin is copied as a standalone file, so it cannot import server helpers. +function canonicalJson(value: unknown): string { + if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null"; + if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`; + const record = value as Record; + return `{${Object.keys(record).filter((key) => record[key] !== undefined).sort() + .map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`).join(",")}}`; +} + async function v2Setup(ctx: any) { if (typeof ctx?.tool?.hook !== "function" || typeof ctx?.session?.hook !== "function" || typeof ctx?.event?.subscribe !== "function") { return; @@ -818,8 +830,26 @@ async function v2Setup(ctx: any) { defaultProjectCwd = location?.directory ?? location?.project?.directory ?? process.cwd(); defaultProjectName = resolveProjectName(defaultProjectCwd); - async function observeV2(sessionId: string, hookType: string, data: Record): Promise { - await observe(sessionId, hookType, data); + async function observeV2( + sessionId: string, + hookType: string, + data: Record, + source?: { id?: unknown; eventID?: unknown; created?: unknown }, + identityData: unknown = data, + ): Promise { + const hostId = source?.id ?? source?.eventID; + const hostTime = source?.created; + const identity = typeof hostId === "string" && hostId + ? ["host", hostId] + : typeof hostTime === "number" && Number.isFinite(hostTime) + ? ["time", hostTime] + : null; + // Without a host identity, retain legacy dedup rather than suppress repeated + // prompts permanently. A shared host ID must still distinguish payload updates. + const eventId = identity + ? `evp_${createHash("sha256").update(canonicalJson([sessionId, hookType, identity, identityData])).digest("hex").slice(0, 32)}` + : undefined; + await observe(sessionId, hookType, data, eventId); } const shellSessions = new Map(); @@ -848,7 +878,7 @@ async function v2Setup(ctx: any) { let lastConfigSnapshot: string | null = null; - async function snapshotConfig(): Promise { + async function snapshotConfig(source?: { id?: unknown; eventID?: unknown; created?: unknown }): Promise { const [agents, providers, mcp, modelDefault] = await Promise.all([ ctx.agent?.list?.().catch(() => null), ctx.provider?.list?.().catch(() => null), @@ -867,7 +897,7 @@ async function v2Setup(ctx: any) { const snapshot = JSON.stringify(payload); if (snapshot === lastConfigSnapshot) return; lastConfigSnapshot = snapshot; - if (activeSessionId) await observeV2(activeSessionId, "config_loaded", payload); + if (activeSessionId) await observeV2(activeSessionId, "config_loaded", payload, source ?? { id: "config_loaded" }); else pendingConfig = payload; } @@ -910,7 +940,7 @@ async function v2Setup(ctx: any) { tool_input: safeSlice(event?.input, 4000), tool_output: safeSlice(text || extractErrorMessage(event?.error) || extractErrorMessage(metadata?.error), 8000), duration_ms: duration, - }); + }, event); if (callId) reportedToolCalls.add(`${sid}:${callId}`); return; } @@ -926,7 +956,7 @@ async function v2Setup(ctx: any) { attachments: Array.isArray(output?.attachments) ? (output?.attachments as Array>).map((a) => a.filename || a.url) : [], - }); + }, event); if (callId) reportedToolCalls.add(`${sid}:${callId}`); }); @@ -944,7 +974,7 @@ async function v2Setup(ctx: any) { agents: event?.prompt?.agents ?? [], skills: event?.prompt?.skills ?? [], delivery: event?.delivery ?? null, - }); + }, { id: event?.messageID }); }); await ctx.session.hook("context", async (event: any) => { @@ -1034,9 +1064,9 @@ async function v2Setup(ctx: any) { const startCtx = (startResult as any)?.context; if (typeof startCtx === "string" && startCtx.length > 0) startContextCache.set(sid0, startCtx); - await observeV2(sid0, "session_started", {}); + await observeV2(sid0, "session_started", {}, { id: "session_started" }); if (pendingConfig) { - await observeV2(sid0, "config_loaded", pendingConfig); + await observeV2(sid0, "config_loaded", pendingConfig, { id: "config_loaded" }); pendingConfig = null; } } @@ -1049,7 +1079,7 @@ async function v2Setup(ctx: any) { tool_name: "session.execution", tool_input: "", tool_output: safeSlice(extractErrorMessage(data.error ?? data), 8000), - }); + }, event); return; } @@ -1060,7 +1090,7 @@ async function v2Setup(ctx: any) { reason: (data.reason as string) ?? null, inputID: (data.inputID as string) ?? null, recent: safeSlice(data.recent, 8000), - }); + }, event); return; } @@ -1071,7 +1101,7 @@ async function v2Setup(ctx: any) { reason: (data.reason as string) ?? null, inputID: (data.inputID as string) ?? null, tool_output: safeSlice(extractErrorMessage(data.error ?? data.reason), 8000), - }); + }, event); return; } @@ -1081,7 +1111,7 @@ async function v2Setup(ctx: any) { messageID: (data.assistantMessageID as string) ?? null, agent: (data.agent as string) ?? null, model: data.model ? `${data.model.providerID ?? ""}/${data.model.id ?? ""}` : null, - }); + }, event); return; } @@ -1097,7 +1127,7 @@ async function v2Setup(ctx: any) { reasoning_tokens: tokens.reasoning ?? 0, cache_read: tokens.cache?.read ?? 0, cache_write: tokens.cache?.write ?? 0, - }); + }, event); return; } @@ -1106,16 +1136,18 @@ async function v2Setup(ctx: any) { await observeV2(sid0, "agent_selected", { name: (data.agent as string) ?? null, previous: (data.previous as string) ?? null, - }); + }, event); return; } case "session.reasoning.ended": { if (!sid0) return; + const text = (data.text as string) ?? ""; + if (!text) return; await observeV2(sid0, "reasoning", { messageID: (data.assistantMessageID as string) ?? null, - text: safeSlice(data.text, 4000), - }); + text: text.slice(0, 4000), + }, event); return; } @@ -1126,7 +1158,7 @@ async function v2Setup(ctx: any) { await observeV2(sid0, "notification", { notification_type: "instructions_updated", sources: sources.slice(0, 50), - }); + }, event); return; } @@ -1137,7 +1169,7 @@ async function v2Setup(ctx: any) { await observeV2(sid0, "assistant_message", { messageID: (data.assistantMessageID as string) ?? null, text: text.slice(0, 8000), - }); + }, event); return; } @@ -1167,7 +1199,7 @@ async function v2Setup(ctx: any) { tool_input: null, tool_output: safeSlice(data.status, 4000), duration_ms: null, - }); + }, event); } return; } @@ -1234,13 +1266,14 @@ async function v2Setup(ctx: any) { if (!sid0) return; const callId = String(data.id ?? ""); if (callId && reportedToolCalls.has(`${sid0}:${callId}`)) return; + // Replays can arrive after the enrichment caches below have been cleared. await observeV2(sid0, "post_tool_failure", { tool_name: toolNames.get(callId) || null, call_id: callId || null, tool_input: safeSlice(toolCallInputs.get(callId), 4000), tool_output: safeSlice(extractErrorMessage(data.error), 8000), duration_ms: null, - }); + }, event, data); if (callId) { toolNames.delete(callId); toolCallInputs.delete(callId); @@ -1258,7 +1291,7 @@ async function v2Setup(ctx: any) { tool_call_id: (data.tool?.callID as string) ?? (data.callID as string) ?? null, title: (data.action as string) ?? (data.permission as string) ?? "", metadata: data.metadata ?? {}, - }); + }, event); return; } @@ -1267,7 +1300,7 @@ async function v2Setup(ctx: any) { await observeV2(sid0, "permission_replied", { permission_id: (data.requestID as string) ?? (data.permissionID as string) ?? "", response: (data.reply as string) ?? (data.response as string) ?? "", - }); + }, event); return; } @@ -1294,7 +1327,7 @@ async function v2Setup(ctx: any) { case "model.updated": case "mcp.status.changed": { if (DEBUG) console.error("[agentmemory] config changed, re-snapshotting"); - void snapshotConfig().catch(() => {}); + void snapshotConfig(event).catch(() => {}); return; } } diff --git a/test/opencode-capture-dedup.test.ts b/test/opencode-capture-dedup.test.ts new file mode 100644 index 0000000000..dc395ab63c --- /dev/null +++ b/test/opencode-capture-dedup.test.ts @@ -0,0 +1,258 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { mockKV, mockSdk } from "./helpers/mocks.js"; +import { KV } from "../src/state/schema.js"; +import type { CompressedObservation } from "../src/types.js"; + +vi.mock("../src/logger.js", () => ({ + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, +})); + +const SID = "ses_opencode_replay"; +const cleanups: Array<() => void | Promise> = []; + +beforeEach(() => { + vi.resetModules(); + vi.stubEnv("AGENTMEMORY_SECRET", "local-capture-fixture"); + vi.stubEnv("AGENTMEMORY_PROJECT_NAME", "capture-fixture"); +}); + +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup(); + vi.unstubAllGlobals(); + vi.unstubAllEnvs(); +}); + +async function setup() { + 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 kv = mockKV(); + const sdk = mockSdk({ looseTrigger: true }); + let dedup = new DedupMap(); + registerObserveFunction(sdk as never, kv as never, dedup); + let capture = registerCaptureFunctions(sdk as never, kv as never); + cleanups.push(() => { capture.stop(); dedup.stop(); }); + const requests: Array> = []; + vi.stubGlobal("fetch", async (url: string, init: RequestInit) => { + const body = JSON.parse(String(init.body)); + if (url.endsWith("/observe")) requests.push(body); + const result = url.endsWith("/observe") + ? await sdk.trigger("mem::capture", { payload: body, eventId: body.eventId }) + : { ok: true }; + return Response.json(result); + }); + const { default: plugin } = await import("../plugin/opencode/agentmemory-capture.js"); + async function instance() { + const hooks = new Map Promise>(); + const sessionHooks = new Map Promise>(); + const controller = new AbortController(); + const queue: Array<{ event: unknown; done: () => void }> = []; + let wake: (() => void) | undefined; + const emit = (event: unknown) => new Promise((done) => { + queue.push({ event, done }); + wake?.(); + }); + const context = { + location: { directory: process.cwd() }, + tool: { hook: async (name: string, handler: (event: unknown) => Promise) => { hooks.set(name, handler); } }, + session: { hook: async (name: string, handler: (event: unknown) => Promise) => { sessionHooks.set(name, handler); } }, + event: { + subscribe: async function* ({ signal }: { signal: AbortSignal }) { + signal.addEventListener("abort", () => wake?.(), { once: true }); + controller.signal.addEventListener("abort", () => wake?.(), { once: true }); + while (!signal.aborted && !controller.signal.aborted) { + if (!queue.length) await new Promise((resolve) => { wake = resolve; }); + const next = queue.shift(); + if (!next) continue; + yield next.event; + next.done(); + } + }, + }, + }; + const dispose = await plugin.setup(context); + const close = async () => { controller.abort(); await dispose?.(); }; + cleanups.push(close); + return { tool: (event: unknown) => hooks.get("execute.after")!(event), + prompt: (event: unknown) => sessionHooks.get("prompt")!(event), emit, close }; + } + const first = await instance(); + const second = await instance(); + const observations = (sid = SID) => [...(kv.store.get(KV.observations(sid))?.values() ?? [])] as CompressedObservation[]; + return { first, second, observations, requests, instance, + restartCapture: () => { + capture.stop(); + dedup.stop(); + dedup = new DedupMap(); + registerObserveFunction(sdk as never, kv as never, dedup); + capture = registerCaptureFunctions(sdk as never, kv as never); + }, + }; +} + +function completedTool(id: string, output = "file contents") { + return { + tool: "read", sessionID: SID, id, status: "completed", + input: { filePath: "/work/readme.txt" }, + result: { content: [{ type: "text", text: output }], metadata: { started: 1, ended: 3 } }, + }; +} + +describe("OpenCode capture delivery", () => { + it("stores a concurrent tool delivery from two plugin instances once", async () => { + const h = await setup(); + const event = completedTool("call_1"); + await Promise.all([h.first.tool(event), h.second.tool(event)]); + expect(h.observations()).toHaveLength(1); + }); + + it("keeps distinct tool calls with the same content", async () => { + const h = await setup(); + await h.first.tool(completedTool("call_1")); + await h.first.tool(completedTool("call_2")); + expect(h.observations()).toHaveLength(2); + }); + + it("keeps different output under the same host call id", async () => { + const h = await setup(); + await h.first.tool(completedTool("call_1", "first result")); + await h.second.tool(completedTool("call_1", "updated result")); + expect(h.observations()).toHaveLength(2); + }); + + it("deduplicates tool metadata with a different object key order", async () => { + const h = await setup(); + const first = completedTool("call_1"); + const second = completedTool("call_1"); + second.result.metadata = { ended: 3, started: 1 }; + await Promise.all([h.first.tool(first), h.second.tool(second)]); + expect(h.observations()).toHaveLength(1); + }); + + it("keeps the same call id in separate sessions", async () => { + const h = await setup(); + await h.first.tool(completedTool("call_1")); + await h.second.tool({ ...completedTool("call_1"), sessionID: "ses_child" }); + expect(h.observations()).toHaveLength(1); + expect(h.observations("ses_child")).toHaveLength(1); + expect(h.requests[0].eventId).not.toBe(h.requests[1].eventId); + }); + + it("deduplicates a replay after plugin disposal and capture restart", async () => { + const h = await setup(); + await h.first.tool(completedTool("call_1")); + await h.first.close(); + h.restartCapture(); + const reloaded = await h.instance(); + await reloaded.tool(completedTool("call_1")); + expect(h.observations()).toHaveLength(1); + await reloaded.tool(completedTool("call_2")); + expect(h.observations()).toHaveLength(2); + }); + + it("retains legacy capture when the callback has no event identity", async () => { + const h = await setup(); + const { id, ...event } = completedTool("unused"); + await h.first.tool(event); + expect(h.requests[0]).not.toHaveProperty("eventId"); + expect(h.observations()).toHaveLength(1); + }); +}); + +function textEvent(id: string, text: string) { + return { type: "session.text.ended", id, created: 1, location: {}, + data: { sessionID: SID, assistantMessageID: "message_1", ordinal: 0, text } }; +} + +describe("OpenCode event stream identity", () => { + it("stores initial snapshots once across concurrent registration and restart", async () => { + const h = await setup(); + const event = { type: "session.created", id: "created_1", created: 1, data: { sessionID: SID }, location: {} }; + await Promise.all([h.first.emit(event), h.second.emit(event)]); + const starts = () => h.observations().filter((observation) => observation.source?.hookType === "session_started"); + const configs = () => h.observations().filter((observation) => observation.source?.hookType === "config_loaded"); + expect(starts()).toHaveLength(1); + expect(configs()).toHaveLength(1); + await h.first.close(); + h.restartCapture(); + const reloaded = await h.instance(); + await reloaded.emit(event); + expect(starts()).toHaveLength(1); + expect(configs()).toHaveLength(1); + }); + + it("deduplicates concurrent event delivery after both instances register the session", async () => { + const h = await setup(); + const created = { type: "session.created", id: "created_1", created: 1, data: { sessionID: SID }, location: {} }; + await h.first.emit(created); + await h.second.emit(created); + const before = h.observations().length; + const event = textEvent("event_1", "finished reading"); + await Promise.all([h.first.emit(event), h.second.emit(event)]); + expect(h.observations()).toHaveLength(before + 1); + }); + + it("preserves changed text when the host reuses its event id", async () => { + const h = await setup(); + await h.first.emit(textEvent("event_1", "first answer")); + const before = h.observations().length; + await h.first.emit(textEvent("event_1", "updated answer")); + expect(h.observations()).toHaveLength(before + 1); + }); + + it("uses the native creation time when the event id is absent", async () => { + const h = await setup(); + const { id, ...event } = textEvent("unused", "answer"); + await h.first.emit(event); + const before = h.observations().length; + await h.second.emit(event); + expect(h.observations()).toHaveLength(before); + await h.second.emit({ ...event, created: 2 }); + expect(h.observations()).toHaveLength(before + 1); + }); + + it("does not capture an empty reasoning completion", async () => { + const h = await setup(); + await h.first.emit({ ...textEvent("reasoning_1", ""), type: "session.reasoning.ended" }); + expect(h.requests.filter((body) => body.hookType === "reasoning")).toHaveLength(0); + }); + + it("deduplicates fallback failures after cached tool details are cleared", async () => { + const h = await setup(); + const event = { type: "session.created", id: "created_1", created: 1, data: { sessionID: SID }, location: {} }; + await Promise.all([h.first.emit(event), h.second.emit(event)]); + await h.first.emit({ ...event, type: "session.tool.input.started", id: "input_1", + data: { sessionID: SID, id: "call_1", name: "read" } }); + await h.first.emit({ ...event, type: "session.tool.called", id: "called_1", + data: { sessionID: SID, id: "call_1", input: { filePath: "/work/readme.txt" } } }); + const failure = { ...event, type: "session.tool.failed", id: "failed_1", + data: { sessionID: SID, id: "call_1", error: { message: "denied" } } }; + const before = h.observations().length; + await h.first.emit(failure); + expect(h.observations()).toHaveLength(before + 1); + await h.first.emit(failure); + await h.second.emit(failure); + expect(h.observations()).toHaveLength(before + 1); + expect(h.observations().find((observation) => observation.source?.hookType === "post_tool_failure")?.source) + .toMatchObject({ toolName: "read", toolInput: JSON.stringify({ filePath: "/work/readme.txt" }), toolOutput: "denied" }); + await h.second.emit({ ...failure, data: { ...failure.data, error: { message: "missing" } } }); + expect(h.observations()).toHaveLength(before + 2); + }); +}); + +describe("OpenCode prompt delivery", () => { + it("deduplicates native prompt delivery by message id across instances", async () => { + const h = await setup(); + const event = { sessionID: SID, messageID: "message_1", prompt: { text: "read the file" }, delivery: "steer" }; + await Promise.all([h.first.prompt(event), h.second.prompt(event)]); + expect(h.observations()).toHaveLength(1); + }); + + it("keeps repeated prompt text with a distinct message id", async () => { + const h = await setup(); + const event = { sessionID: SID, messageID: "message_1", prompt: { text: "read the file" }, delivery: "steer" }; + await h.first.prompt(event); + await h.first.prompt({ ...event, messageID: "message_2" }); + expect(h.observations()).toHaveLength(2); + }); +});