CB-WP-0018-T01: the browser sees the end of its own game

Server::serve_end plus doc::ending, wired into both of run_game's exits.
Where the browser used to get Connection refused it now gets the ending
page with the result and the final table. It serves until the page posts
'done' (the page carries a close control), with a 600s linger so an
abandoned tab cannot hold the process open.

document() split into body() and move_section() so the ending shows the
same table rather than a second rendering of it.

The control had to be built twice and the first was worthless:
the_end_of_the_game_reaches_the_browser calls serve_end directly, and
deleting the call from run_game left it GREEN -- it tested the link and
not the chain, which is CB-EV-0012's finding recurring.
a_real_game_played_to_its_end_leaves_the_ending_on_screen runs the real
play() with a browser seat, drives a real game to its end over a real
socket, and goes red under that mutation printing an empty page -- the
reported symptom exactly.

A weak assertion of mine caught by itself: the first draft grepped the
page for location.reload, which would have forced a second script to
satisfy a test rather than a requirement. It now asserts the ending
endpoint cannot answer 'ok'.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tegwick 2026-08-03 02:08:04 +02:00
parent 78e82497ee
commit 57639623da
4 changed files with 433 additions and 17 deletions

View file

@ -181,6 +181,29 @@ impl<R: BufRead, W: Write> Policy for HumanPolicy<'_, R, W> {
}
}
/// How long the terminal page stays available for an abandoned tab.
///
/// A server that never exits is its own defect; a short timeout races a
/// player reading the result. So it serves until the page posts `done`,
/// with this only as the bound.
const END_LINGER: std::time::Duration = std::time::Duration::from_secs(600);
/// Show the browser that the game ended badly, then return the error.
///
/// CB-WP-0018 T01: this path used to `return Err(...)` straight to a
/// terminal nobody was reading, and the browser got a refused connection —
/// identical to a normal win. An error the only interface cannot see is
/// not reported.
fn end_badly<T>(
server: &Option<std::rc::Rc<crate::hotseat::Server>>,
msg: String,
) -> Result<T, String> {
if let Some(s) = server {
let _ = s.serve_end(None, &msg, END_LINGER);
}
Err(msg)
}
fn bot_policy<'a>(kind: &str, seed: u64) -> Result<Box<dyn Policy + 'a>, String> {
match kind {
"greedy" => Ok(Box::new(GreedyPolicy)),
@ -250,14 +273,17 @@ fn run_game<'a, R: BufRead + 'a, W: Write + 'a>(
// out-of-range index it had to return to get here.
let human_failure = failure.borrow().clone();
let game = match (result, human_failure) {
(_, Some(msg)) => return Err(msg),
(_, Some(msg)) => return end_badly(&server, msg),
(Err(BotError::IllegalChoice { seat, offered, .. }), None) => {
return Err(format!(
"{} chose a command outside the {offered} offered",
seat_name(seat)
))
return end_badly(
&server,
format!(
"{} chose a command outside the {offered} offered",
seat_name(seat)
),
)
}
(Err(e), None) => return Err(e.to_string()),
(Err(e), None) => return end_badly(&server, e.to_string()),
(Ok(game), None) => game,
};
@ -287,6 +313,19 @@ fn run_game<'a, R: BufRead + 'a, W: Write + 'a>(
let _ = w.flush();
drop(w);
// CB-WP-0018 T01: the browser sees the end of its own game. Measured
// before this: a game ended at 5 rounds / 30 commands, the result went
// to stdout, and the page's post-`ok` reload got Connection refused.
if let Some(srv) = &server {
let ended = game.state.project(Viewer::Spectator);
let msg = format!(
"{} commands, hash {}",
game.commands,
&end_hash[..12]
);
let _ = srv.serve_end(Some(&ended), &msg, END_LINGER);
}
let scenario = games_ground::record::to_scenario(
"ground/cb-play-session",
config.seed,