qctl 0.4.2

Control in-repo YAML work queues
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
use crate::cli::{
    AddArgs, ArchiveArgs, CloseFromGitArgs, Disposition, InitArgs, ParkArgs, PromoteArgs,
};
use crate::document::Document;
use crate::ledger::order::{self, Row};
use crate::ledger::{load, next_id, resolve_path};
use crate::report::{Destination, Report};
use crate::schema::{HorizonTask, QueuedTask};
use crate::trailers;
use anyhow::{Context, Result, bail, ensure};
use indoc::formatdoc;
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use time::macros::format_description;
use time::{OffsetDateTime, UtcOffset};

pub fn init(args: &InitArgs) -> Result<Report> {
    let prefix = args.prefix.to_ascii_uppercase();
    ensure!(
        valid_prefix(&prefix),
        "prefix must match [A-Z][A-Z0-9]{{1,7}}"
    );
    let path = resolve_path(&args.ledger);
    if path.exists() && !args.force {
        bail!("{} already exists (pass --force)", path.display());
    }
    if let Some(parent) = path.parent()
        && !parent.as_os_str().is_empty()
    {
        fs::create_dir_all(parent)?;
    }
    let version = crate::schema::VERSION;
    let body = formatdoc! {"
        # yaml-language-server: $schema=https://raw.githubusercontent.com/victor-software-house/qctl/main/schema/tasks.schema.json
        schema_version: {version}
        prefix: {prefix}
        active: null
        queue: []
        archive: []
        horizon: []
    "};
    fs::write(&path, body).with_context(|| path.display().to_string())?;
    Ok(Report::Initialized {
        path: path.display().to_string(),
        prefix,
    })
}

pub fn add(args: &AddArgs) -> Result<Report> {
    if args.horizon {
        ensure!(
            args.blocked_by.is_empty(),
            "horizon rows have no blocked_by"
        );
        ensure!(
            args.acceptance.is_empty(),
            "horizon rows have no acceptance"
        );
        add_horizon(args)
    } else {
        ensure!(
            args.kind.is_none() && args.open.is_none(),
            "--kind and --open belong to --horizon"
        );
        add_queue(args)
    }
}

fn add_queue(args: &AddArgs) -> Result<Report> {
    ensure!(
        !args.acceptance.is_empty(),
        "add to the queue needs --acceptance"
    );
    let path = resolve_path(&args.ledger);
    let ledger = load(&path)?;
    require_plan(&path, args.plan.as_deref())?;
    let id = next_id(&ledger)?;
    let row = QueuedTask {
        id: id.clone(),
        title: args.title.clone(),
        scope: args.scope.clone(),
        outcome: args.outcome.clone(),
        blocked_by: args.blocked_by.clone(),
        acceptance: args.acceptance.clone(),
        patch: args.patch.clone(),
        plan: args.plan.clone(),
        links: args.links.clone(),
        notes: args.note.clone(),
    };

    let mut ids: Vec<String> = ledger.queue.iter().map(|task| task.id.clone()).collect();
    let insertion = insertion_index(&ids, args.before.as_deref(), args.after.as_deref())?;
    if insertion == 0
        && !ids.is_empty()
        && let Some(active) = ledger.active.as_deref()
    {
        bail!(
            "add --before {before} would make {id} queue[0] while active is {active}",
            before = args.before.as_deref().unwrap_or(active)
        );
    }
    let mut ordered: Vec<Row<'_>> = ledger
        .queue
        .iter()
        .map(|task| Row {
            id: &task.id,
            blockers: &task.blocked_by,
        })
        .collect();
    ordered.insert(
        insertion,
        Row {
            id: &id,
            blockers: &args.blocked_by,
        },
    );
    order::validate(&ordered)?;

    let mut document = read(&path)?;
    document.append("queue", &yaml_serde::to_value(&row)?)?;
    if insertion < ids.len() {
        ids.insert(insertion, id.clone());
        document.reorder_rows("queue", &ids)?;
    }
    write(&path, document)?;
    Ok(Report::Added {
        path: path.display().to_string(),
        id,
        destination: Destination::Queue,
    })
}

fn add_horizon(args: &AddArgs) -> Result<Report> {
    let kind = args.kind.context("--horizon needs --kind")?;
    let open = args.open.as_deref().context("--horizon needs --open")?;
    let path = resolve_path(&args.ledger);
    let ledger = load(&path)?;
    require_plan(&path, args.plan.as_deref())?;
    let id = next_id(&ledger)?;
    let row = HorizonTask {
        id: id.clone(),
        title: args.title.clone(),
        scope: args.scope.clone(),
        outcome: args.outcome.clone(),
        kind,
        open: open.to_owned(),
        patch: args.patch.clone(),
        plan: args.plan.clone(),
        links: args.links.clone(),
        notes: args.note.clone(),
    };
    let mut document = read(&path)?;
    document.append("horizon", &yaml_serde::to_value(&row)?)?;
    write(&path, document)?;
    Ok(Report::Added {
        path: path.display().to_string(),
        id,
        destination: Destination::Horizon,
    })
}

fn insertion_index(ids: &[String], before: Option<&str>, after: Option<&str>) -> Result<usize> {
    match (before, after) {
        (None, None) => Ok(ids.len()),
        (Some(_), Some(_)) => bail!("use --before or --after, not both"),
        (Some(before), None) => ids
            .iter()
            .position(|id| id == before)
            .with_context(|| format!("{before} is not queued")),
        (None, Some(after)) => {
            let index = ids
                .iter()
                .position(|id| id == after)
                .with_context(|| format!("{after} is not queued"))?;
            Ok(index + 1)
        }
    }
}

pub fn start(args: &crate::cli::IdArgs) -> Result<Report> {
    let path = resolve_path(&args.ledger);
    let ledger = load(&path)?;
    let task = ledger
        .queue
        .iter()
        .find(|task| task.id == args.id)
        .with_context(|| format!("{} is not queued", args.id))?;
    ensure!(task.blocked_by.is_empty(), "{} is blocked", args.id);

    let mut document = read(&path)?;
    document.move_to_front("queue", &args.id)?;
    document.set("active", yaml_serde::Value::from(args.id.as_str()))?;
    write(&path, document)?;
    Ok(Report::Started {
        path: path.display().to_string(),
        id: args.id.clone(),
    })
}

pub fn park(args: &ParkArgs) -> Result<Report> {
    let path = resolve_path(&args.ledger);
    let ledger = load(&path)?;
    ensure!(
        ledger.queue.iter().any(|task| task.id == args.id),
        "{} is not queued",
        args.id
    );
    for task in &ledger.queue {
        ensure!(
            !task.blocked_by.contains(&args.id),
            "{} still blocks {}; a parked row remains a real blocker",
            args.id,
            task.id
        );
    }

    let mut document = read(&path)?;
    document.move_to_end(
        "queue",
        "horizon",
        &args.id,
        &["blocked_by", "acceptance"],
        &[
            ("kind", yaml_serde::Value::from(args.kind.to_string())),
            ("open", yaml_serde::Value::from(args.open.as_str())),
        ],
    )?;
    if ledger.active.as_deref() == Some(args.id.as_str()) {
        document.set("active", yaml_serde::Value::Null)?;
    }
    write(&path, document)?;
    Ok(Report::Parked {
        path: path.display().to_string(),
        id: args.id.clone(),
    })
}

pub fn promote(args: &PromoteArgs) -> Result<Report> {
    ensure!(
        !args.acceptance.is_empty(),
        "promote onto the queue needs --acceptance"
    );
    let path = resolve_path(&args.ledger);
    let ledger = load(&path)?;
    ensure!(
        ledger.horizon.iter().any(|task| task.id == args.id),
        "{} is not on the horizon",
        args.id
    );
    for blocker in &args.blocked_by {
        ensure!(
            ledger.queue.iter().any(|queued| queued.id == *blocker),
            "{} <- {blocker} is not queued",
            args.id
        );
    }

    let mut document = read(&path)?;
    document.move_to_end(
        "horizon",
        "queue",
        &args.id,
        &["kind", "open"],
        &[
            ("blocked_by", yaml_serde::to_value(&args.blocked_by)?),
            ("acceptance", yaml_serde::to_value(&args.acceptance)?),
        ],
    )?;
    write(&path, document)?;
    Ok(Report::Promoted {
        path: path.display().to_string(),
        id: args.id.clone(),
    })
}

pub fn archive(args: &ArchiveArgs) -> Result<Report> {
    let path = archive_row(args)?;
    Ok(Report::Archived {
        path,
        id: args.id.clone(),
    })
}

fn archive_row(args: &ArchiveArgs) -> Result<String> {
    let path = resolve_path(&args.ledger);
    let ledger = load(&path)?;
    ensure!(
        ledger.queue.iter().any(|task| task.id == args.id),
        "{} is not queued",
        args.id
    );
    let now = now_in(&ledger.style.timezone)?;

    let mut document = read(&path)?;
    // The row loses what only a queued row carries and gains what only an
    // archived one does on the way across, while it stands alone.
    document.move_between(
        "queue",
        "archive",
        &args.id,
        &["blocked_by", "acceptance"],
        &[
            ("completed", yaml_serde::Value::from(now.as_str())),
            ("evidence", yaml_serde::to_value(&args.evidence)?),
            (
                "disposition",
                yaml_serde::Value::from(args.disposition.as_str()),
            ),
        ],
    )?;
    // A blocker is only resolved against the queue, so a row still naming this
    // id would fail `check` the moment this verb returns. What each row keeps
    // comes from the ledger this verb already validated, not from reading its
    // own output back.
    for task in &ledger.queue {
        if !task.blocked_by.contains(&args.id) {
            continue;
        }
        let kept: Vec<String> = task
            .blocked_by
            .iter()
            .filter(|blocker| **blocker != args.id)
            .cloned()
            .collect();
        document.rewrite_blockers("queue", &task.id, &kept)?;
    }
    let next = document.ids("queue")?.first().cloned();
    document.set(
        "active",
        next.map_or(yaml_serde::Value::Null, |id| {
            yaml_serde::Value::from(id.as_str())
        }),
    )?;
    write(&path, document)?;
    Ok(path.display().to_string())
}

pub fn close_from_git(args: &CloseFromGitArgs) -> Result<Report> {
    let path = resolve_path(&args.ledger);
    let root = path.parent().unwrap_or(&path);
    let closed = if args.pre_push {
        let stdin = std::io::read_to_string(std::io::stdin()).context("read pre-push stdin")?;
        trailers::closed_ids_pre_push(root, &stdin)?
    } else if let Some(range) = &args.range {
        trailers::closed_ids_rev(root, &[range.as_str()])?
    } else {
        trailers::closed_ids_rev(root, &[])?
    };
    let ledger = load(&path)?;
    let queued: HashSet<String> = ledger.queue.iter().map(|task| task.id.clone()).collect();
    let mut seen = HashSet::new();
    let mut archived = Vec::new();
    for (id, sha) in closed {
        if !queued.contains(&id) || !seen.insert(id.clone()) {
            continue;
        }
        archive_row(&ArchiveArgs {
            id: id.clone(),
            ledger: args.ledger.clone(),
            evidence: vec![sha],
            disposition: Disposition::Completed,
        })?;
        archived.push(id);
    }
    let ledger = load(&path)?;
    if let Some(active) = &ledger.active
        && seen.contains(active)
    {
        bail!("active {active} still names a closed id");
    }
    Ok(Report::ClosedFromGit {
        path: path.display().to_string(),
        retry_push: args.pre_push && !archived.is_empty(),
        archived,
    })
}

/// Now, in the zone this ledger declares, written the one way a stamp is
/// written. No offset: the file says which zone its stamps are in, once.
fn now_in(zone: &str) -> Result<String> {
    let offset = UtcOffset::parse(
        zone,
        format_description!("[offset_hour sign:mandatory]:[offset_minute]"),
    )
    .with_context(|| format!("{zone} is not an offset from UTC"))?;
    OffsetDateTime::now_utc()
        .to_offset(offset)
        .format(format_description!(
            "[year]-[month]-[day]T[hour]:[minute]:[second]"
        ))
        .context("format the moment this row left the queue")
}

pub(crate) fn read(path: &Path) -> Result<Document> {
    let source = fs::read_to_string(path).with_context(|| path.display().to_string())?;
    Ok(Document::new(source))
}

/// Write only a document that still reads as the ledger it was, so a verb can
/// never leave a file behind that the next one cannot open.
///
/// A ledger whose style asks for it is normalized on the way out. Off by
/// default, because normalizing touches lines the verb had no business in, and a
/// diff that shows only the work is worth more than one that is always tidy.
pub(crate) fn write(path: &Path, document: Document) -> Result<()> {
    let source = document.into_source();
    crate::document::must_still_parse(&source)?;
    let edited: crate::ledger::Ledger =
        serde_yml::from_str(&source).context("read back what this verb wrote")?;
    let source = if edited.style.normalize_on_write {
        crate::format::normalized(&source, &edited)?
    } else {
        source
    };
    fs::write(path, source).with_context(|| path.display().to_string())
}

pub(crate) fn require_plan(ledger: &Path, plan: Option<&str>) -> Result<()> {
    let Some(plan) = plan else {
        return Ok(());
    };
    ensure!(!plan.is_empty(), "--plan needs a path");
    crate::schema::inside_the_repo(plan, &())
        .map_err(|error| anyhow::anyhow!("plan {plan} {error}"))?;
    let parent = ledger.parent().unwrap_or(Path::new("."));
    ensure!(parent.join(plan).is_file(), "missing plan {plan}");
    Ok(())
}

fn valid_prefix(prefix: &str) -> bool {
    let mut chars = prefix.chars();
    matches!(chars.next(), Some('A'..='Z'))
        && prefix.len() >= 2
        && prefix.len() <= 8
        && chars.all(|ch| ch.is_ascii_uppercase() || ch.is_ascii_digit())
}