qk-cli 0.6.1

A fast standalone task runner that reads Nx workspaces
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
//! Recording runs and reading them back: `--report`, `qk show runs`,
//! `qk show run` and `qk show task`.

use std::collections::BTreeMap;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Result, bail};
use qk_config::Workspace;
use qk_history::{Cause, History, RunReport, TaskReport};
use qk_runner::RunResult;
use qk_taskgraph::TaskGraph;

/// Where the workspace's history lives: beside its cache, so linked worktrees
/// share it as they share the cache.
pub fn path(workspace: &Workspace) -> PathBuf {
    let cache = qk_cache::cache_directory(&workspace.root);
    cache
        .parent()
        .and_then(Path::parent)
        .map_or_else(|| cache.join("history"), Path::to_path_buf)
        .join("history.db")
}

fn millis(time: SystemTime) -> u64 {
    time.duration_since(UNIX_EPOCH)
        .map_or(0, |duration| duration.as_millis() as u64)
}

/// Records a finished run and writes its report where asked. Failures are
/// reported without failing the run.
pub fn record(
    workspace: &Workspace,
    graph: &TaskGraph,
    result: &RunResult,
    started: SystemTime,
    report: Option<&Path>,
) -> RunReport {
    let mut inputs = BTreeMap::new();
    let tasks = graph
        .tasks
        .iter()
        .map(|(id, task)| {
            let record = result.tasks.get(id);
            if let Some((key, value)) = record.and_then(|record| record.key.as_ref()) {
                inputs.insert(key.clone(), value.clone());
            }
            let status = match record.map(|record| record.outcome) {
                Some(qk_executor::Outcome::Success) => "success",
                Some(qk_executor::Outcome::Failed(_)) => "failure",
                Some(qk_executor::Outcome::Cancelled) => "cancelled",
                None => "skipped",
            };
            TaskReport {
                id: id.clone(),
                project: task.project.clone(),
                target: task.target.clone(),
                configuration: task.configuration.clone(),
                status: status.into(),
                cache: record.map(|record| {
                    serde_json::to_value(record.cache)
                        .ok()
                        .and_then(|value| value.as_str().map(str::to_owned))
                        .unwrap_or_default()
                }),
                key: record.and_then(|record| record.key.as_ref().map(|(key, _)| key.clone())),
                started: record.map(|record| millis(record.started)),
                ended: record.map(|record| millis(record.ended)),
                dependencies: task.dependencies.iter().cloned().collect(),
                cause: None,
                warm: record
                    .and_then(|record| record.warm.as_ref())
                    .and_then(|warm| serde_json::to_value(warm).ok()),
                threads: record
                    .and_then(|record| record.threads)
                    .map(|threads| threads as u64),
                memory: record.and_then(|record| record.memory),
            }
        })
        .collect();
    let started = millis(started);
    let run = RunReport {
        schema_version: qk_history::SCHEMA_VERSION,
        id: format!("{started}-{}", std::process::id()),
        command: std::env::args().collect(),
        sha: std::process::Command::new("git")
            .current_dir(&workspace.root)
            .args(["rev-parse", "HEAD"])
            .output()
            .ok()
            .filter(|output| output.status.success())
            .and_then(|output| String::from_utf8(output.stdout).ok())
            .map(|sha| sha.trim().to_owned()),
        started,
        ended: millis(SystemTime::now()),
        exit_code: result.exit_code,
        tasks,
        critical_path: Default::default(),
    };
    let executions = result
        .tasks
        .iter()
        .filter_map(|(id, record)| {
            let execution = record.execution.as_ref()?;
            Some((
                id.as_str(),
                qk_history::Execution {
                    data: &execution.log.data,
                    truncated: execution.log.truncated,
                    inputs_unchanged: execution.inputs_unchanged,
                },
            ))
        })
        .collect();
    let run = match History::open(&path(workspace))
        .and_then(|mut history| history.record_with_executions(run.clone(), &inputs, &executions))
    {
        Ok(run) => run,
        Err(error) => {
            qk_executor::status!("qk: could not record the run: {error:#}");
            RunReport {
                critical_path: qk_history::critical_path(&run.tasks),
                ..run
            }
        }
    };
    if let Some(report) = report {
        let written = serde_json::to_vec_pretty(&run)
            .map_err(anyhow::Error::from)
            .and_then(|mut bytes| {
                bytes.push(b'\n');
                std::fs::write(report, bytes).map_err(anyhow::Error::from)
            });
        if let Err(error) = written {
            qk_executor::status!(
                "qk: could not write the run report to {}: {error:#}",
                report.display()
            );
        }
    }
    run
}

/// What each task is expected to take, from the runs recorded so far; none
/// when there are none or they cannot be read.
pub fn expected(workspace: &Workspace) -> std::collections::BTreeMap<String, qk_runner::Expected> {
    let path = path(workspace);
    if !path.exists() {
        return Default::default();
    }
    History::open(&path)
        .and_then(|history| history.expected())
        .map(|expected| {
            expected
                .into_iter()
                .map(|(id, expected)| {
                    let expected = qk_runner::Expected {
                        millis: expected.millis,
                        threads: expected.threads,
                        memory: expected.memory,
                    };
                    (id, expected)
                })
                .collect()
        })
        .unwrap_or_default()
}

pub fn open(workspace: &Workspace) -> Result<History> {
    let path = path(workspace);
    if !path.exists() {
        bail!("no runs recorded yet");
    }
    History::open(&path)
}

pub fn show_runs(
    workspace: &Workspace,
    limit: usize,
    json: bool,
    out: &mut impl Write,
) -> Result<()> {
    let runs = open(workspace)?.runs(limit)?;
    if json {
        serde_json::to_writer_pretty(&mut *out, &runs)?;
        writeln!(out)?;
        return Ok(());
    }
    for run in runs {
        let counts: Vec<String> = run
            .cache
            .iter()
            .map(|(status, count)| format!("{count} {status}"))
            .collect();
        writeln!(
            out,
            "{}  {}  exit {}  {}  {} tasks ({})  {}",
            run.id,
            ago(run.started),
            run.exit_code,
            seconds(run.ended.saturating_sub(run.started)),
            run.tasks,
            counts.join(", "),
            command(&run.command),
        )?;
    }
    Ok(())
}

pub fn show_run(
    workspace: &Workspace,
    id: Option<&str>,
    json: bool,
    out: &mut impl Write,
) -> Result<()> {
    let Some(run) = open(workspace)?.run(id)? else {
        bail!("no run {}", id.unwrap_or("recorded"));
    };
    if json {
        serde_json::to_writer_pretty(&mut *out, &run)?;
        writeln!(out)?;
        return Ok(());
    }
    writeln!(
        out,
        "Run {} {}: {}, exit {}, {}",
        run.id,
        ago(run.started),
        command(&run.command),
        run.exit_code,
        seconds(run.ended.saturating_sub(run.started))
    )?;
    let width = run
        .tasks
        .iter()
        .map(|task| task.id.len())
        .max()
        .unwrap_or(0);
    let mut tasks = run.tasks.clone();
    tasks.sort_by_key(|task| task.started);
    for task in &tasks {
        writeln!(
            out,
            "  {:width$}  {:9}  {:10}  {:>7}  {}",
            task.id,
            task.status,
            task.cache.as_deref().unwrap_or(""),
            seconds(task.duration()),
            match (&task.cause, task.cache.as_deref()) {
                (Some(cause), Some("miss")) if cause.changed == ["unchanged"] => {
                    "same key, but no stored entry".into()
                }
                (cause, _) => cause.as_ref().map(summary).unwrap_or_default(),
            }
        )?;
    }
    if !run.critical_path.tasks.is_empty() {
        writeln!(
            out,
            "Critical path, {}: {}",
            seconds(run.critical_path.duration),
            run.critical_path.tasks.join(" -> ")
        )?;
    }
    Ok(())
}

pub fn show_task(
    workspace: &Workspace,
    task: &str,
    limit: usize,
    json: bool,
    out: &mut impl Write,
) -> Result<()> {
    let records = open(workspace)?.task(task, limit)?;
    if records.is_empty() {
        bail!("no recorded runs of {task}");
    }
    if json {
        let records: Vec<_> = records
            .iter()
            .map(|(run, task)| serde_json::json!({"run": run, "task": task}))
            .collect();
        serde_json::to_writer_pretty(&mut *out, &records)?;
        writeln!(out)?;
        return Ok(());
    }
    writeln!(out, "{task}, most recent first:")?;
    for (run, record) in &records {
        writeln!(
            out,
            "  run {run}  {}  {}  {}  {}",
            record.started.map(ago).unwrap_or_default(),
            record.status,
            record.cache.as_deref().unwrap_or("-"),
            seconds(record.duration()),
        )?;
        if let Some(cause) = &record.cause {
            for line in details(cause) {
                writeln!(out, "      {line}")?;
            }
        }
        if let Some(warm) = record.warm.as_ref().and_then(warm_line) {
            writeln!(out, "      {warm}")?;
        }
        if let Some(threads) = record.threads {
            writeln!(
                out,
                "      ran with {threads} thread{}",
                if threads == 1 { "" } else { "s" }
            )?;
        }
        if let Some(memory) = record.memory {
            writeln!(out, "      used {} of memory at most", bytes(memory))?;
        }
    }
    if let Some(effect) = warm_effect(&records) {
        writeln!(out, "{effect}")?;
    }
    Ok(())
}

/// How long the task took when it ran from warm state and when it ran
/// without, over its successful runs that executed rather than hit the cache.
fn warm_effect(records: &[(String, qk_history::TaskReport)]) -> Option<String> {
    let (mut warm, mut cold) = (Vec::new(), Vec::new());
    for (_, record) in records {
        let Some(state) = &record.warm else {
            continue;
        };
        if record.status != "success"
            || !matches!(record.cache.as_deref(), Some("miss" | "uncached"))
        {
            continue;
        }
        let restored = !restored(state).is_empty();
        let present = state
            .get("present")
            .and_then(serde_json::Value::as_array)
            .is_some_and(|groups| !groups.is_empty());
        if restored || present {
            warm.push(record.duration());
        } else {
            cold.push(record.duration());
        }
    }
    if warm.is_empty() || cold.is_empty() {
        return None;
    }
    let average = |durations: &[u64]| durations.iter().sum::<u64>() / durations.len() as u64;
    let runs = |count: usize| if count == 1 { "run" } else { "runs" };
    Some(format!(
        "From warm state it took {} on average over {} {}; without, {} over {} {}.",
        seconds(average(&warm)),
        warm.len(),
        runs(warm.len()),
        seconds(average(&cold)),
        cold.len(),
        runs(cold.len())
    ))
}

/// An amount of memory, in megabytes below a gigabyte.
pub fn bytes(bytes: u64) -> String {
    if bytes < 1_000_000_000 {
        format!("{} MB", bytes.div_ceil(1_000_000))
    } else {
        format!("{:.1} GB", bytes as f64 / 1e9)
    }
}

/// What each warm entry restored for a run. Records from before a target
/// could have several entries hold one object, or null, instead of a list.
pub fn restored(warm: &serde_json::Value) -> Vec<&serde_json::Value> {
    match warm.get("restored") {
        Some(serde_json::Value::Array(entries)) => entries.iter().collect(),
        Some(entry @ serde_json::Value::Object(_)) => vec![entry],
        _ => Vec::new(),
    }
}

/// What warm state did for a run, in words.
pub fn warm_line(warm: &serde_json::Value) -> Option<String> {
    let saved = warm.get("saveMs").and_then(serde_json::Value::as_u64);
    let mut parts = Vec::new();
    for restored in restored(warm) {
        let number = |field| restored.get(field).and_then(serde_json::Value::as_u64);
        let files = number("files").unwrap_or(0);
        let group = restored
            .get("group")
            .and_then(serde_json::Value::as_str)
            .map(|group| format!(" for group {group}"))
            .unwrap_or_default();
        parts.push(format!(
            "warm state{group} restored from {}: {files} file{}, {:.1} MB",
            restored
                .get("source")
                .and_then(serde_json::Value::as_str)
                .unwrap_or("?"),
            if files == 1 { "" } else { "s" },
            number("bytes").unwrap_or(0) as f64 / 1e6
        ));
    }
    if let Some(saved) = saved {
        parts.push(format!("warm state saved in {}", seconds(saved)));
    } else if warm.get("background").and_then(serde_json::Value::as_bool) == Some(true) {
        parts.push("warm state saved in the background".to_owned());
    }
    (!parts.is_empty()).then(|| parts.join("; "))
}

/// One line on why a key changed.
fn summary(cause: &Cause) -> String {
    match cause.changed.as_slice() {
        [only] if only == "unchanged" => String::new(),
        [only] if only == "first" => "first run".into(),
        [only] if only == "unknown" => "changed; previous inputs not kept".into(),
        groups => format!("changed: {}", groups.join(", ")),
    }
}

/// Why a key changed, in full.
fn details(cause: &Cause) -> Vec<String> {
    let since = cause
        .previous
        .as_ref()
        .map(|previous| format!(" since run {}", previous.run))
        .unwrap_or_default();
    let mut lines = match cause.changed.as_slice() {
        [only] if only == "unchanged" => vec![format!(
            "same key as run {}",
            cause
                .previous
                .as_ref()
                .map_or("-", |previous| &previous.run)
        )],
        [only] if only == "first" => vec!["first recorded run of this task".into()],
        [only] if only == "unknown" => vec![format!(
            "key changed{since}; the previous inputs are no longer kept"
        )],
        groups => vec![format!("key changed{since}: {}", groups.join(", "))],
    };
    lines.extend(differences(cause));
    lines
}

/// The fields, files, values and dependencies two keys differ in, a line each.
pub fn differences(cause: &Cause) -> Vec<String> {
    const LISTED: usize = 10;
    let mut lines = Vec::new();
    let mut list = |label: &str, items: &[String]| {
        for item in items.iter().take(LISTED) {
            lines.push(format!("  {label} {item}"));
        }
        if items.len() > LISTED {
            lines.push(format!("  {label} ... {} more", items.len() - LISTED));
        }
    };
    list("field", &cause.definition);
    list("changed", &cause.files.changed);
    list("added", &cause.files.added);
    list("removed", &cause.files.removed);
    list("value", &cause.values);
    list("dependency", &cause.dependencies);
    lines
}

fn command(arguments: &[String]) -> String {
    let mut arguments = arguments.iter();
    let program = arguments
        .next()
        .map(|program| {
            Path::new(program)
                .file_name()
                .map_or(program.clone(), |name| name.to_string_lossy().into_owned())
        })
        .unwrap_or_default();
    std::iter::once(program)
        .chain(arguments.cloned())
        .collect::<Vec<_>>()
        .join(" ")
}

fn seconds(milliseconds: u64) -> String {
    format!("{:.1}s", milliseconds as f64 / 1000.0)
}

fn ago(millis: u64) -> String {
    let now = self::millis(SystemTime::now());
    let seconds = now.saturating_sub(millis) / 1000;
    match seconds {
        0..60 => format!("{seconds}s ago"),
        60..3600 => format!("{} min ago", seconds / 60),
        3600..86400 => format!("{} h ago", seconds / 3600),
        _ => format!("{} days ago", seconds / 86400),
    }
}

/// Lists mixed outcomes and the exact executions whose logs can be inspected.
pub fn show_flaky(
    workspace: &Workspace,
    task: Option<&str>,
    limit: usize,
    json: bool,
    out: &mut impl Write,
) -> Result<()> {
    let groups = open(workspace)?.flaky(task, limit)?;
    if json {
        serde_json::to_writer_pretty(&mut *out, &groups)?;
        writeln!(out)?;
        return Ok(());
    }
    if groups.is_empty() {
        writeln!(
            out,
            "No mixed outcomes for identical declared inputs in retained history."
        )?;
    }
    for group in groups {
        writeln!(
            out,
            "{}  key {}  {} passed, {} failed (identical declared inputs)",
            group.task, group.key, group.successes, group.failures
        )?;
        for execution in group.executions {
            writeln!(
                out,
                "  {}  {}  {}",
                execution.run,
                execution.status,
                if execution.log_available {
                    "log retained"
                } else {
                    "log unavailable"
                }
            )?;
        }
    }
    Ok(())
}

/// Replays one execution in recorded chunk order, preserving stdout and stderr.
pub fn show_log(workspace: &Workspace, run: &str, task: &str) -> Result<()> {
    let Some(log) = open(workspace)?.log(run, task)? else {
        bail!(
            "no retained execution log for {task} in run {run} (cache hit, older run or log evicted)"
        );
    };
    qk_executor::read_capture(log.data.as_slice(), |stderr, bytes| {
        if stderr {
            std::io::stderr().lock().write_all(bytes)
        } else {
            std::io::stdout().lock().write_all(bytes)
        }
    })?;
    if log.truncated {
        eprintln!("qk: execution log truncated at 4 MiB");
    }
    Ok(())
}