diff --git a/src/daemon.rs b/src/daemon.rs index 1ad5cbf..7f32444 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -8391,6 +8391,38 @@ impl ActorDaemonCoordinator { }; let stash_sha = Self::resolve_stash_sha_for_event(cmd, &operation, stash_ref.as_deref())?; + if stash_sha.is_none() + && matches!( + operation, + StashOperation::Apply + | StashOperation::Pop + | StashOperation::Branch + | StashOperation::Drop + ) + { + // A failed command with no target (a `pop` on an empty + // stack) changed nothing. A successful one with no + // target loses only this stash's attribution. + if cmd.exit_code != 0 { + tracing::debug!( + component = "daemon", + root_sid = %cmd.root_sid, + ?operation, + ?stash_ref, + exit_code = cmd.exit_code, + "skipping failed stash event without a target oid" + ); + } else { + tracing::warn!( + component = "daemon", + root_sid = %cmd.root_sid, + ?operation, + ?stash_ref, + "skipping stash event: target oid is unresolved" + ); + } + continue; + } let head_sha = match operation { StashOperation::Create => Self::resolve_stash_create_head_for_event( cmd, @@ -8412,19 +8444,6 @@ impl ActorDaemonCoordinator { } else { Vec::new() }; - if matches!( - operation, - StashOperation::Apply - | StashOperation::Pop - | StashOperation::Branch - | StashOperation::Drop - ) && stash_sha.is_none() - { - return Err(AutterError::Generic(format!( - "stash {:?} missing resolvable target oid sid={} ref={:?}", - operation, cmd.root_sid, stash_ref - ))); - } if matches!( operation, StashOperation::Create diff --git a/tests/integration/stash_attribution.rs b/tests/integration/stash_attribution.rs index 92a1565..768828b 100644 --- a/tests/integration/stash_attribution.rs +++ b/tests/integration/stash_attribution.rs @@ -349,6 +349,36 @@ fn test_stash_pop_empty_repo() { assert!(result.is_err(), "Should fail when no stash exists"); } +#[test] +fn test_stash_pop_empty_stack_does_not_fail_daemon_side_effects() { + let repo = TestRepo::new(); + + let mut readme = repo.filename("README.md"); + readme.set_contents(vec!["# Test Repo".to_string()]); + repo.stage_all_and_commit("initial commit") + .expect("commit should succeed"); + readme.assert_committed_lines(vec!["# Test Repo".human()]); + + // Pop with no stash ever created. + assert!(repo.git(&["stash", "pop"]).is_err()); + // Panics if the daemon recorded a side-effect error for the failed pop. + repo.sync_daemon(); + + let mut example = repo.filename("example.txt"); + example.set_contents(vec!["AI content".ai()]); + repo.git(&["stash"]).expect("stash should succeed"); + repo.git(&["stash", "pop"]) + .expect("stash pop should succeed"); + + // Pop again after the stack is empty. + assert!(repo.git(&["stash", "pop"]).is_err()); + repo.sync_daemon(); + + repo.stage_all_and_commit("apply stashed changes") + .expect("commit should succeed"); + example.assert_committed_lines(vec!["AI content".ai()]); +} + #[test] fn test_stash_mixed_human_and_ai() { let repo = TestRepo::new();