From cedabc04738c387b25124e8e6b545050c3143693 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 24 Jun 2026 18:32:47 -0400 Subject: [PATCH 1/2] fix(agent): align record_turn detection view with the record view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit record_turn detected changes via status() against the repo's current_view but applied them to session.view_name. The real agent hook flow switches current_view at session start, so production is unaffected — but direct callers that bypass session start (the noname ACP→TurnOrchestrator bridge, tests) leave detection on `dev` while apply targets the session view. A turn whose net effect is invisible from `dev` (deleting the last file recorded on the session view) then early-returns EmptyTurn and silently drops the turn. Fix: align detection with the record view in two places — - before the first status() on the read-only handle, in memory only via the new Repository::set_current_view_in_memory (no txn/lock/disk write, so the read-only anti-hang fast path is preserved); - on the write handle, the persisting set_current_view now tolerates ViewNotFound (first turn) but propagates DB/write failures as RecordFailed instead of swallowing all errors. record()'s filter/apply already used session.view_name, so provenance and attestation landing are unchanged; attestation coverage improves because turns 2..N stop being skipped. Sole production caller (turn.rs) is a no-op in the real flow. Adds modify + delete-only regression tests (the delete-only case is what actually reproduces the bug). Documents a known adjacent racy-mtime hazard in the no-hash status fast path, left unfixed by design. --- atomic-agent/src/record/mod.rs | 49 +++++- .../tests/record_view_mismatch_regression.rs | 157 ++++++++++++++++++ atomic-repository/src/repository/mod.rs | 15 ++ docs/atomic-record-view-mismatch.md | 137 +++++++++++++++ 4 files changed, 356 insertions(+), 2 deletions(-) create mode 100644 atomic-agent/tests/record_view_mismatch_regression.rs create mode 100644 docs/atomic-record-view-mismatch.md diff --git a/atomic-agent/src/record/mod.rs b/atomic-agent/src/record/mod.rs index 59d05253..658dd88f 100644 --- a/atomic-agent/src/record/mod.rs +++ b/atomic-agent/src/record/mod.rs @@ -140,7 +140,7 @@ pub fn record_turn( // Step 1: Open the repository read-only for the initial status check. // This avoids blocking on the redb write lock — we only need read access // to decide whether there's work to do and which files are untracked. - let repo = atomic_repository::Repository::open_readonly(repo_root).map_err(|e| { + let mut repo = atomic_repository::Repository::open_readonly(repo_root).map_err(|e| { AgentError::RecordFailed { session_id: options.session.session_id.clone(), turn_number: options.turn_number, @@ -148,6 +148,25 @@ pub fn record_turn( } })?; + // Align change detection with the view we will record onto, BEFORE the first + // status() — in memory only, so the read-only no-lock fast path is preserved. + // + // `record()` applies/filters against `session.view_name`, but `status()` + // reads `self.current_view`. The real hook flow switches `current_view` at + // session start; direct callers (the noname ACP→TurnOrchestrator bridge, + // tests) do not, leaving detection on `dev`. The dangerous case is a turn + // whose net effect is invisible from `dev` — e.g. deleting the last file + // that lived on the session view: the first status() would see a clean tree + // and the early `EmptyTurn` check below would fire, silently dropping the + // turn, before any later (write-handle) view switch could correct it. + // + // `set_current_view_in_memory` only assigns the field — no txn, no lock, no + // disk write — so the anti-hang design (record/mod.rs reopen-for-write) is + // untouched. If the session view does not exist yet (pure first turn), + // `status()`'s filter falls back to "show everything", so untracked + // detection still works. See `docs/atomic-record-view-mismatch.md`. + repo.set_current_view_in_memory(&options.session.view_name); + // Step 2: Status — find out what the agent changed. // Include untracked files because agent turns commonly create new source, // config, and test files. Those must be auto-added before recording so the @@ -194,7 +213,7 @@ pub fn record_turn( // skips the table-init `begin_write()` that `open()` does — the tables // already exist and that write lock is the primary cause of hook hangs // when another process holds a transaction. - let repo = atomic_repository::Repository::open_existing(repo_root).map_err(|e| { + let mut repo = atomic_repository::Repository::open_existing(repo_root).map_err(|e| { AgentError::RecordFailed { session_id: options.session.session_id.clone(), turn_number: options.turn_number, @@ -202,6 +221,32 @@ pub fn record_turn( } })?; + // Persist the view alignment on the write handle so the post-add status + // refresh and `record()` apply agree with the in-memory alignment done on + // the read-only handle above. + // + // A `ViewNotFound` is the legitimate first-turn case — the session view does + // not exist yet; `record()`'s apply creates it, and detection was already + // aligned in memory. Any other error (DB / failed `.atomic/current_view` + // write) must NOT be swallowed: continuing would leave detection and apply + // reading different views again, so surface it as `RecordFailed`. + match repo.set_current_view(&options.session.view_name) { + Ok(()) => {} + Err(atomic_repository::RepositoryError::ViewNotFound { .. }) => { + log::debug!( + "session view '{}' not yet created; first-turn apply will create it", + options.session.view_name + ); + } + Err(e) => { + return Err(AgentError::RecordFailed { + session_id: options.session.session_id.clone(), + turn_number: options.turn_number, + reason: format!("Failed to align current view before record: {}", e), + }); + } + } + if !untracked_paths.is_empty() { log::info!( "Adding {} untracked file{} created by agent", diff --git a/atomic-agent/tests/record_view_mismatch_regression.rs b/atomic-agent/tests/record_view_mismatch_regression.rs new file mode 100644 index 00000000..cc57e7e0 --- /dev/null +++ b/atomic-agent/tests/record_view_mismatch_regression.rs @@ -0,0 +1,157 @@ +//! Regression test for the `record_turn` detection/apply view mismatch. +//! +//! See `docs/atomic-record-view-mismatch.md`. The bug: `record_turn` runs its +//! change *detection* (`status()`) against the repository's `current_view` +//! (`dev`), but *applies* the recorded change to `session.view_name` — which +//! `AgentSession::new()` sets to a **random haikunator view** that does not +//! exist in a fresh repo. Turn 1 (a new, untracked file) bypasses the view +//! filter and records onto the phantom view; Turn 2 (modifying the now-tracked +//! file) detects against `dev`, the view filter hides the file's creating change +//! (it lives on the random view), and `status` reports clean → `EmptyTurn`. +//! +//! This test uses the **default random `view_name`** from `AgentSession::new()` +//! — it deliberately does NOT pin `view_name = DEFAULT_VIEW`. That distinguishes +//! it from `tests/env5_admission.rs`, which pins the view and therefore cannot +//! catch this bug. Before the fix this test fails on Turn 2 with `EmptyTurn`; +//! after the fix (detection and apply share one view) both turns record. + +use atomic_agent::event::{HookType, TurnEvent}; +use atomic_agent::record::{record_turn, TurnRecordOptions}; +use atomic_agent::turn::AgentSession; + +use atomic_core::types::Base32; +use atomic_repository::Repository; + +/// A session with the DEFAULT (random) view_name, exactly as a real agent run +/// gets it. No `view_name` override — that is the point of this regression. +fn default_session() -> AgentSession { + let mut s = AgentSession::new("regress-sess", "opencode", "OpenCode"); + s.set_model_info("anthropic", "claude-opus-4"); + s +} + +fn options<'a>( + session: &'a AgentSession, + event: &'a TurnEvent, + turn_number: u32, + prompt: &str, +) -> TurnRecordOptions<'a> { + TurnRecordOptions { + session, + event, + turn_number, + turn_duration_ms: 1000, + prompt: Some(prompt.to_string()), + } +} + +/// Two consecutive turns with the default random session view: a new file then a +/// modification to that same file. Both must record. The second turn is the one +/// that regressed (it returned `EmptyTurn` before the fix). +#[test] +fn two_turns_with_default_random_view_both_record() { + let dir = tempfile::tempdir().expect("tempdir"); + let root = dir.path(); + Repository::init(root).expect("init disposable repo"); + + let session = default_session(); + // Sanity: this session really is using a non-default, random view name — + // otherwise the test would not exercise the mismatch. + assert_ne!( + session.view_name, "dev", + "regression precondition: AgentSession::new must use a random (non-default) view_name" + ); + + // Turn 1 — agent creates a new file. + std::fs::write(root.join("login.rs"), "fn attach_pdf() {}\n").expect("write new file"); + let event1 = TurnEvent::new("regress-sess", HookType::TurnEnd); + let first = record_turn(root, &options(&session, &event1, 1, "create login.rs")) + .expect("turn 1 (new file) should record"); + assert!( + !first.hash.to_base32().is_empty(), + "turn 1 must yield a hash" + ); + + // Turn 2 — agent modifies the now-tracked file. + // + // The new content is a DIFFERENT byte length than turn 1's, on purpose: this + // test targets the view-mismatch bug, and we do not want it to also depend on + // mtime granularity. `record_turn`'s detection uses `StatusOptions::fast()` + // (no content hash), so a same-size rewrite landing in the same filesystem + // mtime granule could be mis-classified Clean ("racy git"); keeping the size + // distinct means the size check alone guarantees Modified is observed, + // regardless of scheduling. + let turn1_body = "fn attach_pdf() {}\n"; + let turn2_body = "fn attach_pdf() {\n // Firefox + Safari support\n}\n"; + assert_ne!( + turn1_body.len(), + turn2_body.len(), + "turn 2 must change the file size so detection never relies on mtime granularity" + ); + std::fs::write(root.join("login.rs"), turn2_body).expect("modify tracked file"); + let event2 = TurnEvent::new("regress-sess", HookType::TurnEnd); + let second = record_turn(root, &options(&session, &event2, 2, "fix pdf upload")) + // Before the fix this is `Err(EmptyTurn)`. + .expect("turn 2 (modified tracked file) should record — regression: was EmptyTurn"); + + assert!( + !second.hash.to_base32().is_empty(), + "turn 2 must yield a hash" + ); + assert_ne!( + first.hash.to_base32(), + second.hash.to_base32(), + "the modification must produce a change distinct from the creation" + ); +} + +/// The narrow case that the *modify* test above cannot catch: a turn whose net +/// effect is invisible from the misaligned `dev` view — deleting the **last** +/// file that lived on the session view, leaving the working tree empty. +/// +/// From `dev`, the session view's file is filtered out of the tracked set and, +/// once deleted, is not even present to show as untracked — so the working tree +/// looks clean. Before the fix, `record_turn`'s **first** `status()` (on the +/// read-only handle, before any view switch) saw a clean tree and returned +/// `EmptyTurn`, silently dropping the deletion. The fix aligns the detection +/// view *in memory before* that first status, so the deletion is observed. +/// +/// (A *modify* on the misaligned view surfaces the file as Untracked — so +/// `untracked_count() != 0`, the early check doesn't fire, and it records even +/// pre-fix. Only the full-deletion case exercises the early-return window.) +#[test] +fn delete_last_session_file_records_instead_of_emptyturn() { + let dir = tempfile::tempdir().expect("tempdir"); + let root = dir.path(); + Repository::init(root).expect("init disposable repo"); + + let session = default_session(); + assert_ne!( + session.view_name, "dev", + "regression precondition: session must use a random (non-default) view_name" + ); + + // Turn 1 — create the only file and record it (onto the session view). + std::fs::write(root.join("only.rs"), "fn only() {}\n").expect("write file"); + let event1 = TurnEvent::new("regress-sess", HookType::TurnEnd); + let first = record_turn(root, &options(&session, &event1, 1, "create only.rs")) + .expect("turn 1 (new file) should record"); + + // Turn 2 — delete that one and only file. Net working tree (from dev) is now + // empty: this is the early-return window. + std::fs::remove_file(root.join("only.rs")).expect("delete the only file"); + let event2 = TurnEvent::new("regress-sess", HookType::TurnEnd); + let second = record_turn(root, &options(&session, &event2, 2, "delete only.rs")) + // Before the fix this is `Err(EmptyTurn)` — the deletion is lost. + .expect("turn 2 (delete last session file) should record — regression: was EmptyTurn"); + + assert!( + !second.hash.to_base32().is_empty(), + "the deletion must produce a change hash" + ); + assert_ne!( + first.hash.to_base32(), + second.hash.to_base32(), + "the deletion must be a change distinct from the creation" + ); +} diff --git a/atomic-repository/src/repository/mod.rs b/atomic-repository/src/repository/mod.rs index 781f7538..c3227920 100644 --- a/atomic-repository/src/repository/mod.rs +++ b/atomic-repository/src/repository/mod.rs @@ -577,6 +577,21 @@ default = "{}" Ok(()) } + /// Set the current view in memory only — no disk write, no transaction, no lock. + /// + /// Unlike [`set_current_view`](Self::set_current_view), this does not verify + /// that the view exists and does not persist `.atomic/current_view`. It only + /// assigns `self.current_view`, which governs which view `status()` reads. + /// + /// Use this to align change *detection* with the intended *apply* view on a + /// read-only repository handle, where taking a write lock would defeat the + /// no-lock fast path. If the view does not exist yet, `status()`'s view + /// filter falls back to "show everything", so detection still works. + #[inline] + pub fn set_current_view_in_memory(&mut self, view: &str) { + self.current_view = view.to_string(); + } + // ── Internal helpers ──────────────────────────────────────────────── /// Get a reference to the pristine database. diff --git a/docs/atomic-record-view-mismatch.md b/docs/atomic-record-view-mismatch.md new file mode 100644 index 00000000..da1f8fab --- /dev/null +++ b/docs/atomic-record-view-mismatch.md @@ -0,0 +1,137 @@ +# atomic-agent record-view mismatch (engine bug) + +> Status: **FIXED** on branch `fix/record-turn-view-mismatch` (2026-06-24). +> Surfaced by the env-5 admission test (`atomic-agent/tests/env5_admission.rs`), +> root-caused by two independent investigators (workflow `wf_025ec4b0-43a`), +> fix direction chosen by a safety investigation (workflow `wf_40655da1-f5b`). +> Regression test: `atomic-agent/tests/record_view_mismatch_regression.rs`. + +## TL;DR + +`record_turn` detected changes against the repo's `current_view` but applied them +to `session.view_name`. The **real agent hook flow is unaffected** — `session_start` +switches `current_view` to the session view before any turn is recorded +(`session_start.rs:192`), so detection and apply already agree. The bug only bit +**callers that bypass session start** — tests and, critically, the planned +**noname ACP→TurnOrchestrator bridge**, whose multi-turn runs would silently drop +every change after turn 1. Fixed by aligning the working view to the record view +inside `record_turn` itself, so the engine is self-consistent for any caller. + +## Symptom + +A second `record_turn()` on a **modified, already-tracked** file returns +`AgentError::EmptyTurn` ("nothing to record") in a fresh repository — even though +the file's content and size genuinely changed on disk. The first `record_turn()` +on the same file (when it was new/untracked) succeeds and yields a change hash. + +Not a timing/flush issue: reproduced with a 1.1s delay + a large size change. + +## Root cause + +`record_turn` detects changes against one view but applies them to another: + +- `Repository::init` creates `DEFAULT_VIEW = "dev"` and sets it as `current_view` + (`atomic-repository/src/repository/mod.rs:144`, `:283`). +- `AgentSession::new` sets `view_name` to a **random haikunator name** via + `make_view_name()` → `generate_goal_name()` (`atomic-agent/src/turn/session.rs:213`, `:243`). +- In `record_turn` (`atomic-agent/src/record/mod.rs`): + - **Detection**: `status()` at `:156` runs against the repo's `current_view` + (`dev`) — `status()` uses `self.current_view`, it takes no view argument + (`atomic-repository/src/repository/status.rs:34`). + - **Apply**: `repo.record()` is given `RecordOptions.view(session.view_name)` + at `:278` — the random view. + +Turn 1 (new file): the file is untracked, so it bypasses the view filter, is +auto-added, and is recorded onto the **random** view. Turn 2 (modified file): +`status("dev")` iterates the global TREE, finds the file, but the view-aware +filter (`status.rs:97-106`) sees that the file's creating change lives on the +random view, not on `dev`, so it **skips** the file. With nothing dirty, +`is_clean()` returns true → `EmptyTurn`. + +The FILE_INDEX and TREE are global, so this is purely the view-filter logic — the +file metadata is correct. + +## Fix (applied) + +`record_turn` aligns change detection with the record view in two places +(`atomic-agent/src/record/mod.rs`): + +1. **Before the first `status()`**, on the read-only handle, in memory only: + `repo.set_current_view_in_memory(&options.session.view_name)`. This is a new + `Repository` method (`atomic-repository/src/repository/mod.rs`) that assigns + `self.current_view` with **no transaction, no lock, no disk write** — so the + read-only no-lock fast path (added to avoid hook hangs) is preserved. If the + session view does not exist yet (pure first turn), `status()`'s filter falls + back to "show everything", so untracked detection still works. +2. **On the write handle**, the persisting `set_current_view(&session.view_name)` + remains, now with explicit error handling (see below) so the post-add + `status()` refresh and `record()` apply agree with the in-memory alignment. + +Why the in-memory pre-alignment matters: without it, the **first** `status()` + +early `EmptyTurn` check run on `dev` *before* any view switch. The dangerous +case is a turn whose net effect is invisible from `dev` — e.g. **deleting the +last file that lived on the session view**: the first `status()` sees a clean +tree and the early check fires, silently dropping the turn. (A *modify* does not +trigger it — from `dev` the file shows as Untracked, so `untracked_count() != 0` +and the early check is skipped; only a full deletion opens the early-return +window. This is why the regression suite includes a delete-only case.) + +Error handling on the persisting switch: tolerate `RepositoryError::ViewNotFound` +(legitimate first turn — apply creates the view; detection already aligned in +memory) but propagate DB / `.atomic/current_view` write failures as +`RecordFailed`, rather than swallowing all errors. + +Why this shape (chosen over threading a view param into `status()`): + +- **Minimal blast radius.** `record()` already filtered AND applied against + `session.view_name` (`record.rs:122`, `:705`) — only the standalone pre-flight + `status()` calls were mis-targeted. `status()`'s signature is untouched, so its + many other callers (`status_quick`/`status_tracked`/CLI `status`) are unaffected. +- **No change to provenance/attestation landing** (already on the session view); + attestation coverage *improves* because turns 2..N stop being skipped as `EmptyTurn`. +- **Real flow is a no-op** (`current_view` already switched at session start); only + bypass callers (tests, noname bridge) gain correctness. + +Sole production caller verified: `atomic-agent/src/turn/orchestrator/turn.rs:242`. +No cross-crate callers of `record_turn`. + +## Known adjacent risk (NOT fixed here): racy `EmptyTurn` on same-size rewrites + +`record_turn`'s detection uses `StatusOptions::fast()` (`hash_contents = false`), +whose FILE_INDEX fast path (`atomic-repository/src/repository/status.rs:271-330`) +decides Clean purely from `(mtime_secs, mtime_nanos, size)`. With no content-hash +fallback, a turn that rewrites a tracked file to the **same byte length** within +the **same filesystem mtime granule** as the previous turn's recorded mtime is +mis-classified Clean → spurious `EmptyTurn`. This is the classic "racy git" +problem and is **independent of the view-mismatch bug above**. + +It was investigated while chasing parallel-test flakiness but could **not** be +reproduced here: each test has an isolated tempdir repo (so nothing is shared), +and the regression/admission tests already change file size between turns, which +the size check catches regardless of mtime. The one reproducible failure during +this work was a `cargo` package-cache lock collision from running two `cargo +test` invocations concurrently — a tooling artifact, not a test or engine defect +(direct test-binary runs: 40/40 green; full suite: 15/15 green; single-threaded: +5/5 green). + +Left unfixed deliberately: the proper fix (verify by content hash when an mtime +match is "racy", i.e. within the current granule) touches `status.rs`'s widely +used fast path and is out of scope for this isolated view-mismatch fix. It is +flagged for the noname Y bridge, whose real multi-turn ACP runs could in +principle hit a same-size rewrite under load; if it ever surfaces there, fix it +in `status.rs` with the racy-clean → hash-fallback mitigation. + +## Implication for the Sherpa Y bridge (P0) + +This is a **real P0 requirement**, not just a test artifact: the noname +ACP→TurnOrchestrator bridge must keep the recording view aligned with the +detection view — either record onto the default view, or switch to the session +view before recording — or a real multi-turn ACP run will silently drop changes +after the first turn. Captured in `docs/sherpa-execution-v2.md` §1. + +## Workaround used by the admission test + +`atomic-agent/tests/env5_admission.rs` pins `session.view_name = DEFAULT_VIEW` +so detection and apply target the same view. With that alignment, modified-file +recording works correctly (test passes), confirming the engine's core recording +logic is sound and the defect is isolated to view routing. From 878bb105086855cce7cbd0c76461f69032797e85 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 24 Jun 2026 19:18:12 -0400 Subject: [PATCH 2/2] chore(agent): trim record view mismatch notes --- atomic-agent/src/record/mod.rs | 32 +--- .../tests/record_view_mismatch_regression.rs | 57 +------- atomic-repository/src/repository/mod.rs | 12 +- docs/atomic-record-view-mismatch.md | 137 ------------------ 4 files changed, 16 insertions(+), 222 deletions(-) delete mode 100644 docs/atomic-record-view-mismatch.md diff --git a/atomic-agent/src/record/mod.rs b/atomic-agent/src/record/mod.rs index 658dd88f..1d7cfe00 100644 --- a/atomic-agent/src/record/mod.rs +++ b/atomic-agent/src/record/mod.rs @@ -148,23 +148,9 @@ pub fn record_turn( } })?; - // Align change detection with the view we will record onto, BEFORE the first - // status() — in memory only, so the read-only no-lock fast path is preserved. - // - // `record()` applies/filters against `session.view_name`, but `status()` - // reads `self.current_view`. The real hook flow switches `current_view` at - // session start; direct callers (the noname ACP→TurnOrchestrator bridge, - // tests) do not, leaving detection on `dev`. The dangerous case is a turn - // whose net effect is invisible from `dev` — e.g. deleting the last file - // that lived on the session view: the first status() would see a clean tree - // and the early `EmptyTurn` check below would fire, silently dropping the - // turn, before any later (write-handle) view switch could correct it. - // - // `set_current_view_in_memory` only assigns the field — no txn, no lock, no - // disk write — so the anti-hang design (record/mod.rs reopen-for-write) is - // untouched. If the session view does not exist yet (pure first turn), - // `status()`'s filter falls back to "show everything", so untracked - // detection still works. See `docs/atomic-record-view-mismatch.md`. + // `status()` reads current_view, while `record()` writes to session.view_name. + // Align the read-only handle before the first status check; this keeps the + // no-lock fast path and prevents direct callers from seeing false EmptyTurn. repo.set_current_view_in_memory(&options.session.view_name); // Step 2: Status — find out what the agent changed. @@ -221,15 +207,9 @@ pub fn record_turn( } })?; - // Persist the view alignment on the write handle so the post-add status - // refresh and `record()` apply agree with the in-memory alignment done on - // the read-only handle above. - // - // A `ViewNotFound` is the legitimate first-turn case — the session view does - // not exist yet; `record()`'s apply creates it, and detection was already - // aligned in memory. Any other error (DB / failed `.atomic/current_view` - // write) must NOT be swallowed: continuing would leave detection and apply - // reading different views again, so surface it as `RecordFailed`. + // Keep the write handle on the same view for post-add status and record. + // First turns may target a view that record/apply will create; other failures + // mean we cannot trust the detection/apply alignment. match repo.set_current_view(&options.session.view_name) { Ok(()) => {} Err(atomic_repository::RepositoryError::ViewNotFound { .. }) => { diff --git a/atomic-agent/tests/record_view_mismatch_regression.rs b/atomic-agent/tests/record_view_mismatch_regression.rs index cc57e7e0..f83a9129 100644 --- a/atomic-agent/tests/record_view_mismatch_regression.rs +++ b/atomic-agent/tests/record_view_mismatch_regression.rs @@ -1,19 +1,8 @@ //! Regression test for the `record_turn` detection/apply view mismatch. //! -//! See `docs/atomic-record-view-mismatch.md`. The bug: `record_turn` runs its -//! change *detection* (`status()`) against the repository's `current_view` -//! (`dev`), but *applies* the recorded change to `session.view_name` — which -//! `AgentSession::new()` sets to a **random haikunator view** that does not -//! exist in a fresh repo. Turn 1 (a new, untracked file) bypasses the view -//! filter and records onto the phantom view; Turn 2 (modifying the now-tracked -//! file) detects against `dev`, the view filter hides the file's creating change -//! (it lives on the random view), and `status` reports clean → `EmptyTurn`. -//! -//! This test uses the **default random `view_name`** from `AgentSession::new()` -//! — it deliberately does NOT pin `view_name = DEFAULT_VIEW`. That distinguishes -//! it from `tests/env5_admission.rs`, which pins the view and therefore cannot -//! catch this bug. Before the fix this test fails on Turn 2 with `EmptyTurn`; -//! after the fix (detection and apply share one view) both turns record. +//! `status()` reads the repository current view, while `record_turn` records to +//! `session.view_name`. These tests use the default random session view so +//! direct callers that skip session-start exercise the mismatch. use atomic_agent::event::{HookType, TurnEvent}; use atomic_agent::record::{record_turn, TurnRecordOptions}; @@ -22,8 +11,7 @@ use atomic_agent::turn::AgentSession; use atomic_core::types::Base32; use atomic_repository::Repository; -/// A session with the DEFAULT (random) view_name, exactly as a real agent run -/// gets it. No `view_name` override — that is the point of this regression. +/// Keep the default random view_name; do not pin it to `dev`. fn default_session() -> AgentSession { let mut s = AgentSession::new("regress-sess", "opencode", "OpenCode"); s.set_model_info("anthropic", "claude-opus-4"); @@ -45,9 +33,7 @@ fn options<'a>( } } -/// Two consecutive turns with the default random session view: a new file then a -/// modification to that same file. Both must record. The second turn is the one -/// that regressed (it returned `EmptyTurn` before the fix). +/// A direct caller must be able to record a create turn followed by a modify turn. #[test] fn two_turns_with_default_random_view_both_record() { let dir = tempfile::tempdir().expect("tempdir"); @@ -55,14 +41,11 @@ fn two_turns_with_default_random_view_both_record() { Repository::init(root).expect("init disposable repo"); let session = default_session(); - // Sanity: this session really is using a non-default, random view name — - // otherwise the test would not exercise the mismatch. assert_ne!( session.view_name, "dev", "regression precondition: AgentSession::new must use a random (non-default) view_name" ); - // Turn 1 — agent creates a new file. std::fs::write(root.join("login.rs"), "fn attach_pdf() {}\n").expect("write new file"); let event1 = TurnEvent::new("regress-sess", HookType::TurnEnd); let first = record_turn(root, &options(&session, &event1, 1, "create login.rs")) @@ -72,15 +55,7 @@ fn two_turns_with_default_random_view_both_record() { "turn 1 must yield a hash" ); - // Turn 2 — agent modifies the now-tracked file. - // - // The new content is a DIFFERENT byte length than turn 1's, on purpose: this - // test targets the view-mismatch bug, and we do not want it to also depend on - // mtime granularity. `record_turn`'s detection uses `StatusOptions::fast()` - // (no content hash), so a same-size rewrite landing in the same filesystem - // mtime granule could be mis-classified Clean ("racy git"); keeping the size - // distinct means the size check alone guarantees Modified is observed, - // regardless of scheduling. + // Keep the sizes different so this test does not depend on mtime granularity. let turn1_body = "fn attach_pdf() {}\n"; let turn2_body = "fn attach_pdf() {\n // Firefox + Safari support\n}\n"; assert_ne!( @@ -91,7 +66,6 @@ fn two_turns_with_default_random_view_both_record() { std::fs::write(root.join("login.rs"), turn2_body).expect("modify tracked file"); let event2 = TurnEvent::new("regress-sess", HookType::TurnEnd); let second = record_turn(root, &options(&session, &event2, 2, "fix pdf upload")) - // Before the fix this is `Err(EmptyTurn)`. .expect("turn 2 (modified tracked file) should record — regression: was EmptyTurn"); assert!( @@ -105,20 +79,7 @@ fn two_turns_with_default_random_view_both_record() { ); } -/// The narrow case that the *modify* test above cannot catch: a turn whose net -/// effect is invisible from the misaligned `dev` view — deleting the **last** -/// file that lived on the session view, leaving the working tree empty. -/// -/// From `dev`, the session view's file is filtered out of the tracked set and, -/// once deleted, is not even present to show as untracked — so the working tree -/// looks clean. Before the fix, `record_turn`'s **first** `status()` (on the -/// read-only handle, before any view switch) saw a clean tree and returned -/// `EmptyTurn`, silently dropping the deletion. The fix aligns the detection -/// view *in memory before* that first status, so the deletion is observed. -/// -/// (A *modify* on the misaligned view surfaces the file as Untracked — so -/// `untracked_count() != 0`, the early check doesn't fire, and it records even -/// pre-fix. Only the full-deletion case exercises the early-return window.) +/// Deleting the last file on the session view must also produce a recorded turn. #[test] fn delete_last_session_file_records_instead_of_emptyturn() { let dir = tempfile::tempdir().expect("tempdir"); @@ -131,18 +92,14 @@ fn delete_last_session_file_records_instead_of_emptyturn() { "regression precondition: session must use a random (non-default) view_name" ); - // Turn 1 — create the only file and record it (onto the session view). std::fs::write(root.join("only.rs"), "fn only() {}\n").expect("write file"); let event1 = TurnEvent::new("regress-sess", HookType::TurnEnd); let first = record_turn(root, &options(&session, &event1, 1, "create only.rs")) .expect("turn 1 (new file) should record"); - // Turn 2 — delete that one and only file. Net working tree (from dev) is now - // empty: this is the early-return window. std::fs::remove_file(root.join("only.rs")).expect("delete the only file"); let event2 = TurnEvent::new("regress-sess", HookType::TurnEnd); let second = record_turn(root, &options(&session, &event2, 2, "delete only.rs")) - // Before the fix this is `Err(EmptyTurn)` — the deletion is lost. .expect("turn 2 (delete last session file) should record — regression: was EmptyTurn"); assert!( diff --git a/atomic-repository/src/repository/mod.rs b/atomic-repository/src/repository/mod.rs index c3227920..8d20a1b9 100644 --- a/atomic-repository/src/repository/mod.rs +++ b/atomic-repository/src/repository/mod.rs @@ -577,16 +577,10 @@ default = "{}" Ok(()) } - /// Set the current view in memory only — no disk write, no transaction, no lock. + /// Set the current view on this handle only. /// - /// Unlike [`set_current_view`](Self::set_current_view), this does not verify - /// that the view exists and does not persist `.atomic/current_view`. It only - /// assigns `self.current_view`, which governs which view `status()` reads. - /// - /// Use this to align change *detection* with the intended *apply* view on a - /// read-only repository handle, where taking a write lock would defeat the - /// no-lock fast path. If the view does not exist yet, `status()`'s view - /// filter falls back to "show everything", so detection still works. + /// This does not validate the view or persist `.atomic/current_view`. It is + /// intended for read-only handles that need `status()` to read another view. #[inline] pub fn set_current_view_in_memory(&mut self, view: &str) { self.current_view = view.to_string(); diff --git a/docs/atomic-record-view-mismatch.md b/docs/atomic-record-view-mismatch.md deleted file mode 100644 index da1f8fab..00000000 --- a/docs/atomic-record-view-mismatch.md +++ /dev/null @@ -1,137 +0,0 @@ -# atomic-agent record-view mismatch (engine bug) - -> Status: **FIXED** on branch `fix/record-turn-view-mismatch` (2026-06-24). -> Surfaced by the env-5 admission test (`atomic-agent/tests/env5_admission.rs`), -> root-caused by two independent investigators (workflow `wf_025ec4b0-43a`), -> fix direction chosen by a safety investigation (workflow `wf_40655da1-f5b`). -> Regression test: `atomic-agent/tests/record_view_mismatch_regression.rs`. - -## TL;DR - -`record_turn` detected changes against the repo's `current_view` but applied them -to `session.view_name`. The **real agent hook flow is unaffected** — `session_start` -switches `current_view` to the session view before any turn is recorded -(`session_start.rs:192`), so detection and apply already agree. The bug only bit -**callers that bypass session start** — tests and, critically, the planned -**noname ACP→TurnOrchestrator bridge**, whose multi-turn runs would silently drop -every change after turn 1. Fixed by aligning the working view to the record view -inside `record_turn` itself, so the engine is self-consistent for any caller. - -## Symptom - -A second `record_turn()` on a **modified, already-tracked** file returns -`AgentError::EmptyTurn` ("nothing to record") in a fresh repository — even though -the file's content and size genuinely changed on disk. The first `record_turn()` -on the same file (when it was new/untracked) succeeds and yields a change hash. - -Not a timing/flush issue: reproduced with a 1.1s delay + a large size change. - -## Root cause - -`record_turn` detects changes against one view but applies them to another: - -- `Repository::init` creates `DEFAULT_VIEW = "dev"` and sets it as `current_view` - (`atomic-repository/src/repository/mod.rs:144`, `:283`). -- `AgentSession::new` sets `view_name` to a **random haikunator name** via - `make_view_name()` → `generate_goal_name()` (`atomic-agent/src/turn/session.rs:213`, `:243`). -- In `record_turn` (`atomic-agent/src/record/mod.rs`): - - **Detection**: `status()` at `:156` runs against the repo's `current_view` - (`dev`) — `status()` uses `self.current_view`, it takes no view argument - (`atomic-repository/src/repository/status.rs:34`). - - **Apply**: `repo.record()` is given `RecordOptions.view(session.view_name)` - at `:278` — the random view. - -Turn 1 (new file): the file is untracked, so it bypasses the view filter, is -auto-added, and is recorded onto the **random** view. Turn 2 (modified file): -`status("dev")` iterates the global TREE, finds the file, but the view-aware -filter (`status.rs:97-106`) sees that the file's creating change lives on the -random view, not on `dev`, so it **skips** the file. With nothing dirty, -`is_clean()` returns true → `EmptyTurn`. - -The FILE_INDEX and TREE are global, so this is purely the view-filter logic — the -file metadata is correct. - -## Fix (applied) - -`record_turn` aligns change detection with the record view in two places -(`atomic-agent/src/record/mod.rs`): - -1. **Before the first `status()`**, on the read-only handle, in memory only: - `repo.set_current_view_in_memory(&options.session.view_name)`. This is a new - `Repository` method (`atomic-repository/src/repository/mod.rs`) that assigns - `self.current_view` with **no transaction, no lock, no disk write** — so the - read-only no-lock fast path (added to avoid hook hangs) is preserved. If the - session view does not exist yet (pure first turn), `status()`'s filter falls - back to "show everything", so untracked detection still works. -2. **On the write handle**, the persisting `set_current_view(&session.view_name)` - remains, now with explicit error handling (see below) so the post-add - `status()` refresh and `record()` apply agree with the in-memory alignment. - -Why the in-memory pre-alignment matters: without it, the **first** `status()` + -early `EmptyTurn` check run on `dev` *before* any view switch. The dangerous -case is a turn whose net effect is invisible from `dev` — e.g. **deleting the -last file that lived on the session view**: the first `status()` sees a clean -tree and the early check fires, silently dropping the turn. (A *modify* does not -trigger it — from `dev` the file shows as Untracked, so `untracked_count() != 0` -and the early check is skipped; only a full deletion opens the early-return -window. This is why the regression suite includes a delete-only case.) - -Error handling on the persisting switch: tolerate `RepositoryError::ViewNotFound` -(legitimate first turn — apply creates the view; detection already aligned in -memory) but propagate DB / `.atomic/current_view` write failures as -`RecordFailed`, rather than swallowing all errors. - -Why this shape (chosen over threading a view param into `status()`): - -- **Minimal blast radius.** `record()` already filtered AND applied against - `session.view_name` (`record.rs:122`, `:705`) — only the standalone pre-flight - `status()` calls were mis-targeted. `status()`'s signature is untouched, so its - many other callers (`status_quick`/`status_tracked`/CLI `status`) are unaffected. -- **No change to provenance/attestation landing** (already on the session view); - attestation coverage *improves* because turns 2..N stop being skipped as `EmptyTurn`. -- **Real flow is a no-op** (`current_view` already switched at session start); only - bypass callers (tests, noname bridge) gain correctness. - -Sole production caller verified: `atomic-agent/src/turn/orchestrator/turn.rs:242`. -No cross-crate callers of `record_turn`. - -## Known adjacent risk (NOT fixed here): racy `EmptyTurn` on same-size rewrites - -`record_turn`'s detection uses `StatusOptions::fast()` (`hash_contents = false`), -whose FILE_INDEX fast path (`atomic-repository/src/repository/status.rs:271-330`) -decides Clean purely from `(mtime_secs, mtime_nanos, size)`. With no content-hash -fallback, a turn that rewrites a tracked file to the **same byte length** within -the **same filesystem mtime granule** as the previous turn's recorded mtime is -mis-classified Clean → spurious `EmptyTurn`. This is the classic "racy git" -problem and is **independent of the view-mismatch bug above**. - -It was investigated while chasing parallel-test flakiness but could **not** be -reproduced here: each test has an isolated tempdir repo (so nothing is shared), -and the regression/admission tests already change file size between turns, which -the size check catches regardless of mtime. The one reproducible failure during -this work was a `cargo` package-cache lock collision from running two `cargo -test` invocations concurrently — a tooling artifact, not a test or engine defect -(direct test-binary runs: 40/40 green; full suite: 15/15 green; single-threaded: -5/5 green). - -Left unfixed deliberately: the proper fix (verify by content hash when an mtime -match is "racy", i.e. within the current granule) touches `status.rs`'s widely -used fast path and is out of scope for this isolated view-mismatch fix. It is -flagged for the noname Y bridge, whose real multi-turn ACP runs could in -principle hit a same-size rewrite under load; if it ever surfaces there, fix it -in `status.rs` with the racy-clean → hash-fallback mitigation. - -## Implication for the Sherpa Y bridge (P0) - -This is a **real P0 requirement**, not just a test artifact: the noname -ACP→TurnOrchestrator bridge must keep the recording view aligned with the -detection view — either record onto the default view, or switch to the session -view before recording — or a real multi-turn ACP run will silently drop changes -after the first turn. Captured in `docs/sherpa-execution-v2.md` §1. - -## Workaround used by the admission test - -`atomic-agent/tests/env5_admission.rs` pins `session.view_name = DEFAULT_VIEW` -so detection and apply target the same view. With that alignment, modified-file -recording works correctly (test passes), confirming the engine's core recording -logic is sound and the defect is isolated to view routing.