Skip to content

Name every implicit state machine once; load the vector matrix one kind at a time - #18

Open
MakerViking wants to merge 2 commits into
mainfrom
refactor/explicit-state-machines
Open

MakerViking wants to merge 2 commits into
mainfrom
refactor/explicit-state-machines

Conversation

@MakerViking

Copy link
Copy Markdown
Owner

Two commits. No schema change: the store stays at v14.

1. Name every implicit state machine once (e2c5e3e)

Every state the app moves through (a memory's lifecycle, a project's graph freshness, a session's handoff, a sync conflict, a hook install) now has one name and one function that derives it, and every surface (CLI, MCP, daemon, hooks, dashboard) prints that name. Map: docs/design/state-machines.md. User-facing list: CHANGELOG.md [Unreleased].

What changes for a user:

  • Sync converges. replicate::Standing is the one total order of two versions of a memory: when its text was decided, then forgotten over live, then last write, then digest. A pin, tag or anchor made after a forget the machine had not seen yet no longer brings the memory back. Writes in the same second settle the same way on every machine. meta.text_clock records when a memory's text was decided, separately from updated_at. Random histories across three stores converge in the tests.
  • Worktrees share the main checkout's memories. A linked worktree keeps its own code graph but files and reads memories under the main checkout's project. Only memories newer than the worktree move, and only on memory commands and MCP start, never from hooks, status or doctor. A directory counts as a worktree only when the repository names it back (<gitdir>/gitdir). Handoffs are tied to the checkout that wrote them.
  • project prune stays out of the way. It runs in 250 ms transactions with pauses. It re-links a pruned worktree's memories to the same code in the main checkout. It keeps a main project while a live worktree still files memories under it.
  • Project registration is atomic. Three hooks opening a new directory at once now make one project row, not three.
  • Guard rail: tests/one_definition.rs fails when production code spells a state outside the file that owns it.

Rehearsed on a copy of a real store (3.7M rows, 8.1 GB):

  • Index build: 8 s.
  • Prune: 1.13M symbols from 127 dead projects in 4 to 11 min. 953 writes ran alongside it, none failed, the slowest took 1.24 s.
  • VACUUM afterwards: 8.1 GB down to 3.9 GB.

2. Load the vector matrix one kind at a time (fabca8b)

Found while installing. The bug predates this branch, and the old binary was just as slow.

The warm daemon kept every embedding in one in-memory matrix and reread all of it whenever another process committed. With large code graphs that is 2.9 GB of symbol vectors read to search 1,814 memories. Each prompt took 13 to 99 s, so the hook gave up and fell back to the cold path.

The cache now holds one matrix per kind, loaded the first time a search asks for that kind. Measured on the real store:

before after
warm /inject 13–99 s 0.1–0.66 s
prompt hook end to end timed out 0.17 s
daemon resident memory 3.3 GB 250 MB

Upgrade order

  1. Upgrade the sync hub first.
  2. Install, then restart the daemon, the proxy and the mimir mcp servers.
  3. Run mimir init --hooks.
  4. Run mimir sync push --full, then mimir sync pull --full, once on each machine.
  5. Optional: mimir project prune, then --apply.

Checks

  • Tests: 998 passed, 0 failed.
  • Lints: clippy -D warnings and cargo fmt --check are clean.
  • Evals: the retrieval eval's exact baseline is unchanged.
  • New tests: each fails when its fix is removed.

…e place

Every state the app moves through (a memory's lifecycle, a project's
graph freshness, a session's handoff, a sync conflict, a hook install)
now has one name, one function that derives it, and every surface (CLI,
MCP, daemon, hooks, dashboard) prints that name. The map is in
docs/design/state-machines.md; the user-facing list is CHANGELOG
[Unreleased]. No schema change: the store stays at v14.

Sync
- replicate::Standing is the one total order of two versions of a
  memory: when its text was decided, forgotten over live, last write,
  digest. A pin, tag or anchor made after an unseen forget no longer
  brings the memory back, and same-second writes settle the same way on
  every machine (random three-store histories converge).
- text_clock (meta.text_clock) records when a memory's text was decided,
  apart from updated_at; every write that only annotates calls keep().
  The brief's age tag and the rules-pack choice read it.
- Supersession, pins, anchors and expiry replicate; the hub relays by
  arrival time.

Worktrees
- A git linked worktree keeps its own code graph but files and reads
  memories under the main checkout's project (scope::Checkout,
  search::Reach). Only memories newer than the worktree move, and only
  on memory commands and MCP start, never from hooks, status or doctor.
- A directory counts as a worktree only when the repository names it
  back in <gitdir>/gitdir.
- Handoffs are tied to their checkout.
- project prune re-links a pruned worktree's memories to the same code
  in the main checkout, keeps a main project a live worktree files
  under, and runs in 250 ms transactions with pauses so writers are not
  blocked (rehearsed on a 3.7M-row store: 953 concurrent writes, 0
  failed, slowest 1.24 s).

Store
- Project registration is atomic; a directory with several rows
  resolves to the oldest.
- Opening a store never queues behind a writer to build an index.

Contributors
- tests/one_definition.rs fails when production code spells a state
  outside the file that owns it.
The warm daemon kept every embedding in one in-memory matrix and reread
all of it whenever another process committed. On a store holding the
code graphs of many projects that is 2.9 GB of symbol vectors (1.97M
rows) to answer the prompt hook, which searches 1,814 memories: 13 to
99 s per prompt, so the hook timed out and fell back to the cold path.

The cache now holds one matrix per kind, loaded from the node side the
first time a search asks for that kind. A memory recall reads memory
vectors only. Measured on the real store after the fix: warm /inject
0.1 to 0.66 s, the hook 0.17 s end to end, daemon resident memory
3.3 GB -> 250 MB. A search that includes code still reloads the symbol
vectors after a commit elsewhere.
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.

1 participant