glass_dev/development/
diff.rs1use super::{
2 DEVELOPMENT_SCHEMA_VERSION, DevelopmentError, DevelopmentGraph, DevelopmentResult,
3 ProcessManager, Timeline,
4};
5use serde::{Deserialize, Serialize};
6use std::{collections::BTreeMap, path::Path, process::Command};
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
9#[serde(rename_all = "camelCase")]
10pub struct FileChange {
11 pub path: String,
12 pub status: String,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
16#[serde(rename_all = "camelCase")]
17pub struct ProjectDiff {
18 pub schema_version: String,
19 pub files: Vec<FileChange>,
20 pub runtime: BTreeMap<String, serde_json::Value>,
21 pub semantic: BTreeMap<String, serde_json::Value>,
22 pub visual: BTreeMap<String, serde_json::Value>,
23 pub workflow: BTreeMap<String, serde_json::Value>,
24 pub test_impact: BTreeMap<String, serde_json::Value>,
25}
26
27pub fn build_diff(
28 root: &Path,
29 timeline: &Timeline,
30 graph: &DevelopmentGraph,
31 processes: &mut ProcessManager,
32) -> DevelopmentResult<ProjectDiff> {
33 let output = Command::new("git")
34 .args(["status", "--short", "--untracked-files=all"])
35 .current_dir(root)
36 .output()
37 .map_err(DevelopmentError::Io)?;
38 let mut files = Vec::new();
39 if output.status.success() {
40 for line in String::from_utf8_lossy(&output.stdout).lines().take(512) {
41 if line.len() < 4 {
42 continue;
43 }
44 let status = line[..2].trim().to_string();
45 let path = line[3..].trim().to_string();
46 if !path.is_empty() {
47 files.push(FileChange { path, status });
48 }
49 }
50 }
51 let process_snapshots = processes.list();
52 let mut runtime = BTreeMap::new();
53 runtime.insert(
54 "processCount".into(),
55 serde_json::json!(process_snapshots.len()),
56 );
57 runtime.insert("processes".into(), serde_json::to_value(process_snapshots)?);
58 let mut semantic = BTreeMap::new();
59 semantic.insert(
60 "linkCount".into(),
61 serde_json::json!(graph.links.values().map(Vec::len).sum::<usize>()),
62 );
63 semantic.insert(
64 "breakpointHits".into(),
65 serde_json::json!(
66 timeline
67 .events()
68 .filter(|event| matches!(
69 event.kind,
70 super::DevelopmentEventKind::SemanticBreakpointHit
71 ))
72 .count()
73 ),
74 );
75 let mut visual = BTreeMap::new();
76 visual.insert("status".into(), serde_json::json!("not-captured"));
77 visual.insert(
78 "reason".into(),
79 serde_json::json!(
80 "visual evidence requires explicit capture; screenshots are never implicit"
81 ),
82 );
83 semantic.insert(
84 "entities".into(),
85 serde_json::to_value(graph.links.keys().collect::<Vec<_>>())?,
86 );
87 let mut workflow = BTreeMap::new();
88 let verification_count = timeline
89 .events()
90 .filter(|event| {
91 matches!(
92 event.kind,
93 super::DevelopmentEventKind::VerificationCompleted
94 )
95 })
96 .count();
97 workflow.insert(
98 "verificationCount".into(),
99 serde_json::json!(verification_count),
100 );
101 workflow.insert(
102 "recentEventCount".into(),
103 serde_json::json!(timeline.events().count()),
104 );
105 let mut test_impact = BTreeMap::new();
106 test_impact.insert(
107 "started".into(),
108 serde_json::json!(
109 timeline
110 .events()
111 .filter(|event| matches!(event.kind, super::DevelopmentEventKind::TestStarted))
112 .count()
113 ),
114 );
115 test_impact.insert(
116 "completed".into(),
117 serde_json::json!(
118 timeline
119 .events()
120 .filter(|event| matches!(event.kind, super::DevelopmentEventKind::TestCompleted))
121 .count()
122 ),
123 );
124 Ok(ProjectDiff {
125 schema_version: DEVELOPMENT_SCHEMA_VERSION.into(),
126 files,
127 runtime,
128 semantic,
129 visual,
130 workflow,
131 test_impact,
132 })
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138 use crate::development::{Actor, DevelopmentEventKind, Timeline};
139 use std::fs;
140
141 #[test]
142 fn diff_is_bounded_and_reports_runtime_and_semantic_sections() {
143 let root = std::env::temp_dir().join(format!("glass-diff-{}", std::process::id()));
144 let _ = fs::create_dir_all(&root);
145 let mut timeline = Timeline::open(root.join("timeline.jsonl")).unwrap();
146 timeline
147 .record(
148 Actor::local(),
149 DevelopmentEventKind::VerificationCompleted,
150 "/tmp",
151 serde_json::json!({}),
152 )
153 .unwrap();
154 let mut manager = ProcessManager::new(&root);
155 let diff =
156 build_diff(&root, &timeline, &DevelopmentGraph::default(), &mut manager).unwrap();
157 assert_eq!(diff.schema_version, DEVELOPMENT_SCHEMA_VERSION);
158 assert!(diff.runtime.contains_key("processes"));
159 assert!(diff.semantic.contains_key("entities"));
160 assert_eq!(diff.visual["status"], "not-captured");
161 let _ = fs::remove_dir_all(root);
162 }
163}