Skip to content

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

Rust

fix(pa-daemon): ACP and TUI noSession creates are client-owned (#3237)

Sebastian Müllera0010150d67f
diff --git a/.changes/fix-nosession-client-owned.md b/.changes/fix-nosession-client-owned.md
new file mode 100644
index 0000000000..c9ad8727e4
--- /dev/null
+++ b/.changes/fix-nosession-client-owned.md
@@ -0,0 +1 @@
+- ACP sessions and TUI `--no-session` sessions are now owned by the client that created them: they are never idle-passivated, and their agents-view row is removed when they stop instead of lingering as a passive row that cannot be resumed.
diff --git a/crates/pa-cli/tests/acp_mode_e2e.rs b/crates/pa-cli/tests/acp_mode_e2e.rs
index 34b6bde8c7..0dba9a7726 100644
--- a/crates/pa-cli/tests/acp_mode_e2e.rs
+++ b/crates/pa-cli/tests/acp_mode_e2e.rs
@@ -707,7 +707,7 @@ fn acp_daemon_attached_serves_a_client_owned_session() {
// the sandboxed socket, hosts a client-owned scripted session, and
// serves the same ACP surface (chunk + settle envelope + end_turn).
let script = json!({ "engine": "faux", "responses": ["The Thames flows through London."] });
- let (mut client, _socket) =
+ let (mut client, socket) =
AcpChild::spawn_daemon_attached(&["--mode", "acp", "--no-session"], &script);
let init = client.request("initialize", &initialize_params());
let _ = client.wait_response(init, TIMEOUT);
@@ -721,6 +721,23 @@ fn acp_daemon_attached_serves_a_client_owned_session() {
.as_str()
.unwrap()
.to_string();
+ // Read before session/close: its Kill removes the descriptor.
+ let home = socket.parent().expect("the sandbox home");
+ let descriptor = std::fs::read_dir(home.join(".prime/agent/daemon-workers"))
+ .expect("descriptor instances")
+ .flatten()
+ .flat_map(|instance| {
+ std::fs::read_dir(instance.path())
+ .expect("instance dir")
+ .flatten()
+ })
+ .filter_map(|file| {
+ serde_json::from_str::<Value>(&std::fs::read_to_string(file.path()).ok()?).ok()
+ })
+ .find(|descriptor| descriptor.get("authenticationToken").is_some())
+ .expect("the worker descriptor");
+ let acp_pid = client.child.id();
+ assert_eq!(descriptor["ownerClientId"], json!(format!("acp:{acp_pid}")));
let prompt = client.request(
"session/prompt",
&json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "Name a river." }] }),
diff --git a/crates/pa-daemon/src/acp/daemon.rs b/crates/pa-daemon/src/acp/daemon.rs
index 3dc006c448..56de9fafdb 100644
--- a/crates/pa-daemon/src/acp/daemon.rs
+++ b/crates/pa-daemon/src/acp/daemon.rs
@@ -24,7 +24,7 @@ use std::sync::Arc;
use pa_types::daemon::{
DaemonCommand, DaemonCommandEnvelope, DaemonCommandFrameType, DaemonProtocolInfo,
- DaemonResponse, DAEMON_PROTOCOL_NAME, DAEMON_PROTOCOL_VERSION,
+ DaemonResponse, DaemonSessionLifecycle, DAEMON_PROTOCOL_NAME, DAEMON_PROTOCOL_VERSION,
};
use serde_json::{json, Map, Value};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
@@ -553,7 +553,7 @@ async fn handle_session_new(
// (settings + env); external clients never toggle telemetry.
telemetry_disabled: None,
runtime_metadata: None,
- lifecycle: None,
+ lifecycle: Some(DaemonSessionLifecycle::ClientOwned),
env: None,
launch_env: None,
rest: Map::default(),
diff --git a/crates/pa-daemon/src/peer_tickets.rs b/crates/pa-daemon/src/peer_tickets.rs
index 1f8e2a988b..1906a85149 100644
--- a/crates/pa-daemon/src/peer_tickets.rs
+++ b/crates/pa-daemon/src/peer_tickets.rs
@@ -10,8 +10,7 @@
//! session socket directly.
//!
//! TS refuses tickets for client-owned workers (`ownerClientId`); the Rust
-//! supervisor has no client-owned worker lifecycle (every worker it spawns or
-//! adopts is a resident session), so there is no such class to refuse.
+//! supervisor has not ported that refusal yet (a disclosed divergence).
use std::sync::atomic::Ordering;
use std::sync::Arc;
diff --git a/crates/pa-daemon/src/supervisor/tests.rs b/crates/pa-daemon/src/supervisor/tests.rs
index 854a4db075..1e850dfe28 100644
--- a/crates/pa-daemon/src/supervisor/tests.rs
+++ b/crates/pa-daemon/src/supervisor/tests.rs
@@ -1070,11 +1070,10 @@ async fn idle_passivation_requires_the_worker_token() {
}
#[tokio::test]
-async fn idle_passivation_refuses_client_owned_and_in_memory_workers() {
- // The refusal gate (TS `canEvictWorker`'s `hasOwnerClient` arm,
- // widened): a client-owned worker never passivates itself, and an
- // in-memory (noSession) root has no file to wake from. An unowned
- // sessioned root passes this gate; with a route in flight, its ask
+async fn idle_passivation_refuses_a_client_owned_worker() {
+ // The owner refusal (TS `canEvictWorker`'s `hasOwnerClient` arm):
+ // ownership alone carries it (TS `clientOwned: noSession`). An
+ // unowned root passes this gate; with a route in flight, its ask
// defers instead of stopping the worker underneath it (the e2e
// drives the pass side end to end).
let dir = tempfile::TempDir::new().unwrap();
@@ -1119,14 +1118,6 @@ async fn idle_passivation_refuses_client_owned_and_in_memory_workers() {
last_error: None,
rest: Map::default(),
};
- let in_memory = {
- let mut descriptor = descriptor.clone();
- descriptor.worker_id = "w-mem".to_string();
- descriptor.authentication_token = "mem-token".to_string();
- descriptor.owner_client_id = None;
- descriptor.create_command.no_session = Some(true);
- descriptor
- };
let idle = pa_types::daemon::DaemonWorkerDescriptor {
worker_id: "w-idle".to_string(),
authentication_token: "idle-token".to_string(),
@@ -1144,23 +1135,9 @@ async fn idle_passivation_refuses_client_owned_and_in_memory_workers() {
.error
.as_deref()
.unwrap_or("")
- .contains("client-owned or in-memory (noSession) worker"),
+ .contains("client-owned worker"),
"the client-owned worker's ask is refused: {response:?}"
);
- let resident = ResidentWorker::new("w-mem".to_string(), in_memory, dir.path().join("w-mem.d"));
- supervisor.registry.insert(resident).await;
- let response = supervisor
- .handle_worker_idle_passivation("c2", "worker_idle_passivation", "mem-token", Some(1))
- .await;
- assert!(!response.success);
- assert!(
- response
- .error
- .as_deref()
- .unwrap_or("")
- .contains("client-owned or in-memory (noSession) worker"),
- "the in-memory worker's ask is refused: {response:?}"
- );
// The eviction fence (TS `withEvictionFence`): a request in flight
// when the ask arrives defers the passivation — no stop, no
// tombstone — and the same ask stops the worker once the route
diff --git a/crates/pa-daemon/src/supervisor/worker_lifecycle.rs b/crates/pa-daemon/src/supervisor/worker_lifecycle.rs
index 147c987cd2..16ede79e77 100644
--- a/crates/pa-daemon/src/supervisor/worker_lifecycle.rs
+++ b/crates/pa-daemon/src/supervisor/worker_lifecycle.rs
@@ -622,10 +622,8 @@ impl Supervisor {
/// idle state and crossed the threshold asks for its own graceful
/// stop over the supervisor link — TS `canEvictWorker` reaches roots
/// and children alike. The supervisor verifies the worker token,
- /// refuses a client-owned or noSession descriptor (TS
- /// `hasOwnerClient`: the owner's disconnect cleanup owns that stop;
- /// an in-memory session has no file to wake from), and re-reads the
- /// setting — the supervisor's own fresh-snapshot fence: a setting
+ /// refuses a client-owned descriptor (TS `hasOwnerClient`), and re-reads
+ /// the setting — the supervisor's own fresh-snapshot fence: a setting
/// flipped to `"off"` (or past the threshold) between the worker's
/// ask and the stop cancels the passivation. The stop runs under the
/// eviction fence (TS `withEvictionFence`) and is the existing
@@ -649,21 +647,15 @@ impl Supervisor {
None,
);
};
- // Rust clients create noSession roots unowned (`lifecycle: None`);
- // TS makes them client-owned, so its `hasOwnerClient` refuses them.
- // An in-memory session has no file to wake from.
- {
- let descriptor = resident.descriptor.lock().await;
- if descriptor.owner_client_id.is_some()
- || descriptor.create_command.no_session == Some(true)
- {
- return response_failure(
- Some(command_id),
- type_name,
- "Idle passivation is refused for a client-owned or in-memory (noSession) worker",
- None,
- );
- }
+ // The owner gate (TS `canEvictWorker`'s `hasOwnerClient` arm): a
+ // client-owned worker never passivates itself.
+ if resident.descriptor.lock().await.owner_client_id.is_some() {
+ return response_failure(
+ Some(command_id),
+ type_name,
+ "Idle passivation is refused for a client-owned worker",
+ None,
+ );
}
// The supervisor-side settings re-read (the fence): the same
// `idleEvictionMinutes` surface the worker read.
diff --git a/crates/pa-daemon/src/worker/turn.rs b/crates/pa-daemon/src/worker/turn.rs
index b4badb6121..1628baffe8 100644
--- a/crates/pa-daemon/src/worker/turn.rs
+++ b/crates/pa-daemon/src/worker/turn.rs
@@ -234,9 +234,8 @@ impl TurnRunner {
/// `isSessionActive`'s pending-prompt-admissions arm) and the
/// setting is a live threshold; `None` otherwise (attached
/// sessions, `"off"`, and any state the engine gates would reject
- /// stay parked without a timer). The client-owned and noSession
- /// refusals are supervisor-side (the descriptor's `ownerClientId`
- /// and `noSession`). The engine gate
+ /// stay parked without a timer). The client-owned refusal is
+ /// supervisor-side (the descriptor's `ownerClientId`). The engine gate
/// (`SessionEngine::can_passivate_worker`) is re-checked at the fire
/// inside [`Self::maybe_request_idle_passivation`] — the
/// fresh-snapshot fence — so this window only decides whether to
diff --git a/crates/pa-tui/src/session_ui/sessions_fork.rs b/crates/pa-tui/src/session_ui/sessions_fork.rs
index e35782b07c..e9d0678330 100644
--- a/crates/pa-tui/src/session_ui/sessions_fork.rs
+++ b/crates/pa-tui/src/session_ui/sessions_fork.rs
@@ -6,6 +6,7 @@ use super::{
SessionSelection, SessionUi, TreeSelector, TreeSelectorAction, UserMessageSelector,
UserMessageSelectorAction, Value, UI_REQUEST_TIMEOUT_MS,
};
+use pa_types::daemon::DaemonSessionLifecycle;
impl SessionUi {
// ------------------------------------------------------------------
@@ -538,7 +539,10 @@ pub(super) async fn create_session(
config: Some(options.create_config()),
telemetry_disabled: options.telemetry_disabled.filter(|disabled| *disabled),
runtime_metadata: None,
- lifecycle: None,
+ // TS `clientOwned: parsed.noSession`: only a noSession create is client-owned.
+ lifecycle: options
+ .no_session
+ .then_some(DaemonSessionLifecycle::ClientOwned),
env: None,
launch_env: None,
rest: Map::default(),