onetaskgraph-github-projects 0.2.24

A onetaskgraph source over GitHub Projects.
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! The live lane's own decisions, asserted without reaching GitHub.
//!
//! Which board and repository the credentialed lane may write to, which artifacts a run
//! recognises as its own and as an interrupted earlier run's, and that cleanup runs whether the
//! journey passed or failed. None of it touches the network or reads a credential, so these
//! assertions hold on every machine — including the ones the journey beside them skips on for
//! want of a credential, which is where the decisions asserted here would otherwise go
//! unproven.

mod lane;

use std::time::Duration;

use lane::{
    LABEL_PREFIX, LiveLane, LiveSecret, SESSION_NAME, artifact_label, artifact_title,
    is_orphan_label, is_orphan_title, is_run_artifact_label, is_run_artifact_title, live_lane,
    live_write_config, rest_outcome, run_then_cleanup,
};
use onetaskgraph_github_projects::DESIGN_TITLE_PREFIX;
use onetaskgraph_github_projects::accounting::{Outcome, StatusCode};
use onetaskgraph_live::artifact::{Registration, Registry, Run, Sweep, WIDEST_STAMP};
use onetaskgraph_live::{Credential, Exclusivity, Session};
use onetaskgraph_plugin_api::{SourceName, SourcePlugin};
use std::num::NonZeroU32;

/// A window these assertions drive either side of, rather than waiting out the real one.
///
/// Safety here does not rest on the window's length — what permits a removal is the owning
/// run's registration lock, and the window can only hold a removal back — so a check may
/// choose whichever window makes what it is proving visible.
const WINDOW: Duration = Duration::from_secs(60);

/// Microseconds since the epoch, far enough in that ageing a stamp cannot go negative.
const NOW: u64 = 1_787_816_134_627_361;

/// A machine identity written out by hand, for the runs this file names itself.
fn host(number: u32) -> NonZeroU32 {
    NonZeroU32::new(number).expect("a check's own host identity is never zero")
}

/// The same for a run's process id, which no operating system numbers zero.
fn process(number: u32) -> NonZeroU32 {
    NonZeroU32::new(number).expect("a process id this check names is never zero")
}

/// A stamp `by` wrote `windows` windows ago.
fn aged(windows: u64) -> u64 {
    NOW - windows * u64::try_from(WINDOW.as_micros()).expect("a minute fits in microseconds")
}

#[tokio::test]
async fn cleanup_runs_after_a_successful_live_journey() {
    let cleaned = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let observed = cleaned.clone();
    assert_eq!(
        run_then_cleanup(
            || async { Ok(()) },
            || async move {
                observed.store(true, std::sync::atomic::Ordering::SeqCst);
                Ok(())
            }
        )
        .await,
        Ok(())
    );
    assert!(cleaned.load(std::sync::atomic::Ordering::SeqCst));
}

#[tokio::test]
async fn cleanup_runs_after_a_failed_live_journey() {
    let cleaned = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let observed = cleaned.clone();
    assert_eq!(
        run_then_cleanup(
            || async { Err("injected mutation failure".to_owned()) },
            || async move {
                observed.store(true, std::sync::atomic::Ordering::SeqCst);
                Ok(())
            }
        )
        .await,
        Err("injected mutation failure".to_owned())
    );
    assert!(cleaned.load(std::sync::atomic::Ordering::SeqCst));
}

#[test]
fn a_response_this_lane_could_not_read_is_not_reported_as_one_github_answered() {
    // The session report is what says a run's calls were worth what they cost, so a `2xx`
    // carrying a body this lane could not decode has to be recorded as the refusal it was
    // — otherwise the report names a call that succeeded and produced nothing.
    let ok = StatusCode::OK;
    assert_eq!(
        rest_outcome(ok, false, "{\"id\":\"L_1\"}", true),
        Outcome::Answered,
        "a 2xx this lane read is what it says it is"
    );
    assert_eq!(
        rest_outcome(ok, false, "<html>a proxy said hello</html>", false),
        Outcome::Refused,
        "a 2xx this lane could not read answered nothing"
    );
    // The two the status alone settles are still the status's to settle: a refusal stays a
    // refusal whether or not its body parsed, and a rate-limited call still did not run.
    assert_eq!(
        rest_outcome(StatusCode::NOT_FOUND, false, "{}", true),
        Outcome::Refused
    );
    assert_eq!(
        rest_outcome(StatusCode::FORBIDDEN, true, "rate limit exceeded", false),
        Outcome::RateLimited,
        "a body that did not parse must not turn a rate-limited call into an ordinary refusal"
    );
}

#[test]
fn the_lane_takes_its_board_and_repository_only_from_the_names_it_is_given() {
    assert_eq!(
        live_lane(
            Some("live-token"),
            Some("nickderobertis"),
            Some("1"),
            Some("acme/work"),
            None
        ),
        Ok(LiveLane::Run {
            token: Credential::new("live-token").expect("a placeholder credential is not blank"),
            owner: "nickderobertis".to_owned(),
            project_number: 1,
            repository: "acme/work".to_owned(),
        })
    );
}

#[test]
fn the_lanes_write_configuration_is_accepted_whatever_the_board_calls_its_first_column() {
    // The lane discovers this option name from the board at run time, so its configuration
    // has to be accepted whichever name comes back — including each name a shipped default
    // already claims, which is where pointing `unknown` at the column collided.
    for option in ["Todo", "Backlog", "In Progress", "Ready for review"] {
        onetaskgraph_github_projects::Plugin
            .build(
                &SourceName::new("github-live").unwrap(),
                &live_write_config("nickderobertis", 1, "acme/work", option),
                &LiveSecret("live-token".into()),
            )
            .unwrap_or_else(|error| {
                panic!(
                    "the live write configuration was refused for a board whose first Status \
                     option is {option:?}: {error}"
                )
            });
    }
}

#[test]
fn an_unnamed_board_skips_the_lane_and_says_which_two_names_it_needs() {
    let Ok(LiveLane::Skip(reason)) =
        live_lane(Some("live-token"), None, None, Some("acme/work"), None)
    else {
        panic!("a credential without a named board must skip rather than discover one");
    };
    assert!(
        reason.contains("GH_PROJECTS_OWNER") && reason.contains("GH_PROJECTS_NUMBER"),
        "the skip must name both variables, not just report a skip: {reason}"
    );
}

#[test]
fn an_unnamed_board_fails_the_lane_when_the_live_tier_is_required() {
    let error = live_lane(Some("live-token"), None, None, Some("acme/work"), Some("1"))
        .expect_err("ONETASKGRAPH_LIVE_REQUIRED=1 must turn the unnamed-board skip into a failure");
    assert!(
        error.contains("GH_PROJECTS_OWNER")
            && error.contains("GH_PROJECTS_NUMBER")
            && error.contains("ONETASKGRAPH_LIVE_REQUIRED"),
        "the failure must name both variables and what demanded them: {error}"
    );
}

#[test]
fn an_absent_credential_keeps_its_own_skip_or_fail_pairing() {
    let Ok(LiveLane::Skip(reason)) = live_lane(
        None,
        Some("nickderobertis"),
        Some("1"),
        Some("acme/work"),
        None,
    ) else {
        panic!("an absent credential must skip");
    };
    assert!(reason.contains("GH_PROJECTS_TOKEN"), "{reason}");
    let error = live_lane(
        None,
        Some("nickderobertis"),
        Some("1"),
        Some("acme/work"),
        Some("1"),
    )
    .expect_err("ONETASKGRAPH_LIVE_REQUIRED=1 must turn the absent-credential skip into a failure");
    assert!(error.contains("GH_PROJECTS_TOKEN"), "{error}");
    let Ok(LiveLane::Skip(empty)) = live_lane(
        Some("  "),
        Some("nickderobertis"),
        Some("1"),
        Some("acme/work"),
        None,
    ) else {
        panic!("an empty credential must skip");
    };
    assert!(empty.contains("GH_PROJECTS_TOKEN"), "{empty}");
}

#[test]
fn half_a_board_and_an_unusable_number_are_misconfigurations_rather_than_skips() {
    for (owner, number) in [
        (Some("nickderobertis"), None),
        (None, Some("1")),
        (Some("nickderobertis"), Some("0")),
        (Some("nickderobertis"), Some("not-a-number")),
        (Some("nickderobertis"), Some("4294967295")),
    ] {
        live_lane(Some("live-token"), owner, number, Some("acme/work"), None).expect_err(&format!(
            "GH_PROJECTS_OWNER={owner:?} with GH_PROJECTS_NUMBER={number:?} must fail rather than \
             skip or select a board"
        ));
    }
}

#[test]
fn an_unnamed_repository_skips_the_lane_and_a_malformed_one_is_a_misconfiguration() {
    // A lane that creates issues names the repository it creates them in, for the reason it
    // names the board: a credentialed write must not land somewhere nobody nominated.
    let Ok(LiveLane::Skip(reason)) = live_lane(
        Some("live-token"),
        Some("nickderobertis"),
        Some("1"),
        None,
        None,
    ) else {
        panic!("an unnamed repository must skip rather than discover one");
    };
    assert!(reason.contains("GH_PROJECTS_REPOSITORY"), "{reason}");
    let required = live_lane(
        Some("live-token"),
        Some("nickderobertis"),
        Some("1"),
        None,
        Some("1"),
    )
    .expect_err("ONETASKGRAPH_LIVE_REQUIRED=1 turns that skip into a failure");
    assert!(required.contains("GH_PROJECTS_REPOSITORY"), "{required}");
    for malformed in ["nameless", "acme/", "/work", "acme/work/extra"] {
        live_lane(
            Some("live-token"),
            Some("nickderobertis"),
            Some("1"),
            Some(malformed),
            None,
        )
        .expect_err(&format!("GH_PROJECTS_REPOSITORY={malformed:?} must fail"));
    }
}

#[test]
fn a_repository_nomination_that_would_address_something_else_is_refused_before_anything_is_sent() {
    // Both halves are filled into this lane's REST endpoint templates, so a character that
    // is significant in a URL does not name a repository — it redirects a credentialed
    // call, and the endpoint the session report names is then not the one that was made.
    // Each of these splits on a slash and would have passed the owner/name check alone.
    for redirecting in [
        "acme/work?per_page=1",
        "acme/work#fragment",
        "acme/work%2f..",
        "acme/..",
        "acme/.",
        "acme/wo rk",
        "acme/{repo}",
        "-acme/work",
        "acme-/work",
        "ac--me/work",
        "ac me/work",
        "acme./work",
    ] {
        let refusal = live_lane(
            Some("live-token"),
            Some("nickderobertis"),
            Some("1"),
            Some(redirecting),
            None,
        )
        .expect_err(&format!(
            "GH_PROJECTS_REPOSITORY={redirecting:?} reaches a URL, so it must be refused rather \
             than sent"
        ));
        assert!(
            refusal.contains("GH_PROJECTS_REPOSITORY") && refusal.contains(redirecting),
            "the refusal names the variable and the value given: {refusal}"
        );
    }
    // The nomination CI actually sets still passes, so the grammar refuses a redirect
    // rather than the real board's own repository.
    assert!(matches!(
        live_lane(
            Some("live-token"),
            Some("nickderobertis"),
            Some("1"),
            Some("nickderobertis/onetaskgraph"),
            None,
        ),
        Ok(LiveLane::Run { .. })
    ));
}

/// A registry of this check's own, holding one run that has ended and one that is live.
///
/// Real registrations rather than a description of them: `ended` was taken and given up, so
/// the kernel reports its lock free, and `live` is still held. That is the whole of the
/// evidence a sweep decides on, and it is the same evidence whichever process holds the lock
/// — flock and `LockFileEx` both refuse a second handle in the process that already has one.
struct Runs {
    directory: std::path::PathBuf,
    registry: Registry,
    mine: Run,
    ended: Run,
    live: Run,
    _held: Registration,
}

impl Runs {
    fn take() -> Self {
        let directory = std::env::temp_dir().join(format!(
            "onetaskgraph-lane-shape-runs-{}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&directory);
        let registry = Registry::at(&directory);
        let mine = registry.enrol();
        // Registered, and then really given up — which is the state the kernel leaves behind
        // for a process that has died, and the only state that authorises a removal.
        let ended = {
            let registration =
                Registration::take(&registry, process(2533)).expect("a run that then ends here");
            registration.run()
        };
        let held = Registration::take(&registry, process(9998)).expect("a run that is still going");
        let live = held.run();
        Self {
            directory,
            registry,
            mine,
            ended,
            live,
            _held: held,
        }
    }

    fn sweep(&self, now: u64) -> Sweep {
        self.registry.sweep(self.mine, now, WINDOW)
    }

    /// The machine this check's own registry answers for.
    fn host(&self) -> NonZeroU32 {
        self.registry
            .host()
            .expect("a registry this check can write has an identity")
    }
}

impl Drop for Runs {
    /// Best effort: the registration this check still holds is an open file, which Windows
    /// will not let a directory be removed under. Leaving it is what the platform's own
    /// temporary directory is for.
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.directory);
    }
}

#[test]
fn a_sweep_takes_an_ended_runs_artifacts_and_leaves_every_live_runs_alone() {
    let runs = Runs::take();
    let sweep = runs.sweep(NOW);
    let (ended, live, mine) = (runs.ended, runs.live, runs.mine);
    // What the sweep exists for: a run the kernel says has ended, whose artifact has waited
    // the window out.
    assert!(is_orphan_title(&sweep, &artifact_title(ended, aged(2))));
    // And the things it must never take. A fresh artifact of an ended run has not waited the
    // window out; an artifact of a run that is STILL GOING is that run's however old it is,
    // which is what a hung or rate-limited session looks like and what age alone cannot
    // protect; one of THIS run is this run's whatever its age; and a title that is not
    // spelled the way this lane spells one belongs to somebody else entirely.
    for left in [
        artifact_title(ended, NOW),
        artifact_title(live, aged(1_000)),
        artifact_title(live, NOW),
        artifact_title(mine, aged(1_000)),
        artifact_title(mine, NOW),
        artifact_title(Run::vouched(runs.host(), process(4242)), aged(1_000)),
        artifact_title(
            Run::vouched(
                host(runs.host().get().wrapping_add(1).max(1)),
                process(2533),
            ),
            aged(1_000),
        ),
        "AI Orchestrator plan".to_owned(),
        "onetaskgraph live cleanup".to_owned(),
        "onetaskgraph live cleanup 2533".to_owned(),
        format!("onetaskgraph live cleanup abc-{}", aged(2)),
        "onetaskgraph live cleanup 9999-".to_owned(),
        format!("copy of {}", artifact_title(ended, aged(2))),
    ] {
        assert!(
            !is_orphan_title(&sweep, &left),
            "a sweep must not take {left:?}"
        );
    }

    // A *document* this lane writes is titled the way this source spells one — the design
    // prefix, put there by the source rather than by the caller — so cleanup reads a title
    // the artifact prefix does not start. Recognition takes that prefix off first: without
    // it, a document a run created would be residue no sweep could ever name, on somebody's
    // real board.
    assert!(is_orphan_title(
        &sweep,
        &format!("{DESIGN_TITLE_PREFIX}{}", artifact_title(ended, aged(2)))
    ));
    assert!(
        !is_orphan_title(
            &sweep,
            &format!("{DESIGN_TITLE_PREFIX}{}", artifact_title(mine, aged(2)))
        ),
        "and this run's own document is this run's, exactly as its issue is"
    );
    for foreign in [
        format!("{DESIGN_TITLE_PREFIX}AI Orchestrator plan"),
        format!("{DESIGN_TITLE_PREFIX}onetaskgraph live cleanup 9999"),
        format!(
            "copy of {DESIGN_TITLE_PREFIX}{}",
            artifact_title(ended, aged(2))
        ),
    ] {
        assert!(
            !is_orphan_title(&sweep, &foreign),
            "a sweep must not take {foreign:?}"
        );
    }

    // The label is residue exactly as an item is, and is decided the same way.
    assert!(is_orphan_label(&sweep, &artifact_label(ended, aged(2))));
    for left in [
        artifact_label(ended, NOW),
        artifact_label(live, aged(1_000)),
        artifact_label(mine, aged(1_000)),
        "bug".to_owned(),
        "otg-live-".to_owned(),
        "otg-live-9999".to_owned(),
        format!("otg-live-abc-{}", aged(2)),
        format!("not-{}", artifact_label(ended, aged(2))),
    ] {
        assert!(
            !is_orphan_label(&sweep, &left),
            "a label sweep must not take {left:?}"
        );
    }
}

#[test]
fn a_label_this_lane_writes_fits_inside_the_limit_github_holds_one_to() {
    // Fifty characters, and a stamp now names the machine as well as the process — which is
    // why the label prefix is the short one. A label GitHub refuses is an artifact this lane
    // cannot write at all, and it would fail on somebody's real repository rather than here.
    // The room a stamp needs is the contract's own figure rather than one counted again here.
    assert!(
        LABEL_PREFIX.len() + WIDEST_STAMP <= 50,
        "the widest label this lane could write is {} characters",
        LABEL_PREFIX.len() + WIDEST_STAMP
    );
}

#[test]
fn a_run_names_its_own_artifacts_and_no_other_runs() {
    // Teardown's half: a run removes everything it wrote, whether its assertions passed or
    // failed, by the run every one of its artifacts carries.
    let mine = Run::vouched(host(41), process(2533));
    assert!(is_run_artifact_title(mine, &artifact_title(mine, 17)));
    assert!(is_run_artifact_title(
        mine,
        &format!("{DESIGN_TITLE_PREFIX}{}", artifact_title(mine, 17))
    ));
    assert!(is_run_artifact_label(mine, &artifact_label(mine, 17)));
    for other in [
        artifact_title(Run::vouched(host(41), process(25330)), 17),
        artifact_title(Run::vouched(host(41), process(253)), 17),
        artifact_title(Run::vouched(host(41), process(12533)), 17),
        // The same process id on another machine is another run, and this is the one a
        // process id alone could not tell apart.
        artifact_title(Run::vouched(host(42), process(2533)), 17),
        format!(
            "{DESIGN_TITLE_PREFIX}{}",
            artifact_title(Run::vouched(host(41), process(25330)), 17)
        ),
        "onetaskgraph live cleanup 2533".to_owned(),
        "AI Orchestrator plan".to_owned(),
    ] {
        assert!(
            !is_run_artifact_title(mine, &other),
            "one run's cleanup must not match {other:?}"
        );
    }
    for other in [
        artifact_label(Run::vouched(host(41), process(25330)), 17),
        artifact_label(Run::vouched(host(42), process(2533)), 17),
        "otg-live-2533".to_owned(),
        "bug".to_owned(),
    ] {
        assert!(
            !is_run_artifact_label(mine, &other),
            "one run's label cleanup must not match {other:?}"
        );
    }
}

#[test]
fn a_second_session_of_this_lane_opens_beside_the_first_rather_than_being_declined() {
    // The seat this lane used to take is gone, and this is what that means: two sessions of
    // it on one machine no longer exclude one another. They cannot reach each other's work —
    // every artifact carries its own run's process id and the sweep above is decided on
    // that — and a seat never excluded the case that is left, which is two hosted runners.
    let held = Session::open(
        SESSION_NAME,
        Credential::new("live-token").expect("a placeholder credential is not blank"),
        Exclusivity::Shared,
    )
    .unwrap_or_else(|declined| {
        panic!(
            "this lane takes no seat, so nothing may decline it here: {}",
            declined.message()
        )
    });
    assert_eq!(held.seat_path(), None, "this lane took a seat");
    let beside = Session::open(
        SESSION_NAME,
        Credential::new("live-token").expect("a placeholder credential is not blank"),
        Exclusivity::Shared,
    )
    .unwrap_or_else(|declined| {
        panic!(
            "a second session of this lane was declined while the first was open: {}",
            declined.message()
        )
    });
    assert_eq!(beside.seat_path(), None, "this lane took a seat");
}

#[test]
fn an_unreadable_live_tier_demand_is_a_misconfiguration() {
    for unusable in ["yes", "true", "2", "on"] {
        live_lane(
            Some("live-token"),
            Some("nickderobertis"),
            Some("1"),
            Some("acme/work"),
            Some(unusable),
        )
        .expect_err(&format!(
            "ONETASKGRAPH_LIVE_REQUIRED={unusable:?} must fail rather than quietly mean not-required"
        ));
    }
    assert_eq!(
        live_lane(Some("live-token"), None, None, Some("acme/work"), Some("0")),
        live_lane(Some("live-token"), None, None, Some("acme/work"), None),
        "0 and unset both mean the lane may skip"
    );
}