diff --git a/README.md b/README.md index 530c167..a63ce1c 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,10 @@ needed, or download it for your desktop below. Download the package for your platform from the [Releases](https://github.com/schreck61/intersection-lab/releases) page: a universal DMG for macOS, an MSI (or portable zip) for Windows on x64 and ARM64, and `.deb`, `.rpm` or -`.tar.gz` for Linux on x86_64 and aarch64. All are built and signed by GitHub Actions; see -[packaging/README.md](packaging/README.md) for how releases are made and verified. +`.tar.gz` for Linux on x86_64 and aarch64. All are built by GitHub Actions and come with a +signed build-provenance attestation, and the macOS app and the Windows executable and +installer are also code-signed; see [packaging/README.md](packaging/README.md) for how +releases are made and verified. If you have a current Rust toolchain, you can instead build and install it from [crates.io](https://crates.io/crates/intersection-lab): diff --git a/SECURITY.md b/SECURITY.md index e74379f..39069e4 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -16,12 +16,14 @@ it, and what it doesn't protect against: it has loaded. * **No extra privileges.** Once installed, the app runs with your ordinary user rights and never asks for more. -* **Only its own files.** The app reads trained-weights files: from a `weights` folder in - the folder it's started from, then from its folder in your user data directory, and - otherwise from the copies built into it. It writes them only when you save weights. - The `train` tool writes the weights it trains to a `weights` folder in the folder it's - run from, `bench` writes no files, and the app's screenshot mode (`TS_SHOT`) writes - only the image you name. +* **Only its own files.** The app reads trained-weights files: from a `weights` folder + in the folder it's started from, then from its folder in your user data directory, and + otherwise from the copies built into it. It writes them only when you save weights. To + draw its window, the desktop app also reads the fonts installed on your system and, on + Linux, desktop settings such as the mouse-pointer theme, and it loads the system's + windowing and graphics libraries. The `train` tool writes the weights it trains to a + `weights` folder in the folder it's run from, `bench` writes no files, and the app's + screenshot mode (`TS_SHOT`) writes only the image you name. * **Files are checked before use.** A weights file is parsed into typed data, and its network is checked against what its controller accepts (layer sizes, connections between layers, finite values). A file that doesn't parse is skipped for the next @@ -31,9 +33,11 @@ it, and what it doesn't protect against: controller manage traffic badly, so use weights only from sources you trust. * **No code from data.** The app has no plugins, scripting or other way to run code from the files it reads, and its own code is safe Rust with no `unsafe` blocks. -* **Verifiable releases.** Every release file is signed and comes with a - build-provenance attestation (see [Verifying downloads](#verifying-downloads)). - Packages from anywhere else aren't covered. +* **Verifiable releases.** Every release package comes with a signed build-provenance + attestation. The macOS disk image and the app inside it, and the Windows executable + and installer, also carry their platform's signature (see + [Verifying downloads](#verifying-downloads)). Packages from anywhere else aren't + covered. It doesn't protect against someone who can already change files in your user account, who could replace the app or its weights, and a very large weights file is read whole @@ -80,8 +84,9 @@ Out of scope: unrealistic simulation behavior or results. Those are welcome as o ## Verifying downloads -Every release includes `SHA256SUMS.txt` and a signed build-provenance attestation for -each file. To check that a download was built by this repository's release workflow: +Every release includes `SHA256SUMS.txt` and a signed build-provenance attestation that +covers each package and that checksum file. To check that a download was built by this +repository's release workflow: ```bash gh attestation verify --repo schreck61/intersection-lab @@ -92,6 +97,6 @@ pass it with `--bundle` to verify without fetching it from GitHub. The release's `IntersectionLab-.intoto.jsonl` is an identical copy, named the way supply-chain scanners such as the OpenSSF Scorecard expect provenance to be. -The macOS app is also signed with a Developer ID and notarized, and the Windows -executable and installer are signed with Azure Artifact Signing. See -[packaging/README.md](packaging/README.md) for details. +The macOS disk image and the app inside it are also signed with a Developer ID and +notarized, and the Windows executable and installer are signed with Azure Artifact +Signing. See [packaging/README.md](packaging/README.md) for details. diff --git a/deny.toml b/deny.toml index fe7f371..ff2158a 100644 --- a/deny.toml +++ b/deny.toml @@ -29,8 +29,12 @@ confidence-threshold = 0.9 # this project. osv-scanner.toml ignores the same IDs for the OpenSSF Scorecard; # remove an entry from both files once no dependency needs the crate (cargo-deny # warns when an ignored advisory no longer applies). +# +# A locked version that has been yanked from crates.io fails the check too, whether +# or not an advisory exists for it. Update the crate, or, while no usable version +# exists, ignore it below as { crate = "name@version", reason = "..." }. unmaintained = "all" -yanked = "warn" +yanked = "deny" ignore = [ { id = "RUSTSEC-2026-0192", reason = "ttf-parser is unmaintained, with no known flaw; only the Linux build uses it, for the Wayland window title (winit 0.30, sctk-adwaita, ab_glyph), and no winit 0.30 release drops it" }, { id = "RUSTSEC-2026-0370", reason = "zvariant is unmaintained, with no known flaw, since it was merged into zbus 6; only the Linux build uses it, for AccessKit's screen-reader bridge, which still needs zbus 5" }, diff --git a/docs/architecture.md b/docs/architecture.md index 2fa3b4c..de6178b 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -236,10 +236,10 @@ docs/ this document, insights.md, cooperative-intersections.md (wi | `src/control/` | 8 | 1,608 | | `src/rl/` | 3 | 361 | | `src/gui/` | 5 | 4,905 | -| `src/bin/` | 2 | 693 | +| `src/bin/` | 2 | 727 | | `src/lib.rs`, `src/main.rs` | 2 | 96 | | `examples/` | 9 | 999 | -| `tests/` | 11 | 4,187 | +| `tests/` | 11 | 4,271 | | `fuzz/fuzz_targets/` | 1 | 61 | | Target | Built from | Purpose | @@ -1776,7 +1776,7 @@ Hybrid's major/minor and T-intersection files hold only its best static tuning | File | Lines | What it holds | |---|---:|---| -| `src/bin/train.rs` | 441 | Training, model selection and the test report | +| `src/bin/train.rs` | 475 | Training, model selection and the test report | | `rl/nn.rs` | 190 | `Mlp`, `Grads` and `Adam` | | `rl/dqn.rs` | 166 | `Dqn`, `Transition`, `SavedModel` and `argmax_masked` | | `rl/mod.rs` | 5 | The module list | @@ -1985,7 +1985,8 @@ tuning. ### 8.5 `bin/train.rs`: training and model selection `train [layout|all] [episodes] [episode-seconds] [dqn|hybrid|both]` trains on the -signalized layouts. The defaults are `all 200 900 both`; the README's commands train +signalized layouts. A name that matches no signalized layout stops it with an error +before it makes `weights/`. The defaults are `all 200 900 both`; the README's commands train 800 episodes of 900 s. It runs one thread per layout and controller, so `train all` trains eight agents at once, and the private `Agent` enum wraps the two controllers behind one interface. It moves the boxed controller into a `Sim`, runs an episode, and @@ -2253,7 +2254,7 @@ flowchart TB * **What happens when a check fails:** nothing is logged, and the next copy is tried, so a damaged file doesn't hide a good copy further down the list. A Hybrid file whose network doesn't fit still loads, without the network, since its base tuning - can still run. + can still run. A base tuning past the last of the 48 is read as the last one. | The copy | Deep RL | Hybrid | |---|---|---| @@ -2269,11 +2270,15 @@ tuning". A Hybrid that loaded only a base tuning says "no network beat the best tuning" when its file has no network, the same as for the shipped static files, and "the saved network doesn't fit this layout" when the file's network was left out. -Saving goes through `save_path`. It writes to `weights/` in the working directory when -that directory exists, which, run from a checkout, overwrites the shipped files. -Otherwise it writes to the user data directory, creating it first, or to `weights/` when -there is no user data directory. `train` creates `weights/` when it saves, and the next -build embeds whatever it wrote. +Saving from the app goes through `save_path`. It writes to `weights/` in the working +directory when that directory exists, which, run from a checkout, overwrites the shipped +files. Otherwise it writes to the user data directory, creating it first, or to +`weights/` when there is no user data directory. `train` doesn't use `save_path`: it +writes only to `weights/` in the working directory, which it creates before it trains, +and stops with an error if it can't make the folder or write a file. Unlike `save_path`, +it doesn't turn to the user data directory when `weights/` is missing. It does write +over files of the same name already in `weights/`, including ones saved there from the +app. The next build embeds whatever `train` wrote. A Hybrid file also records `base_score`, the validation person delay of what it holds, when there is one. The app's *Save weights* measures no score, and when no @@ -2295,9 +2300,9 @@ Weights files can be damaged or edited by hand, so their loading is tested and f * `tests/weights_files.rs` checks that every shipped file loads, that seven kinds of misshapen network are rejected (one with a weight of 10³⁹, which is infinite as an `f32`), that a Hybrid whose network doesn't fit keeps its base tuning and says so, - that Hybrid files saved with a score that isn't finite load again, that a copy that - doesn't load is skipped for the next one and that networks whose outputs overflow - don't crash anything. + that a base tuning out of range is read as the last one, that Hybrid files saved + with a score that isn't finite load again, that a copy that doesn't load is skipped + for the next one and that networks whose outputs overflow don't crash anything. * The `weights` fuzz target reads each input as both kinds of file for the T-intersection. A file that is accepted gets a training step if it has a network, then 20 s of simulation. The CI job "Fuzz (weights)" runs it for 60 s, starting from @@ -3352,12 +3357,12 @@ release mode on Linux, macOS and Windows, and measures how much of the crate the | `tests/cooperative.rs` | 14 | The cooperative crossing: no signals, sensors or crosswalks; its geometry and phase order; every gap policy safe under heavy demand, bursts, heavy vehicles and live switching; about 18,000 vehicles per hour served at the design demand, about 19,000 oversaturated and less with single left lanes or first come, first served; reproducible runs; light traffic barely delayed; emergency vehicles first; hand-placed vehicles crossing every way. Every run, except those of the no-signals, single-left-lane and reproducibility tests, must show no overlap between vehicle bodies, no zone entered while occupied, no box entry without a reservation, no step off a granted trajectory, no crossing inside its required gap and no line or zone time off its reservation (both to within a microsecond), and at least one crossing measured | | `tests/failures.rs` | 20 | Failures at the cooperative crossing. When a vehicle stops dead at the worst moment, fail-safe gaps stop crossing traffic short of it under both schedules, mitigated gaps let it arrive at no more than 25 km/h and nominal gaps faster; a vehicle that brakes to a stop is survived; in batches a failure at a random place rarely reaches crossing traffic, though a dead stop is struck from behind; the crossing serves what a disabled vehicle leaves clear and recovers once it is cleared away; a failure outside the box only holds up its lane; a failure can be repeated at the same place on another run, and random failures are the same on runs with the same seed; twenty minutes of failures in heavy traffic leave nothing broken, and with fail-safe gaps no vehicles on crossing paths touch through some two hundred dead stops; a vehicle stopped in the box waits for one that passes it, one stopped behind a failure can fail in turn and one that fails at the edge of the map rolls off it; a failure only holds back the vehicles that leave their crossings for an emergency stop: none at rest is set going, none gets ahead of the trajectory it had, and one standing before its line loses its crossing instead; a vehicle that can't stop behind a dead stop is expected where it ends up, as is one that fails close behind it and one already on an emergency stop when the vehicle ahead stops short, and it stops by itself if the wreck is cleared away first; the log keeps the latest fifty failures; other layouts have none. Every run with a failure in it, except those of the test that repeats one, must show no vehicle off its trajectory, in the box without one, or overlapping another outside a recorded contact | | `tests/pedestrian_rule.rs` | 4 | Deep RL's shield serves a crosswalk whose pedestrians have waited longer than `PED_MAX_WAIT`, longest wait first, and leaves shorter waits to the controller; the reward counts waiting pedestrians | -| `tests/weights_files.rs` | 6 | The shipped weights load, misshapen networks are rejected, a Hybrid without a fitting network keeps its base tuning and says so, Hybrid files saved with a score that isn't finite load again, a copy that doesn't load is skipped for the next one and networks that overflow don't crash (§8.7) | +| `tests/weights_files.rs` | 6 | The shipped weights load, misshapen networks are rejected, a Hybrid without a fitting network keeps its base tuning and says so, a base tuning out of range is read as the last one, Hybrid files saved with a score that isn't finite load again, a copy that doesn't load is skipped for the next one and networks that overflow don't crash (§8.7) | | `tests/green_wave.rs` | 2 | The green wave runs every signal of a corridor fixed-time, and ending it gives back the controllers they ran before, except where another was chosen during the wave, without collisions; a single intersection isn't coordinated | | `tests/networks.rs` | 7 | Backpropagation agrees with the gradient measured numerically, Adam fits a line, the shape check rejects every malformed network (sizes whose product overflows included), the target network moves part way toward the online one, the action mask is respected, Double DQN learns which action pays in which state and the replay memory keeps the latest 60,000 transitions | | `tests/gui.rs` | 22 | The app, run without a window by `egui_kittest` and driven through its accessibility tree, the keyboard and the pointer: every panel opens and every control has a name; Space pauses and Step advances one second; every layout, control mode and controller runs, side by side too and along a corridor with the green wave; the demand, sensor and operations settings apply; the learning controllers show what they learn; every overlay and coloring draws; vehicles and pedestrians placed by hand, movements an approach lacks refused; a clicked vehicle's details; a vehicle of the cooperative crossing made to fail from the buttons and by a click, on both sides of a comparison; what *Stall one in the box* does on an empty crossing and once every lane with a vehicle on its way has a failure set; what a failed vehicle, one stopped behind it, one on its way out of the box and one whose crossing was withdrawn say when selected; pan, zoom and *Fit view*; both benchmark windows; *About* | | `tests/scripted.rs` | 9 | Each `TS_*` variable of the scripted mode sets up its scene, a scripted screenshot stops the clock at its moment and screenshots are valid PNG files. The app reads the variables as it starts, so each scene opens in a process of its own | -| `tests/tools.rs` | 7 | `bench` compares every strategy on every layout without collisions, takes each arrival pattern and runs the sensor study; `train` trains, saves and resumes both learning controllers, in a scratch directory with its own home directory | +| `tests/tools.rs` | 10 | `bench` compares every strategy on every layout without collisions, takes each arrival pattern and runs the sensor study; `train` trains, saves and resumes both learning controllers, in a scratch directory with its own home directory, stops without touching the weights saved from the app when it can't make its `weights` folder, stops instead of reporting a result when it can't write the Hybrid's weights file, and stops with an error, making no folder, when no signalized layout has the name it is given | | `src/bin/bench.rs` | 1 | A run for a strategy table counts a vehicle placed on top of another, so the `coll` column can show an overlap | | `src/sim/coop.rs` | 4 | Approach profiles arrive on time and at speed, a short run-up enters slower but on time, the gap policies give the gaps in §6.11 and the following law holds its time gap | | `src/sim/coop/failures.rs` | 6 | An emergency stop ends at rest after its braking distance, a trajectory holds the zones it rests in without end, a vehicle that can't stop behind another is expected at its rear and holds the zones it will stand in, a vehicle can be put anywhere along its route and passes clear of one at rest on another approach, a vehicle whose crossing was withdrawn brakes for its line while it doesn't yet respond to the vehicle ahead, and every gap allows the reaction time | @@ -3428,7 +3433,9 @@ date: against the version and runs `cargo publish` through crates.io's Trusted Publishing. * **`scorecard.yml`** runs the OpenSSF Scorecard on every push to `main`, every Monday and when branch protection changes. -* **Dependabot** proposes updates to the actions and the Cargo dependencies weekly. +* **Dependabot** proposes updates to the actions and to the dependencies named in + `Cargo.toml` weekly, and a security update for a version in `Cargo.lock` that GitHub + has an advisory for, when a fixed version exists. | Platform | Packages | Built by | Signing | |---|---|---|---| diff --git a/docs/assurance-case.md b/docs/assurance-case.md index 8dbe84b..836e65b 100644 --- a/docs/assurance-case.md +++ b/docs/assurance-case.md @@ -24,7 +24,7 @@ server side, so the threats that matter are these: | Threat | Example | Main defenses | |---|---|---| | Malicious data file | A crafted weights file, received from someone or placed in a `weights` folder | Typed parsing, checks on shape and values, fuzzing, safe Rust | -| Tampered download | A modified package from a mirror or a look-alike site | Signed packages, build-provenance attestations, checksums, HTTPS | +| Tampered download | A modified package from a mirror or a look-alike site | Signed build-provenance attestations, checksums, code signing on macOS and Windows, HTTPS | | Vulnerable or malicious dependency | A crate or GitHub Action with a known flaw, or a hijacked release of one | Few dependencies, locked versions, `cargo deny` with the RustSec database, actions pinned to commit hashes, Dependabot | | Malicious contribution | A pull request that hides harmful code or attacks CI | Required checks, the maintainer's review of outside contributions, read-only tokens for pull requests from forks, CodeQL for workflows | | Stolen release credentials | Misuse of signing or publishing access | Short-lived credentials (OIDC, Sigstore, crates.io Trusted Publishing) and secrets limited to version-tag builds | @@ -38,8 +38,11 @@ signing services). 1. **Files on disk → the app.** Weights files are untrusted input, whichever folder they come from: anyone who can write to those folders, or who sends a user a file, decides - their contents. The executable and the weights built into it are trusted, since the - release signatures cover them. + their contents. The desktop app also reads the fonts installed on the system and, on + Linux, desktop settings such as the mouse-pointer theme, a user's own included, to + draw its window: code in its dependencies parses them (boundary 6; see *Remaining + risks*). The executable and the weights built into it are trusted, since the release + signatures cover them. 2. **The person running it → the app.** Command-line arguments and environment variables (the `bench` and `train` settings, and the app's `TS_` settings such as the screenshot mode) come from the person running the program, who already has the same @@ -100,11 +103,11 @@ The design follows the principles set out by Saltzer and Schroeder: | Integer overflow (CWE-190) | Sizes read from files are multiplied with checked arithmetic, and the fuzzer runs with overflow checks. | | Deserialization of untrusted data (CWE-502) | Files deserialize into plain data structures with serde_json; nothing in a file selects a type or code to run. | | Uncontrolled resource consumption (CWE-400) | Malformed networks are rejected before they run, and network outputs that overflow to NaN are sorted safely (`total_cmp`). A very large file is still read whole into memory (see *Remaining risks*). | -| Injection: OS commands, SQL, cross-site scripting, path traversal (CWE-78, CWE-89, CWE-79, CWE-22) | Not applicable by construction: the app runs no commands, has no database and generates no HTML, and it builds file paths only from fixed names, the platform's standard directories and paths the person running it supplies. | +| Injection: OS commands, SQL, cross-site scripting, path traversal (CWE-78, CWE-89, CWE-79, CWE-22) | Not applicable by construction: the app's own code starts no programs, has no database and generates no HTML, and it builds file paths only from fixed names, the platform's standard directories and paths the person running it supplies. The window libraries it depends on do start a few programs, and nothing from a weights file is part of what they run: the default browser (on Linux, the one the `BROWSER` variable or the desktop's settings name), given the project's fixed address, when the link in the *About* window is clicked, and, on Linux under Wayland when the app draws its own title bar, `gsettings`, `dbus-send` and `fc-match`, found on the `PATH`, to read the desktop's settings and find the title-bar font (`fc-match` is given the font name that `gsettings` reports). | | Hard-coded or leaked credentials (CWE-798) | The app has no credentials, and the repository holds none: the macOS signing settings are GitHub environment secrets, and everything else signs in with short-lived OIDC credentials. | -| Vulnerable and outdated components (OWASP A06) | `cargo deny` checks every change against the RustSec advisory database, Dependabot proposes updates, and the OpenSSF Scorecard's vulnerability check runs weekly. Advisories that don't apply are recorded with the reason. | +| Vulnerable and outdated components (OWASP A06) | `cargo deny` checks every change against the RustSec advisory database and fails on a locked version that has been yanked, Dependabot proposes weekly updates to the dependencies named in `Cargo.toml` and a security update for a locked version that GitHub has an advisory for, when a fixed version exists, and the OpenSSF Scorecard's vulnerability check runs weekly. Advisories that don't apply are recorded with the reason. | | Untrusted code in the build (CWE-829) | Every GitHub Action is pinned to a commit hash, CodeQL analyzes the workflows, and no workflow runs pull-request code with write access or secrets. | -| Download of code without an integrity check (CWE-494) | Releases are signed, carry build-provenance attestations and SHA-256 checksums, and are served over HTTPS. | +| Download of code without an integrity check (CWE-494) | Every release package comes with a signed build-provenance attestation and a SHA-256 checksum, the macOS disk image and the app inside it and the Windows executable and installer are also code-signed, and releases are served over HTTPS. | CodeQL (for Rust and for GitHub Actions) and Clippy, with every warning treated as an error, also check all code on every pull request. @@ -119,5 +122,12 @@ error, also check all code on every pull request. computer it runs on. * **Very large files.** A weights file is read whole into memory before it's checked, so an enormous one can make the app slow or exhaust its memory. +* **Installed fonts.** The fonts the desktop app reads (boundary 1) are parsed by code + in its dependencies that this project neither wrote nor fuzzes: fontique, read-fonts, + skrifa and harfrust, and on Linux under Wayland also ttf-parser, which is + unmaintained (see deny.toml). A flaw there could be reached by a font a user was + persuaded to install. `cargo deny` checks these crates against the RustSec advisory + database on every change, as it does every dependency, so it catches only flaws that + have been reported. * **Platform trust.** The project relies on GitHub, crates.io, Sigstore, Apple and Microsoft for hosting, signing and publishing. diff --git a/packaging/README.md b/packaging/README.md index 137cda6..e1461d2 100644 --- a/packaging/README.md +++ b/packaging/README.md @@ -1,6 +1,8 @@ # Packaging and releases -Intersection Lab ships as signed, installable packages for every major desktop platform. +Intersection Lab ships as installable packages for every major desktop platform. Each comes +with a signed build-provenance attestation, and the macOS app and the Windows executable +and installer are also code-signed. GitHub Actions builds them all from a version tag ([`.github/workflows/release.yml`](../.github/workflows/release.yml)). It's also published to crates.io, so Rust users can `cargo install --locked intersection-lab` (see [crates.io](#cratesio)). @@ -12,7 +14,7 @@ It's also published to crates.io, so Rust users can `cargo install --locked inte | Linux (glibc 2.35+) | x86_64, aarch64 | `.deb`, `.rpm`, `.tar.gz` | Build provenance attestations + SHA-256 checksums | Every release also carries `SHA256SUMS.txt` and a signed build-provenance attestation -for each file. Users can verify a download with: +that covers each package and that checksum file. Users can verify a download with: ```bash gh attestation verify IntersectionLab-0.6.0-macos-universal.dmg --repo schreck61/intersection-lab diff --git a/src/bin/train.rs b/src/bin/train.rs index e0047e3..4f43f39 100644 --- a/src/bin/train.rs +++ b/src/bin/train.rs @@ -15,6 +15,9 @@ //! //! Environment: `RESUME=1` fine-tunes the saved weights, `SEED=n` changes the random //! seed, and `HIDDEN=128,128` overrides the hidden layer sizes of a new network. +//! +//! Weights are written to `weights/` in the working directory and nowhere else. If that +//! folder can't be made, or a file in it can't be written, `train` stops with the error. use intersection_lab::control::hybrid::{self, Hybrid}; use intersection_lab::control::rl::{self, RlController}; @@ -24,6 +27,7 @@ use intersection_lab::sim::network::{LayoutKind, Network}; use intersection_lab::sim::rng::Rng; use intersection_lab::sim::sensors::{Conditions, SensorConfig}; use intersection_lab::sim::{ControlMode, Sim}; +use std::path::{Path, PathBuf}; use std::time::Instant; /// A checkpoint is accepted only if its mean pedestrian wait is within this many @@ -142,11 +146,16 @@ impl Agent { } } fn save(&self, layout: LayoutKind, score: f64) { - std::fs::create_dir_all("weights").ok(); match self { - Agent::Dqn(a) => a.save(&rl::weights_path(layout.key())).ok(), - Agent::Hybrid(a) => a.save(&hybrid::weights_path(layout.key()), Some(score), true).ok(), - }; + Agent::Dqn(a) => { + let path = weights_dir().join(rl::weights_file(layout.key())); + written(a.save(&path), &path); + } + Agent::Hybrid(a) => { + let path = weights_dir().join(hybrid::weights_file(layout.key())); + written(a.save(&path, Some(score), true), &path); + } + } } fn loss(&self) -> f32 { let l = match self { @@ -280,8 +289,8 @@ fn train(kind: LayoutKind, episodes: u64, secs: f64, which: &str) { // When no tuning met the pedestrian limit, its score is infinite and isn't saved. h.base_action = base.1; best = base.0; - std::fs::create_dir_all("weights").ok(); - h.save(&hybrid::weights_path(kind.key()), Some(base.0), false).ok(); + let path = weights_dir().join(hybrid::weights_file(kind.key())); + written(h.save(&path, Some(base.0), false), &path); saved_any = true; } agent.set(true, 0.0, 0.0); @@ -407,6 +416,22 @@ fn report_test( println!("{line} agent {}", fmt(evaluate(net, agent, &test, secs).0)); } +/// Where `train` writes: `weights/` in the working directory. It doesn't use the app's +/// `save_path`, which turns to the per-user data directory when that folder is missing +/// and would then write over the weights saved from the app. +fn weights_dir() -> PathBuf { + PathBuf::from("weights") +} + +/// Stop with the error if weights file `path` could not be written: a run must not go on +/// to report a "saved" checkpoint, or score some other copy of the file as its result. +fn written(result: std::io::Result<()>, path: &Path) { + if let Err(e) = result { + eprintln!("train: can't write {}: {e}", path.display()); + std::process::exit(1); + } +} + fn resume_salt() -> u64 { if std::env::var_os("RESUME").is_some() { 99 } else { 0 } } @@ -431,6 +456,15 @@ fn main() { .copied() .filter(|k| k.signalized() && (which == "all" || k.key() == which)) .collect(); + if kinds.is_empty() { + eprintln!("train: no signalized layout is named {which}"); + std::process::exit(1); + } + // Before any training, so a run doesn't work for minutes only to find the folder can't be made. + if let Err(e) = std::fs::create_dir_all(weights_dir()) { + eprintln!("train: can't make the folder {}: {e}", weights_dir().display()); + std::process::exit(1); + } let hs: Vec<_> = kinds .into_iter() .flat_map(|k| agents.iter().map(move |&a| std::thread::spawn(move || train(k, episodes, secs, a)))) diff --git a/tests/tools.rs b/tests/tools.rs index 196d535..d313a11 100644 --- a/tests/tools.rs +++ b/tests/tools.rs @@ -160,6 +160,85 @@ fn train_saves_the_hybrid_tuning() { assert!(dir.join("weights/hybrid_tee.json").exists(), "{out}"); } +#[test] +fn train_stops_when_it_cannot_write_to_the_weights_folder() { + // `weights` is a file here, so the folder can't be made. `train` used to turn to the + // per-user data directory then, and write over the weights saved from the app. + let dir = scratch("train_no_folder"); + std::fs::write(dir.join("weights"), "not a folder").unwrap(); + let per_user = [ + "Library/Application Support/Intersection Lab/weights", + "Intersection Lab/weights", + "intersection-lab/weights", + ]; + for folder in per_user { + std::fs::create_dir_all(dir.join(folder)).unwrap(); + std::fs::write(dir.join(folder).join("hybrid_tee.json"), "saved from the app").unwrap(); + } + let out = Command::new(env!("CARGO_BIN_EXE_train")) + .args(["tee", "1", "150", "hybrid"]) + .current_dir(&dir) + .env("HOME", &dir) + .env("XDG_DATA_HOME", &dir) + .env("APPDATA", &dir) + .env_remove("RESUME") + .env_remove("SEED") + .env("HIDDEN", "8") + .output() + .unwrap(); + let said = String::from_utf8_lossy(&out.stderr); + assert!(!out.status.success(), "train went on without a weights folder:\n{said}"); + assert!(said.contains("train: can't make the folder weights"), "{said}"); + for folder in per_user { + let kept = std::fs::read_to_string(dir.join(folder).join("hybrid_tee.json")).unwrap(); + assert_eq!(kept, "saved from the app", "{folder}"); + } +} + +#[test] +fn train_stops_when_it_cannot_write_a_weights_file() { + // The folder is there, but a folder stands where the weights file goes. + let dir = scratch("train_no_file"); + std::fs::create_dir_all(dir.join("weights").join("hybrid_tee.json")).unwrap(); + let out = Command::new(env!("CARGO_BIN_EXE_train")) + .args(["tee", "1", "150", "hybrid"]) + .current_dir(&dir) + .env("HOME", &dir) + .env("XDG_DATA_HOME", &dir) + .env("APPDATA", &dir) + .env_remove("RESUME") + .env_remove("SEED") + .env("HIDDEN", "8") + .output() + .unwrap(); + let said = String::from_utf8_lossy(&out.stderr); + let printed = String::from_utf8_lossy(&out.stdout); + assert!(!out.status.success(), "train went on without saving:\n{printed}\n{said}"); + assert!(said.contains("train: can't write weights"), "{said}"); + assert!(!printed.contains("TEST person delay"), "{printed}"); +} + +#[test] +fn train_stops_at_a_layout_it_does_not_train() { + // A mistyped layout, and one without signals, train nothing: say so, and leave no + // empty `weights` folder behind for the app to save into. + for layout in ["arterial", "roundabout"] { + let dir = scratch("train_no_layout"); + let out = Command::new(env!("CARGO_BIN_EXE_train")) + .args([layout, "1", "150", "hybrid"]) + .current_dir(&dir) + .env("HOME", &dir) + .env("XDG_DATA_HOME", &dir) + .env("APPDATA", &dir) + .output() + .unwrap(); + let said = String::from_utf8_lossy(&out.stderr); + assert!(!out.status.success(), "train {layout} reported success"); + assert!(said.contains(&format!("train: no signalized layout is named {layout}")), "{said}"); + assert!(!dir.join("weights").exists(), "train {layout} left a weights folder"); + } +} + #[test] fn train_resumes_the_built_in_hybrid() { let dir = scratch("train_hybrid_resume"); diff --git a/tests/weights_files.rs b/tests/weights_files.rs index 98d9e75..fc36ec5 100644 --- a/tests/weights_files.rs +++ b/tests/weights_files.rs @@ -122,6 +122,11 @@ fn hybrid_without_a_fitting_network_keeps_its_base_tuning() { let no_network = json!({ "base_action": 10, "base_score": 0.0, "model": null }).to_string(); let notes = status(Hybrid::from_saved(&net, &no_network).unwrap()); assert!(notes.contains("no network beat the best static tuning"), "{notes}"); + + // A tuning number past the last one can only come from a damaged or edited file. It + // is read as the last tuning, not skipped. + let past = json!({ "base_action": 1000, "base_score": 0.0, "model": null }).to_string(); + assert_eq!(Hybrid::from_saved(&net, &past).unwrap().base_action, N_ACTIONS - 1); } #[test]