Skip to content

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

Rust

Release print session leases after runtime writers stop (#3405)

Sebastian Müller80dfbf51cb32
diff --git a/.changes/fix-print-session-lease-lifetime.md b/.changes/fix-print-session-lease-lifetime.md
new file mode 100644
index 0000000000..3e72877c46
--- /dev/null
+++ b/.changes/fix-print-session-lease-lifetime.md
@@ -0,0 +1 @@
+- Print and JSON runs now release their session lease after the runtime stops, including error exits and panic unwinding, so detached session writers finish or are cancelled before another run can reopen the session.
diff --git a/crates/pa-cli/src/print_runtime.rs b/crates/pa-cli/src/print_runtime.rs
index ec5a8f09da..6b44fd3943 100644
--- a/crates/pa-cli/src/print_runtime.rs
+++ b/crates/pa-cli/src/print_runtime.rs
@@ -228,7 +228,8 @@ fn run_rpc_mode(options: &RunOptions) -> Result<i32, String> {
async fn rpc_mode_main(options: &RunOptions) -> Result<i32, String> {
let config = &options.config;
- let (parts, initial_lease) = build_headless_engine_parts_with_lease(options, "rpc").await?;
+ let mut initial_lease = None;
+ let parts = build_headless_engine_parts(options, "rpc", &mut initial_lease).await?;
if let Some(goal) = &config.initial_goal {
parts
.engine
@@ -357,17 +358,42 @@ impl From<HeadlessEngine> for pa_daemon::rpc::session::RpcEngineHandle {
}
fn run_print_mode(options: &RunOptions) -> Result<i32, String> {
+ with_print_runtime(options, |options, lease| {
+ Box::pin(print_mode_main(options, lease))
+ })
+}
+
+/// Own the print lease through runtime shutdown, including errors and unwinding.
+/// The operation seam lets the regression exercise a writer pending at shutdown.
+fn with_print_runtime<Context>(
+ context: &Context,
+ run: impl for<'a> FnOnce(
+ &'a Context,
+ &'a mut Option<pa_daemon::lease::SessionLease>,
+ ) -> std::pin::Pin<
+ Box<dyn std::future::Future<Output = Result<i32, String>> + 'a>,
+ >,
+) -> Result<i32, String> {
+ // Declared first so unwinding also stops all runtime tasks before release.
+ let mut lease = None;
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|error| error.to_string())?;
- rt.block_on(print_mode_main(options))
+ let result = rt.block_on(run(context, &mut lease));
+ // Runtime Drop joins blocking writers and cancels async host tasks. Engine
+ // Drop alone cannot do that: detached host requests retain session writers.
+ drop(rt);
+ result
}
-async fn print_mode_main(options: &RunOptions) -> Result<i32, String> {
+async fn print_mode_main(
+ options: &RunOptions,
+ lease: &mut Option<pa_daemon::lease::SessionLease>,
+) -> Result<i32, String> {
// `print` or `json`: the telemetry execution mode is the app mode (TS
// main.ts `executionMode: appMode`).
- let headless = build_headless_engine(options, options.app_mode.as_str()).await?;
+ let headless = build_headless_engine_parts(options, options.app_mode.as_str(), lease).await?;
let engine = std::sync::Arc::new(headless.engine);
// The CLI `--goal` seed: a fresh root branch starts the goal; a resumed
// branch keeps its persisted goal. Depth 0 only — the print session is a root.
@@ -402,34 +428,23 @@ struct HeadlessEngine {
provider_target: ProviderTargetSlot,
}
+/// Transfer the opened lease to its mode before fallible engine assembly.
async fn build_headless_engine_parts(
options: &RunOptions,
execution_mode: &str,
+ lease: &mut Option<pa_daemon::lease::SessionLease>,
) -> Result<HeadlessEngine, String> {
- let (engine, lease) = build_headless_engine_parts_with_lease(options, execution_mode).await?;
- std::mem::forget(lease);
- Ok(engine)
-}
-
-/// The same assembly, returning the opened session's runtime lease alongside (long-lived
-/// connections hold it on the engine handle).
-async fn build_headless_engine_parts_with_lease(
- options: &RunOptions,
- execution_mode: &str,
-) -> Result<(HeadlessEngine, Option<pa_daemon::lease::SessionLease>), String> {
// Every headless mode discloses immediately (TS main: only the
// interactive `deferTelemetryNoticeForOnboarding` holds the notice
// back behind onboarding; `--list-models` never reaches this
// assembly, matching the TS exit before its diagnostics report).
crate::telemetry_notice::print_if_due(&options.config);
- let (session_manager, lease) = select_session_manager_with_lease(options)?;
+ let (session_manager, opened_lease) = select_session_manager_with_lease(options)?;
+ *lease = opened_lease;
if let Ok(script) = std::env::var("PRIME_AGENT_FAUX_SCRIPT") {
- let engine =
- build_faux_engine_with(options, &script, session_manager, execution_mode).await?;
- return Ok((engine, lease));
+ return build_faux_engine_with(options, &script, session_manager, execution_mode).await;
}
- let engine = build_headless_engine_with(options, session_manager, execution_mode).await?;
- Ok((engine, lease))
+ build_headless_engine_with(options, session_manager, execution_mode).await
}
/// The session-manager selection every engine build shares, returning the opened session's runtime
@@ -780,14 +795,6 @@ fn headless_image_model_router(
}
}
-/// The engine alone (callers that do not drive session commands).
-async fn build_headless_engine(
- options: &RunOptions,
- execution_mode: &str,
-) -> Result<HeadlessEngine, String> {
- build_headless_engine_parts(options, execution_mode).await
-}
-
/// The session header line: the session file's `type: "session"` entry in the
/// TS wire shape and field order.
async fn session_header_json(
@@ -961,9 +968,8 @@ fn select_headless_session(options: &RunOptions) -> Result<HeadlessSession, Stri
}
/// The in-process session manager for the selected session. The opened
-/// session's runtime lease returns alongside (a long-lived connection
-/// holds it on the engine handle; the one-shot modes forget it for the
-/// process lifetime).
+/// session's runtime lease returns alongside: the driving mode owns it for
+/// its run's lifetime.
fn build_session_manager_with_lease(
options: &RunOptions,
) -> Result<
@@ -1534,6 +1540,96 @@ async fn build_faux_engine_with(
#[cfg(test)]
mod tests {
+ /// Runtime cancellation unblocks a pending writer; ownership must remain
+ /// held until that blocking writer finishes, even on errors or unwinding.
+ #[test]
+ fn print_lease_outlives_runtime_writers_on_every_return_path() {
+ #[derive(Clone, Copy)]
+ enum Outcome {
+ Success,
+ Error,
+ Panic,
+ }
+ struct Fixture {
+ session_path: std::path::PathBuf,
+ agent_dir: std::path::PathBuf,
+ outcome: Outcome,
+ }
+ struct UnblockWriter(std::sync::mpsc::Sender<()>);
+ impl Drop for UnblockWriter {
+ fn drop(&mut self) {
+ let _ = self.0.send(());
+ }
+ }
+ for outcome in [Outcome::Success, Outcome::Error, Outcome::Panic] {
+ let home = tempfile::TempDir::new().unwrap();
+ let fixture = Fixture {
+ session_path: home.path().join("session.jsonl"),
+ agent_dir: home.path().join("agent"),
+ outcome,
+ };
+ let held_at_write = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
+ let writer_observation = std::sync::Arc::clone(&held_at_write);
+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ super::with_print_runtime(&fixture, |fixture, lease| {
+ Box::pin(async move {
+ *lease = Some(
+ pa_daemon::lease::acquire_runtime_session_lease(
+ &fixture.session_path,
+ &fixture.agent_dir,
+ )
+ .unwrap(),
+ );
+ let (cancel_tx, cancel_rx) = std::sync::mpsc::channel();
+ let (ready_tx, ready_rx) = tokio::sync::oneshot::channel();
+ tokio::spawn(async move {
+ let _unblock = UnblockWriter(cancel_tx);
+ let _ = ready_tx.send(());
+ std::future::pending::<()>().await;
+ });
+ ready_rx.await.unwrap();
+ let session_path = fixture.session_path.clone();
+ let agent_dir = fixture.agent_dir.clone();
+ let (writer_ready_tx, writer_ready_rx) = tokio::sync::oneshot::channel();
+ tokio::task::spawn_blocking(move || {
+ let _ = writer_ready_tx.send(());
+ cancel_rx.recv().unwrap();
+ writer_observation.store(
+ pa_daemon::lease::live_lease_owner(&agent_dir, &session_path)
+ .is_some(),
+ std::sync::atomic::Ordering::SeqCst,
+ );
+ std::fs::write(&session_path, "writer settled\n").unwrap();
+ });
+ writer_ready_rx.await.unwrap();
+ match fixture.outcome {
+ Outcome::Success => Ok(0),
+ Outcome::Error => Err("failed after lease acquisition".to_string()),
+ Outcome::Panic => panic!("unwind after lease acquisition"),
+ }
+ })
+ })
+ }));
+ match outcome {
+ Outcome::Success => assert_eq!(result.unwrap(), Ok(0)),
+ Outcome::Error => assert_eq!(
+ result.unwrap(),
+ Err("failed after lease acquisition".to_string())
+ ),
+ Outcome::Panic => assert!(result.is_err()),
+ }
+ assert!(held_at_write.load(std::sync::atomic::Ordering::SeqCst));
+ assert_eq!(
+ std::fs::read_to_string(&fixture.session_path).unwrap(),
+ "writer settled\n"
+ );
+ assert!(
+ pa_daemon::lease::live_lease_owner(&fixture.agent_dir, &fixture.session_path)
+ .is_none()
+ );
+ }
+ }
+
/// The settle restores the captured session target only while the slot still holds the route;
/// a mid-run `/model` switch rewrote the slot, and stays.
#[test]
diff --git a/crates/pa-cli/tests/print_runtime_e2e.rs b/crates/pa-cli/tests/print_runtime_e2e.rs
index 315170cef4..6a6f80a655 100644
--- a/crates/pa-cli/tests/print_runtime_e2e.rs
+++ b/crates/pa-cli/tests/print_runtime_e2e.rs
@@ -154,6 +154,27 @@ fn read_entries(path: &std::path::Path) -> Vec<serde_json::Value> {
.unwrap()
}
+/// The session files holding a runtime lease right now (the owner records
+/// under the agent dir's `session-leases`).
+fn leased_session_files(home: &std::path::Path) -> Vec<String> {
+ let mut leased = Vec::new();
+ let Ok(entries) = std::fs::read_dir(home.join(".prime/agent/session-leases")) else {
+ return leased;
+ };
+ for entry in entries.flatten() {
+ let Ok(text) = std::fs::read_to_string(entry.path().join("owner.json")) else {
+ continue;
+ };
+ let Ok(owner) = serde_json::from_str::<serde_json::Value>(&text) else {
+ continue;
+ };
+ if let Some(path) = owner.get("sessionPath").and_then(serde_json::Value::as_str) {
+ leased.push(path.to_string());
+ }
+ }
+ leased
+}
+
#[test]
fn print_mode_persists_a_session_file_by_default() {
let home = isolated_home();
@@ -257,6 +278,23 @@ fn print_mode_continue_recent_reuses_the_latest_session() {
);
}
+/// A finished print run releases the session file's runtime lease: the run
+/// exits and no owner record survives under `session-leases`.
+#[test]
+fn print_mode_releases_the_session_lease_when_the_run_ends() {
+ let home = isolated_home();
+ let script = serde_json::json!({ "responses": ["leased answer"] });
+ let (stdout, stderr, code) = run_in_home(home.path(), &["-p", "hi"], &script);
+ assert_eq!(code, 0, "stderr: {stderr}");
+ assert_eq!(stdout, "leased answer\n");
+ let files = session_files(home.path());
+ assert_eq!(files.len(), 1, "one session file, got {files:?}");
+ assert!(
+ leased_session_files(home.path()).is_empty(),
+ "the ended run left a lease owner behind"
+ );
+}
+
#[test]
fn print_mode_resume_unknown_selector_fails_with_browse_hint() {
let home = isolated_home();