greengate-runtime 4.3.0

The amont hook logic: registry, dispatchers, checks and the trust model
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! `amont slot run` and `amont slots` (ADR-0014): the host slots for any
//! command, and who holds them.
//!
//! `amont slot run [--weight N] [--max-wait S] [--on-timeout run|exit] -- <cmd…>`
//! waits for a slot, runs the command with `GREENHARNESS_HOST_SLOT=held`, passes
//! its exit status through and frees the slot when the command exits. The
//! wrapper alone holds the slot: its descriptors are close-on-exec, so a
//! daemon the command leaves behind never keeps it. A wait longer than
//! `--max-wait` (default 90 s, under Claude Code's 120 s Bash timeout) exits
//! 75 without running anything, unless `--on-timeout run` says otherwise.

/// The holder name a `slot run` queues under, as `greengate slots` shows it.
const SLOT_RUN_HOLDER: &str = concat!(crate::bin!(), " slot run");

use crate::names::BIN;
use std::ffi::OsString;
use std::process::Command;
use std::time::{Duration, Instant};

/// Exit status for "the host stayed busy; nothing ran" (EX_TEMPFAIL).
pub const BUSY_EXIT: i32 = 75;

/// The default `--max-wait`: under Claude Code's 120 s Bash timeout, so a
/// wrapped command is never killed while it is still queued.
pub const DEFAULT_MAX_WAIT: Duration = Duration::from_secs(90);

/// `amont slot <args>`.
pub fn slot_command(args: &[OsString]) -> i32 {
    let (own, cmd) = split_at_dashes(args);
    match own.first().and_then(|a| a.to_str()) {
        Some("run") => run(&own[1..], cmd),
        _ => {
            eprintln!("{BIN}: usage: {BIN} slot run [--weight N] [--max-wait S] [--on-timeout run|exit] -- <command…>");
            2
        }
    }
}

/// The arguments before `--`, and the command after it.
pub fn split_at_dashes(args: &[OsString]) -> (&[OsString], &[OsString]) {
    match args.iter().position(|a| a == "--") {
        Some(i) => (&args[..i], &args[i + 1..]),
        None => (args, &[]),
    }
}

/// The flags `slot run` takes, each with a value.
const VALUED: &[&str] = &["--weight", "--max-wait", "--on-timeout"];

/// Read `own` (the words before `--`) into flag values, as `--flag value`
/// or `--flag=value`. Anything else is a usage error.
fn parse(own: &[OsString]) -> Result<Vec<(&'static str, String)>, String> {
    let mut out = Vec::new();
    let mut words = own.iter();
    while let Some(word) = words.next() {
        let word = word.to_str().ok_or("an argument is not valid UTF-8")?;
        let (name, inline) = match word.split_once('=') {
            Some((n, v)) => (n, Some(v.to_string())),
            None => (word, None),
        };
        let Some(flag) = VALUED.iter().find(|f| **f == name) else {
            return Err(format!("unknown argument {word:?}"));
        };
        let value = match inline {
            Some(v) => v,
            None => words
                .next()
                .and_then(|v| v.to_str())
                .ok_or(format!("{flag} needs a value"))?
                .to_string(),
        };
        out.push((*flag, value));
    }
    Ok(out)
}

fn value<'a>(flags: &'a [(&'static str, String)], flag: &str) -> Option<&'a str> {
    flags
        .iter()
        .find(|(f, _)| *f == flag)
        .map(|(_, v)| v.as_str())
}

/// How the command runs: inside a held slot, or with none.
#[derive(Clone, Copy)]
enum Launch {
    /// A slot is held for it (here or by an ancestor): `GREENHARNESS_HOST_SLOT=held`.
    Held,
    /// No slot: off, unsupported, unavailable, or `--on-timeout run`.
    Unqueued,
}

fn run(own: &[OsString], cmd: &[OsString]) -> i32 {
    let own = match parse(own) {
        Ok(flags) => flags,
        Err(why) => {
            eprintln!("{BIN}: slot run: {why}");
            return 2;
        }
    };
    let own = own.as_slice();
    let Some((program, rest)) = cmd.split_first() else {
        eprintln!("{BIN}: `{BIN} slot run` needs a command after `--`");
        return 2;
    };
    let weight = match value(own, "--weight").map(str::parse::<u32>) {
        None => 1,
        Some(Ok(n)) if n >= 1 => n,
        Some(_) => {
            eprintln!("{BIN}: --weight is a whole number from 1");
            return 2;
        }
    };
    let max_wait = match value(own, "--max-wait").map(str::parse::<u64>) {
        None => DEFAULT_MAX_WAIT,
        Some(Ok(s)) => Duration::from_secs(s),
        Some(Err(_)) => {
            eprintln!("{BIN}: --max-wait is a whole number of seconds");
            return 2;
        }
    };
    let run_on_timeout = match value(own, "--on-timeout") {
        None | Some("exit") => false,
        Some("run") => true,
        Some(other) => {
            eprintln!("{BIN}: --on-timeout is `run` or `exit`, not {other:?}");
            return 2;
        }
    };
    let label: String = std::iter::once(program)
        .chain(rest.iter().take(2))
        .map(|a| a.to_string_lossy().into_owned())
        .collect::<Vec<_>>()
        .join(" ");

    // Already inside a slot (a wrapped command running this one): no second
    // queue for the slot the parent holds.
    let nested = std::env::var_os(greenharness_slots::HELD_ENV).is_some_and(|v| !v.is_empty());
    let settings = crate::config::Settings::default();
    let (sizing, machine) = crate::host_slots::sizing_and_machine(&settings);
    let (Some(sizing), false, true) = (sizing, nested, greenharness_slots::SUPPORTED) else {
        return spawn(
            program,
            rest,
            if nested {
                Launch::Held
            } else {
                Launch::Unqueued
            },
            None,
        );
    };
    // The width this command may run at, held or not: an unqueued run after
    // a timeout happens when the host is busiest, so it is bounded too.
    let slot_cap = sizing.cap_for(machine.cores);
    let jobs = greenharness_slots::parallelism(weight, machine.cores, slot_cap);

    let dir = greenharness_slots::dir();
    let started = Instant::now();
    let mut waiter = match greenharness_slots::Waiter::new_in_on(
        &dir,
        sizing,
        weight,
        greenharness_slots::Holder::here(SLOT_RUN_HOLDER, &label),
        machine,
    ) {
        Ok(w) => w,
        Err(e) => {
            eprintln!("{BIN}: host slots unavailable ({e}); running unqueued");
            return spawn(program, rest, Launch::Unqueued, Some(jobs));
        }
    };
    let mut last_said: Option<Instant> = None;
    let lease = loop {
        match waiter.poll() {
            Ok(greenharness_slots::Poll::Granted(lease)) => break Some(lease),
            Ok(greenharness_slots::Poll::Waiting(queue)) => {
                let due = last_said.map_or(true, |t| t.elapsed() >= Duration::from_secs(30));
                if due && (!queue.holders.is_empty() || queue.unknown > 0) {
                    eprintln!("{BIN}: {}", crate::host_slots::behind(&queue));
                    // The pointer once, not on every 30 s repeat.
                    if last_said.is_none() {
                        eprintln!("  see: {BIN} slots");
                    }
                    last_said = Some(Instant::now());
                }
            }
            Err(e) => {
                eprintln!("{BIN}: host slots unavailable ({e}); running unqueued");
                return spawn(program, rest, Launch::Unqueued, Some(jobs));
            }
        }
        if started.elapsed() >= max_wait {
            break None;
        }
        std::thread::sleep(Duration::from_millis(200));
    };
    drop(waiter);
    let waited = started.elapsed().as_millis();
    // Every line of this run names the cap and cores it was sized for.
    let record = |outcome: &str| {
        greenharness_slots::record_wait_named(
            &dir,
            SLOT_RUN_HOLDER,
            greenharness_slots::WaitContext {
                cap: Some(slot_cap),
                cores: Some(machine.cores),
                ..greenharness_slots::WaitContext::default()
            },
            weight,
            Some(jobs),
            waited,
            outcome,
        );
    };
    let Some(lease) = lease else {
        if run_on_timeout {
            record("unqueued");
            eprintln!(
                "{BIN}: no host slot freed in {}s; running unqueued (--on-timeout run)",
                max_wait.as_secs()
            );
            return spawn(program, rest, Launch::Unqueued, Some(jobs));
        }
        record("timeout");
        eprintln!(
            "{BIN}: no host slot freed in {}s; `{label}` did not run (exit {BUSY_EXIT}). \
             Retry the same command later, or let CI run it.",
            max_wait.as_secs()
        );
        return BUSY_EXIT;
    };
    record("admitted");
    let code = spawn(program, rest, Launch::Held, Some(jobs));
    drop(lease);
    code
}

/// Run the command, inheriting stdio, and pass its exit status through: the
/// code, or 128 + the signal that ended it. `jobs` bounds its width
/// (`greenharness_slots::bound_width`); `None` (slots off, unsupported, or nested
/// under a holder whose own bounds it inherits) sets nothing.
fn spawn(program: &OsString, rest: &[OsString], launch: Launch, jobs: Option<u32>) -> i32 {
    let mut cmd = Command::new(program);
    cmd.args(rest);
    if let Some(jobs) = jobs {
        greenharness_slots::bound_width(&mut cmd, jobs);
    }
    match launch {
        Launch::Held => {
            cmd.env(greenharness_slots::HELD_ENV, "held");
        }
        Launch::Unqueued => {}
    }
    match cmd.status() {
        Ok(status) => exit_code(status),
        Err(e) => {
            eprintln!("{BIN}: cannot run {}: {e}", program.to_string_lossy());
            127
        }
    }
}

fn exit_code(status: std::process::ExitStatus) -> i32 {
    if let Some(code) = status.code() {
        return code;
    }
    #[cfg(unix)]
    {
        use std::os::unix::process::ExitStatusExt;
        if let Some(sig) = status.signal() {
            return 128 + sig;
        }
    }
    1
}

/// `amont slots [--json]`.
/// `6 cores, 12 threads`: the threads only where they differ from the cores
/// (SMT), and a short tag when the count did not come from plain detection,
/// so the `slots` header stays inside 80 columns. `doctor` names the key.
pub fn cores_phrase(cores: u32, threads: u32, source: greenharness_slots::host::Source) -> String {
    use greenharness_slots::host::Source;
    let mut phrase = format!("{cores} cores");
    if threads != cores {
        phrase.push_str(&format!(", {threads} threads"));
    }
    let tag = match source {
        Source::Env => Some("env"),
        Source::Global | Source::System => Some("git"),
        Source::LogicalFallback => Some("logical"),
        Source::LimitedByAffinity => Some("affinity"),
        Source::Detected => None,
        // `Source` is non_exhaustive: a new variant needs a tag here.
        _ => None,
    };
    if let Some(tag) = tag {
        phrase.push_str(&format!(" ({tag})"));
    }
    phrase
}

/// Where a host key's value came from, in words a person can act on: the
/// variable or the `git config` scope and key, else the detection outcome.
fn source_label(source: greenharness_slots::host::Source, key: &str, env: &str) -> String {
    use greenharness_slots::host::Source;
    match source {
        Source::Env => env.to_string(),
        Source::Global => format!("git config --global {key}"),
        Source::System => format!("git config --system {key}"),
        other => other.name().to_string(),
    }
}

/// The host sizing lines `doctor` prints, one per key so each fits in 80
/// columns: the core count and the slot count, each with the source that set
/// it. Any warning about either key is printed separately, once, by
/// `host_slots::host_sizing`.
pub fn doctor_line() -> String {
    use greenharness_slots::host::{CORES_ENV, CORES_KEY, SLOTS_ENV, SLOTS_KEY};
    let settings = crate::config::Settings::default();
    let host = crate::host_slots::host_sizing(&settings);
    let slots = match host.slots.value {
        None => "off".to_string(),
        Some(greenharness_slots::Sizing::Auto) => "auto".to_string(),
        Some(greenharness_slots::Sizing::Fixed(n)) => n.to_string(),
    };
    format!(
        "host cores: {}, {} threads ({})\nhost slots: {slots} ({})",
        host.cores.value,
        greenharness_slots::logical_cores(),
        source_label(host.cores.source, CORES_KEY, CORES_ENV),
        source_label(host.slots.source, SLOTS_KEY, SLOTS_ENV),
    )
}

pub fn slots_command(args: &[OsString]) -> i32 {
    if let Some(bad) = args.iter().find(|a| *a != "--json") {
        eprintln!("{BIN}: slots: unknown argument {:?}", bad.to_string_lossy());
        return 2;
    }
    let json = !args.is_empty();
    let settings = crate::config::Settings::default();
    let host = crate::host_slots::host_sizing(&settings);
    let machine = greenharness_slots::Machine {
        cores: host.cores.value.max(1),
    };
    // Additive fields: the core count the slots are sized from, and its source.
    let cores_json = format!(
        "\"cores\":{},\"cores_source\":\"{}\"",
        machine.cores,
        host.cores.source.token()
    );
    let Some(sizing) = host.slots.value else {
        if json {
            println!(
                "{{\"cap\":0,{cores_json},\"held\":0,\"holders\":[],\"unknown\":0,\"waiting\":0}}"
            );
        } else {
            let by = match host.slots.source {
                greenharness_slots::host::Source::Env => {
                    format!("{}=0", greenharness_slots::host::SLOTS_ENV)
                }
                _ => format!("{} 0", greenharness_slots::host::SLOTS_KEY),
            };
            println!("host slots are off ({by})");
        }
        return 0;
    };
    let status = match greenharness_slots::status_in_on(&greenharness_slots::dir(), sizing, machine)
    {
        Ok(s) => s,
        Err(e) => {
            eprintln!("{BIN}: cannot read the host slots: {e}");
            return 1;
        }
    };
    let held = status.holders.len() + status.unknown;
    let mode = match sizing {
        greenharness_slots::Sizing::Auto => format!(
            "auto: load {}, {}",
            greenharness_slots::load1().map_or("?".to_string(), |l| format!("{l:.1}")),
            cores_phrase(
                machine.cores,
                greenharness_slots::logical_cores(),
                host.cores.source
            )
        ),
        greenharness_slots::Sizing::Fixed(n) => format!("greengate.hostSlots {n}"),
    };
    if json {
        let holders: Vec<String> = status
            .holders
            .iter()
            .map(greenharness_slots::Holder::to_json)
            .collect();
        println!(
            "{{\"cap\":{},{cores_json},\"held\":{held},\"holders\":[{}],\"unknown\":{},\"waiting\":{}}}",
            status.cap,
            holders.join(","),
            status.unknown,
            status.waiting
        );
        return 0;
    }
    let now = greenharness_slots::now_ms();
    println!("host slots: {held} of {} held ({mode})", status.cap);
    for h in &status.holders {
        let jobs = h.jobs.map_or("-".to_string(), |j| j.to_string());
        println!(
            "  {}  pid {}  jobs {jobs}  session {}",
            h.describe(now),
            h.pid,
            h.short_session()
        );
    }
    if status.unknown > 0 {
        println!("  {} unknown (old client)", status.unknown);
    }
    println!("waiting: {}", status.waiting);
    0
}

#[cfg(test)]
mod tests {
    use super::*;

    fn os(v: &[&str]) -> Vec<OsString> {
        v.iter().map(OsString::from).collect()
    }

    #[test]
    fn the_cores_phrase_names_threads_and_overrides_only_when_they_matter() {
        use greenharness_slots::host::Source;
        assert_eq!(cores_phrase(6, 12, Source::Detected), "6 cores, 12 threads");
        assert_eq!(cores_phrase(10, 10, Source::Detected), "10 cores");
        assert_eq!(
            cores_phrase(4, 12, Source::Env),
            "4 cores, 12 threads (env)"
        );
        assert_eq!(
            cores_phrase(4, 12, Source::Global),
            "4 cores, 12 threads (git)"
        );
        assert_eq!(
            cores_phrase(8, 8, Source::LogicalFallback),
            "8 cores (logical)"
        );
        // A large host with every slot held, a heavy load and the longest tag
        // still fits in 80 columns.
        let header = format!(
            "host slots: 64 of 64 held (auto: load 99.9, {})",
            cores_phrase(128, 256, Source::LimitedByAffinity)
        );
        assert!(header.len() <= 80, "{} columns: {header}", header.len());
    }

    #[test]
    fn flags_take_a_separate_or_an_inline_value_and_nothing_else() {
        let flags = parse(&os(&["--weight", "2", "--on-timeout=run"])).unwrap();
        assert_eq!(value(&flags, "--weight"), Some("2"));
        assert_eq!(value(&flags, "--on-timeout"), Some("run"));
        assert!(parse(&os(&["--wieght", "2"]))
            .unwrap_err()
            .contains("unknown"));
        assert!(parse(&os(&["extra"])).unwrap_err().contains("unknown"));
        assert!(parse(&os(&["--max-wait"]))
            .unwrap_err()
            .contains("needs a value"));
    }

    #[test]
    fn the_command_is_everything_after_the_first_dashes() {
        let args = os(&[
            "run",
            "--weight",
            "2",
            "--",
            "cargo",
            "test",
            "--",
            "--nocapture",
        ]);
        let (own, cmd) = split_at_dashes(&args);
        assert_eq!(own, &os(&["run", "--weight", "2"])[..]);
        assert_eq!(cmd, &os(&["cargo", "test", "--", "--nocapture"])[..]);
    }
}