Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions atomic-agent/src/turn/orchestrator/session_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ impl TurnOrchestrator {
/// 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 changes were recorded. Cost and token
/// data are left at zero (they're not available from the hook) and
Expand Down Expand Up @@ -46,9 +50,7 @@ impl TurnOrchestrator {
// Flush any unrecorded turn BEFORE finalizing. Headless agents such as
// Cursor's CLI fire sessionStart/postToolUse/sessionEnd but no per-turn
// `stop` (TurnEnd), so the turn's file changes are still uncommitted at
// session end. Record them now — crucially *before* switching back to
// the parent view below, which restores the working copy and would
// otherwise discard the agent's work. Idempotent: agents that already
// 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.
{
Expand Down Expand Up @@ -157,30 +159,12 @@ impl TurnOrchestrator {
self.create_session_attestation(&session);
}

// Switch back to the user's original view. session-start switched
// to the agent view so that all file writes happened there. Now
// that the session is over, restore the user's view.
if let Some(ref parent) = session.parent_view {
match atomic_repository::Repository::open_existing(&self.repo_root) {
Ok(mut repo) => {
if let Err(e) = repo.switch_view(parent) {
log::warn!(
"Could not switch back to user view '{}': {} (non-fatal)",
parent,
e,
);
} else {
log::info!("Restored user view '{}'", parent);
}
}
Err(e) => {
log::warn!(
"Could not open repository to restore user view: {} (non-fatal)",
e,
);
}
}
}
// Deliberately do NOT switch back to `session.parent_view`: the
// working copy stays on the session's agent view so the user lands
// where the work happened and can review it (`atomic log`/`diff`)
// before deciding to insert it into a shared view. Switching back
// forced users to hunt through `atomic view list` for the view
// holding their agent's changes.

Ok(DispatchResult::new(session_id, session.phase))
}
Expand Down
5 changes: 3 additions & 2 deletions atomic-agent/src/turn/orchestrator/session_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ impl TurnOrchestrator {
// Switch to the agent view so that all file writes during
// the session (tool calls, npm install, builds, etc.) happen
// while current_view points to the agent view. This ensures
// status/add/record see the right view. session-end will
// switch back to the user's view.
// status/add/record see the right view. The working copy
// stays on the agent view after session-end so the user
// lands where the agent's work happened.
if let Err(e) = repo.align_to_view(&session.view_name) {
log::warn!(
"Could not align to agent view '{}': {} (non-fatal)",
Expand Down
9 changes: 5 additions & 4 deletions atomic-agent/src/turn/orchestrator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ async fn test_session_start_adopts_declared_managed_view() {
assert_eq!(
session.parent_view.as_deref(),
Some("dev"),
"parent view is remembered so session-end can restore it"
"parent view is remembered as the fork/insert baseline"
);
let stamp = session
.managed_run
Expand All @@ -987,15 +987,16 @@ async fn test_session_start_adopts_declared_managed_view() {
);
assert_eq!(result.view.as_deref(), Some("sherpa-run-view"));

// Session end restores the parent view.
// Session end leaves the working copy on the agent's view: the user
// lands where the work happened and can review/insert from there.
orch.dispatch(session_end_event("sess-managed"))
.await
.unwrap();
let repo = Repository::open_existing(dir.path()).unwrap();
assert_eq!(
repo.current_view(),
"dev",
"session-end restores the user's view"
"sherpa-run-view",
"session-end leaves the working copy on the agent's view"
);
}

Expand Down
3 changes: 2 additions & 1 deletion atomic-repository/vault/skills/atomic-vault.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ workflow orchestrator own all of that:
view — depending on how you were launched).
- **Turn end** records automatically with full AI provenance (model,
session, decision graph).
- **Session end** restores the original view.
- **Session end** leaves the working copy on the session's view, where
the recorded work lives — review it there.
- **Merging into a shared view** (`atomic insert`) is a human/orchestrator
decision made at review time — never run it yourself.

Expand Down
2 changes: 1 addition & 1 deletion docs/adding-agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ invocation is a fresh, stateless CLI process keyed by `session_id`.
| `PreToolUse` | `before-tool`, `pre-tool` | before a tool runs | Bookkeeping only (no output yet) |
| `PostToolUse` | `after-tool`, `post-tool` | after a tool runs | Appends a classified node (`Exploration` / `Commitment` / `Verification` / `Execution` / `Error`) plus inferred causal edges to the session's **decision graph** |
| `TurnEnd` | `stop`, `after-agent`, `turn-end` | agent finishes a turn | **Records a change** on the draft view with full AI provenance embedded |
| `SessionEnd` | `session-end` | agent session closes | Flushes any unrecorded work, writes the **AI attestation**, restores the parent view |
| `SessionEnd` | `session-end` | agent session closes | Flushes any unrecorded work, writes the **AI attestation**; the working copy stays on the session's view so the user lands where the work happened |

**The golden rule of responsibility:** the agent side (plugin or hook command)
only does two things — fire the event at the right moment, and attach a JSON
Expand Down
Loading