Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugin/opencode/agentmemory-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
59 changes: 58 additions & 1 deletion test/opencode-auto-context.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<typeof vi.fn>;

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<string, unknown>): Promise<unknown> {
const { AgentmemoryCapturePlugin } = await import(
"../plugin/opencode/agentmemory-capture.ts"
);
const handlers = await (AgentmemoryCapturePlugin as (c: unknown) => Promise<{
event: (msg: unknown) => Promise<void>;
}>)(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());
});
});