1mod hunks;
20mod note;
21
22use std::collections::BTreeMap;
23
24use serde::{Deserialize, Serialize};
25
26use crate::engine::UntrustedKind;
27
28pub use hunks::added_lines;
29pub use note::{AuditHead, Discrepancy, EventHash, NOTE_FORMAT, Note, NoteFormat, compare};
30
31pub const CLOCK_SLACK_MS: i64 = 2_000;
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
37#[serde(deny_unknown_fields)]
38pub struct LineRange {
39 pub start: u32,
41 pub end: u32,
43}
44
45impl LineRange {
46 #[must_use]
48 pub fn len(self) -> u32 {
49 self.end.saturating_sub(self.start).saturating_add(1)
50 }
51
52 #[must_use]
54 pub fn is_empty(self) -> bool {
55 false
56 }
57}
58
59#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61#[serde(deny_unknown_fields)]
62pub struct Untrusted {
63 pub kind: UntrustedKind,
65 pub source: String,
67}
68
69#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct AgentWrite {
72 pub path: String,
74 pub host: String,
76 pub session: String,
78 pub event: i64,
80 pub ts_ms: i64,
82 pub sha256: Option<String>,
84 pub before: Option<String>,
86 pub untrusted: Option<Untrusted>,
88}
89
90#[derive(Debug, Clone, PartialEq, Eq, Default)]
92pub struct ChangedFile {
93 pub path: String,
95 pub before: Option<String>,
98 pub after: Option<String>,
100 pub added: Vec<LineRange>,
102}
103
104#[derive(Debug, Clone, Copy)]
106pub struct Commit<'a> {
107 pub parent_ms: Option<i64>,
109 pub commit_ms: i64,
111 pub files: &'a [ChangedFile],
113}
114
115#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
117#[serde(rename_all = "kebab-case")]
118pub enum Evidence {
119 Lines,
121 Content,
123 Touched,
125}
126
127impl Evidence {
128 #[must_use]
130 pub fn as_str(self) -> &'static str {
131 match self {
132 Self::Lines => "lines",
133 Self::Content => "content",
134 Self::Touched => "touched",
135 }
136 }
137}
138
139#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
141#[serde(deny_unknown_fields)]
142pub struct FileRecord {
143 pub path: String,
145 pub host: String,
147 pub session: String,
149 pub event: i64,
151 pub ts_ms: i64,
153 pub evidence: Evidence,
155 #[serde(default, skip_serializing_if = "Vec::is_empty")]
158 pub lines: Vec<LineRange>,
159 #[serde(default, skip_serializing_if = "Option::is_none")]
161 pub untrusted: Option<Untrusted>,
162}
163
164impl FileRecord {
165 #[must_use]
167 pub fn line_count(&self) -> u64 {
168 self.lines.iter().map(|r| u64::from(r.len())).sum()
169 }
170}
171
172#[must_use]
176pub fn attribute(commit: &Commit<'_>, writes: &[AgentWrite]) -> Vec<FileRecord> {
177 let until = commit.commit_ms.saturating_add(CLOCK_SLACK_MS);
178 let mut out = Vec::new();
179 for file in commit.files {
180 let mut newest: BTreeMap<(&str, &str), &AgentWrite> = BTreeMap::new();
182 for write in writes
183 .iter()
184 .filter(|w| w.path == file.path && w.ts_ms <= until)
185 {
186 let slot = newest
187 .entry((write.host.as_str(), write.session.as_str()))
188 .or_insert(write);
189 if write.event > slot.event {
190 *slot = write;
191 }
192 }
193 for write in newest.into_values() {
194 if let Some(evidence) = evidence(commit, file, write) {
195 out.push(FileRecord {
196 path: file.path.clone(),
197 host: write.host.clone(),
198 session: write.session.clone(),
199 event: write.event,
200 ts_ms: write.ts_ms,
201 evidence,
202 lines: if evidence == Evidence::Lines {
203 file.added.clone()
204 } else {
205 Vec::new()
206 },
207 untrusted: write.untrusted.clone(),
208 });
209 }
210 }
211 }
212 out.sort_by(|a, b| (&a.path, a.event).cmp(&(&b.path, b.event)));
213 out
214}
215
216fn evidence(commit: &Commit<'_>, file: &ChangedFile, write: &AgentWrite) -> Option<Evidence> {
217 let after = file.after.as_deref().filter(|h| !h.is_empty());
218 if let (Some(after), Some(wrote)) = (after, write.sha256.as_deref())
219 && after == wrote
220 {
221 let whole = file.before.is_some() && file.before == write.before;
222 return Some(if whole {
223 Evidence::Lines
224 } else {
225 Evidence::Content
226 });
227 }
228 let after_parent = commit.parent_ms.is_none_or(|parent| write.ts_ms > parent);
229 after_parent.then_some(Evidence::Touched)
230}
231
232#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
234pub struct SessionSummary {
235 pub host: String,
237 pub session: String,
239 pub files: usize,
241 pub lines: u64,
243 #[serde(skip_serializing_if = "Option::is_none")]
246 pub untrusted: Option<Untrusted>,
247 pub commits: usize,
249}
250
251#[must_use]
253pub fn summarise(notes: &[Note]) -> Vec<SessionSummary> {
254 type Acc<'a> = (
255 std::collections::BTreeSet<&'a str>,
256 u64,
257 Option<(i64, &'a Untrusted)>,
258 std::collections::BTreeSet<&'a str>,
259 );
260 let mut by: BTreeMap<(&str, &str), Acc<'_>> = BTreeMap::new();
261 for note in notes {
262 for file in ¬e.files {
263 let acc = by
264 .entry((file.host.as_str(), file.session.as_str()))
265 .or_default();
266 acc.0.insert(file.path.as_str());
267 acc.1 = acc.1.saturating_add(file.line_count());
268 if let Some(untrusted) = &file.untrusted
269 && acc.2.is_none_or(|(event, _)| file.event < event)
270 {
271 acc.2 = Some((file.event, untrusted));
272 }
273 acc.3.insert(note.commit.as_str());
274 }
275 }
276 by.into_iter()
277 .map(
278 |((host, session), (files, lines, untrusted, commits))| SessionSummary {
279 host: host.to_owned(),
280 session: session.to_owned(),
281 files: files.len(),
282 lines,
283 untrusted: untrusted.map(|(_, u)| u.clone()),
284 commits: commits.len(),
285 },
286 )
287 .collect()
288}
289
290#[cfg(test)]
291mod tests;