tmprl-core 0.1.0

Pure domain logic for tmprl: modes, keymap, command registry
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
//! The workflow domain model.
//!
//! These types live here rather than in `tmprl-client` because logic hangs off them,
//! status ordering, relative ages, merge-sorting a multi-namespace fan-out, re-finding the
//! cursor after a refresh. All of that is computable without a server or a terminal, so it
//! belongs in the crate that needs neither to be tested. `tmprl-client` maps protobuf into
//! these; nothing above it ever sees a generated type.

use std::cmp::Ordering;

/// Execution status, mirroring `temporal.api.enums.v1.WorkflowExecutionStatus`.
///
/// This is a hand-written mirror rather than a re-export so that `tmprl-core` stays free of
/// the generated protos. The conversion in `tmprl-client` matches on the proto enum
/// exhaustively, so a status added by Temporal is a compile error there, not a blank cell
/// here.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum WorkflowStatus {
    #[default]
    Unspecified,
    Running,
    Completed,
    Failed,
    Canceled,
    Terminated,
    ContinuedAsNew,
    TimedOut,
    Paused,
}

impl WorkflowStatus {
    /// Every status, in the order the header renders them: the ones an operator is looking
    /// for first. "How many are broken" is the question people open this screen to answer.
    pub const DISPLAY_ORDER: [WorkflowStatus; 9] = [
        WorkflowStatus::Running,
        WorkflowStatus::Failed,
        WorkflowStatus::TimedOut,
        WorkflowStatus::Terminated,
        WorkflowStatus::Canceled,
        WorkflowStatus::Completed,
        WorkflowStatus::ContinuedAsNew,
        WorkflowStatus::Paused,
        WorkflowStatus::Unspecified,
    ];

    /// The name Temporal uses in a visibility query, and in the `GROUP BY` payloads that
    /// `CountWorkflowExecutions` returns. Round-trips with [`WorkflowStatus::parse`].
    pub fn query_name(self) -> &'static str {
        match self {
            WorkflowStatus::Unspecified => "Unspecified",
            WorkflowStatus::Running => "Running",
            WorkflowStatus::Completed => "Completed",
            WorkflowStatus::Failed => "Failed",
            WorkflowStatus::Canceled => "Canceled",
            WorkflowStatus::Terminated => "Terminated",
            WorkflowStatus::ContinuedAsNew => "ContinuedAsNew",
            WorkflowStatus::TimedOut => "TimedOut",
            WorkflowStatus::Paused => "Paused",
        }
    }

    /// A glyph, so status is legible without colour. `NO_COLOR`, a 16-colour terminal and a
    /// colour-blind reader all get the same information as everyone else.
    pub fn glyph(self) -> char {
        match self {
            WorkflowStatus::Unspecified => '?',
            WorkflowStatus::Running => '',
            WorkflowStatus::Completed => '',
            WorkflowStatus::Failed => '',
            WorkflowStatus::Canceled => '',
            WorkflowStatus::Terminated => '',
            WorkflowStatus::ContinuedAsNew => '',
            WorkflowStatus::TimedOut => '',
            WorkflowStatus::Paused => '',
        }
    }

    /// Parse the name Temporal returns. Accepts the query spelling (`ContinuedAsNew`) and
    /// the proto spelling (`WORKFLOW_EXECUTION_STATUS_CONTINUED_AS_NEW`), because the two
    /// arrive from different RPCs and it is not worth making callers care which.
    pub fn parse(s: &str) -> Option<Self> {
        let t = s.trim().trim_matches('"');
        let squashed: String = t
            .chars()
            .filter(|c| c.is_ascii_alphanumeric())
            .map(|c| c.to_ascii_lowercase())
            .collect();
        let squashed = squashed
            .strip_prefix("workflowexecutionstatus")
            .unwrap_or(&squashed);
        WorkflowStatus::DISPLAY_ORDER
            .iter()
            .copied()
            .find(|s| {
                let name: String = s
                    .query_name()
                    .chars()
                    .map(|c| c.to_ascii_lowercase())
                    .collect();
                name == squashed
            })
            .filter(|_| !squashed.is_empty())
    }

    /// Whether the execution is still open. Closed workflows never change again, which is
    /// what lets the list cache them across a refresh.
    pub fn is_running(self) -> bool {
        matches!(self, WorkflowStatus::Running | WorkflowStatus::Paused)
    }
}

/// One row of the workflow table.
///
/// `namespace` is carried on the row rather than held once for the whole list because a
/// multi-namespace fan-out merges rows from several namespaces into one table, and a row
/// that cannot say where it came from is not actionable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkflowRow {
    pub namespace: String,
    pub workflow_id: String,
    pub run_id: String,
    pub workflow_type: String,
    pub task_queue: String,
    pub status: WorkflowStatus,
    /// Epoch milliseconds. `None` when the server did not set it, which is rare but legal.
    pub start_time: Option<i64>,
    pub close_time: Option<i64>,
    pub history_length: i64,
}

impl WorkflowRow {
    /// Identity of a row across refreshes and across namespaces.
    ///
    /// A run id is unique within a namespace but not across a fan-out, so the key is the
    /// pair. This is what the cursor is anchored to. See [`find_by_key`].
    pub fn key(&self) -> (&str, &str) {
        (self.namespace.as_str(), self.run_id.as_str())
    }
}

/// Newest first, which is the order the workflow list is read in.
///
/// Ties break on the row key so the order is total: a merge of several namespaces that
/// started workflows in the same millisecond must not shuffle between refreshes.
pub fn by_start_time_desc(a: &WorkflowRow, b: &WorkflowRow) -> Ordering {
    b.start_time
        .cmp(&a.start_time)
        .then_with(|| a.namespace.cmp(&b.namespace))
        .then_with(|| a.run_id.cmp(&b.run_id))
}

/// Merge per-namespace pages into one table, newest first.
///
/// This sorts rather than merges pre-sorted runs, because there is nothing to merge: the
/// server does not order `ListWorkflowExecutions`, and standard visibility rejects an
/// `ORDER BY` clause. Ordering is entirely tmprl's job. See [`WorkflowList`].
pub fn merge_by_start_time(pages: Vec<Vec<WorkflowRow>>) -> Vec<WorkflowRow> {
    let mut all: Vec<WorkflowRow> = pages.into_iter().flatten().collect();
    all.sort_by(by_start_time_desc);
    all
}

/// Where a row with this key ended up after a refresh.
///
/// The workflow list is live: rows appear above the cursor while you are reading, so a
/// cursor stored as a row index silently points at a different workflow a second later.
/// Anchoring to the key and re-finding it is the whole fix.
pub fn find_by_key(rows: &[WorkflowRow], key: (&str, &str)) -> Option<usize> {
    rows.iter().position(|r| r.key() == key)
}

/// Counts per status for the list header, from `CountWorkflowExecutions ... GROUP BY`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct StatusCounts {
    /// The server's total. Grouped counts are approximate and may sum to less than this,
    /// which is documented Temporal behaviour, so the total is kept separately rather than
    /// derived.
    pub total: i64,
    counts: Vec<(WorkflowStatus, i64)>,
}

impl StatusCounts {
    pub fn new(total: i64, counts: impl IntoIterator<Item = (WorkflowStatus, i64)>) -> Self {
        let mut counts: Vec<(WorkflowStatus, i64)> = counts.into_iter().collect();
        counts.sort_by_key(|(s, _)| {
            WorkflowStatus::DISPLAY_ORDER
                .iter()
                .position(|d| d == s)
                .unwrap_or(usize::MAX)
        });
        Self { total, counts }
    }

    /// Non-zero counts, in display order.
    pub fn iter(&self) -> impl Iterator<Item = (WorkflowStatus, i64)> + '_ {
        self.counts.iter().copied().filter(|(_, n)| *n > 0)
    }

    pub fn get(&self, status: WorkflowStatus) -> i64 {
        self.counts
            .iter()
            .find(|(s, _)| *s == status)
            .map(|(_, n)| *n)
            .unwrap_or(0)
    }
}

/// A short, fixed-width age like `4s`, `12m`, `3h`, `9d`.
///
/// The workflow list is a dense table on a terminal that may be 80 columns wide, so this
/// trades precision for a column that never wraps. Exact timestamps belong in the detail
/// view, where there is room for them.
pub fn humanize_age_ms(millis: i64) -> String {
    if millis < 0 {
        // Clock skew between the server and this machine. Better to show `0s` than a
        // negative age that looks like a bug in the table.
        return "0s".into();
    }
    let secs = millis / 1000;
    match secs {
        s if s < 60 => format!("{s}s"),
        s if s < 3600 => format!("{}m", s / 60),
        s if s < 86_400 => format!("{}h", s / 3600),
        s => format!("{}d", s / 86_400),
    }
}

/// The workflow table as it accumulates pages.
///
/// Infinite scroll appends pages; a refresh or a new query replaces them. Two properties
/// have to hold no matter which happened:
///
/// * **Sorted newest-first.** The server does not order `ListWorkflowExecutions`, and the
///   dev server's standard visibility store rejects `ORDER BY` outright, so the ordering is
///   this type's job rather than the query's.
/// * **No duplicates.** Pages are snapshots of a set that is changing underneath them, so
///   the same execution can legitimately arrive on two pages. A table that shows a workflow
///   twice makes the operator doubt the whole screen.
#[derive(Debug, Clone, Default)]
pub struct WorkflowList {
    rows: Vec<WorkflowRow>,
    tokens: Vec<(String, Vec<u8>)>,
}

impl WorkflowList {
    pub fn rows(&self) -> &[WorkflowRow] {
        &self.rows
    }

    pub fn len(&self) -> usize {
        self.rows.len()
    }

    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }

    /// Per-namespace continuation tokens to send with the next page request.
    pub fn tokens(&self) -> &[(String, Vec<u8>)] {
        &self.tokens
    }

    /// Whether any namespace still has pages left.
    pub fn has_more(&self) -> bool {
        !self.tokens.is_empty()
    }

    /// Start over: a new query, or a refresh of the current one.
    pub fn reset(&mut self, rows: Vec<WorkflowRow>, tokens: Vec<(String, Vec<u8>)>) {
        self.rows.clear();
        self.tokens = tokens;
        self.insert_sorted(rows);
    }

    /// Add the next page.
    pub fn append(&mut self, rows: Vec<WorkflowRow>, tokens: Vec<(String, Vec<u8>)>) {
        self.tokens = tokens;
        self.insert_sorted(rows);
    }

    /// Where the row with this key sits now, if it is still listed.
    pub fn position_of(&self, key: (&str, &str)) -> Option<usize> {
        find_by_key(&self.rows, key)
    }

    fn insert_sorted(&mut self, rows: Vec<WorkflowRow>) {
        self.rows.extend(rows);
        self.rows.sort_by(by_start_time_desc);
        // `by_start_time_desc` breaks ties on the key, so duplicates are adjacent.
        self.rows.dedup_by(|a, b| a.key() == b.key());
    }
}

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

    fn row(ns: &str, run: &str, start: Option<i64>) -> WorkflowRow {
        WorkflowRow {
            namespace: ns.into(),
            workflow_id: format!("wf-{run}"),
            run_id: run.into(),
            workflow_type: "T".into(),
            task_queue: "q".into(),
            status: WorkflowStatus::Running,
            start_time: start,
            close_time: None,
            history_length: 3,
        }
    }

    #[test]
    fn status_names_round_trip() {
        for s in WorkflowStatus::DISPLAY_ORDER {
            assert_eq!(WorkflowStatus::parse(s.query_name()), Some(s));
        }
    }

    #[test]
    fn status_parses_both_spellings_temporal_uses() {
        // `GROUP BY` payloads arrive quoted, and the proto spelling turns up in errors.
        assert_eq!(
            WorkflowStatus::parse("\"ContinuedAsNew\""),
            Some(WorkflowStatus::ContinuedAsNew)
        );
        assert_eq!(
            WorkflowStatus::parse("WORKFLOW_EXECUTION_STATUS_CONTINUED_AS_NEW"),
            Some(WorkflowStatus::ContinuedAsNew)
        );
        assert_eq!(
            WorkflowStatus::parse("  running  "),
            Some(WorkflowStatus::Running)
        );
        assert_eq!(WorkflowStatus::parse("nonsense"), None);
        assert_eq!(WorkflowStatus::parse(""), None);
    }

    #[test]
    fn every_status_has_a_distinct_glyph() {
        // Two statuses sharing a glyph would make the column ambiguous for exactly the
        // readers the glyph column exists for.
        let mut g: Vec<char> = WorkflowStatus::DISPLAY_ORDER
            .iter()
            .map(|s| s.glyph())
            .collect();
        g.sort_unstable();
        let before = g.len();
        g.dedup();
        assert_eq!(before, g.len(), "duplicate status glyph");
    }

    #[test]
    fn display_order_covers_every_status() {
        // A status missing here would be silently dropped from the header counts.
        assert_eq!(
            WorkflowStatus::DISPLAY_ORDER.len(),
            9,
            "DISPLAY_ORDER must list every WorkflowStatus variant"
        );
        let mut seen = WorkflowStatus::DISPLAY_ORDER.to_vec();
        seen.sort_unstable();
        seen.dedup();
        assert_eq!(seen.len(), 9, "DISPLAY_ORDER repeats a status");
    }

    #[test]
    fn merge_orders_newest_first_across_namespaces() {
        let merged = merge_by_start_time(vec![
            vec![row("a", "a2", Some(200)), row("a", "a1", Some(100))],
            vec![row("b", "b3", Some(300)), row("b", "b0", Some(50))],
        ]);
        let ids: Vec<&str> = merged.iter().map(|r| r.run_id.as_str()).collect();
        assert_eq!(ids, ["b3", "a2", "a1", "b0"]);
    }

    #[test]
    fn merge_is_stable_when_start_times_collide() {
        // Same millisecond, different namespaces: the order must not depend on which page
        // happened to arrive first, or rows shuffle under the cursor on every refresh.
        let one = merge_by_start_time(vec![
            vec![row("b", "x", Some(100))],
            vec![row("a", "y", Some(100))],
        ]);
        let two = merge_by_start_time(vec![
            vec![row("a", "y", Some(100))],
            vec![row("b", "x", Some(100))],
        ]);
        assert_eq!(one, two);
    }

    #[test]
    fn rows_without_a_start_time_sort_last() {
        let merged = merge_by_start_time(vec![
            [row("a", "none", None), row("a", "has", Some(10))].into(),
        ]);
        assert_eq!(merged[0].run_id, "has");
    }

    #[test]
    fn the_cursor_follows_its_run_id_when_rows_shift() {
        let before = [row("a", "r1", Some(100)), row("a", "r2", Some(90))];
        let key = before[0].key();
        let key = (key.0.to_string(), key.1.to_string());

        // A newer workflow arrives at the top, pushing everything down one row.
        let after = vec![
            row("a", "r0", Some(110)),
            row("a", "r1", Some(100)),
            row("a", "r2", Some(90)),
        ];
        assert_eq!(
            find_by_key(&after, (&key.0, &key.1)),
            Some(1),
            "the cursor must follow the run id, not stay on index 0"
        );
    }

    #[test]
    fn a_vanished_row_reports_no_position() {
        let rows = vec![row("a", "r1", Some(100))];
        assert_eq!(find_by_key(&rows, ("a", "gone")), None);
        // A run id from another namespace must not match.
        assert_eq!(find_by_key(&rows, ("other", "r1")), None);
    }

    #[test]
    fn counts_render_in_display_order_and_skip_zeroes() {
        let c = StatusCounts::new(
            10,
            [
                (WorkflowStatus::Completed, 6),
                (WorkflowStatus::Running, 3),
                (WorkflowStatus::Failed, 1),
                (WorkflowStatus::Canceled, 0),
            ],
        );
        let got: Vec<_> = c.iter().map(|(s, n)| (s.query_name(), n)).collect();
        assert_eq!(got, [("Running", 3), ("Failed", 1), ("Completed", 6)]);
        assert_eq!(c.total, 10);
        assert_eq!(c.get(WorkflowStatus::Failed), 1);
        assert_eq!(c.get(WorkflowStatus::TimedOut), 0);
    }

    #[test]
    fn ages_are_short_enough_for_a_narrow_column() {
        assert_eq!(humanize_age_ms(4_000), "4s");
        assert_eq!(humanize_age_ms(59_999), "59s");
        assert_eq!(humanize_age_ms(60_000), "1m");
        assert_eq!(humanize_age_ms(3_600_000), "1h");
        assert_eq!(humanize_age_ms(86_400_000), "1d");
        // Server clock ahead of ours must not render as a negative age.
        assert_eq!(humanize_age_ms(-5_000), "0s");
    }
}

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

    fn row(ns: &str, run: &str, start: i64) -> WorkflowRow {
        WorkflowRow {
            namespace: ns.into(),
            workflow_id: format!("wf-{run}"),
            run_id: run.into(),
            workflow_type: "T".into(),
            task_queue: "q".into(),
            status: WorkflowStatus::Running,
            start_time: Some(start),
            close_time: None,
            history_length: 1,
        }
    }

    fn ids(list: &WorkflowList) -> Vec<&str> {
        list.rows().iter().map(|r| r.run_id.as_str()).collect()
    }

    #[test]
    fn an_empty_list_has_nothing_more_to_fetch() {
        let list = WorkflowList::default();
        assert!(list.is_empty() && !list.has_more() && list.rows().is_empty());
    }

    #[test]
    fn appended_pages_stay_sorted_newest_first() {
        // The server returns pages in no particular order, so a later page routinely
        // contains rows that belong above rows already on screen.
        let mut list = WorkflowList::default();
        list.reset(vec![row("a", "r2", 200)], vec![("a".into(), vec![1])]);
        list.append(vec![row("a", "r3", 300), row("a", "r1", 100)], vec![]);

        assert_eq!(ids(&list), ["r3", "r2", "r1"]);
        assert!(!list.has_more(), "an empty token list ends the scroll");
    }

    #[test]
    fn a_row_arriving_on_two_pages_is_listed_once() {
        // Pages are snapshots of a set that changes underneath them, so overlap is normal.
        let mut list = WorkflowList::default();
        list.reset(vec![row("a", "r1", 100)], vec![("a".into(), vec![1])]);
        list.append(vec![row("a", "r1", 100), row("a", "r0", 50)], vec![]);
        assert_eq!(ids(&list), ["r1", "r0"]);
    }

    #[test]
    fn the_same_run_id_in_two_namespaces_is_two_rows() {
        // Run ids are unique per namespace, not globally: deduplicating on the run id
        // alone would silently hide a row in a fan-out.
        let mut list = WorkflowList::default();
        list.reset(
            vec![row("a", "shared", 100), row("b", "shared", 90)],
            vec![],
        );
        assert_eq!(list.len(), 2);
    }

    #[test]
    fn reset_drops_the_previous_query_s_rows() {
        let mut list = WorkflowList::default();
        list.reset(vec![row("a", "old", 100)], vec![("a".into(), vec![1])]);
        list.reset(vec![row("a", "new", 200)], vec![]);
        assert_eq!(ids(&list), ["new"]);
        assert!(
            !list.has_more(),
            "reset must clear the old continuation token"
        );
    }

    #[test]
    fn the_cursor_key_survives_a_page_landing_above_it() {
        let mut list = WorkflowList::default();
        list.reset(vec![row("a", "r1", 100)], vec![("a".into(), vec![1])]);
        assert_eq!(list.position_of(("a", "r1")), Some(0));

        list.append(vec![row("a", "r9", 900)], vec![]);
        assert_eq!(
            list.position_of(("a", "r1")),
            Some(1),
            "the anchored row moved down; its key must still find it"
        );
        assert_eq!(list.position_of(("a", "gone")), None);
    }

    #[test]
    fn tokens_track_which_namespaces_still_have_pages() {
        let mut list = WorkflowList::default();
        list.reset(
            vec![row("a", "r1", 100)],
            vec![("a".into(), vec![1]), ("b".into(), vec![2])],
        );
        assert!(list.has_more());
        assert_eq!(list.tokens().len(), 2);

        // Namespace `a` exhausts; `b` still has pages.
        list.append(vec![row("b", "r2", 90)], vec![("b".into(), vec![3])]);
        assert_eq!(list.tokens(), &[("b".to_string(), vec![3])]);
        assert!(list.has_more());
    }
}