Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore: files changed crates/openhuman-core/src/memory/README.md,gitbooks/developing/architecture/mem

Steven Enamakeld06fb970cbfc
diff --git a/crates/openhuman-core/src/memory/README.md b/crates/openhuman-core/src/memory/README.md
index b83c493ca0..b09160104c 100644
--- a/crates/openhuman-core/src/memory/README.md
+++ b/crates/openhuman-core/src/memory/README.md
@@ -1,76 +1,64 @@
# memory
-Memory v2: Recall, Fetch and Store over a pluggable engine, plus the compiled
-`context.md`. The spec is [`docs/specs/memory-v2.md`](../../../../docs/specs/memory-v2.md);
-the engine contract and engines live in `vendor/tinymemory`
-(`tinymemory-api`, `-cortex`, `-documents`, `-sources`, `-safety`, `-context`,
-`-import`). This directory is the host side: it binds an engine, adapts it to
-OpenHuman's controllers, tool and event bus, and applies OpenHuman policy
-(credentials, consent, scheduling).
+The host side of TinyMemory's agent memory lifecycle. The spec is
+[`docs/specs/memory-v2.md`](../../../../docs/specs/memory-v2.md); the engine
+contract, the lifecycle and the engines live in `vendor/tinymemory`
+(`tinymemory-api`, `tinymemory-tools`, `tinymemory-integrations`). This
+directory binds an engine, decides who is acting, calls the lifecycle around
+every turn, queues the slow work, and adapts it all to OpenHuman's
+controllers, tool, cron and event bus under OpenHuman policy (credentials,
+consent, scrubbing, scheduling).
Chat thread persistence is not memory: see [`threads/store`](../threads/store/README.md).
## Responsibilities
-- Bind the configured engine (`tinyhumans` over the host backend credential,
- or `cortexdb` with the key stored as `memory-cortexdb`); memory is **off**
- when neither is usable, and then the tool is not registered, ingestion is a
- no-op and RPCs answer `MEMORY_OFF`.
-- Serve recall, fetch, learn, forget and list; scrub every item before store.
-- Buffer committed conversation turns per thread and store one `Conversation`
- item at `batch_turns` or after `idle_secs`; flush on exit.
-- Keep the `[[memory.sources]]` registry and sync it on demand and on a cron job.
-- Compile, read and inject `context.md`.
-- Import a previous (v1) store into the selected engine after explicit consent.
+- Bind the configured engine (`tinyhumans` over the host backend credential
+ and attribution headers, or `cortexdb` with the key stored as
+ `memory-cortexdb`); memory is **off** when neither is usable: the tool is
+ not registered, the hooks do nothing and RPCs answer `MEMORY_OFF`.
+- Scrub every write (`guard`).
+- Resolve the acting identity to a layout root and memory agent id (`scope`).
+- Before each turn, log the user turn and recall its pack; after the commit,
+ log the reply; on compaction, recall what was folded away (`lifecycle`).
+- Queue and run belief builds and deferred ingests (`lifecycle::jobs`).
+- File synced sources into the brain (`sources`, `brain`).
+- Store past chats and import a v1 store, each after explicit consent.
## Key files
| File | Role |
| --- | --- |
| `engine.rs` | `resolve` binds the `[memory]` engine or says why memory is off; engines are cached per config fingerprint. |
-| `ops.rs` | Select engine, recall, fetch, learn, forget, list. `store_item` scrubs first. |
-| `explore.rs` | The explorer: `memory_explore` by facet under a `{facet, value}` path, `memory_items_get`, and the path → filter narrowing `memory_items_list` shares. |
-| `scope.rs` | Agent namespaces: the `MemoryIdentity` scoped around every agent turn and the memory node and reach it resolves to. |
-| `tools.rs` | The single `memory` agent tool (`recall`, `fetch`, `learn`, `forget`), confined to the calling agent's reach. |
-| `conversations/` | Per-thread turn buffer and idle flusher. Tool calls keep name and id only, never arguments. `backfill.rs` stores past chats from the thread store, resumably, with consent. |
+| `guard.rs` | `ScrubbingEngine`, wrapped around every bound engine. |
+| `scope.rs` | `MemoryIdentity`, `ResolvedIdentity` and their resolution order; `within_agent` / `within`. |
+| `lifecycle/mod.rs` | `AgentMemory` for an identity under the `[memory.recall]` policy. |
+| `lifecycle/hooks.rs` | `pre_turn`, `post_turn`, `compaction`; `TurnPack` and `MemoryTurn`. |
+| `lifecycle/jobs.rs` | The persisted job queue (`<workspace>/memory/jobs.json`) and the `memory_background` run. |
+| `lifecycle/views.rs` | Policy get/set, pack preview, agents list, jobs list/run. |
+| `brain.rs` | Brain source mapping and the brain ops. |
| `sources/` | Source registry, state and sync (folder, file, link, github, rss, composio). |
-| `context.rs` | Compile one `context.md` per memory node (`<workspace>/memory/context.md` for the root, `memory/context/<kind>-<id>/…` for others) and build the acting agent's `<memory-context>` injection for new sessions. |
-| `import.rs` | Consent-gated, resumable v1 import; state in `<workspace>/memory/import_state.json`. |
-| `exit.rs` | Stores buffered turns on quit within `EXIT_BUDGET` (2 s). |
-| `bus.rs` | Turn-commit subscriber and the `memory_context_refresh` / `memory_sources_sync` cron jobs. |
-| `status.rs` | The memory row of the subsystem status table. |
+| `channels.rs` | Channel → thread records for forgetting a channel. |
+| `backfill.rs` | Past chats from the thread store, resumable, with consent. |
+| `import.rs` | Consent-gated, resumable v1 import. |
+| `tools.rs` | The single `memory` agent tool and the turn's memory citations. |
+| `ops.rs`, `explore.rs` | Engine selection, recall, fetch, learn, forget, listing; the explorer. |
+| `bus.rs` | The `memory_sources_sync` / `memory_background` cron subscriber. |
| `schemas.rs`, `schemas/` | The `openhuman.memory_*` controllers. |
-| `error.rs`, `types.rs` | `MemoryError` (`MEMORY_OFF`, `UNSUPPORTED`, `INVALID_REQUEST`, `UNAUTHORIZED`, `ENGINE`) and shared types. |
+| `status.rs`, `error.rs`, `types.rs` | Subsystem status, error codes, shared types. |
| `*_tests.rs`, `test_fixtures.rs` | Sibling unit tests. |
-## RPC
-
-`openhuman.memory_engines_list`, `_engine_get`, `_engine_set`, `_recall`,
-`_fetch`, `_learn`, `_forget`, `_items_list`, `_explore`, `_items_get`,
-`_conversations_get/set`, `_conversations_backfill_status/start`,
-`_sources_list/add/remove/sync`, `_context_get/refresh/set`,
-`_import_scan/start/status`. Params and results are in the spec. The MCP server
-exposes `memory.recall`, `memory.fetch`, `memory.list`, `memory.learn` and
-`memory.forget` over the same handlers (`mcp/server/tools/specs.rs`).
-
## Persistence
-No local database. Items live in the engine. Local files: `<workspace>/memory/context.md`
-(and `memory/context/…` per agent node),
-`context_state.json`, `import_state.json`; the sources registry is `[[memory.sources]]`
-in `config.toml`; the CortexDB key is in the OS keychain.
+No local database: items live in the engine. Local files under
+`<workspace>/memory/`: `jobs.json`, `channel_threads.json`,
+`conversations_backfill.json`, `import_state.json`, `sources_state.json`. The
+sources registry is `[[memory.sources]]` in `config.toml`; the CortexDB key is
+in the OS keychain.
## Tests
-`tests/memory_v2_e2e.rs` (JSON-RPC against the mock backend),
-`app/test/playwright/specs/memory-v2.spec.ts` (UI), and the sibling `*_tests.rs`.
-
-Against a real CortexDB server, `scripts/test-memory-cortexdb-live.sh` boots the
-pinned harness in Docker (`vendor/tinymemory/integration/cortexdb/`, v0.10.4 by
-default, `CORTEXDB_VERSION=v0.9.9` for the older release) and runs
-`tests/memory_cortexdb_live.rs`. That test spawns the real `openhuman-core`
-binary with the `cortexdb` engine and checks learnings, a synced folder source,
-web-chat conversation ingestion, recall, and `context.md` (compiled by its cron
-job, written to disk, and injected into a new thread's first message). It skips
-unless `OPENHUMAN_LIVE_CORTEXDB_URL` is set, so plain test runs never need
-Docker.
+`tests/memory_v2_e2e.rs` (JSON-RPC and a full web-chat turn against the mock
+backend), `tests/memory_cortexdb_live.rs` (live CortexDB, opt-in),
+`app/test/playwright/specs/memory-v2.spec.ts` (UI), and the sibling
+`*_tests.rs`.
diff --git a/gitbooks/developing/architecture/memory.md b/gitbooks/developing/architecture/memory.md
index 22a7a700c9..c4229145a6 100644
--- a/gitbooks/developing/architecture/memory.md
+++ b/gitbooks/developing/architecture/memory.md
@@ -1,60 +1,77 @@
---
description: >-
- The Memory v2 host layer in crates/openhuman-core/src/memory/ and the
- TinyMemory crates it sits on.
+ The memory host layer in crates/openhuman-core/src/memory/: TinyMemory's
+ agent lifecycle bound to OpenHuman's turns, RPC and UI.
icon: diagram-project
---
# Memory (`crates/openhuman-core/src/memory/`)
-Memory v2 is three operations (Recall, Fetch, Store) over a pluggable engine, plus an engine-neutral `context.md`. The contract lives in TinyMemory (`vendor/tinymemory/docs/specs/memory-v2.md`); the OpenHuman side is `docs/specs/memory-v2.md`. The user-facing feature is described in [Memory](../../features/memory.md).
+OpenHuman drives TinyMemory's agent memory lifecycle around every turn: a
+memory pack recalled before the model runs, the turn logged on both sides,
+memory recalled into compaction checkpoints, and belief builds run in the
+background. The behaviour is specified in `docs/specs/memory-v2.md`; the
+user-facing feature is [Memory](../../features/memory.md).
## TinyMemory crates (`vendor/tinymemory/crates/`)
| Crate | Owns |
| --- | --- |
-| `tinymemory-api` | `MemoryEngine`, request/response types, `MemoryMeta`, `MetaFilter`, `EngineDescriptor`, `Error`. No I/O. |
-| `tinymemory-cortex` | The CortexDB engine, registered as `cortexdb` (direct `/v1/*`) and `tinyhumans` (behind the backend `/memory/*`, host bearer). |
-| `tinymemory-documents` | Format sniffing and conversion to markdown. |
-| `tinymemory-sources` | Readers for folder, file, link, github, rss and composio payloads, with the SSRF guard. |
-| `tinymemory-safety` | Secret and PII scrubbing before every store. |
-| `tinymemory-context` | `ContextCompiler` for `context.md`. |
-| `tinymemory-import` | Reads a legacy v1 workspace and yields items. |
-| `tinymemory-conformance` | Behaviour suite every engine passes, plus a reference engine. |
-| `tinymemory` | Facade: engine registry, `MemoryConfig`, `build_engine`. |
+| `tinymemory-api` | The engine contract: `MemoryEngine` (recall, fetch, store, forget, list, consolidate, beliefs), `StoreItem`, `MemoryMeta`, `MetaFilter`, `Namespace`/`Reach`, `EngineDescriptor`, `Error`. No I/O. Its `conformance` feature adds the suite every engine passes and the in-memory `ReferenceEngine` the unit tests use. |
+| `tinymemory-tools` | The agent surface over any engine: `AgentMemory` (`start_session`, `pre_turn`, `post_turn`, `recall_for_compaction`, `recall`), `MemoryLayout`, `Brain`, holistic recall (`ContextPack`), `BackgroundJob`/`BackgroundRunner`. |
+| `tinymemory-integrations` | Everything that touches the outside world: the CortexDB engine on both wires (`cortexdb` direct, `tinyhumans` behind the backend), the registry, document conversion and the brain filer, source readers (SSRF-guarded), secret/PII scrubbing, the v1 importer. |
## Host modules
| Module | Role |
| --- | --- |
-| `engine` | Binds `[memory] engine`: `tinyhumans` over the host's backend credential (resolved per request), or `cortexdb` with the key stored as `memory-cortexdb`. Engines are cached per config fingerprint. Off when neither is usable. |
-| `ops` | Select engine, recall, fetch, learn, forget, list. `store_item` scrubs before storing. |
-| `tools` | The single `memory` agent tool (`recall`, `fetch`, `learn`, `forget`); not registered when memory is off. |
-| `conversations` | Per-thread buffering of committed turns; stores a `Conversation` item at `batch_turns` or `idle_secs`. Tool calls keep name and id only. |
-| `sources` | The `[[memory.sources]]` registry, on-demand and scheduled sync. |
-| `context` | Compiles, reads and injects `<workspace>/memory/context.md` (cron job `memory_context_refresh`, default every 360 minutes). |
-| `import` | Consent-gated, resumable import of a v1 store; state in `<workspace>/memory/import_state.json`. |
-| `exit` | Flushes buffered conversation turns on quit within a 2 second budget. |
-| `bus` | Turn-commit and cron subscribers, and the idle flusher. |
-| `schemas` | The `openhuman.memory_*` controllers (engines, recall, fetch, learn, forget, items, conversations, sources, context, import). |
-| `status`, `error`, `types` | Engine status, `MEMORY_OFF` / `UNSUPPORTED` / `INVALID_REQUEST` / `UNAUTHORIZED` / `ENGINE` errors, shared types. |
+| `engine` | Binds `[memory] engine`: `tinyhumans` over the host's backend credential (resolved per request) with the transport's attribution headers, or `cortexdb` with the key stored as `memory-cortexdb`. Off when neither is usable. |
+| `guard` | `ScrubbingEngine`: every write is scrubbed under the host policy, whichever path makes it. |
+| `scope` | `MemoryIdentity` scoped around every turn, resolved to a layout root and a memory agent id (host binding, definition pin, team, default). |
+| `lifecycle::hooks` | `pre_turn`, `post_turn`, `compaction`: bounded, never fail a turn. |
+| `lifecycle::jobs` | The persisted background queue and the `memory_background` cron job. |
+| `lifecycle::views` | Policy, pack preview, agents list, job views for the RPCs. |
+| `brain` | Brain source mapping for synced items and the `memory_brain_*` ops. |
+| `sources` | The `[[memory.sources]]` registry and sync into the brain. |
+| `channels` | Which channel each logged thread arrived on, for forgetting a channel. |
+| `backfill` | Consent-gated storing of past chats in the lifecycle's shape. |
+| `import` | Consent-gated, resumable v1 import. |
+| `tools` | The single `memory` agent tool (`recall`, `fetch`, `learn`, `forget`), confined to the identity's layout. |
+| `ops`, `explore` | Engine selection, recall, fetch, learn, forget, listing, the explorer. |
+| `bus` | The cron subscriber (`memory_sources_sync`, `memory_background`). |
+| `schemas` | The `openhuman.memory_*` controllers. |
+| `status`, `error`, `types` | Subsystem status, error codes, shared types. |
-The session host prepends `context.md`, wrapped in `<memory-context>`, as the first user message of a new session only; resumed sessions keep their frozen transcript.
+## Where the agent loop calls memory
-Chat thread persistence is not memory. The JSONL thread store (formerly `memory::conversations` over `tinymemory-conversations`) is now `tinyagents_session::threads` in `vendor/tinyagents`, wrapped by `crates/openhuman-core/src/threads/store`.
+| Hook | File |
+| --- | --- |
+| Pre-turn (log + recall), first turn after a compaction also `start_session` | `agent/session_host/runtime_session/memory_ingest.rs`, from `before_turn` in `runtime_session.rs` |
+| Pack injection, ephemeral, every model request of the turn | `agent/tinyagents/middleware/memory_pack.rs` |
+| Post-turn (log the reply, queue builds), after the durable commit | `memory_ingest.rs`, from `finalize_after_durable_commit` |
+| Compaction recall into the checkpoint | `agent/tinyagents/memory_summarizer.rs`, wrapped around the summarizer in `harness_context_ladder.rs` |
+
+The turn's `MemoryTurn` (config, identity, thread, pack) rides
+`OpenHumanRunContext::memory_turn`; a child run has its own.
-The MCP server exposes `memory.recall`, `memory.fetch`, `memory.list`, `memory.learn` and `memory.forget` (`mcp/server/tools/specs.rs`).
+Chat thread persistence is not memory: it is `tinyagents_session::threads`,
+wrapped by `crates/openhuman-core/src/threads/store`.
+
+The MCP server exposes `memory.recall`, `memory.fetch`, `memory.list`,
+`memory.learn` and `memory.forget` (`mcp/server/tools/specs.rs`).
## Tests
-`tests/memory_v2_e2e.rs` (JSON-RPC), `app/test/playwright/specs/memory-v2.spec.ts` (UI), and unit tests beside each module (`*_tests.rs`).
-
-Against a real CortexDB server, `scripts/test-memory-cortexdb-live.sh` boots the
-pinned harness in Docker (`vendor/tinymemory/integration/cortexdb/`, v0.10.4 by
-default, `CORTEXDB_VERSION=v0.9.9` for the older release) and runs
-`tests/memory_cortexdb_live.rs`. That test spawns the real `openhuman-core`
-binary with the `cortexdb` engine and checks learnings, a synced folder source,
-web-chat conversation ingestion, recall, and `context.md` (compiled by its cron
-job, written to disk, and injected into a new thread's first message). It skips
-unless `OPENHUMAN_LIVE_CORTEXDB_URL` is set, so plain test runs never need
-Docker.
+- Unit tests beside each module (`*_tests.rs`), against `ReferenceEngine`.
+- `tests/memory_v2_e2e.rs`: every `memory_*` RPC against the mock backend, and
+ a web-chat turn whose inference request carries the pack, whose turns are
+ logged with `x-sdk-name`, and whose transcript holds no pack.
+- `crates/openhuman-embed/tests/runtime_agents.rs`: `AgentSpec::memory`.
+- `app/test/playwright/specs/memory-v2.spec.ts` (UI).
+
+Against a real CortexDB server, `scripts/test-memory-cortexdb-live.sh` boots
+the pinned harness in Docker (`vendor/tinymemory/integration/cortexdb/`) and
+runs `tests/memory_cortexdb_live.rs`: learnings, a synced folder source,
+web-chat turn logging, recall, the pack preview, the pack injected into a
+turn's inference request, and belief builds. It skips unless
+`OPENHUMAN_LIVE_CORTEXDB_URL` is set.
diff --git a/gitbooks/features/native-tools/memory-tools.md b/gitbooks/features/native-tools/memory-tools.md
index 940ee12bc9..137a1ebf79 100644
--- a/gitbooks/features/native-tools/memory-tools.md
+++ b/gitbooks/features/native-tools/memory-tools.md
@@ -14,11 +14,11 @@ icon: brain
| `learn` | Save one durable learning (a preference, fact, procedure or correction). |
| `forget` | Remove items by id. |
-The tool is only registered when a memory engine is usable. With none selected, memory is off and the agent never sees it. Each learning is tagged with the workspace, thread, agent and tool call that produced it.
+The tool is only registered when a memory engine is usable. With none selected, memory is off and the agent never sees it. Each learning is shared with every agent under the same memory root and tagged with the workspace, thread, agent and tool call that produced it.
-## Why a tool, not implicit context
+## The tool and the per-turn pack
-Memory is too large to put in every prompt. Each new chat starts with a short compiled brief ([context.md](../memory.md#contextmd)); for everything else the model asks ("what do I know about the Stripe webhook?") and gets back just the relevant answer with citations.
+Every turn already arrives with a small [memory pack](../memory.md#how-the-agent-uses-memory) (`<memory-context>`): the learnings, documents and history most relevant to what you just said. The tool is for going further than the pack: asking a question the pack does not cover ("what do I know about the Stripe webhook?"), searching with metadata filters, or saving something worth remembering next time.
External MCP clients get the same abilities from OpenHuman's [MCP server](../../developing/mcp-server.md) as `memory.recall`, `memory.fetch`, `memory.list`, `memory.learn` and `memory.forget`.