A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix(pa-daemon): agent messages to a passivated child wake it by its durable id (#3244)
Sebastian Müller5212e4158de9
diff --git a/.changes/agent-message-wakes-passivated-child.md b/.changes/agent-message-wakes-passivated-child.mdnew file mode 100644index 0000000000..d5b7a0f857--- /dev/null+++ b/.changes/agent-message-wakes-passivated-child.md@@ -0,0 +1 @@+- Messaging a subagent whose worker was idle-passivated now wakes it and delivers the message, instead of failing with "Unknown active session".diff --git a/crates/pa-daemon/src/agent_messaging/controller_tests.rs b/crates/pa-daemon/src/agent_messaging/controller_tests.rsindex cad4178b05..9b4e5b8d08 100644--- a/crates/pa-daemon/src/agent_messaging/controller_tests.rs+++ b/crates/pa-daemon/src/agent_messaging/controller_tests.rs@@ -294,7 +294,8 @@ async fn family_resolves_the_parent_by_session_id() {}/// Registry children the roster does not list stay addressable as Child-/// members keyed by their registry identity.+/// members keyed by their durable session id (the wake-resolvable+/// selector), the spawn-time live id kept as an alias.#[tokio::test]async fn family_keeps_off_roster_children_addressable() {let dir = tempfile::TempDir::new().unwrap();@@ -321,9 +322,9 @@ async fn family_keeps_off_roster_children_addressable() {let family = controller.family().await.unwrap();assert_eq!(family.len(), 1, "{family:?}");assert_eq!(family[0].relationship, AgentFamilyRelationship::Child);- assert_eq!(family[0].id, "eee555");+ assert_eq!(family[0].id, "sess-e");assert_eq!(family[0].name.as_deref(), Some("worker-b"));- assert_eq!(family[0].aliases, vec!["sub-kid2", "sess-e"]);+ assert_eq!(family[0].aliases, vec!["sub-kid2", "sess-e", "eee555"]);}/// A refused ticket falls back to the supervisor-routed `send_message`.@@ -367,10 +368,10 @@ async fn self_target_is_refused() {);}-/// A minted ticket delivers straight to the target worker's socket:-/// `peer_auth` with the worker purpose, then `worker_deliver_message`-/// carrying the TS sender identity block, and the receipt maps onto-/// the kernel shape.+/// A minted ticket delivers straight to the target worker's socket,+/// addressed to the ticket's resolved session: `peer_auth` with the+/// worker purpose, then `worker_deliver_message` carrying the TS+/// sender identity block, and the receipt maps onto the kernel shape.#[tokio::test]async fn direct_ticket_delivers_to_the_target_worker_socket() {let dir = tempfile::TempDir::new().unwrap();@@ -470,7 +471,14 @@ async fn direct_ticket_delivers_to_the_target_worker_socket() {let socket = dir.path().join("sup.sock");spawn_fake_supervisor(socket.clone(), json!({ "sessions": [] }), Some(ticket)).await;let controller = controller(socket, own_summary());- let receipt = controller.send_agent_message(input()).await.unwrap();+ // The supervisor resolved the durable selector "sess-b" into the ticket.+ let receipt = controller+ .send_agent_message(AgentMessageSendInput {+ target: "sess-b".to_string(),+ ..input()+ })+ .await+ .unwrap();assert_eq!(receipt.id, "agentmsg_peer");assert_eq!(receipt.delivery_status, AgentMessageDeliveryStatus::Queued);assert_eq!(receipt.target, "bbb222");diff --git a/crates/pa-daemon/src/agent_messaging/message.rs b/crates/pa-daemon/src/agent_messaging/message.rsindex 71561c9574..2c86057774 100644--- a/crates/pa-daemon/src/agent_messaging/message.rs+++ b/crates/pa-daemon/src/agent_messaging/message.rs@@ -198,11 +198,22 @@ impl AgentMessageController for LinkAgentMessageController {.collect(),});}- // Children the roster does not list (a passivating or- // mid-registration child worker) stay addressable: the delivery- // transports resolve or wake them from their persisted identity.+ // Children the roster does not list (a passivated or+ // mid-registration child worker) are addressed by their DURABLE+ // id, like TS family entries (`id: agent.sessionId`): the+ // supervisor's send_message wake resolves it through the spawn+ // ledger and relaunches a passivated child. The spawn-time live id+ // stays an alias selector.for child in children {- child_members.push(child_member(&child, None));+ let mut member = child_member(&child, None);+ let durable = crate::rlm_children::durable_child_selector(+ child.session_id.as_deref(),+ &child.rlm_child_id,+ );+ member+ .aliases+ .push(std::mem::replace(&mut member.id, durable));+ child_members.push(member);}// TS `selectAgentFamily` order: parent, siblings by name, then// children by name.@@ -290,7 +301,6 @@ impl LinkAgentMessageController {};match crate::peer_client::deliver_message_over_peer_transport(&ticket,- &input.target,&input.message,&self.sender_block(),None,diff --git a/crates/pa-daemon/src/peer_client.rs b/crates/pa-daemon/src/peer_client.rsindex 7192b235f2..08db87de4a 100644--- a/crates/pa-daemon/src/peer_client.rs+++ b/crates/pa-daemon/src/peer_client.rs@@ -39,21 +39,15 @@ pub(crate) enum PeerDeliveryOutcome {}/// Burn the ticket's grant on the target worker's socket and deliver one-/// agent message directly.+/// agent message directly, addressed to the session the ticket's grant is+/// bound to (the supervisor's resolution of the selector).pub(crate) async fn deliver_message_over_peer_transport(ticket: &DaemonPeerTransportTicket,- target_active_session_id: &str,message: &str,sender: &Value,delivery_mode: Option<&str>,) -> PeerDeliveryOutcome {- let future = deliver_once(- ticket,- target_active_session_id,- message,- sender,- delivery_mode,- );+ let future = deliver_once(ticket, message, sender, delivery_mode);match tokio::time::timeout(std::time::Duration::from_millis(DELIVERY_TIMEOUT_MS),future,@@ -67,7 +61,6 @@ pub(crate) async fn deliver_message_over_peer_transport(async fn deliver_once(ticket: &DaemonPeerTransportTicket,- target_active_session_id: &str,message: &str,sender: &Value,delivery_mode: Option<&str>,@@ -106,7 +99,7 @@ async fn deliver_once(// Delivery: the command is sent, so every outcome from here is final.let mut payload = json!({"type": "worker_deliver_message",- "targetActiveSessionId": target_active_session_id,+ "targetActiveSessionId": ticket.active_session_id,"message": message,"sender": sender,});diff --git a/crates/pa-daemon/src/rlm_children.rs b/crates/pa-daemon/src/rlm_children.rsindex ea37517cdd..12f50361ed 100644--- a/crates/pa-daemon/src/rlm_children.rs+++ b/crates/pa-daemon/src/rlm_children.rs@@ -150,6 +150,14 @@ pub struct RlmChildIdentity {pub session_name: String,}+/// The selector that reaches a child whether or not its worker is+/// resident: the persisted session id (the session-file stem the+/// supervisor's ledger wake resolves), else the RLM child id. The+/// spawn-time live id stops resolving once the worker passivates.+pub(crate) fn durable_child_selector(session_id: Option<&str>, rlm_child_id: &str) -> String {+ session_id.unwrap_or(rlm_child_id).to_string()+}+/// One tracked child session.#[derive(Debug)]struct ChildRecord {diff --git a/crates/pa-daemon/src/rlm_children/lifecycle.rs b/crates/pa-daemon/src/rlm_children/lifecycle.rsindex 1928df4cdc..bfa5a7992c 100644--- a/crates/pa-daemon/src/rlm_children/lifecycle.rs+++ b/crates/pa-daemon/src/rlm_children/lifecycle.rs@@ -252,10 +252,10 @@ impl SupervisorChildSessionsInner {for record in children.iter() {let record = record.lock().await;if record.active_session_id == active_session_id {- durable = record- .session_id- .clone()- .or_else(|| Some(record.rlm_child_id.clone()));+ durable = Some(crate::rlm_children::durable_child_selector(+ record.session_id.as_deref(),+ &record.rlm_child_id,+ ));break;}}diff --git a/crates/pa-daemon/tests/idle_passivation_e2e.rs b/crates/pa-daemon/tests/idle_passivation_e2e.rsindex 7ec5a1b0d8..b950c22bce 100644--- a/crates/pa-daemon/tests/idle_passivation_e2e.rs+++ b/crates/pa-daemon/tests/idle_passivation_e2e.rs@@ -1,8 +1,8 @@//! The whole-worker idle passivation e2e (TS `idleEvictionMinutes`)://! a settled RLM child's supervisor stop leaves the session file and the-//! parent's roster row intact; a follow-up prompt to the child's live id-//! WAKES a fresh worker over the saved file and answers; the child's-//! delete tombstones without a live worker.+//! parent's roster row intact; the parent's `agent_message.send` to the+//! passivated child WAKES a fresh worker over the saved file and+//! delivers.//!//! The test drives the worker->supervisor passivation request directly//! (the child worker's supervisor-link ask, replayed with the child's own@@ -20,12 +20,15 @@ use std::io::{BufRead, BufReader, Write};use std::os::unix::net::UnixStream;use std::path::{Path, PathBuf};use std::process::{Child, Command, Stdio};-use std::sync::Arc;+use std::sync::{Arc, Mutex};use std::time::{Duration, Instant};use serde_json::{json, Value};+use pa_core::kernel::shared::{HostRequestHandlers, HostRequestPayload};+use pa_core::session_engine::agent_messaging::register_agent_message_host_handlers;use pa_core::session_engine::rlm_host::{RlmSpawnRequest, RlmSubagentHost};+use pa_daemon::agent_messaging::LinkAgentMessageController;use pa_daemon::rlm_children::{ParentIdentity, SupervisorChildSessions};use pa_daemon::supervisor_link::SupervisorLink;@@ -183,11 +186,11 @@ fn write_faux_script(dir: &Path, name: &str, responses: &Value) -> PathBuf {}/// A settled RLM child's whole-worker idle passivation: the stop keeps-/// the parent's roster row (done — the POSITIVE verdict), a follow-up-/// prompt WAKES a fresh worker over the child's session file and-/// answers, and the child's delete tombstones without a live worker.+/// the parent's roster row (done — the POSITIVE verdict), the parent's+/// `agent_message.send` WAKES a fresh worker over the child's session+/// file and delivers.#[tokio::test]-async fn a_settled_child_passivates_stays_listable_and_revives_by_prompt() {+async fn a_settled_child_passivates_stays_listable_and_revives_by_agent_message() {let Some(kernel_python) = kernel_python() else {eprintln!("kernel python unavailable; skipping the passivation e2e");return;@@ -288,17 +291,6 @@ async fn a_settled_child_passivates_stays_listable_and_revives_by_prompt() {// worker's engine), so the boundary bump is simulated here.children.notify_turn_done();- // The child's durable session id (the passive row keeps it; the- // routing id is the live-worker field TS strips at passivation —- // clients address a passivated session by the durable id).- let child_session_id = {- let roster = children.list_subagents().await.expect("child roster");- roster- .first()- .and_then(|row| row.session_id.clone())- .expect("the child's durable session id")- };-// The child settles done with a resident worker.let deadline = Instant::now() + Duration::from_secs(30);let child_active_session_id = loop {@@ -402,43 +394,53 @@ async fn a_settled_child_passivates_stays_listable_and_revives_by_prompt() {row.status);- // THE REVIVAL: a prompt addressed by the child's DURABLE session id- // wakes a fresh worker over the saved file (the route's wake arm- // resolves the saved session and launches). The faux engine's script+ // THE REVIVAL: the parent's real `agent_message.send` to the child,+ // through the same controller and handler the worker's engine wires:+ // the send resolves the passivated child through the family view+ // (keyed by its durable id) and wakes a fresh worker over the saved+ // file (the supervisor's ledger wake). The faux engine's script// is spawn-time config (not session-file state), so the replayed// worker's turn runs the default provider: the response's outcome// depends on the host's credentials and is not asserted — the WAKE- // oracle is the revival prompt's row in the child's session file+ // oracle is the delivered message's row in the child's session file// (only a woken worker writes it); the model-answer revival is the// VM census's leg (the real binary against the offline mock).- let child_file_rows_before = std::fs::read_to_string({- let roster = children- .list_subagents()- .await- .expect("roster for the file");- let row = roster- .iter()- .find(|row| row.active_session_id.as_deref() == Some(child_active_session_id.as_str()))- .expect("the child row");- std::path::Path::new(&row.session_dir).join(format!(- "{}.jsonl",- row.session_id.clone().expect("the child's session id")- ))- })- .map_or(0, |content| content.lines().count());let revive_prompt = "revive: answer again";- client.send_command(- "revive",- &json!({- "type": "prompt_and_wait",- "activeSessionId": child_session_id,+ let parent_token =+ worker_token(&agent_dir, &parent_active_session_id).expect("the parent worker's token");+ let own_summary = json!({+ "activeSessionId": parent_active_session_id,+ "sessionId": parent_session_id,+ "sessionName": "parent",+ "runtimeKind": "top-level",+ });+ let children = Arc::new(children);+ let controller = Arc::new(LinkAgentMessageController::new(+ Arc::clone(&link),+ parent_active_session_id.clone(),+ parent_token,+ Arc::new(Mutex::new(Some(own_summary))),+ Some(Arc::clone(&children)),+ ));+ let mut handlers = HostRequestHandlers::default();+ register_agent_message_host_handlers(Arc::clone(&controller) as Arc<_>, &mut handlers);+ let send = handlers.get("agent_message.send").expect("send handler");+ let revived = send(HostRequestPayload {+ data: json!({"message": revive_prompt,+ "receiver_role": "child",+ "receiver_name": "parked-kid",}),- );- let revived = client.read_response("revive");- // The revival respawned a worker for the child's session (a fresh- // pid serves the replayed file) and the prompt's row landed in the- // child's session file (the delivery half of the revival).+ cell_source_code: None,+ })+ .await+ .expect("the send must wake the passivated child and deliver");+ // The send woke a fresh worker for the child's session (a new pid+ // serves the replayed file); the delivery's receipt arrives before+ // the delivered turn writes its rows, so the wake's proof is the+ // message's row itself landing in the child's session file (the+ // revived worker serves the SAME file — its fresh routing id+ // differs, so only a woken worker writes it).let deadline = Instant::now() + Duration::from_secs(15);let child_file = {let roster = children@@ -455,25 +457,17 @@ async fn a_settled_child_passivates_stays_listable_and_revives_by_prompt() {))};loop {- // The delivery oracle: the woken worker replayed the file and- // the prompt's user row landed in it (the revived worker serves- // the SAME file — its fresh routing id differs, so the file's- // growth is the wake's proof).- let rows_after = std::fs::read_to_string(&child_file).map_or(0, |c| c.lines().count());- if rows_after > child_file_rows_before {+ let grown = std::fs::read_to_string(&child_file).unwrap_or_default();+ if grown.contains(revive_prompt) {break;}assert!(Instant::now() < deadline,- "the revival never delivered the prompt into the child's session file ({child_file:?}, rows {rows_after} <= {child_file_rows_before}, response: {revived})"+ "the send never delivered the message into the child's session file ({child_file:?}, tail: {:?}, receipt: {revived:?})",+ grown.lines().rev().take(3).collect::<Vec<_>>().join(" | "));tokio::time::sleep(Duration::from_millis(100)).await;}- let grown = std::fs::read_to_string(&child_file).expect("the child session file");- assert!(- grown.contains(revive_prompt),- "the revival prompt never landed in the child's session file ({child_file:?}, response: {revived})"- );}/// An idle unowned ROOT passivates through the same worker-driven ask