//! What a heavy job costs, what a Mac may promise, and whether the one fits in //! the other. //! //! **Hardware is budgeted by Cotton itself** //! ([build-runners-simulators-and-sessions-are-budgeted-by-cotton-its]): every //! Mac reports what it has, every job declares what it costs, and a Mac takes //! work up to a known point and never past it. This module holds both halves — //! the COST table ([`cost`], one entry per kind of heavy job) and the BUDGET //! arithmetic ([`budget_gb`], [`free_gb`]) — because a cost and a budget //! written in two places are a cost and a budget that stop meaning the same //! thing. The ADMISSION is [`super::dispatch::pick_device`], which is the only //! caller that puts them together. //! //! **The build lock is a COST, not a throttle.** `/tmp/tinmen-build.lock` //! serialises the heavy builds a person starts by hand, and it used to refuse //! every session outright while it was held — a Mac with 48 GB in it turned //! away a 0.6 GB agent because somebody was compiling. What the lock holder is //! really costing is measured and added to the Mac's committed total; whether //! anything else fits is then the same arithmetic as everything else. //! //! **A guess is a starting point, not the answer.** Every default here is a //! number somebody measured once, on one Mac, and it is wrong on the next one: //! an M4 Pro's `claude` is not an M1 Air's. So a Mac that has watched a kind of //! job [`MEASURED_MINIMUM_SAMPLES`] times uses its OWN peak instead //! ([`mem_gb`]) — the table is where a Mac starts, not where it stays. //! //! No clock, no database, no `Ctx`: every function is of its arguments, which //! is what lets `budget_tests.rs` state the whole table as arithmetic. /// A kind of heavy job a Mac can be asked to take. /// /// **Adding a variant is a compile error until it has a cost** — [`cost`] is an /// exhaustive `match`, not a lookup that can miss — and a runtime guard holds /// [`JOB_KINDS`] to the same list, so a kind that exists but is never offered /// to the budget cannot hide either. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum JobKind { /// One coding agent in its own pseudo-terminal — `claude` or `codex`. CodingSession, /// One MCP server a session opens. Counted from the session's settings, /// because a session with eight of them is a different job from one with /// none. McpServer, /// One booted iOS simulator. IosSimulator, /// An `xcodebuild archive` — the heaviest thing this repo does. XcodebuildArchive, /// One `cargo build` job. The COUNT is `CARGO_BUILD_JOBS`, because cargo's /// memory is per compiler process and nothing else about the build moves. CargoBuild, /// One booted Android emulator. AndroidEmulator, } /// Every [`JobKind`], in the order a person would read them. /// /// Held equal to the enum by `budget_tests.rs`. It exists so the table can /// be swept — a `match` proves each kind HAS a cost, and only a list can prove /// the costs are sane as a set (no duplicate label, no zero, no negative). pub const JOB_KINDS: &[JobKind] = &[ JobKind::CodingSession, JobKind::McpServer, JobKind::IosSimulator, JobKind::XcodebuildArchive, JobKind::CargoBuild, JobKind::AndroidEmulator, ]; /// How many cores one unit of a job wants. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Cores { /// A fixed number, whatever the Mac is. Fixed(i64), /// Every core the Mac has. `xcodebuild` and a linker do not leave one. All, } impl Cores { /// The number, on a Mac with `cpu_count` cores. #[must_use] pub fn on(self, cpu_count: i64) -> i64 { match self { Cores::Fixed(n) => n, Cores::All => cpu_count, } } } /// What ONE unit of a job kind costs. #[derive(Debug, Clone, Copy, PartialEq)] pub struct JobCost { /// The line a refusal calls it by — `dispatch.jobs.