Elffuss BraidHub

utopia-ia/elffuss-braid public

verify: count the hour's reverts by each one's own committer time

Committer times do not only grow along a branch: a changeset that landed
by a fast-forward keeps the time it was made. The walk for verify's
reverts of the last hour stopped at the first changeset older than an
hour, and missed the reverts below it. It now goes down 200 changesets,
or to the first made more than a day ago, and counts each revert by its
own time.
Changeset9598fdbd3dec4ebcea41526cff928bf411a9b0b29efa132978490ecc6e8117d6
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 15:54 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 15:54 +02:00
Parents1e0ec6999675

Browse the files at this changeset

1 file changed

modified crates/cli/src/verify.rs +80 −15
@@ -16,8 +16,8 @@
1616 //! fresh clone, as CI has, follows the same rules: a revert verify made
1717 //! carries a `reverts` field and the agent `braid-verify`, and verify never
1818 //! reverts such a changeset, nor lands more reverts an hour than it is
19-//! allowed, counting them by their committer time. A label anyone can write
20-//! is safe to refuse on. `.braid/verify/` keeps a record of the reverts this
19+//! allowed, counting them by their own committer time. A label anyone can
20+//! write is safe to refuse on. `.braid/verify/` keeps a record of the reverts this
2121 //! repository landed, as a second source for both rules.
2222
2323 use std::collections::{BTreeSet, HashSet};
@@ -61,9 +61,13 @@
6161 /// The prefix of the scratch directories the tests run in.
6262 const SCRATCH_PREFIX: &str = "braid-verify-";
6363
64-/// How far down the branch verify looks for its own reverts of the last
65-/// hour, at most.
66-const HISTORY_LIMIT: usize = 10_000;
64+/// How many changesets down the branch verify looks for its own reverts of
65+/// the last hour, at most.
66+const HISTORY_DEPTH: usize = 200;
67+
68+/// The walk for those reverts stops at the first changeset committed more
69+/// than this long, in seconds, before now: a day.
70+const HISTORY_AGE: i64 = 86_400;
6771
6872 /// What the watchdog says once it runs, before braid starts any test. The
6973 /// number changes with the protocol, so that another braid's watchdog is
@@ -129,9 +133,10 @@
129133 - With --revert, the culprit is reverted and the revert landed, as
130134 `braid revert` does. Verify does not revert a merge, nor a revert it
131135 made (one with a reverts field and the agent braid-verify), nor more
132- than --max-reverts-per-hour (3 by default), which it counts on the
133- branch by committer time and in .braid/verify/. The green ref does not
134- move: the next run tests the new head.
136+ than --max-reverts-per-hour (3 by default), which it counts by each
137+ revert's committer time among the last 200 changesets of the branch
138+ (down to one made more than a day before), and in .braid/verify/. The
139+ green ref does not move: the next run tests the new head.
135140
136141 The green ref is local: a fresh clone has none, so give --from there.
137142 --dry-run runs the tests but moves no ref, records nothing, lands nothing
@@ -354,20 +359,25 @@
354359 extra(changeset, REVERTS).is_some() && extra(changeset, AGENT_FIELD) == Some(AGENT.as_bytes())
355360 }
356361
357-/// The reverts verify made that landed on the branch below `tip` in the
358-/// hour before `now`, by committer time. The walk down first parents stops
359-/// at the first changeset committed earlier than that.
362+/// The reverts verify made on the branch below `tip` that were committed in
363+/// the hour before `now`, each by its own committer time. Committer times
364+/// do not only grow along a branch (a changeset that landed by a
365+/// fast-forward keeps the time it was made), so the walk down first parents
366+/// goes on past older changesets: it stops after [`HISTORY_DEPTH`]
367+/// changesets, or at the first one committed more than [`HISTORY_AGE`]
368+/// before now.
360369 fn reverts_in_last_hour(repo: &Repo, tip: ObjectId, now: u64) -> Result<HashSet<String>> {
361- let since = now as i64 - 3600;
370+ let now = i64::try_from(now).unwrap_or(i64::MAX);
362371 let mut found = HashSet::new();
363372 let mut current = Some(tip);
364- for _ in 0..HISTORY_LIMIT {
373+ for _ in 0..HISTORY_DEPTH {
365374 let Some(id) = current else { break };
366375 let changeset = braid_engine::read_changeset(repo.store(), id)?;
367- if changeset.committer.seconds <= since {
376+ let committed = changeset.committer.seconds;
377+ if committed < now.saturating_sub(HISTORY_AGE) {
368378 break;
369379 }
370- if is_verify_revert(&changeset) {
380+ if committed > now.saturating_sub(3600) && is_verify_revert(&changeset) {
371381 found.insert(id.to_hex());
372382 }
373383 current = changeset.parents.first().copied();
@@ -1312,6 +1322,61 @@
13121322 assert!(err.contains("delete it"), "{err}");
13131323 }
13141324
1325+ #[test]
1326+ fn the_hourly_cap_counts_by_each_reverts_own_time() {
1327+ let temp = tempfile::tempdir().unwrap();
1328+ let repo = Repo::init(temp.path()).unwrap();
1329+ let now: i64 = 1_800_000_000;
1330+ let signature = |seconds| braid_format::Signature {
1331+ name: b"Ada".to_vec(),
1332+ email: b"ada@example.org".to_vec(),
1333+ seconds,
1334+ offset_minutes: 0,
1335+ };
1336+ let revert_by = |agent: &str| {
1337+ vec![
1338+ (AGENT_FIELD.to_vec(), agent.as_bytes().to_vec()),
1339+ (REVERTS.to_vec(), "c".repeat(64).into_bytes()),
1340+ ]
1341+ };
1342+ let commit = |parent: Option<ObjectId>, ago: i64, extras| {
1343+ braid_engine::commit(
1344+ repo.store(),
1345+ parent.into_iter().collect(),
1346+ Vec::new(),
1347+ signature(now - ago),
1348+ signature(now - ago),
1349+ b"change\n".to_vec(),
1350+ extras,
1351+ )
1352+ .unwrap()
1353+ };
1354+ // Down from the tip: a revert by someone else, one of verify's, a
1355+ // changeset that landed by a fast-forward hours after it was made,
1356+ // another of verify's below it, one older than an hour, and a base
1357+ // older than a day, where the walk stops.
1358+ let base = commit(None, 2 * 86_400, Vec::new());
1359+ let too_old = commit(Some(base), 2 * 3600, revert_by(AGENT));
1360+ let below = commit(Some(too_old), 30 * 60, revert_by(AGENT));
1361+ let made_earlier = commit(Some(below), 3 * 3600, Vec::new());
1362+ let recent = commit(Some(made_earlier), 10 * 60, revert_by(AGENT));
1363+ let others = commit(Some(recent), 5 * 60, revert_by("ada"));
1364+ let tip = commit(Some(others), 60, Vec::new());
1365+ let found = reverts_in_last_hour(&repo, tip, now as u64).unwrap();
1366+ let expected: HashSet<String> = [below, recent].iter().map(ObjectId::to_hex).collect();
1367+ assert_eq!(found, expected);
1368+
1369+ // A revert deeper than the walk goes is not counted.
1370+ let mut top = commit(Some(tip), 60, revert_by(AGENT));
1371+ let deep = top;
1372+ for _ in 0..HISTORY_DEPTH {
1373+ top = commit(Some(top), 30, Vec::new());
1374+ }
1375+ let found = reverts_in_last_hour(&repo, top, now as u64).unwrap();
1376+ assert!(!found.contains(&deep.to_hex()));
1377+ assert!(found.is_empty(), "{found:?}");
1378+ }
1379+
13151380 /// A stand-in for the watchdog: `script`, run by `sh`.
13161381 #[cfg(unix)]
13171382 fn fake_watchdog(script: &str) -> Command {