pitchfork-cli 2.35.0

Daemons with DX
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
//! Program Status Protocol (OSC 7501) reports from daemon output.
//!
//! A daemon can tell its supervisor what it is doing by writing
//! `ESC ] 7501 ; state=working:progress=40:msg=<base64> ESC \` (or ending in
//! BEL). Only the root record is tracked: a report that names an `id` belongs
//! to a child record and is ignored.
//!
//! Spec: <https://www.superlogical.com/rex/docs/build/program-status>

use base64::Engine as _;
use base64::engine::general_purpose::STANDARD;
use serde::{Deserialize, Serialize};
use std::fmt;

const PREFIX: &str = "\x1b]7501;";
const MAX_SEQUENCE: usize = 4096;
const MAX_KEY: usize = 16;
const MAX_MSG_ENCODED: usize = 2732;
const MAX_MSG_DECODED: usize = 2048;
const MAX_TITLE_ENCODED: usize = 256;
const MAX_TITLE_DECODED: usize = 192;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProgramState {
    Idle,
    Working,
    Done,
    Blocked,
    Error,
}

impl ProgramState {
    fn parse(s: &str) -> Option<Self> {
        Some(match s {
            "idle" => Self::Idle,
            "working" => Self::Working,
            "done" => Self::Done,
            "blocked" => Self::Blocked,
            "error" => Self::Error,
            _ => return None,
        })
    }

    pub fn as_str(self) -> &'static str {
        match self {
            Self::Idle => "idle",
            Self::Working => "working",
            Self::Done => "done",
            Self::Blocked => "blocked",
            Self::Error => "error",
        }
    }

    /// `idle`, `working` and `blocked` describe a live process; `done` and
    /// `error` outlive it.
    pub fn ends_with_process(self) -> bool {
        matches!(self, Self::Idle | Self::Working | Self::Blocked)
    }
}

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

/// The last root-record status a daemon reported.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProgramStatus {
    pub state: ProgramState,
    /// 0 to 100; only kept for `working` and `blocked`.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub progress: Option<u8>,
    /// One human-readable line, with control and invisible formatting
    /// characters removed.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub msg: Option<String>,
}

impl ProgramStatus {
    /// `working 40% Syncing issues`
    pub fn summary(&self) -> String {
        let mut out = self.state.to_string();
        if let Some(p) = self.progress {
            out.push_str(&format!(" {p}%"));
        }
        if let Some(msg) = &self.msg {
            out.push(' ');
            out.push_str(msg);
        }
        out
    }
}

/// What a report does to the root record.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StatusUpdate {
    Set(ProgramStatus),
    Clear,
}

/// Every valid root-record report in `line`, in order.
#[cfg(test)]
fn parse_line(line: &str) -> Vec<StatusUpdate> {
    OscScanner::default().feed(line.as_bytes())
}

enum ScanState {
    /// Matching the opening `ESC ] 7501 ;`; holds how many bytes matched.
    Search(usize),
    Body,
    /// Saw `ESC` in the body, which ends it only if `\` follows.
    BodyEsc,
}

/// Finds reports in a byte stream, however it is split into reads.
///
/// A report ends with ST (`ESC \` or BEL), not a newline, so a daemon that
/// prints one on its own still has it seen right away. State carries across
/// calls to `feed`; a report that never terminates is dropped.
pub struct OscScanner {
    state: ScanState,
    body: Vec<u8>,
}

impl Default for OscScanner {
    fn default() -> Self {
        Self {
            state: ScanState::Search(0),
            body: Vec::new(),
        }
    }
}

impl OscScanner {
    pub fn feed(&mut self, bytes: &[u8]) -> Vec<StatusUpdate> {
        let prefix = PREFIX.as_bytes();
        let mut updates = Vec::new();
        for &b in bytes {
            match self.state {
                ScanState::Search(n) => {
                    self.state = if b == prefix[n] {
                        if n + 1 == prefix.len() {
                            self.body.clear();
                            ScanState::Body
                        } else {
                            ScanState::Search(n + 1)
                        }
                    } else if b == prefix[0] {
                        ScanState::Search(1)
                    } else {
                        ScanState::Search(0)
                    };
                }
                ScanState::Body => match b {
                    0x07 => self.finish(1, &mut updates),
                    0x1b => self.state = ScanState::BodyEsc,
                    // The body has no newline, so this is not a report.
                    b'\n' => self.state = ScanState::Search(0),
                    _ if self.body.len() >= MAX_SEQUENCE => self.state = ScanState::Search(0),
                    _ => self.body.push(b),
                },
                ScanState::BodyEsc => {
                    if b == b'\\' {
                        self.finish(2, &mut updates);
                    } else {
                        // An escape that does not end the report starts afresh.
                        self.state = if b == prefix[0] {
                            ScanState::Search(1)
                        } else {
                            ScanState::Search(0)
                        };
                    }
                }
            }
        }
        updates
    }

    fn finish(&mut self, term_len: usize, updates: &mut Vec<StatusUpdate>) {
        self.state = ScanState::Search(0);
        if PREFIX.len() + self.body.len() + term_len > MAX_SEQUENCE {
            return;
        }
        if let Ok(body) = std::str::from_utf8(&self.body)
            && let Some(update) = parse_body(body)
        {
            updates.push(update);
        }
    }
}

fn parse_body(body: &str) -> Option<StatusUpdate> {
    // Feature detection query; nothing to record.
    if body.trim() == "?" {
        return None;
    }
    let mut state = None;
    let mut id_present = false;
    let mut progress = None;
    let mut msg = None;
    for pair in body.split(':') {
        let Some((key, value)) = pair.split_once('=') else {
            continue;
        };
        let (key, value) = (key.trim(), value.trim());
        if key.is_empty()
            || key.len() > MAX_KEY
            || !key.bytes().all(|b| b.is_ascii_lowercase())
            || !value
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b"_.,+/=-".contains(&b))
        {
            continue;
        }
        match key {
            "state" => state = Some(value),
            "id" => id_present = true,
            "progress" => {
                progress = value.parse::<u8>().ok().filter(|p| *p <= 100);
            }
            "msg" => msg = Some(value),
            "title" => {
                // Not shown, but a title that breaks a limit voids the report.
                decode_text(value, MAX_TITLE_ENCODED, MAX_TITLE_DECODED)?;
            }
            _ => {}
        }
    }
    let state_str = state?;
    if state_str == "clear" {
        // `clear` with an id removes a child record, which is not tracked.
        return (!id_present).then_some(StatusUpdate::Clear);
    }
    let state = ProgramState::parse(state_str)?;
    let msg = match msg {
        Some(m) => {
            Some(decode_text(m, MAX_MSG_ENCODED, MAX_MSG_DECODED)?).filter(|m| !m.is_empty())
        }
        None => None,
    };
    if id_present {
        return None;
    }
    let progress =
        progress.filter(|_| matches!(state, ProgramState::Working | ProgramState::Blocked));
    Some(StatusUpdate::Set(ProgramStatus {
        state,
        progress,
        msg,
    }))
}

/// Decode a base64 free-text value. `None` voids the report: a limit broken, bad
/// base64 or UTF-8, or a control character in the text. Invisible formatting
/// characters, such as text direction overrides, are dropped.
fn decode_text(encoded: &str, max_encoded: usize, max_decoded: usize) -> Option<String> {
    if encoded.len() > max_encoded {
        return None;
    }
    let bytes = STANDARD.decode(encoded).ok()?;
    if bytes.len() > max_decoded {
        return None;
    }
    let text = String::from_utf8(bytes).ok()?;
    if text.chars().any(char::is_control) {
        return None;
    }
    Some(text.chars().filter(|c| !is_invisible_format(*c)).collect())
}

fn is_invisible_format(c: char) -> bool {
    matches!(
        c,
        '\u{00AD}'
            | '\u{061C}'
            | '\u{180E}'
            | '\u{200B}'..='\u{200F}'
            | '\u{202A}'..='\u{202E}'
            | '\u{2060}'..='\u{2064}'
            | '\u{2066}'..='\u{206F}'
            | '\u{FEFF}'
    )
}

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

    fn enc(s: &str) -> String {
        STANDARD.encode(s)
    }

    fn set(state: ProgramState, progress: Option<u8>, msg: Option<&str>) -> StatusUpdate {
        StatusUpdate::Set(ProgramStatus {
            state,
            progress,
            msg: msg.map(str::to_string),
        })
    }

    #[test]
    fn parses_state_progress_and_message() {
        let line = format!(
            "\x1b]7501;state=working:progress=40:msg={}\x1b\\",
            enc("Syncing issues")
        );
        assert_eq!(
            parse_line(&line),
            vec![set(ProgramState::Working, Some(40), Some("Syncing issues"))]
        );
    }

    #[test]
    fn accepts_bel_terminator_and_surrounding_output() {
        let line = "before \x1b]7501;state=done\x07 after";
        assert_eq!(parse_line(line), vec![set(ProgramState::Done, None, None)]);
    }

    #[test]
    fn returns_reports_in_order() {
        let line = "\x1b]7501;state=working\x07\x1b]7501;state=error\x07";
        assert_eq!(
            parse_line(line),
            vec![
                set(ProgramState::Working, None, None),
                set(ProgramState::Error, None, None)
            ]
        );
    }

    #[test]
    fn progress_only_with_working_or_blocked() {
        assert_eq!(
            parse_line("\x1b]7501;state=done:progress=50\x07"),
            vec![set(ProgramState::Done, None, None)]
        );
        assert_eq!(
            parse_line("\x1b]7501;state=blocked:kind=auth:progress=7\x07"),
            vec![set(ProgramState::Blocked, Some(7), None)]
        );
        assert_eq!(
            parse_line("\x1b]7501;state=working:progress=101\x07"),
            vec![set(ProgramState::Working, None, None)]
        );
    }

    #[test]
    fn ignores_unknown_state_and_missing_state() {
        assert!(parse_line("\x1b]7501;state=sleeping\x07").is_empty());
        assert!(parse_line("\x1b]7501;progress=3\x07").is_empty());
    }

    #[test]
    fn ignores_child_records_and_feature_queries() {
        assert!(parse_line("\x1b]7501;state=working:id=build/test\x07").is_empty());
        assert!(parse_line("\x1b]7501;?\x07").is_empty());
        assert!(parse_line("\x1b]7501;state=clear:id=build\x07").is_empty());
    }

    #[test]
    fn clear_without_id_clears() {
        assert_eq!(
            parse_line("\x1b]7501;state=clear\x07"),
            vec![StatusUpdate::Clear]
        );
    }

    #[test]
    fn malformed_pairs_are_skipped_and_last_value_wins() {
        let line = "\x1b]7501;junk:state=idle:Bad=1:state=working\x07";
        assert_eq!(
            parse_line(line),
            vec![set(ProgramState::Working, None, None)]
        );
    }

    #[test]
    fn unknown_keys_are_ignored() {
        let line = "\x1b]7501;state=idle:app=cargo:future=1\x07";
        assert_eq!(parse_line(line), vec![set(ProgramState::Idle, None, None)]);
    }

    #[test]
    fn message_with_control_character_voids_the_report() {
        let line = format!("\x1b]7501;state=working:msg={}\x07", enc("a\x1b[31mb"));
        assert!(parse_line(&line).is_empty());
    }

    #[test]
    fn invisible_formatting_is_stripped() {
        let line = format!("\x1b]7501;state=done:msg={}\x07", enc("ok\u{202E}gnp"));
        assert_eq!(
            parse_line(&line),
            vec![set(ProgramState::Done, None, Some("okgnp"))]
        );
    }

    #[test]
    fn oversized_message_voids_the_report() {
        let line = format!(
            "\x1b]7501;state=working:msg={}\x07",
            enc(&"x".repeat(MAX_MSG_DECODED + 1))
        );
        assert!(parse_line(&line).is_empty());
    }

    #[test]
    fn invalid_base64_voids_the_report() {
        // A byte outside the value set skips only that pair.
        assert_eq!(
            parse_line("\x1b]7501;state=working:msg=@@@\x07"),
            vec![set(ProgramState::Working, None, None)]
        );
        assert!(parse_line("\x1b]7501;state=working:msg=abc\x07").is_empty());
    }

    #[test]
    fn unterminated_sequence_is_not_a_report() {
        assert!(parse_line("\x1b]7501;state=working").is_empty());
    }

    #[test]
    fn a_report_split_across_reads_and_without_newline_is_seen() {
        let mut scanner = OscScanner::default();
        assert!(scanner.feed(b"x\x1b]75").is_empty());
        assert!(scanner.feed(b"01;state=work").is_empty());
        assert_eq!(
            scanner.feed(b"ing\x1b"),
            vec![],
            "ESC alone does not end a report"
        );
        assert_eq!(
            scanner.feed(b"\\"),
            vec![set(ProgramState::Working, None, None)]
        );
    }

    #[test]
    fn a_newline_inside_a_report_abandons_it() {
        let mut scanner = OscScanner::default();
        assert!(scanner.feed(b"\x1b]7501;state=working\n\x07").is_empty());
        assert_eq!(
            scanner.feed(b"\x1b]7501;state=done\x07"),
            vec![set(ProgramState::Done, None, None)]
        );
    }

    #[test]
    fn summary_joins_parts() {
        let status = ProgramStatus {
            state: ProgramState::Working,
            progress: Some(40),
            msg: Some("Syncing".into()),
        };
        assert_eq!(status.summary(), "working 40% Syncing");
    }
}