Skip to main content

runner_manager_agent/
reconcile.rs

1// owner: e1-reconciliation-capacity
2
3//! The loop that turns GitHub demand into a decision to start runners — and
4//! that refuses to start them when it should not.
5//!
6//! Every ceiling in this product is enforced from here, so the module is
7//! organised around the four things that can go wrong silently:
8//!
9//! * [`PollSchedule`] — the budget-aware interval. Demand shares one 5,000
10//!   requests/hour ceiling with inventory and workflow counts, so this loop
11//!   polls an *active* target on a bounded interval (default 60 s, hard floor
12//!   30 s per target) and *increases* the delay under a rate-limit signal,
13//!   never decreases it to catch up. A target with nothing happening is polled
14//!   at the shorter *idle* interval (default 10 s), because its polls are
15//!   conditional requests answered `304`, which GitHub does not charge against
16//!   the hourly ceiling — and because work usually arrives after idle. When
17//!   the account's remaining quota runs low, the schedule stretches.
18//! * [`RepositoryCache`] — the per-organization repository list, refreshed on
19//!   an interval materially slower than the demand poll. Re-listing an
20//!   organization at demand-poll frequency is what exhausts the shared budget
21//!   the paragraph above exists to protect.
22//! * [`Reconciler::reconcile`] — the allocation pass. It re-reads the attempt
23//!   set **under the host-wide allocation lock, once per runtime created**, so
24//!   two policies reconciling concurrently cannot both spend the same headroom.
25//! * [`LifecycleEvent`] — what `g2` and the local log sink see. Every field is
26//!   an identifier, a count, an enumerated state or a duration; nothing free
27//!   text, and nothing that came off the wire.
28//!
29//! # There is no acquisition step, and none may be added
30//!
31//! The scale-set model called `AcquireJobs` to reserve an assignment before
32//! scaling. The REST path has no equivalent (`01-current-architecture.md`, edge
33//! case 6), so demand is **advisory**. Two consequences are load-bearing here
34//! and neither is a defect:
35//!
36//! 1. **A surplus runner is an accepted outcome.** Another host serving the
37//!    same labels may take the job first; this host's runner then finds no work
38//!    and exits on its idle timeout, having cost one capacity slot and one cold
39//!    start. That terminal outcome is
40//!    [`AttemptOutcome::ExitedIdleWithoutWork`], is cleaned like any other, and
41//!    is counted apart from a failure — see [`ReconcileReport::idle_exits`].
42//! 2. **The same job is still `queued` on the next poll** while its runner
43//!    starts. Subtracting only unassigned supply in
44//!    [`HostAllocator::allocate`] is what stops that from starting a second
45//!    runner, and then a third. This module's only job in that arithmetic is to
46//!    hand the allocator the attempt set the host actually holds — which is why
47//!    [`RunnerLauncher`] supplies both the attempts and the launch, from one
48//!    supply point, for the reason `b1` gives at
49//!    [`HostAllocator::from_attempts`].
50//!
51//! `tests::nothing_in_this_module_reserves_or_claims_a_job` is a tripwire on the
52//! obvious shape of a reservation being added back.
53//!
54//! # Demand is measured in JOBS, filtered by this policy's routing labels
55//!
56//! `02-target-architecture.md` writes the formula as *"queued jobs whose
57//! `runs-on` matches this policy's routing labels"*, and that is now exactly
58//! what this module clamps. It was not always: an earlier owner decision priced
59//! the per-run job listing out and left this module clamping a count of
60//! **runs**, unfiltered. `crates/github/src/demand.rs` records that decision,
61//! why it was reversed, and what the reversal costs in requests.
62//!
63//! What the reversal means here is two changes to one line:
64//!
65//! * **A run of eight jobs is now eight units of demand, not one.** Under the
66//!   run count a matrix filled one runner per poll while the rest of the matrix
67//!   waited, so a host configured for ten concurrent runners served an
68//!   eight-job matrix nearly serially. That was the defect that forced the
69//!   decision back.
70//! * **A job this host cannot serve is no longer demand.** A repository whose
71//!   jobs target `ubuntu-latest`, or another host's `rm-<host>-…` label, used to
72//!   drive its policy toward `max_capacity` and start runners that idled until
73//!   they timed out. The gateway now returns each queued job's `runs-on`, so
74//!   `b1`'s predicate finally has its input.
75//!
76//! **The predicate is still `b1`'s and the input is still `c4`'s.** This module
77//! calls [`runner_manager_domain::policy::RoutingLabels`]'s `tally` and
78//! implements no label comparison of its own;
79//! `tests::the_label_predicate_is_b1s_and_this_module_only_applies_it` scans
80//! this file's own source and fails if a second implementation grows here, which
81//! is the same tripwire `c4` carries one layer down.
82//!
83//! # The filtering happens here rather than in the gateway, on purpose
84//!
85//! One target can be watched by more than one policy, each with its own routing
86//! labels, and [`Reconciler`]'s `poll_targets` deliberately polls a target **once**
87//! for all of them. A gateway that filtered would have to be told whose labels to
88//! filter by, which would make the poll per-policy and multiply its request cost
89//! by the number of policies sharing the target — the budget model prices a
90//! target, not a policy. So the gateway returns the jobs and each policy tallies
91//! them against its own labels.
92//!
93//! # What is still approximate
94//!
95//! The surplus-runner path above is narrowed by this change and not closed. A
96//! `runs-on: ${{ matrix.runner }}` cannot be resolved without evaluating the
97//! workflow, so `b1` reports it as unresolvable: never counted as demand, never
98//! silently dropped, and surfaced through
99//! [`LifecycleEvent::DemandObserved::unresolvable`] so that an operator can see
100//! a workflow this host will never serve sitting in the queue. And demand
101//! remains advisory — another host may still take a job this one started a
102//! runner for — which is what the two ceilings bound.
103//!
104//! # What is testable without a network, a filesystem, or a process
105//!
106//! All of it. [`DemandSource`], [`RunnerLauncher`], [`AllocationLock`],
107//! [`RepositoryDirectory`], [`Jitter`] and [`EventSink`] are ports;
108//! [`GatewayDemand`], [`FileAllocationLock`], [`RandomJitter`] and
109//! [`TracingEvents`] are the production adapters, and every one of them is a
110//! thin shell over a decision made in this file.
111
112use std::collections::{BTreeMap, BTreeSet};
113use std::fmt;
114use std::num::NonZeroU16;
115use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
116use std::sync::{Arc, Mutex};
117use std::time::Duration;
118
119use runner_manager_domain::attempt::{
120    AttemptOutcome, AttemptState, FailureReason, IsolationProviderFailure, RunnerAttempt,
121};
122use runner_manager_domain::capacity::{Allocation, HostAllocator, LimitingFactor};
123use runner_manager_domain::model::{
124    AttemptId, Clock, Host, IdlePollInterval, Org, OwnerRepo, PolicyId, RefreshInterval,
125    ScaleTarget, Timestamp,
126};
127use runner_manager_domain::policy::{DemandTally, ScalePolicy};
128use runner_manager_github::demand::{DemandGateway, QueuedDemand, demand_requests_per_poll};
129use runner_manager_github::rest::{
130    ActivityScope, CancelToken, InventoryError, RateLimitKind, RefreshState,
131};
132use runner_manager_github::traffic::{PrimaryPressure, RateLimitSnapshot};
133
134// ---------------------------------------------------------------------------
135// Constants
136// ---------------------------------------------------------------------------
137
138/// How much slower than the demand poll the per-organization repository list is
139/// refreshed.
140///
141/// There is no organization-wide workflow-runs endpoint, so an organization
142/// target costs one demand request **per repository the App is installed on**
143/// (`crates/github/src/demand.rs`). Discovering that repository list costs
144/// requests of its own, and it is the one input to a demand poll that changes on
145/// a human timescale: repositories are added to an installation by hand, not by
146/// a workflow starting.
147///
148/// Thirty polls is 30 minutes at the 60-second default and 15 at the 30-second
149/// floor — slow enough that the list is a rounding error against the demand
150/// requests it scopes, and fast enough that a repository added to the
151/// installation starts being served within one coffee break rather than at the
152/// next restart.
153pub const REPOSITORY_LIST_REFRESH_MULTIPLE: u32 = 30;
154
155/// The longest the *unjittered* offline back-off may grow to.
156///
157/// A back-off is a safety mechanism, and an unclamped one is an outage with
158/// extra steps. Fifteen minutes matches
159/// [`runner_manager_github::rest::MAX_RATE_LIMIT_BACKOFF`], which is the other
160/// place in this product where a delay is allowed to grow, and it is far inside
161/// the 24-hour bound at which GitHub cancels the queued jobs this loop exists to
162/// serve.
163pub const MAX_OFFLINE_BACKOFF: Duration = Duration::from_secs(15 * 60);
164
165/// The most the offline back-off is doubled, before the cap applies.
166///
167/// At the 60-second default this reaches [`MAX_OFFLINE_BACKOFF`] on the sixth
168/// consecutive failure, which is roughly half an hour of outage. Past that the
169/// cap holds it flat.
170const MAX_BACKOFF_DOUBLINGS: u32 = 5;
171
172/// How much of the computed back-off is jitter.
173///
174/// Jitter is **added** rather than subtracted, so a back-off never comes out
175/// shorter than the delay it was computed from. Subtractive jitter would let the
176/// first offline poll retry sooner than the nominal interval, which is the
177/// opposite of backing off; it is spelled out because "add jitter" reads as
178/// symmetric and is not.
179const JITTER_RATIO: f64 = 0.5;
180
181/// How much of a rate-limit wait is jitter: added, never subtracted.
182///
183/// Every target loop on a host shares one rate-limit quiet period, so without
184/// it they all wake at the same instant the period ends, and a burst is the
185/// shape most likely to meet the secondary limit again. A tenth spreads them
186/// over a few seconds to a minute and a half without overshooting a
187/// 15-minute wait by much.
188const RATE_LIMIT_JITTER_RATIO: f64 = 0.1;
189
190/// The most demand requests a minute one target's idle polling may cost.
191///
192/// A third of GitHub's 900-a-minute secondary limit. A target of one or a few
193/// repositories never reaches it at the 5-second floor, but an organization of
194/// a hundred repositories costs about four hundred requests a pass, and at a
195/// 10-second idle interval that alone would exceed the whole account's
196/// secondary limit. Its idle interval stretches instead.
197pub const MAX_IDLE_REQUESTS_PER_MINUTE_PER_TARGET: u32 = 300;
198
199/// GitHub cancels a queued job after this long.
200///
201/// `01-current-architecture.md` records the measurement; `03-control-flows.md`
202/// flow 3.3 requires that the offline state **states** it, because an agent
203/// offline for longer than this has lost queued work and the operator cannot
204/// infer that from "offline". [`OfflineState`] is where it is said.
205pub const GITHUB_CANCELS_QUEUED_JOBS_AFTER: Duration = Duration::from_secs(24 * 60 * 60);
206
207/// How long [`FileAllocationLock`] waits for the host-wide allocation lock
208/// before reporting contention.
209///
210/// Contention here is expected rather than exceptional — it is two of this
211/// host's own policies creating runtimes at the same moment — and each hold
212/// lasts only as long as one runtime creation. Waiting a few seconds turns the
213/// common case into a short pause instead of a skipped runner.
214///
215/// # How many of these a poll actually costs
216///
217/// One per runtime created, none for a policy that is granted nothing, and at
218/// most one further hold per policy — the case where the pre-check proposed a
219/// grant and the under-lock re-read found the host had filled up underneath it,
220/// so that hold creates nothing and ends the loop. `(3..=5)` in
221/// `two_policies_reconciling_concurrently_never_exceed_host_capacity` is that
222/// bound with two policies and three runtimes; the deterministic single-policy
223/// case is pinned at exactly one per runtime.
224///
225/// That is worth stating because it did not used to be true and the
226/// difference only shows up here: the budget was checked *after* the lock had
227/// been taken and the attempt set re-read, so a policy granted N runners took
228/// N+1 holds, and `start_runners` ran for every readable autoscale policy
229/// including the zero-demand ones — so an idle host with P policies took P
230/// host-wide locks per poll for nothing. Free under
231/// [`InProcessAllocationLock`]; under [`FileAllocationLock`] each one is a
232/// `spawn_blocking` plus a filesystem lock, with this wait behind it.
233///
234/// [`Reconciler::start_runners`] now pre-checks lock-free and stops as soon as
235/// the budget is spent. The under-lock re-read still decides.
236pub const ALLOCATION_LOCK_WAIT: Duration = Duration::from_secs(5);
237
238// ---------------------------------------------------------------------------
239// What one demand poll produced
240// ---------------------------------------------------------------------------
241
242/// One target's demand poll, as a value this module can decide from.
243///
244/// The failure half is `c3`'s [`RefreshState`] rather than an
245/// [`InventoryError`], for the reason `c3` gives: `InventoryError` owns a
246/// `reqwest::Error` and a `serde_json::Error`, so it is neither `Clone` nor
247/// `PartialEq` and cannot be stored, compared, or rendered. Summarising at the
248/// gateway boundary — exactly once, in [`GatewayDemand`] — is what lets the
249/// whole schedule below be a pure function of values a test can construct.
250///
251/// [`RefreshState::Ready`] never appears in [`PollOutcome::Failed`]:
252/// [`RefreshState::from_error`] cannot produce it, and a demand poll returns a
253/// [`QueuedDemand`] rather than the runner inventory that variant carries.
254#[derive(Debug, Clone, PartialEq, Eq)]
255pub enum PollOutcome {
256    /// GitHub answered. The count may still be a floor — see
257    /// [`QueuedDemand::is_complete`].
258    Ready(QueuedDemand),
259    /// GitHub did not answer, or answered something this loop must slow down
260    /// for.
261    Failed(RefreshState),
262}
263
264impl PollOutcome {
265    /// The demand reading, when there is one.
266    #[must_use]
267    pub const fn reading(&self) -> Option<&QueuedDemand> {
268        match self {
269            Self::Ready(demand) => Some(demand),
270            Self::Failed(_) => None,
271        }
272    }
273
274    /// The failure, when there is one.
275    #[must_use]
276    pub const fn failure(&self) -> Option<&RefreshState> {
277        match self {
278            Self::Failed(state) => Some(state),
279            Self::Ready(_) => None,
280        }
281    }
282
283    /// Whether GitHub could not be reached at all, as opposed to answering
284    /// something unwelcome.
285    ///
286    /// The whole of flow 3.3 turns on this distinction: an outage retains
287    /// running runners and backs off, while a rejection is a configuration
288    /// problem that waiting does not fix.
289    #[must_use]
290    pub fn is_offline(&self) -> bool {
291        matches!(self, Self::Failed(RefreshState::Offline))
292    }
293}
294
295/// Where this loop gets its demand from.
296///
297/// A port rather than a direct [`DemandGateway`] dependency, because the two
298/// failures this loop must handle differently — unreachable and rate-limited —
299/// are distinguished by [`RefreshState`], and a test that wants to drive the
300/// offline path should not have to manufacture a `reqwest::Error` to do it.
301/// [`GatewayDemand`] is the one adapter that talks to `c4`.
302#[async_trait::async_trait]
303pub trait DemandSource: fmt::Debug + Send + Sync {
304    /// Queued runs across `scope`, or why there are none to report.
305    async fn poll(&self, scope: &ActivityScope) -> PollOutcome;
306}
307
308/// [`DemandSource`] over `c4`'s [`DemandGateway`].
309///
310/// Holds the [`CancelToken`] so that a shutting-down daemon can withdraw a poll
311/// that is already blocked on a socket; `f3` keeps a clone and cancels it.
312#[derive(Debug)]
313pub struct GatewayDemand<G> {
314    gateway: G,
315    cancel: CancelToken,
316}
317
318impl<G: DemandGateway> GatewayDemand<G> {
319    #[must_use]
320    pub const fn new(gateway: G, cancel: CancelToken) -> Self {
321        Self { gateway, cancel }
322    }
323
324    #[must_use]
325    pub const fn gateway(&self) -> &G {
326        &self.gateway
327    }
328}
329
330#[async_trait::async_trait]
331impl<G: DemandGateway + 'static> DemandSource for GatewayDemand<G> {
332    async fn poll(&self, scope: &ActivityScope) -> PollOutcome {
333        match self.gateway.queued_demand(scope, &self.cancel).await {
334            Ok(demand) => PollOutcome::Ready(demand),
335            // The one place an `InventoryError` is summarised. `c3` owns the
336            // mapping — including transport-to-`Offline`, which is what flow
337            // 3.3 branches on — so this loop never re-decides it.
338            Err(error) => PollOutcome::Failed(RefreshState::from_error(&error)),
339        }
340    }
341}
342
343// ---------------------------------------------------------------------------
344// The repository list, cached
345// ---------------------------------------------------------------------------
346
347/// Which repositories an organization installation reaches.
348///
349/// `f1` already holds this, from
350/// [`runner_manager_github::AuthenticatedClient::discover_installations`]. It is
351/// a port here so that [`RepositoryCache`] can be tested for the property that
352/// matters — how *often* it asks — without a network.
353#[async_trait::async_trait]
354pub trait RepositoryDirectory: fmt::Debug + Send + Sync {
355    /// The repositories this credential reaches in `org`.
356    ///
357    /// # Errors
358    /// Anything the underlying gateway reports.
359    async fn repositories(&self, org: &Org) -> Result<Vec<OwnerRepo>, InventoryError>;
360}
361
362#[derive(Debug, Clone)]
363struct CachedRepositories {
364    repositories: Vec<OwnerRepo>,
365    fetched_at: Timestamp,
366}
367
368/// The per-organization repository list, refreshed far more slowly than demand.
369///
370/// # Why this is not just "call the directory each poll"
371///
372/// An organization demand poll already costs one request per repository. Adding
373/// the installation listing to every poll makes the *scoping* of a poll cost
374/// requests on the same schedule as the poll itself, which is how a
375/// ten-repository organization at the 30-second floor stops fitting inside the
376/// half-of-5,000 allowance `f2` admits targets against. The repository list is
377/// also the one input that changes on a human timescale, so refreshing it
378/// [`REPOSITORY_LIST_REFRESH_MULTIPLE`] times more slowly costs nothing real.
379///
380/// # A repository target never consults the directory at all
381///
382/// Its scope is itself. That is not an optimisation; asking an installation
383/// listing which repositories a single named repository covers would be asking a
384/// question whose answer is already in the target.
385#[derive(Debug)]
386pub struct RepositoryCache {
387    directory: Arc<dyn RepositoryDirectory>,
388    clock: Arc<dyn Clock>,
389    ttl: Duration,
390    entries: Mutex<BTreeMap<Org, CachedRepositories>>,
391    lookups: AtomicU64,
392}
393
394impl RepositoryCache {
395    /// Build a cache whose refresh interval is `poll` slowed by
396    /// [`REPOSITORY_LIST_REFRESH_MULTIPLE`].
397    #[must_use]
398    pub fn new(
399        directory: Arc<dyn RepositoryDirectory>,
400        clock: Arc<dyn Clock>,
401        poll: RefreshInterval,
402    ) -> Self {
403        let ttl = Duration::from_secs(u64::from(poll.as_secs()))
404            .saturating_mul(REPOSITORY_LIST_REFRESH_MULTIPLE);
405        Self {
406            directory,
407            clock,
408            ttl,
409            entries: Mutex::new(BTreeMap::new()),
410            lookups: AtomicU64::new(0),
411        }
412    }
413
414    /// How long a cached repository list is reused for.
415    #[must_use]
416    pub const fn ttl(&self) -> Duration {
417        self.ttl
418    }
419
420    /// How many times the underlying directory was actually asked.
421    ///
422    /// Measured rather than assumed, for the reason `c4` measures its own
423    /// request count: a budget nothing counts is a table in a document.
424    #[must_use]
425    pub fn lookups(&self) -> u64 {
426        self.lookups.load(Ordering::SeqCst)
427    }
428
429    /// The scope one demand poll of `target` covers.
430    ///
431    /// # Errors
432    /// Whatever the directory reported, for an organization target whose list is
433    /// stale or absent. A repository target cannot fail.
434    pub async fn scope_for(&self, target: &ScaleTarget) -> Result<ActivityScope, InventoryError> {
435        match target {
436            ScaleTarget::Repository(repository) => {
437                Ok(ActivityScope::repository(repository.clone()))
438            }
439            ScaleTarget::Organization(org) => {
440                let repositories = self.repositories_of(org).await?;
441                Ok(ActivityScope::organization(org.clone(), repositories))
442            }
443        }
444    }
445
446    async fn repositories_of(&self, org: &Org) -> Result<Vec<OwnerRepo>, InventoryError> {
447        let now = self.clock.now();
448        if let Some(fresh) = self.fresh_entry(org, now) {
449            return Ok(fresh);
450        }
451
452        // The directory call is deliberately made with no lock held. Two
453        // concurrent misses can therefore both ask, which costs one extra
454        // listing on the poll that follows a restart; holding a `std::sync`
455        // mutex across an `await` would cost a blocked executor thread and, on
456        // a current-thread runtime, a deadlock. The cheaper mistake is the one
457        // that spends a request.
458        let repositories = self.directory.repositories(org).await?;
459        self.lookups.fetch_add(1, Ordering::SeqCst);
460        self.store(org.clone(), repositories.clone(), now);
461        Ok(repositories)
462    }
463
464    fn fresh_entry(&self, org: &Org, now: Timestamp) -> Option<Vec<OwnerRepo>> {
465        let entries = self.entries.lock().ok()?;
466        let entry = entries.get(org)?;
467        let age = now.signed_duration_since(entry.fetched_at).to_std().ok()?;
468        (age < self.ttl).then(|| entry.repositories.clone())
469    }
470
471    fn store(&self, org: Org, repositories: Vec<OwnerRepo>, fetched_at: Timestamp) {
472        if let Ok(mut entries) = self.entries.lock() {
473            entries.insert(
474                org,
475                CachedRepositories {
476                    repositories,
477                    fetched_at,
478                },
479            );
480        }
481    }
482}
483
484// ---------------------------------------------------------------------------
485// Jitter
486// ---------------------------------------------------------------------------
487
488/// The randomness in the offline back-off, as a port.
489///
490/// Flow 3.3 requires jittered back-off, and a jittered delay is by construction
491/// not reproducible — so the source of the randomness is a port, and every test
492/// below asserts the *bounds* of the delay against a fixed fraction rather than
493/// asserting a number it could only have got by running the generator.
494pub trait Jitter: fmt::Debug + Send + Sync {
495    /// A fraction in `[0.0, 1.0)`. Values outside that range are clamped by the
496    /// caller, so an implementation cannot lengthen a back-off without bound.
497    fn fraction(&self) -> f64;
498}
499
500/// The production source.
501#[derive(Debug, Clone, Copy, Default)]
502pub struct RandomJitter;
503
504impl Jitter for RandomJitter {
505    fn fraction(&self) -> f64 {
506        rand::random::<f64>()
507    }
508}
509
510/// A fixed fraction, for tests and for the acceptance suite.
511#[derive(Debug, Clone, Copy)]
512pub struct FixedJitter(pub f64);
513
514impl Jitter for FixedJitter {
515    fn fraction(&self) -> f64 {
516        self.0
517    }
518}
519
520/// No jitter at all: the back-off is exactly what the schedule computed.
521#[derive(Debug, Clone, Copy, Default)]
522pub struct NoJitter;
523
524impl Jitter for NoJitter {
525    fn fraction(&self) -> f64 {
526        0.0
527    }
528}
529
530// ---------------------------------------------------------------------------
531// The schedule
532// ---------------------------------------------------------------------------
533
534/// Why the next poll is when it is.
535///
536/// Reported rather than inferred, because
537/// `04-subsystem-contracts.md` requires that rate limiting be *"displayed, never
538/// hidden"* — and a delay that grew for a reason the caller cannot name is
539/// hidden however visible the number is.
540#[derive(Debug, Clone, Copy, PartialEq, Eq)]
541pub enum PollPace {
542    /// The configured (active) interval. Nothing is throttling this loop.
543    Nominal,
544    /// The idle interval: nothing is happening on this target, so its polls
545    /// are `304`s and cost nothing against the hourly quota. Not a slowdown.
546    Idle,
547    /// The account's remaining hourly quota is low, so this loop waits a
548    /// multiple of its active interval. GitHub's quota is per user, shared by
549    /// every host signed in as that user, so this can be another machine's
550    /// doing.
551    Stretched { pressure: PrimaryPressure },
552    /// GitHub's rate limit is exhausted. Resolves by waiting.
553    RateLimited { kind: RateLimitKind },
554    /// GitHub's temporary authentication lockout. The credential is fine.
555    LockedOut,
556    /// GitHub could not be reached. `consecutive` counts the unbroken run of
557    /// failures the back-off was computed from.
558    Offline { consecutive: u32 },
559    /// GitHub answered something no amount of waiting fixes — a rejected
560    /// credential, a permissions refusal, or an error status. The loop keeps
561    /// polling at its nominal interval so that a fix is noticed, and says that
562    /// it is blocked rather than pretending the poll succeeded.
563    Blocked,
564}
565
566impl PollPace {
567    /// Whether this pace is a slowdown the operator should be told about.
568    #[must_use]
569    pub const fn is_throttled(&self) -> bool {
570        !matches!(self, Self::Nominal | Self::Idle)
571    }
572
573    /// A fixed, credential-free name for the log sink and for `g2`.
574    #[must_use]
575    pub const fn as_str(&self) -> &'static str {
576        match self {
577            Self::Nominal => "nominal",
578            Self::Idle => "idle",
579            Self::Stretched {
580                pressure: PrimaryPressure::Normal,
581            } => "nominal",
582            Self::Stretched {
583                pressure: PrimaryPressure::Low,
584            } => "stretched_low_budget",
585            Self::Stretched {
586                pressure: PrimaryPressure::Critical,
587            } => "stretched_critical_budget",
588            Self::RateLimited {
589                kind: RateLimitKind::Primary,
590            } => "rate_limited_primary",
591            Self::RateLimited {
592                kind: RateLimitKind::Secondary,
593            } => "rate_limited_secondary",
594            Self::LockedOut => "locked_out",
595            Self::Offline { .. } => "offline",
596            Self::Blocked => "blocked",
597        }
598    }
599}
600
601impl fmt::Display for PollPace {
602    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
603        f.write_str(self.as_str())
604    }
605}
606
607/// When to poll next, and why then.
608#[derive(Debug, Clone, Copy, PartialEq, Eq)]
609pub struct NextPoll {
610    pub delay: Duration,
611    pub pace: PollPace,
612}
613
614/// The bounded, budget-aware poll interval.
615///
616/// # The floor is a rate-budget constraint, not a preference
617///
618/// [`RefreshInterval`] refuses anything under 30 seconds at construction, and
619/// every delay this type produces is at least that — including the ones it
620/// computes from a remote header. A rate limit may only ever make this loop
621/// *slower*.
622///
623/// # `retry_delay` is an absolute floor, not an addend
624///
625/// `c3` documents [`RefreshState::retry_delay`] as *"the earliest time a retry
626/// may occur"*: the scheduling rule is `next_attempt_at = now + retry_delay`,
627/// and **not** the ordinary interval plus it. Adding the two compounds on every
628/// successive retry — each new answer carries the remaining window, so an
629/// addend ratchets outward — and the symptom is a dashboard that stays dark
630/// long after GitHub said it could come back, which reads as a hang rather than
631/// as a rate limit. So the two are combined with `max`, which is what makes the
632/// floor a floor.
633#[derive(Debug, Clone)]
634pub struct PollSchedule {
635    interval: RefreshInterval,
636    /// The idle interval. Equal to the active one until [`Self::with_idle`]
637    /// says otherwise, so a caller that never heard of it keeps its schedule.
638    idle: Duration,
639    consecutive_offline: u32,
640    offline_since: Option<Timestamp>,
641}
642
643/// What a successful pass found, beyond "GitHub answered".
644#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
645pub struct PollConditions {
646    /// Nothing is happening: no live runner, no run whose listings changed.
647    /// See `QueuedDemand::is_quiet`.
648    pub quiet: bool,
649    /// The account's hourly quota as GitHub last reported it.
650    pub quota: Option<RateLimitSnapshot>,
651    /// What one poll of this target costs, in demand requests. Stretches the
652    /// idle interval of a target too large to poll that often; see
653    /// [`MAX_IDLE_REQUESTS_PER_MINUTE_PER_TARGET`].
654    pub requests_per_pass: u32,
655}
656
657impl PollSchedule {
658    #[must_use]
659    pub const fn new(interval: RefreshInterval) -> Self {
660        Self {
661            interval,
662            idle: Duration::from_secs(interval.as_secs() as u64),
663            consecutive_offline: 0,
664            offline_since: None,
665        }
666    }
667
668    /// Poll a quiet target every `idle` instead of every active interval.
669    ///
670    /// Never longer than the active interval: an idle interval above it would
671    /// make a quiet target *slower* to notice work than a busy one.
672    #[must_use]
673    pub fn with_idle(mut self, idle: Duration) -> Self {
674        self.set_intervals(self.interval, idle);
675        self
676    }
677
678    /// Change both intervals while running.
679    pub fn set_intervals(&mut self, interval: RefreshInterval, idle: Duration) {
680        self.interval = interval;
681        self.idle = idle.min(self.nominal()).max(Self::idle_floor());
682    }
683
684    #[must_use]
685    pub const fn interval(&self) -> RefreshInterval {
686        self.interval
687    }
688
689    /// The interval a quiet target is polled at.
690    #[must_use]
691    pub const fn idle(&self) -> Duration {
692        self.idle
693    }
694
695    /// The shortest delay this schedule ever produces: the idle interval's
696    /// floor. See `IdlePollInterval` for why it is lower than [`Self::floor`].
697    #[must_use]
698    pub const fn idle_floor() -> Duration {
699        Duration::from_secs(IdlePollInterval::MIN_SECS as u64)
700    }
701
702    /// The nominal interval as a [`Duration`].
703    #[must_use]
704    pub const fn nominal(&self) -> Duration {
705        Duration::from_secs(self.interval.as_secs() as u64)
706    }
707
708    /// The unbroken run of offline polls this schedule has seen.
709    #[must_use]
710    pub const fn consecutive_offline(&self) -> u32 {
711        self.consecutive_offline
712    }
713
714    /// How long GitHub has been unreachable, or `None` when it is not.
715    ///
716    /// Measured from the first poll of the current run rather than inferred
717    /// from [`Self::consecutive_offline`] times the interval. The two diverge
718    /// as soon as the back-off starts doubling, and this is the number the
719    /// 24-hour queue-cancellation warning is compared against — an estimate
720    /// would make that warning fire early or late, and it is the one thing the
721    /// offline state exists to say.
722    #[must_use]
723    pub fn offline_for(&self, now: Timestamp) -> Option<Duration> {
724        let since = self.offline_since?;
725        now.signed_duration_since(since).to_std().ok()
726    }
727
728    /// The hard floor no computed delay may go below.
729    #[must_use]
730    pub const fn floor() -> Duration {
731        Duration::from_secs(RefreshInterval::MIN_SECS as u64)
732    }
733
734    /// Decide when to poll next, given how this pass ended.
735    ///
736    /// `failure` is the most severe failure across the targets polled this pass,
737    /// or `None` when every target answered. A pass that answered resets the
738    /// offline run, which is the whole of "recovery needs no bookkeeping":
739    /// demand is recomputed from the current queued-run set on every poll, so
740    /// there is nothing else to unwind.
741    pub fn next_poll(
742        &mut self,
743        failure: Option<&RefreshState>,
744        now: Timestamp,
745        jitter: &dyn Jitter,
746    ) -> NextPoll {
747        self.next_poll_with(failure, PollConditions::default(), now, jitter)
748    }
749
750    /// [`Self::next_poll`], with what a successful pass found.
751    ///
752    /// # On success, three paces, in this order
753    ///
754    /// 1. **Stretched**, when the account's remaining quota is under
755    ///    `LOW_PRIMARY_REMAINING_PERCENT`: the active interval times
756    ///    [`PrimaryPressure::stretch_factor`] (2, or 4 under
757    ///    `CRITICAL_PRIMARY_REMAINING_PERCENT`), but never past the quota's
758    ///    reset — waiting longer than that buys nothing, because the bucket is
759    ///    full again. This wins over idle: a `304` is free, but the poll that
760    ///    finds new work is not, and every host on the account draws from the
761    ///    same bucket.
762    /// 2. **Idle**, when the pass was quiet.
763    /// 3. **Nominal**, the active interval, otherwise.
764    pub fn next_poll_with(
765        &mut self,
766        failure: Option<&RefreshState>,
767        conditions: PollConditions,
768        now: Timestamp,
769        jitter: &dyn Jitter,
770    ) -> NextPoll {
771        let nominal = self.nominal();
772
773        let next = match failure {
774            None => {
775                self.recovered();
776                let pressure = conditions
777                    .quota
778                    .map_or(PrimaryPressure::Normal, |quota| quota.pressure(now));
779                match (pressure, conditions.quota) {
780                    (PrimaryPressure::Low | PrimaryPressure::Critical, Some(quota)) => NextPoll {
781                        delay: nominal
782                            .saturating_mul(pressure.stretch_factor())
783                            .min(quota.reset_in(now).max(nominal)),
784                        pace: PollPace::Stretched { pressure },
785                    },
786                    // An idle interval no shorter than the active one is no
787                    // idle interval at all, and naming it would be noise.
788                    _ if conditions.quiet && self.idle < nominal => NextPoll {
789                        delay: self.idle.max(idle_floor_for(conditions.requests_per_pass)),
790                        pace: PollPace::Idle,
791                    },
792                    _ => NextPoll {
793                        delay: nominal,
794                        pace: PollPace::Nominal,
795                    },
796                }
797            }
798            Some(RefreshState::Offline) => {
799                self.consecutive_offline = self.consecutive_offline.saturating_add(1);
800                // The instant the *run* began, not the instant of this poll.
801                self.offline_since.get_or_insert(now);
802                NextPoll {
803                    delay: self.offline_delay(nominal, jitter),
804                    pace: PollPace::Offline {
805                        consecutive: self.consecutive_offline,
806                    },
807                }
808            }
809            Some(state @ RefreshState::RateLimited(limit)) => {
810                self.recovered();
811                // `max`, never `+`. See the type documentation. The jitter is
812                // added to the result, never to the floor GitHub named.
813                let wait = retry_floor(state, now).max(nominal);
814                NextPoll {
815                    delay: wait.saturating_add(
816                        wait.mul_f64(RATE_LIMIT_JITTER_RATIO * jitter.fraction().clamp(0.0, 1.0)),
817                    ),
818                    pace: PollPace::RateLimited { kind: limit.kind },
819                }
820            }
821            Some(state @ RefreshState::LockedOut { .. }) => {
822                self.recovered();
823                NextPoll {
824                    delay: retry_floor(state, now).max(nominal),
825                    pace: PollPace::LockedOut,
826                }
827            }
828            // Unauthorized, Forbidden, Failed, Cancelled. `retry_delay` is
829            // `None` for all of them, and deliberately: no wait fixes a revoked
830            // credential or a missing grant. Polling stops being useful but
831            // does not stop, because the poll is also how a re-authentication
832            // is noticed.
833            Some(_) => {
834                // GitHub answered, so it is reachable: whatever is wrong, it is
835                // not an outage, and an outage run that was open must close.
836                self.recovered();
837                NextPoll {
838                    delay: nominal,
839                    pace: PollPace::Blocked,
840                }
841            }
842        };
843
844        debug_assert!(
845            next.delay >= Self::floor() || next.pace == PollPace::Idle,
846            "the 30-second floor is a rate-budget constraint and no charged branch may go below it"
847        );
848        debug_assert!(
849            next.delay >= Self::idle_floor(),
850            "the idle floor is a secondary-limit constraint and no branch may go below it"
851        );
852        next
853    }
854
855    /// GitHub answered something. Whatever it was, the outage run is over.
856    fn recovered(&mut self) {
857        self.consecutive_offline = 0;
858        self.offline_since = None;
859    }
860
861    fn offline_delay(&self, nominal: Duration, jitter: &dyn Jitter) -> Duration {
862        let doublings = self
863            .consecutive_offline
864            .saturating_sub(1)
865            .min(MAX_BACKOFF_DOUBLINGS);
866        let grown = nominal.saturating_mul(1_u32 << doublings);
867        let capped = grown.min(MAX_OFFLINE_BACKOFF);
868        // Additive, never subtractive: see `JITTER_RATIO`. The result may exceed
869        // `MAX_OFFLINE_BACKOFF` by up to the jitter ratio, which is the price of
870        // keeping a fleet of agents from retrying in lockstep at the plateau —
871        // a cap applied *after* jitter would collapse every agent onto the same
872        // instant precisely when the outage is longest.
873        let spread = capped.mul_f64(JITTER_RATIO * jitter.fraction().clamp(0.0, 1.0));
874        capped.saturating_add(spread).max(Self::floor())
875    }
876}
877
878/// Whether a pass found nothing happening, so the next one may come at the
879/// idle interval.
880///
881/// All three are needed. Every target answered, and quietly
882/// (`QueuedDemand::is_quiet`); this pass started no runner; and no policy
883/// here holds a live one. A live runner means a job is running or about to,
884/// and its follow-on work (`needs:`, the next matrix wave) is exactly what an
885/// active target is polled for. A pass that polled nothing is not quiet: there
886/// is nothing to be quiet about, and the active interval is what it had
887/// before.
888fn is_quiet(readings: &BTreeMap<ScaleTarget, PollOutcome>, report: &ReconcileReport) -> bool {
889    !readings.is_empty()
890        && readings
891            .values()
892            .all(|outcome| outcome.reading().is_some_and(QueuedDemand::is_quiet))
893        && report.started == 0
894        // A missing allocation after a journal failure is unknown activity,
895        // not evidence that the host is idle.
896        && report.host_capacity_unreadable == 0
897        && report.attempts_unreadable == 0
898        // Demand this host matched and did not serve is not quiet either: a
899        // launch that keeps failing (a refused JIT registration) would
900        // otherwise be retried, and charged, every few seconds.
901        && report
902            .allocations
903            .iter()
904            .all(|allocation| allocation.active_owned == 0 && allocation.demand == 0)
905}
906
907/// The shortest idle delay a poll of `requests_per_pass` requests may have.
908fn idle_floor_for(requests_per_pass: u32) -> Duration {
909    Duration::from_secs(
910        u64::from(requests_per_pass) * 60 / u64::from(MAX_IDLE_REQUESTS_PER_MINUTE_PER_TARGET),
911    )
912}
913
914/// `c3`'s retry floor, with the one fallback this loop needs.
915///
916/// [`RefreshState::retry_delay`] answers `None` for the states no wait fixes,
917/// and those never reach here — the caller matches them into
918/// [`PollPace::Blocked`] first. The fallback exists so that a future
919/// `RefreshState` variant added to the two arms above cannot silently schedule a
920/// zero-second retry against an endpoint that asked for quiet.
921fn retry_floor(state: &RefreshState, now: Timestamp) -> Duration {
922    state.retry_delay(now).unwrap_or(PollSchedule::floor())
923}
924
925// ---------------------------------------------------------------------------
926// Offline
927// ---------------------------------------------------------------------------
928
929/// What an operator is told while GitHub is unreachable.
930///
931/// Flow 3.3 requires four things of an outage — start no new runner, retain
932/// existing runner processes, report `offline`, back off with jitter — and one
933/// thing of the *state*: that it says GitHub cancels queued jobs after 24 hours,
934/// so a prolonged outage loses queued work. That bound is stated here rather
935/// than left for a reader to infer, because an operator who does not know it has
936/// no reason to treat a long outage as urgent.
937#[derive(Debug, Clone, Copy, PartialEq, Eq)]
938pub struct OfflineState {
939    /// The unbroken run of failed polls.
940    pub consecutive: u32,
941    /// How long until the next attempt.
942    pub retry_in: Duration,
943    /// How long this loop has been unable to reach GitHub, when it is known.
944    pub offline_for: Option<Duration>,
945}
946
947impl OfflineState {
948    #[must_use]
949    pub const fn new(consecutive: u32, retry_in: Duration) -> Self {
950        Self {
951            consecutive,
952            retry_in,
953            offline_for: None,
954        }
955    }
956
957    #[must_use]
958    pub const fn since(mut self, offline_for: Duration) -> Self {
959        self.offline_for = Some(offline_for);
960        self
961    }
962
963    /// Whether the outage has already outlasted GitHub's queue.
964    ///
965    /// `false` when the duration is unknown: this reports a fact, and "we cannot
966    /// tell" is not the same fact as "not yet".
967    #[must_use]
968    pub fn has_outlasted_the_queue(&self) -> bool {
969        self.offline_for
970            .is_some_and(|elapsed| elapsed >= GITHUB_CANCELS_QUEUED_JOBS_AFTER)
971    }
972}
973
974impl fmt::Display for OfflineState {
975    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
976        write!(
977            f,
978            "GitHub is unreachable; no new runners are being started and running \
979             runners are left alone. Retrying in {}s",
980            self.retry_in.as_secs()
981        )?;
982        if self.has_outlasted_the_queue() {
983            f.write_str(
984                ". This outage has lasted more than 24 hours, and GitHub cancels a queued \
985                 job after 24 hours, so queued work has been lost",
986            )
987        } else {
988            f.write_str(
989                ". GitHub cancels a queued job after 24 hours, so an outage longer than \
990                 that loses queued work",
991            )
992        }
993    }
994}
995
996// ---------------------------------------------------------------------------
997// The launcher port
998// ---------------------------------------------------------------------------
999
1000/// What this loop asks `e3` to create.
1001#[derive(Debug, Clone, Copy)]
1002pub struct LaunchRequest<'a> {
1003    pub host: &'a Host,
1004    pub policy: &'a ScalePolicy,
1005    /// Proof that e1 owns the host allocation lock while e3 reserves the
1006    /// runner and prunes what is obsolete.
1007    // Crate-visible so only this allocator can mint the request that reaches
1008    // package pruning. A caller holding an unrelated public AllocationLock can
1009    // no longer assemble a LaunchRequest and present that guard as authority.
1010    pub(crate) allocation_guard: &'a AllocationGuard,
1011}
1012
1013/// Why one runner could not be started.
1014///
1015/// Carries `b1`'s [`FailureReason`] rather than a taxonomy of this module's own:
1016/// the reasons a runner fails to start are `e3`'s to know and `b1`'s to name,
1017/// and a third vocabulary here would be a third answer to a question the
1018/// operator asks once.
1019#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
1020#[error("the runner could not be started: {reason}")]
1021pub struct LaunchFailure {
1022    pub reason: FailureReason,
1023}
1024
1025/// A lifecycle conclusion that must return through ordinary demand and
1026/// capacity allocation before another runner may start.
1027#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1028pub struct ReplacementIntent {
1029    pub policy: PolicyId,
1030    pub previous_attempt: AttemptId,
1031    pub operation: &'static str,
1032}
1033
1034impl LaunchFailure {
1035    #[must_use]
1036    pub const fn new(reason: FailureReason) -> Self {
1037        Self { reason }
1038    }
1039}
1040
1041/// Why the persisted host ceiling could not be read. Closed, non-secret reasons
1042/// remain distinguishable in reports and structured logs.
1043#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1044pub enum HostCapacityError {
1045    MissingHost,
1046    JournalUnreadable,
1047}
1048
1049impl HostCapacityError {
1050    #[must_use]
1051    pub const fn reason(self) -> &'static str {
1052        match self {
1053            Self::MissingHost => "host_missing",
1054            Self::JournalUnreadable => "host_journal_unreadable",
1055        }
1056    }
1057}
1058
1059#[derive(Debug)]
1060enum AllocationFailure {
1061    HostCapacity(HostCapacityError),
1062    Attempts(LaunchFailure),
1063}
1064
1065/// The seam between the decision to start a runner and the act of starting one.
1066///
1067/// `e3` implements this; every test in this file fakes it, which is what makes
1068/// the whole allocator path decidable with no process, no filesystem and no
1069/// network.
1070///
1071/// # Why the attempt set comes through the same port as the launch
1072///
1073/// `b1` makes this argument at [`HostAllocator::from_attempts`] and it applies
1074/// one layer up: the host-wide total (D9) and every per-policy count (D7) are
1075/// two questions asked of **one** set, and a design that let the caller supply
1076/// the set separately from the thing that creates its members is a design in
1077/// which the two can disagree. Worse, it makes `&[]` expressible — and an empty
1078/// attempt set is exactly the shape that drops the `- active_owned_runners`
1079/// term, starts a second runner for a job already being served, and reports no
1080/// error while doing it.
1081///
1082/// So the launcher is asked, under the allocation lock, immediately before each
1083/// runtime is created. There is no second supply point and no cached copy.
1084///
1085/// # The two ways an implementer can say "I hold no attempts"
1086///
1087/// The argument above closes the hole for a *caller*. It stayed open one level
1088/// down for the **implementer**, in two shapes that both oversubscribe the
1089/// machine and neither of which reports anything:
1090///
1091/// * **By failing.** `attempts()` used to be infallible, which left `e3` — which
1092///   reads a journal off a disk — a choice between panicking and answering
1093///   `vec![]` on an I/O error. An empty set is indistinguishable from an idle
1094///   host, so a transient read failure reads as "nothing is running" and the
1095///   next pass allocates the whole machine for jobs already being served. It is
1096///   fallible now, and [`Reconciler`] treats a failure the way it treats a lock
1097///   it could not take: start nothing, say so, try again next pass.
1098/// * **By lagging.** [`Self::launch`] returns the attempt it created rather than
1099///   its identifier, so the caller can carry it. See that method for the
1100///   measurement that made this necessary.
1101#[async_trait::async_trait]
1102pub trait RunnerLauncher: fmt::Debug + Send + Sync {
1103    /// Read this host's current ceiling from the journal, including edits made
1104    /// after daemon startup. An unreadable or missing host is an error, never
1105    /// permission to fall back to the startup ceiling.
1106    async fn host_capacity(&self, host: &Host) -> Result<NonZeroU16, HostCapacityError>;
1107
1108    /// Reconcile this policy's existing processes before demand is read and
1109    /// capacity is recomputed. A concluded pre-acceptance attempt thereby
1110    /// becomes an ordinary allocation candidate in this same pass; replacement
1111    /// never bypasses the allocator.
1112    async fn supervise(
1113        &self,
1114        _policy: &ScalePolicy,
1115    ) -> Result<Vec<ReplacementIntent>, LaunchFailure> {
1116        Ok(Vec::new())
1117    }
1118
1119    /// Every attempt this host holds, across every policy, terminal ones
1120    /// included.
1121    ///
1122    /// Terminal attempts are included rather than filtered out because the
1123    /// caller needs both answers from one set:
1124    /// [`AttemptState::counts_against_capacity`] decides the ceiling, and the
1125    /// terminal ones are what [`RunnerLauncher::clean`] is for.
1126    ///
1127    /// # Errors
1128    /// [`LaunchFailure`] when the set could not be read. **Never answer `Ok`
1129    /// with an empty vector to signal a failure** — the caller cannot tell that
1130    /// from an idle host, and the two lead to opposite actions.
1131    async fn attempts(&self) -> Result<Vec<RunnerAttempt>, LaunchFailure>;
1132
1133    /// Create exactly one runtime and start one runner, and return the attempt
1134    /// that now exists.
1135    ///
1136    /// Called once per grant, with the host-wide allocation lock held.
1137    ///
1138    /// # The attempt is returned, not just its identifier
1139    ///
1140    /// The host ceiling is enforced against a host-wide total, and that total is
1141    /// recomputed from [`Self::attempts`] on every hold. If a launch is not yet
1142    /// visible there when the *next* policy is allocated for — a journal write
1143    /// that has not landed, an asynchronous store, a cache — then that policy's
1144    /// grant is computed from a set missing the previous policy's runners, and
1145    /// it is too large.
1146    ///
1147    /// That is measured, not hypothetical. With a launcher whose attempts never
1148    /// became visible, two policies on a host of **three** started **six**
1149    /// runners, with the allocation lock held correctly throughout:
1150    /// `host_capacity=3, started=6, launches=6`. Serialisation was never the
1151    /// problem; the arithmetic under it was reading a stale set.
1152    ///
1153    /// So an implementer *should* make the new attempt visible to
1154    /// [`Self::attempts`] before returning — and the caller does not depend on
1155    /// it. [`Reconciler`] carries what this pass created and merges it, by
1156    /// [`RunnerAttempt::id`], with whatever the launcher reports. A launcher
1157    /// that honours the contract is not double-counted, and one that lags cannot
1158    /// oversubscribe the host.
1159    ///
1160    /// # Every call must return a **fresh** [`RunnerAttempt::id`]
1161    ///
1162    /// This is a requirement, not a convention, because the merge above is what
1163    /// carries the host ceiling and the merge is keyed on the identifier. Two
1164    /// calls that answer with the same id are two runtimes that the host-wide
1165    /// total counts once, and the machine is then allocated past
1166    /// `host_capacity`: probed at `host_capacity = 3` with one slot already
1167    /// busy and a launcher answering with a duplicate id, the pass started
1168    /// **four** runners for five occupied slots.
1169    ///
1170    /// That is a narrower defect than the lagging launcher above — that one
1171    /// needed no bug at all, this one needs a broken id generator — but `e3` is
1172    /// the implementor and cannot honour a requirement nobody states.
1173    /// [`Reconciler::host_attempts`] carries a `debug_assert` that fires on a
1174    /// collision, so a development build finds it at the first duplicate rather
1175    /// than through an oversubscribed host.
1176    ///
1177    /// # Errors
1178    /// [`LaunchFailure`], carrying the [`FailureReason`] `e3` recorded.
1179    ///
1180    /// Called under the allocation lock, so it stops at what must be decided
1181    /// there: the attempt, journalled, holding its slot. The runner itself is
1182    /// [`Self::finish_launch`], outside the lock.
1183    async fn launch(&self, request: LaunchRequest<'_>) -> Result<RunnerAttempt, LaunchFailure>;
1184
1185    /// Bring up a runner [`Self::launch`] reserved, after the host allocation
1186    /// lock was released: its package, its registration and its process, none
1187    /// of which decides a ceiling, and all of which may take minutes.
1188    ///
1189    /// The reservation is what holds the slot, and it is journalled, so another
1190    /// allocator counts it from the moment the lock is let go, and a crash at
1191    /// any point here leaves a journalled attempt that startup recovery
1192    /// concludes. A launcher whose `launch` does everything keeps the default.
1193    ///
1194    /// # Errors
1195    /// [`LaunchFailure`], carrying the [`FailureReason`] recorded.
1196    async fn finish_launch(
1197        &self,
1198        _policy: &ScalePolicy,
1199        attempt: RunnerAttempt,
1200        _lock_wait: Duration,
1201    ) -> Result<RunnerAttempt, LaunchFailure> {
1202        Ok(attempt)
1203    }
1204
1205    /// Remove a terminal attempt's runtime and mark it `cleaned`.
1206    ///
1207    /// Never called for a non-terminal attempt: capacity is reclaimed when an
1208    /// attempt reaches a terminal state and at no other time.
1209    ///
1210    /// # Errors
1211    /// [`LaunchFailure`], carrying the [`FailureReason`] `e3` recorded.
1212    async fn clean(&self, attempt: AttemptId) -> Result<(), LaunchFailure>;
1213}
1214
1215/// A [`RunnerLauncher`] that refuses every launch with
1216/// [`FailureReason::HostUnfit`] while `unfit` is set, and passes everything
1217/// else straight through.
1218///
1219/// The daemon sets the flag while a required host check fails (`host
1220/// doctor`). The refusal comes before the inner launcher runs, so nothing is
1221/// registered with GitHub, and the reconciler reports it as a
1222/// `runner_start_failed` event with reason `host_unfit` -- visible, rather than
1223/// a host that silently stopped serving. Supervision, the attempt set and
1224/// cleanup are never gated: what already runs is still looked after.
1225#[derive(Debug)]
1226pub struct FitnessGatedLauncher {
1227    inner: Arc<dyn RunnerLauncher>,
1228    unfit: Arc<AtomicBool>,
1229}
1230
1231impl FitnessGatedLauncher {
1232    #[must_use]
1233    pub const fn new(inner: Arc<dyn RunnerLauncher>, unfit: Arc<AtomicBool>) -> Self {
1234        Self { inner, unfit }
1235    }
1236}
1237
1238#[async_trait::async_trait]
1239impl RunnerLauncher for FitnessGatedLauncher {
1240    async fn host_capacity(&self, host: &Host) -> Result<NonZeroU16, HostCapacityError> {
1241        self.inner.host_capacity(host).await
1242    }
1243
1244    async fn supervise(
1245        &self,
1246        policy: &ScalePolicy,
1247    ) -> Result<Vec<ReplacementIntent>, LaunchFailure> {
1248        self.inner.supervise(policy).await
1249    }
1250
1251    async fn attempts(&self) -> Result<Vec<RunnerAttempt>, LaunchFailure> {
1252        self.inner.attempts().await
1253    }
1254
1255    async fn launch(&self, request: LaunchRequest<'_>) -> Result<RunnerAttempt, LaunchFailure> {
1256        if self.unfit.load(Ordering::Acquire) {
1257            return Err(LaunchFailure::new(FailureReason::HostUnfit));
1258        }
1259        self.inner.launch(request).await
1260    }
1261
1262    async fn finish_launch(
1263        &self,
1264        policy: &ScalePolicy,
1265        attempt: RunnerAttempt,
1266        lock_wait: Duration,
1267    ) -> Result<RunnerAttempt, LaunchFailure> {
1268        self.inner.finish_launch(policy, attempt, lock_wait).await
1269    }
1270
1271    async fn clean(&self, attempt: AttemptId) -> Result<(), LaunchFailure> {
1272        self.inner.clean(attempt).await
1273    }
1274}
1275
1276// ---------------------------------------------------------------------------
1277// The host-wide allocation lock
1278// ---------------------------------------------------------------------------
1279
1280/// The lock is held for as long as this value lives.
1281///
1282/// Opaque on purpose: what is being held differs between the in-process and the
1283/// file-backed implementation, and a caller that could see which one it has
1284/// would eventually branch on it.
1285pub struct AllocationGuard {
1286    _held: Box<dyn std::any::Any + Send + Sync>,
1287    launch_hold: LaunchHold,
1288}
1289
1290impl AllocationGuard {
1291    /// Wrap whatever the implementation holds. Dropping the guard drops it.
1292    #[must_use]
1293    fn new<T: Send + Sync + 'static>(held: T) -> Self {
1294        Self {
1295            _held: Box::new(held),
1296            launch_hold: LaunchHold::default(),
1297        }
1298    }
1299
1300    /// What a launch keeps after the lock is released; see [`LaunchHold`].
1301    fn launch_hold(&self) -> LaunchHold {
1302        self.launch_hold.clone()
1303    }
1304}
1305
1306/// What must stay held while a runner reserved under the allocation lock is
1307/// brought up outside it. Empty, except where a boundary other than the lock
1308/// must not be crossed by a launch in progress: see
1309/// [`WslRecoveryAllocationLock`].
1310#[derive(Clone, Default)]
1311struct LaunchHold {
1312    _held: Option<Arc<dyn std::any::Any + Send + Sync>>,
1313}
1314
1315impl fmt::Debug for AllocationGuard {
1316    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1317        f.write_str("AllocationGuard")
1318    }
1319}
1320
1321/// The host-wide allocation lock could not be taken.
1322#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
1323#[error("the host-wide allocation lock is held by another allocator; no runtime was created")]
1324pub struct AllocationLockBusy;
1325
1326/// Flow 2.4's *"takes the host-wide allocation lock before creating each local
1327/// runtime"*, as a port.
1328///
1329/// # Why a lock is needed at all, given the allocator already exists
1330///
1331/// [`HostAllocator`] enforces D9 across the policies of **one** pass. It cannot
1332/// enforce anything across two passes running at once, and `f3` runs one
1333/// demand-polling loop per target: without serialisation, two loops read the
1334/// same headroom, each finds it sufficient, and the host ends up with the sum of
1335/// two grants it only ever had room for one of. The lock is what makes the
1336/// read-decide-create sequence atomic, and it is taken once per runtime rather
1337/// than once per pass so that a slow package download in one policy does not
1338/// hold the whole host still.
1339#[async_trait::async_trait]
1340pub trait AllocationLock: fmt::Debug + Send + Sync {
1341    /// Take the lock, waiting briefly for it.
1342    ///
1343    /// # Errors
1344    /// [`AllocationLockBusy`] when it could not be taken. A refused grant is
1345    /// always safe: the next pass re-reads the headroom and tries again.
1346    async fn acquire(&self) -> Result<AllocationGuard, AllocationLockBusy>;
1347}
1348
1349/// The lock every task inside one agent process contends for.
1350///
1351/// This is the implementation that matters in practice, because the
1352/// single-instance lock (`d1`) already guarantees one agent per host: the
1353/// concurrency the allocation lock actually has to serialise is `f3`'s
1354/// per-target loops inside that one process. A `tokio` mutex rather than a
1355/// `std` one because it is held across the `await` that creates the runtime.
1356#[derive(Debug, Default)]
1357pub struct InProcessAllocationLock {
1358    mutex: Arc<tokio::sync::Mutex<()>>,
1359}
1360
1361impl InProcessAllocationLock {
1362    #[must_use]
1363    pub fn new() -> Self {
1364        Self::default()
1365    }
1366}
1367
1368#[async_trait::async_trait]
1369impl AllocationLock for InProcessAllocationLock {
1370    async fn acquire(&self) -> Result<AllocationGuard, AllocationLockBusy> {
1371        let mutex = Arc::clone(&self.mutex);
1372        let guard = mutex.lock_owned().await;
1373        Ok(AllocationGuard::new(guard))
1374    }
1375}
1376
1377/// `d1`'s file lock, which is host-wide across processes as well as across
1378/// tasks.
1379///
1380/// Defence in depth behind [`InProcessAllocationLock`], for the configuration
1381/// `d1` documents as the one where two agents can genuinely coexist: the
1382/// platform state directory is per-account, so a service-account daemon and an
1383/// interactive `daemon run` resolve different paths and do not contend for the
1384/// single-instance lock. They do contend here if they share a state directory.
1385///
1386/// [`runner_manager_platform::lock::HostLock::acquire`] blocks the calling
1387/// thread and its own documentation names this caller: *"Async callers must wrap
1388/// it in [`tokio::task::spawn_blocking`]"*. That is what this does, and the
1389/// returned `HostLock` lives inside the guard, because dropping it is the
1390/// release.
1391#[derive(Debug, Clone)]
1392pub struct FileAllocationLock {
1393    paths: Arc<runner_manager_platform::paths::AppPaths>,
1394    wait: Duration,
1395}
1396
1397impl FileAllocationLock {
1398    #[must_use]
1399    pub const fn new(paths: Arc<runner_manager_platform::paths::AppPaths>) -> Self {
1400        Self {
1401            paths,
1402            wait: ALLOCATION_LOCK_WAIT,
1403        }
1404    }
1405
1406    #[must_use]
1407    pub const fn with_wait(mut self, wait: Duration) -> Self {
1408        self.wait = wait;
1409        self
1410    }
1411}
1412
1413#[async_trait::async_trait]
1414impl AllocationLock for FileAllocationLock {
1415    async fn acquire(&self) -> Result<AllocationGuard, AllocationLockBusy> {
1416        use runner_manager_platform::lock::{HostLock, LockKind};
1417
1418        let paths = Arc::clone(&self.paths);
1419        let wait = self.wait;
1420        let held = tokio::task::spawn_blocking(move || {
1421            HostLock::acquire(&paths, LockKind::Allocation, wait)
1422        })
1423        .await;
1424
1425        match held {
1426            Ok(Ok(lock)) => Ok(AllocationGuard::new(lock)),
1427            // A refused lock and a panicked blocking task are the same outcome
1428            // to this caller: no runtime was created and the next pass will
1429            // re-read the headroom. Neither is allowed to look like a grant.
1430            Ok(Err(_)) | Err(_) => Err(AllocationLockBusy),
1431        }
1432    }
1433}
1434
1435/// Adds the Windows/WSL recovery fence to an ordinary allocation lock, and
1436/// says on disk when allocations keep being refused.
1437///
1438/// A Windows watchdog writes its drain request before attempting the same
1439/// atomic directory claim. Therefore either this process owns the directory and
1440/// finishes the launches already in progress, or Windows owns it and no new
1441/// launch can pass. The claim is shared by every launch of this process, from
1442/// its reservation until its runner is up ([`LaunchHold`]), and released with
1443/// the last: a drain request refuses new reservations, so the launches in
1444/// progress run out and Windows gets the directory, as when there was one.
1445/// A malformed configured fence fails closed. A guest claim
1446/// whose owner is provably gone is reclaimed first; see
1447/// [`runner_manager_platform::wsl::fence`].
1448///
1449/// # Refusals that persist are recorded
1450///
1451/// A refusal is always safe for one pass, and a refusal on every pass is a host
1452/// that starts nothing while it looks healthy -- 13 days of it, once. So after
1453/// [`launch_health::BLOCKED_AFTER_DEFERRALS`] consecutive refusals spanning at
1454/// least [`launch_health::BLOCKED_AFTER`], with no launch of this process in
1455/// progress, the lock writes the `state/` record `status`, `service status`,
1456/// `wsl status` and `host doctor` read. The next granted allocation removes it.
1457/// Refusals while this process is itself launching are its own contention and
1458/// count neither way.
1459///
1460/// [`launch_health::BLOCKED_AFTER_DEFERRALS`]: runner_manager_platform::launch_health::BLOCKED_AFTER_DEFERRALS
1461/// [`launch_health::BLOCKED_AFTER`]: runner_manager_platform::launch_health::BLOCKED_AFTER
1462#[derive(Debug)]
1463pub struct WslRecoveryAllocationLock {
1464    paths: Arc<runner_manager_platform::paths::AppPaths>,
1465    inner: Arc<dyn AllocationLock>,
1466    blocked_after_refusals: u32,
1467    blocked_after: Duration,
1468    /// A quiet spell this long between refusals starts a new run.
1469    streak_gap: Duration,
1470    in_flight: Arc<std::sync::atomic::AtomicUsize>,
1471    /// The claim the launches in progress share; taking it is serialised.
1472    claim: tokio::sync::Mutex<std::sync::Weak<LaunchFenceGuard>>,
1473    refusals: Mutex<Refusals>,
1474}
1475
1476/// The refusals since the last grant.
1477#[derive(Debug, Default)]
1478struct Refusals {
1479    /// The current run of refusals: when it began, the latest one, and how
1480    /// many it holds.
1481    streak: Option<Streak>,
1482    recorded: bool,
1483}
1484
1485#[derive(Debug, Clone, Copy)]
1486struct Streak {
1487    since: chrono::DateTime<chrono::Utc>,
1488    latest: chrono::DateTime<chrono::Utc>,
1489    count: u32,
1490}
1491
1492/// Counts a launch of this process as in progress until dropped.
1493struct InFlight(Arc<std::sync::atomic::AtomicUsize>);
1494
1495impl InFlight {
1496    fn enter(count: &Arc<std::sync::atomic::AtomicUsize>) -> Self {
1497        count.fetch_add(1, Ordering::SeqCst);
1498        Self(Arc::clone(count))
1499    }
1500}
1501
1502impl Drop for InFlight {
1503    fn drop(&mut self) {
1504        self.0.fetch_sub(1, Ordering::SeqCst);
1505    }
1506}
1507
1508/// Releases the guest launch claim when the allocation ends, and says so when
1509/// it cannot: this once failed silently while a distribution started nothing
1510/// for 13 days.
1511struct LaunchFenceGuard(Option<runner_manager_platform::wsl::fence::GuestLaunchClaim>);
1512
1513impl Drop for LaunchFenceGuard {
1514    fn drop(&mut self) {
1515        if let Some(claim) = self.0.take()
1516            && let Err(error) = claim.release()
1517        {
1518            tracing::warn!(
1519                %error,
1520                "the WSL launch fence could not be released; the next launch reclaims it"
1521            );
1522        }
1523    }
1524}
1525
1526impl WslRecoveryAllocationLock {
1527    #[must_use]
1528    pub fn new(
1529        paths: Arc<runner_manager_platform::paths::AppPaths>,
1530        inner: Arc<dyn AllocationLock>,
1531    ) -> Self {
1532        use runner_manager_platform::launch_health::{BLOCKED_AFTER, BLOCKED_AFTER_DEFERRALS};
1533        Self {
1534            paths,
1535            inner,
1536            blocked_after_refusals: BLOCKED_AFTER_DEFERRALS,
1537            blocked_after: BLOCKED_AFTER,
1538            streak_gap: BLOCKED_AFTER,
1539            in_flight: Arc::default(),
1540            claim: tokio::sync::Mutex::default(),
1541            refusals: Mutex::default(),
1542        }
1543    }
1544
1545    /// Record blocked launches after `refusals` consecutive refusals spanning
1546    /// at least `after`, instead of the defaults.
1547    #[cfg(test)]
1548    const fn with_blocked_after(mut self, refusals: u32, after: Duration) -> Self {
1549        self.blocked_after_refusals = refusals;
1550        self.blocked_after = after;
1551        self
1552    }
1553
1554    /// Start a new run of refusals after a quiet spell of `gap`.
1555    #[cfg(test)]
1556    const fn with_streak_gap(mut self, gap: Duration) -> Self {
1557        self.streak_gap = gap;
1558        self
1559    }
1560
1561    fn refusals(&self) -> std::sync::MutexGuard<'_, Refusals> {
1562        self.refusals
1563            .lock()
1564            .unwrap_or_else(std::sync::PoisonError::into_inner)
1565    }
1566
1567    fn note_granted(&self) {
1568        let mut refusals = self.refusals();
1569        refusals.streak = None;
1570        if refusals.recorded {
1571            match runner_manager_platform::launch_health::clear_launches_blocked(&self.paths) {
1572                Ok(()) => refusals.recorded = false,
1573                // Kept, so the next grant tries again.
1574                Err(error) => tracing::warn!(%error, "the blocked-launch record cannot be cleared"),
1575            }
1576        }
1577    }
1578
1579    fn note_refused(&self, cause: &runner_manager_platform::launch_health::BlockCause) {
1580        use runner_manager_platform::launch_health::{Viewpoint, record_launches_blocked};
1581        use runner_manager_platform::wsl::fence::elapsed_at_least;
1582
1583        if self.in_flight.load(Ordering::SeqCst) > 0 {
1584            return;
1585        }
1586        let now = chrono::Utc::now();
1587        let mut refusals = self.refusals();
1588        // A refusal long after the previous one starts a new run: two brief
1589        // contentions days apart are not five minutes of nothing but refusals.
1590        let streak = match refusals.streak {
1591            Some(streak) if !elapsed_at_least(streak.latest, now, self.streak_gap) => Streak {
1592                latest: now,
1593                count: streak.count.saturating_add(1),
1594                ..streak
1595            },
1596            _ => Streak {
1597                since: now,
1598                latest: now,
1599                count: 1,
1600            },
1601        };
1602        refusals.streak = Some(streak);
1603        if streak.count < self.blocked_after_refusals
1604            || !elapsed_at_least(streak.since, now, self.blocked_after)
1605        {
1606            return;
1607        }
1608        let blocked = cause.blocked_since(streak.since, Viewpoint::Guest);
1609        if !refusals.recorded {
1610            tracing::warn!(
1611                refusals = streak.count,
1612                "runner launches are blocked {blocked}: {}",
1613                blocked.remedy
1614            );
1615        }
1616        // Rewritten on every refusal, so readers can tell a record nobody
1617        // refreshes -- no demand any more -- from one still being refused.
1618        match record_launches_blocked(&self.paths, &blocked, now) {
1619            Ok(()) => refusals.recorded = true,
1620            Err(error) => tracing::warn!(%error, "the blocked-launch record cannot be written"),
1621        }
1622    }
1623}
1624
1625/// Claim the guest launch fence under `root`, or say what holds it.
1626fn claim_guest_fence(
1627    root: &std::path::Path,
1628) -> Result<Arc<LaunchFenceGuard>, runner_manager_platform::launch_health::BlockCause> {
1629    use runner_manager_platform::launch_health::BlockCause;
1630    use runner_manager_platform::wsl::fence::{
1631        FENCE_DIRECTORY, FenceClaim, GuestClaimAttempt, try_claim_guest_launch,
1632    };
1633
1634    match try_claim_guest_launch(root) {
1635        Ok(GuestClaimAttempt::Claimed { claim, reclaimed }) => {
1636            if let Some(reclaimed) = reclaimed {
1637                tracing::warn!(
1638                    shared_root = %root.display(),
1639                    %reclaimed,
1640                    "reclaimed a WSL launch fence claim whose owner is gone"
1641                );
1642            }
1643            // Guarded at once, so a claim let go on the drain path is released
1644            // -- and logged when it cannot be -- like any other.
1645            Ok(Arc::new(LaunchFenceGuard(Some(claim))))
1646        }
1647        Ok(GuestClaimAttempt::Busy) => {
1648            let directory = root.join(FENCE_DIRECTORY);
1649            Err(match FenceClaim::owner(root) {
1650                Ok(owner) => BlockCause::FenceHeld { directory, owner },
1651                Err(error) => BlockCause::FenceOwnerUnreadable {
1652                    directory,
1653                    detail: error.to_string(),
1654                },
1655            })
1656        }
1657        Err(error) => {
1658            // An owner record nobody can decode stops the reclaim the same way
1659            // on every pass; name it rather than the config.
1660            Err(match FenceClaim::owner(root) {
1661                Err(owner_error) => BlockCause::FenceOwnerUnreadable {
1662                    directory: root.join(FENCE_DIRECTORY),
1663                    detail: owner_error.to_string(),
1664                },
1665                Ok(_) => BlockCause::FenceUnusable {
1666                    detail: error.to_string(),
1667                },
1668            })
1669        }
1670    }
1671}
1672
1673#[async_trait::async_trait]
1674impl AllocationLock for WslRecoveryAllocationLock {
1675    async fn acquire(&self) -> Result<AllocationGuard, AllocationLockBusy> {
1676        use runner_manager_platform::launch_health::BlockCause;
1677        use runner_manager_platform::wsl::fence::{DrainRequest, GuestRecoveryConfig};
1678
1679        let paths = Arc::clone(&self.paths);
1680        let mut shared = self.claim.lock().await;
1681        let held = shared.upgrade();
1682        let claim = tokio::task::spawn_blocking(move || {
1683            let unusable = |error: &dyn fmt::Display| BlockCause::FenceUnusable {
1684                detail: error.to_string(),
1685            };
1686            let config = match GuestRecoveryConfig::read(&paths) {
1687                Ok(Some(config)) => config,
1688                Ok(None) => return Ok(Arc::new(LaunchFenceGuard(None))),
1689                Err(error) => return Err(unusable(&error)),
1690            };
1691            let root = config.shared_root;
1692            let claim = match held {
1693                // A launch of this process already owns the directory.
1694                Some(held) => {
1695                    if let Some(Err(error)) = held.0.as_ref().map(|claim| claim.renew()) {
1696                        tracing::warn!(%error, "the WSL launch fence's age could not be renewed");
1697                    }
1698                    held
1699                }
1700                None => claim_guest_fence(&root)?,
1701            };
1702            match DrainRequest::read(&root) {
1703                Ok(None) => Ok(claim),
1704                Ok(Some(_)) => Err(BlockCause::DrainRequested { root }),
1705                Err(error) => Err(unusable(&error)),
1706            }
1707        })
1708        .await
1709        .map_err(|_| AllocationLockBusy)?;
1710
1711        let claim = match claim {
1712            Ok(claim) => claim,
1713            Err(cause) => {
1714                drop(shared);
1715                self.note_refused(&cause);
1716                return Err(AllocationLockBusy);
1717            }
1718        };
1719        if claim.0.is_some() {
1720            *shared = Arc::downgrade(&claim);
1721        }
1722        drop(shared);
1723        let Ok(inner) = self.inner.acquire().await else {
1724            self.note_refused(&BlockCause::AllocationLockBusy {
1725                path: self
1726                    .paths
1727                    .state_dir()
1728                    .join(runner_manager_platform::lock::LockKind::Allocation.file_name()),
1729            });
1730            return Err(AllocationLockBusy);
1731        };
1732        self.note_granted();
1733        let in_flight = InFlight::enter(&self.in_flight);
1734        let mut guard = AllocationGuard::new(inner);
1735        guard.launch_hold = LaunchHold {
1736            _held: Some(Arc::new((claim, in_flight))),
1737        };
1738        Ok(guard)
1739    }
1740}
1741
1742// ---------------------------------------------------------------------------
1743// Lifecycle events
1744// ---------------------------------------------------------------------------
1745
1746/// Which terminal thing happened, as a closed vocabulary.
1747///
1748/// The distinction `g2` renders: an idle exit is the accepted surplus case and
1749/// **not** a failure, and showing it as one sends an operator hunting a fault
1750/// that does not exist.
1751#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1752pub enum OutcomeKind {
1753    CompletedJob,
1754    IdleExit,
1755    Failed,
1756    Orphaned,
1757}
1758
1759impl OutcomeKind {
1760    #[must_use]
1761    pub const fn of(outcome: &AttemptOutcome) -> Self {
1762        match outcome {
1763            AttemptOutcome::CompletedJob => Self::CompletedJob,
1764            AttemptOutcome::ExitedIdleWithoutWork => Self::IdleExit,
1765            AttemptOutcome::Failed { .. } => Self::Failed,
1766            AttemptOutcome::Orphaned => Self::Orphaned,
1767        }
1768    }
1769
1770    #[must_use]
1771    pub const fn is_failure(&self) -> bool {
1772        matches!(self, Self::Failed | Self::Orphaned)
1773    }
1774
1775    #[must_use]
1776    pub const fn as_str(&self) -> &'static str {
1777        match self {
1778            Self::CompletedJob => "completed_job",
1779            Self::IdleExit => "exited_idle_without_work",
1780            Self::Failed => "failed",
1781            Self::Orphaned => "orphaned",
1782        }
1783    }
1784}
1785
1786impl fmt::Display for OutcomeKind {
1787    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1788        f.write_str(self.as_str())
1789    }
1790}
1791
1792/// A [`FailureReason`]'s variant name, with no detail.
1793///
1794/// [`FailureReason::Other`] carries a `String` that `e3` fills in, and an event
1795/// is not the place for it: `07-security.md`'s log scan runs over everything
1796/// this loop emits, and free text is the one shape that can carry a credential
1797/// past a field allow-list. The operator-facing detail reaches the journal
1798/// through `b2` and the screen through `g2`; what reaches an *event* is the
1799/// variant.
1800#[must_use]
1801pub const fn failure_reason_kind(reason: &FailureReason) -> &'static str {
1802    match reason {
1803        FailureReason::JitRequestFailed => "jit_request_failed",
1804        FailureReason::JitExpired => "jit_expired",
1805        FailureReason::RunnerPackageUnverified => "runner_package_unverified",
1806        FailureReason::RunnerVersionRejected => "runner_version_rejected",
1807        FailureReason::ProcessStartFailed => "process_start_failed",
1808        FailureReason::ProcessExitedUnexpectedly => "process_exited_unexpectedly",
1809        FailureReason::RegistrationTimedOut => "registration_timed_out",
1810        FailureReason::TerminatedAfterRegistrationTimeout => {
1811            "terminated_after_registration_timeout"
1812        }
1813        FailureReason::IsolationProvider(category) => match category {
1814            IsolationProviderFailure::Unsupported => "isolation_provider_unsupported",
1815            IsolationProviderFailure::NotInstalled => "isolation_provider_not_installed",
1816            IsolationProviderFailure::PermissionDenied => "isolation_provider_permission_denied",
1817            IsolationProviderFailure::ImageUnavailableOrIncompatible => {
1818                "isolation_image_unavailable"
1819            }
1820            IsolationProviderFailure::DiskQuotaUnavailable => "isolation_disk_quota_unavailable",
1821            IsolationProviderFailure::Degraded => "isolation_provider_degraded",
1822            IsolationProviderFailure::RuntimeOperationFailed => {
1823                "isolation_runtime_operation_failed"
1824            }
1825            IsolationProviderFailure::OwnershipMismatch => "isolation_ownership_mismatch",
1826            IsolationProviderFailure::UnsafeRuntimePath => "isolation_unsafe_runtime_path",
1827            IsolationProviderFailure::JitHandoffRejected => "isolation_jit_handoff_rejected",
1828        },
1829        FailureReason::WorkspaceCleanupDeferred => "ephemeral_workspace_could_not_be_removed",
1830        FailureReason::HostUnfit => "host_unfit",
1831        FailureReason::Other(_) => "other",
1832    }
1833}
1834
1835/// Fixed, credential-free reasons a queued job cannot route to one profile.
1836#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1837pub enum RoutingRefusal {
1838    OverlappingProfiles,
1839}
1840
1841impl RoutingRefusal {
1842    #[must_use]
1843    pub const fn as_str(self) -> &'static str {
1844        match self {
1845            Self::OverlappingProfiles => "overlapping_profile_matches",
1846        }
1847    }
1848}
1849
1850impl fmt::Display for RoutingRefusal {
1851    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1852        f.write_str(match self {
1853            Self::OverlappingProfiles => {
1854                "repair overlapping profile labels or require a unique profile selector"
1855            }
1856        })
1857    }
1858}
1859
1860/// What `g2`'s activity view and the local log sink see.
1861///
1862/// **Every field is an identifier, a count, a duration, or a `&'static str`
1863/// drawn from a closed set.** There is no `String` anywhere in this enum, which
1864/// is what makes "no emitted event contains a token, a JIT blob, or a credential
1865/// header" a property of the type rather than a discipline each call site has to
1866/// keep. `tests::no_emitted_event_can_carry_a_credential` renders every variant
1867/// through `d1`'s scrubber and asserts nothing changes, with a positive control
1868/// so the assertion cannot pass vacuously.
1869#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1870pub enum LifecycleEvent {
1871    /// A demand poll answered for one target.
1872    DemandObserved {
1873        policy: PolicyId,
1874        /// Queued jobs this policy's routing labels match. The number clamped.
1875        demand: u32,
1876        /// Queued jobs whose required labels this policy does not carry.
1877        ///
1878        /// Never demand. Reported because the difference between this and
1879        /// `demand` is the whole value of the label filtering, and an operator
1880        /// wondering why a busy repository started no runners is owed it.
1881        not_matched: u32,
1882        /// Queued jobs whose `runs-on` could not be resolved statically.
1883        ///
1884        /// `b1` requires these be "reported as unresolvable rather than silently
1885        /// counted or silently dropped": counting one would start a runner for a
1886        /// job that may not be ours, and dropping it would hide a workflow this
1887        /// host can never serve. A count rather than the reasons themselves
1888        /// because this type is `Copy`, and `c4` logs the reasons where it
1889        /// builds them.
1890        unresolvable: u32,
1891        /// `false` when the count is a floor rather than a total.
1892        complete: bool,
1893    },
1894    /// A queued job cannot be assigned unambiguously to this target's profiles.
1895    RoutingRefused {
1896        policy: PolicyId,
1897        reason: RoutingRefusal,
1898        count: u32,
1899    },
1900    /// A target could not be polled, so its policies start nothing this pass.
1901    TargetUnreadable {
1902        policy: PolicyId,
1903        reason: &'static str,
1904    },
1905    /// One policy's share of the pass.
1906    Allocated {
1907        policy: PolicyId,
1908        demand: u32,
1909        desired: u16,
1910        active_owned: u16,
1911        headroom: u16,
1912        to_start: u16,
1913        limiting: LimitingFactor,
1914    },
1915    /// A monitor-only policy was skipped entirely, before any demand request
1916    /// was issued for it (D19).
1917    MonitorOnlySkipped { policy: PolicyId },
1918    /// One runtime was created and one runner started.
1919    RunnerStarted {
1920        policy: PolicyId,
1921        attempt: AttemptId,
1922    },
1923    /// One runner could not be started.
1924    RunnerStartFailed {
1925        policy: PolicyId,
1926        reason: &'static str,
1927    },
1928    /// The allocation lock was not free, so `count` runners this policy was
1929    /// granted were not created this pass.
1930    AllocationDeferred { policy: PolicyId, count: u16 },
1931    /// The host's attempt set could not be read at all.
1932    ///
1933    /// Distinct from an empty set on purpose, and the whole reason
1934    /// [`RunnerLauncher::attempts`] is fallible: the two produce the same
1935    /// *number* and demand opposite actions.
1936    AttemptsUnreadable { reason: &'static str },
1937    /// The ceiling is unknown; this does not imply that the attempt set is unreadable.
1938    HostCapacityUnreadable { reason: HostCapacityError },
1939    /// A terminal attempt's runtime was removed.
1940    AttemptCleaned {
1941        policy: PolicyId,
1942        attempt: AttemptId,
1943        outcome: OutcomeKind,
1944    },
1945    /// A terminal attempt's runtime could not be removed. It will be retried on
1946    /// the next pass, and this is what keeps that retry from being silent.
1947    AttemptCleanFailed {
1948        policy: PolicyId,
1949        attempt: AttemptId,
1950        reason: &'static str,
1951    },
1952    /// Scale-down declined to remove a runner that is executing a job.
1953    ScaleDownRefused {
1954        policy: PolicyId,
1955        attempt: AttemptId,
1956    },
1957    /// When the next poll is, and why then.
1958    PollScheduled { retry_in_ms: u64, pace: PollPace },
1959}
1960
1961impl LifecycleEvent {
1962    /// A fixed name, for the `event` field `d1`'s sink allows verbatim.
1963    #[must_use]
1964    pub const fn name(&self) -> &'static str {
1965        match self {
1966            Self::DemandObserved { .. } => "demand_observed",
1967            Self::RoutingRefused { .. } => "routing_refused",
1968            Self::TargetUnreadable { .. } => "target_unreadable",
1969            Self::Allocated { .. } => "allocated",
1970            Self::MonitorOnlySkipped { .. } => "monitor_only_skipped",
1971            Self::RunnerStarted { .. } => "runner_started",
1972            Self::RunnerStartFailed { .. } => "runner_start_failed",
1973            Self::AllocationDeferred { .. } => "allocation_deferred",
1974            Self::AttemptsUnreadable { .. } => "attempts_unreadable",
1975            Self::HostCapacityUnreadable { .. } => "host_capacity_unreadable",
1976            Self::AttemptCleaned { .. } => "attempt_cleaned",
1977            Self::AttemptCleanFailed { .. } => "attempt_clean_failed",
1978            Self::ScaleDownRefused { .. } => "scale_down_refused",
1979            Self::PollScheduled { .. } => "poll_scheduled",
1980        }
1981    }
1982
1983    /// Which policy this event is about.
1984    #[must_use]
1985    pub const fn policy(&self) -> Option<PolicyId> {
1986        match self {
1987            Self::DemandObserved { policy, .. }
1988            | Self::RoutingRefused { policy, .. }
1989            | Self::TargetUnreadable { policy, .. }
1990            | Self::Allocated { policy, .. }
1991            | Self::MonitorOnlySkipped { policy }
1992            | Self::RunnerStarted { policy, .. }
1993            | Self::RunnerStartFailed { policy, .. }
1994            | Self::AllocationDeferred { policy, .. }
1995            | Self::AttemptCleaned { policy, .. }
1996            | Self::AttemptCleanFailed { policy, .. }
1997            | Self::ScaleDownRefused { policy, .. } => Some(*policy),
1998            Self::PollScheduled { .. }
1999            | Self::AttemptsUnreadable { .. }
2000            | Self::HostCapacityUnreadable { .. } => None,
2001        }
2002    }
2003}
2004
2005impl fmt::Display for LifecycleEvent {
2006    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2007        match self {
2008            Self::DemandObserved {
2009                policy,
2010                demand,
2011                not_matched,
2012                unresolvable,
2013                complete,
2014            } => write!(
2015                f,
2016                "policy {policy}: {demand} queued jobs for this host{}{}{}",
2017                if *not_matched == 0 {
2018                    String::new()
2019                } else {
2020                    format!(", {not_matched} for other labels")
2021                },
2022                if *unresolvable == 0 {
2023                    String::new()
2024                } else {
2025                    format!(", {unresolvable} with an unresolvable `runs-on`")
2026                },
2027                if *complete {
2028                    ""
2029                } else {
2030                    " (a floor, not a total)"
2031                }
2032            ),
2033            Self::TargetUnreadable { policy, reason } => {
2034                write!(f, "policy {policy}: target unreadable ({reason})")
2035            }
2036            Self::RoutingRefused {
2037                policy,
2038                reason,
2039                count,
2040            } => write!(f, "policy {policy}: {count} queued jobs blocked: {reason}"),
2041            Self::Allocated {
2042                policy,
2043                demand,
2044                desired,
2045                active_owned,
2046                headroom,
2047                to_start,
2048                limiting,
2049            } => write!(
2050                f,
2051                "policy {policy}: demand {demand}, desired {desired}, {active_owned} in \
2052                 flight, {headroom} free on this host, starting {to_start} ({limiting})"
2053            ),
2054            Self::MonitorOnlySkipped { policy } => {
2055                write!(f, "policy {policy}: monitor-only, skipped")
2056            }
2057            Self::RunnerStarted { policy, attempt } => {
2058                write!(f, "policy {policy}: started attempt {attempt}")
2059            }
2060            Self::RunnerStartFailed { policy, reason } => {
2061                write!(f, "policy {policy}: could not start a runner ({reason})")
2062            }
2063            Self::AllocationDeferred { policy, count } => write!(
2064                f,
2065                "policy {policy}: the allocation lock was held; {count} granted runners \
2066                 were not created"
2067            ),
2068            Self::AttemptsUnreadable { reason } => write!(
2069                f,
2070                "the host's attempt set could not be read ({reason}); nothing was started, \
2071                 and this is not the same as the host being idle"
2072            ),
2073            Self::HostCapacityUnreadable { reason } => write!(
2074                f,
2075                "the host capacity could not be read ({}); new launches wait for readable host settings",
2076                reason.reason()
2077            ),
2078            Self::AttemptCleaned {
2079                policy,
2080                attempt,
2081                outcome,
2082            } => write!(f, "policy {policy}: cleaned attempt {attempt} ({outcome})"),
2083            Self::AttemptCleanFailed {
2084                policy,
2085                attempt,
2086                reason,
2087            } => write!(
2088                f,
2089                "policy {policy}: attempt {attempt} could not be cleaned ({reason}); it \
2090                 will be retried"
2091            ),
2092            Self::ScaleDownRefused { policy, attempt } => write!(
2093                f,
2094                "policy {policy}: attempt {attempt} is executing a job and was not removed"
2095            ),
2096            Self::PollScheduled { retry_in_ms, pace } => {
2097                write!(f, "next poll in {retry_in_ms}ms ({pace})")
2098            }
2099        }
2100    }
2101}
2102
2103/// Where lifecycle events go.
2104pub trait EventSink: fmt::Debug + Send + Sync {
2105    fn emit(&self, event: LifecycleEvent);
2106}
2107
2108/// Discards everything. For callers that only want the report.
2109#[derive(Debug, Clone, Copy, Default)]
2110pub struct NoEvents;
2111
2112impl EventSink for NoEvents {
2113    fn emit(&self, _event: LifecycleEvent) {}
2114}
2115
2116/// The local log sink, through `d1`'s redacting layer.
2117///
2118/// Every field name below is on
2119/// [`runner_manager_platform::logging::ALLOWED_FIELDS`]; anything else would be
2120/// replaced with `[redacted]` and the line would lose its meaning rather than
2121/// its safety. `tests::every_field_name_this_sink_emits_is_one_d1_allows` keeps
2122/// that true.
2123#[derive(Debug, Clone, Copy, Default)]
2124pub struct TracingEvents;
2125
2126impl EventSink for TracingEvents {
2127    fn emit(&self, event: LifecycleEvent) {
2128        let name = event.name();
2129        match event {
2130            LifecycleEvent::DemandObserved {
2131                policy,
2132                demand,
2133                not_matched,
2134                unresolvable,
2135                complete,
2136            } => {
2137                // An empty reading is the idle steady state, and the idle
2138                // interval repeats it every few seconds per target: at `info`
2139                // it would be most of the log. Anything worth reading is still
2140                // `info`.
2141                if demand == 0 && not_matched == 0 && unresolvable == 0 && complete {
2142                    tracing::debug!(
2143                        event = name,
2144                        policy_id = %policy,
2145                        demand,
2146                        not_matched,
2147                        unresolvable,
2148                        count = u64::from(complete),
2149                    );
2150                } else {
2151                    tracing::info!(
2152                        event = name,
2153                        policy_id = %policy,
2154                        demand,
2155                        not_matched,
2156                        unresolvable,
2157                        count = u64::from(complete),
2158                    );
2159                }
2160                // There is deliberately no `warn!` here for the "demand is zero
2161                // but jobs were not matched" shape, though it is the one this
2162                // change introduced: before demand was filtered, a repository
2163                // with work in it always produced some, and now a policy whose
2164                // labels do not cover its jobs produces none.
2165                //
2166                // The reason is that the shape is indistinguishable from a
2167                // healthy one. A repository served by a Windows host and a macOS
2168                // host has the other host's jobs queued in it constantly, so
2169                // each agent would warn on every poll about work that is being
2170                // served correctly by the other machine. Telling the two apart
2171                // needs to know whether this policy has *ever* matched anything,
2172                // which is state across polls that this loop does not keep.
2173                //
2174                // What an operator gets instead is the `not_matched` count, on
2175                // this event and in its `Display`, which `g2` renders. "0 queued
2176                // jobs for this host, 5 for other labels" is the diagnosis; a
2177                // warning that fired on every healthy minute would be the kind
2178                // nobody reads.
2179            }
2180            LifecycleEvent::TargetUnreadable { policy, reason } => {
2181                tracing::warn!(event = name, policy_id = %policy, reason);
2182            }
2183            LifecycleEvent::RoutingRefused {
2184                policy,
2185                reason,
2186                count,
2187            } => tracing::warn!(
2188                event = name,
2189                policy_id = %policy,
2190                reason = reason.as_str(),
2191                count,
2192                "{reason}"
2193            ),
2194            LifecycleEvent::Allocated {
2195                policy,
2196                demand,
2197                desired,
2198                active_owned,
2199                headroom,
2200                to_start,
2201                limiting,
2202            } if demand == 0 && to_start == 0 && active_owned == 0 => tracing::debug!(
2203                // Nothing wanted, nothing held, nothing started: the idle
2204                // steady state, see `DemandObserved` above.
2205                event = name,
2206                policy_id = %policy,
2207                demand,
2208                desired,
2209                capacity = active_owned,
2210                headroom,
2211                count = to_start,
2212                reason = %limiting,
2213            ),
2214            LifecycleEvent::Allocated {
2215                policy,
2216                demand,
2217                desired,
2218                active_owned,
2219                headroom,
2220                to_start,
2221                limiting,
2222            } => tracing::info!(
2223                event = name,
2224                policy_id = %policy,
2225                demand,
2226                desired,
2227                capacity = active_owned,
2228                headroom,
2229                count = to_start,
2230                reason = %limiting,
2231            ),
2232            LifecycleEvent::MonitorOnlySkipped { policy } => {
2233                tracing::debug!(event = name, policy_id = %policy, mode = "monitor_only");
2234            }
2235            LifecycleEvent::RunnerStarted { policy, attempt } => {
2236                tracing::info!(event = name, policy_id = %policy, attempt_id = %attempt);
2237            }
2238            LifecycleEvent::RunnerStartFailed { policy, reason } => {
2239                tracing::warn!(event = name, policy_id = %policy, reason);
2240            }
2241            LifecycleEvent::AllocationDeferred { policy, count } => {
2242                // Contention is normally brief, but a configured WSL recovery
2243                // fence whose shared root is unwritable reaches the same
2244                // fail-closed result forever. Demand was already observed and
2245                // capacity was already granted, so hiding this at DEBUG makes
2246                // a host claim healthy while it starts nothing.
2247                tracing::warn!(
2248                    event = name,
2249                    policy_id = %policy,
2250                    reason = "allocation_lock_unavailable",
2251                    count
2252                );
2253            }
2254            LifecycleEvent::AttemptsUnreadable { reason } => {
2255                tracing::warn!(event = name, reason);
2256            }
2257            LifecycleEvent::HostCapacityUnreadable { reason } => {
2258                tracing::warn!(event = name, reason = reason.reason());
2259            }
2260            LifecycleEvent::AttemptCleaned {
2261                policy,
2262                attempt,
2263                outcome,
2264            } => tracing::info!(
2265                event = name,
2266                policy_id = %policy,
2267                attempt_id = %attempt,
2268                outcome = outcome.as_str(),
2269            ),
2270            LifecycleEvent::AttemptCleanFailed {
2271                policy,
2272                attempt,
2273                reason,
2274            } => tracing::warn!(
2275                event = name,
2276                policy_id = %policy,
2277                attempt_id = %attempt,
2278                reason,
2279            ),
2280            LifecycleEvent::ScaleDownRefused { policy, attempt } => tracing::info!(
2281                event = name,
2282                policy_id = %policy,
2283                attempt_id = %attempt,
2284                attempt_state = "busy",
2285            ),
2286            LifecycleEvent::PollScheduled { retry_in_ms, pace } => {
2287                if pace == PollPace::Idle {
2288                    tracing::debug!(event = name, retry_in_ms, state = pace.as_str());
2289                } else {
2290                    tracing::info!(event = name, retry_in_ms, state = pace.as_str());
2291                }
2292            }
2293        }
2294    }
2295}
2296
2297/// Keeps every event, in order.
2298///
2299/// `g2`'s activity view is a reader of this, and so is every test below.
2300#[derive(Debug, Default)]
2301pub struct EventLog {
2302    events: Mutex<Vec<LifecycleEvent>>,
2303}
2304
2305impl EventLog {
2306    #[must_use]
2307    pub fn new() -> Self {
2308        Self::default()
2309    }
2310
2311    #[must_use]
2312    pub fn events(&self) -> Vec<LifecycleEvent> {
2313        self.events.lock().map(|e| e.clone()).unwrap_or_default()
2314    }
2315
2316    /// How many events of one name were emitted.
2317    #[must_use]
2318    pub fn count_of(&self, name: &str) -> usize {
2319        self.events()
2320            .iter()
2321            .filter(|event| event.name() == name)
2322            .count()
2323    }
2324}
2325
2326impl EventSink for EventLog {
2327    fn emit(&self, event: LifecycleEvent) {
2328        if let Ok(mut events) = self.events.lock() {
2329            events.push(event);
2330        }
2331    }
2332}
2333
2334/// Both sinks at once: the log sink for the operator's file, the buffer for
2335/// `g2`'s screen.
2336#[derive(Debug)]
2337pub struct TeeEvents(pub Arc<dyn EventSink>, pub Arc<dyn EventSink>);
2338
2339impl EventSink for TeeEvents {
2340    fn emit(&self, event: LifecycleEvent) {
2341        self.0.emit(event);
2342        self.1.emit(event);
2343    }
2344}
2345
2346// ---------------------------------------------------------------------------
2347// The reconciler
2348// ---------------------------------------------------------------------------
2349
2350/// Where a policy's demand comes from when the optional coordinator places
2351/// its runners (coordinator stage 3).
2352///
2353/// A profile carrying a shared pool label (`rm-pool-linux`, ...) on a target
2354/// the coordinator covers starts runners only for the launches the
2355/// coordinator placed on this host and the host accepted — never from its own
2356/// reading of the queue, which every host in the pool sees the same way. This
2357/// is a *count*, not a job: a runner still takes whatever matching job GitHub
2358/// hands it, nothing here reserves one, and the number still goes through
2359/// [`HostAllocator`] and every ceiling, exactly as a polled count does.
2360pub trait DemandDirector: fmt::Debug + Send + Sync {
2361    /// `Some(n)`: `policy`'s demand this pass is `n`, not its reading.
2362    /// `None`: its own reading, as without a coordinator.
2363    fn directed_demand(&self, policy: &ScalePolicy) -> Option<u32>;
2364
2365    /// A runner was started for `policy` (as soon as it exists, whether or
2366    /// not its demand was directed).
2367    fn launched(&self, policy: &ScalePolicy, attempt: AttemptId);
2368
2369    /// No runner could be started for `policy`, for a real reason: not a
2370    /// runner still being brought up. `attempt` is the one reported through
2371    /// [`Self::launched`] whose bring-up failed, if it got that far: what it was
2372    /// reported for is waiting again, to be retried rather than placed anew.
2373    fn start_failed(&self, _policy: &ScalePolicy, _attempt: Option<AttemptId>) {}
2374
2375    /// Whether an older launch of another policy on this host still has no
2376    /// runner, so `policy` should let its target's loop reserve first.
2377    fn older_waiting(&self, _policy: &ScalePolicy) -> bool {
2378        false
2379    }
2380}
2381
2382/// How long a pass lets older launches of other policies reserve first.
2383const OLDER_LAUNCH_TURN: Duration = Duration::from_secs(2);
2384const OLDER_LAUNCH_POLL: Duration = Duration::from_millis(100);
2385
2386/// A runner reserved under the allocation lock, to bring up outside it.
2387struct Reserved {
2388    policy: ScalePolicy,
2389    attempt: RunnerAttempt,
2390    lock_wait: Duration,
2391    reserved_at: std::time::Instant,
2392    /// Let go when this runner is up or has failed.
2393    _hold: LaunchHold,
2394}
2395
2396/// Every policy's demand is its own reading: no coordinator places runners.
2397#[derive(Debug, Default, Clone, Copy)]
2398pub struct OwnDemand;
2399
2400impl DemandDirector for OwnDemand {
2401    fn directed_demand(&self, _policy: &ScalePolicy) -> Option<u32> {
2402        None
2403    }
2404
2405    fn launched(&self, _policy: &ScalePolicy, _attempt: AttemptId) {}
2406}
2407
2408/// Everything one reconciler needs, written down at the call site.
2409///
2410/// A struct rather than seven positional arguments, for the reason `b1` gives at
2411/// `PersistedAttempt`: several of these are `Arc<dyn …>` and transposing two of
2412/// them type-checks. Construct it with a struct literal so every port is named.
2413pub struct ReconcilerPorts {
2414    pub demand: Arc<dyn DemandSource>,
2415    pub launcher: Arc<dyn RunnerLauncher>,
2416    pub lock: Arc<dyn AllocationLock>,
2417    pub directory: Arc<dyn RepositoryDirectory>,
2418    pub clock: Arc<dyn Clock>,
2419    pub jitter: Arc<dyn Jitter>,
2420    pub events: Arc<dyn EventSink>,
2421}
2422
2423impl fmt::Debug for ReconcilerPorts {
2424    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2425        f.debug_struct("ReconcilerPorts").finish_non_exhaustive()
2426    }
2427}
2428
2429/// What one reconciliation pass did.
2430///
2431/// `started` and the allocations are reported separately on purpose: an
2432/// allocation is what the pass *decided* under the lock, and `started` is what
2433/// actually came up. They differ when a launch fails or when the lock was held,
2434/// and collapsing them would hide both.
2435#[derive(Debug, Clone, Default)]
2436pub struct ReconcileReport {
2437    /// One entry per policy that got as far as being allocated for.
2438    pub allocations: Vec<Allocation>,
2439    /// Policies skipped because they are monitor-only (D19).
2440    pub monitor_only: Vec<PolicyId>,
2441    /// Policies whose target could not be polled this pass.
2442    pub unreadable: Vec<PolicyId>,
2443    /// Policies whose target GitHub actually answered for this pass.
2444    ///
2445    /// The counterpart to [`Self::unreadable`], and the only honest evidence
2446    /// that this host reached GitHub at all. [`Self::allocations`] is not: a
2447    /// policy this host does not own is allocated for with no demand and
2448    /// without any target being polled, so a pass where every poll failed can
2449    /// still end with allocations in it.
2450    pub targets_read: u16,
2451    /// Runners actually started.
2452    pub started: u16,
2453    /// Pre-acceptance attempts routed back through this pass's ordinary
2454    /// demand/capacity decision.
2455    pub replacement_intents: u16,
2456    /// Terminal attempts whose runtime was removed.
2457    pub cleaned: u16,
2458    /// Of those, the surplus case: registered, got no job, exited on its idle
2459    /// timeout. **Not** a failure.
2460    pub idle_exits: u16,
2461    /// Of those, the ones an operator should look at.
2462    pub failures: u16,
2463    /// Runners this pass was granted but did not start because the allocation
2464    /// lock was held.
2465    ///
2466    /// **Grants, not policies.** It used to be incremented once per
2467    /// `start_runners` call that met a held lock, so a policy that launched two
2468    /// of five and then lost the lock reported `1` while three runners went
2469    /// unstarted -- a number that agreed with neither its own name nor its
2470    /// documentation.
2471    pub deferred: u16,
2472    /// Times the host's attempt set could not be read this pass.
2473    ///
2474    /// Non-zero means the pass decided less than it looks like it decided: a
2475    /// policy whose attempt set was unreadable started nothing and is *not* in
2476    /// [`Self::allocations`], because there was no set to compute an allocation
2477    /// from. It is not the same as the host being idle, which is the whole
2478    /// reason [`RunnerLauncher::attempts`] is fallible.
2479    ///
2480    /// **A count, where [`Self::unreadable`] is a `Vec<PolicyId>`, and that
2481    /// asymmetry is deliberate.** An unreadable *target* is a fact about one
2482    /// policy's GitHub target; an unreadable *attempt set* is a fact about this
2483    /// host's journal, which no policy owns — two of the three paths that reach
2484    /// it (`clean_terminal_attempts` and `scale_down`) have no policy in hand at
2485    /// all. Naming policies here would mean either inventing an owner for a
2486    /// host-wide failure or reporting a partial list, and both read as more
2487    /// precision than there is. The pass is distinguishable from an idle one,
2488    /// which is what the field exists for; the per-policy attribution is not
2489    /// available, and is recorded as missing rather than faked.
2490    pub attempts_unreadable: u16,
2491    /// Allocation checks blocked by a missing or unreadable host ceiling.
2492    pub host_capacity_unreadable: u16,
2493    /// Terminal attempts whose runtime could not be removed. Retried next pass.
2494    pub clean_failures: u16,
2495    /// The most severe failure across the targets polled, when there was one.
2496    pub failure: Option<RefreshState>,
2497    /// What to display while GitHub is unreachable, including how long the
2498    /// outage has run and therefore whether queued work has already been lost.
2499    pub offline: Option<OfflineState>,
2500    /// When to poll next, and why then.
2501    pub next_poll: NextPoll,
2502    /// Demand requests this pass projected against the shared hourly ceiling.
2503    pub demand_requests: u32,
2504}
2505
2506impl ReconcileReport {
2507    /// Whether this pass actually reached GitHub, which is the only thing that
2508    /// entitles it to write a `last GitHub contact`.
2509    ///
2510    /// # Positive evidence, because the absence of a failure is not evidence
2511    ///
2512    /// The record used to be written whenever [`Self::failure`] was `None`, on
2513    /// the belief that an unauthorized target lands in [`Self::unreadable`]
2514    /// rather than in `failure`. **That belief is wrong.** `unreadable` is
2515    /// pushed only from the `PollOutcome::Failed` arm, `failure` is the maximum
2516    /// over every `Failed` reading, and `RefreshState::Unauthorized` scores 2 —
2517    /// so a non-empty `unreadable` always implies `failure.is_some()`, and
2518    /// guarding on both would have changed nothing at all.
2519    ///
2520    /// The path that really writes a contact record without touching GitHub is
2521    /// a pass that polls **nothing**: every policy draining, owned by another
2522    /// host, or monitor-only. `pollable` is then empty, no reading exists, no
2523    /// failure is computed, and the old guard passed. That is how
2524    /// `service status` can answer `healthy` on a host doing nothing at all.
2525    ///
2526    /// So this asks for evidence rather than for the absence of a complaint. A
2527    /// pass with nothing to ask reaches nobody and records nothing, which is
2528    /// what `never` in `service status` is for.
2529    ///
2530    /// Conservative on purpose: `repositories.scope_for` is a real request that
2531    /// can succeed before a demand poll fails, and it is not counted. Contact
2532    /// that cannot be proven is not claimed.
2533    #[must_use]
2534    pub const fn reached_github(&self) -> bool {
2535        self.targets_read > 0
2536    }
2537}
2538
2539impl Default for NextPoll {
2540    fn default() -> Self {
2541        Self {
2542            delay: PollSchedule::floor(),
2543            pace: PollPace::Nominal,
2544        }
2545    }
2546}
2547
2548impl ReconcileReport {
2549    /// Whether GitHub was unreachable this pass.
2550    #[must_use]
2551    pub fn is_offline(&self) -> bool {
2552        matches!(self.failure, Some(RefreshState::Offline))
2553    }
2554
2555    /// The offline state to display, when this pass was one.
2556    #[must_use]
2557    pub const fn offline_state(&self) -> Option<&OfflineState> {
2558        self.offline.as_ref()
2559    }
2560
2561    /// Attempts this pass created. The idle-host assertion reads this.
2562    #[must_use]
2563    pub const fn starts_nothing(&self) -> bool {
2564        self.started == 0
2565    }
2566}
2567
2568/// What one scale-down request did.
2569#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
2570pub struct ScaleDownReport {
2571    /// Terminal attempts whose runtime was removed.
2572    pub removed: u16,
2573    /// Attempts executing a job. **Removed nothing, left `busy`.**
2574    pub refused_busy: u16,
2575    /// Terminal attempts whose runtime could not be removed.
2576    pub clean_failures: u16,
2577    /// Live attempts that are not yet busy. Also removed nothing: capacity is
2578    /// reclaimed only when an attempt reaches a terminal state.
2579    pub retained: u16,
2580    /// The host's attempt set could not be read, so **every other field here is
2581    /// meaningless** rather than zero.
2582    ///
2583    /// This is the same distinction [`ReconcileReport::attempts_unreadable`]
2584    /// draws, and it is here for the same reason: a default
2585    /// [`ScaleDownReport`] and a scale-down that could not see the machine are
2586    /// both all-zeros, and they mean opposite things — "there was nothing to
2587    /// reclaim" against "we do not know what there was". Check
2588    /// [`Self::is_conclusive`] before reading a zero as an answer.
2589    pub attempts_unreadable: bool,
2590}
2591
2592impl ScaleDownReport {
2593    /// Whether the counts here describe the machine at all.
2594    ///
2595    /// `false` means the attempt set could not be read, so every zero is
2596    /// "unknown" rather than "none".
2597    #[must_use]
2598    pub const fn is_conclusive(&self) -> bool {
2599        !self.attempts_unreadable
2600    }
2601}
2602
2603/// How long a failed cleanup of one attempt waits before it is tried again,
2604/// doubling per consecutive failure up to [`CLEAN_RETRY_MAXIMUM`].
2605///
2606/// A runtime that cannot be removed used to be retried on every pass, about
2607/// every six seconds, and each failure logs a warning: one stuck Windows
2608/// workspace wrote some 15,000 of them a day. The retry is still real; it is
2609/// just no longer a log flood.
2610const CLEAN_RETRY_INITIAL: Duration = Duration::from_secs(30);
2611
2612/// The longest a failing cleanup waits between tries.
2613const CLEAN_RETRY_MAXIMUM: Duration = Duration::from_secs(30 * 60);
2614
2615/// One attempt's consecutive cleanup failures, and when it may be tried again.
2616#[derive(Debug, Clone, Copy)]
2617struct CleanRetry {
2618    failures: u32,
2619    not_before: Timestamp,
2620}
2621
2622/// Per-attempt cleanup backoff. One each in the [`Reconciler`]'s sweep of
2623/// concluded attempts and in the lifecycle launcher's own supervision pass,
2624/// the two places a failed cleanup is retried.
2625#[derive(Debug, Default)]
2626pub(crate) struct CleanBackoff {
2627    retries: BTreeMap<AttemptId, CleanRetry>,
2628}
2629
2630impl CleanBackoff {
2631    /// The doubling-with-a-ceiling schedule; the attempt bound is unused.
2632    const SCHEDULE: crate::lifecycle::RetryPolicy =
2633        crate::lifecycle::RetryPolicy::bounded(u32::MAX, CLEAN_RETRY_INITIAL, CLEAN_RETRY_MAXIMUM);
2634
2635    /// Whether `attempt`'s cleanup should be tried at `now`.
2636    pub(crate) fn is_due(&self, attempt: AttemptId, now: Timestamp) -> bool {
2637        self.retries
2638            .get(&attempt)
2639            .is_none_or(|retry| now >= retry.not_before)
2640    }
2641
2642    /// Records a failed cleanup and schedules the next try.
2643    pub(crate) fn failed(&mut self, attempt: AttemptId, now: Timestamp) {
2644        let failures = self
2645            .retries
2646            .get(&attempt)
2647            .map_or(1, |retry| retry.failures.saturating_add(1));
2648        let delay = Self::SCHEDULE.delay(failures);
2649        self.retries.insert(
2650            attempt,
2651            CleanRetry {
2652                failures,
2653                // At most `CLEAN_RETRY_MAXIMUM`, so always representable.
2654                not_before: now + chrono::Duration::from_std(delay).unwrap_or_default(),
2655            },
2656        );
2657    }
2658
2659    /// Forgets `attempt`: it was cleaned.
2660    pub(crate) fn succeeded(&mut self, attempt: AttemptId) {
2661        self.retries.remove(&attempt);
2662    }
2663
2664    /// Forgets every attempt not in `present`, so the map cannot outgrow the
2665    /// journal.
2666    fn retain(&mut self, present: &BTreeSet<AttemptId>) {
2667        self.retries.retain(|attempt, _| present.contains(attempt));
2668    }
2669}
2670
2671/// The reconciliation loop.
2672///
2673/// One per target, as `f3` runs them; they share a [`RunnerLauncher`] and an
2674/// [`AllocationLock`], which is what keeps the host ceiling true across all of
2675/// them.
2676#[derive(Debug)]
2677pub struct Reconciler {
2678    host: Host,
2679    demand: Arc<dyn DemandSource>,
2680    launcher: Arc<dyn RunnerLauncher>,
2681    lock: Arc<dyn AllocationLock>,
2682    repositories: RepositoryCache,
2683    clock: Arc<dyn Clock>,
2684    jitter: Arc<dyn Jitter>,
2685    events: Arc<dyn EventSink>,
2686    schedule: PollSchedule,
2687    clean_backoff: CleanBackoff,
2688    director: Arc<dyn DemandDirector>,
2689}
2690
2691impl Reconciler {
2692    /// Build a reconciler polling at the host's configured interval.
2693    #[must_use]
2694    pub fn new(host: Host, ports: ReconcilerPorts) -> Self {
2695        let interval = host.refresh_interval;
2696        let repositories = RepositoryCache::new(
2697            Arc::clone(&ports.directory),
2698            Arc::clone(&ports.clock),
2699            interval,
2700        );
2701        Self {
2702            host,
2703            demand: ports.demand,
2704            launcher: ports.launcher,
2705            lock: ports.lock,
2706            repositories,
2707            clock: ports.clock,
2708            jitter: ports.jitter,
2709            events: ports.events,
2710            schedule: PollSchedule::new(interval),
2711            clean_backoff: CleanBackoff::default(),
2712            director: Arc::new(OwnDemand),
2713        }
2714    }
2715
2716    /// Lets `director` replace a policy's polled demand with the launches the
2717    /// coordinator placed here. See [`DemandDirector`].
2718    #[must_use]
2719    pub fn with_director(mut self, director: Arc<dyn DemandDirector>) -> Self {
2720        self.director = director;
2721        self
2722    }
2723
2724    #[must_use]
2725    pub const fn host(&self) -> &Host {
2726        &self.host
2727    }
2728
2729    #[must_use]
2730    pub const fn schedule(&self) -> &PollSchedule {
2731        &self.schedule
2732    }
2733
2734    /// Adopt intervals an operator changed while the loop runs.
2735    pub fn set_intervals(&mut self, active: RefreshInterval, idle: Duration) {
2736        self.host.refresh_interval = active;
2737        self.schedule.set_intervals(active, idle);
2738    }
2739
2740    /// The repository-list cache, so `f1` can report what it has spent.
2741    #[must_use]
2742    pub const fn repositories(&self) -> &RepositoryCache {
2743        &self.repositories
2744    }
2745
2746    /// Starts the runners of `policies`' coordinator launches, and nothing
2747    /// else: no GitHub poll, no supervision. A launch the coordinator placed
2748    /// here (`DemandDirector::directed_demand`) needs no reading of the queue,
2749    /// so this runs as soon as one is accepted rather than at the next poll,
2750    /// and every launch open at that moment is served by one call. The
2751    /// runners go through the same allocator, ceilings and host-wide lock as
2752    /// in [`Self::reconcile`]; the next full pass supervises them.
2753    pub async fn start_directed(&mut self, policies: &[ScalePolicy]) -> ReconcileReport {
2754        let mut report = ReconcileReport::default();
2755        let mut reserved: Vec<Reserved> = Vec::new();
2756        for policy in policies {
2757            if !policy.owns_runners()
2758                || !policy.is_owned_by(self.host.id)
2759                || !policy.may_start_runners()
2760            {
2761                continue;
2762            }
2763            if let Some(directed) = self.director.directed_demand(policy) {
2764                self.start_runners(policy, directed, &mut report, &mut reserved)
2765                    .await;
2766            }
2767        }
2768        self.finish_reserved(reserved, &mut report).await;
2769        report
2770    }
2771
2772    /// One reconciliation pass over `policies`.
2773    ///
2774    /// The order of operations is `03-control-flows.md` flow 2, and the two
2775    /// steps most worth naming are the ones that are silent when they are wrong:
2776    ///
2777    /// * **Monitor-only policies are removed before the demand poll**, not
2778    ///   after. D19 says such a policy "is skipped entirely by reconciliation",
2779    ///   and a poll issued on its behalf would spend requests from the shared
2780    ///   ceiling for a policy that can never act on the answer. This is asserted
2781    ///   on [`ScalePolicy::owns_runners`] rather than deduced from
2782    ///   `max_capacity` being absent.
2783    /// * **The attempt set is re-read under the lock, once per runtime.** See
2784    ///   [`RunnerLauncher`] for why it comes from there and nowhere else.
2785    pub async fn reconcile(&mut self, policies: &[ScalePolicy]) -> ReconcileReport {
2786        let mut report = ReconcileReport::default();
2787        // Everything this pass has created, carried across policies so that the
2788        // host-wide total cannot be computed from a set that is missing it. See
2789        // `RunnerLauncher::launch`.
2790        let mut reserved: Vec<Reserved> = Vec::new();
2791
2792        // --- Flow 2.1-2.2: who is even asking, and what did GitHub say -------
2793        let mut pollable: Vec<&ScalePolicy> = Vec::new();
2794        let mut supervision_failed = BTreeSet::new();
2795        for policy in policies {
2796            if !policy.owns_runners() {
2797                report.monitor_only.push(policy.id);
2798                self.events
2799                    .emit(LifecycleEvent::MonitorOnlySkipped { policy: policy.id });
2800                continue;
2801            }
2802            if !policy.is_owned_by(self.host.id) {
2803                // Ownership rule 2 and precedence rule 4. The allocator reports
2804                // both by name below; polling on their behalf would spend
2805                // requests for an answer that cannot be acted on.
2806                continue;
2807            }
2808            match self.launcher.supervise(policy).await {
2809                Ok(intents) => {
2810                    report.replacement_intents = report
2811                        .replacement_intents
2812                        .saturating_add(u16::try_from(intents.len()).unwrap_or(u16::MAX));
2813                }
2814                Err(failure) => {
2815                    supervision_failed.insert(policy.id);
2816                    self.report_unreadable_attempts(&mut report, &failure);
2817                    continue;
2818                }
2819            }
2820            if !policy.may_start_runners() {
2821                continue;
2822            }
2823            pollable.push(policy);
2824        }
2825
2826        let readings = self.poll_targets(&pollable, &mut report).await;
2827        report.targets_read = u16::try_from(
2828            readings
2829                .values()
2830                .filter(|outcome| matches!(outcome, PollOutcome::Ready(_)))
2831                .count(),
2832        )
2833        .unwrap_or(u16::MAX);
2834        let mut routed = BTreeMap::new();
2835        for (target, outcome) in &readings {
2836            let PollOutcome::Ready(reading) = outcome else {
2837                continue;
2838            };
2839            let profiles: Vec<&ScalePolicy> = pollable
2840                .iter()
2841                .copied()
2842                .filter(|policy| &policy.target == target)
2843                .collect();
2844            let (tallies, refusals) = route_demand(policies, &profiles, reading);
2845            if let Some(first) = profiles.first() {
2846                for (reason, count) in refusals {
2847                    self.events.emit(LifecycleEvent::RoutingRefused {
2848                        policy: first.id,
2849                        reason,
2850                        count,
2851                    });
2852                }
2853            }
2854            routed.insert(target.clone(), tallies);
2855        }
2856
2857        // --- Flow 2.8: terminal attempts, whatever else this pass does -------
2858        //
2859        // Run before the allocation phase so that a report's `cleaned` count
2860        // describes the same instant its allocations do. It does not change the
2861        // arithmetic: a terminal attempt already stopped counting against
2862        // capacity when it became terminal, which is `b1`'s
2863        // `counts_against_capacity`. It touches no live process, so it is also
2864        // safe during an outage — flow 3.3 requires that running runners be
2865        // retained, and nothing here can reach one.
2866        self.clean_terminal_attempts(&mut report).await;
2867
2868        // --- Flow 2.3-2.6: the allocation -----------------------------------
2869        //
2870        // The predicates are re-tested here rather than the reading being looked
2871        // up by target, and that is not redundancy. **Targets are shared.** A
2872        // monitor-only policy watching `acme/app` alongside an autoscale policy
2873        // on the *same* repository finds a reading in the map that the other
2874        // policy paid for, and a lookup-driven loop then serves it: it emits a
2875        // demand observation on its behalf and clamps a number it has no
2876        // business seeing.
2877        //
2878        // Nothing downstream goes wrong when that happens — `may_start_runners`
2879        // is false for a monitor-only policy, so `HostAllocator` refuses it and
2880        // `to_start` is zero. It simply is not *skipped*, and D19's word is
2881        // "entirely".
2882        for policy in policies {
2883            if !policy.owns_runners() {
2884                // Already recorded and reported above, before any demand request
2885                // was issued. It owns no routing labels, takes no part in
2886                // demand, and can never be the reason a runner starts. Asserted
2887                // on the mode rather than deduced from `max_capacity` being
2888                // absent, which is what the specification requires.
2889                continue;
2890            }
2891            if !policy.is_owned_by(self.host.id) || !policy.may_start_runners() {
2892                // Ownership rule 2 and precedence rule 4. Allocated for with no
2893                // demand, so the refusal is reported by name rather than by
2894                // absence.
2895                match self.allocate_only(policy, 0, &reserved).await {
2896                    Ok(allocation) => {
2897                        self.emit_allocation(&allocation);
2898                        report.allocations.push(allocation);
2899                    }
2900                    Err(failure) => self.report_allocation_failure(&mut report, &failure),
2901                }
2902                continue;
2903            }
2904            if supervision_failed.contains(&policy.id) {
2905                continue;
2906            }
2907            // Coordinated placement: the launches the coordinator placed
2908            // here are the demand, whatever this host's own reading says (or
2909            // whether it could read at all).
2910            if let Some(directed) = self.director.directed_demand(policy) {
2911                self.start_runners(policy, directed, &mut report, &mut reserved)
2912                    .await;
2913                continue;
2914            }
2915            let Some(reading) = readings.get(&policy.target) else {
2916                // Unreachable: every policy reaching here was in `pollable`, and
2917                // `poll_targets` inserts an outcome for each of their targets.
2918                debug_assert!(false, "a pollable policy's target has no reading");
2919                continue;
2920            };
2921            match reading {
2922                PollOutcome::Failed(state) => {
2923                    report.unreadable.push(policy.id);
2924                    self.events.emit(LifecycleEvent::TargetUnreadable {
2925                        policy: policy.id,
2926                        reason: unreadable_reason(state),
2927                    });
2928                }
2929                PollOutcome::Ready(demand) => {
2930                    let tally = routed
2931                        .get(&policy.target)
2932                        .and_then(|tallies| tallies.get(&policy.id))
2933                        .expect("every pollable profile has a routed tally");
2934                    let count = tally.demand();
2935                    self.events.emit(LifecycleEvent::DemandObserved {
2936                        policy: policy.id,
2937                        demand: count,
2938                        not_matched: tally.not_matched,
2939                        unresolvable: u32::try_from(tally.unresolvable.len()).unwrap_or(u32::MAX),
2940                        complete: demand.is_complete(),
2941                    });
2942                    self.start_runners(policy, count, &mut report, &mut reserved)
2943                        .await;
2944                }
2945            }
2946        }
2947        self.finish_reserved(reserved, &mut report).await;
2948
2949        // --- Flow 2.1 / 3.3: when to come back ------------------------------
2950        let failure = readings
2951            .values()
2952            .filter_map(PollOutcome::failure)
2953            .max_by_key(|state| severity(state))
2954            .cloned();
2955        let now = self.clock.now();
2956        let conditions = PollConditions {
2957            quiet: is_quiet(&readings, &report),
2958            requests_per_pass: report.demand_requests,
2959            quota: readings
2960                .values()
2961                .filter_map(PollOutcome::reading)
2962                .filter_map(QueuedDemand::rate_limit)
2963                .max_by_key(|quota| quota.observed_at)
2964                .copied(),
2965        };
2966        report.next_poll =
2967            self.schedule
2968                .next_poll_with(failure.as_ref(), conditions, now, self.jitter.as_ref());
2969        report.failure = failure;
2970        // Flow 3.3's fourth obligation: the offline state carries the 24-hour
2971        // bound, and it can only say whether that bound has passed if it is
2972        // given the real elapsed time rather than an estimate from the interval.
2973        if let PollPace::Offline { consecutive } = report.next_poll.pace {
2974            let state = OfflineState::new(consecutive, report.next_poll.delay);
2975            report.offline = Some(match self.schedule.offline_for(now) {
2976                Some(elapsed) => state.since(elapsed),
2977                None => state,
2978            });
2979        }
2980        self.events.emit(LifecycleEvent::PollScheduled {
2981            retry_in_ms: u64::try_from(report.next_poll.delay.as_millis()).unwrap_or(u64::MAX),
2982            pace: report.next_poll.pace,
2983        });
2984
2985        report
2986    }
2987
2988    /// Poll each distinct target once, however many policies share it.
2989    ///
2990    /// Two policies on one repository are one demand request, not two. That is
2991    /// not a micro-optimisation: the budget model in
2992    /// `04-subsystem-contracts.md` prices a *target*, and a loop that spent per
2993    /// policy would quietly exceed the projection `f2` admitted the
2994    /// configuration against.
2995    async fn poll_targets(
2996        &self,
2997        pollable: &[&ScalePolicy],
2998        report: &mut ReconcileReport,
2999    ) -> BTreeMap<ScaleTarget, PollOutcome> {
3000        let targets: BTreeSet<ScaleTarget> = pollable.iter().map(|p| p.target.clone()).collect();
3001
3002        let mut readings = BTreeMap::new();
3003        for target in targets {
3004            let outcome = match self.repositories.scope_for(&target).await {
3005                Ok(scope) => {
3006                    report.demand_requests = report
3007                        .demand_requests
3008                        .saturating_add(demand_requests_per_poll(&scope));
3009                    self.demand.poll(&scope).await
3010                }
3011                // The repository list could not be refreshed, so the scope of
3012                // the poll is unknown. Polling a stale or empty scope would
3013                // report a demand number for a set of repositories nobody
3014                // chose, which is worse than reporting that the target could
3015                // not be read.
3016                Err(error) => PollOutcome::Failed(RefreshState::from_error(&error)),
3017            };
3018            readings.insert(target, outcome);
3019        }
3020        readings
3021    }
3022
3023    /// Compute one policy's allocation without creating anything.
3024    ///
3025    /// # Why this is safe without the lock
3026    ///
3027    /// **Not** because it cannot grant — it can, and
3028    /// [`Reconciler::start_runners`] uses it as a pre-check precisely for the
3029    /// number it returns. That was the original reason and this function
3030    /// outgrew it; the reason now is that it *decides* nothing. Nothing is
3031    /// created here, the headroom it read is re-read under the lock before any
3032    /// runtime exists, and the under-lock allocation may only lower what this
3033    /// one proposed. So there is no read-decide-create sequence here to make
3034    /// atomic, and the worst this can be is optimistic — which the lock then
3035    /// corrects.
3036    ///
3037    /// # Errors
3038    /// A host-ceiling or attempt-journal read failure. Neither permits an
3039    /// allocation, and they remain distinct in diagnostics.
3040    async fn allocate_only(
3041        &self,
3042        policy: &ScalePolicy,
3043        demand: u32,
3044        reserved: &[Reserved],
3045    ) -> Result<Allocation, AllocationFailure> {
3046        // Read again under the shared launch lock, not just at each target's
3047        // poll boundary: another target must not use an old, higher ceiling.
3048        let mut host = self.host.clone();
3049        host.host_capacity = self
3050            .launcher
3051            .host_capacity(&self.host)
3052            .await
3053            .map_err(AllocationFailure::HostCapacity)?;
3054        let attempts = self
3055            .host_attempts(reserved)
3056            .await
3057            .map_err(AllocationFailure::Attempts)?;
3058        let mut allocator = HostAllocator::from_attempts(&host, &attempts);
3059        Ok(allocator.allocate(policy, demand))
3060    }
3061
3062    /// Flow 2.4-2.6: start runners for one policy, one lock hold per runtime.
3063    ///
3064    /// # Two stopping conditions, and both are needed
3065    ///
3066    /// The loop re-reads the attempt set under every hold, so the obvious stop
3067    /// is "the allocator granted nothing". That condition **alone does not
3068    /// terminate**, and the failure is not hypothetical — it was measured.
3069    /// Handing the allocator a set that does not include the runners this loop
3070    /// just started (an empty one, a stale one, or a launcher whose journal
3071    /// write has not landed yet) makes every grant look like the first, and the
3072    /// pass starts runners until something outside it intervenes. With the set
3073    /// dropped entirely, the three-consecutive-polls test below does not report
3074    /// three attempts; it *never returns*.
3075    ///
3076    /// So the grant decided on the first hold is also a **budget**. A later hold
3077    /// may lower it — the host may have filled up meanwhile — and can never
3078    /// raise it, which bounds the pass at the number this policy was actually
3079    /// allocated. That is `c2`'s reasoning for `MAX_PAGES` one layer down: the
3080    /// reconciliation loop is the one place in this product that must not be
3081    /// able to wedge, so the bound is structural rather than a consequence of
3082    /// every input being well behaved.
3083    async fn start_runners(
3084        &self,
3085        policy: &ScalePolicy,
3086        demand: u32,
3087        report: &mut ReconcileReport,
3088        reserved: &mut Vec<Reserved>,
3089    ) {
3090        // A lock-free pre-check, for one reason only: the host-wide lock should
3091        // not be taken by a policy that is going to be granted nothing. On an
3092        // idle host with P policies that was P lock acquisitions per poll --
3093        // free under `InProcessAllocationLock`, a `spawn_blocking` and a
3094        // filesystem lock apiece under `FileAllocationLock`.
3095        //
3096        // It is safe because it can only be optimistic. Anything it grants is
3097        // re-decided under the lock below and may be lowered there; the only
3098        // thing it can get wrong in the other direction is refusing a grant that
3099        // headroom freed a moment later would have allowed, which the next poll
3100        // picks up.
3101        let intent = match self.allocate_only(policy, demand, reserved).await {
3102            Ok(intent) => intent,
3103            Err(failure) => {
3104                self.report_allocation_failure(report, &failure);
3105                self.director.start_failed(policy, None);
3106                return;
3107            }
3108        };
3109
3110        // The allocation that is *reported* is the one taken under the lock when
3111        // a lock was taken, because that is the one that decided anything. The
3112        // pre-check stands in only when no hold was ever obtained.
3113        let mut decided: Option<Allocation> = None;
3114        let mut budget = intent.to_start;
3115        let turn_limit = tokio::time::Instant::now() + OLDER_LAUNCH_TURN;
3116
3117        while budget > 0 {
3118            // Oldest first across targets. Each target's loop reserves for its
3119            // own launches, and may be anywhere in its pass: so a younger launch
3120            // lets an older one go first, briefly, and never for long enough to
3121            // matter if that one's loop is occupied.
3122            while self.director.older_waiting(policy) && tokio::time::Instant::now() < turn_limit {
3123                tokio::time::sleep(OLDER_LAUNCH_POLL).await;
3124            }
3125            let waiting_since = std::time::Instant::now();
3126            let guard = match self.lock.acquire().await {
3127                Ok(guard) => guard,
3128                Err(_) => {
3129                    // Grants, not policies: this is what the policy was owed and
3130                    // did not get.
3131                    report.deferred = report.deferred.saturating_add(budget);
3132                    self.events.emit(LifecycleEvent::AllocationDeferred {
3133                        policy: policy.id,
3134                        count: budget,
3135                    });
3136                    // The lock is held briefly now, so this is not a turn: a
3137                    // Windows drain, the fence, or another process.
3138                    self.director.start_failed(policy, None);
3139                    break;
3140                }
3141            };
3142            let lock_wait = waiting_since.elapsed();
3143
3144            // The read and the decision are both inside the hold, and so is the
3145            // journalled reservation below. Two concurrent passes therefore serialise on the
3146            // whole sequence rather than on the decision alone -- reading the
3147            // headroom outside the lock is the shape in which two policies both
3148            // find room for the last slot.
3149            let allocation = match self.allocate_only(policy, demand, reserved).await {
3150                Ok(allocation) => allocation,
3151                Err(failure) => {
3152                    drop(guard);
3153                    self.report_allocation_failure(report, &failure);
3154                    self.director.start_failed(policy, None);
3155                    break;
3156                }
3157            };
3158
3159            if decided.is_none() {
3160                // The under-lock decision may be smaller than the pre-check, and
3161                // never larger: `min` rather than assignment, so a later hold
3162                // cannot raise the bound either.
3163                budget = budget.min(allocation.to_start);
3164                decided = Some(allocation.clone());
3165            }
3166
3167            // Either stop is sufficient on its own in the well-behaved case;
3168            // neither is sufficient when the launcher lags. See the doc comment.
3169            if allocation.starts_nothing() || budget == 0 {
3170                drop(guard);
3171                break;
3172            }
3173
3174            let created = self
3175                .launcher
3176                .launch(LaunchRequest {
3177                    host: &self.host,
3178                    policy,
3179                    allocation_guard: &guard,
3180                })
3181                .await;
3182            let hold = guard.launch_hold();
3183            drop(guard);
3184
3185            match created {
3186                Ok(attempt) => {
3187                    let id = attempt.id;
3188                    // At once, not once it is up: the launch has its runner, and
3189                    // the runner's report names the launch it was started for.
3190                    self.director.launched(policy, id);
3191                    budget -= 1;
3192                    // Carried across policies for the rest of this pass, so the
3193                    // host-wide total cannot be computed from a set that is
3194                    // missing it. See `RunnerLauncher::launch`.
3195                    reserved.push(Reserved {
3196                        policy: policy.clone(),
3197                        attempt,
3198                        lock_wait,
3199                        reserved_at: std::time::Instant::now(),
3200                        _hold: hold,
3201                    });
3202                }
3203                Err(failure) => {
3204                    self.note_start_failed(policy, None, &failure);
3205                    break;
3206                }
3207            }
3208        }
3209
3210        let allocation = decided.unwrap_or(intent);
3211        self.emit_allocation(&allocation);
3212        report.allocations.push(allocation);
3213    }
3214
3215    /// Bring up what this pass reserved, together and outside the allocation
3216    /// lock, so one slow package holds up neither another runner nor another
3217    /// target. How many prepare at once is the launcher's to bound.
3218    ///
3219    /// Awaited within the pass, because supervision is what would otherwise see
3220    /// these attempts half made: one target's attempts are only ever supervised
3221    /// by its own loop, which is here.
3222    async fn finish_reserved(&self, reserved: Vec<Reserved>, report: &mut ReconcileReport) {
3223        let finished = futures::future::join_all(reserved.into_iter().map(|reserved| async move {
3224            let id = reserved.attempt.id;
3225            let waited = reserved.lock_wait + reserved.reserved_at.elapsed();
3226            let result = self
3227                .launcher
3228                .finish_launch(&reserved.policy, reserved.attempt, waited)
3229                .await;
3230            // Told here and not after the others are up: the journal already
3231            // says this one failed, and the attempt reports read it.
3232            match &result {
3233                Ok(_) => self.events.emit(LifecycleEvent::RunnerStarted {
3234                    policy: reserved.policy.id,
3235                    attempt: id,
3236                }),
3237                Err(failure) => self.note_start_failed(&reserved.policy, Some(id), failure),
3238            }
3239            result.is_ok()
3240        }))
3241        .await;
3242        let started = finished.into_iter().filter(|started| *started).count();
3243        report.started = report
3244            .started
3245            .saturating_add(u16::try_from(started).unwrap_or(u16::MAX));
3246    }
3247
3248    fn note_start_failed(
3249        &self,
3250        policy: &ScalePolicy,
3251        attempt: Option<AttemptId>,
3252        failure: &LaunchFailure,
3253    ) {
3254        self.events.emit(LifecycleEvent::RunnerStartFailed {
3255            policy: policy.id,
3256            reason: failure_reason_kind(&failure.reason),
3257        });
3258        self.director.start_failed(policy, attempt);
3259    }
3260
3261    /// The attempt set the host holds, plus everything this pass has already
3262    /// created.
3263    ///
3264    /// The merge is by [`RunnerAttempt::id`], so a launcher that makes its
3265    /// launches visible before returning -- which
3266    /// [`RunnerLauncher::launch`] asks for -- contributes each attempt once, and
3267    /// one that lags still cannot hide a runner from the host-wide total. The
3268    /// ceiling therefore holds on the strength of this function rather than on
3269    /// the strength of an implementer honouring a comment.
3270    ///
3271    /// # Errors
3272    /// Whatever [`RunnerLauncher::attempts`] reported.
3273    async fn host_attempts(
3274        &self,
3275        reserved: &[Reserved],
3276    ) -> Result<Vec<RunnerAttempt>, LaunchFailure> {
3277        let mut attempts = self.launcher.attempts().await?;
3278
3279        // Each `launch` creates one runtime, so each must answer with an
3280        // identifier no other attempt has. Two entries sharing one here are two
3281        // runtimes the host-wide total below counts once, which is the ceiling
3282        // failing silently -- so a development build stops at the first
3283        // duplicate instead. `RunnerLauncher::launch` states the requirement;
3284        // this is what makes it findable.
3285        debug_assert!(
3286            reserved
3287                .iter()
3288                .map(|reserved| reserved.attempt.id)
3289                .collect::<BTreeSet<AttemptId>>()
3290                .len()
3291                == reserved.len(),
3292            "`RunnerLauncher::launch` returned an AttemptId this pass had already seen; \
3293             the host ceiling is enforced against a set keyed on that identifier, so a \
3294             duplicate is two runtimes counted as one"
3295        );
3296
3297        let known: BTreeSet<AttemptId> = attempts.iter().map(|attempt| attempt.id).collect();
3298        attempts.extend(
3299            reserved
3300                .iter()
3301                .filter(|reserved| !known.contains(&reserved.attempt.id))
3302                .map(|reserved| reserved.attempt.clone()),
3303        );
3304        Ok(attempts)
3305    }
3306
3307    fn report_allocation_failure(&self, report: &mut ReconcileReport, failure: &AllocationFailure) {
3308        match failure {
3309            AllocationFailure::HostCapacity(reason) => {
3310                report.host_capacity_unreadable = report.host_capacity_unreadable.saturating_add(1);
3311                self.events
3312                    .emit(LifecycleEvent::HostCapacityUnreadable { reason: *reason });
3313            }
3314            AllocationFailure::Attempts(failure) => {
3315                self.report_unreadable_attempts(report, failure)
3316            }
3317        }
3318    }
3319
3320    /// The attempt set could not be read, so nothing may be decided from it.
3321    ///
3322    /// Counted rather than swallowed for the reason the module documentation
3323    /// gives: an unreadable set and an idle host produce the same *number* and
3324    /// demand opposite actions, so the difference has to survive into the
3325    /// report.
3326    fn report_unreadable_attempts(&self, report: &mut ReconcileReport, failure: &LaunchFailure) {
3327        report.attempts_unreadable = report.attempts_unreadable.saturating_add(1);
3328        // The variant, never a literal and never the detail. A hand-written
3329        // `"attempts_unreadable"` said only what the event's own name already
3330        // said, and threw away the one thing the field is for -- *which* failure
3331        // it was. `FailureReason::Other` carries free text that must not reach
3332        // an event, which is what `failure_reason_kind` is for and what
3333        // `a_cleanup_that_cannot_succeed_...` pins for the sibling path.
3334        self.events.emit(LifecycleEvent::AttemptsUnreadable {
3335            reason: failure_reason_kind(&failure.reason),
3336        });
3337    }
3338
3339    /// Remove the runtimes of attempts that have already concluded.
3340    ///
3341    /// `is_concluded` and not `is_terminal`: `cleaned` is terminal and already
3342    /// done, and `busy` is not terminal at all. That is what makes it impossible
3343    /// for this path to reach a runner executing a job.
3344    async fn clean_terminal_attempts(&mut self, report: &mut ReconcileReport) {
3345        let attempts = match self.launcher.attempts().await {
3346            Ok(attempts) => attempts,
3347            Err(failure) => {
3348                self.report_unreadable_attempts(report, &failure);
3349                return;
3350            }
3351        };
3352        self.clean_backoff
3353            .retain(&attempts.iter().map(|attempt| attempt.id).collect());
3354        let now = self.clock.now();
3355        for attempt in attempts {
3356            if !attempt.state().is_concluded() {
3357                continue;
3358            }
3359            let Some(outcome) = attempt.outcome() else {
3360                continue;
3361            };
3362            // Backed off after failing: not tried, so neither cleaned nor a
3363            // fresh failure to report.
3364            if !self.clean_backoff.is_due(attempt.id, now) {
3365                continue;
3366            }
3367            let kind = OutcomeKind::of(outcome);
3368            match self.launcher.clean(attempt.id).await {
3369                Ok(()) => {
3370                    self.clean_backoff.succeeded(attempt.id);
3371                    report.cleaned = report.cleaned.saturating_add(1);
3372                    if kind.is_failure() {
3373                        report.failures = report.failures.saturating_add(1);
3374                    } else if kind == OutcomeKind::IdleExit {
3375                        // The surplus case. Counted apart from a failure because
3376                        // `g2` renders it apart, and because an operator told
3377                        // that a normal surplus exit is an error goes hunting a
3378                        // fault that does not exist.
3379                        report.idle_exits = report.idle_exits.saturating_add(1);
3380                    }
3381                    self.events.emit(LifecycleEvent::AttemptCleaned {
3382                        policy: attempt.policy_id,
3383                        attempt: attempt.id,
3384                        outcome: kind,
3385                    });
3386                }
3387                // A runtime directory that cannot be removed is retried on every
3388                // poll. Silently, before this arm existed: no event, no counter,
3389                // no report field, so a cleanup that can never succeed was an
3390                // invisible permanent loop. It wedges no capacity -- a terminal
3391                // attempt already stopped counting -- but this module's
3392                // organising principle is the things that go wrong silently, and
3393                // `clean` returns a `Result` precisely so the caller can say
3394                // something. It is retried with per-attempt backoff
3395                // ([`CLEAN_RETRY_INITIAL`]) so the saying is not a flood.
3396                Err(failure) => {
3397                    self.clean_backoff.failed(attempt.id, now);
3398                    report.clean_failures = report.clean_failures.saturating_add(1);
3399                    self.events.emit(LifecycleEvent::AttemptCleanFailed {
3400                        policy: attempt.policy_id,
3401                        attempt: attempt.id,
3402                        reason: failure_reason_kind(&failure.reason),
3403                    });
3404                }
3405            }
3406        }
3407    }
3408
3409    /// Reclaim what can be reclaimed for one policy, and nothing else.
3410    ///
3411    /// **A busy attempt is never removed.** `04-subsystem-contracts.md`:
3412    /// *"`busy` cannot transition to cleanup due to a scale-down request"*.
3413    /// Capacity comes back when an attempt reaches a terminal state and at no
3414    /// other time, so a scale-down against a host full of busy runners removes
3415    /// nothing, changes nothing, and says so.
3416    pub async fn scale_down(&self, policy: &ScalePolicy) -> ScaleDownReport {
3417        let mut report = ScaleDownReport::default();
3418        let attempts = match self.launcher.attempts().await {
3419            Ok(attempts) => attempts,
3420            Err(failure) => {
3421                // The same rule as everywhere else, and this was the one place
3422                // it was still broken: an unreadable set is not an empty one,
3423                // and a bare `default()` here reported all zeros -- byte for
3424                // byte an idle host with nothing to reclaim.
3425                self.events.emit(LifecycleEvent::AttemptsUnreadable {
3426                    reason: failure_reason_kind(&failure.reason),
3427                });
3428                report.attempts_unreadable = true;
3429                return report;
3430            }
3431        };
3432        for attempt in attempts {
3433            if attempt.policy_id != policy.id {
3434                continue;
3435            }
3436            match attempt.state() {
3437                AttemptState::Busy => {
3438                    report.refused_busy = report.refused_busy.saturating_add(1);
3439                    self.events.emit(LifecycleEvent::ScaleDownRefused {
3440                        policy: policy.id,
3441                        attempt: attempt.id,
3442                    });
3443                }
3444                state if state.is_concluded() => {
3445                    let kind = attempt
3446                        .outcome()
3447                        .map_or(OutcomeKind::Failed, OutcomeKind::of);
3448                    match self.launcher.clean(attempt.id).await {
3449                        Ok(()) => {
3450                            report.removed = report.removed.saturating_add(1);
3451                            self.events.emit(LifecycleEvent::AttemptCleaned {
3452                                policy: policy.id,
3453                                attempt: attempt.id,
3454                                outcome: kind,
3455                            });
3456                        }
3457                        Err(failure) => {
3458                            report.clean_failures = report.clean_failures.saturating_add(1);
3459                            self.events.emit(LifecycleEvent::AttemptCleanFailed {
3460                                policy: policy.id,
3461                                attempt: attempt.id,
3462                                reason: failure_reason_kind(&failure.reason),
3463                            });
3464                        }
3465                    }
3466                }
3467                AttemptState::Cleaned => {}
3468                // `allocated`, `jit_received`, `starting`, `idle`: live, holding
3469                // a slot, and not this function's to end.
3470                _ => report.retained = report.retained.saturating_add(1),
3471            }
3472        }
3473        report
3474    }
3475
3476    fn emit_allocation(&self, allocation: &Allocation) {
3477        self.events.emit(LifecycleEvent::Allocated {
3478            policy: allocation.policy_id,
3479            demand: allocation.demand,
3480            desired: allocation.desired,
3481            active_owned: allocation.active_owned,
3482            headroom: allocation.headroom_before,
3483            to_start: allocation.to_start,
3484            limiting: allocation.limiting_factor,
3485        });
3486    }
3487}
3488
3489/// One policy's demand, from the reading its target answered with.
3490///
3491/// A repository target tallies its own repository's queued jobs; an organization
3492/// target tallies every repository its scope covered, because one policy watching
3493/// an organization serves any repository in it.
3494///
3495/// # Why this takes a whole policy rather than a target and a label set
3496///
3497/// Because both halves have to come from the same policy, and a signature that
3498/// took them separately made it possible for them not to. The predecessor took a
3499/// `&ScaleTarget` alone and could not filter at all; the obvious repair was to
3500/// add a `&RoutingLabels` beside it, and at three call sites — two of them in
3501/// tests — nothing would have caught passing one policy's target with another
3502/// policy's labels. It compiles, it runs, and it silently serves the wrong
3503/// repository's queue.
3504///
3505/// # A monitor-only policy has no labels, and cannot reach here
3506///
3507/// [`Reconciler::reconcile`] filters on [`ScalePolicy::owns_runners`] before any
3508/// demand request is issued (D19), so the `None` arm is unreachable rather than
3509/// merely unlikely. It returns an empty tally instead of unwrapping, because a
3510/// panic in the reconciliation loop would take the daemon down over a policy
3511/// that was only ever going to start nothing.
3512fn demand_for(policy: &ScalePolicy, reading: &QueuedDemand) -> DemandTally {
3513    let Some(labels) = policy.routing_labels() else {
3514        debug_assert!(
3515            false,
3516            "a monitor-only policy is skipped before the demand poll (D19)"
3517        );
3518        return DemandTally::default();
3519    };
3520
3521    match &policy.target {
3522        ScaleTarget::Repository(repository) => labels.tally(reading.jobs_for(repository)),
3523        ScaleTarget::Organization(_) => labels.tally(reading.jobs()),
3524    }
3525}
3526
3527/// Tally one shared target reading using GitHub's required-label subset rule.
3528/// Refuse ambiguous ownership, including inactive local profiles: disabling an
3529/// isolated profile must not redirect its broad-label demand to a native sibling.
3530fn route_demand(
3531    all_policies: &[ScalePolicy],
3532    profiles: &[&ScalePolicy],
3533    reading: &QueuedDemand,
3534) -> (
3535    BTreeMap<PolicyId, DemandTally>,
3536    BTreeMap<RoutingRefusal, u32>,
3537) {
3538    let mut tallies: BTreeMap<PolicyId, DemandTally> = profiles
3539        .iter()
3540        .map(|policy| (policy.id, demand_for(policy, reading)))
3541        .collect();
3542    let mut refusals: BTreeMap<RoutingRefusal, u32> = BTreeMap::new();
3543    let Some(first) = profiles.first() else {
3544        return (tallies, refusals);
3545    };
3546    let owners: Vec<_> = all_policies
3547        .iter()
3548        .filter(|policy| policy.target == first.target && policy.host_id == first.host_id)
3549        .filter(|policy| policy.owns_runners())
3550        .collect();
3551    let jobs: Vec<&runner_manager_domain::policy::RunsOn> = match &first.target {
3552        ScaleTarget::Repository(repository) => reading.jobs_for(repository).iter().collect(),
3553        ScaleTarget::Organization(_) => reading.jobs().collect(),
3554    };
3555    for job in jobs {
3556        let Ok(_) = job.required_labels() else {
3557            // `demand_for` already preserves this as unresolvable for each profile.
3558            continue;
3559        };
3560        let matched: Vec<PolicyId> = owners
3561            .iter()
3562            .filter(|policy| {
3563                policy
3564                    .routing_labels()
3565                    .is_some_and(|labels| labels.matches(job).is_match())
3566            })
3567            .map(|policy| policy.id)
3568            .collect();
3569        if matched.len() > 1 {
3570            *refusals
3571                .entry(RoutingRefusal::OverlappingProfiles)
3572                .or_default() += 1;
3573            for id in matched {
3574                if let Some(tally) = tallies.get_mut(&id) {
3575                    tally.matched -= 1;
3576                    tally.not_matched += 1;
3577                }
3578            }
3579        }
3580    }
3581    (tallies, refusals)
3582}
3583
3584/// How urgently one failure should slow the loop down.
3585///
3586/// Ordering matters only for picking the worst of several targets: an outage
3587/// outranks a rate limit because backing off a socket that is not answering is
3588/// the safer error, and both outrank a per-target rejection that says nothing
3589/// about the credential as a whole.
3590const fn severity(state: &RefreshState) -> u8 {
3591    match state {
3592        RefreshState::Offline => 5,
3593        RefreshState::RateLimited(_) => 4,
3594        RefreshState::LockedOut { .. } => 3,
3595        RefreshState::Unauthorized => 2,
3596        RefreshState::Forbidden { .. } | RefreshState::Failed { .. } => 1,
3597        RefreshState::Cancelled | RefreshState::Ready(_) => 0,
3598    }
3599}
3600
3601/// Why one target could not be read, as a fixed, credential-free name.
3602///
3603/// Deliberately not a [`PollPace`]: a pace describes the *schedule*, which is a
3604/// property of the whole pass, and stamping one onto a single target would have
3605/// meant inventing a `consecutive` count for a target that has none. What an
3606/// event needs here is the reason, and `c3`'s [`RefreshState`] already names it.
3607///
3608/// `RefreshState::Failed` carries GitHub's own message and
3609/// `RefreshState::Forbidden` may carry one too. Neither reaches the event: this
3610/// returns the variant, for the reason [`failure_reason_kind`] states.
3611const fn unreadable_reason(state: &RefreshState) -> &'static str {
3612    match state {
3613        RefreshState::Ready(_) => "ready",
3614        RefreshState::Offline => "offline",
3615        RefreshState::RateLimited(_) => "rate_limited",
3616        RefreshState::LockedOut { .. } => "locked_out",
3617        RefreshState::Unauthorized => "unauthorized",
3618        RefreshState::Forbidden { .. } => "forbidden",
3619        RefreshState::Failed { .. } => "failed",
3620        RefreshState::Cancelled => "cancelled",
3621    }
3622}
3623
3624#[cfg(test)]
3625mod tests {
3626    use super::*;
3627
3628    /// The reading that said `healthy` for 28 hours while nothing worked.
3629    ///
3630    /// A daemon every one of whose targets answered `401` kept writing a fresh
3631    /// `last GitHub contact`, because an unauthorized target is `unreadable`
3632    /// rather than a `failure`. Both that record and the `service status` built
3633    /// on it were used as evidence during the investigation, and both were
3634    /// wrong; see `docs/spikes/token-expiry-and-renewal.md`.
3635    #[test]
3636    fn a_pass_that_reached_no_target_does_not_claim_it_reached_github() {
3637        let mut report = ReconcileReport::default();
3638        assert!(
3639            !report.reached_github(),
3640            "a pass that polled nothing -- every policy draining, owned elsewhere, or \
3641             monitor-only -- reached nobody. This is the case the old guard let through, and \
3642             the only one it ever let through."
3643        );
3644
3645        report.unreadable.push(PolicyId::from_u128(1));
3646        assert!(
3647            !report.reached_github(),
3648            "every target this pass tried was unreadable, so there is no contact to record"
3649        );
3650
3651        report.targets_read = 1;
3652        assert!(
3653            report.reached_github(),
3654            "one target answering is contact, whatever else failed alongside it"
3655        );
3656
3657        // `allocations` deliberately does not count: a policy this host does
3658        // not own is allocated for with no demand and without polling anything,
3659        // so a pass where every poll failed can still carry allocations.
3660        let mut unowned = ReconcileReport::default();
3661        unowned.unreadable.push(PolicyId::from_u128(2));
3662        unowned.allocations.push(Allocation {
3663            policy_id: PolicyId::from_u128(2),
3664            demand: 0,
3665            desired: 0,
3666            active_owned: 0,
3667            headroom_before: 0,
3668            to_start: 0,
3669            limiting_factor: LimitingFactor::Demand,
3670        });
3671        assert!(
3672            !unowned.reached_github(),
3673            "an allocation is not evidence that GitHub answered"
3674        );
3675    }
3676
3677    /// The claim the old guard rested on, checked rather than assumed.
3678    ///
3679    /// `report.failure.is_none()` was believed to be compatible with an
3680    /// all-unauthorized pass. It is not: `unreadable` is pushed only from the
3681    /// `Failed` arm and `failure` is the maximum over every `Failed` reading,
3682    /// so guarding on `failure.is_none() && reached_github()` would have been
3683    /// `failure.is_none()` with extra words. This pins the severity that makes
3684    /// it so, because a future `severity(Unauthorized) == 0` would quietly
3685    /// restore the belief.
3686    #[test]
3687    fn an_unauthorized_target_is_a_failure_and_not_merely_unreadable() {
3688        assert!(
3689            severity(&RefreshState::Unauthorized) > 0,
3690            "an unauthorized reading must survive `max_by_key(severity)` into `report.failure`, \
3691             or a pass where every target was refused would report no failure at all"
3692        );
3693    }
3694
3695    use std::sync::atomic::AtomicUsize;
3696
3697    use std::num::NonZeroU16;
3698
3699    use runner_manager_domain::attempt::PersistedAttempt;
3700    use runner_manager_domain::model::{CachePolicy, HostId};
3701    use runner_manager_domain::policy::PolicyMode;
3702    use runner_manager_domain::workspace::WorkspaceKind;
3703    use runner_manager_github::rest::RateLimited;
3704    use runner_manager_testkit::clock::FakeClock;
3705    use runner_manager_testkit::fixtures;
3706    use runner_manager_testkit::github::FakeGithub;
3707
3708    // =======================================================================
3709    // Fakes
3710    // =======================================================================
3711
3712    fn host_with(capacity: u16) -> Host {
3713        fixtures::host().capacity(capacity).build()
3714    }
3715
3716    fn repo(raw: &str) -> OwnerRepo {
3717        OwnerRepo::parse(raw).expect("a valid OWNER/REPO")
3718    }
3719
3720    /// An `active`, enabled autoscale policy on the fixture host.
3721    use runner_manager_domain::policy::RunsOn;
3722
3723    fn policy(id: u128, target: &str, max: u16) -> ScalePolicy {
3724        fixtures::policy()
3725            .id(PolicyId::from_u128(id))
3726            .repository(target)
3727            .autoscale("home", max)
3728            .active()
3729            .build()
3730    }
3731
3732    /// The host label every policy in these tests carries.
3733    ///
3734    /// `policy` above builds through `fixtures::policy().autoscale("home", …)`,
3735    /// which derives `rm-home-win-x64`. A job fixture that did not carry it
3736    /// would be filtered out as another host's work, so the two are tied
3737    /// together here rather than repeated as a literal at each call site.
3738    const HOST_LABEL: &str = "rm-home-win-x64";
3739
3740    /// `n` queued jobs this host's policies match.
3741    ///
3742    /// The ordinary demand fixture. Since the reversal of the run-counting
3743    /// decision the unit `e1` clamps is a job, so a test wanting demand `n` asks
3744    /// for `n` jobs rather than for `n` runs.
3745    fn jobs(n: usize) -> Vec<RunsOn> {
3746        fixtures::queued_jobs(&[HOST_LABEL], n)
3747    }
3748
3749    /// `e3`, faked: an attempt table and a launch counter, no process anywhere.
3750    #[derive(Debug, Default)]
3751    struct FakeLauncher {
3752        capacity: Mutex<Option<NonZeroU16>>,
3753        capacity_unreadable: AtomicBool,
3754        capacity_after_launch: Mutex<Option<NonZeroU16>>,
3755        attempts: Mutex<Vec<RunnerAttempt>>,
3756        next_id: AtomicU64,
3757        launches: AtomicUsize,
3758        cleaned: Mutex<Vec<AttemptId>>,
3759        /// Yields this many times between reading the attempt set and recording
3760        /// a new one, so an unserialised allocator has a window to be wrong in.
3761        yields_before_recording: usize,
3762        /// Reports success without the attempt ever becoming visible, which is
3763        /// the shape a slow journal write has. Every grant then looks like the
3764        /// first.
3765        forgetful: bool,
3766        fail_next: Mutex<Option<FailureReason>>,
3767        /// Reports that the attempt set cannot be read at all, which is the one
3768        /// answer a caller must never confuse with an idle host.
3769        attempts_fail: Mutex<bool>,
3770        /// Refuses every cleanup, so the silent-retry path has something to be
3771        /// loud about.
3772        clean_fails: bool,
3773        replacements: Mutex<Vec<ReplacementIntent>>,
3774    }
3775
3776    impl FakeLauncher {
3777        fn new() -> Self {
3778            Self::default()
3779        }
3780
3781        fn with_yields(mut self, yields: usize) -> Self {
3782            self.yields_before_recording = yields;
3783            self
3784        }
3785
3786        fn forgetful() -> Self {
3787            Self {
3788                forgetful: true,
3789                ..Self::default()
3790            }
3791        }
3792
3793        fn seeded(self, attempts: Vec<RunnerAttempt>) -> Self {
3794            *self.attempts.lock().unwrap() = attempts;
3795            self
3796        }
3797
3798        fn launches(&self) -> usize {
3799            self.launches.load(Ordering::SeqCst)
3800        }
3801
3802        fn snapshot(&self) -> Vec<RunnerAttempt> {
3803            self.attempts.lock().unwrap().clone()
3804        }
3805
3806        fn live_count(&self) -> usize {
3807            self.snapshot()
3808                .iter()
3809                .filter(|a| a.counts_against_capacity())
3810                .count()
3811        }
3812
3813        fn fail_next(&self, reason: FailureReason) {
3814            *self.fail_next.lock().unwrap() = Some(reason);
3815        }
3816
3817        fn fail_attempts(&self, failing: bool) {
3818            *self.attempts_fail.lock().unwrap() = failing;
3819        }
3820
3821        fn refusing_cleanup(attempts: Vec<RunnerAttempt>) -> Self {
3822            Self {
3823                clean_fails: true,
3824                ..Self::default()
3825            }
3826            .seeded(attempts)
3827        }
3828
3829        fn cleaned(&self) -> Vec<AttemptId> {
3830            self.cleaned.lock().unwrap().clone()
3831        }
3832
3833        fn replacing(self, intent: ReplacementIntent) -> Self {
3834            self.replacements.lock().unwrap().push(intent);
3835            self
3836        }
3837    }
3838
3839    #[async_trait::async_trait]
3840    impl RunnerLauncher for FakeLauncher {
3841        async fn host_capacity(&self, host: &Host) -> Result<NonZeroU16, HostCapacityError> {
3842            if self.capacity_unreadable.load(Ordering::SeqCst) {
3843                return Err(HostCapacityError::JournalUnreadable);
3844            }
3845            Ok(self.capacity.lock().unwrap().unwrap_or(host.host_capacity))
3846        }
3847
3848        async fn supervise(
3849            &self,
3850            policy: &ScalePolicy,
3851        ) -> Result<Vec<ReplacementIntent>, LaunchFailure> {
3852            let mut replacements = self.replacements.lock().unwrap();
3853            let selected: Vec<_> = replacements
3854                .extract_if(.., |intent| intent.policy == policy.id)
3855                .collect();
3856            if !selected.is_empty() {
3857                let retired: BTreeSet<_> = selected
3858                    .iter()
3859                    .map(|intent| intent.previous_attempt)
3860                    .collect();
3861                self.attempts
3862                    .lock()
3863                    .unwrap()
3864                    .retain(|attempt| !retired.contains(&attempt.id));
3865            }
3866            Ok(selected)
3867        }
3868
3869        async fn attempts(&self) -> Result<Vec<RunnerAttempt>, LaunchFailure> {
3870            if *self.attempts_fail.lock().unwrap() {
3871                return Err(LaunchFailure::new(FailureReason::Other(
3872                    "the journal could not be read".into(),
3873                )));
3874            }
3875            Ok(self.snapshot())
3876        }
3877
3878        async fn launch(&self, request: LaunchRequest<'_>) -> Result<RunnerAttempt, LaunchFailure> {
3879            if let Some(reason) = self.fail_next.lock().unwrap().take() {
3880                return Err(LaunchFailure::new(reason));
3881            }
3882            // The window an unserialised caller would lose the race in.
3883            for _ in 0..self.yields_before_recording {
3884                tokio::task::yield_now().await;
3885            }
3886            let id =
3887                AttemptId::from_u128(u128::from(self.next_id.fetch_add(1, Ordering::SeqCst) + 1));
3888            let created = RunnerAttempt::allocate(
3889                id,
3890                request.policy.id,
3891                "runtime/p/a",
3892                request.host.created_at,
3893            );
3894            self.launches.fetch_add(1, Ordering::SeqCst);
3895            if let Some(capacity) = self.capacity_after_launch.lock().unwrap().take() {
3896                *self.capacity.lock().unwrap() = Some(capacity);
3897            }
3898            if !self.forgetful {
3899                self.attempts.lock().unwrap().push(created.clone());
3900            }
3901            Ok(created)
3902        }
3903
3904        async fn clean(&self, attempt: AttemptId) -> Result<(), LaunchFailure> {
3905            if self.clean_fails {
3906                return Err(LaunchFailure::new(FailureReason::Other(
3907                    "the runtime directory is locked".into(),
3908                )));
3909            }
3910            self.cleaned.lock().unwrap().push(attempt);
3911            let mut attempts = self.attempts.lock().unwrap();
3912            attempts.retain(|a| a.id != attempt);
3913            Ok(())
3914        }
3915    }
3916
3917    /// A demand source a test programs directly, with no gateway underneath.
3918    #[derive(Debug, Default)]
3919    struct FakeDemand {
3920        outcome: Mutex<Option<PollOutcome>>,
3921        /// Answers programmed for one target, which beat the blanket one.
3922        per_target: Mutex<BTreeMap<ScaleTarget, PollOutcome>>,
3923        scopes: Mutex<Vec<ActivityScope>>,
3924    }
3925
3926    impl FakeDemand {
3927        fn ready(count: u32, repository: &OwnerRepo) -> Self {
3928            let fake = Self::default();
3929            fake.set(PollOutcome::Ready(QueuedDemand::of(
3930                repository.clone(),
3931                jobs(count as usize),
3932            )));
3933            fake
3934        }
3935
3936        fn failing(state: RefreshState) -> Self {
3937            let fake = Self::default();
3938            fake.set(PollOutcome::Failed(state));
3939            fake
3940        }
3941
3942        fn set(&self, outcome: PollOutcome) {
3943            *self.outcome.lock().unwrap() = Some(outcome);
3944        }
3945
3946        /// Program one target's answer, overriding the blanket one.
3947        fn set_for(&self, target: &ScaleTarget, outcome: PollOutcome) {
3948            self.per_target
3949                .lock()
3950                .unwrap()
3951                .insert(target.clone(), outcome);
3952        }
3953
3954        fn polls(&self) -> Vec<ActivityScope> {
3955            self.scopes.lock().unwrap().clone()
3956        }
3957    }
3958
3959    #[async_trait::async_trait]
3960    impl DemandSource for FakeDemand {
3961        async fn poll(&self, scope: &ActivityScope) -> PollOutcome {
3962            self.scopes.lock().unwrap().push(scope.clone());
3963            if let Some(outcome) = self.per_target.lock().unwrap().get(scope.target()) {
3964                return outcome.clone();
3965            }
3966            self.outcome
3967                .lock()
3968                .unwrap()
3969                .clone()
3970                .unwrap_or(PollOutcome::Ready(QueuedDemand::default()))
3971        }
3972    }
3973
3974    #[derive(Debug, Default)]
3975    struct FakeDirectory {
3976        repositories: Vec<OwnerRepo>,
3977        calls: AtomicUsize,
3978    }
3979
3980    impl FakeDirectory {
3981        fn of(repositories: Vec<OwnerRepo>) -> Self {
3982            Self {
3983                repositories,
3984                calls: AtomicUsize::new(0),
3985            }
3986        }
3987
3988        fn calls(&self) -> usize {
3989            self.calls.load(Ordering::SeqCst)
3990        }
3991    }
3992
3993    #[async_trait::async_trait]
3994    impl RepositoryDirectory for FakeDirectory {
3995        async fn repositories(&self, _org: &Org) -> Result<Vec<OwnerRepo>, InventoryError> {
3996            self.calls.fetch_add(1, Ordering::SeqCst);
3997            Ok(self.repositories.clone())
3998        }
3999    }
4000
4001    /// A lock that grants everything and counts how many holders it had at once.
4002    ///
4003    /// The counter is the assertion: "under simulated lock contention" is only
4004    /// meaningful if something measures that the contention was actually
4005    /// serialised.
4006    #[derive(Debug)]
4007    struct CountingLock {
4008        inner: InProcessAllocationLock,
4009        concurrent: Arc<AtomicUsize>,
4010        peak: Arc<AtomicUsize>,
4011        acquisitions: Arc<AtomicUsize>,
4012    }
4013
4014    impl CountingLock {
4015        fn new() -> Self {
4016            Self {
4017                inner: InProcessAllocationLock::new(),
4018                concurrent: Arc::new(AtomicUsize::new(0)),
4019                peak: Arc::new(AtomicUsize::new(0)),
4020                acquisitions: Arc::new(AtomicUsize::new(0)),
4021            }
4022        }
4023
4024        fn peak(&self) -> usize {
4025            self.peak.load(Ordering::SeqCst)
4026        }
4027
4028        fn acquisitions(&self) -> usize {
4029            self.acquisitions.load(Ordering::SeqCst)
4030        }
4031    }
4032
4033    #[derive(Debug)]
4034    struct CountingGuard {
4035        _inner: AllocationGuard,
4036        concurrent: Arc<AtomicUsize>,
4037    }
4038
4039    impl Drop for CountingGuard {
4040        fn drop(&mut self) {
4041            self.concurrent.fetch_sub(1, Ordering::SeqCst);
4042        }
4043    }
4044
4045    #[async_trait::async_trait]
4046    impl AllocationLock for CountingLock {
4047        async fn acquire(&self) -> Result<AllocationGuard, AllocationLockBusy> {
4048            let inner = self.inner.acquire().await?;
4049            self.acquisitions.fetch_add(1, Ordering::SeqCst);
4050            let now = self.concurrent.fetch_add(1, Ordering::SeqCst) + 1;
4051            self.peak.fetch_max(now, Ordering::SeqCst);
4052            Ok(AllocationGuard::new(CountingGuard {
4053                _inner: inner,
4054                concurrent: Arc::clone(&self.concurrent),
4055            }))
4056        }
4057    }
4058
4059    /// The lock that is not one: what the host looks like with the serialisation
4060    /// removed. Used only by the control half of the contention test.
4061    #[derive(Debug, Default)]
4062    struct NoLock;
4063
4064    #[async_trait::async_trait]
4065    impl AllocationLock for NoLock {
4066        async fn acquire(&self) -> Result<AllocationGuard, AllocationLockBusy> {
4067            Ok(AllocationGuard::new(()))
4068        }
4069    }
4070
4071    #[tokio::test]
4072    async fn a_wsl_drain_request_fences_the_exact_pre_launch_boundary() {
4073        use runner_manager_platform::wsl::fence::DrainRequest;
4074
4075        let (_local, shared, paths) = guest_paths();
4076        DrainRequest::new(4, chrono::Utc::now())
4077            .write(shared.path())
4078            .unwrap();
4079        let lock = WslRecoveryAllocationLock::new(paths, Arc::new(NoLock));
4080
4081        assert!(lock.acquire().await.is_err());
4082    }
4083
4084    #[tokio::test]
4085    async fn a_guest_launch_claim_is_released_with_its_allocation_guard() {
4086        use runner_manager_platform::wsl::fence::FENCE_DIRECTORY;
4087
4088        let (_local, shared, paths) = guest_paths();
4089        let lock = WslRecoveryAllocationLock::new(paths, Arc::new(NoLock));
4090        let guard = lock.acquire().await.unwrap();
4091        assert!(shared.path().join(FENCE_DIRECTORY).exists());
4092        drop(guard);
4093        assert!(!shared.path().join(FENCE_DIRECTORY).exists());
4094    }
4095
4096    /// Captures what `tracing` writes on this thread.
4097    #[derive(Clone, Default)]
4098    struct Logs(Arc<Mutex<Vec<u8>>>);
4099
4100    impl std::io::Write for Logs {
4101        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
4102            self.0.lock().unwrap().extend_from_slice(buf);
4103            Ok(buf.len())
4104        }
4105        fn flush(&mut self) -> std::io::Result<()> {
4106            Ok(())
4107        }
4108    }
4109
4110    /// The release that once failed with no trace while a distribution
4111    /// started nothing for 13 days is now in the log, with its path.
4112    #[tokio::test]
4113    async fn a_launch_fence_that_cannot_be_released_is_logged_with_its_path() {
4114        use runner_manager_platform::wsl::fence::FENCE_DIRECTORY;
4115
4116        let (_local, shared, paths) = guest_paths();
4117        let lock = WslRecoveryAllocationLock::new(paths, Arc::new(NoLock));
4118        let guard = lock.acquire().await.unwrap();
4119        // Something the removal cannot take away: a file where the claim
4120        // directory was.
4121        let fence = shared.path().join(FENCE_DIRECTORY);
4122        std::fs::remove_dir_all(&fence).unwrap();
4123        std::fs::write(&fence, b"not a directory").unwrap();
4124
4125        let logs = Logs::default();
4126        let writer = logs.clone();
4127        let subscriber = tracing_subscriber::fmt()
4128            .with_writer(move || writer.clone())
4129            .with_ansi(false)
4130            .finish();
4131        tracing::subscriber::with_default(subscriber, || drop(guard));
4132
4133        let text = String::from_utf8(logs.0.lock().unwrap().clone()).unwrap();
4134        assert!(text.contains("WARN"), "{text}");
4135        assert!(text.contains("could not be released"), "{text}");
4136        assert!(text.contains(FENCE_DIRECTORY), "the path is named: {text}");
4137    }
4138
4139    fn guest_paths() -> (
4140        tempfile::TempDir,
4141        tempfile::TempDir,
4142        Arc<runner_manager_platform::paths::AppPaths>,
4143    ) {
4144        use runner_manager_platform::paths::AppPaths;
4145        use runner_manager_platform::wsl::fence::GuestRecoveryConfig;
4146
4147        let local = tempfile::tempdir().unwrap();
4148        let shared = tempfile::tempdir().unwrap();
4149        let paths = Arc::new(AppPaths::rooted_at(local.path()));
4150        paths.create_all().unwrap();
4151        GuestRecoveryConfig::new(shared.path().to_path_buf())
4152            .write(&paths)
4153            .unwrap();
4154        (local, shared, paths)
4155    }
4156
4157    /// The incident end to end: a guest claim left by a daemon of an earlier
4158    /// WSL boot no longer stops a launch.
4159    #[tokio::test]
4160    async fn a_stale_guest_claim_no_longer_blocks_the_next_launch() {
4161        use runner_manager_platform::wsl::fence::{FENCE_DIRECTORY, OWNER_FILE};
4162
4163        let (_local, shared, paths) = guest_paths();
4164        let fence = shared.path().join(FENCE_DIRECTORY);
4165        std::fs::create_dir_all(&fence).unwrap();
4166        let pid = std::process::id();
4167        std::fs::write(
4168            fence.join(OWNER_FILE),
4169            format!(
4170                r#"{{"schema_version":1,"kind":"guest_launch","generation":null,"process_id":{pid},"acquired_at":"2026-09-24T00:47:18.724Z","identity":{{"pid":{pid},"start_token":"linux:earlier-boot:1"}}}}"#
4171            ),
4172        )
4173        .unwrap();
4174        let lock = WslRecoveryAllocationLock::new(paths, Arc::new(NoLock));
4175
4176        let guard = lock.acquire().await.expect("the stale claim was reclaimed");
4177        drop(guard);
4178        assert!(!fence.exists());
4179    }
4180
4181    #[tokio::test]
4182    async fn refusals_that_persist_are_recorded_and_a_grant_clears_them() {
4183        use runner_manager_platform::launch_health::recorded_launches_blocked;
4184        use runner_manager_platform::wsl::fence::{FenceClaim, FenceOwnerKind};
4185
4186        let (_local, shared, paths) = guest_paths();
4187        let lock = WslRecoveryAllocationLock::new(Arc::clone(&paths), Arc::new(NoLock))
4188            .with_blocked_after(3, Duration::ZERO);
4189        // Windows owns the fence, so every guest launch is refused.
4190        let windows =
4191            FenceClaim::try_claim(shared.path(), FenceOwnerKind::WindowsRecovery, Some(1))
4192                .unwrap()
4193                .unwrap();
4194
4195        for _ in 0..2 {
4196            assert!(lock.acquire().await.is_err());
4197        }
4198        assert_eq!(
4199            recorded_launches_blocked(&paths, chrono::Utc::now()).unwrap(),
4200            None,
4201            "not yet"
4202        );
4203        assert!(lock.acquire().await.is_err());
4204        let blocked = recorded_launches_blocked(&paths, chrono::Utc::now())
4205            .unwrap()
4206            .expect("three refusals in a row are recorded");
4207        assert!(
4208            blocked.reason.contains("Windows WSL recovery"),
4209            "{blocked:?}"
4210        );
4211
4212        windows.release().unwrap();
4213        let guard = lock.acquire().await.unwrap();
4214        assert_eq!(
4215            recorded_launches_blocked(&paths, chrono::Utc::now()).unwrap(),
4216            None
4217        );
4218        drop(guard);
4219    }
4220
4221    #[tokio::test]
4222    async fn refusals_while_this_process_is_launching_are_its_own_contention() {
4223        use runner_manager_platform::launch_health::recorded_launches_blocked;
4224
4225        use runner_manager_platform::wsl::fence::DrainRequest;
4226
4227        let (_local, shared, paths) = guest_paths();
4228        let lock = WslRecoveryAllocationLock::new(Arc::clone(&paths), Arc::new(NoLock))
4229            .with_blocked_after(1, Duration::ZERO);
4230        let launching = lock.acquire().await.unwrap();
4231        DrainRequest::new(1, chrono::Utc::now())
4232            .write(shared.path())
4233            .unwrap();
4234        for _ in 0..3 {
4235            assert!(lock.acquire().await.is_err());
4236        }
4237        assert_eq!(
4238            recorded_launches_blocked(&paths, chrono::Utc::now()).unwrap(),
4239            None
4240        );
4241        drop(launching);
4242    }
4243
4244    /// The launches of this process share the guest claim from reservation to
4245    /// runner: the other target's launch is not refused, and Windows gets the
4246    /// directory only when the last has ended.
4247    #[tokio::test]
4248    async fn launches_in_progress_share_the_fence_until_the_last_ends() {
4249        use runner_manager_platform::wsl::fence::{DrainRequest, FenceClaim, FenceOwnerKind};
4250
4251        let (_local, shared, paths) = guest_paths();
4252        let lock = WslRecoveryAllocationLock::new(paths, Arc::new(NoLock));
4253        let claimed = |root: &std::path::Path| FenceClaim::owner(root).unwrap().is_some();
4254
4255        let first = lock.acquire().await.unwrap();
4256        let first_runner = first.launch_hold();
4257        drop(first);
4258        assert!(claimed(shared.path()), "held while its runner comes up");
4259
4260        let claimed_at = FenceClaim::owner(shared.path())
4261            .unwrap()
4262            .unwrap()
4263            .acquired_at;
4264        tokio::time::sleep(Duration::from_millis(5)).await;
4265        let second = lock.acquire().await.expect("this process owns the fence");
4266        assert!(
4267            FenceClaim::owner(shared.path())
4268                .unwrap()
4269                .unwrap()
4270                .acquired_at
4271                > claimed_at,
4272            "a claim launches keep sharing is not one stuck for that long"
4273        );
4274        let second_runner = second.launch_hold();
4275        drop(second);
4276        drop(first_runner);
4277        assert!(claimed(shared.path()), "the other launch is still running");
4278
4279        // A drain stops new launches and lets the one in progress finish.
4280        DrainRequest::new(1, chrono::Utc::now())
4281            .write(shared.path())
4282            .unwrap();
4283        assert!(lock.acquire().await.is_err());
4284        assert!(claimed(shared.path()));
4285        assert!(
4286            FenceClaim::try_claim(shared.path(), FenceOwnerKind::WindowsRecovery, Some(1))
4287                .unwrap()
4288                .is_none()
4289        );
4290        drop(second_runner);
4291        assert!(!claimed(shared.path()), "the last one lets it go");
4292        assert!(
4293            FenceClaim::try_claim(shared.path(), FenceOwnerKind::WindowsRecovery, Some(1))
4294                .unwrap()
4295                .is_some()
4296        );
4297    }
4298
4299    /// Refusals separated by quiet spells -- brief contention days apart --
4300    /// never add up to a blocked host.
4301    #[tokio::test]
4302    async fn refusals_with_quiet_spells_between_them_never_add_up() {
4303        use runner_manager_platform::launch_health::recorded_launches_blocked;
4304        use runner_manager_platform::wsl::fence::{FenceClaim, FenceOwnerKind};
4305
4306        let (_local, shared, paths) = guest_paths();
4307        let lock = WslRecoveryAllocationLock::new(Arc::clone(&paths), Arc::new(NoLock))
4308            .with_blocked_after(3, Duration::ZERO)
4309            .with_streak_gap(Duration::ZERO);
4310        let windows =
4311            FenceClaim::try_claim(shared.path(), FenceOwnerKind::WindowsRecovery, Some(1))
4312                .unwrap()
4313                .unwrap();
4314        for _ in 0..5 {
4315            assert!(lock.acquire().await.is_err());
4316        }
4317        assert_eq!(
4318            recorded_launches_blocked(&paths, chrono::Utc::now()).unwrap(),
4319            None
4320        );
4321        windows.release().unwrap();
4322    }
4323
4324    /// A drain request refuses the launch after the claim was made; that
4325    /// claim is released rather than left to block the next pass.
4326    #[tokio::test]
4327    async fn a_claim_refused_by_a_drain_request_is_released() {
4328        use runner_manager_platform::wsl::fence::{DrainRequest, FENCE_DIRECTORY};
4329
4330        let (_local, shared, paths) = guest_paths();
4331        DrainRequest::new(4, chrono::Utc::now())
4332            .write(shared.path())
4333            .unwrap();
4334        let lock = WslRecoveryAllocationLock::new(paths, Arc::new(NoLock));
4335        assert!(lock.acquire().await.is_err());
4336        assert!(!shared.path().join(FENCE_DIRECTORY).exists());
4337    }
4338
4339    /// A lock nobody can take.
4340    #[derive(Debug, Default)]
4341    struct HeldLock;
4342
4343    #[async_trait::async_trait]
4344    impl AllocationLock for HeldLock {
4345        async fn acquire(&self) -> Result<AllocationGuard, AllocationLockBusy> {
4346            Err(AllocationLockBusy)
4347        }
4348    }
4349
4350    /// Everything one test needs, wired together.
4351    struct Harness {
4352        launcher: Arc<FakeLauncher>,
4353        demand: Arc<FakeDemand>,
4354        events: Arc<EventLog>,
4355        clock: Arc<FakeClock>,
4356        reconciler: Reconciler,
4357    }
4358
4359    impl Harness {
4360        fn build(
4361            host: Host,
4362            launcher: Arc<FakeLauncher>,
4363            demand: Arc<FakeDemand>,
4364            lock: Arc<dyn AllocationLock>,
4365        ) -> Self {
4366            let events = Arc::new(EventLog::new());
4367            let clock = Arc::new(FakeClock::default());
4368            let reconciler = Reconciler::new(
4369                host,
4370                ReconcilerPorts {
4371                    demand: Arc::clone(&demand) as Arc<dyn DemandSource>,
4372                    launcher: Arc::clone(&launcher) as Arc<dyn RunnerLauncher>,
4373                    lock,
4374                    directory: Arc::new(FakeDirectory::default()),
4375                    clock: Arc::clone(&clock) as Arc<dyn Clock>,
4376                    jitter: Arc::new(NoJitter) as Arc<dyn Jitter>,
4377                    events: Arc::clone(&events) as Arc<dyn EventSink>,
4378                },
4379            );
4380            Self {
4381                launcher,
4382                demand,
4383                events,
4384                clock,
4385                reconciler,
4386            }
4387        }
4388
4389        fn simple(capacity: u16, demand_count: u32, target: &str) -> Self {
4390            let launcher = Arc::new(FakeLauncher::new());
4391            let demand = Arc::new(FakeDemand::ready(demand_count, &repo(target)));
4392            Self::build(
4393                host_with(capacity),
4394                launcher,
4395                demand,
4396                Arc::new(InProcessAllocationLock::new()),
4397            )
4398        }
4399    }
4400
4401    fn attempt_in(state: AttemptState, id: u128, policy: u128) -> RunnerAttempt {
4402        let outcome = state.is_terminal().then(|| match state {
4403            AttemptState::Failed => {
4404                AttemptOutcome::failed(FailureReason::ProcessExitedUnexpectedly)
4405            }
4406            AttemptState::Orphaned => AttemptOutcome::Orphaned,
4407            _ => AttemptOutcome::CompletedJob,
4408        });
4409        RunnerAttempt::from_persisted(PersistedAttempt {
4410            id: AttemptId::from_u128(id),
4411            policy_id: PolicyId::from_u128(policy),
4412            github_runner_id: None,
4413            state,
4414            outcome,
4415            process_id: None,
4416            execution: runner_manager_domain::execution::AttemptExecution::Native {
4417                process_id: None,
4418            },
4419            runtime_path: "runtime/p/a".into(),
4420            workspace_kind: WorkspaceKind::Ephemeral,
4421            workspace_slot: None,
4422            created_at: fixtures::created_at(),
4423            terminal_at: state.is_terminal().then(fixtures::created_at),
4424            last_state_change_at: fixtures::created_at(),
4425        })
4426        .expect("a state/outcome pair the domain accepts")
4427    }
4428
4429    /// A concluded attempt carrying a specific outcome.
4430    fn concluded(id: u128, policy: u128, outcome: AttemptOutcome) -> RunnerAttempt {
4431        RunnerAttempt::from_persisted(PersistedAttempt {
4432            id: AttemptId::from_u128(id),
4433            policy_id: PolicyId::from_u128(policy),
4434            github_runner_id: None,
4435            state: outcome.terminal_state(),
4436            outcome: Some(outcome),
4437            process_id: None,
4438            execution: runner_manager_domain::execution::AttemptExecution::Native {
4439                process_id: None,
4440            },
4441            runtime_path: "runtime/p/a".into(),
4442            workspace_kind: WorkspaceKind::Ephemeral,
4443            workspace_slot: None,
4444            created_at: fixtures::created_at(),
4445            terminal_at: Some(fixtures::created_at()),
4446            last_state_change_at: fixtures::created_at(),
4447        })
4448        .expect("a state/outcome pair the domain accepts")
4449    }
4450
4451    // =======================================================================
4452    // The in-flight term: the single most likely way this task goes wrong
4453    // =======================================================================
4454
4455    /// `e1`'s Definition of Done, verbatim: *"A job that remains `queued` across
4456    /// three consecutive polls while its attempt is `starting` yields exactly
4457    /// one attempt — the test fails if the in-flight term is dropped from the
4458    /// formula."*
4459    ///
4460    /// `b1` tests the arithmetic underneath this
4461    /// (`capacity::tests::the_same_queued_job_on_two_polls_yields_one_attempt_
4462    /// not_two`). What *this* test covers is the only way `e1` can drop the
4463    /// term without touching `b1` at all: handing the allocator an attempt set
4464    /// that is not the one the host holds.
4465    ///
4466    /// # This was measured, not assumed, and the first measurement was worse
4467    /// # than the failure it was looking for
4468    ///
4469    /// Replacing `self.launcher.attempts().await` in
4470    /// [`Reconciler::start_runners`] with `Vec::new()` compiles and runs. Before
4471    /// that function carried a budget, this test did not go red — it **never
4472    /// returned**: every grant looked like the first, so the pass started
4473    /// runners forever inside poll 1. That is the runaway-runner failure exactly
4474    /// as an operator would meet it, and it is why the budget exists.
4475    ///
4476    /// With the budget in place the same injection fails cleanly and says what
4477    /// happened: `poll 2 … left: 2, right: 1`. Both measurements were run
4478    /// before this assertion was written.
4479    #[tokio::test]
4480    async fn three_polls_of_one_still_queued_run_yield_exactly_one_attempt() {
4481        let mut harness = Harness::simple(4, 1, "acme/app");
4482        let policy = policy(1, "acme/app", 4);
4483
4484        for poll in 1..=3 {
4485            let report = harness
4486                .reconciler
4487                .reconcile(std::slice::from_ref(&policy))
4488                .await;
4489            assert_eq!(
4490                harness.launcher.launches(),
4491                1,
4492                "poll {poll} started another runner for a job already being served; the \
4493                 `- active_owned_runners` term reached `HostAllocator` as a set this host \
4494                 does not hold"
4495            );
4496            assert_eq!(report.allocations.len(), 1);
4497            let allocation = &report.allocations[0];
4498            assert_eq!(allocation.demand, 1, "poll {poll}: still queued at GitHub");
4499            if poll == 1 {
4500                assert_eq!(allocation.to_start, 1);
4501                assert_eq!(report.started, 1);
4502            } else {
4503                assert_eq!(allocation.active_owned, 1, "poll {poll}");
4504                assert_eq!(allocation.to_start, 0, "poll {poll}");
4505                assert_eq!(report.started, 0, "poll {poll}");
4506            }
4507        }
4508        assert_eq!(harness.launcher.live_count(), 1);
4509    }
4510
4511    #[tokio::test]
4512    async fn draining_parallel_workflow_starts_runners_for_the_remaining_queue() {
4513        let mut harness = Harness::simple(10, 8, "acme/app");
4514        let policy = policy(1, "acme/app", 10);
4515        let first = harness
4516            .reconciler
4517            .reconcile(std::slice::from_ref(&policy))
4518            .await;
4519        assert_eq!(first.started, 8);
4520
4521        // Five jobs finish while three longer jobs keep their runners busy.
4522        let attempts = harness.launcher.snapshot();
4523        *harness.launcher.attempts.lock().unwrap() = attempts
4524            .iter()
4525            .take(3)
4526            .map(|attempt| attempt_in(AttemptState::Busy, attempt.id.as_uuid().as_u128(), 1))
4527            .collect();
4528        harness.demand.set(PollOutcome::Ready(QueuedDemand::of(
4529            repo("acme/app"),
4530            jobs(2),
4531        )));
4532
4533        for poll in 0..3 {
4534            let report = harness
4535                .reconciler
4536                .reconcile(std::slice::from_ref(&policy))
4537                .await;
4538            assert_eq!(report.started, if poll == 0 { 2 } else { 0 });
4539            assert_eq!(harness.launcher.live_count(), 5);
4540        }
4541    }
4542
4543    /// The other half of the measurement above: the loop must terminate even
4544    /// when the attempt set never catches up with it.
4545    ///
4546    /// Dropping the in-flight term made
4547    /// `three_polls_of_one_still_queued_run_yield_exactly_one_attempt` hang
4548    /// rather than fail — the loop had one stopping condition and it was the one
4549    /// the bug removed. A launcher whose journal write has not landed presents
4550    /// exactly the same shape without any bug at all, so the budget in
4551    /// [`Reconciler::start_runners`] bounds the pass structurally. This is what
4552    /// asserts the bound is really there.
4553    #[tokio::test]
4554    async fn a_launcher_whose_attempts_never_appear_cannot_wedge_the_pass() {
4555        let launcher = Arc::new(FakeLauncher::forgetful());
4556        let mut harness = Harness::build(
4557            host_with(64),
4558            Arc::clone(&launcher),
4559            Arc::new(FakeDemand::ready(3, &repo("acme/app"))),
4560            Arc::new(InProcessAllocationLock::new()),
4561        );
4562
4563        let report = harness
4564            .reconciler
4565            .reconcile(&[policy(1, "acme/app", 8)])
4566            .await;
4567
4568        assert_eq!(
4569            report.started, 3,
4570            "the pass is bounded by the grant it was given, not by the attempt set catching \
4571             up with it"
4572        );
4573        assert_eq!(launcher.launches(), 3);
4574        assert!(
4575            launcher.snapshot().is_empty(),
4576            "the launcher never recorded anything, which is the whole point of the fixture"
4577        );
4578    }
4579
4580    /// The host-wide ceiling must hold across policies even when the launcher
4581    /// lags, and the per-policy budget alone does not reach that case.
4582    ///
4583    /// Review found this, with this file's own `forgetful` fixture and one more
4584    /// policy: the budget bounds *each policy's* loop to its own first grant,
4585    /// but policy B's first grant is computed from a set that does not yet
4586    /// contain policy A's launches, so B's bound is itself too large. Two
4587    /// policies on a host of three started **six** runners --
4588    /// `host_capacity=3, started=6, launches=6` -- with the lock held correctly
4589    /// throughout. Serialisation was never the problem; the arithmetic under it
4590    /// was reading a stale set.
4591    #[tokio::test]
4592    async fn two_policies_cannot_exceed_host_capacity_even_when_the_launcher_lags() {
4593        let launcher = Arc::new(FakeLauncher::forgetful());
4594        let demand = Arc::new(FakeDemand::default());
4595        demand.set_for(
4596            &ScaleTarget::repository("acme/left").unwrap(),
4597            PollOutcome::Ready(QueuedDemand::of(repo("acme/left"), jobs(3))),
4598        );
4599        demand.set_for(
4600            &ScaleTarget::repository("acme/right").unwrap(),
4601            PollOutcome::Ready(QueuedDemand::of(repo("acme/right"), jobs(3))),
4602        );
4603        let mut harness = Harness::build(
4604            host_with(3),
4605            Arc::clone(&launcher),
4606            demand,
4607            Arc::new(InProcessAllocationLock::new()),
4608        );
4609
4610        let report = harness
4611            .reconciler
4612            .reconcile(&[policy(1, "acme/left", 3), policy(2, "acme/right", 3)])
4613            .await;
4614
4615        assert_eq!(
4616            report.started, 3,
4617            "host_capacity is 3 and two policies each allowed 3 started {} runners \
4618             between them; the second policy's grant was computed from a set that did \
4619             not yet contain the first policy's launches",
4620            report.started
4621        );
4622        assert_eq!(launcher.launches(), 3);
4623    }
4624
4625    /// Finding 1: an attempt set that cannot be read is not an empty one.
4626    ///
4627    /// `attempts()` used to be infallible, which left `e3` — reading a journal
4628    /// off a disk — a choice between panicking and answering `vec![]`. The
4629    /// second is silent and catastrophic: an empty set is indistinguishable from
4630    /// an idle host, so a transient read failure reads as "nothing is running"
4631    /// and the pass allocates the whole machine for jobs already being served.
4632    ///
4633    /// The contrast is the assertion. Identical host, identical demand,
4634    /// identical policy; the only difference is whether the launcher can answer.
4635    #[tokio::test]
4636    async fn an_unreadable_attempt_set_starts_nothing_and_is_not_read_as_an_idle_host() {
4637        let launcher = Arc::new(FakeLauncher::new());
4638        let mut harness = Harness::build(
4639            host_with(8),
4640            Arc::clone(&launcher),
4641            Arc::new(FakeDemand::ready(4, &repo("acme/app"))),
4642            Arc::new(InProcessAllocationLock::new()),
4643        );
4644        let policy = policy(1, "acme/app", 8);
4645
4646        launcher.fail_attempts(true);
4647        let unreadable = harness
4648            .reconciler
4649            .reconcile(std::slice::from_ref(&policy))
4650            .await;
4651
4652        assert_eq!(
4653            unreadable.started, 0,
4654            "nothing may be decided from a set that was not read"
4655        );
4656        assert_eq!(launcher.launches(), 0);
4657        assert!(unreadable.attempts_unreadable > 0, "and the pass says so");
4658        assert!(
4659            unreadable.allocations.is_empty(),
4660            "no allocation is reported either: there was no set to compute one from, and \
4661             an allocation of zero would claim a decision nobody made"
4662        );
4663        assert!(harness.events.count_of("attempts_unreadable") > 0);
4664
4665        // The same everything, with a launcher that can answer.
4666        launcher.fail_attempts(false);
4667        let readable = harness
4668            .reconciler
4669            .reconcile(std::slice::from_ref(&policy))
4670            .await;
4671        assert_eq!(
4672            readable.started, 4,
4673            "the difference between the two passes is only whether the set could be read"
4674        );
4675        assert_eq!(readable.attempts_unreadable, 0);
4676    }
4677
4678    /// Finding 3: a cleanup that can never succeed was an invisible permanent
4679    /// loop.
4680    ///
4681    /// `if …clean(…).await.is_ok()` had no `else`, so a runtime directory that
4682    /// could not be removed was retried on every poll with no event, no counter
4683    /// and no report field. It wedges no capacity — a terminal attempt already
4684    /// stopped counting — but `clean` returns a `Result` precisely so the caller
4685    /// can say something, and this module's organising principle is the things
4686    /// that go wrong silently.
4687    #[tokio::test]
4688    async fn a_cleanup_that_cannot_succeed_is_reported_rather_than_retried_in_silence() {
4689        let launcher = Arc::new(FakeLauncher::refusing_cleanup(vec![concluded(
4690            1,
4691            1,
4692            AttemptOutcome::ExitedIdleWithoutWork,
4693        )]));
4694        let mut harness = Harness::build(
4695            host_with(4),
4696            Arc::clone(&launcher),
4697            Arc::new(FakeDemand::ready(0, &repo("acme/app"))),
4698            Arc::new(InProcessAllocationLock::new()),
4699        );
4700
4701        let report = harness
4702            .reconciler
4703            .reconcile(&[policy(1, "acme/app", 4)])
4704            .await;
4705
4706        assert_eq!(report.cleaned, 0);
4707        assert_eq!(report.clean_failures, 1);
4708        assert_eq!(harness.events.count_of("attempt_clean_failed"), 1);
4709        assert_eq!(
4710            harness.events.count_of("attempt_cleaned"),
4711            0,
4712            "and it is not reported as cleaned"
4713        );
4714        assert_eq!(
4715            launcher.snapshot().len(),
4716            1,
4717            "the attempt is still there, so the retry is real -- what changed is that it \
4718             is no longer silent"
4719        );
4720
4721        // The reason is the variant, never the detail: the fixture's failure
4722        // carries free text and none of it reaches the event.
4723        let reasons: Vec<&'static str> = harness
4724            .events
4725            .events()
4726            .into_iter()
4727            .filter_map(|event| match event {
4728                LifecycleEvent::AttemptCleanFailed { reason, .. } => Some(reason),
4729                _ => None,
4730            })
4731            .collect();
4732        assert_eq!(reasons, vec!["other"]);
4733    }
4734
4735    /// A cleanup that keeps failing is retried with per-attempt exponential
4736    /// backoff rather than on every pass, and a success forgets the backoff.
4737    #[tokio::test]
4738    async fn a_failing_cleanup_backs_off_exponentially_per_attempt() {
4739        let launcher = Arc::new(FakeLauncher::refusing_cleanup(vec![concluded(
4740            1,
4741            1,
4742            AttemptOutcome::ExitedIdleWithoutWork,
4743        )]));
4744        let mut harness = Harness::build(
4745            host_with(4),
4746            Arc::clone(&launcher),
4747            Arc::new(FakeDemand::ready(0, &repo("acme/app"))),
4748            Arc::new(InProcessAllocationLock::new()),
4749        );
4750        let policies = [policy(1, "acme/app", 4)];
4751        let start = harness.clock.now();
4752        let at = |offset: Duration| start + chrono::Duration::from_std(offset).unwrap();
4753        let mut failures_at = async |offset: Duration| {
4754            harness.clock.set(at(offset));
4755            harness.reconciler.reconcile(&policies).await.clean_failures
4756        };
4757
4758        assert_eq!(failures_at(Duration::ZERO).await, 1);
4759        // Inside the first delay: not tried, so nothing new to report.
4760        assert_eq!(
4761            failures_at(CLEAN_RETRY_INITIAL - Duration::from_secs(1)).await,
4762            0,
4763            "a failed cleanup was retried on the very next pass"
4764        );
4765        assert_eq!(failures_at(CLEAN_RETRY_INITIAL).await, 1);
4766        // The second delay is twice the first.
4767        let second = CLEAN_RETRY_INITIAL + CLEAN_RETRY_INITIAL * 2;
4768        assert_eq!(
4769            failures_at(second - Duration::from_secs(1)).await,
4770            0,
4771            "the delay did not double after a second failure"
4772        );
4773        assert_eq!(failures_at(second).await, 1);
4774        assert_eq!(harness.events.count_of("attempt_clean_failed"), 3);
4775
4776        // The delay never exceeds the ceiling, however many failures accrue.
4777        let mut backoff = CleanBackoff::default();
4778        let id = AttemptId::from_u128(9);
4779        for _ in 0..40 {
4780            backoff.failed(id, start);
4781        }
4782        assert!(!backoff.is_due(id, at(CLEAN_RETRY_MAXIMUM - Duration::from_secs(1))));
4783        assert!(backoff.is_due(id, at(CLEAN_RETRY_MAXIMUM)));
4784        backoff.succeeded(id);
4785        assert!(
4786            backoff.is_due(id, start),
4787            "a cleaned attempt kept its backoff"
4788        );
4789    }
4790
4791    /// N2: an unreadable attempt set makes a scale-down inconclusive, not empty.
4792    ///
4793    /// Making `attempts()` fallible closed this everywhere the allocation path
4794    /// touches, and left it open in the one place that returns a different type:
4795    /// `scale_down` answered `ScaleDownReport::default()`, which is all zeros
4796    /// and byte-for-byte identical to an idle host with nothing to reclaim. The
4797    /// two mean opposite things — "there was nothing to remove" against "we
4798    /// cannot see what there was".
4799    ///
4800    /// Measured as the sibling test measures it: identical host, identical
4801    /// attempts, identical policy, and the only difference is whether the
4802    /// launcher can answer.
4803    #[tokio::test]
4804    async fn an_unreadable_attempt_set_makes_scale_down_inconclusive_rather_than_empty() {
4805        let launcher = Arc::new(FakeLauncher::new().seeded(vec![
4806            attempt_in(AttemptState::Busy, 1, 1),
4807            concluded(2, 1, AttemptOutcome::CompletedJob),
4808        ]));
4809        let harness = Harness::build(
4810            host_with(4),
4811            Arc::clone(&launcher),
4812            Arc::new(FakeDemand::ready(0, &repo("acme/app"))),
4813            Arc::new(InProcessAllocationLock::new()),
4814        );
4815        let policy = policy(1, "acme/app", 4);
4816
4817        launcher.fail_attempts(true);
4818        let blind = harness.reconciler.scale_down(&policy).await;
4819
4820        assert!(!blind.is_conclusive(), "the machine was never read");
4821        assert_ne!(
4822            blind,
4823            ScaleDownReport::default(),
4824            "a scale-down that could not see the host must not be equal to one that saw \
4825             an idle host; that equality is the whole finding"
4826        );
4827        assert_eq!(blind.removed, 0);
4828        assert_eq!(
4829            blind.refused_busy, 0,
4830            "and this zero means `unknown`, not `none`"
4831        );
4832        assert_eq!(harness.events.count_of("attempts_unreadable"), 1);
4833
4834        // The same everything, with a launcher that can answer.
4835        launcher.fail_attempts(false);
4836        let seeing = harness.reconciler.scale_down(&policy).await;
4837
4838        assert!(seeing.is_conclusive());
4839        assert_eq!(seeing.removed, 1, "the concluded attempt was reclaimed");
4840        assert_eq!(seeing.refused_busy, 1, "and the busy one was left alone");
4841        assert_ne!(
4842            seeing, blind,
4843            "the difference between the two is only whether the set could be read"
4844        );
4845    }
4846
4847    /// Finding 7: the lower arm of the clamp, driven through the reconciler.
4848    ///
4849    /// `demand_below_min_capacity_starts_nothing_in_v1` runs `demand = 0`
4850    /// against `min_capacity = 0`, which is *at* the floor and never raises
4851    /// `desired` — the assertion held for a reason unrelated to the boundary it
4852    /// named. D7 fixes `min` at 0 for v1, but `AutoscaleConfig::new` accepts
4853    /// `min > 0` today, so the path is representable and was undriven.
4854    #[tokio::test]
4855    async fn demand_below_min_capacity_is_raised_to_min_capacity() {
4856        let mut warm = ScalePolicy::new(
4857            PolicyId::from_u128(1),
4858            ScaleTarget::repository("acme/app").unwrap(),
4859            1,
4860            fixtures::HOST_ID,
4861            PolicyMode::autoscale(
4862                fixtures::routing_labels("home"),
4863                2,
4864                NonZeroU16::new(5).expect("non-zero"),
4865            )
4866            .expect("min <= max"),
4867            CachePolicy::default(),
4868        );
4869        warm.activate().expect("pending -> active");
4870
4871        let mut harness = Harness::simple(8, 0, "acme/app");
4872        let report = harness.reconciler.reconcile(&[warm]).await;
4873
4874        assert_eq!(
4875            report.allocations[0].demand, 0,
4876            "GitHub reported no queued runs"
4877        );
4878        assert_eq!(
4879            report.allocations[0].desired, 2,
4880            "min_capacity raised the target above demand"
4881        );
4882        assert_eq!(
4883            report.allocations[0].limiting_factor,
4884            LimitingFactor::MinCapacity
4885        );
4886        assert_eq!(report.started, 2, "and two runners were actually started");
4887        assert_eq!(harness.launcher.live_count(), 2);
4888    }
4889
4890    // =======================================================================
4891    // Capacity, at the boundaries
4892    // =======================================================================
4893
4894    #[tokio::test]
4895    async fn demand_above_max_capacity_is_clamped_to_max_capacity() {
4896        let mut harness = Harness::simple(100, 10, "acme/app");
4897        let report = harness
4898            .reconciler
4899            .reconcile(&[policy(1, "acme/app", 3)])
4900            .await;
4901
4902        assert_eq!(report.allocations[0].demand, 10);
4903        assert_eq!(
4904            report.allocations[0].desired, 3,
4905            "max_capacity beats demand"
4906        );
4907        assert_eq!(report.started, 3);
4908        assert_eq!(
4909            report.allocations[0].limiting_factor,
4910            LimitingFactor::MaxCapacity
4911        );
4912    }
4913
4914    #[tokio::test]
4915    async fn demand_below_min_capacity_starts_nothing_in_v1() {
4916        // D7 fixes `min_capacity` at 0, so "below the floor" is "no demand", and
4917        // the product requirement it satisfies is "no idle runners when unused".
4918        let mut harness = Harness::simple(8, 0, "acme/app");
4919        let report = harness
4920            .reconciler
4921            .reconcile(&[policy(1, "acme/app", 4)])
4922            .await;
4923
4924        assert_eq!(report.allocations[0].desired, 0);
4925        assert_eq!(report.started, 0);
4926        assert!(report.starts_nothing());
4927    }
4928
4929    #[tokio::test]
4930    async fn lifecycle_replacement_intent_is_consumed_by_the_ordinary_allocator() {
4931        let policy = policy(1, "octo/repo", 1);
4932        let previous = attempt_in(AttemptState::Starting, 41, 1);
4933        let intent = ReplacementIntent {
4934            policy: policy.id,
4935            previous_attempt: previous.id,
4936            operation: "exit_before_acceptance_replacement",
4937        };
4938        let launcher = Arc::new(FakeLauncher::new().seeded(vec![previous]).replacing(intent));
4939        let demand = Arc::new(FakeDemand::ready(1, &repo("octo/repo")));
4940        let mut harness = Harness::build(
4941            host_with(1),
4942            Arc::clone(&launcher),
4943            demand,
4944            Arc::new(InProcessAllocationLock::new()),
4945        );
4946
4947        let report = harness.reconciler.reconcile(&[policy]).await;
4948
4949        assert_eq!(report.replacement_intents, 1);
4950        assert_eq!(report.started, 1);
4951        assert_eq!(launcher.launches(), 1);
4952        assert_eq!(launcher.live_count(), 1);
4953    }
4954
4955    #[tokio::test]
4956    async fn zero_host_headroom_starts_nothing_at_maximum_demand() {
4957        let launcher = Arc::new(FakeLauncher::new().seeded(vec![
4958            attempt_in(AttemptState::Busy, 1, 1),
4959            attempt_in(AttemptState::Busy, 2, 1),
4960        ]));
4961        let demand = Arc::new(FakeDemand::ready(u32::from(u16::MAX), &repo("acme/app")));
4962        let mut harness = Harness::build(
4963            host_with(2),
4964            launcher,
4965            demand,
4966            Arc::new(InProcessAllocationLock::new()),
4967        );
4968
4969        let report = harness
4970            .reconciler
4971            .reconcile(&[policy(1, "acme/app", 2)])
4972            .await;
4973        assert_eq!(report.started, 0);
4974        assert_eq!(report.allocations[0].headroom_before, 0);
4975        assert_eq!(harness.launcher.launches(), 0);
4976    }
4977
4978    #[tokio::test]
4979    async fn headroom_smaller_than_the_per_policy_allowance_wins() {
4980        // Four slots held by *another* policy on a host of six: this policy is
4981        // allowed five and gets two.
4982        let launcher = Arc::new(FakeLauncher::new().seeded(vec![
4983            attempt_in(AttemptState::Busy, 1, 99),
4984            attempt_in(AttemptState::Busy, 2, 99),
4985            attempt_in(AttemptState::Idle, 3, 99),
4986            attempt_in(AttemptState::Starting, 4, 99),
4987        ]));
4988        let demand = Arc::new(FakeDemand::ready(5, &repo("acme/app")));
4989        let mut harness = Harness::build(
4990            host_with(6),
4991            launcher,
4992            demand,
4993            Arc::new(InProcessAllocationLock::new()),
4994        );
4995
4996        let report = harness
4997            .reconciler
4998            .reconcile(&[policy(1, "acme/app", 5)])
4999            .await;
5000        assert_eq!(
5001            report.allocations[0].desired, 5,
5002            "its own ceiling allows five"
5003        );
5004        assert_eq!(report.started, 2, "the host has two slots free");
5005        assert_eq!(
5006            report.allocations[0].limiting_factor,
5007            LimitingFactor::HostCapacity
5008        );
5009        assert_eq!(harness.launcher.live_count(), 6);
5010    }
5011
5012    #[tokio::test]
5013    async fn the_idle_host_assertion_holds() {
5014        // "No demand means zero runner processes and zero attempts out of
5015        // terminal state."
5016        let mut harness = Harness::simple(8, 0, "acme/app");
5017        let report = harness
5018            .reconciler
5019            .reconcile(&[policy(1, "acme/app", 4), policy(2, "acme/app", 4)])
5020            .await;
5021
5022        assert_eq!(report.started, 0);
5023        assert_eq!(harness.launcher.launches(), 0);
5024        assert!(harness.launcher.snapshot().is_empty());
5025        assert_eq!(
5026            harness
5027                .launcher
5028                .snapshot()
5029                .iter()
5030                .filter(|a| !a.is_terminal())
5031                .count(),
5032            0
5033        );
5034    }
5035
5036    // =======================================================================
5037    // Live host capacity, including changes while a pass is creating runners.
5038    // =======================================================================
5039
5040    #[tokio::test]
5041    async fn live_capacity_increase_is_used_without_rebuilding_the_reconciler() {
5042        let mut harness = Harness::simple(1, 5, "acme/app");
5043        let policies = [policy(1, "acme/app", 5)];
5044        assert_eq!(harness.reconciler.reconcile(&policies).await.started, 1);
5045
5046        *harness.launcher.capacity.lock().unwrap() = NonZeroU16::new(4);
5047        assert_eq!(harness.reconciler.reconcile(&policies).await.started, 3);
5048        assert_eq!(harness.launcher.live_count(), 4);
5049    }
5050
5051    #[tokio::test]
5052    async fn live_capacity_decrease_keeps_busy_runners_and_waits_for_headroom() {
5053        let mut harness = Harness::simple(5, 5, "acme/app");
5054        let busy: Vec<_> = (1..=3)
5055            .map(|id| attempt_in(AttemptState::Busy, id, 1))
5056            .collect();
5057        *harness.launcher.attempts.lock().unwrap() = busy.clone();
5058        *harness.launcher.capacity.lock().unwrap() = NonZeroU16::new(2);
5059        let policies = [policy(1, "acme/app", 5)];
5060
5061        assert_eq!(harness.reconciler.reconcile(&policies).await.started, 0);
5062        assert_eq!(harness.launcher.snapshot(), busy);
5063        assert!(harness.launcher.cleaned().is_empty());
5064
5065        // Two jobs finish; one busy job still owns a slot.
5066        harness.launcher.attempts.lock().unwrap().truncate(1);
5067        assert_eq!(harness.reconciler.reconcile(&policies).await.started, 1);
5068        assert_eq!(harness.launcher.live_count(), 2);
5069    }
5070
5071    #[tokio::test]
5072    async fn live_capacity_is_rechecked_between_launches_in_the_same_pass() {
5073        let mut harness = Harness::simple(5, 5, "acme/app");
5074        *harness.launcher.capacity_after_launch.lock().unwrap() = NonZeroU16::new(1);
5075
5076        let report = harness
5077            .reconciler
5078            .reconcile(&[policy(1, "acme/app", 5)])
5079            .await;
5080        assert_eq!(report.started, 1);
5081        assert_eq!(harness.launcher.live_count(), 1);
5082    }
5083
5084    #[tokio::test]
5085    async fn live_capacity_unreadable_blocks_launches_until_the_journal_recovers() {
5086        let mut harness = Harness::simple(5, 5, "acme/app");
5087        harness
5088            .launcher
5089            .capacity_unreadable
5090            .store(true, Ordering::SeqCst);
5091        let policies = [policy(1, "acme/app", 5)];
5092        let report = harness.reconciler.reconcile(&policies).await;
5093        assert_eq!(report.started, 0);
5094        assert_eq!(report.host_capacity_unreadable, 1);
5095        assert_eq!(report.attempts_unreadable, 0);
5096        assert_eq!(harness.events.count_of("attempts_unreadable"), 0);
5097        assert!(
5098            harness
5099                .events
5100                .events()
5101                .contains(&LifecycleEvent::HostCapacityUnreadable {
5102                    reason: HostCapacityError::JournalUnreadable,
5103                })
5104        );
5105
5106        harness
5107            .launcher
5108            .capacity_unreadable
5109            .store(false, Ordering::SeqCst);
5110        *harness.launcher.capacity.lock().unwrap() = NonZeroU16::new(2);
5111        assert_eq!(harness.reconciler.reconcile(&policies).await.started, 2);
5112    }
5113
5114    #[tokio::test]
5115    async fn unreadable_local_state_does_not_treat_a_busy_host_as_idle() {
5116        for host_failure in [true, false] {
5117            let mut harness = Harness::simple(5, 0, "acme/app");
5118            harness
5119                .reconciler
5120                .set_intervals(RefreshInterval::default(), Duration::from_secs(10));
5121            *harness.launcher.attempts.lock().unwrap() = vec![attempt_in(AttemptState::Busy, 1, 1)];
5122            harness
5123                .launcher
5124                .capacity_unreadable
5125                .store(host_failure, Ordering::SeqCst);
5126            harness.launcher.fail_attempts(!host_failure);
5127            let report = harness
5128                .reconciler
5129                .reconcile(&[policy(1, "acme/app", 5)])
5130                .await;
5131            assert_eq!(report.host_capacity_unreadable, u16::from(host_failure));
5132            assert_eq!(report.next_poll.pace, PollPace::Nominal);
5133            assert_eq!(harness.launcher.live_count(), 1);
5134        }
5135    }
5136
5137    #[derive(Debug)]
5138    struct HostEditOnLock {
5139        inner: InProcessAllocationLock,
5140        launcher: Arc<FakeLauncher>,
5141        unreadable: bool,
5142    }
5143
5144    #[async_trait::async_trait]
5145    impl AllocationLock for HostEditOnLock {
5146        async fn acquire(&self) -> Result<AllocationGuard, AllocationLockBusy> {
5147            let guard = self.inner.acquire().await?;
5148            *self.launcher.capacity.lock().unwrap() = NonZeroU16::new(1);
5149            self.launcher
5150                .capacity_unreadable
5151                .store(self.unreadable, Ordering::SeqCst);
5152            Ok(guard)
5153        }
5154    }
5155
5156    #[tokio::test]
5157    async fn live_capacity_edits_and_read_failures_while_waiting_for_the_lock_block_launches() {
5158        for unreadable in [false, true] {
5159            let launcher =
5160                Arc::new(FakeLauncher::new().seeded(vec![attempt_in(AttemptState::Busy, 1, 1)]));
5161            let lock = Arc::new(HostEditOnLock {
5162                inner: InProcessAllocationLock::new(),
5163                launcher: Arc::clone(&launcher),
5164                unreadable,
5165            });
5166            let mut harness = Harness::build(
5167                host_with(5),
5168                Arc::clone(&launcher),
5169                Arc::new(FakeDemand::ready(5, &repo("acme/app"))),
5170                lock,
5171            );
5172            let report = harness
5173                .reconciler
5174                .reconcile(&[policy(1, "acme/app", 5)])
5175                .await;
5176            assert_eq!(report.started, 0);
5177            assert_eq!(launcher.live_count(), 1);
5178            assert!(launcher.cleaned().is_empty());
5179            assert_eq!(report.host_capacity_unreadable, u16::from(unreadable));
5180            assert_eq!(report.attempts_unreadable, 0);
5181        }
5182    }
5183
5184    // =======================================================================
5185    // D9 under concurrency: the other silent failure
5186    // =======================================================================
5187
5188    /// `e1`'s Definition of Done: *"Two policies on one host with
5189    /// `host_capacity` smaller than the sum of their `max_capacity` values never
5190    /// exceed `host_capacity` under concurrent reconciliation — asserted under
5191    /// simulated lock contention, with no duplicate runners."*
5192    ///
5193    /// The contention is simulated by [`FakeLauncher::with_yields`], which puts
5194    /// executor yield points *between* the launcher reading the attempt set and
5195    /// recording the new one. Without serialisation both tasks read a headroom
5196    /// of three and both spend it.
5197    ///
5198    /// # Watched failing before it was made to pass
5199    ///
5200    /// Granting from this lock without taking the inner mutex — leaving every
5201    /// counter and every yield point exactly as they are — fails this assertion
5202    /// with `left: 4, right: 3`: four runners on a host of three, from two
5203    /// policies each individually inside their own `max_capacity`. The control
5204    /// test below keeps that measurement standing permanently by running the
5205    /// same body against [`NoLock`].
5206    #[tokio::test(flavor = "current_thread")]
5207    async fn two_policies_reconciling_concurrently_never_exceed_host_capacity() {
5208        let lock = Arc::new(CountingLock::new());
5209        let (launches, live) =
5210            two_policies_concurrently(Arc::clone(&lock) as Arc<dyn AllocationLock>, None).await;
5211
5212        assert_eq!(
5213            launches, 3,
5214            "the sum across policies must never exceed host_capacity, and each policy is \
5215             individually within its own max_capacity of 3"
5216        );
5217        assert_eq!(live, 3, "and no duplicate runner survived the race");
5218        assert_eq!(
5219            lock.peak(),
5220            1,
5221            "the allocation lock had one holder at a time; without that the read of the \
5222             headroom and the creation of the runtime are not atomic"
5223        );
5224        assert!(
5225            (3..=5).contains(&lock.acquisitions()),
5226            "the lock is taken before *each* runtime, not once per pass: three runtimes \
5227             means at least three holds, and at most one further hold per policy to \
5228             discover the host filled up underneath it. It was taken {} times",
5229            lock.acquisitions()
5230        );
5231    }
5232
5233    /// The control for the test above: the same body with the lock removed.
5234    ///
5235    /// It exists so that the assertion above cannot pass vacuously. If a future
5236    /// change makes the unserialised path safe by accident — a launcher that
5237    /// records synchronously, say — this test goes red and says so, rather than
5238    /// the other one silently proving nothing.
5239    #[tokio::test(flavor = "current_thread")]
5240    async fn without_the_allocation_lock_two_policies_oversubscribe_the_host() {
5241        let (launches, _) =
5242            two_policies_concurrently(Arc::new(NoLock) as Arc<dyn AllocationLock>, None).await;
5243
5244        assert!(
5245            launches > 3,
5246            "with no serialisation both policies must be able to spend the same headroom; \
5247             they started {launches} runners on a host of 3. If this is ever 3, the \
5248             contention window closed and `two_policies_reconciling_concurrently_never_\
5249             exceed_host_capacity` has stopped proving anything"
5250        );
5251    }
5252
5253    /// Two policies, one host of three, each allowed three, reconciled at once.
5254    ///
5255    /// Returns `(launches, live attempts)`.
5256    async fn two_policies_concurrently(
5257        lock: Arc<dyn AllocationLock>,
5258        live_capacity: Option<NonZeroU16>,
5259    ) -> (usize, usize) {
5260        let launcher = Arc::new(FakeLauncher::new().with_yields(4));
5261        *launcher.capacity.lock().unwrap() = live_capacity;
5262        let host = host_with(3);
5263
5264        let mut left = Harness::build(
5265            host.clone(),
5266            Arc::clone(&launcher),
5267            Arc::new(FakeDemand::ready(3, &repo("acme/left"))),
5268            Arc::clone(&lock),
5269        )
5270        .reconciler;
5271        let mut right = Harness::build(
5272            host,
5273            Arc::clone(&launcher),
5274            Arc::new(FakeDemand::ready(3, &repo("acme/right"))),
5275            Arc::clone(&lock),
5276        )
5277        .reconciler;
5278
5279        let a = policy(1, "acme/left", 3);
5280        let b = policy(2, "acme/right", 3);
5281
5282        let left = tokio::spawn(async move { left.reconcile(&[a]).await });
5283        let right = tokio::spawn(async move { right.reconcile(&[b]).await });
5284        let (_, _) = (left.await.unwrap(), right.await.unwrap());
5285
5286        (launcher.launches(), launcher.live_count())
5287    }
5288
5289    #[tokio::test(flavor = "current_thread")]
5290    async fn live_capacity_is_shared_by_concurrent_targets_with_stale_startup_hosts() {
5291        let (launches, live) =
5292            two_policies_concurrently(Arc::new(InProcessAllocationLock::new()), NonZeroU16::new(2))
5293                .await;
5294        assert_eq!(launches, 2);
5295        assert_eq!(live, 2);
5296    }
5297
5298    // =======================================================================
5299    // D19: monitor-only
5300    // =======================================================================
5301
5302    /// `e1`'s Definition of Done: *"A `MonitorOnly` policy under maximum demand
5303    /// starts zero runners and issues no demand request."*
5304    ///
5305    /// Driven through `c4`'s real gateway fake so that "issued no demand
5306    /// request" is asserted against the thing that would have issued it, rather
5307    /// than against this module's own bookkeeping. `FakeGithub` records every
5308    /// call it is asked to make.
5309    #[tokio::test]
5310    async fn a_monitor_only_policy_under_maximum_demand_starts_nothing_and_polls_nothing() {
5311        let gateway = FakeGithub::new().with_queued_jobs(repo("acme/app"), jobs(10_000));
5312        let gateway = Arc::new(GatewayDemand::new(gateway, CancelToken::new()));
5313        let launcher = Arc::new(FakeLauncher::new());
5314        let events = Arc::new(EventLog::new());
5315
5316        let mut reconciler = Reconciler::new(
5317            host_with(10),
5318            ReconcilerPorts {
5319                demand: Arc::clone(&gateway) as Arc<dyn DemandSource>,
5320                launcher: Arc::clone(&launcher) as Arc<dyn RunnerLauncher>,
5321                lock: Arc::new(InProcessAllocationLock::new()),
5322                directory: Arc::new(FakeDirectory::default()),
5323                clock: Arc::new(FakeClock::default()),
5324                jitter: Arc::new(NoJitter),
5325                events: Arc::clone(&events) as Arc<dyn EventSink>,
5326            },
5327        );
5328
5329        let monitor = fixtures::policy()
5330            .id(PolicyId::from_u128(1))
5331            .repository("acme/app")
5332            .monitor_only()
5333            .active()
5334            .build();
5335
5336        let report = reconciler.reconcile(&[monitor]).await;
5337
5338        assert_eq!(report.started, 0);
5339        assert_eq!(launcher.launches(), 0);
5340        assert_eq!(report.monitor_only, vec![PolicyId::from_u128(1)]);
5341        assert_eq!(
5342            report.demand_requests, 0,
5343            "a monitor-only policy spends nothing from the shared hourly ceiling"
5344        );
5345        assert!(
5346            gateway.gateway().calls().is_empty(),
5347            "a monitor-only policy issued a demand request: {:?}",
5348            gateway.gateway().calls()
5349        );
5350        assert_eq!(events.count_of("monitor_only_skipped"), 1);
5351        assert_eq!(
5352            events.count_of("demand_observed"),
5353            0,
5354            "and it contributed no demand"
5355        );
5356    }
5357
5358    /// D19 says a monitor-only policy is *"skipped entirely by
5359    /// reconciliation"*, and "entirely" is the load-bearing word once two
5360    /// policies share a target.
5361    ///
5362    /// This defect was found by review rather than by the test above, which
5363    /// cannot see it: there, the monitor-only policy is the *only* policy, so
5364    /// nobody polls its target and the lookup finds nothing. Give it a
5365    /// repository an autoscale policy already polls and the lookup succeeds —
5366    /// and the monitor-only policy was then allocated for and had a demand
5367    /// observation emitted on its behalf. It still started nothing, because
5368    /// `may_start_runners` is false for it and `HostAllocator` refuses it by
5369    /// name, so no ceiling was ever at risk. It simply was not skipped.
5370    ///
5371    /// Removing the `owns_runners` guard from the allocation loop was watched
5372    /// failing this test before it was restored:
5373    /// `a monitor-only policy was allocated for: [… limiting_factor:
5374    /// MonitorOnly]`.
5375    #[tokio::test]
5376    async fn a_monitor_only_policy_sharing_a_target_is_still_skipped_entirely() {
5377        let lock = Arc::new(CountingLock::new());
5378        let launcher = Arc::new(FakeLauncher::new());
5379        let events = Arc::new(EventLog::new());
5380        let mut reconciler = Reconciler::new(
5381            host_with(4),
5382            ReconcilerPorts {
5383                demand: Arc::new(FakeDemand::ready(2, &repo("acme/app"))),
5384                launcher: Arc::clone(&launcher) as Arc<dyn RunnerLauncher>,
5385                lock: Arc::clone(&lock) as Arc<dyn AllocationLock>,
5386                directory: Arc::new(FakeDirectory::default()),
5387                clock: Arc::new(FakeClock::default()),
5388                jitter: Arc::new(NoJitter),
5389                events: Arc::clone(&events) as Arc<dyn EventSink>,
5390            },
5391        );
5392
5393        let watcher = fixtures::policy()
5394            .id(PolicyId::from_u128(2))
5395            .repository("acme/app")
5396            .monitor_only()
5397            .active()
5398            .build();
5399
5400        let report = reconciler
5401            .reconcile(&[policy(1, "acme/app", 4), watcher])
5402            .await;
5403
5404        assert_eq!(report.started, 2, "the autoscale policy is served normally");
5405        assert_eq!(report.monitor_only, vec![PolicyId::from_u128(2)]);
5406        assert_eq!(
5407            events.count_of("demand_observed"),
5408            1,
5409            "the demand observation belongs to the autoscale policy alone"
5410        );
5411        assert!(
5412            report
5413                .allocations
5414                .iter()
5415                .all(|a| a.policy_id == PolicyId::from_u128(1)),
5416            "a monitor-only policy was allocated for: {:?}",
5417            report.allocations
5418        );
5419        assert_eq!(
5420            lock.acquisitions(),
5421            2,
5422            "one hold per runtime created, and none on behalf of the monitor-only policy. \
5423             It was three before the budget was checked at the top of the loop rather than \
5424             after the re-read, which cost every policy a surplus hold to discover there \
5425             was nothing left to grant"
5426        );
5427    }
5428
5429    #[tokio::test]
5430    async fn the_monitor_only_refusal_is_asserted_on_the_mode_not_on_a_missing_ceiling() {
5431        // The specification requires this to be asserted rather than deduced
5432        // from `max_capacity` being absent. `HostAllocator` reports it by name,
5433        // and this loop reaches that arm through `owns_runners`, which is a
5434        // question about the mode.
5435        let monitor = fixtures::monitor_only_policy();
5436        assert!(!monitor.owns_runners());
5437        assert_eq!(monitor.max_capacity(), None);
5438
5439        let host = host_with(10);
5440        let attempts: Vec<RunnerAttempt> = Vec::new();
5441        let mut allocator = HostAllocator::from_attempts(&host, &attempts);
5442        let allocation = allocator.allocate(&monitor, 10_000);
5443        assert_eq!(allocation.limiting_factor, LimitingFactor::MonitorOnly);
5444        assert_eq!(allocation.to_start, 0);
5445        assert_eq!(
5446            allocator.headroom(),
5447            10,
5448            "and it consumes no headroom, so an autoscale policy on the same host is \
5449             unaffected"
5450        );
5451    }
5452
5453    // =======================================================================
5454    // The surplus runner, and busy protection
5455    // =======================================================================
5456
5457    /// `e1`'s Definition of Done: *"A surplus attempt that receives no job
5458    /// reaches a terminal state recorded as an idle exit, is cleaned, and is not
5459    /// reported as a failure."*
5460    #[tokio::test]
5461    async fn a_surplus_attempt_is_cleaned_as_an_idle_exit_and_not_as_a_failure() {
5462        let launcher = Arc::new(FakeLauncher::new().seeded(vec![
5463            concluded(1, 1, AttemptOutcome::ExitedIdleWithoutWork),
5464            concluded(
5465                2,
5466                1,
5467                AttemptOutcome::failed(FailureReason::JitRequestFailed),
5468            ),
5469            concluded(3, 1, AttemptOutcome::CompletedJob),
5470        ]));
5471        let mut harness = Harness::build(
5472            host_with(4),
5473            Arc::clone(&launcher),
5474            Arc::new(FakeDemand::ready(0, &repo("acme/app"))),
5475            Arc::new(InProcessAllocationLock::new()),
5476        );
5477
5478        let report = harness
5479            .reconciler
5480            .reconcile(&[policy(1, "acme/app", 4)])
5481            .await;
5482
5483        assert_eq!(report.cleaned, 3);
5484        assert_eq!(report.idle_exits, 1, "the surplus case, counted apart");
5485        assert_eq!(
5486            report.failures, 1,
5487            "only the failed attempt is a failure; the idle exit and the completed job are \
5488             not"
5489        );
5490        assert_eq!(launcher.cleaned().len(), 3);
5491        assert!(launcher.snapshot().is_empty());
5492
5493        let cleaned: Vec<OutcomeKind> = harness
5494            .events
5495            .events()
5496            .into_iter()
5497            .filter_map(|event| match event {
5498                LifecycleEvent::AttemptCleaned { outcome, .. } => Some(outcome),
5499                _ => None,
5500            })
5501            .collect();
5502        assert!(cleaned.contains(&OutcomeKind::IdleExit));
5503        assert!(
5504            !OutcomeKind::IdleExit.is_failure(),
5505            "an idle exit rendered as a failure sends an operator hunting a fault that does \
5506             not exist"
5507        );
5508    }
5509
5510    /// `e1`'s Definition of Done: *"A scale-down request with a busy attempt
5511    /// removes nothing and leaves the attempt `busy`."*
5512    #[tokio::test]
5513    async fn scale_down_removes_nothing_from_a_busy_attempt() {
5514        let busy = attempt_in(AttemptState::Busy, 1, 1);
5515        let launcher = Arc::new(FakeLauncher::new().seeded(vec![
5516            busy.clone(),
5517            attempt_in(AttemptState::Starting, 2, 1),
5518            concluded(3, 1, AttemptOutcome::CompletedJob),
5519        ]));
5520        let harness = Harness::build(
5521            host_with(4),
5522            Arc::clone(&launcher),
5523            Arc::new(FakeDemand::ready(0, &repo("acme/app"))),
5524            Arc::new(InProcessAllocationLock::new()),
5525        );
5526
5527        let report = harness
5528            .reconciler
5529            .scale_down(&policy(1, "acme/app", 4))
5530            .await;
5531
5532        assert_eq!(report.refused_busy, 1);
5533        assert_eq!(
5534            report.retained, 1,
5535            "the `starting` attempt is not ended either"
5536        );
5537        assert_eq!(report.removed, 1, "only the concluded attempt is reclaimed");
5538
5539        let after = launcher.snapshot();
5540        let still_busy = after
5541            .iter()
5542            .find(|a| a.id == AttemptId::from_u128(1))
5543            .expect("the busy attempt is still there");
5544        assert_eq!(
5545            still_busy.state(),
5546            AttemptState::Busy,
5547            "scale-down removed nothing from a runner that is executing a job, and left it \
5548             busy"
5549        );
5550        assert!(!launcher.cleaned().contains(&AttemptId::from_u128(1)));
5551
5552        // And the domain refuses it from the other side too, by name, so a
5553        // future caller that tried anyway would not get a generic transition
5554        // error.
5555        let mut busy = busy;
5556        assert!(matches!(
5557            busy.clean(fixtures::created_at()),
5558            Err(runner_manager_domain::attempt::AttemptError::BusyCannotBeCleaned)
5559        ));
5560        assert_eq!(harness.events.count_of("scale_down_refused"), 1);
5561    }
5562
5563    // =======================================================================
5564    // The schedule
5565    // =======================================================================
5566
5567    #[test]
5568    fn the_default_interval_is_sixty_seconds_and_the_floor_is_thirty() {
5569        assert_eq!(RefreshInterval::DEFAULT_SECS, 60);
5570        assert_eq!(RefreshInterval::MIN_SECS, 30);
5571        assert_eq!(PollSchedule::floor(), Duration::from_secs(30));
5572        assert!(
5573            RefreshInterval::from_secs(29).is_err(),
5574            "the floor is a rate-budget constraint, and a caller must not be able to write \
5575             a shorter interval at all"
5576        );
5577
5578        let mut schedule = PollSchedule::new(RefreshInterval::default());
5579        let next = schedule.next_poll(None, fixtures::created_at(), &NoJitter);
5580        assert_eq!(next.delay, Duration::from_secs(60));
5581        assert_eq!(next.pace, PollPace::Nominal);
5582
5583        let mut floored = PollSchedule::new(RefreshInterval::from_secs(30).unwrap());
5584        assert_eq!(
5585            floored
5586                .next_poll(None, fixtures::created_at(), &NoJitter)
5587                .delay,
5588            Duration::from_secs(30)
5589        );
5590    }
5591
5592    /// `e1`'s Definition of Done: *"The poll interval … increases under a
5593    /// rate-limit signal, and the increase is visible in emitted state rather
5594    /// than silent."*
5595    #[test]
5596    fn a_rate_limit_increases_the_delay_and_names_itself() {
5597        let now = fixtures::created_at();
5598        let mut schedule = PollSchedule::new(RefreshInterval::default());
5599
5600        let limited = RefreshState::RateLimited(RateLimited {
5601            kind: RateLimitKind::Secondary,
5602            retry_after: Some(Duration::from_secs(300)),
5603            remaining: None,
5604            reset_unix_secs: None,
5605        });
5606        let next = schedule.next_poll(Some(&limited), now, &NoJitter);
5607
5608        assert_eq!(next.delay, Duration::from_secs(300));
5609        assert_eq!(
5610            next.pace,
5611            PollPace::RateLimited {
5612                kind: RateLimitKind::Secondary
5613            },
5614            "the increase is reported, never hidden"
5615        );
5616        assert!(next.pace.is_throttled());
5617        assert_eq!(next.pace.as_str(), "rate_limited_secondary");
5618    }
5619
5620    /// Constraint on this task: *"Read `RefreshState::retry_delay` as an
5621    /// absolute floor, not an addend."*
5622    #[test]
5623    fn the_retry_delay_is_an_absolute_floor_and_never_an_addend() {
5624        let now = fixtures::created_at();
5625        let mut schedule = PollSchedule::new(RefreshInterval::default());
5626
5627        let limited = RefreshState::RateLimited(RateLimited {
5628            kind: RateLimitKind::Primary,
5629            retry_after: Some(Duration::from_secs(300)),
5630            remaining: Some(0),
5631            reset_unix_secs: None,
5632        });
5633
5634        // Five successive answers, each carrying the window that is *left*.
5635        // An addend would compound: 360, 660, 960 … and look like a hang.
5636        for _ in 0..5 {
5637            let next = schedule.next_poll(Some(&limited), now, &NoJitter);
5638            assert_eq!(
5639                next.delay,
5640                Duration::from_secs(300),
5641                "the delay is `max(interval, retry_delay)`; `interval + retry_delay` would \
5642                 have compounded on every successive retry"
5643            );
5644        }
5645
5646        // And when GitHub asks for less than the interval, the interval wins:
5647        // the floor is never crossed to catch up.
5648        let brief = RefreshState::RateLimited(RateLimited {
5649            kind: RateLimitKind::Secondary,
5650            retry_after: Some(Duration::from_secs(5)),
5651            remaining: None,
5652            reset_unix_secs: None,
5653        });
5654        let next = schedule.next_poll(Some(&brief), now, &NoJitter);
5655        assert_eq!(
5656            next.delay,
5657            Duration::from_secs(60),
5658            "a short `retry-after` may not drop the loop below its own interval"
5659        );
5660        assert!(next.delay >= PollSchedule::floor());
5661    }
5662
5663    #[test]
5664    fn no_branch_of_the_schedule_can_go_below_the_thirty_second_floor() {
5665        let now = fixtures::created_at();
5666        let states = [
5667            None,
5668            Some(RefreshState::Offline),
5669            Some(RefreshState::RateLimited(RateLimited {
5670                kind: RateLimitKind::Secondary,
5671                retry_after: Some(Duration::from_secs(1)),
5672                remaining: None,
5673                reset_unix_secs: None,
5674            })),
5675            Some(RefreshState::LockedOut {
5676                retry_after: Duration::from_secs(1),
5677            }),
5678            Some(RefreshState::Unauthorized),
5679            Some(RefreshState::Forbidden { message: None }),
5680            Some(RefreshState::Failed {
5681                status: Some(500),
5682                message: "server error".into(),
5683            }),
5684            Some(RefreshState::Cancelled),
5685        ];
5686
5687        for state in &states {
5688            let mut schedule = PollSchedule::new(RefreshInterval::from_secs(30).unwrap());
5689            let next = schedule.next_poll(state.as_ref(), now, &NoJitter);
5690            assert!(
5691                next.delay >= PollSchedule::floor(),
5692                "{state:?} scheduled a poll {}ms away, under the 30-second floor",
5693                next.delay.as_millis()
5694            );
5695        }
5696    }
5697
5698    #[test]
5699    fn an_offline_run_backs_off_with_jitter_and_a_recovery_resets_it() {
5700        let now = fixtures::created_at();
5701        let mut schedule = PollSchedule::new(RefreshInterval::default());
5702
5703        // Doubling, from the nominal interval.
5704        let mut previous = Duration::ZERO;
5705        for consecutive in 1..=6_u32 {
5706            let next = schedule.next_poll(Some(&RefreshState::Offline), now, &NoJitter);
5707            assert_eq!(next.pace, PollPace::Offline { consecutive });
5708            assert!(
5709                next.delay >= previous,
5710                "the back-off must not shrink while the outage continues"
5711            );
5712            assert!(next.delay >= Duration::from_secs(60));
5713            previous = next.delay;
5714        }
5715        assert!(previous <= MAX_OFFLINE_BACKOFF, "and it is capped");
5716
5717        // Jitter widens the delay rather than narrowing it, so a fleet of
5718        // agents does not retry in lockstep.
5719        let mut jittered = PollSchedule::new(RefreshInterval::default());
5720        let none = jittered.next_poll(Some(&RefreshState::Offline), now, &NoJitter);
5721        let mut jittered = PollSchedule::new(RefreshInterval::default());
5722        let full = jittered.next_poll(Some(&RefreshState::Offline), now, &FixedJitter(0.999));
5723        assert!(full.delay > none.delay);
5724        assert!(full.delay <= none.delay.mul_f64(1.0 + JITTER_RATIO));
5725
5726        // Recovery resets the run with no bookkeeping of its own.
5727        assert_eq!(schedule.consecutive_offline(), 6);
5728        let recovered = schedule.next_poll(None, now, &NoJitter);
5729        assert_eq!(recovered.pace, PollPace::Nominal);
5730        assert_eq!(recovered.delay, Duration::from_secs(60));
5731        assert_eq!(schedule.consecutive_offline(), 0);
5732    }
5733
5734    #[test]
5735    fn the_offline_state_states_the_twenty_four_hour_bound() {
5736        assert_eq!(
5737            GITHUB_CANCELS_QUEUED_JOBS_AFTER,
5738            Duration::from_secs(24 * 60 * 60)
5739        );
5740
5741        let brief = OfflineState::new(1, Duration::from_secs(120));
5742        let rendered = brief.to_string();
5743        assert!(rendered.contains("24 hours"), "{rendered}");
5744        assert!(rendered.contains("Retrying in 120s"), "{rendered}");
5745        assert!(!brief.has_outlasted_the_queue());
5746
5747        let long = brief.since(GITHUB_CANCELS_QUEUED_JOBS_AFTER + Duration::from_secs(1));
5748        assert!(long.has_outlasted_the_queue());
5749        assert!(
5750            long.to_string().contains("queued work has been lost"),
5751            "{long}"
5752        );
5753
5754        // "We cannot tell" is not "not yet".
5755        assert!(!OfflineState::new(9, Duration::from_secs(60)).has_outlasted_the_queue());
5756    }
5757
5758    // =======================================================================
5759    // Idle polling and the shared quota
5760    // =======================================================================
5761
5762    fn quota(remaining: u64, reset_in_secs: i64, now: Timestamp) -> RateLimitSnapshot {
5763        RateLimitSnapshot {
5764            limit: 5_000,
5765            remaining,
5766            used: Some(5_000 - remaining),
5767            reset_unix_secs: u64::try_from(now.timestamp() + reset_in_secs).expect("positive"),
5768            observed_at: now,
5769        }
5770    }
5771
5772    fn quiet() -> PollConditions {
5773        PollConditions {
5774            quiet: true,
5775            quota: None,
5776            requests_per_pass: 0,
5777        }
5778    }
5779
5780    #[test]
5781    fn a_quiet_pass_polls_at_the_idle_interval_and_a_busy_one_at_the_active_interval() {
5782        let now = fixtures::created_at();
5783        let mut schedule =
5784            PollSchedule::new(RefreshInterval::default()).with_idle(Duration::from_secs(10));
5785
5786        let idle = schedule.next_poll_with(None, quiet(), now, &NoJitter);
5787        assert_eq!(idle.delay, Duration::from_secs(10));
5788        assert_eq!(idle.pace, PollPace::Idle);
5789        assert!(!idle.pace.is_throttled(), "idle is faster, not a slowdown");
5790
5791        let busy = schedule.next_poll_with(None, PollConditions::default(), now, &NoJitter);
5792        assert_eq!(busy.delay, Duration::from_secs(60));
5793        assert_eq!(busy.pace, PollPace::Nominal);
5794    }
5795
5796    #[test]
5797    fn a_schedule_never_told_an_idle_interval_keeps_its_active_one() {
5798        let now = fixtures::created_at();
5799        let mut schedule = PollSchedule::new(RefreshInterval::default());
5800        assert_eq!(
5801            schedule.next_poll_with(None, quiet(), now, &NoJitter).delay,
5802            Duration::from_secs(60)
5803        );
5804    }
5805
5806    #[test]
5807    fn an_idle_interval_is_clamped_between_its_floor_and_the_active_interval() {
5808        let schedule = PollSchedule::new(RefreshInterval::from_secs(30).expect("the floor"))
5809            .with_idle(Duration::from_secs(120));
5810        assert_eq!(
5811            schedule.idle(),
5812            Duration::from_secs(30),
5813            "an idle interval above the active one would make quiet targets slower to react"
5814        );
5815        let schedule =
5816            PollSchedule::new(RefreshInterval::default()).with_idle(Duration::from_secs(1));
5817        assert_eq!(schedule.idle(), PollSchedule::idle_floor());
5818    }
5819
5820    /// The owner's fourth requirement: when the shared quota runs low, stretch
5821    /// the interval automatically, and say so.
5822    #[test]
5823    fn a_low_quota_stretches_polling_and_wins_over_idle() {
5824        let now = fixtures::created_at();
5825        let mut schedule =
5826            PollSchedule::new(RefreshInterval::default()).with_idle(Duration::from_secs(10));
5827
5828        let healthy = schedule.next_poll_with(
5829            None,
5830            PollConditions {
5831                quiet: true,
5832                quota: Some(quota(1_000, 3_000, now)),
5833                requests_per_pass: 0,
5834            },
5835            now,
5836            &NoJitter,
5837        );
5838        assert_eq!(healthy.pace, PollPace::Idle, "a fifth left is still normal");
5839
5840        let low = schedule.next_poll_with(
5841            None,
5842            PollConditions {
5843                quiet: true,
5844                quota: Some(quota(999, 3_000, now)),
5845                requests_per_pass: 0,
5846            },
5847            now,
5848            &NoJitter,
5849        );
5850        assert_eq!(low.delay, Duration::from_secs(120));
5851        assert_eq!(
5852            low.pace,
5853            PollPace::Stretched {
5854                pressure: PrimaryPressure::Low
5855            }
5856        );
5857        assert!(low.pace.is_throttled());
5858        assert_eq!(low.pace.as_str(), "stretched_low_budget");
5859
5860        let critical = schedule.next_poll_with(
5861            None,
5862            PollConditions {
5863                quiet: false,
5864                quota: Some(quota(100, 3_000, now)),
5865                requests_per_pass: 0,
5866            },
5867            now,
5868            &NoJitter,
5869        );
5870        assert_eq!(critical.delay, Duration::from_secs(240));
5871        assert_eq!(critical.pace.as_str(), "stretched_critical_budget");
5872    }
5873
5874    #[test]
5875    fn a_stretch_never_outlasts_the_quotas_reset_nor_undercuts_the_active_interval() {
5876        let now = fixtures::created_at();
5877        let mut schedule = PollSchedule::new(RefreshInterval::default());
5878
5879        let soon = schedule.next_poll_with(
5880            None,
5881            PollConditions {
5882                quiet: false,
5883                quota: Some(quota(10, 90, now)),
5884                requests_per_pass: 0,
5885            },
5886            now,
5887            &NoJitter,
5888        );
5889        assert_eq!(
5890            soon.delay,
5891            Duration::from_secs(90),
5892            "the bucket refills in 90 s; waiting 240 s buys nothing"
5893        );
5894
5895        let imminent = schedule.next_poll_with(
5896            None,
5897            PollConditions {
5898                quiet: false,
5899                quota: Some(quota(10, 5, now)),
5900                requests_per_pass: 0,
5901            },
5902            now,
5903            &NoJitter,
5904        );
5905        assert_eq!(imminent.delay, Duration::from_secs(60));
5906
5907        let expired = schedule.next_poll_with(
5908            None,
5909            PollConditions {
5910                quiet: true,
5911                quota: Some(quota(10, -5, now)),
5912                requests_per_pass: 0,
5913            },
5914            now,
5915            &NoJitter,
5916        );
5917        assert_eq!(
5918            expired.pace,
5919            PollPace::Nominal,
5920            "a reading from a window that has reset says nothing; the schedule here had no \
5921             idle interval, so it is the active one"
5922        );
5923    }
5924
5925    #[test]
5926    fn an_idle_target_too_large_to_poll_that_often_stretches_its_idle_interval() {
5927        let now = fixtures::created_at();
5928        let mut schedule =
5929            PollSchedule::new(RefreshInterval::default()).with_idle(Duration::from_secs(10));
5930        let small = schedule.next_poll_with(
5931            None,
5932            PollConditions {
5933                quiet: true,
5934                quota: None,
5935                requests_per_pass: 40,
5936            },
5937            now,
5938            &NoJitter,
5939        );
5940        assert_eq!(small.delay, Duration::from_secs(10), "ten repositories fit");
5941        let organization = schedule.next_poll_with(
5942            None,
5943            PollConditions {
5944                quiet: true,
5945                quota: None,
5946                requests_per_pass: 400,
5947            },
5948            now,
5949            &NoJitter,
5950        );
5951        assert_eq!(
5952            organization.delay,
5953            Duration::from_secs(80),
5954            "400 requests a pass at 300 a minute is one pass every 80 s"
5955        );
5956        assert_eq!(organization.pace, PollPace::Idle);
5957    }
5958
5959    #[test]
5960    fn a_rate_limit_wait_is_jittered_upward_only() {
5961        let now = fixtures::created_at();
5962        let mut schedule = PollSchedule::new(RefreshInterval::default());
5963        let limited = RefreshState::RateLimited(RateLimited {
5964            kind: RateLimitKind::Secondary,
5965            retry_after: Some(Duration::from_secs(300)),
5966            remaining: None,
5967            reset_unix_secs: None,
5968        });
5969        assert_eq!(
5970            schedule
5971                .next_poll(Some(&limited), now, &FixedJitter(0.5))
5972                .delay,
5973            Duration::from_secs(315),
5974            "every loop shares one quiet period; without jitter they all wake together"
5975        );
5976        assert_eq!(
5977            schedule.next_poll(Some(&limited), now, &NoJitter).delay,
5978            Duration::from_secs(300),
5979            "never shorter than GitHub asked"
5980        );
5981    }
5982
5983    #[tokio::test]
5984    async fn demand_this_host_did_not_serve_keeps_the_target_off_the_idle_pace() {
5985        // Capacity zero would refuse the policy outright; a host already full
5986        // is the ordinary way matched demand goes unserved for a pass.
5987        let launcher =
5988            Arc::new(FakeLauncher::new().seeded(vec![attempt_in(AttemptState::Busy, 9, 9)]));
5989        let demand = Arc::new(FakeDemand::ready(2, &repo("acme/app")));
5990        let mut harness = Harness::build(
5991            host_with(1),
5992            launcher,
5993            demand,
5994            Arc::new(InProcessAllocationLock::new()),
5995        );
5996        harness
5997            .reconciler
5998            .set_intervals(RefreshInterval::default(), Duration::from_secs(10));
5999        let policy = policy(1, "acme/app", 4);
6000
6001        let report = harness
6002            .reconciler
6003            .reconcile(std::slice::from_ref(&policy))
6004            .await;
6005
6006        assert_eq!(report.started, 0, "the host is full");
6007        assert_eq!(
6008            report.next_poll.pace,
6009            PollPace::Nominal,
6010            "matched demand it could not serve is work waiting, not idle"
6011        );
6012    }
6013
6014    #[tokio::test]
6015    async fn a_target_with_nothing_to_do_is_polled_at_the_idle_interval() {
6016        let mut harness = Harness::simple(4, 0, "acme/app");
6017        harness
6018            .reconciler
6019            .set_intervals(RefreshInterval::default(), Duration::from_secs(10));
6020        let policy = policy(1, "acme/app", 4);
6021
6022        let report = harness
6023            .reconciler
6024            .reconcile(std::slice::from_ref(&policy))
6025            .await;
6026
6027        assert_eq!(report.next_poll.pace, PollPace::Idle);
6028        assert_eq!(report.next_poll.delay, Duration::from_secs(10));
6029    }
6030
6031    #[tokio::test]
6032    async fn a_target_whose_runs_are_changing_is_polled_at_the_active_interval() {
6033        let mut harness = Harness::simple(4, 0, "acme/app");
6034        harness
6035            .reconciler
6036            .set_intervals(RefreshInterval::default(), Duration::from_secs(10));
6037        harness.demand.set(PollOutcome::Ready(
6038            QueuedDemand::of(repo("acme/app"), Vec::new()).with_busy(repo("acme/app")),
6039        ));
6040        let policy = policy(1, "acme/app", 4);
6041
6042        let report = harness
6043            .reconciler
6044            .reconcile(std::slice::from_ref(&policy))
6045            .await;
6046
6047        assert_eq!(report.next_poll.pace, PollPace::Nominal);
6048        assert_eq!(report.next_poll.delay, Duration::from_secs(60));
6049    }
6050
6051    #[tokio::test]
6052    async fn a_target_with_a_live_runner_is_polled_at_the_active_interval() {
6053        let launcher =
6054            Arc::new(FakeLauncher::new().seeded(vec![attempt_in(AttemptState::Busy, 1, 1)]));
6055        let demand = Arc::new(FakeDemand::ready(0, &repo("acme/app")));
6056        let mut harness = Harness::build(
6057            host_with(4),
6058            launcher,
6059            demand,
6060            Arc::new(InProcessAllocationLock::new()),
6061        );
6062        harness
6063            .reconciler
6064            .set_intervals(RefreshInterval::default(), Duration::from_secs(10));
6065        let policy = policy(1, "acme/app", 4);
6066
6067        let report = harness
6068            .reconciler
6069            .reconcile(std::slice::from_ref(&policy))
6070            .await;
6071
6072        assert_eq!(
6073            report.next_poll.pace,
6074            PollPace::Nominal,
6075            "a job is running: its follow-on work is what the active interval watches for"
6076        );
6077    }
6078
6079    #[tokio::test]
6080    async fn a_pass_that_starts_a_runner_is_not_idle() {
6081        let mut harness = Harness::simple(4, 2, "acme/app");
6082        harness
6083            .reconciler
6084            .set_intervals(RefreshInterval::default(), Duration::from_secs(10));
6085        let policy = policy(1, "acme/app", 4);
6086
6087        let report = harness
6088            .reconciler
6089            .reconcile(std::slice::from_ref(&policy))
6090            .await;
6091
6092        assert!(report.started > 0);
6093        assert_eq!(report.next_poll.pace, PollPace::Nominal);
6094    }
6095
6096    #[tokio::test]
6097    async fn the_quota_reported_with_a_reading_stretches_the_next_poll() {
6098        let mut harness = Harness::simple(4, 0, "acme/app");
6099        harness
6100            .reconciler
6101            .set_intervals(RefreshInterval::default(), Duration::from_secs(10));
6102        let now = harness.clock.now();
6103        harness.demand.set(PollOutcome::Ready(
6104            QueuedDemand::of(repo("acme/app"), Vec::new())
6105                .with_rate_limit(Some(quota(200, 1_800, now))),
6106        ));
6107        let policy = policy(1, "acme/app", 4);
6108
6109        let report = harness
6110            .reconciler
6111            .reconcile(std::slice::from_ref(&policy))
6112            .await;
6113
6114        assert_eq!(
6115            report.next_poll.pace,
6116            PollPace::Stretched {
6117                pressure: PrimaryPressure::Critical
6118            }
6119        );
6120        assert_eq!(report.next_poll.delay, Duration::from_secs(240));
6121    }
6122
6123    // =======================================================================
6124    // Offline, end to end
6125    // =======================================================================
6126
6127    /// `e1`'s Definition of Done: *"An unreachable GitHub yields `offline`, zero
6128    /// new runners, retained existing processes, and jittered backoff; recovery
6129    /// resumes polling and does not double-count a job that was already being
6130    /// served."*
6131    #[tokio::test]
6132    async fn an_unreachable_github_starts_nothing_retains_everything_and_backs_off() {
6133        let live = vec![
6134            attempt_in(AttemptState::Busy, 1, 1),
6135            attempt_in(AttemptState::Starting, 2, 1),
6136        ];
6137        let launcher = Arc::new(FakeLauncher::new().seeded(live.clone()));
6138        let demand = Arc::new(FakeDemand::failing(RefreshState::Offline));
6139        let mut harness = Harness::build(
6140            host_with(8),
6141            Arc::clone(&launcher),
6142            Arc::clone(&demand),
6143            Arc::new(InProcessAllocationLock::new()),
6144        );
6145        let policy = policy(1, "acme/app", 8);
6146
6147        let report = harness
6148            .reconciler
6149            .reconcile(std::slice::from_ref(&policy))
6150            .await;
6151
6152        assert!(report.is_offline());
6153        assert_eq!(report.started, 0, "no new runner during an outage");
6154        assert_eq!(launcher.launches(), 0);
6155        assert_eq!(
6156            launcher.snapshot(),
6157            live,
6158            "existing runner processes are retained, untouched"
6159        );
6160        assert_eq!(report.unreadable, vec![PolicyId::from_u128(1)]);
6161        assert_eq!(report.next_poll.pace, PollPace::Offline { consecutive: 1 });
6162        assert!(report.next_poll.delay >= Duration::from_secs(60));
6163        let offline = report.offline_state().expect("an offline state to display");
6164        assert!(offline.to_string().contains("24 hours"));
6165
6166        // Recovery: two jobs are queued, but only the starting runner can
6167        // cover them. The busy runner consumes capacity and serves other work.
6168        demand.set(PollOutcome::Ready(QueuedDemand::of(
6169            repo("acme/app"),
6170            jobs(2),
6171        )));
6172        let recovered = harness.reconciler.reconcile(&[policy]).await;
6173
6174        assert!(!recovered.is_offline());
6175        assert_eq!(recovered.next_poll.pace, PollPace::Nominal);
6176        assert_eq!(
6177            recovered.started, 1,
6178            "two queued jobs and one starting runner leave one job uncovered"
6179        );
6180        assert_eq!(recovered.allocations[0].active_owned, 2);
6181        assert_eq!(launcher.live_count(), 3);
6182    }
6183
6184    /// One unreachable target must not idle a whole host.
6185    ///
6186    /// The failure that decides the *schedule* is the most severe across every
6187    /// target polled — backing the whole loop off during an outage is the safe
6188    /// error, and `f3` runs one reconciler per target anyway, so in production
6189    /// the two are usually the same thing. What must not follow from that is
6190    /// refusing to serve a policy whose own target answered perfectly well, and
6191    /// the two are easy to conflate because the offline reading is sitting in
6192    /// the same map.
6193    #[tokio::test]
6194    async fn one_offline_target_does_not_stop_a_reachable_one() {
6195        let mut harness = Harness::simple(8, 0, "acme/app");
6196        harness.demand.set_for(
6197            &ScaleTarget::repository("acme/app").unwrap(),
6198            PollOutcome::Ready(QueuedDemand::of(repo("acme/app"), jobs(2))),
6199        );
6200        harness.demand.set_for(
6201            &ScaleTarget::repository("acme/broken").unwrap(),
6202            PollOutcome::Failed(RefreshState::Offline),
6203        );
6204
6205        let report = harness
6206            .reconciler
6207            .reconcile(&[policy(1, "acme/app", 4), policy(2, "acme/broken", 4)])
6208            .await;
6209
6210        assert_eq!(
6211            report.started, 2,
6212            "the reachable target was served; an unreachable sibling repository must not \
6213             idle the host"
6214        );
6215        assert_eq!(report.unreadable, vec![PolicyId::from_u128(2)]);
6216        assert_eq!(harness.demand.polls().len(), 2, "both targets were polled");
6217
6218        // And the schedule takes the worse of the two.
6219        assert!(report.is_offline());
6220        assert_eq!(report.next_poll.pace, PollPace::Offline { consecutive: 1 });
6221    }
6222
6223    /// The 24-hour bound has to be reachable in production, not only in a unit
6224    /// test of [`OfflineState`].
6225    ///
6226    /// This was a real gap: the reconciler built its offline state from the
6227    /// back-off count alone, so `offline_for` was always `None` and
6228    /// [`OfflineState::has_outlasted_the_queue`] could never be true outside a
6229    /// test that constructed the value by hand. An operator whose agent had been
6230    /// offline for two days would have been told that an outage longer than 24
6231    /// hours *would* lose queued work, in the future tense, having already lost
6232    /// it.
6233    ///
6234    /// The elapsed time is measured from the first poll of the run rather than
6235    /// derived from the interval, because the back-off doubles and the two
6236    /// diverge immediately.
6237    #[tokio::test]
6238    async fn a_day_long_outage_says_that_queued_work_has_already_been_lost() {
6239        let clock = Arc::new(FakeClock::default());
6240        let launcher = Arc::new(FakeLauncher::new());
6241        let demand = Arc::new(FakeDemand::failing(RefreshState::Offline));
6242        let mut reconciler = Reconciler::new(
6243            host_with(4),
6244            ReconcilerPorts {
6245                demand: Arc::clone(&demand) as Arc<dyn DemandSource>,
6246                launcher: Arc::clone(&launcher) as Arc<dyn RunnerLauncher>,
6247                lock: Arc::new(InProcessAllocationLock::new()),
6248                directory: Arc::new(FakeDirectory::default()),
6249                clock: Arc::clone(&clock) as Arc<dyn Clock>,
6250                jitter: Arc::new(NoJitter),
6251                events: Arc::new(NoEvents),
6252            },
6253        );
6254        let policy = policy(1, "acme/app", 4);
6255
6256        // The outage begins.
6257        let first = reconciler.reconcile(std::slice::from_ref(&policy)).await;
6258        let state = first.offline_state().expect("an offline state");
6259        assert!(!state.has_outlasted_the_queue());
6260        assert!(
6261            state
6262                .to_string()
6263                .contains("an outage longer than that loses"),
6264            "{state}"
6265        );
6266
6267        // A day and a minute later, still unreachable.
6268        clock.advance_secs(24 * 60 * 60 + 60);
6269        let later = reconciler.reconcile(std::slice::from_ref(&policy)).await;
6270        let state = later.offline_state().expect("an offline state");
6271        assert!(state.has_outlasted_the_queue());
6272        assert!(
6273            state.to_string().contains("queued work has been lost"),
6274            "{state}"
6275        );
6276        assert_eq!(launcher.launches(), 0, "and still nothing was started");
6277
6278        // Recovery closes the run, so a *later* outage measures from itself
6279        // rather than from the first one.
6280        demand.set(PollOutcome::Ready(QueuedDemand::of(
6281            repo("acme/app"),
6282            jobs(0),
6283        )));
6284        let recovered = reconciler.reconcile(std::slice::from_ref(&policy)).await;
6285        assert!(recovered.offline_state().is_none());
6286        assert_eq!(reconciler.schedule().offline_for(clock.now()), None);
6287
6288        demand.set(PollOutcome::Failed(RefreshState::Offline));
6289        let again = reconciler.reconcile(std::slice::from_ref(&policy)).await;
6290        assert!(
6291            !again
6292                .offline_state()
6293                .expect("an offline state")
6294                .has_outlasted_the_queue(),
6295            "a new outage must not inherit the age of the one before it"
6296        );
6297    }
6298
6299    /// Finding 5: the adapter, not the lock underneath it.
6300    ///
6301    /// `d1` covers `LockKind::Allocation` including a contended `acquire_at`
6302    /// with a wait. What that does not reach is this adapter: the
6303    /// `spawn_blocking` wrapper, the collapse of both a refused lock and a
6304    /// panicked blocking task into `AllocationLockBusy`, and — the one that
6305    /// would be silent — whether [`AllocationGuard`] really holds the
6306    /// `HostLock`, since dropping it is the only release there is. A guard that
6307    /// dropped the lock on the way out would make every acquisition succeed and
6308    /// the ceiling would hold by luck.
6309    ///
6310    /// The original disclosure said this needed a real filesystem and was
6311    /// therefore expensive. `AppPaths::rooted_at` plus `tempfile` — already a
6312    /// non-dev dependency of this crate — makes it about fifteen lines, so the
6313    /// reason was weaker than stated.
6314    #[tokio::test]
6315    async fn the_file_allocation_lock_excludes_a_second_holder_and_releases_on_drop() {
6316        let root = tempfile::tempdir().expect("a temporary directory");
6317        let paths = Arc::new(runner_manager_platform::paths::AppPaths::rooted_at(
6318            root.path(),
6319        ));
6320        let lock = FileAllocationLock::new(paths).with_wait(Duration::from_millis(50));
6321
6322        let held = lock.acquire().await.expect("an uncontended lock is free");
6323        assert!(
6324            matches!(lock.acquire().await, Err(AllocationLockBusy)),
6325            "a second holder was admitted; on Unix the lock is per open file description \
6326             and on Windows the share mode denies write, so this must be refused even \
6327             from inside the same process"
6328        );
6329
6330        drop(held);
6331        let regained = lock.acquire().await;
6332        assert!(
6333            regained.is_ok(),
6334            "dropping the guard is the only release there is, so a guard that does not \
6335             hold the `HostLock` leaves it held forever"
6336        );
6337    }
6338
6339    #[test]
6340    fn tee_events_reaches_both_sinks() {
6341        // `f3` wires the log sink and `g2`'s buffer at once, and an event that
6342        // reached only one of them would be an activity view missing lines the
6343        // log file has, or the reverse.
6344        let left = Arc::new(EventLog::new());
6345        let right = Arc::new(EventLog::new());
6346        let tee = TeeEvents(
6347            Arc::clone(&left) as Arc<dyn EventSink>,
6348            Arc::clone(&right) as Arc<dyn EventSink>,
6349        );
6350
6351        tee.emit(LifecycleEvent::MonitorOnlySkipped {
6352            policy: PolicyId::from_u128(1),
6353        });
6354
6355        assert_eq!(left.count_of("monitor_only_skipped"), 1);
6356        assert_eq!(right.count_of("monitor_only_skipped"), 1);
6357    }
6358
6359    // =======================================================================
6360    // Budget: the repository list, and the per-target poll
6361    // =======================================================================
6362
6363    #[tokio::test]
6364    async fn the_repository_list_refreshes_far_more_slowly_than_the_demand_poll() {
6365        let clock = Arc::new(FakeClock::default());
6366        let directory = Arc::new(FakeDirectory::of(vec![repo("acme/one"), repo("acme/two")]));
6367        let cache = RepositoryCache::new(
6368            Arc::clone(&directory) as Arc<dyn RepositoryDirectory>,
6369            Arc::clone(&clock) as Arc<dyn Clock>,
6370            RefreshInterval::default(),
6371        );
6372        let target = ScaleTarget::organization("acme").unwrap();
6373
6374        assert_eq!(
6375            cache.ttl(),
6376            Duration::from_secs(60 * u64::from(REPOSITORY_LIST_REFRESH_MULTIPLE))
6377        );
6378
6379        // Every poll inside the window reuses the list.
6380        for _ in 0..REPOSITORY_LIST_REFRESH_MULTIPLE {
6381            let scope = cache.scope_for(&target).await.unwrap();
6382            assert_eq!(scope.repositories().len(), 2);
6383            clock.advance_secs(60);
6384        }
6385        assert_eq!(
6386            directory.calls(),
6387            1,
6388            "re-listing an organization at demand-poll frequency is what exhausts the \
6389             shared request budget"
6390        );
6391        assert_eq!(cache.lookups(), 1);
6392
6393        // Past it, exactly one more.
6394        cache.scope_for(&target).await.unwrap();
6395        assert_eq!(directory.calls(), 2);
6396    }
6397
6398    #[tokio::test]
6399    async fn a_repository_target_never_consults_the_directory() {
6400        let directory = Arc::new(FakeDirectory::of(vec![repo("acme/other")]));
6401        let cache = RepositoryCache::new(
6402            Arc::clone(&directory) as Arc<dyn RepositoryDirectory>,
6403            Arc::new(FakeClock::default()) as Arc<dyn Clock>,
6404            RefreshInterval::default(),
6405        );
6406        let target = ScaleTarget::repository("acme/app").unwrap();
6407
6408        let scope = cache.scope_for(&target).await.unwrap();
6409        assert_eq!(scope.repositories(), &[repo("acme/app")]);
6410        assert_eq!(directory.calls(), 0);
6411    }
6412
6413    #[tokio::test]
6414    async fn two_policies_on_one_target_cost_one_demand_poll_not_two() {
6415        // `04-subsystem-contracts.md` prices a *target*. A loop that spent per
6416        // policy would exceed the projection `f2` admitted the configuration
6417        // against, silently.
6418        let mut harness = Harness::simple(8, 4, "o/r");
6419        harness.demand.set(PollOutcome::Ready(QueuedDemand::of(
6420            repo("o/r"),
6421            [
6422                fixtures::queued_job(&[HOST_LABEL]),
6423                fixtures::queued_job(&[HOST_LABEL]),
6424                fixtures::queued_job(&["rm-home-win-x64-py-isolated"]),
6425                fixtures::queued_job(&["rm-home-win-x64-py-isolated"]),
6426            ],
6427        )));
6428        let mut isolated = fixtures::named_policy("py-isolated", PolicyId::from_u128(2));
6429        isolated.activate().unwrap();
6430        let report = harness
6431            .reconciler
6432            .reconcile(&[policy(1, "o/r", 2), isolated])
6433            .await;
6434
6435        assert_eq!(harness.demand.polls().len(), 1);
6436        assert_eq!(report.targets_read, 1, "profiles share one target reading");
6437        assert_eq!(
6438            report.demand_requests,
6439            runner_manager_github::demand::DEMAND_REQUESTS_PER_REPOSITORY_PER_POLL,
6440            "one repository's worth of demand requests, not two policies' worth. Read              from the constant rather than written as a literal so that repricing the              poll cannot silently turn this into an assertion about the wrong thing"
6441        );
6442        assert_eq!(
6443            report.started, 4,
6444            "native and isolated profiles each get two"
6445        );
6446    }
6447
6448    #[tokio::test]
6449    async fn subset_labels_without_selector_scale_only_the_compatible_local_profile() {
6450        use runner_manager_domain::model::{Arch, HostLabel, Os};
6451        use runner_manager_domain::policy::RoutingLabels;
6452        let mut harness = Harness::build(
6453            fixtures::host()
6454                .os(Os::MacOs)
6455                .architecture(Arch::Arm64)
6456                .capacity(10)
6457                .build(),
6458            Arc::new(FakeLauncher::new()),
6459            Arc::new(FakeDemand::ready(0, &repo("o/r"))),
6460            Arc::new(InProcessAllocationLock::new()),
6461        );
6462        harness.demand.set(PollOutcome::Ready(QueuedDemand::of(
6463            repo("o/r"),
6464            [
6465                fixtures::queued_job(&["self-hosted", "macOS", "ARM64"]),
6466                fixtures::queued_job(&["SELF-HOSTED", "MACOS", "arm64"]),
6467                fixtures::queued_job(&["self-hosted", "macOS", "X64"]),
6468                fixtures::queued_job(&["self-hosted", "Windows", "ARM64"]),
6469                fixtures::queued_job(&["self-hosted", "macOS", "ARM64", "gpu"]),
6470                fixtures::queued_job(&["rm-other-osx-arm64", "macOS", "ARM64"]),
6471            ],
6472        )));
6473        let mut local = fixtures::policy()
6474            .id(PolicyId::from_u128(1))
6475            .repository("o/r")
6476            .autoscale("macmini", 10)
6477            .routing_labels(RoutingLabels::derive(
6478                &HostLabel::new("macmini").unwrap(),
6479                Os::MacOs,
6480                Arch::Arm64,
6481            ))
6482            .active()
6483            .build();
6484        for label in ["self-hosted", "macos", "arm64", "extra"] {
6485            local.add_routing_label(fixtures::label(label)).unwrap();
6486        }
6487        let mut foreign = local.to_persisted();
6488        foreign.id = PolicyId::from_u128(2);
6489        foreign.host_id = HostId::from_u128(0xdead);
6490        let foreign = ScalePolicy::from_persisted(foreign).unwrap();
6491        let report = harness.reconciler.reconcile(&[local, foreign]).await;
6492        assert_eq!(report.started, 2);
6493        assert_eq!(report.targets_read, 1);
6494        assert!(harness.events.events().iter().any(|event| matches!(
6495            event,
6496            LifecycleEvent::DemandObserved {
6497                demand: 2,
6498                not_matched: 4,
6499                ..
6500            }
6501        )));
6502        assert!(
6503            !harness
6504                .events
6505                .events()
6506                .iter()
6507                .any(|event| matches!(event, LifecycleEvent::RoutingRefused { .. }))
6508        );
6509    }
6510
6511    #[tokio::test]
6512    async fn isolated_overlap_never_falls_back_to_native_in_either_state_or_order() {
6513        use runner_manager_domain::execution::{
6514            Backend, ExecutionPolicy, ImageReference, ResourceLimits,
6515        };
6516        let mut native = policy(1, "o/r", 2);
6517        native
6518            .add_routing_label(fixtures::label("self-hosted"))
6519            .unwrap();
6520        let mut isolated = fixtures::named_policy("py-isolated", PolicyId::from_u128(2));
6521        isolated
6522            .add_routing_label(fixtures::label("self-hosted"))
6523            .unwrap();
6524        isolated
6525            .set_execution_policy(ExecutionPolicy::Isolated {
6526                backend: Backend::Oci,
6527                image: ImageReference::new(format!(
6528                    "registry.example/runner@sha256:{}",
6529                    "a".repeat(64)
6530                ))
6531                .unwrap(),
6532                resources: ResourceLimits {
6533                    cpu_millis: 1000,
6534                    memory_mib: 512,
6535                    disk_mib: 1024,
6536                },
6537            })
6538            .unwrap();
6539        assert!(!isolated.may_start_runners());
6540        let mut active_isolated = isolated.clone();
6541        active_isolated.activate().unwrap();
6542        for profiles in [
6543            [native.clone(), isolated.clone()],
6544            [isolated.clone(), native.clone()],
6545            [native.clone(), active_isolated.clone()],
6546            [active_isolated, native.clone()],
6547        ] {
6548            let mut harness = Harness::simple(4, 0, "o/r");
6549            harness.demand.set(PollOutcome::Ready(QueuedDemand::of(
6550                repo("o/r"),
6551                [fixtures::queued_job(&["SELF-HOSTED"])],
6552            )));
6553            let report = harness.reconciler.reconcile(&profiles).await;
6554            assert_eq!(report.started, 0);
6555            assert!(harness.events.events().iter().any(|event| matches!(
6556                event,
6557                LifecycleEvent::RoutingRefused {
6558                    reason: RoutingRefusal::OverlappingProfiles,
6559                    count: 1,
6560                    ..
6561                }
6562            )));
6563        }
6564        // A job explicitly requiring the unavailable isolated selector cannot
6565        // be served by the native sibling either.
6566        let mut harness = Harness::simple(4, 0, "o/r");
6567        harness.demand.set(PollOutcome::Ready(QueuedDemand::of(
6568            repo("o/r"),
6569            [fixtures::queued_job(&["rm-home-win-x64-py-isolated"])],
6570        )));
6571        assert_eq!(
6572            harness
6573                .reconciler
6574                .reconcile(&[native, isolated])
6575                .await
6576                .started,
6577            0
6578        );
6579    }
6580
6581    #[tokio::test]
6582    async fn shared_optional_labels_do_not_duplicate_native_and_isolated_demand() {
6583        let mut harness = Harness::simple(2, 0, "o/r");
6584        harness.demand.set(PollOutcome::Ready(QueuedDemand::of(
6585            repo("o/r"),
6586            [
6587                fixtures::queued_job(&[HOST_LABEL, "gpu"]),
6588                fixtures::queued_job(&["rm-home-win-x64-py-isolated", "gpu"]),
6589                fixtures::queued_job(&["gpu"]),
6590                fixtures::unresolvable_job(),
6591            ],
6592        )));
6593        let mut native = policy(1, "o/r", 2);
6594        native.add_routing_label(fixtures::label("gpu")).unwrap();
6595        let mut isolated = fixtures::named_policy("py-isolated", PolicyId::from_u128(2));
6596        isolated.add_routing_label(fixtures::label("gpu")).unwrap();
6597        isolated.activate().unwrap();
6598
6599        let report = harness.reconciler.reconcile(&[native, isolated]).await;
6600        assert_eq!(harness.demand.polls().len(), 1);
6601        assert_eq!(report.targets_read, 1);
6602        assert_eq!(report.started, 2);
6603        assert_eq!(report.allocations.len(), 2);
6604        assert!(
6605            report
6606                .allocations
6607                .iter()
6608                .all(|allocation| allocation.to_start == 1)
6609        );
6610        assert!(harness.events.events().iter().any(|event| matches!(
6611            event,
6612            LifecycleEvent::RoutingRefused {
6613                reason: RoutingRefusal::OverlappingProfiles,
6614                count: 1,
6615                ..
6616            }
6617        )));
6618        assert_eq!(
6619            harness
6620                .events
6621                .events()
6622                .iter()
6623                .filter(|event| matches!(
6624                    event,
6625                    LifecycleEvent::DemandObserved {
6626                        unresolvable: 1,
6627                        ..
6628                    }
6629                ))
6630                .count(),
6631            2,
6632            "an expression stays unresolvable for both profiles"
6633        );
6634    }
6635
6636    #[tokio::test]
6637    async fn missing_required_selector_labels_do_not_match_and_overlaps_show_remedies() {
6638        let mut harness = Harness::simple(4, 0, "o/r");
6639        harness.demand.set(PollOutcome::Ready(QueuedDemand::of(
6640            repo("o/r"),
6641            [fixtures::queued_job(&[
6642                HOST_LABEL,
6643                "rm-home-win-x64-py-isolated",
6644            ])],
6645        )));
6646        let mut isolated = fixtures::named_policy("py-isolated", PolicyId::from_u128(2));
6647        isolated.activate().unwrap();
6648        let report = harness
6649            .reconciler
6650            .reconcile(&[policy(1, "o/r", 2), isolated])
6651            .await;
6652        assert_eq!(report.started, 0);
6653        assert_eq!(
6654            harness
6655                .events
6656                .events()
6657                .iter()
6658                .filter(|event| matches!(
6659                    event,
6660                    LifecycleEvent::DemandObserved {
6661                        demand: 0,
6662                        not_matched: 1,
6663                        ..
6664                    }
6665                ))
6666                .count(),
6667            2
6668        );
6669
6670        // Domain constructors permit extra selector-shaped labels. Routing
6671        // depends on all required labels and unique ownership, not their shape.
6672        // The store separately rejects sibling selector reuse on write/load.
6673        let mut superset = policy(1, "o/r", 2);
6674        assert!(
6675            superset
6676                .add_routing_label(fixtures::label("rm-home-win-x64-py-isolated"))
6677                .unwrap()
6678        );
6679        let mut sibling = fixtures::named_policy("py-isolated", PolicyId::from_u128(2));
6680        sibling.activate().unwrap();
6681        let mut unique = Harness::simple(4, 0, "o/r");
6682        unique.demand.set(PollOutcome::Ready(QueuedDemand::of(
6683            repo("o/r"),
6684            [fixtures::queued_job(&[
6685                HOST_LABEL,
6686                "rm-home-win-x64-py-isolated",
6687            ])],
6688        )));
6689        let report = unique.reconciler.reconcile(&[superset, sibling]).await;
6690        assert_eq!(report.started, 1);
6691        assert_eq!(
6692            report
6693                .allocations
6694                .iter()
6695                .find(|allocation| allocation.to_start == 1)
6696                .unwrap()
6697                .policy_id,
6698            PolicyId::from_u128(1)
6699        );
6700        assert!(
6701            !unique
6702                .events
6703                .events()
6704                .iter()
6705                .any(|event| matches!(event, LifecycleEvent::RoutingRefused { .. }))
6706        );
6707
6708        let mut overlap = Harness::simple(4, 1, "o/r");
6709        let report = overlap
6710            .reconciler
6711            .reconcile(&[policy(1, "o/r", 2), policy(2, "o/r", 2)])
6712            .await;
6713        assert_eq!(report.started, 0);
6714        assert!(overlap.events.events().iter().any(|event| matches!(
6715            event,
6716            LifecycleEvent::RoutingRefused {
6717                reason: RoutingRefusal::OverlappingProfiles,
6718                count: 1,
6719                ..
6720            }
6721        )));
6722        assert!(
6723            RoutingRefusal::OverlappingProfiles
6724                .to_string()
6725                .contains("repair")
6726        );
6727    }
6728
6729    // =======================================================================
6730    // Failure paths
6731    // =======================================================================
6732
6733    #[tokio::test]
6734    async fn a_failed_launch_stops_the_run_and_is_reported_without_free_text() {
6735        let launcher = Arc::new(FakeLauncher::new());
6736        launcher.fail_next(FailureReason::Other("token ghp_0123456789abcdef".into()));
6737        let mut harness = Harness::build(
6738            host_with(4),
6739            Arc::clone(&launcher),
6740            Arc::new(FakeDemand::ready(3, &repo("acme/app"))),
6741            Arc::new(InProcessAllocationLock::new()),
6742        );
6743
6744        let report = harness
6745            .reconciler
6746            .reconcile(&[policy(1, "acme/app", 4)])
6747            .await;
6748        assert_eq!(report.started, 0);
6749        assert_eq!(report.allocations[0].to_start, 3, "the decision stands");
6750
6751        let failures: Vec<&'static str> = harness
6752            .events
6753            .events()
6754            .into_iter()
6755            .filter_map(|event| match event {
6756                LifecycleEvent::RunnerStartFailed { reason, .. } => Some(reason),
6757                _ => None,
6758            })
6759            .collect();
6760        assert_eq!(failures, vec!["other"]);
6761        assert!(
6762            !failures[0].contains("ghp_"),
6763            "an event carried a `FailureReason::Other` detail verbatim"
6764        );
6765    }
6766
6767    /// The host-fitness preflight: while a required host check fails, the pass
6768    /// still decides what it would start, but nothing reaches the inner
6769    /// launcher -- so nothing is registered with GitHub -- and the event names
6770    /// `host_unfit`. Clearing the flag lets the next pass start runners.
6771    #[tokio::test]
6772    async fn an_unfit_host_registers_no_runner_and_reports_host_unfit() {
6773        let launcher = Arc::new(FakeLauncher::new());
6774        let unfit = Arc::new(AtomicBool::new(true));
6775        let events = Arc::new(EventLog::new());
6776        let mut reconciler = Reconciler::new(
6777            host_with(4),
6778            ReconcilerPorts {
6779                demand: Arc::new(FakeDemand::ready(2, &repo("acme/app"))),
6780                launcher: Arc::new(FitnessGatedLauncher::new(
6781                    Arc::clone(&launcher) as Arc<dyn RunnerLauncher>,
6782                    Arc::clone(&unfit),
6783                )),
6784                lock: Arc::new(InProcessAllocationLock::new()),
6785                directory: Arc::new(FakeDirectory::default()),
6786                clock: Arc::new(FakeClock::default()) as Arc<dyn Clock>,
6787                jitter: Arc::new(NoJitter) as Arc<dyn Jitter>,
6788                events: Arc::clone(&events) as Arc<dyn EventSink>,
6789            },
6790        );
6791        let policies = [policy(1, "acme/app", 4)];
6792
6793        let report = reconciler.reconcile(&policies).await;
6794        assert_eq!(report.started, 0);
6795        assert_eq!(launcher.launches(), 0, "the inner launcher must not run");
6796        let reasons: Vec<&'static str> = events
6797            .events()
6798            .into_iter()
6799            .filter_map(|event| match event {
6800                LifecycleEvent::RunnerStartFailed { reason, .. } => Some(reason),
6801                _ => None,
6802            })
6803            .collect();
6804        assert_eq!(reasons, ["host_unfit"]);
6805
6806        unfit.store(false, Ordering::Release);
6807        let report = reconciler.reconcile(&policies).await;
6808        assert_eq!(report.started, 2);
6809        assert_eq!(launcher.launches(), 2);
6810    }
6811
6812    #[tokio::test]
6813    async fn a_held_allocation_lock_starts_nothing_and_says_so() {
6814        let launcher = Arc::new(FakeLauncher::new());
6815        let mut harness = Harness::build(
6816            host_with(4),
6817            Arc::clone(&launcher),
6818            Arc::new(FakeDemand::ready(3, &repo("acme/app"))),
6819            Arc::new(HeldLock),
6820        );
6821
6822        let report = harness
6823            .reconciler
6824            .reconcile(&[policy(1, "acme/app", 4)])
6825            .await;
6826        assert_eq!(report.started, 0);
6827        assert_eq!(
6828            report.deferred, 3,
6829            "three runners were granted and none was created; `deferred` counts grants, \
6830             not policies -- it reported `1` when a policy that launched two of five and \
6831             then lost the lock had left three unstarted"
6832        );
6833        assert_eq!(launcher.launches(), 0);
6834        assert_eq!(harness.events.count_of("allocation_deferred"), 1);
6835        assert!(
6836            harness
6837                .events
6838                .events()
6839                .iter()
6840                .any(|event| matches!(event, LifecycleEvent::AllocationDeferred { count: 3, .. })),
6841            "the event carries the same number the report does"
6842        );
6843        assert_eq!(
6844            report.allocations.len(),
6845            1,
6846            "the intent is still reported, so an operator staring at a queue sees why \
6847             nothing started"
6848        );
6849    }
6850
6851    #[tokio::test]
6852    async fn a_foreign_or_draining_policy_is_reported_by_name_and_polls_nothing() {
6853        let mut harness = Harness::simple(8, 5, "acme/app");
6854
6855        let foreign = fixtures::policy()
6856            .id(PolicyId::from_u128(1))
6857            .repository("acme/app")
6858            .host(HostId::from_u128(0xdead))
6859            .autoscale("office", 4)
6860            .active()
6861            .build();
6862        let mut draining = policy(2, "acme/app", 4);
6863        draining.request_disable().unwrap();
6864
6865        let report = harness.reconciler.reconcile(&[foreign, draining]).await;
6866
6867        assert_eq!(report.started, 0);
6868        assert_eq!(
6869            harness.demand.polls().len(),
6870            0,
6871            "neither can act on an answer"
6872        );
6873        let factors: Vec<LimitingFactor> = report
6874            .allocations
6875            .iter()
6876            .map(|a| a.limiting_factor)
6877            .collect();
6878        assert!(factors.contains(&LimitingFactor::ForeignHost));
6879        assert!(factors.contains(&LimitingFactor::NotReconciling));
6880    }
6881
6882    #[tokio::test]
6883    async fn an_unreadable_repository_list_makes_the_target_unreadable_not_empty() {
6884        // Polling a scope nobody chose would report a demand number for the
6885        // wrong set of repositories, which is worse than reporting nothing.
6886        #[derive(Debug)]
6887        struct BrokenDirectory;
6888
6889        #[async_trait::async_trait]
6890        impl RepositoryDirectory for BrokenDirectory {
6891            async fn repositories(&self, _org: &Org) -> Result<Vec<OwnerRepo>, InventoryError> {
6892                Err(InventoryError::Cancelled)
6893            }
6894        }
6895
6896        let launcher = Arc::new(FakeLauncher::new());
6897        let events = Arc::new(EventLog::new());
6898        let mut reconciler = Reconciler::new(
6899            host_with(4),
6900            ReconcilerPorts {
6901                demand: Arc::new(FakeDemand::default()),
6902                launcher: Arc::clone(&launcher) as Arc<dyn RunnerLauncher>,
6903                lock: Arc::new(InProcessAllocationLock::new()),
6904                directory: Arc::new(BrokenDirectory),
6905                clock: Arc::new(FakeClock::default()),
6906                jitter: Arc::new(NoJitter),
6907                events: Arc::clone(&events) as Arc<dyn EventSink>,
6908            },
6909        );
6910
6911        let org_policy = fixtures::policy()
6912            .id(PolicyId::from_u128(1))
6913            .organization("acme")
6914            .autoscale("home", 4)
6915            .active()
6916            .build();
6917
6918        let report = reconciler.reconcile(&[org_policy]).await;
6919        assert_eq!(report.started, 0);
6920        assert_eq!(report.unreadable, vec![PolicyId::from_u128(1)]);
6921        assert_eq!(events.count_of("target_unreadable"), 1);
6922    }
6923
6924    // =======================================================================
6925    // What the events may carry
6926    // =======================================================================
6927
6928    /// One value of every [`LifecycleEvent`] variant.
6929    ///
6930    /// Hand-written, and what keeps it honest is the wildcard-free `match` in
6931    /// [`LifecycleEvent::name`]: adding a variant stops that compiling and puts
6932    /// the author here. The same residual `b1` records for `FailureReason::ALL`
6933    /// applies — an author who writes the `name` arm and forgets this list gets
6934    /// a green suite with the variant unscanned.
6935    fn every_event() -> Vec<LifecycleEvent> {
6936        let policy = PolicyId::from_u128(0xabcd_ef01);
6937        let attempt = AttemptId::from_u128(0x1234_5678);
6938        vec![
6939            LifecycleEvent::DemandObserved {
6940                policy,
6941                demand: u32::MAX,
6942                not_matched: u32::MAX,
6943                unresolvable: u32::MAX,
6944                complete: false,
6945            },
6946            LifecycleEvent::TargetUnreadable {
6947                policy,
6948                reason: unreadable_reason(&RefreshState::Failed {
6949                    status: Some(500),
6950                    message: "Authorization: Bearer ghp_0123456789abcdefghijklmnopqrstuvwxyz"
6951                        .into(),
6952                }),
6953            },
6954            LifecycleEvent::Allocated {
6955                policy,
6956                demand: u32::MAX,
6957                desired: u16::MAX,
6958                active_owned: 7,
6959                headroom: 9,
6960                to_start: 2,
6961                limiting: LimitingFactor::HostCapacity,
6962            },
6963            LifecycleEvent::MonitorOnlySkipped { policy },
6964            LifecycleEvent::RunnerStarted { policy, attempt },
6965            LifecycleEvent::RunnerStartFailed {
6966                policy,
6967                reason: failure_reason_kind(&FailureReason::Other(
6968                    "Authorization: Bearer ghp_0123456789abcdefghijklmnopqrstuvwxyz".into(),
6969                )),
6970            },
6971            LifecycleEvent::AllocationDeferred { policy, count: 4 },
6972            LifecycleEvent::AttemptsUnreadable {
6973                reason: failure_reason_kind(&FailureReason::Other(
6974                    "x-api-key: ghp_0123456789abcdefghijklmnopqrstuvwxyz".into(),
6975                )),
6976            },
6977            LifecycleEvent::HostCapacityUnreadable {
6978                reason: HostCapacityError::MissingHost,
6979            },
6980            LifecycleEvent::HostCapacityUnreadable {
6981                reason: HostCapacityError::JournalUnreadable,
6982            },
6983            LifecycleEvent::AttemptCleanFailed {
6984                policy,
6985                attempt,
6986                reason: failure_reason_kind(&FailureReason::ProcessExitedUnexpectedly),
6987            },
6988            LifecycleEvent::AttemptCleaned {
6989                policy,
6990                attempt,
6991                outcome: OutcomeKind::IdleExit,
6992            },
6993            LifecycleEvent::ScaleDownRefused { policy, attempt },
6994            LifecycleEvent::PollScheduled {
6995                retry_in_ms: 900_000,
6996                pace: PollPace::RateLimited {
6997                    kind: RateLimitKind::Primary,
6998                },
6999            },
7000        ]
7001    }
7002
7003    /// `e1`'s Definition of Done: *"No emitted event contains a token, a JIT
7004    /// blob, or a credential header."*
7005    ///
7006    /// Asserted by rendering every variant and putting the result through `d1`'s
7007    /// own scrubber: if any of it looked like a credential to the redactor that
7008    /// guards the log file, the round trip would not be the identity. The
7009    /// positive control at the bottom is what stops that assertion passing
7010    /// because the scrubber is asleep.
7011    #[test]
7012    fn no_emitted_event_can_carry_a_credential() {
7013        use runner_manager_platform::logging::redact;
7014
7015        for event in every_event() {
7016            let displayed = event.to_string();
7017            assert_eq!(
7018                redact(&displayed),
7019                displayed,
7020                "`{}` renders something `d1`'s sink would have to redact",
7021                event.name()
7022            );
7023
7024            let debugged = format!("{event:?}");
7025            assert_eq!(
7026                redact(&debugged),
7027                debugged,
7028                "`{}`'s Debug renders something `d1`'s sink would have to redact",
7029                event.name()
7030            );
7031        }
7032
7033        // The control: the scrubber is awake, and would have caught a credential
7034        // had one been there.
7035        let secret = "Authorization: Bearer ghp_0123456789abcdefghijklmnopqrstuvwxyz";
7036        assert_ne!(
7037            redact(secret),
7038            secret,
7039            "the scan above proves nothing if `redact` no longer recognises a credential"
7040        );
7041    }
7042
7043    #[test]
7044    fn every_field_name_this_sink_emits_is_one_d1_allows() {
7045        use runner_manager_platform::logging::is_field_allowed;
7046
7047        // The names `TracingEvents` writes. Kept beside the sink rather than
7048        // derived from it, because a derived list would move with the code and
7049        // assert nothing.
7050        for field in [
7051            "event",
7052            "policy_id",
7053            "attempt_id",
7054            "attempt_state",
7055            "demand",
7056            "desired",
7057            "capacity",
7058            "headroom",
7059            "count",
7060            "reason",
7061            "outcome",
7062            "mode",
7063            "lock",
7064            "retry_in_ms",
7065            "state",
7066        ] {
7067            assert!(
7068                is_field_allowed(field),
7069                "`{field}` is not on `d1`'s allow-list, so this sink would emit \
7070                 `[redacted]` in its place and the line would lose its meaning"
7071            );
7072        }
7073    }
7074
7075    #[test]
7076    fn every_failure_reason_has_a_credential_free_kind() {
7077        for reason in FailureReason::ALL {
7078            let kind = failure_reason_kind(&reason);
7079            assert!(!kind.is_empty());
7080            assert!(
7081                kind.chars().all(|c| c.is_ascii_lowercase() || c == '_'),
7082                "`{kind}` is not a fixed identifier"
7083            );
7084        }
7085        assert_eq!(
7086            failure_reason_kind(&FailureReason::Other("ghp_secret".into())),
7087            "other",
7088            "the detail of an `Other` reason never reaches an event"
7089        );
7090        let categories = [
7091            IsolationProviderFailure::NotInstalled,
7092            IsolationProviderFailure::PermissionDenied,
7093            IsolationProviderFailure::ImageUnavailableOrIncompatible,
7094            IsolationProviderFailure::DiskQuotaUnavailable,
7095            IsolationProviderFailure::RuntimeOperationFailed,
7096        ];
7097        let mut kinds = std::collections::BTreeSet::new();
7098        for category in categories {
7099            let kind = failure_reason_kind(&FailureReason::IsolationProvider(category));
7100            assert_ne!(kind, "other");
7101            assert!(!kind.contains("secret"));
7102            kinds.insert(kind);
7103        }
7104        assert_eq!(kinds.len(), categories.len());
7105    }
7106
7107    // =======================================================================
7108    // The two tripwires
7109    // =======================================================================
7110
7111    /// One source file's production half, with comment lines dropped.
7112    ///
7113    /// Both exclusions are `c4`'s, and load-bearing for the same reasons. The
7114    /// **test module** goes because the tests in it legitimately name the shapes
7115    /// they forbid — this module's own positive control is a literal
7116    /// `async fn acquire_jobs`, which would accuse the file of the thing it is
7117    /// proving it does not do. The **comments** go because this module's
7118    /// documentation explains the seam at length and has to name what does not
7119    /// exist in order to say why; a scan that forbade the explanation is a scan
7120    /// that gets the explanation deleted.
7121    fn production_half_of(source: &str) -> String {
7122        let production = source
7123            .split_once("\n#[cfg(test)]")
7124            .map_or(source, |(production, _)| production);
7125        production
7126            .lines()
7127            .filter(|line| !line.trim_start().starts_with("//"))
7128            .collect::<Vec<_>>()
7129            .join("\n")
7130    }
7131
7132    /// This file's own production half.
7133    fn this_file_above_its_tests_without_prose() -> String {
7134        production_half_of(include_str!("reconcile.rs"))
7135    }
7136
7137    /// The one normalisation both halves of the scan use.
7138    ///
7139    /// # This is a second copy of `crates/github/src/demand.rs`, deliberately
7140    ///
7141    /// `production_half_of`, this function, [`FORBIDDEN`] and
7142    /// `forbidden_shape_in` together duplicate `demand.rs:1530-1619`. Sharing
7143    /// them would mean putting them in `crates/testkit`, which `e1` does not
7144    /// own, so the copy was the only option available to this task.
7145    ///
7146    /// **It is worth consolidating later, and here is the specific hazard.**
7147    /// The last defect in `c4`'s copy was two spellings of "the same"
7148    /// normalisation drifting apart — the haystack lower-cased and the needle
7149    /// not — which made three of its seven assertions vacuously true from the
7150    /// day they were written. Two copies is the same hazard one level up. The
7151    /// mitigation inside *this* copy is that one function serves both the scan
7152    /// and its positive control, so a normaliser that stops matching fails the
7153    /// control loudly rather than passing the scan silently; what that cannot
7154    /// catch is this copy and `c4`'s diverging from each other.
7155    fn normalise_for_scan(text: &str) -> String {
7156        text.to_ascii_lowercase().replace(['_', ' '], "")
7157    }
7158
7159    /// The Actions-service call this design has no equivalent of, plus the
7160    /// shapes an implementer would invent in its place.
7161    ///
7162    /// Spelled in halves so that no needle ever appears whole in the text being
7163    /// scanned, and keyed to `fn`/`struct` so that the prose above may keep
7164    /// explaining why there is no reservation. `c4` records both trades at
7165    /// length; this list is its counterpart one layer up. Note that the
7166    /// allocation lock's own `fn acquire` is deliberately *not* matched: the
7167    /// needle is `acquire`-a-**job**, and a lock is not one.
7168    const FORBIDDEN: &[&str] = &[
7169        concat!("fn ", "acquire", "_job"),
7170        concat!("fn ", "claim", "_job"),
7171        concat!("fn ", "lease", "_job"),
7172        concat!("fn ", "reserve", "_job"),
7173        concat!("fn ", "ack", "nowledge"),
7174        concat!("struct ", "Job", "Lease"),
7175        concat!("struct ", "Job", "Claim"),
7176        concat!("struct ", "Job", "Reservation"),
7177    ];
7178
7179    fn forbidden_shape_in(source: &str) -> Option<&'static str> {
7180        let haystack = normalise_for_scan(source);
7181        FORBIDDEN
7182            .iter()
7183            .copied()
7184            .find(|forbidden| haystack.contains(&normalise_for_scan(forbidden)))
7185    }
7186
7187    /// `e1`'s Definition of Done: *"No reservation, claim, lease, or acquisition
7188    /// call exists in the crate; a test or review note records that this is
7189    /// deliberate rather than missing."*
7190    ///
7191    /// **Deliberate, not missing.** The scale-set model let a listener call
7192    /// `AcquireJobs` to claim an assignment before scaling; the REST path has no
7193    /// equivalent, so demand is advisory and two hosts serving the same labels
7194    /// can both start a runner for one queued run. Adding a local reservation
7195    /// table would not remove that — the other host cannot see it — it would
7196    /// only hide the surplus case from the tests that measure it. The three
7197    /// controls that actually bound it are host-scoped routing labels,
7198    /// `max_capacity`, and `host_capacity`, and the last two are enforced in
7199    /// this file.
7200    ///
7201    /// The scan is a tripwire on the obvious shape rather than a proof: a
7202    /// reservation reached through a trait method or a differently-named helper
7203    /// would walk past it. Review is the primary control, exactly as `c4` states
7204    /// for its own copy.
7205    ///
7206    /// # It scans the crate, because the bullet says "in the crate"
7207    ///
7208    /// It used to scan this file alone while quoting a crate-wide claim, which
7209    /// left `lifecycle.rs` — `e3`, the launcher, and by far the likeliest place
7210    /// for someone to "fix" the surplus-runner case with a local lease — covered
7211    /// by nothing. Reading another owner's file is not editing it, so ownership
7212    /// was never the obstacle.
7213    ///
7214    /// The walk below is `c4`'s, and it **recurses** for the reason `c4`
7215    /// records: a module directory (`src/reconcile/mod.rs`) arrives as an entry
7216    /// that does not end in `.rs`, so a flat filter drops it and takes every
7217    /// file underneath with it, leaving the scan passing over files it covers
7218    /// by nothing at all. The listed-versus-on-disk assertion is what stops
7219    /// `SOURCES` going stale the moment `e2` or `e3` adds a module.
7220    #[test]
7221    fn nothing_in_this_crate_reserves_or_claims_a_job() {
7222        const SOURCES: &[(&str, &str)] = &[
7223            ("dependency_caches.rs", include_str!("dependency_caches.rs")),
7224            ("docker_cleanup.rs", include_str!("docker_cleanup.rs")),
7225            (
7226                "docker_cleanup/http.rs",
7227                include_str!("docker_cleanup/http.rs"),
7228            ),
7229            (
7230                "docker_cleanup/proxy.rs",
7231                include_str!("docker_cleanup/proxy.rs"),
7232            ),
7233            (
7234                "docker_cleanup/tests.rs",
7235                include_str!("docker_cleanup/tests.rs"),
7236            ),
7237            ("lib.rs", include_str!("lib.rs")),
7238            ("lifecycle.rs", include_str!("lifecycle.rs")),
7239            (
7240                "lifecycle/windows_hyperv.rs",
7241                include_str!("lifecycle/windows_hyperv.rs"),
7242            ),
7243            ("oci.rs", include_str!("oci.rs")),
7244            ("package.rs", include_str!("package.rs")),
7245            ("reconcile.rs", include_str!("reconcile.rs")),
7246        ];
7247
7248        fn walk(directory: &std::path::Path, prefix: &str, found: &mut Vec<String>) {
7249            for entry in std::fs::read_dir(directory).expect("the crate's own src/ is readable") {
7250                let entry = entry.expect("a readable directory entry");
7251                let name = entry.file_name().to_string_lossy().into_owned();
7252                // `/`-joined, which is what `include_str!` takes on every
7253                // platform, so the two sides compare directly.
7254                let joined = if prefix.is_empty() {
7255                    name.clone()
7256                } else {
7257                    format!("{prefix}/{name}")
7258                };
7259                if entry.path().is_dir() {
7260                    walk(&entry.path(), &joined, found);
7261                } else if name.ends_with(".rs") {
7262                    found.push(joined);
7263                }
7264            }
7265        }
7266
7267        let mut listed: Vec<&str> = SOURCES.iter().map(|(name, _)| *name).collect();
7268        listed.sort_unstable();
7269        let mut on_disk = Vec::new();
7270        walk(
7271            std::path::Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src")),
7272            "",
7273            &mut on_disk,
7274        );
7275        on_disk.sort_unstable();
7276        assert_eq!(
7277            listed, on_disk,
7278            "a source file was added or removed; this scan claims to cover the whole crate \
7279             and a stale list makes that claim false"
7280        );
7281
7282        for (name, source) in SOURCES {
7283            assert_eq!(
7284                forbidden_shape_in(&production_half_of(source)),
7285                None,
7286                "{name} names a forbidden shape: there is no `AcquireJobs` equivalent over \
7287                 REST, and a local lease coordinates this host with itself and with nothing \
7288                 else. If an owner decision restored one, that decision belongs in this \
7289                 module's documentation and in this test before it belongs in the code"
7290            );
7291        }
7292
7293        // The control: the scan can see a shape when there is one, through the
7294        // same matcher the loop above uses.
7295        assert!(
7296            forbidden_shape_in("async fn acquire_jobs(&self) -> Vec<Job> { todo!() }").is_some(),
7297            "the scan above proves nothing if the needles no longer match"
7298        );
7299    }
7300
7301    /// This module **applies** `b1`'s label predicate and implements none of it.
7302    ///
7303    /// The counterpart to `c4`'s scan over `crates/github/src/demand.rs`, and it
7304    /// checks the opposite thing, because the two modules sit on opposite sides
7305    /// of the same seam. `c4` builds a `RunsOn` per queued job and must name no
7306    /// `RoutingLabels`; this module holds the policy whose labels decide, so it
7307    /// must call `RoutingLabels::tally` and must not re-derive what that call
7308    /// answers.
7309    ///
7310    /// So the scan is in two halves:
7311    ///
7312    /// * **Present.** `DemandTally` has to appear, because [`demand_for`]
7313    ///   returns one. A production half that named it nowhere would mean the
7314    ///   filtering had been dropped and every queued job in a watched repository
7315    ///   was driving this policy toward `max_capacity` again.
7316    /// * **Absent.** The vocabulary of a *second* implementation. `b1` names the
7317    ///   three outcomes of matching one job; this module consumes the aggregate
7318    ///   and never a single job's verdict, so naming `RunsOnMatch` or
7319    ///   `UnresolvableRunsOn` here means a `match` on an outcome that
7320    ///   `RoutingLabels::tally` has already decided — which is how two copies of
7321    ///   a predicate start.
7322    ///
7323    /// Like the needles in `nothing_in_this_module_reserves_or_claims_a_job`,
7324    /// this is a tripwire on the obvious shape rather than a proof: a hand-rolled
7325    /// comparison of raw label strings that never names a `policy` type would
7326    /// walk past it. Stated rather than implied, for the same reason it is
7327    /// stated there.
7328    #[test]
7329    fn the_label_predicate_is_b1s_and_this_module_only_applies_it() {
7330        let production = this_file_above_its_tests_without_prose();
7331
7332        assert!(
7333            production.contains("DemandTally"),
7334            "the reconciliation loop must tally queued jobs against this policy's routing \
7335             labels. A production half that named `DemandTally` nowhere would mean the \
7336             label filtering had been removed, and a repository whose jobs target \
7337             `ubuntu-latest` would drive its policy toward `max_capacity` again"
7338        );
7339
7340        for second_implementation in ["RunsOnMatch", "UnresolvableRunsOn"] {
7341            assert!(
7342                !production.contains(second_implementation),
7343                "the reconciliation loop names `{second_implementation}`, which is the \
7344                 vocabulary of deciding one job's `runs-on` -- and `RoutingLabels::tally` \
7345                 has already decided it. This module applies the predicate and does not \
7346                 re-implement it; if an owner decision changed that, it belongs in this \
7347                 module's documentation and in this test before it belongs in the code"
7348            );
7349        }
7350    }
7351
7352    /// The demand this module clamps is the *matched* count, and a job this host
7353    /// cannot serve is not demand.
7354    ///
7355    /// The behaviour the whole reversal was for, asserted end to end through
7356    /// `demand_for` rather than through `b1`'s predicate in isolation: a policy
7357    /// carrying this host's labels, a reading holding some of its jobs and some
7358    /// of somebody else's, and the three counts kept apart.
7359    #[test]
7360    fn demand_is_the_queued_jobs_this_policy_can_actually_serve() {
7361        let policy = policy(1, "acme/app", 10);
7362        let reading = QueuedDemand::of(
7363            repo("acme/app"),
7364            [
7365                fixtures::queued_job(&[HOST_LABEL]),
7366                fixtures::queued_job(&[HOST_LABEL]),
7367                fixtures::queued_job(&["ubuntu-latest"]),
7368                fixtures::unresolvable_job(),
7369            ],
7370        );
7371
7372        let tally = demand_for(&policy, &reading);
7373
7374        assert_eq!(
7375            tally.demand(),
7376            2,
7377            "only the jobs whose required labels this policy carries are demand"
7378        );
7379        assert_eq!(
7380            tally.not_matched, 1,
7381            "a `ubuntu-latest` job is somebody else's work; counting it would start a \
7382             runner that idles until it times out"
7383        );
7384        assert_eq!(
7385            tally.unresolvable.len(),
7386            1,
7387            "an unresolvable `runs-on` is never demand and never discarded"
7388        );
7389    }
7390
7391    /// A repository target reads its own repository; an organization target
7392    /// reads the whole scope.
7393    #[test]
7394    fn an_organization_policy_tallies_every_repository_its_scope_covers() {
7395        let mut per_repository = BTreeMap::new();
7396        per_repository.insert(repo("acme/left"), fixtures::queued_jobs(&[HOST_LABEL], 3));
7397        per_repository.insert(repo("acme/right"), fixtures::queued_jobs(&[HOST_LABEL], 4));
7398        let reading = QueuedDemand::new(per_repository);
7399
7400        let repository_policy = policy(1, "acme/left", 10);
7401        assert_eq!(
7402            demand_for(&repository_policy, &reading).demand(),
7403            3,
7404            "a repository target reads its own repository's queue and not the aggregate"
7405        );
7406
7407        let org_policy = fixtures::policy()
7408            .id(PolicyId::from_u128(2))
7409            .organization("acme")
7410            .autoscale("home", 10)
7411            .active()
7412            .build();
7413        assert_eq!(
7414            demand_for(&org_policy, &reading).demand(),
7415            7,
7416            "an organization policy serves any repository in its scope, so its demand is \
7417             the whole aggregate's"
7418        );
7419    }
7420
7421    #[test]
7422    fn the_accepted_over_count_is_bounded_by_the_two_ceilings_and_nothing_else() {
7423        // The owner decision accepts that a repository whose jobs only target
7424        // `ubuntu-latest` still drives its policy toward `max_capacity`. What
7425        // stops that being unbounded is exactly what stops any other demand
7426        // being unbounded, which is asserted here rather than assumed.
7427        let host = host_with(2);
7428        let policy = policy(1, "acme/app", 5);
7429        let attempts: Vec<RunnerAttempt> = Vec::new();
7430        let mut allocator = HostAllocator::from_attempts(&host, &attempts);
7431
7432        let allocation = allocator.allocate(&policy, u32::MAX);
7433        assert_eq!(allocation.desired, 5, "max_capacity beats reported demand");
7434        assert_eq!(allocation.to_start, 2, "host_capacity beats max_capacity");
7435        assert_eq!(allocation.limiting_factor, LimitingFactor::HostCapacity);
7436    }
7437
7438    /// A director standing in for the coordinator's launches: a fixed count
7439    /// per policy, and every attempt it was told about.
7440    #[derive(Debug, Default)]
7441    struct FixedDirector {
7442        directed: Mutex<BTreeMap<PolicyId, u32>>,
7443        started: Mutex<Vec<(PolicyId, AttemptId)>>,
7444        failed: Mutex<Vec<(PolicyId, Option<AttemptId>)>>,
7445        /// How many more times an older launch is said to be waiting.
7446        older: AtomicUsize,
7447    }
7448
7449    impl DemandDirector for FixedDirector {
7450        fn directed_demand(&self, policy: &ScalePolicy) -> Option<u32> {
7451            self.directed.lock().unwrap().get(&policy.id).copied()
7452        }
7453
7454        fn launched(&self, policy: &ScalePolicy, attempt: AttemptId) {
7455            self.started.lock().unwrap().push((policy.id, attempt));
7456        }
7457
7458        fn start_failed(&self, policy: &ScalePolicy, attempt: Option<AttemptId>) {
7459            self.failed.lock().unwrap().push((policy.id, attempt));
7460        }
7461
7462        fn older_waiting(&self, _policy: &ScalePolicy) -> bool {
7463            self.older
7464                .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |left| {
7465                    left.checked_sub(1)
7466                })
7467                .is_ok()
7468        }
7469    }
7470
7471    /// A launcher that holds every runner in its bring-up until as many others
7472    /// (or the test) are at the barrier.
7473    #[derive(Debug)]
7474    struct HeldBringUp {
7475        inner: Arc<FakeLauncher>,
7476        barrier: Arc<tokio::sync::Barrier>,
7477        fail: Option<FailureReason>,
7478    }
7479
7480    #[async_trait::async_trait]
7481    impl RunnerLauncher for HeldBringUp {
7482        async fn host_capacity(&self, host: &Host) -> Result<NonZeroU16, HostCapacityError> {
7483            self.inner.host_capacity(host).await
7484        }
7485
7486        async fn supervise(
7487            &self,
7488            policy: &ScalePolicy,
7489        ) -> Result<Vec<ReplacementIntent>, LaunchFailure> {
7490            self.inner.supervise(policy).await
7491        }
7492
7493        async fn attempts(&self) -> Result<Vec<RunnerAttempt>, LaunchFailure> {
7494            self.inner.attempts().await
7495        }
7496
7497        async fn launch(&self, request: LaunchRequest<'_>) -> Result<RunnerAttempt, LaunchFailure> {
7498            self.inner.launch(request).await
7499        }
7500
7501        async fn finish_launch(
7502            &self,
7503            _policy: &ScalePolicy,
7504            attempt: RunnerAttempt,
7505            _lock_wait: Duration,
7506        ) -> Result<RunnerAttempt, LaunchFailure> {
7507            self.barrier.wait().await;
7508            match &self.fail {
7509                Some(reason) => Err(LaunchFailure::new(reason.clone())),
7510                None => Ok(attempt),
7511            }
7512        }
7513
7514        async fn clean(&self, attempt: AttemptId) -> Result<(), LaunchFailure> {
7515            self.inner.clean(attempt).await
7516        }
7517    }
7518
7519    fn directed_reconciler(
7520        capacity: u16,
7521        launcher: Arc<dyn RunnerLauncher>,
7522        lock: Arc<dyn AllocationLock>,
7523        director: Arc<FixedDirector>,
7524    ) -> Reconciler {
7525        Reconciler::new(
7526            host_with(capacity),
7527            ReconcilerPorts {
7528                demand: Arc::new(FakeDemand::ready(0, &repo("acme/app"))),
7529                launcher,
7530                lock,
7531                directory: Arc::new(FakeDirectory::default()),
7532                clock: Arc::new(FakeClock::default()),
7533                jitter: Arc::new(NoJitter),
7534                events: Arc::new(NoEvents),
7535            },
7536        )
7537        .with_director(director as Arc<dyn DemandDirector>)
7538    }
7539
7540    /// Two targets' runners are being brought up at the same time: each is at
7541    /// the barrier with the allocation lock released, or neither gets there.
7542    #[tokio::test]
7543    async fn two_targets_bring_up_their_runners_at_once() {
7544        let fake = Arc::new(FakeLauncher::new());
7545        let lock: Arc<dyn AllocationLock> = Arc::new(InProcessAllocationLock::new());
7546        let barrier = Arc::new(tokio::sync::Barrier::new(2));
7547        let held = Arc::new(HeldBringUp {
7548            inner: Arc::clone(&fake),
7549            barrier,
7550            fail: None,
7551        });
7552        let director = Arc::new(FixedDirector::default());
7553        let (one, two) = (policy(1, "acme/app", 2), policy(2, "acme/other", 2));
7554        director.directed.lock().unwrap().insert(one.id, 1);
7555        director.directed.lock().unwrap().insert(two.id, 1);
7556        let mut first = directed_reconciler(
7557            2,
7558            Arc::clone(&held) as _,
7559            Arc::clone(&lock),
7560            Arc::clone(&director),
7561        );
7562        let mut second = directed_reconciler(2, held as _, lock, director);
7563
7564        let (a, b) = tokio::time::timeout(Duration::from_secs(10), async {
7565            tokio::join!(
7566                first.start_directed(std::slice::from_ref(&one)),
7567                second.start_directed(std::slice::from_ref(&two)),
7568            )
7569        })
7570        .await
7571        .expect("neither waits for the other's bring-up");
7572        assert_eq!((a.started, b.started), (1, 1));
7573    }
7574
7575    /// No oversubscription: the slot is held from the reservation, while the
7576    /// runner is still being brought up and the lock is free.
7577    #[tokio::test]
7578    async fn a_runner_coming_up_holds_its_slot_against_another_target() {
7579        let fake = Arc::new(FakeLauncher::new());
7580        let lock: Arc<dyn AllocationLock> = Arc::new(InProcessAllocationLock::new());
7581        let barrier = Arc::new(tokio::sync::Barrier::new(2));
7582        let held = Arc::new(HeldBringUp {
7583            inner: Arc::clone(&fake),
7584            barrier: Arc::clone(&barrier),
7585            fail: None,
7586        });
7587        let director = Arc::new(FixedDirector::default());
7588        let (one, two) = (policy(1, "acme/app", 2), policy(2, "acme/other", 2));
7589        director.directed.lock().unwrap().insert(one.id, 1);
7590        director.directed.lock().unwrap().insert(two.id, 1);
7591        let mut first = directed_reconciler(
7592            1,
7593            Arc::clone(&held) as _,
7594            Arc::clone(&lock),
7595            Arc::clone(&director),
7596        );
7597        let mut second = directed_reconciler(1, held as _, lock, director);
7598
7599        let (a, b) = tokio::time::timeout(Duration::from_secs(10), async {
7600            tokio::join!(first.start_directed(std::slice::from_ref(&one)), async {
7601                while fake.live_count() == 0 {
7602                    tokio::task::yield_now().await;
7603                }
7604                let report = second.start_directed(std::slice::from_ref(&two)).await;
7605                barrier.wait().await;
7606                report
7607            })
7608        })
7609        .await
7610        .expect("the first is let go");
7611        assert_eq!((a.started, b.started), (1, 0));
7612        assert_eq!(fake.live_count(), 1);
7613    }
7614
7615    /// The launch was given its runner at the reservation, and a bring-up that
7616    /// fails says so, rather than counting as started.
7617    #[tokio::test]
7618    async fn a_failed_bring_up_is_a_failed_start() {
7619        let fake = Arc::new(FakeLauncher::new());
7620        let held = Arc::new(HeldBringUp {
7621            inner: fake,
7622            barrier: Arc::new(tokio::sync::Barrier::new(1)),
7623            fail: Some(FailureReason::JitRequestFailed),
7624        });
7625        let director = Arc::new(FixedDirector::default());
7626        let pool = policy(1, "acme/app", 2);
7627        director.directed.lock().unwrap().insert(pool.id, 1);
7628        let mut reconciler = directed_reconciler(
7629            2,
7630            held as _,
7631            Arc::new(InProcessAllocationLock::new()),
7632            Arc::clone(&director),
7633        );
7634
7635        let report = reconciler.start_directed(std::slice::from_ref(&pool)).await;
7636
7637        assert_eq!(report.started, 0);
7638        let started = director.started.lock().unwrap()[0].1;
7639        assert_eq!(
7640            *director.failed.lock().unwrap(),
7641            vec![(pool.id, Some(started))]
7642        );
7643    }
7644
7645    /// Oldest first across targets: a younger launch waits for an older one to
7646    /// get its runner, and not indefinitely when that one's loop is occupied.
7647    #[tokio::test(start_paused = true)]
7648    async fn a_younger_launch_lets_an_older_one_reserve_first_for_a_bounded_time() {
7649        let fake = Arc::new(FakeLauncher::new());
7650        let director = Arc::new(FixedDirector::default());
7651        let pool = policy(1, "acme/app", 3);
7652        director.directed.lock().unwrap().insert(pool.id, 1);
7653        let mut reconciler = directed_reconciler(
7654            5,
7655            Arc::clone(&fake) as _,
7656            Arc::new(InProcessAllocationLock::new()),
7657            Arc::clone(&director),
7658        );
7659
7660        // The older launch gets its runner after three looks.
7661        director.older.store(3, Ordering::SeqCst);
7662        let began = tokio::time::Instant::now();
7663        let report = reconciler.start_directed(std::slice::from_ref(&pool)).await;
7664        assert_eq!(report.started, 1);
7665        assert_eq!(began.elapsed(), OLDER_LAUNCH_POLL * 3);
7666
7667        // It never does.
7668        director.directed.lock().unwrap().insert(pool.id, 2);
7669        director.older.store(usize::MAX, Ordering::SeqCst);
7670        let began = tokio::time::Instant::now();
7671        let report = reconciler.start_directed(std::slice::from_ref(&pool)).await;
7672        assert_eq!(report.started, 1, "started anyway");
7673        assert!(began.elapsed() >= OLDER_LAUNCH_TURN);
7674        assert!(began.elapsed() < OLDER_LAUNCH_TURN + Duration::from_secs(1));
7675    }
7676
7677    /// Coordinated placement: a directed policy starts runners for the
7678    /// coordinator's launches only, through every ceiling, whatever its own
7679    /// reading says; an undirected one keeps its own demand.
7680    #[tokio::test]
7681    async fn a_directed_policy_starts_only_what_it_was_directed_to_within_its_ceilings() {
7682        let demand = Arc::new(FakeDemand::ready(5, &repo("acme/app")));
7683        let launcher = Arc::new(FakeLauncher::new());
7684        let director = Arc::new(FixedDirector::default());
7685        let mut reconciler = Reconciler::new(
7686            host_with(10),
7687            ReconcilerPorts {
7688                demand: Arc::clone(&demand) as Arc<dyn DemandSource>,
7689                launcher: Arc::clone(&launcher) as Arc<dyn RunnerLauncher>,
7690                lock: Arc::new(InProcessAllocationLock::new()),
7691                directory: Arc::new(FakeDirectory::default()),
7692                clock: Arc::new(FakeClock::default()),
7693                jitter: Arc::new(NoJitter),
7694                events: Arc::new(NoEvents),
7695            },
7696        )
7697        .with_director(Arc::clone(&director) as Arc<dyn DemandDirector>);
7698        let pool = policy(1, "acme/app", 2);
7699
7700        // Five queued jobs on the poll, but no launch placed here: nothing.
7701        director.directed.lock().unwrap().insert(pool.id, 0);
7702        let report = reconciler.reconcile(std::slice::from_ref(&pool)).await;
7703        assert_eq!(report.started, 0);
7704
7705        // One launch: one runner, reported back to the director.
7706        director.directed.lock().unwrap().insert(pool.id, 1);
7707        let report = reconciler.reconcile(std::slice::from_ref(&pool)).await;
7708        assert_eq!(report.started, 1);
7709        assert_eq!(director.started.lock().unwrap().len(), 1);
7710
7711        // The same launch again is covered by the runner already starting.
7712        let report = reconciler.reconcile(std::slice::from_ref(&pool)).await;
7713        assert_eq!(report.started, 0);
7714
7715        // Three launches against max_capacity 2: the ceiling holds.
7716        director.directed.lock().unwrap().insert(pool.id, 3);
7717        let report = reconciler.reconcile(std::slice::from_ref(&pool)).await;
7718        assert_eq!(report.started, 1);
7719        assert_eq!(launcher.live_count(), 2);
7720
7721        // A launch-only start: every open launch at once, and no poll.
7722        let polls = demand.polls().len();
7723        director.directed.lock().unwrap().insert(pool.id, 0);
7724        let report = reconciler.start_directed(std::slice::from_ref(&pool)).await;
7725        assert_eq!(report.started, 0);
7726        let fresh = Arc::new(FakeLauncher::new());
7727        let mut starter = Reconciler::new(
7728            host_with(10),
7729            ReconcilerPorts {
7730                demand: Arc::clone(&demand) as Arc<dyn DemandSource>,
7731                launcher: Arc::clone(&fresh) as Arc<dyn RunnerLauncher>,
7732                lock: Arc::new(InProcessAllocationLock::new()),
7733                directory: Arc::new(FakeDirectory::default()),
7734                clock: Arc::new(FakeClock::default()),
7735                jitter: Arc::new(NoJitter),
7736                events: Arc::new(NoEvents),
7737            },
7738        )
7739        .with_director(Arc::clone(&director) as Arc<dyn DemandDirector>);
7740        let wide = policy(2, "acme/app", 6);
7741        director.directed.lock().unwrap().insert(wide.id, 6);
7742        let report = starter.start_directed(std::slice::from_ref(&wide)).await;
7743        assert_eq!(report.started, 6, "six launches, one call");
7744        assert_eq!(demand.polls().len(), polls, "GitHub was not polled");
7745        // An undirected policy starts nothing this way.
7746        assert_eq!(
7747            starter
7748                .start_directed(&[policy(3, "acme/app", 2)])
7749                .await
7750                .started,
7751            0
7752        );
7753
7754        // Its own reading failed: a directed policy still starts its launch.
7755        let failing = Arc::new(FakeLauncher::new());
7756        let mut reconciler = Reconciler::new(
7757            host_with(10),
7758            ReconcilerPorts {
7759                demand: Arc::new(FakeDemand::failing(RefreshState::Offline)),
7760                launcher: Arc::clone(&failing) as Arc<dyn RunnerLauncher>,
7761                lock: Arc::new(InProcessAllocationLock::new()),
7762                directory: Arc::new(FakeDirectory::default()),
7763                clock: Arc::new(FakeClock::default()),
7764                jitter: Arc::new(NoJitter),
7765                events: Arc::new(NoEvents),
7766            },
7767        )
7768        .with_director(Arc::clone(&director) as Arc<dyn DemandDirector>);
7769        director.directed.lock().unwrap().insert(pool.id, 1);
7770        assert_eq!(reconciler.reconcile(&[pool]).await.started, 1);
7771
7772        // Undirected (fallback): its own five jobs, clamped to max 2.
7773        let own = Arc::new(FakeLauncher::new());
7774        let mut reconciler = Reconciler::new(
7775            host_with(10),
7776            ReconcilerPorts {
7777                demand: Arc::new(FakeDemand::ready(5, &repo("acme/app"))),
7778                launcher: Arc::clone(&own) as Arc<dyn RunnerLauncher>,
7779                lock: Arc::new(InProcessAllocationLock::new()),
7780                directory: Arc::new(FakeDirectory::default()),
7781                clock: Arc::new(FakeClock::default()),
7782                jitter: Arc::new(NoJitter),
7783                events: Arc::new(NoEvents),
7784            },
7785        )
7786        .with_director(Arc::new(OwnDemand));
7787        assert_eq!(
7788            reconciler
7789                .reconcile(&[policy(1, "acme/app", 2)])
7790                .await
7791                .started,
7792            2
7793        );
7794    }
7795}