runner-manager 0.4.12

Local-first autoscaling manager for ephemeral GitHub Actions self-hosted runners, with a CLI and a Ratatui TUI.
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
// owner: b2-local-chain-runner
//
//! Expectation against observation, plane by plane.
//!
//! `02-target-architecture.md`, "After every action, the oracle compares":
//! the exit code and the stable semantic fields of stdout/stderr; the read
//! views where the action is one; persisted state through the public `Store`;
//! filesystem existence beneath the scenario's roots; and the fake GitHub's
//! request history. Each of those is a [`Plane`], and every divergence names
//! the plane it was found on so a failure says *what* disagreed as well as
//! *where*.
//!
//! The expectation is always computed by the model **before** the process
//! runs (see `run.rs`), so nothing here can be satisfied by the product
//! agreeing with itself.
//!
//! Human prose is checked for the model's semantic fragments, not whole
//! snapshots. The two machine-shaped reads are compared field by field:
//! `status --json` against the model's status projection, and the policy rows
//! of `repo list` / `org list` as a set against the model's list lines.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

use serde_json::Value;

use crate::cli_chains::action::Action;
use crate::cli_chains::model::{Model, StatusProjection};
use crate::cli_chains::transition::Transition;
use crate::cli_chains::values::PathValue;

use super::observe::ObservedState;
use super::scenario::{Invocation, RealResolver};

/// Where a divergence was found.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Plane {
    /// The process exit code.
    Exit,
    /// Stdout/stderr fragments and the semantic read views.
    Output,
    /// Credential presence, the host record, policies and retained
    /// diagnostics, read through the public `Store` and the secret store.
    Store,
    /// Scratch directories, the package cache, and confinement inside the
    /// scenario.
    Filesystem,
    /// The fake GitHub's request history for this step.
    Requests,
    /// Protected values in output, logs, files, or a SQLite textual dump.
    Security,
}

impl Plane {
    /// Behavioral expectation planes. Security has its own protected-value by
    /// scanned-plane mutation matrix.
    pub const ALL: [Plane; 5] = [
        Plane::Exit,
        Plane::Output,
        Plane::Store,
        Plane::Filesystem,
        Plane::Requests,
    ];

    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Plane::Exit => "exit",
            Plane::Output => "output",
            Plane::Store => "store",
            Plane::Filesystem => "filesystem",
            Plane::Requests => "requests",
            Plane::Security => "security",
        }
    }
}

impl fmt::Display for Plane {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// One divergence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Mismatch {
    pub plane: Plane,
    pub detail: String,
}

impl Mismatch {
    fn new(plane: Plane, detail: impl Into<String>) -> Self {
        Self {
            plane,
            detail: detail.into(),
        }
    }

    /// A protected value reached a scanned artifact.
    #[must_use]
    pub fn security(detail: impl Into<String>) -> Self {
        Self::new(Plane::Security, detail)
    }
}

/// Judges one executed action.
#[must_use]
pub fn judge_action(
    action: &Action,
    before: &Model,
    expected: &Transition,
    invocation: &Invocation,
    observed: &Result<ObservedState, String>,
    resolver: &RealResolver,
) -> Vec<Mismatch> {
    let mut found = Vec::new();

    if invocation.code != expected.exit.code() {
        found.push(Mismatch::new(
            Plane::Exit,
            format!(
                "expected {}, observed exit code {}",
                expected.exit.describe(),
                invocation.code
            ),
        ));
    }

    for (stream, fragments, text) in [
        ("stdout", &expected.stdout, &invocation.stdout),
        ("stderr", &expected.stderr, &invocation.stderr),
    ] {
        for fragment in fragments {
            let fragment = resolver.resolve_text(fragment);
            if !text.contains(&fragment) {
                found.push(Mismatch::new(
                    Plane::Output,
                    format!("{stream} lacks the fragment {fragment:?}"),
                ));
            }
        }
    }
    found.extend(
        read_view(action, before, &invocation.stdout, resolver)
            .into_iter()
            .map(|detail| Mismatch::new(Plane::Output, detail)),
    );

    found.extend(judge_state(&expected.next, observed));

    let requests: Vec<String> = expected
        .requests
        .iter()
        .map(|request| request.render())
        .collect();
    if invocation.requests != requests {
        found.push(Mismatch::new(
            Plane::Requests,
            format!(
                "expected requests {requests:?}, the fake GitHub saw {:?}",
                invocation.requests
            ),
        ));
    }
    found
}

/// Judges persisted state alone: after a seed, and as part of every action.
#[must_use]
pub fn judge_state(expected: &Model, observed: &Result<ObservedState, String>) -> Vec<Mismatch> {
    let observed = match observed {
        Ok(observed) => observed,
        Err(problem) => {
            return vec![Mismatch::new(
                Plane::Store,
                format!("the persisted state could not be read: {problem}"),
            )];
        }
    };
    let mut found: Vec<Mismatch> = store_differences(expected, &observed.model)
        .into_iter()
        .chain(
            observed
                .anomalies
                .iter()
                .map(|anomaly| format!("unmodelled store state: {anomaly}")),
        )
        .map(|detail| Mismatch::new(Plane::Store, detail))
        .collect();
    found.extend(
        filesystem_differences(expected, &observed.model)
            .into_iter()
            .chain(observed.stray.iter().cloned())
            .map(|detail| Mismatch::new(Plane::Filesystem, detail)),
    );
    found
}

/// The store-plane fields: credential, host record, policies, retained
/// diagnostics.
fn store_differences(expected: &Model, observed: &Model) -> Vec<String> {
    let mut found = Vec::new();
    if expected.credential != observed.credential {
        found.push(format!(
            "credential present: expected {}, observed {}",
            expected.credential, observed.credential
        ));
    }
    if expected.host != observed.host {
        found.push(format!(
            "host record: expected {:?}, observed {:?}",
            expected.host, observed.host
        ));
    }
    let keys: BTreeSet<_> = expected
        .policies
        .keys()
        .chain(observed.policies.keys())
        .collect();
    for key in keys {
        match (expected.policies.get(key), observed.policies.get(key)) {
            (Some(want), Some(have)) if want != have => {
                found.push(format!(
                    "policy {key}: expected {want:?}, observed {have:?}"
                ));
            }
            (Some(_), None) => found.push(format!("policy {key}: expected, but not stored")),
            (None, Some(have)) => {
                found.push(format!("policy {key}: stored but not expected: {have:?}"))
            }
            _ => {}
        }
    }
    if expected.retained != observed.retained {
        found.push(format!(
            "retained diagnostics: expected {:?}, observed {:?}",
            expected.retained, observed.retained
        ));
    }
    found
}

/// The filesystem-plane fields: scratch directories and the package cache.
fn filesystem_differences(expected: &Model, observed: &Model) -> Vec<String> {
    let mut found = Vec::new();
    if expected.directories != observed.directories {
        found.push(format!(
            "scratch directories: expected {:?}, observed {:?}",
            expected.directories, observed.directories
        ));
    }
    if expected.package_cache != observed.package_cache {
        found.push(format!(
            "package cache present: expected {}, observed {}",
            expected.package_cache, observed.package_cache
        ));
    }
    found
}

/// The semantic comparison of a read's machine-shaped output.
fn read_view(
    action: &Action,
    before: &Model,
    stdout: &str,
    resolver: &RealResolver,
) -> Vec<String> {
    match action {
        Action::StatusJson => match serde_json::from_str::<Value>(stdout) {
            Ok(document) => status_differences(&before.status(), &document, resolver),
            Err(error) => vec![format!("status --json is not JSON: {error}")],
        },
        Action::List(scope) => {
            let expected: BTreeSet<String> = before.list_lines(*scope).into_iter().collect();
            let observed: BTreeSet<String> = stdout
                .lines()
                .filter(|line| line.contains("\tenabled="))
                .map(str::to_string)
                .collect();
            if expected == observed {
                Vec::new()
            } else {
                vec![format!(
                    "{} list rows: expected {expected:?}, observed {observed:?}",
                    scope.word()
                )]
            }
        }
        _ => Vec::new(),
    }
}

/// `status --json` against the model's projection, field by field.
#[allow(
    clippy::too_many_lines,
    reason = "one flat field-by-field comparison reads best whole"
)]
fn status_differences(
    expected: &StatusProjection,
    document: &Value,
    resolver: &RealResolver,
) -> Vec<String> {
    let mut found = Vec::new();
    let mut field = |path: &str, want: Value| {
        let have = path
            .split('.')
            .fold(document, |value, segment| &value[segment]);
        if *have != want {
            found.push(format!("status.{path}: expected {want}, observed {have}"));
        }
    };
    field(
        "credential.present",
        Value::from(expected.credential_present),
    );
    field("credential.unreadable", Value::Null);
    field("host.configured", Value::from(expected.host_configured));
    field("host.capacity", Value::from(expected.capacity));
    field("host.in_use", Value::from(expected.in_use));
    field("host.headroom", Value::from(expected.headroom));
    field(
        "host.runner_root_source",
        Value::from(expected.runner_root_source),
    );
    field(
        "host.active_ephemeral_attempts",
        Value::from(expected.active_ephemeral_attempts),
    );
    field(
        "host.cleanup_blocked_ephemeral_attempts",
        Value::from(expected.cleanup_blocked_ephemeral_attempts),
    );
    field(
        "budget.projected_requests_per_hour",
        Value::from(expected.projected_requests_per_hour),
    );
    field(
        "budget.projection_is_floor",
        Value::from(expected.projection_is_floor),
    );
    field("github_contacted", Value::from(false));

    let configured = document["host"]["configured_runner_root"].as_str();
    if let Some(problem) = path_difference(
        "status.host.configured_runner_root",
        expected.configured_runner_root,
        configured,
        resolver,
    ) {
        found.push(problem);
    }

    let empty = Vec::new();
    let reported = document["policies"].as_array().unwrap_or(&empty);
    let observed: BTreeMap<(String, String), &Value> = reported
        .iter()
        .map(|policy| {
            (
                (
                    policy["scope"].as_str().unwrap_or_default().to_string(),
                    policy["target"]
                        .as_str()
                        .unwrap_or_default()
                        .to_ascii_lowercase(),
                ),
                policy,
            )
        })
        .collect();
    if observed.len() != reported.len() {
        found.push(format!(
            "status.policies holds {} entries for {} distinct targets",
            reported.len(),
            observed.len()
        ));
    }
    let wanted: BTreeMap<(String, String), _> = expected
        .policies
        .iter()
        .map(|policy| {
            (
                (policy.scope.to_string(), policy.target.to_ascii_lowercase()),
                policy,
            )
        })
        .collect();
    let keys: BTreeSet<_> = wanted.keys().chain(observed.keys()).cloned().collect();
    for key in keys {
        let label = format!("status.policies[{}:{}]", key.0, key.1);
        let (Some(want), Some(have)) = (wanted.get(&key), observed.get(&key)) else {
            let (present, absent) = if wanted.contains_key(&key) {
                ("expected", "reported")
            } else {
                ("reported", "expected")
            };
            found.push(format!("{label}: {present} but not {absent}"));
            continue;
        };
        let mut compare = |name: &str, want: Value| {
            if have[name] != want {
                found.push(format!(
                    "{label}.{name}: expected {want}, observed {}",
                    have[name]
                ));
            }
        };
        compare("target", Value::from(want.target.clone()));
        compare("mode", Value::from(want.mode));
        compare("state", Value::from(want.state));
        compare("enabled", Value::from(want.enabled));
        compare("min_capacity", Value::from(want.min_capacity));
        compare(
            "max_capacity",
            want.max_capacity.map_or(Value::Null, Value::from),
        );
        compare("active_attempts", Value::from(want.active_attempts));
        compare(
            "cleanup_blocked_attempts",
            Value::from(want.cleanup_blocked_attempts),
        );
        compare("workspace_mode", Value::from(want.workspace_mode));
        compare(
            "workspace_root_source",
            Value::from(want.workspace_root_source),
        );
        if let Some(problem) = path_difference(
            &format!("{label}.workspace_root"),
            want.workspace_root,
            have["workspace_root"].as_str(),
            resolver,
        ) {
            found.push(problem);
        }
        let labels: Vec<String> = have["routing_labels"]
            .as_array()
            .map(|labels| {
                labels
                    .iter()
                    .map(|label| resolver.symbolic_label(label.as_str().unwrap_or_default()))
                    .collect()
            })
            .unwrap_or_default();
        if !same_routing_labels(&want.routing_labels, &labels) {
            found.push(format!(
                "{label}.routing_labels: expected {:?}, observed {labels:?}",
                want.routing_labels
            ));
        }
    }
    found
}

/// The derived label first, then the optional labels in any order.
fn same_routing_labels(expected: &[String], observed: &[String]) -> bool {
    match (expected.split_first(), observed.split_first()) {
        (None, None) => true,
        (Some((want_first, want_rest)), Some((have_first, have_rest))) => {
            let want: BTreeSet<&String> = want_rest.iter().collect();
            let have: BTreeSet<&String> = have_rest.iter().collect();
            want_first == have_first && want == have && want_rest.len() == have_rest.len()
        }
        _ => false,
    }
}

/// A reported path against a modelled one.
fn path_difference(
    what: &str,
    expected: Option<PathValue>,
    reported: Option<&str>,
    resolver: &RealResolver,
) -> Option<String> {
    let observed = reported.map(|text| resolver.identify(text).ok_or(text));
    match (expected, observed) {
        (None, None) => None,
        (Some(want), Some(Ok(have))) if want == have => None,
        (want, have) => Some(format!(
            "{what}: expected {:?}, observed {:?}",
            want.map(PathValue::symbolic),
            have.map(|found| found.map(PathValue::symbolic))
        )),
    }
}