Handle a SessionEnd event. Transitions the session to Ended, saves it, and creates an attestation covering all changes recorded during the session. The working copy stays on the session's agent view so the user lands where the work happened and can review it before inserting it into a shared view. The attestation is a graph-level audit node — it captures agent identity, timing, and which change
(
&mut self,
event: TurnEvent,
)
| 22 | /// data are left at zero (they're not available from the hook) and |
| 23 | /// can be enriched later via `atomic agent attest --enrich`. |
| 24 | pub(super) async fn handle_session_end( |
| 25 | &mut self, |
| 26 | event: TurnEvent, |
| 27 | ) -> AgentResult<DispatchResult> { |
| 28 | let session_id = &event.session_id; |
| 29 | |
| 30 | // Refusal is terminal and precedes watcher cancellation, view alignment, |
| 31 | // status, flush-recording, provenance, and attestation. Repeated |
| 32 | // session-end hooks return the same durable recovery object. |
| 33 | if let Some(result) = self.persist_boundary_incomplete(session_id)? { |
| 34 | return Ok(result); |
| 35 | } |
| 36 | |
| 37 | let mut session = match self.session_store.load(session_id)? { |
| 38 | Some(s) => s, |
| 39 | None => { |
| 40 | // Session not found — nothing to end |
| 41 | log::info!("SessionEnd for unknown session {} — ignoring", session_id); |
| 42 | return Ok(DispatchResult::new(session_id, Phase::Ended)); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | let had_active_turn = session.is_turn_active(); |
| 47 | let journal_turn = if had_active_turn { |
| 48 | session.turn_count.saturating_add(1) |
| 49 | } else { |
| 50 | session.turn_count.max(1) |
| 51 | }; |
| 52 | if had_active_turn { |
| 53 | self.commit_hook_event(&event, journal_turn)?; |
| 54 | } |
| 55 | |
| 56 | // If a turn is still active, cancel the watcher |
| 57 | if had_active_turn { |
| 58 | if let Err(e) = self.watcher.cancel_turn().await { |
| 59 | log::warn!( |
| 60 | "Failed to cancel turn watcher for session {}: {}", |
| 61 | session_id, |
| 62 | e |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Flush any unrecorded turn BEFORE finalizing. Headless agents such as |
| 68 | // Cursor's CLI fire sessionStart/postToolUse/sessionEnd but no per-turn |
| 69 | // `stop` (TurnEnd), so the turn's file changes are still uncommitted at |
| 70 | // session end. Record them now. Idempotent: agents that already |
| 71 | // recorded each turn on `stop` leave a clean working copy here, so |
| 72 | // `record_turn` returns `EmptyTurn` and this is a no-op for them. |
| 73 | let mut flush_incomplete: Option<crate::turn::session::IncompleteSession> = None; |
| 74 | // Scoped sessions (dev sync): when hook file ownership narrows the |
| 75 | // turn and no turn is in flight, alignment and flush are skipped — |
| 76 | // there is nothing owned to record. |
| 77 | if !session.explicit_record_files || had_active_turn { |
| 78 | { |
| 79 | // Ensure a non-sandbox working copy still desires the session's |
| 80 | // agent view before recording. session-start aligns it, but that can |
| 81 | // drift back to the parent view by session end (observed with |
no test coverage detected