A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix idle parent child spawn prompt timing
Sebastian Müller815e64b51df8
diff --git a/.changes/idle-spawn-prompt.md b/.changes/idle-spawn-prompt.mdnew file mode 100644index 0000000000..95c635d5d0--- /dev/null+++ b/.changes/idle-spawn-prompt.md@@ -0,0 +1 @@+- A subagent spawned while its parent has no turn running (for example a factory run resumed between turns) now gets its task prompt at once instead of up to 60 s later. A parent turn aborted by a manual compaction or tree move no longer leaves later spawns waiting for that bound either.diff --git a/crates/pa-daemon/src/agent_engine/session_engine_impl.rs b/crates/pa-daemon/src/agent_engine/session_engine_impl.rsindex 50e0a30b78..56759926d6 100644--- a/crates/pa-daemon/src/agent_engine/session_engine_impl.rs+++ b/crates/pa-daemon/src/agent_engine/session_engine_impl.rs@@ -947,6 +947,12 @@ impl SessionEngine for AgentSessionEngine {}}+ fn on_turn_start(&self) {+ if let Some(children) = &self.children {+ children.notify_turn_started();+ }+ }+/// The worker's turn completed: release child prompt tasks waiting on the boundary.fn on_turn_done(&self) {*selfdiff --git a/crates/pa-daemon/src/engine.rs b/crates/pa-daemon/src/engine.rsindex 7451f58f2d..a5f86e33f5 100644--- a/crates/pa-daemon/src/engine.rs+++ b/crates/pa-daemon/src/engine.rs@@ -193,6 +193,9 @@ pub trait SessionEngine: Send + Sync {fn model_context_window(&self) -> Option<u64> {None}+ /// The worker's turn loop started: engines hosting RLM children hold+ /// the task prompts of children spawned from here until `on_turn_done`.+ fn on_turn_start(&self) {}/// The worker's turn loop completed: engines hosting RLM children/// release prompt tasks spawned mid-turn, so the parent's continuation/// request always reaches the provider before a child's first turn.diff --git a/crates/pa-daemon/src/rlm_children.rs b/crates/pa-daemon/src/rlm_children.rsindex 02d9cf72cb..40ac12bc10 100644--- a/crates/pa-daemon/src/rlm_children.rs+++ b/crates/pa-daemon/src/rlm_children.rs@@ -364,6 +364,13 @@ enum RetirementIntent {Close,}+/// The parent worker's turn state as the detached child prompt tasks read it.+#[derive(Debug, Clone, Copy, Default)]+struct ParentTurn {+ completed: u64,+ running: bool,+}+#[derive(Clone)]struct RetiredChild {record: Arc<Mutex<ChildRecord>>,@@ -395,9 +402,9 @@ struct SupervisorChildSessionsInner {/// Delete-receipt tombstones: a deleted child's identity stays behind/// the registry so `collect` answers a just-deleted selector.deleted_children: std::sync::Mutex<std::collections::HashMap<String, DeletedChild>>,- /// Bumped once per completed parent turn: mid-turn prompt tasks wait for the next bump (the- /// continuation is in flight before the child's first model turn).- turn_done: tokio::sync::watch::Sender<u64>,+ /// The parent worker's turn boundary: prompt tasks spawned mid-turn wait for that turn to+ /// complete (the continuation is in flight before the child's first model turn).+ parent_turn: tokio::sync::watch::Sender<ParentTurn>,/// The parent engine's child-settle hook (goal continuation resume).settle_hook: std::sync::Mutex<Option<Arc<dyn Fn() + Send + Sync>>>,/// Whether any tracked child run is unsettled (the `any_running`@@ -464,7 +471,7 @@ impl SupervisorChildSessions {retired_usage: std::sync::Mutex::new(Vec::new()),pending_spawn_names: std::sync::Mutex::new(std::collections::HashSet::new()),deleted_children: std::sync::Mutex::new(std::collections::HashMap::new()),- turn_done: tokio::sync::watch::Sender::new(0),+ parent_turn: tokio::sync::watch::Sender::new(ParentTurn::default()),settle_hook: std::sync::Mutex::new(None),running: tokio::sync::watch::Sender::new(false),settle_notify: tokio::sync::Notify::new(),@@ -498,10 +505,21 @@ impl SupervisorChildSessions {.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(sink);}+ /// The worker started a parent turn: spawns admitted from here on hold+ /// their task prompt until the turn completes.+ pub fn notify_turn_started(&self) {+ self.inner+ .parent_turn+ .send_modify(|turn| turn.running = true);+ }+/// The worker saw the parent's turn end: release prompt tasks waiting/// on the boundary (called once per `EngineEvent::Done`).pub fn notify_turn_done(&self) {- self.inner.turn_done.send_modify(|value| *value += 1);+ self.inner.parent_turn.send_modify(|turn| {+ turn.completed += 1;+ turn.running = false;+ });}/// Register the child-settle hook: fired once per settled child run@@ -1039,10 +1057,10 @@ impl SupervisorChildSessionsInner {/// strictly greater than the one captured at spawn admission). Bounded:/// a turn that never settles releases the child anyway.pub async fn wait_turn_done(&self, generation: u64) {- let mut receiver = self.turn_done.subscribe();+ let mut receiver = self.parent_turn.subscribe();let _ = tokio::time::timeout(std::time::Duration::from_secs(TURN_DONE_WAIT_SECS),- receiver.wait_for(|value| *value > generation),+ receiver.wait_for(|turn| turn.completed > generation),).await;}diff --git a/crates/pa-daemon/src/rlm_children/host.rs b/crates/pa-daemon/src/rlm_children/host.rsindex e868016246..79c9f6cce6 100644--- a/crates/pa-daemon/src/rlm_children/host.rs+++ b/crates/pa-daemon/src/rlm_children/host.rs@@ -239,12 +239,20 @@ impl RlmSubagentHost for SupervisorChildSessions {let child_active_session_id = created.active_session_id.clone();let child_session_file = created.session_file.clone();let child_log_id = child_id.clone();- // Capture the current turn boundary before detaching: spawn- // admission happens mid-turn, so the parent's continuation- // request reaches the provider first (see `wait_turn_done`).- let turn_generation = *this.turn_done.subscribe().borrow();+ // Capture the spawning turn before detaching: a spawn admitted+ // mid-turn prompts after that turn completes, so the parent's+ // continuation request reaches the provider first (see+ // `wait_turn_done`). A spawn admitted while the parent is idle+ // (a background kernel task, e.g. a factory run) has no turn+ // to wait for.+ let spawning_turn = {+ let turn = *this.parent_turn.borrow();+ turn.running.then_some(turn.completed)+ };tokio::spawn(async move {- watcher_this.wait_turn_done(turn_generation).await;+ if let Some(generation) = spawning_turn {+ watcher_this.wait_turn_done(generation).await;+ }// The parent closed before the prompt admitted: the// child is closed with it, so the detached task prompt// never fires.diff --git a/crates/pa-daemon/src/rlm_children/watch_tests.rs b/crates/pa-daemon/src/rlm_children/watch_tests.rsindex 78fd103784..a350b41bbd 100644--- a/crates/pa-daemon/src/rlm_children/watch_tests.rs+++ b/crates/pa-daemon/src/rlm_children/watch_tests.rs@@ -797,6 +797,63 @@ async fn a_settled_child_without_a_reply_delivers_the_terminal_notice() {assert!(extra.is_err(), "no second notice may arrive");}+/// A spawn admitted while the parent is idle (a background kernel task: a+/// factory run resumed or advanced between turns) has no turn to wait+/// for: the task prompt goes out at once and the run settles.+#[tokio::test]+async fn a_child_spawned_while_the_parent_is_idle_runs_without_a_turn_boundary() {+ let (follow_up_tx, mut follow_up_rx) = mpsc::unbounded_channel();+ let (sessions, _kill_rx) =+ sessions_with_fake_supervisor(follow_up_tx, 0, FakeKill::Success, FakeChild::Healthy).await;+ spawn_child(&sessions).await;++ let notice = tokio::time::timeout(Duration::from_secs(10), follow_up_rx.recv())+ .await+ .expect("an idle parent's child must not wait for a parent turn boundary")+ .expect("the follow_up channel stays open");+ assert_eq!(+ notice["customMessage"]["customType"],+ "rlm_child_terminal_notice"+ );+}++/// A spawn admitted mid-turn holds its task prompt until that parent turn+/// completes, so the parent's continuation request reaches the provider+/// before the child's first turn.+#[tokio::test]+async fn a_child_spawned_mid_turn_prompts_after_the_parent_turn_completes() {+ let (follow_up_tx, _follow_up_rx) = mpsc::unbounded_channel();+ let gate = Arc::new(FakePeerGate {+ armed: AtomicBool::new(true),+ ..FakePeerGate::default()+ });+ let (sessions, _kill_rx) = sessions_with_fake_child_subagents(+ follow_up_tx,+ 0,+ FakeKill::Success,+ FakeChild::Healthy,+ Arc::new(FakeChildSubagents {+ prompt_gate: Some(Arc::clone(&gate)),+ ..FakeChildSubagents::default()+ }),+ )+ .await;+ sessions.notify_turn_started();+ spawn_child(&sessions).await;+ assert!(+ tokio::time::timeout(Duration::from_millis(300), gate.copied.notified())+ .await+ .is_err(),+ "the task prompt must wait for the spawning turn"+ );++ sessions.notify_turn_done();+ tokio::time::timeout(Duration::from_secs(10), gate.copied.notified())+ .await+ .expect("the turn boundary releases the task prompt");+ gate.release.notify_one();+}+/// A `collect` that lands while the task prompt is still in flight reads/// the child's pre-prompt idle worker: the child is still running, never a/// `done` settle with no answer (the factory settled its verifier beforediff --git a/crates/pa-daemon/src/worker/turn.rs b/crates/pa-daemon/src/worker/turn.rsindex db9ec5dcd0..adf3629f84 100644--- a/crates/pa-daemon/src/worker/turn.rs+++ b/crates/pa-daemon/src/worker/turn.rs@@ -321,6 +321,7 @@ impl TurnRunner {self.emit_turn_event(json!({ "type": "turn_start" }));// The pane reporter's run boundary (TS `agent_start`): working.self.herdr.lock().unwrap().run_started();+ engine.on_turn_start();let prompt_index = {let core = self.core.lock().unwrap();@@ -491,6 +492,11 @@ impl TurnRunner {if agent_end && !more_queued {engine.clear_progress_note();}+ // The turn boundary releases RLM child prompt tasks waiting on+ // it, also when the abort gate below swallows the frame.+ if matches!(event, EngineEvent::Done(_) | EngineEvent::DoneAborted) {+ engine.on_turn_done();+ }// Durable model cleanup is independent of visual abort-row// suppression and emits no client-facing frame.if core.abort_requested && !matches!(&event, EngineEvent::ModelChange(_)) {@@ -683,9 +689,6 @@ impl TurnRunner {}let done_result = match &event {EngineEvent::Done(result) => {- // The turn boundary releases RLM child prompt- // tasks waiting on it.- engine.on_turn_done();pa_core::session_engine::compaction_trace::trace("turn.done_emitted",&serde_json::json!({@@ -699,10 +702,7 @@ impl TurnRunner {}// The aborted settle carries its classification// structurally, not through the error text.- EngineEvent::DoneAborted => {- engine.on_turn_done();- Some(TurnSettle::Aborted)- }+ EngineEvent::DoneAborted => Some(TurnSettle::Aborted),_ => None,};// One event may map to several wire frames (a custom rowdiff --git a/crates/pa-daemon/src/worker/turn_stream_tests.rs b/crates/pa-daemon/src/worker/turn_stream_tests.rsindex 572fca1dd8..03bea316dd 100644--- a/crates/pa-daemon/src/worker/turn_stream_tests.rs+++ b/crates/pa-daemon/src/worker/turn_stream_tests.rs@@ -127,6 +127,7 @@ fn positions_of(events: &[Value], frame_type: &str) -> Vec<usize> {struct GateProbeEngine {frames: Vec<EngineEvent>,boundaries: Mutex<Vec<Option<String>>>,+ turns_done: std::sync::atomic::AtomicUsize,}impl SessionEngine for GateProbeEngine {@@ -134,6 +135,11 @@ impl SessionEngine for GateProbeEngine {self.boundaries.lock().unwrap().push(leaf_id);}+ fn on_turn_done(&self) {+ self.turns_done+ .fetch_add(1, std::sync::atomic::Ordering::SeqCst);+ }+fn run_prompt(&self,_prompt_index: usize,@@ -198,7 +204,7 @@ async fn gate_sighting_events() -> Vec<Value> {let engine: Arc<dyn SessionEngine> = Arc::new(GateProbeEngine {frames,- boundaries: Mutex::new(Vec::new()),+ ..GateProbeEngine::default()});let runner = burst_runner(Arc::clone(&engine));{@@ -267,6 +273,49 @@ async fn done_aborted_arms_the_fallback_silence() {);}+/// A turn aborted under row suppression (a manual compaction or tree move)+/// still reaches the engine's turn boundary at its swallowed `Done`, so+/// child prompts held for that turn are released.+#[tokio::test]+async fn a_suppressed_abort_still_ends_the_engine_turn() {+ for done in [EngineEvent::Done(Ok(())), EngineEvent::DoneAborted] {+ let engine = Arc::new(GateProbeEngine {+ frames: vec![done],+ ..GateProbeEngine::default()+ });+ let dynamic: Arc<dyn SessionEngine> = engine.clone();+ let runner = burst_runner(Arc::clone(&dynamic));+ {+ let mut core = runner.core.lock().unwrap();+ core.abort_requested = true;+ core.suppress_aborted_row = true;+ }+ runner+ .run_turn(+ dynamic,+ vec![QueuedItem {+ priority: QueuePriority::Human,+ preview: None,+ message: "aborted".to_string(),+ custom_message: None,+ agent_message: None,+ queue_key: None,+ admission_id: None,+ images: Vec::new(),+ done: None,+ queue_visible: true,+ policy: TurnPolicy::Queued,+ forced_batch: false,+ }],+ )+ .await;+ assert_eq!(+ engine.turns_done.load(std::sync::atomic::Ordering::SeqCst),+ 1+ );+ }+}+/// #4: the turn's tool executions count onto the session's `toolUseCount`/// (TS bumps the child's counter per `tool_execution_start`), the value the/// worker's roster deltas carry.@@ -398,7 +447,7 @@ async fn model_transitions_reach_the_worker_owned_session_file() {}let engine: Arc<dyn SessionEngine> = Arc::new(GateProbeEngine {frames,- boundaries: Mutex::new(Vec::new()),+ ..GateProbeEngine::default()});let runner = burst_runner(Arc::clone(&engine));let mut store = crate::session_store::SessionFile::create("/fixture", None, 0);@@ -468,7 +517,7 @@ async fn prompt_restore_boundary_is_supplied_only_for_file_backed_stores() {for file_backed in [false, true] {let engine = Arc::new(GateProbeEngine {frames: vec![EngineEvent::Done(Ok(()))],- boundaries: Mutex::new(Vec::new()),+ ..GateProbeEngine::default()});let dynamic: Arc<dyn SessionEngine> = engine.clone();let runner = burst_runner(Arc::clone(&dynamic));