Skip to content

An open-source, faster Devin alternative

TypeScript

api-edge: pass project memory document routes through

Igor Zalutskia0664cbd440e
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.ts
index c79882a0..7bd5dd2b 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts
@@ -1846,4 +1846,315 @@ describe("managed agents proxy", () => {
expect(serialized).toContain("research-assistant:digest");
expect(serialized).not.toMatch(/artifact|bucket|imageArn|imageVersion/i);
});
+
+ // Project memory (docs/agents/document-memory.mdx). The fake backend below
+ // answers with the documented bodies and headers; the edge must hand them
+ // to the caller unchanged, including the conditional-request headers.
+ const memoryEnv = {
+ OC_MANAGED_AGENTS_SECRET: "test-secret",
+ MANAGED_AGENTS_API_URL: "https://managedagents.test",
+ };
+ const memoryCaller = { orgID: "org_test", userID: "user_test" };
+ const workshopDocument = {
+ id: "workshop",
+ title: "Workshop requirements",
+ text: "Exercises must run on Node.js 22.",
+ summary: "Workshop runtime requirements.",
+ agentWrites: "enabled",
+ revision: "rev-7",
+ bytes: 33,
+ maxBytes: 8192,
+ updatedAt: "2026-09-10T12:00:00.000Z",
+ writer: { kind: "agent", sessionId: "ses_1", accountId: "acc_private" },
+ accountId: "acc_private",
+ };
+
+ it("reads a memory document with its ETag and no backend-only fields", async () => {
+ const fetchSpy = vi.fn(async (target: RequestInfo | URL) => {
+ expect(String(target)).toBe(
+ "https://managedagents.test/v1/projects/prj_1/memory/requirements/documents/workshop?environment=development",
+ );
+ return Response.json(workshopDocument, {
+ headers: { etag: '"rev-7"', "x-upstream": "private" },
+ });
+ });
+ vi.stubGlobal("fetch", fetchSpy);
+
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory/requirements/documents/workshop?environment=development",
+ ),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+
+ expect(response.status).toBe(200);
+ expect(response.headers.get("etag")).toBe('"rev-7"');
+ expect(response.headers.get("cache-control")).toBe("no-store");
+ expect(response.headers.get("x-upstream")).toBeNull();
+ expect(await response.json()).toEqual({
+ id: "workshop",
+ title: "Workshop requirements",
+ text: "Exercises must run on Node.js 22.",
+ summary: "Workshop runtime requirements.",
+ agentWrites: "enabled",
+ revision: "rev-7",
+ bytes: 33,
+ maxBytes: 8192,
+ updatedAt: "2026-09-10T12:00:00.000Z",
+ writer: { kind: "agent", sessionId: "ses_1" },
+ });
+ });
+
+ it("lists memory document metadata with the next cursor and without text", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json({
+ documents: [workshopDocument],
+ nextCursor: "cursor-2",
+ }),
+ ),
+ );
+
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory/requirements/documents?environment=development&cursor=cursor-1",
+ ),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+ const body = (await response.json()) as {
+ documents: Array<Record<string, unknown>>;
+ nextCursor: string | null;
+ };
+
+ expect(response.status).toBe(200);
+ expect(body.nextCursor).toBe("cursor-2");
+ expect(body.documents).toHaveLength(1);
+ expect(body.documents[0]).toMatchObject({
+ id: "workshop",
+ revision: "rev-7",
+ writer: { kind: "agent", sessionId: "ses_1" },
+ });
+ expect(body.documents[0]).not.toHaveProperty("text");
+ expect(JSON.stringify(body)).not.toContain("acc_private");
+ });
+
+ it("forwards memory conditional headers and returns the documented status codes", async () => {
+ const fetchSpy = vi.fn(async (_target: RequestInfo | URL, init?: RequestInit) => {
+ const method = init?.method ?? "GET";
+ const headers = new Headers(init?.headers);
+ if (method === "PUT" && headers.get("if-none-match") === "*") {
+ expect(headers.get("if-match")).toBeNull();
+ expect(await new Response(init?.body).json()).toEqual({
+ title: "Workshop requirements",
+ text: "Exercises must run on Node.js 22.",
+ });
+ return Response.json(workshopDocument, {
+ status: 201,
+ headers: { etag: '"rev-7"' },
+ });
+ }
+ if (method === "PATCH") {
+ expect(headers.get("if-match")).toBe('"rev-7"');
+ return Response.json(
+ { ...workshopDocument, agentWrites: "disabled", revision: "rev-8" },
+ { headers: { etag: '"rev-8"' } },
+ );
+ }
+ if (method === "DELETE") {
+ expect(headers.get("if-match")).toBe('"rev-8"');
+ return new Response(null, { status: 204 });
+ }
+ throw new Error(`unexpected ${method}`);
+ });
+ vi.stubGlobal("fetch", fetchSpy);
+ const path =
+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory/requirements/documents/workshop?environment=development";
+
+ const created = await proxyManagedAgents(
+ new Request(path, {
+ method: "PUT",
+ headers: {
+ "content-type": "application/json",
+ "if-none-match": "*",
+ },
+ body: JSON.stringify({
+ title: "Workshop requirements",
+ text: "Exercises must run on Node.js 22.",
+ }),
+ }),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+ expect(created.status).toBe(201);
+ expect(created.headers.get("etag")).toBe('"rev-7"');
+
+ const patched = await proxyManagedAgents(
+ new Request(path, {
+ method: "PATCH",
+ headers: { "content-type": "application/json", "if-match": '"rev-7"' },
+ body: JSON.stringify({ agentWrites: "disabled" }),
+ }),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+ expect(patched.status).toBe(200);
+ expect(patched.headers.get("etag")).toBe('"rev-8"');
+ expect(await patched.json()).toMatchObject({
+ agentWrites: "disabled",
+ revision: "rev-8",
+ });
+
+ const deleted = await proxyManagedAgents(
+ new Request(path, {
+ method: "DELETE",
+ headers: { "if-match": '"rev-8"' },
+ }),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+ expect(deleted.status).toBe(204);
+ expect(await deleted.text()).toBe("");
+ expect(fetchSpy).toHaveBeenCalledTimes(3);
+ });
+
+ it("passes memory client errors through in the documented envelope", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json(
+ {
+ error: {
+ code: "precondition_failed",
+ message: "The document changed since revision rev-6.",
+ },
+ },
+ { status: 412 },
+ ),
+ ),
+ );
+
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory/requirements/documents/workshop?environment=development",
+ {
+ method: "PUT",
+ headers: { "content-type": "application/json", "if-match": '"rev-6"' },
+ body: JSON.stringify({ text: "stale" }),
+ },
+ ),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+
+ expect(response.status).toBe(412);
+ expect(await response.json()).toEqual({
+ error: {
+ code: "precondition_failed",
+ message: "The document changed since revision rev-6.",
+ },
+ });
+ });
+
+ it("keeps the generic redaction for memory backend failures", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json(
+ {
+ error: {
+ code: "memory_storage_failed",
+ message: "Durable Object MEMORY threw in SQLite",
+ },
+ },
+ { status: 503 },
+ ),
+ ),
+ );
+
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory/requirements/documents?environment=development",
+ ),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+ const body = (await response.json()) as {
+ error: { code: string; message: string };
+ };
+
+ expect(response.status).toBe(503);
+ expect(body.error.code).toBe("memory_storage_failed");
+ expect(body.error.message).not.toMatch(/durable|sqlite/i);
+ });
+
+ it("rejects memory methods the management API does not document", async () => {
+ const fetchSpy = vi.fn();
+ vi.stubGlobal("fetch", fetchSpy);
+
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory/requirements/documents?environment=development",
+ { method: "POST", body: "{}" },
+ ),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+
+ expect(response.status).toBe(404);
+ expect(fetchSpy).not.toHaveBeenCalled();
+ });
+
+ it("returns session memory bindings with their writable flag", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json({
+ id: "ses_1",
+ agentId: "hello-world",
+ status: "running",
+ memory: [
+ {
+ resource: "requirements",
+ scope: "document",
+ id: "workshop",
+ access: "read-write",
+ writable: true,
+ },
+ ],
+ runtimeToken: "private",
+ }),
+ ),
+ );
+
+ const response = await proxyManagedAgents(
+ new Request("https://app.opencomputer.dev/api/managed-agents/sessions/ses_1"),
+ memoryEnv,
+ memoryCaller,
+ "/api/managed-agents",
+ );
+ const body = (await response.json()) as Record<string, unknown>;
+
+ expect(response.status).toBe(200);
+ expect(body.memory).toEqual([
+ {
+ resource: "requirements",
+ scope: "document",
+ id: "workshop",
+ access: "read-write",
+ writable: true,
+ },
+ ]);
+ expect(body).not.toHaveProperty("runtimeToken");
+ });
});
diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.ts
index a916bb5d..71f8c44f 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.ts
@@ -215,6 +215,103 @@ function strings(value: unknown): string[] {
: [];
}
+// Project memory (docs/agents/document-memory.mdx, "Management API"). The
+// backend mirrors the public routes one-to-one, so the edge passes the
+// documented bodies, status codes, error envelope and the conditional
+// headers (`ETag`, `If-Match`, `If-None-Match`) through untouched.
+const MEMORY_DOCUMENTS_ROUTE = /^\/projects\/[^/]+\/memory\/[^/]+\/documents$/;
+const MEMORY_DOCUMENT_ROUTE =
+ /^\/projects\/[^/]+\/memory\/[^/]+\/documents\/[^/]+$/;
+const MEMORY_CONDITIONAL_REQUEST_HEADERS = ["if-match", "if-none-match"];
+
+function isMemoryRoute(method: string, suffix: string): boolean {
+ if (method === "GET" && MEMORY_DOCUMENTS_ROUTE.test(suffix)) return true;
+ return (
+ (method === "GET" ||
+ method === "PUT" ||
+ method === "PATCH" ||
+ method === "DELETE") &&
+ MEMORY_DOCUMENT_ROUTE.test(suffix)
+ );
+}
+
+function publicMemoryWriter(value: unknown): Record<string, unknown> {
+ const writer = record(value) ?? {};
+ return writer.kind === "agent"
+ ? { kind: "agent", sessionId: writer.sessionId }
+ : { kind: "owner" };
+}
+
+function publicMemoryDocumentMeta(value: unknown): Record<string, unknown> {
+ const document = record(value) ?? {};
+ return {
+ id: document.id,
+ title: document.title,
+ summary: document.summary,
+ agentWrites: document.agentWrites,
+ revision: document.revision,
+ bytes: document.bytes,
+ maxBytes: document.maxBytes,
+ updatedAt: document.updatedAt,
+ writer: publicMemoryWriter(document.writer),
+ };
+}
+
+function publicMemoryDocument(value: unknown): Record<string, unknown> {
+ const document = record(value) ?? {};
+ return { ...publicMemoryDocumentMeta(document), text: document.text };
+}
+
+// Memory errors are the documented `{ error: { code, message } }` envelope
+// with user-facing messages (stale revision, oversized text, missing
+// condition header), so client errors pass through as the backend wrote them.
+// Server errors keep the generic redaction every other route gets.
+async function memoryResponse(
+ upstream: Response,
+ method: string,
+ suffix: string,
+): Promise<Response> {
+ const headers = new Headers({ "cache-control": "no-store" });
+ const etag = upstream.headers.get("etag");
+ if (etag) headers.set("etag", etag);
+ if (upstream.status === 204 || upstream.status === 304) {
+ return new Response(null, { status: upstream.status, headers });
+ }
+ if (!upstream.ok) {
+ if (upstream.status >= 500) return publicErrorResponse(upstream);
+ const body = record(await upstream.clone().json().catch(() => null));
+ const error = record(body?.error);
+ const code =
+ typeof error?.code === "string" && /^[a-z][a-z0-9_]{0,63}$/.test(error.code)
+ ? error.code
+ : null;
+ const message =
+ typeof error?.message === "string" ? error.message.slice(0, 1_000) : null;
+ if (!code || message === null) return publicErrorResponse(upstream);
+ headers.set("content-type", "application/json");
+ return new Response(JSON.stringify({ error: { code, message } }), {
+ status: upstream.status,
+ headers,
+ });
+ }
+ const body = record(await upstream.json().catch(() => null)) ?? {};
+ headers.set("content-type", "application/json");
+ const value =
+ method === "GET" && MEMORY_DOCUMENTS_ROUTE.test(suffix)
+ ? {
+ documents: Array.isArray(body.documents)
+ ? body.documents.map(publicMemoryDocumentMeta)
+ : [],
+ nextCursor:
+ typeof body.nextCursor === "string" ? body.nextCursor : null,
+ }
+ : publicMemoryDocument(body);
+ return new Response(JSON.stringify(value), {
+ status: upstream.status,
+ headers,
+ });
+}
+
function publicDeployment(value: unknown): Record<string, unknown> {
const deployment = record(value) ?? {};
return {
@@ -1162,6 +1259,7 @@ function isAllowedManagedAgentsRoute(method: string, suffix: string): boolean {
) {
return true;
}
+ if (isMemoryRoute(method, suffix)) return true;
if (
(method === "GET" || method === "PUT" || method === "DELETE") &&
/^\/projects\/[^/]+\/runtime-variables(?:\/[^/]+)?$/.test(suffix)
@@ -1609,6 +1707,13 @@ export async function proxyManagedAgents(
);
}
const headers = copyRequestHeaders(request);
+ const memoryRoute = isMemoryRoute(method, suffix);
+ if (memoryRoute) {
+ for (const name of MEMORY_CONDITIONAL_REQUEST_HEADERS) {
+ const value = request.headers.get(name);
+ if (value) headers.set(name, value);
+ }
+ }
headers.set(
"x-opencomputer-agent-token",
await mintManagedAgentsAssertion(env.OC_MANAGED_AGENTS_SECRET, caller),
@@ -1640,6 +1745,7 @@ export async function proxyManagedAgents(
}
try {
const upstream = await fetch(target, init);
+ if (memoryRoute) return memoryResponse(upstream, method, suffix);
if (!upstream.ok) return publicErrorResponse(upstream);
if (upstream.status === 204) return new Response(null, { status: 204 });
if (/^\/projects\/[^/]+\/source-archive$/.test(suffix)) {