This repository has been archived on 2026-10-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
maestro/crates/maestro-daemon/tests/common/mod.rs
T
Miguel PalhasandClaude Opus 5 5a47f6fbf6 feat(maestro): persist PTY scrollback across a reboot
A resumed session opened on a blank pane: the agent's conversation came
back, the terminal history the operator was reading did not. Each PTY
bridge's replay ring is now mirrored to `<state_dir>/scrollback/<id>.bin`
and handed to the new bridge on resume, and on the recovery pass that
re-adopts a session tmux still holds — that one lost its ring with the
previous daemon too. Restored bytes go out ahead of the live ones, and
are dropped once the ring holds a full budget of new output of its own.

Flushed every 10 seconds by the liveness watcher, and once more when a
bridge is sealed on exit. The timer is the part that matters: a host
reboot runs no exit path at all, so a scrollback written only on a clean
shutdown is missing in exactly the case this exists for. Ten seconds
bounds the loss to the last few lines — this is visual continuity, not
state — and costs at most one file write per active session per tick,
since a session whose bridge has read nothing since the last flush is
skipped.

The file is capped at the existing `pty_replay_bytes` budget rather than
a second one, is deleted on kill and when recovery prunes a sidecar, and
a missing, unreadable or oversized one degrades to an empty pane instead
of failing the resume.

Closes #171

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-04 09:37:53 +01:00

219 lines
6.7 KiB
Rust

//! Shared helpers for the maestro-daemon integration tests.
//!
//! Each integration test file compiles as its own binary and only uses a
//! subset of these helpers, so `dead_code` warnings are spurious here.
#![allow(dead_code)]
use std::future::Future;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use maestro_daemon::sidecar::SidecarStore;
use maestro_daemon::spawn_history::SpawnHistory;
use maestro_daemon::tmux::Tmux;
use maestro_daemon::worktree::WorktreeManager;
use maestro_daemon::SessionManager;
use tempfile::TempDir;
use uuid::Uuid;
/// An isolated tmux instance scoped to a temp `TMUX_TMPDIR` and unique socket
/// name. Drops kill the server so leftover sockets don't accumulate.
pub struct TmuxFixture {
pub tmux: Tmux,
_home: TempDir,
_tmpdir: TempDir,
}
impl TmuxFixture {
pub fn new() -> Self {
let home = TempDir::new().expect("tempdir HOME");
let tmpdir = TempDir::new().expect("tempdir TMUX_TMPDIR");
let socket = format!("maestro-test-{}", Uuid::new_v4().simple());
let tmux = Tmux::new()
.with_socket(socket)
.with_tmpdir(tmpdir.path().to_path_buf());
Self {
tmux,
_home: home,
_tmpdir: tmpdir,
}
}
}
impl Drop for TmuxFixture {
fn drop(&mut self) {
let _ = self.tmux.kill_server();
}
}
/// Wait until `cond` returns `true`, polling every 25ms. Panics on timeout.
pub fn wait_for(timeout: Duration, mut cond: impl FnMut() -> bool) {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
if cond() {
return;
}
thread::sleep(Duration::from_millis(25));
}
panic!("timed out after {timeout:?} waiting for condition");
}
/// `git init` a fresh repo in `dir` with one commit so it can host worktrees.
pub fn init_repo(dir: &Path) {
run(Command::new("git")
.arg("-C")
.arg(dir)
.args(["init", "-q", "-b", "main"]));
run(Command::new("git")
.arg("-C")
.arg(dir)
.args(["config", "user.email", "test@example.com"]));
run(Command::new("git")
.arg("-C")
.arg(dir)
.args(["config", "user.name", "Test"]));
std::fs::write(dir.join("README"), b"hello\n").unwrap();
run(Command::new("git").arg("-C").arg(dir).args(["add", "."]));
run(Command::new("git")
.arg("-C")
.arg(dir)
.args(["commit", "-q", "-m", "initial"]));
}
fn run(cmd: &mut Command) {
let out = cmd.output().expect("git spawn");
assert!(
out.status.success(),
"git command failed: {} stderr={}",
out.status,
String::from_utf8_lossy(&out.stderr)
);
}
/// An empty bare repository at `dir`, usable as a push target (#79).
pub fn init_bare_repo(dir: &Path) {
run(Command::new("git")
.arg("init")
.arg("-q")
.arg("--bare")
.arg(dir.to_str().expect("non-utf8 bare repo path")));
}
/// Push every branch of `repo` to `origin`, creating remote-tracking refs.
///
/// The unsaved-work probe (#79) counts commits reachable from no
/// remote-tracking ref as unpushed, so tests that want a killable worktree
/// must give its branch one. Safe to call again: re-adding an existing
/// remote is ignored.
pub fn push_all_to_origin(repo: &Path, origin: &Path) {
let _ = Command::new("git")
.arg("-C")
.arg(repo)
.args([
"remote",
"add",
"origin",
origin.to_str().expect("non-utf8 origin path"),
])
.status();
run(Command::new("git")
.arg("-C")
.arg(repo)
.args(["push", "-q", "--all", "origin"]));
}
/// Async polling sibling of [`wait_for`] for tokio tests.
pub async fn wait_for_async<F, Fut>(timeout: Duration, mut cond: F)
where
F: FnMut() -> Fut,
Fut: Future<Output = bool>,
{
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
if cond().await {
return;
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
panic!("timed out after {timeout:?} waiting for condition");
}
/// Bundle of dirs + a SessionManager hooked to an isolated tmux instance.
/// Holds the `TempDir`s so they survive the test lifetime.
pub struct DaemonFixture {
pub tmux: TmuxFixture,
pub manager: SessionManager,
pub state_dir: TempDir,
}
impl DaemonFixture {
pub fn new() -> Self {
Self::build(None, maestro_daemon::pty::DEFAULT_REPLAY_BYTES)
}
/// Variant whose manager runs a custom PTY replay budget (NAP-229), which
/// also caps the scrollback it persists (#171).
pub fn new_with_replay(replay_bytes: usize) -> Self {
Self::build(None, replay_bytes)
}
/// Variant whose manager has a durable spawn history (#24) attached, with
/// `retention_days` recency cap, logging to `<state_dir>/spawn-history.jsonl`.
pub fn new_with_history(retention_days: u32) -> Self {
Self::build(
Some(retention_days),
maestro_daemon::pty::DEFAULT_REPLAY_BYTES,
)
}
fn build(history_days: Option<u32>, replay_bytes: usize) -> Self {
let tmux = TmuxFixture::new();
let state_dir = TempDir::new().unwrap();
let sidecar = SidecarStore::new(state_dir.path().join("sessions"));
// Default placement: worktrees land under their base repo (#56).
let worktree_mgr = WorktreeManager::new(None);
let mut manager = SessionManager::new_with_replay(
Arc::new(tmux.tmux.clone()),
sidecar,
worktree_mgr,
state_dir.path().join("fifos"),
replay_bytes,
);
// #171: production wires this, so the fixtures do too — every test
// then exercises the same flush/restore/delete paths the daemon runs.
manager = manager.with_scrollback(state_dir.path().join("scrollback"));
if let Some(days) = history_days {
manager =
manager.with_spawn_history(SpawnHistory::new(Self::history_path(&state_dir), days));
}
Self {
tmux,
manager,
state_dir,
}
}
fn history_path(state_dir: &TempDir) -> PathBuf {
state_dir.path().join("spawn-history.jsonl")
}
pub fn spawn_history_path(&self) -> PathBuf {
Self::history_path(&self.state_dir)
}
pub fn sidecar_dir(&self) -> PathBuf {
self.state_dir.path().join("sessions")
}
pub fn fifo_dir(&self) -> PathBuf {
self.state_dir.path().join("fifos")
}
pub fn scrollback_dir(&self) -> PathBuf {
self.state_dir.path().join("scrollback")
}
}