use super::{
CONTEXT_CHECKPOINT_ADAPTER_SCHEMA_VERSION, ContextCheckpointLegacyCommitV1,
ContextCheckpointLegacyErrorV1, ContextCheckpointLegacyFieldV1, ContextCheckpointLegacyHashV1,
ContextCheckpointLegacyLabelV1, ContextCheckpointLegacyTargetV1,
MAX_CONTEXT_CHECKPOINT_CARRIER_ENVELOPE_BYTES, parse_json_object, require_capacity,
validate_optional_label, validate_project_identity, validate_sorted_unique_timestamps,
validate_timestamp,
};
use lean_ctx_protocol::{SemanticVersion, Sha256Digest, UtcTimestamp, WorkspaceId};
use serde::{Deserialize, Deserializer, Serialize, de::Error as DeError};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacyPolicyIdentityV1 {
pub name: ContextCheckpointLegacyLabelV1,
pub policy_digest: ContextCheckpointLegacyHashV1,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacyProjectIdentityV1 {
pub project_root_hash: Option<ContextCheckpointLegacyHashV1>,
pub project_identity_hash: Option<ContextCheckpointLegacyHashV1>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacySessionCountersV1 {
pub total_tool_calls: u32,
pub total_tokens_saved: u64,
pub total_tokens_input: u64,
pub cache_hits: u32,
pub files_read: u32,
pub commands_run: u32,
pub intents_inferred: u32,
pub intents_explicit: u32,
pub unsaved_changes: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacyGitAnchorV1 {
pub commit: Option<ContextCheckpointLegacyCommitV1>,
pub branch: Option<ContextCheckpointLegacyLabelV1>,
pub dirty: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacyRoiV1 {
pub input_tokens: u64,
pub output_tokens: u64,
pub tokens_saved: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacyLedgerTotalsV1 {
pub window_size: u32,
pub total_tokens_sent: u32,
pub total_tokens_saved: u32,
pub lineage_items_recorded: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointSessionBundleInputsV1 {
pub exported_at: UtcTimestamp,
pub project: ContextCheckpointLegacyProjectIdentityV1,
pub role: ContextCheckpointLegacyPolicyIdentityV1,
pub profile: ContextCheckpointLegacyPolicyIdentityV1,
pub decision_observed_at: Vec<UtcTimestamp>,
pub finding_observed_at: Vec<UtcTimestamp>,
pub stats: ContextCheckpointLegacySessionCountersV1,
pub compression_level: Option<ContextCheckpointLegacyLabelV1>,
pub terse_mode: bool,
}
impl ContextCheckpointSessionBundleInputsV1 {
pub fn validate(&self) -> Result<(), ContextCheckpointLegacyErrorV1> {
require_capacity(
self.decision_observed_at.len(),
lean_ctx_protocol::MAX_CONTEXT_CHECKPOINT_DECISIONS,
ContextCheckpointLegacyFieldV1::DecisionObservedAt,
)?;
require_capacity(
self.finding_observed_at.len(),
lean_ctx_protocol::MAX_CONTEXT_CHECKPOINT_FINDINGS,
ContextCheckpointLegacyFieldV1::FindingObservedAt,
)?;
validate_sorted_unique_timestamps(
&self.decision_observed_at,
ContextCheckpointLegacyFieldV1::DecisionObservedAt,
)?;
validate_sorted_unique_timestamps(
&self.finding_observed_at,
ContextCheckpointLegacyFieldV1::FindingObservedAt,
)
}
}
validated_deserialize!(ContextCheckpointSessionBundleInputsV1 {
exported_at: UtcTimestamp,
project: ContextCheckpointLegacyProjectIdentityV1,
role: ContextCheckpointLegacyPolicyIdentityV1,
profile: ContextCheckpointLegacyPolicyIdentityV1,
decision_observed_at: Vec<UtcTimestamp>,
finding_observed_at: Vec<UtcTimestamp>,
stats: ContextCheckpointLegacySessionCountersV1,
compression_level: Option<ContextCheckpointLegacyLabelV1>,
terse_mode: bool,
});
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointHandoffBundleInputsV1 {
pub exported_at: UtcTimestamp,
pub ledger_created_at: UtcTimestamp,
pub manifest_digest: ContextCheckpointLegacyHashV1,
pub project: ContextCheckpointLegacyProjectIdentityV1,
pub agent_id: Option<ContextCheckpointLegacyLabelV1>,
pub client_name: Option<ContextCheckpointLegacyLabelV1>,
}
impl ContextCheckpointHandoffBundleInputsV1 {
pub fn validate(&self) -> Result<(), ContextCheckpointLegacyErrorV1> {
validate_project_identity(&self.project)?;
validate_timestamp(&self.exported_at, ContextCheckpointLegacyFieldV1::Payload)?;
validate_timestamp(
&self.ledger_created_at,
ContextCheckpointLegacyFieldV1::Payload,
)?;
validate_optional_label(self.agent_id.as_ref())?;
validate_optional_label(self.client_name.as_ref())
}
}
validated_deserialize!(ContextCheckpointHandoffBundleInputsV1 {
exported_at: UtcTimestamp,
ledger_created_at: UtcTimestamp,
manifest_digest: ContextCheckpointLegacyHashV1,
project: ContextCheckpointLegacyProjectIdentityV1,
agent_id: Option<ContextCheckpointLegacyLabelV1>,
client_name: Option<ContextCheckpointLegacyLabelV1>,
});
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointContextSnapshotInputsV1 {
pub created_at: UtcTimestamp,
pub lean_ctx_version: SemanticVersion,
pub project: ContextCheckpointLegacyProjectIdentityV1,
pub parent_snapshot_id: Option<ContextCheckpointLegacyHashV1>,
pub git: ContextCheckpointLegacyGitAnchorV1,
pub roi: ContextCheckpointLegacyRoiV1,
pub ledger_totals: ContextCheckpointLegacyLedgerTotalsV1,
}
impl ContextCheckpointContextSnapshotInputsV1 {
pub fn validate(&self) -> Result<(), ContextCheckpointLegacyErrorV1> {
validate_project_identity(&self.project)?;
validate_timestamp(&self.created_at, ContextCheckpointLegacyFieldV1::Payload)?;
if let Some(parent) = &self.parent_snapshot_id {
ContextCheckpointLegacyHashV1::new(parent.as_str().to_owned())?;
}
if let Some(commit) = &self.git.commit {
ContextCheckpointLegacyCommitV1::new(commit.as_str().to_owned())?;
}
if let Some(branch) = &self.git.branch {
ContextCheckpointLegacyLabelV1::new(branch.as_str().to_owned())?;
}
Ok(())
}
}
validated_deserialize!(ContextCheckpointContextSnapshotInputsV1 {
created_at: UtcTimestamp,
lean_ctx_version: SemanticVersion,
project: ContextCheckpointLegacyProjectIdentityV1,
parent_snapshot_id: Option<ContextCheckpointLegacyHashV1>,
git: ContextCheckpointLegacyGitAnchorV1,
roi: ContextCheckpointLegacyRoiV1,
ledger_totals: ContextCheckpointLegacyLedgerTotalsV1,
});
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointCtxpkgInputsV1 {
pub carrier_envelope_json: Option<String>,
}
impl ContextCheckpointCtxpkgInputsV1 {
pub fn validate(&self) -> Result<(), ContextCheckpointLegacyErrorV1> {
if let Some(json) = &self.carrier_envelope_json {
if json.len() > MAX_CONTEXT_CHECKPOINT_CARRIER_ENVELOPE_BYTES {
return Err(ContextCheckpointLegacyErrorV1::LegacyCapacityExceeded {
field: ContextCheckpointLegacyFieldV1::Payload,
limit: MAX_CONTEXT_CHECKPOINT_CARRIER_ENVELOPE_BYTES as u64,
actual: json.len() as u64,
});
}
parse_json_object(json)?;
}
Ok(())
}
}
validated_deserialize!(ContextCheckpointCtxpkgInputsV1 {
carrier_envelope_json: Option<String>,
});
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextCheckpointLegacyInputsV1 {
SessionBundle(ContextCheckpointSessionBundleInputsV1),
HandoffBundle(ContextCheckpointHandoffBundleInputsV1),
Ctxpkg(ContextCheckpointCtxpkgInputsV1),
ContextSnapshot(ContextCheckpointContextSnapshotInputsV1),
}
impl ContextCheckpointLegacyInputsV1 {
pub const fn target(&self) -> ContextCheckpointLegacyTargetV1 {
match self {
Self::SessionBundle(_) => ContextCheckpointLegacyTargetV1::SessionBundle,
Self::HandoffBundle(_) => ContextCheckpointLegacyTargetV1::HandoffBundle,
Self::Ctxpkg(_) => ContextCheckpointLegacyTargetV1::Ctxpkg,
Self::ContextSnapshot(_) => ContextCheckpointLegacyTargetV1::ContextSnapshot,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointLegacyRequestV1 {
pub adapter_schema_version: u32,
pub target: ContextCheckpointLegacyTargetV1,
pub target_schema_version: u32,
pub inputs: ContextCheckpointLegacyInputsV1,
}
impl ContextCheckpointLegacyRequestV1 {
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,
});
}
let inputs_target = self.inputs.target();
if inputs_target != self.target {
return Err(ContextCheckpointLegacyErrorV1::TargetInputsMismatch {
target: self.target,
inputs_target,
});
}
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(),
});
}
match &self.inputs {
ContextCheckpointLegacyInputsV1::SessionBundle(inputs) => inputs.validate()?,
ContextCheckpointLegacyInputsV1::HandoffBundle(inputs) => inputs.validate()?,
ContextCheckpointLegacyInputsV1::Ctxpkg(inputs) => inputs.validate()?,
ContextCheckpointLegacyInputsV1::ContextSnapshot(inputs) => inputs.validate()?,
}
Ok(())
}
}
validated_deserialize!(ContextCheckpointLegacyRequestV1 {
adapter_schema_version: u32,
target: ContextCheckpointLegacyTargetV1,
target_schema_version: u32,
inputs: ContextCheckpointLegacyInputsV1,
});
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextCheckpointCarrierAdmissionV1 {
pub workspace_id: WorkspaceId,
pub state_digest: Sha256Digest,
pub envelope_digest: Sha256Digest,
}