diff --git a/CHANGELOG.md b/CHANGELOG.md index 89e0695..29b5c87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ This changelog is automatically generated by AI when releases are published. ### Fixed +- **A note with its integrity stamp removed no longer passes as `legacy`.** Deleting the `integrity` member from a note and editing it made `autter verify` report `legacy` (exit 0, also with `--strict`), and `autter stats` then used the edited note: a commit with 83% AI lines could be reported at 33%. An unstamped note now passes as `legacy` only when it can have been written before stamping. A note whose metadata says autter 3.8.0 or later wrote it fails with the new status `stamp_removed` (exit 5, with and without `--strict`), since those releases stamp every note; so does a note with no autter version on a commit that descends from a commit whose note is stamped. `autter stats`, `autter blame` and range stats count its lines as unknown and print the one-line notice, and a rebase, amend, cherry-pick or `autter ci` merge that carries it writes `carried_failure`. + - A note that says an autter older than 3.8.0 wrote it, on a commit after stamping began, is the new warning `unstamped_by_older_autter`: a teammate who has not upgraded writes exactly that, so it passes and its lines count, but `--strict` fails it (exit 5), as it fails `rebound_by_older_autter`, because the version cannot be checked. A repository whose notes all predate 3.8.0 still verifies as `legacy`, and so does an old note copied in by `autter notes backfill`. + - "After stamping began" is commit ancestry, not dates, which can be set to anything and change on a rebase. autter computes the earliest stamped commits once and keeps them in `.git/ai/stamp_floor.json`, keyed by the notes ref, reading only the notes that changed when it moves; the check runs only for notes without a stamp. + - Stash notes: an unstamped stash note that says autter 3.8.2 or later wrote it is no longer restored (until now it restored like one from 3.8.1), nor is one with no version made on a commit after stamping began. + - Still not caught: a note whose stamp was removed and whose version was edited to a release before 3.8.0 (a warning after stamping began, `legacy` before it, and a failure only with `--strict`; a stash note edited to 3.8.1 or earlier still restores), and with the `http` notes backend only the version is checked. - **Upgrading on Windows while the background service runs works, and a failed upgrade says so.** Windows does not replace a program that is running, and the background service runs `autter.exe`. An npm upgrade (`npm install -g @autter/cli@`) then failed with EPERM, kept the old version and still exited 0; `autter upgrade` ran the installer in the background and exited 0 before it knew the result. Now the npm package, `autter upgrade` and `install.ps1` stop the service first (as `autter bg shutdown` does), rename a binary that is still in use to `autter.exe.old` and put the new one in its place, and start the service again on the new binary if it was running. The `.old` file is deleted once nothing runs it, by the next install or when the service starts. `autter upgrade` on Windows now waits for the installer and exits with its result. - If the binary still cannot be replaced (for example, another program holds it open), the install exits non-zero, says the file is in use and to run `autter bg shutdown` and try again, and leaves the previous version installed and working. - On macOS and Linux nothing changes, except that the npm install now fails instead of passing when it cannot move the new binary into place. diff --git a/docs/scripting.md b/docs/scripting.md index f0fe1df..191828d 100644 --- a/docs/scripting.md +++ b/docs/scripting.md @@ -213,15 +213,17 @@ autter verify origin/main..HEAD --json --strict | Status | Exit | Meaning | |---|---|---| | `verified` | 0 | Written for this commit and unchanged since | -| `legacy` | 0 (warning) | Written before the integrity check (autter 3.7.1 and earlier). Its commit and line ranges are still checked | +| `legacy` | 0 (warning) | Written before the integrity check (autter 3.7.1 and earlier). Its commit and line ranges are still checked. See "Notes without a stamp" below for when an unstamped note is not `legacy` | | `missing` | 0 (warning) | The commit has no note | | `pending_rewrite` | 0 (warning) | Git copied another commit's note here during a rebase or amend and autter has not rewritten it yet (the background service was not running). The two commits carry the same patch | +| `unstamped_by_older_autter` | 0 (warning); 5 with `--strict` | No stamp, and the note says an autter older than 3.8.0 wrote it, but its commit comes after a commit whose note is stamped. A teammate still on an older autter writes such notes; so does anyone who removes a stamp and edits the version. Its lines still count | | `rebound_by_older_autter` | 0 (warning); 5 with `--strict` | An autter release older than the integrity check moved the note here and kept the old stamp, so the digest cannot be checked. The stamp's tree and blobs are checked against the commit it names (`recorded_commit`) when that commit is in this clone | | `unavailable` | 0 (warning) | The commit or its files are not in this clone (a shallow or partial clone), so the note could not be checked. This is not tampering | | `wrong_commit` | 5 | The note was written for another commit with a different change: it was copied or moved | | `content_mismatch` | 5 | The note's record of the commit's files does not match the commit; for a note that looks rebound, its stamp does not describe the commit it names (`recorded_commit`) | | `range_out_of_bounds` | 5 | The note attributes lines past the end of a file, or lines of a file the commit does not have | | `digest_mismatch` | 5 | The note was changed after autter wrote it | +| `stamp_removed` | 5 | No stamp, but the note cannot predate stamping: it says autter 3.8.0 or later wrote it (those releases stamp every note), or it has no autter version and its commit comes after a commit whose note is stamped | | `carried_failure` | 5 | Autter derived the note, during a rebase, amend, cherry-pick or `autter ci` merge, from a note that failed the check, or an older autter rebound it from a note that fails the check. `recorded_commit` names that source commit | | `not_recorded` | 0 (warning); 5 with `--require-record` | Connected mode only: the note passed the local check, but the server has no record of it yet. Notes upload in the background, so a commit made a moment ago is usually not recorded yet | | `changed_after_recorded` | 5 | Connected mode only: the server's record of this note has more than one version, or the note here matches none of them, or only a later one. The note was changed after it was uploaded | @@ -232,16 +234,55 @@ record?, local_status?, record_summary?}`; on exit 5 the same report comes with `error.code` = `integrity_failed`, and `failed` counts the commits that made it fail. -`--strict` fails `rebound_by_older_autter` (exit 5; the status stays -`rebound_by_older_autter` and the human report marks it `FAIL`): its digest -cannot be checked, so anyone who can write notes could otherwise turn a -failure into a warning by making a note look rebound. To -clear it, rewrite the commits with this autter (a rebase or `autter ci` -merge), which stamps them again. `--strict` still passes `legacy` and +`--strict` fails `rebound_by_older_autter` and `unstamped_by_older_autter` +(exit 5; the status stays the same and the human report marks it `FAIL`): +neither can be checked against a digest, so anyone who can write notes +could otherwise turn a failure into a warning by making a note look rebound, +or by removing its stamp and writing an older version into it. To clear +them, upgrade autter everywhere and rewrite the commits with this autter (a +rebase or `autter ci` merge), which stamps them again. `--strict` still passes `legacy` and `missing` notes, with a warning on stderr. **A later release will fail them under `--strict`**, once notes written before the integrity check have aged out of the ranges you check. +### Notes without a stamp + +A note without an `integrity` member reads as `legacy`, so notes written +before 3.8.0 keep working. Removing the member from a note is easy, so an +unstamped note is `legacy` only when it can have been written before +stamping: + +| The note says it was written by | Its commit comes after stamping began | Otherwise | +|---|---|---| +| autter 3.8.0 or later | `stamp_removed` (5) | `stamp_removed` (5) | +| no readable autter version | `stamp_removed` (5) | `legacy` | +| an autter older than 3.8.0 | `unstamped_by_older_autter` (warning; 5 with `--strict`) | `legacy` | + +"The note says" is the `autter_version` field of the note's metadata (debug +builds write `development:`). A commit "comes after stamping began" +when it descends from a commit whose note is stamped. This uses commit +ancestry, not dates: committer dates can be set to anything and change on a +rebase, so a 3.7 note committed yesterday on a branch that forked before +your first stamped commit is still `legacy`, and a commit dated in the past +on top of a stamped one is still after it. autter finds the earliest stamped +commits once and keeps them in `.git/ai/stamp_floor.json`, keyed by the +notes ref; when the notes change it reads only the notes that changed, so +`stats` and `blame` do not walk the history each time. The check runs only +for notes that have no stamp. + +A team where some people have not upgraded keeps passing without +`--strict`: their notes say an older version and read as +`unstamped_by_older_autter`, a warning whose lines still count. That +version cannot be checked, so `--strict` fails it, as it fails +`rebound_by_older_autter`. Notes that are legitimately unstamped are not +flagged: `autter notes backfill` copies notes as stored, so a pre-3.8.0 note +keeps its old version (and an old commit), a note rebound by an older autter +keeps its stamp, and every rewrite this autter performs (rebase, amend, +cherry-pick, `autter ci` merges) stamps the note it writes. + +With the `http` notes backend there is no local `refs/notes/ai`, so only the +version column applies; the server's record covers those notes. + `autter stats`, `autter blame` and the range stats do not trust a note that fails: its lines count as `unknown`, and they print one line naming the commit and pointing at `autter verify`. @@ -266,8 +307,10 @@ The same holds for the other ways attribution comes back: autter prints one warning line naming the stash (in the background service's log when the service handles the stash). A stash note without a stamp, written by autter 3.8.1 or earlier, is restored as before, so stashes made - before an upgrade keep their AI lines; a stamped note with its stamp removed - looks the same and is restored too. + before an upgrade keep their AI lines. A stash note with its stamp removed + is not restored when it says autter 3.8.2 or later wrote it (those releases + stamp every stash note), or has no autter version and the stash was made on + a commit at or after the first stamped commit. - **`autter notes backfill`** (and the automatic backfill when a machine moves off the `http` notes backend) copies notes from the local notes database into `refs/notes/ai` as stored, without a new stamp. A note changed in the @@ -360,6 +403,12 @@ commit, moved, or edited by hand. Not caught: whose commit is not in the clone, or whose claims hold: it is a warning without `--strict`, and a rewrite by this autter carries it like any other warning; +- a note whose stamp was removed and whose `autter_version` was edited to a + release older than 3.8.0: on a commit after stamping began it is + `unstamped_by_older_autter`, a warning without `--strict`; on a commit + before stamping began, or in a repository with no stamped notes, it is + `legacy`. A stash note edited the same way (to 3.8.1 or earlier) is + restored; - edits to the working log in `.git/ai/`, which the next commit records as they are. diff --git a/src/authorship/mod.rs b/src/authorship/mod.rs index a3aec17..7f7ab52 100644 --- a/src/authorship/mod.rs +++ b/src/authorship/mod.rs @@ -16,6 +16,7 @@ pub mod move_detection; pub mod no_commit_pick; pub mod note_integrity; pub mod note_server_record; +pub mod note_stamp_floor; pub mod post_commit; pub mod prompt_utils; diff --git a/src/authorship/note_integrity.rs b/src/authorship/note_integrity.rs index 9a9b556..c1f6826 100644 --- a/src/authorship/note_integrity.rs +++ b/src/authorship/note_integrity.rs @@ -22,6 +22,34 @@ //! unknown member and keep reading new notes, and notes written before this //! existed read as [`IntegrityStatus::Legacy`]. //! +//! **A removed stamp.** Anyone can delete the `integrity` member, edit the +//! note, and have it read as legacy. So an unstamped note passes as legacy +//! only when it can be one: +//! +//! * its metadata's `autter_version` names autter 3.8.0 or later: every such +//! release stamps every note it writes, so the stamp was removed +//! ([`IntegrityStatus::StampRemoved`], a failure); +//! * its commit comes after stamping began, meaning it descends from a commit +//! whose note is stamped (commit topology, not dates; see +//! `note_stamp_floor`), and it has no readable `autter_version`: also +//! [`IntegrityStatus::StampRemoved`]; +//! * its commit comes after stamping began and its `autter_version` names a +//! release older than 3.8.0: [`IntegrityStatus::UnstampedByOlderAutter`], +//! a warning (a failure under `autter verify --strict`), because a teammate +//! who has not upgraded writes exactly this, and the version cannot be +//! checked: whoever removes a stamp can edit it too. This mirrors +//! [`IntegrityStatus::ReboundByOlderAutter`]; +//! * otherwise (older release, or no version, on a commit before stamping +//! began or with no stamped notes in the repository): legacy. +//! +//! The paths that legitimately write unstamped notes after stamping began +//! all stay out of the failure: `autter notes backfill` copies notes as +//! stored, so a pre-3.8.0 note keeps its old version and an old commit; a +//! note rebound by an older autter keeps a stamp and is not unstamped; a +//! teammate on an older autter writes its own older version; and every +//! rewrite this autter performs (rebase, amend, cherry-pick, `autter ci`) +//! stamps the note it writes. +//! //! **What it does not do.** On its own this is an integrity check, not tamper //! evidence: anyone who can write the repository can rewrite a note and //! recompute every field. In connected mode the server keeps every version of @@ -61,7 +89,12 @@ //! **Stash notes.** `git stash` saves the working log as a note in //! `refs/notes/ai-stash`, stamped for the stash commit //! ([`seal_stash_note`]); `stash pop`/`apply` checks it -//! ([`check_stash_note`]) and does not restore one that fails. +//! ([`check_stash_note`]) and does not restore one that fails. An unstamped +//! stash note is not restored when it says autter 3.8.2 or later wrote it +//! (the first release that stamps stash notes), or has no version and the +//! stash was made on a commit at or after the first stamped commit. One that +//! says 3.8.1 or earlier is restored, since 3.8.0 and 3.8.1 stamped commit +//! notes but not stash notes. //! //! **Notes copied from the notes database** (`autter notes backfill`) are //! written as stored, without a new stamp, so a failing or legacy note is not @@ -157,6 +190,18 @@ pub enum IntegrityStatus { /// local check but the server has no record of it yet (notes upload in /// the background). A warning; a failure with `--require-record`. NotRecorded, + /// The note has no integrity stamp, but it cannot have been written + /// before stamping existed: its metadata says autter 3.8.0 or later wrote + /// it (every such release stamps every note), or it has no readable + /// autter version and its commit descends from a commit whose note is + /// stamped. The stamp was removed. + StampRemoved, + /// The note has no integrity stamp and its metadata says an autter older + /// than 3.8.0 wrote it, but its commit descends from a commit whose note + /// is stamped. A teammate on an older autter writes such notes; so does + /// anyone who removes a stamp and edits the version. A warning, but a + /// failure under `autter verify --strict`. + UnstampedByOlderAutter, } impl IntegrityStatus { @@ -175,6 +220,8 @@ impl IntegrityStatus { IntegrityStatus::CarriedFailure => "carried_failure", IntegrityStatus::ChangedAfterRecorded => "changed_after_recorded", IntegrityStatus::NotRecorded => "not_recorded", + IntegrityStatus::StampRemoved => "stamp_removed", + IntegrityStatus::UnstampedByOlderAutter => "unstamped_by_older_autter", } } @@ -190,6 +237,7 @@ impl IntegrityStatus { | IntegrityStatus::DigestMismatch | IntegrityStatus::CarriedFailure | IntegrityStatus::ChangedAfterRecorded + | IntegrityStatus::StampRemoved ) } @@ -203,10 +251,70 @@ impl IntegrityStatus { | IntegrityStatus::ReboundByOlderAutter | IntegrityStatus::Unavailable | IntegrityStatus::NotRecorded + | IntegrityStatus::UnstampedByOlderAutter ) } } +// --------------------------------------------------------------------------- +// Unstamped notes: which autter says it wrote them +// --------------------------------------------------------------------------- + +/// The first release that stamps every authorship note it writes. +pub const FIRST_STAMPING_RELEASE: (u64, u64, u64) = (3, 8, 0); + +/// The first release that stamps every stash note it writes. +pub const FIRST_STASH_STAMPING_RELEASE: (u64, u64, u64) = (3, 8, 2); + +/// What an unstamped note's `autter_version` says about who wrote it, +/// compared with the first release that would have stamped it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum VersionClaim { + /// A release that stamps (this version or later): the stamp was removed. + Stamping(String), + /// A release older than that: it may well be legacy. + Older(String), + /// No version, or one that is not a release number. + Unknown, +} + +/// `autter_version` as `(major, minor, patch)`. Release builds write +/// `3.9.1`, debug builds `development:3.9.1`; a pre-release or build suffix +/// is ignored. `None` for anything else (`development` alone, missing). +fn release_of(version: &str) -> Option<(u64, u64, u64)> { + let version = version.trim(); + let version = version.strip_prefix("development:").unwrap_or(version); + let version = version.strip_prefix('v').unwrap_or(version); + let core = version.split(['-', '+']).next()?; + let mut parts = core.split('.'); + let major = parts.next()?.parse().ok()?; + let minor = parts.next()?.parse().ok()?; + let patch = match parts.next() { + Some(p) => p.parse().ok()?, + None => 0, + }; + if parts.next().is_some() { + return None; + } + Some((major, minor, patch)) +} + +/// What `log`'s `autter_version` claims, against `first_stamping`. +pub fn version_claim(log: &AuthorshipLog, first_stamping: (u64, u64, u64)) -> VersionClaim { + let Some(version) = log.metadata.autter_version.as_deref() else { + return VersionClaim::Unknown; + }; + match release_of(version) { + Some(release) if release >= first_stamping => VersionClaim::Stamping(version.to_string()), + Some(_) => VersionClaim::Older(version.to_string()), + None => VersionClaim::Unknown, + } +} + +fn release_name((major, minor, patch): (u64, u64, u64)) -> String { + format!("{major}.{minor}.{patch}") +} + /// One commit's result, with a sentence saying why. #[derive(Debug, Clone, Serialize)] pub struct CommitIntegrity { @@ -695,6 +803,53 @@ fn verify_notes_once( &patch_ids, )); } + + // An unstamped note that passed as legacy (or as a copy git made during + // a rewrite) on a commit made after stamping began cannot simply be + // trusted as legacy: see `note_stamp_floor`. + let unstamped: Vec = results + .iter() + .filter(|r| { + matches!( + r.status, + IntegrityStatus::Legacy | IntegrityStatus::PendingRewrite + ) + }) + .filter(|r| { + parsed + .get(r.commit.as_str()) + .is_some_and(|p| p.split.integrity.is_none() && !p.split.integrity_moved) + }) + .map(|r| r.commit.clone()) + .collect(); + if !unstamped.is_empty() { + let after = crate::authorship::note_stamp_floor::after_stamping(repo, &unstamped); + for r in results.iter_mut().filter(|r| after.contains(&r.commit)) { + let Some(log) = parsed.get(r.commit.as_str()).and_then(|p| p.log.as_ref()) else { + continue; + }; + let copied = match (&r.recorded_commit, r.status) { + (Some(from), IntegrityStatus::PendingRewrite) => { + format!(" (git copied it from {} during a rewrite)", short(from)) + } + _ => String::new(), + }; + match version_claim(log, FIRST_STAMPING_RELEASE) { + VersionClaim::Older(version) => { + r.status = IntegrityStatus::UnstampedByOlderAutter; + r.detail = format!( + "the note has no integrity stamp and says autter {version} wrote it{copied}, but its commit comes after commits whose notes are stamped: written by a teammate on an older autter, or its stamp was removed and its version edited" + ); + } + VersionClaim::Unknown | VersionClaim::Stamping(_) => { + r.status = IntegrityStatus::StampRemoved; + r.detail = format!( + "the note has no integrity stamp and no autter version{copied}, but its commit comes after commits whose notes are stamped, so it was not written before stamping began: the stamp was removed" + ); + } + } + } + } Ok(results) } @@ -901,6 +1056,18 @@ fn check_one( ) } None => { + // Every release since stamping began stamps every note it writes, + // so a note that says one of them wrote it had its stamp removed. + if let VersionClaim::Stamping(version) = version_claim(log, FIRST_STAMPING_RELEASE) { + return result( + IntegrityStatus::StampRemoved, + format!( + "the note has no integrity stamp but says autter {version} wrote it; autter {} and later stamp every note they write, so the stamp was removed", + release_name(FIRST_STAMPING_RELEASE) + ), + None, + ); + } if !recorded.is_empty() && recorded != commit { return moved(recorded); } @@ -1155,6 +1322,7 @@ pub fn failure_notice(result: &CommitIntegrity) -> String { IntegrityStatus::ContentMismatch => "does not match this commit's files", IntegrityStatus::CarriedFailure => "was carried from a note that failed the check", IntegrityStatus::ChangedAfterRecorded => "was changed after the server recorded it", + IntegrityStatus::StampRemoved => "had its integrity stamp removed", _ => "was changed after autter wrote it", }, short(&result.commit) @@ -1383,8 +1551,11 @@ pub enum StashNoteCheck { /// Stamped for this stash and unchanged since. Verified, /// No integrity member: written by an autter older than stash-note - /// stamping (3.8.1 and earlier). Restored as before; a stamped note with - /// its member removed looks the same, which this check cannot tell apart. + /// stamping (3.8.1 and earlier). Restored as before. A note with its + /// member removed is not this but `Failed` when its metadata names a + /// release that stamps stash notes, or has no version and the stash was + /// made after stamping began; one whose version was also edited to an + /// older release still reads as this. Legacy, /// The note does not belong to this stash or was changed after autter /// wrote it; the reason completes "its record ...". @@ -1402,7 +1573,7 @@ pub fn check_stash_note(repo: &Repository, stash_sha: &str, note: &str) -> Stash ); } let integrity = match split.integrity { - None => return StashNoteCheck::Legacy, + None => return check_unstamped_stash_note(repo, stash_sha, &split.body), Some(Err(e)) => { return StashNoteCheck::Failed(format!("has an unreadable integrity record ({e})")); } @@ -1457,6 +1628,46 @@ pub fn check_stash_note(repo: &Repository, stash_sha: &str, note: &str) -> Stash StashNoteCheck::Verified } +/// A stash note without a stamp is restored as one written before stash +/// notes were stamped (3.8.1 and earlier), unless that cannot be: its +/// metadata says a release that stamps stash notes wrote it, or it has no +/// readable autter version and the commit the stash was made on comes at or +/// after a commit whose note is stamped (`note_stamp_floor`). A note that +/// says an older release wrote it is restored: 3.8.0 and 3.8.1 stamped +/// commit notes but not stash notes, so such a stash on a stamped commit is +/// ordinary. +fn check_unstamped_stash_note(repo: &Repository, stash_sha: &str, body: &str) -> StashNoteCheck { + let Ok(log) = AuthorshipLog::deserialize_from_string(body) else { + return StashNoteCheck::Legacy; + }; + match version_claim(&log, FIRST_STASH_STAMPING_RELEASE) { + VersionClaim::Older(_) => StashNoteCheck::Legacy, + VersionClaim::Stamping(version) => StashNoteCheck::Failed(format!( + "has no integrity stamp but says autter {version} wrote it; autter {} and later stamp every stash note, so the stamp was removed", + release_name(FIRST_STASH_STAMPING_RELEASE) + )), + VersionClaim::Unknown => { + let base_query = format!("{stash_sha}^1"); + let base = batch_check(repo, std::slice::from_ref(&base_query)) + .ok() + .and_then(|found| found.get(&base_query).map(|(_, oid)| oid.clone())); + match base { + Some(base) + if crate::authorship::note_stamp_floor::at_or_after_stamping( + repo, &base, + ) => + { + StashNoteCheck::Failed( + "has no integrity stamp and no autter version, but the stash was made on a commit after stamping began, so the stamp was removed" + .to_string(), + ) + } + _ => StashNoteCheck::Legacy, + } + } + } +} + // --------------------------------------------------------------------------- // Readers: stats and blame // --------------------------------------------------------------------------- @@ -1640,9 +1851,59 @@ mod tests { RangeOutOfBounds, DigestMismatch, CarriedFailure, + ChangedAfterRecorded, + NotRecorded, + StampRemoved, + UnstampedByOlderAutter, ] { assert!(!(s.is_failure() && s.is_warning()), "{}", s.as_str()); assert_eq!(s == Verified, !s.is_failure() && !s.is_warning()); } + assert!(StampRemoved.is_failure()); + assert!(UnstampedByOlderAutter.is_warning()); + } + + fn log_with_version(version: Option<&str>) -> AuthorshipLog { + let mut log = AuthorshipLog::deserialize_from_string(NOTE).unwrap(); + log.metadata.autter_version = version.map(str::to_string); + log + } + + #[test] + fn version_claims_compare_with_the_first_stamping_release() { + let claim = |v: Option<&str>| version_claim(&log_with_version(v), FIRST_STAMPING_RELEASE); + assert_eq!( + claim(Some("3.9.1")), + VersionClaim::Stamping("3.9.1".to_string()) + ); + assert_eq!( + claim(Some("3.8.0")), + VersionClaim::Stamping("3.8.0".to_string()) + ); + assert_eq!( + claim(Some("development:3.9.1")), + VersionClaim::Stamping("development:3.9.1".to_string()) + ); + assert_eq!( + claim(Some("4.0.0-rc.1")), + VersionClaim::Stamping("4.0.0-rc.1".to_string()) + ); + assert_eq!( + claim(Some("3.7.1")), + VersionClaim::Older("3.7.1".to_string()) + ); + assert_eq!( + claim(Some("2.4.0")), + VersionClaim::Older("2.4.0".to_string()) + ); + assert_eq!(claim(Some("development")), VersionClaim::Unknown); + assert_eq!(claim(Some("")), VersionClaim::Unknown); + assert_eq!(claim(Some("3.x")), VersionClaim::Unknown); + assert_eq!(claim(None), VersionClaim::Unknown); + // Stash notes were stamped from 3.8.2. + let stash = + |v: &str| version_claim(&log_with_version(Some(v)), FIRST_STASH_STAMPING_RELEASE); + assert_eq!(stash("3.8.1"), VersionClaim::Older("3.8.1".to_string())); + assert_eq!(stash("3.8.2"), VersionClaim::Stamping("3.8.2".to_string())); } } diff --git a/src/authorship/note_stamp_floor.rs b/src/authorship/note_stamp_floor.rs new file mode 100644 index 0000000..0b7eee4 --- /dev/null +++ b/src/authorship/note_stamp_floor.rs @@ -0,0 +1,450 @@ +//! Where stamping began: the earliest commits whose authorship note carries +//! an integrity stamp (`note_integrity`). +//! +//! A note without a stamp reads as legacy, as autter releases before 3.8.0 +//! wrote it. On a commit made after commits with stamped notes, that claim +//! needs a second look (`note_integrity::verify_notes`): either the note was +//! written by an older autter (a teammate who has not upgraded) or its stamp +//! was removed. +//! +//! **What "after" means.** Commit topology, not dates: a commit comes after +//! stamping began when it descends from a commit whose note is stamped. +//! Commit dates are set by whoever makes the commit and change on a rebase, +//! so a date cut-off would let a forged or rebased date move a commit to +//! either side of it. Dates are used here only to choose the order in which +//! the floor is built, which changes how many git calls that takes, never +//! the result. +//! +//! **The floor.** The stamped commits that have no stamped ancestor (the +//! minimal elements of the set of stamped commits; on a linear history, the +//! first one). A commit descends from some stamped commit exactly when it +//! descends from a floor commit. Because no floor commit is an ancestor of +//! another, `git rev-list --ancestry-path --not ` lists +//! every one of `commits` that descends from the floor, in one call. +//! +//! **Cost.** Only notes without a stamp ask the question, so a repository +//! whose checked notes are all stamped never computes the floor. When it is +//! needed, it is computed once per process (and notes ref), and kept in +//! `.git/ai/stamp_floor.json` keyed by the tree of `refs/notes/ai`. When the +//! notes ref has moved since, only the notes that changed are read (one +//! `git diff-tree` and one `git cat-file`), and the floor is extended with +//! the new stamped ones. It is computed again from every note (one +//! `git grep` over the notes tree) only when there is no usable cache or a +//! floor commit's note changed or was removed. +//! +//! **Limits.** Notes are read from `refs/notes/ai`. With the `http` notes +//! backend there is no local notes ref, so the floor is empty and only the +//! version rule in `note_integrity` applies (the server's record covers +//! those notes). Commits whose notes are stamped but which are not in this +//! clone are left out of the floor. + +use crate::error::AutterError; +use crate::git::refs::AI_AUTHORSHIP_FULL_REF; +use crate::git::repository::{Repository, exec_git, exec_git_allow_nonzero, exec_git_stdin}; +use serde::{Deserialize, Serialize}; +use std::collections::{HashMap, HashSet}; +use std::path::PathBuf; +use std::sync::{Mutex, OnceLock}; + +/// What a stamped note looks like in the notes tree (`git grep -F`). +const STAMP_PATTERN: &str = "\"integrity\": {"; + +/// The cache file's format version. +const CACHE_VERSION: u32 = 1; + +/// Upper bound on the commits the floor may hold; past it the floor stops +/// growing (the check is then less strict, never stricter). +const MAX_FLOOR: usize = 256; + +#[derive(Serialize, Deserialize)] +struct CacheFile { + v: u32, + notes_tree: String, + floor: Vec, +} + +/// The floor of the repository's notes, as of the current `refs/notes/ai`. +/// Empty when there are no stamped notes, no notes ref, or the floor could +/// not be computed (the check is then skipped, never failed). +pub fn stamp_floor(repo: &Repository) -> Vec { + match try_stamp_floor(repo) { + Ok(floor) => floor, + Err(e) => { + tracing::debug!("note integrity: stamp floor not computed: {e}"); + Vec::new() + } + } +} + +/// The commits among `commits` made after stamping began: each descends from +/// a commit whose note is stamped. Never fails: on any error, none. +pub fn after_stamping(repo: &Repository, commits: &[String]) -> HashSet { + if commits.is_empty() { + return HashSet::new(); + } + let floor = stamp_floor(repo); + if floor.is_empty() { + return HashSet::new(); + } + match descendants_among(repo, &floor, commits) { + Ok(after) => after, + Err(e) => { + tracing::debug!("note integrity: ancestry of unstamped notes not checked: {e}"); + HashSet::new() + } + } +} + +/// Whether `commit` is a commit whose note is in the floor, or descends +/// from one. +pub fn at_or_after_stamping(repo: &Repository, commit: &str) -> bool { + let floor = stamp_floor(repo); + if floor.iter().any(|c| c == commit) { + return true; + } + match descendants_among(repo, &floor, &[commit.to_string()]) { + Ok(after) => after.contains(commit), + Err(e) => { + tracing::debug!("note integrity: ancestry of {commit} not checked: {e}"); + false + } + } +} + +/// (cache file, notes tree) -> floor, for this process. +type FloorMemo = Mutex>>; + +fn memo() -> &'static FloorMemo { + static MEMO: OnceLock = OnceLock::new(); + MEMO.get_or_init(|| Mutex::new(HashMap::new())) +} + +fn try_stamp_floor(repo: &Repository) -> Result, AutterError> { + let Some(notes_tree) = notes_tree(repo)? else { + return Ok(Vec::new()); + }; + let cache_path = repo.storage.ai_dir.join("stamp_floor.json"); + let key = (cache_path.clone(), notes_tree.clone()); + if let Some(floor) = memo().lock().ok().and_then(|m| m.get(&key).cloned()) { + return Ok(floor); + } + + let cached = std::fs::read(&cache_path) + .ok() + .and_then(|bytes| serde_json::from_slice::(&bytes).ok()) + .filter(|c| c.v == CACHE_VERSION); + let floor = match cached { + Some(c) if c.notes_tree == notes_tree => c.floor, + Some(c) => match extend_from_diff(repo, &c.notes_tree, ¬es_tree, c.floor) { + Ok(Some(floor)) => floor, + Ok(None) => full_floor(repo, ¬es_tree)?, + Err(e) => { + tracing::debug!("note integrity: stamp floor rebuilt: {e}"); + full_floor(repo, ¬es_tree)? + } + }, + None => full_floor(repo, ¬es_tree)?, + }; + + write_cache(&cache_path, ¬es_tree, &floor); + if let Ok(mut m) = memo().lock() { + // A long-running service sees many notes trees; keep the map small. + if m.len() > 64 { + m.clear(); + } + m.insert(key, floor.clone()); + } + Ok(floor) +} + +fn write_cache(path: &std::path::Path, notes_tree: &str, floor: &[String]) { + let Some(dir) = path.parent() else { + return; + }; + if !dir.is_dir() { + return; + } + let cache = CacheFile { + v: CACHE_VERSION, + notes_tree: notes_tree.to_string(), + floor: floor.to_vec(), + }; + let Ok(bytes) = serde_json::to_vec(&cache) else { + return; + }; + let tmp = dir.join(format!("stamp_floor.json.{}.tmp", std::process::id())); + if std::fs::write(&tmp, bytes).is_ok() && std::fs::rename(&tmp, path).is_err() { + let _ = std::fs::remove_file(&tmp); + } +} + +/// The tree of `refs/notes/ai`, or `None` when there is no such ref. +fn notes_tree(repo: &Repository) -> Result, AutterError> { + let mut args = repo.global_args_for_exec(); + args.extend( + [ + "rev-parse", + "--verify", + "--quiet", + &format!("{AI_AUTHORSHIP_FULL_REF}^{{tree}}"), + ] + .iter() + .map(|s| s.to_string()), + ); + let out = exec_git_allow_nonzero(&args)?; + if !out.status.success() { + return Ok(None); + } + let tree = String::from_utf8_lossy(&out.stdout).trim().to_string(); + Ok((!tree.is_empty()).then_some(tree)) +} + +/// A notes-tree path (`ab/cdef...` with fan-out, or flat) as a commit SHA. +fn commit_of_note_path(path: &str) -> Option { + let sha: String = path.chars().filter(|c| *c != '/').collect(); + let hex = sha.chars().all(|c| c.is_ascii_hexdigit()); + (hex && (sha.len() == 40 || sha.len() == 64)).then_some(sha) +} + +/// The floor computed from every note: one `git grep` over the notes tree +/// finds the stamped ones. +fn full_floor(repo: &Repository, notes_tree: &str) -> Result, AutterError> { + let mut args = repo.global_args_for_exec(); + args.extend( + [ + "grep", + "--no-color", + "-I", + "-l", + "-F", + "-e", + STAMP_PATTERN, + notes_tree, + "--", + ] + .iter() + .map(|s| s.to_string()), + ); + let out = exec_git_allow_nonzero(&args)?; + // `git grep` exits 1 when nothing matches. + match out.status.code() { + Some(0) => {} + Some(1) => return Ok(Vec::new()), + _ => { + return Err(AutterError::Generic(format!( + "git grep over the notes tree failed: {}", + String::from_utf8_lossy(&out.stderr).trim() + ))); + } + } + let prefix = format!("{notes_tree}:"); + let stamped: Vec = String::from_utf8_lossy(&out.stdout) + .lines() + .filter_map(|l| l.strip_prefix(&prefix)) + .filter_map(commit_of_note_path) + .collect(); + extend_floor(repo, Vec::new(), stamped) +} + +/// The cached floor brought up to date with the notes that changed between +/// `old_tree` and `new_tree`. `None` when it has to be computed again from +/// every note: a floor commit's note changed or was removed. +fn extend_from_diff( + repo: &Repository, + old_tree: &str, + new_tree: &str, + floor: Vec, +) -> Result>, AutterError> { + let mut args = repo.global_args_for_exec(); + args.extend( + [ + "diff-tree", + "-r", + "--no-renames", + "--raw", + "--no-abbrev", + old_tree, + new_tree, + ] + .iter() + .map(|s| s.to_string()), + ); + let out = exec_git(&args)?; + let in_floor: HashSet<&str> = floor.iter().map(String::as_str).collect(); + // commit -> new note blob, for notes added or changed. + let mut changed: Vec<(String, String)> = Vec::new(); + for line in String::from_utf8_lossy(&out.stdout).lines() { + // ": \t" + let Some((meta, path)) = line.split_once('\t') else { + continue; + }; + let fields: Vec<&str> = meta.split_whitespace().collect(); + let (Some(new_oid), Some(status)) = (fields.get(3), fields.get(4)) else { + continue; + }; + let Some(commit) = commit_of_note_path(path) else { + continue; + }; + let added = status.starts_with('A'); + if !added && in_floor.contains(commit.as_str()) { + return Ok(None); + } + if added || status.starts_with('M') { + changed.push((commit, new_oid.to_string())); + } + } + if changed.is_empty() { + return Ok(Some(floor)); + } + let blobs: Vec = changed.iter().map(|(_, oid)| oid.clone()).collect(); + let contents = crate::git::refs::batch_read_blob_contents(repo, &blobs)?; + let stamped: Vec = changed + .into_iter() + .filter(|(_, oid)| { + contents + .get(oid) + .is_some_and(|note| note.contains(STAMP_PATTERN)) + }) + .map(|(commit, _)| commit) + .collect(); + extend_floor(repo, floor, stamped).map(Some) +} + +/// `floor` (no member an ancestor of another) extended with the stamped +/// commits `stamped`: the result is the minimal elements of both together. +fn extend_floor( + repo: &Repository, + mut floor: Vec, + stamped: Vec, +) -> Result, AutterError> { + let mut seen: HashSet = floor.iter().cloned().collect(); + let mut pending: Vec = stamped + .into_iter() + .filter(|c| seen.insert(c.clone())) + .collect(); + // Only commits in this clone can be walked. + pending = present_commits(repo, &pending)?; + if pending.is_empty() { + return Ok(floor); + } + // Oldest first, so the first one taken is usually the floor itself. + pending = oldest_first(repo, &pending)?; + loop { + if !floor.is_empty() { + let later = descendants_among(repo, &floor, &pending)?; + pending.retain(|c| !later.contains(c)); + } + if pending.is_empty() || floor.len() >= MAX_FLOOR { + break; + } + let first = pending.remove(0); + let later = descendants_among(repo, std::slice::from_ref(&first), &floor)?; + floor.retain(|c| !later.contains(c)); + floor.push(first); + } + floor.sort(); + Ok(floor) +} + +/// The commits among `commits` that are in this clone. +fn present_commits(repo: &Repository, commits: &[String]) -> Result, AutterError> { + if commits.is_empty() { + return Ok(Vec::new()); + } + let mut args = repo.global_args_for_exec(); + args.push("cat-file".to_string()); + args.push("--batch-check=%(objecttype)".to_string()); + let stdin = commits.join("\n") + "\n"; + let out = exec_git_stdin(&args, stdin.as_bytes())?; + let text = String::from_utf8_lossy(&out.stdout); + Ok(commits + .iter() + .zip(text.lines()) + .filter(|(_, kind)| kind.trim() == "commit") + .map(|(c, _)| c.clone()) + .collect()) +} + +/// `commits` sorted by committer date, oldest first. +fn oldest_first(repo: &Repository, commits: &[String]) -> Result, AutterError> { + let mut args = repo.global_args_for_exec(); + args.extend( + ["rev-list", "--no-walk=sorted", "--stdin"] + .iter() + .map(|s| s.to_string()), + ); + let stdin = commits.join("\n") + "\n"; + let out = exec_git_stdin(&args, stdin.as_bytes())?; + let mut sorted: Vec = String::from_utf8_lossy(&out.stdout) + .lines() + .map(|l| l.trim().to_string()) + .filter(|l| !l.is_empty()) + .collect(); + sorted.reverse(); + Ok(sorted) +} + +/// The commits among `tips` that descend from (and are not) one of +/// `bottoms`, with one `git rev-list --ancestry-path`. `bottoms` must not +/// contain one another's ancestors: a tip that descends from one bottom and +/// is an ancestor of another would otherwise be missed. +fn descendants_among( + repo: &Repository, + bottoms: &[String], + tips: &[String], +) -> Result, AutterError> { + if bottoms.is_empty() || tips.is_empty() { + return Ok(HashSet::new()); + } + let mut args = repo.global_args_for_exec(); + args.extend( + ["rev-list", "--ancestry-path", "--stdin"] + .iter() + .map(|s| s.to_string()), + ); + let mut stdin = String::new(); + for tip in tips { + stdin.push_str(tip); + stdin.push('\n'); + } + for bottom in bottoms { + stdin.push('^'); + stdin.push_str(bottom); + stdin.push('\n'); + } + let out = exec_git_stdin(&args, stdin.as_bytes())?; + let wanted: HashSet<&str> = tips.iter().map(String::as_str).collect(); + Ok(String::from_utf8_lossy(&out.stdout) + .lines() + .map(str::trim) + .filter(|c| wanted.contains(c)) + .map(str::to_string) + .collect()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn note_paths_read_as_commits() { + let sha = "0123456789abcdef0123456789abcdef01234567"; + assert_eq!(commit_of_note_path(sha).as_deref(), Some(sha)); + assert_eq!( + commit_of_note_path("01/23456789abcdef0123456789abcdef01234567").as_deref(), + Some(sha) + ); + assert_eq!(commit_of_note_path("not-a-note"), None); + assert_eq!(commit_of_note_path("0123"), None); + } + + #[test] + fn the_pattern_matches_the_stamp_line_only() { + let stamped = + "x\n---\n{\n \"schema_version\": \"authorship/3.0.0\",\n \"integrity\": {\"v\":1}\n}"; + assert!(stamped.contains(STAMP_PATTERN)); + // An escaped mention inside a prompt does not match. + let mention = "x\n---\n{\n \"prompts\": {\"m\": \"\\\"integrity\\\": {\"}\n}"; + assert!(!mention.contains(STAMP_PATTERN)); + } +} diff --git a/src/commands/verify.rs b/src/commands/verify.rs index 1a2aaca..222dfe9 100644 --- a/src/commands/verify.rs +++ b/src/commands/verify.rs @@ -5,9 +5,11 @@ //! //! Exit codes: 0 when no note fails; [`EXIT_INTEGRITY_FAILED`] (5) when a note //! belongs to another commit, was changed (locally detected, or against the -//! server's record: `changed_after_recorded`), or was carried by a rewrite -//! from a note that failed. With `--strict`, a note rebound by an older -//! autter (whose digest cannot be checked) also exits 5. Missing and legacy +//! server's record: `changed_after_recorded`), was carried by a rewrite +//! from a note that failed, or had its stamp removed (`stamp_removed`). With +//! `--strict`, a note rebound by an older autter (whose digest cannot be +//! checked) and an unstamped note an older autter says it wrote on a commit +//! after stamping began (`unstamped_by_older_autter`) also exit 5. Missing and legacy //! notes are warnings, also with `--strict` for now (a later release will //! fail them under `--strict`). `not_recorded` is a warning, also with //! `--strict`; with `--require-record` it exits 5, and so does a run whose @@ -247,11 +249,16 @@ pub fn handle_verify(args: &[String]) { /// Whether `status` fails `autter verify`: every failure; under `--strict` /// a note rebound by an older autter, whose digest cannot be checked (anyone -/// can make a note look rebound); under `--require-record` a note the -/// server has no record of. +/// can make a note look rebound), and an unstamped note on a commit after +/// stamping began that says an older autter wrote it (anyone can edit that +/// version); under `--require-record` a note the server has no record of. fn fails(status: IntegrityStatus, policy: Policy) -> bool { status.is_failure() - || (policy.strict && status == IntegrityStatus::ReboundByOlderAutter) + || (policy.strict + && matches!( + status, + IntegrityStatus::ReboundByOlderAutter | IntegrityStatus::UnstampedByOlderAutter + )) || (policy.require_record && status == IntegrityStatus::NotRecorded) } @@ -359,6 +366,16 @@ fn print_human(target: &str, results: &[VerifyResult], policy: Policy, record_ch "--strict: a note rebound by an older autter cannot be checked against its digest, so it fails; run a newer autter over the range (rebase or `autter ci`) to stamp it again." ); } + if count(IntegrityStatus::StampRemoved) > 0 { + eprintln!( + "stamp_removed: autter 3.8.0 and later stamp every note they write, so a note without a stamp that one of them wrote, or one on a commit after stamping began with no autter version, had its stamp removed." + ); + } + if policy.strict && count(IntegrityStatus::UnstampedByOlderAutter) > 0 { + eprintln!( + "--strict: an unstamped note on a commit after stamping began says an older autter wrote it, which cannot be checked, so it fails; upgrade autter everywhere and rewrite the range (rebase or `autter ci`) to stamp it." + ); + } if policy.require_record && count(IntegrityStatus::NotRecorded) > 0 { eprintln!( "--require-record: a note the server has no record of fails; notes upload in the background, so run `autter verify` again once the upload has happened." diff --git a/tests/integration/note_integrity_check.rs b/tests/integration/note_integrity_check.rs index 0c3ef31..b917ac9 100644 --- a/tests/integration/note_integrity_check.rs +++ b/tests/integration/note_integrity_check.rs @@ -19,6 +19,7 @@ const FAILURES: &[&str] = &[ "range_out_of_bounds", "digest_mismatch", "carried_failure", + "stamp_removed", ]; const NOTICE_PREFIX: &str = "Autter's record for"; @@ -31,14 +32,14 @@ fn rev(repo: &TestRepo, r: &str) -> String { repo.git_og(&["rev-parse", r]).unwrap().trim().to_string() } -fn raw_note(repo: &TestRepo, commit: &str) -> String { +pub(crate) fn raw_note(repo: &TestRepo, commit: &str) -> String { repo.sync_daemon(); repo.git_og(&["notes", "--ref=ai", "show", commit]) .unwrap_or_else(|e| panic!("commit {commit} should have a note: {e}")) } /// Replace `commit`'s note with `content`, byte for byte (no git cleanup). -fn write_raw_note(repo: &TestRepo, commit: &str, content: &str) { +pub(crate) fn write_raw_note(repo: &TestRepo, commit: &str, content: &str) { let git_dir = repo .git_og(&["rev-parse", "--absolute-git-dir"]) .unwrap() @@ -137,6 +138,42 @@ fn ai_additions(repo: &TestRepo, commit: &str) -> u64 { doc["ai_additions"].as_u64().unwrap() } +/// `note` as an autter older than the integrity check wrote it: no +/// `integrity` member, and `autter_version` set to `version` (or removed, +/// for `None`). Notes from before 3.8.0 look like this, and so does a +/// stamped note whose stamp was removed and version edited. +pub(crate) fn as_unstamped_note(note: &str, version: Option<&str>) -> String { + let body = strip_integrity(note); + let key = "\"autter_version\": \""; + let start = body + .find(key) + .unwrap_or_else(|| panic!("note has no autter_version:\n{body}")); + let value_start = start + key.len(); + let value_end = value_start + body[value_start..].find('"').unwrap(); + match version { + Some(version) => format!("{}{}{}", &body[..value_start], version, &body[value_end..]), + None => { + let line_start = body[..start].rfind('\n').map_or(0, |i| i + 1); + let line_end = value_end + body[value_end..].find('\n').map_or(0, |i| i + 1); + format!("{}{}", &body[..line_start], &body[line_end..]) + } + } +} + +/// Replace every note in `refs/notes/ai` with `as_unstamped_note(note, +/// version)`: a repository whose notes all predate the integrity check. +pub(crate) fn make_every_note_unstamped(repo: &TestRepo, version: &str) { + repo.sync_daemon(); + let list = repo.git_og(&["notes", "--ref=ai", "list"]).unwrap(); + for line in list.lines() { + let Some(commit) = line.split_whitespace().nth(1) else { + continue; + }; + let note = raw_note(repo, commit); + write_raw_note(repo, commit, &as_unstamped_note(¬e, Some(version))); + } +} + /// The attestation section's first range line with its ranges replaced. fn replace_first_range(note: &str, ranges: &str) -> String { let divider = note.find("\n---\n").expect("note has a divider"); @@ -358,7 +395,7 @@ fn test_legacy_note_with_lines_past_the_file_fails_range_check() { let commit = repo.stage_all_and_commit("ai commit").unwrap().commit_sha; file.assert_committed_lines(crate::lines!["ai one".ai(), "human two".human()]); - let legacy = strip_integrity(&raw_note(&repo, &commit)); + let legacy = as_unstamped_note(&raw_note(&repo, &commit), Some("3.7.1")); write_raw_note(&repo, &commit, &replace_first_range(&legacy, "1-40")); assert_status(&repo, &commit, "range_out_of_bounds"); @@ -397,7 +434,8 @@ fn test_note_without_integrity_member_is_legacy() { let commit = repo.stage_all_and_commit("ai commit").unwrap().commit_sha; file.assert_committed_lines(crate::lines!["ai one".ai(), "human two".human()]); - write_raw_note(&repo, &commit, &strip_integrity(&raw_note(&repo, &commit))); + let note = raw_note(&repo, &commit); + write_raw_note(&repo, &commit, &as_unstamped_note(¬e, Some("3.7.1"))); assert_status(&repo, &commit, "legacy"); file.assert_committed_lines(crate::lines!["ai one".ai(), "human two".human()]); @@ -1266,7 +1304,7 @@ fn test_unstamped_stash_note_from_older_autter_still_restores() { let repo = TestRepo::new(); let stash = stash_an_ai_edit(&repo); let note = raw_stash_note(&repo, &stash); - write_raw_stash_note(&repo, &stash, &strip_integrity(¬e)); + write_raw_stash_note(&repo, &stash, &as_unstamped_note(¬e, Some("3.8.1"))); assert!(read_integrity(&raw_stash_note(&repo, &stash)).is_none()); let commit = pop_and_commit(&repo); @@ -1379,11 +1417,12 @@ fn test_backfilled_tampered_note_still_fails() { } /// A note written before the integrity check stays legacy: stamping it on -/// the way into git would make an unchecked note verified. +/// the way into git would make an unchecked note verified. Later commits +/// with stamped notes do not change that: the old commit comes before them. #[test] fn test_backfilled_legacy_note_stays_legacy() { let repo = TestRepo::new(); - let commit = note_only_in_db(&repo, strip_integrity); + let commit = note_only_in_db(&repo, |note| as_unstamped_note(note, Some("3.7.1"))); let (stdout, _) = backfill(&repo); assert!(stdout.contains("Backfilled 1 authorship note "), "{stdout}"); @@ -1391,4 +1430,382 @@ fn test_backfilled_legacy_note_stays_legacy() { assert_status(&repo, &commit, "legacy"); let mut file = repo.filename("app.txt"); file.assert_committed_lines(crate::lines!["human one".human(), "ai one".ai()]); + + // Stamping begins after the backfilled commit. + let mut later = repo.filename("later.txt"); + later.set_contents(crate::lines!["ai later".ai()]); + let stamped = repo.stage_all_and_commit("later").unwrap().commit_sha; + later.assert_committed_lines(crate::lines!["ai later".ai()]); + assert_status(&repo, &stamped, "verified"); + assert_status(&repo, &commit, "legacy"); + let (code, doc, _) = verify_json(&repo, &commit, &["--strict"]); + assert_eq!(code, 0, "{doc}"); + file.assert_committed_lines(crate::lines!["human one".human(), "ai one".ai()]); +} + +/// A stamped note copied into the notes database with its stamp removed is +/// written as stored by the backfill, and fails like any note whose stamp +/// was removed. +#[test] +fn test_backfilled_note_with_its_stamp_removed_fails() { + let repo = TestRepo::new(); + let commit = note_only_in_db(&repo, strip_integrity); + + let (stdout, stderr) = backfill(&repo); + assert!(stdout.contains("Backfilled 1 authorship note "), "{stdout}"); + assert_status(&repo, &commit, "stamp_removed"); + assert!( + stderr.contains("1 of them fails the integrity check") && stderr.contains(&commit), + "{stderr}" + ); + assert_eq!(ai_additions(&repo, &commit), 0); +} + +// --------------------------------------------------------------------------- +// Notes whose stamp was removed +// --------------------------------------------------------------------------- +// +// A note without an `integrity` member used to read as legacy whatever it +// said, so removing the stamp from a note and editing it passed `autter +// verify`, also with `--strict`. An unstamped note now passes as legacy only +// when it can be one: its `autter_version` names a release older than 3.8.0 +// (autter 3.8.0 and later stamp every note), and, when its commit descends +// from a commit whose note is stamped, it is a warning +// (`unstamped_by_older_autter`, a failure under `--strict`). + +fn stats_json(repo: &TestRepo, commit: &str) -> Value { + repo.sync_daemon(); + let out = repo + .autter_command(&["stats", commit, "--json"]) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&out.stdout).to_string(); + let line = stdout + .lines() + .rev() + .find(|l| l.trim_start().starts_with('{')) + .unwrap_or_else(|| panic!("no stats JSON: {stdout}")); + serde_json::from_str(line).unwrap() +} + +/// An AI commit (stamped) that comes before the commits under test, so +/// stamping began before them. +fn stamped_base_commit(repo: &TestRepo) -> String { + let mut base = repo.filename("base.txt"); + base.set_contents(crate::lines!["base ai".ai(), "base human"]); + let commit = repo + .stage_all_and_commit("stamped base") + .unwrap() + .commit_sha; + base.assert_committed_lines(crate::lines!["base ai".ai(), "base human".human()]); + assert_status(repo, &commit, "verified"); + commit +} + +/// Five AI lines and one human line in `app.txt` (83% AI). Returns the commit. +fn five_of_six_ai_lines(repo: &TestRepo) -> String { + let mut file = repo.filename("app.txt"); + file.set_contents(crate::lines![ + "ai 1".ai(), + "ai 2".ai(), + "ai 3".ai(), + "ai 4".ai(), + "ai 5".ai(), + "human 6" + ]); + let commit = repo.stage_all_and_commit("ai commit").unwrap().commit_sha; + file.assert_committed_lines(crate::lines![ + "ai 1".ai(), + "ai 2".ai(), + "ai 3".ai(), + "ai 4".ai(), + "ai 5".ai(), + "human 6".human() + ]); + commit +} + +/// The reported case: a 3.9.1 note's stamp removed and an attestation edited +/// (five AI lines down to two, 83% to 33%) passed as legacy, also with +/// `--strict`, and stats reported 33% AI. It now fails with `stamp_removed` +/// (exit 5, with and without `--strict`) and its lines count as unknown. +#[test] +fn test_note_with_its_stamp_removed_and_ranges_edited_fails() { + let repo = TestRepo::new(); + let mut readme = repo.filename("README.md"); + readme.set_contents(crate::lines!["readme"]); + repo.stage_all_and_commit("readme").unwrap(); + readme.assert_committed_lines(crate::lines!["readme".human()]); + let base = stamped_base_commit(&repo); + let commit = five_of_six_ai_lines(&repo); + assert_status(&repo, &commit, "verified"); + let stats = stats_json(&repo, &commit); + assert_eq!(stats["ai_additions"], 5, "{stats}"); + + let note = raw_note(&repo, &commit); + assert!( + note.contains("\"autter_version\": \"development:"), + "{note}" + ); + let edited = replace_first_range(&strip_integrity(¬e), "1-2"); + assert!(edited.contains(" 1-2\n"), "{edited}"); + write_raw_note(&repo, &commit, &edited); + + assert_status(&repo, &commit, "stamp_removed"); + let (code, doc, _) = verify_json(&repo, &commit, &["--strict"]); + assert_eq!(code, 5, "{doc}"); + assert_eq!(doc["results"][0]["status"], "stamp_removed", "{doc}"); + assert_eq!(doc["error"]["code"], "integrity_failed", "{doc}"); + let detail = doc["results"][0]["detail"].as_str().unwrap(); + assert!(detail.contains("stamp was removed"), "{detail}"); + + // Not 2 of 6 (33%) AI: the note is not trusted, so all six are unknown. + let stats = stats_json(&repo, &commit); + assert_eq!(stats["ai_additions"], 0, "{stats}"); + assert_eq!(stats["unknown_additions"], 6, "{stats}"); + assert_blame_distrusts_note(&repo, &repo.filename("app.txt"), "app.txt"); + let out = repo.autter_command(&["stats", &commit]).output().unwrap(); + let stderr = String::from_utf8_lossy(&out.stderr); + assert!( + stderr.contains(NOTICE_PREFIX) && stderr.contains("had its integrity stamp removed"), + "{stderr}" + ); + + // A range over both commits fails on the tampered one only. + let (code, doc, _) = verify_json(&repo, &format!("{base}~1..{commit}"), &[]); + assert_eq!(code, 5, "{doc}"); + assert_eq!(doc["counts"]["verified"], 1, "{doc}"); + assert_eq!(doc["counts"]["stamp_removed"], 1, "{doc}"); + assert_eq!(doc["failed"], 1, "{doc}"); + assert_status(&repo, &base, "verified"); +} + +/// The very first stamped commit has no stamped commit before it; its note +/// says which autter wrote it, and every autter since 3.8.0 stamps. +#[test] +fn test_first_stamped_note_with_its_stamp_removed_fails() { + let repo = TestRepo::new(); + let commit = five_of_six_ai_lines(&repo); + let note = raw_note(&repo, &commit); + write_raw_note(&repo, &commit, &strip_integrity(¬e)); + + assert_status(&repo, &commit, "stamp_removed"); + assert_eq!(ai_additions(&repo, &commit), 0); +} + +/// Removing the stamp and the version too: the note no longer says who wrote +/// it, but its commit comes after a stamped commit, so it was not written +/// before stamping began. +#[test] +fn test_stamp_and_version_removed_after_stamping_fails() { + let repo = TestRepo::new(); + stamped_base_commit(&repo); + let commit = five_of_six_ai_lines(&repo); + let note = raw_note(&repo, &commit); + let edited = replace_first_range(&as_unstamped_note(¬e, None), "1-2"); + assert!(!edited.contains("autter_version"), "{edited}"); + write_raw_note(&repo, &commit, &edited); + + assert_status(&repo, &commit, "stamp_removed"); + assert_eq!(ai_additions(&repo, &commit), 0); + assert_blame_distrusts_note(&repo, &repo.filename("app.txt"), "app.txt"); +} + +/// A repository whose notes all predate the integrity check (no stamps, +/// autter 3.7.1) verifies as legacy, also with `--strict`, and its lines are +/// still used. A note with no version at all is legacy there too: nothing +/// in the repository is stamped. +#[test] +fn test_repository_with_only_pre_3_8_notes_is_legacy() { + let repo = TestRepo::new(); + let mut one = repo.filename("one.txt"); + one.set_contents(crate::lines!["ai one".ai(), "human one"]); + let first = repo.stage_all_and_commit("one").unwrap().commit_sha; + one.assert_committed_lines(crate::lines!["ai one".ai(), "human one".human()]); + let mut two = repo.filename("two.txt"); + two.set_contents(crate::lines!["human two", "ai two".ai()]); + let second = repo.stage_all_and_commit("two").unwrap().commit_sha; + two.assert_committed_lines(crate::lines!["human two".human(), "ai two".ai()]); + + make_every_note_unstamped(&repo, "3.7.1"); + let note = raw_note(&repo, &second); + write_raw_note(&repo, &second, &as_unstamped_note(¬e, None)); + + assert_status(&repo, &first, "legacy"); + assert_status(&repo, &second, "legacy"); + let (code, doc, stderr) = verify_json(&repo, &format!("{first}..{second}"), &["--strict"]); + assert_eq!(code, 0, "{doc}"); + assert_eq!(doc["counts"]["legacy"], 1, "{doc}"); + assert_eq!(doc["failed"], 0, "{doc}"); + assert!( + stderr.contains("a later release will fail them"), + "{stderr}" + ); + let (code, doc, _) = verify_json(&repo, &first, &["--strict"]); + assert_eq!(code, 0, "{doc}"); + one.assert_committed_lines(crate::lines!["ai one".ai(), "human one".human()]); + two.assert_committed_lines(crate::lines!["human two".human(), "ai two".ai()]); + assert_eq!(ai_additions(&repo, &second), 1); +} + +/// A teammate still on autter 3.7 commits after this machine started +/// stamping: their unstamped notes say 3.7.1. That is +/// `unstamped_by_older_autter`, a warning whose lines still count, and a +/// failure only under `--strict`, like `rebound_by_older_autter`: the version +/// cannot be checked, and someone who removes a stamp can edit it too. +/// +/// "After" is topology, not dates: a 3.7.1 note on a branch that forked +/// before the first stamped commit stays legacy even though it was committed +/// later. +#[test] +fn test_older_autter_teammate_after_stamping_warns_and_fails_only_under_strict() { + let repo = TestRepo::new(); + // A history from before the upgrade: every note from 3.7.1. + let mut old = repo.filename("old.txt"); + old.set_contents(crate::lines!["old ai".ai(), "old human"]); + let fork_point = repo.stage_all_and_commit("old").unwrap().commit_sha; + old.assert_committed_lines(crate::lines!["old ai".ai(), "old human".human()]); + make_every_note_unstamped(&repo, "3.7.1"); + let main = repo.current_branch(); + repo.git(&["branch", "side"]).unwrap(); + + // This machine upgrades: the next commit's note is stamped. + let stamped = stamped_base_commit(&repo); + + // A teammate on 3.7 commits on top of it. + let mut team = repo.filename("team.txt"); + team.set_contents(crate::lines!["team ai".ai(), "team human"]); + let teammate = repo.stage_all_and_commit("teammate").unwrap().commit_sha; + team.assert_committed_lines(crate::lines!["team ai".ai(), "team human".human()]); + let note = raw_note(&repo, &teammate); + write_raw_note(&repo, &teammate, &as_unstamped_note(¬e, Some("3.7.1"))); + + // Another teammate on 3.7 commits later, on a branch from before stamping. + repo.git(&["checkout", "side"]).unwrap(); + let mut side = repo.filename("side.txt"); + side.set_contents(crate::lines!["side ai".ai(), "side human"]); + let side_commit = repo.stage_all_and_commit("side").unwrap().commit_sha; + side.assert_committed_lines(crate::lines!["side ai".ai(), "side human".human()]); + let note = raw_note(&repo, &side_commit); + write_raw_note( + &repo, + &side_commit, + &as_unstamped_note(¬e, Some("3.7.1")), + ); + repo.git(&["checkout", &main]).unwrap(); + + assert_status(&repo, &fork_point, "legacy"); + assert_status(&repo, &stamped, "verified"); + assert_status(&repo, &side_commit, "legacy"); + assert_status(&repo, &teammate, "unstamped_by_older_autter"); + let (_, _, doc) = status_of(&repo, &teammate); + let detail = doc["results"][0]["detail"].as_str().unwrap(); + assert!(detail.contains("3.7.1"), "{detail}"); + + // Its lines are still used, as a warning's are. + team.assert_committed_lines(crate::lines!["team ai".ai(), "team human".human()]); + assert_eq!(ai_additions(&repo, &teammate), 1); + + // --strict fails it, and only it. + let (code, doc, _) = verify_json(&repo, &teammate, &["--strict"]); + assert_eq!(code, 5, "{doc}"); + assert_eq!(doc["failed"], 1, "{doc}"); + let (code, doc, _) = verify_json(&repo, &side_commit, &["--strict"]); + assert_eq!(code, 0, "{doc}"); + let out = repo + .autter_command(&["verify", &teammate, "--strict"]) + .output() + .unwrap(); + assert_eq!(out.status.code(), Some(5)); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!( + stdout.contains("FAIL") && stdout.contains("unstamped_by_older_autter"), + "{stdout}" + ); + + // When the stamped commit's own note is changed back to a 3.7.1 one, + // nothing in the history is stamped any more and the teammate's note is + // legacy again (the cached floor is rebuilt, not reused). + let note = raw_note(&repo, &stamped); + write_raw_note(&repo, &stamped, &as_unstamped_note(¬e, Some("3.7.1"))); + assert_status(&repo, &stamped, "legacy"); + assert_status(&repo, &teammate, "legacy"); + old.assert_committed_lines(crate::lines!["old ai".ai(), "old human".human()]); +} + +/// Rewrites stamp what they write: a branch whose notes are all from 3.7.1, +/// rebased onto commits with stamped notes, gets notes that verify (not +/// `stamp_removed` or `unstamped_by_older_autter`); amending and +/// cherry-picking a stamped commit leave notes that verify. +#[test] +fn test_rewrites_after_stamping_began_verify() { + let repo = TestRepo::new(); + let main = feature_behind_main(&repo); + make_every_note_unstamped(&repo, "3.7.1"); + repo.git(&["checkout", &main]).unwrap(); + let stamped = stamped_base_commit(&repo); + repo.git(&["checkout", "feature"]).unwrap(); + assert_status(&repo, &rev(&repo, "HEAD"), "legacy"); + + repo.git(&["rebase", &main]).unwrap(); + repo.sync_daemon(); + assert_feature_lines(&repo); + assert_verified(&repo, &["HEAD~1", "HEAD"]); + assert_status(&repo, &stamped, "verified"); + + // Amend a stamped commit. + repo.git(&["commit", "--amend", "-m", "feature two, reworded"]) + .unwrap(); + repo.sync_daemon(); + assert_feature_lines(&repo); + assert_verified(&repo, &["HEAD"]); + + // Cherry-pick a stamped commit onto main. + let picked = rev(&repo, "HEAD~1"); + repo.git(&["checkout", &main]).unwrap(); + repo.git(&["cherry-pick", &picked]).unwrap(); + repo.sync_daemon(); + let mut one = repo.filename("one.txt"); + one.assert_committed_lines(crate::lines!["human one".human(), "ai one".ai()]); + assert_verified(&repo, &["HEAD"]); +} + +/// A stash note with its stamp removed is not restored: the release that +/// wrote it stamps every stash note. (Until now it restored like a note +/// from 3.8.1.) +#[test] +fn test_stash_note_with_its_stamp_removed_is_not_restored() { + let repo = TestRepo::new(); + let stash = stash_an_ai_edit(&repo); + let note = raw_stash_note(&repo, &stash); + write_raw_stash_note(&repo, &stash, &strip_integrity(¬e)); + + let commit = pop_and_commit(&repo); + let mut file = repo.filename("app.txt"); + file.assert_committed_lines(crate::lines![ + "base".unattributed_human(), + "untracked line".unattributed_human(), + "ai line".unattributed_human() + ]); + assert_eq!(ai_additions(&repo, &commit), 0); +} + +/// A stash note with its stamp and version removed, made on a commit after +/// stamping began, is not restored either. +#[test] +fn test_stash_note_without_stamp_or_version_after_stamping_is_not_restored() { + let repo = TestRepo::new(); + stamped_base_commit(&repo); + let stash = stash_an_ai_edit(&repo); + let note = raw_stash_note(&repo, &stash); + write_raw_stash_note(&repo, &stash, &as_unstamped_note(¬e, None)); + + let commit = pop_and_commit(&repo); + let mut file = repo.filename("app.txt"); + file.assert_committed_lines(crate::lines![ + "base".unattributed_human(), + "untracked line".unattributed_human(), + "ai line".unattributed_human() + ]); + assert_eq!(ai_additions(&repo, &commit), 0); } diff --git a/tests/integration/verify_server_record.rs b/tests/integration/verify_server_record.rs index 3cb1049..3c93573 100644 --- a/tests/integration/verify_server_record.rs +++ b/tests/integration/verify_server_record.rs @@ -848,10 +848,8 @@ fn test_ci_rerun_on_the_same_merge_writes_one_note() { fn test_legacy_notes_on_unchanged_commits_are_not_restamped() { let repo = TestRepo::new(); let (main, commits) = feature_on_top_of_main(&repo); - for commit in &commits { - let legacy = autter::authorship::note_integrity::strip_integrity(&raw_note(&repo, commit)); - write_raw_note(&repo, commit, &legacy); - } + // Every note in the repository as autter 3.7.1 wrote it. + crate::note_integrity_check::make_every_note_unstamped(&repo, "3.7.1"); let before = notes_of(&repo, &commits); assert!(read_integrity(&before[0].1).is_none());