diff --git a/Cargo.lock b/Cargo.lock index ee82e1b..19973ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8070,6 +8070,8 @@ dependencies = [ "cognition_plan", "glam", "recreation_towers", + "serde", + "serde_json", "simulation_sitl", ] diff --git a/domains/recreation/README.md b/domains/recreation/README.md index 1911d1e..4169293 100644 --- a/domains/recreation/README.md +++ b/domains/recreation/README.md @@ -180,6 +180,42 @@ the drones on their last orders until a person comes back). A seat that watches whole game. At the end the server prints `RESULT` lines (the outcome, drones in AGENT, refused commands, crashes, towers each team took). +#### Advisors beside a commander + +A commander has an advisor that looks at the team's map in its own time and proposes moves +(`recreation_towers::Advise`: a `Proposal` is a drone, an order and queue, the points it expects +to gain and the game time until which it is good). At a seat, the advisors do not run in the +commander's loop: each runs on a thread of its own at the station behind a mailbox +(`recreation_towers_ground::advisors::Runner`: the newest map and hand go in, older ones that were +not looked at are dropped, proposals come out when they are ready). The computer commander puts +what has arrived in hand with `Commander::advise` before each decision, takes the best proposal +that gains more than 2 points, is not stale and still fits, and then shows the advisors the map +and its hand. It never waits. Which advisor each computer team has: + +```bash +towers_server --blue-advisor lookahead --red-advisor none # the default is lookahead on both +towers_server --blue-advisor "towers_advisor --claude" # asks claude -p every 8 s; costs money +towers_commander --team blue --advisor "towers_advisor" # the same flag on the stand-alone commander +``` + +`lookahead` is the lookahead on a thread of the station, `none` no advisor, and anything else a +command (run with `sh -c`; `towers_advisor` is found beside the other binaries) that serves an +advisor: one JSON line in for each map, `{"Look": {"view": TeamView, "hand": Hand}}`, one line out, +a list of proposals (`[]` for none), after one `{"Start": {"team": "Blue", "rules": {...}}}` that is +answered `"ok"` (`recreation_towers_ground::advisors::{ToAdvisor, serve}`; any `Advise` can be such a +process). A process that dies, hangs (90 s), writes what is not a list of proposals or proposes for a +drone or tower that is not in the game is dropped with a line on stderr, and the game goes on. The +binary `towers_advisor` serves the lookahead; `towers_advisor --claude [--model NAME] [--every S]` +every few seconds gives `claude -p` (JSON output, a budget of 0.25 dollars a question, no tools, the +command from `SIM001_CLAUDE` if set) a text picture of the map and the hand, asks for at most two +proposals as JSON, reads them strictly and stamps `until` itself. The server's log shows each +proposal that arrives and each that a commander takes (`advice Blue: ...`), and the final `RESULT` +lines count them (`proposals`, `proposals_taken`). + +A person's seat runs advisors too (`advisors::attend`, in the commander app, `--advisor` as above, +lookahead by default): the app's ADVISOR panel lists the live proposals, best first, and a click on +one, or the key Y for the first, sends that order and queue for that drone. + The rules and the commander are specified in [expectations/09-towers](../../expectations/09-towers) and run with no window by `cargo test --release -p recreation_towers`; a whole game with ArduPilot, with the fog checked from a seat against a spectator, by diff --git a/domains/recreation/towers/src/advice.rs b/domains/recreation/towers/src/advice.rs index fa8d1f5..411c019 100644 --- a/domains/recreation/towers/src/advice.rs +++ b/domains/recreation/towers/src/advice.rs @@ -11,9 +11,10 @@ use crate::lookahead::{self, HORIZON}; use crate::{Belief, Order, Queued, Rules, Team, TeamView}; +use serde::{Deserialize, Serialize}; /// A proposal: one order for one drone, with what the advisor expects to gain by it. -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Proposal { /// The drone, by its number in the team. pub drone: usize, @@ -32,7 +33,7 @@ pub struct Proposal { /// What a commander has in hand when an advisor looks: the part of its own state that an advisor /// needs and that the map does not show. A seat has it, for the seat is the commander. -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Hand { /// The order each drone is to fly, by its number in the team. pub orders: Vec>, @@ -44,7 +45,7 @@ pub struct Hand { /// Looks at the team's map and proposes moves. It may be slow; it is called when the map is shown /// to it, and what it answers reaches the commander later. -pub trait Advise: std::fmt::Debug { +pub trait Advise: std::fmt::Debug + Send { /// The proposals on this map, if any. Called with each map the advisor is shown (the /// commander shows it one each second); it decides for itself how often it thinks. fn advise(&mut self, view: &TeamView, hand: &Hand) -> Vec; diff --git a/domains/recreation/towers/src/commander.rs b/domains/recreation/towers/src/commander.rs index b79dfb3..c2306dd 100644 --- a/domains/recreation/towers/src/commander.rs +++ b/domains/recreation/towers/src/commander.rs @@ -105,6 +105,8 @@ pub struct Commander { advice_margin: f32, /// The queue that goes with the order a proposal gave a drone, while it still flies it. queues: Vec)>>, + /// What the commander had in hand at its last decision (for advisors beside it). + hand: Option, } impl Commander { @@ -127,6 +129,7 @@ impl Commander { proposals: vec![], advice_margin: ADVICE_MARGIN, queues: vec![None; n], + hand: None, } .with_lookahead(std::env::var("SIM001_LOOKAHEAD").as_deref() != Ok("0")) } @@ -183,6 +186,14 @@ impl Commander { } } + /// What the commander had in hand at its last decision: its orders after its own rules, the + /// drones it may still change and the towers each must stay away from. It is what an advisor + /// that runs beside the commander (at a seat) is shown with the map. `None` before the first + /// decision. + pub fn hand(&self) -> Option<&Hand> { + self.hand.as_ref() + } + pub fn tuning(&self) -> CommanderTuning { self.tuning } @@ -356,8 +367,9 @@ impl Commander { .min_by(|&&a, &&b| view.towers[a].at.distance(at).total_cmp(&view.towers[b].at.distance(at))); *order = spare.map(|&t| Order::Take(t)); } + let free: Vec = (0..self.orders.len()).map(|k| up[k] && charge[k].is_none()).collect(); + self.hand = Some(Hand { orders: self.orders.clone(), free: free.clone(), away: away.clone() }); if !self.advisors.is_empty() || !self.late.is_empty() || !self.proposals.is_empty() { - let free: Vec = (0..self.orders.len()).map(|k| up[k] && charge[k].is_none()).collect(); self.take_advice(view, free, away); } for (k, order) in charge.into_iter().enumerate() { @@ -435,6 +447,18 @@ mod tests { (0..3).map(|k| c.order(k)).collect() } + #[test] + fn after_a_decision_it_has_a_hand_for_the_advisors_beside_it() { + let g = Game::new(Rules { drones: 3, ..Rules::default() }); + let mut c = Commander::new(g.rules).with_lookahead(false); + assert!(c.hand().is_none(), "no hand before the first decision"); + c.decide(&g.view(Team::Blue)); + let hand = c.hand().expect("a hand"); + assert_eq!(hand.orders, (0..3).map(|k| c.order(k)).collect::>()); + assert_eq!(hand.free.len(), 3); + assert_eq!(hand.away.len(), 3); + } + #[test] fn a_tuning_goes_to_numbers_and_back_and_refuses_what_is_out_of_bounds() { let d = CommanderTuning::DEFAULT; diff --git a/domains/recreation/towers_ground/Cargo.toml b/domains/recreation/towers_ground/Cargo.toml index e4d0cfa..5e43db4 100644 --- a/domains/recreation/towers_ground/Cargo.toml +++ b/domains/recreation/towers_ground/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" edition = "2024" description = "Recreation domain: the ground of Towers: the MAVLink side of a team's drones (AGENT mode, plans, standing orders, traffic) behind a Transport, as the commander app runs it and as the game server runs it for a computer team" +[[bin]] +name = "towers_advisor" +path = "src/bin/towers_advisor.rs" + [[bin]] name = "towers_commander" path = "src/bin/towers_commander.rs" @@ -14,3 +18,5 @@ simulation_sitl = { path = "../../simulation/sitl" } cognition_agent = { path = "../../cognition/agent" } cognition_plan = { path = "../../cognition/plan" } glam.workspace = true +serde.workspace = true +serde_json.workspace = true diff --git a/domains/recreation/towers_ground/src/advisors.rs b/domains/recreation/towers_ground/src/advisors.rs new file mode 100644 index 0000000..a0ae4f1 --- /dev/null +++ b/domains/recreation/towers_ground/src/advisors.rs @@ -0,0 +1,548 @@ +//! Advisors beside a commander, at its ground station (plans/actors.md, step 4). +//! +//! An advisor ([`recreation_towers::Advise`]) is slow, and it is not in the commander's loop. Here +//! each one runs on a thread of its own, behind a mailbox ([`Runner`]): the newest map and hand go +//! in (an older one that was not looked at is dropped, never queued), and proposals come out when the +//! advisor has them. The commander only ever does two quick things: take what has arrived +//! ([`Advisors::take`], then `Commander::advise`) and show the advisors the map it decided on +//! ([`Advisors::show`]). Proposals that are stale when they arrive are dropped on the way. +//! +//! An advisor can also be a program of its own ([`ProcessAdvisor`]): the station starts a command +//! and speaks JSON lines with it (see [`ToAdvisor`]); [`serve`] is the other end, for any `Advise`. +//! A process that dies, hangs or writes nonsense is dropped with a line on stderr, and the game +//! goes on. A person's seat runs advisors the same way ([`attend`]) and the app lists what they +//! propose. + +use crate::seat::{Seat, Status}; +use recreation_towers::seat::Join; +use recreation_towers::{Advise, Hand, Lookahead, Order, Phase, Proposal, Rules, Team, TeamView}; +use serde::{Deserialize, Serialize}; +use std::io::{BufRead, Write}; +use std::process::{Child, Command as Process, Stdio}; +use std::sync::mpsc::{Receiver, RecvTimeoutError, channel}; +use std::sync::{Arc, Condvar, Mutex}; +use std::time::Duration; + +/// How long an advisor in a process has to answer one map before it is dropped. +pub const ANSWER_WITHIN: Duration = Duration::from_secs(90); +/// How long a process has to answer the start. +pub const START_WITHIN: Duration = Duration::from_secs(20); + +/// What the station says to an advisor in a process, one JSON line each. The answer to `Start` is +/// the line `"ok"`; the answer to `Look` is a list of [`Proposal`]s (maybe empty). +#[derive(Serialize, Deserialize)] +pub enum ToAdvisor { + /// Once, at the start: the rules of the game and the team the advisor is for. + Start { team: Team, rules: Rules }, + /// The newest map of the team and what its commander has in hand. + Look { view: Box, hand: Hand }, +} + +/// Which advisor a commander has beside it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum AdvisorChoice { + /// The lookahead, on a thread of the station (the default). + Lookahead, + /// None: the commander does not think ahead. + None, + /// A command, started with `sh -c`, that serves an advisor ([`serve`]; `towers_advisor`). + Command(String), +} + +impl std::str::FromStr for AdvisorChoice { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.trim() { + "" => Err("an advisor is lookahead, none or a command".into()), + "lookahead" => Ok(AdvisorChoice::Lookahead), + "none" => Ok(AdvisorChoice::None), + command => Ok(AdvisorChoice::Command(command.into())), + } + } +} + +impl AdvisorChoice { + /// The advisors of this choice for `team`, each on a thread of its own. A command that does not + /// start gives none, with a line on stderr. + pub fn start(&self, rules: Rules, team: Team) -> Advisors { + let mut advisors = Advisors::default(); + match self { + AdvisorChoice::None => {} + AdvisorChoice::Lookahead => advisors.add(Runner::start("lookahead", Box::new(Lookahead::new(rules)))), + AdvisorChoice::Command(command) => match ProcessAdvisor::spawn(command, rules, team) { + Ok(p) => advisors.add(Runner::start(command, Box::new(p))), + Err(e) => eprintln!("advisor {command}: not started: {e}"), + }, + } + advisors + } +} + +struct Slot { + next: Option<(TeamView, Hand)>, + out: Vec, + stop: bool, + alive: bool, +} + +struct Shared { + slot: Mutex, + wake: Condvar, +} + +/// An advisor on a thread of its own, behind a mailbox. +pub struct Runner { + name: String, + shared: Arc, +} + +impl Runner { + /// Starts `advisor` on a thread. It looks at each map it is shown, one at a time. + pub fn start(name: impl Into, mut advisor: Box) -> Runner { + let name = name.into(); + let shared = Arc::new(Shared { + slot: Mutex::new(Slot { next: None, out: vec![], stop: false, alive: true }), + wake: Condvar::new(), + }); + let (s, label) = (shared.clone(), name.clone()); + std::thread::Builder::new() + .name(format!("advisor-{name}")) + .spawn(move || { + loop { + let (view, hand) = { + let mut slot = s.slot.lock().unwrap(); + while slot.next.is_none() && !slot.stop { + slot = s.wake.wait(slot).unwrap(); + } + if slot.stop { + return; + } + slot.next.take().expect("a map") + }; + let said = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| advisor.advise(&view, &hand))); + let mut slot = s.slot.lock().unwrap(); + match said { + Ok(proposals) => slot.out.extend(proposals), + Err(_) => { + eprintln!("advisor {label}: dropped: it panicked"); + slot.alive = false; + return; + } + } + } + }) + .expect("an advisor thread"); + Runner { name, shared } + } + + /// Puts the newest map and hand in the mailbox. It does not wait: a map in the mailbox that the + /// advisor has not looked at yet is replaced. + pub fn show(&self, view: &TeamView, hand: &Hand) { + let mut slot = self.shared.slot.lock().unwrap(); + if slot.alive { + slot.next = Some((view.clone(), hand.clone())); + self.shared.wake.notify_one(); + } + } + + /// The proposals that have arrived since the last call. + pub fn take(&self) -> Vec { + std::mem::take(&mut self.shared.slot.lock().unwrap().out) + } + + /// The advisor goes on (its thread has not ended on a panic). + pub fn alive(&self) -> bool { + self.shared.slot.lock().unwrap().alive + } + + pub fn name(&self) -> &str { + &self.name + } +} + +impl Drop for Runner { + fn drop(&mut self) { + self.shared.slot.lock().unwrap().stop = true; + self.shared.wake.notify_one(); + } +} + +/// The advisors beside one commander. +#[derive(Default)] +pub struct Advisors { + runners: Vec, +} + +impl Advisors { + pub fn add(&mut self, runner: Runner) { + self.runners.push(runner); + } + + pub fn len(&self) -> usize { + self.runners.len() + } + + pub fn is_empty(&self) -> bool { + self.runners.is_empty() + } + + /// Shows every advisor the map and the hand. Never waits. + pub fn show(&self, view: &TeamView, hand: &Hand) { + for r in &self.runners { + r.show(view, hand); + } + } + + /// The proposals that have arrived and are not stale at game time `now`. A stale one is dropped. + pub fn take(&self, now: f32) -> Vec { + let mut out: Vec = self.runners.iter().flat_map(Runner::take).collect(); + out.retain(|p| p.until >= now); + out + } +} + +/// An advisor in a process of its own: a command, started by the station, that serves an advisor +/// ([`serve`]). One JSON line goes in for each map, one comes out. +pub struct ProcessAdvisor { + name: String, + rules: Rules, + team: Team, + command: Option, + child: Option, + to: Option>, + lines: Receiver, + timeout: Duration, + dropped: Option, +} + +impl std::fmt::Debug for ProcessAdvisor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "ProcessAdvisor({}, {})", self.name, self.dropped.as_deref().unwrap_or("running")) + } +} + +impl ProcessAdvisor { + /// Starts `command` (with `sh -c`; `towers_advisor` is found beside this program too) and + /// sends it the rules. An error if it does not answer. + pub fn spawn(command: &str, rules: Rules, team: Team) -> Result { + ProcessAdvisor::spawn_within(command, rules, team, START_WITHIN) + } + + /// The same, with `start` as the time the process has to answer the start. + pub fn spawn_within(command: &str, rules: Rules, team: Team, start: Duration) -> Result { + let mut process = Process::new("sh"); + process.arg("-c").arg(format!("exec {command}")); + // `towers_advisor`, beside this program, is on the way. + if let Some(dir) = std::env::current_exe().ok().and_then(|p| p.parent().map(|p| p.to_path_buf())) { + let mut path = vec![dir]; + path.extend(std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default())); + if let Ok(path) = std::env::join_paths(path) { + process.env("PATH", path); + } + } + let mut child = process + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::inherit()) + .spawn() + .map_err(|e| format!("{command}: {e}"))?; + let to = child.stdin.take().expect("a pipe in"); + let from = std::io::BufReader::new(child.stdout.take().expect("a pipe out")); + let mut a = ProcessAdvisor::over_within(Box::new(to), Box::new(from), command, rules, team, start)?; + a.command = Some(command.to_string()); + a.child = Some(child); + Ok(a) + } + + /// An advisor at the other end of a pair of streams (a thread that runs [`serve`], in a test). + pub fn over( + to: Box, + from: Box, + name: &str, + rules: Rules, + team: Team, + ) -> Result { + ProcessAdvisor::over_within(to, from, name, rules, team, START_WITHIN) + } + + fn over_within( + to: Box, + from: Box, + name: &str, + rules: Rules, + team: Team, + start: Duration, + ) -> Result { + let (tx, lines) = channel(); + std::thread::Builder::new() + .name(format!("advisor-{name}-lines")) + .spawn(move || { + for line in from.lines().map_while(Result::ok) { + if tx.send(line).is_err() { + return; + } + } + }) + .expect("a reader thread"); + let mut a = ProcessAdvisor { + name: name.to_string(), + rules, + team, + command: None, + child: None, + to: Some(to), + lines, + timeout: ANSWER_WITHIN, + dropped: None, + }; + match a.ask(&ToAdvisor::Start { team, rules }, start) { + Ok(line) if line.trim() == "\"ok\"" => Ok(a), + Ok(line) => Err(format!("the advisor answered the start with {line:?}")), + Err(e) => Err(e), + } + } + + /// The time the process has to answer one map (default [`ANSWER_WITHIN`]). + pub fn with_timeout(mut self, timeout: Duration) -> ProcessAdvisor { + self.timeout = timeout; + self + } + + /// Why the advisor was dropped, if it was. + pub fn dropped(&self) -> Option<&str> { + self.dropped.as_deref() + } + + fn ask(&mut self, msg: &ToAdvisor, within: Duration) -> Result { + let to = self.to.as_mut().ok_or("the advisor's process is gone")?; + let line = serde_json::to_string(msg).map_err(|e| e.to_string())?; + writeln!(to, "{line}").and_then(|_| to.flush()).map_err(|e| format!("the advisor's process ended ({e})"))?; + match self.lines.recv_timeout(within) { + Ok(line) => Ok(line), + Err(RecvTimeoutError::Timeout) => Err(format!("the advisor did not answer in {} s", within.as_secs_f32())), + Err(RecvTimeoutError::Disconnected) => Err("the advisor's process ended".into()), + } + } + + /// Drops the advisor for good, with a line on stderr. + fn drop_with(&mut self, why: String) { + eprintln!("advisor {}: dropped: {why}", self.name); + self.dropped = Some(why); + self.to = None; + if let Some(mut c) = self.child.take() { + let _ = c.kill(); + let _ = c.wait(); + } + } +} + +impl Advise for ProcessAdvisor { + fn advise(&mut self, view: &TeamView, hand: &Hand) -> Vec { + if self.dropped.is_some() { + return vec![]; + } + let msg = ToAdvisor::Look { view: Box::new(view.clone()), hand: hand.clone() }; + let line = match self.ask(&msg, self.timeout) { + Ok(line) => line, + Err(e) => { + self.drop_with(e); + return vec![]; + } + }; + let proposals: Vec = match serde_json::from_str(&line) { + Ok(p) => p, + Err(e) => { + let said: String = line.chars().take(80).collect(); + self.drop_with(format!("it said {said:?}, which is not a list of proposals ({e})")); + return vec![]; + } + }; + if let Some(bad) = proposals.iter().find(|p| !sane(p, view)) { + self.drop_with(format!("it proposed {bad:?}, which is not for a drone and a tower of this game")); + return vec![]; + } + proposals + } + + fn clone_box(&self) -> Box { + // A process is not copied: the copy starts its own, if it was started from a command. + match self.command.as_deref().map(|c| ProcessAdvisor::spawn(c, self.rules, self.team)) { + Some(Ok(p)) => Box::new(p), + _ => { + let (_, lines) = channel(); + Box::new(ProcessAdvisor { + name: self.name.clone(), + rules: self.rules, + team: self.team, + command: None, + child: None, + to: None, + lines, + timeout: self.timeout, + dropped: Some("not copied".into()), + }) + } + } + } +} + +impl Drop for ProcessAdvisor { + fn drop(&mut self) { + self.to = None; + if let Some(mut c) = self.child.take() { + let _ = c.kill(); + let _ = c.wait(); + } + } +} + +/// A proposal that names a drone of the team, a tower of the game and a gain that is a number. +pub fn sane(p: &Proposal, view: &TeamView) -> bool { + p.drone < view.drones.len() + && p.gain.is_finite() + && p.made.is_finite() + && p.until.is_finite() + && match p.order { + Order::Take(t) => t < view.towers.len(), + _ => true, + } +} + +/// The other end of a [`ProcessAdvisor`]: reads [`ToAdvisor`] lines from `input` and answers each on +/// `output`, with the advisor `make` builds from the rules at the start. Any [`Advise`] can be a +/// process this way. Returns when `input` ends, or on a line that is not a [`ToAdvisor`]. +pub fn serve( + input: impl BufRead, + mut output: impl Write, + make: impl Fn(Rules, Team) -> Box, +) -> Result<(), String> { + let mut advisor: Option> = None; + for line in input.lines() { + let line = line.map_err(|e| e.to_string())?; + let reply = match serde_json::from_str::(&line).map_err(|e| e.to_string())? { + ToAdvisor::Start { team, rules } => { + advisor = Some(make(rules, team)); + "\"ok\"".to_string() + } + ToAdvisor::Look { view, hand } => { + let a = advisor.as_mut().ok_or("a map before the start")?; + serde_json::to_string(&a.advise(&view, &hand)).map_err(|e| e.to_string())? + } + }; + writeln!(output, "{reply}").and_then(|_| output.flush()).map_err(|e| e.to_string())?; + } + Ok(()) +} + +/// What a person's seat has in hand, for advisors: the orders the map shows, and the drones that +/// are up and in contact. A person's seat keeps no record of towers given up. +pub fn hand_of(view: &TeamView) -> Hand { + Hand { + orders: view.drones.iter().map(|d| d.order).collect(), + free: view.drones.iter().map(|d| d.linked && d.phase != Phase::Down).collect(), + away: vec![vec![false; view.towers.len()]; view.drones.len()], + } +} + +/// The proposals to list for a person at game time `view.time`: not stale, and not for an order the +/// drone already has; the one that gains most first. +pub fn listed<'a>(proposals: &'a [Proposal], view: &TeamView) -> Vec<&'a Proposal> { + let mut live: Vec<&Proposal> = proposals + .iter() + .filter(|p| p.until >= view.time && view.drones.get(p.drone).is_some_and(|d| d.order != Some(p.order))) + .collect(); + live.sort_by(|a, b| b.gain.total_cmp(&a.gain)); + live +} + +/// Runs advisors for a seat that a person commands, on a thread of its own, until the seat is given +/// up: what they propose is in `seat.state().proposals` (`Seat::accept` takes one). Advisors are +/// started when the seat is seated at a commander's place and stopped when it is not. +pub fn attend(seat: Arc, choice: AdvisorChoice) { + std::thread::Builder::new() + .name("attend".into()) + .spawn(move || { + let mut advisors: Option = None; + let mut shown = f32::NEG_INFINITY; + while !seat.stopped() { + std::thread::sleep(Duration::from_millis(50)); + let (status, rules, view) = { + let s = seat.state(); + (s.status.clone(), s.rules, s.view.clone()) + }; + let (Status::Seated(Join::Command(team)), Some(rules), Some(view)) = (status, rules, view) else { + advisors = None; + seat.state().proposals.clear(); + continue; + }; + let advisors = advisors.get_or_insert_with(|| choice.start(rules, team)); + if view.time < shown { + // A new game on the same seat. + seat.state().proposals.clear(); + } + if view.time != shown { + shown = view.time; + advisors.show(&view, &hand_of(&view)); + } + let arrived = advisors.take(view.time); + let mut s = seat.state(); + s.proposals.extend(arrived); + s.proposals.retain(|p| p.until >= view.time); + } + }) + .expect("an attending thread"); +} + +#[cfg(test)] +mod tests { + use super::*; + use recreation_towers::{Game, Phase}; + + fn view() -> TeamView { + let mut g = Game::new(Rules::default()); + for k in 0..g.rules.drones { + let i = g.drone(Team::Blue, k); + g.drones[i].phase = Phase::Flying; + } + g.time = 20.0; + g.view(Team::Blue) + } + + fn take(drone: usize, tower: usize, gain: f32, until: f32) -> Proposal { + Proposal { drone, order: Order::Take(tower), queue: vec![], gain, made: 20.0, until, why: String::new() } + } + + #[test] + fn the_panel_lists_the_live_proposals_best_first_and_not_the_stale_or_the_done() { + let mut v = view(); + v.drones[2].order = Some(Order::Take(3)); + let all = vec![take(0, 1, 3.0, 30.0), take(1, 2, 8.0, 30.0), take(0, 2, 9.0, 10.0), take(2, 3, 7.0, 30.0)]; + let shown: Vec<(usize, f32)> = listed(&all, &v).iter().map(|p| (p.drone, p.gain)).collect(); + // Drone 0's 9-point proposal is stale (until 10, now 20); drone 2 already has its order. + assert_eq!(shown, vec![(1, 8.0), (0, 3.0)]); + v.time = 31.0; + assert!(listed(&all, &v).is_empty(), "all are stale after their time"); + } + + #[test] + fn a_person_s_hand_is_the_orders_on_the_map_and_the_drones_in_contact() { + let mut v = view(); + v.drones[1].linked = false; + v.drones[3].phase = Phase::Down; + v.drones[0].order = Some(Order::Hold); + let h = hand_of(&v); + assert_eq!(h.orders[0], Some(Order::Hold)); + assert_eq!(h.free, vec![true, false, true, false, true][..v.drones.len()].to_vec()); + assert_eq!(h.away.len(), v.drones.len()); + } + + #[test] + fn a_proposal_for_a_tower_that_is_not_in_the_game_is_not_sane() { + let v = view(); + assert!(sane(&take(0, 1, 5.0, 30.0), &v)); + assert!(!sane(&take(0, 99, 5.0, 30.0), &v)); + assert!(!sane(&take(99, 1, 5.0, 30.0), &v)); + assert!(!sane(&take(0, 1, f32::NAN, 30.0), &v)); + } +} diff --git a/domains/recreation/towers_ground/src/bin/towers_advisor.rs b/domains/recreation/towers_ground/src/bin/towers_advisor.rs new file mode 100644 index 0000000..e241323 --- /dev/null +++ b/domains/recreation/towers_ground/src/bin/towers_advisor.rs @@ -0,0 +1,42 @@ +//! An advisor for Towers as a program of its own, for a station to start (`towers_commander +//! --advisor towers_advisor`, `towers_server --blue-advisor "towers_advisor --claude"`): it reads +//! one JSON line for each map on its input and writes one line of proposals on its output (see +//! `recreation_towers_ground::advisors::ToAdvisor`). +//! +//! `towers_advisor` serves the lookahead. `towers_advisor --claude [--model NAME] [--every S]` +//! asks Claude (`claude -p`, or the command in `SIM001_CLAUDE`) every `S` s of the game (default 8) +//! and costs money: it is not run by any test. + +use recreation_towers::{Advise, Lookahead}; +use recreation_towers_ground::advisors::serve; +use recreation_towers_ground::claude::ClaudeAdvisor; + +fn main() { + let args: Vec = std::env::args().collect(); + let arg = |name: &str| args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).cloned(); + if args.iter().any(|a| a == "--help" || a == "-h") { + eprintln!("usage: towers_advisor [--claude [--model NAME] [--every SECONDS]]"); + return; + } + let claude = args.iter().any(|a| a == "--claude"); + let (model, every) = (arg("--model"), arg("--every").and_then(|e| e.parse::().ok())); + let stdin = std::io::stdin(); + let result = serve(stdin.lock(), std::io::stdout().lock(), |rules, _| -> Box { + if claude { + let mut a = ClaudeAdvisor::new(); + if let Some(m) = &model { + a = a.with_model(m); + } + if let Some(e) = every { + a.every = e; + } + Box::new(a) + } else { + Box::new(Lookahead::new(rules)) + } + }); + if let Err(e) = result { + eprintln!("towers_advisor: {e}"); + std::process::exit(1); + } +} diff --git a/domains/recreation/towers_ground/src/bin/towers_commander.rs b/domains/recreation/towers_ground/src/bin/towers_commander.rs index 612ddea..9e301c8 100644 --- a/domains/recreation/towers_ground/src/bin/towers_commander.rs +++ b/domains/recreation/towers_ground/src/bin/towers_commander.rs @@ -1,10 +1,15 @@ //! A computer commander for one team of Towers, as a seat of its own: -//! `towers_commander --team blue|red [--host 127.0.0.1] [--port 5955] [--idle]`. It takes the +//! `towers_commander --team blue|red [--host 127.0.0.1] [--port 5955] [--idle] +//! [--advisor lookahead|none|COMMAND]`. It takes the //! team's seat, flies its drones from the map its drones' telemetry makes, and gives them orders //! once a second. If a person holds the seat it waits for it to be free; if a person joins the -//! team, it leaves. `--idle` sits in the seat and gives no orders. Ends when the game is over. +//! team, it leaves. `--idle` sits in the seat and gives no orders. +//! `--advisor` is the advisor beside the commander, on a thread of its own (it never waits for it): +//! `lookahead` (the default), `none`, or a command that serves an advisor, run with `sh -c`, such as +//! `towers_advisor` or `"towers_advisor --claude"` (which costs money: see `towers_advisor`). Ends when the game is over. use recreation_towers::Team; +use recreation_towers_ground::advisors::AdvisorChoice; use recreation_towers_ground::commander::{self, Brain}; use std::sync::atomic::AtomicBool; @@ -15,12 +20,21 @@ fn main() { Some("blue") => Team::Blue, Some("red") => Team::Red, _ => { - eprintln!("usage: towers_commander --team blue|red [--host H] [--port P] [--idle]"); + eprintln!( + "usage: towers_commander --team blue|red [--host H] [--port P] [--idle] [--advisor lookahead|none|COMMAND]" + ); std::process::exit(2); } }; let host = arg("--host").unwrap_or_else(|| "127.0.0.1".into()); let port: u16 = arg("--port").and_then(|p| p.parse().ok()).unwrap_or(recreation_towers::seat::PORT); let brain = if args.iter().any(|a| a == "--idle") { Brain::Idle } else { Brain::Play }; - commander::run(&format!("{host}:{port}"), team, brain, None, &AtomicBool::new(false)); + let advisor = match arg("--advisor").map_or(Ok(AdvisorChoice::Lookahead), |a| a.parse()) { + Ok(a) => a, + Err(e) => { + eprintln!("--advisor: {e}"); + std::process::exit(2); + } + }; + commander::run_with(&format!("{host}:{port}"), team, brain, None, &AtomicBool::new(false), &advisor); } diff --git a/domains/recreation/towers_ground/src/claude.rs b/domains/recreation/towers_ground/src/claude.rs new file mode 100644 index 0000000..88302d5 --- /dev/null +++ b/domains/recreation/towers_ground/src/claude.rs @@ -0,0 +1,470 @@ +//! An advisor that asks Claude: every few seconds of the game it gives `claude -p` (headless, JSON +//! output, a small budget, no tools) a text picture of the team's map and what its commander has in +//! hand, and asks for at most two proposals as JSON. The reply is read strictly: a proposal for a +//! drone or a tower that is not in the game, or with no gain, is dropped. The advisor stamps +//! `made` and `until` itself, from the map it showed. +//! +//! The command is `claude`, or what `SIM001_CLAUDE` names (as the league runs it, and a stand-in in +//! the tests: nothing here is run against the real thing by a test). It runs in a process of its own +//! with the session's `CLAUDE*` variables taken out, so that it uses its own login. + +use recreation_towers::{Advise, Class, Hand, Kind, Order, Phase, Proposal, Rules, Team, TeamView}; +use std::io::Read; +use std::path::PathBuf; +use std::process::{Command as Process, Stdio}; +use std::sync::mpsc::{RecvTimeoutError, channel}; +use std::time::{Duration, Instant}; + +/// Claude is asked this often (s of the game). +pub const EVERY: f32 = 8.0; +/// ...and not before this time in the game (s): at the start the map shows little. +pub const AFTER: f32 = 10.0; +/// A proposal of Claude is good this long (s of the game), counted from the map it was made on. +pub const VALID_FOR: f32 = 30.0; +/// At most this many proposals are read from a reply. +pub const MOST: usize = 2; +/// The most a gain may claim (points): a number Claude makes up is not to outweigh the rules. +pub const MOST_GAIN: f32 = 50.0; +/// One question may cost at most this much (US dollars). +pub const BUDGET_USD: f32 = 0.25; +/// One question may take this long. +pub const WITHIN: Duration = Duration::from_secs(60); + +/// The advisor. +#[derive(Clone, Debug)] +pub struct ClaudeAdvisor { + /// The `claude` command, or a stand-in. + pub command: PathBuf, + pub model: Option, + pub budget_usd: f32, + pub within: Duration, + /// Asked this often (s of the game). + pub every: f32, + pub valid_for: f32, + last: f32, +} + +impl ClaudeAdvisor { + /// With `claude`, or `SIM001_CLAUDE`. + pub fn new() -> ClaudeAdvisor { + let command = std::env::var_os("SIM001_CLAUDE").map(PathBuf::from).unwrap_or_else(|| "claude".into()); + ClaudeAdvisor::with_command(command) + } + + pub fn with_command(command: PathBuf) -> ClaudeAdvisor { + ClaudeAdvisor { + command, + model: None, + budget_usd: BUDGET_USD, + within: WITHIN, + every: EVERY, + valid_for: VALID_FOR, + last: f32::NEG_INFINITY, + } + } + + /// Runs `claude --model NAME`. + pub fn with_model(mut self, model: &str) -> ClaudeAdvisor { + self.model = Some(model.to_string()); + self + } + + /// The command line, but for the prompt. + fn process(&self, prompt: &str) -> Process { + let mut cmd = Process::new(&self.command); + cmd.arg("-p") + .arg(prompt) + .args(["--output-format", "json", "--permission-mode", "dontAsk", "--no-session-persistence"]) + .args(["--tools", ""]) + .args(["--max-budget-usd", &self.budget_usd.to_string()]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()); + if let Some(m) = &self.model { + cmd.args(["--model", m]); + } + own_login(&mut cmd, std::env::vars_os().map(|(n, _)| n.to_string_lossy().into_owned())); + cmd + } + + /// Asks Claude, and returns the text of its answer (the `result` of `--output-format json`). + pub fn ask(&self, prompt: &str) -> Result { + let mut child = self.process(prompt).spawn().map_err(|e| format!("{}: {e}", self.command.display()))?; + let mut out = child.stdout.take().expect("a pipe"); + let (tx, rx) = channel(); + std::thread::spawn(move || { + let mut bytes = vec![]; + let _ = out.read_to_end(&mut bytes); + let _ = tx.send(bytes); + }); + let began = Instant::now(); + let bytes = match rx.recv_timeout(self.within) { + Ok(b) => b, + Err(RecvTimeoutError::Timeout) => { + let _ = child.kill(); + let _ = child.wait(); + return Err(format!("no answer in {} s", self.within.as_secs_f32())); + } + Err(RecvTimeoutError::Disconnected) => vec![], + }; + let _ = child.wait(); + let v: serde_json::Value = serde_json::from_slice(&bytes) + .map_err(|e| format!("not JSON after {:.0} s: {e}", began.elapsed().as_secs_f32()))?; + if v.get("is_error").and_then(|e| e.as_bool()) == Some(true) { + return Err(format!("an error: {}", v.get("result").and_then(|r| r.as_str()).unwrap_or(""))); + } + v.get("result").and_then(|r| r.as_str()).map(str::to_string).ok_or_else(|| "no result in its answer".into()) + } +} + +impl Default for ClaudeAdvisor { + fn default() -> ClaudeAdvisor { + ClaudeAdvisor::new() + } +} + +/// Takes the session's own login variables out of the environment of `cmd`: started from inside a +/// Claude session, they point at that session's auth and endpoint, and `claude` must use its own. +pub fn own_login(cmd: &mut Process, names: impl IntoIterator) { + for n in names { + if n.starts_with("CLAUDE") || n == "ANTHROPIC_BASE_URL" { + cmd.env_remove(&n); + } + } +} + +impl Advise for ClaudeAdvisor { + fn advise(&mut self, view: &TeamView, hand: &Hand) -> Vec { + if view.time < AFTER || view.time - self.last < self.every { + return vec![]; + } + self.last = view.time; + // The game may have gone on while Claude thought: the proposals are stamped with the map + // they were made on, and the commander drops them when they are stale. + match self.ask(&prompt(view, hand, self.valid_for)) { + Ok(text) => parse(&text, view, hand, self.valid_for), + Err(e) => { + eprintln!("claude advisor: no proposals this time: {e}"); + vec![] + } + } + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} + +fn holder(h: Option) -> &'static str { + match h { + Some(Team::Blue) => "Blue", + Some(Team::Red) => "Red", + None => "nobody", + } +} + +fn kind(k: Kind) -> &'static str { + match k { + Kind::Fort => "home", + Kind::Spire => "spire (the most points)", + Kind::Depot => "depot (recharges drones)", + _ => "relay", + } +} + +fn order(o: Option, view: &TeamView) -> String { + match o { + None => "none".into(), + Some(Order::Take(t)) => format!("take the {} tower", view.towers.get(t).map_or("?", |t| t.name.as_str())), + Some(Order::Goto(p)) => format!("go to {:.0},{:.0}", p.x, p.y), + Some(Order::Circle(p, r)) => format!("circle {:.0},{:.0} at {r:.0} m", p.x, p.y), + Some(Order::Hold) => "hold".into(), + Some(Order::Home) => "home".into(), + } +} + +/// A compact text picture of the map and the hand, for a model to read. Drones are numbered from 1. +pub fn picture(view: &TeamView, hand: &Hand) -> String { + let rules = Rules::default(); + let mut s = format!( + "You are the advisor of the {:?} team. Time {:.0} s, {:.0} s left. Score: Blue {:.0}, Red {:.0}.\n", + view.team, view.time, view.time_left, view.score[0], view.score[1] + ); + s.push_str("Positions are metres north, east of the centre of the field.\nTowers:\n"); + for t in &view.towers { + let last_seen = if t.seen { String::new() } else { format!(", last seen {:.0} s ago", t.age) }; + s.push_str(&format!( + "- {} ({}) at {:.0},{:.0}: held by {}, control {:+.0}% for Blue{}{}\n", + t.name, + kind(t.kind), + t.at.x, + t.at.y, + holder(t.holder), + 100.0 * t.control, + if t.contested { ", contested" } else { "" }, + last_seen + )); + } + s.push_str("Your drones:\n"); + for d in &view.drones { + let class = match rules.class(d.k) { + Class::Light => "light", + Class::Standard => "standard", + Class::Heavy => "heavy", + }; + let state = match (d.phase, d.linked) { + (Phase::Down, _) => "down".to_string(), + (Phase::Ground, _) => "on the ground".to_string(), + (_, false) => format!("out of contact for {:.0} s", d.silent), + _ => "flying".to_string(), + }; + s.push_str(&format!( + "- drone {} ({class}) at {:.0},{:.0}, {state}, battery {:.0} s, order: {}{}\n", + d.k + 1, + d.at.x, + d.at.y, + d.battery, + order(hand.orders.get(d.k).copied().flatten(), view), + if hand.free.get(d.k).copied().unwrap_or(false) { "" } else { " (cannot be given a new order now)" } + )); + } + let seen: Vec<_> = view.enemies.iter().filter(|e| e.seen || e.age < 30.0).collect(); + if seen.is_empty() { + s.push_str("Enemy drones seen lately: none.\n"); + } else { + s.push_str("Enemy drones seen lately:\n"); + for e in seen { + s.push_str(&format!( + "- enemy {} at {:.0},{:.0}, {}\n", + e.k + 1, + e.at.x, + e.at.y, + if e.seen { "in sight now".to_string() } else { format!("last seen {:.0} s ago", e.age) } + )); + } + } + s +} + +/// The question: the picture, what is asked, and the shape of the answer. +pub fn prompt(view: &TeamView, hand: &Hand, valid_for: f32) -> String { + format!( + "You help the commander of a team in Towers, a game of two teams of drones. A team scores \ + points each second for each tower it holds (the spire more). Drones take a tower by \ + circling it low; a tower flips when more of your drones than theirs circle it. Do not use \ + tools.\n\n{}\nPropose at most {MOST} orders that would win the team more points over the next \ + {valid_for:.0} s than its commander's present orders. Only propose for a drone that can be \ + given an order, and only to take a tower.\n\nAnswer with only a JSON list, nothing else, for \ + example [{{\"drone\": 2, \"tower\": \"centre\", \"gain\": 6, \"why\": \"the centre is \ + undefended\"}}]. drone is the number above, tower is a tower's name above, gain is the \ + points you expect to win, why is one short sentence. Answer [] if the orders are good.", + picture(view, hand) + ) +} + +/// The proposals in a reply, strictly: the first [`MOST`] items that name a drone that can take an +/// order and a tower of the game, with a gain over 0. Anything else is dropped. `made` and `until` +/// are stamped here, from the map. +pub fn parse(reply: &str, view: &TeamView, hand: &Hand, valid_for: f32) -> Vec { + let (Some(a), Some(b)) = (reply.find('['), reply.rfind(']')) else { return vec![] }; + let Some(items) = reply.get(a..=b).and_then(|j| serde_json::from_str::>(j).ok()) else { + return vec![]; + }; + let mut out: Vec = vec![]; + for item in items { + let Some(drone) = item.get("drone").and_then(|d| d.as_u64()).and_then(|d| (d as usize).checked_sub(1)) else { + continue; + }; + let Some(tower) = item + .get("tower") + .and_then(|t| t.as_str()) + .and_then(|name| view.towers.iter().position(|t| t.name.eq_ignore_ascii_case(name.trim()))) + else { + continue; + }; + let Some(gain) = + item.get("gain").and_then(|g| g.as_f64()).map(|g| g as f32).filter(|g| g.is_finite() && *g > 0.0) + else { + continue; + }; + let order = Order::Take(tower); + if drone >= view.drones.len() + || !hand.free.get(drone).copied().unwrap_or(false) + || hand.orders.get(drone).copied().flatten() == Some(order) + || out.iter().any(|p| p.drone == drone) + { + continue; + } + let why = item.get("why").and_then(|w| w.as_str()).unwrap_or("").trim().chars().take(160).collect::(); + out.push(Proposal { + drone, + order, + queue: vec![], + gain: gain.min(MOST_GAIN), + made: view.time, + until: view.time + valid_for, + why: if why.is_empty() { + format!("Claude: send drone {} to the {} tower.", drone + 1, view.towers[tower].name) + } else { + format!("Claude: {why}") + }, + }); + if out.len() == MOST { + break; + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + use recreation_towers::Game; + use std::os::unix::fs::PermissionsExt; + + fn view() -> (TeamView, Hand) { + let mut g = Game::new(Rules::default()); + for k in 0..g.rules.drones { + let i = g.drone(Team::Blue, k); + g.drones[i].phase = Phase::Flying; + } + g.time = 30.0; + let v = g.view(Team::Blue); + let n = v.drones.len(); + let hand = Hand { orders: vec![None; n], free: vec![true; n], away: vec![vec![false; v.towers.len()]; n] }; + (v, hand) + } + + /// A stand-in for `claude` that prints `body`'s output. + fn stand_in(name: &str, body: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("claude-stand-in-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(name); + std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + path + } + + fn canned(result: &str) -> String { + let reply = serde_json::json!({"type": "result", "is_error": false, "result": result, "total_cost_usd": 0.01}); + format!("cat <<'EOF'\n{reply}\nEOF") + } + + #[test] + fn the_picture_names_the_drones_the_towers_and_what_is_in_hand() { + let (mut v, mut hand) = view(); + hand.orders[0] = Some(Order::Take(1)); + hand.free[2] = false; + v.score = [40.0, 55.0]; + let p = picture(&v, &hand); + assert!(p.contains("Blue team") && p.contains("Score: Blue 40, Red 55"), "{p}"); + assert!(p.contains("- west (") && p.contains("- drone 1 (") && p.contains("- drone 5"), "{p}"); + assert!(p.contains(&format!("order: take the {} tower", v.towers[1].name)), "{p}"); + assert!(p.contains("(cannot be given a new order now)"), "{p}"); + assert!(prompt(&v, &hand, 30.0).contains("JSON list")); + } + + #[test] + fn a_good_reply_is_read_and_stamped_with_the_map_s_time() { + let (v, hand) = view(); + let reply = r#"Here you go: +```json +[{"drone": 2, "tower": "West", "gain": 6, "why": "the west is open"}, {"drone": 3, "tower": "centre", "gain": 400}] +```"#; + let p = parse(reply, &v, &hand, 30.0); + assert_eq!(p.len(), 2); + let west = v.towers.iter().position(|t| t.name == "west").unwrap(); + assert_eq!((p[0].drone, p[0].order, p[0].gain), (1, Order::Take(west), 6.0)); + assert_eq!((p[0].made, p[0].until), (30.0, 60.0)); + assert!(p[0].why.contains("the west is open")); + assert_eq!(p[1].gain, MOST_GAIN, "a gain is held to what the rules can be worth"); + } + + #[test] + fn a_reply_is_read_strictly() { + let (v, mut hand) = view(); + let one = |s: &str| parse(s, &v, &hand, 30.0); + assert!(one("no list here").is_empty()); + assert!(one("[{\"drone\": 1, \"tower\": \"west\"").is_empty(), "cut off"); + assert!(one(r#"[{"drone": 99, "tower": "west", "gain": 5}]"#).is_empty(), "unknown drone"); + assert!(one(r#"[{"drone": 0, "tower": "west", "gain": 5}]"#).is_empty(), "drones count from 1"); + assert!(one(r#"[{"drone": 1, "tower": "moon", "gain": 5}]"#).is_empty(), "unknown tower"); + assert!(one(r#"[{"drone": 1, "tower": "west"}]"#).is_empty(), "no gain"); + assert!(one(r#"[{"drone": 1, "tower": "west", "gain": "lots"}]"#).is_empty(), "gain that is no number"); + assert!(one(r#"[{"drone": 1, "tower": "west", "gain": -3}]"#).is_empty(), "no gain over nothing"); + assert!(one(r#"[{"drone": "one", "tower": "west", "gain": 5}]"#).is_empty()); + // A good one among bad ones is kept; two for the same drone are one; three are two. + let mixed = r#"[{"drone": 99, "tower": "west", "gain": 5}, {"drone": 1, "tower": "west", "gain": 5}, + {"drone": 1, "tower": "east", "gain": 9}, {"drone": 2, "tower": "east", "gain": 5}, {"drone": 3, "tower": "east", "gain": 5}]"#; + assert_eq!(one(mixed).iter().map(|p| p.drone).collect::>(), vec![0, 1]); + // A drone that cannot take an order gets none. + hand.free[0] = false; + assert!(parse(r#"[{"drone": 1, "tower": "west", "gain": 5}]"#, &v, &hand, 30.0).is_empty()); + } + + #[test] + fn it_asks_the_command_and_reads_what_it_proposes() { + let (v, hand) = view(); + let good = stand_in("good", &canned(r#"[{"drone": 2, "tower": "east", "gain": 7, "why": "east is open"}]"#)); + let mut a = ClaudeAdvisor::with_command(good); + let p = a.advise(&v, &hand); + assert_eq!(p.len(), 1); + assert_eq!((p[0].drone, p[0].gain), (1, 7.0)); + // Asked again at once, it does not ask: every few seconds. + assert!(a.advise(&v, &hand).is_empty()); + let mut later = v.clone(); + later.time += EVERY; + assert_eq!(a.advise(&later, &hand).len(), 1); + } + + #[test] + fn it_is_asked_with_headless_json_a_budget_and_no_tools() { + let (v, hand) = view(); + let args = std::env::temp_dir().join(format!("claude-args-{}", std::process::id())); + let spy = stand_in("spy", &format!("printf '%s\\n' \"$@\" > {}\n{}", args.display(), canned("[]"))); + let a = ClaudeAdvisor::with_command(spy); + a.ask(&prompt(&v, &hand, 30.0)).unwrap(); + let args = std::fs::read_to_string(args).unwrap(); + let args: Vec<&str> = args.lines().collect(); + assert_eq!(args[0], "-p"); + for want in ["--output-format", "json", "--max-budget-usd", "--tools", "--no-session-persistence"] { + assert!(args.contains(&want), "{want} in {args:?}"); + } + } + + #[test] + fn a_reply_that_is_not_json_or_an_error_or_late_gives_no_proposals() { + let (v, hand) = view(); + for (name, body) in [ + ("malformed", "echo 'this is not what claude prints'".to_string()), + ("empty", "true".to_string()), + ("error", canned("x").replace("\"is_error\":false", "\"is_error\":true")), + ("nonsense-result", canned("I think you should attack!")), + ("missing-command-output", "exit 1".to_string()), + ] { + let mut a = ClaudeAdvisor::with_command(stand_in(name, &body)); + assert!(a.advise(&v, &hand).is_empty(), "{name}"); + } + let mut gone = ClaudeAdvisor::with_command("/no/such/claude".into()); + assert!(gone.advise(&v, &hand).is_empty()); + let mut slow = ClaudeAdvisor::with_command(stand_in("slow", &format!("sleep 5\n{}", canned("[]")))); + slow.within = Duration::from_millis(300); + let t = Instant::now(); + assert!(slow.advise(&v, &hand).is_empty()); + assert!(t.elapsed() < Duration::from_secs(3), "{:?}", t.elapsed()); + } + + #[test] + fn it_runs_with_its_own_login_not_the_session_it_was_started_from() { + let mut cmd = Process::new("true"); + own_login( + &mut cmd, + ["PATH", "CLAUDE_CODE_OAUTH_TOKEN", "CLAUDECODE", "ANTHROPIC_BASE_URL", "SIM001_CLAUDE", "HOME"] + .map(String::from), + ); + let mut removed: Vec = + cmd.get_envs().filter(|(_, v)| v.is_none()).map(|(k, _)| k.to_string_lossy().into_owned()).collect(); + removed.sort(); + assert_eq!(removed, ["ANTHROPIC_BASE_URL", "CLAUDECODE", "CLAUDE_CODE_OAUTH_TOKEN"]); + } +} diff --git a/domains/recreation/towers_ground/src/commander.rs b/domains/recreation/towers_ground/src/commander.rs index b749e0c..273f7f8 100644 --- a/domains/recreation/towers_ground/src/commander.rs +++ b/domains/recreation/towers_ground/src/commander.rs @@ -3,11 +3,17 @@ //! station of the seat (`station`: its own ground, the map from telemetry) and, once a second of //! game time, asks [`recreation_towers::Commander`] (as a [`Command`]) for directives, and sends each with its queue, on the map the station built. It //! knows only what a seat gets. +//! +//! It takes advice and does not wait for it: its advisors ([`AdvisorChoice`], the lookahead by +//! default) run beside it on threads of their own (`advisors`). Before each decision it puts the +//! proposals that have arrived in hand (`Commander::advise`); after it, it shows the advisors the +//! map and what it has in hand. The built-in lookahead of `Commander` is off. +use crate::advisors::{AdvisorChoice, Advisors}; use crate::seat::{Seat, Status}; use crate::station::{self, Tally}; use recreation_towers::seat::Join; -use recreation_towers::{Class, Command, Commander, Team}; +use recreation_towers::{Class, Command, Commander, Proposal, Team}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; @@ -27,10 +33,24 @@ pub enum Brain { /// Takes the seat of `team` on the server at `addr` (`host:port`) and commands it until the game is /// over, a person takes the seat, or `stop` is set. If the seat is held, it waits for it to be free. pub fn run(addr: &str, team: Team, brain: Brain, tally: Option>, stop: &AtomicBool) { + run_with(addr, team, brain, tally, stop, &AdvisorChoice::Lookahead); +} + +/// The same, with the advisor `advisor` beside the commander. +pub fn run_with( + addr: &str, + team: Team, + brain: Brain, + tally: Option>, + stop: &AtomicBool, + advisor: &AdvisorChoice, +) { let host = addr.rsplit_once(':').map_or("127.0.0.1", |(h, _)| h).to_string(); let seat = Seat::connect(addr, Join::Compute(team)); - station::start(seat.clone(), host, tally); - let mut commander: Option<(Commander, f32)> = None; + station::start(seat.clone(), host, tally.clone()); + let mut commander: Option<(Commander, f32, Advisors)> = None; + // Proposals that arrived and are not yet taken or stale (for the log). + let mut pending: Vec = vec![]; while !stop.load(Ordering::SeqCst) { std::thread::sleep(Duration::from_millis(50)); let (status, rules, view) = { @@ -54,18 +74,49 @@ pub fn run(addr: &str, team: Team, brain: Brain, tally: Option>, stop continue; } // The seat starts from the orders the drones have: they are on the map. - let (c, next) = commander.get_or_insert_with(|| { + let (c, next, advisors) = commander.get_or_insert_with(|| { let mut c = Commander::new(rules); for d in &view.drones { c.assign(d.k, d.order); } - (c, f32::NEG_INFINITY) + (c.with_lookahead(false), f32::NEG_INFINITY, advisor.start(rules, team)) }); if view.time < *next { continue; } *next = view.time + DECIDE_EVERY; + // What the advisors have for the commander by now, and no more: it does not wait for them. + let arrived = advisors.take(view.time); + for p in &arrived { + println!( + "advice {team:?}: proposal at {:.0} s: drone {} {:?} for {:.1} points, good until {:.0} s", + view.time, + p.drone + 1, + p.order, + p.gain, + p.until + ); + } + if let Some(t) = &tally { + t.proposals[team.index()].fetch_add(arrived.len(), Ordering::SeqCst); + } + pending.extend(arrived.iter().cloned()); + c.advise(arrived); let Ok(directives) = Command::decide(c, &view) else { continue }; + // A proposal is taken when the drone's order is the proposal's now. + pending.retain(|p| { + if directives.iter().any(|d| d.drone == p.drone && d.order == p.order) && p.until >= view.time { + println!("advice {team:?}: taken at {:.0} s: drone {} {:?}", view.time, p.drone + 1, p.order); + if let Some(t) = &tally { + t.taken[team.index()].fetch_add(1, Ordering::SeqCst); + } + return false; + } + p.until >= view.time + }); + if let Some(hand) = c.hand() { + advisors.show(&view, hand); + } for d in directives { seat.order_with_queue(d.drone, d.order, rules.class(d.drone) == Class::Light, d.queue); } diff --git a/domains/recreation/towers_ground/src/lib.rs b/domains/recreation/towers_ground/src/lib.rs index 565fc70..bc9258a 100644 --- a/domains/recreation/towers_ground/src/lib.rs +++ b/domains/recreation/towers_ground/src/lib.rs @@ -11,6 +11,8 @@ //! through a [`Transport`]: TCP to the simulated ground radio, or a serial line to a real one. //! What it knows of the game is a [`Situation`]. +pub mod advisors; +pub mod claude; pub mod commander; pub mod map; pub mod seat; diff --git a/domains/recreation/towers_ground/src/seat.rs b/domains/recreation/towers_ground/src/seat.rs index 9507bed..a63bf26 100644 --- a/domains/recreation/towers_ground/src/seat.rs +++ b/domains/recreation/towers_ground/src/seat.rs @@ -3,7 +3,7 @@ //! the server is not up yet, it tries again. use recreation_towers::seat::{self, Join, ToSeat, ToServer}; -use recreation_towers::{Advice, Order, Rules, Sites, TeamView, Umpire}; +use recreation_towers::{Advice, Order, Proposal, Rules, Sites, TeamView, Umpire}; use std::io::{BufRead, BufReader, Write}; use std::net::TcpStream; use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; @@ -35,6 +35,9 @@ pub struct State { pub orders: Vec<(Option, bool)>, /// What the advisor said lately, and when (the last few). pub advice: Vec<(Advice, Instant)>, + /// What the advisors beside the seat propose now (advisors.rs: `attend`), each good until its + /// own game time. The app lists them and `Seat::accept` takes one. + pub proposals: Vec, } /// How many pieces of advice the seat keeps. @@ -71,6 +74,7 @@ impl Seat { air: vec![], orders: vec![], advice: vec![], + proposals: vec![], })), orders: Mutex::new(tx), generation: Arc::new(AtomicU64::new(0)), @@ -109,6 +113,30 @@ impl Seat { let _ = self.orders.lock().unwrap().send(ToServer::Order { drone, order, low, standing: None, queue }); } + /// Puts proposals among the live ones, as if an advisor had just sent them. + pub fn offer(&self, proposals: impl IntoIterator) { + self.state().proposals.extend(proposals); + } + + /// Takes the proposal that is `nth` in the list a person sees (`advisors::listed`, the one that + /// gains most first): gives the drone its order and queue, flown as it flies now, and takes the + /// proposal off the list. False if there is no such proposal (it went stale). + pub fn accept(&self, nth: usize) -> bool { + let taken = { + let mut s = self.state(); + let Some(view) = s.view.clone() else { return false }; + let Some(p) = crate::advisors::listed(&s.proposals, &view).get(nth).map(|p| (*p).clone()) else { + return false; + }; + s.proposals.retain(|q| *q != p); + let low = view.drones.get(p.drone).is_some_and(|d| d.low); + (p, low) + }; + let (p, low) = taken; + self.order_with_queue(p.drone, p.order, low, p.queue); + true + } + /// Stops connecting: the connection that is open ends with its server, and is not made /// again. (Before the server stops, so that it does not take the seat of the next game first.) pub fn hang_up(&self) { @@ -138,6 +166,7 @@ impl Seat { air: vec![], orders: vec![], advice: vec![], + proposals: vec![], }; let (tx, rx) = channel(); *self.orders.lock().unwrap() = tx; @@ -225,3 +254,50 @@ fn run(addr: &str, join: Join, state: &Mutex, orders: Receiver, std::thread::sleep(Duration::from_millis(300)); } } + +#[cfg(test)] +mod tests { + use super::*; + use recreation_towers::{Game, Phase, Team}; + + /// A seat on a server that is not there, with a map of the game at `time`. + fn seat_at(time: f32) -> Arc { + let seat = Seat::connect("127.0.0.1:1", Join::Command(Team::Blue)); + let mut g = Game::new(Rules::default()); + g.time = time; + for k in 0..g.rules.drones { + let i = g.drone(Team::Blue, k); + g.drones[i].phase = Phase::Flying; + } + let mut view = g.view(Team::Blue); + view.drones[1].low = true; + seat.state().view = Some(view); + seat + } + + fn take(drone: usize, tower: usize, gain: f32, until: f32) -> Proposal { + Proposal { drone, order: Order::Take(tower), queue: vec![], gain, made: 20.0, until, why: String::new() } + } + + #[test] + fn accepting_a_proposal_gives_the_order_as_the_drone_flies_and_takes_it_off_the_list() { + let seat = seat_at(20.0); + seat.offer([take(0, 1, 3.0, 50.0), take(1, 2, 8.0, 50.0)]); + // The list a person sees has the proposal that gains most first. + assert!(seat.accept(0)); + assert_eq!(seat.state().orders[1], (Some(Order::Take(2)), true), "drone 2 flies low, and keeps it"); + assert_eq!(seat.state().proposals, vec![take(0, 1, 3.0, 50.0)]); + assert!(seat.accept(0)); + assert_eq!(seat.state().orders[0], (Some(Order::Take(1)), false)); + assert!(seat.state().proposals.is_empty()); + assert!(!seat.accept(0), "nothing is left to take"); + } + + #[test] + fn a_stale_proposal_is_not_taken() { + let seat = seat_at(60.0); + seat.offer([take(0, 1, 9.0, 50.0)]); + assert!(!seat.accept(0)); + assert!(seat.state().orders.is_empty(), "no order went out"); + } +} diff --git a/domains/recreation/towers_ground/src/station.rs b/domains/recreation/towers_ground/src/station.rs index bb27ad4..b7d2e95 100644 --- a/domains/recreation/towers_ground/src/station.rs +++ b/domains/recreation/towers_ground/src/station.rs @@ -70,6 +70,9 @@ pub struct Tally { pub in_agent: [AtomicUsize; 2], /// Commands that the drones refused, by team. pub refused: [AtomicUsize; 2], + /// Proposals that the advisors of the commanders made, and the ones a commander took, by team. + pub proposals: [AtomicUsize; 2], + pub taken: [AtomicUsize; 2], /// A line about each drone of the game, by number in the game. pub lines: Mutex>, /// Warnings of the stations, by team. @@ -81,6 +84,14 @@ impl Tally { self.in_agent.iter().map(|n| n.load(Ordering::SeqCst)).sum() } + pub fn proposals(&self) -> usize { + self.proposals.iter().map(|n| n.load(Ordering::SeqCst)).sum() + } + + pub fn taken(&self) -> usize { + self.taken.iter().map(|n| n.load(Ordering::SeqCst)).sum() + } + pub fn refused(&self) -> usize { self.refused.iter().map(|n| n.load(Ordering::SeqCst)).sum() } diff --git a/domains/recreation/towers_ground/tests/advisors.rs b/domains/recreation/towers_ground/tests/advisors.rs new file mode 100644 index 0000000..7ce599a --- /dev/null +++ b/domains/recreation/towers_ground/tests/advisors.rs @@ -0,0 +1,256 @@ +//! Advisors beside the commander (plans/actors.md, step 4): each runs on a thread of its own with a +//! mailbox, the commander never waits for one, and one in another process that dies, hangs or +//! writes nonsense is dropped. + +use recreation_towers::{Advise, Commander, Game, Hand, Order, Phase, Proposal, Rules, Team, TeamView}; +use recreation_towers_ground::advisors::{self, Advisors, ProcessAdvisor, Runner}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +fn game_with_drones_up() -> (Game, TeamView) { + let mut g = Game::new(Rules::default()); + for k in 0..g.rules.drones { + let i = g.drone(Team::Blue, k); + g.drones[i].phase = Phase::Flying; + g.drones[i].alt = 50.0; + } + g.time = 20.0; + let v = g.view(Team::Blue); + (g, v) +} + +fn hand(view: &TeamView) -> Hand { + let n = view.drones.len(); + Hand { orders: vec![None; n], free: vec![true; n], away: vec![vec![false; view.towers.len()]; n] } +} + +fn proposal(view: &TeamView, drone: usize, tower: usize, gain: f32, until: f32) -> Proposal { + Proposal { drone, order: Order::Take(tower), queue: vec![], gain, made: view.time, until, why: "test".into() } +} + +/// An advisor that thinks for `think` and proposes a take of tower 1 by drone 0, good for `good` s. +#[derive(Clone, Debug)] +struct Slow { + think: Duration, + good: f32, + looked: Arc>>, +} + +impl Advise for Slow { + fn advise(&mut self, view: &TeamView, _: &Hand) -> Vec { + self.looked.lock().unwrap().push(view.time); + std::thread::sleep(self.think); + vec![proposal(view, 0, 1, 9.0, view.time + self.good)] + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} + +fn wait_for(what: &str, mut f: impl FnMut() -> Option) -> T { + let end = Instant::now() + Duration::from_secs(10); + loop { + if let Some(x) = f() { + return x; + } + assert!(Instant::now() < end, "waited for {what}"); + std::thread::sleep(Duration::from_millis(10)); + } +} + +#[test] +fn an_advisor_that_sleeps_for_seconds_does_not_delay_the_commander() { + let (g, mut view) = game_with_drones_up(); + let looked = Arc::new(Mutex::new(vec![])); + let mut advisors = Advisors::default(); + advisors.add(Runner::start( + "slow", + Box::new(Slow { think: Duration::from_secs(3), good: 4.0, looked: looked.clone() }), + )); + let mut commander = Commander::new(g.rules).with_lookahead(false); + let began = Instant::now(); + let mut worst = Duration::ZERO; + // Ten decisions, one for each game second; the wall clock runs 20 times faster. + for second in 0..10 { + let t = Instant::now(); + view.time = 20.0 + second as f32; + commander.advise(advisors.take(view.time)); + let orders = commander.decide(&view); + assert_eq!(orders.len(), g.rules.drones, "every drone has its order at second {second}"); + advisors.show(&view, commander.hand().expect("a hand after a decision")); + worst = worst.max(t.elapsed()); + std::thread::sleep(Duration::from_millis(50)); + } + assert!(worst < Duration::from_millis(100), "the slowest decision took {worst:?}"); + assert!(began.elapsed() < Duration::from_secs(2), "ten decisions took {:?}", began.elapsed()); + // The advisor is still thinking about the first map; the others went into its mailbox. + assert_eq!(looked.lock().unwrap().len(), 1); +} + +#[test] +fn a_late_proposal_is_dropped_once_it_is_stale() { + let (_, mut view) = game_with_drones_up(); + let mut advisors = Advisors::default(); + let looked = Arc::new(Mutex::new(vec![])); + // Good for 4 s of the game, and it takes 300 ms to think. + advisors.add(Runner::start("slow", Box::new(Slow { think: Duration::from_millis(300), good: 4.0, looked }))); + advisors.show(&view, &hand(&view)); + std::thread::sleep(Duration::from_millis(500)); + // The game has gone on 10 s while it thought: the proposal is stale when it arrives. + assert!(advisors.take(view.time + 10.0).is_empty(), "a stale proposal is not handed on"); + // The next one, with the game at the time it was made, is handed on. + view.time = 40.0; + advisors.show(&view, &hand(&view)); + let p = wait_for("a fresh proposal", || Some(advisors.take(41.0)).filter(|p| !p.is_empty())); + assert_eq!(p.len(), 1); + assert_eq!(p[0].made, 40.0); + assert!(advisors.take(41.0).is_empty(), "a proposal is handed on once"); +} + +#[test] +fn the_mailbox_keeps_only_the_newest_map() { + let (_, mut view) = game_with_drones_up(); + let looked = Arc::new(Mutex::new(vec![])); + let runner = + Runner::start("slow", Box::new(Slow { think: Duration::from_millis(400), good: 50.0, looked: looked.clone() })); + runner.show(&view, &hand(&view)); + wait_for("the first look", || (!looked.lock().unwrap().is_empty()).then_some(())); + for t in 1..=4 { + view.time = 20.0 + t as f32; + runner.show(&view, &hand(&view)); + } + wait_for("the second look", || (looked.lock().unwrap().len() >= 2).then_some(())); + std::thread::sleep(Duration::from_millis(900)); + assert_eq!(*looked.lock().unwrap(), vec![20.0, 24.0], "the maps in between were dropped, not queued"); +} + +#[test] +fn an_advisor_that_panics_is_dropped_and_the_commander_goes_on() { + #[derive(Clone, Debug)] + struct Bad; + impl Advise for Bad { + fn advise(&mut self, _: &TeamView, _: &Hand) -> Vec { + panic!("a bug in an advisor"); + } + fn clone_box(&self) -> Box { + Box::new(Bad) + } + } + let (_, view) = game_with_drones_up(); + let runner = Runner::start("bad", Box::new(Bad)); + runner.show(&view, &hand(&view)); + wait_for("the runner to give up", || (!runner.alive()).then_some(())); + runner.show(&view, &hand(&view)); + assert!(runner.take().is_empty()); +} + +fn script(name: &str, body: &str) -> String { + let dir = std::env::temp_dir().join(format!("advisors-test-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(name); + std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap(); + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + path.to_string_lossy().into_owned() +} + +#[test] +fn a_process_advisor_is_one_line_in_and_one_line_out_and_the_rules_come_once() { + let (g, mut view) = game_with_drones_up(); + // The process is `serve` with the lookahead: a thread plays it through two pipes. + let (to_r, to_w) = std::io::pipe().unwrap(); + let (from_r, from_w) = std::io::pipe().unwrap(); + std::thread::spawn(move || { + advisors::serve(std::io::BufReader::new(to_r), from_w, |rules, _| { + Box::new(recreation_towers::Lookahead::new(rules)) + }) + }); + let mut piped = + ProcessAdvisor::over(Box::new(to_w), Box::new(std::io::BufReader::new(from_r)), "piped", g.rules, Team::Blue) + .expect("the start is answered"); + view.time = 30.0; + let proposals = piped.advise(&view, &hand(&view)); + assert!(piped.dropped().is_none(), "{:?}", piped.dropped()); + // Whatever the lookahead proposes, it comes back as proposals made on this map. + assert!(proposals.iter().all(|p| p.made == 30.0 && p.drone < g.rules.drones)); +} + +#[test] +fn a_process_advisor_that_answers_gives_its_proposals() { + let (g, view) = game_with_drones_up(); + let cmd = script( + "good.sh", + r#"read start; echo '"ok"' +while read look; do echo '[{"drone":1,"order":{"Take":2},"queue":[],"gain":6.5,"made":20.0,"until":60.0,"why":"from a process"}]'; done"#, + ); + let mut a = ProcessAdvisor::spawn(&cmd, g.rules, Team::Blue).unwrap(); + let p = a.advise(&view, &hand(&view)); + assert_eq!(p.len(), 1); + assert_eq!((p[0].drone, p[0].order, p[0].gain, p[0].why.as_str()), (1, Order::Take(2), 6.5, "from a process")); + assert!(a.dropped().is_none()); +} + +#[test] +fn a_process_advisor_that_writes_nonsense_is_dropped_with_a_line_and_the_game_goes_on() { + let (g, view) = game_with_drones_up(); + let cmd = script("nonsense.sh", "read start; echo '\"ok\"'\nwhile read look; do echo 'this is not json'; done"); + let mut a = ProcessAdvisor::spawn(&cmd, g.rules, Team::Blue).unwrap(); + assert!(a.advise(&view, &hand(&view)).is_empty()); + let why = a.dropped().expect("dropped"); + assert!(why.contains("said"), "{why}"); + // Dropped for good: no further question is asked of it. + let t = Instant::now(); + assert!(a.advise(&view, &hand(&view)).is_empty()); + assert!(t.elapsed() < Duration::from_millis(50)); +} + +#[test] +fn a_process_advisor_that_proposes_for_a_drone_that_does_not_exist_is_dropped() { + let (g, view) = game_with_drones_up(); + let cmd = script( + "ghost.sh", + r#"read start; echo '"ok"' +while read look; do echo '[{"drone":99,"order":{"Take":2},"queue":[],"gain":6.5,"made":20.0,"until":60.0,"why":"x"}]'; done"#, + ); + let mut a = ProcessAdvisor::spawn(&cmd, g.rules, Team::Blue).unwrap(); + assert!(a.advise(&view, &hand(&view)).is_empty()); + assert!(a.dropped().is_some()); +} + +#[test] +fn a_process_advisor_that_dies_is_dropped() { + let (g, view) = game_with_drones_up(); + let cmd = script("dies.sh", "read start; echo '\"ok\"'\nread look; exit 3"); + let mut a = ProcessAdvisor::spawn(&cmd, g.rules, Team::Blue).unwrap(); + assert!(a.advise(&view, &hand(&view)).is_empty()); + assert!(a.dropped().is_some_and(|w| w.contains("ended")), "{:?}", a.dropped()); +} + +#[test] +fn a_process_advisor_that_hangs_is_dropped_after_its_time() { + let (g, view) = game_with_drones_up(); + let cmd = script("hangs.sh", "read start; echo '\"ok\"'\nread look; sleep 30"); + let mut a = ProcessAdvisor::spawn(&cmd, g.rules, Team::Blue).unwrap().with_timeout(Duration::from_millis(300)); + let t = Instant::now(); + assert!(a.advise(&view, &hand(&view)).is_empty()); + assert!(t.elapsed() < Duration::from_secs(3)); + assert!(a.dropped().is_some_and(|w| w.contains("did not answer")), "{:?}", a.dropped()); +} + +#[test] +fn a_command_that_does_not_start_is_no_advisor() { + let g = Game::new(Rules::default()); + assert!(ProcessAdvisor::spawn("/no/such/advisor", g.rules, Team::Blue).is_err()); + let cmd = script("mute.sh", "sleep 30"); + assert!(ProcessAdvisor::spawn_within(&cmd, g.rules, Team::Blue, Duration::from_millis(300)).is_err()); +} + +#[test] +fn the_choices_of_advisor_are_read_from_a_flag() { + use advisors::AdvisorChoice; + assert_eq!("lookahead".parse(), Ok(AdvisorChoice::Lookahead)); + assert_eq!("none".parse(), Ok(AdvisorChoice::None)); + assert_eq!("towers_advisor --claude".parse(), Ok(AdvisorChoice::Command("towers_advisor --claude".into()))); + assert!("".parse::().is_err()); +} diff --git a/domains/recreation/towers_ground/tests/commander.rs b/domains/recreation/towers_ground/tests/commander.rs index e599e7b..e3ace85 100644 --- a/domains/recreation/towers_ground/tests/commander.rs +++ b/domains/recreation/towers_ground/tests/commander.rs @@ -1,70 +1,107 @@ //! A computer commander is a seat like a person's: against a fake game server that speaks only the //! seat protocol (no SITL), it takes the seat as `Join::Compute`, and from the umpire's word and //! what its sites report it gives orders; it goes on from the orders the drones have, and it leaves -//! when it is relieved. +//! when it is relieved. Its advisors run beside it: it never waits for one, and it takes the +//! proposals that arrive. use recreation_towers::seat::{self, Join, ToSeat, ToServer}; use recreation_towers::{Game, Order, Rules, Team}; +use recreation_towers_ground::advisors::AdvisorChoice; use recreation_towers_ground::commander::{self, Brain}; use std::io::{BufRead, BufReader, Write}; use std::net::{TcpListener, TcpStream}; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::mpsc::channel; +use std::sync::mpsc::{Receiver, channel}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; -#[test] -fn a_computer_commander_takes_a_seat_gives_orders_from_the_sites_and_leaves_when_relieved() { - let rules = Rules::default(); - // Air ports for the team's drones that accept and say nothing: no telemetry at all. - let air: Vec = (0..rules.drones).map(|_| TcpListener::bind("127.0.0.1:0").unwrap()).collect(); - let ports: Vec = air.iter().map(|l| l.local_addr().unwrap().port()).collect(); - let held: Arc>> = Arc::default(); - for l in air { - let held = held.clone(); +/// A fake game server that speaks only the seat protocol, and the computer commander of Blue on it. +struct Fake { + out: TcpStream, + rules: Rules, + game: Game, + orders: Receiver<(usize, Order, bool)>, + done: Receiver<()>, + stop: Arc, + _held: Arc>>, +} + +fn said(out: &mut TcpStream, m: ToSeat) { + writeln!(out, "{}", seat::encode(&m)).unwrap(); +} + +impl Fake { + fn start(advisor: AdvisorChoice) -> Fake { + let rules = Rules::default(); + // Air ports for the team's drones that accept and say nothing: no telemetry at all. + let air: Vec = (0..rules.drones).map(|_| TcpListener::bind("127.0.0.1:0").unwrap()).collect(); + let ports: Vec = air.iter().map(|l| l.local_addr().unwrap().port()).collect(); + let held: Arc>> = Arc::default(); + for l in air { + let held = held.clone(); + std::thread::spawn(move || { + for s in l.incoming().flatten() { + held.lock().unwrap().push(s); + } + }); + } + let server = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = format!("127.0.0.1:{}", server.local_addr().unwrap().port()); + let stop = Arc::new(AtomicBool::new(false)); + let (done_tx, done) = channel(); + let (a, s) = (addr.clone(), stop.clone()); + std::thread::spawn(move || { + commander::run_with(&a, Team::Blue, Brain::Play, None, &s, &advisor); + done_tx.send(()).unwrap(); + }); + let (stream, _) = server.accept().unwrap(); + let mut out = stream.try_clone().unwrap(); + let mut lines = BufReader::new(stream).lines().map_while(Result::ok); + let ToServer::Join(join) = seat::decode(&lines.next().unwrap()).unwrap() else { panic!("not a join") }; + assert_eq!(join, Join::Compute(Team::Blue)); + said(&mut out, ToSeat::Welcome { join, rules, air: ports }); + // The drones have orders already: a person was commanding them. + said(&mut out, ToSeat::Orders(vec![(Some(Order::Hold), true); rules.drones])); + let mut game = Game::new(rules); + game.time = 5.0; + let (orders_tx, orders) = channel(); std::thread::spawn(move || { - for s in l.incoming().flatten() { - held.lock().unwrap().push(s); + for line in lines { + if let Ok(ToServer::Order { drone, order, low, .. }) = seat::decode::(&line) { + let _ = orders_tx.send((drone, order, low)); + } } }); + Fake { out, rules, game, orders, done, stop, _held: held } } - let server = TcpListener::bind("127.0.0.1:0").unwrap(); - let addr = format!("127.0.0.1:{}", server.local_addr().unwrap().port()); - let stop = Arc::new(AtomicBool::new(false)); - let (done_tx, done_rx) = channel(); - let (a, s) = (addr.clone(), stop.clone()); - std::thread::spawn(move || { - commander::run(&a, Team::Blue, Brain::Play, None, &s); - done_tx.send(()).unwrap(); - }); - let (stream, _) = server.accept().unwrap(); - let mut out = stream.try_clone().unwrap(); - let mut lines = BufReader::new(stream).lines().map_while(Result::ok); - let ToServer::Join(join) = seat::decode(&lines.next().unwrap()).unwrap() else { panic!("not a join") }; - assert_eq!(join, Join::Compute(Team::Blue)); - let said = |out: &mut TcpStream, m: ToSeat| writeln!(out, "{}", seat::encode(&m)).unwrap(); - said(&mut out, ToSeat::Welcome { join, rules, air: ports }); - // The drones have orders already: a person was commanding them. - said(&mut out, ToSeat::Orders(vec![(Some(Order::Hold), true); rules.drones])); - let mut game = Game::new(rules); - game.time = 5.0; - let (orders_tx, orders_rx) = channel(); - let reader = std::thread::spawn(move || { - for line in lines { - if let Ok(ToServer::Order { drone, order, low, .. }) = seat::decode::(&line) { - orders_tx.send((drone, order, low)).unwrap(); - } - } - }); + + /// The game goes on 0.25 s, and the seat is told. + fn tell(&mut self) { + self.game.time += 0.25; + said(&mut self.out, ToSeat::Umpire(self.game.umpire())); + said(&mut self.out, ToSeat::Sites(self.game.sites(Team::Blue))); + } +} + +fn script(name: &str, body: &str) -> String { + let path = std::env::temp_dir().join(format!("{name}-{}.sh", std::process::id())); + std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap(); + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + path.to_string_lossy().into_owned() +} + +#[test] +fn a_computer_commander_takes_a_seat_gives_orders_from_the_sites_and_leaves_when_relieved() { + let mut fake = Fake::start(AdvisorChoice::Lookahead); + let rules = fake.rules; // Tell it the game until it has given every drone an order. let end = Instant::now() + Duration::from_secs(10); let mut given = std::collections::BTreeMap::new(); while given.len() < rules.drones { assert!(Instant::now() < end, "it gave orders to {given:?}"); - game.time += 0.25; - said(&mut out, ToSeat::Umpire(game.umpire())); - said(&mut out, ToSeat::Sites(game.sites(Team::Blue))); - while let Ok((drone, order, low)) = orders_rx.recv_timeout(Duration::from_millis(100)) { + fake.tell(); + while let Ok((drone, order, low)) = fake.orders.recv_timeout(Duration::from_millis(100)) { given.insert(drone, (order, low)); } } @@ -73,9 +110,61 @@ fn a_computer_commander_takes_a_seat_gives_orders_from_the_sites_and_leaves_when assert_eq!(*low, rules.class(*k) == recreation_towers::Class::Light, "drone {k} is flown low if it is light"); } // Relieved by a person: it leaves, and does not come back. - said(&mut out, ToSeat::Refused("relieved: a person took the seat".into())); - drop(out); - done_rx.recv_timeout(Duration::from_secs(5)).expect("the commander leaves a seat it is relieved of"); - stop.store(true, Ordering::SeqCst); - let _ = reader; + said(&mut fake.out, ToSeat::Refused("relieved: a person took the seat".into())); + fake.done.recv_timeout(Duration::from_secs(5)).expect("the commander leaves a seat it is relieved of"); + fake.stop.store(true, Ordering::SeqCst); +} + +#[test] +fn a_commander_with_an_advisor_that_never_answers_still_orders_every_game_second() { + // The advisor is a process that answers the start and then sleeps: the commander must not wait. + let sleeper = script("advisor-sleeper", "read s; echo '\"ok\"'\nwhile read l; do sleep 60; done"); + let mut fake = Fake::start(AdvisorChoice::Command(sleeper)); + let started = Instant::now(); + let mut batches = 0; + let mut seen = std::collections::BTreeSet::new(); + // 12 game seconds in about 2 wall seconds. + for _ in 0..48 { + fake.tell(); + std::thread::sleep(Duration::from_millis(40)); + while let Ok((drone, ..)) = fake.orders.try_recv() { + if !seen.insert(drone) { + batches += 1; + seen.clear(); + seen.insert(drone); + } + } + } + assert!(batches >= 8, "{batches} decisions in {:?}: the commander waited for its advisor", started.elapsed()); + fake.stop.store(true, Ordering::SeqCst); +} + +#[test] +fn a_proposal_that_arrives_from_a_station_advisor_is_taken_by_the_commander() { + let east = Game::new(Rules::default()).tower("east"); + // The advisor sends the first drone to the east tower, good for ever. The commander's own + // order for the first drone is the west tower. + let advisor = script( + "advisor-proposes", + &format!( + "read s; echo '\"ok\"'\nwhile read l; do echo '[{{\"drone\":0,\"order\":{{\"Take\":{east}}},\"queue\":[],\"gain\":9.0,\"made\":0.0,\"until\":1000.0,\"why\":\"test\"}}]'; done" + ), + ); + let mut fake = Fake::start(AdvisorChoice::Command(advisor)); + let end = Instant::now() + Duration::from_secs(15); + let mut first = vec![]; + loop { + assert!(Instant::now() < end, "the first drone was given {first:?}"); + fake.tell(); + while let Ok((drone, order, _)) = fake.orders.recv_timeout(Duration::from_millis(60)) { + if drone == 0 { + first.push(order); + } + } + if first.last() == Some(&Order::Take(east)) { + break; + } + } + assert!(first[0] != Order::Take(east), "it started with its own order: {first:?}"); + fake.stop.store(true, Ordering::SeqCst); } diff --git a/domains/recreation/towers_server/src/main.rs b/domains/recreation/towers_server/src/main.rs index 01b77e2..d09186d 100644 --- a/domains/recreation/towers_server/src/main.rs +++ b/domains/recreation/towers_server/src/main.rs @@ -1,6 +1,6 @@ //! The game server of Towers (see plans/towers.md): `cargo run --release -p //! recreation_towers_server -- [--speedup N] [--blue computer|person|nobody] [--red computer|person|nobody] -//! [--on-leave computer|wait]`. +//! [--on-leave computer|wait] [--blue-advisor ADVISOR] [--red-advisor ADVISOR]`. //! //! Each drone has an ArduPlane SITL of its own, flying the plane of its class (`recreation_towers::Class`: standard T1-S VTOLs, a heavy T2 Cruza and a light Seeker 3) (MAVLink system ids 1 to 10), with //! the AGENT mode and `ardupilot/towers.parm`. The SITL instances are 30 to 39 @@ -18,6 +18,13 @@ //! `nobody` one that gives no orders. What the server does itself is the flight of the light quads //! from the orders the seat gave, the take-off, the referee and the air. //! +//! A computer commander has an advisor beside it, on a thread of its own: it never waits for it, and +//! takes the proposals that have arrived (plans/actors.md). `--blue-advisor` and `--red-advisor` +//! choose each computer team's: `lookahead` (the default), `none`, or a command that serves an advisor +//! (run with `sh -c`: `towers_advisor`, `"towers_advisor --claude"`, which costs money). An advisor +//! that dies, hangs or writes nonsense is dropped with a line in the log, and the game goes on. A +//! person's seat runs its own advisors in the commander app. +//! //! A seat changes hands during the game. A person who joins a team that a computer commands takes //! the seat (the computer is relieved; the person's seat starts with the orders the drones have). When //! a person leaves, the seat is free; a second later, with `--on-leave computer` (the default), a @@ -36,6 +43,7 @@ mod world; use glam::Vec3; use recreation_games::Radio; use recreation_towers::{Advisor, Class, Outcome, Rules, Team}; +use recreation_towers_ground::advisors::AdvisorChoice; use recreation_towers_ground::commander::{self, Brain}; use recreation_towers_ground::station::Tally; use simulation_sitl::{Lockstep, launch, port_for, sensor_json}; @@ -69,6 +77,8 @@ struct Args { /// A computer takes the seat that a person leaves. on_leave_computer: bool, stop_with_stdin: bool, + /// The advisor beside each team's computer commander. + advisors: [AdvisorChoice; 2], } fn args() -> Args { @@ -86,6 +96,14 @@ fn args() -> Args { nobody: [nobody("--blue"), nobody("--red")], on_leave_computer: arg("--on-leave").is_none_or(|v| v != "wait"), stop_with_stdin: args.iter().any(|a| a == "--stop-with-stdin"), + advisors: ["--blue-advisor", "--red-advisor"].map(|flag| { + arg(flag).map_or(AdvisorChoice::Lookahead, |v| { + v.parse().unwrap_or_else(|e| { + eprintln!("{flag}: {e}"); + std::process::exit(2); + }) + }) + }), } } @@ -241,12 +259,13 @@ fn main() { // The computer commanders are seats of their own, in this process, over loopback TCP. let tally = Arc::new(Tally::default()); let spawn_commander: Arc = { - let (port_cell, tally) = (port_cell.clone(), tally.clone()); + let (port_cell, tally, advisors) = (port_cell.clone(), tally.clone(), a.advisors.clone()); Arc::new(move |team, brain| { let (addr, tally) = (format!("127.0.0.1:{}", port_cell.load(Ordering::SeqCst)), tally.clone()); + let advisor = advisors[team.index()].clone(); std::thread::Builder::new() .name(format!("commander-{team:?}")) - .spawn(move || commander::run(&addr, team, brain, Some(tally), &AtomicBool::new(false))) + .spawn(move || commander::run_with(&addr, team, brain, Some(tally), &AtomicBool::new(false), &advisor)) .expect("a commander thread"); }) }; @@ -406,6 +425,8 @@ fn main() { println!("RESULT in_agent={in_agent}"); println!("RESULT refused={refused}"); println!("RESULT crashes={}", w.crashes); + println!("RESULT proposals={}", tally.proposals()); + println!("RESULT proposals_taken={}", tally.taken()); println!("RESULT taken_blue={}", w.taken[0]); println!("RESULT taken_red={}", w.taken[1]); let light: Vec = w.of_class(Class::Light).collect(); diff --git a/domains/teleoperation/command/README.md b/domains/teleoperation/command/README.md index a3cbe68..63fa253 100644 --- a/domains/teleoperation/command/README.md +++ b/domains/teleoperation/command/README.md @@ -60,6 +60,14 @@ tree); LOW again makes it fly high. The drone's orders after that keep the setti and NONE do what Ctrl/Cmd+A, Space and Esc do. Until you give a drone an order, it circles over your base (HOME). +## Advice + +Beside your commander runs an advisor (the lookahead; `--advisor none|lookahead|COMMAND` chooses, see +`domains/recreation/README.md`). The ADVISOR panel, top left of the battlefield, lists what it +proposes now, in green, best first, with the seconds left: click a line, or press Y for the first, +and the drone gets that order. A proposal that is stale leaves the panel. The amber lines are the +older warnings in words. + ## Tests `cargo run --release -- --expect` runs the app's tests of the "Towers" section diff --git a/domains/teleoperation/command/src/expect.rs b/domains/teleoperation/command/src/expect.rs index b5f39a1..df8045e 100644 --- a/domains/teleoperation/command/src/expect.rs +++ b/domains/teleoperation/command/src/expect.rs @@ -83,6 +83,9 @@ const EVENTS: &[&str] = &[ "the commander clicks the camera of the second Blue drone", "the game runs for 2 minutes", "the game runs for 3 minutes", + "the time of the proposal is over", + "the commander clicks the proposal", + "the commander presses the accept key", "the commander waits for the battlefield", "the commander clicks the west tower on the minimap", "the commander pans the battlefield to the front", @@ -107,6 +110,7 @@ const OBSERVED: &[&str] = &[ "the order of the second Blue drone", "the order of the third Blue drone", "the flight of the first Blue drone", + "the proposal in the ADVISOR panel", "the camera feeds in the app", "the drones that the app flies in AGENT mode", "the position reports that the app heard from its drones", @@ -274,7 +278,61 @@ struct AppDriver { suite: suite_budget::Budget, } +/// A proposal as the spec words it: `the west tower for the first Blue drone, 6 points`, and `, good +/// for 3 seconds` if it is good for less than 20 s. +fn proposal_words(p: &recreation_towers::Proposal, view: &TeamView) -> String { + let Order::Take(t) = p.order else { return "something else".into() }; + let life = p.until - p.made; + format!( + "the {} tower for {}, {:.0} points{}", + view.towers[t].name, + DRONES.get(p.drone).map_or("another drone", |d| d), + p.gain, + if life < 20.0 { format!(", good for {life:.0} seconds") } else { String::new() } + ) +} + +/// What a spec's proposal says: the tower, the drone, the points and how long it is good for (s). +fn proposal_of(words: &str) -> Option<(&str, usize, f32, f32)> { + let rest = words.strip_prefix("the ")?; + let (tower, rest) = rest.split_once(" tower for ")?; + let k = DRONES.iter().position(|d| rest.starts_with(&d.to_lowercase()))?; + let (_, rest) = rest.split_once(", ")?; + let (points, rest) = rest.split_once(" points")?; + let life = rest + .strip_prefix(", good for ") + .and_then(|r| r.strip_suffix(" seconds")) + .and_then(|n| n.parse().ok()) + .unwrap_or(60.0); + Some((tower, k, points.parse().ok()?, life)) +} + impl AppDriver { + /// A proposal reaches the app, as if an advisor had sent it (`words` as in the spec): the + /// ADVISOR panel lists it until its time is over. + fn propose(&self, words: &str) -> Result<(), String> { + let (tower, drone, gain, life) = proposal_of(words).ok_or_else(|| format!("no proposal like {words}"))?; + let tower = tower.to_string(); + let ok = self.call(move |v, _, cx| { + let view = v.seat.state().view.clone()?; + let t = view.towers.iter().position(|x| x.name == tower)?; + v.seat.offer([recreation_towers::Proposal { + drone, + order: Order::Take(t), + queue: vec![], + gain, + made: view.time, + until: view.time + life, + why: format!("Send drone {} to the {tower} tower: about {gain:.0} points.", drone + 1), + }]); + cx.notify(); + Some(()) + })?; + ok.ok_or_else(|| format!("the app has no map or no tower for {words}"))?; + std::thread::sleep(Duration::from_millis(200)); + Ok(()) + } + /// Runs `f` on the UI thread, with the app's view, and returns what it returns. fn call( &self, @@ -620,6 +678,9 @@ impl Driver for AppDriver { if is(subject, "the battlefield picture in the app") && text(value) == "live" { return if self.battlefield_live()? { Ok(()) } else { Err("the 3D view sent no picture".into()) }; } + if is(subject, "the proposal in the ADVISOR panel") { + return self.propose(&text(value)); + } if is(subject, "the order of the first Blue drone") && text(value) == "circle a point" { // As a commander does: B1 selected, and a circle drawn. self.click_button("drone-0")?; @@ -677,7 +738,8 @@ impl Driver for AppDriver { fn can_arrange(&self, var: &Variable, value: &Value) -> bool { let subject = var.subject.as_str(); - START.iter().any(|(s, v)| is(subject, s) && text(value) == v.to_lowercase()) + is(subject, "the proposal in the ADVISOR panel") && proposal_of(&text(value)).is_some() + || START.iter().any(|(s, v)| is(subject, s) && text(value) == v.to_lowercase()) || is(subject, "the selected drone") || CAMERA.iter().any(|c| is(subject, c)) || (is(subject, "the first Blue drone") && text(value) == "in the west circle") @@ -745,6 +807,9 @@ impl Driver for AppDriver { self.until(ANSWER, "the server's answer to LOW", |v| v.drones.first().is_some_and(|d| d.low)) } "the commander clicks the camera of the second blue drone" => self.click_button("camera-1"), + // The panel's first line is the proposal that gains most. + "the commander clicks the proposal" => self.click_button("proposal-0"), + "the commander presses the accept key" => self.press_key("y"), // The check that follows waits for the picture. "the commander waits for the battlefield" => Ok(()), "the commander clicks the west tower on the minimap" => self.click_minimap_tower("west"), @@ -810,17 +875,26 @@ impl Driver for AppDriver { "the commander centres the battlefield on the selected drone" => self.press_key("space"), "the commander draws a circle of 120 m on the battlefield" => self.draw_circle(120.0), "the commander adds a point to the route of the selected drone" => self.add_route_point(), + // The game's clock runs past the time of the proposal that the panel lists. + "the time of the proposal is over" => { + let (now, until) = self.call(|v, _, _| { + let s = v.seat.state(); + let now = s.view.as_ref().map_or(0.0, |v| v.time); + (now, s.proposals.iter().map(|p| p.until).fold(now, f32::max)) + })?; + self.until(GAME, "the proposal's time to be over", move |v| v.time > until + 1.0 || v.time < now) + } e @ ("the game runs for 2 minutes" | "the game runs for 3 minutes") => { - let minutes: f32 = if e.ends_with("2 minutes") { 2.0 } else { 3.0 }; + let seconds: f32 = if e.ends_with("2 minutes") { 120.0 } else { 180.0 }; let from = self.view()?.map_or(0.0, |v| v.time); let end = Instant::now() + self.suite.cap(GAME); loop { let v = self.view()?; - if v.as_ref().is_some_and(|v| v.time >= from + 60.0 * minutes || v.outcome.is_some()) { + if v.as_ref().is_some_and(|v| v.time >= from + seconds || v.outcome.is_some()) { return Ok(()); } if Instant::now() > end { - return Err(format!("the game's clock did not run for {minutes} minutes")); + return Err(format!("the game's clock did not run for {seconds} s")); } std::thread::sleep(Duration::from_millis(200)); } @@ -936,6 +1010,16 @@ impl Driver for AppDriver { std::thread::sleep(Duration::from_millis(250)); } } + if lower == "the proposal in the advisor panel" { + // What the panel lists first: the same list that it draws. + let shown = self.call(|v, _, _| { + let s = v.seat.state(); + let view = s.view.clone()?; + let live = recreation_towers_ground::advisors::listed(&s.proposals, &view); + live.first().map(|p| proposal_words(p, &view)) + })?; + return Ok(Value::from(shown.unwrap_or_else(|| "none".into()))); + } let (status, view, selected) = self.call(|v, _, _| { let s = v.seat.state(); (s.status.clone(), s.view.clone(), v.selected.first().copied()) @@ -1001,6 +1085,7 @@ impl Driver for AppDriver { || is(&v.subject, "the flight of the first Blue drone") || is(&v.subject, "the points in the route of the first Blue drone") || is(&v.subject, "the battlefield picture in the app") + || is(&v.subject, "the proposal in the ADVISOR panel") } Need::Set(v) => is(&v.subject, "the selected drone") || CAMERA.iter().any(|c| is(&v.subject, c)), Need::Perform(e) => EVENTS.iter().any(|s| is(&e.text, s)), @@ -1163,3 +1248,23 @@ mod room_tests { assert!(!room_to_circle(&map, near, 120.0), "but a circle round it reaches the mountains"); } } + +#[cfg(test)] +mod proposal_tests { + use super::*; + + #[test] + fn a_proposal_is_worded_as_the_spec_words_it_and_read_back() { + assert_eq!( + proposal_of("the west tower for the first blue drone, 6 points"), + Some(("west", 0, 6.0, 60.0)), + "a proposal is good for a minute unless the spec says" + ); + assert_eq!( + proposal_of("the west tower for the first blue drone, 6 points, good for 3 seconds"), + Some(("west", 0, 6.0, 3.0)) + ); + assert_eq!(proposal_of("none"), None); + assert_eq!(proposal_of("the west tower for the sixth blue drone, 6 points"), None); + } +} diff --git a/domains/teleoperation/command/src/main.rs b/domains/teleoperation/command/src/main.rs index 039804b..2eb7b43 100644 --- a/domains/teleoperation/command/src/main.rs +++ b/domains/teleoperation/command/src/main.rs @@ -24,6 +24,7 @@ use gpui::{ }; use recreation_towers::Team; use recreation_towers::seat::{Join, PORT}; +use recreation_towers_ground::advisors::AdvisorChoice; use std::path::PathBuf; use std::sync::{Arc, Mutex}; @@ -43,6 +44,9 @@ sim001_command [options] each on a fresh game of its own server (needs the AGENT build of ArduPlane), and exit --platform field with --expect: read the suite as the field and run it on the bench (a ground radio and a flight controller: SIM001_FIELD_RADIO, SIM001_FIELD_FC); without them, nothing is supported yet + --advisor NAME the advisor beside your commander: lookahead (the default), none (the default with --expect, which puts its own proposals in the panel), or a command that + serves an advisor, such as towers_advisor --claude (costs money). Its proposals + are in the ADVISOR panel; press Y or click one to give that order --only TEXT with --expect: run only the tests whose title contains TEXT --docs DIR with --expect: write the results and screenshots of the window to DIR (merged with the others')"; @@ -118,6 +122,14 @@ fn main() { // A seat that commands is the team's ground station: it flies the team's drones itself. if matches!(join, Join::Command(_)) { station::start(seat.clone(), addr.rsplit_once(':').map_or("127.0.0.1", |(h, _)| h).to_string(), None); + // Its advisors run beside it, and what they propose is listed in the ADVISOR panel. + let advisor = arg("--advisor") + .map_or(Ok(if expect { AdvisorChoice::None } else { AdvisorChoice::Lookahead }), |a| a.parse()) + .unwrap_or_else(|e: String| { + eprintln!("--advisor: {e}"); + std::process::exit(2); + }); + recreation_towers_ground::advisors::attend(seat.clone(), advisor); } // The pictures from the team's drones: the 3D view of the game is on the server's machine. let host = addr.rsplit_once(':').map_or("127.0.0.1", |(h, _)| h).to_string(); diff --git a/domains/teleoperation/command/src/ui.rs b/domains/teleoperation/command/src/ui.rs index 695f208..c77bd74 100644 --- a/domains/teleoperation/command/src/ui.rs +++ b/domains/teleoperation/command/src/ui.rs @@ -27,6 +27,7 @@ use gpui::{ use recreation_towers::map::Map; use recreation_towers::seat::Join; use recreation_towers::{Class, Order, Phase, Rules, Team, TeamView}; +use recreation_towers_ground::advisors; use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::rc::Rc; @@ -236,6 +237,14 @@ impl CommanderView { matches!(self.seat.state().status, Status::Seated(Join::Command(_))) } + /// ACCEPT (Y, or a click on a proposal): takes the `nth` proposal of the ADVISOR panel, the one that + /// gains most first. The drone gets that order and queue. Nothing happens if it went stale. + pub fn accept(&mut self, nth: usize) { + if self.commands() { + self.seat.accept(nth); + } + } + /// Selects drone `k` alone, or none. pub fn select(&mut self, k: Option) { self.selected = k.into_iter().collect(); @@ -398,6 +407,7 @@ impl CommanderView { "b" => self.give(Order::Home), "l" => self.toggle_low(), "c" => self.arm_circle(), + "y" => self.accept(0), "escape" => { self.circle.cancel(); self.selected.clear() @@ -807,6 +817,39 @@ impl Render for CommanderView { .child(format!("ADVISOR {line}")), ); } + // The advisors' proposals: one line each, best first. A click on one, or Y for the first, + // gives that order. A stale one is gone. + let proposed: Vec<(String, f32)> = match &view { + Some(v) if self.commands() => advisors::listed(&self.seat.state().proposals, v) + .into_iter() + .map(|p| (p.why.clone(), p.until - v.time)) + .collect(), + _ => vec![], + }; + for (i, (why, left)) in proposed.into_iter().enumerate() { + let key = if i == 0 { " [Y]" } else { "" }; + toasts = toasts.child( + div() + .id(SharedString::from(format!("proposal-{i}"))) + .relative() + .px_2() + .py(px(3.0)) + .bg(ca(0x000000, 0.7)) + .border_l_2() + .border_color(c(GREEN)) + .text_sm() + .text_color(c(GREEN)) + .cursor_pointer() + .hover(|s| s.bg(ca(0x203020, 0.85))) + .child(format!("ADVISOR {why} ({left:.0} s){key}")) + .child(recorder(format!("proposal-{i}").into())) + .occlude() + .on_click(cx.listener(move |this, _, _, cx| { + this.accept(i); + cx.notify(); + })), + ); + } let map = div() .id("map") .relative() diff --git a/expectations/09-towers/05-advice.expect b/expectations/09-towers/05-advice.expect new file mode 100644 index 0000000..686397b --- /dev/null +++ b/expectations/09-towers/05-advice.expect @@ -0,0 +1,41 @@ +Feature: Advice in the app + Beside your commander, an [[advisor]] looks at the map of your team in its own time. By default + it is the lookahead. When it sees a better move, it sends a [[proposal]]. The ADVISOR panel, at + the top left of the battlefield, lists each proposal that is good now. A line gives the + reason, and the time that is left before the proposal is stale. The line with the most points + is the first. + + The advisor does not give orders. You decide. To take a proposal, click its line, or press Y + to take the first one. The drone gets the order and the [[queue]] of the proposal, and the + line leaves the panel. If you do not take a proposal, it goes when its time is over. The + advisor can be slow, so a proposal can be some seconds old when it arrives. + + The warnings of [[The advisor]] stay as they are. They appear as one line each, in amber. The + proposals are green. + + Expectation: The ADVISOR panel is empty when the advisor has no proposal + When the commander joins the game + Then the proposal in the ADVISOR panel is none + + Expectation: The ADVISOR panel lists a proposal with its drone, its order and its points + Given the proposal in the ADVISOR panel is the west tower for the first Blue drone, 6 points + Then the proposal in the ADVISOR panel is the west tower for the first Blue drone, 6 points + + Expectation: A click on a proposal gives its order + Given the proposal in the ADVISOR panel is the west tower for the first Blue drone, 6 points + When the commander clicks the proposal + Then the order of the first Blue drone is take the west tower + And the proposal in the ADVISOR panel is none + + Expectation: The accept key gives the order of the first proposal + Given the proposal in the ADVISOR panel is the west tower for the first Blue drone, 6 points + When the commander presses the accept key + Then the order of the first Blue drone is take the west tower + And the proposal in the ADVISOR panel is none + + Expectation: A proposal that is stale leaves the panel + The proposal is good for 3 seconds. Nobody takes it. + + Given the proposal in the ADVISOR panel is the west tower for the first Blue drone, 6 points, good for 3 seconds + When the time of the proposal is over + Then the proposal in the ADVISOR panel is none diff --git a/expectations/09-towers/section.expect b/expectations/09-towers/section.expect index 577666b..94efa16 100644 --- a/expectations/09-towers/section.expect +++ b/expectations/09-towers/section.expect @@ -284,6 +284,17 @@ Vocabulary: State: the advisor of the computer commander of Blue is the lookahead | none Shows: the lookahead: the computer commander thinks ahead Shows: none: the computer commander does not think ahead + # The ADVISOR panel of the app: the proposals of the advisor beside a person's commander. + State: the proposal in the ADVISOR panel is none | the west tower for the first Blue drone, 6 points | the west tower for the first Blue drone, 6 points, good for 3 seconds + Shows: none: the panel has no green line + Shows: the west tower for the first Blue drone, 6 points: the panel has a green line that sends the first Blue drone to the west tower, and shows the time that is left + Shows: the west tower for the first Blue drone, 6 points, good for 3 seconds: the panel has the same line, and it goes after 3 seconds of the game + Event: the commander clicks the proposal + Say: click the green line of the proposal in the ADVISOR panel + Event: the commander presses the accept key + Say: press Y + Event: the time of the proposal is over + Say: wait until the seconds that the panel shows are 0 # The commander app (domains/teleoperation/command), in the Blue seat of a game on the server. State: the seat of the app is none | Blue | refused Shows: Blue: the top bar of the app says BLUE COMMANDER diff --git a/expectations/glossary.expect b/expectations/glossary.expect index 493463f..f9cf4f0 100644 --- a/expectations/glossary.expect +++ b/expectations/glossary.expect @@ -392,14 +392,18 @@ Term: Advisor In [[Towers]], what helps a [[commander]]: it looks at the map of the team, as the commander does, and sends a [[proposal]] when it sees a better move. It does not give orders. It can be slow: its proposals then reach the commander some seconds late. The first advisor is the lookahead, which plays the - game forward in a small model. A person who commands also has an advisor, which warns of - danger in words. + game forward in a small model. An advisor runs beside the commander, in its own time, and + the commander never waits for it. It can be a program of its own: one that asks Claude, for + example. A person who commands has the same advisors: the ADVISOR panel of the app lists + their proposals, and a click on one gives its order. The person also has an advisor that warns + of danger in words. Term: Proposal In [[Towers]], what an [[advisor]] sends to a [[commander]]: one order for a drone, the [[queue]] that goes with it, the points that the advisor expects to gain by it, and the time after which it is stale. A computer commander takes a proposal that gains more than 2 points - and is not stale. + and is not stale. A person takes a proposal with a click on its line in the ADVISOR panel, or + with the key Y. Term: Unit In [[Towers]], what flies one drone: one for each drone. It runs all the time, in contact or not, diff --git a/plans/actors.md b/plans/actors.md index 7c99036..ee9f297 100644 --- a/plans/actors.md +++ b/plans/actors.md @@ -190,6 +190,41 @@ server. proposals taken, and how many are stale or no longer fit, are not counted). So the lookahead is a plumbing test for advisors, not a gain: it is the model and its enemy guesses that need work, before advice from a slower and better advisor can be told from lateness. + **Results (the seat side: advisors beside the commander, a process advisor, a Claude advisor, + and the app's accept).** Done in `recreation_towers_ground::{advisors, claude}`, the commander + app and the server. An advisor at a seat runs on a thread of its own (`advisors::Runner`) behind + a mailbox: the newest map and hand go in, an older one that was not looked at is dropped, never + queued, and proposals come out when they are ready (`Advisors::take(now)` drops the ones that are + stale on arrival). A computer commander on a seat (`commander::run_with`) has its built-in + lookahead off and never calls an advisor in `decide`: before each decision it puts what has + arrived in hand (`Commander::advise`), after it it shows the advisors the map and + `Commander::hand()` (new: the orders after its rules, the drones it may change, the towers each + keeps away from). `Advise` is now `Send`. Tests: an advisor that sleeps 3 s does not delay ten + decisions (the slowest takes under 100 ms, the advisor looked at one map); a late proposal is + dropped once stale; the mailbox keeps the newest of five maps (looks at the first and the last); + an advisor that panics is dropped; and, through the commander on a fake server, a process that + never answers does not slow its orders, while one that proposes gets its proposal taken. + `ProcessAdvisor` (`sh -c COMMAND`, `serve` for the other end, binary `towers_advisor`): one JSON + line in (`{"Start":{"team","rules"}}` once, answered `"ok"`; then `{"Look":{"view","hand"}}`), + one out (a list of proposals). A process that dies, hangs (90 s), writes what is not a list or + proposes for a drone or tower that is not in the game is dropped with a line, and the game goes + on. The Claude advisor (`towers_advisor --claude`, `SIM001_CLAUDE` for the command) is tested only + against stand-in commands (good, malformed, error, slow, no command): **it has not been run + against the real `claude`**. Flags: `towers_commander --advisor`, `towers_server --blue-advisor` + and `--red-advisor`, the app's `--advisor` (`none` by default under `--expect`). The app lists + the live proposals in the ADVISOR panel (best first, seconds left, a click or Y takes it; the + text warnings stay) and `expectations/09-towers/05-advice.expect` covers it. + + On SITL (the server's acceptance test, the lookahead as a station advisor on both sides, two + games at 10x): both games play to the end and both teams take towers (Blue 1 and Red 2; Blue 2 + and Red 1). The server's log shows the proposals arriving and the commanders taking some: 91 + proposals and 61 taken in the first game, 86 and 44 in the second (`RESULT proposals`, + `proposals_taken`; "taken" counts a proposal whose drone and order the commander's decision then + had, so one that matched its own order anyway is counted too). Failing: "No drone crashes" (7 and + 3), the known one, and in the first game "the commands that the drones refused are 0" (1 refused; + 0 in the second game, and not seen again: not looked into). The app's expectations ran in the + real window (`--expect --only proposal --no-cameras`): the four tests of 05-advice pass; the + empty-panel check inside the story "Your first tower" was not run (it needs the cameras). 5. **A person flies a unit.** A `Unit` seat. The server gives that drone's radio channels to the seat: the sim (or a TX15 on CRSF) joins a game as one drone, with the drone's camera and the mission on its HUD. The pilot sees only what the drone sees. Take-off is the pilot's.