diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 6f783327..e6ad869d 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -21,7 +21,16 @@ jobs: CARGO_PROFILE_DEV_DEBUG: line-tables-only CARGO_PROFILE_TEST_DEBUG: line-tables-only steps: + # The parity floor compares against the commit before this one, so + # the checkout needs one more commit than the default. - uses: actions/checkout@v4 + with: + fetch-depth: 2 + - name: The base branch, for the parity floor + if: github.event_name == 'pull_request' + run: | + git fetch --no-tags --depth=1 origin \ + "+refs/heads/${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}" - name: Room for the build (the image ships toolchains we never use) run: | sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc \ @@ -30,6 +39,16 @@ jobs: - uses: jdx/mise-action@v2 - name: GUI build dependencies run: sudo apt-get update && sudo apt-get install -y libxkbcommon-dev libgtk-3-dev + # docs/parity.md's floor never decreases (AGENTS.md, "Parity + # tracking"); a test checks it inside `just check`, this checks the + # commit's floor against its base. + - name: The parity floor holds + run: | + if [ "${{ github.event_name }}" = "pull_request" ]; then + scripts/parity-floor.sh "origin/${{ github.base_ref }}" + else + scripts/parity-floor.sh HEAD^1 + fi - run: just check - name: What the build used if: always() diff --git a/AGENTS.md b/AGENTS.md index d5f7abf5..44b87fec 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -145,8 +145,11 @@ clone, with status. Rules: - Every feature change updates `docs/parity.md` in the same commit. - The **floor never decreases**: the count of `done` items is monotonic. - CI will enforce this once the catalog is machine-readable (roadmap phase 3); - until then, it is enforced by review — yours. + `crates/ledger-cli/tests/parity_floor.rs` runs in `cargo test` (so in + `just check`) and fails unless every status is one of the three and the + `done` count equals the floor line exactly; the CI step + `scripts/parity-floor.sh` fails when the floor falls below the base + commit's. - Statuses are `done` (implemented + tested + golden-covered), `partial`, `todo`. No other statuses. `done` requires all three. diff --git a/crates/ledger-cli/tests/parity_floor.rs b/crates/ledger-cli/tests/parity_floor.rs new file mode 100644 index 00000000..5b2b0241 --- /dev/null +++ b/crates/ledger-cli/tests/parity_floor.rs @@ -0,0 +1,193 @@ +//! The parity catalog's floor, machine-checked (docs/parity.md; +//! AGENTS.md, "Parity tracking"). +//! +//! Every table with a `Status` column must hold only `done`, `partial` or +//! `todo`, and the number of `done` rows must equal the catalog's +//! `**Floor: N done**` line exactly. Equal, not at least: adding a done row +//! raises the floor in the same commit, so a later drop is always caught. +//! The history half (the floor never falls between commits) is +//! `scripts/parity-floor.sh`, run by CI before `just check`. + +use std::path::{Path, PathBuf}; + +fn root() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../..") + .canonicalize() + .unwrap() +} + +fn catalog() -> String { + let path = root().join("docs/parity.md"); + std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())) +} + +/// The only statuses the catalog allows (docs/parity.md: +/// `done` = implemented + invariant-tested + golden-covered, +/// `partial` = implemented but missing tests or goldens, `todo` = not +/// started). +const STATUSES: [&str; 3] = ["done", "partial", "todo"]; + +#[derive(Debug)] +struct Row { + area: String, + status: String, +} + +/// A markdown table row's cells, trimmed, without the outer pipes. +fn cells(line: &str) -> Vec<&str> { + line.trim() + .trim_matches('|') + .split('|') + .map(str::trim) + .collect() +} + +/// The `**Floor: N done**` line, or `None` when the file has none. +fn parse_floor(text: &str) -> Option { + for line in text.lines() { + let Some(rest) = line.trim().strip_prefix("**Floor: ") else { + continue; + }; + let mut words = rest.split_whitespace(); + let (Some(number), Some(marker)) = (words.next(), words.next()) else { + continue; + }; + if marker != "done**" { + continue; + } + return number.parse().ok(); + } + None +} + +/// Every data row of every table whose header has a `Status` column. +/// Errors name the row's first cell and the bad status. +fn parse_tables(text: &str) -> Result, Vec> { + let mut rows = Vec::new(); + let mut errors = Vec::new(); + let mut status_col: Option = None; + + for (index, line) in text.lines().enumerate() { + let line_no = index + 1; + if !line.trim_start().starts_with('|') { + // A table ends at the first non-table line. + status_col = None; + continue; + } + let parts = cells(line); + let separator = parts + .iter() + .all(|cell| !cell.is_empty() && cell.chars().all(|c| c == '-' || c == ':')); + if separator { + continue; + } + let Some(col) = status_col else { + if let Some(col) = parts.iter().position(|cell| *cell == "Status") { + status_col = Some(col); + } + continue; + }; + let area = parts.first().copied().unwrap_or_default().to_string(); + match parts.get(col).copied() { + None => errors.push(format!( + "docs/parity.md:{line_no}: row `{area}` has no status cell" + )), + Some("") => errors.push(format!( + "docs/parity.md:{line_no}: row `{area}` has an empty status" + )), + Some(status) if !STATUSES.contains(&status) => errors.push(format!( + "docs/parity.md:{line_no}: row `{area}` has status `{status}`, \ + not one of done, partial, todo" + )), + Some(status) => rows.push(Row { + area, + status: status.to_string(), + }), + } + } + if errors.is_empty() { + Ok(rows) + } else { + Err(errors) + } +} + +#[test] +fn every_row_has_a_known_status() { + let text = catalog(); + if let Err(errors) = parse_tables(&text) { + panic!( + "docs/parity.md's statuses must be exactly done, partial or todo:\n{}", + errors.join("\n") + ); + } +} + +#[test] +fn the_done_count_is_the_floor() { + let text = catalog(); + let rows = parse_tables(&text).unwrap_or_else(|errors| { + panic!( + "docs/parity.md's statuses must be exactly done, partial or todo:\n{}", + errors.join("\n") + ) + }); + let done: Vec<&str> = rows + .iter() + .filter(|row| row.status == "done") + .map(|row| row.area.as_str()) + .collect(); + let floor = parse_floor(&text).unwrap_or_else(|| { + panic!( + "docs/parity.md has no `**Floor: N done**` line; restore it \ + (docs/parity.md's intro and AGENTS.md, \"Parity tracking\")" + ) + }); + if done.len() > floor as usize { + panic!( + "docs/parity.md has {} `done` rows but its floor line says {floor}. \ + Raise the line to `**Floor: {} done**` in this commit: adding a \ + done row raises the floor in the same commit, so a later drop is \ + always caught.\nDone rows:\n {}", + done.len(), + done.len(), + done.join("\n ") + ); + } + if done.len() < floor as usize { + panic!( + "docs/parity.md has {} `done` rows but its floor line says {floor}. \ + Restore the missing row: the floor never decreases, and lowering \ + it needs the owner's decision.\nDone rows:\n {}", + done.len(), + done.join("\n ") + ); + } +} + +#[test] +fn a_bad_status_names_its_row() { + let text = "| Area | Status | Notes |\n|---|---|---|\n| Ledger | finished | — |\n"; + let errors = parse_tables(text).unwrap_err(); + assert_eq!(errors.len(), 1, "{errors:?}"); + assert!(errors[0].contains("Ledger"), "{errors:?}"); + assert!(errors[0].contains("finished"), "{errors:?}"); +} + +#[test] +fn a_missing_status_cell_is_an_error() { + let text = "| Area | Status | Notes |\n|---|---|---|\n| Ledger | | — |\n"; + let errors = parse_tables(text).unwrap_err(); + assert_eq!(errors.len(), 1, "{errors:?}"); + assert!(errors[0].contains("Ledger"), "{errors:?}"); +} + +#[test] +fn a_missing_floor_line_is_none() { + assert_eq!(parse_floor("# Parity catalog\n\nNo floor here.\n"), None); + assert_eq!( + parse_floor("**Floor: 7 done** (2026-01-01; the sections below record each rise)"), + Some(7) + ); +} diff --git a/docs/parity.md b/docs/parity.md index eeaf8c88..9e4318b2 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -6,9 +6,11 @@ same commit as the feature. **The floor never decreases**: the count of Statuses: `done` = implemented + invariant-tested + golden-covered. `partial` = implemented but missing tests or goldens. `todo` = not started. -No other statuses. +No other statuses. `crates/ledger-cli/tests/parity_floor.rs` enforces the +statuses and the floor in `cargo test`; `scripts/parity-floor.sh` enforces +in CI that the floor never falls between commits. -**Floor: 21 done** (2026-10-28; it was 10 on 2026-10-10, when the fixed-asset register was added, and the sections below record each rise). +**Floor: 25 done** (2026-10-28; it was 10 on 2026-10-10, when the fixed-asset register was added, and the sections below record each rise; 2026-11-06: P4b S5 added one `done` row and S6 three, to 25, which the line had missed — the four rows' implementations and tests are named below). | Area | GnuCash counterpart (a cross-check, not the spec) | Status | Notes | |---|---|---|---| diff --git a/scripts/parity-floor.sh b/scripts/parity-floor.sh new file mode 100755 index 00000000..ce104493 --- /dev/null +++ b/scripts/parity-floor.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# The parity catalog's floor never decreases (docs/parity.md; AGENTS.md, +# "Parity tracking"). `crates/ledger-cli/tests/parity_floor.rs` checks the +# floor against the rows in one commit; this checks it against the base +# commit, which a test cannot do. +# +# Usage: scripts/parity-floor.sh +# prints both floors; exits 1 when the working tree's floor is lower. +set -euo pipefail + +base_ref="${1:?usage: scripts/parity-floor.sh }" +cd "$(git rev-parse --show-toplevel)" + +floor_of() { + # The first `**Floor: N done**` line, or nothing. + sed -n 's/^\*\*Floor: \([0-9][0-9]*\) done\*\*.*/\1/p' | head -n 1 +} + +if ! base_doc=$(git show "${base_ref}:docs/parity.md" 2>/dev/null); then + echo "parity floor: cannot read docs/parity.md at ${base_ref}" >&2 + exit 1 +fi +base_floor=$(printf '%s\n' "$base_doc" | floor_of) + +if [ -z "$base_floor" ]; then + echo "parity floor: ${base_ref} has no floor line; nothing to compare" + exit 0 +fi + +work_floor=$(floor_of < docs/parity.md) +if [ -z "$work_floor" ]; then + echo "parity floor: docs/parity.md in the working tree has no floor line" >&2 + exit 1 +fi + +echo "parity floor: ${base_ref} says ${base_floor} done; the working tree says ${work_floor} done" +if [ "$work_floor" -lt "$base_floor" ]; then + echo "parity floor: the floor never decreases (docs/parity.md; AGENTS.md, \"Parity tracking\")" >&2 + exit 1 +fi