From 0ed88abe273c814ae4e034f62ca5eeb7ad895fed Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 18 Nov 2026 06:03:53 -0600 Subject: [PATCH] fix(id): one archive-tag rule for writer and reader (#11790) The FileOps writer reserved any non-empty stem with an all-digit suffix, while the reader required a parseable u64 suffix and accepted an empty stem. So '.archive.5' was an archive to the reader only, and overflowing digit suffixes were reserved by the writer but primary to the reader. Both sides now use archive_tag_timestamp in id-sync: non-empty stem, unsigned decimal suffix that fits in u64. Tests cover both shapes. --- pkgs/id/crates/id-sync/src/fileops.rs | 38 ++++++++++++++++++++++++--- pkgs/id/crates/id-web/src/routes.rs | 35 +++++++++++++----------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/pkgs/id/crates/id-sync/src/fileops.rs b/pkgs/id/crates/id-sync/src/fileops.rs index b583f7cb92..4b53edf7dd 100644 --- a/pkgs/id/crates/id-sync/src/fileops.rs +++ b/pkgs/id/crates/id-sync/src/fileops.rs @@ -180,12 +180,21 @@ async fn free_archive_name(store: &Store, name: &str, ts: u64) -> Result } } +/// The archive timestamp of `name` when it has the archive tag shape used by +/// FileOps: `{stem}.archive.{ts}` with a non-empty stem and an unsigned +/// decimal `ts` that fits in u64. Readers that classify archives must use this +/// same rule so writer and reader never disagree on a name. +pub fn archive_tag_timestamp(name: &str) -> Option { + let (file_name, suffix) = name.rsplit_once(".archive.")?; + if file_name.is_empty() || suffix.is_empty() || !suffix.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + suffix.parse::().ok() +} + /// Whether `name` has the archive tag shape used by FileOps. fn is_archive_tag_name(name: &str) -> bool { - let Some((file_name, suffix)) = name.rsplit_once(".archive.") else { - return false; - }; - !file_name.is_empty() && !suffix.is_empty() && suffix.bytes().all(|b| b.is_ascii_digit()) + archive_tag_timestamp(name).is_some() } /// Resolve a free archive name for `name` at `ts` and claim it for @@ -797,6 +806,27 @@ fn is_owned_archive_tag_name(name: &str, archive_name: &str) -> bool { .is_some_and(|suffix| !suffix.is_empty() && suffix.bytes().all(|b| b.is_ascii_digit())) } +#[cfg(test)] +mod archive_tag_shape_tests { + use super::archive_tag_timestamp; + + #[test] + fn archive_tag_timestamp_matches_writer_and_reader_rule() { + assert_eq!(archive_tag_timestamp("x.archive.5"), Some(5)); + assert_eq!( + archive_tag_timestamp("x.archive.18446744073709551615"), + Some(u64::MAX) + ); + assert_eq!(archive_tag_timestamp(".archive.5"), None); + assert_eq!( + archive_tag_timestamp("x.archive.18446744073709551616"), + None + ); + assert_eq!(archive_tag_timestamp("x.archive.+5"), None); + assert_eq!(archive_tag_timestamp("x.archive.notes"), None); + } +} + #[cfg(test)] mod archive_reference_tests { use super::is_owned_archive_tag_name; diff --git a/pkgs/id/crates/id-web/src/routes.rs b/pkgs/id/crates/id-web/src/routes.rs index d884ed0a80..503a26e581 100644 --- a/pkgs/id/crates/id-web/src/routes.rs +++ b/pkgs/id/crates/id-web/src/routes.rs @@ -367,22 +367,11 @@ fn classify_tag(name: &str) -> (FileKind, Option) { } } - // Check for archive: "{name}.archive.{unix_timestamp}" - // Only an archive when the suffix actually parses as a unix - // timestamp (#2793) — the same rule #809 applied to the "auto-" - // prefix above. An ordinary file that merely contains ".archive." - // (notes.archive.txt) is Primary. No producer mints a non-numeric - // suffix: the archive claim path always formats a u64. - if let Some(pos) = name.rfind(".archive.") { - let ts_str = &name[pos + ".archive.".len()..]; - // The producer's Display form is unsigned decimal digits only: - // parse::() would also accept a leading '+', a spelling no - // producer mints (#4329). - if !ts_str.is_empty() && ts_str.bytes().all(|b| b.is_ascii_digit()) { - if let Ok(ts) = ts_str.parse::() { - return (FileKind::Archive, Some(ts)); - } - } + // Check for archive: "{name}.archive.{unix_timestamp}". The shared + // FileOps rule decides it, so the writer's reservation and this reader + // classify every name the same way (#2793, #4329, #11790). + if let Some(ts) = id_sync::fileops::archive_tag_timestamp(name) { + return (FileKind::Archive, Some(ts)); } (FileKind::Primary, None) @@ -5457,6 +5446,20 @@ mod tests { } } + #[test] + fn test_classify_tag_archive_matches_writer_rule() { + // Regression (#11790): the reader must agree with the FileOps writer + // on empty-stem and u64-overflow archive suffixes. + for name in [".archive.5", "x.archive.18446744073709551616"] { + let (kind, ts) = classify_tag(name); + assert_eq!(kind, FileKind::Primary, "{name}"); + assert!(ts.is_none(), "{name}"); + } + let (kind, ts) = classify_tag("x.archive.18446744073709551615"); + assert_eq!(kind, FileKind::Archive); + assert_eq!(ts, Some(u64::MAX)); + } + #[test] fn test_parse_iso_timestamp_rejects_out_of_range_components() { // Each of these used to roll over into a different, plausible