Skip to main content

qualia_core_db/inference/conditioning/codec/
decode.rs

1//! Canonical CBOR decoding for compiled plan summary.
2
3use super::super::budget::ConditioningBudget;
4use super::super::compile::CompiledPlanSummary;
5use super::super::spec::ConditioningError;
6use super::CODEC_VERSION;
7
8/// Decode a CompiledPlanSummary from Tag-4201 CBOR bytes.
9/// Fails closed on malformed headers, invalid version, or incomplete payloads.
10pub fn decode_plan_cbor<'a>(input: &'a [u8]) -> Result<CompiledPlanSummary<'a>, ConditioningError> {
11    if input.len() < 4 + 2 + 8 + 2 + 2 + 4 + 4 + 4 {
12        return Err(ConditioningError::CodecError);
13    }
14
15    // Check Tag 4201: 0xd9, 0x10, 0x69 and map header: 0xa7
16    if input[0] != 0xd9 || input[1] != 0x10 || input[2] != 0x69 || input[3] != 0xa7 {
17        return Err(ConditioningError::CodecError);
18    }
19
20    let mut idx = 4;
21
22    // Field 0: Version
23    let version = u16::from_be_bytes([input[idx], input[idx + 1]]);
24    idx += 2;
25    if version != CODEC_VERSION {
26        return Err(ConditioningError::UnsupportedVersion);
27    }
28
29    // Field 1: Plan ID
30    let mut plan_id_bytes = [0u8; 8];
31    plan_id_bytes.copy_from_slice(&input[idx..idx + 8]);
32    let plan_id = u64::from_be_bytes(plan_id_bytes);
33    idx += 8;
34
35    // Field 2: Profile ID
36    let p_len = u16::from_be_bytes([input[idx], input[idx + 1]]) as usize;
37    idx += 2;
38    if idx + p_len > input.len() {
39        return Err(ConditioningError::CodecError);
40    }
41    let profile_id =
42        std::str::from_utf8(&input[idx..idx + p_len]).map_err(|_| ConditioningError::CodecError)?;
43    idx += p_len;
44
45    // Field 3: Objective
46    if idx + 2 > input.len() {
47        return Err(ConditioningError::CodecError);
48    }
49    let o_len = u16::from_be_bytes([input[idx], input[idx + 1]]) as usize;
50    idx += 2;
51    if idx + o_len > input.len() {
52        return Err(ConditioningError::CodecError);
53    }
54    let objective =
55        std::str::from_utf8(&input[idx..idx + o_len]).map_err(|_| ConditioningError::CodecError)?;
56    idx += o_len;
57
58    // Field 4: Requirements count
59    if idx + 4 > input.len() {
60        return Err(ConditioningError::CodecError);
61    }
62    let req_count =
63        u32::from_be_bytes([input[idx], input[idx + 1], input[idx + 2], input[idx + 3]]) as usize;
64    idx += 4;
65
66    // Field 5: Outcomes count
67    if idx + 4 > input.len() {
68        return Err(ConditioningError::CodecError);
69    }
70    let outcomes_count =
71        u32::from_be_bytes([input[idx], input[idx + 1], input[idx + 2], input[idx + 3]]) as usize;
72    idx += 4;
73
74    // Field 6: Evidence count
75    if idx + 4 > input.len() {
76        return Err(ConditioningError::CodecError);
77    }
78    let evidence_count =
79        u32::from_be_bytes([input[idx], input[idx + 1], input[idx + 2], input[idx + 3]]) as usize;
80
81    Ok(CompiledPlanSummary {
82        plan_id,
83        profile_id,
84        objective,
85        requirements_count: req_count,
86        outcomes_count,
87        evidence_count,
88        budget: ConditioningBudget {
89            input_tokens: 0,
90            output_tokens: 0,
91            tool_rounds: 0,
92            max_bytes: 0,
93            thinking_token_budget: None,
94        },
95    })
96}