Files
wanderbooks/crates/ledger-host/src/knobs.rs
T
swaitsandClaude Fable 5.2 2ff2913274 fix(host): what holds the books is known, not guessed; a command joins what starts beside it
Plan P15 unit A5, fix round. A server answering its admin socket with no host
over it was taken to be "the app". Three wrong outcomes followed: a `serve`
started while a killed host's server was still finishing its last backup was
told "the app has these books open" and exited, `close` and `doctor` said "the
app" during such a close, and a command attached to another command's short-lived
server as if it were the app (refused "not open" for another company, or its
server closed under the other).

A server now says what it is. `wanderbooks server --started-by app|host|command`
writes run/server.info beside its lock (who started it, its pid, whether it has
begun to close), moved into place whole as soon as the lock is its, rewritten
`closing` the moment it is told to close, removed just before the lock goes. It
is read only while the lock is held, which is when it is the lock holder's. A
file beside the lock, not a field in the admin hello: it also covers a server
that has no admin socket yet (opening) and costs the older admin protocol
nothing. A server that says nothing (an earlier build's) is taken for the app's,
as before. `opener::what_holds` reads it: only a server the app started, not
closing and answering, is the app. A command's server, or one that is closing
(the app's, or the orphan of a host that went), is waited for, said in words,
bounded by `open_wait` (a bridge waits as long as its session does), and the
holder or command goes on once it is gone. `close`, `doctor` and the bridge say
the true thing for each.

A command racing another program's start no longer fails with "close it" or waits
silently 300 s: if its own server loses the lock to a program that started after
its look, it lets its start go and joins what runs (`wait_ready_unless_beaten`,
`reach`), inside one `open_wait`.

A command's own server is started clean, as a host is: the host's environment
only, the root as an absolute path, `/` as its folder (Windows: `run/`), and
every server closes the descriptors above 2 at its start, so one that outlives
its command (still closing after 90 s) holds none of the invoker's pipes. The
app's own child keeps its environment: it is the window's child, closes with the
window and needs the user's session (ssh agent, git configuration) for backups;
it too loses stray descriptors, since the server does that for any starter.

Test knobs: `--start-delay` (a server's slow start) and `timer_hold=1` for a
command's own server, following `--timer-hold`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 12:12:28 -07:00

191 lines
7.4 KiB
Rust

//! The host's and bridge's timings, which tests shorten (`--test-knobs`).
//!
//! Every mode that starts another passes its knobs on, so a test sets
//! them once, on the holder it starts. Nothing else sets them: the
//! defaults are the plan's (3.9).
use std::time::Duration;
/// The timings in force.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Knobs {
/// How long the host waits after the last lease ends before it
/// closes the books (plan: 10 s).
pub close_after: Duration,
/// How often the host looks whether its executable was replaced.
pub exe_poll: Duration,
/// The first wait before a holder starts a failed host again; it
/// doubles each time (plan: 1, 2, 4 … 60 s, five tries).
pub backoff: Duration,
/// How long the host waits before starting the server (tests of a
/// slow start only; 0 otherwise).
pub server_delay: Duration,
/// How long the bridge waits for the server before answering
/// `initialize` itself (plan: 5 s).
pub init_wait: Duration,
/// How long the bridge holds a call while the books open (plan: 60 s).
pub queue_wait: Duration,
/// How long a command waits for the books to open.
pub open_wait: Duration,
/// How long the books stay unwritten before the server backs them up
/// (plan 3.7: 30 s), passed to the server as `--backup-quiet`.
pub backup_quiet: Duration,
/// How long after books open the server first looks for a drill due
/// (60 s), passed to the server as `--drill-first`.
pub drill_first: Duration,
/// How long between the host's looks for a newer release (plan
/// P11a step 4: a day).
pub update_check: Duration,
/// A command's own server holds the jobs it runs inside their phases
/// while `run/timer-hold` names them (the server's `--timer-hold`;
/// tests of what a command holds while it works). Given as
/// `timer_hold=1`.
pub timer_hold: bool,
}
impl Default for Knobs {
fn default() -> Self {
Self {
close_after: Duration::from_secs(10),
exe_poll: Duration::from_secs(5),
backoff: Duration::from_secs(1),
server_delay: Duration::ZERO,
init_wait: Duration::from_secs(5),
queue_wait: Duration::from_secs(60),
open_wait: Duration::from_secs(120),
backup_quiet: Duration::from_secs(30),
drill_first: Duration::from_secs(60),
update_check: Duration::from_secs(24 * 60 * 60),
timer_hold: false,
}
}
}
/// The flag that carries them.
pub const FLAG: &str = "--test-knobs";
impl Knobs {
/// Read `name=millis,...` (any subset; the rest stay default).
pub fn parse(text: &str) -> Result<Self, String> {
let mut k = Self::default();
for pair in text.split(',').filter(|p| !p.is_empty()) {
let (name, value) = pair
.split_once('=')
.ok_or_else(|| format!("{FLAG}: {pair:?} isn't name=milliseconds"))?;
let ms: u64 = value
.parse()
.map_err(|_| format!("{FLAG}: {value:?} isn't a number of milliseconds"))?;
let d = Duration::from_millis(ms);
match name {
"close_after" => k.close_after = d,
"exe_poll" => k.exe_poll = d,
"backoff" => k.backoff = d,
"server_delay" => k.server_delay = d,
"init_wait" => k.init_wait = d,
"queue_wait" => k.queue_wait = d,
"open_wait" => k.open_wait = d,
"backup_quiet" => k.backup_quiet = d,
"drill_first" => k.drill_first = d,
"update_check" => k.update_check = d,
"timer_hold" => k.timer_hold = ms != 0,
other => return Err(format!("{FLAG}: there is no knob {other:?}")),
}
}
Ok(k)
}
/// The flag's value that gives these knobs, or `None` for the
/// defaults (then nothing is passed on).
pub fn to_arg(&self) -> Option<String> {
let d = Self::default();
let mut parts = Vec::new();
for (name, now, default) in [
("close_after", self.close_after, d.close_after),
("exe_poll", self.exe_poll, d.exe_poll),
("backoff", self.backoff, d.backoff),
("server_delay", self.server_delay, d.server_delay),
("init_wait", self.init_wait, d.init_wait),
("queue_wait", self.queue_wait, d.queue_wait),
("open_wait", self.open_wait, d.open_wait),
("backup_quiet", self.backup_quiet, d.backup_quiet),
("drill_first", self.drill_first, d.drill_first),
("update_check", self.update_check, d.update_check),
] {
if now != default {
parts.push(format!("{name}={}", now.as_millis()));
}
}
if self.timer_hold {
parts.push("timer_hold=1".to_string());
}
(!parts.is_empty()).then(|| parts.join(","))
}
/// The arguments that pass these knobs on to another mode.
pub fn args(&self) -> Vec<String> {
match self.to_arg() {
Some(v) => vec![FLAG.to_string(), v],
None => Vec::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_defaults_are_the_plans() {
let k = Knobs::default();
assert_eq!(k.close_after, Duration::from_secs(10));
assert_eq!(k.backoff, Duration::from_secs(1));
assert_eq!(k.init_wait, Duration::from_secs(5));
assert_eq!(k.queue_wait, Duration::from_secs(60));
assert_eq!(k.exe_poll, Duration::from_secs(5));
assert_eq!(k.server_delay, Duration::ZERO);
assert_eq!(k.open_wait, Duration::from_secs(120));
assert_eq!(k.backup_quiet, Duration::from_secs(30));
assert_eq!(k.drill_first, Duration::from_secs(60));
assert_eq!(k.update_check, Duration::from_secs(86_400));
assert_eq!(k.to_arg(), None);
assert!(k.args().is_empty());
assert_eq!(Knobs::parse("").unwrap(), k);
}
#[test]
fn knobs_pass_on_exactly() {
let k = Knobs::parse(
"close_after=500,backoff=20,server_delay=3000,init_wait=100,queue_wait=900,exe_poll=50,open_wait=70,backup_quiet=40,drill_first=50,update_check=60",
)
.unwrap();
assert_eq!(k.close_after, Duration::from_millis(500));
assert_eq!(k.backoff, Duration::from_millis(20));
assert_eq!(k.server_delay, Duration::from_millis(3000));
assert_eq!(k.init_wait, Duration::from_millis(100));
assert_eq!(k.queue_wait, Duration::from_millis(900));
assert_eq!(k.exe_poll, Duration::from_millis(50));
assert_eq!(k.open_wait, Duration::from_millis(70));
assert_eq!(k.backup_quiet, Duration::from_millis(40));
assert_eq!(k.drill_first, Duration::from_millis(50));
assert_eq!(k.update_check, Duration::from_millis(60));
let args = k.args();
assert_eq!(args[0], FLAG);
assert_eq!(Knobs::parse(&args[1]).unwrap(), k);
let one = Knobs::parse("close_after=1").unwrap();
assert_eq!(one.to_arg().as_deref(), Some("close_after=1"));
}
#[test]
fn a_bad_knob_is_refused() {
for bad in [
"close_after",
"close_after=x",
"nope=1",
"close_after=-1",
"token_file=1",
] {
assert!(Knobs::parse(bad).is_err(), "{bad}");
}
}
}