agent-sdk-core 0.1.0-alpha.3

Product-neutral primitive kernel and contracts for a Rust-first Agent SDK.
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
//! Domain primitives for stable SDK vocabulary. Use these items for IDs, refs,
//! policy, privacy, trust, and errors that cross crate or host boundaries. They are
//! data-only and must not perform provider, filesystem, network, or UI side effects.
//! This file contains the refs portion of that contract.
//!
use core::fmt;
use serde::{Deserialize, Deserializer, Serialize, de::Error as DeError};

use crate::ids::{
    AgentId, AgentPoolId, ApprovalRequestId, ArtifactId, AttemptId, ContextItemId,
    ContextProjectionId, CorrelationEntry, EffectId, EventId, IdValidationError, LineageId,
    MessageId, RunId, RuntimePackageId, ToolCallId, TopicId, TurnId, WakeConditionId,
    validate_identifier,
};
use crate::privacy::{PrivacyClass, RetentionClass, TrustClass};

#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite entity kind cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum EntityKind {
    /// Use this variant when the contract needs to represent run; selecting it has no side effect by itself.
    Run,
    /// Use this variant when the contract needs to represent turn; selecting it has no side effect by itself.
    Turn,
    /// Use this variant when the contract needs to represent attempt; selecting it has no side effect by itself.
    Attempt,
    /// Use this variant when the contract needs to represent agent; selecting it has no side effect by itself.
    Agent,
    /// Use this variant when the contract needs to represent agent pool; selecting it has no side effect by itself.
    AgentPool,
    /// Use this variant when the contract needs to represent topic; selecting it has no side effect by itself.
    Topic,
    /// Use this variant when the contract needs to represent event; selecting it has no side effect by itself.
    Event,
    /// Use this variant when the contract needs to represent message; selecting it has no side effect by itself.
    Message,
    /// Use this variant when the contract needs to represent wake condition; selecting it has no side effect by itself.
    WakeCondition,
    /// Use this variant when the contract needs to represent context contribution; selecting it has no side effect by itself.
    ContextContribution,
    /// Use this variant when the contract needs to represent context item; selecting it has no side effect by itself.
    ContextItem,
    /// Use this variant when the contract needs to represent context projection; selecting it has no side effect by itself.
    ContextProjection,
    /// Use this variant when the contract needs to represent content; selecting it has no side effect by itself.
    Content,
    /// Use this variant when the contract needs to represent artifact; selecting it has no side effect by itself.
    Artifact,
    /// Use this variant when the contract needs to represent capability; selecting it has no side effect by itself.
    Capability,
    /// Use this variant when the contract needs to represent package sidecar; selecting it has no side effect by itself.
    PackageSidecar,
    /// Use this variant when the contract needs to represent runtime package; selecting it has no side effect by itself.
    RuntimePackage,
    /// Use this variant when the contract needs to represent policy decision; selecting it has no side effect by itself.
    PolicyDecision,
    /// Use this variant when the contract needs to represent effect; selecting it has no side effect by itself.
    Effect,
    /// Use this variant when the contract needs to represent effect intent; selecting it has no side effect by itself.
    EffectIntent,
    /// Use this variant when the contract needs to represent effect result; selecting it has no side effect by itself.
    EffectResult,
    /// Use this variant when the contract needs to represent tool call; selecting it has no side effect by itself.
    ToolCall,
    /// Use this variant when the contract needs to represent approval request; selecting it has no side effect by itself.
    ApprovalRequest,
    /// Use this variant when the contract needs to represent stream rule; selecting it has no side effect by itself.
    StreamRule,
    /// Use this variant when the contract needs to represent realtime session; selecting it has no side effect by itself.
    RealtimeSession,
    /// Use this variant when the contract needs to represent hook; selecting it has no side effect by itself.
    Hook,
    /// Use this variant when the contract needs to represent execution environment; selecting it has no side effect by itself.
    ExecutionEnvironment,
    /// Use this variant when the contract needs to represent child artifact; selecting it has no side effect by itself.
    ChildArtifact,
    /// Use this variant when the contract needs to represent subagent run; selecting it has no side effect by itself.
    SubagentRun,
    /// Use this variant when the contract needs to represent extension action; selecting it has no side effect by itself.
    ExtensionAction,
    /// Use this variant when the contract needs to represent output delivery; selecting it has no side effect by itself.
    OutputDelivery,
}

#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
/// Defines the entity id SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct EntityId(String);

impl EntityId {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    ///
    /// # Panics
    ///
    /// Panics if constructor invariants fail, such as invalid identifier
    /// text or constructor-specific bounds. Use a fallible constructor such as
    /// `try_new` when one is available for untrusted input.
    pub fn new(value: impl Into<String>) -> Self {
        Self::try_new(value).expect("EntityId must be valid")
    }

    /// Creates a new domain::refs value after validation. Returns an
    /// SDK error instead of panicking when the identifier or input does
    /// not satisfy the contract.
    pub fn try_new(value: impl Into<String>) -> Result<Self, IdValidationError> {
        let value = value.into();
        validate_identifier(&value)?;
        Ok(Self(value))
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Debug for EntityId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("EntityId(redacted)")
    }
}

impl fmt::Display for EntityId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("EntityId(redacted)")
    }
}

impl From<&str> for EntityId {
    fn from(value: &str) -> Self {
        Self::try_new(value).expect("EntityId must be valid")
    }
}

impl<'de> Deserialize<'de> for EntityId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::try_new(value).map_err(D::Error::custom)
    }
}

impl From<RunId> for EntityId {
    fn from(value: RunId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<TurnId> for EntityId {
    fn from(value: TurnId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<AttemptId> for EntityId {
    fn from(value: AttemptId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<AgentId> for EntityId {
    fn from(value: AgentId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<AgentPoolId> for EntityId {
    fn from(value: AgentPoolId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<TopicId> for EntityId {
    fn from(value: TopicId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<EventId> for EntityId {
    fn from(value: EventId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<MessageId> for EntityId {
    fn from(value: MessageId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<WakeConditionId> for EntityId {
    fn from(value: WakeConditionId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<ContextItemId> for EntityId {
    fn from(value: ContextItemId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<ContextProjectionId> for EntityId {
    fn from(value: ContextProjectionId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<ArtifactId> for EntityId {
    fn from(value: ArtifactId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<RuntimePackageId> for EntityId {
    fn from(value: RuntimePackageId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<EffectId> for EntityId {
    fn from(value: EffectId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<ToolCallId> for EntityId {
    fn from(value: ToolCallId) -> Self {
        Self::new(value.as_str())
    }
}

impl From<ApprovalRequestId> for EntityId {
    fn from(value: ApprovalRequestId) -> Self {
        Self::new(value.as_str())
    }
}

#[derive(Clone, Deserialize, Eq, PartialEq, Serialize)]
/// Defines the entity ref SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct EntityRef {
    /// Kind/category for this record, capability, event, or detected
    /// resource.
    pub kind: EntityKind,
    /// Stable identifier for this record.
    pub id: EntityId,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Source label or ref for this item; it is metadata and does not fetch
    /// content by itself.
    pub source: Option<SourceRef>,
    /// Privacy class used for projection, telemetry, and raw-content access
    /// decisions.
    pub privacy: PrivacyClass,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Redacted human-readable summary safe for events, telemetry, and logs.
    pub redacted_summary: Option<String>,
}

impl EntityRef {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    pub fn new(kind: EntityKind, id: impl Into<EntityId>) -> Self {
        Self {
            kind,
            id: id.into(),
            source: None,
            privacy: PrivacyClass::ContentRefsOnly,
            redacted_summary: None,
        }
    }

    /// Builds the run value with the documented defaults.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn run(id: RunId) -> Self {
        Self::new(EntityKind::Run, id)
    }

    /// Returns agent for the current value.
    /// This is a read-only or data-construction helper unless the method body explicitly calls
    /// a port or store.
    pub fn agent(id: AgentId) -> Self {
        Self::new(EntityKind::Agent, id)
    }

    /// Builds the agent pool value.
    /// This is data construction and performs no I/O, journal append, event publication, or
    /// process work.
    pub fn agent_pool(id: AgentPoolId) -> Self {
        Self::new(EntityKind::AgentPool, id)
    }

    /// Returns an updated value with topic configured.
    /// This is data-only and does not perform I/O, call host ports, append journals, publish
    /// events, or start processes.
    pub fn topic(id: TopicId) -> Self {
        Self::new(EntityKind::Topic, id)
    }

    /// Builds the message value.
    /// This is data construction and performs no I/O, journal append, event publication, or
    /// process work.
    pub fn message(id: MessageId) -> Self {
        Self::new(EntityKind::Message, id)
    }

    /// Builds the wake condition value.
    /// This is data construction and performs no I/O, journal append, event publication, or
    /// process work.
    pub fn wake_condition(id: WakeConditionId) -> Self {
        Self::new(EntityKind::WakeCondition, id)
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        self.id.as_str()
    }
}

impl fmt::Debug for EntityRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("EntityRef")
            .field("kind", &self.kind)
            .field("id", &"redacted")
            .field("source", &self.source)
            .field("privacy", &self.privacy)
            .field("redacted_summary", &self.redacted_summary)
            .finish()
    }
}

impl fmt::Display for EntityRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{:?}:redacted", self.kind)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite source kind cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum SourceKind {
    /// Use this variant when the contract needs to represent sdk; selecting it has no side effect by itself.
    Sdk,
    /// Use this variant when the contract needs to represent host; selecting it has no side effect by itself.
    Host,
    /// Use this variant when the contract needs to represent user; selecting it has no side effect by itself.
    User,
    /// Use this variant when the contract needs to represent system; selecting it has no side effect by itself.
    System,
    /// Use this variant when the contract needs to represent developer; selecting it has no side effect by itself.
    Developer,
    /// Use this variant when the contract needs to represent memory; selecting it has no side effect by itself.
    Memory,
    /// Use this variant when the contract needs to represent compaction; selecting it has no side effect by itself.
    Compaction,
    /// Use this variant when the contract needs to represent hook; selecting it has no side effect by itself.
    Hook,
    /// Use this variant when the contract needs to represent extension; selecting it has no side effect by itself.
    Extension,
    /// Use this variant when the contract needs to represent tool; selecting it has no side effect by itself.
    Tool,
    /// Use this variant when the contract needs to represent remote channel; selecting it has no side effect by itself.
    RemoteChannel,
    /// Use this variant when the contract needs to represent scheduled task; selecting it has no side effect by itself.
    ScheduledTask,
    /// Use this variant when the contract needs to represent subagent; selecting it has no side effect by itself.
    Subagent,
    /// Use this variant when the contract needs to represent external runtime; selecting it has no side effect by itself.
    ExternalRuntime,
    /// Use this variant when the contract needs to represent replay; selecting it has no side effect by itself.
    Replay,
}

#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
/// Defines the source id SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct SourceId(String);

impl SourceId {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    ///
    /// # Panics
    ///
    /// Panics if constructor invariants fail, such as invalid identifier
    /// text or constructor-specific bounds. Use a fallible constructor such as
    /// `try_new` when one is available for untrusted input.
    pub fn new(value: impl Into<String>) -> Self {
        Self::try_new(value).expect("SourceId must be valid")
    }

    /// Creates a new domain::refs value after validation. Returns an
    /// SDK error instead of panicking when the identifier or input does
    /// not satisfy the contract.
    pub fn try_new(value: impl Into<String>) -> Result<Self, IdValidationError> {
        let value = value.into();
        validate_identifier(&value)?;
        Ok(Self(value))
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl<'de> Deserialize<'de> for SourceId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::try_new(value).map_err(D::Error::custom)
    }
}

impl fmt::Debug for SourceId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("SourceId(redacted)")
    }
}

impl fmt::Display for SourceId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("SourceId(redacted)")
    }
}

#[derive(Clone, Deserialize, Eq, PartialEq, Serialize)]
/// Defines the source ref SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct SourceRef {
    /// Kind/category for this record, capability, event, or detected
    /// resource.
    pub kind: SourceKind,
    /// Stable identifier for this record.
    pub id: SourceId,
    /// Trust class used when deciding whether context or capabilities may be
    /// admitted.
    pub trust: TrustClass,
    /// Privacy class used for projection, telemetry, and raw-content access
    /// decisions.
    pub privacy: PrivacyClass,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    /// Collection of correlation values.
    /// Ordering and membership should be treated as part of the serialized contract when
    /// relevant.
    pub correlation: Vec<CorrelationEntry>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Redacted human-readable summary safe for events, telemetry, and logs.
    pub redacted_summary: Option<String>,
}

impl SourceRef {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    pub fn new(id: impl Into<String>) -> Self {
        Self::with_kind(SourceKind::Host, id)
    }

    /// Returns this value with its kind setting replaced. The method
    /// follows builder-style data construction and does not execute
    /// external work.
    pub fn with_kind(kind: SourceKind, id: impl Into<String>) -> Self {
        Self {
            kind,
            id: SourceId::new(id),
            trust: TrustClass::HostProvided,
            privacy: PrivacyClass::ContentRefsOnly,
            correlation: Vec::new(),
            redacted_summary: None,
        }
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        self.id.as_str()
    }
}

impl fmt::Debug for SourceRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("SourceRef")
            .field("kind", &self.kind)
            .field("id", &"redacted")
            .field("trust", &self.trust)
            .field("privacy", &self.privacy)
            .field("correlation", &self.correlation)
            .field("redacted_summary", &self.redacted_summary)
            .finish()
    }
}

impl fmt::Display for SourceRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{:?}:redacted", self.kind)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite destination kind cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum DestinationKind {
    /// Use this variant when the contract needs to represent agent; selecting it has no side effect by itself.
    Agent,
    /// Use this variant when the contract needs to represent agent pool; selecting it has no side effect by itself.
    AgentPool,
    /// Use this variant when the contract needs to represent topic; selecting it has no side effect by itself.
    Topic,
    /// Use this variant when the contract needs to represent provider; selecting it has no side effect by itself.
    Provider,
    /// Use this variant when the contract needs to represent context projection; selecting it has no side effect by itself.
    ContextProjection,
    /// Use this variant when the contract needs to represent journal; selecting it has no side effect by itself.
    Journal,
    /// Use this variant when the contract needs to represent event stream; selecting it has no side effect by itself.
    EventStream,
    /// Use this variant when the contract needs to represent telemetry; selecting it has no side effect by itself.
    Telemetry,
    /// Use this variant when the contract needs to represent output sink; selecting it has no side effect by itself.
    OutputSink,
    /// Use this variant when the contract needs to represent host; selecting it has no side effect by itself.
    Host,
    /// Use this variant when the contract needs to represent user; selecting it has no side effect by itself.
    User,
    /// Use this variant when the contract needs to represent remote channel; selecting it has no side effect by itself.
    RemoteChannel,
    /// Use this variant when the contract needs to represent tool; selecting it has no side effect by itself.
    Tool,
    /// Use this variant when the contract needs to represent extension; selecting it has no side effect by itself.
    Extension,
    /// Use this variant when the contract needs to represent subagent; selecting it has no side effect by itself.
    Subagent,
    /// Use this variant when the contract needs to represent external runtime; selecting it has no side effect by itself.
    ExternalRuntime,
}

#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
/// Defines the destination id SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct DestinationId(String);

impl DestinationId {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    ///
    /// # Panics
    ///
    /// Panics if constructor invariants fail, such as invalid identifier
    /// text or constructor-specific bounds. Use a fallible constructor such as
    /// `try_new` when one is available for untrusted input.
    pub fn new(value: impl Into<String>) -> Self {
        Self::try_new(value).expect("DestinationId must be valid")
    }

    /// Creates a new domain::refs value after validation. Returns an
    /// SDK error instead of panicking when the identifier or input does
    /// not satisfy the contract.
    pub fn try_new(value: impl Into<String>) -> Result<Self, IdValidationError> {
        let value = value.into();
        validate_identifier(&value)?;
        Ok(Self(value))
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl<'de> Deserialize<'de> for DestinationId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::try_new(value).map_err(D::Error::custom)
    }
}

impl fmt::Debug for DestinationId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("DestinationId(redacted)")
    }
}

impl fmt::Display for DestinationId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("DestinationId(redacted)")
    }
}

#[derive(Clone, Deserialize, Eq, PartialEq, Serialize)]
/// Defines the destination ref SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct DestinationRef {
    /// Kind/category for this record, capability, event, or detected
    /// resource.
    pub kind: DestinationKind,
    /// Stable identifier for this record.
    pub id: DestinationId,
    /// Privacy class used for projection, telemetry, and raw-content access
    /// decisions.
    pub privacy: PrivacyClass,
    /// Retention class used by hosts and sinks when storing or exporting this
    /// item.
    pub retention: RetentionClass,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    /// Collection of correlation values.
    /// Ordering and membership should be treated as part of the serialized contract when
    /// relevant.
    pub correlation: Vec<CorrelationEntry>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Redacted human-readable summary safe for events, telemetry, and logs.
    pub redacted_summary: Option<String>,
}

impl DestinationRef {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    pub fn new(id: impl Into<String>) -> Self {
        Self::with_kind(DestinationKind::Host, id)
    }

    /// Returns this value with its kind setting replaced. The method
    /// follows builder-style data construction and does not execute
    /// external work.
    pub fn with_kind(kind: DestinationKind, id: impl Into<String>) -> Self {
        Self {
            kind,
            id: DestinationId::new(id),
            privacy: PrivacyClass::ContentRefsOnly,
            retention: RetentionClass::HostPolicy,
            correlation: Vec::new(),
            redacted_summary: None,
        }
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        self.id.as_str()
    }
}

impl fmt::Debug for DestinationRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DestinationRef")
            .field("kind", &self.kind)
            .field("id", &"redacted")
            .field("privacy", &self.privacy)
            .field("retention", &self.retention)
            .field("correlation", &self.correlation)
            .field("redacted_summary", &self.redacted_summary)
            .finish()
    }
}

impl fmt::Display for DestinationRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{:?}:redacted", self.kind)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
/// Enumerates the finite policy kind cases.
/// Serialized names are part of the SDK contract; update fixtures when variants change.
pub enum PolicyKind {
    /// Use this variant when the contract needs to represent approval; selecting it has no side effect by itself.
    Approval,
    /// Use this variant when the contract needs to represent permission; selecting it has no side effect by itself.
    Permission,
    /// Use this variant when the contract needs to represent sandbox; selecting it has no side effect by itself.
    Sandbox,
    /// Use this variant when the contract needs to represent context; selecting it has no side effect by itself.
    Context,
    /// Use this variant when the contract needs to represent privacy; selecting it has no side effect by itself.
    Privacy,
    /// Use this variant when the contract needs to represent retention; selecting it has no side effect by itself.
    Retention,
    /// Use this variant when the contract needs to represent redaction; selecting it has no side effect by itself.
    Redaction,
    /// Use this variant when the contract needs to represent runtime package; selecting it has no side effect by itself.
    RuntimePackage,
    /// Use this variant when the contract needs to represent host; selecting it has no side effect by itself.
    Host,
}

#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
/// Defines the policy id SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct PolicyId(String);

impl PolicyId {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    ///
    /// # Panics
    ///
    /// Panics if constructor invariants fail, such as invalid identifier
    /// text or constructor-specific bounds. Use a fallible constructor such as
    /// `try_new` when one is available for untrusted input.
    pub fn new(value: impl Into<String>) -> Self {
        Self::try_new(value).expect("PolicyId must be valid")
    }

    /// Creates a new domain::refs value after validation. Returns an
    /// SDK error instead of panicking when the identifier or input does
    /// not satisfy the contract.
    pub fn try_new(value: impl Into<String>) -> Result<Self, IdValidationError> {
        let value = value.into();
        validate_identifier(&value)?;
        Ok(Self(value))
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl<'de> Deserialize<'de> for PolicyId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::try_new(value).map_err(D::Error::custom)
    }
}

#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
/// Defines the adapter ref SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct AdapterRef(String);

impl AdapterRef {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    ///
    /// # Panics
    ///
    /// Panics if constructor invariants fail, such as invalid identifier
    /// text or constructor-specific bounds. Use a fallible constructor such as
    /// `try_new` when one is available for untrusted input.
    pub fn new(value: impl Into<String>) -> Self {
        Self::try_new(value).expect("AdapterRef must be valid")
    }

    /// Creates a new domain::refs value after validation. Returns an
    /// SDK error instead of panicking when the identifier or input does
    /// not satisfy the contract.
    pub fn try_new(value: impl Into<String>) -> Result<Self, IdValidationError> {
        let value = value.into();
        validate_identifier(&value)?;
        Ok(Self(value))
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Debug for AdapterRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("AdapterRef(redacted)")
    }
}

impl fmt::Display for AdapterRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("AdapterRef(redacted)")
    }
}

impl From<&str> for AdapterRef {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

impl<'de> Deserialize<'de> for AdapterRef {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::try_new(value).map_err(D::Error::custom)
    }
}

impl fmt::Debug for PolicyId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("PolicyId(redacted)")
    }
}

impl fmt::Display for PolicyId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("PolicyId(redacted)")
    }
}

#[derive(Clone, Deserialize, Eq, PartialEq, Serialize)]
/// Defines the policy ref SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct PolicyRef {
    /// Kind/category for this record, capability, event, or detected
    /// resource.
    pub kind: PolicyKind,
    /// Stable identifier for this record.
    pub id: PolicyId,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Version string for this capability, package, or protocol surface.
    /// Use it for compatibility checks during package or adapter resolution.
    pub version: Option<String>,
}

impl PolicyRef {
    /// Creates a new domain::refs value with explicit caller-provided
    /// inputs. This constructor is data-only and performs no I/O or
    /// external side effects.
    pub fn new(id: impl Into<String>) -> Self {
        Self::with_kind(PolicyKind::Host, id)
    }

    /// Returns this value with its kind setting replaced. The method
    /// follows builder-style data construction and does not execute
    /// external work.
    pub fn with_kind(kind: PolicyKind, id: impl Into<String>) -> Self {
        Self {
            kind,
            id: PolicyId::new(id),
            version: None,
        }
    }

    /// Returns this value as str. The accessor is side-effect free and
    /// keeps ownership with the caller.
    pub fn as_str(&self) -> &str {
        self.id.as_str()
    }
}

impl fmt::Debug for PolicyRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PolicyRef")
            .field("kind", &self.kind)
            .field("id", &"redacted")
            .field("version", &self.version)
            .finish()
    }
}

impl fmt::Display for PolicyRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{:?}:redacted", self.kind)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
/// Defines the lineage ref SDK value.
/// Construction records local state only; documented runtimes, executors, or ports own side effects.
pub struct LineageRef {
    /// Stable lineage id used for typed lineage, lookup, or dedupe.
    pub lineage_id: LineageId,
    /// Source label or ref for this item; it is metadata and does not fetch
    /// content by itself.
    pub source: SourceRef,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Destination label or ref for this item; it is metadata and does not
    /// deliver content by itself.
    pub destination: Option<DestinationRef>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    /// Policy references that govern admission, projection, execution, or
    /// delivery.
    pub policy_refs: Vec<PolicyRef>,
}