#1 Persistent memory for AI coding agents based on real-world benchmarks
TypeScript agentmemoryagentsaiclaudeclaudecodecodexcopilotcursorgenaiharnesshermesmemoryompopenclawopencodepi
fix: slots HTTP triggers return 503 not 500 + read .env (closes #678) (#682)
Rohit Ghumare4dd1ddeff1cb
diff --git a/src/functions/slots.ts b/src/functions/slots.tsindex a6f61753..47b49d49 100644--- a/src/functions/slots.ts+++ b/src/functions/slots.ts@@ -4,6 +4,7 @@ import { KV } from "../state/schema.js";import { StateKV } from "../state/kv.js";import { withKeyedLock } from "../state/keyed-mutex.js";import { recordAudit } from "./audit.js";+import { getEnvVar } from "../config.js";import { logger } from "../logger.js";type SlotScope = "project" | "global";@@ -95,12 +96,14 @@ export const DEFAULT_SLOTS: ReadonlyArray<},];+// Read merged env so values loaded from ~/.agentmemory/.env are+// honoured. process.env alone misses .env-only exports (#678).export function isSlotsEnabled(): boolean {- return process.env["AGENTMEMORY_SLOTS"] === "true";+ return getEnvVar("AGENTMEMORY_SLOTS") === "true";}export function isReflectEnabled(): boolean {- return process.env["AGENTMEMORY_REFLECT"] === "true";+ return getEnvVar("AGENTMEMORY_REFLECT") === "true";}function scopeKv(scope: SlotScope): string {diff --git a/src/triggers/api.ts b/src/triggers/api.tsindex 8f80379b..211bb638 100644--- a/src/triggers/api.ts+++ b/src/triggers/api.ts@@ -8,6 +8,7 @@ import type { MetricsStore } from "../eval/metrics-store.js";import type { ResilientProvider } from "../providers/resilient.js";import { VERSION } from "../version.js";import { timingSafeCompare } from "../auth.js";+import { isSlotsEnabled, isReflectEnabled } from "../functions/slots.js";import { renderViewerDocument } from "../viewer/document.js";import { getBoundViewerPort, getViewerSkipped } from "../viewer/server.js";import { MAX_FILES_UPPER_BOUND } from "../functions/replay.js";@@ -90,6 +91,24 @@ function consolidationDisabledResponse(): Response {});}+function slotsDisabledResponse(): Response {+ return flagDisabledResponse({+ error: "Memory slots not enabled",+ flag: "AGENTMEMORY_SLOTS",+ enableHow: "Set AGENTMEMORY_SLOTS=true (in ~/.agentmemory/.env or the shell) and restart.",+ docsHref: "https://github.com/rohitg00/agentmemory#memory-slots",+ });+}++function reflectDisabledResponse(): Response {+ return flagDisabledResponse({+ error: "Slot reflection not enabled",+ flag: "AGENTMEMORY_REFLECT",+ enableHow: "Set AGENTMEMORY_REFLECT=true (in ~/.agentmemory/.env or the shell) and restart. Requires AGENTMEMORY_SLOTS=true.",+ docsHref: "https://github.com/rohitg00/agentmemory#memory-slots",+ });+}+function asNonEmptyString(value: unknown): string | null {if (typeof value !== "string") return null;const trimmed = value.trim();@@ -1796,6 +1815,7 @@ export function registerApiTriggers(sdk.registerFunction("api::slot-list", async (req: ApiRequest): Promise<Response> => {const authErr = checkAuth(req, secret);if (authErr) return authErr;+ if (!isSlotsEnabled()) return slotsDisabledResponse();const result = await sdk.trigger({ function_id: "mem::slot-list", payload: {} });return { status_code: 200, body: result };});@@ -1808,6 +1828,7 @@ export function registerApiTriggers(sdk.registerFunction("api::slot-get", async (req: ApiRequest): Promise<Response> => {const authErr = checkAuth(req, secret);if (authErr) return authErr;+ if (!isSlotsEnabled()) return slotsDisabledResponse();const label = asNonEmptyString(req.query_params?.["label"]);if (!label) return { status_code: 400, body: { error: "label query param required" } };const result = await sdk.trigger({ function_id: "mem::slot-get", payload: { label } });@@ -1826,6 +1847,7 @@ export function registerApiTriggers(sdk.registerFunction("api::slot-create", async (req: ApiRequest): Promise<Response> => {const authErr = checkAuth(req, secret);if (authErr) return authErr;+ if (!isSlotsEnabled()) return slotsDisabledResponse();const body = (req.body ?? {}) as Record<string, unknown>;const label = asNonEmptyString(body["label"]);if (!label) return { status_code: 400, body: { error: "label required" } };@@ -1875,6 +1897,7 @@ export function registerApiTriggers(sdk.registerFunction("api::slot-append", async (req: ApiRequest): Promise<Response> => {const authErr = checkAuth(req, secret);if (authErr) return authErr;+ if (!isSlotsEnabled()) return slotsDisabledResponse();const body = (req.body ?? {}) as Record<string, unknown>;const label = asNonEmptyString(body["label"]);const text = typeof body["text"] === "string" ? body["text"] : null;@@ -1897,6 +1920,7 @@ export function registerApiTriggers(sdk.registerFunction("api::slot-replace", async (req: ApiRequest): Promise<Response> => {const authErr = checkAuth(req, secret);if (authErr) return authErr;+ if (!isSlotsEnabled()) return slotsDisabledResponse();const body = (req.body ?? {}) as Record<string, unknown>;const label = asNonEmptyString(body["label"]);const content = body["content"];@@ -1921,6 +1945,7 @@ export function registerApiTriggers(sdk.registerFunction("api::slot-delete", async (req: ApiRequest): Promise<Response> => {const authErr = checkAuth(req, secret);if (authErr) return authErr;+ if (!isSlotsEnabled()) return slotsDisabledResponse();const label = asNonEmptyString(req.query_params?.["label"]);if (!label) return { status_code: 400, body: { error: "label query param required" } };const result = await sdk.trigger({ function_id: "mem::slot-delete", payload: { label } });@@ -1939,6 +1964,8 @@ export function registerApiTriggers(sdk.registerFunction("api::slot-reflect", async (req: ApiRequest): Promise<Response> => {const authErr = checkAuth(req, secret);if (authErr) return authErr;+ if (!isSlotsEnabled()) return slotsDisabledResponse();+ if (!isReflectEnabled()) return reflectDisabledResponse();const body = (req.body ?? {}) as Record<string, unknown>;const sessionId = asNonEmptyString(body["sessionId"]);if (!sessionId) return { status_code: 400, body: { error: "sessionId required" } };diff --git a/test/slots-flag-gate.test.ts b/test/slots-flag-gate.test.tsnew file mode 100644index 00000000..287bcbd8--- /dev/null+++ b/test/slots-flag-gate.test.ts@@ -0,0 +1,90 @@+import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";+import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";++// Regression tests for #678:+// - isSlotsEnabled / isReflectEnabled must read from ~/.agentmemory/.env+// (not only process.env), so users who set AGENTMEMORY_SLOTS in the+// dotfile see the flag take effect.+// - HTTP triggers must return 503 with enableHow when the flag is off,+// not 500.++describe("isSlotsEnabled — reads merged env (#678)", () => {+ let home: string;+ let ORIG_HOME: string | undefined;+ let ORIG_FLAG: string | undefined;++ beforeEach(() => {+ home = mkdtempSync(join(tmpdir(), "am-slots-flag-"));+ mkdirSync(join(home, ".agentmemory"), { recursive: true });+ ORIG_HOME = process.env["HOME"];+ ORIG_FLAG = process.env["AGENTMEMORY_SLOTS"];+ process.env["HOME"] = home;+ delete process.env["AGENTMEMORY_SLOTS"];+ vi.resetModules();+ });++ afterEach(() => {+ if (ORIG_HOME !== undefined) process.env["HOME"] = ORIG_HOME;+ if (ORIG_FLAG !== undefined) process.env["AGENTMEMORY_SLOTS"] = ORIG_FLAG;+ else delete process.env["AGENTMEMORY_SLOTS"];+ rmSync(home, { recursive: true, force: true });+ });++ it("returns false when neither process.env nor .env sets the flag", async () => {+ const { isSlotsEnabled } = await import("../src/functions/slots.js");+ expect(isSlotsEnabled()).toBe(false);+ });++ it("returns true when AGENTMEMORY_SLOTS=true lives only in ~/.agentmemory/.env", async () => {+ writeFileSync(+ join(home, ".agentmemory", ".env"),+ "AGENTMEMORY_SLOTS=true\n",+ );+ const { isSlotsEnabled } = await import("../src/functions/slots.js");+ expect(isSlotsEnabled()).toBe(true);+ });++ it("returns true when process.env wins over .env (existing behaviour preserved)", async () => {+ writeFileSync(+ join(home, ".agentmemory", ".env"),+ "AGENTMEMORY_SLOTS=false\n",+ );+ process.env["AGENTMEMORY_SLOTS"] = "true";+ const { isSlotsEnabled } = await import("../src/functions/slots.js");+ expect(isSlotsEnabled()).toBe(true);+ });+});++describe("isReflectEnabled — reads merged env (#678)", () => {+ let home: string;+ let ORIG_HOME: string | undefined;+ let ORIG_FLAG: string | undefined;++ beforeEach(() => {+ home = mkdtempSync(join(tmpdir(), "am-reflect-flag-"));+ mkdirSync(join(home, ".agentmemory"), { recursive: true });+ ORIG_HOME = process.env["HOME"];+ ORIG_FLAG = process.env["AGENTMEMORY_REFLECT"];+ process.env["HOME"] = home;+ delete process.env["AGENTMEMORY_REFLECT"];+ vi.resetModules();+ });++ afterEach(() => {+ if (ORIG_HOME !== undefined) process.env["HOME"] = ORIG_HOME;+ if (ORIG_FLAG !== undefined) process.env["AGENTMEMORY_REFLECT"] = ORIG_FLAG;+ else delete process.env["AGENTMEMORY_REFLECT"];+ rmSync(home, { recursive: true, force: true });+ });++ it("returns true when AGENTMEMORY_REFLECT=true is only in ~/.agentmemory/.env", async () => {+ writeFileSync(+ join(home, ".agentmemory", ".env"),+ "AGENTMEMORY_REFLECT=true\n",+ );+ const { isReflectEnabled } = await import("../src/functions/slots.js");+ expect(isReflectEnabled()).toBe(true);+ });+});