# Memory introspection

Asked how its memory works, an agent with no answer in context searches the disk and describes whatever other harness it finds there. This page covers what each turn tells the model. The memory subsystem itself: [Memory overview](https://fzkuji.github.io/OpenProgram/docs/reference/design/memory/overview.html).

## What each turn tells the model

| Question | Answer today | Source |
| --- | --- | --- |
| Do I have long-term memory? | Implied | `memory_search`, `memory_get`, `memory_update` in the deferred catalog; no sentence says so |
| Where is it on disk? | Unknown | `<environment>` states OS, shell, working directory and repository only |
| Is this block memory? | Partly | `fence_memory` says recalled content; no owner or source |
| Complete or summary? | Unknown | Rendered under a token budget; not stated |
| How do I see the rest? | Yes | `tool_search` loads the three tools |
| Empty or broken? | Unknown | See below |
| Harness or project? | Partly | The base prompt names OpenProgram; its state location is not stated |

## The memory block

`fence_memory` wraps both paths in `<memory-context>` with one note, written so recalled facts do not read as new requests.

| Path | Content | Lands in |
| --- | --- | --- |
| Resident | Each layer's `MEMORY.md`: map, then core, general to specific (`LocalMemoryBackend.system_prompt`) | System prompt, L1 order 30 |
| Per-turn recall | Search results for the turn (`search`) | Prefix inside the current user message |

Fencing is not idempotent: `sanitize_context` removes a whole `<memory-context>` block, body included, so fencing twice returns an empty fence. Identity must replace the opening sentence, never add a wrapper.

## Silent failures

| Layer | On failure |
| --- | --- |
| `_core_block`, `system_prompt` | `OSError`: empty string, same as a file with nothing in it |
| `ScopedBackend._read` | Any exception: empty string |
| `_build_memory` | Any exception: empty string |
| `assemble_sections` | Skips a failing component; empty strings are dropped |
| `build_system_prompt` | Falls back to the agent's inline prompt |

Under a required policy these raise `MemoryPolicyError` and end the turn. Otherwise `require_or_empty` records `MEMORY_UNAVAILABLE` and emits `memory.degraded`, which the model never sees.

| State | Model sees | Should conclude |
| --- | --- | --- |
| `memory.backend = none` | Nothing | Nothing; the user's decision |
| Empty workspace | Nothing | No record yet; normal |
| Read failed | Nothing | Unavailable; never "the user did not tell me" |

## Eight implementations

|  | Says it has memory | Tools by default | Empty or failed | Host vs project |
| --- | --- | --- | --- | --- |
| OpenProgram | Names only | 3, deferred | Silent | Name only |
| hermes-agent | Yes, most explicit | 1 (add, replace, remove) | Silent when empty; tool calls report | Yes: memory is about the user |
| openclaw | Yes | 2 | All three visible | Yes |
| opencode | Model-dependent | 0; read, write, grep | Silent | Paths only |
| pi-mono | No | 0 | Silent | Paths only |
| claude-code | Yes, with path | 0; files through generic tools | Placeholder for subagents only | Yes: five source labels |
| codex-cli | When enabled | 4, two switches off | Missing memory silent; retired instructions announced | Yes |
| weclaw | No concept | 0 | Not applicable | No |

`pi-ai` is a read-only snapshot of pi-mono's transport layer: no agent loop or prompt assembly, so every question is not applicable.

**How each one injects and labels memory**

|  | Injection | Label |
| --- | --- | --- |
| hermes-agent | Volatile section of a three-part prompt, snapshotted at session start (`agent/system_prompt.py`) | Banner, title and live character count (`tools/memory_tool.py`) |
| openclaw | `# Project Context` section, `MEMORY.md` last (`agents/system-prompt.ts`) | Heading, per-file introduction, `## MEMORY.md`; `## Memory Recall` names the tool to call |
| opencode | One prompt item; nested `AGENTS.md` as a read-tool suffix (`session/instruction.ts`, `tool/read.ts`) | A path line; nested content in `<system-reminder>` |
| pi-mono | `<project_context>` between identity and skills (`core/system-prompt.ts`) | Tag and per-file `path` attribute; memory never named |
| claude-code | Usage guidance in the system prompt; content in a first synthetic user message | Outer `<system-reminder>`; each file labelled with path and owner and scope |
| codex-cli | `AGENTS.md` in user-role fragments, memory summary in developer-role fragments | `# AGENTS.md instructions`, `<INSTRUCTIONS>`; delimiter lines around the summary |
| weclaw | Configured static string (`agent/cli_agent.go`) | None |

| What a label should answer | Who answers it |
| --- | --- |
| What is this block? | claude-code, openclaw, hermes; OpenProgram only as recalled content |
| Who wrote it? | claude-code is the most precise |
| How do I see more? | openclaw, claude-code, hermes |
| How much is there now? | hermes alone |

**Tools and failure handling in detail**

- openclaw's memory plugin is the default slot; its tool description makes recall mandatory and requires surfacing `disabled=true` to the user. Failed retrieval returns `disabled`, `unavailable`, a warning and an action. An empty workspace is declared normal in its bootstrap template.
- hermes has no list action; every write returns the full list.
- codex-cli has List, Read, Search and AddAdHocNote behind `Feature::MemoryTool` (off) and `memories.dedicated_tools`.
- OpenProgram's memory writer is a detached session with its own system prompt and only the memory tools, so the host's `CLAUDE.md` never reaches it (`runtime/conversation/memory_agent.py`). The reading agent has no equivalent statement.

## Changes

| Change | What | Where | Status |
| --- | --- | --- | --- |
| A. Catalog names memory | Three `memory_*` tools in the default set, deferred | `programs/__init__.py` | Built |
| B. Resident identity | `fence_core` beside `fence_memory`, sharing `sanitize_context`; opens `<memory>`. Recall path unchanged | `state/memory/backend.py`, `local_backend.py` | Not built |
| C. Three states | Off: nothing. Empty: one line, healthy. Failed: one line, absence proves nothing | `_build_memory` | Not built |
| D. Host | `<environment>` names the harness and its state directory | `environment_block` | Not built |

**What the model would see after B and D**

```
<memory>
[System note: this is your long-term memory, the always-on summary the
 memory subsystem keeps at ~/.openprogram/memory. It is written in the
 background, not by the user, and it is a summary under a token budget.
 Browse the rest with memory_get / memory_search.]
…rendered MEMORY.md…
</memory>

<environment>
OS: Darwin · Shell: bash (/bin/bash)
Harness: OpenProgram · its own state lives at ~/.openprogram (not the project you are working on)
…
</environment>
```

### Cost per turn

| Option | Tokens |
| --- | --- |
| Three names in the catalog (chosen) | 8 |
| Three schemas resident (rejected) | About 1045: `memory_search` 208, `memory_get` 216, `memory_update` 621; rarely called |
| Prose identity (B and D) | About 65, in the cached prefix |
| Empty or failed turn after C | One status line replaces the resident block; net saving |

Both are needed. The model trusts its tool list, which is cheap; prose answers what a name cannot: location, completeness, authorship.

## Further out

| Target | Needs | Why not yet |
| --- | --- | --- |
| Memory describes itself: name, root, topic count, last write, budget used | `MemoryBackend.describe()` | The interface stays small; C covers failures |
| Each component declares its source, as `Contents of {path} ({owner})` | An `origin` field; edits to every builder; every cached prefix invalidates | One block causes the problem; untagged components (`base`, `inline_prompt`, `current_date`) can wait |
| `context_status` tool: what this turn saw, which block is memory, what was absent | Expose `context/token_accounting/breakdown.py` to the model | A and C give the needed knowledge |
| Status lines carry checkable quantities | `describe()`, as above | Do with the first item, to change the interface once |

[Composition: what else is in each call](https://fzkuji.github.io/OpenProgram/docs/reference/design/context/composition.html)
