Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

feat(opencode): allow AGENTMEMORY_PROJECT_NAME to override project name (#1125)

Abdullah Alaqeel9be3d2743af4
diff --git a/plugin/opencode/agentmemory-capture.ts b/plugin/opencode/agentmemory-capture.ts
index 056d53d3..46419ef8 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 3ed49590..2691e096 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<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());
+ });
+});