Skip to content

memoryTool

JavaScript · package toolnexus · SPEC §7E · js/src/agents/home.ts

function memoryTool(dir: string): Tool

Builds the memory tool — one tool, three actions (add, replace, remove) over two files in dir: MEMORY.md (target: "self", the default — the agent’s own durable notes) and USER.md (target: "user" — its model of the user). It is source: "native" under the hood (built with defineTool), and it is not one of the default builtins — it exists only where a persona wires it in, either via fromDir (on by default) or by adding memoryTool(dir) to uses.tools yourself.

Wire memoryTool in whenever a long-lived agent needs to remember things across sessions without you writing the file I/O by hand — “the user prefers metric units,” “last week’s incident was a DNS misconfiguration,” anything worth carrying forward. Every action writes straight to disk; there is no in-memory-only mode.

The tool deliberately does not touch the running session — the description string handed to the model says so, and the mechanics back it up: a write lands on disk immediately, but the composed soul is a frozen snapshot for the whole run (§7E), so the change is visible only from the next session onward. That is what keeps a long-lived persona cache-stable: the system prompt never mutates mid-conversation just because a tool call happened.

import assert from "node:assert"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { agents } from "toolnexus"
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "persona-"))
const memory = agents.memoryTool(dir)
assert.equal(memory.name, "memory")
assert.equal(memory.source, "native")
assert.deepEqual(memory.inputSchema.properties?.action.enum, ["add", "replace", "remove"])
const res = await memory.execute({ action: "add", text: "prefers metric units" })
assert.equal(res.isError, false)
assert.match(fs.readFileSync(path.join(dir, "MEMORY.md"), "utf8"), /prefers metric units/)
console.log("ok:", res.output)

2. replace and remove — a missing substring is a loud error, never a silent no-op

Section titled “2. replace and remove — a missing substring is a loud error, never a silent no-op”
import assert from "node:assert"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { agents } from "toolnexus"
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "persona-"))
const memory = agents.memoryTool(dir)
await memory.execute({ action: "add", text: "prefers metric units" })
const replaced = await memory.execute({ action: "replace", text: "prefers metric units", with: "prefers SI units" })
assert.equal(replaced.isError, false)
assert.match(fs.readFileSync(path.join(dir, "MEMORY.md"), "utf8"), /prefers SI units/)
// The substring is no longer there — replace/remove on a miss is a LOUD isError, not a no-op.
const missed = await memory.execute({ action: "remove", text: "prefers metric units" })
assert.equal(missed.isError, true)
assert.equal(missed.output, "not found: prefers metric units")
console.log("ok:", replaced.output, "|", missed.output)

3. target: "user" writes USER.md instead of MEMORY.md

Section titled “3. target: "user" writes USER.md instead of MEMORY.md”
import assert from "node:assert"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { agents } from "toolnexus"
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "persona-"))
const memory = agents.memoryTool(dir)
const self = await memory.execute({ action: "add", text: "shipped the v2 migration" })
const user = await memory.execute({ action: "add", text: "likes terse status updates", target: "user" })
assert.match(fs.readFileSync(path.join(dir, "MEMORY.md"), "utf8"), /shipped the v2 migration/)
assert.match(fs.readFileSync(path.join(dir, "USER.md"), "utf8"), /likes terse status updates/)
// Both report the SAME session-boundary contract — the write lands now, loads next session.
assert.match(self.output, /loads next session/)
assert.match(user.output, /loads next session/)
console.log("ok:", self.output, "|", user.output)

The tool’s own inputSchema — what the model fills in on a call:

Field Type What it does
action "add" | "replace" | "remove" Required. Which mutation to apply.
target "self" | "user" self → MEMORY.md (default). user → USER.md.
text string Required. For add: the entry. For replace/remove: the existing substring to match.
with string For replace: the replacement text.
memoryTool(dir: string, opts?: { notes?: boolean }): Tool
fromDir(dir, { notes?: boolean, memoryIndexCap?: boolean | { maxLines?: number; maxBytes?: number } })

Turn on notes (off by default) and the same memory tool grows a second tier. MEMORY.md stays a short index loaded every session; the detail lives in notes at <dir>/memory/<slug>.md, which are never injected into the prompt and are read only on demand. With notes off, the tool’s schema, description and behaviour are exactly as above.

Addition What it does
target: "note:<slug>" add / replace / remove on memory/<slug>.md. add to a missing note creates it (and memory/).
action: "read" Returns a file’s content (target required: self, user, or a note). A missing note is an error.
action: "list" One line per note, <slug>\t<bytes>, sorted by slug; (no notes) when empty.

A slug matches ^[a-z0-9][a-z0-9._-]{0,63}$ and may not contain ..; anything else is an error before any file is touched. replace and remove change only the first occurrence of the text, in every target. Writes are atomic (temp file, then rename), and writes to the same file in one process are serialised, so parallel adds never lose an entry. Two processes writing the same file: the last write wins.

import { agents } from "toolnexus"
const memory = agents.memoryTool("./personas/ava", { notes: true })
await memory.execute({ action: "add", target: "note:travel", text: "prefers aisle seats" })
await memory.execute({ action: "add", text: "travel prefs: see [[travel]]" }) // the index line
console.log((await memory.execute({ action: "list" })).output) // travel\t22
console.log((await memory.execute({ action: "read", target: "note:travel" })).output) // - prefers aisle seats

The 2 MB cap only limits what goes into the prompt. Writes always read and rewrite the whole file, so a MEMORY.md over 2 MB is never cut short on disk. The separate memoryIndexCap option on fromDir caps the injected index to whole lines (true = 200 lines / 25600 bytes) and adds a notice telling the model to move detail into notes.

  • composeSoul — Build a persona’s system prompt from its home directory: identity, memory, skills.
  • fromDir — Point at an agent home directory and get a configured agent back; wires this tool by default.
  • defineTool — What memoryTool is built with.
  • dreamer — The built-in agent that keeps notes true against the transcripts.
  • transcriptSearchTool — Read-only search over past conversations.