ara-core 0.1.16

Shared parsing, normalization, and DAG layout for the ARA viewer runtime.
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Normalized wire types — the single manifest every downstream consumer reads.
//!
//! This is the logical graph produced by [`crate::parse`]. It is the *only*
//! public data model: no `serde-saphyr` types leak here (that stays confined to
//! [`crate::schema`] / [`crate::claims`]), which keeps a future YAML-backend
//! swap cheap. Layout/geometry is **not** part of Stage 1 — it lands in Stage 2.
//!
//! Ordering is significant and always mirrors the source: `nodes` are in
//! pre-order DFS of the tree, `links`/`bindings` follow per-node source order.
//! Nothing is ever sorted by id.

use serde::{Deserialize, Serialize};

use crate::layout::{Point, Rect};

/// A node identifier (`^N\d+$`, case-sensitive, trimmed).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct NodeId(String);

/// A claim identifier (`^C\d+$`, case-sensitive, trimmed).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ClaimId(String);

impl NodeId {
    /// Wraps an already-normalized id. Callers pass a trimmed string.
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// The underlying string.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// True when the id matches the canonical grammar `^N\d+$`.
    pub fn is_canonical(&self) -> bool {
        is_canonical_id(&self.0, 'N')
    }
}

impl ClaimId {
    /// Wraps an already-normalized id. Callers pass a trimmed string.
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// The underlying string.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// True when the id matches the canonical grammar `^C\d+$`.
    pub fn is_canonical(&self) -> bool {
        is_canonical_id(&self.0, 'C')
    }
}

impl std::fmt::Display for NodeId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::fmt::Display for ClaimId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

/// Checks `^<prefix>\d+$`: the given prefix followed by one or more ASCII
/// digits, nothing else. Regex-free to keep the dependency surface small.
pub(crate) fn is_canonical_id(s: &str, prefix: char) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c == prefix => {}
        _ => return false,
    }
    let rest = chars.as_str();
    !rest.is_empty() && rest.bytes().all(|b| b.is_ascii_digit())
}

/// The normalized artifact: the logical exploration graph plus claim content.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Manifest {
    /// Pre-order DFS of the tree, source order preserved.
    pub nodes: Vec<Node>,
    /// Node → node edges (`children` and `also_depends_on`).
    pub links: Vec<Link>,
    /// Node → claim references, resolved against `claims`.
    pub bindings: Vec<Binding>,
    /// Claim content, for the viewer.
    pub claims: Vec<Claim>,
    /// Bounding rectangle enclosing all laid-out nodes. Populated by layout.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bounds: Option<Rect>,
    /// Paper-level metadata from `PAPER.md` frontmatter. Absent when the file is
    /// missing or has no frontmatter fence.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub paper: Option<PaperMeta>,
    /// Typed prior-work dependencies from `logic/related_work.md`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub related_work: Vec<RelatedWork>,
    /// Glossary terms from `logic/concepts.md`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub concepts: Vec<Concept>,
    /// Problem framing from `logic/problem.md`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub problem: Option<Problem>,
    /// Solution recipes, one per `logic/solution/*.md` file (source order).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub recipes: Vec<Recipe>,
    /// Figures/tables from `evidence/`. Populated by a later evidence task.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub exhibits: Vec<Exhibit>,
    /// Node → related-work edges. Populated by a later resolution task.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub built_on: Vec<BuiltOn>,
    /// Node → exhibit edges. Populated by a later resolution task.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub node_exhibits: Vec<NodeExhibit>,
}

/// Paper-level metadata, parsed from `PAPER.md` YAML frontmatter.
///
/// Every field is optional: a `PAPER.md` with no frontmatter fence still yields
/// a `PaperMeta` carrying only the `title` (from the first `# H1`). `year` is
/// normalized to a `String` even when the source encodes it as an integer.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct PaperMeta {
    /// Paper title.
    pub title: Option<String>,
    /// Author names, in source order.
    pub authors: Vec<String>,
    /// Publication year, normalized from int or string.
    pub year: Option<String>,
    /// Venue string.
    pub venue: Option<String>,
    /// DOI or arXiv id. `None` when the source is `null` or absent.
    pub doi: Option<String>,
    /// Abstract text (`abstract` in the source).
    #[serde(rename = "abstract")]
    pub abstract_: Option<String>,
    /// Keyword list, in source order.
    pub keywords: Vec<String>,
}

/// One typed prior-work dependency, parsed from `logic/related_work.md`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RelatedWork {
    /// Reference id (`RW01`).
    pub id: String,
    /// Citation text — the header content after the id.
    pub cite: String,
    /// DOI or arXiv id, when present.
    pub doi: Option<String>,
    /// Relationship kind (`Type:` value), raw — may combine (e.g.
    /// `baseline, extends`).
    pub kind: Option<String>,
    /// `Delta → What changed`.
    pub what_changed: Option<String>,
    /// `Delta → Why`.
    pub why: Option<String>,
    /// `Adopted elements`.
    pub adopted: Option<String>,
    /// Claims this reference affects, from the inline `C##` list. A prose
    /// `none` resolves to an empty list.
    pub claims_affected: Vec<ClaimId>,
}

/// One glossary term, parsed from `logic/concepts.md`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Concept {
    /// The term (the `## <Term>` header text).
    pub term: String,
    /// Notation, LaTeX preserved verbatim.
    pub notation: Option<String>,
    /// Definition prose.
    pub definition: Option<String>,
    /// Boundary conditions (`Boundary` or `Boundary conditions`).
    pub boundary: Option<String>,
    /// Related term names, split from a comma-separated list.
    pub related: Vec<String>,
}

/// Problem framing, parsed from `logic/problem.md`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Problem {
    /// Intro prose before the first `##` section.
    pub statement: Option<String>,
    /// `O#` observation items, full text including the id, in source order.
    pub observations: Vec<String>,
    /// `G#` gap items, full text including the id, in source order.
    pub gaps: Vec<String>,
    /// Key-insight / `I#` items, in source order.
    pub insights: Vec<String>,
}

/// One solution recipe, one per `logic/solution/*.md` file.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Recipe {
    /// Filename stem (e.g. `algorithm`).
    pub name: String,
    /// First `# Title` in the file, when present.
    pub title: Option<String>,
    /// Raw markdown body, verbatim.
    pub body: String,
}

/// The kind of an exhibit. `Other` preserves anything not a figure, proof,
/// result, or table.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExhibitKind {
    Figure,
    Table,
    Result,
    Proof,
    Other,
}

/// One evidence exhibit — a figure, proof, result, or table body file plus
/// its index metadata, parsed from `evidence/`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Exhibit {
    /// Exhibit id.
    pub id: String,
    /// Source file, relative to the artifact root.
    pub file: String,
    /// Figure / proof / result / table / other.
    pub kind: ExhibitKind,
    /// Origin of the exhibit, when stated.
    pub source: Option<String>,
    /// Caption / description prose.
    pub description: Option<String>,
    /// Claims this exhibit supports.
    pub claims: Vec<ClaimId>,
    /// Raw markdown body, verbatim.
    pub body: String,
}

/// A node → related-work edge. Populated by a later resolution task.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BuiltOn {
    pub node: NodeId,
    pub related_work: String,
}

/// A node → exhibit edge. Populated by a later resolution task.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NodeExhibit {
    pub node: NodeId,
    pub exhibit: String,
}

/// One exploration node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Node {
    /// Unique node id.
    pub id: NodeId,
    /// Node type.
    pub kind: NodeKind,
    /// Display label, from `title:` only. Consumers fall back to `id`.
    pub label: Option<String>,
    /// `explicit` | `inferred` when present.
    pub support_level: Option<String>,
    /// Free-form provenance refs (`§1`, `Fig. 1`, ...).
    pub source_refs: Vec<String>,
    /// Prose description.
    pub description: Option<String>,
    /// Free-form provenance tag (`user`, `ai-suggested`, ...). No vocabulary
    /// validation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provenance: Option<String>,
    /// ISO date string; carried verbatim, never parsed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<String>,
    /// Typed per-kind body.
    pub fields: NodeFields,
    /// Free-text evidence entries (the non-`C##` part of `evidence:`).
    pub evidence_notes: Vec<String>,
    /// Whether this node is the root of an *isolated* subtree — a branch the
    /// exploration reached but that hangs off the main tree on its own. Drives
    /// the viewer's "isolated subtree" partition. Defaults to `false`; only the
    /// root of a subtree carries it (children inherit placement from their root).
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub isolated: bool,
    /// Center position assigned by layout. Absent when layout has not run.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pos: Option<Point>,
}

/// The canonical node types, plus a preserved escape hatch.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NodeKind {
    Question,
    Experiment,
    Decision,
    DeadEnd,
    Insight,
    Pivot,
    /// An unrecognized `type:`; the raw string is preserved.
    Other(String),
}

/// Typed body fields, one variant per canonical kind.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NodeFields {
    Question,
    Experiment {
        result: Option<String>,
        exploration: Option<String>,
        outcome: Option<String>,
        /// Experiment-scoped lifecycle status (`planned`, `running`, ...).
        status: Option<String>,
    },
    Decision {
        choice: Option<String>,
        alternatives: Vec<String>,
        rationale: Option<String>,
    },
    DeadEnd {
        hypothesis: Option<String>,
        failure_mode: Option<String>,
        lesson: Option<String>,
        why_failed: Option<String>,
    },
    Insight,
    Pivot {
        prior_direction: Option<String>,
        new_direction: Option<String>,
        reason: Option<String>,
        lesson: Option<String>,
    },
    /// Unknown kind: body fields are captured (as warnings) at the raw layer.
    Other,
}

/// A directed node → node edge.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Link {
    pub from: NodeId,
    pub to: NodeId,
    pub kind: LinkKind,
}

/// Kind of a node → node edge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LinkKind {
    /// Nesting edge, from `children:`.
    Child,
    /// Cross-reference edge, from `also_depends_on:`.
    DependsOn,
}

/// A resolved node → claim reference.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Binding {
    pub node: NodeId,
    pub claim: ClaimId,
    pub role: BindingRole,
}

/// Role of a node → claim reference.
///
/// `non_exhaustive`: `Verifies` (SOULFuzz-only) is intentionally out of scope
/// and may be added later without breaking consumers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum BindingRole {
    /// From a node's `evidence:` list.
    Evidence,
}

/// Claim content, parsed from `logic/claims.md`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Claim {
    pub id: ClaimId,
    pub title: String,
    pub statement: Option<String>,
    pub status: Option<String>,
    /// `E##` proof refs, stored raw. Not validated — no evidence registry yet.
    pub proof: Vec<String>,
    /// Claim → claim dependencies.
    pub deps: Vec<ClaimId>,
}

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

    #[test]
    fn canonical_id_grammar() {
        assert!(is_canonical_id("N01", 'N'));
        assert!(is_canonical_id("N7", 'N'));
        assert!(is_canonical_id("C123", 'C'));
        assert!(!is_canonical_id("N", 'N')); // no digits
        assert!(!is_canonical_id("n01", 'N')); // case-sensitive
        assert!(!is_canonical_id("C01", 'N')); // wrong prefix
        assert!(!is_canonical_id("N01a", 'N')); // trailing junk
        assert!(!is_canonical_id("", 'N'));
    }

    #[test]
    fn id_accessors_and_display() {
        let n = NodeId::new("N01");
        assert_eq!(n.as_str(), "N01");
        assert_eq!(n.to_string(), "N01");
        assert!(n.is_canonical());
        assert!(!NodeId::new("nope").is_canonical());
        assert!(ClaimId::new("C02").is_canonical());
    }

    #[test]
    fn experiment_fields_round_trip() {
        let f = NodeFields::Experiment {
            result: Some("28.4 BLEU".into()),
            exploration: Some("grid over k".into()),
            outcome: Some("sparse wins".into()),
            status: Some("completed".into()),
        };
        let json = serde_json::to_string(&f).unwrap();
        assert_eq!(
            json,
            r#"{"experiment":{"result":"28.4 BLEU","exploration":"grid over k","outcome":"sparse wins","status":"completed"}}"#
        );
        let back: NodeFields = serde_json::from_str(&json).unwrap();
        assert_eq!(back, f);
    }

    #[test]
    fn pivot_fields_round_trip() {
        let f = NodeFields::Pivot {
            prior_direction: Some("dense retrieval".into()),
            new_direction: Some("sparse retrieval".into()),
            reason: Some("latency budget".into()),
            lesson: Some("profile first".into()),
        };
        let json = serde_json::to_string(&f).unwrap();
        assert_eq!(
            json,
            r#"{"pivot":{"prior_direction":"dense retrieval","new_direction":"sparse retrieval","reason":"latency budget","lesson":"profile first"}}"#
        );
        let back: NodeFields = serde_json::from_str(&json).unwrap();
        assert_eq!(back, f);
    }

    #[test]
    fn node_provenance_timestamp_round_trip_and_skip() {
        let mut node = Node {
            id: NodeId::new("N01"),
            kind: NodeKind::Question,
            label: None,
            support_level: None,
            source_refs: vec![],
            description: None,
            provenance: Some("user".into()),
            timestamp: Some("2026-08-19".into()),
            fields: NodeFields::Question,
            evidence_notes: vec![],
            isolated: false,
            pos: None,
        };
        let json = serde_json::to_string(&node).unwrap();
        let back: Node = serde_json::from_str(&json).unwrap();
        assert_eq!(back, node);
        assert!(json.contains(r#""provenance":"user""#));
        assert!(json.contains(r#""timestamp":"2026-08-19""#));

        node.provenance = None;
        node.timestamp = None;
        let json = serde_json::to_string(&node).unwrap();
        assert!(!json.contains("provenance"));
        assert!(!json.contains("timestamp"));
        let back: Node = serde_json::from_str(&json).unwrap();
        assert_eq!(back, node);
    }

    #[test]
    fn exhibit_kind_new_variants_round_trip() {
        for (kind, wire) in [
            (ExhibitKind::Figure, "figure"),
            (ExhibitKind::Table, "table"),
            (ExhibitKind::Result, "result"),
            (ExhibitKind::Proof, "proof"),
            (ExhibitKind::Other, "other"),
        ] {
            let json = serde_json::to_string(&kind).unwrap();
            assert_eq!(json, format!("\"{wire}\""));
            let back: ExhibitKind = serde_json::from_str(&json).unwrap();
            assert_eq!(back, kind);
        }
    }
}