Skip to content

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

Rust

remove the session archive (#3657)

Sebastian Müller7889188e442f
diff --git a/.changes/remove-session-archive.md b/.changes/remove-session-archive.md
new file mode 100644
index 0000000000..3d3e8abcbc
--- /dev/null
+++ b/.changes/remove-session-archive.md
@@ -0,0 +1,2 @@
+- Remove automatic session archiving and the `sessionArchiveMaxAgeDays` and `sessionArchiveMaxSessions` settings. The agents view and `/resume` page through the live session store instead.
+- Keep lazy recovery of files archived by older Rust versions: a saved-session wake by ID, ID prefix or exact name restores a matching archived file when no live session matches. Existing live files are never overwritten; other archived files stay in place until addressed.
diff --git a/crates/pa-core/src/session_engine/telemetry.rs b/crates/pa-core/src/session_engine/telemetry.rs
index dee853548e..50b5196c9d 100644
--- a/crates/pa-core/src/session_engine/telemetry.rs
+++ b/crates/pa-core/src/session_engine/telemetry.rs
@@ -36,8 +36,8 @@ use super::error_classify::classify_error_message;
mod track;
pub use track::{
track_catalog_refresh, track_compaction_abort_declared, track_daemon_event_summary,
- track_deleted_child_usage_captured, track_model_refused, track_sessions_archived,
- track_worker_adoption, track_worker_children_closed,
+ track_deleted_child_usage_captured, track_model_refused, track_worker_adoption,
+ track_worker_children_closed,
};
mod classify;
diff --git a/crates/pa-core/src/session_engine/telemetry/track.rs b/crates/pa-core/src/session_engine/telemetry/track.rs
index 3f00dcacfe..fdba0b66eb 100644
--- a/crates/pa-core/src/session_engine/telemetry/track.rs
+++ b/crates/pa-core/src/session_engine/telemetry/track.rs
@@ -59,15 +59,6 @@ pub fn track_model_refused(
client.track("model refused", properties);
}
-/// Track the disk-archive sweep's `daemon event` (schema v1, kind
-/// `sessions_archived`): a count only, never session payload.
-pub fn track_sessions_archived(client: &TelemetryClient, count: usize) {
- let mut properties = base_properties("daemon");
- properties.set("kind", Value::from("sessions_archived"));
- properties.set("count", Value::from(count));
- client.track("daemon event", properties);
-}
-
/// Track the boot descriptor-adoption pass's `daemon event` (schema v1, kind
/// `worker_adoption`): the boot kind and per-outcome counts, never session payload.
/// `skipped_idle` counts the dead descriptors the busy-evidence filter parked.
diff --git a/crates/pa-core/src/settings/manager.rs b/crates/pa-core/src/settings/manager.rs
index 9b2048e734..6e9af35b51 100644
--- a/crates/pa-core/src/settings/manager.rs
+++ b/crates/pa-core/src/settings/manager.rs
@@ -15,8 +15,6 @@ use super::types::{
pub const RECENT_MODELS_LIMIT: usize = 20;
pub const DEFAULT_IDLE_EVICTION_MINUTES: u64 = 90;
-pub const DEFAULT_SESSION_ARCHIVE_MAX_AGE_DAYS: u64 = 30;
-pub const DEFAULT_SESSION_ARCHIVE_MAX_SESSIONS: usize = 200;
#[derive(Debug, Clone)]
pub struct SettingsError {
@@ -807,34 +805,6 @@ impl SettingsManager {
}
}
- /// Resolved session-archiving policy: both rules are independent — a session is
- /// archived when EITHER fires; `None` on a field disables that rule.
- #[must_use]
- pub fn get_session_archive_policy(&self) -> SessionArchivePolicy {
- let max_age_days = match &self.global.session_archive_max_age_days {
- Some(serde_json::Value::String(text)) if text == "off" || text == "none" => None,
- Some(serde_json::Value::Number(number)) => Some(
- number
- .as_u64()
- .filter(|days| *days > 0)
- .unwrap_or(DEFAULT_SESSION_ARCHIVE_MAX_AGE_DAYS),
- ),
- _ => Some(DEFAULT_SESSION_ARCHIVE_MAX_AGE_DAYS),
- };
- let max_sessions = match &self.global.session_archive_max_sessions {
- Some(serde_json::Value::String(text)) if text == "off" || text == "none" => None,
- Some(serde_json::Value::Number(number)) => number
- .as_u64()
- .filter(|count| *count > 0)
- .map(|count| count as usize),
- _ => Some(DEFAULT_SESSION_ARCHIVE_MAX_SESSIONS),
- };
- SessionArchivePolicy {
- max_age_days,
- max_sessions,
- }
- }
-
#[must_use]
pub fn get_transport(&self) -> TransportSetting {
self.merged.transport.unwrap_or(TransportSetting::Auto)
@@ -984,14 +954,6 @@ pub enum IdleEviction {
Off,
}
-/// Resolved session-archiving settings: the age rule (sessions untouched for
-/// `max_age_days` days) and the count rule (keep the newest `max_sessions`).
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub struct SessionArchivePolicy {
- pub max_age_days: Option<u64>,
- pub max_sessions: Option<usize>,
-}
-
fn strings(array: &[serde_json::Value]) -> Vec<String> {
array
.iter()
@@ -1263,33 +1225,6 @@ mod tests {
assert!(!manager.get_compaction_enabled());
}
- #[test]
- fn session_archive_policy_semantics() {
- let mut manager = SettingsManager::in_memory(&Settings::default());
- assert_eq!(
- manager.get_session_archive_policy(),
- SessionArchivePolicy {
- max_age_days: Some(DEFAULT_SESSION_ARCHIVE_MAX_AGE_DAYS),
- max_sessions: Some(DEFAULT_SESSION_ARCHIVE_MAX_SESSIONS),
- }
- );
- manager.global.session_archive_max_age_days = Some(serde_json::json!("off"));
- assert_eq!(manager.get_session_archive_policy().max_age_days, None);
- manager.global.session_archive_max_age_days = Some(serde_json::json!(0));
- assert_eq!(
- manager.get_session_archive_policy().max_age_days,
- Some(DEFAULT_SESSION_ARCHIVE_MAX_AGE_DAYS)
- );
- manager.global.session_archive_max_age_days = Some(serde_json::json!(14));
- assert_eq!(manager.get_session_archive_policy().max_age_days, Some(14));
- manager.global.session_archive_max_sessions = Some(serde_json::json!("none"));
- assert_eq!(manager.get_session_archive_policy().max_sessions, None);
- manager.global.session_archive_max_sessions = Some(serde_json::json!(0));
- assert_eq!(manager.get_session_archive_policy().max_sessions, None);
- manager.global.session_archive_max_sessions = Some(serde_json::json!(50));
- assert_eq!(manager.get_session_archive_policy().max_sessions, Some(50));
- }
-
#[test]
fn image_model_reads_trimmed_or_unset() {
let manager = SettingsManager::in_memory(&Settings {
diff --git a/crates/pa-core/src/settings/mod.rs b/crates/pa-core/src/settings/mod.rs
index f9f2c5a81d..0a88642793 100644
--- a/crates/pa-core/src/settings/mod.rs
+++ b/crates/pa-core/src/settings/mod.rs
@@ -11,11 +11,7 @@ pub(crate) mod types;
#[cfg(test)]
mod persistence_tests;
-pub use manager::{
- IdleEviction, SessionArchivePolicy, SettingsError, SettingsManager,
- DEFAULT_IDLE_EVICTION_MINUTES, DEFAULT_SESSION_ARCHIVE_MAX_AGE_DAYS,
- DEFAULT_SESSION_ARCHIVE_MAX_SESSIONS,
-};
+pub use manager::{IdleEviction, SettingsError, SettingsManager, DEFAULT_IDLE_EVICTION_MINUTES};
pub use storage::{
FileSettingsStorage, InMemorySettingsStorage, SettingsScope, SettingsStorage, CONFIG_DIR_NAME,
};
diff --git a/crates/pa-core/src/settings/types.rs b/crates/pa-core/src/settings/types.rs
index 7a991d7a64..0cd795ac6d 100644
--- a/crates/pa-core/src/settings/types.rs
+++ b/crates/pa-core/src/settings/types.rs
@@ -315,13 +315,6 @@ pub struct Settings {
pub rlm_max_depth: Option<u64>,
/// `number | "off" | "none"`; raw JSON because the TS getter validates at access time.
pub idle_eviction_minutes: Option<serde_json::Value>,
- /// Session archiving age rule: sessions untouched for this many days are
- /// moved to the daemon's archive directory. `number | "off" | "none"`; raw
- /// JSON validated at access time (same grammar as `idleEvictionMinutes`).
- pub session_archive_max_age_days: Option<serde_json::Value>,
- /// Session archiving count rule: keep at most this many unarchived sessions (newest by mtime
- /// win); `0 | "off" | "none"` disables. Raw JSON validated at access time.
- pub session_archive_max_sessions: Option<serde_json::Value>,
pub transport: Option<TransportSetting>,
pub steering_mode: Option<QueueModeSetting>,
pub follow_up_mode: Option<QueueModeSetting>,
diff --git a/crates/pa-daemon/README.md b/crates/pa-daemon/README.md
index 66eed1af77..027009ab09 100644
--- a/crates/pa-daemon/README.md
+++ b/crates/pa-daemon/README.md
@@ -30,21 +30,13 @@ command gating on peer links), kernel agent_message/agent_observe bridge (`agent
`worker`-purpose single-use grants minted by `get_worker_peer_transport`
for a source worker's kernel `agent_message.send`, direct
`worker_deliver_message` on the target worker's socket with the supervisor
-routed `send_message` as the never-retried fallback), session archiving
-(`session_archive.rs`: the sessions directory must not grow forever — a
-supervisor boot sweep plus a periodic re-sweep at the TS idle-eviction
-cadence retire sessions by age (settings `sessionArchiveMaxAgeDays`,
-default 30) and count (settings `sessionArchiveMaxSessions`, default 200;
-each rule independently off-able) by MOVING them into
-`<agent-dir>/sessions-archive`; resident-worker sessions and sessions with
-active scheduled jobs are never archived; an archived session stays
-resumable — the catalog resolve falls back to the archive, restores the
-file into the sessions dir, and the wake spawns over the live path),
+routed `send_message` as the never-retried fallback),
saved-session wake
for non-resident `send_message` targets (`session_catalog.rs`: catalog
-resolve by session-id prefix or exact name, cwd-scoped first, archived
-fallback with restore; a resident
-worker hosting the file is reused, otherwise one spawns over it; RLM
+resolve by session-id prefix or exact name, cwd-scoped first; a live miss
+lazily restores a matching file from `sessions-archive/` without
+overwriting an existing live file; no new sessions are physically archived;
+a resident worker hosting the file is reused, otherwise one spawns over it; RLM
children fall back to the spawn ledger's live child edges when the catalog
misses - children persist outside the sessions dir, under the parent's
session-artifacts tree - and wake over their own session file), wire protocol serve/negotiation (including the
@@ -84,8 +76,7 @@ merge non-resident ledger children (walk roots = saved + resident session
files), so passivated children stay roster-visible (TS
`walkPassiveRlmSubagents` / `withPassiveRlmDescendantInfos`). The saved
session scan (`session_store.rs`) folds each file once into the durable
-catalog row. Archived sessions live in `<agent-dir>/sessions-archive` and
-never reach the catalog scan, so search covers live sessions only.
+catalog row.
Supervisor-backed RLM child sessions
(`rlm_children.rs`, the daemon side of the pa-core `RlmSubagentHost` seam):
`rlm.spawn`/`rlm.create_session` create real daemon sessions through the
diff --git a/crates/pa-daemon/src/lib.rs b/crates/pa-daemon/src/lib.rs
index 05686a3c22..5775d7c8af 100644
--- a/crates/pa-daemon/src/lib.rs
+++ b/crates/pa-daemon/src/lib.rs
@@ -86,7 +86,6 @@ pub mod rpc;
pub(crate) mod saved_session_commands;
pub(crate) mod scheduled_jobs;
pub(crate) mod scheduling_catalog;
-pub(crate) mod session_archive;
pub(crate) mod session_bindings;
pub(crate) mod session_catalog;
pub(crate) mod session_commands;
diff --git a/crates/pa-daemon/src/messaging.rs b/crates/pa-daemon/src/messaging.rs
index c8901f2150..b92f019fe2 100644
--- a/crates/pa-daemon/src/messaging.rs
+++ b/crates/pa-daemon/src/messaging.rs
@@ -216,24 +216,31 @@ impl Supervisor {
Err(error) => return WakeOutcome::Failed(error.to_string()),
},
};
- let archive_dir = crate::session_archive::archive_dir(&self.options.agent_dir);
- let info = match crate::session_catalog::resolve_saved_session(
- &sessions_dir,
- &archive_dir,
- selector,
- &cwd,
- ) {
- Ok(Some(info)) => info,
+ // The catalog scans and an archive restore's copy are blocking file I/O.
+ let archive_dir = self.options.agent_dir.join("sessions-archive");
+ let catalog_selector = selector.to_string();
+ let resolved = tokio::task::spawn_blocking(move || {
+ crate::session_catalog::resolve_saved_session(
+ &sessions_dir,
+ &archive_dir,
+ &catalog_selector,
+ &cwd,
+ )
+ })
+ .await;
+ let info = match resolved {
+ Ok(Ok(Some(info))) => info,
// The saved-session catalog misses RLM children (they persist in
// the parent's session-artifacts tree); the spawn ledger still
// tracks them, so a child selector falls back to its live edges.
- Ok(None) => {
+ Ok(Ok(None)) => {
return match self.wake_ledger_child(selector).await {
Some(outcome) => outcome,
None => WakeOutcome::Unknown,
}
}
- Err(error) => return WakeOutcome::Failed(error.to_string()),
+ Ok(Err(error)) => return WakeOutcome::Failed(error.to_string()),
+ Err(error) => return WakeOutcome::Failed(format!("the saved-session lookup: {error}")),
};
let session_path = info.path.to_string_lossy().to_string();
// Reuse before spawning (TS `createOrReuseWorker`): a resident
diff --git a/crates/pa-daemon/src/rlm_ledger/replay.rs b/crates/pa-daemon/src/rlm_ledger/replay.rs
index 22d9c3b82a..038dd9590b 100644
--- a/crates/pa-daemon/src/rlm_ledger/replay.rs
+++ b/crates/pa-daemon/src/rlm_ledger/replay.rs
@@ -160,7 +160,7 @@ pub(super) struct ReplayState {
pub(super) deleted_bucket: Mutex<Option<DeletedBucket>>,
}
-/// Ledger rows are immutable within a replay, but restore/archive and transcript
+/// Ledger rows are immutable within a replay, but file moves and transcript
/// edits can change the inputs of the deleted-spend fold without any append.
#[derive(Debug)]
pub(super) struct DeletedBucket {
@@ -228,12 +228,6 @@ impl<'a> LivePathResolver<'a> {
if is_file(&sessions_candidate) {
return Ok(Some(sessions_candidate));
}
- if is_file(
- &crate::session_archive::archive_dir(&self.agent_dir)
- .join(crate::session_store::session_file_name(&id)),
- ) {
- return Ok(None);
- }
let mut index = self
.artifact_index
.get_or_init(|| Mutex::new(artifact_session_index(&self.agent_dir)))
diff --git a/crates/pa-daemon/src/rlm_ledger/tests.rs b/crates/pa-daemon/src/rlm_ledger/tests.rs
index e0e147fffb..686711572e 100644
--- a/crates/pa-daemon/src/rlm_ledger/tests.rs
+++ b/crates/pa-daemon/src/rlm_ledger/tests.rs
@@ -517,14 +517,15 @@ fn live_edges_of_parent_bounds_the_liveness_work() {
}
#[test]
-fn a_dead_endpoint_under_an_archived_parent_resolves_without_a_tree_walk() {
+fn a_parent_left_in_the_archive_dir_is_a_dead_endpoint_walked_once() {
let agent_dir = temp_dir("archive-dead");
let sessions_dir = agent_dir.join("sessions");
fs::create_dir_all(&sessions_dir).unwrap();
let ledger = ledger_over(&agent_dir, &sessions_dir);
let child = sessions_dir.join("sess-c.jsonl");
fs::write(&child, "{}").unwrap();
- let archived = crate::session_archive::archive_dir(&agent_dir)
+ let archived = agent_dir
+ .join("sessions-archive")
.join(crate::session_store::session_file_name("sess-p"));
fs::create_dir_all(archived.parent().unwrap()).unwrap();
fs::write(&archived, "{}").unwrap();
@@ -537,19 +538,10 @@ fn a_dead_endpoint_under_an_archived_parent_resolves_without_a_tree_walk() {
name: "w".into(),
})
.unwrap();
- assert!(
- ledger.live_edges().unwrap().is_empty(),
- "an archived parent is a dead endpoint"
- );
- assert!(
- ledger
- .replay_cached()
- .unwrap()
- .artifact_index
- .get()
- .is_none(),
- "the archive stat answers without the artifacts walk"
- );
+ let scans = replay::ARTIFACT_SCANS.with(std::cell::Cell::get);
+ assert_eq!(ledger.live_edges().unwrap(), Vec::new());
+ assert_eq!(ledger.live_edges().unwrap(), Vec::new());
+ assert_eq!(replay::ARTIFACT_SCANS.with(std::cell::Cell::get), scans + 1);
}
/// A parent that moved (recorded path gone, same stem under the sessions
@@ -1438,7 +1430,7 @@ fn a_restored_tombstoned_child_stops_billing_the_parent_without_an_append() {
&usage_summary(0.25),
)
.unwrap();
- let archive = crate::session_archive::archive_dir(&dir);
+ let archive = dir.join("sessions-archive");
fs::create_dir_all(&archive).unwrap();
let archived = archive.join(crate::session_store::session_file_name("child"));
fs::rename(&child, &archived).unwrap();
@@ -1449,7 +1441,7 @@ fn a_restored_tombstoned_child_stops_billing_the_parent_without_an_append() {
ledger.deleted_descendant_usage_by_parent().unwrap(),
HashMap::from([(key, usage_summary(0.25))])
);
- crate::session_archive::restore_session(&archive, &sessions, "child").unwrap();
+ fs::rename(&archived, &child).unwrap();
assert_eq!(
ledger.deleted_descendant_usage_by_parent().unwrap(),
HashMap::new()
diff --git a/crates/pa-daemon/src/session_archive.rs b/crates/pa-daemon/src/session_archive.rs
deleted file mode 100644
index eeaaef83ce..0000000000
--- a/crates/pa-daemon/src/session_archive.rs
+++ /dev/null
@@ -1,450 +0,0 @@
-//! Session archiving: retired sessions MOVE (never delete) to
-//! `<agent-dir>/sessions-archive` under two settings rules — age and count
-//! (oldest first). Protected sessions (resident workers, active scheduled jobs)
-//! are counted but never archived; restore moves a session back before its
-//! worker spawns, so every selector still finds it.
-
-use std::collections::HashSet;
-use std::fs;
-use std::path::{Path, PathBuf};
-use std::time::{Duration, SystemTime};
-
-use anyhow::{anyhow, Context, Result};
-use pa_core::settings::SessionArchivePolicy;
-
-use crate::lease::canonical_session_path;
-use crate::session_store::session_file_name;
-
-/// Archive directory name under the agent dir.
-pub const ARCHIVE_DIR_NAME: &str = "sessions-archive";
-
-/// The archive location for one agent dir.
-pub fn archive_dir(agent_dir: &Path) -> PathBuf {
- agent_dir.join(ARCHIVE_DIR_NAME)
-}
-
-/// One sessions-dir file the policy evaluates.
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct SweepCandidate {
- pub path: PathBuf,
- pub session_id: String,
- pub modified: SystemTime,
- /// Resident or scheduled-work-protected: never archived, but counted
- /// toward the count cap.
- pub protected: bool,
-}
-
-/// Sessions the policy retires, oldest first (the order they archive).
-pub fn plan_archive<'a>(
- candidates: &'a [SweepCandidate],
- policy: &SessionArchivePolicy,
- now: SystemTime,
-) -> Vec<&'a SweepCandidate> {
- let max_age = policy
- .max_age_days
- .map(|days| Duration::from_secs(days * 24 * 60 * 60));
- let mut doomed: Vec<&SweepCandidate> = Vec::new();
- for candidate in candidates {
- if candidate.protected {
- continue;
- }
- if let (Some(max_age), Ok(age)) = (max_age, now.duration_since(candidate.modified)) {
- // The boundary is inclusive: a session is archival the moment it
- // reaches the age threshold.
- if age >= max_age {
- doomed.push(candidate);
- }
- }
- }
- if let Some(max_sessions) = policy.max_sessions {
- if candidates.len() > max_sessions {
- // Keep the newest `max_sessions` files by mtime (ties by path
- // for determinism); everything older archives unless protected.
- let mut ranked: Vec<&SweepCandidate> = candidates.iter().collect();
- ranked.sort_by(|left, right| {
- right
- .modified
- .cmp(&left.modified)
- .then_with(|| left.path.cmp(&right.path))
- });
- doomed.extend(ranked[max_sessions..].iter().filter(|c| !c.protected));
- }
- }
- doomed.sort_by(|left, right| {
- left.modified
- .cmp(&right.modified)
- .then_with(|| left.path.cmp(&right.path))
- });
- doomed.dedup_by_key(|candidate| candidate.path.clone());
- doomed
-}
-
-/// One sweep over the sessions directory: collect, plan, move the doomed files
-/// into the archive. Idempotent; a move failure skips that file.
-pub fn sweep_sessions(
- sessions_dir: &Path,
- archive_dir: &Path,
- protected: &HashSet<PathBuf>,
- policy: &SessionArchivePolicy,
- now: SystemTime,
-) -> Result<Vec<String>> {
- if policy.max_age_days.is_none() && policy.max_sessions.is_none() {
- return Ok(Vec::new());
- }
- let candidates = collect_candidates(sessions_dir, protected)?;
- let doomed = plan_archive(&candidates, policy, now);
- if doomed.is_empty() {
- return Ok(Vec::new());
- }
- fs::create_dir_all(archive_dir)
- .with_context(|| format!("create archive dir {}", archive_dir.display()))?;
- let mut archived = Vec::new();
- for candidate in doomed {
- let destination = archive_dir.join(session_file_name(&candidate.session_id));
- if destination.exists() {
- // A same-id file already archived: leave the live file alone
- // rather than clobber history (this guards a corrupted archive).
- continue;
- }
- match move_file(&candidate.path, &destination) {
- Ok(()) => archived.push(candidate.session_id.clone()),
- Err(error) => {
- // A failed move leaves the session in place; the sweep retries on its next
- // pass.
- eprintln!(
- "session archive: could not move {}: {error:#}",
- candidate.path.display()
- );
- }
- }
- }
- Ok(archived)
-}
-
-/// Restore an archived session into the sessions directory (the resume
-/// path): returns the restored file path.
-pub fn restore_session(
- archive_dir: &Path,
- sessions_dir: &Path,
- session_id: &str,
-) -> Result<PathBuf> {
- let source = archive_dir.join(session_file_name(session_id));
- if !source.is_file() {
- return Err(anyhow!(
- "archived session \"{session_id}\" not found in {}",
- archive_dir.display()
- ));
- }
- let destination = sessions_dir.join(session_file_name(session_id));
- if destination.exists() {
- return Err(anyhow!(
- "session \"{session_id}\" already exists in {}",
- sessions_dir.display()
- ));
- }
- fs::create_dir_all(sessions_dir)
- .with_context(|| format!("create sessions dir {}", sessions_dir.display()))?;
- move_file(&source, &destination)?;
- Ok(destination)
-}
-
-/// Rename with a copy+delete fallback (rename fails across filesystems).
-fn move_file(source: &Path, destination: &Path) -> Result<()> {
- if let Ok(()) = fs::rename(source, destination) {
- Ok(())
- } else {
- fs::copy(source, destination)
- .with_context(|| format!("copy {} -> {}", source.display(), destination.display()))?;
- fs::remove_file(source).with_context(|| format!("remove {}", source.display()))?;
- Ok(())
- }
-}
-
-/// One candidate per valid session file directly under the sessions dir
-/// (the archive layout never nests; subdirectories are not sessions).
-fn collect_candidates(
- sessions_dir: &Path,
- protected: &HashSet<PathBuf>,
-) -> Result<Vec<SweepCandidate>> {
- let Ok(read) = fs::read_dir(sessions_dir) else {
- return Ok(Vec::new());
- };
- let mut candidates = Vec::new();
- for entry in read.flatten() {
- let path = entry.path();
- if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
- continue;
- }
- let Some(modified) = fs::metadata(&path).and_then(|m| m.modified()).ok() else {
- continue;
- };
- // A session file is a valid first-line `session` header with an id.
- let Some(header) = crate::session_store::read_session_header(&path) else {
- continue;
- };
- candidates.push(SweepCandidate {
- protected: protected.contains(&canonical_session_path(&path)),
- session_id: header.id,
- path,
- modified,
- });
- }
- Ok(candidates)
-}
-
-// Supervisor sweep seam
-
-/// Sweep cadence: a boot sweep, then a periodic re-sweep.
-const SWEEP_INTERVAL: Duration = Duration::from_mins(5);
-/// The periodic loop sleeps in chunks so a shutdown exits promptly.
-const SWEEP_SLEEP_CHUNK: Duration = Duration::from_secs(5);
-
-/// The daemon's archive-sweep loop: one sweep at boot, then every
-/// [`SWEEP_INTERVAL`]. Failures log and retry (never take the daemon down).
-pub(crate) async fn archive_sweep_loop(supervisor: &std::sync::Arc<crate::supervisor::Supervisor>) {
- loop {
- match run_archive_sweep(supervisor).await {
- Ok(()) => {}
- Err(error) => {
- supervisor.log_line(&format!("session archive sweep failed: {error:#}"));
- }
- }
- let mut remaining = SWEEP_INTERVAL;
- while remaining > Duration::ZERO {
- if supervisor.is_shutting_down() {
- return;
- }
- let chunk = remaining.min(SWEEP_SLEEP_CHUNK);
- tokio::time::sleep(chunk).await;
- remaining -= chunk;
- }
- }
-}
-
-/// One sweep: resolve the policy from the current settings, collect the
-/// protected paths, and move the retired sessions into the archive.
-pub(crate) async fn run_archive_sweep(
- supervisor: &std::sync::Arc<crate::supervisor::Supervisor>,
-) -> Result<()> {
- use std::collections::HashSet as Set;
- use std::path::Path;
-
- let agent_dir = supervisor.options.agent_dir.clone();
- let settings = pa_core::settings::SettingsManager::create(
- std::env::current_dir().unwrap_or_default(),
- &agent_dir,
- );
- let policy = settings.get_session_archive_policy();
- let sessions_dir = crate::paths::sessions_dir(&agent_dir)?;
- let mut protected = Set::new();
- for resident in supervisor.registry.list().await {
- let descriptor = resident.descriptor.lock().await;
- if let Some(session_file) = &descriptor.session_file {
- protected.insert(canonical_session_path(Path::new(session_file)));
- }
- }
- // Active scheduled jobs own their saved session files: a wake target
- // must never move.
- for job in crate::update_roster::scan_scheduled_jobs(&agent_dir) {
- if job.status == pa_core::cron::JobStatus::Active && !job.session_file.is_empty() {
- protected.insert(canonical_session_path(Path::new(&job.session_file)));
- }
- }
- let archived = sweep_sessions(
- &sessions_dir,
- &archive_dir(&agent_dir),
- &protected,
- &policy,
- SystemTime::now(),
- )?;
- if !archived.is_empty() {
- supervisor.log_line(&format!(
- "archived {} session(s) into {}",
- archived.len(),
- archive_dir(&agent_dir).display()
- ));
- supervisor.note_sessions_archived(archived.len());
- }
- Ok(())
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use std::collections::HashSet;
-
- fn candidate(id: &str, days_ago: u64, protected: bool, now: SystemTime) -> SweepCandidate {
- SweepCandidate {
- path: PathBuf::from(format!("/sessions/{id}.jsonl")),
- session_id: id.to_string(),
- modified: now - Duration::from_secs(days_ago * 24 * 60 * 60),
- protected,
- }
- }
-
- fn policy(age: Option<u64>, max_sessions: Option<usize>) -> SessionArchivePolicy {
- SessionArchivePolicy {
- max_age_days: age,
- max_sessions,
- }
- }
-
- #[test]
- fn age_rule_archives_at_and_past_the_boundary() {
- let now = SystemTime::now();
- let candidates = [
- candidate("fresh", 0, false, now),
- candidate("edge", 30, false, now),
- candidate("old", 31, false, now),
- ];
- let doomed = plan_archive(&candidates, &policy(Some(30), None), now);
- let ids: Vec<&str> = doomed.iter().map(|c| c.session_id.as_str()).collect();
- // The boundary is inclusive: 30 days untouched is archival; oldest first.
- assert_eq!(ids, vec!["old", "edge"]);
- }
-
- #[test]
- fn age_rule_off_archives_nothing() {
- let now = SystemTime::now();
- let candidates = [candidate("ancient", 400, false, now)];
- let doomed = plan_archive(&candidates, &policy(None, None), now);
- assert!(doomed.is_empty());
- }
-
- #[test]
- fn count_rule_keeps_the_newest_cap_and_spares_protected() {
- let now = SystemTime::now();
- // Cap 2 keeps the two newest ("a", "b"); the rest archive, except the
- // protected file which is counted toward the cap but never archives.
- let candidates = [
- candidate("a", 1, false, now),
- candidate("b", 2, false, now),
- candidate("c", 3, false, now),
- candidate("protected-old", 9, true, now),
- ];
- let doomed = plan_archive(&candidates, &policy(None, Some(2)), now);
- let ids: Vec<&str> = doomed.iter().map(|c| c.session_id.as_str()).collect();
- assert_eq!(ids, vec!["c"]);
- // The protected file beyond the cap stays too: it ranks into the doomed set but the
- // plan spares it.
- let candidates = [
- candidate("a", 1, false, now),
- candidate("b", 2, false, now),
- candidate("c", 3, false, now),
- candidate("protected-oldest", 4, true, now),
- ];
- let doomed = plan_archive(&candidates, &policy(None, Some(2)), now);
- let ids: Vec<&str> = doomed.iter().map(|c| c.session_id.as_str()).collect();
- assert_eq!(ids, vec!["c"]);
- }
-
- #[test]
- fn count_rule_under_the_cap_archives_nothing() {
- let now = SystemTime::now();
- let candidates = [
- candidate("a", 100, false, now),
- candidate("b", 200, false, now),
- ];
- let doomed = plan_archive(&candidates, &policy(None, Some(5)), now);
- assert!(doomed.is_empty());
- }
-
- #[test]
- fn both_rules_union_without_duplicates() {
- let now = SystemTime::now();
- // "old" hits the age rule; "b" is beyond the count cap but not aged — both go, oldest
- // first.
- let candidates = [
- candidate("a", 0, false, now),
- candidate("b", 29, false, now),
- candidate("old", 31, false, now),
- ];
- let doomed = plan_archive(&candidates, &policy(Some(30), Some(1)), now);
- let ids: Vec<&str> = doomed.iter().map(|c| c.session_id.as_str()).collect();
- assert_eq!(ids, vec!["old", "b"]);
- }
-
- #[test]
- fn sweep_moves_doomed_sessions_and_keeps_the_rest() {
- let dir = tempfile::tempdir().expect("temp dir");
- let sessions = dir.path().join("sessions");
- let archive = dir.path().join("archive");
- fs::create_dir_all(&sessions).expect("sessions dir");
- let fresh = write_session(&sessions, "11111111-1111-1111-1111-111111111111");
- let old = write_session(&sessions, "22222222-2222-2222-2222-222222222222");
- set_mtime_days_ago(&old, 40);
- // A malformed jsonl file is not a session: never moved.
- fs::write(sessions.join("junk.jsonl"), "not a session").expect("junk");
-
- let doomed = sweep_sessions(
- &sessions,
- &archive,
- &HashSet::new(),
- &policy(Some(30), None),
- SystemTime::now(),
- )
- .expect("sweep");
- assert_eq!(doomed, vec!["22222222-2222-2222-2222-222222222222"]);
- assert!(fresh.is_file(), "fresh session stays");
- assert!(sessions.join("junk.jsonl").is_file(), "junk stays");
- let archived_file = archive.join(session_file_name("22222222-2222-2222-2222-222222222222"));
- assert!(archived_file.is_file(), "aged session archived");
- assert!(!old.is_file(), "aged session left the sessions dir");
- }
-
- #[test]
- fn sweep_with_both_rules_off_is_a_noop() {
- let dir = tempfile::tempdir().expect("temp dir");
- let sessions = dir.path().join("sessions");
- fs::create_dir_all(&sessions).expect("sessions dir");
- let old = write_session(&sessions, "33333333-3333-3333-3333-333333333333");
- set_mtime_days_ago(&old, 400);
- let doomed = sweep_sessions(
- &sessions,
- &dir.path().join("archive"),
- &HashSet::new(),
- &policy(None, None),
- SystemTime::now(),
- )
- .expect("sweep");
- assert!(doomed.is_empty());
- assert!(old.is_file());
- }
-
- #[test]
- fn restore_round_trips_an_archived_session() {
- let dir = tempfile::tempdir().expect("temp dir");
- let sessions = dir.path().join("sessions");
- let archive = dir.path().join("archive");
- fs::create_dir_all(&archive).expect("archive dir");
- let id = "44444444-4444-4444-4444-444444444444";
- let archived = write_session(&archive, id);
- let restored = restore_session(&archive, &sessions, id).expect("restore");
- assert!(restored.is_file());
- assert!(sessions.join(session_file_name(id)).is_file());
- assert!(!archived.is_file(), "left the archive");
- // Restoring again misses: the file is live again.
- assert!(restore_session(&archive, &sessions, id).is_err());
- // A missing id is a typed miss.
- assert!(restore_session(&archive, &sessions, "missing").is_err());
- }
-
- /// A minimal valid session file: the `session` header line carries the
- /// id, and the file name matches it (the sessions-dir layout).
- fn write_session(dir: &Path, id: &str) -> PathBuf {
- let path = dir.join(session_file_name(id));
- let header = serde_json::json!({
- "type": "session",
- "id": id,
- "cwd": "/work",
- "timestamp": "2026-01-01T00:00:00.000Z",
- });
- fs::write(&path, format!("{header}\n")).expect("write session");
- path
- }
-
- fn set_mtime_days_ago(path: &Path, days: u64) {
- let mtime = filetime::FileTime::from_unix_time((days * 24 * 60 * 60) as i64, 0);
- filetime::set_file_mtime(path, mtime).expect("set mtime");
- }
-}
diff --git a/crates/pa-daemon/src/session_catalog.rs b/crates/pa-daemon/src/session_catalog.rs
index e2170a4df8..6873d3084c 100644
--- a/crates/pa-daemon/src/session_catalog.rs
+++ b/crates/pa-daemon/src/session_catalog.rs
@@ -7,8 +7,7 @@ use std::path::Path;
use anyhow::{anyhow, Context, Result};
-use crate::session_archive::restore_session;
-use crate::session_store::{list_sessions, SessionInfo};
+use crate::session_store::{list_sessions, session_file_name, SessionInfo};
/// One selector match against saved sessions: the session id is addressable
/// by prefix, the name only exactly; more than one match is ambiguous.
@@ -28,9 +27,9 @@ fn catalog_session_match<'a>(
}
/// Resolve one selector to a saved session: cwd-scoped sessions first, the
-/// whole catalog second; a live miss falls back to the archive, whose match
-/// RESTORES first so the wake spawns over the live path. Misses are `None`
-/// (the caller answers its unknown-target error, not a catalog one).
+/// whole live catalog second. A live miss lazily restores a match from
+/// `sessions-archive/` (written by older Rust releases) before waking its worker. Misses are `None` (the caller
+/// answers its unknown-target error, not a catalog one).
pub(crate) fn resolve_saved_session(
sessions_dir: &Path,
archive_dir: &Path,
@@ -46,22 +45,42 @@ pub(crate) fn resolve_saved_session(
return Ok(Some(info.clone()));
}
let archived = list_sessions(archive_dir);
- let archived_local =
- catalog_session_match(archived.iter().filter(|info| info.cwd == cwd), selector)?;
- let matched = match archived_local {
+ let local = catalog_session_match(archived.iter().filter(|info| info.cwd == cwd), selector)?;
+ let matched = match local {
Some(info) => Some(info),
None => catalog_session_match(archived.iter(), selector)?,
};
- match matched {
- Some(info) => {
- let restored = restore_session(archive_dir, sessions_dir, &info.id)
- .with_context(|| format!("restore archived session {}", info.id))?;
- let mut info = info.clone();
- info.path = restored;
- Ok(Some(info))
- }
- None => Ok(None),
- }
+ let Some(info) = matched else {
+ return Ok(None);
+ };
+ let destination = sessions_dir.join(session_file_name(&info.id));
+ std::fs::create_dir_all(sessions_dir)?;
+ // Stage in the live directory so custom sessions dirs on another
+ // filesystem work too. Publish a complete file without replacing any
+ // live path, even one the catalog could not parse.
+ let temp = sessions_dir.join(format!(".restore-{}.tmp", uuid::Uuid::new_v4()));
+ let mut options = std::fs::OpenOptions::new();
+ options.write(true).create_new(true);
+ pa_core::platform::perms::set_private_mode(&mut options);
+ let mut target = options.open(&temp)?;
+ let result = (|| -> Result<()> {
+ let mut source = std::fs::File::open(&info.path)?;
+ std::io::copy(&mut source, &mut target)?;
+ target.set_times(std::fs::FileTimes::new().set_modified(source.metadata()?.modified()?))?;
+ target.sync_all()?;
+ Ok(())
+ })();
+ drop(target);
+ let result = result
+ .and_then(|()| std::fs::hard_link(&temp, &destination).context("publish restored session"));
+ let cleanup = std::fs::remove_file(&temp);
+ result.with_context(|| format!("restore archived session {}", info.id))?;
+ cleanup.with_context(|| format!("remove restore staging file {}", temp.display()))?;
+ std::fs::remove_file(&info.path)
+ .with_context(|| format!("unlink restored archive {}", info.path.display()))?;
+ let mut restored = info.clone();
+ restored.path = destination;
+ Ok(Some(restored))
}
#[cfg(test)]
@@ -93,13 +112,12 @@ mod tests {
#[test]
fn resolves_by_id_prefix_and_exact_name() {
let dir = temp_dir();
- let archive = dir.join("archive");
let (id, _path) = write_session(&dir, "/work/a", Some("alpha"));
- let found = resolve_saved_session(&dir, &archive, &id[..8], "/work/a")
+ let found = resolve_saved_session(&dir, &dir.join("archive"), &id[..8], "/work/a")
.unwrap()
.expect("id prefix resolves");
assert_eq!(found.id, id);
- let found = resolve_saved_session(&dir, &archive, "alpha", "/work/a")
+ let found = resolve_saved_session(&dir, &dir.join("archive"), "alpha", "/work/a")
.unwrap()
.expect("name resolves");
assert_eq!(found.id, id);
@@ -109,15 +127,14 @@ mod tests {
#[test]
fn local_scope_wins_and_global_is_the_fallback() {
let dir = temp_dir();
- let archive = dir.join("archive");
let (id, _path) = write_session(&dir, "/work/a", Some("alpha"));
// A different cwd still resolves through the global pass.
- let found = resolve_saved_session(&dir, &archive, "alpha", "/work/other")
+ let found = resolve_saved_session(&dir, &dir.join("archive"), "alpha", "/work/other")
.unwrap()
.expect("global fallback");
assert_eq!(found.id, id);
// The local pass serves the session's own cwd.
- let found = resolve_saved_session(&dir, &archive, &id[..8], "/work/a")
+ let found = resolve_saved_session(&dir, &dir.join("archive"), &id[..8], "/work/a")
.unwrap()
.expect("local pass");
assert_eq!(found.cwd, "/work/a");
@@ -127,68 +144,113 @@ mod tests {
#[test]
fn ambiguous_selectors_carry_the_ts_error() {
let dir = temp_dir();
- let archive = dir.join("archive");
write_session(&dir, "/work/a", Some("alpha"));
write_session(&dir, "/work/a", Some("alpha"));
- let error = resolve_saved_session(&dir, &archive, "alpha", "/work/a").unwrap_err();
+ let error =
+ resolve_saved_session(&dir, &dir.join("archive"), "alpha", "/work/a").unwrap_err();
assert_eq!(error.to_string(), "Ambiguous session selector \"alpha\"");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
- fn an_archived_match_restores_into_the_sessions_dir() {
- let dir = temp_dir();
- let archive = dir.join("archive");
+ fn archived_roots_restore_by_id_prefix_or_name_and_resolve_again() {
+ for selector_index in 0..3 {
+ let root = tempfile::TempDir::new().unwrap();
+ let sessions = root.path().join("sessions");
+ let archive = root.path().join("sessions-archive");
+ std::fs::create_dir_all(&archive).unwrap();
+ let (id, path) = write_session(&archive, "/work/a", Some("alpha"));
+ let bytes = std::fs::read(&path).unwrap();
+ let modified = std::fs::metadata(&path).unwrap().modified().unwrap();
+ let mut expected = crate::session_store::read_session_info(&path).unwrap();
+ expected.path = sessions.join(session_file_name(&id));
+ let selector = [id.as_str(), &id[..8], "alpha"][selector_index];
+ // A global lookup must still recover a root created before upgrade.
+ let restored = resolve_saved_session(&sessions, &archive, selector, "/other")
+ .unwrap()
+ .expect("archived selector resolves");
+ assert_eq!(restored, expected);
+ assert_eq!(std::fs::read(&restored.path).unwrap(), bytes);
+ assert_eq!(
+ std::fs::metadata(&restored.path)
+ .unwrap()
+ .modified()
+ .unwrap(),
+ modified
+ );
+ assert!(!path.exists(), "restored file leaves the archive");
+ let again = resolve_saved_session(&sessions, &archive, selector, "/other")
+ .unwrap()
+ .expect("restored session resolves again");
+ assert_eq!(again.id, id);
+ assert_eq!(again.path, restored.path);
+ assert_eq!(std::fs::read(&again.path).unwrap(), bytes);
+ }
+ }
+
+ #[test]
+ fn a_restore_never_clobbers_an_unreadable_live_session() {
+ let root = tempfile::TempDir::new().unwrap();
+ let sessions = root.path().join("sessions");
+ let archive = root.path().join("sessions-archive");
+ std::fs::create_dir_all(&sessions).unwrap();
std::fs::create_dir_all(&archive).unwrap();
let (id, path) = write_session(&archive, "/work/a", Some("alpha"));
- // The archived file restores and resolves with its live path.
- let found = resolve_saved_session(&dir, &archive, "alpha", "/work/a")
- .unwrap()
- .expect("archived name resolves");
- assert_eq!(found.id, id);
- assert_eq!(found.path, dir.join(session_file_name(&id)));
- assert!(dir.join(session_file_name(&id)).is_file(), "restored live");
- assert!(!path.is_file(), "left the archive");
- // After the restore the live catalog serves the selector, and a second resolve
- // does not fail (no stale archive row).
- let again = resolve_saved_session(&dir, &archive, "alpha", "/work/a")
- .unwrap()
- .expect("live again");
- assert_eq!(again.path, dir.join(session_file_name(&id)));
+ let bytes = std::fs::read(&path).unwrap();
+ let live_path = sessions.join(session_file_name(&id));
+ let live_bytes = b"unparseable live session bytes\n";
+ std::fs::write(&live_path, live_bytes).unwrap();
+ for _ in 0..2 {
+ assert!(resolve_saved_session(&sessions, &archive, "alpha", "/work/a").is_err());
+ assert_eq!(std::fs::read(&live_path).unwrap(), live_bytes);
+ assert_eq!(std::fs::read(&path).unwrap(), bytes);
+ let remaining: Vec<_> = std::fs::read_dir(&sessions)
+ .unwrap()
+ .map(|entry| entry.unwrap().path())
+ .collect();
+ assert_eq!(remaining, vec![live_path.clone()]);
+ }
}
#[test]
fn live_matches_win_over_archived_ones() {
- let dir = temp_dir();
- let archive = dir.join("archive");
+ let root = tempfile::TempDir::new().unwrap();
+ let sessions = root.path().join("sessions");
+ let archive = root.path().join("sessions-archive");
+ std::fs::create_dir_all(&sessions).unwrap();
std::fs::create_dir_all(&archive).unwrap();
- let (live_id, _path) = write_session(&dir, "/work/a", Some("alpha"));
- let (archived_id, _path) = write_session(&archive, "/work/a", Some("alpha"));
- let found = resolve_saved_session(&dir, &archive, "alpha", "/work/a")
+ let (live_id, _) = write_session(&sessions, "/other", Some("alpha"));
+ let (_, archived_path) = write_session(&archive, "/work/a", Some("alpha"));
+ let bytes = std::fs::read(&archived_path).unwrap();
+ let found = resolve_saved_session(&sessions, &archive, "alpha", "/work/a")
.unwrap()
- .expect("resolves");
+ .expect("live global match wins over archived local match");
assert_eq!(found.id, live_id);
- assert!(archive.join(session_file_name(&archived_id)).is_file());
- assert!(dir.join(session_file_name(&live_id)).is_file());
+ assert_eq!(std::fs::read(&archived_path).unwrap(), bytes);
}
#[test]
- fn an_ambiguous_archive_selector_carries_the_ts_error() {
- let dir = temp_dir();
- let archive = dir.join("archive");
+ fn archived_matches_keep_local_scope_and_ambiguity_rules() {
+ let root = tempfile::TempDir::new().unwrap();
+ let sessions = root.path().join("sessions");
+ let archive = root.path().join("sessions-archive");
std::fs::create_dir_all(&archive).unwrap();
- write_session(&archive, "/work/a", Some("alpha"));
- write_session(&archive, "/work/a", Some("alpha"));
- let error = resolve_saved_session(&dir, &archive, "alpha", "/work/a").unwrap_err();
+ let (local_id, _) = write_session(&archive, "/work/a", Some("alpha"));
+ let (_, other_path) = write_session(&archive, "/other", Some("alpha"));
+ let error = resolve_saved_session(&sessions, &archive, "alpha", "/third").unwrap_err();
assert_eq!(error.to_string(), "Ambiguous session selector \"alpha\"");
+ let found = resolve_saved_session(&sessions, &archive, "alpha", "/work/a")
+ .unwrap()
+ .expect("local archived match wins");
+ assert_eq!(found.id, local_id);
+ assert!(other_path.is_file(), "unselected archives stay in place");
}
#[test]
fn a_miss_is_none_not_an_error() {
let dir = temp_dir();
- let archive = dir.join("archive");
write_session(&dir, "/work/a", Some("alpha"));
- let found = resolve_saved_session(&dir, &archive, "ghost", "/work/a").unwrap();
+ let found = resolve_saved_session(&dir, &dir.join("archive"), "ghost", "/work/a").unwrap();
assert!(found.is_none());
let _ = std::fs::remove_dir_all(&dir);
}
diff --git a/crates/pa-daemon/src/supervisor.rs b/crates/pa-daemon/src/supervisor.rs
index 6614642f05..3f211454e3 100644
--- a/crates/pa-daemon/src/supervisor.rs
+++ b/crates/pa-daemon/src/supervisor.rs
@@ -467,16 +467,6 @@ impl Supervisor {
});
}
- // Session-archive sweep (roadmap: the sessions directory must not
- // grow forever): boot sweep, then the periodic re-sweep at the TS
- // idle-eviction cadence. Housekeeping only — it never gates serving.
- {
- let supervisor = Arc::clone(&self);
- tokio::spawn(async move {
- crate::session_archive::archive_sweep_loop(&supervisor).await;
- });
- }
-
// Journals without verifiable ownership and flat TS update status
// records stay intact: a missing descriptor or a dead coordinator
// does not prove that no worker or waiting caller still needs them.
diff --git a/crates/pa-daemon/src/supervisor/notes.rs b/crates/pa-daemon/src/supervisor/notes.rs
index 4f990e8f47..97d4baf0d1 100644
--- a/crates/pa-daemon/src/supervisor/notes.rs
+++ b/crates/pa-daemon/src/supervisor/notes.rs
@@ -48,17 +48,6 @@ impl Supervisor {
(switch.enabled)()
}
- /// Emit the `daemon event` adoption signal for a session-archive sweep
- /// (best-effort, non-blocking; no-op when the daemon is opted out).
- pub(crate) fn note_sessions_archived(&self, count: usize) {
- if !self.telemetry_recording_on() {
- return;
- }
- if let Some(client) = &*self.telemetry.lock().unwrap() {
- pa_core::session_engine::telemetry::track_sessions_archived(client, count);
- }
- }
-
/// Emit the parent-death child close's `daemon event`: a count only. Zero closes
/// never emit (no children died with the worker).
pub(crate) fn note_children_closed(&self, count: usize) {
diff --git a/crates/pa-daemon/tests/saved_catalog_stream_e2e.rs b/crates/pa-daemon/tests/saved_catalog_stream_e2e.rs
index ad95e6bfe1..8be2b10d06 100644
--- a/crates/pa-daemon/tests/saved_catalog_stream_e2e.rs
+++ b/crates/pa-daemon/tests/saved_catalog_stream_e2e.rs
@@ -330,11 +330,6 @@ fn a_paged_catalog_folds_the_newest_page_and_the_cursor_reaches_the_oldest() {
let agent_dir = dir.path().join("agent");
let sessions_dir = agent_dir.join("sessions");
std::fs::create_dir_all(&sessions_dir).expect("sessions dir");
- std::fs::write(
- agent_dir.join("settings.json"),
- r#"{"sessionArchiveMaxSessions":"off"}"#,
- )
- .expect("write settings");
let base = SystemTime::now() - Duration::from_hours(24);
for index in 0..FILES {
write_fixture(
diff --git a/crates/pa-daemon/tests/saved_session_wake_e2e.rs b/crates/pa-daemon/tests/saved_session_wake_e2e.rs
index f963b5b110..009e48a11e 100644
--- a/crates/pa-daemon/tests/saved_session_wake_e2e.rs
+++ b/crates/pa-daemon/tests/saved_session_wake_e2e.rs
@@ -240,8 +240,23 @@ fn messages(client: &mut Client, id: &str, active_session_id: &str) -> String {
serde_json::to_string(&response["data"]).expect("messages json")
}
+#[derive(Clone, Copy)]
+enum SavedLocation {
+ Live,
+ Archive,
+}
+
#[test]
fn send_to_a_saved_session_wakes_it_and_runs_the_turn() {
+ saved_session_wake(SavedLocation::Live);
+}
+
+#[test]
+fn send_to_an_archived_root_restores_it_and_runs_the_turn() {
+ saved_session_wake(SavedLocation::Archive);
+}
+
+fn saved_session_wake(location: SavedLocation) {
let dir = tempfile::TempDir::new().expect("temp dir");
let agent_dir = dir.path().join("agent");
let sessions = agent_dir.join("sessions");
@@ -271,7 +286,7 @@ fn send_to_a_saved_session_wakes_it_and_runs_the_turn() {
)
.expect("write models.json");
let socket = dir.path().join("daemon.sock");
- let _daemon = spawn_daemon(&socket, &agent_dir);
+ let daemon = spawn_daemon(&socket, &agent_dir);
let (mut client, hello) = Client::connect(&socket);
assert_eq!(hello["type"], "daemon_hello");
@@ -344,6 +359,26 @@ fn send_to_a_saved_session_wakes_it_and_runs_the_turn() {
assert_eq!(saved_rows.len(), 1, "the killed session is saved");
assert_eq!(saved_rows[0]["name"], "alpha");
+ // Simulate upgrading a store archived by an older Rust daemon. The
+ // current daemon starts with the root already in its archive directory.
+ let archive_path = agent_dir
+ .join("sessions-archive")
+ .join(format!("{session_id}.jsonl"));
+ let live_path = sessions.join(format!("{session_id}.jsonl"));
+ let saved_bytes = std::fs::read(&live_path).expect("saved transcript");
+ let (_daemon, mut client) = match location {
+ SavedLocation::Live => (daemon, client),
+ SavedLocation::Archive => {
+ drop(client);
+ drop(daemon);
+ std::fs::create_dir_all(archive_path.parent().unwrap()).expect("archive dir");
+ std::fs::rename(&live_path, &archive_path).expect("pre-upgrade archive fixture");
+ let daemon = spawn_daemon(&socket, &agent_dir);
+ let (client, _) = Client::connect(&socket);
+ (daemon, client)
+ }
+ };
+
client.send_command(
"s1",
&json!({ "type": "send_message", "targetActiveSessionId": "alpha", "message": "wake up" }),
@@ -358,6 +393,11 @@ fn send_to_a_saved_session_wakes_it_and_runs_the_turn() {
.expect("woken active id")
.to_string();
assert_ne!(woken_id, active_id, "a new worker hosts the woken session");
+ assert!(!archive_path.exists(), "woken root is in the live store");
+ assert!(
+ std::fs::read(&live_path).unwrap().starts_with(&saved_bytes),
+ "wake preserves the complete pre-upgrade transcript"
+ );
let listed = wait_until(Duration::from_secs(15), || {
client.send_command("l3", &json!({ "type": "list" }));
diff --git a/crates/pa-daemon/tests/session_archive_e2e.rs b/crates/pa-daemon/tests/session_archive_e2e.rs
deleted file mode 100644
index f4de39ecde..0000000000
--- a/crates/pa-daemon/tests/session_archive_e2e.rs
+++ /dev/null
@@ -1,478 +0,0 @@
-//! Session-archive e2e: the boot sweep archives aged sessions into
-//! `<agent-dir>/sessions-archive` (default 30 days), spares fresh and
-//! job-pinned ones, and the catalog excludes the archived rows; an archived
-//! session resumes through the wake path (TS parity).
-// Stack-resident futures by design on the daemon's hot paths.
-#![allow(clippy::large_futures)]
-// Narrowing casts sit at OS boundaries (pid/fd/time/size) where the kernel
-// bounds the values.
-#![allow(
- clippy::cast_possible_truncation,
- clippy::cast_possible_wrap,
- clippy::cast_sign_loss,
- clippy::cast_precision_loss
-)]
-// Style gate only, not correctness.
-#![allow(clippy::too_many_lines)]
-// API-shape opinions, not defects; the surfaces are deliberate.
-#![allow(
- clippy::unnecessary_wraps,
- clippy::zero_sized_map_values,
- clippy::struct_excessive_bools,
- clippy::struct_field_names
-)]
-#![cfg(unix)]
-
-use std::fmt::Write as _;
-use std::io::{BufRead, BufReader, Read, Write};
-use std::net::{TcpListener, TcpStream};
-use std::os::unix::net::UnixStream;
-use std::path::{Path, PathBuf};
-use std::process::{Child, Command, Stdio};
-use std::time::{Duration, Instant};
-
-use serde_json::{json, Value};
-
-struct Daemon {
- child: Child,
-}
-
-impl Drop for Daemon {
- fn drop(&mut self) {
- let _ = self.child.kill();
- let _ = self.child.wait();
- }
-}
-
-/// One always-200 SSE answer per request (the mock the woken session turns
-/// against).
-fn spawn_mock(answer: &'static str) -> PathBuf /* url */ {
- let listener = TcpListener::bind("127.0.0.1:0").expect("bind mock");
- let url = format!(
- "http://127.0.0.1:{}/v1",
- listener.local_addr().unwrap().port()
- );
- std::thread::spawn(move || {
- for stream in listener.incoming() {
- let Ok(stream) = stream else { continue };
- std::thread::spawn(move || {
- let _ = serve(stream, answer);
- });
- }
- });
- PathBuf::from(url)
-}
-
-fn chunk(delta: &Value, finish_reason: Option<&str>) -> String {
- json!({
- "id": "chatcmpl-archive",
- "object": "chat.completion.chunk",
- "created": 1_750_000_000,
- "model": "mock-1",
- "choices": [{"index": 0, "delta": delta, "finish_reason": finish_reason}],
- })
- .to_string()
-}
-
-fn serve(mut stream: TcpStream, answer: &str) -> std::io::Result<()> {
- let mut reader = BufReader::new(stream.try_clone()?);
- let mut content_length = 0usize;
- loop {
- let mut line = String::new();
- if reader.read_line(&mut line)? == 0 {
- return Ok(());
- }
- if line == "\r\n" {
- break;
- }
- let lower = line.to_ascii_lowercase();
- if let Some(value) = lower.strip_prefix("content-length:") {
- content_length = value.trim().parse().unwrap_or_default();
- }
- }
- let mut body = vec![0u8; content_length];
- if content_length > 0 {
- reader.read_exact(&mut body)?;
- }
- let mut payload = String::new();
- for data in [
- chunk(&json!({"role": "assistant", "content": answer}), None),
- chunk(&json!({}), Some("stop")),
- ] {
- write!(payload, "data: {data}\n\n").expect("write to String");
- }
- payload.push_str("data: [DONE]\n\n");
- stream.write_all(
- format!("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n{payload}")
- .as_bytes(),
- )
-}
-
-#[allow(clippy::zombie_processes)]
-fn spawn_daemon(socket: &Path, agent_dir: &Path) -> Daemon {
- let child = Command::new(env!("CARGO_BIN_EXE_pa-daemon"))
- .arg("supervisor")
- .arg("--socket")
- .arg(socket)
- .arg("--agent-dir")
- .arg(agent_dir)
- .env("PRIME_AGENT_MODEL_PROVIDER", "prime-inference")
- .env("PRIME_AGENT_MODEL", "mock-1")
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- // A supervisor killed at teardown must not leak its session workers
- // into later test binaries: the worker's supervisor-lost exit runs
- // on this short window instead of the 5-minute default.
- .env(
- pa_daemon::worker::WORKER_SUPERVISOR_LOST_EXIT_MS_ENV,
- "15000",
- )
- .spawn()
- .expect("spawn pa-daemon supervisor");
- Daemon { child }
-}
-
-struct Client {
- reader: BufReader<UnixStream>,
- writer: UnixStream,
-}
-
-impl Client {
- /// Connect once the supervisor accepts (a restarted supervisor parks
- /// on the stale socket file before replacing it, so file existence is
- /// not readiness).
- fn connect(socket: &Path) -> (Self, Value) {
- let deadline = Instant::now() + Duration::from_secs(15);
- let stream = loop {
- match UnixStream::connect(socket) {
- Ok(stream) => break stream,
- Err(_) if Instant::now() < deadline => {
- std::thread::sleep(Duration::from_millis(20));
- }
- Err(error) => panic!("connect supervisor: {error}"),
- }
- };
- let writer = stream.try_clone().expect("clone socket");
- let mut client = Client {
- reader: BufReader::new(stream),
- writer,
- };
- let hello = client.read_line();
- (client, hello)
- }
-
- fn send_command(&mut self, id: &str, command: &Value) {
- let envelope = json!({
- "type": "command",
- "id": id,
- "protocol": { "name": "prime-agent.daemon", "version": 7 },
- "command": command,
- });
- let mut line = serde_json::to_string(&envelope).expect("serialize command");
- line.push('\n');
- self.writer.write_all(line.as_bytes()).expect("send");
- self.writer.flush().expect("flush");
- }
-
- fn read_line(&mut self) -> Value {
- let mut line = String::new();
- let deadline = Instant::now() + Duration::from_secs(30);
- self.reader
- .get_mut()
- .set_read_timeout(Some(Duration::from_millis(100)))
- .expect("set timeout");
- loop {
- match self.reader.read_line(&mut line) {
- Ok(0) => panic!("supervisor closed the connection"),
- Ok(_) if line.trim().is_empty() => line.clear(),
- Ok(_) => return serde_json::from_str(line.trim()).expect("parse line"),
- Err(error) => {
- assert!(
- Instant::now() < deadline,
- "timed out waiting for supervisor line: {error}"
- );
- }
- }
- }
- }
-
- fn read_response(&mut self, id: &str) -> Value {
- let deadline = Instant::now() + Duration::from_mins(1);
- loop {
- assert!(Instant::now() < deadline, "no response for id {id}");
- let line = self.read_line();
- if line.get("id").and_then(Value::as_str) == Some(id) {
- return line;
- }
- }
- }
-}
-
-fn wait_until<T>(deadline: Duration, mut probe: impl FnMut() -> Option<T>) -> T {
- let deadline = Instant::now() + deadline;
- loop {
- if let Some(value) = probe() {
- return value;
- }
- assert!(Instant::now() < deadline, "condition never became true");
- std::thread::sleep(Duration::from_millis(50));
- }
-}
-
-fn write_fixture(sessions_dir: &Path, name: &str) -> String {
- let mut session = pa_daemon::session_store::SessionFile::create("/work", None, 0);
- let id = session.session_id().to_string();
- session.append_session_info(name);
- session.append_message(&serde_json::json!({
- "role": "user", "content": "hi", "timestamp": 1u64
- }));
- let path = sessions_dir.join(format!("{id}.jsonl"));
- session.set_path(path);
- session.rewrite().expect("write fixture session");
- id
-}
-
-/// Backdate a file's mtime (the age rule keys on it).
-fn age_days(path: &Path, days: u64) {
- let seconds = days * 24 * 60 * 60;
- let mtime = filetime::FileTime::from_unix_time(seconds as i64, 0);
- filetime::set_file_mtime(path, mtime).expect("set mtime");
-}
-
-/// An active scheduled job pinning one session file (the disk analogue of
-/// TS `hasRegisteredCronJob`): the sweep must never archive its target.
-fn pin_with_scheduled_job(agent_dir: &Path, session_id: &str, session_file: &Path) {
- let partition = agent_dir.join("session-artifacts").join(session_id);
- std::fs::create_dir_all(&partition).expect("artifacts partition");
- // No `nextRunAt`: the job is never due, so the boot re-arm never wakes
- // it; its active status alone pins the file.
- let job = json!({
- "id": "job-pin",
- "status": "active",
- "activeSessionId": session_id,
- "sessionId": session_id,
- "sessionFile": session_file.to_string_lossy(),
- "cwd": "/work",
- "prompt": "tick",
- "schedule": { "kind": "interval", "expression": "every 1h", "intervalMs": 3_600_000 },
- "createdAt": "2026-01-01T00:00:00.000Z",
- "updatedAt": "2026-01-01T00:00:00.000Z",
- });
- std::fs::write(
- partition.join("scheduled-jobs.json"),
- serde_json::to_string(&json!({ "jobs": [job], "dispatches": [] })).expect("job json"),
- )
- .expect("write scheduled-jobs.json");
-}
-
-fn saved_rows(client: &mut Client, id: &str, cwd: &Path) -> Vec<Value> {
- client.send_command(
- id,
- &json!({ "type": "list_saved_sessions", "cwd": cwd.to_string_lossy() }),
- );
- loop {
- let line = client.read_line();
- if line.get("id").and_then(Value::as_str) == Some(id) {
- if let Some(sessions) = line["data"]["sessions"].as_array() {
- return sessions.clone();
- }
- }
- }
-}
-
-fn log_mentions(agent_dir: &Path, needle: &str) -> bool {
- let Ok(entries) = std::fs::read_dir(agent_dir.join("logs")) else {
- return false;
- };
- entries
- .flatten()
- .map(|entry| entry.path())
- .any(|path| std::fs::read_to_string(&path).is_ok_and(|content| content.contains(needle)))
-}
-
-#[test]
-fn boot_sweep_archives_aged_sessions_and_the_catalog_excludes_them() {
- let dir = tempfile::tempdir().expect("temp dir");
- let agent_dir = dir.path().join("agent");
- let sessions = agent_dir.join("sessions");
- std::fs::create_dir_all(&sessions).expect("sessions dir");
-
- let aged_one = write_fixture(&sessions, "aged-one");
- let aged_two = write_fixture(&sessions, "aged-two");
- let fresh = write_fixture(&sessions, "fresh-one");
- let pinned = write_fixture(&sessions, "pinned-old");
- age_days(&sessions.join(format!("{aged_one}.jsonl")), 40);
- age_days(&sessions.join(format!("{aged_two}.jsonl")), 40);
- age_days(&sessions.join(format!("{pinned}.jsonl")), 40);
- pin_with_scheduled_job(
- &agent_dir,
- &pinned,
- &sessions.join(format!("{pinned}.jsonl")),
- );
-
- let socket = dir.path().join("daemon.sock");
- let _daemon = spawn_daemon(&socket, &agent_dir);
- let (mut client, hello) = Client::connect(&socket);
- assert_eq!(hello["type"], "daemon_hello");
-
- let archive = agent_dir.join("sessions-archive");
- wait_until(Duration::from_secs(10), || {
- (archive.join(format!("{aged_one}.jsonl")).is_file()
- && archive.join(format!("{aged_two}.jsonl")).is_file())
- .then_some(())
- });
- assert!(!sessions.join(format!("{aged_one}.jsonl")).is_file());
- assert!(!sessions.join(format!("{aged_two}.jsonl")).is_file());
- assert!(
- sessions.join(format!("{fresh}.jsonl")).is_file(),
- "fresh session stays live"
- );
- assert!(
- sessions.join(format!("{pinned}.jsonl")).is_file(),
- "scheduled-job sessions are never archived"
- );
- assert!(
- !archive.join(format!("{fresh}.jsonl")).is_file()
- && !archive.join(format!("{pinned}.jsonl")).is_file(),
- "only the aged sessions archived"
- );
-
- let rows = saved_rows(&mut client, "l1", dir.path());
- let names: Vec<&str> = rows.iter().filter_map(|row| row["name"].as_str()).collect();
- assert_eq!(names, vec!["fresh-one", "pinned-old"], "rows: {rows:?}");
-
- wait_until(Duration::from_secs(5), || {
- log_mentions(&agent_dir, "archived 2 session(s)").then_some(())
- });
-}
-
-#[test]
-fn an_archived_session_resumes_through_the_wake() {
- let dir = tempfile::tempdir().expect("temp dir");
- let agent_dir = dir.path().join("agent");
- let sessions = agent_dir.join("sessions");
- std::fs::create_dir_all(&sessions).expect("sessions dir");
- let url = spawn_mock("archive reply");
- std::fs::write(
- agent_dir.join("models.json"),
- json!({
- "providers": {
- "prime-inference": {
- "api": "openai-completions",
- "baseUrl": url.to_string_lossy(),
- "apiKey": "sk-archive",
- "models": [
- {
- "id": "mock-1",
- "name": "Mock 1",
- "api": "openai-completions",
- "contextWindow": 128_000,
- "maxTokens": 4096
- }
- ]
- }
- }
- })
- .to_string(),
- )
- .expect("write models.json");
-
- let socket = dir.path().join("daemon.sock");
- let first = spawn_daemon(&socket, &agent_dir);
- let session_id = {
- let (mut client, hello) = Client::connect(&socket);
- assert_eq!(hello["type"], "daemon_hello");
- client.send_command(
- "c1",
- &json!({
- "type": "create",
- "name": "beta",
- "config": {
- "cwd": dir.path().to_string_lossy(),
- "sessionDir": sessions.to_string_lossy(),
- "provider": "prime-inference",
- "model": "mock-1",
- },
- }),
- );
- let created = client.read_response("c1");
- assert_eq!(created["success"], true, "create failed: {created}");
- let active_id = created["data"]["id"]
- .as_str()
- .or_else(|| created["data"]["activeSessionId"].as_str())
- .expect("active id")
- .to_string();
- let session_id = created["data"]["sessionId"]
- .as_str()
- .expect("session id")
- .to_string();
- client.send_command(
- "p1",
- &json!({
- "type": "prompt_and_wait",
- "activeSessionId": active_id,
- "message": "first turn",
- }),
- );
- assert_eq!(
- client.read_response("p1")["success"],
- true,
- "first turn failed"
- );
- client.send_command(
- "k1",
- &json!({ "type": "kill", "activeSessionId": active_id }),
- );
- assert_eq!(client.read_response("k1")["success"], true, "kill failed");
- session_id
- };
- drop(first);
-
- let saved = sessions.join(format!("{session_id}.jsonl"));
- assert!(
- saved.is_file(),
- "killed session saved at {}",
- saved.display()
- );
- age_days(&saved, 40);
- let _second = spawn_daemon(&socket, &agent_dir);
- let archive = agent_dir.join("sessions-archive");
- let (mut client, hello) = Client::connect(&socket);
- assert_eq!(hello["type"], "daemon_hello");
- wait_until(Duration::from_secs(10), || {
- archive
- .join(format!("{session_id}.jsonl"))
- .is_file()
- .then_some(())
- });
- assert!(!saved.is_file(), "aged session left the sessions dir");
-
- client.send_command(
- "s1",
- &json!({ "type": "send_message", "targetActiveSessionId": "beta", "message": "wake up" }),
- );
- let sent = client.read_response("s1");
- assert_eq!(sent["success"], true, "send by name failed: {sent}");
- let receipt = &sent["data"];
- assert_eq!(receipt["deliveryStatus"], "delivered", "{receipt}");
- let woken_id = receipt["target"]["activeSessionId"]
- .as_str()
- .expect("woken active id")
- .to_string();
-
- wait_until(Duration::from_secs(10), || saved.is_file().then_some(()));
- wait_until(Duration::from_secs(30), || {
- client.send_command(
- "gm1",
- &json!({ "type": "get_messages", "activeSessionId": woken_id }),
- );
- let response = client.read_response("gm1");
- assert_eq!(response["success"], true, "get_messages failed");
- let text = serde_json::to_string(&response["data"]).expect("messages json");
- text.contains("archive reply").then_some(text)
- });
- assert!(
- !archive.join(format!("{session_id}.jsonl")).is_file(),
- "restore left the archive"
- );
-}
diff --git a/crates/pa-perf/README.md b/crates/pa-perf/README.md
index 5a46a4036e..9bf07b3d49 100644
--- a/crates/pa-perf/README.md
+++ b/crates/pa-perf/README.md
@@ -92,12 +92,6 @@ records (`parentSession`/`child` are canonical paths).
the warm scan via the daemon's in-process cache instead.
- `pa-perf run` generates BOTH corpora (cold for the cold scenarios, warm
for warm-start/warm-ready) — every scenario runs on the state it needs.
-- The corpus settings disable BOTH archive rules (`sessionArchiveMaxAgeDays`
- / `sessionArchiveMaxSessions` "off"): the product's defaults sweep the
- live store down to 200 unarchived sessions (the operator's real store:
- 196 live + 211 archived), so large/xl with archiving off are scaling
- stress profiles, not typical stores, and the boot sweep itself is not
- measured.
## The bench harness
diff --git a/crates/pa-perf/src/corpus/mod.rs b/crates/pa-perf/src/corpus/mod.rs
index 5e0b208a6f..dce1d8b520 100644
--- a/crates/pa-perf/src/corpus/mod.rs
+++ b/crates/pa-perf/src/corpus/mod.rs
@@ -125,7 +125,7 @@ const TASKS: [&str; 16] = [
/// note.)
const BASE_MS: u64 = 1_790_000_000_000;
/// The mtime step between sessions (roster order is mtime-desc; the
-/// spread is realism only — the archive-off settings are the sweep guard).
+/// spread is realism only).
const MTIME_STEP_MS: u64 = 720_000;
/// The header-timestamp hour step (a session's rows spread over an hour).
const HOUR_MS: u64 = 3_600_000;
@@ -232,17 +232,11 @@ pub fn generate(options: &CorpusOptions) -> anyhow::Result<Manifest> {
}
let cwd = project_dir.display().to_string();
- // Onboarding is pre-answered, traces stay off, and BOTH archive rules
- // are disabled: the daemon's boot sweep would otherwise retire the
- // corpus's own sessions (the defaults keep 200 files / 30 days) and
- // move them out from under the bench's manifest baseline.
std::fs::write(
agent_dir.join("settings.json"),
serde_json::json!({
"onboardingShown": true,
"agentTraces": { "enabled": false },
- "sessionArchiveMaxAgeDays": "off",
- "sessionArchiveMaxSessions": "off",
})
.to_string(),
)?;
diff --git a/crates/pa-perf/tests/corpus_verification.rs b/crates/pa-perf/tests/corpus_verification.rs
index dfdcdcea48..ace7796ed2 100644
--- a/crates/pa-perf/tests/corpus_verification.rs
+++ b/crates/pa-perf/tests/corpus_verification.rs
@@ -301,28 +301,6 @@ fn the_switch_query_can_only_match_switch_b() {
assert_eq!(scanned, options.sessions, "every session checked");
}
-/// The corpus settings disable BOTH archive rules: the daemon's boot sweep
-/// would otherwise retire corpus sessions out from under the manifest
-/// baseline (the defaults keep 200 files / 30 days).
-#[test]
-fn the_archive_policy_is_off() {
- let dir = tempfile::tempdir().expect("temp dir");
- let options = tiny(Profile::Small, dir.path());
- let manifest = generate(&options).expect("generate");
- let settings: serde_json::Value = serde_json::from_str(
- &std::fs::read_to_string(manifest.paths.agent_dir.join("settings.json")).expect("settings"),
- )
- .expect("a settings object");
- assert_eq!(
- settings["sessionArchiveMaxAgeDays"],
- serde_json::json!("off")
- );
- assert_eq!(
- settings["sessionArchiveMaxSessions"],
- serde_json::json!("off")
- );
-}
-
/// The corpus manifest records the generated file set (the bench's
/// per-run restore baseline) and the archive dir holds the archived files.
#[test]