Skip to main content

Module native_drift

Module native_drift 

Source
Expand description

Out-of-band write detection for native memory files — §4.5 of docs/specs/SPEC_MEMORY_VERSION_CONTROL_AND_ARMORY_AUDIT_2026_08_19.md. Claude Code’s CLI writes memory .md files directly to ~/.claude/projects/<hash>/memory/*.md, with no obligation to go through agent:memory:write_file/memory.write — an agent’s own general-purpose filesystem tools can (and, per this spec’s own motivating incident, did) write there directly. When that happens, no RPC fires, so nothing in native_memory_handlers.rs or app_api::mod ever sees it.

Two independent layers close that gap, so failure of one degrades rather than blinds the whole system:

  1. Fast path ([spawn_fast_path]) — subscribe to every known agent’s memory directory via the shared FsWatchPool (backend::fs_watch), and record a new external_fs_write version on any change whose hash doesn’t match the latest recorded version.
  2. Slow path ([reconciliation_sweep_once]) — a periodic sweep, run on the same cadence class as FsWatchPool’s own HEALTH_SWEEP_INTERVAL (30s), that hashes every known agent’s live memory files and compares against the latest recorded version — the backstop for a missed fs-watch event, a watch still in retry-backoff, or srv having been down entirely when the write happened.

Both layers call the same [check_and_record_drift] — the only difference between them is how they learn a file might have changed. Neither promises zero data loss (see the spec’s §4.5 “precision, honestly bounded” section): two out-of-band writes to the same file between one sweep and the next collapse into one detected version, and source_detail never guesses a session_id for a drift-detected write.

Functions§

owned_targets
The roster’s memory directories of the agents running in this process. An agent is matched by the id the roster and its memory directory are keyed by (AgentDefinition::id).
spawn
Start both layers. Call once at server startup with a fully-built AppState’s fs_watch_pool/mstore/id_store, and owned: the agents running in this process, the only ones either layer looks at (OwnedAgents). Returns immediately — both loops run as spawned background tasks for the lifetime of the process (no shutdown handle; srv itself owns the process lifetime).

Type Aliases§

OwnedAgents
The ids of the agents running in this process now. The agent roster and the identity folders are shared by every channel on the host, but a channel looks at an agent’s memory folder only while that agent runs in it: a second channel starting beside a populated one (a headless start next to a desktop) reads nothing of the others’ agents, records nothing about them, and logs nothing about them. An agent that is not running is brought up to date when it next spawns (the reconcile before spawn, reconcile.rs).