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
//! Typed readiness report emitted by `fallow doctor`.
use crate::root_envelopes::{RootEnvelopeMode, serialize_named_json_output};
use fallow_types::envelope::{SchemaVersion, ToolVersion};
use serde::Serialize;
/// Current schema version for `fallow doctor --format json`.
///
/// Bumped to 2 for the `dependencies`, `cache`, and `graph-cache` checks. All
/// three are appended after the existing five, so the previous order is
/// unchanged, but a consumer that enumerates `checks[]` sees three new `id`
/// values.
pub const DOCTOR_SCHEMA_VERSION: u32 = 2;
/// Schema projection for the exact doctor envelope version.
#[cfg(feature = "schema")]
#[allow(dead_code, reason = "schema-only type used by the field projection")]
#[derive(schemars::JsonSchema)]
#[schemars(extend("const" = DOCTOR_SCHEMA_VERSION))]
struct DoctorSchemaVersion(u32);
/// Schema projection for `.` as the privacy-safe diagnosed project root.
#[cfg(feature = "schema")]
#[allow(dead_code, reason = "schema-only type used by the field projection")]
#[derive(schemars::JsonSchema)]
#[schemars(extend("const" = "."))]
struct DoctorProjectRoot(String);
/// Aggregate readiness outcome.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum DoctorStatus {
/// Every applicable check passed.
Pass,
/// Readiness is usable, with one or more advisory warnings.
Warn,
/// At least one required readiness check failed.
Fail,
}
/// Per-check readiness outcome.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum DoctorCheckStatus {
/// The check completed successfully.
Pass,
/// The check completed with an advisory concern.
Warn,
/// The check found a blocking readiness problem.
Fail,
/// The check does not apply or an earlier prerequisite failed.
Skipped,
}
/// Stable identifier for a doctor check. Declaration order is output order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum DoctorCheckId {
/// Project root availability.
Root,
/// Fallow configuration resolution.
Config,
/// Workspace declaration discovery.
Workspaces,
/// External plugin configuration and activation.
Plugins,
/// Optional type-aware companion discovery.
TypeAware,
/// Installed dependency tree availability.
Dependencies,
/// Persisted extraction-cache reuse.
Cache,
/// Persisted module-graph reuse.
///
/// Separate from [`Cache`](Self::Cache) because a warm run reuses the two
/// blobs independently: the extraction cache can be perfectly reusable
/// while the graph is discarded on every run, and the graph is the larger
/// of the two on a real project.
GraphCache,
}
/// Stable category for a doctor check.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum DoctorCheckCategory {
/// Project filesystem readiness.
Project,
/// Fallow configuration readiness.
Configuration,
/// Workspace topology readiness.
Workspace,
/// Plugin readiness.
Plugin,
/// Optional companion readiness.
Companion,
/// Warm-run readiness of persisted analysis state.
Cache,
}
/// Actionable remediation attached to a doctor check.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct DoctorRemediation {
/// Command the user or agent can choose to run.
pub command: String,
/// Working directory for the command, relative to the diagnosed root.
#[cfg_attr(feature = "schema", schemars(with = "DoctorProjectRoot"))]
pub cwd: String,
/// Whether running the command can modify project files or dependencies.
pub mutating: bool,
}
/// One deterministic doctor check result.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct DoctorCheck {
/// Stable check identifier.
pub id: DoctorCheckId,
/// Readiness area this check covers.
pub category: DoctorCheckCategory,
/// Check outcome.
pub status: DoctorCheckStatus,
/// Whether failure makes the project not ready.
pub required: bool,
/// Human-readable result with no host-specific absolute paths.
pub message: String,
/// Optional actionable next command.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remediation: Option<DoctorRemediation>,
}
/// Counts for every per-check status.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct DoctorSummary {
/// Successful checks.
pub pass: usize,
/// Advisory checks.
pub warn: usize,
/// Failed checks.
pub fail: usize,
/// Inapplicable or prerequisite-blocked checks.
pub skipped: usize,
}
/// Versioned readiness envelope emitted by `fallow doctor --format json`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "schema", schemars(title = "fallow doctor --format json"))]
pub struct DoctorOutput {
/// Independent doctor contract version.
#[cfg_attr(feature = "schema", schemars(with = "DoctorSchemaVersion"))]
pub schema_version: SchemaVersion,
/// Fallow version that produced the report.
pub version: ToolVersion,
/// Stable project-root identifier. Always `.` to avoid exposing host paths.
#[cfg_attr(feature = "schema", schemars(with = "DoctorProjectRoot"))]
pub root: String,
/// Aggregate readiness outcome.
pub status: DoctorStatus,
/// Counts derived from `checks`.
pub summary: DoctorSummary,
/// Checks in stable contract order.
pub checks: Vec<DoctorCheck>,
}
/// Serialize a doctor report with its root discriminator.
///
/// # Errors
///
/// Returns a serde error if the typed report cannot be converted to JSON.
pub fn serialize_doctor_json_output(
output: DoctorOutput,
mode: RootEnvelopeMode,
) -> Result<serde_json::Value, serde_json::Error> {
serialize_named_json_output(output, "doctor", mode)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tagged_json_uses_the_stable_privacy_safe_contract() {
let value = serialize_doctor_json_output(
DoctorOutput {
schema_version: SchemaVersion(DOCTOR_SCHEMA_VERSION),
version: ToolVersion("1.2.3".to_string()),
root: ".".to_string(),
status: DoctorStatus::Warn,
summary: DoctorSummary {
pass: 4,
warn: 1,
fail: 0,
skipped: 0,
},
checks: vec![DoctorCheck {
id: DoctorCheckId::TypeAware,
category: DoctorCheckCategory::Companion,
status: DoctorCheckStatus::Warn,
required: false,
message: "Companion is unavailable.".to_string(),
remediation: Some(DoctorRemediation {
command: "npm install --save-dev fallow-type-aware@1.2.3".to_string(),
cwd: ".".to_string(),
mutating: true,
}),
}],
},
RootEnvelopeMode::Tagged,
)
.expect("serialize doctor output");
assert_eq!(value["kind"], "doctor");
assert_eq!(value["schema_version"], DOCTOR_SCHEMA_VERSION);
assert_eq!(value["root"], ".");
assert_eq!(value["checks"][0]["remediation"]["cwd"], ".");
assert_eq!(value["checks"][0]["remediation"]["mutating"], true);
assert!(
value["checks"][0]["remediation"]
.get("mutates_project")
.is_none()
);
}
}