From d73e928039b324af8aa96cfdd7011eb3fd721200 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 13:23:39 -0600 Subject: [PATCH] fleet placement: distinguish unreadable runner load from measured idle capacity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The placement collector collapsed every load-read failure — missing token, non-200, transport error, and even an HTTP 200 with a missing or invalid total_count — into the same numeric 0 as a genuinely idle account, and tier-1 selection sorted those figures as measured load. - build_ctx now records per-owner LoadCoverage (measured / partial / unknown / simulated) with the known running+queued subtotal, the configured cap, read time, source, and the missing reads; a malformed 200 is unobserved instead of silently zero. - The decision JSON and both session records carry owner_load_coverage; capacity summaries give a numeric free-slot estimate only for measured/simulated coverage, except that a known subtotal at or above the cap still proves saturation (estimate 0). - Tier 1 prefers measured headroom; when none exists, an eligible surface with unknown load may be picked under an explicit unknown-capacity rule with deterministic ordering, named in the reasoning. Unread load never authorizes paid overage. - Fake-adapter fixture tests cover the six named cases (fully observed zero at cap 40, missing token, 403, malformed 200, one failed status with 3 on the other, explicit override) plus the partial-saturation proof. - Book updated in the same change (DOC-01): concepts/placement, library/placement, tutorials/dispatch-a-build. Implements the design Lane AU31 verified and recorded on the issue. --- pkgs/fleet/book/concepts/placement.md | 25 +- pkgs/fleet/book/library/placement.md | 15 +- pkgs/fleet/book/tutorials/dispatch-a-build.md | 8 +- .../fleet/crates/fleet-queue/src/placement.rs | 495 +++++++++++++++++- .../fleet-queue/src/placement/adapters.rs | 56 +- 5 files changed, 568 insertions(+), 31 deletions(-) diff --git a/pkgs/fleet/book/concepts/placement.md b/pkgs/fleet/book/concepts/placement.md index a94a02c02f..e9a13ecbc5 100644 --- a/pkgs/fleet/book/concepts/placement.md +++ b/pkgs/fleet/book/concepts/placement.md @@ -101,7 +101,13 @@ macOS-specific load measurement anywhere. surface_id)`: least-loaded owner first; among equals, the least-recently-used owner (an owner never used sorts before all used owners); then registry priority; then per-surface last dispatch; then the - surface id alphabetically. The rotation is owner-first. + surface id alphabetically. The rotation is owner-first. Surfaces whose + owner load is not fully observed carry no numeric estimate; if no + eligible surface has measured headroom, one unknown-capacity surface may + still be picked — deterministically, by the same rotation order minus + load — under the unknown-capacity rule, which the pick's reasoning line + names. A surface whose known subtotal already reaches its cap is proven + saturated and is never picked this way. 3. **Tier 2 — Blacksmith.** `blacksmith` surfaces in registry order; the first eligible one with `remaining_total_minutes > 0` wins. 4. **Tier 3 — Codespaces.** `codespace` surfaces in registry order; the @@ -125,9 +131,20 @@ that surface's token, placement reads only `total_count` from `GET /repos/{host}/actions/runs?status=in_progress&per_page=1` and the same with `status=queued` — the run lists themselves are never enumerated. Tier-1 load per owner is `running + queued`, summed across its host repos. Failures -are soft: no token, a non-200, or a transport error contributes 0 for that -repo/status and adds a `load_read_errors` entry, which is surfaced in the -dispatch output when non-empty. Owner and surface last-dispatch times come +are soft but never silent: no token, a non-200, a transport error, or a +200 whose body lacks a valid `total_count` makes that repo/status read +*missing* rather than zero, adds a `load_read_errors` entry (surfaced in +the dispatch output when non-empty), and is recorded in the owner's load +coverage. Coverage is one of `measured` (every required read returned a +count), `partial` (some did), `unknown` (none did), or `simulated` (an +`--assume-owner-load` override supplied the figure). The decision output +carries `owner_load_coverage` per owner — status, known running/queued +subtotal, configured cap, read time, source, and the missing reads — and +session records store the same fragment. The known subtotal is exact for +measured/simulated coverage and a lower bound for partial coverage; only +measured or simulated coverage (or proven saturation) yields a numeric +free-slot estimate — any other owner reports unknown capacity instead of +a dressed-up zero. Owner and surface last-dispatch times come from the local session store, not the API. Two testing overrides exist (`--assume-owner-load owner=N,…` and `--assume-blacksmith-used MINUTES`); they are simulation, are labelled as such in the output, and do not appear diff --git a/pkgs/fleet/book/library/placement.md b/pkgs/fleet/book/library/placement.md index df4f3c98d9..b83c0a235a 100644 --- a/pkgs/fleet/book/library/placement.md +++ b/pkgs/fleet/book/library/placement.md @@ -199,7 +199,11 @@ allow_paid_blacksmith}` — exactly the fields a session record stores `dispatch(job, gh) -> DispatchResult`, and `status(session, gh)` (defaulting to the stored state). `free_slots` and `remaining_total_minutes` are both optional in a capacity report: a - summary-only adapter (codespace) reports neither. + summary-only adapter (codespace) reports neither. For the GitHub + Actions adapter, `free_slots` is present only when the owner's load + coverage is measured or simulated — or when a partial known subtotal + already proves saturation, yielding a proven `0`; an unreadable owner + reports no estimate rather than a false zero. - **`DispatchResult`**: `{run_id, run_url, note, codespace, terminal_state}` — what a dispatch attempt hands back to be recorded as a session. @@ -211,7 +215,10 @@ terminal_state}` — what a dispatch attempt hands back to be recorded the local store — per-owner load `(running, queued)` counted only over that owner's fleet runner-host repos (two `total_count` reads per host repo, `in_progress` and `queued`; failures are soft and recorded in - `load_read_errors`), per-owner and per-surface last-dispatch epochs + `load_read_errors`), per-owner load coverage (status + measured/partial/unknown/simulated, known subtotal, cap, read time, + source, and missing reads — also emitted as the decision output's + `owner_load_coverage`), per-owner and per-surface last-dispatch epochs from the session store, the month's Blacksmith minutes used, and the laptop-busy flag (`--laptop-busy` or the registry's `laptop.busy`). @@ -219,7 +226,9 @@ The cascade itself — hard pin, follow-up affinity (kept only while the preferred surface is eligible, has capacity, and its owner's queued count is strictly below its `sticky_queue_threshold`), Tier 1 free Actions sorted by `(load, owner_last_dispatch, selection_priority, -surface_last_dispatch, surface_id)`, Tier 2 Blacksmith in registry +surface_last_dispatch, surface_id)` — with a deterministic +unknown-capacity fallback, named in the reasoning, when no eligible +surface has measured headroom — Tier 2 Blacksmith in registry order, Tier 3 Codespaces, then exhaustion — is walked step by step in [Tutorial: dispatch a build remotely](../tutorials/dispatch-a-build.md). diff --git a/pkgs/fleet/book/tutorials/dispatch-a-build.md b/pkgs/fleet/book/tutorials/dispatch-a-build.md index 9f5bebf2d4..fa00dd6e17 100644 --- a/pkgs/fleet/book/tutorials/dispatch-a-build.md +++ b/pkgs/fleet/book/tutorials/dispatch-a-build.md @@ -69,9 +69,11 @@ repo-queue remains its system of record)". For machine-readable output, add `--json`. A dry run's object carries `job`, `decision`, `reasoning` and `"mode": "dry-run (no session written; pass --dispatch to submit)"`, plus a `note` when a testing override was -used and `load_read_errors` when an owner's load could not be read (load -failures are soft: that owner's load counts as 0 and the error is -reported, not hidden). +used, `load_read_errors` when any load read failed, and +`owner_load_coverage` — per owner: whether its load was measured, +partial, unknown, or simulated, the known subtotal, the cap, and which +reads are missing. Load failures are soft, but an owner whose load could +not be read is reported as unknown capacity, never as an idle zero. What can go wrong, and the fix: diff --git a/pkgs/fleet/crates/fleet-queue/src/placement.rs b/pkgs/fleet/crates/fleet-queue/src/placement.rs index 49a2f9aac9..48f027701e 100644 --- a/pkgs/fleet/crates/fleet-queue/src/placement.rs +++ b/pkgs/fleet/crates/fleet-queue/src/placement.rs @@ -192,6 +192,7 @@ impl Job { pub struct Ctx { pub owners: Value, pub owner_load: HashMap, // owner -> (running, queued) + pub owner_load_coverage: HashMap, pub owner_last_dispatch: HashMap, pub surface_last_dispatch: HashMap, pub owner_load_overridden: Vec, @@ -211,6 +212,99 @@ impl Ctx { } } +/// How completely an owner's live load was observed while building +/// the context. The collector counts running+queued runs per owner +/// through the GitHub API; every required read either yields a count +/// or is recorded as missing, so a zero is only ever reported as a +/// *measured* zero when every read succeeded. Before this existed, a +/// missing token, a 403, or a malformed 200 all collapsed to the +/// same numeric 0 as a genuinely idle account (issue #13611). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LoadCoverageStatus { + /// Every required host/status read returned a valid count. + Measured, + /// Some reads returned counts; the rest are missing. The known + /// subtotal is a lower bound on the owner's true load. + Partial, + /// No read returned a count; the owner's load is unknown. + Unknown, + /// The load was supplied by the explicit `--assume-owner-load` + /// simulation override, not read from GitHub. + Simulated, +} + +impl LoadCoverageStatus { + pub fn as_str(&self) -> &'static str { + match self { + LoadCoverageStatus::Measured => "measured", + LoadCoverageStatus::Partial => "partial", + LoadCoverageStatus::Unknown => "unknown", + LoadCoverageStatus::Simulated => "simulated", + } + } +} + +/// Per-owner load-read coverage: what was observed, what is known, +/// and what could not be read. Carried into the decision JSON so an +/// operator can tell a measured idle account from an unreadable one. +#[derive(Debug, Clone)] +pub struct LoadCoverage { + pub status: LoadCoverageStatus, + pub known_running: i64, + pub known_queued: i64, + pub cap: i64, + pub read_at: i64, + pub source: String, + pub missing_reads: Vec, +} + +impl LoadCoverage { + /// The observed running+queued subtotal: exact for measured and + /// simulated coverage, a lower bound for partial coverage. + pub fn known_load(&self) -> i64 { + self.known_running.saturating_add(self.known_queued) + } + + /// A numeric free-slot estimate is only meaningful when the + /// load figure is complete (measured) or an explicitly labeled + /// simulation. Partial coverage still proves saturation when + /// the known subtotal already reaches the cap. + pub fn estimated_free(&self, cap: i64) -> Option { + match self.status { + LoadCoverageStatus::Measured | LoadCoverageStatus::Simulated => { + Some((cap - self.known_load()).max(0)) + } + LoadCoverageStatus::Partial if self.known_load() >= cap => Some(0), + _ => None, + } + } + + pub fn to_json(&self) -> Value { + json!({ + "status": self.status.as_str(), + "known_running": self.known_running, + "known_queued": self.known_queued, + "known_load": self.known_load(), + "cap": self.cap, + "read_at": self.read_at, + "source": self.source, + "missing_reads": self.missing_reads, + }) + } +} + +/// The `owner_load_coverage` fragment of the decision JSON, keyed +/// by owner in sorted order so the output is deterministic. +pub fn owner_load_coverage_json(ctx: &Ctx) -> Value { + let mut owners: Vec<&String> = ctx.owner_load_coverage.keys().collect(); + owners.sort(); + let mut out = Map::new(); + for owner in owners { + out.insert(owner.clone(), ctx.owner_load_coverage[owner].to_json()); + } + Value::Object(out) +} + // ------------------------------------------------------- side-effect surface /// The boundary between the pure placement logic and the world: @@ -909,10 +1003,15 @@ pub fn build_ctx( } } let mut owner_load: HashMap = HashMap::new(); + let mut owner_load_coverage: HashMap = HashMap::new(); let mut read_errors: Vec = Vec::new(); + let read_at = gh.now_epoch(); for (owner, repos) in &hosts { let mut running: i64 = 0; let mut queued: i64 = 0; + let mut observed: usize = 0; + let mut expected: usize = 0; + let mut missing_reads: Vec = Vec::new(); for repo in repos { let minted = if user_repos.contains(repo) { gh.user_token() @@ -922,13 +1021,17 @@ pub fn build_ctx( let token = match minted { Some(t) => t, None => { - read_errors.push(format!( - "{repo}: no token available (not covered) — load counted as 0" - )); + expected += 2; + let msg = format!( + "{repo}: no token available (not covered) — load unobserved for this repo" + ); + read_errors.push(msg.clone()); + missing_reads.push(msg); continue; } }; for status_name in ["in_progress", "queued"] { + expected += 1; match gh.api( "GET", &format!("/repos/{repo}/actions/runs?status={status_name}&per_page=1"), @@ -936,28 +1039,72 @@ pub fn build_ctx( None, ) { Ok((200, payload)) => { - let total = payload - .get("total_count") - .and_then(|t| t.as_i64()) - .unwrap_or(0); - if status_name == "in_progress" { - running += total; - } else { - queued += total; + match payload.get("total_count").and_then(|t| t.as_i64()) { + Some(total) => { + observed += 1; + if status_name == "in_progress" { + running += total; + } else { + queued += total; + } + } + None => { + let msg = format!( + "{repo} ({status_name}): HTTP 200 without a valid \ + total_count — load unobserved" + ); + read_errors.push(msg.clone()); + missing_reads.push(msg); + } } } Ok((code, _)) => { - read_errors.push(format!("{repo} ({status_name}): HTTP {code}")) + let msg = format!("{repo} ({status_name}): HTTP {code}"); + read_errors.push(msg.clone()); + missing_reads.push(msg); + } + Err(e) => { + let msg = format!("{repo} ({status_name}): {}", e.0); + read_errors.push(msg.clone()); + missing_reads.push(msg); } - Err(e) => read_errors.push(format!("{repo} ({status_name}): {}", e.0)), } } } + let status = if overrides.contains_key(owner) { + LoadCoverageStatus::Simulated + } else if observed == expected { + LoadCoverageStatus::Measured + } else if observed > 0 { + LoadCoverageStatus::Partial + } else { + LoadCoverageStatus::Unknown + }; if let Some(n) = overrides.get(owner) { running = *n; queued = 0; } owner_load.insert(owner.clone(), (running, queued)); + owner_load_coverage.insert( + owner.clone(), + LoadCoverage { + status, + known_running: running, + known_queued: queued, + cap: owners + .get(owner) + .and_then(|o| o.get("concurrent_jobs_cap")) + .and_then(|c| c.as_i64()) + .unwrap_or(0), + read_at, + source: if status == LoadCoverageStatus::Simulated { + "--assume-owner-load override (simulation, not live counts)".to_string() + } else { + "live GitHub Actions run counts over fleet runner hosts".to_string() + }, + missing_reads, + }, + ); } let mut owner_last_dispatch: HashMap = HashMap::new(); let mut surface_last_dispatch: HashMap = HashMap::new(); @@ -1020,6 +1167,7 @@ pub fn build_ctx( Ctx { owners, owner_load, + owner_load_coverage, owner_last_dispatch, surface_last_dispatch, owner_load_overridden: overridden, @@ -1112,6 +1260,10 @@ pub fn decide( // distribute idle bursts evenly across accounts. Within an owner, // prefer its Fleet runner repo so followups can reuse that cache. let mut tier1: Vec<(i64, i64, i64, i64, String)> = Vec::new(); + // Eligible surfaces whose owner load could not be fully read + // (and is not proven saturated): they carry no numeric estimate, + // but stay placeable under the unknown-capacity rule below. + let mut tier1_unknown: Vec<(i64, i64, i64, String)> = Vec::new(); for s in &surfaces { if surf_str(s, "kind") != "github-actions" { continue; @@ -1140,6 +1292,15 @@ pub fn decide( } else { reasoning.push(format!("tier1 unavailable {id}: no free account slots")); } + } else { + let owner = surf_str(s, "owner"); + let owner_last = ctx.owner_last_dispatch.get(owner).copied().unwrap_or(0); + let surface_last = ctx.surface_last_dispatch.get(&id).copied().unwrap_or(0); + let priority = s + .get("selection_priority") + .and_then(Value::as_i64) + .unwrap_or(50); + tier1_unknown.push((owner_last, priority, surface_last, id)); } } if !tier1.is_empty() { @@ -1157,6 +1318,31 @@ pub fn decide( let surface = find_surface(registry, &sid).unwrap().clone(); return Ok((adapter_for(&surface), reasoning)); } + if !tier1_unknown.is_empty() { + // Unknown-capacity rule: with no measured headroom anywhere, + // an eligible free surface whose load is merely unreadable + // may still be picked — deterministically, and with the + // uncertainty named in the receipt. Unread load never + // authorizes paid overage: this tier is free GitHub-hosted + // capacity only, and tier 2 (Blacksmith) is not reached on + // the strength of an unknown. + tier1_unknown.sort_by(|a, b| { + a.0.cmp(&b.0) + .then_with(|| a.1.cmp(&b.1)) + .then_with(|| a.2.cmp(&b.2)) + .then_with(|| a.3.cmp(&b.3)) + }); + let sid = tier1_unknown[0].3.clone(); + reasoning.push(format!( + "tier1 pick {sid}: unknown-capacity rule — no eligible surface has \ + measured headroom, and this surface's owner load could not be \ + fully read, so its occupancy is unknown rather than zero; picked \ + deterministically among unknown-capacity surfaces (owner \ + rotation, selection priority, surface last dispatch, surface id)" + )); + let surface = find_surface(registry, &sid).unwrap().clone(); + return Ok((adapter_for(&surface), reasoning)); + } reasoning .push("tier1 exhausted: no eligible GitHub-hosted surface has a free slot".to_string()); @@ -1736,6 +1922,9 @@ fn run_dispatch(args: &[String]) -> R { if !ctx.load_read_errors.is_empty() { out.insert("load_read_errors".into(), json!(ctx.load_read_errors)); } + if !ctx.owner_load_coverage.is_empty() { + out.insert("owner_load_coverage".into(), owner_load_coverage_json(&ctx)); + } if !dispatch { out.insert( "mode".into(), @@ -1752,6 +1941,7 @@ fn run_dispatch(args: &[String]) -> R { "job": job.to_json(), "surface": Value::Null, "decision_reasoning": reasoning, + "owner_load_coverage": owner_load_coverage_json(&ctx), "run_id": Value::Null, "run_url": Value::Null, "state": "queued", @@ -1785,6 +1975,7 @@ fn run_dispatch(args: &[String]) -> R { ); } rec.insert("decision_reasoning".into(), json!(reasoning)); + rec.insert("owner_load_coverage".into(), owner_load_coverage_json(&ctx)); rec.insert( "run_id".into(), result.run_id.map(Value::from).unwrap_or(Value::Null), @@ -2080,6 +2271,7 @@ mod tests { post_runs: Vec, runs_by_id: Map2, load_by_repo: Map2, // repo -> (in_progress, queued) + load_replies: Map2<(String, String), (u16, Value)>, // (repo, status) -> canned load reply; repo "*" matches any codespace_ok: bool, codespace_items: Vec, codespace_exit: i32, @@ -2096,6 +2288,7 @@ mod tests { post_runs: Vec::new(), runs_by_id: Map2::new(), load_by_repo: Map2::new(), + load_replies: Map2::new(), codespace_ok: false, codespace_items: Vec::new(), codespace_exit: 0, @@ -2173,6 +2366,21 @@ mod tests { .next() .unwrap_or("") .to_string(); + let status_name = if path.contains("status=in_progress") { + "in_progress" + } else { + "queued" + }; + if let Some((code, payload)) = self + .load_replies + .get(&(repo.clone(), status_name.to_string())) + .or_else(|| { + self.load_replies + .get(&("*".to_string(), status_name.to_string())) + }) + { + return Ok((*code, payload.clone())); + } let (running, queued) = self.load_by_repo.get(&repo).copied().unwrap_or((0, 0)); let total = if path.contains("status=in_progress") { running @@ -2261,9 +2469,38 @@ mod tests { } fn ctx(load: &[(&str, (i64, i64))], blacksmith_used: f64, laptop_busy: bool) -> Ctx { + let owners = registry()["owners"].clone(); + let owner_load: HashMap = + load.iter().map(|(k, v)| (k.to_string(), *v)).collect(); + // Fixtures state load as fact, so coverage is measured for + // every registry owner (defaulting to an observed zero). + let owner_load_coverage = owners + .as_object() + .unwrap() + .iter() + .map(|(owner, o)| { + let (running, queued) = owner_load.get(owner).copied().unwrap_or((0, 0)); + ( + owner.clone(), + LoadCoverage { + status: LoadCoverageStatus::Measured, + known_running: running, + known_queued: queued, + cap: o + .get("concurrent_jobs_cap") + .and_then(Value::as_i64) + .unwrap_or(0), + read_at: FIXED_NOW, + source: "test fixture".to_string(), + missing_reads: vec![], + }, + ) + }) + .collect(); Ctx { - owners: registry()["owners"].clone(), - owner_load: load.iter().map(|(k, v)| (k.to_string(), *v)).collect(), + owners, + owner_load, + owner_load_coverage, owner_last_dispatch: HashMap::new(), surface_last_dispatch: HashMap::new(), owner_load_overridden: vec![], @@ -3290,4 +3527,232 @@ mod tests { &args(&["bogus", "--help"]) )); } + + /// A load-reply table answering every fixture-registry host + /// repo with one canned reply per status. + fn all_load_replies(code: u16, payload: Value) -> Map2<(String, String), (u16, Value)> { + let mut replies = Map2::new(); + for repo in [ + "dezren39/fleet", + "developing-today/code", + "developing-today/fleet", + ] { + for status in ["in_progress", "queued"] { + replies.insert( + (repo.to_string(), status.to_string()), + (code, payload.clone()), + ); + } + } + replies + } + + #[test] + fn coverage_measured_zero_yields_numeric_headroom() { + // Fixture oracle (issue #13611): a fully observed zero with + // cap 40 is a measured zero — numeric headroom, normal pick, + // no unknown-capacity reasoning. + let reg = registry(); + let gh = FakeGh::default(); + let c = build_ctx(®, &json!({}), false, "", None, &gh); + let cov = &c.owner_load_coverage["dezren39"]; + assert_eq!(cov.status, LoadCoverageStatus::Measured); + assert_eq!(cov.known_load(), 0); + assert_eq!(cov.cap, 40); + assert_eq!(cov.read_at, FIXED_NOW); + assert!(cov.missing_reads.is_empty()); + let j = owner_load_coverage_json(&c); + assert_eq!(j["dezren39"]["status"], json!("measured")); + assert_eq!(j["dezren39"]["known_load"], json!(0)); + assert_eq!(j["dezren39"]["cap"], json!(40)); + let ad = adapter_for(find_surface(®, "fleet-personal").unwrap()).unwrap(); + assert_eq!(ad.capacity(&job(), &c).free_slots, Some(40)); + let (picked, reasoning) = decide(®, &job(), &c, &gh).unwrap(); + assert_eq!(surf_str(picked.unwrap().surface(), "id"), "fleet-personal"); + assert!( + !reasoning.iter().any(|r| r.contains("unknown-capacity")), + "{reasoning:?}" + ); + } + + #[test] + fn coverage_missing_token_is_unknown_not_zero() { + // Fixture oracle: no covering token -> the owner's load is + // unknown; no numeric headroom may be reported for it. + let reg = registry(); + let gh = FakeGh { + tokens_ok: false, + ..Default::default() + }; + let c = build_ctx(®, &json!({}), false, "", None, &gh); + let cov = &c.owner_load_coverage["dezren39"]; + assert_eq!(cov.status, LoadCoverageStatus::Unknown); + assert_eq!(cov.known_load(), 0); + assert!(!cov.missing_reads.is_empty()); + assert!( + cov.missing_reads + .iter() + .all(|m| m.contains("no token available")), + "{:?}", + cov.missing_reads + ); + let j = owner_load_coverage_json(&c); + assert_eq!(j["dezren39"]["status"], json!("unknown")); + let ad = adapter_for(find_surface(®, "fleet-personal").unwrap()).unwrap(); + let cap = ad.capacity(&job(), &c); + assert_eq!(cap.free_slots, None); + assert!(cap.summary.contains("load unknown"), "{}", cap.summary); + } + + #[test] + fn coverage_http_403_is_unknown_and_falls_back_by_rule() { + // Fixture oracle: every load read refused (403) -> unknown + // coverage; with no measured headroom anywhere, tier 1 may + // still pick a free surface, but only under the named + // unknown-capacity rule, deterministically. + let reg = registry(); + let gh = FakeGh { + load_replies: all_load_replies(403, json!({ "message": "Forbidden" })), + ..Default::default() + }; + let c = build_ctx(®, &json!({}), false, "", None, &gh); + let cov = &c.owner_load_coverage["dezren39"]; + assert_eq!(cov.status, LoadCoverageStatus::Unknown); + assert!( + cov.missing_reads.iter().all(|m| m.contains("HTTP 403")), + "{:?}", + cov.missing_reads + ); + let ad = adapter_for(find_surface(®, "fleet-personal").unwrap()).unwrap(); + assert_eq!(ad.capacity(&job(), &c).free_slots, None); + let (picked, reasoning) = decide(®, &job(), &c, &gh).unwrap(); + assert_eq!(surf_str(picked.unwrap().surface(), "id"), "fleet-personal"); + assert!( + reasoning + .iter() + .any(|r| r.contains("unknown-capacity rule")), + "{reasoning:?}" + ); + } + + #[test] + fn coverage_malformed_200_is_unobserved_not_zero() { + // Fixture oracle: an HTTP 200 without a valid total_count + // observes nothing — it must not collapse to a measured 0. + let reg = registry(); + let gh = FakeGh { + load_replies: all_load_replies(200, json!({ "workflow_runs": [] })), + ..Default::default() + }; + let c = build_ctx(®, &json!({}), false, "", None, &gh); + let cov = &c.owner_load_coverage["dezren39"]; + assert_eq!(cov.status, LoadCoverageStatus::Unknown); + assert!( + cov.missing_reads + .iter() + .all(|m| m.contains("without a valid total_count")), + "{:?}", + cov.missing_reads + ); + let j = owner_load_coverage_json(&c); + assert_eq!(j["dezren39"]["status"], json!("unknown")); + let ad = adapter_for(find_surface(®, "fleet-personal").unwrap()).unwrap(); + assert_eq!(ad.capacity(&job(), &c).free_slots, None); + } + + #[test] + fn coverage_partial_keeps_known_subtotal_and_yields_no_estimate() { + // Fixture oracle: in_progress reads 3 while the queued read + // fails -> partial coverage; the known subtotal 3 is kept as + // a lower bound, no numeric headroom is reported, and a + // measured surface elsewhere still wins the pick. + let reg = registry(); + let mut replies = Map2::new(); + replies.insert( + ("dezren39/fleet".to_string(), "in_progress".to_string()), + (200, json!({ "total_count": 3 })), + ); + replies.insert( + ("dezren39/fleet".to_string(), "queued".to_string()), + (500, json!({ "message": "boom" })), + ); + let gh = FakeGh { + load_replies: replies, + ..Default::default() + }; + let c = build_ctx(®, &json!({}), false, "", None, &gh); + let cov = &c.owner_load_coverage["dezren39"]; + assert_eq!(cov.status, LoadCoverageStatus::Partial); + assert_eq!(cov.known_running, 3); + assert_eq!(cov.known_queued, 0); + assert_eq!(cov.known_load(), 3); + assert_eq!(cov.missing_reads.len(), 1); + let j = owner_load_coverage_json(&c); + assert_eq!(j["dezren39"]["status"], json!("partial")); + assert_eq!(j["dezren39"]["known_load"], json!(3)); + let ad = adapter_for(find_surface(®, "fleet-personal").unwrap()).unwrap(); + let cap = ad.capacity(&job(), &c); + assert_eq!(cap.free_slots, None); + assert!(cap.summary.contains("at least 3"), "{}", cap.summary); + let (picked, reasoning) = decide(®, &job(), &c, &gh).unwrap(); + assert_eq!(surf_str(picked.unwrap().surface(), "id"), "org-run"); + assert!( + !reasoning + .iter() + .any(|r| r.contains("unknown-capacity rule")), + "{reasoning:?}" + ); + } + + #[test] + fn coverage_partial_subtotal_at_cap_proves_saturation() { + // A known subtotal that already reaches the cap proves + // saturation even when the remaining reads are missing: + // the estimate is a proven 0, not an unknown. + let reg = registry(); + let mut replies = Map2::new(); + replies.insert( + ("dezren39/fleet".to_string(), "in_progress".to_string()), + (200, json!({ "total_count": 40 })), + ); + replies.insert( + ("dezren39/fleet".to_string(), "queued".to_string()), + (500, json!({ "message": "boom" })), + ); + let gh = FakeGh { + load_replies: replies, + ..Default::default() + }; + let c = build_ctx(®, &json!({}), false, "", None, &gh); + let cov = &c.owner_load_coverage["dezren39"]; + assert_eq!(cov.status, LoadCoverageStatus::Partial); + assert_eq!(cov.estimated_free(40), Some(0)); + let ad = adapter_for(find_surface(®, "fleet-personal").unwrap()).unwrap(); + let cap = ad.capacity(&job(), &c); + assert_eq!(cap.free_slots, Some(0)); + assert!(cap.summary.contains("saturated"), "{}", cap.summary); + let (picked, _) = decide(®, &job(), &c, &gh).unwrap(); + assert_eq!(surf_str(picked.unwrap().surface(), "id"), "org-run"); + } + + #[test] + fn coverage_override_is_labeled_simulated() { + // Fixture oracle: an explicit --assume-owner-load override + // is a simulation — labeled in the JSON and the summary — + // and may yield a numeric estimate on that basis alone. + let reg = registry(); + let gh = FakeGh::default(); + let c = build_ctx(®, &json!({}), false, "dezren39=5", None, &gh); + let cov = &c.owner_load_coverage["dezren39"]; + assert_eq!(cov.status, LoadCoverageStatus::Simulated); + assert_eq!(cov.known_load(), 5); + assert!(cov.source.contains("simulation"), "{}", cov.source); + let j = owner_load_coverage_json(&c); + assert_eq!(j["dezren39"]["status"], json!("simulated")); + assert_eq!(j["dezren39"]["known_load"], json!(5)); + let ad = adapter_for(find_surface(®, "fleet-personal").unwrap()).unwrap(); + let cap = ad.capacity(&job(), &c); + assert_eq!(cap.free_slots, Some(35)); + assert!(cap.summary.contains("simulated"), "{}", cap.summary); + } } diff --git a/pkgs/fleet/crates/fleet-queue/src/placement/adapters.rs b/pkgs/fleet/crates/fleet-queue/src/placement/adapters.rs index e851a70c19..e44ca84c79 100644 --- a/pkgs/fleet/crates/fleet-queue/src/placement/adapters.rs +++ b/pkgs/fleet/crates/fleet-queue/src/placement/adapters.rs @@ -502,13 +502,57 @@ impl Adapter for GithubActionsAdapter { ctx.owner_cap(owner) }; let active = running + queued; - let free = (cap - active).max(0); - Capacity { - summary: format!( - "owner {owner}: {active} active job(s) on fleet runner hosts \ - (proxy for account load) vs cap {cap} -> {free} free slot(s)" + // A numeric free-slot estimate requires complete load + // knowledge: measured reads, an explicitly labeled + // simulation, or a known subtotal that already proves + // saturation. Anything else reports unknown capacity + // instead of dressing an unreadable owner up as idle + // (issue #13611). + let coverage = ctx.owner_load_coverage.get(owner); + let free_slots = coverage.and_then(|c| c.estimated_free(cap)); + let summary = match coverage { + Some(c) => match c.status { + LoadCoverageStatus::Measured => format!( + "owner {owner}: {active} active job(s) on fleet runner hosts \ + (proxy for account load) vs cap {cap} -> {} free slot(s)", + free_slots.unwrap_or(0) + ), + LoadCoverageStatus::Simulated => format!( + "owner {owner}: {active} active job(s) (simulated via \ + --assume-owner-load, not live counts) vs cap {cap} -> {} free slot(s)", + free_slots.unwrap_or(0) + ), + LoadCoverageStatus::Partial => { + if free_slots == Some(0) { + format!( + "owner {owner}: load only partially observed, but the known \ + subtotal of {active} active job(s) already reaches cap {cap} \ + — saturated on the evidence available (missing reads: {})", + c.missing_reads.join("; ") + ) + } else { + format!( + "owner {owner}: load partially observed — at least {active} \ + active job(s) known vs cap {cap}; free slots unknown \ + (missing reads: {})", + c.missing_reads.join("; ") + ) + } + } + LoadCoverageStatus::Unknown => format!( + "owner {owner}: load unknown — no runner-host read succeeded \ + (missing reads: {}); free slots unknown, not zero load", + c.missing_reads.join("; ") + ), + }, + None => format!( + "owner {owner}: load unknown — no coverage recorded for this owner; \ + free slots unknown, not zero load" ), - free_slots: Some(free), + }; + Capacity { + summary, + free_slots, remaining_total_minutes: None, } }