Skip to main content

logcompact_core/
model.rs

1use serde::{Deserialize, Serialize};
2
3/// Severity assigned by a parser or structured-input adapter.
4#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
5#[serde(rename_all = "snake_case")]
6pub enum Severity {
7    Error,
8    Warning,
9    Note,
10}
11
12/// Provider-neutral diagnostic family used by presentation adapters.
13#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
14#[non_exhaustive]
15#[serde(rename_all = "snake_case")]
16pub enum DiagnosticClass {
17    Compiler,
18    Test,
19    Lint,
20    Tool,
21    Infrastructure,
22}
23
24/// How directly a finding is supported by the observed evidence.
25#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
26#[serde(rename_all = "snake_case")]
27pub enum EvidenceQuality {
28    Located,
29    #[default]
30    Structured,
31    Summary,
32    Fallback,
33}
34
35/// Optional source coordinate extracted from diagnostic text.
36#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
37pub struct Location {
38    pub path: String,
39    pub line: Option<u32>,
40    pub column: Option<u32>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub end_line: Option<u32>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub end_column: Option<u32>,
45}
46
47/// Input channel within a scope.
48#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
49#[serde(rename_all = "snake_case")]
50pub enum Stream {
51    Stdout,
52    Stderr,
53    #[default]
54    Combined,
55    Annotation,
56    Structured,
57}
58
59/// Provider-neutral unit that owns a related log stream.
60#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
61#[serde(rename_all = "snake_case")]
62pub enum ScopeKind {
63    Invocation,
64    Job,
65    Step,
66    Command,
67    Test,
68    Attempt,
69    #[default]
70    Other,
71}
72
73/// Caller-owned identity for one independently bounded parser scope.
74#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
75pub struct Scope {
76    pub id: String,
77    pub kind: ScopeKind,
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub label: Option<String>,
80}
81
82impl Scope {
83    #[must_use]
84    pub fn new(id: impl Into<String>, kind: ScopeKind) -> Self {
85        Self {
86            id: id.into(),
87            kind,
88            label: None,
89        }
90    }
91
92    #[must_use]
93    pub fn with_label(mut self, label: impl Into<String>) -> Self {
94        self.label = Some(label.into());
95        self
96    }
97
98    #[must_use]
99    pub fn step(id: impl Into<String>) -> Self {
100        Self::new(id, ScopeKind::Step)
101    }
102}
103
104/// Why a parser scope ended.
105#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
106#[serde(rename_all = "snake_case")]
107pub enum EndReason {
108    Complete,
109    Truncated,
110    Cancelled,
111    Interrupted,
112}
113
114/// Caller and state-machine provenance for one returned finding.
115#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
116pub struct Provenance {
117    pub source: String,
118    #[serde(skip_serializing_if = "Option::is_none")]
119    pub label: Option<String>,
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub scope_id: Option<String>,
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub scope_kind: Option<ScopeKind>,
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub stream: Option<Stream>,
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub start_line: Option<u64>,
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub end_line: Option<u64>,
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub parser: Option<String>,
132}
133
134impl Provenance {
135    #[must_use]
136    pub fn new(source: impl Into<String>) -> Self {
137        Self {
138            source: source.into(),
139            label: None,
140            scope_id: None,
141            scope_kind: None,
142            stream: None,
143            start_line: None,
144            end_line: None,
145            parser: None,
146        }
147    }
148
149    #[must_use]
150    pub fn with_label(mut self, label: impl Into<String>) -> Self {
151        self.label = Some(label.into());
152        self
153    }
154
155    #[must_use]
156    pub fn with_scope(mut self, scope: &Scope) -> Self {
157        self.scope_id = Some(scope.id.clone());
158        self.scope_kind = Some(scope.kind);
159        if self.label.is_none() {
160            self.label = scope.label.clone();
161        }
162        self
163    }
164
165    #[must_use]
166    pub fn with_span(mut self, stream: Stream, start_line: u64, end_line: u64) -> Self {
167        self.stream = Some(stream);
168        self.start_line = Some(start_line);
169        self.end_line = Some(end_line.max(start_line));
170        self
171    }
172
173    #[must_use]
174    pub fn with_parser(mut self, parser: impl Into<String>) -> Self {
175        self.parser = Some(parser.into());
176        self
177    }
178}
179
180/// One normalized and redacted diagnostic.
181#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
182pub struct Diagnostic {
183    pub severity: Severity,
184    pub class: DiagnosticClass,
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub code: Option<String>,
187    pub message: String,
188    #[serde(skip_serializing_if = "Option::is_none")]
189    pub location: Option<Location>,
190    #[serde(skip_serializing_if = "Option::is_none")]
191    pub provenance: Option<Provenance>,
192    #[serde(default, skip_serializing_if = "is_structured_quality")]
193    pub quality: EvidenceQuality,
194    pub repetition_count: u32,
195}
196
197fn is_structured_quality(value: &EvidenceQuality) -> bool {
198    *value == EvidenceQuality::Structured
199}
200
201/// Structured failed-test evidence independent of a particular test provider.
202#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
203pub struct TestFailure {
204    pub name: String,
205    pub message: String,
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub framework: Option<String>,
208    #[serde(skip_serializing_if = "Option::is_none")]
209    pub location: Option<Location>,
210    #[serde(skip_serializing_if = "Option::is_none")]
211    pub provenance: Option<Provenance>,
212}
213
214/// Combined item and serialized-finding byte limits.
215#[derive(Clone, Copy, Debug, Eq, PartialEq)]
216pub struct Budget {
217    pub max_bytes: usize,
218    pub max_items: usize,
219}
220
221impl Budget {
222    #[must_use]
223    pub const fn unbounded() -> Self {
224        Self {
225            max_bytes: usize::MAX,
226            max_items: usize::MAX,
227        }
228    }
229}
230
231/// Controls whether unclaimed actionable lines may become tool diagnostics.
232#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
233pub enum FallbackPolicy {
234    Disabled,
235    #[default]
236    Generic,
237}
238
239/// Stable options for one synchronous batch reduction call.
240#[derive(Clone, Copy, Debug, Eq, PartialEq)]
241pub struct ReductionOptions {
242    pub budget: Budget,
243    pub fallback: FallbackPolicy,
244}
245
246impl Default for ReductionOptions {
247    fn default() -> Self {
248        Self {
249            budget: Budget {
250                max_bytes: 4 * 1024,
251                max_items: 20,
252            },
253            fallback: FallbackPolicy::Generic,
254        }
255    }
256}
257
258/// Hard bounds for incremental input and candidate retention.
259#[derive(Clone, Copy, Debug, Eq, PartialEq)]
260pub struct Limits {
261    pub max_scopes: usize,
262    pub max_scope_bytes: usize,
263    pub max_line_bytes: usize,
264    pub max_candidates: usize,
265}
266
267impl Default for Limits {
268    fn default() -> Self {
269        Self {
270            max_scopes: 128,
271            max_scope_bytes: 1024 * 1024,
272            max_line_bytes: 64 * 1024,
273            max_candidates: 10_000,
274        }
275    }
276}
277
278/// Options for an incremental reduction session.
279#[derive(Clone, Copy, Debug, Eq, PartialEq)]
280pub struct SessionOptions {
281    pub budget: Budget,
282    pub limits: Limits,
283}
284
285impl Default for SessionOptions {
286    fn default() -> Self {
287        Self {
288            budget: ReductionOptions::default().budget,
289            limits: Limits::default(),
290        }
291    }
292}
293
294/// Borrowed text plus optional owned output provenance.
295#[derive(Clone, Copy, Debug)]
296pub struct TextInput<'a> {
297    pub text: &'a [u8],
298    pub provenance: Option<&'a Provenance>,
299}
300
301impl<'a> TextInput<'a> {
302    #[must_use]
303    pub const fn new(text: &'a [u8]) -> Self {
304        Self {
305            text,
306            provenance: None,
307        }
308    }
309
310    #[must_use]
311    pub const fn with_provenance(mut self, provenance: &'a Provenance) -> Self {
312        self.provenance = Some(provenance);
313        self
314    }
315}
316
317/// One normalized input line delivered to an immutable parser plan.
318#[derive(Clone, Copy, Debug)]
319pub struct LogLine<'a> {
320    pub scope: &'a Scope,
321    pub stream: Stream,
322    pub ordinal: u64,
323    pub stream_line: u64,
324    pub text: &'a str,
325    pub truncated: bool,
326}
327
328/// Accounting that never contains raw input text.
329#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
330pub struct ReductionStats {
331    pub bytes_seen: u64,
332    pub bytes_retained: u64,
333    pub lines_seen: u64,
334    pub truncated_lines: u64,
335    pub scopes_started: u64,
336    pub scopes_completed: u64,
337    pub candidates_emitted: u64,
338    pub candidates_dropped: u64,
339    pub omitted_test_failures: usize,
340}
341
342/// Bounded result and explicit truncation accounting.
343#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
344pub struct Reduction {
345    pub diagnostics: Vec<Diagnostic>,
346    #[serde(default, skip_serializing_if = "Vec::is_empty")]
347    pub test_failures: Vec<TestFailure>,
348    pub truncated: bool,
349    pub omitted_diagnostics: usize,
350    pub used_bytes: usize,
351    #[serde(default, skip_serializing_if = "is_default_stats")]
352    pub stats: ReductionStats,
353}
354
355fn is_default_stats(value: &ReductionStats) -> bool {
356    value == &ReductionStats::default()
357}