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
62 changes: 62 additions & 0 deletions desktop/ox-desk-core/src/store/claude/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,68 @@ mod tests {
assert!(!store.thread_busy(&child));
}

#[test]
fn a_failed_subagent_child_raises_no_failure_alert_but_its_parent_does() {
use crate::host_protocol::{Change, NotificationKind, Scope};
let (mut s, a, _) = fixture();
let scope = Scope {
project: s.profile.project.clone(),
route: s.profile.route.clone(),
};
let mut decoder = wire::Decoder::default();
let mut apply = |store: &mut Store, frame: Value| {
for event in decoder.decode_value(frame).unwrap() {
update(store, &a, 2, Data::Stock(pipe::Event::Frame(event))).unwrap();
}
};
let mut tool = None;
let mut notification = None;
let mut result = None;
for line in include_str!("../../../tests/fixtures/claude-agent.jsonl").lines() {
let mut frame: Value = serde_json::from_str(line).unwrap();
frame["session_id"] = json!(A);
if frame["type"] == "system" && frame["subtype"] == "init" {
frame["cwd"] = json!("/fixture");
}
if frame["subtype"] == "task_started" {
tool = frame["tool_use_id"].as_str().map(str::to_owned);
}
match frame["type"].as_str() {
Some("result") => result = Some(frame),
Some("system") if frame["subtype"] == "task_notification" => {
notification = Some(frame);
}
_ => apply(&mut s, frame),
}
}
let child = child_key(&a, tool.as_deref().unwrap());
// Capture the state before either turn finishes.
let mut state = s.host_snapshot(&scope);
let mut notification = notification.unwrap();
notification["status"] = json!("failed");
notification["summary"] = json!("the subagent failed");
apply(&mut s, notification);
let mut result = result.unwrap();
result["is_error"] = json!(true);
apply(&mut s, result);

let failed: Vec<_> = s
.host_project(&scope, &s.changed, &mut state)
.into_iter()
.filter_map(|change| match change {
Change::Notification {
thread,
event: NotificationKind::Failed,
..
} => Some(thread),
_ => None,
})
.collect();
assert_eq!(failed.len(), 1);
assert_eq!(failed[0], a);
assert_ne!(failed[0], child);
}

#[test]
fn ending_a_session_drops_its_process_and_keeps_it_resumable() {
let (mut s, a, b) = fixture();
Expand Down
144 changes: 134 additions & 10 deletions desktop/ox-desk-core/src/store/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,32 @@ impl Store {
if previous.detail["last_finished"] != detail["last_finished"]
&& !detail["last_finished"].is_null()
{
let event = if t.error().is_some() {
NotificationKind::Failed
} else {
NotificationKind::Finished
};
out.push(self.note(
id,
event,
event == NotificationKind::Finished && t.finished_goal(),
));
// The turn's own recorded result decides the alert. `error` is a
// sticky banner that an unrelated request or an earlier turn can
// still hold when this turn completed or was stopped.
let failed = t
.last_result
.as_ref()
.filter(|result| Some(result.turn.as_str()) == t.last_finished())
.is_some_and(|result| result.outcome == SessionOutcome::Failed);
// A child thread's failure is delivered to the parent agent as a
// tool result; the parent's own turn is what the operator hears
// about, so the child raises no failure alert of its own.
let reports_to_parent = self
.catalog_row(id)
.is_some_and(|row| row.parent_thread_id.is_some());
if !(failed && reports_to_parent) {
let event = if failed {
NotificationKind::Failed
} else {
NotificationKind::Finished
};
out.push(self.note(
id,
event,
event == NotificationKind::Finished && t.finished_goal(),
));
}
}
if detail != previous.detail {
previous.detail = detail.clone();
Expand Down Expand Up @@ -719,6 +735,114 @@ mod session_change_tests {
assert_eq!(ids(session_changes(&[], &first)), ["all:2"]);
}

#[test]
fn a_completed_turn_is_finished_even_when_a_stale_error_is_still_set() {
use crate::{codex::frame::Frame, links::codex, runtime::Route, store::Profile};
let scope = Scope {
project: "/test".into(),
route: Route::CODEX_SUBSCRIPTION,
};
let link =
codex::Link::from_command(std::process::Command::new("/bin/cat"), |_| {}).unwrap();
let mut store = Store::open(
Profile {
project: scope.project.clone(),
route: scope.route.clone(),
provider: "openai".into(),
},
link,
)
.unwrap();
let t = store.transcripts.entry("thread".into()).or_default();
t.verified = true;
t.ready = true;
let mut state = store.host_snapshot(&scope);
let mut notices = |events: Vec<Value>| -> (Vec<NotificationKind>, bool) {
for event in events {
let frame = Frame::decode(&serde_json::to_vec(&event).unwrap(), |_| None).unwrap();
store.apply(codex::LinkEvent::Frame(frame));
}
let events = store
.host_project(&scope, &store.changed, &mut state)
.into_iter()
.filter_map(|change| match change {
Change::Notification { event, .. } => Some(event),
_ => None,
})
.collect::<Vec<_>>();
let errored = store
.transcript("thread")
.is_some_and(|t| t.error.is_some());
(events, errored)
};
let complete = |turn: &str, status: &str| json!({"method":"turn/completed","params":{"threadId":"thread","turn":{"id":turn,"status":status}}});
use NotificationKind::{Failed, Finished};
let (events, errored) = notices(vec![complete("broken", "failed")]);
assert_eq!(events, [Failed]);
assert!(errored);
// The failed turn leaves its error banner in place. The next turn
// completed, which the recorded result reports as a success.
let (events, errored) = notices(vec![complete("recovered", "completed")]);
assert_eq!(events, [Finished]);
assert!(errored);
// A turn the user stopped is not a failure either, with the same stale
// error still set.
let (events, errored) = notices(vec![complete("stopped", "interrupted")]);
assert_eq!(events, [Finished]);
assert!(errored);
}

#[test]
fn a_subagent_child_that_fails_raises_no_failure_alert() {
use crate::{links::codex, runtime::Route, store::Profile};
let scope = Scope {
project: "/test".into(),
route: Route::CODEX_SUBSCRIPTION,
};
let link =
codex::Link::from_command(std::process::Command::new("/bin/cat"), |_| {}).unwrap();
let mut store = Store::open(
Profile {
project: scope.project.clone(),
route: scope.route.clone(),
provider: "openai".into(),
},
link,
)
.unwrap();
for (id, parent) in [("parent", None), ("child", Some("parent"))] {
store.catalog.insert(
serde_json::from_value(json!({
"id":id,"cwd":scope.project,"modelProvider":"openai","name":id,
"parentThreadId":parent,
}))
.unwrap(),
);
store.transcripts.entry(id.into()).or_default();
}
// Take the initial state before the turns finish, so the projection has
// an unfinished previous value to compare against.
let mut state = store.host_snapshot(&scope);
for id in ["parent", "child"] {
let transcript = store.transcripts.get_mut(id).unwrap();
transcript.record_result("turn", true, false);
transcript.last_finished = Some("turn".into());
transcript.error = Some("the turn failed".into());
}
let mut changed = Changed::default();
changed.transcript("parent");
changed.transcript("child");
let changes = store.host_project(&scope, &changed, &mut state);
let notified: Vec<_> = changes
.iter()
.filter_map(|change| match change {
Change::Notification { thread, event, .. } => Some((thread.as_str(), *event)),
_ => None,
})
.collect();
assert_eq!(notified, [("parent", NotificationKind::Failed)]);
}

#[test]
fn only_a_confirmed_goal_on_the_matching_successful_turn_changes_the_alert() {
use crate::{codex::frame::Frame, links::codex, runtime::Route, store::Profile};
Expand Down
13 changes: 13 additions & 0 deletions desktop/ox-host/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,19 @@ impl Engine {
row,
)
})?;
// A worker that reports its result to a parent agent does not alert the
// operator about its own failed turn: the failure reaches the agent as
// a tool result and the agent decides what happens next.
changes.retain(|change| {
!matches!(
change,
Change::Notification {
thread,
event: wire::NotificationKind::Failed,
..
} if self.workers.reports_to_parent(scope, thread)
)
});
crate::yolo::merge(
&self.asking,
self.mode_requests
Expand Down
38 changes: 38 additions & 0 deletions desktop/ox-host/src/workers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ impl Registry {
pub fn get_mut(&mut self, scope: &Scope, id: &str) -> Option<&mut Entry> {
self.sessions.get_mut(&(scope.clone(), id.to_owned()))
}
/// Whether this session's turn results are delivered to a parent agent.
/// Such a session's failure reaches the agent as a tool result, so it does
/// not raise a native failure alert of its own.
pub fn reports_to_parent(&self, scope: &Scope, id: &str) -> bool {
self.get(scope, id).is_some_and(|entry| {
let origin = &entry.origin;
origin.relation == Relation::Worker
&& !origin.by_operator
&& (origin.linked.is_some()
|| (origin.parent.scope.is_some() && origin.parent.session.is_some()))
})
}
/// A stamp is written once. A second stamp for the same session is refused.
pub fn stamp(&mut self, scope: &Scope, id: &str, origin: Origin) -> bool {
let key = (scope.clone(), id.to_owned());
Expand Down Expand Up @@ -776,6 +788,32 @@ mod tests {
r.map(|r| (r.level, r.access))
}

#[test]
fn only_a_worker_that_reports_to_its_parent_hides_its_own_failure_alert() {
let scope = scope(Route::CODEX_SUBSCRIPTION);
let mut registry = Registry::default();
let base = worker(Level::Read, Runtime::Codex);

let mut child = base.clone();
child.job_id = 7;
registry.stamp(&scope, "child", child);
assert!(registry.reports_to_parent(&scope, "child"));

let mut operator = base.clone();
operator.job_id = 8;
operator.by_operator = true;
registry.stamp(&scope, "operator", operator);

let mut detached = base;
detached.job_id = 9;
detached.parent.session = None;
registry.stamp(&scope, "detached", detached);

assert!(!registry.reports_to_parent(&scope, "operator"));
assert!(!registry.reports_to_parent(&scope, "detached"));
assert!(!registry.reports_to_parent(&scope, "unknown"));
}

#[test]
fn read_plan_exits_follow_the_operators_confirmed_worker_mode() {
let origin = worker(Level::Read, Runtime::Claude);
Expand Down
Loading