shape-diagnostics 0.3.2

LLM-Structured Diagnostic Schema (LSDS) for the Shape compiler — primary diagnostic format per ADR-006 §9.
Documentation
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! LLM-Structured Diagnostic Schema (LSDS).
//!
//! Per ADR-006 §9, LSDS is the primary compiler diagnostic format. Renderers
//! (terminal, LSP, MCP) consume LSDS and produce human-readable / machine-
//! readable output. **LSDS is the source of truth** — text strings, LSP
//! `Diagnostic` payloads, and MCP tool responses are all derived from it.
//!
//! # Crate layout
//!
//! - [`Diagnostic`] — the canonical struct. JSON-serializable. Stable across
//!   versions per the ADR.
//! - [`Severity`], [`Location`], [`TypeWitness`], [`SuggestedFix`],
//!   [`ContextWindow`] — sub-structures referenced from `Diagnostic`.
//! - [`render`] — built-in renderers. Currently:
//!   - [`render::terminal`] — human-readable text output.
//!   LSP and MCP renderers are reserved for subsequent Phase 2 sessions.
//!
//! # Stability contract
//!
//! Field names in [`Diagnostic`] (and nested types) are part of the public
//! wire format. They must not be renamed or reordered without bumping the
//! schema version. Add new optional fields only; never remove or rename
//! existing ones.
//!
//! The schema version is exposed as [`SCHEMA_VERSION`].
//!
//! # Cross-references
//!
//! - ADR-006 §9 (`docs/adr/006-value-and-memory-model.md`) — binding spec.
//! - ADR-006 §13.5 success metric — average payload ≤500 cl100k tokens.
//! - `crates/shape-vm/src/mir/analysis.rs` — `BorrowError` /
//!   `BorrowErrorKind` / `BorrowErrorCode`, the source for the B-series
//!   diagnostics.

#![warn(missing_docs)]

use serde::{Deserialize, Serialize};

pub mod render;

/// Wire-format schema version. Bumped on breaking changes.
pub const SCHEMA_VERSION: u32 = 1;

/// Severity of a diagnostic.
///
/// Lower-cased in the wire format (`"error"`, `"warning"`, `"info"`,
/// `"hint"`). Renderers map these to terminal colours, LSP severities, and
/// MCP severity strings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    /// Compilation cannot proceed.
    Error,
    /// Compilation proceeds but the user should look.
    Warning,
    /// Informational — used for `var` inference inlay-hint suggestions
    /// (ADR-006 §1.3) and similar non-actionable feedback.
    Info,
    /// Hint — soft suggestions, e.g. style nits or refactor proposals
    /// surfaced by tooling consumers.
    Hint,
}

/// Source location of a diagnostic — a 1-based line/column plus an
/// absolute byte span.
///
/// `file` is the canonical path string; absent for synthetic / REPL
/// diagnostics.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Location {
    /// File path; absent for synthetic / REPL / in-memory sources.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub file: Option<String>,
    /// 1-based line number.
    pub line: u32,
    /// 1-based column number.
    pub col: u32,
    /// Absolute byte span `[start, end)` into the source buffer.
    pub span: [u32; 2],
}

impl Location {
    /// Construct a `Location` with a 1-based line/column and an absolute
    /// byte span.
    pub fn new(file: Option<String>, line: u32, col: u32, span_start: u32, span_end: u32) -> Self {
        Self {
            file,
            line,
            col,
            span: [span_start, span_end],
        }
    }

    /// Synthetic location with no file and zero positions — used for
    /// diagnostics not anchored to source (e.g., compiler-internal
    /// configuration errors).
    pub fn synthetic() -> Self {
        Self::new(None, 0, 0, 0, 0)
    }
}

/// A type witness — a concrete value that satisfies (`expected`) or
/// violates (`found`) the type constraint at the diagnostic site, per
/// ADR-006 §9.3.
///
/// `r#type` is the type's surface name (e.g. `"int"`, `"string"`,
/// `"Array<int>"`). `witness` is an optional concrete example value.
///
/// For simple primitive types (`int`, `number`, `bool`, `string`), the
/// emitter is encouraged to populate `witness`. For recursive / generic /
/// trait-bounded types, `witness` may be `None`; the surface name alone
/// communicates the constraint.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TypeWitness {
    /// Surface name of the type (`"int"`, `"Option<string>"`, ...).
    #[serde(rename = "type")]
    pub r#type: String,
    /// Optional concrete value satisfying or violating the constraint.
    /// Encoded as a JSON value; LLM consumers parse it directly.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub witness: Option<serde_json::Value>,
}

impl TypeWitness {
    /// Construct a witness from a type name and an optional JSON value.
    pub fn new(type_name: impl Into<String>, witness: Option<serde_json::Value>) -> Self {
        Self {
            r#type: type_name.into(),
            witness,
        }
    }

    /// Construct a witness with only a type name and no concrete value.
    pub fn type_only(type_name: impl Into<String>) -> Self {
        Self::new(type_name, None)
    }
}

/// A suggested fix — a ranked, optionally-diff-bearing proposal that a
/// renderer (LSP code action, MCP `apply_fix` tool call) can apply.
///
/// `confidence` is in `[0.0, 1.0]`. Phase-2 first-session emitters may
/// produce empty `fixes` lists; richer fix generation is later-session
/// scope per the dispatch.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SuggestedFix {
    /// Short user-facing label (e.g. `"convert string to int"`).
    pub label: String,
    /// Optional unified-diff fragment. Renderers that can apply diffs
    /// (LSP, MCP) consume this directly. May be empty.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub diff: Option<String>,
    /// Confidence in the fix, `0.0..=1.0`. Renderers may rank by this.
    pub confidence: f32,
}

impl SuggestedFix {
    /// Construct a suggestion with a label and a confidence.
    pub fn new(label: impl Into<String>, confidence: f32) -> Self {
        Self {
            label: label.into(),
            diff: None,
            confidence,
        }
    }

    /// Attach a unified-diff fragment.
    pub fn with_diff(mut self, diff: impl Into<String>) -> Self {
        self.diff = Some(diff.into());
        self
    }
}

/// A token-budgeted context window — the smallest set of source spans
/// needed to understand the diagnostic, with a token count.
///
/// Per ADR-006 §9.5. LLM consumers use this to bound the source they
/// must include alongside the diagnostic. `tokens` is an estimate against
/// the cl100k tokenizer (per ADR-006 §13.5 success metric).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContextWindow {
    /// Estimated token count for the included spans (cl100k).
    pub tokens: u32,
    /// Spans that comprise the context window.
    pub spans: Vec<ContextSpan>,
}

impl ContextWindow {
    /// Construct an empty context window with a token budget of zero.
    pub fn empty() -> Self {
        Self {
            tokens: 0,
            spans: Vec::new(),
        }
    }
}

/// A span of source — a file plus an inclusive line range.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContextSpan {
    /// File path; absent for synthetic / REPL.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub file: Option<String>,
    /// Inclusive 1-based line range `[start, end]`.
    pub lines: [u32; 2],
}

/// The canonical LSDS diagnostic.
///
/// JSON shape matches ADR-006 §9.2. Field names are part of the public
/// wire format; see crate-level docs for the stability contract.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Diagnostic {
    /// Stable diagnostic identifier — e.g. `"B0013"`, `"E0100"`. The
    /// scheme matches the existing `BorrowErrorCode` (`B`-series for
    /// borrow / lifetime / aliasing) and `ErrorCode` (`E`-series for
    /// type, parse, semantic) namespaces.
    pub diagnostic_id: String,
    /// Severity bucket.
    pub severity: Severity,
    /// Primary source location.
    pub location: Location,
    /// Expected type at this site, when applicable. `None` for
    /// non-type-related diagnostics (e.g. parse errors).
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub expected: Option<TypeWitness>,
    /// Found type at this site, when applicable.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub found: Option<TypeWitness>,
    /// Human-readable message body (does NOT include the
    /// `[B00XX]` prefix — that's the `diagnostic_id` field's job;
    /// renderers prepend it on output).
    pub message: String,
    /// Ranked suggested fixes; may be empty.
    #[serde(default)]
    pub fixes: Vec<SuggestedFix>,
    /// Token-budgeted context window for LLM consumers.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub context_window: Option<ContextWindow>,
    /// Citation pointing at the binding spec section that governs this
    /// diagnostic. E.g. `"ADR-006-§1.1"` or `"ADR-005-§4"`.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub rule: Option<String>,
    /// Auxiliary notes. Each note has its own location (e.g. "borrow
    /// originates here") so renderers can present them as related-info
    /// callouts.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub notes: Vec<DiagnosticNote>,
}

/// Auxiliary note attached to a diagnostic — e.g. "borrow originates
/// here", "binding declared here". Mirrors the existing `ErrorNote`
/// structure used by `ShapeError::SemanticError.location.notes`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DiagnosticNote {
    /// Note message.
    pub message: String,
    /// Location the note refers to; `None` for synthetic notes.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub location: Option<Location>,
}

impl DiagnosticNote {
    /// Construct a note with a message and an optional location.
    pub fn new(message: impl Into<String>, location: Option<Location>) -> Self {
        Self {
            message: message.into(),
            location,
        }
    }
}

/// Builder for [`Diagnostic`]. Use this rather than struct literal at
/// emission sites so future schema evolution doesn't ripple through.
#[derive(Debug)]
pub struct DiagnosticBuilder {
    diagnostic_id: String,
    severity: Severity,
    location: Location,
    expected: Option<TypeWitness>,
    found: Option<TypeWitness>,
    message: String,
    fixes: Vec<SuggestedFix>,
    context_window: Option<ContextWindow>,
    rule: Option<String>,
    notes: Vec<DiagnosticNote>,
}

impl DiagnosticBuilder {
    /// Start building a diagnostic with the required minimum (id,
    /// severity, location, message).
    pub fn new(
        diagnostic_id: impl Into<String>,
        severity: Severity,
        location: Location,
        message: impl Into<String>,
    ) -> Self {
        Self {
            diagnostic_id: diagnostic_id.into(),
            severity,
            location,
            expected: None,
            found: None,
            message: message.into(),
            fixes: Vec::new(),
            context_window: None,
            rule: None,
            notes: Vec::new(),
        }
    }

    /// Attach an `expected` type witness.
    pub fn expected(mut self, witness: TypeWitness) -> Self {
        self.expected = Some(witness);
        self
    }

    /// Attach a `found` type witness.
    pub fn found(mut self, witness: TypeWitness) -> Self {
        self.found = Some(witness);
        self
    }

    /// Append a suggested fix.
    pub fn with_fix(mut self, fix: SuggestedFix) -> Self {
        self.fixes.push(fix);
        self
    }

    /// Attach a context window.
    pub fn context_window(mut self, window: ContextWindow) -> Self {
        self.context_window = Some(window);
        self
    }

    /// Attach a rule citation (`"ADR-006-§1.1"` etc.).
    pub fn rule(mut self, rule: impl Into<String>) -> Self {
        self.rule = Some(rule.into());
        self
    }

    /// Append an auxiliary note.
    pub fn with_note(mut self, note: DiagnosticNote) -> Self {
        self.notes.push(note);
        self
    }

    /// Finalize.
    pub fn build(self) -> Diagnostic {
        Diagnostic {
            diagnostic_id: self.diagnostic_id,
            severity: self.severity,
            location: self.location,
            expected: self.expected,
            found: self.found,
            message: self.message,
            fixes: self.fixes,
            context_window: self.context_window,
            rule: self.rule,
            notes: self.notes,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn schema_version_is_one() {
        assert_eq!(SCHEMA_VERSION, 1);
    }

    #[test]
    fn diagnostic_round_trips_through_json() {
        let diag = DiagnosticBuilder::new(
            "B0013",
            Severity::Error,
            Location::new(Some("src/main.shape".into()), 12, 4, 102, 145),
            "expected int, found string",
        )
        .expected(TypeWitness::new("int", Some(json!(42))))
        .found(TypeWitness::new("string", Some(json!("hello"))))
        .with_fix(
            SuggestedFix::new("convert string to int", 0.85)
                .with_diff("let x: int = parse_int(value)?"),
        )
        .rule("ADR-006-§1.1")
        .build();

        let s = serde_json::to_string(&diag).expect("serialize");
        let back: Diagnostic = serde_json::from_str(&s).expect("deserialize");
        assert_eq!(diag, back);
    }

    #[test]
    fn omitted_optional_fields_round_trip() {
        let diag = DiagnosticBuilder::new(
            "E0100",
            Severity::Error,
            Location::synthetic(),
            "type mismatch",
        )
        .build();

        let s = serde_json::to_string(&diag).expect("serialize");
        // No expected/found/fixes/context_window/rule/notes appear when empty.
        assert!(!s.contains("\"expected\""));
        assert!(!s.contains("\"found\""));
        // `fixes` is `default` (empty Vec) — not skipped, but encoded as `[]`.
        assert!(s.contains("\"fixes\":[]"));
        assert!(!s.contains("\"context_window\""));
        assert!(!s.contains("\"rule\""));
        assert!(!s.contains("\"notes\""));

        let back: Diagnostic = serde_json::from_str(&s).expect("deserialize");
        assert_eq!(diag, back);
    }

    #[test]
    fn severity_serializes_lowercase() {
        let s = serde_json::to_string(&Severity::Error).unwrap();
        assert_eq!(s, "\"error\"");
        let s = serde_json::to_string(&Severity::Warning).unwrap();
        assert_eq!(s, "\"warning\"");
        let s = serde_json::to_string(&Severity::Info).unwrap();
        assert_eq!(s, "\"info\"");
        let s = serde_json::to_string(&Severity::Hint).unwrap();
        assert_eq!(s, "\"hint\"");
    }
}