From d6419f63d9b399c6659d4a13cf9614149be0a2c6 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 16 Sep 2026 16:47:50 -0400 Subject: [PATCH 1/3] fix: wait for database contention during session initialization Atomic intent: ATOM::vince::12 (01M2NYA6AV3JHQZP31Z4R8R4MG). Atomic change: RKNP2QWZDOE7ZRANEFYLQFBKNG2LGW6XTDLHYSHAPN2ACSNQAJ2A. Based on PR #190 without PR #189 source changes. --- .../src/turn/orchestrator/session_start.rs | 20 ++++-- .../tests/database_owner_integration_test.rs | 52 ++++++++++++++ docs/subagent-session-start.md | 68 +++++++++++++++++++ 3 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 docs/subagent-session-start.md diff --git a/atomic-agent/src/turn/orchestrator/session_start.rs b/atomic-agent/src/turn/orchestrator/session_start.rs index 83859cff..a434e6c0 100644 --- a/atomic-agent/src/turn/orchestrator/session_start.rs +++ b/atomic-agent/src/turn/orchestrator/session_start.rs @@ -7,6 +7,10 @@ use crate::turn::session::AgentSession; use super::{vendor_from_agent_name, DispatchResult, TurnOrchestrator}; +// Session lifecycle operations use short writable handles, just like recording. +// Retry acquisition only; replaying the whole lifecycle could fork/switch twice. +const SESSION_DATABASE_WAIT: std::time::Duration = std::time::Duration::from_secs(10); + impl TurnOrchestrator { /// Handle a SessionStart event. /// @@ -170,7 +174,10 @@ impl TurnOrchestrator { // exists (resumed session), we log and continue — recording will // still work, it just won't have the parent's history. if session.parent_view.is_none() { - match atomic_repository::Repository::open_existing(&self.repo_root) { + match atomic_repository::Repository::open_existing_wait( + &self.repo_root, + SESSION_DATABASE_WAIT, + ) { Ok(repo) if repo.is_sandbox() => { // A sandbox is a materialized copy of the project; `record` // writes to the *canonical* graph (shared pristine + @@ -322,7 +329,10 @@ impl TurnOrchestrator { session: &crate::turn::session::AgentSession, ended_at: Option, ) { - match atomic_repository::Repository::open_existing(&self.repo_root) { + match atomic_repository::Repository::open_existing_wait( + &self.repo_root, + SESSION_DATABASE_WAIT, + ) { Ok(repo) => { if let Err(e) = repo.upsert_session_lifecycle( &session.session_id, @@ -396,8 +406,10 @@ impl TurnOrchestrator { // view from the user's view so that the agent's changes // are isolated and the view filter only exposes the // parent's files. - if let Ok(mut repo) = atomic_repository::Repository::open_existing(&self.repo_root) - { + if let Ok(mut repo) = atomic_repository::Repository::open_existing_wait( + &self.repo_root, + SESSION_DATABASE_WAIT, + ) { let current = repo.current_view().to_string(); if repo.is_sandbox() { diff --git a/atomic-cli/tests/database_owner_integration_test.rs b/atomic-cli/tests/database_owner_integration_test.rs index 68418f52..ff531c90 100644 --- a/atomic-cli/tests/database_owner_integration_test.rs +++ b/atomic-cli/tests/database_owner_integration_test.rs @@ -197,6 +197,58 @@ fn run_agent_hook(repository: &std::path::Path, agent: &str, verb: &str, payload wait_for_output(child, &format!("{agent} {verb}")) } +#[test] +fn concurrent_session_starts_wait_for_writer_and_persist_views_and_lifecycle() { + let temp = TempDir::new().unwrap(); + let repository = temp.path().join("repo"); + let writer = Repository::init(&repository).unwrap(); + let pending: Vec<_> = (0..8) + .map(|index| { + let root = repository.clone(); + thread::spawn(move || { + let session = format!("start-contended-{index}"); + let payload = serde_json::to_vec(&serde_json::json!({ + "session_id": session, "cwd": root, + })) + .unwrap(); + run_agent_hook(&root, "opencode", "session-start", &payload) + }) + }) + .collect(); + // Hold a real incompatible handle across all eight independent processes. + // The hook must wait internally, without a test-side retry or startup queue. + thread::sleep(Duration::from_millis(300)); + let returned_early = pending.iter().any(thread::JoinHandle::is_finished); + drop(writer); + let outputs: Vec<_> = pending.into_iter().map(|p| p.join().unwrap()).collect(); + assert!( + !returned_early, + "session start skipped a contended database" + ); + let repo = Repository::open_existing(&repository).unwrap(); + let mut views = HashSet::new(); + for (index, output) in outputs.iter().enumerate() { + assert!(output.status.success(), "{output:?}"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(!stderr.contains("Cannot acquire lock"), "{stderr}"); + let sid = format!("start-contended-{index}"); + let session: Value = serde_json::from_slice( + &std::fs::read(repository.join(format!(".atomic/sessions/{sid}.json"))).unwrap(), + ) + .unwrap(); + let view = session["view_name"].as_str().unwrap(); + assert!( + views.insert(view.to_string()), + "sessions must have distinct views" + ); + assert!(repo.get_view_info(view).is_ok(), "view {view} missing"); + assert!( + repo.get_session_ledger(&sid).unwrap().is_some(), + "lifecycle missing for {sid}" + ); + } +} + #[test] fn read_only_turn_does_not_reuse_its_goal_for_the_next_turn_across_agents() { for (agent, prompt_verb) in [ diff --git a/docs/subagent-session-start.md b/docs/subagent-session-start.md new file mode 100644 index 00000000..85aae0ec --- /dev/null +++ b/docs/subagent-session-start.md @@ -0,0 +1,68 @@ +# Subagent session-start contention + +This follow-up is based on PR #190 at +`67b74c8881ce4afe8a758beea093bc486a1da3f7`. It keeps redb 4.2 and does not +include Aaron's identity-delegation changes. Atomic intent: `ATOM::vince::12` +(`01M2NYA6AV3JHQZP31Z4R8R4MG`), view `fix-subagent-session-start`. + +## Problem and change + +Live OpenCode parent/subagent testing exposed session-start warnings that skipped +view creation when another process held the database. The existing record and +ordinary-read waiting paths did not cover session initialization. + +Session creation, fallback creation and lifecycle synchronization now use the +existing typed contention retry helper, with a ten-second limit per database +acquisition. Only opening the handle is retried; view creation and lifecycle +operations are not replayed. Non-contention errors retain their existing +behavior. This does not make a permanently occupied database available. + +## Validation (2026-09-16, macOS) + +- New regression launches eight independent foreground OpenCode session-start + processes while a real writable handle holds the database. After releasing + the handle, all eight create distinct views and persist their lifecycle rows, + without lock warnings or test-side retries. +- 19 database-owner integration tests, 4 ordinary-read contention tests, and + 36 orchestrator tests passed. Workspace Clippy with `-D warnings` passed. +- An initial concurrently loaded run hit an existing three-second corruption + test deadline. The complete two-integration-test-binary rerun passed unchanged; + the deadline was not relaxed. + +An independent fix in `atomic-opencode` is also required: its former plugin-global +session ID and buffers mixed parent/child events. That fix keeps separate state +and ordered foreground hook calls per session. + +With both fixes and Aaron's PR #189 at +`9c6fdff35a6a6ce73ea6a8c79eea1a26af136828` merged **only in a disposable local +build**, OpenCode 1.18.30 ran a real parent and two overlapping atomic subagents +using `openrouter/openai/gpt-5-mini`: + +- All 21 tool calls completed; both child task intervals overlapped for about + 39 seconds. Each child ran three intent-list/status pairs. +- Three separate journal checkpoints reached `Published`. Their provenance + content hashes verified. Frozen journal tool IDs matched their originating + OpenCode sessions, with zero foreign or missing completed tool IDs. +- No database-lock warnings or plugin hook failures were observed. + +The earlier run on #190 plus these fixes also published three correctly routed +checkpoints. Its parent attempted an external `/tmp` write that the test's +OpenCode permissions rejected; that run is not counted as a complete three-file +write test. The combined run constrained all files to its disposable directory. + +## Remaining shared-directory limitation + +This is not a claim of complete multi-agent workspace isolation. In the combined +run, one child's Stop recorded both child result files; the other child's +checkpoint contained provenance but no change hash. The parent recorded its own +file. Both child files exist in the recorded change, but appear untracked from +the parent's view after it becomes current again. + +Separate hook event streams do not assign ownership of all files in a shared +working tree. Workspace/view isolation and merging child changes into the parent +remain separate integration work. This result does not establish overall bridge +or release readiness. + +Local raw evidence is under `.gstack/subagent-fixes-20260916/` in the enclosing +workspace: Rust test logs, plugin regression logs, and `live-with189/` containing +the OpenCode events, session audit, frozen-journal audit and child change details. From 4686a4fac95322bd41010e1f391fc6e478a3840a Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 16 Sep 2026 17:07:55 -0400 Subject: [PATCH 2/3] fix: scope shared-workspace checkpoints to verified session files --- Cargo.lock | 1 + atomic-agent/Cargo.toml | 1 + atomic-agent/src/record/mod.rs | 24 ++- atomic-agent/src/record/scope.rs | 166 ++++++++++++++++++ atomic-agent/src/record/tests.rs | 82 +++++++++ .../src/turn/orchestrator/session_end.rs | 2 +- .../src/turn/orchestrator/session_start.rs | 43 +++++ atomic-agent/src/turn/orchestrator/turn.rs | 7 +- atomic-agent/src/turn/session.rs | 5 + atomic-cli/src/commands/agent/hooks.rs | 20 +++ .../tests/database_owner_integration_test.rs | 91 ++++++++++ 11 files changed, 438 insertions(+), 4 deletions(-) create mode 100644 atomic-agent/src/record/scope.rs diff --git a/Cargo.lock b/Cargo.lock index 0fc902b0..41f84dd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -147,6 +147,7 @@ dependencies = [ "serde", "serde_json", "serial_test", + "sha2", "tempfile", "thiserror 1.0.69", "tokio", diff --git a/atomic-agent/Cargo.toml b/atomic-agent/Cargo.toml index 9ac07fcd..50be2818 100644 --- a/atomic-agent/Cargo.toml +++ b/atomic-agent/Cargo.toml @@ -29,6 +29,7 @@ zstd = { workspace = true } # Hashing blake3 = { workspace = true } +sha2 = "0.10" # Time chrono = { workspace = true } diff --git a/atomic-agent/src/record/mod.rs b/atomic-agent/src/record/mod.rs index 88e637f1..cc20f4cf 100644 --- a/atomic-agent/src/record/mod.rs +++ b/atomic-agent/src/record/mod.rs @@ -58,6 +58,7 @@ pub mod message; pub mod options; pub mod provenance; +pub mod scope; #[cfg(test)] mod tests; @@ -138,6 +139,16 @@ pub fn record_turn( repo_root: &Path, options: &TurnRecordOptions<'_>, ) -> AgentResult { + let manifest = scope::manifest(options)?; + if let Some(files) = &manifest { + scope::validate(repo_root, files, options)?; + if files.is_empty() { + return Err(AgentError::EmptyTurn { + session_id: options.session.session_id.clone(), + turn_number: options.turn_number, + }); + } + } // Step 1: Open the repository read-only for the initial status check. // This can coexist with other readers. Wait for a transient incompatible // writer before deciding whether work or untracked files exist. @@ -169,6 +180,8 @@ pub fn record_turn( reason: format!("Failed to get repository status: {}", e), })?; + let status = scope::filter(status, manifest.as_ref()); + // Check if there's anything to record at all if status.is_clean() && status.untracked_count() == 0 { return Err(AgentError::EmptyTurn { @@ -289,6 +302,10 @@ pub fn record_turn( } } + if let Some(files) = &manifest { + scope::validate(repo_root, files, options)?; + } + if !untracked_paths.is_empty() { log::info!( "Adding {} untracked file{} created by agent", @@ -323,6 +340,7 @@ pub fn record_turn( reason: format!("Failed to refresh repository status after add: {}", e), })?; + let status = scope::filter(status, manifest.as_ref()); if status.is_clean() { return Err(AgentError::EmptyTurn { session_id: options.session.session_id.clone(), @@ -360,7 +378,7 @@ pub fn record_turn( // HashedChange.metadata — part of the change's cryptographic identity. // This means session structure (turn number, timing, files, agent name) // is tamper-evident and commutes via patch theory. - let record_options = atomic_repository::record::RecordOptions::new() + let mut record_options = atomic_repository::record::RecordOptions::new() .with_all(true) .view(options.session.view_name.clone()) .apply_after_record(true) @@ -373,6 +391,10 @@ pub fn record_turn( .provenance(vec![provenance_entry]) .metadata_bytes(envelope_bytes); + if manifest.is_some() { + record_options = record_options.with_all(false).paths(status_files.clone()); + } + let mut outcome = match repo.record(header, record_options) { Ok(outcome) => outcome, Err(atomic_repository::record::RecordError::NothingToRecord) => { diff --git a/atomic-agent/src/record/scope.rs b/atomic-agent/src/record/scope.rs new file mode 100644 index 00000000..d2989c7b --- /dev/null +++ b/atomic-agent/src/record/scope.rs @@ -0,0 +1,166 @@ +//! Explicit, fingerprinted file ownership for hooks sharing a working tree. +use std::collections::BTreeMap; +use std::path::{Component, Path}; + +use atomic_repository::status::{RepositoryStatus, StatusOptions}; +use atomic_repository::Repository; +use sha2::{Digest, Sha256}; + +use super::TurnRecordOptions; +use crate::error::{AgentError, AgentResult}; + +pub type FileManifest = BTreeMap>; + +fn checked_path(root: &Path, path: &str) -> Result { + if path.is_empty() + || !Path::new(path) + .components() + .all(|c| matches!(c, Component::Normal(_))) + { + return Err(format!("invalid scoped file path: {path:?}")); + } + let root = root.canonicalize().map_err(|e| e.to_string())?; + let full = root.join(path); + let mut parent = full.parent(); + while let Some(p) = parent { + if p.exists() { + if !p + .canonicalize() + .map_err(|e| e.to_string())? + .starts_with(&root) + { + return Err(format!("scoped path escapes repository: {path}")); + } + break; + } + parent = p.parent(); + } + if Path::new(path) + .components() + .any(|c| matches!(c, Component::Normal(n) if n == ".atomic" || n == ".git")) + { + return Err(format!("repository metadata cannot be scoped: {path}")); + } + Ok(full) +} + +/// Fingerprint a file without following its final symlink. `None` means deleted. +/// Reject directories and paths outside the working tree. +pub fn fingerprint(root: &Path, path: &str) -> Result, String> { + let full = checked_path(root, path)?; + let meta = match std::fs::symlink_metadata(&full) { + Ok(m) => m, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(e.to_string()), + }; + if meta.file_type().is_symlink() { + let target = std::fs::read_link(full).map_err(|e| e.to_string())?; + return Ok(Some(format!( + "link:{:x}", + Sha256::digest(target.as_os_str().as_encoded_bytes()) + ))); + } + if !meta.is_file() { + return Err(format!("not a regular scoped file: {path}")); + } + #[cfg(unix)] + let executable = { + use std::os::unix::fs::PermissionsExt; + meta.permissions().mode() & 0o111 != 0 + }; + #[cfg(not(unix))] + let executable = false; + let mut file = std::fs::File::open(full).map_err(|e| e.to_string())?; + let mut hash = Sha256::new(); + std::io::copy(&mut file, &mut hash).map_err(|e| e.to_string())?; + Ok(Some(format!( + "file:{hash:x}:{executable}", + hash = hash.finalize() + ))) +} + +/// Return fingerprints for dirty files and explicitly requested prior candidates. +/// The protocol version also lets plugins refuse an older, unscoped CLI. +pub fn snapshot(root: &Path, extra: &[String]) -> Result { + let repo = Repository::open_readonly_wait(root, std::time::Duration::from_secs(10)) + .map_err(|e| e.to_string())?; + let status = repo + .status(StatusOptions::fast().with_untracked(true)) + .map_err(|e| e.to_string())?; + let dirty: Vec = status + .entries() + .iter() + .map(|e| e.path().to_string_lossy().to_string()) + .collect(); + let mut files = FileManifest::new(); + for path in status + .entries() + .iter() + .map(|e| e.path().to_string_lossy().to_string()) + .chain(extra.iter().cloned()) + { + if root.join(&path).is_dir() && !root.join(&path).is_symlink() { + continue; + } + files.insert(path.clone(), fingerprint(root, &path)?); + } + Ok( + serde_json::json!({"scope_version":1, "view":repo.current_view(), "files":files, "dirty":dirty}), + ) +} + +pub(super) fn manifest(options: &TurnRecordOptions<'_>) -> AgentResult> { + let raw = options + .event + .raw_json + .as_ref() + .and_then(|v| v.get("record_files")); + let fail = |reason| AgentError::RecordFailed { + session_id: options.session.session_id.clone(), + turn_number: options.turn_number, + reason, + }; + match raw { + Some(value) => serde_json::from_value(value.clone()) + .map(Some) + .map_err(|e| fail(format!("invalid record_files manifest: {e}"))), + None if options.session.explicit_record_files => Err(fail( + "explicit record_files manifest required; refusing to record shared working tree" + .into(), + )), + None => Ok(None), + } +} + +pub(super) fn validate( + root: &Path, + files: &FileManifest, + options: &TurnRecordOptions<'_>, +) -> AgentResult<()> { + for (path, expected) in files { + let actual = fingerprint(root, path).map_err(|reason| AgentError::RecordFailed { + session_id: options.session.session_id.clone(), + turn_number: options.turn_number, + reason, + })?; + if &actual != expected { + return Err(AgentError::RecordFailed { session_id: options.session.session_id.clone(), turn_number: options.turn_number, reason: format!("scoped file changed after this session's tool finished: {path}; refusing ambiguous ownership") }); + } + } + Ok(()) +} + +pub(super) fn filter(status: RepositoryStatus, files: Option<&FileManifest>) -> RepositoryStatus { + let Some(files) = files else { + return status; + }; + let mut result = RepositoryStatus::new(status.view().to_string(), status.state().copied()); + for entry in status.entries() { + if entry.details() != Some("directory") + && files.contains_key(entry.path().to_string_lossy().as_ref()) + { + result.add_entry(entry.clone()); + } + } + result +} diff --git a/atomic-agent/src/record/tests.rs b/atomic-agent/src/record/tests.rs index 0b37e163..4da15da1 100644 --- a/atomic-agent/src/record/tests.rs +++ b/atomic-agent/src/record/tests.rs @@ -1175,3 +1175,85 @@ fn test_orphaned_session_view_duplicates_content_on_merge() { "session B's edit should survive the merge" ); } + +#[test] +fn scoped_record_keeps_sibling_and_preexisting_files_out() { + let dir = tempfile::tempdir().unwrap(); + let mut repo = atomic_repository::Repository::init(dir.path()).unwrap(); + let parent = repo.current_view().to_string(); + let mut a = AgentSession::new("child-a", "opencode", "OpenCode"); + let mut b = AgentSession::new("child-b", "opencode", "OpenCode"); + for s in [&mut a, &mut b] { + s.explicit_record_files = true; + s.set_parent_view(repo.current_view()); + repo.create_view_from(&s.view_name, &parent).unwrap(); + } + drop(repo); + for (name, text) in [ + ("a.txt", "child a"), + ("b.txt", "child b"), + ("human.txt", "leave alone"), + ] { + std::fs::write(dir.path().join(name), text).unwrap(); + } + let mut hashes = Vec::new(); + for (s, file) in [(&a, "a.txt"), (&b, "b.txt")] { + let event = make_event().with_raw_json( + serde_json::json!({"record_files":{file:scope::fingerprint(dir.path(),file).unwrap()}}), + ); + let outcome = record_turn(dir.path(), &make_options(s, &event)).unwrap(); + assert_eq!(outcome.recorded_file_list(), &[file.to_string()]); + hashes.push(outcome.hash); + } + assert_ne!(hashes[0], hashes[1]); + assert_eq!( + std::fs::read_to_string(dir.path().join("human.txt")).unwrap(), + "leave alone" + ); +} + +#[test] +fn scoped_record_empty_missing_invalid_or_stale_manifest_never_sweeps() { + let dir = tempfile::tempdir().unwrap(); + let repo = atomic_repository::Repository::init(dir.path()).unwrap(); + let mut session = make_session(); + session.view_name = repo.current_view().to_string(); + session.explicit_record_files = true; + drop(repo); + std::fs::write(dir.path().join("unrelated.txt"), "human").unwrap(); + let empty = make_event().with_raw_json(serde_json::json!({"record_files":{}})); + assert!(matches!( + record_turn(dir.path(), &make_options(&session, &empty)), + Err(AgentError::EmptyTurn { .. }) + )); + for payload in [ + serde_json::json!({}), + serde_json::json!({"record_files":null}), + serde_json::json!({"record_files":{"../escape":null}}), + serde_json::json!({"record_files":{"unrelated.txt":"wrong digest"}}), + serde_json::json!({"record_files":{".atomic/config.toml":null}}), + ] { + let event = make_event().with_raw_json(payload); + assert!(matches!( + record_turn(dir.path(), &make_options(&session, &event)), + Err(AgentError::RecordFailed { .. }) + )); + } + assert_eq!( + std::fs::read_to_string(dir.path().join("unrelated.txt")).unwrap(), + "human" + ); +} + +#[test] +fn scoped_snapshot_includes_requested_files_restored_to_clean_and_deletions() { + let dir = tempfile::tempdir().unwrap(); + drop(atomic_repository::Repository::init(dir.path()).unwrap()); + std::fs::write(dir.path().join("a.txt"), "a").unwrap(); + let first = scope::snapshot(dir.path(), &[]).unwrap(); + assert!(first["files"]["a.txt"].is_string()); + std::fs::remove_file(dir.path().join("a.txt")).unwrap(); + let after = scope::snapshot(dir.path(), &["a.txt".into()]).unwrap(); + assert!(after["files"].as_object().unwrap().contains_key("a.txt")); + assert!(after["files"]["a.txt"].is_null()); +} diff --git a/atomic-agent/src/turn/orchestrator/session_end.rs b/atomic-agent/src/turn/orchestrator/session_end.rs index c9bc5bd3..9ae32b89 100644 --- a/atomic-agent/src/turn/orchestrator/session_end.rs +++ b/atomic-agent/src/turn/orchestrator/session_end.rs @@ -63,7 +63,7 @@ impl TurnOrchestrator { // session end. Record them now. Idempotent: agents that already // recorded each turn on `stop` leave a clean working copy here, so // `record_turn` returns `EmptyTurn` and this is a no-op for them. - { + if !session.explicit_record_files || had_active_turn { // Ensure the working copy is on the session's agent view before // recording. session-start aligns to it, but that can drift back to // the parent view by session end (observed with Cursor's CLI), which diff --git a/atomic-agent/src/turn/orchestrator/session_start.rs b/atomic-agent/src/turn/orchestrator/session_start.rs index a434e6c0..1965c2ed 100644 --- a/atomic-agent/src/turn/orchestrator/session_start.rs +++ b/atomic-agent/src/turn/orchestrator/session_start.rs @@ -173,6 +173,49 @@ impl TurnOrchestrator { // Best-effort: if the repo can't be opened or the view already // exists (resumed session), we log and continue — recording will // still work, it just won't have the parent's history. + if event + .raw_json + .as_ref() + .and_then(|v| v.get("recording_scope")) + .and_then(|v| v.as_str()) + == Some("explicit-files-v1") + { + session.explicit_record_files = true; + } + + // Several OpenCode sessions can share one actual working directory. + // Explicit file scopes separate authorship; a common view gives their + // status/add/record operations one baseline and one directory identity. + if let Some(owner_id) = event + .raw_json + .as_ref() + .and_then(|v| v.get("workspace_session_id")) + .and_then(|v| v.as_str()) + { + if !session.explicit_record_files { + return Err(crate::error::AgentError::Internal( + "shared workspace requires explicit file recording".into(), + )); + } + if owner_id != session_id { + let owner = self.session_store.load(owner_id)?.ok_or_else(|| { + crate::error::AgentError::Internal("workspace owner session is missing".into()) + })?; + if !owner.explicit_record_files { + return Err(crate::error::AgentError::Internal( + "workspace owner does not use explicit file recording".into(), + )); + } + if self.managed_run.is_some() && session.view_name != owner.view_name { + return Err(crate::error::AgentError::Internal( + "workspace view conflicts with managed run".into(), + )); + } + session.view_name = owner.view_name.clone(); + session.parent_view = Some(owner.view_name); + } + } + if session.parent_view.is_none() { match atomic_repository::Repository::open_existing_wait( &self.repo_root, diff --git a/atomic-agent/src/turn/orchestrator/turn.rs b/atomic-agent/src/turn/orchestrator/turn.rs index 63caa872..1a1c075c 100644 --- a/atomic-agent/src/turn/orchestrator/turn.rs +++ b/atomic-agent/src/turn/orchestrator/turn.rs @@ -154,8 +154,11 @@ impl TurnOrchestrator { let mut session = self.load_or_create_session(session_id, &event)?; - let has_changes = self.has_working_copy_changes(); - if !session.is_turn_active() && session.turn_count > 0 && !has_changes { + let has_changes = session.explicit_record_files || self.has_working_copy_changes(); + if !session.is_turn_active() + && session.turn_count > 0 + && (session.explicit_record_files || !has_changes) + { // A retried Stop after successful publication must not create a // second empty checkpoint for the same completed interaction. return Ok(DispatchResult::new(session_id, session.phase)); diff --git a/atomic-agent/src/turn/session.rs b/atomic-agent/src/turn/session.rs index 781c654c..e9ead522 100644 --- a/atomic-agent/src/turn/session.rs +++ b/atomic-agent/src/turn/session.rs @@ -116,6 +116,10 @@ pub struct AgentSession { #[serde(alias = "stack_name")] pub view_name: String, + /// Require an explicit file manifest; never sweep a shared working tree. + #[serde(default)] + pub explicit_record_files: bool, + /// Current lifecycle phase. pub phase: Phase, @@ -244,6 +248,7 @@ impl AgentSession { Self { session_id, view_name, + explicit_record_files: false, phase: Phase::Idle, turn_count: 0, agent_name: agent_name.into(), diff --git a/atomic-cli/src/commands/agent/hooks.rs b/atomic-cli/src/commands/agent/hooks.rs index 83e61589..3a115235 100644 --- a/atomic-cli/src/commands/agent/hooks.rs +++ b/atomic-cli/src/commands/agent/hooks.rs @@ -123,6 +123,26 @@ impl Command for Hooks { )) })?; + if self.agent_name == "opencode" && self.verb == "file-snapshot" { + let value: serde_json::Value = + serde_json::from_slice(&input).map_err(|e| CliError::Internal(anyhow!(e)))?; + let cwd = value + .get("cwd") + .and_then(|v| v.as_str()) + .ok_or_else(|| CliError::Internal(anyhow!("file-snapshot requires cwd")))?; + let extra: Vec = serde_json::from_value( + value + .get("paths") + .cloned() + .unwrap_or_else(|| serde_json::json!([])), + ) + .map_err(|e| CliError::Internal(anyhow!(e)))?; + let snapshot = atomic_agent::record::scope::snapshot(std::path::Path::new(cwd), &extra) + .map_err(|e| CliError::Internal(anyhow!(e)))?; + println!("{}", snapshot); + return Ok(()); + } + if self.should_handoff_codex_lifecycle() { return self.handoff_codex_lifecycle(&input); } diff --git a/atomic-cli/tests/database_owner_integration_test.rs b/atomic-cli/tests/database_owner_integration_test.rs index ff531c90..5eb2acdb 100644 --- a/atomic-cli/tests/database_owner_integration_test.rs +++ b/atomic-cli/tests/database_owner_integration_test.rs @@ -1832,3 +1832,94 @@ fn owner_election_commit_reconnect_and_crash_restart() { ); wait_for_shutdown(&repository); } + +#[test] +fn scoped_concurrent_stops_and_session_end_do_not_record_sibling_files() { + let temp = TempDir::new().unwrap(); + let root = temp.path().join("repo"); + drop(Repository::init(&root).unwrap()); + let hook = |sid: &str, verb: &str, extra: Value| { + let mut value = extra; + value["session_id"] = sid.into(); + let output = run_agent_hook( + &root, + "opencode", + verb, + &serde_json::to_vec(&value).unwrap(), + ); + assert!(output.status.success(), "{output:?}"); + }; + hook( + "workspace-owner", + "session-start", + serde_json::json!({"recording_scope":"explicit-files-v1"}), + ); + for sid in ["child-a", "child-b"] { + hook( + sid, + "session-start", + serde_json::json!({"recording_scope":"explicit-files-v1", "workspace_session_id":"workspace-owner"}), + ); + hook( + sid, + "user-prompt", + serde_json::json!({"prompt":"Write only the assigned file"}), + ); + } + for name in ["child-a.txt", "child-b.txt", "human.txt"] { + std::fs::write(root.join(name), name).unwrap(); + } + let pending:Vec<_> = ["child-a", "child-b"].into_iter().map(|sid|{ + let root=root.clone(); + thread::spawn(move||{ + let path=format!("{sid}.txt"); + let digest=atomic_agent::record::scope::fingerprint(&root,&path).unwrap(); + run_agent_hook(&root,"opencode","stop",&serde_json::to_vec(&serde_json::json!({"session_id":sid,"record_files":{path:digest},"response":"Done"})).unwrap()) + }) + }).collect(); + for handle in pending { + let result = handle.join().unwrap(); + assert!(result.status.success(), "{result:?}"); + } + for sid in ["child-a", "child-b"] { + let state: Value = serde_json::from_slice( + &std::fs::read(root.join(format!(".atomic/sessions/{sid}.json"))).unwrap(), + ) + .unwrap(); + let owner: Value = serde_json::from_slice( + &std::fs::read(root.join(".atomic/sessions/workspace-owner.json")).unwrap(), + ) + .unwrap(); + assert_eq!(state["view_name"], owner["view_name"]); + assert_eq!( + state["files_touched"], + serde_json::json!([format!("{sid}.txt")]) + ); + assert_eq!(state["recorded_change_hashes"].as_array().unwrap().len(), 1); + hook( + sid, + "stop", + serde_json::json!({"record_files":{},"response":"duplicate idle"}), + ); + hook( + sid, + "session-end", + serde_json::json!({"record_files":{},"reason":"deleted"}), + ); + let after: Value = serde_json::from_slice( + &std::fs::read(root.join(format!(".atomic/sessions/{sid}.json"))).unwrap(), + ) + .unwrap(); + assert_eq!( + after["recorded_change_hashes"], + state["recorded_change_hashes"] + ); + assert_eq!(after["turn_count"], state["turn_count"]); + } + assert_eq!( + std::fs::read_to_string(root.join("human.txt")).unwrap(), + "human.txt" + ); + assert!(run_owner(&root, "shutdown").status.success()); + wait_for_shutdown(&root); +} From a8b06cc0d790ff3991da717dba5f49c4381def39 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 16 Sep 2026 17:17:23 -0400 Subject: [PATCH 3/3] fix: confirm scoped file contents and document shared workspace validation Atomic intent ATOM::vince::13; recorded by scoped Stop as NGHDDSQINARM3B2BJOUB2J5ZUPBD22D4MYPBKVBPCO2O3O7FDKCA. Independent follow-up to #190; no PR #189 source included. --- atomic-agent/src/record/scope.rs | 2 +- docs/subagent-file-ownership.md | 93 ++++++++++++++++++++++++++++++++ docs/subagent-session-start.md | 5 ++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 docs/subagent-file-ownership.md diff --git a/atomic-agent/src/record/scope.rs b/atomic-agent/src/record/scope.rs index d2989c7b..1a51611e 100644 --- a/atomic-agent/src/record/scope.rs +++ b/atomic-agent/src/record/scope.rs @@ -85,7 +85,7 @@ pub fn snapshot(root: &Path, extra: &[String]) -> Result = status .entries() diff --git a/docs/subagent-file-ownership.md b/docs/subagent-file-ownership.md new file mode 100644 index 00000000..8e6479d3 --- /dev/null +++ b/docs/subagent-file-ownership.md @@ -0,0 +1,93 @@ +# Shared-workspace file ownership + +Follow-up to the session-start lock and OpenCode session-routing fixes. The +Atomic implementation remains on redb 4.2 and is isolated from PR #190 on +`fix/subagent-file-ownership`. Intent: `ATOM::vince::13` +(`01M2NZT2H8RPJ0RFPAYX8G8QCY`). The matching atomic-opencode change has intent +`ATOM::vince::2` (`01M2NZT228QP7FKNGJM14E4TMM`). + +## Behavior + +Previously each Stop scanned and recorded the whole working directory. A child +could therefore record its sibling's file even with correctly routed hook events. + +The new OpenCode plugin negotiates `explicit-files-v1` through the CLI's +`agent hooks opencode file-snapshot` operation. It compares dirty-file fingerprints +before and after potentially mutating tools (including bash and unknown tools), +then sends only that session's changed files in `record_files` on Stop. +Fingerprints of candidate files cover contents, executable mode, symlink targets +and deletions. Candidate discovery uses Atomic status and therefore inherits +its file-index/mtime detection limits. It is not a full filesystem audit. +Unchanged pre-existing edits are excluded. Changed pre-existing unowned files, +foreign session rewrites and view changes during a tool block automatic recording. + +Potentially mutating tools, session startup and publication use a workspace queue +with a 30-second acquisition limit. A timeout prevents that queued tool from +starting and does not unlock another running operation. Read tools and model +reasoning can overlap; a parent waiting on `task` never holds the queue. Error +and idle callbacks drain unfinished tool snapshots so partial writes remain +attributed and other sessions can proceed. + +Parent and child sessions in the same plugin instance adopt one working view. +Each retains its own session ID, scoped change and provenance. This matches the +single actual filesystem baseline: the parent sees recorded child work without +an implicit cross-view merge. Separate session views remain the default for +other integrations and sessions that do not request shared-workspace recording. + +The Rust recorder validates the manifest and current fingerprints before adding +and recording, filters status and envelope files to the same scope, and persists +the explicit-scope requirement in the session. An empty scope is a provenance-only +checkpoint. A missing/invalid scope is an error, not permission to sweep the tree. +SessionEnd does not sweep unrelated files after an already-completed scoped turn. + +The plugin must be installed with the matching CLI. It refuses an older CLI +instead of falling back to unscoped recording. + +## Validation on macOS, 2026-09-16 + +- 1,342 atomic-agent unit tests passed, including three new scoped-record tests. +- 20 owner integration and 4 ordinary-read integration tests passed. The added + concurrent-Stop test verifies distinct file lists, a common declared workspace + view, duplicate-Stop idempotency and no SessionEnd sweep. +- 13 plugin tests / 47 assertions passed, covering routing plus file snapshots, + unrelated edits, foreign rewrites, deletions, partial failures, view drift, + capability refusal and bounded queue acquisition. +- Workspace Clippy with `-D warnings` passed. +- A real OpenCode 1.18.30 parent and two overlapping atomic subagents used + openrouter/openai/gpt-5-mini. All 21 tools completed. Three separate changes + contained exactly `results/parent.txt`, `results/child-a.txt`, and + `results/child-b.txt`, respectively. All three checkpoints published; frozen + journal call IDs matched their sessions and all provenance hashes verified. + There were no hook errors or lock warnings, and the final working directory + was clean without a manual insert or record after the run. + +The same real scenario also passed after a disposable local Git merge with +Aaron's PR #189 at `9c6fdff35a6a6ce73ea6a8c79eea1a26af136828`: +21 successful tool calls, three correctly scoped changes, three verified +published checkpoints, no hook errors, and a clean working directory. Child +execution intervals overlapped. This merge is test-only and is not included in +the follow-up branch. Evidence: `live-combined/`. + +## Scope and remaining limits + +This is coordinated attribution within one OpenCode instance, not an operating +system sandbox. Other processes/instances or human edits can still change files; +a post-tool fingerprint change is rejected, but snapshots cannot prove who wrote +bytes concurrently inside an uncoordinated tool interval. Independent overlapping +writers should use separate working directories. + +Conflicting shared-file writes are not automatically merged or rolled back. The +plugin blocks publication and leaves the disk state for explicit reconciliation. +In-memory ownership and queued events are not a crash-persistent execution log. +Immediate OpenCode process exit may still interrupt pending callbacks; the live +test used a persistent server and a 15-second drain period. + +An earlier scoped-record experiment retained separate child views. It preserved +each file's authorship, but explicit insertion into the parent left a child file +untracked even after reindexing. That experiment is retained in local evidence; +the shipped shared-workspace mode uses one baseline instead. It does not claim +to repair general cross-view directory merging. + +Raw local evidence: enclosing workspace `.gstack/subagent-ownership-20260916/`, +particularly `live-shared/`, `integration.log`, `shared-integration.log`, +`agent-tests-final.log`, `integration-final.log`, `plugin-tests.log`, and `clippy-final.log`. diff --git a/docs/subagent-session-start.md b/docs/subagent-session-start.md index 85aae0ec..2399f342 100644 --- a/docs/subagent-session-start.md +++ b/docs/subagent-session-start.md @@ -1,5 +1,10 @@ # Subagent session-start contention +Follow-up: the shared-directory file-ownership limitation described below is +addressed by the separate `fix/subagent-file-ownership` branch; see +[file ownership](subagent-file-ownership.md). + + This follow-up is based on PR #190 at `67b74c8881ce4afe8a758beea093bc486a1da3f7`. It keeps redb 4.2 and does not include Aaron's identity-delegation changes. Atomic intent: `ATOM::vince::12`