# Virtual memory

A compact index distilled from the concrete layer (session DAG and project git), not a replacement for it. Every memory points back to its source, records `event_time` and `ingestion_time`, and is never deleted: a conflict tags the old record `superseded_by`. Designed, not built; see [status](#implementation-status).

|  | Timeline | Knowledge graph | Core.md |
| --- | --- | --- | --- |
| Answers | What happened, and when | How things relate | The minimum read on every model call (2 KB or less) |
| Stored in `memory/` | `timeline/YYYY-MM.jsonl` | `graph/entities.jsonl`, `graph/edges.jsonl`, `graph/views/entity/<slug>.md` | `core.md` |
| Created | Idle session (30 minutes or more) through the watcher, daily 03:00 consolidation; extraction with a `Provenance` pointer, appended to the month's file | Same triggers; entities and relations from extraction, aliases resolved, contradictions tagged, appended | Regenerated at 03:00 from top-N events (recency × importance) and high-frequency, high-confidence graph facts; every line carries `↪ session:<id>` |
| Read | `memory_timeline(since, until)`, `memory_timeline(entity=…)`; recent high-signal events reach Core.md | `memory_graph_neighbors(entity, hops=2)`, `memory_search(query)`, filtered to `global` plus `project:<current>` | Injected into every system prompt |
| Updated | Re-distilled fact tags the old record | Aliases merge into one node; a conflicting edge tags the old one; views re-projected | Whole file replaced |
| Deleted | Never: an event is a historical fact | Never: full history stays | Overwritten |

**Record formats**

Timeline event. `kind`: `work`, `decision`, `learning`, `event`.

```
{
  "id": "ev_abc",
  "summary": "Fixed the Windows cp1252 encoding bug in OpenProgram, touching 38 files",
  "kind": "work",
  "provenance": {
    "project_id": "proj_openprogram",
    "session_id": "local_13d5",
    "commit": "<sha>",
    "event_time": 1779900000,
    "ingestion_time": 1779986400
  },
  "entities": ["project.openprogram", "issue.cp1252"]
}
```

Entity.

```
{
  "id": "project.openprogram",
  "type": "project",
  "name": "OpenProgram",
  "attrs": {"path": "~/OpenProgram", "lang": "python"},
  "scope": "global"
}
```

Edge.

```
{
  "from": "issue.cp1252",
  "to": "commit.<sha>",
  "relation": "fixed-by",
  "event_time": 1779900000,
  "ingestion_time": 1779986400,
  "provenance": {"project_id": "proj_openprogram", "session_id": "local_13d5"},
  "confidence": 0.95,
  "superseded_by": null
}
```

| Scope on an entity or edge | Visible |
| --- | --- |
| `global` | Across projects, such as a language preference |
| `project:openprogram` | This project |
| `agent:research` | This agent |

Files live under `~/.openprogram/projects/default/memory/`.

## What the model sees

```
OpenProgram Memory — project: OpenProgram, last consolidated <date>
[Timeline · recent]
· <date> Refactored the Programs page into a three-tab layout   ↪ session:local_fc03
· <date> Fixed the CLI attended-mode issue                       ↪ session:local_d125

[Graph · current project]
· OpenProgram at ~/OpenProgram (python, next.js)
· worker ──listens-on──► :18100

For details: memory_open_session(<id>) / memory_git_log(<project>)
```

Core.md; timeline or graph results are optional, per query. No raw chat is injected.

## Navigation tools

| Tool | Returns | From |
| --- | --- | --- |
| `memory_open_session(session_id, turn)` | Raw messages | Session git history |
| `memory_git_log(project_id, since)` | Commit history | Project git |
| `memory_git_show(project_id, commit)` | What one commit changed | Project git |
| `memory_timeline(entity\|since\|until)` | Timeline slice | Timeline |
| `memory_graph_neighbors(entity, hops)` | Neighbors | Graph |
| `memory_search(query)` | Hybrid search | Full text and optional vectors |

## Distillation

| Trigger | When | Does |
| --- | --- | --- |
| Session end | Idle 30 minutes or more | Distills new turns |
| Daily | 03:00 | Consolidation, disambiguation, contradiction detection, Core.md rebuild |
| Pre-compress | Context near its limit | Flushes key information to the concrete layer |

1. **Collect**: commits since the last pass from session git and project git; every DAG node (user, llm, code, plus `reads` edges).
2. **Extract**: one model pass for events, entities and relations, each with provenance. The costliest stage; a rules-based version can come first.
3. **Link**: resolve aliases against the graph (worker, backend, daemon become one node).
4. **Reconcile**: detect contradictions, tag old edges.
5. **Project**: `core.md`, entity views, timeline shards.

## Compared with today's memory

|  | Built today | This design |
| --- | --- | --- |
| Reads | User and assistant text from the session branch | The `Call` DAG, `code` nodes and `reads` edges, project commits |
| Keeps | `topics/`, rewritten at 03:00; derived `timeline/`, `recent_events.jsonl`, `relations.json` | Append-only timeline and graph with two timestamps |
| Always-on block | Core section of `MEMORY.md`, from `topics/core.md` | `core.md` projected from timeline and graph |
| Tool calls, decision inputs, project history | Absent | Present |

## Implementation status

| Status | Items |
| --- | --- |
| Implemented | Read layer `openprogram/state/store/session/reading/provenance.py`: `iter_nodes_since()`, `node_provenance()`, `project_commits()`, `session_project_id()`. Tool `memory_search`. |
| Not yet | Timeline and graph stores, this `core.md`, the distillation stages, every other navigation tool, a per-session commit reader |

[Memory overview](https://fzkuji.github.io/OpenProgram/docs/reference/design/memory/overview.html)
