Make liveness thresholds configurable and cover the reaper end-to-end

Move the reaper's silence timeout and scan cadence into DaemonConfig
(defaulting to the existing 60s/15s constants) with a
`with_browser_liveness` builder, so tests can drive the task in
sub-second windows.

Add an integration test that runs a real daemon and asserts a silent
heartbeat-capable browser — and its sessions — get reaped, while a
legacy browser that never heartbeated is left untouched.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
drakezhang
2026-07-22 13:06:17 +08:00
co-authored by Cursor
parent 31d2a1b3dc
commit 29ff239726
2 changed files with 135 additions and 4 deletions
+24 -4
View File
@@ -23,7 +23,7 @@ use tracing::{debug, info, warn};
use crate::cli::daemon::StartArgs;
use crate::daemon::{
browsers::EXTENSION_CONNECT_WAIT,
browsers::{BROWSER_LIVENESS_TICK, BROWSER_LIVENESS_TIMEOUT, EXTENSION_CONNECT_WAIT},
info as daemon_info, ipc, lockfile, paths,
sessions::{StopSessionError, forget_session, stop_session},
state::DaemonState,
@@ -45,6 +45,12 @@ pub struct DaemonConfig {
/// How long `session.start` polls for an extension handshake before
/// returning `no_browser_connected`.
pub extension_connect_wait: Duration,
/// A heartbeat-capable browser silent for at least this long is
/// reaped by the liveness task. Defaults to [`BROWSER_LIVENESS_TIMEOUT`].
pub browser_liveness_timeout: Duration,
/// How often the liveness task scans the registry. Defaults to
/// [`BROWSER_LIVENESS_TICK`].
pub browser_liveness_tick: Duration,
}
impl DaemonConfig {
@@ -58,6 +64,8 @@ impl DaemonConfig {
daemon_idle: Duration::from_secs(60 * 30),
allow_any_origin: false,
extension_connect_wait: EXTENSION_CONNECT_WAIT,
browser_liveness_timeout: BROWSER_LIVENESS_TIMEOUT,
browser_liveness_tick: BROWSER_LIVENESS_TICK,
}
}
@@ -65,6 +73,15 @@ impl DaemonConfig {
self.extension_connect_wait = wait;
self
}
/// Override the liveness reaper's silence threshold and scan cadence.
/// Primarily for tests that need the reaper to act within
/// sub-second windows instead of the production 60s/15s defaults.
pub fn with_browser_liveness(mut self, timeout: Duration, tick: Duration) -> Self {
self.browser_liveness_timeout = timeout;
self.browser_liveness_tick = tick;
self
}
}
impl From<&StartArgs> for DaemonConfig {
@@ -75,6 +92,8 @@ impl From<&StartArgs> for DaemonConfig {
daemon_idle: args.resolved_daemon_idle(),
allow_any_origin: false,
extension_connect_wait: EXTENSION_CONNECT_WAIT,
browser_liveness_timeout: BROWSER_LIVENESS_TIMEOUT,
browser_liveness_tick: BROWSER_LIVENESS_TICK,
}
}
}
@@ -391,16 +410,17 @@ pub fn run_foreground(cfg: DaemonConfig) -> Result<()> {
pub(crate) fn spawn_browser_liveness_reaper(
state: Arc<DaemonState>,
) -> tokio::task::JoinHandle<()> {
use crate::daemon::browsers::{BROWSER_LIVENESS_TICK, BROWSER_LIVENESS_TIMEOUT};
let timeout = state.config.browser_liveness_timeout;
let tick = state.config.browser_liveness_tick;
tokio::spawn(async move {
let mut ticker = tokio::time::interval(BROWSER_LIVENESS_TICK);
let mut ticker = tokio::time::interval(tick);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
// The first tick is immediate; skip it so a freshly connected
// browser always gets at least one full window before a scan.
ticker.tick().await;
loop {
ticker.tick().await;
for client in state.browsers.stale_browsers(BROWSER_LIVENESS_TIMEOUT) {
for client in state.browsers.stale_browsers(timeout) {
if state
.browsers
.remove_if_generation_matches(&client.id, client.generation)