selfware 0.6.1

Your personal AI workshop — software you own, software that lasts
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
//! Merge-readiness and deterministic multi-hop recommendations.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::Path;

use super::diagnostics::{AnalysisKind, AnalysisReport, DiagnosticsEngine};
use super::git::{GitEngine, GitStatusReport};
use super::ontology::ValidationReport;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GateState {
    Pass,
    Fail,
    Unknown,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GateEvidence {
    pub source: String,
    pub detail: String,
    pub path: Option<String>,
    pub line: Option<usize>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReadinessGate {
    pub id: String,
    pub label: String,
    pub state: GateState,
    pub summary: String,
    pub evidence: Vec<GateEvidence>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecommendationHop {
    pub order: usize,
    pub action: String,
    pub target: String,
    pub verification: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeterministicRecommendation {
    pub id: String,
    pub severity: String,
    pub title: String,
    pub rationale: String,
    pub evidence: Vec<GateEvidence>,
    pub evidence_complete: bool,
    pub hops: Vec<RecommendationHop>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReadinessReport {
    pub ready: bool,
    pub status: String,
    pub gates: Vec<ReadinessGate>,
    pub recommendations: Vec<DeterministicRecommendation>,
    pub git: GitStatusReport,
    pub analyses: Vec<AnalysisReport>,
}

#[derive(Debug, Clone)]
pub struct ReadinessEngine {
    diagnostics: DiagnosticsEngine,
    git: GitEngine,
}

impl ReadinessEngine {
    pub fn new(project_root: impl AsRef<Path>) -> Self {
        let root = project_root.as_ref();
        Self {
            diagnostics: DiagnosticsEngine::new(root),
            git: GitEngine::new(root),
        }
    }

    /// Run a single analysis kind with a timeout. On timeout, returns a report
    /// with `success = false` and a descriptive message instead of hanging.
    async fn run_with_timeout(
        &self,
        kind: AnalysisKind,
        timeout: std::time::Duration,
    ) -> AnalysisReport {
        match tokio::time::timeout(timeout, self.diagnostics.run(kind)).await {
            Ok(Ok(report)) => report,
            Ok(Err(error)) => AnalysisReport {
                kind,
                label: kind.label().to_string(),
                command: std::iter::once("cargo".to_string())
                    .chain(kind.args().iter().map(|arg| (*arg).to_string()))
                    .collect(),
                success: false,
                exit_code: None,
                duration_ms: timeout.as_millis() as u64,
                diagnostics: Vec::new(),
                errors: 0,
                warnings: 0,
                stdout_tail: String::new(),
                stderr_tail: format!("analysis failed: {error}"),
                evidence_complete: false,
            },
            Err(_) => AnalysisReport {
                kind,
                label: kind.label().to_string(),
                command: std::iter::once("cargo".to_string())
                    .chain(kind.args().iter().map(|arg| (*arg).to_string()))
                    .collect(),
                success: false,
                exit_code: None,
                duration_ms: timeout.as_millis() as u64,
                diagnostics: Vec::new(),
                errors: 0,
                warnings: 0,
                stdout_tail: String::new(),
                stderr_tail: format!("analysis timed out after {}s", timeout.as_secs()),
                evidence_complete: false,
            },
        }
    }

    pub async fn evaluate(&self, graph: &ValidationReport) -> Result<ReadinessReport> {
        let git = self.git.status()?;

        // Run the three cargo analyses concurrently instead of sequentially so
        // the total wall-clock time is ~max(check, clippy, tests) instead of
        // check + clippy + tests. Each command is individually bounded by a
        // timeout so a stuck build cannot block the HTTP request indefinitely.
        const ANALYSIS_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(120);

        let (check, clippy, tests) = tokio::join!(
            self.run_with_timeout(AnalysisKind::Check, ANALYSIS_TIMEOUT),
            self.run_with_timeout(AnalysisKind::Clippy, ANALYSIS_TIMEOUT),
            self.run_with_timeout(AnalysisKind::EvolveTests, ANALYSIS_TIMEOUT),
        );

        let analyses = vec![check, clippy, tests];

        let mut gates = Vec::new();
        for analysis in &analyses {
            gates.push(ReadinessGate {
                id: format!("analysis:{:?}", analysis.kind).to_lowercase(),
                label: analysis.label.clone(),
                state: if analysis.success {
                    GateState::Pass
                } else {
                    GateState::Fail
                },
                summary: format!(
                    "{} error(s), {} warning(s), {} ms",
                    analysis.errors, analysis.warnings, analysis.duration_ms
                ),
                evidence: vec![GateEvidence {
                    source: "command".to_string(),
                    detail: analysis.command.join(" "),
                    path: None,
                    line: None,
                }],
            });
        }
        gates.push(ReadinessGate {
            id: "graph_integrity".to_string(),
            label: "Graph integrity".to_string(),
            state: if graph.valid {
                GateState::Pass
            } else {
                GateState::Fail
            },
            summary: if graph.valid {
                "No blocking structural defects".to_string()
            } else {
                format!(
                    "{} cycle(s), {} dangling edge(s), {} isolated node(s)",
                    graph.cycles.len(),
                    graph.dangling_edges.len(),
                    graph.isolated_nodes.len()
                )
            },
            evidence: vec![GateEvidence {
                source: "graph_validator".to_string(),
                detail: "GET /api/ontology/validate".to_string(),
                path: Some(".selfware/evolve-graph.yaml".to_string()),
                line: None,
            }],
        });
        gates.push(ReadinessGate {
            id: "git_clean".to_string(),
            label: "Reviewable working tree".to_string(),
            state: if git.dirty {
                GateState::Fail
            } else {
                GateState::Pass
            },
            summary: if git.dirty {
                format!("{} uncommitted path(s)", git.files.len())
            } else {
                "Working tree is clean".to_string()
            },
            evidence: vec![GateEvidence {
                source: "git".to_string(),
                detail: format!("HEAD {}", git.head),
                path: None,
                line: None,
            }],
        });
        gates.push(ReadinessGate {
            id: "coverage".to_string(),
            label: "Coverage delta".to_string(),
            state: GateState::Unknown,
            summary: "No fresh coverage run is attached to this snapshot".to_string(),
            evidence: Vec::new(),
        });
        gates.push(ReadinessGate {
            id: "hotpath_profile".to_string(),
            label: "Hot-path profile".to_string(),
            state: GateState::Unknown,
            summary: "No benchmark or profiler artifact is attached to this snapshot".to_string(),
            evidence: Vec::new(),
        });

        let recommendations = deterministic_recommendations(&analyses, &git, graph);
        let ready = gates.iter().all(|gate| gate.state == GateState::Pass);
        let status = if ready {
            "ready"
        } else if gates.iter().any(|gate| gate.state == GateState::Fail) {
            "blocked"
        } else {
            "incomplete"
        }
        .to_string();

        Ok(ReadinessReport {
            ready,
            status,
            gates,
            recommendations,
            git,
            analyses,
        })
    }
}

fn deterministic_recommendations(
    analyses: &[AnalysisReport],
    git: &GitStatusReport,
    graph: &ValidationReport,
) -> Vec<DeterministicRecommendation> {
    let mut recommendations = Vec::new();
    let mut index = 1usize;

    for report in analyses {
        for diagnostic in report.diagnostics.iter().take(40) {
            let primary = diagnostic.spans.iter().find(|span| span.is_primary);
            let target = primary
                .map(|span| format!("{}:{}", span.file, span.line_start))
                .unwrap_or_else(|| "workspace".to_string());
            let dead_code = diagnostic.code.as_deref() == Some("dead_code")
                || diagnostic.message.contains("never used")
                || diagnostic.message.contains("unused");
            let evidence = vec![GateEvidence {
                source: report.command.join(" "),
                detail: diagnostic.message.clone(),
                path: primary.map(|span| span.file.clone()),
                line: primary.map(|span| span.line_start),
            }];
            let hops = if dead_code {
                vec![
                    RecommendationHop {
                        order: 1,
                        action: "Inspect declaration and inbound references".to_string(),
                        target: target.clone(),
                        verification: "Graph and AST references are enumerated".to_string(),
                    },
                    RecommendationHop {
                        order: 2,
                        action: "Run focused tests for the owning component".to_string(),
                        target: target.clone(),
                        verification: "Focused tests pass before any source deletion is staged"
                            .to_string(),
                    },
                    RecommendationHop {
                        order: 3,
                        action: "Stage deletion preview".to_string(),
                        target: target.clone(),
                        verification:
                            "Exact content hash, dependents, diff, and rollback checkpoint exist"
                                .to_string(),
                    },
                ]
            } else {
                vec![
                    RecommendationHop {
                        order: 1,
                        action: "Open grounded diagnostic span".to_string(),
                        target: target.clone(),
                        verification: "Source hash still matches the diagnostic snapshot"
                            .to_string(),
                    },
                    RecommendationHop {
                        order: 2,
                        action: "Apply a scoped edit".to_string(),
                        target: target.clone(),
                        verification: report.command.join(" "),
                    },
                    RecommendationHop {
                        order: 3,
                        action: "Run evolve tests".to_string(),
                        target: "tests/evolve".to_string(),
                        verification: "cargo test --test evolve".to_string(),
                    },
                ]
            };
            recommendations.push(DeterministicRecommendation {
                id: format!("R{index}"),
                severity: diagnostic.level.clone(),
                title: if dead_code {
                    format!("Investigate possible dead code at {target}")
                } else {
                    format!("Resolve {} diagnostic at {target}", diagnostic.level)
                },
                rationale: diagnostic.message.clone(),
                evidence,
                evidence_complete: report.evidence_complete && primary.is_some(),
                hops,
            });
            index += 1;
        }
    }

    if git.dirty {
        recommendations.push(DeterministicRecommendation {
            id: format!("R{index}"),
            severity: "warning".to_string(),
            title: "Separate the working-tree overlay before branch creation".to_string(),
            rationale: format!("Git reports {} changed path(s)", git.files.len()),
            evidence: git
                .files
                .iter()
                .take(40)
                .map(|file| GateEvidence {
                    source: "git status".to_string(),
                    detail: format!("index={}, worktree={}", file.index, file.worktree),
                    path: Some(file.path.clone()),
                    line: None,
                })
                .collect(),
            evidence_complete: git.files.len() <= 40,
            hops: vec![
                RecommendationHop {
                    order: 1,
                    action: "Review exact working-tree diff".to_string(),
                    target: "repository".to_string(),
                    verification: "Every changed path has an owner and intent".to_string(),
                },
                RecommendationHop {
                    order: 2,
                    action: "Commit or preserve unrelated work".to_string(),
                    target: "repository".to_string(),
                    verification: "git status is clean".to_string(),
                },
                RecommendationHop {
                    order: 3,
                    action: "Create exact-head evolve branch".to_string(),
                    target: git.head.clone(),
                    verification: "Branch starts at the approved HEAD".to_string(),
                },
            ],
        });
        index += 1;
    }

    if !graph.valid {
        recommendations.push(DeterministicRecommendation {
            id: format!("R{index}"),
            severity: "error".to_string(),
            title: "Repair graph integrity before mutation actions".to_string(),
            rationale: "The graph validator reports incomplete or contradictory structure"
                .to_string(),
            evidence: vec![GateEvidence {
                source: "graph_validator".to_string(),
                detail: format!(
                    "cycles={}, dangling={}, isolated={}",
                    graph.cycles.len(),
                    graph.dangling_edges.len(),
                    graph.isolated_nodes.len()
                ),
                path: Some(".selfware/evolve-graph.yaml".to_string()),
                line: None,
            }],
            evidence_complete: true,
            hops: vec![
                RecommendationHop {
                    order: 1,
                    action: "Inspect invalid graph evidence".to_string(),
                    target: "ontology".to_string(),
                    verification: "Every reported endpoint maps to a current node version"
                        .to_string(),
                },
                RecommendationHop {
                    order: 2,
                    action: "Rebuild the derived graph".to_string(),
                    target: "workspace".to_string(),
                    verification: "Graph revision changes deterministically".to_string(),
                },
                RecommendationHop {
                    order: 3,
                    action: "Re-run structural validation".to_string(),
                    target: "ontology".to_string(),
                    verification: "No blocking graph defects remain".to_string(),
                },
            ],
        });
    }

    recommendations
}