An open-source, faster Devin alternative
TypeScript
useMemory returns undefined for an unbound resource
Mohamed Habib via Computerabaf30798af6
diff --git a/agent/src/index.test.ts b/agent/src/index.test.tsindex 90683502..3da1f168 100644--- a/agent/src/index.test.ts+++ b/agent/src/index.test.ts@@ -462,18 +462,17 @@ test("useMemory reads the host projection and records the selection", () => {);});-test("useMemory fails a render whose session has no binding for the resource", () => {- assert.throws(- () => renderWith({}, () => useMemory("requirements")),- /Memory "requirements" is not bound to this session/,- );+test("useMemory returns undefined when the session has no binding for the resource", () => {+ const unbound = renderWith({}, () => useMemory("requirements"));+ assert.equal(unbound.result, undefined);+ assert.deepEqual(unbound.selectedMemory, []);assert.throws(() =>renderWith({ requirements: { text: "x" } as never },() => useMemory("requirements"),),- /Memory "requirements" is not bound to this session/,+ /Memory "requirements" projection from the host is malformed/,);assert.throws(() => renderWith({}, () => useMemory("Not Valid")));assert.throws(diff --git a/agent/src/index.ts b/agent/src/index.tsindex 6d399105..26b34fcb 100644--- a/agent/src/index.ts+++ b/agent/src/index.ts@@ -1696,14 +1696,14 @@ export function defineTool<Output extends DataValue = DataValue>(}/**- * The projection the host recalled for this session's binding of `memory`.- * It never fetches: a session without that binding fails the render here,- * before inference. Calling it also selects the binding's permitted tools for- * this model request; omitting it exposes none of them.+ * The projection the host recalled for this session's binding of `memory`,+ * or undefined when the session has no such binding. It never fetches.+ * Calling it also selects the binding's permitted tools for this model+ * request; omitting it exposes none of them.*/export function useMemory(memory: string | ResourceReference,-): MemoryProjection {+): MemoryProjection | undefined {const id = memoryId(typeof memory === "string" ? memory : memory.id,"useMemory",diff --git a/agent/src/memory.ts b/agent/src/memory.tsindex 0c695998..144e85e7 100644--- a/agent/src/memory.ts+++ b/agent/src/memory.ts@@ -402,10 +402,15 @@ export function memoryToolNames(provider: MemoryProvider): string[] {}/**- * The projection the host resolved for the session's binding of `id`, or a- * render failure when the session has no such binding.+ * The projection the host resolved for the session's binding of `id`, or+ * undefined when the session has no such binding. A host value of any other+ * shape is a host bug and fails the render.*/-export function memoryProjection(id: string, value: unknown): MemoryProjection {+export function memoryProjection(+ id: string,+ value: unknown,+): MemoryProjection | undefined {+ if (value === undefined || value === null) return undefined;if (!isRecord(value) ||typeof value.text !== "string" ||@@ -413,7 +418,7 @@ export function memoryProjection(id: string, value: unknown): MemoryProjection {typeof value.writable !== "boolean") {throw new Error(- `Memory ${JSON.stringify(id)} is not bound to this session; create the session with a memory binding for it`,+ `Memory ${JSON.stringify(id)} projection from the host is malformed`,);}return value as unknown as MemoryProjection;diff --git a/cli/src/memory.ts b/cli/src/memory.tsindex 0c695998..144e85e7 100644--- a/cli/src/memory.ts+++ b/cli/src/memory.ts@@ -402,10 +402,15 @@ export function memoryToolNames(provider: MemoryProvider): string[] {}/**- * The projection the host resolved for the session's binding of `id`, or a- * render failure when the session has no such binding.+ * The projection the host resolved for the session's binding of `id`, or+ * undefined when the session has no such binding. A host value of any other+ * shape is a host bug and fails the render.*/-export function memoryProjection(id: string, value: unknown): MemoryProjection {+export function memoryProjection(+ id: string,+ value: unknown,+): MemoryProjection | undefined {+ if (value === undefined || value === null) return undefined;if (!isRecord(value) ||typeof value.text !== "string" ||@@ -413,7 +418,7 @@ export function memoryProjection(id: string, value: unknown): MemoryProjection {typeof value.writable !== "boolean") {throw new Error(- `Memory ${JSON.stringify(id)} is not bound to this session; create the session with a memory binding for it`,+ `Memory ${JSON.stringify(id)} projection from the host is malformed`,);}return value as unknown as MemoryProjection;diff --git a/cli/src/project.test.ts b/cli/src/project.test.tsindex 85cd43b6..d9ea4383 100644--- a/cli/src/project.test.ts+++ b/cli/src/project.test.ts@@ -2317,7 +2317,7 @@ export default function Agent() {const saved = useMemory(requirements);if (input.text?.includes("facts")) useMemory(knowledge);useModel("anthropic/claude-sonnet-4.6");- return "Saved requirements (JSON string): " + JSON.stringify(saved.text) + " writable=" + String(saved.writable);+ return "Saved requirements (JSON string): " + JSON.stringify(saved?.text) + " writable=" + String(saved?.writable);}`,);@@ -2383,10 +2383,10 @@ export default function Agent() {);assert.deepEqual([...scope.selectedMemory], ["requirements"]);scope.input = { source: "user", text: "facts please" };- assert.throws(- () => module.default(),- /Memory "knowledge" is not bound to this session/,- );+ scope.selectedMemory.clear();+ // An unbound resource renders as undefined instead of failing.+ assert.equal(typeof module.default(), "string");+ assert.deepEqual([...scope.selectedMemory], ["requirements"]);} finally {delete (globalThis as Record<PropertyKey, unknown>)[Symbol.for("opencomputer.agent-hooks")diff --git a/cli/src/project.ts b/cli/src/project.tsindex 543a20b1..d3840adf 100644--- a/cli/src/project.ts+++ b/cli/src/project.ts@@ -3484,7 +3484,7 @@ export const publishOutbox = async (outbox, input) => {// The host resolves every session binding before the render and exposes the// projections on its render scope as scope.memory[id]; the hook returns one// and the host records the id in scope.selectedMemory, returned to it as-// selectedMemory next to enabledTools. An unbound id fails the render here.+// selectedMemory next to enabledTools. An unbound id returns undefined.export const useMemory = (memory) => {const resource = memoryId(typeof memory === "string" ? memory : memory && memory.id, "useMemory");return memoryProjection(resource, hooks().useMemory(resource));diff --git a/docs/agents/document-memory.mdx b/docs/agents/document-memory.mdxindex 029bc073..a75abcea 100644--- a/docs/agents/document-memory.mdx+++ b/docs/agents/document-memory.mdx@@ -76,7 +76,7 @@ Set `memory.<resource>` when creating a session:it suits a single owner or a coordinating agent, not one user among many.Calling `useMemory` exposes the binding's tools; omitting it exposes none.-An unbound hook fails before inference. Tools accept a `memory` resource ID,+An unbound hook returns `undefined`, so an agent can render without the binding. Tools accept a `memory` resource ID,required when multiple resources **selected by that render** offer the tool.Descriptions list those eligible resources and their declared guidance.