opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[FEATURE]: Auto-memory like Claude Code: per-project memory files, auto-loaded, agent-curated

Open Art4Man opened this issue 1 week ago • 4 comments

Verification

  • [x] I searched existing issues. Direct ancestors of this request: #16077, #8043, #9211, #43068, #48497, #52934, and the canonical #20322.
  • [x] #20322 was closed in Oct 2026 by the 60-day inactivity bot while still assigned — not a maintainer decision — and no native memory feature has shipped since (the docs describe none as of 2026-11-23).

Problem

OpenCode has no way to persist learnings across sessions. AGENTS.md is fully manual, so project conventions, user corrections and infra lessons only survive if the user types them into a file by hand. Third-party memory plugins fill the gap with SQLite/RAG/MCP machinery — heavy infrastructure for what is ultimately "remember these facts next session".

Describe the enhancement you want to request

Adopt Claude Code's file-based auto-memory model. It fits OpenCode's existing plumbing almost perfectly:

  1. Store — plain markdown, one scope per project:
    • per-project: ~/.local/share/opencode/memory/<project-id>/*.md
    • global: ~/.config/opencode/memory/*.md
    • one MEMORY.md index + one topic file per lesson, frontmatter with name / description / type.
  2. Load — inject the matching scope's files at session start. Or zero new plumbing: simply bless/document the pattern of pointing the existing instructions globs at these directories (that already works today — I use it in production).
  3. Write — the agent curates: when it learns something durable (a user correction, a project convention, an infra fact), it writes/updates a topic file and appends an index line. Opt-in config, e.g. { "memory": { "enabled": true } }.

Why files instead of SQLite (how this differs from #20322)

Portable, diffable, greppable, no new dependencies — and critically, cross-agent. I run Claude Code, OpenCode and pi side by side on the same projects. Claude Code's native memory directory (~/.claude/projects/<slug>/memory/) is already my single source of truth: OpenCode reads it via instructions globs, pi via a generated AGENTS.md index. It works well. The only missing piece is that OpenCode and pi cannot write to it natively.

A file convention lets one memory store serve every agent a person uses. A SQLite blob serves exactly one.

Claude Code shipped this exact UX and it works in practice — this request is parity, not invention.

Art4Man avatar Nov 23 '26 11:11 Art4Man

Thanks for the detailed write-up! You're asking for a native, file-based auto-memory like Claude Code's. Per-project and global markdown memory directories, with a MEMORY.md index plus one topic file per lesson, would be loaded automatically at session start, and the agent would curate them itself, opt-in through config. Maintainers will consider it. I can't promise whether or when it will land.

Related earlier requests (all closed, which is why this one stays open):

  • #20322: native auto-memory for cross-session learning, SQLite-based, closed as not planned
  • #48497: SQLite memory with /teach, /recall, /learn, /memory, closed as a duplicate
  • #43068: per-folder notebook memory (notes_get / notes_commit)
  • #52934: documenting external memory as a plugin category
  • #16077: persistent session memory

You can have most of this now as a plugin

The V2 plugin API covers both parts of this. A context session hook can add text to the system instructions of each agent-loop request, and ctx.tool.transform can register a tool. Here is a plugin that loads ~/.config/opencode/memory/*.md (global) and ~/.local/share/opencode/memory/<project-id>/*.md (per project) into every agent-loop request. It also gives the agent a memory_save tool that writes a topic file with name / description / type frontmatter and appends a line to MEMORY.md.

Save it as ~/.config/opencode/plugins/file-memory/index.ts; global plugins there are discovered automatically, and .opencode/plugins/ works per project. To share the Claude Code store, pass a dir option pointing at ~/.claude/projects/<slug>/memory with the object form of plugins in opencode.json(c); the plugin reads it from ctx.options. You could also keep loading that directory through your instructions globs.

import { Plugin } from "@opencode/plugin"
import { mkdir, readdir, readFile, writeFile, appendFile } from "node:fs/promises"
import { homedir } from "node:os"
import path from "node:path"

const load = async (dir: string) => {
  const files = await readdir(dir).catch(() => [] as string[])
  const parts = await Promise.all(
    files
      .filter((f) => f.endsWith(".md"))
      .sort((a, b) => (a === "MEMORY.md" ? -1 : b === "MEMORY.md" ? 1 : a.localeCompare(b)))
      .map(async (f) => `<file name="${f}">\n${await readFile(path.join(dir, f), "utf8")}\n</file>`),
  )
  return parts.join("\n")
}

export default Plugin.define({
  id: "file-memory",
  async setup(ctx) {
    const project =
      typeof ctx.options.dir === "string"
        ? ctx.options.dir
        : path.join(homedir(), ".local/share/opencode/memory", ctx.location.project.id)
    const global = path.join(homedir(), ".config/opencode/memory")

    await ctx.session.hook("context", async (event) => {
      const text = [await load(global), await load(project)].filter(Boolean).join("\n")
      event.system.push({
        type: "text",
        text:
          "You have a persistent file-based memory. When you learn something durable (a user correction, " +
          "a project convention, an infrastructure fact), save it with the memory_save tool.\n" +
          (text ? `<memory>\n${text}\n</memory>` : "<memory>(empty)</memory>"),
      })
    })

    await ctx.tool.transform((editor) => {
      editor.add({
        name: "save",
        description: "Save or update a durable memory as a topic file and add it to the MEMORY.md index",
        input: {
          type: "object",
          properties: {
            name: { type: "string", description: "Short kebab-case file name, without .md" },
            description: { type: "string", description: "One-line summary for the index" },
            type: { type: "string", enum: ["user", "project", "feedback", "reference"] },
            content: { type: "string", description: "The memory itself, in markdown" },
            scope: { type: "string", enum: ["project", "global"] },
          },
          required: ["name", "description", "type", "content"],
          additionalProperties: false,
        },
        options: { namespace: "memory" },
        execute: async (raw) => {
          const input = raw as { name: string; description: string; type: string; content: string; scope?: string }
          const dir = input.scope === "global" ? global : project
          const name = input.name.replace(/[^a-z0-9-_]/gi, "-")
          await mkdir(dir, { recursive: true })
          const file = path.join(dir, `${name}.md`)
          const existed = await readFile(file, "utf8").then(() => true, () => false)
          await writeFile(
            file,
            `---\nname: ${name}\ndescription: ${input.description}\ntype: ${input.type}\n---\n\n${input.content}\n`,
          )
          if (!existed) await appendFile(path.join(dir, "MEMORY.md"), `- [${name}](${name}.md): ${input.description}\n`)
          return { content: `Saved memory ${file}` }
        },
      })
    })
  },
})

I wrote this against the plugin API in the server plugin docs and checked it against the types in packages/plugin. I couldn't run the type-checker in my environment, though, so please treat it as a starting point and test it. It loads every memory file on every request, so a large store will use context; you may want to load only MEMORY.md and let the agent read topic files on demand.

opencodejr[bot] avatar Nov 23 '26 11:11 opencodejr[bot]

Thanks @opencodejr — I took your plugin for a spin against the exact setup from the issue (Claude Code + OpenCode sharing one memory store) and it works. Two adaptations I made, in case they're useful for others:

1. Per-session store resolution. Your version resolves the project dir from ctx.location at setup. For a global plugin I wanted the store to follow the session's project, so the context hook now resolves it per request: ctx.session.get({ sessionID }) → session.location.directory → slug (/ → -) → ~/.claude/projects/<slug>/memory. Zero per-project config, and it's literally the same store Claude Code's auto-memory writes to.

2. Index-only injection. As you suggested for large stores: the hook injects only each store's MEMORY.md (with the store path inside the block, so the agent can read topic files on demand) instead of every file. My staging project has 37 topic files; index-only keeps the per-request context cost flat.

With those two changes it's live in daily use: saved a note from an OpenCode session via memory_save, and it landed in the shared store that Claude Code reads natively — no sync script anywhere.

API notes since you mentioned you couldn't run the type-checker: works on @opencode/[email protected]; SessionContext does carry sessionID; and the running server picked up the newly installed plugin without a restart.

Art4Man avatar Nov 23 '26 12:11 Art4Man

Thanks for the write-up, and for sharing your plugin changes! Something like this is on our roadmap, but there's no ETA yet.

I'm closing this to keep the issue list tidy. The plugin approach above works well in the meantime.

neriousy avatar Nov 23 '26 16:11 neriousy

neat that the shared store already works across claude code and opencode. one thing a memory dir doesn't cover for me is 'where did this particular session stop', like half-finished decisions and the next step. i handle that with jotter.fyi (dev here): say 'jot this' at the end of a session, then 'continue my last handoff' in the other tool. it's a brief, not a transcript, and it only saves when you ask, so it sits next to a memory index rather than replacing it.

ojjsaw avatar Nov 27 '26 16:11 ojjsaw