From 410a37083384bb091560b48dad869727817f3e95 Mon Sep 17 00:00:00 2001 From: Abdullah Alaqeel Date: Wed, 29 Jul 2026 22:23:18 +0300 Subject: [PATCH 1/2] feat(opencode): allow AGENTMEMORY_PROJECT_NAME to override project name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The OpenCode capture plugin resolved the project as ctx.worktree || ctx.project?.id || process.cwd() (a full path) with no way to override it. It now consults AGENTMEMORY_PROJECT_NAME first — the same env var the Claude hooks use in resolveProject() — falling back to the existing resolution when unset. This lets users pin a stable project name and aligns the two plugin ecosystems so OpenCode and Claude sessions can share memory when the env var is set to the same value. Signed-off-by: Abdullah Alaqeel --- plugin/opencode/agentmemory-capture.ts | 3 ++- test/opencode-auto-context.test.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/plugin/opencode/agentmemory-capture.ts b/plugin/opencode/agentmemory-capture.ts index 056d53d38d..46419ef8c0 100644 --- a/plugin/opencode/agentmemory-capture.ts +++ b/plugin/opencode/agentmemory-capture.ts @@ -168,7 +168,8 @@ function extractErrorMessage(err: unknown): string { } export const AgentmemoryCapturePlugin: Plugin = async (ctx) => { - projectPath = ctx.worktree || ctx.project?.id || process.cwd(); + const explicitProject = process.env.AGENTMEMORY_PROJECT_NAME?.trim(); + projectPath = explicitProject || ctx.worktree || ctx.project?.id || process.cwd(); return { event: async ({ event }) => { diff --git a/test/opencode-auto-context.test.ts b/test/opencode-auto-context.test.ts index 3ed4959032..c0293798c2 100644 --- a/test/opencode-auto-context.test.ts +++ b/test/opencode-auto-context.test.ts @@ -35,3 +35,17 @@ describe("OpenCode plugin auto-context injection (#431)", () => { expect(deletedBlock).toMatch(/startContextCache\.delete\(sid\)/); }); }); + +describe("OpenCode plugin project name override", () => { + const plugin = readFileSync( + "plugin/opencode/agentmemory-capture.ts", + "utf-8", + ); + + it("honours AGENTMEMORY_PROJECT_NAME before falling back to ctx/cwd", () => { + expect(plugin).toMatch(/AGENTMEMORY_PROJECT_NAME/); + expect(plugin).toMatch( + /explicitProject\s*\|\|\s*ctx\.worktree\s*\|\|\s*ctx\.project\?\.id\s*\|\|\s*process\.cwd\(\)/, + ); + }); +}); From 7e39b74d1dd115cfa286c692e515ab020b0ee426 Mon Sep 17 00:00:00 2001 From: Abdullah Alaqeel Date: Wed, 29 Jul 2026 23:36:20 +0300 Subject: [PATCH 2/2] test(opencode): replace source-text assertions with runtime project-name tests Execute the plugin end-to-end, fire a session.created event, and assert the /session/start payload's project field. Covers whitespace trimming of AGENTMEMORY_PROJECT_NAME and the documented fallback chain (ctx.worktree -> ctx.project.id -> process.cwd). Signed-off-by: Abdullah Alaqeel --- test/opencode-auto-context.test.ts | 63 +++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/test/opencode-auto-context.test.ts b/test/opencode-auto-context.test.ts index c0293798c2..2691e0965f 100644 --- a/test/opencode-auto-context.test.ts +++ b/test/opencode-auto-context.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect } from "vitest"; +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import { readFileSync } from "node:fs"; // OpenCode plugin needs zero-config memory injection. Plugin @@ -36,16 +36,59 @@ describe("OpenCode plugin auto-context injection (#431)", () => { }); }); -describe("OpenCode plugin project name override", () => { - const plugin = readFileSync( - "plugin/opencode/agentmemory-capture.ts", - "utf-8", - ); +describe("OpenCode plugin project name resolution", () => { + const savedProjectName = process.env.AGENTMEMORY_PROJECT_NAME; + let fetchMock: ReturnType; - it("honours AGENTMEMORY_PROJECT_NAME before falling back to ctx/cwd", () => { - expect(plugin).toMatch(/AGENTMEMORY_PROJECT_NAME/); - expect(plugin).toMatch( - /explicitProject\s*\|\|\s*ctx\.worktree\s*\|\|\s*ctx\.project\?\.id\s*\|\|\s*process\.cwd\(\)/, + beforeEach(() => { + fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }); + vi.stubGlobal("fetch", fetchMock); + vi.resetModules(); + delete process.env.AGENTMEMORY_PROJECT_NAME; + }); + + afterEach(() => { + vi.unstubAllGlobals(); + if (savedProjectName === undefined) delete process.env.AGENTMEMORY_PROJECT_NAME; + else process.env.AGENTMEMORY_PROJECT_NAME = savedProjectName; + }); + + async function projectFor(ctx: Record): Promise { + const { AgentmemoryCapturePlugin } = await import( + "../plugin/opencode/agentmemory-capture.ts" + ); + const handlers = await (AgentmemoryCapturePlugin as (c: unknown) => Promise<{ + event: (msg: unknown) => Promise; + }>)(ctx); + await handlers.event({ + event: { type: "session.created", properties: { info: { id: "s1" } } }, + }); + const startCall = fetchMock.mock.calls.find( + (c: unknown[]) => typeof c[0] === "string" && (c[0] as string).includes("/session/start"), ); + if (!startCall) throw new Error("no /session/start call captured"); + return JSON.parse((startCall[1] as { body: string }).body).project; + } + + it("uses trimmed AGENTMEMORY_PROJECT_NAME when set", async () => { + process.env.AGENTMEMORY_PROJECT_NAME = " my-proj "; + expect(await projectFor({ worktree: "/should/be/ignored" })).toBe("my-proj"); + }); + + it("treats whitespace-only env value as unset and falls back", async () => { + process.env.AGENTMEMORY_PROJECT_NAME = " "; + expect(await projectFor({ worktree: "/repo/alpha" })).toBe("/repo/alpha"); + }); + + it("falls back to ctx.worktree when env is unset", async () => { + expect(await projectFor({ worktree: "/repo/alpha" })).toBe("/repo/alpha"); + }); + + it("falls back to ctx.project.id when worktree is absent", async () => { + expect(await projectFor({ project: { id: "/repo/beta" } })).toBe("/repo/beta"); + }); + + it("falls back to process.cwd() when no ctx field is present", async () => { + expect(await projectFor({})).toBe(process.cwd()); }); });