Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

bound the supervisor's roster reads so one wedged worker cannot stall list and spawn (#3382)

Sebastian Müller67cbae0a12c7
diff --git a/.changes/supervisor-roster-scan-bound.md b/.changes/supervisor-roster-scan-bound.md
new file mode 100644
index 0000000000..b91765f841
--- /dev/null
+++ b/.changes/supervisor-roster-scan-bound.md
@@ -0,0 +1 @@
+- Listing agents (CLI list, resume preflight, the agent roster) and spawning a named session no longer stall behind one unresponsive session worker: resident summaries are read concurrently under a short per-worker bound that degrades an unreachable worker to its recovering row, and the saved-session and scheduled-jobs roster scans moved off the supervisor's runtime threads.
diff --git a/crates/pa-daemon/src/registry.rs b/crates/pa-daemon/src/registry.rs
index ddc5a3c209..c67c15dbad 100644
--- a/crates/pa-daemon/src/registry.rs
+++ b/crates/pa-daemon/src/registry.rs
@@ -371,6 +371,15 @@ pub(crate) struct SessionRegistry {
adoption_locks: Mutex<HashMap<String, Arc<AdoptionLock>>>,
}
+pub(crate) fn canonical_session_file_string(session_file: &str) -> String {
+ std::path::Path::new(session_file)
+ .canonicalize()
+ .map_or_else(
+ |_| session_file.to_string(),
+ |path| path.to_string_lossy().to_string(),
+ )
+}
+
impl SessionRegistry {
pub(crate) fn new() -> Self {
SessionRegistry {
@@ -424,22 +433,61 @@ impl SessionRegistry {
.next()
}
+ pub(crate) async fn session_files(&self) -> Vec<String> {
+ let mut files = Vec::new();
+ for resident in self.list().await {
+ // The boot-reconciliation quarantine: an unreconciled persisted
+ // identity never serves a by-file reuse.
+ if resident.identity_quarantined() {
+ continue;
+ }
+ let owned = resident
+ .descriptor
+ .lock()
+ .await
+ .session_file
+ .clone()
+ .unwrap_or_default();
+ if !owned.is_empty() {
+ files.push(owned);
+ }
+ }
+ files
+ }
+
+ /// Check current descriptor paths without touching the filesystem. The scan
+ /// canonicalizes existing owners in the blocking pool; this catches workers
+ /// registered while that scan was in flight.
+ pub(crate) async fn owns_session_file_path(
+ &self,
+ session_file: &str,
+ canonical_file: &str,
+ ) -> bool {
+ for resident in self.list().await {
+ if resident.identity_quarantined() {
+ continue;
+ }
+ let descriptor = resident.descriptor.lock().await;
+ if descriptor
+ .session_file
+ .as_deref()
+ .is_some_and(|owned| owned == session_file || owned == canonical_file)
+ {
+ return true;
+ }
+ }
+ false
+ }
+
/// Every resident registered for one session file, order unspecified:
/// a replacement window can briefly hold two; the caller classifies.
pub(crate) async fn list_by_session_file(
&self,
session_file: &str,
) -> Vec<Arc<ResidentWorker>> {
- let target = std::path::Path::new(session_file)
- .canonicalize()
- .map_or_else(
- |_| session_file.to_string(),
- |path| path.to_string_lossy().to_string(),
- );
+ let target = canonical_session_file_string(session_file);
let mut matches = Vec::new();
for resident in self.list().await {
- // The boot-reconciliation quarantine: an unreconciled persisted
- // identity never serves a by-file reuse.
if resident.identity_quarantined() {
continue;
}
@@ -450,11 +498,7 @@ impl SessionRegistry {
.session_file
.clone()
.unwrap_or_default();
- let owned = std::path::Path::new(&owned)
- .canonicalize()
- .map(|path| path.to_string_lossy().to_string())
- .unwrap_or(owned);
- if owned == target {
+ if canonical_session_file_string(&owned) == target {
matches.push(resident);
}
}
diff --git a/crates/pa-daemon/src/scheduling_catalog.rs b/crates/pa-daemon/src/scheduling_catalog.rs
index dbd0479dc9..cf5b100b13 100644
--- a/crates/pa-daemon/src/scheduling_catalog.rs
+++ b/crates/pa-daemon/src/scheduling_catalog.rs
@@ -18,7 +18,7 @@ use crate::backpressure::RouteAdmission;
use crate::protocol::{
command_type_name, response_failure, response_line, response_success, DaemonResponse,
};
-use crate::registry::ResidentWorker;
+use crate::registry::{canonical_session_file_string, ResidentWorker};
use crate::scheduled_jobs::session_artifact_dir;
use crate::session_store::read_session_info;
use crate::supervisor::{client_command_payload, Supervisor};
@@ -30,6 +30,7 @@ const CATALOG_FORWARD_TIMEOUT_MS: u64 = 5000;
pub(crate) struct PassiveJob {
pub(crate) job: AgentCronJob,
pub(crate) info: crate::session_store::SessionInfo,
+ canonical_session_file: String,
}
/// The supervisor-side passive snapshot the catalog READ paths serve: the artifacts-tree
@@ -71,8 +72,26 @@ impl Supervisor {
/// Jobs stored under the session-artifacts tree whose session file exists, is
/// active, and has no live worker: the supervisor only merges what no worker can list.
async fn collect_passive_scheduled_jobs(&self, include_inactive: bool) -> Vec<PassiveJob> {
+ let resident_files = self.registry.session_files().await;
+ let agent_dir = self.options.agent_dir.clone();
+ let scan = tokio::task::spawn_blocking(move || {
+ let resident_files: HashSet<String> = resident_files
+ .iter()
+ .map(|file| canonical_session_file_string(file))
+ .collect();
+ let candidates = Self::scan_passive_candidates(&agent_dir, include_inactive);
+ (resident_files, candidates)
+ });
+ let (resident_files, candidates) = scan.await.unwrap();
+ self.classify_passive_candidates(candidates, &resident_files)
+ .await
+ }
+
+ /// Filesystem work stays in the blocking pool; ownership is classified
+ /// afterward, when workers registered during the scan are visible.
+ fn scan_passive_candidates(agent_dir: &Path, include_inactive: bool) -> Vec<PassiveJob> {
let mut out = Vec::new();
- for job in crate::update_roster::scan_scheduled_jobs(&self.options.agent_dir) {
+ for job in crate::update_roster::scan_scheduled_jobs(agent_dir) {
if !include_inactive && !matches!(job.status, JobStatus::Active | JobStatus::Paused) {
continue;
}
@@ -80,21 +99,41 @@ impl Supervisor {
if !session_file.is_file() {
continue;
}
- if self
- .registry
- .find_by_session_file(&job.session_file)
- .await
- .is_some()
- {
- continue;
- }
let Some(info) = read_session_info(session_file) else {
continue;
};
if info.state.as_deref() != Some("active") {
continue;
}
- out.push(PassiveJob { job, info });
+ let canonical_file = canonical_session_file_string(&job.session_file);
+ out.push(PassiveJob {
+ job,
+ info,
+ canonical_session_file: canonical_file,
+ });
+ }
+ out
+ }
+
+ async fn classify_passive_candidates(
+ &self,
+ candidates: Vec<PassiveJob>,
+ resident_files: &HashSet<String>,
+ ) -> Vec<PassiveJob> {
+ let mut out = Vec::new();
+ for passive in candidates {
+ if resident_files.contains(&passive.canonical_session_file)
+ || self
+ .registry
+ .owns_session_file_path(
+ &passive.job.session_file,
+ &passive.canonical_session_file,
+ )
+ .await
+ {
+ continue;
+ }
+ out.push(passive);
}
out
}
@@ -564,26 +603,32 @@ impl Supervisor {
passive.job.id == *job_id && passive.job.active_session_id == *active_session_id
});
if let Some(passive) = passive {
- let store = Self::passive_job_store(&passive.info);
- // A passive row that cannot be managed falls through to the live-worker route.
- if let Ok(Some(heartbeat)) = store.manage_heartbeat(
- active_session_id,
- job_id,
- heartbeat_manage_action(action),
- crate::util::now_ms(),
- ) {
- self.broadcast_heartbeats_changed();
- return (
- vec![response_line(&response_success(
- Some(command_id),
- type_name,
- Some(json!({
- "heartbeat": serde_json::to_value(&heartbeat)
- .unwrap_or(Value::Null),
- })),
- ))],
- false,
- );
+ let owned_elsewhere = self
+ .registry
+ .owns_session_file_path(&passive.job.session_file, &passive.canonical_session_file)
+ .await;
+ if !owned_elsewhere {
+ let store = Self::passive_job_store(&passive.info);
+ // A passive row that cannot be managed falls through to the live-worker route.
+ if let Ok(Some(heartbeat)) = store.manage_heartbeat(
+ active_session_id,
+ job_id,
+ heartbeat_manage_action(action),
+ crate::util::now_ms(),
+ ) {
+ self.broadcast_heartbeats_changed();
+ return (
+ vec![response_line(&response_success(
+ Some(command_id),
+ type_name,
+ Some(json!({
+ "heartbeat": serde_json::to_value(&heartbeat)
+ .unwrap_or(Value::Null),
+ })),
+ ))],
+ false,
+ );
+ }
}
}
// No passive job managed: the live worker owns the heartbeat.
@@ -821,6 +866,165 @@ impl Supervisor {
mod tests {
use super::*;
+ /// A worker registering while the blocking artifact scan is in flight
+ /// owns its job before the passive path can write to the shared store.
+ #[tokio::test(start_paused = true)]
+ async fn a_worker_registered_during_the_scan_gets_its_heartbeat_manage() {
+ use crate::registry::{ResidentWorker, WorkerReply};
+ use crate::supervisor::subscribers::ClientSubscriptions;
+ use pa_types::daemon::DaemonWorkerDescriptor;
+
+ let dir = tempfile::tempdir().expect("temp dir");
+ let agent_dir = dir.path().join("agent");
+ let sessions_dir = agent_dir.join("sessions");
+ std::fs::create_dir_all(&sessions_dir).expect("sessions dir");
+ let session_file = sessions_dir.join("mid-scan.jsonl");
+ std::fs::write(
+ &session_file,
+ [
+ json!({
+ "type": "session", "version": 3, "id": "mid-scan",
+ "timestamp": "2026-10-04T00:00:00.000Z", "cwd": "/c",
+ }),
+ json!({
+ "type": "session_state", "id": "mid-scan",
+ "timestamp": "2026-10-04T00:00:01.000Z",
+ "state": { "status": "active" },
+ }),
+ ]
+ .iter()
+ .map(std::string::ToString::to_string)
+ .collect::<Vec<_>>()
+ .join("\n")
+ + "\n",
+ )
+ .expect("session file");
+ let artifacts = agent_dir.join("session-artifacts").join("mid-scan");
+ std::fs::create_dir_all(&artifacts).expect("artifacts partition");
+ let job_file = artifacts.join("scheduled-jobs.json");
+ let artifact = json!({
+ "jobs": [{
+ "id": "hb-mid", "status": "active",
+ "activeSessionId": "mid-scan", "sessionId": "mid-scan",
+ "sessionFile": session_file.display().to_string(),
+ "cwd": dir.path().display().to_string(), "prompt": "heartbeat",
+ "schedule": { "kind": "interval", "expression": "", "intervalMs": 60000 },
+ "createdAt": "2026-10-04T00:00:02.000Z",
+ "updatedAt": "2026-10-04T00:00:02.000Z",
+ "nextRunAt": "2026-10-04T00:01:02.000Z",
+ }],
+ })
+ .to_string();
+ std::fs::write(&job_file, &artifact).expect("scheduled jobs");
+ let supervisor = Arc::new(
+ Supervisor::new(crate::supervisor::SupervisorOptions {
+ socket_path: dir.path().join("daemon.sock"),
+ agent_dir: agent_dir.clone(),
+ })
+ .expect("supervisor"),
+ );
+
+ let old_files: HashSet<String> = supervisor
+ .registry
+ .session_files()
+ .await
+ .iter()
+ .map(|file| canonical_session_file_string(file))
+ .collect();
+ let (started_tx, started_rx) = tokio::sync::oneshot::channel();
+ let (continue_tx, continue_rx) = std::sync::mpsc::channel();
+ let scan = tokio::task::spawn_blocking(move || {
+ started_tx.send(()).expect("scan start");
+ continue_rx.recv().expect("resume scan");
+ Supervisor::scan_passive_candidates(&agent_dir, /*include_inactive*/ false)
+ });
+ started_rx.await.expect("scan started");
+
+ let mut descriptor = json!({
+ "version": 2, "workerId": "mid-scan", "pid": 0,
+ "socketPath": "/tmp/none.sock",
+ "recoveryJournalPath": "/tmp/none.jsonl",
+ "supervisorSocketPath": "/tmp/none.sock",
+ "authenticationToken": "test", "rootActiveSessionId": "mid-scan",
+ "createdAt": "2026-10-04T00:00:00Z",
+ "updatedAt": "2026-10-04T00:00:00Z",
+ "lifecycle": "ready", "createCommand": {}, "consecutiveFailures": 0,
+ });
+ descriptor["sessionFile"] = json!(session_file.to_string_lossy());
+ let descriptor: DaemonWorkerDescriptor =
+ serde_json::from_value(descriptor).expect("descriptor");
+ let worker = ResidentWorker::new(
+ "mid-scan".to_string(),
+ descriptor,
+ dir.path().join("worker.json"),
+ );
+ let (cmd_tx, mut cmd_rx) = tokio::sync::mpsc::channel(1);
+ *worker.cmd_tx.lock().await = Some(cmd_tx);
+ worker.note_connection_live();
+ worker.note_session_ready();
+ supervisor.registry.insert(Arc::clone(&worker)).await;
+ continue_tx.send(()).expect("resume scan");
+
+ let candidates = scan.await.expect("blocking scan");
+ assert_eq!(candidates.len(), 1);
+ assert!(
+ supervisor
+ .classify_passive_candidates(candidates, &old_files)
+ .await
+ .is_empty(),
+ "the pre-registration snapshot must not make a live job passive"
+ );
+
+ let (seen_tx, seen_rx) = tokio::sync::oneshot::channel();
+ let responder = Arc::clone(&worker);
+ tokio::spawn(async move {
+ let request = cmd_rx.recv().await.expect("forwarded heartbeat_manage");
+ seen_tx
+ .send(request.command_type.clone())
+ .expect("forward observed");
+ let reply = responder.pending.lock().await.remove(&request.request_id);
+ assert!(
+ reply
+ .expect("pending forward")
+ .send(WorkerReply::Typed(response_success(
+ Some(&request.request_id),
+ &request.command_type,
+ None,
+ )))
+ .is_ok(),
+ "send worker reply"
+ );
+ });
+ let command: DaemonCommand = serde_json::from_value(json!({
+ "type": "heartbeat_manage", "activeSessionId": "mid-scan",
+ "jobId": "hb-mid", "action": "pause",
+ }))
+ .expect("heartbeat_manage command");
+ let (queue, _receiver) = tokio::sync::mpsc::channel(1);
+ let attached = ClientSubscriptions::new("client".to_string(), queue);
+ let (response, _) = supervisor
+ .handle_heartbeat_manage_catalog(
+ &command,
+ "client",
+ &attached,
+ "manage-1",
+ "heartbeat_manage",
+ )
+ .await;
+ assert_eq!(
+ tokio::time::timeout(std::time::Duration::from_secs(1), seen_rx)
+ .await
+ .expect("worker did not receive the forward")
+ .expect("worker saw forward"),
+ "heartbeat_manage"
+ );
+ assert_eq!(response[0]["success"], true);
+ assert_eq!(
+ std::fs::read_to_string(job_file).expect("read artifact"),
+ artifact
+ );
+ }
+
/// The passive-catalog warmup (the input-latency lane): the boot's
/// warm scan stores the snapshot with NO read anywhere, and the first
/// catalog read serves that stored snapshot instead of scanning the
diff --git a/crates/pa-daemon/src/supervisor.rs b/crates/pa-daemon/src/supervisor.rs
index 4acfaeef7c..661737a530 100644
--- a/crates/pa-daemon/src/supervisor.rs
+++ b/crates/pa-daemon/src/supervisor.rs
@@ -50,7 +50,9 @@ pub(crate) use clients::client_command_payload;
// The routing consts and refusal string keep their crate::supervisor::* paths stable
// (external callers: supervisor_parent_death, create_reuse, prompt_admission, update_restore).
-pub(crate) use routing::{client_route_timeout, ROUTE_TIMEOUT_MS, WORKER_NOT_CONNECTED};
+pub(crate) use routing::{
+ client_route_timeout, ROUTE_TIMEOUT_MS, SUMMARY_TIMEOUT_MS, WORKER_NOT_CONNECTED,
+};
// Called only by the supervision sibling module and in-file tests; lib-target unused.
#[allow(unused_imports)]
diff --git a/crates/pa-daemon/src/supervisor/routing.rs b/crates/pa-daemon/src/supervisor/routing.rs
index 4fd3efb1f6..5c9cf7174e 100644
--- a/crates/pa-daemon/src/supervisor/routing.rs
+++ b/crates/pa-daemon/src/supervisor/routing.rs
@@ -16,6 +16,7 @@ pub(super) enum WakeRoute {
}
pub(crate) const ROUTE_TIMEOUT_MS: u64 = 30_000;
+pub(crate) const SUMMARY_TIMEOUT_MS: u64 = 5_000;
/// The route failure for a worker whose command channel is gone: the request did not
/// leave the supervisor, so the route may retry it without risking a duplicate landing.
pub(crate) const WORKER_NOT_CONNECTED: &str = "Session worker is not connected";
diff --git a/crates/pa-daemon/src/supervisor/sessions.rs b/crates/pa-daemon/src/supervisor/sessions.rs
index 1c2886ddee..2773a8ceed 100644
--- a/crates/pa-daemon/src/supervisor/sessions.rs
+++ b/crates/pa-daemon/src/supervisor/sessions.rs
@@ -2,10 +2,11 @@
//! handlers, the stale-id binding and rebind seam, and the saved-row
//! builders.
use super::{
- anyhow, bail, json, list_sessions, mpsc, name_unavailable_error, paths, reservation_key,
- response_failure, response_line, response_success, subscribers, Arc, DaemonCommand,
- DaemonResponse, DaemonSessionLifecycle, NameScope, Outbound, Path, PathBuf, ResidentWorker,
- Result, RouteAdmission, Supervisor, Value, ROUTE_TIMEOUT_MS,
+ anyhow, bail, join_all, json, list_sessions, mpsc, name_unavailable_error, paths,
+ reservation_key, response_failure, response_line, response_success, subscribers, Arc,
+ DaemonCommand, DaemonResponse, DaemonSessionLifecycle, NameScope, Outbound, Path, PathBuf,
+ ResidentWorker, Result, RouteAdmission, Supervisor, Value, ROUTE_TIMEOUT_MS,
+ SUMMARY_TIMEOUT_MS,
};
/// One spawn-name reservation held across a fresh-launch create (TS #2396): the only
@@ -406,53 +407,21 @@ impl Supervisor {
let summaries: Vec<Value> = if let Some(true) = all {
// TS `buildSessionList` order: saved rows (resident ones replaced in place by
// their live summary), then passive children, then resident-only rows.
- let mut infos = list_sessions(&dir);
- if let Some(cwd) = cwd {
- infos.retain(|info| info.cwd == cwd);
- }
- let residents = self.registry.list().await;
- let mut resident_by_file: Vec<ResidentRoot> = Vec::new();
- for resident in &residents {
+ let mut residents = Vec::new();
+ let mut resident_roots = Vec::new();
+ for resident in self.registry.list().await {
let descriptor = resident.descriptor.lock().await;
if let Some(session_file) = &descriptor.session_file {
- resident_by_file.push(ResidentRoot {
- session_file: crate::lease::canonical_session_path(Path::new(session_file)),
- resident: Arc::clone(resident),
- // Resident roots carry their active session id so passive children of a
- // resident parent report parentActiveSessionId.
- active_session_id: Some(descriptor.root_active_session_id.clone()),
- });
+ residents.push(Arc::clone(&resident));
+ // Resident roots carry their active session id so passive children of a
+ // resident parent report parentActiveSessionId.
+ resident_roots.push((
+ session_file.clone(),
+ descriptor.root_active_session_id.clone(),
+ ));
}
}
- let mut summaries = Vec::new();
- let mut roots: Vec<crate::rlm_roster::RosterWalkRoot> = Vec::new();
- for info in &infos {
- roots.push(crate::rlm_roster::RosterWalkRoot {
- session_file: info.path.clone(),
- active_session_id: None,
- });
- let canonical = crate::lease::canonical_session_path(&info.path);
- let resident = resident_by_file
- .iter()
- .position(|root| root.session_file == canonical)
- .map(|at| resident_by_file.swap_remove(at));
- match resident {
- Some(root) => {
- roots.last_mut().expect("saved root").active_session_id =
- root.active_session_id;
- summaries.push(self.worker_summary(&root.resident).await);
- }
- None => summaries.push(saved_session_summary(info)),
- }
- }
- let mut resident_only = Vec::new();
- for root in resident_by_file {
- roots.push(crate::rlm_roster::RosterWalkRoot {
- session_file: root.session_file,
- active_session_id: root.active_session_id,
- });
- resident_only.push(self.worker_summary(&root.resident).await);
- }
+ let summaries_by_resident = self.worker_summaries(&residents).await;
let ledger = match self.rlm_spawn_ledger_for(session_dir.as_deref()).await {
Ok(ledger) => ledger,
Err(error) => {
@@ -464,28 +433,85 @@ impl Supervisor {
);
}
};
- match crate::rlm_roster::walk_passive_rlm_children(&ledger, &roots) {
- Ok(children) => {
- for child in &children {
- summaries.push(crate::rlm_roster::passive_child_summary(child));
+ let scan = tokio::task::spawn_blocking(move || -> Result<Vec<Value>> {
+ let mut infos = list_sessions(&dir);
+ if let Some(cwd) = &cwd {
+ infos.retain(|info| info.cwd == *cwd);
+ }
+ let mut resident_by_file: Vec<ResidentRoot> = Vec::new();
+ for ((session_file, active_session_id), summary) in
+ resident_roots.into_iter().zip(summaries_by_resident)
+ {
+ resident_by_file.push(ResidentRoot {
+ session_file: crate::lease::canonical_session_path(Path::new(
+ &session_file,
+ )),
+ summary,
+ active_session_id: Some(active_session_id),
+ });
+ }
+ let mut summaries = Vec::new();
+ let mut roots: Vec<crate::rlm_roster::RosterWalkRoot> = Vec::new();
+ for info in &infos {
+ roots.push(crate::rlm_roster::RosterWalkRoot {
+ session_file: info.path.clone(),
+ active_session_id: None,
+ });
+ let canonical = crate::lease::canonical_session_path(&info.path);
+ let resident = resident_by_file
+ .iter()
+ .position(|root| root.session_file == canonical)
+ .map(|at| resident_by_file.swap_remove(at));
+ match resident {
+ Some(root) => {
+ roots.last_mut().expect("saved root").active_session_id =
+ root.active_session_id;
+ summaries.push(root.summary);
+ }
+ None => summaries.push(saved_session_summary(info)),
}
}
- Err(error) => {
- let message = format!("Could not walk passive RLM children: {error:#}");
+ let mut resident_only = Vec::new();
+ for root in resident_by_file {
+ roots.push(crate::rlm_roster::RosterWalkRoot {
+ session_file: root.session_file,
+ active_session_id: root.active_session_id,
+ });
+ resident_only.push(root.summary);
+ }
+ match crate::rlm_roster::walk_passive_rlm_children(&ledger, &roots) {
+ Ok(children) => {
+ for child in &children {
+ summaries.push(crate::rlm_roster::passive_child_summary(child));
+ }
+ }
+ Err(error) => {
+ return Err(anyhow!("Could not walk passive RLM children: {error:#}"));
+ }
+ }
+ summaries.append(&mut resident_only);
+ Ok(summaries)
+ });
+ match scan.await {
+ Ok(Ok(summaries)) => summaries,
+ Ok(Err(message)) => {
+ let message = message.to_string();
self.log_line(&message);
return response_failure(Some(&command_id), &type_name, &message, None);
}
+ Err(error) => {
+ return response_failure(
+ Some(&command_id),
+ &type_name,
+ &format!("the saved-session scan failed: {error}"),
+ None,
+ );
+ }
}
-
- summaries.append(&mut resident_only);
- summaries
} else {
// Live residents of this supervisor.
- let mut summaries = Vec::new();
- for resident in self.registry.list().await {
- summaries.push(self.worker_summary(&resident).await);
- }
- summaries
+ let residents = self.registry.list().await;
+ self.worker_summaries(&residents).await
};
response_success(
Some(&command_id),
@@ -502,7 +528,7 @@ impl Supervisor {
resident,
"get_state",
json!({}),
- ROUTE_TIMEOUT_MS,
+ SUMMARY_TIMEOUT_MS,
RouteAdmission::SupervisorInternal,
)
.await;
@@ -514,6 +540,15 @@ impl Supervisor {
}
}
+ async fn worker_summaries(self: &Arc<Self>, residents: &[Arc<ResidentWorker>]) -> Vec<Value> {
+ join_all(
+ residents
+ .iter()
+ .map(|resident| self.worker_summary(resident)),
+ )
+ .await
+ }
+
pub(crate) async fn handle_create(
self: &Arc<Self>,
command: &DaemonCommand,
@@ -608,28 +643,15 @@ impl Supervisor {
if name.trim().is_empty() {
return Err(anyhow!("Session name cannot be empty"));
}
- for resident in self.registry.list().await {
- let response = self
- .route_command_typed(
- &resident,
- "get_state",
- json!({}),
- ROUTE_TIMEOUT_MS,
- RouteAdmission::SupervisorInternal,
- )
- .await;
- if let Ok(response) = response {
- if let Some(data) = &response.data {
- let session_name = data
- .get("sessionName")
- .and_then(Value::as_str)
- .unwrap_or_default();
- if session_name == name {
- return Err(anyhow!(
- "Agent name \"{name}\" is unavailable: an agent of that name already exists at depth 0 under this parent"
- ));
- }
- }
+ for summary in self.worker_summaries(&self.registry.list().await).await {
+ let session_name = summary
+ .get("sessionName")
+ .and_then(Value::as_str)
+ .unwrap_or_default();
+ if session_name == name {
+ return Err(anyhow!(
+ "Agent name \"{name}\" is unavailable: an agent of that name already exists at depth 0 under this parent"
+ ));
}
}
Ok(())
@@ -692,7 +714,7 @@ impl Supervisor {
/// One resident's roster identity for the `list --all` merge.
struct ResidentRoot {
session_file: PathBuf,
- resident: Arc<ResidentWorker>,
+ summary: Value,
active_session_id: Option<String>,
}
@@ -811,3 +833,145 @@ pub(super) fn saved_session_row(info: &crate::session_store::SessionInfo) -> Val
}
row
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::backpressure::WORKER_INFLIGHT_CAPACITY;
+ use crate::registry::{WorkerReply, WorkerRequest};
+ use crate::supervisor::SupervisorOptions;
+ use pa_types::daemon::DaemonWorkerDescriptor;
+
+ fn resident(worker_id: &str, session_file: Option<&Path>) -> Arc<ResidentWorker> {
+ let mut descriptor = json!({
+ "version": 2,
+ "workerId": worker_id,
+ "pid": 0,
+ "socketPath": "/tmp/none.sock",
+ "recoveryJournalPath": "/tmp/none.jsonl",
+ "supervisorSocketPath": "/tmp/none.sock",
+ "authenticationToken": "test",
+ "rootActiveSessionId": "none",
+ "createdAt": "2026-09-26T00:00:00Z",
+ "updatedAt": "2026-09-26T00:00:00Z",
+ "lifecycle": "ready",
+ "createCommand": {},
+ "consecutiveFailures": 0,
+ });
+ if let Some(session_file) = session_file {
+ descriptor["sessionFile"] = json!(session_file.to_string_lossy());
+ }
+ let descriptor: DaemonWorkerDescriptor =
+ serde_json::from_value(descriptor).expect("descriptor");
+ ResidentWorker::new(
+ worker_id.to_string(),
+ descriptor,
+ PathBuf::from("/tmp/none.descriptor.json"),
+ )
+ }
+
+ async fn wedged_worker(
+ resident: &Arc<ResidentWorker>,
+ ) -> tokio::sync::mpsc::Receiver<WorkerRequest> {
+ let (cmd_tx, cmd_rx) =
+ tokio::sync::mpsc::channel::<WorkerRequest>(WORKER_INFLIGHT_CAPACITY);
+ *resident.cmd_tx.lock().await = Some(cmd_tx);
+ cmd_rx
+ }
+
+ async fn responsive_worker(resident: &Arc<ResidentWorker>, state: Value) {
+ let (cmd_tx, mut cmd_rx) =
+ tokio::sync::mpsc::channel::<WorkerRequest>(WORKER_INFLIGHT_CAPACITY);
+ *resident.cmd_tx.lock().await = Some(cmd_tx);
+ let responder = Arc::clone(resident);
+ tokio::spawn(async move {
+ while let Some(request) = cmd_rx.recv().await {
+ let reply = responder.pending.lock().await.remove(&request.request_id);
+ if let Some(reply) = reply {
+ let _ = reply.send(WorkerReply::Typed(response_success(
+ Some(&request.request_id),
+ &request.command_type,
+ Some(state.clone()),
+ )));
+ }
+ }
+ });
+ }
+
+ fn supervisor(dir: &Path) -> Supervisor {
+ Supervisor::new(SupervisorOptions {
+ socket_path: dir.join("daemon.sock"),
+ agent_dir: dir.join("agent"),
+ })
+ .expect("supervisor")
+ }
+
+ #[tokio::test(start_paused = true)]
+ async fn a_wedged_resident_does_not_hold_the_all_true_list_past_the_client_deadline() {
+ let dir = tempfile::TempDir::new().expect("temp dir");
+ let supervisor = Arc::new(supervisor(dir.path()));
+ let wedged = resident("w-wedged", Some(&dir.path().join("w-wedged.jsonl")));
+ let _wedged_rx = wedged_worker(&wedged).await;
+ supervisor.registry.insert(Arc::clone(&wedged)).await;
+ let healthy = resident("w-healthy", Some(&dir.path().join("w-healthy.jsonl")));
+ let state =
+ json!({ "id": "w-healthy", "lifecycle": "live", "sessionName": "healthy-name" });
+ responsive_worker(&healthy, state).await;
+ supervisor.registry.insert(Arc::clone(&healthy)).await;
+
+ let start = tokio::time::Instant::now();
+ let response = supervisor
+ .handle_list("l1".to_string(), "list".to_string(), Some(true), None, None)
+ .await;
+ let elapsed = start.elapsed();
+
+ assert!(response.success);
+ let rows = response
+ .data
+ .expect("sessions data")
+ .get("sessions")
+ .and_then(Value::as_array)
+ .cloned()
+ .expect("session rows");
+ let wedged_row = rows
+ .iter()
+ .find(|row| row.get("id").and_then(Value::as_str) == Some("w-wedged"))
+ .expect("wedged row");
+ assert_eq!(
+ wedged_row.get("lifecycle").and_then(Value::as_str),
+ Some("recovering")
+ );
+ let healthy_row = rows
+ .iter()
+ .find(|row| row.get("id").and_then(Value::as_str) == Some("w-healthy"))
+ .expect("healthy row");
+ assert_eq!(
+ healthy_row.get("sessionName").and_then(Value::as_str),
+ Some("healthy-name")
+ );
+ assert!(
+ elapsed < std::time::Duration::from_millis(ROUTE_TIMEOUT_MS),
+ "one wedged resident held the list for {elapsed:?}"
+ );
+ }
+
+ #[tokio::test(start_paused = true)]
+ async fn a_named_create_name_check_does_not_wait_out_the_client_deadline_behind_a_wedged_resident(
+ ) {
+ let dir = tempfile::TempDir::new().expect("temp dir");
+ let supervisor = Arc::new(supervisor(dir.path()));
+ let wedged = resident("w-wedged", None);
+ let _wedged_rx = wedged_worker(&wedged).await;
+ supervisor.registry.insert(Arc::clone(&wedged)).await;
+
+ let start = tokio::time::Instant::now();
+ let verdict = supervisor.assert_session_name_available("fresh-name").await;
+ let elapsed = start.elapsed();
+
+ assert!(verdict.is_ok(), "an unreachable resident is skipped");
+ assert!(
+ elapsed < std::time::Duration::from_millis(ROUTE_TIMEOUT_MS),
+ "the name check waited {elapsed:?} behind one wedged resident"
+ );
+ }
+}