use super::{
CCP_SESSION_BUNDLE_V1_SCHEMA_VERSION, CONTEXT_CHECKPOINT_ADAPTER_SCHEMA_VERSION,
CONTEXT_PACKAGE_V1_SCHEMA_VERSION, CONTEXT_PACKAGE_V2_SCHEMA_VERSION,
CONTEXT_SNAPSHOT_V1_SCHEMA_VERSION, ContextCheckpointProjectionInputV1,
ContextCheckpointProjectionLossReasonV1, ContextCheckpointProjectionLossV1,
ContextCheckpointProjectionTargetV1, ContextCheckpointV1,
HANDOFF_TRANSFER_BUNDLE_V1_SCHEMA_VERSION, LEGACY_HASH_HEX_LENGTH,
MAX_CONTEXT_CHECKPOINT_LEGACY_LOSSES, MAX_CONTEXT_CHECKPOINT_LEGACY_PAYLOAD_BYTES,
MAX_CONTEXT_CHECKPOINT_REQUIRED_INPUTS, MAX_LEGACY_LABEL_BYTES, parse_canonical_json,
projection_input_field_path, projection_input_target, validate_legacy_target_value,
};
use serde::{Deserialize, Deserializer, Serialize, de::Error as DeError};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextCheckpointLegacyTargetV1 {
SessionBundle,
HandoffBundle,
Ctxpkg,
ContextSnapshot,
}
impl ContextCheckpointLegacyTargetV1 {
pub const fn all() -> [Self; 4] {
[
Self::SessionBundle,
Self::HandoffBundle,
Self::Ctxpkg,
Self::ContextSnapshot,
]
}
pub const fn projection_target(self) -> ContextCheckpointProjectionTargetV1 {
match self {
Self::SessionBundle => ContextCheckpointProjectionTargetV1::SessionBundle,
Self::HandoffBundle => ContextCheckpointProjectionTargetV1::HandoffBundle,
Self::Ctxpkg => ContextCheckpointProjectionTargetV1::Ctxpkg,
Self::ContextSnapshot => ContextCheckpointProjectionTargetV1::ContextSnapshot,
}
}
pub const fn target_id(self) -> &'static str {
self.projection_target().target_id()
}
pub const fn schema_version(self) -> u32 {
match self {
Self::SessionBundle => CCP_SESSION_BUNDLE_V1_SCHEMA_VERSION,
Self::HandoffBundle => HANDOFF_TRANSFER_BUNDLE_V1_SCHEMA_VERSION,
Self::Ctxpkg => CONTEXT_PACKAGE_V2_SCHEMA_VERSION,
Self::ContextSnapshot => CONTEXT_SNAPSHOT_V1_SCHEMA_VERSION,
}
}
pub fn accepts_schema_version(self, version: u32) -> bool {
match self {
Self::Ctxpkg => {
version == CONTEXT_PACKAGE_V1_SCHEMA_VERSION
|| version == CONTEXT_PACKAGE_V2_SCHEMA_VERSION
}
_ => version == self.schema_version(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextCheckpointLegacyFieldV1 {
Payload,
SourceIdentity,
TaskTitle,
Decisions,
Findings,
NextSteps,
DecisionObservedAt,
FindingObservedAt,
SessionId,
SessionRevision,
Evidence,
Files,
Progress,
TestResults,
ToolCalls,
Knowledge,
CuratedRefs,
Artifacts,
Workflow,
SessionSnapshot,
ActiveOverlays,
CarrierBinding,
CarrierSchemaId,
CarrierWorkspaceId,
CarrierStateDigest,
CarrierEnvelopeDigest,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum ContextCheckpointLegacyErrorV1 {
UnsupportedAdapterVersion { actual: u32, expected: u32 },
UnsupportedSourceVersion { actual: u32, expected: u32 },
UnsupportedTargetVersion {
target: ContextCheckpointLegacyTargetV1,
requested: u32,
supported: u32,
},
TargetInputsMismatch {
target: ContextCheckpointLegacyTargetV1,
inputs_target: ContextCheckpointLegacyTargetV1,
},
InvalidSource,
InvalidInputs {
field: ContextCheckpointLegacyFieldV1,
},
InputArityMismatch {
field: ContextCheckpointLegacyFieldV1,
expected: u32,
actual: u32,
},
LegacyCapacityExceeded {
field: ContextCheckpointLegacyFieldV1,
limit: u64,
actual: u64,
},
IdentityMismatch {
field: ContextCheckpointLegacyFieldV1,
},
MalformedSignature,
LossyReverseConversion {
field: ContextCheckpointLegacyFieldV1,
},
SemanticOverwrite {
field: ContextCheckpointLegacyFieldV1,
},
MissingLegacySource {
field: ContextCheckpointLegacyFieldV1,
},
DuplicateLegacyInput {
field: ContextCheckpointLegacyFieldV1,
},
InvalidLegacyPayload {
field: ContextCheckpointLegacyFieldV1,
},
LegacyOwnerRejected,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextCheckpointLegacyRefusalReasonV1 {
TargetOwnerDependency,
MissingSessionState,
MissingCarrierBinding,
ReverseNotRepresentable,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacyRefusalV1 {
pub target: ContextCheckpointLegacyTargetV1,
pub target_schema_version: u32,
pub reason: ContextCheckpointLegacyRefusalReasonV1,
pub dependency: String,
pub required_inputs: Vec<ContextCheckpointProjectionInputV1>,
}
validated_deserialize!(ContextCheckpointLegacyRefusalV1 {
target: ContextCheckpointLegacyTargetV1,
target_schema_version: u32,
reason: ContextCheckpointLegacyRefusalReasonV1,
dependency: String,
required_inputs: Vec<ContextCheckpointProjectionInputV1>,
});
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacySourceIdentityV1 {
pub checkpoint_id: lean_ctx_protocol::ContextCheckpointIdV1,
pub tenant_id: lean_ctx_protocol::TenantId,
pub project_id: lean_ctx_protocol::ProjectId,
pub workspace_id: lean_ctx_protocol::WorkspaceId,
pub task_id: lean_ctx_protocol::TaskId,
pub plan_id: Option<lean_ctx_protocol::PlanId>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacyPayloadV1 {
pub adapter_schema_version: u32,
pub source_identity: ContextCheckpointLegacySourceIdentityV1,
pub target: ContextCheckpointLegacyTargetV1,
pub target_schema_version: u32,
pub canonical_json: String,
pub payload_content_hash: ContextCheckpointLegacyHashV1,
pub losses: Vec<ContextCheckpointProjectionLossV1>,
}
impl ContextCheckpointLegacyPayloadV1 {
pub fn validate(&self) -> Result<(), ContextCheckpointLegacyErrorV1> {
if self.adapter_schema_version != CONTEXT_CHECKPOINT_ADAPTER_SCHEMA_VERSION {
return Err(ContextCheckpointLegacyErrorV1::UnsupportedAdapterVersion {
actual: self.adapter_schema_version,
expected: CONTEXT_CHECKPOINT_ADAPTER_SCHEMA_VERSION,
});
}
if !self
.target
.accepts_schema_version(self.target_schema_version)
{
return Err(ContextCheckpointLegacyErrorV1::UnsupportedTargetVersion {
target: self.target,
requested: self.target_schema_version,
supported: self.target.schema_version(),
});
}
if self.losses.len() > MAX_CONTEXT_CHECKPOINT_LEGACY_LOSSES {
return Err(ContextCheckpointLegacyErrorV1::LegacyCapacityExceeded {
field: ContextCheckpointLegacyFieldV1::Payload,
limit: MAX_CONTEXT_CHECKPOINT_LEGACY_LOSSES as u64,
actual: self.losses.len() as u64,
});
}
if self
.losses
.iter()
.filter(|loss| loss.reason == ContextCheckpointProjectionLossReasonV1::RequiredInput)
.count()
> MAX_CONTEXT_CHECKPOINT_REQUIRED_INPUTS
{
return Err(ContextCheckpointLegacyErrorV1::LegacyCapacityExceeded {
field: ContextCheckpointLegacyFieldV1::Payload,
limit: MAX_CONTEXT_CHECKPOINT_REQUIRED_INPUTS as u64,
actual: self.losses.len() as u64,
});
}
if self.canonical_json.len() > MAX_CONTEXT_CHECKPOINT_LEGACY_PAYLOAD_BYTES {
return Err(ContextCheckpointLegacyErrorV1::LegacyCapacityExceeded {
field: ContextCheckpointLegacyFieldV1::Payload,
limit: MAX_CONTEXT_CHECKPOINT_LEGACY_PAYLOAD_BYTES as u64,
actual: self.canonical_json.len() as u64,
});
}
let value = parse_canonical_json(&self.canonical_json)?;
validate_legacy_target_value(self.target, self.target_schema_version, &value)?;
if crate::core::hasher::hash_hex(self.canonical_json.as_bytes())
!= self.payload_content_hash.as_str()
{
return Err(ContextCheckpointLegacyErrorV1::InvalidLegacyPayload {
field: ContextCheckpointLegacyFieldV1::Payload,
});
}
if self.losses.windows(2).any(|window| window[0] >= window[1]) {
return Err(ContextCheckpointLegacyErrorV1::DuplicateLegacyInput {
field: ContextCheckpointLegacyFieldV1::Payload,
});
}
for loss in &self.losses {
if loss.field_path.is_empty()
|| loss.field_path.len() > 128
|| !loss.field_path.bytes().all(|byte| {
byte.is_ascii_lowercase()
|| byte.is_ascii_digit()
|| matches!(byte, b'.' | b'_' | b'[' | b']' | b'*')
})
{
return Err(ContextCheckpointLegacyErrorV1::InvalidLegacyPayload {
field: ContextCheckpointLegacyFieldV1::Payload,
});
}
match loss.reason {
ContextCheckpointProjectionLossReasonV1::NotRepresentable
if loss.required_input.is_none() => {}
ContextCheckpointProjectionLossReasonV1::RequiredInput => {
let Some(input) = loss.required_input else {
return Err(ContextCheckpointLegacyErrorV1::InvalidLegacyPayload {
field: ContextCheckpointLegacyFieldV1::Payload,
});
};
if projection_input_target(input) != self.target.projection_target()
|| projection_input_field_path(input) != loss.field_path
{
return Err(ContextCheckpointLegacyErrorV1::InvalidLegacyPayload {
field: ContextCheckpointLegacyFieldV1::Payload,
});
}
}
ContextCheckpointProjectionLossReasonV1::NotRepresentable => {
return Err(ContextCheckpointLegacyErrorV1::InvalidLegacyPayload {
field: ContextCheckpointLegacyFieldV1::Payload,
});
}
}
}
Ok(())
}
}
validated_deserialize!(ContextCheckpointLegacyPayloadV1 {
adapter_schema_version: u32,
source_identity: ContextCheckpointLegacySourceIdentityV1,
target: ContextCheckpointLegacyTargetV1,
target_schema_version: u32,
canonical_json: String,
payload_content_hash: ContextCheckpointLegacyHashV1,
losses: Vec<ContextCheckpointProjectionLossV1>,
});
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextCheckpointLegacyMaterializationV1 {
Materialized(ContextCheckpointLegacyPayloadV1),
Refused(ContextCheckpointLegacyRefusalV1),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContextCheckpointLegacyAdoptionV1 {
Adopted(Box<ContextCheckpointV1>),
Refused(ContextCheckpointLegacyRefusalV1),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct ContextCheckpointLegacyHashV1(String);
impl ContextCheckpointLegacyHashV1 {
pub fn new(value: impl Into<String>) -> Result<Self, ContextCheckpointLegacyErrorV1> {
let value = value.into();
if value.len() != LEGACY_HASH_HEX_LENGTH || !is_lowercase_hex(&value) {
return Err(ContextCheckpointLegacyErrorV1::InvalidInputs {
field: ContextCheckpointLegacyFieldV1::Payload,
});
}
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl<'de> Deserialize<'de> for ContextCheckpointLegacyHashV1 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
Self::new(value).map_err(|error| DeError::custom(format!("{error:?}")))
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct ContextCheckpointLegacyLabelV1(String);
impl ContextCheckpointLegacyLabelV1 {
pub fn new(value: impl Into<String>) -> Result<Self, ContextCheckpointLegacyErrorV1> {
let value = value.into();
let lower = value.to_ascii_lowercase();
let invalid = value.is_empty()
|| value.len() > MAX_LEGACY_LABEL_BYTES
|| value.starts_with(' ')
|| value.ends_with(' ')
|| !value.bytes().all(|byte| (0x20..0x7f).contains(&byte))
|| contains_machine_local_or_secret_label(&lower);
if invalid {
return Err(ContextCheckpointLegacyErrorV1::InvalidInputs {
field: ContextCheckpointLegacyFieldV1::Payload,
});
}
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
fn contains_machine_local_or_secret_label(value: &str) -> bool {
const ASSIGNMENTS: [&str; 14] = [
"api_key=",
"authorization:",
"bearer ",
"cache_handle=",
"client_secret=",
"password=",
"passwd=",
"path=",
"pid=",
"provider_handle=",
"temp_dir=",
"tmpdir=",
"token=",
"-----begin",
];
ASSIGNMENTS.iter().any(|marker| value.contains(marker))
|| value.contains("://")
|| value.split_whitespace().any(|token| {
let token = token.trim_matches(|character: char| {
matches!(character, '(' | '[' | '{' | '<' | '\'' | '"')
});
token.starts_with('/')
|| token.starts_with("~/")
|| (token.len() >= 3
&& token.as_bytes()[0].is_ascii_alphabetic()
&& token.as_bytes()[1] == b':'
&& matches!(token.as_bytes()[2], b'/' | b'\\'))
})
}
impl<'de> Deserialize<'de> for ContextCheckpointLegacyLabelV1 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
Self::new(value).map_err(|error| DeError::custom(format!("{error:?}")))
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct ContextCheckpointLegacyCommitV1(String);
impl ContextCheckpointLegacyCommitV1 {
pub fn new(value: impl Into<String>) -> Result<Self, ContextCheckpointLegacyErrorV1> {
let value = value.into();
if !(7..=40).contains(&value.len()) || !is_lowercase_hex(&value) {
return Err(ContextCheckpointLegacyErrorV1::InvalidInputs {
field: ContextCheckpointLegacyFieldV1::Payload,
});
}
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl<'de> Deserialize<'de> for ContextCheckpointLegacyCommitV1 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
Self::new(value).map_err(|error| DeError::custom(format!("{error:?}")))
}
}
fn is_lowercase_hex(value: &str) -> bool {
!value.is_empty()
&& value
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}