kmp-application 0.14.0

Application services behind KMP's ten memory moves and evidence-backed recall
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
use std::collections::{BTreeMap, BTreeSet};

use kmp_domain::{
    BundleRelationship, DECLARED_FROM_RELATE_METHOD, DimensionSelection, KmpBundle,
    MemoryRelationType, RelationSemanticClass, TemporalAxis, TemporalCoordinate, TemporalCursor,
    TemporalDirection, TemporalWindow,
};
use serde::Serialize;
use sha2::{Digest, Sha256};

use crate::ApplicationError;

use super::{TemporalIncludeOptions, TemporalMemoryQuery, TemporalMemoryResult, VisualLabel};

pub const MAX_VISUAL_SOURCE_ENTRIES: usize = 65_536;
pub const MAX_VISUAL_PAGE_ENTRIES: usize = 2_048;
pub const MAX_VISUAL_BINS: usize = 512;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum VisualLevelOfDetail {
    #[default]
    Atlas,
    Episode,
    Moment,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VisualProjectionQuery {
    pub about: String,
    pub from: String,
    pub to: String,
    pub axis: TemporalAxis,
    pub dimensions: DimensionSelection,
    pub level_of_detail: VisualLevelOfDetail,
    pub bin_count: usize,
    pub page_entries: usize,
    pub cursor: Option<String>,
    pub depth: u32,
}

impl VisualProjectionQuery {
    pub fn temporal_query(&self) -> Result<TemporalMemoryQuery, ApplicationError> {
        Ok(TemporalMemoryQuery {
            about: self.about.clone(),
            direction: TemporalDirection::Goto,
            axis: self.axis,
            cursor: TemporalCursor::time(self.to.clone())?,
            dimensions: self.dimensions.clone(),
            window: TemporalWindow::new(0, 0),
            limit_entries: Some(MAX_VISUAL_SOURCE_ENTRIES),
            include: TemporalIncludeOptions {
                evidence: false,
                relations: true,
                raw_refs: false,
            },
            token_budget: 262_144,
            depth: self.depth.max(1),
            max_tier: None,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct VisualRange {
    pub from: String,
    pub to: String,
}

/// One bin of one label: the lane is the key, the row inside it the value,
/// and a bin counts the entries standing in that pair whose position falls
/// in its span. A renderer that folds a lane sums its rows.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct VisualBin {
    pub dimension: String,
    pub scope_id: String,
    pub from: String,
    pub to: String,
    pub total: usize,
    pub by_kind: BTreeMap<String, usize>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct VisualCluster {
    pub dimension: String,
    pub scope_id: String,
    pub from: String,
    pub to: String,
    pub total: usize,
    pub refs: Vec<String>,
    pub by_kind: BTreeMap<String, usize>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct VisualEntry {
    pub ref_id: String,
    pub kind: String,
    pub text: String,
    pub coordinates: Vec<TemporalCoordinateView>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct TemporalCoordinateView {
    pub dimension: String,
    pub scope_id: String,
    pub occurred_at: Option<String>,
    pub observed_at: Option<String>,
    pub ingested_at: Option<String>,
    pub valid_from: Option<String>,
    pub valid_until: Option<String>,
    pub sequence: Option<u32>,
    pub rank: Option<u32>,
    /// How the label came to stand on the entry, when it was not the write:
    /// the method (`kmp_relabel`), the why, and who did it when.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub why: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub motivation: Option<String>,
}

impl From<&TemporalCoordinate> for TemporalCoordinateView {
    fn from(value: &TemporalCoordinate) -> Self {
        Self {
            dimension: value.dimension().to_string(),
            scope_id: value.scope_id().to_string(),
            occurred_at: value.occurred_at().map(ToString::to_string),
            observed_at: value.observed_at().map(ToString::to_string),
            ingested_at: value.ingested_at().map(ToString::to_string),
            valid_from: value.valid_from().map(ToString::to_string),
            valid_until: value.valid_until().map(ToString::to_string),
            sequence: value.sequence(),
            rank: value.rank(),
            method: value.origin().method().map(ToString::to_string),
            why: value.origin().rationale().map(ToString::to_string),
            motivation: value.origin().motivation().map(ToString::to_string),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct VisualRelation {
    pub from: String,
    pub to: String,
    pub rel: String,
    pub class: String,
    pub why: Option<String>,
    pub evidence: Option<String>,
    pub confidence: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct VisualMetric {
    pub name: String,
    pub value: f64,
    pub unit: String,
    pub scope: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct VisualProjectionPage {
    pub returned: usize,
    pub total: usize,
    pub has_more: bool,
    pub next_cursor: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct VisualProjectionResult {
    pub contract: String,
    pub about: String,
    pub axis: TemporalAxisView,
    pub level_of_detail: VisualLevelOfDetail,
    pub range: VisualRange,
    pub bins: Vec<VisualBin>,
    pub clusters: Vec<VisualCluster>,
    pub entries: Vec<VisualEntry>,
    /// Distinct entries by kind across the selected range. Unlike bin and
    /// cluster aggregates, these totals never count one entry once per lane.
    pub by_kind: BTreeMap<String, usize>,
    pub relations: Vec<VisualRelation>,
    pub metrics: Vec<VisualMetric>,
    /// Every label the about holds, most used first, each with how many
    /// entries stand in it across all time and inside this range. Read
    /// before the read's own filter narrowed the bundle, so an empty label
    /// is listed as empty rather than left out.
    pub labels: Vec<VisualLabel>,
    pub included_dimensions: Vec<String>,
    pub missing_dimensions: Vec<String>,
    pub revision: u64,
    pub content_hash: String,
    pub page: VisualProjectionPage,
    pub truncated: bool,
    pub missing: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TemporalAxisView {
    Default,
    Occurred,
    Observed,
    Ingested,
    Validity,
}

impl From<TemporalAxis> for TemporalAxisView {
    fn from(value: TemporalAxis) -> Self {
        match value {
            TemporalAxis::Default => Self::Default,
            TemporalAxis::Occurred => Self::Occurred,
            TemporalAxis::Observed => Self::Observed,
            TemporalAxis::Ingested => Self::Ingested,
            TemporalAxis::Validity => Self::Validity,
        }
    }
}

#[derive(Debug, Clone)]
struct PositionedEntry {
    entry: VisualEntry,
    position: i128,
    position_text: String,
    /// The labels the entry stands in, as (key, namespaced value) pairs.
    labels: BTreeSet<(String, String)>,
}

/// Bins, clusters and entries of one range, on one clock, one row per
/// label. `catalogue` is the about's labels as read before the dimension
/// filter, so the result lists a label that is empty here as empty.
pub fn build_visual_projection(
    query: &VisualProjectionQuery,
    temporal: TemporalMemoryResult,
    catalogue: Vec<VisualLabel>,
) -> Result<VisualProjectionResult, ApplicationError> {
    let from = timestamp_nanos(&query.from).ok_or_else(|| {
        ApplicationError::Validation("visual projection `from` is not a timestamp".to_string())
    })?;
    let to = timestamp_nanos(&query.to).ok_or_else(|| {
        ApplicationError::Validation("visual projection `to` is not a timestamp".to_string())
    })?;
    if to <= from {
        return Err(ApplicationError::Validation(
            "visual projection range requires `to` after `from`".to_string(),
        ));
    }
    let bin_count = query.bin_count.clamp(1, MAX_VISUAL_BINS);
    let page_entries = query.page_entries.clamp(1, MAX_VISUAL_PAGE_ENTRIES);
    let revision = temporal.source_bundle.metadata().revision;
    let content_hash = temporal.source_bundle.metadata().content_hash.clone();
    let selection_hash = selection_hash(query, revision, &content_hash);
    let offset = cursor_offset(query.cursor.as_deref(), &selection_hash)?;

    let mut positioned = temporal
        .traversal
        .entries()
        .iter()
        .filter_map(|entry| {
            let (position, position_text) = entry
                .coordinates()
                .iter()
                .filter_map(|coordinate| {
                    let value = axis_time(coordinate, query.axis)?;
                    Some((timestamp_nanos(value)?, value.to_string()))
                })
                .min_by_key(|(position, _)| *position)?;
            if position < from || position >= to {
                return None;
            }
            let labels = entry
                .coordinates()
                .iter()
                .map(|coordinate| {
                    (
                        coordinate.dimension().to_string(),
                        coordinate.scope_id().to_string(),
                    )
                })
                .collect();
            Some(PositionedEntry {
                entry: VisualEntry {
                    ref_id: entry.ref_id().to_string(),
                    kind: entry.kind().to_string(),
                    text: entry.text().to_string(),
                    coordinates: entry
                        .coordinates()
                        .iter()
                        .map(TemporalCoordinateView::from)
                        .collect(),
                },
                position,
                position_text,
                labels,
            })
        })
        .collect::<Vec<_>>();
    positioned.sort_by(|left, right| {
        left.position
            .cmp(&right.position)
            .then_with(|| left.entry.ref_id.cmp(&right.entry.ref_id))
    });

    let by_kind = positioned
        .iter()
        .fold(BTreeMap::new(), |mut counts, entry| {
            *counts.entry(entry.entry.kind.clone()).or_default() += 1;
            counts
        });
    let bins = visual_bins(&positioned, from, to, bin_count);
    let clusters = if query.level_of_detail == VisualLevelOfDetail::Episode {
        visual_clusters(&positioned, from, to, bin_count)
    } else {
        Vec::new()
    };
    let total = positioned.len();
    let end = offset.saturating_add(page_entries).min(total);
    let entries = if query.level_of_detail == VisualLevelOfDetail::Moment {
        positioned[offset.min(total)..end]
            .iter()
            .map(|entry| entry.entry.clone())
            .collect()
    } else {
        Vec::new()
    };
    let page_returned = if query.level_of_detail == VisualLevelOfDetail::Moment {
        entries.len()
    } else {
        total
    };
    let has_more = query.level_of_detail == VisualLevelOfDetail::Moment && end < total;
    let next_cursor = has_more.then(|| format!("kmp-visual-v1:{selection_hash}:{end}"));
    let page_refs = entries
        .iter()
        .map(|entry| entry.ref_id.clone())
        .collect::<BTreeSet<_>>();
    let relations = if query.level_of_detail == VisualLevelOfDetail::Moment {
        visual_relations(&temporal, &page_refs)
    } else {
        Vec::new()
    };
    let included_dimensions = positioned
        .iter()
        .flat_map(|entry| entry.labels.iter().map(|(dimension, _)| dimension.clone()))
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect::<Vec<_>>();
    let labels = VisualLabel::counted(
        catalogue,
        positioned.iter().flat_map(|entry| entry.labels.iter()),
    );
    let causal = causal_relation_count(&relations);
    let relation_count = relations.len();
    let source_truncated = temporal.traversal.page().has_more();
    let missing = if source_truncated {
        vec!["visual_source_entries".to_string()]
    } else {
        Vec::new()
    };

    Ok(VisualProjectionResult {
        contract: "kmp.visual.projection.v1".to_string(),
        about: query.about.clone(),
        axis: query.axis.into(),
        level_of_detail: query.level_of_detail,
        range: VisualRange {
            from: query.from.clone(),
            to: query.to.clone(),
        },
        bins,
        clusters,
        entries,
        by_kind,
        relations,
        labels,
        metrics: vec![
            VisualMetric {
                name: "entry_count".to_string(),
                value: total as f64,
                unit: "entries".to_string(),
                scope: "selected_range".to_string(),
            },
            VisualMetric {
                name: "relation_count".to_string(),
                value: relation_count as f64,
                unit: "relations".to_string(),
                scope: "selected_subgraph".to_string(),
            },
            VisualMetric {
                name: "causal_density".to_string(),
                value: ratio(causal, relation_count),
                unit: "ratio".to_string(),
                scope: "selected_subgraph".to_string(),
            },
        ],
        included_dimensions,
        missing_dimensions: temporal.traversal.missing_dimensions().to_vec(),
        revision,
        content_hash,
        page: VisualProjectionPage {
            returned: page_returned,
            total,
            has_more,
            next_cursor,
        },
        truncated: source_truncated || has_more,
        missing,
    })
}

fn visual_bins(entries: &[PositionedEntry], from: i128, to: i128, count: usize) -> Vec<VisualBin> {
    let span = (to - from).max(1);
    let mut bins = BTreeMap::<(String, String, usize), (usize, BTreeMap<String, usize>)>::new();
    for entry in entries {
        let index = (((entry.position - from) * count as i128) / span)
            .clamp(0, count.saturating_sub(1) as i128) as usize;
        for (dimension, scope_id) in &entry.labels {
            let (total, by_kind) = bins
                .entry((dimension.clone(), scope_id.clone(), index))
                .or_default();
            *total += 1;
            *by_kind.entry(entry.entry.kind.clone()).or_default() += 1;
        }
    }
    bins.into_iter()
        .map(
            |((dimension, scope_id, index), (total, by_kind))| VisualBin {
                dimension,
                scope_id,
                from: nanos_timestamp(from + span * index as i128 / count as i128),
                to: nanos_timestamp(from + span * (index + 1) as i128 / count as i128),
                total,
                by_kind,
            },
        )
        .collect()
}

fn visual_clusters(
    entries: &[PositionedEntry],
    from: i128,
    to: i128,
    count: usize,
) -> Vec<VisualCluster> {
    let span = (to - from).max(1);
    let mut clusters = BTreeMap::<(String, String, usize), Vec<&PositionedEntry>>::new();
    for entry in entries {
        let index = (((entry.position - from) * count as i128) / span)
            .clamp(0, count.saturating_sub(1) as i128) as usize;
        for (dimension, scope_id) in &entry.labels {
            clusters
                .entry((dimension.clone(), scope_id.clone(), index))
                .or_default()
                .push(entry);
        }
    }
    clusters
        .into_iter()
        .map(|((dimension, scope_id, _), entries)| {
            let mut by_kind = BTreeMap::new();
            let mut refs = Vec::new();
            for entry in &entries {
                *by_kind.entry(entry.entry.kind.clone()).or_default() += 1;
                refs.push(entry.entry.ref_id.clone());
            }
            VisualCluster {
                dimension,
                scope_id,
                from: entries
                    .first()
                    .map(|entry| entry.position_text.clone())
                    .unwrap_or_default(),
                to: entries
                    .last()
                    .map(|entry| entry.position_text.clone())
                    .unwrap_or_default(),
                total: entries.len(),
                refs,
                by_kind,
            }
        })
        .collect()
}

fn visual_relations(
    temporal: &TemporalMemoryResult,
    refs: &BTreeSet<String>,
) -> Vec<VisualRelation> {
    temporal
        .source_bundle
        .relationships()
        .iter()
        .filter(|relation| {
            relation.explanation().semantic_class()
                != &kmp_domain::RelationSemanticClass::Structural
                && ((refs.contains(relation.source_node_id())
                    && refs.contains(relation.target_node_id()))
                    || (relation.relationship_type() == "supports"
                        && refs.contains(relation.target_node_id())))
        })
        .map(visual_relation)
        .collect()
}

fn visual_relation(relation: &BundleRelationship) -> VisualRelation {
    VisualRelation {
        from: relation.source_node_id().to_string(),
        to: relation.target_node_id().to_string(),
        rel: relation.relationship_type().to_string(),
        class: relation.explanation().semantic_class().as_str().to_string(),
        why: relation.explanation().rationale().map(ToString::to_string),
        evidence: relation.explanation().evidence().map(ToString::to_string),
        confidence: relation.explanation().confidence().map(ToString::to_string),
        method: relation.explanation().method().map(ToString::to_string),
    }
}

/// Keep the writer's stored declaration before dimension filtering drops
/// its foreign endpoint. This does not fetch or admit that endpoint.
pub(super) fn declared_equivalences(bundle: &KmpBundle) -> Vec<VisualRelation> {
    bundle
        .relationships()
        .iter()
        .filter(|relation| {
            let proof = relation.explanation();
            MemoryRelationType::new(relation.relationship_type())
                .is_ok_and(|kind| kind.may_cross_abouts())
                && proof.semantic_class() == &RelationSemanticClass::Evidential
                && proof
                    .method()
                    .is_some_and(|method| method.starts_with(DECLARED_FROM_RELATE_METHOD))
                && proof.rationale().is_some_and(|why| !why.trim().is_empty())
                && proof
                    .evidence()
                    .is_some_and(|evidence| !evidence.trim().is_empty())
        })
        .map(visual_relation)
        .collect()
}

pub(super) fn include_owned_declarations(
    projection: &mut VisualProjectionResult,
    declarations: Vec<VisualRelation>,
) {
    let refs: BTreeSet<_> = projection
        .entries
        .iter()
        .map(|entry| entry.ref_id.as_str())
        .collect();
    let mut keys: BTreeSet<_> = projection
        .relations
        .iter()
        .map(|edge| (edge.from.clone(), edge.rel.clone(), edge.to.clone()))
        .collect();
    for relation in declarations {
        if refs.contains(relation.from.as_str())
            && keys.insert((
                relation.from.clone(),
                relation.rel.clone(),
                relation.to.clone(),
            ))
        {
            projection.relations.push(relation);
        }
    }
    for metric in &mut projection.metrics {
        match metric.name.as_str() {
            "relation_count" => metric.value = projection.relations.len() as f64,
            "causal_density" => {
                metric.value = ratio(
                    causal_relation_count(&projection.relations),
                    projection.relations.len(),
                )
            }
            _ => {}
        }
    }
}

fn axis_time(coordinate: &TemporalCoordinate, axis: TemporalAxis) -> Option<&str> {
    match axis {
        TemporalAxis::Default => coordinate
            .occurred_at()
            .or(coordinate.valid_from())
            .or(coordinate.observed_at())
            .or(coordinate.ingested_at()),
        TemporalAxis::Occurred => coordinate.occurred_at(),
        TemporalAxis::Observed => coordinate.observed_at(),
        TemporalAxis::Ingested => coordinate.ingested_at(),
        TemporalAxis::Validity => coordinate.valid_from().or(coordinate.valid_until()),
    }
}

fn selection_hash(query: &VisualProjectionQuery, revision: u64, content_hash: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(query.about.as_bytes());
    hasher.update(query.from.as_bytes());
    hasher.update(query.to.as_bytes());
    hasher.update(format!("{:?}", query.axis).as_bytes());
    hasher.update(format!("{:?}", query.dimensions).as_bytes());
    hasher.update(format!("{:?}", query.level_of_detail).as_bytes());
    hasher.update(query.bin_count.to_le_bytes());
    hasher.update(revision.to_le_bytes());
    hasher.update(content_hash.as_bytes());
    format!("{:x}", hasher.finalize())[..24].to_string()
}

fn cursor_offset(cursor: Option<&str>, selection_hash: &str) -> Result<usize, ApplicationError> {
    let Some(cursor) = cursor else {
        return Ok(0);
    };
    let mut parts = cursor.split(':');
    let valid = parts.next() == Some("kmp-visual-v1")
        && parts.next() == Some(selection_hash)
        && parts.clone().count() == 1;
    let offset = parts.next().and_then(|value| value.parse::<usize>().ok());
    if !valid || offset.is_none() {
        return Err(ApplicationError::Validation(
            "visual projection cursor is malformed or belongs to another selection".to_string(),
        ));
    }
    Ok(offset.unwrap_or_default())
}

fn ratio(numerator: usize, denominator: usize) -> f64 {
    if denominator == 0 {
        0.0
    } else {
        numerator as f64 / denominator as f64
    }
}

fn causal_relation_count(relations: &[VisualRelation]) -> usize {
    relations
        .iter()
        .filter(|relation| relation.class == "causal")
        .count()
}

fn timestamp_nanos(value: &str) -> Option<i128> {
    if let Some(value) = value.strip_prefix("unix:") {
        let (seconds, nanos) = value.split_once(':')?;
        let seconds = seconds.parse::<i128>().ok()? - 100_000_000_000i128;
        let nanos = nanos.parse::<i128>().ok()?;
        return Some(seconds * 1_000_000_000 + nanos);
    }
    basic_rfc3339_nanos(value)
}

fn nanos_timestamp(value: i128) -> String {
    let seconds = value.div_euclid(1_000_000_000);
    let nanos = value.rem_euclid(1_000_000_000);
    format!("unix:{:012}:{:09}", seconds + 100_000_000_000i128, nanos)
}

fn basic_rfc3339_nanos(value: &str) -> Option<i128> {
    let value = value.trim();
    if value.len() < 20 {
        return None;
    }
    let number = |from: usize, to: usize| -> Option<i64> { value.get(from..to)?.parse().ok() };
    let year = number(0, 4)?;
    let month = number(5, 7)?;
    let day = number(8, 10)?;
    let hour = number(11, 13)?;
    let minute = number(14, 16)?;
    let second = number(17, 19)?;
    if value.get(4..5)? != "-"
        || value.get(7..8)? != "-"
        || value.get(10..11)? != "T"
        || value.get(13..14)? != ":"
        || value.get(16..17)? != ":"
    {
        return None;
    }
    let tail = value.get(19..)?;
    let timezone_start = tail
        .char_indices()
        .find_map(|(index, character)| matches!(character, 'Z' | '+' | '-').then_some(index))?;
    let fraction = tail.get(..timezone_start)?;
    let timezone = tail.get(timezone_start..)?;
    let nanos = match fraction.strip_prefix('.') {
        Some(digits) if !digits.is_empty() && digits.len() <= 9 => {
            let padded = format!("{digits:0<9}");
            padded.parse::<i128>().ok()?
        }
        None if fraction.is_empty() => 0,
        _ => return None,
    };
    let offset_seconds = match timezone {
        "Z" => 0,
        offset if offset.len() == 6 && offset.get(3..4) == Some(":") => {
            let sign = match offset.get(..1)? {
                "+" => 1,
                "-" => -1,
                _ => return None,
            };
            let hours = offset.get(1..3)?.parse::<i64>().ok()?;
            let minutes = offset.get(4..6)?.parse::<i64>().ok()?;
            if hours > 23 || minutes > 59 {
                return None;
            }
            sign * (hours * 3_600 + minutes * 60)
        }
        _ => return None,
    };
    let seconds = days_from_civil(year, month, day) * 86_400 + hour * 3_600 + minute * 60 + second
        - offset_seconds;
    Some(seconds as i128 * 1_000_000_000 + nanos)
}

fn days_from_civil(year: i64, month: i64, day: i64) -> i64 {
    let year = if month <= 2 { year - 1 } else { year };
    let era = if year >= 0 { year } else { year - 399 } / 400;
    let year_of_era = year - era * 400;
    let day_of_year = (153 * (if month > 2 { month - 3 } else { month + 9 }) + 2) / 5 + day - 1;
    let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
    era * 146_097 + day_of_era - 719_468
}

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

    #[test]
    fn sortable_and_rfc3339_timestamps_share_one_coordinate_space() {
        assert_eq!(
            timestamp_nanos("2026-08-27T12:00:00Z"),
            timestamp_nanos("unix:101787832000:000000000")
        );
        assert_eq!(
            timestamp_nanos("2026-08-27T14:00:00.125+02:00"),
            timestamp_nanos("unix:101787832000:125000000")
        );
    }

    #[test]
    fn projection_cursor_is_bound_to_its_selection() {
        assert_eq!(cursor_offset(None, "abc").expect("first page"), 0);
        assert_eq!(
            cursor_offset(Some("kmp-visual-v1:abc:12"), "abc").expect("same selection"),
            12
        );
        assert!(cursor_offset(Some("kmp-visual-v1:def:12"), "abc").is_err());
    }

    #[test]
    fn visual_causal_density_does_not_count_other_explanatory_classes() {
        let relation = |class: &str| VisualRelation {
            from: "a".to_string(),
            to: "b".to_string(),
            rel: "rel".to_string(),
            class: class.to_string(),
            why: None,
            evidence: None,
            confidence: None,
            method: None,
        };
        let relations = [
            relation("causal"),
            relation("evidential"),
            relation("motivational"),
        ];

        assert_eq!(causal_relation_count(&relations), 1);
    }
}