# Adoption and adaptation

What happens to each practice picked out by the [framework comparison](https://fzkuji.github.io/OpenProgram/docs/reference/design/memory/comparison.html). Design: [overview](https://fzkuji.github.io/OpenProgram/docs/reference/design/memory/overview.html). Mechanisms: [illustrated](https://fzkuji.github.io/OpenProgram/docs/reference/design/memory/architecture.html).

## Four decisions

| Practice | From | Decision | Reason |
| --- | --- | --- | --- |
| [Retrieval without an index](#recall) | claude-code | Rejected | The extra model call buys semantic lookup without an index. An index already exists, at finer than file granularity. |
| [Trim resident content by ownership](#core) | openclaw | Adapted, built | Ownership matters only when removal destroys content, so the resident section is derived instead. |
| [Summarize a branch on leaving it](#branch) | pi-mono | Adapted, built | The goal stays, the summary node goes: session boundaries visit every live branch. |
| [Lost cursor means nothing written](#cursor) | claude-code | Adopted, built | Repeated writes cost a bounded amount; a silent stop does not. |

## Retrieval without an index

|  | OpenProgram | claude-code |
| --- | --- | --- |
| Selection | Index search before each turn | A small model picks from a list of one-line file descriptions |
| Result | `retrieval_top_k` paragraphs (default 5, at most 10) with block IDs and footnotes, fenced into the prompt | Five filenames; the Agent then reads them |
| Extra model round trips per turn | 0 | 1, while the user waits |
| Maintained | BM25 and `retrieval/embedding.py` indexes | Nothing: no inverted index, vectors or cache, part of why its implementation is the smallest compared |
| Cut-off | Dropped only at `lexical<=0` and `adjustment<=0.02`, so one shared word is a hit and an unrelated turn still gets paragraphs | Omit uncertain matches, prefer empty (`findRelevantMemories.ts`) |

| Part | Decision |
| --- | --- |
| The model call | Not taken. It would pay every turn for what the index already does. |
| File descriptions | Not taken. Results are paragraphs, and a path such as `topics/people/dave.md` already describes its one topic. Strict parsing also rejects an introductory paragraph or blockquote (`TopicFormatError: memory block ID required`); only an HTML comment passes. |
| "Prefer empty" | Taken as a minimum score for results. |

### Search time by workspace size

| Topic bytes | First search | New process, cache on disk | Same process, files unchanged |
| --- | --- | --- | --- |
| 0.5 MB | 108 ms | 57 ms | 50 ms |
| 3.35 MB | 584 ms | 413 ms | 342 ms |
| 9.8 MB | 1832 ms | 1216 ms | 1101 ms |

Synthetic workspaces in the real format, 12 blocks per file, mean of three runs on one machine. Parsing is cached by file signature, but every search rebuilds the scoring corpus, so time stays linear in bytes. That cost is local and fixable; an added model round trip can only be avoided.

## Trimming resident content

The resident section is the core of `MEMORY.md`, rendered from `topics/core.md`.

|  | openclaw | OpenProgram |
| --- | --- | --- |
| Who writes | Automatic promotion and hand edits, in one file | Writers and people edit `topics/core.md`; the Runtime renders the section |
| Limit | 10000 characters | Render budget `core_max_tokens`, 2000 |
| When full | Removes oldest automatic paragraphs, keeps hand-written ones (`memory-budget.ts`) | Renders in file order until the next paragraph does not fit; a heading left with nothing under it goes too |
| Removal destroys content | Yes, so ownership must be tracked | No: the rest stays in the source, indexed and citing its evidence |
| Runs | During writes | After each write, with `timeline/`, `recent_events.jsonl` and `relations.json` |
| Reports | Removed dates, to the caller | Rendered tokens and left-out block IDs, on `last_core_block` |

To keep a paragraph resident, move it earlier in the file. No marker exists for this.

**Why a derived section, and a budget rather than a limit that rejects**

| Concern | Effect |
| --- | --- |
| Block IDs | Every block ID present before an edit must survive it, or the transaction fails with `block ID must not be removed`. Trimming a file that holds the only copy of an ID cannot pass. Rendered IDs are always a copy of an ID in `topics/`. |
| A rejecting limit | One overflow would roll back the whole transaction, Topic changes included, and every later stable fact would be sent elsewhere, leaving the section frozen. |
| One file kind | `topics/core.md` follows the Topic contract. `validate_writable_path` refuses edits outside `topics/**/*.md` with `READ_ONLY_PATH`. |
| Old root `core.md` | `promote_legacy_core` moves it into `topics/` once. |
| Reorganization | The organize prompt keeps `topics/core.md` to facts useful across most interactions; being past the budget is no reason to keep a paragraph. Size-based splitting skips this file. |

### Reporting whether anything changed

Reorganization rests on model judgment, and a model that finds nothing to do does nothing. `reorganize` returns `changed_files` beside `topics`, the count of files examined, so an empty list is visible.

## Departed branches

1. **Current head** always written
2. **Other live tips** long branch left: written at the token threshold one-turn retry: below the threshold, skipped rewound: filtered out by `get_branch`

The idle watcher's forced write calls `list_branches`, skips archived branches, then `get_branch(session_id, tip)` for each. Written markers cover shared prefixes. Post-turn writes follow the head only.

**Why not pi-mono's summary node (`branch-summarization.ts`)**

| Point | Reason |
| --- | --- |
| Destination | pi-mono has only session JSONL and `AGENTS.md`, so a summary is the only place abandoned statements can go. Here facts belong in Topic files; a node on the DAG has no footnotes or block IDs and cannot be retrieved. |
| Another location | Compaction summaries in `context/summary` already attach to the DAG, for the context window. A second kind is one more place to look. |
| No leave event | Neither head movement nor branch switching is an event. Head writes are spread across `set_head`, `append_message`, `SessionNodeWriter.append`, `update_session` and rewind, and `SessionMemoryIndex.set_head` discards the old value. Copying pi-mono would add an event and retain the old head for work that serves equally well at session end. |
| Deliberate abandonment | Needs no inference: rewinding marks a segment `rewound`; its nodes stay in the graph and leave the conversation path. |
| Idle rule | `should_incremental_write` also fires when anything is pending and the last message is an hour old, which every left branch satisfies. Non-head branches check the token threshold first, so one-turn retries are skipped. |

## Cursor fallback

| Item | Behaviour |
| --- | --- |
| claude-code | The cursor is a message UUID. If that message is gone, everything counts as unwritten, because returning zero messages would end extraction for the session. |
| Rule here | Unreadable accounting means nothing was written, never an exception that blocks writing for good. Rewriting a batch is bounded, and transactions merge paragraphs stating the same fact. |
| Node markers | Written state is a marker on session nodes (`memory_written_scriptorium`), so no cursor file can go unreadable. A missing marker causes repeated work. |
| `runtime.json` | Missing, unparsable or the wrong shape: `RuntimeStateStore.load` returns empty state. The file counts toward the workspace revision, so restored content does not pair with newer state. |
| Upgrades | A separate, one-time case with the same tradeoff: see [written markers](https://fzkuji.github.io/OpenProgram/docs/reference/design/memory/written-marker.html). |

## Change locations

Paths under `openprogram/state/memory/`. The rejected practice changes none.

| Path | Role |
| --- | --- |
| Derived core | Derived core |
| `runtime/derived_views.py` | `render_core_block`, `promote_legacy_core` |
| `runtime/memory_guide.py` | Writes the core between its markers in `MEMORY.md` |
| `management/block_views.py` | Calls the render after the other views |
| `management/transaction.py` | Writable paths |
| `prompts/write.py`, `prompts/system.py`, `prompts/organize.py` | Direct agents to `topics/core.md` |
| All branches at session boundaries | All branches at session boundaries |
| `writing.py` | `_eligible_session_branches`, forced write loop |
| `session_watcher.py` | Idle entry, `force=True` |
| Unreadable state | Unreadable state |
| `runtime/state.py` | `load` |
| Node markers | Node markers |
| `runtime/online.py`, `writing.py` | Marker reads and writes at the `_records` boundary |

## Implementation status

| Status | Items |
| --- | --- |
| Implemented | Derived core, all-branch writes at session boundaries, unreadable state as empty state, node markers, `changed_files` |
| Not yet | A minimum result score; search that does not rebuild the scoring corpus; a writer notified directly by head changes (post-turn thresholds and the idle watcher trigger writes) |
