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
//! Diagnostics as data: the closed reason vocabulary and the typed error
//! report.
//!
//! An error is a value with named parts, rendered at the boundary — never
//! a pre-formatted string assembled where the failure happened. The exit
//! code stays the coarse machine signal; the `reason` beside it is the
//! fine one, drawn from one closed vocabulary agents can branch on the
//! way scripts branch on exit codes. The vocabulary is append-only: a
//! reason is never renamed and never reused.
use serde::Serialize;
/// The version of the JSON diagnostic's shape.
pub const DIAGNOSTIC_SCHEMA: &str = "rk.diagnostic/1";
/// The closed reason vocabulary, the machine twin of the exit-code matrix.
///
/// Many reasons map to one exit code — that is the point: the code carries
/// the category, the reason carries the instance. Classification is honest
/// or absent: a failure nothing can classify further stays [`Reason::Io`]
/// or [`Reason::Internal`] rather than guessing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum Reason {
/// Semantically invalid arguments or names.
Usage,
/// The named target does not exist or is not usable as a target.
TargetNotFound,
/// No forge could be detected from the repository's remote.
ForgeUndetected,
/// The detected or named forge is not one the binary supports.
ForgeUnsupported,
/// A prerequisite probe failed before any side effect.
PrerequisiteUnmet,
/// The forge CLI is present and not authenticated.
ForgeAuthentication,
/// The forge refused the caller's permissions.
ForgePermission,
/// The forge rate-limited the call.
ForgeRateLimit,
/// The forge failed in a way a retry can cure.
ForgeTemporary,
/// The remote state changed under the run.
RemoteConflict,
/// The command refused an action it judged destructive.
DestructiveRefusal,
/// The state found differs from what would permit the action.
StateDrift,
/// A record or document declares a schema this binary does not know.
UnsupportedSchema,
/// A mutating run could not create its journal.
JournalUnavailable,
/// A child process could not be spawned.
SubprocessSpawn,
/// A child process ran and failed without a finer classification.
SubprocessFailed,
/// Filesystem failure.
Io,
/// A defect in this binary.
Internal,
/// A hand-authored target configuration is invalid.
ConfigInvalid,
}
/// Every reason, in declaration order; a test asserts against this so an
/// addition is deliberate and a rename impossible.
pub const REASONS: [Reason; 19] = [
Reason::Usage,
Reason::TargetNotFound,
Reason::ForgeUndetected,
Reason::ForgeUnsupported,
Reason::PrerequisiteUnmet,
Reason::ForgeAuthentication,
Reason::ForgePermission,
Reason::ForgeRateLimit,
Reason::ForgeTemporary,
Reason::RemoteConflict,
Reason::DestructiveRefusal,
Reason::StateDrift,
Reason::UnsupportedSchema,
Reason::JournalUnavailable,
Reason::SubprocessSpawn,
Reason::SubprocessFailed,
Reason::Io,
Reason::Internal,
Reason::ConfigInvalid,
];
impl Reason {
/// The kebab-case wire form, identical to the serde rendering.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Usage => "usage",
Self::TargetNotFound => "target-not-found",
Self::ForgeUndetected => "forge-undetected",
Self::ForgeUnsupported => "forge-unsupported",
Self::PrerequisiteUnmet => "prerequisite-unmet",
Self::ForgeAuthentication => "forge-authentication",
Self::ForgePermission => "forge-permission",
Self::ForgeRateLimit => "forge-rate-limit",
Self::ForgeTemporary => "forge-temporary",
Self::RemoteConflict => "remote-conflict",
Self::DestructiveRefusal => "destructive-refusal",
Self::StateDrift => "state-drift",
Self::UnsupportedSchema => "unsupported-schema",
Self::JournalUnavailable => "journal-unavailable",
Self::SubprocessSpawn => "subprocess-spawn",
Self::SubprocessFailed => "subprocess-failed",
Self::Io => "io",
Self::Internal => "internal",
Self::ConfigInvalid => "config-invalid",
}
}
}
/// One failure, with its parts named.
///
/// A hint that is not known is omitted rather than invented, so every
/// optional field serializes only when present.
#[derive(Debug, Clone, Serialize)]
pub struct Diagnostic {
/// The shape version of the JSON rendering.
pub schema: &'static str,
/// One entry from the closed vocabulary.
pub reason: Reason,
/// What happened, one line.
pub message: String,
/// What would have had to be true.
#[serde(skip_serializing_if = "Option::is_none")]
pub expected: Option<String>,
/// The exact command or change that fixes it, when known.
#[serde(skip_serializing_if = "Option::is_none")]
pub action: Option<String>,
/// Whether rerunning as-is can succeed.
#[serde(skip_serializing_if = "Option::is_none")]
pub retry: Option<bool>,
/// What the run left behind, stated plainly.
#[serde(skip_serializing_if = "Option::is_none")]
pub target_state: Option<String>,
/// The step it happened in, where there is one.
#[serde(skip_serializing_if = "Option::is_none")]
pub step: Option<String>,
/// The run journal explaining it, where one was written.
#[serde(skip_serializing_if = "Option::is_none")]
pub run: Option<String>,
}
impl Diagnostic {
/// A diagnostic carrying only its reason and message; the builder
/// methods add what is actually known.
#[must_use]
pub fn new(reason: Reason, message: impl Into<String>) -> Self {
Self {
schema: DIAGNOSTIC_SCHEMA,
reason,
message: message.into(),
expected: None,
action: None,
retry: None,
target_state: None,
step: None,
run: None,
}
}
/// State what would have had to be true.
#[must_use]
pub fn expected(mut self, expected: impl Into<String>) -> Self {
self.expected = Some(expected.into());
self
}
/// Name the command or change that fixes it.
#[must_use]
pub fn action(mut self, action: impl Into<String>) -> Self {
self.action = Some(action.into());
self
}
/// State what the run left behind.
#[must_use]
pub fn target_state(mut self, state: impl Into<String>) -> Self {
self.target_state = Some(state.into());
self
}
/// Name the step the failure happened in.
#[must_use]
pub fn step(mut self, step: impl Into<String>) -> Self {
self.step = Some(step.into());
self
}
/// Name the run journal that explains the failure.
#[must_use]
pub fn run(mut self, run: impl Into<String>) -> Self {
self.run = Some(run.into());
self
}
/// The human rendering: the five questions in order — what happened,
/// what was expected, what to do, how to resume, what state the target
/// is in — with unknown lines absent rather than invented.
#[must_use]
pub fn render_human(&self) -> String {
use std::fmt::Write as _;
let mut text = format!("error: {}", self.message);
if let Some(expected) = &self.expected {
let _ = write!(text, "\n expected {expected}");
}
if let Some(action) = &self.action {
let _ = write!(text, "\n next {action}");
}
if let Some(retry) = self.retry {
let answer = if retry {
"rerunning as-is can succeed"
} else {
"rerunning as-is fails the same way"
};
let _ = write!(text, "\n retry {answer}");
}
if let Some(state) = &self.target_state {
let _ = write!(text, "\n state {state}");
}
if let Some(run) = &self.run {
let _ = write!(text, "\n run {run}");
}
text
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::expect_used)]
use super::{Diagnostic, REASONS, Reason};
/// The vocabulary is closed and append-only: this list is the one a
/// deliberate addition extends, and a rename fails here first.
#[test]
fn the_reason_vocabulary_is_closed() {
let wire: Vec<&str> = REASONS.iter().map(|reason| reason.as_str()).collect();
assert_eq!(
wire,
[
"usage",
"target-not-found",
"forge-undetected",
"forge-unsupported",
"prerequisite-unmet",
"forge-authentication",
"forge-permission",
"forge-rate-limit",
"forge-temporary",
"remote-conflict",
"destructive-refusal",
"state-drift",
"unsupported-schema",
"journal-unavailable",
"subprocess-spawn",
"subprocess-failed",
"io",
"internal",
"config-invalid",
]
);
}
#[test]
fn the_serde_rendering_and_the_wire_form_agree() {
for reason in REASONS {
let json = serde_json::to_string(&reason).expect("a reason serializes");
assert_eq!(json, format!("\"{}\"", reason.as_str()));
}
}
/// The `rk.diagnostic/1` schema, held by snapshot: a field rename or
/// removal fails here and becomes a schema-version bump instead of a
/// silent parser break at some agent.
#[test]
fn the_diagnostic_schema_snapshot_holds() {
let full = Diagnostic::new(Reason::StateDrift, "what happened")
.expected("what would have had to be true")
.action("the command that fixes it")
.target_state("what the run left behind");
assert_eq!(
serde_json::to_string(&full).expect("a diagnostic serializes"),
r#"{"schema":"rk.diagnostic/1","reason":"state-drift","message":"what happened","expected":"what would have had to be true","action":"the command that fixes it","target_state":"what the run left behind"}"#
);
let bare = Diagnostic::new(Reason::Io, "disk fell over");
assert_eq!(
serde_json::to_string(&bare).expect("a diagnostic serializes"),
r#"{"schema":"rk.diagnostic/1","reason":"io","message":"disk fell over"}"#,
"an unknown hint must be omitted, not serialized as null"
);
}
#[test]
fn the_human_rendering_answers_only_what_is_known() {
let bare = Diagnostic::new(Reason::Io, "disk fell over");
assert_eq!(bare.render_human(), "error: disk fell over");
let full = Diagnostic::new(Reason::StateDrift, "the target drifted")
.expected("a clean target")
.action("rk init --apply")
.target_state("nothing was written");
assert_eq!(
full.render_human(),
"error: the target drifted\n expected a clean target\n next rk init --apply\n state nothing was written"
);
}
}