agents: refine Rust conventions and CI flake/retry handling (#4501)

In Rust crates, repeated string literals and duplicated format parsers
can drift, bare `match` identifiers silently bind on typos, and
`#[cfg(unix)]` `vec![...]` bases fail Windows `clippy::useless_vec`. In
CI triage, log parsing leaves residual Buildkite `bk;t=` prefixes after
ANSI removal, misclassifies runner DNS, git-mirror, and Windows
`modextwd` file-lock flakes as code bugs, and `--rebuild` reruns
discard passing jobs.

Document module-qualified `match` constants, shared workspace parser
crates, `HashMap` over linear `Vec` scans, and Windows-clean
`#[cfg(unix)]` bases in the Rust rules and review prompt. Strip
residual `bk;t=` timestamp prefixes and classify runner DNS,
git-mirror, and `modextwd` cleanup flakes with Bazel-executed unit
tests in `analyze-ci-failure`, and prefer in-place job retries over
`--rebuild` in `retry-ci-job`.
diff --git a/.agents/rules/rust.md b/.agents/rules/rust.md
index adaa035..8e60c0f 100644
--- a/.agents/rules/rust.md
+++ b/.agents/rules/rust.md
@@ -53,12 +53,26 @@
   and `PathBuf`; never round-trip `OsString -> String -> PathBuf` via
   `to_string_lossy()`. Return `std::process::ExitCode` from `main()` and
   `run_cli`.
+* **Symbolic constants in `match` arms**: When string tokens (subcommand names,
+  CLI flags, or wire-format tags) appear across multiple sites (`match` arms,
+  registries, or error messages), group them as constants in a module (`mod
+  flags`, `mod subcommands`) and match via qualified paths (`flags::TEMPLATE`,
+  `subcommands::ZIPPER`). A misspelled bare identifier in a Rust `match` arm
+  parses as a catch-all variable binding, whereas a module-qualified path fails
+  compilation (`E0531`). Keep raw string literals in tests so tests catch typos
+  in constant values, and pass `case=no` to `keep-sorted` when `_` vs.
+  uppercase ASCII would invert runtime lowercase sort order (`"zip_main_maker"`
+  < `"zipper"`, whereas `ZIPPER` < `ZIP_MAIN_MAKER`).
 * **Collections**: Default to `Vec` and `HashMap`/`HashSet` over
-  `BTreeMap`/`BTreeSet` unless sorted-key traversal is a data-structure
+  `BTreeMap`/`BTreeSet` (and over `Vec<(K, V)>` linear scans when insertion
+  order is not required) unless sorted-key traversal is a data-structure
   invariant. Sort a `Vec` at formatting or output boundaries when deterministic
-  order is required.
+  order is required; omit defensive key sorting when template placeholders are
+  disjoint delimited tokens (e.g., `%KEY%`).
 * **Cohesive file organization**: Place core logic in library crates
-  (`src/lib.rs`) so unit tests call library functions directly. Group CLI
+  (`src/lib.rs`) so unit tests call library functions directly, and extract
+  shared wire/manifest parsers into a small workspace library crate rather than
+  duplicating line parsing across sibling `shrubbery` crates. Group CLI
   option structs, `parse_args`, and `run_cli` together in one contiguous
   section rather than splitting `parse_args` at the top and `run_cli` at the
   bottom around domain logic.
@@ -94,8 +108,11 @@
   `allow-*-in-tests` settings only recognize `#[test]` functions and
   `#[cfg(test)]` items, so helper functions in integration-test crates
   (`tests/*/*_test.rs`) would be flagged. Code under `#[cfg(unix)]` must
-  leave no `unused_mut`/`useless_vec` behind on Windows; rebind instead of
-  conditionally `push`ing.
+  leave no `unused_mut`/`useless_vec` behind on Windows: initialize base
+  collections as fixed-size arrays (`[...]`) or strings rather than `vec![...]`
+  before rebinding under `#[cfg(unix)]` (spot-check the non-Unix branch locally
+  on Linux by temporarily flipping `#[cfg(unix)]` to `#[cfg(windows)]` before
+  pushing).
 * **WORKSPACE-mode stub**: Any new `@rules_rust//...` flag or aspect
   referenced from `.bazelrc` must also exist in
   `tests/modules/rules_rust_stub/`, which WORKSPACE CI jobs use in place of
diff --git a/.agents/skills/analyze-ci-failure/scripts/BUILD.bazel b/.agents/skills/analyze-ci-failure/scripts/BUILD.bazel
new file mode 100644
index 0000000..d990069
--- /dev/null
+++ b/.agents/skills/analyze-ci-failure/scripts/BUILD.bazel
@@ -0,0 +1,18 @@
+load("//python:py_library.bzl", "py_library")
+load("//python:py_test.bzl", "py_test")
+
+package(default_visibility = ["//visibility:private"])
+
+py_library(
+    name = "analyze_ci_failure",
+    testonly = True,
+    srcs = ["analyze_ci_failure.py"],
+    imports = ["."],
+)
+
+py_test(
+    name = "analyze_ci_failure_test",
+    size = "small",
+    srcs = ["analyze_ci_failure_test.py"],
+    deps = [":analyze_ci_failure"],
+)
diff --git a/.agents/skills/analyze-ci-failure/scripts/analyze_ci_failure.py b/.agents/skills/analyze-ci-failure/scripts/analyze_ci_failure.py
index e11334a..094d4a5 100755
--- a/.agents/skills/analyze-ci-failure/scripts/analyze_ci_failure.py
+++ b/.agents/skills/analyze-ci-failure/scripts/analyze_ci_failure.py
@@ -8,7 +8,10 @@
 import urllib.parse
 import urllib.request
 
-_ANSI_ESCAPE = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
+_ANSI_ESCAPE = re.compile(
+    r"\x1B(?:[_\]][^\x07\x1B]*(?:\x07|\x1B\\)|[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])"
+)
+_BUILDKITE_TIMESTAMP = re.compile(r"^_?bk;t=\d+\s*")
 _HTTP_5XX_DOWNLOAD_ERROR = re.compile(r"get returned 5\d\d\b", re.IGNORECASE)
 
 _HTTP_GATEWAY_ERRORS = (
@@ -19,38 +22,57 @@
     # keep-sorted end
 )
 
-_ERROR_KEYWORDS = _HTTP_GATEWAY_ERRORS + (
+_DNS_OR_GIT_MIRROR_ERRORS = (
     # keep-sorted start
-    "##[error]",
-    "connection reset by peer",
-    "critical path",
-    "curl: (",
-    "errno 5",
-    "error downloading [",
-    "error during connect:",
-    "error waiting for container",
-    "error:",
-    "error]",
-    "exception",
-    "exit code",
-    "exit-code",
-    "failed:",
-    "fatal",
-    "fatal:",
-    "filenotfounderror",
-    "get returned 5",
-    "input/output error",
-    "no such package",
-    "no such target",
-    "status 125",
-    "traceback",
-    "unable to find bazelisk version",
-    "user command error:",
-    "would be reformatted",
-    "would reformat:",
+    "could not resolve host:",
+    "getting/updating git mirror:",
+    "temporary failure in name resolution",
     # keep-sorted end
 )
 
+_MODEXTWD_CLEANUP_ERRORS = (
+    # keep-sorted start
+    "failed to clean up module context directory:",
+    # keep-sorted end
+)
+
+_ERROR_KEYWORDS = (
+    _HTTP_GATEWAY_ERRORS
+    + _DNS_OR_GIT_MIRROR_ERRORS
+    + _MODEXTWD_CLEANUP_ERRORS
+    + (
+        # keep-sorted start
+        "##[error]",
+        "connection reset by peer",
+        "critical path",
+        "curl: (",
+        "errno 5",
+        "error downloading [",
+        "error during connect:",
+        "error waiting for container",
+        "error:",
+        "error]",
+        "exception",
+        "exit code",
+        "exit-code",
+        "failed:",
+        "fatal",
+        "fatal:",
+        "filenotfounderror",
+        "get returned 5",
+        "input/output error",
+        "no such package",
+        "no such target",
+        "status 125",
+        "traceback",
+        "unable to find bazelisk version",
+        "user command error:",
+        "would be reformatted",
+        "would reformat:",
+        # keep-sorted end
+    )
+)
+
 
 def fetch_log(job_name, build_id, job_id, output_path):
     if (
@@ -200,8 +222,8 @@
     errors = []
     for line in lines:
         clean_line = _ANSI_ESCAPE.sub("", line).strip()
-        # Clean buildkite timestamp prefix: _bk;t=...
-        clean_line = re.sub(r"^_bk;t=\d+\s*", "", clean_line)
+        # Strip pre-stripped `_bk;t=` / `bk;t=` prefixes in addition to APC sequences.
+        clean_line = _BUILDKITE_TIMESTAMP.sub("", clean_line)
         lower_line = clean_line.lower()
         if clean_line and any(kw in lower_line for kw in _ERROR_KEYWORDS):
             errors.append(clean_line)
@@ -272,6 +294,19 @@
             "GitHub Actions `setup-bazel` step (`Unable to find Bazelisk version`). "
             "This is an infrastructure network flake, not a codebase bug."
         )
+    elif _contains_any(errors, _DNS_OR_GIT_MIRROR_ERRORS):
+        is_flake = True
+        flake_reason = (
+            "Transient CI runner DNS resolution or git mirror checkout failure. "
+            "This is an infrastructure network flake, not a codebase bug."
+        )
+    elif _contains_any(errors, _MODEXTWD_CLEANUP_ERRORS):
+        is_flake = True
+        flake_reason = (
+            "Transient Windows file-lock race while Bazel cleaned up a temporary "
+            "`modextwd` module extension directory (`Permission denied`). "
+            "This is an infrastructure flake, not a codebase bug."
+        )
 
     classification = (
         "⚡ **Classification**: **Infrastructure / Flake Issue** (Not a codebase bug)"
diff --git a/.agents/skills/analyze-ci-failure/scripts/analyze_ci_failure_test.py b/.agents/skills/analyze-ci-failure/scripts/analyze_ci_failure_test.py
index 05a6089..bb41dc3 100755
--- a/.agents/skills/analyze-ci-failure/scripts/analyze_ci_failure_test.py
+++ b/.agents/skills/analyze-ci-failure/scripts/analyze_ci_failure_test.py
@@ -74,6 +74,44 @@
             errors, ["ERROR: Analysis of target '//foo:bar' failed: build failed"]
         )
 
+    def test_buildkite_apc_timestamp_prefix_stripped_from_errors(self) -> None:
+        errors, _ = self._classify_log(
+            "\x1b_bk;t=1794137028816\x07ERROR: Build did NOT complete successfully\n"
+            "_bk;t=1794137028817 FAILED: Build did NOT complete successfully\n"
+        )
+        self.assertEqual(
+            errors,
+            [
+                "ERROR: Build did NOT complete successfully",
+                "FAILED: Build did NOT complete successfully",
+            ],
+        )
+
+    def test_runner_dns_and_git_mirror_checkout_classified_as_flake(self) -> None:
+        for log_text in (
+            "fatal: unable to access 'https://github.com/bazel-contrib/rules_python.git/': Could not resolve host: github.com\n",
+            "Error: getting/updating git mirror: exit status 128\n",
+            "HTTPSConnectionPool(host='docs.python.org', port=443): Temporary failure in name resolution\n",
+        ):
+            with self.subTest(log_text=log_text):
+                _, plan = self._classify_log(log_text)
+                self.assertIn(_FLAKE_BANNER, plan)
+
+    def test_windows_modextwd_cleanup_permission_denied_classified_as_flake(
+        self,
+    ) -> None:
+        errors, plan = self._classify_log(
+            "WARNING: errors encountered while analyzing target '//tests/py_zipapp:system_python_bin'\n"
+            "Failed to clean up module context directory: C:/b/nzh6davs/modextwd/rules_rust++crate/cargo-bazel.exe (Permission denied)\n"
+        )
+        self.assertEqual(
+            errors,
+            [
+                "Failed to clean up module context directory: C:/b/nzh6davs/modextwd/rules_rust++crate/cargo-bazel.exe (Permission denied)"
+            ],
+        )
+        self.assertIn(_FLAKE_BANNER, plan)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/.agents/skills/retry-ci-job/SKILL.md b/.agents/skills/retry-ci-job/SKILL.md
index 6737ebd..9909b2e 100644
--- a/.agents/skills/retry-ci-job/SKILL.md
+++ b/.agents/skills/retry-ci-job/SKILL.md
@@ -55,7 +55,9 @@
 
 4. **Rebuild Full Build**:
    Re-runs the entire pipeline/workflow build from scratch via `bk build
-   rebuild` or `gh run rerun`:
+   rebuild` or `gh run rerun`. Prefer default in-place job retries (mode 1 or
+   3, without `--rebuild`) when most Buildkite jobs have already passed, so
+   passing jobs are preserved; reserve `--rebuild` for mass early failures:
    ```bash
    ./.agents/skills/retry-ci-job/scripts/retry_ci_jobs.py \
      --pr <pr_number> --rebuild
diff --git a/.agents/skills/review-code/review-rust-prompt.md b/.agents/skills/review-code/review-rust-prompt.md
index 07da9e3..6842f34 100644
--- a/.agents/skills/review-code/review-rust-prompt.md
+++ b/.agents/skills/review-code/review-rust-prompt.md
@@ -39,8 +39,12 @@
    - Verify `Vec` and `HashMap`/`HashSet` are preferred over `BTreeMap`/
      `BTreeSet` unless sorted traversal is an invariant, CLI types/`parse_args`/
      `run_cli` are grouped cohesively (no top/bottom CLI sandwich), tests use
-     real temporary directories instead of mocked filesystem IO, and `rustfmt`
-     + `bazel test --config=fast-tests` pass.
+     real temporary directories instead of mocked filesystem IO, repeated
+     string tokens in `match` arms / registries / diagnostics use
+     module-qualified constants (`mod::CONST`), sibling crates share format
+     parsers instead of duplicating them, `#[cfg(unix)]` test fixtures avoid
+     leaving `vec![...]` (`clippy::useless_vec`) on Windows, and `rustfmt` +
+     `bazel test --config=fast-tests` pass.
 5. Guardrails: Never nitpick generated `Cargo.lock` or `Cargo.Bazel.lock`
    formatting; only flag stale lockfiles or unvetted/outdated crate versions.