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..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 @@ -35,3 +35,60 @@ describe("OpenCode plugin auto-context injection (#431)", () => { expect(deletedBlock).toMatch(/startContextCache\.delete\(sid\)/); }); }); + +describe("OpenCode plugin project name resolution", () => { + const savedProjectName = process.env.AGENTMEMORY_PROJECT_NAME; + let fetchMock: ReturnType; + + 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()); + }); +});