Skip to main content

isothrum_runner/error/
timed_out.rs

1//! Which limit a step that ran out of time ran into.
2
3use serde::{Deserialize, Serialize};
4
5/// Which limit a step ran into: a timeout's, also the `error` event's `timeout`.
6///
7/// A step's limit (its `timeout_s`) counts the time it shows no progress: progress is an output
8/// line, on either stream, that isn't blank and isn't one of the last distinct lines it printed
9/// (a stuck download repeating `Downloading 1.121GB` makes none). A step that prints nothing has
10/// its limit from its start. A step that keeps making progress still stops at a ceiling: six
11/// hours from its start, or its limit when that is longer.
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
13#[serde(tag = "kind", rename_all = "snake_case")]
14#[non_exhaustive]
15pub enum TimedOut {
16    /// It showed no progress for its limit.
17    NoProgress {
18        /// The limit, in seconds.
19        after_s: u64,
20        /// The last line it printed (secrets masked, cut to 300 bytes); none when it printed
21        /// nothing.
22        #[serde(default, skip_serializing_if = "Option::is_none")]
23        last_line: Option<String>,
24    },
25    /// It was still running at the ceiling: a step still making progress, or a tool call at its
26    /// deadline (a command in a machine that never returned).
27    Ceiling {
28        /// The ceiling, in seconds from the step's start.
29        after_s: u64,
30        /// The last line it printed (secrets masked, cut to 300 bytes); none when it printed
31        /// nothing, or when it isn't said.
32        #[serde(default, skip_serializing_if = "Option::is_none")]
33        last_line: Option<String>,
34    },
35}
36
37/// What a timeout keeps of a tool's last line: its first 300 bytes (on a character's boundary).
38pub(crate) fn kept_line(line: &str) -> String {
39    let mut end = line.len().min(300);
40    while !line.is_char_boundary(end) {
41        end -= 1;
42    }
43    line.get(..end).unwrap_or_default().to_owned()
44}
45
46impl TimedOut {
47    /// The limit it ran into, in seconds.
48    #[must_use]
49    pub fn after_s(&self) -> u64 {
50        match self {
51            TimedOut::NoProgress { after_s, .. } | TimedOut::Ceiling { after_s, .. } => *after_s,
52        }
53    }
54
55    /// The same, its last line passed through `mask`.
56    pub(crate) fn masked(self, mask: impl FnOnce(&str) -> String) -> TimedOut {
57        match self {
58            TimedOut::NoProgress { after_s, last_line } => TimedOut::NoProgress {
59                after_s,
60                last_line: last_line.map(|l| mask(&l)),
61            },
62            TimedOut::Ceiling { after_s, last_line } => TimedOut::Ceiling {
63                after_s,
64                last_line: last_line.map(|l| mask(&l)),
65            },
66        }
67    }
68}
69
70impl std::fmt::Display for TimedOut {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        match self {
73            TimedOut::NoProgress {
74                after_s,
75                last_line: Some(line),
76            } => {
77                write!(f, "showed no progress for {after_s}s and was stopped; its last line: `{line}`")
78            }
79            TimedOut::NoProgress { after_s, last_line: None } => write!(f, "printed nothing for {after_s}s and was stopped"),
80            TimedOut::Ceiling { after_s, last_line: None } => {
81                write!(f, "was stopped after {after_s}s, the most a step may run, though it kept making progress")
82            }
83            TimedOut::Ceiling {
84                after_s,
85                last_line: Some(line),
86            } => write!(
87                f,
88                "was still running after {after_s}s, the most it may run, and was stopped; its last line: `{line}`"
89            ),
90        }
91    }
92}