weftgraph 0.1.0

Graph Storage gear: typed, multi-tenant knowledge graph with search and traversal over a pluggable store
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
//! REST DTOs. All serde and `OpenAPI` schema live here; the SDK models stay
//! transport-agnostic. Names are `Graph*`-prefixed so they cannot collide in
//! a shared `OpenAPI` component registry.

use graph_storage_sdk::models as m;

// ---------------------------------------------------------------------------
// Ontology
// ---------------------------------------------------------------------------

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphTypeRegistrationDto {
    /// Canonical GTS identifier of the type being registered.
    pub type_id: String,
    /// Its draft-07 JSON Schema.
    pub schema: serde_json::Value,
}

/// What a batch may do to an identifier that is already registered with a
/// different schema.
#[derive(Debug, Default)]
#[toolkit_macros::api_dto(request)]
pub struct GraphTypeRegisterOptionsDto {
    /// `reject` (the default) makes a changed schema a conflict, as it always
    /// was; `update` admits the change when it is admissible and refuses it
    /// with the offending schema locations when it is not.
    pub on_existing: Option<String>,
    /// Admit a change the schemas cannot prove compatible when every stored
    /// row of the type still validates against the candidate. A statement
    /// about *these rows*, not about the type, and it costs a scan bounded by
    /// `type_update_max_rows`.
    pub revalidate: Option<bool>,
}

/// One step of a payload migration.
///
/// A closed set spelled as `op` plus that step's fields, rather than a JSON
/// one-of: it is one `OpenAPI` object, and a misspelled `op` is refused by
/// name instead of silently matching nothing.
#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphMigrationStepDto {
    /// `rename` | `default` | `drop`.
    pub op: String,
    /// `rename`: the payload path to move the value from.
    pub from: Option<String>,
    /// `rename`: the payload path to move it to.
    pub to: Option<String>,
    /// `default` and `drop`: the payload path they act on.
    pub path: Option<String>,
    /// `default`: the value to set where nothing is there.
    pub value: Option<serde_json::Value>,
}

/// What to do with one type's stored payloads so they satisfy the candidate.
#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphTypeMigrationDto {
    pub type_id: String,
    pub steps: Vec<GraphMigrationStepDto>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphRegisterTypesRequest {
    pub types: Vec<GraphTypeRegistrationDto>,
    pub options: Option<GraphTypeRegisterOptionsDto>,
    /// Payload migrations, at most one per type in `types`. A migration needs
    /// `options.on_existing: "update"`, and it needs a schema change to
    /// migrate towards.
    pub migrations: Option<Vec<GraphTypeMigrationDto>>,
}

/// One reason a directional verdict does not hold, with the schema location
/// that carries it.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphSchemaDiagnosticDto {
    /// Location in the resolved schema; `$` is the document root.
    pub location: String,
    /// Machine-readable finding kind, as `gts` names it (`property_added`,
    /// `required_changed`, `enum_changed`, `not_provable`, ...).
    pub finding: String,
    pub message: String,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphTraitChangeDto {
    pub trait_name: String,
    pub added: Vec<String>,
    pub removed: Vec<String>,
}

/// The verdict on one candidate definition.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphTypeChangeDto {
    pub type_id: String,
    /// `new` | `unchanged` | `compatible` | `incompatible` | `undecidable`.
    pub state: String,
    /// `Valid(old) ⊆ Valid(new)` — the direction that gates admission.
    pub backward: String,
    /// Reported, never enforced: whether a reader pinned to the old
    /// definition still accepts payloads written against the new one.
    pub forward: String,
    pub diagnostics: Vec<GraphSchemaDiagnosticDto>,
    pub traits_changed: Vec<GraphTraitChangeDto>,
    /// Live rows of the type, when the operation needed to know.
    pub rows: Option<i64>,
    /// Rows a migration changed, or would change in a dry run.
    pub rows_rewritten: Option<i64>,
    /// Object levels where a *later* definition will not be able to add an
    /// optional property, so "your next edit is a major" is a warning now.
    pub levels_not_evolvable_in_place: Vec<String>,
    pub migration_required: bool,
    /// Whether the gear would admit this change under the request's options.
    pub admissible: bool,
}

/// A registered type and what the call did to it.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphRegisteredTypeDto {
    pub type_id: String,
    pub type_uuid: String,
    pub kind: String,
    pub is_abstract: bool,
    pub schema: serde_json::Value,
    pub effective_traits: GraphEffectiveTraitsDto,
    /// Which retained definition is in force (ADR-0005).
    pub revision: i32,
    /// `created` | `unchanged` | `updated`.
    pub outcome: String,
    /// `schema_proved` when the schemas prove inclusion, `data_backed` when
    /// the type's own rows were validated instead. Absent unless something
    /// was updated.
    pub admission_basis: Option<String>,
    /// Rows read for a `data_backed` or `migrated` admission.
    pub rows_validated: Option<i64>,
    /// Rows a `migrated` admission actually changed.
    pub rows_rewritten: Option<i64>,
    /// The verdict, present whenever the identifier was already registered,
    /// and always in a dry run.
    pub change: Option<GraphTypeChangeDto>,
}

/// What registering this batch would do. Nothing is written.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphTypeCompatibilityDto {
    pub items: Vec<GraphRegisteredTypeDto>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphEffectiveTraitsDto {
    pub family: Option<String>,
    pub scope_managed: bool,
    pub emit_events: bool,
    pub index: Vec<String>,
    pub full_text_search: Vec<String>,
    pub vector_search: Vec<String>,
    pub src_types: Vec<String>,
    pub dst_types: Vec<String>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphTypeDto {
    pub type_id: String,
    pub type_uuid: String,
    pub kind: String,
    pub is_abstract: bool,
    pub schema: serde_json::Value,
    pub effective_traits: GraphEffectiveTraitsDto,
    /// Which retained definition of this identifier is in force: `1` until it
    /// is first updated in place (types-registry ADR-0005).
    pub revision: i32,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphTypeListDto {
    pub items: Vec<GraphTypeDto>,
    pub next_cursor: Option<String>,
    pub revision: GraphRevisionDto,
}

/// One capability's readiness, as DESIGN's matrix reports it.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphComponentReadinessDto {
    /// The matrix's own name for the component.
    pub component: String,
    /// `healthy` | `degraded` | `unhealthy` | `not_implemented`.
    pub state: String,
    /// What is wrong, named. Absent when healthy.
    pub problem: Option<String>,
    /// What this state rejects.
    pub blocked: Option<String>,
    /// The condition being waited on.
    pub recovery: Option<String>,
}

/// Readiness per capability, never one global boolean.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphReadinessDto {
    /// Ready when no component whose failure blocks everything is unhealthy.
    /// An embedding-space mismatch is unhealthy and leaves the gear ready:
    /// it blocks the vector arms and nothing else.
    pub ready: bool,
    pub components: Vec<GraphComponentReadinessDto>,
}

/// One source namespace and the producer principal bound to it.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphSourceNamespaceDto {
    /// The `source.system` value of a reference node's identity triple.
    pub namespace: String,
    pub owner_principal: String,
    #[serde(with = "time::serde::rfc3339")]
    pub claimed_at: time::OffsetDateTime,
    /// Who held it before the last transfer, if it was ever transferred.
    pub previous_owner: Option<String>,
    #[serde(with = "time::serde::rfc3339::option", default)]
    pub transferred_at: Option<time::OffsetDateTime>,
    pub transferred_by: Option<GraphSubjectDto>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphSourceNamespaceListDto {
    pub items: Vec<GraphSourceNamespaceDto>,
}

/// Who a namespace is being transferred to.
#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphTransferNamespaceRequest {
    /// The producer principal that may write the namespace from now on.
    pub owner_principal: String,
}

// ---------------------------------------------------------------------------
// Revision
// ---------------------------------------------------------------------------

/// The snapshot identity every read reports: the deployment-wide,
/// non-reusable source epoch paired with the per-tenant revision.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphRevisionDto {
    pub source_epoch: i64,
    pub revision: i64,
}

// ---------------------------------------------------------------------------
// Element envelope
// ---------------------------------------------------------------------------

/// The acting party behind a write, in the platform's subject vocabulary
/// rather than as a user id -- most writes into this gear come from an
/// automation or a service integration.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphSubjectDto {
    pub subject_id: String,
    /// GTS type of the subject, e.g. `gts.cf.core.security.subject_user.v1~`.
    /// Absent when the security context carries none.
    pub subject_type: Option<String>,
}

/// The gear-assigned half of an element (`fr-audit-envelope`): identical for
/// every node and every edge, described here rather than by the element's GTS
/// type, and read-only on every write surface.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphElementEnvelopeDto {
    pub tenant_id: String,
    /// A node's producer-supplied `node_key`, an edge's derived `edge_key`.
    pub key: String,
    #[serde(with = "time::serde::rfc3339")]
    pub created_at: time::OffsetDateTime,
    pub created_by: GraphSubjectDto,
    #[serde(with = "time::serde::rfc3339")]
    pub updated_at: time::OffsetDateTime,
    pub updated_by: GraphSubjectDto,
    /// Soft-delete tombstone; absent on a live element.
    #[serde(with = "time::serde::rfc3339::option", default)]
    pub deleted_at: Option<time::OffsetDateTime>,
    pub deleted_by: Option<GraphSubjectDto>,
    /// The revision the read that produced this element observed.
    pub graph_revision: GraphRevisionDto,
}

// ---------------------------------------------------------------------------
// Ingest
// ---------------------------------------------------------------------------

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphNodeSpecDto {
    pub node_key: String,
    pub type_id: String,
    pub name: Option<String>,
    /// Omitted clears the stored payload: ingest replaces, never merges.
    pub payload: Option<serde_json::Value>,
    pub expected_version: Option<i64>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphEdgeSpecDto {
    pub type_id: String,
    pub src_node_key: String,
    pub dst_node_key: String,
    pub discriminator: Option<String>,
    pub payload: Option<serde_json::Value>,
}

#[derive(Debug, Default)]
#[toolkit_macros::api_dto(request)]
pub struct GraphIngestOptionsDto {
    pub create_phantoms: Option<bool>,
    #[serde(default)]
    pub report_per_item: bool,
    /// `false` skips embedding; existing vectors are kept, not cleared.
    /// Omitted means the deployment default (on).
    pub embed: Option<bool>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphReplaceScopeDto {
    pub attribute: String,
    pub value: String,
    pub generation: i64,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphIngestRequest {
    #[serde(default)]
    pub nodes: Vec<GraphNodeSpecDto>,
    #[serde(default)]
    pub edges: Vec<GraphEdgeSpecDto>,
    #[serde(default)]
    pub options: GraphIngestOptionsDto,
    pub replace_scope: Option<GraphReplaceScopeDto>,
    /// Mirrors the `Idempotency-Key` header for SDK callers; the header wins
    /// when both are present.
    pub idempotency_key: Option<String>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphIngestCountsDto {
    pub nodes_inserted: u64,
    pub nodes_updated: u64,
    pub nodes_unchanged: u64,
    pub edges_inserted: u64,
    pub edges_updated: u64,
    pub edges_unchanged: u64,
    pub phantoms_created: u64,
    pub phantoms_materialized: u64,
    /// Static content a scope replacement removed because the batch no longer
    /// named it. Zero unless `replace_scope` was set.
    pub scope_removed_nodes: u64,
    pub scope_removed_edges: u64,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphIngestResultDto {
    pub revision: GraphRevisionDto,
    /// True when an idempotency receipt answered without touching state.
    pub replayed: bool,
    pub counts: GraphIngestCountsDto,
    /// One of `inserted | updated | unchanged | materialized` per node of the
    /// batch, in batch order. Present only when `options.report_per_item`
    /// was set, and absent on a replayed call: the receipt keeps the counts,
    /// not the list.
    pub per_item_nodes: Option<Vec<String>>,
    /// The same for the batch's edges (`inserted | updated | unchanged`).
    pub per_item_edges: Option<Vec<String>>,
}

fn item_outcome_name(outcome: &m::ItemOutcome) -> String {
    outcome.as_str().to_owned()
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphDeleteResultDto {
    pub revision: GraphRevisionDto,
    pub tombstoned_nodes: u64,
    pub tombstoned_edges: u64,
}

// ---------------------------------------------------------------------------
// Reads
// ---------------------------------------------------------------------------

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphAdjacencyEntryDto {
    pub edge_key: String,
    pub edge_type_id: String,
    pub direction: String,
    pub neighbor_key: String,
    pub neighbor_type_id: String,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphNodeDto {
    pub node_key: String,
    pub type_id: String,
    pub name: Option<String>,
    pub payload: Option<serde_json::Value>,
    pub has_embedding: bool,
    pub adjacency: Vec<GraphAdjacencyEntryDto>,
    pub adjacency_truncated: bool,
    pub envelope: GraphElementEnvelopeDto,
}

/// One edge as an element: what the topology references carry, plus the
/// payload and the envelope (`fr-audit-envelope`).
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphEdgeDto {
    pub edge_key: String,
    pub edge_type_id: String,
    pub src: String,
    pub dst: String,
    pub discriminator: Option<String>,
    pub payload: Option<serde_json::Value>,
    pub envelope: GraphElementEnvelopeDto,
}

/// One projection row. What `$filter` and `$orderby` may name is declared
/// once, on `graph_storage_sdk::NodeQuery`.
#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphNodeRowDto {
    pub node_key: String,
    pub type_id: String,
    pub name: Option<String>,
    pub payload: Option<serde_json::Value>,
    /// On this path the envelope is also the only carrier of the observed
    /// revision: the page wrapper is the platform's and has no member for one.
    pub envelope: GraphElementEnvelopeDto,
}

// ---------------------------------------------------------------------------
// Search
// ---------------------------------------------------------------------------

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphSearchRequest {
    /// `lexical`, `vector` or `hybrid`.
    pub mode: String,
    /// Required by every mode. The vector arm embeds this same text through
    /// the deployment's provider -- the one ingest used -- so a caller never
    /// supplies a vector of its own.
    pub query: Option<String>,
    pub arm_limit: Option<u32>,
    pub limit: Option<u32>,
    #[serde(default)]
    pub type_patterns: Vec<String>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphArmHitDto {
    pub arm: String,
    pub rank: u32,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphSearchHitDto {
    pub node_key: String,
    pub type_id: String,
    pub name: Option<String>,
    /// Fused (RRF) score.
    pub score: f64,
    /// Which arms matched, and at what rank in each.
    pub arms: Vec<GraphArmHitDto>,
    pub snippet: Option<String>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphSearchResponseDto {
    pub hits: Vec<GraphSearchHitDto>,
    pub revision: GraphRevisionDto,
    /// `response_bytes` when the hit list was cut short by the response byte
    /// budget rather than by the caller's `limit`; absent otherwise.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub truncated: Option<String>,
}

// ---------------------------------------------------------------------------
// Traversal
// ---------------------------------------------------------------------------

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphTraverseRequest {
    pub seeds: Vec<String>,
    pub depth: u8,
    #[serde(default)]
    pub edge_type_patterns: Vec<String>,
    #[serde(default)]
    pub node_type_patterns: Vec<String>,
    pub max_nodes: Option<u32>,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(request)]
pub struct GraphNeighborhoodRequest {
    pub root: String,
    pub depth: u8,
    pub node_budget: Option<u32>,
    #[serde(default)]
    pub include_phantoms: bool,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphEdgeRefDto {
    pub edge_key: String,
    pub edge_type_id: String,
    pub src: String,
    pub dst: String,
}

#[derive(Debug)]
#[toolkit_macros::api_dto(response)]
pub struct GraphTraversalResponseDto {
    pub nodes: Vec<GraphNodeDto>,
    pub edges: Vec<GraphEdgeRefDto>,
    /// The seeds the walk started from: requested, deduped, and filtered to
    /// what the caller may see. Unknown and unauthorized seeds are absent
    /// alike.
    pub seeds: Vec<String>,
    /// Present when a budget stopped the walk. Never silent.
    pub truncated: Option<String>,
    pub revision: GraphRevisionDto,
    /// Whether every arm of this read observed one graph state.
    ///
    /// `false` means the store cannot hold a repeatable-read snapshot and the
    /// revision moved while the walk ran, so `revision` is where it ended
    /// rather than a state the whole answer existed at. A revision-keyed
    /// cache should not store this response under that key.
    pub consistent_snapshot: bool,
}

// ---------------------------------------------------------------------------
// Conversions
// ---------------------------------------------------------------------------

impl From<m::GraphRevision> for GraphRevisionDto {
    fn from(value: m::GraphRevision) -> Self {
        Self {
            source_epoch: value.source_epoch,
            revision: value.revision,
        }
    }
}

impl From<m::EffectiveTraits> for GraphEffectiveTraitsDto {
    fn from(value: m::EffectiveTraits) -> Self {
        Self {
            family: value.family,
            scope_managed: value.scope_managed,
            emit_events: value.emit_events,
            index: value.index,
            full_text_search: value.full_text_search,
            vector_search: value.vector_search,
            src_types: value.src_types,
            dst_types: value.dst_types,
        }
    }
}

impl From<m::TypeRecord> for GraphTypeDto {
    fn from(value: m::TypeRecord) -> Self {
        Self {
            type_id: value.type_id,
            type_uuid: value.type_uuid.to_string(),
            kind: value.kind.as_str().to_owned(),
            is_abstract: value.is_abstract,
            schema: value.schema,
            effective_traits: value.effective_traits.into(),
            revision: value.revision,
        }
    }
}

impl From<m::ComponentReadiness> for GraphComponentReadinessDto {
    fn from(value: m::ComponentReadiness) -> Self {
        Self {
            component: value.component,
            state: value.state.as_str().to_owned(),
            problem: value.problem,
            blocked: value.blocked,
            recovery: value.recovery,
        }
    }
}

impl From<m::Readiness> for GraphReadinessDto {
    fn from(value: m::Readiness) -> Self {
        Self {
            ready: value.ready,
            components: value.components.into_iter().map(Into::into).collect(),
        }
    }
}

impl From<m::SourceNamespaceOwner> for GraphSourceNamespaceDto {
    fn from(value: m::SourceNamespaceOwner) -> Self {
        Self {
            namespace: value.namespace,
            owner_principal: value.owner_principal,
            claimed_at: value.claimed_at,
            previous_owner: value.previous_owner,
            transferred_at: value.transferred_at,
            transferred_by: value.transferred_by.map(Into::into),
        }
    }
}

impl From<m::SchemaDiagnostic> for GraphSchemaDiagnosticDto {
    fn from(value: m::SchemaDiagnostic) -> Self {
        Self {
            location: value.location,
            finding: value.finding,
            message: value.message,
        }
    }
}

impl From<m::TraitChange> for GraphTraitChangeDto {
    fn from(value: m::TraitChange) -> Self {
        Self {
            trait_name: value.trait_name,
            added: value.added,
            removed: value.removed,
        }
    }
}

impl From<m::TypeChange> for GraphTypeChangeDto {
    fn from(value: m::TypeChange) -> Self {
        Self {
            type_id: value.type_id,
            state: value.state.as_str().to_owned(),
            backward: value.backward,
            forward: value.forward,
            diagnostics: value.diagnostics.into_iter().map(Into::into).collect(),
            traits_changed: value.traits_changed.into_iter().map(Into::into).collect(),
            // A count crosses the boundary as a signed integer: `OpenAPI`
            // integers are signed, and a row count cannot approach the bound.
            rows: value.rows.and_then(|rows| i64::try_from(rows).ok()),
            rows_rewritten: value
                .rows_rewritten
                .and_then(|rows| i64::try_from(rows).ok()),
            levels_not_evolvable_in_place: value.levels_not_evolvable_in_place,
            migration_required: value.migration_required,
            admissible: value.admissible,
        }
    }
}

impl From<m::RegisteredType> for GraphRegisteredTypeDto {
    fn from(value: m::RegisteredType) -> Self {
        let (admission_basis, rows_validated, rows_rewritten) = match value.basis {
            None => (None, None, None),
            Some(m::AdmissionBasis::SchemaProved) => (Some("schema_proved".to_owned()), None, None),
            Some(m::AdmissionBasis::DataBacked { rows_validated }) => (
                Some("data_backed".to_owned()),
                i64::try_from(rows_validated).ok(),
                None,
            ),
            Some(m::AdmissionBasis::Migrated {
                rows_scanned,
                rows_rewritten,
            }) => (
                Some("migrated".to_owned()),
                i64::try_from(rows_scanned).ok(),
                i64::try_from(rows_rewritten).ok(),
            ),
        };
        let record = value.record;
        Self {
            type_id: record.type_id,
            type_uuid: record.type_uuid.to_string(),
            kind: record.kind.as_str().to_owned(),
            is_abstract: record.is_abstract,
            schema: record.schema,
            effective_traits: record.effective_traits.into(),
            revision: record.revision,
            outcome: value.outcome.as_str().to_owned(),
            admission_basis,
            rows_validated,
            rows_rewritten,
            change: value.change.map(Into::into),
        }
    }
}

impl From<GraphTypeRegistrationDto> for m::TypeRegistration {
    fn from(value: GraphTypeRegistrationDto) -> Self {
        Self {
            type_id: value.type_id,
            schema: value.schema,
        }
    }
}

impl From<GraphNodeSpecDto> for m::NodeSpec {
    fn from(value: GraphNodeSpecDto) -> Self {
        Self {
            node_key: value.node_key,
            type_id: value.type_id,
            name: value.name,
            payload: value.payload,
            expected_version: value.expected_version,
        }
    }
}

impl From<GraphEdgeSpecDto> for m::EdgeSpec {
    fn from(value: GraphEdgeSpecDto) -> Self {
        Self {
            type_id: value.type_id,
            src_node_key: value.src_node_key,
            dst_node_key: value.dst_node_key,
            discriminator: value.discriminator,
            payload: value.payload,
        }
    }
}

impl From<GraphReplaceScopeDto> for m::ReplaceScope {
    fn from(value: GraphReplaceScopeDto) -> Self {
        Self {
            attribute: value.attribute,
            value: value.value,
            generation: value.generation,
        }
    }
}

impl From<m::IngestOutcome> for GraphIngestResultDto {
    fn from(value: m::IngestOutcome) -> Self {
        Self {
            revision: value.revision.into(),
            replayed: value.replayed,
            counts: GraphIngestCountsDto {
                nodes_inserted: value.counts.nodes_inserted,
                nodes_updated: value.counts.nodes_updated,
                nodes_unchanged: value.counts.nodes_unchanged,
                edges_inserted: value.counts.edges_inserted,
                edges_updated: value.counts.edges_updated,
                edges_unchanged: value.counts.edges_unchanged,
                phantoms_created: value.counts.phantoms_created,
                phantoms_materialized: value.counts.phantoms_materialized,
                scope_removed_nodes: value.counts.scope_removed_nodes,
                scope_removed_edges: value.counts.scope_removed_edges,
            },
            per_item_nodes: value
                .per_item_nodes
                .map(|items| items.iter().map(item_outcome_name).collect()),
            per_item_edges: value
                .per_item_edges
                .map(|items| items.iter().map(item_outcome_name).collect()),
        }
    }
}

impl From<m::DeleteOutcome> for GraphDeleteResultDto {
    fn from(value: m::DeleteOutcome) -> Self {
        Self {
            revision: value.revision.into(),
            tombstoned_nodes: value.tombstoned_nodes,
            tombstoned_edges: value.tombstoned_edges,
        }
    }
}

impl From<m::AdjacencyEntry> for GraphAdjacencyEntryDto {
    fn from(value: m::AdjacencyEntry) -> Self {
        Self {
            edge_key: value.edge_key,
            edge_type_id: value.edge_type_id,
            direction: value.side.as_str().to_owned(),
            neighbor_key: value.neighbor_key,
            neighbor_type_id: value.neighbor_type_id,
        }
    }
}

impl From<m::Subject> for GraphSubjectDto {
    fn from(value: m::Subject) -> Self {
        Self {
            subject_id: value.subject_id.to_string(),
            subject_type: value.subject_type,
        }
    }
}

impl From<m::ElementEnvelope> for GraphElementEnvelopeDto {
    fn from(value: m::ElementEnvelope) -> Self {
        Self {
            tenant_id: value.tenant_id.to_string(),
            key: value.key,
            created_at: value.created_at,
            created_by: value.created_by.into(),
            updated_at: value.updated_at,
            updated_by: value.updated_by.into(),
            deleted_at: value.deleted_at,
            deleted_by: value.deleted_by.map(Into::into),
            graph_revision: value.graph_revision.into(),
        }
    }
}

impl From<m::NodeView> for GraphNodeDto {
    fn from(value: m::NodeView) -> Self {
        Self {
            node_key: value.node_key,
            type_id: value.type_id,
            name: value.name,
            payload: value.payload,
            has_embedding: value.has_embedding,
            adjacency: value.adjacency.into_iter().map(Into::into).collect(),
            adjacency_truncated: value.adjacency_truncated,
            envelope: value.envelope.into(),
        }
    }
}

impl From<m::EdgeView> for GraphEdgeDto {
    fn from(value: m::EdgeView) -> Self {
        Self {
            edge_key: value.edge_key,
            edge_type_id: value.edge_type_id,
            src: value.src,
            dst: value.dst,
            discriminator: value.discriminator,
            payload: value.payload,
            envelope: value.envelope.into(),
        }
    }
}

impl From<m::NodeRow> for GraphNodeRowDto {
    fn from(value: m::NodeRow) -> Self {
        Self {
            node_key: value.node_key,
            type_id: value.type_id,
            name: value.name,
            payload: value.payload,
            envelope: value.envelope.into(),
        }
    }
}

impl From<m::SearchHit> for GraphSearchHitDto {
    fn from(value: m::SearchHit) -> Self {
        Self {
            node_key: value.node_key,
            type_id: value.type_id,
            name: value.name,
            score: value.score,
            arms: value
                .arms
                .into_iter()
                .map(|arm| GraphArmHitDto {
                    arm: arm.arm.as_str().to_owned(),
                    rank: arm.rank,
                })
                .collect(),
            snippet: value.snippet,
        }
    }
}

impl From<m::SearchResponse> for GraphSearchResponseDto {
    fn from(value: m::SearchResponse) -> Self {
        Self {
            hits: value.hits.into_iter().map(Into::into).collect(),
            revision: value.revision.into(),
            truncated: value.truncated.map(truncation_name),
        }
    }
}

impl From<m::EdgeRef> for GraphEdgeRefDto {
    fn from(value: m::EdgeRef) -> Self {
        Self {
            edge_key: value.edge_key,
            edge_type_id: value.edge_type_id,
            src: value.src,
            dst: value.dst,
        }
    }
}

impl From<m::TraversalResponse> for GraphTraversalResponseDto {
    fn from(value: m::TraversalResponse) -> Self {
        Self {
            nodes: value.nodes.into_iter().map(Into::into).collect(),
            edges: value.edges.into_iter().map(Into::into).collect(),
            seeds: value.seeds,
            truncated: value.truncated.map(truncation_name),
            revision: value.revision.into(),
            consistent_snapshot: value.consistent_snapshot,
        }
    }
}

/// The wire name of a truncation reason, shared by every answer that can
/// carry one so two surfaces cannot spell the same cause differently.
fn truncation_name(reason: m::TruncationReason) -> String {
    reason.as_str().to_owned()
}