Skip to main content

Module reconcile

Module reconcile 

Source
Expand description

Reconcile an agent’s memory record with the provider folder its spawn is about to use (SPEC_MEMORY_FOLLOWS_THE_AGENT_2026_09_24.md §2.1.3, M3b).

Runs before the provider process starts, so a new account, working directory or channel starts its first session WITH the agent’s memory. Only a directory proven exclusive (memory::dir_claims) is touched.

Per file, comparing disk, the record’s head, and the version last projected into this directory:

  • disk equals the head → record the projection, nothing else (rule 0: covers a crash between a write and its record);
  • the record changed, disk didn’t → write the head to disk (or, for a deletion, remove the file — only if it still holds what was projected);
  • disk changed, the record didn’t → capture it as a new version;
  • both changed → conflict: disk’s version becomes the head, marked conflicts_with the record’s, which is written beside it as <stem>__conflict_<short>.md and recorded, so it follows the agent;
  • the first time the record meets an exclusive directory, its files are adopted together as the baseline.

Safety rules, each from a data-loss review of the first version:

  • A file that can’t be read is left alone — never taken for deleted, and never overwritten. A folder that can’t be listed skips the pass. A folder that doesn’t exist, or holds none of the files projected into it, has its projections forgotten first and is refilled — never tombstoned, even if the refill stops part-way.
  • MEMORY.md is reconciled first, and every file is decided against a fresh read of the record, never a snapshot from the start of the pass.
  • MEMORY.md is edited (to index a conflict file) only when disk, the head and this directory’s projection all agree on it; otherwise a later pass adds the line.
  • A deletion re-reads the file and removes it only if it still holds the projected content, and only in a pass that finished everything else.
  • The pass has a time budget; exclusivity that can’t be proven in time counts as shared.
  • The folder is the one the CLI really uses: keyed by the repository’s main checkout, not the working directory (crate::backend::claude_layout::memory_project_root). A folder the spawn or a settings file may redirect is left alone.
  • Each file is read again when its turn comes, and again right before it is overwritten; a provider write in between wins, and is captured next pass.
  • One pass per agent at a time, across AgentMux processes (a lease in the global store); a second one skips.

Functions§

record_agentmux_write
An AgentMux write to an agent’s memory — MemoryWrite, the Memory editor, a revert, a bundle import — recorded before the file is written (SPEC_MEMORY_FOLLOWS_THE_AGENT_2026_09_24.md §2.1.1), so the next spawn doesn’t take it for the provider’s, and it follows the agent at once.