Skip to content

feat(core): add sqlite long-term memory persistence with teach, recall, and learn - #48498

Open
prethiv wants to merge 5 commits into
anomalyco:devfrom
prethiv:dev
Open

feat(core): add sqlite long-term memory persistence with teach, recall, and learn#48498
prethiv wants to merge 5 commits into
anomalyco:devfrom
prethiv:dev

Conversation

@prethiv

@prethiv prethiv commented Sep 11, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #48497

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Implements an SQLite-backed long-term persistence memory system for OpenCode:

  1. Storage Engine (packages/core/src/memory):

    • SQLite .db persistence located in Global.Path.data (memory.db) with WAL mode.
    • SQLite FTS5 virtual table (memory_fts) with triggers for fast full-text search and BM25 ranking.
    • Core API supporting teach, recall, learn, list, get, and remove.
  2. Agent Tool & Context (packages/opencode/src/tool/memory.ts):

    • Conversational memory tool registered in ToolRegistry with actions for teach, recall, learn, list, and delete.
    • System prompt instructions so agents proactively consult and persist guidelines.
  3. CLI & Slash Commands (packages/opencode):

    • Registered CLI commands: opencode memory [teach|recall|learn|list|delete] with top-level aliases (opencode teach, opencode recall, opencode learn, opencode memory).
    • Registered built-in TUI slash commands: /teach, /recall, /learn, and /memory.
  4. TUI Explorer Dialog (packages/tui/src/component/dialog-memory.tsx):

    • Interactive memory browser dialog wired to /memory and command palette prompt.memory.

How did you verify your code works?

  1. Unit tests: Ran bun test test/memory.test.ts in packages/core (2/2 passing).
  2. Integration tests: Ran bun test test/tool/memory.test.ts in packages/opencode (1/1 passing).
  3. Typechecking: Ran bun turbo typecheck across all 36 workspace packages (30/30 passed).
  4. CLI verification:
    • bun dev teach "Always use bun test for tests" --category "convention"
    • bun dev recall "bun test" (verified FTS5 match returned)
    • bun dev memory list (verified stored items listed)
    • bun dev learn "Insight text" --title "Test Insight"

Screenshots / recordings

Attached a screenshot or terminal recording of the /memory TUI dialog and CLI output._

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Found 2 potentially related PRs (excluding the current PR #48498):

  1. PR feat(memory): add agent_memory table and memory-tools plugin #42425: feat(memory): add agent_memory table and memory-tools plugin

  2. PR feat(opencode): native auto-memory for cross-session learning #20344: feat(opencode): native auto-memory for cross-session learning

These PRs may be addressing similar memory persistence and learning features. Check if they're already closed/merged or if there's overlap in implementation.

@prethiv

prethiv commented Sep 11, 2026

Copy link
Copy Markdown
Author
open_source_MT_1 open_source_MT_2 open_source_MT_3 Manual Testing Screenshots

@prethiv

prethiv commented Sep 11, 2026

Copy link
Copy Markdown
Author

Summary of Review Fixes

All review comments have been resolved across core, tool, and TUI packages:

  1. FTS5 & bm25 Safety & Observability (packages/core/src/memory/memory.ts):

    • initDatabase now surfaces descriptive warnings with dbPath and records errors if FTS5 setup fails.
    • Startup detection checks both hasFtsTable and hasBm25, emitting structured Effect telemetry via Effect.annotateLogs({ dbPath, fts, bm25 }).
    • Exposed fts: { available: boolean, bm25: boolean } on Memory.Interface and Service.of.
    • recall() gracefully falls back to time_created DESC or LIKE search when FTS/bm25 are unavailable.
    • Added PRAGMA user_version = 1 and documented SQLite implicit rowid semantics with content_rowid='rowid'.
  2. Session Context Learning (packages/opencode/src/tool/memory.ts):

    • When learn is called without content, recent session messages are extracted, formatted into excerpts, and returned to guide the agent in distilling teachings.
    • Parameter description updated to clarify this workflow.
  3. TUI Dialog Error Propagation (packages/tui/src/component/dialog-memory.tsx):

    • Replaced inner silent catch in loadMemories() with try ... finally { db.close() }, allowing database errors to properly display in the dialog's error view.
  4. CI-Resilient Unit Tests (packages/core/test/memory.test.ts & packages/opencode/test/tool/memory.test.ts):

    • Tests assert boolean types (typeof memory.fts.available === "boolean") and verify end-to-end teach + recall portability across diverse SQLite builds.
    • Added test coverage for session message extraction in tool.memory.

@prethiv

prethiv commented Sep 11, 2026

Copy link
Copy Markdown
Author

Manual tested with the latest file system and fallback copilot review fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Long-term persistence memory system (/teach, /recall, /learn, /memory)

1 participant