Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
test(trace): pin the ThreadId Debug parse, and give it one home (#4184)
Worktrunk Botb917a87bd5de
diff --git a/src/logging.rs b/src/logging.rsindex 402bdeb55..b31f6912e 100644--- a/src/logging.rs+++ b/src/logging.rs@@ -56,33 +56,24 @@ use crate::output;/// Single-character thread label (e.g. `a`, `b`, …, `A`, …) used to group/// concurrent records by thread in stderr / trace.log output.+///+/// Shares [`thread_id`]'s parse rather than repeating it, so the test that+/// pins that parse covers this label too.fn thread_label() -> char {- let thread_id = format!("{:?}", std::thread::current().id());- let parsed = thread_id- .strip_prefix("ThreadId(")- .and_then(|s| s.strip_suffix(")"))- .and_then(|s| s.parse::<usize>().ok());- label_for_thread_index(parsed)+ label_for_thread_index(thread_id())}-/// Pure helper: map a parsed `ThreadId` number to a single-char label.+/// Pure helper: map a thread id to a single-char label.///-/// `n == 0` → `'0'`; `1..=26` → `'a'..='z'`; `27..=52` → `'A'..='Z'`;-/// everything else (including a `None` from a `ThreadId` whose `Debug`-/// shape we don't recognize) → `'?'`. Tested via the branch coverage-/// below — `thread_label` itself never sees `n == 0` or `n > 52` in-/// practice, so its `unwrap_or` chain stays exercised only through-/// `label_for_thread_index`.-fn label_for_thread_index(n: Option<usize>) -> char {- let Some(n) = n else { return '?' };- if n == 0 {- '0'- } else if n <= 26 {- char::from(b'a' + (n - 1) as u8)- } else if n <= 52 {- char::from(b'A' + (n - 27) as u8)- } else {- '?'+/// `1..=26` → `'a'..='z'`; `27..=52` → `'A'..='Z'`; everything else → `'?'`,+/// including the `0` [`thread_id`] falls back to when `ThreadId`'s `Debug`+/// shape stops matching. Tested via the branch coverage below —+/// `thread_label` itself never sees `n == 0` or `n > 52` in practice.+fn label_for_thread_index(n: u64) -> char {+ match n {+ 1..=26 => char::from(b'a' + (n - 1) as u8),+ 27..=52 => char::from(b'A' + (n - 27) as u8),+ _ => '?',}}@@ -780,17 +771,17 @@ mod tests {/// hands it `n == 0` or `n > 52` in practice (Rust's `ThreadId`/// numbering starts at 1 and the main process won't spawn 53+ threads/// during the lifetime of the logger), but the branches are there for- /// the day either invariant changes.+ /// the day either invariant changes. `0` is `thread_id`'s parse+ /// fallback, so it labels like any other unrecognized id.#[test]fn label_covers_each_branch() {- assert_eq!(label_for_thread_index(None), '?');- assert_eq!(label_for_thread_index(Some(0)), '0');- assert_eq!(label_for_thread_index(Some(1)), 'a');- assert_eq!(label_for_thread_index(Some(26)), 'z');- assert_eq!(label_for_thread_index(Some(27)), 'A');- assert_eq!(label_for_thread_index(Some(52)), 'Z');- assert_eq!(label_for_thread_index(Some(53)), '?');- assert_eq!(label_for_thread_index(Some(9999)), '?');+ assert_eq!(label_for_thread_index(0), '?');+ assert_eq!(label_for_thread_index(1), 'a');+ assert_eq!(label_for_thread_index(26), 'z');+ assert_eq!(label_for_thread_index(27), 'A');+ assert_eq!(label_for_thread_index(52), 'Z');+ assert_eq!(label_for_thread_index(53), '?');+ assert_eq!(label_for_thread_index(9999), '?');}/// Each shape `StderrFormat` recognises — verified ANSI-stripped sodiff --git a/src/trace/emit.rs b/src/trace/emit.rsindex 7beaf9d13..0f7f2901a 100644--- a/src/trace/emit.rs+++ b/src/trace/emit.rs@@ -106,6 +106,15 @@ pub fn now_us() -> u64 {/// Numeric thread id, extracted from `ThreadId`'s `Debug` representation./// `ThreadId` debug format is `ThreadId(N)`.+///+/// Parsing `Debug` because `std` exposes no stable accessor for the number+/// (`ThreadId::as_u64` is unstable). The substitute is kept to exactly that+/// question, it is the only place that parse lives (the stderr thread label+/// reads it through here), and `thread_ids_are_distinct_and_nonzero` pins it:+/// a `Debug` format that stopped matching would fall through to the `0` below+/// on every thread, and `Profile`'s thread count — the `-vv` diagnostic+/// report's answer to "did this run in parallel?" — would silently read 1 for+/// every run.pub fn thread_id() -> u64 {let thread_id = std::thread::current().id();let debug_str = format!("{:?}", thread_id);@@ -440,6 +449,19 @@ impl Drop for Span {mod tests {use super::*;+ /// `thread_id` reads a number out of `ThreadId`'s `Debug` string, so a+ /// change to that format degrades silently to `0` everywhere rather than+ /// failing. Two threads reporting distinct non-zero ids is what proves the+ /// parse still lands.+ #[test]+ fn thread_ids_are_distinct_and_nonzero() {+ let current = thread_id();+ let spawned = std::thread::spawn(thread_id).join().unwrap();+ assert_ne!(current, 0, "this thread's id parsed as the 0 fallback");+ assert_ne!(spawned, 0, "spawned thread id parsed as the 0 fallback");+ assert_ne!(current, spawned, "two threads reported the same id");+ }+// Resolution (complete/fail) marks the guard so its drop is a no-op. No// tracing subscriber is installed, so the records themselves are dropped —// the point is that the drop-time tripwire does not fire on a resolved