lean_ctx/core/context_checkpoint/adapters/
validation.rs1use super::{
6 ContextCheckpointLegacyErrorV1, ContextCheckpointLegacyFieldV1, ContextCheckpointLegacyHashV1,
7 ContextCheckpointLegacyLabelV1, ContextCheckpointLegacyProjectIdentityV1,
8 ContextCheckpointLegacyRefusalV1, ContextCheckpointProjectionInputV1,
9 ContextCheckpointProjectionTargetV1,
10};
11use chrono::DateTime;
12use lean_ctx_protocol::UtcTimestamp;
13
14pub(super) fn require_capacity(
15 actual: usize,
16 limit: usize,
17 field: ContextCheckpointLegacyFieldV1,
18) -> Result<(), ContextCheckpointLegacyErrorV1> {
19 if actual > limit {
20 return Err(ContextCheckpointLegacyErrorV1::LegacyCapacityExceeded {
21 field,
22 limit: limit as u64,
23 actual: actual as u64,
24 });
25 }
26 Ok(())
27}
28
29pub(super) fn require_arity(
30 actual: usize,
31 expected: usize,
32 field: ContextCheckpointLegacyFieldV1,
33) -> Result<(), ContextCheckpointLegacyErrorV1> {
34 if actual != expected {
35 return Err(ContextCheckpointLegacyErrorV1::InputArityMismatch {
36 field,
37 expected: expected as u32,
38 actual: actual as u32,
39 });
40 }
41 Ok(())
42}
43
44pub(super) fn projection_input_field_path(
45 input: ContextCheckpointProjectionInputV1,
46) -> &'static str {
47 match input {
48 ContextCheckpointProjectionInputV1::SessionBundleExportedAt
49 | ContextCheckpointProjectionInputV1::HandoffBundleExportedAt => "target.exported_at",
50 ContextCheckpointProjectionInputV1::SessionBundlePolicyIdentities => "target.role_profile",
51 ContextCheckpointProjectionInputV1::SessionBundleSessionExcerpt
52 | ContextCheckpointProjectionInputV1::ContextSnapshotSession => "target.session",
53 ContextCheckpointProjectionInputV1::HandoffBundleLedger
54 | ContextCheckpointProjectionInputV1::ContextSnapshotLedger => "target.ledger",
55 ContextCheckpointProjectionInputV1::HandoffBundleProjectIdentity => "target.project",
56 ContextCheckpointProjectionInputV1::CtxpkgManifest => "target.manifest",
57 ContextCheckpointProjectionInputV1::CtxpkgLogicalState => "target.logical_state",
58 ContextCheckpointProjectionInputV1::CtxpkgIntegrity => "target.integrity",
59 ContextCheckpointProjectionInputV1::ContextSnapshotCreatedAt => "target.created_at",
60 ContextCheckpointProjectionInputV1::ContextSnapshotGitAnchor => "target.git",
61 ContextCheckpointProjectionInputV1::ContextSnapshotRoi => "target.roi",
62 ContextCheckpointProjectionInputV1::ContextSnapshotLineage => "target.lineage",
63 }
64}
65
66pub(super) fn projection_input_target(
67 input: ContextCheckpointProjectionInputV1,
68) -> ContextCheckpointProjectionTargetV1 {
69 match input {
70 ContextCheckpointProjectionInputV1::SessionBundleExportedAt
71 | ContextCheckpointProjectionInputV1::SessionBundlePolicyIdentities
72 | ContextCheckpointProjectionInputV1::SessionBundleSessionExcerpt => {
73 ContextCheckpointProjectionTargetV1::SessionBundle
74 }
75 ContextCheckpointProjectionInputV1::HandoffBundleExportedAt
76 | ContextCheckpointProjectionInputV1::HandoffBundleLedger
77 | ContextCheckpointProjectionInputV1::HandoffBundleProjectIdentity => {
78 ContextCheckpointProjectionTargetV1::HandoffBundle
79 }
80 ContextCheckpointProjectionInputV1::CtxpkgManifest
81 | ContextCheckpointProjectionInputV1::CtxpkgLogicalState
82 | ContextCheckpointProjectionInputV1::CtxpkgIntegrity => {
83 ContextCheckpointProjectionTargetV1::Ctxpkg
84 }
85 ContextCheckpointProjectionInputV1::ContextSnapshotCreatedAt
86 | ContextCheckpointProjectionInputV1::ContextSnapshotGitAnchor
87 | ContextCheckpointProjectionInputV1::ContextSnapshotRoi
88 | ContextCheckpointProjectionInputV1::ContextSnapshotLineage
89 | ContextCheckpointProjectionInputV1::ContextSnapshotLedger
90 | ContextCheckpointProjectionInputV1::ContextSnapshotSession => {
91 ContextCheckpointProjectionTargetV1::ContextSnapshot
92 }
93 }
94}
95
96impl ContextCheckpointLegacyRefusalV1 {
97 pub fn validate(&self) -> Result<(), ContextCheckpointLegacyErrorV1> {
99 if !self
100 .target
101 .accepts_schema_version(self.target_schema_version)
102 {
103 return Err(ContextCheckpointLegacyErrorV1::UnsupportedTargetVersion {
104 target: self.target,
105 requested: self.target_schema_version,
106 supported: self.target.schema_version(),
107 });
108 }
109 if self.dependency.is_empty()
110 || self.dependency.len() > 160
111 || self.dependency.chars().any(char::is_control)
112 {
113 return Err(ContextCheckpointLegacyErrorV1::InvalidInputs {
114 field: ContextCheckpointLegacyFieldV1::Payload,
115 });
116 }
117 if self
118 .required_inputs
119 .iter()
120 .any(|input| projection_input_target(*input) != self.target.projection_target())
121 {
122 return Err(ContextCheckpointLegacyErrorV1::TargetInputsMismatch {
123 target: self.target,
124 inputs_target: self.target,
125 });
126 }
127 if self
128 .required_inputs
129 .windows(2)
130 .any(|window| window[0] >= window[1])
131 {
132 return Err(ContextCheckpointLegacyErrorV1::DuplicateLegacyInput {
133 field: ContextCheckpointLegacyFieldV1::Payload,
134 });
135 }
136 Ok(())
137 }
138}
139
140pub(super) fn validate_project_identity(
141 project: &ContextCheckpointLegacyProjectIdentityV1,
142) -> Result<(), ContextCheckpointLegacyErrorV1> {
143 if let Some(hash) = &project.project_root_hash {
144 ContextCheckpointLegacyHashV1::new(hash.as_str().to_owned())?;
145 }
146 if let Some(hash) = &project.project_identity_hash {
147 ContextCheckpointLegacyHashV1::new(hash.as_str().to_owned())?;
148 }
149 Ok(())
150}
151
152pub(super) fn validate_optional_label(
153 value: Option<&ContextCheckpointLegacyLabelV1>,
154) -> Result<(), ContextCheckpointLegacyErrorV1> {
155 if let Some(value) = value {
156 ContextCheckpointLegacyLabelV1::new(value.as_str().to_owned())?;
157 }
158 Ok(())
159}
160
161pub(super) fn validate_timestamp(
162 value: &UtcTimestamp,
163 field: ContextCheckpointLegacyFieldV1,
164) -> Result<(), ContextCheckpointLegacyErrorV1> {
165 DateTime::parse_from_rfc3339(value.as_str())
166 .map(|_| ())
167 .map_err(|_| ContextCheckpointLegacyErrorV1::InvalidInputs { field })
168}
169
170pub(super) fn validate_sorted_unique_timestamps(
171 values: &[UtcTimestamp],
172 field: ContextCheckpointLegacyFieldV1,
173) -> Result<(), ContextCheckpointLegacyErrorV1> {
174 if values.windows(2).any(|window| window[0] >= window[1]) {
175 return Err(ContextCheckpointLegacyErrorV1::DuplicateLegacyInput { field });
176 }
177 Ok(())
178}