Skip to main content

khive_changeset/
changeset.rs

1//! The change-set: an envelope plus its ordered op-list, and NDJSON-delta codec.
2
3use crate::envelope::{Envelope, CURRENT_SCHEMA_VERSION};
4use crate::op::Op;
5
6/// Envelope metadata plus an ordered list of staged operations.
7///
8/// Operation order is semantically load-bearing (a `link` may target an
9/// earlier `create` ID) and is preserved exactly by the NDJSON codec.
10/// See `crates/khive-changeset/docs/api/ndjson-codec.md` for the wire contract.
11#[derive(Clone, Debug)]
12pub struct ChangeSet {
13    /// Change-set-wide stage-time provenance.
14    pub envelope: Envelope,
15    /// Operations in semantic application order.
16    pub ops: Vec<Op>,
17}
18
19impl ChangeSet {
20    /// Stores `envelope` and `ops` without reordering or validation.
21    pub fn new(envelope: Envelope, ops: Vec<Op>) -> Self {
22        Self { envelope, ops }
23    }
24}
25
26/// In-memory NDJSON encode/decode errors; no variant represents I/O.
27///
28/// See `crates/khive-changeset/docs/api/ndjson-codec.md` for variant semantics.
29#[derive(Debug, thiserror::Error)]
30pub enum ChangeSetError {
31    #[error("NDJSON-delta input is empty; expected an envelope header line")]
32    Empty,
33    #[error("line {line} is not valid JSON: {source}")]
34    MalformedLine {
35        line: usize,
36        #[source]
37        source: serde_json::Error,
38    },
39    #[error(
40        "envelope schema_version {found} is not supported (this crate emits and \
41         accepts schema_version {expected})"
42    )]
43    UnsupportedSchemaVersion { found: u32, expected: u32 },
44    #[error("failed to serialize change-set: {0}")]
45    Serialize(#[source] serde_json::Error),
46}
47
48/// Encodes the envelope as line one and each op on a following line, in order.
49///
50/// The returned in-memory string ends every line, including the last, with `\n`.
51///
52/// # Errors
53///
54/// Returns [`ChangeSetError::UnsupportedSchemaVersion`] for a stale envelope, or
55/// [`ChangeSetError::Serialize`] if any envelope or op cannot serialize.
56pub fn to_ndjson(changeset: &ChangeSet) -> Result<String, ChangeSetError> {
57    if changeset.envelope.schema_version != CURRENT_SCHEMA_VERSION {
58        return Err(ChangeSetError::UnsupportedSchemaVersion {
59            found: changeset.envelope.schema_version,
60            expected: CURRENT_SCHEMA_VERSION,
61        });
62    }
63    let mut out = String::new();
64    out.push_str(&serde_json::to_string(&changeset.envelope).map_err(ChangeSetError::Serialize)?);
65    out.push('\n');
66    for op in &changeset.ops {
67        out.push_str(&serde_json::to_string(op).map_err(ChangeSetError::Serialize)?);
68        out.push('\n');
69    }
70    Ok(out)
71}
72
73/// Decodes an envelope header and every following line as one ordered [`Op`].
74///
75/// Blank lines and unknown fields are errors; schema version must equal
76/// [`CURRENT_SCHEMA_VERSION`].
77///
78/// # Errors
79///
80/// Returns [`ChangeSetError::Empty`], [`ChangeSetError::MalformedLine`], or
81/// [`ChangeSetError::UnsupportedSchemaVersion`] according to the failed stage.
82/// See `crates/khive-changeset/docs/api/ndjson-codec.md` for line semantics.
83pub fn from_ndjson(input: &str) -> Result<ChangeSet, ChangeSetError> {
84    let mut lines = input.lines().enumerate();
85    let (_, first_line) = lines.next().ok_or(ChangeSetError::Empty)?;
86    reject_repeated_members(first_line)
87        .map_err(|source| ChangeSetError::MalformedLine { line: 1, source })?;
88    let envelope: Envelope = serde_json::from_str(first_line)
89        .map_err(|source| ChangeSetError::MalformedLine { line: 1, source })?;
90    if envelope.schema_version != CURRENT_SCHEMA_VERSION {
91        return Err(ChangeSetError::UnsupportedSchemaVersion {
92            found: envelope.schema_version,
93            expected: CURRENT_SCHEMA_VERSION,
94        });
95    }
96
97    // Parse blank lines too, preserving “every post-header line is an op.”
98    let mut ops = Vec::new();
99    for (idx, line) in lines {
100        reject_repeated_members(line).map_err(|source| ChangeSetError::MalformedLine {
101            line: idx + 1,
102            source,
103        })?;
104        let op: Op =
105            serde_json::from_str(line).map_err(|source| ChangeSetError::MalformedLine {
106                line: idx + 1,
107                source,
108            })?;
109        ops.push(op);
110    }
111    Ok(ChangeSet { envelope, ops })
112}
113
114/// Refuses a line in which any JSON object, at any depth, names the same member twice.
115///
116/// The typed decode keeps only the last of a repeated name, so a caller's scan of the
117/// decoded values would never see the earlier copies while the stored text still holds
118/// them. Names are compared after JSON unescaping, and the error names a position only,
119/// never the member or its value.
120fn reject_repeated_members(line: &str) -> Result<(), serde_json::Error> {
121    serde_json::from_str::<UniqueMembers>(line).map(|_| ())
122}
123
124struct UniqueMembers;
125
126impl<'de> serde::Deserialize<'de> for UniqueMembers {
127    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
128        deserializer.deserialize_any(UniqueMembersVisitor)
129    }
130}
131
132struct UniqueMembersVisitor;
133
134impl<'de> serde::de::Visitor<'de> for UniqueMembersVisitor {
135    type Value = UniqueMembers;
136
137    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
138        formatter.write_str("a JSON value")
139    }
140
141    fn visit_bool<E>(self, _: bool) -> Result<UniqueMembers, E> {
142        Ok(UniqueMembers)
143    }
144
145    fn visit_i64<E>(self, _: i64) -> Result<UniqueMembers, E> {
146        Ok(UniqueMembers)
147    }
148
149    fn visit_u64<E>(self, _: u64) -> Result<UniqueMembers, E> {
150        Ok(UniqueMembers)
151    }
152
153    fn visit_f64<E>(self, _: f64) -> Result<UniqueMembers, E> {
154        Ok(UniqueMembers)
155    }
156
157    fn visit_str<E>(self, _: &str) -> Result<UniqueMembers, E> {
158        Ok(UniqueMembers)
159    }
160
161    fn visit_unit<E>(self) -> Result<UniqueMembers, E> {
162        Ok(UniqueMembers)
163    }
164
165    fn visit_seq<A: serde::de::SeqAccess<'de>>(
166        self,
167        mut seq: A,
168    ) -> Result<UniqueMembers, A::Error> {
169        while seq.next_element::<UniqueMembers>()?.is_some() {}
170        Ok(UniqueMembers)
171    }
172
173    fn visit_map<A: serde::de::MapAccess<'de>>(
174        self,
175        mut map: A,
176    ) -> Result<UniqueMembers, A::Error> {
177        let mut seen = std::collections::HashSet::new();
178        while let Some(name) = map.next_key::<String>()? {
179            if !seen.insert(name) {
180                return Err(serde::de::Error::custom("an object repeats a member name"));
181            }
182            map.next_value::<UniqueMembers>()?;
183        }
184        Ok(UniqueMembers)
185    }
186}
187
188#[cfg(test)]
189mod tests {
190    use super::*;
191    use crate::op::{CreateOp, CreateTarget, EntityCreateFields};
192    use khive_types::{EntityKind, Id128, Namespace, Timestamp};
193
194    fn sample_changeset() -> ChangeSet {
195        let envelope = Envelope::new("agent:test", "family:sonnet", Timestamp::from_secs(1));
196        let create = Op::Create(CreateOp {
197            id: Id128::from_u128(1),
198            namespace: Namespace::local(),
199            target: CreateTarget::Entity(EntityCreateFields {
200                entity_kind: EntityKind::Concept,
201                entity_type: None,
202                name: "X".into(),
203                description: None,
204                properties: Default::default(),
205                tags: vec![],
206                key: None,
207                aliases: vec![],
208            }),
209        });
210        ChangeSet::new(envelope, vec![create])
211    }
212
213    #[test]
214    fn round_trips_envelope_and_ops() {
215        let cs = sample_changeset();
216        let text = to_ndjson(&cs).unwrap();
217        let decoded = from_ndjson(&text).unwrap();
218        assert_eq!(decoded.envelope, cs.envelope);
219        assert_eq!(decoded.ops.len(), 1);
220        let text2 = to_ndjson(&decoded).unwrap();
221        assert_eq!(text, text2, "re-serialization must be byte-identical");
222    }
223
224    #[test]
225    fn envelope_is_line_one() {
226        let cs = sample_changeset();
227        let text = to_ndjson(&cs).unwrap();
228        let first_line = text.lines().next().unwrap();
229        let parsed_env: Envelope = serde_json::from_str(first_line).unwrap();
230        assert_eq!(parsed_env, cs.envelope);
231    }
232
233    #[test]
234    fn envelope_batch_id_round_trips_through_ndjson() {
235        let mut cs = sample_changeset();
236        cs.envelope = cs.envelope.with_batch_id("batch-xyz");
237        let text = to_ndjson(&cs).unwrap();
238        let decoded = from_ndjson(&text).unwrap();
239        assert_eq!(decoded.envelope.batch_id.as_deref(), Some("batch-xyz"));
240        assert_eq!(decoded.envelope, cs.envelope);
241    }
242
243    #[test]
244    fn envelope_without_batch_id_round_trips_through_ndjson() {
245        let cs = sample_changeset();
246        assert_eq!(cs.envelope.batch_id, None);
247        let text = to_ndjson(&cs).unwrap();
248        let decoded = from_ndjson(&text).unwrap();
249        assert_eq!(decoded.envelope.batch_id, None);
250        assert_eq!(decoded.envelope, cs.envelope);
251    }
252
253    #[test]
254    fn empty_input_errors() {
255        let result = from_ndjson("");
256        assert!(matches!(result, Err(ChangeSetError::Empty)));
257    }
258
259    #[test]
260    fn malformed_op_line_reports_correct_line_number() {
261        let envelope = Envelope::new("agent:test", "family:sonnet", Timestamp::from_secs(1));
262        let env_line = serde_json::to_string(&envelope).unwrap();
263        let text = format!("{env_line}\n{{\"op\": \"create\", not json}}\n");
264        let err = from_ndjson(&text).unwrap_err();
265        match err {
266            ChangeSetError::MalformedLine { line, .. } => assert_eq!(line, 2),
267            other => panic!("expected MalformedLine, got {other:?}"),
268        }
269    }
270
271    #[test]
272    fn blank_line_between_ops_is_malformed() {
273        let envelope = Envelope::new("agent:test", "family:sonnet", Timestamp::from_secs(1));
274        let env_line = serde_json::to_string(&envelope).unwrap();
275        let text = format!("{env_line}\n\n");
276        let err = from_ndjson(&text).unwrap_err();
277        match err {
278            ChangeSetError::MalformedLine { line, .. } => assert_eq!(line, 2),
279            other => panic!("expected MalformedLine, got {other:?}"),
280        }
281    }
282
283    /// One sample create, its properties holding `first` then `second` under one name.
284    fn create_line_with_repeated_property(first: &str, second: &str) -> String {
285        let id = uuid_text(7);
286        format!(
287            "{{\"op\":\"create\",\"id\":\"{id}\",\"namespace\":\"local\",\"target\":{{\
288             \"kind\":\"entity\",\"entity_kind\":\"concept\",\"name\":\"X\",\
289             \"properties\":{{\"{first}\":\"kept-out\",\"{second}\":\"benign\"}}}}}}"
290        )
291    }
292
293    fn uuid_text(n: u128) -> String {
294        serde_json::to_value(Id128::from_u128(n))
295            .unwrap()
296            .as_str()
297            .unwrap()
298            .to_owned()
299    }
300
301    fn env_line() -> String {
302        let envelope = Envelope::new("agent:test", "family:sonnet", Timestamp::from_secs(1));
303        serde_json::to_string(&envelope).unwrap()
304    }
305
306    #[test]
307    fn a_distinct_property_map_decodes() {
308        let text = format!(
309            "{}\n{}\n",
310            env_line(),
311            create_line_with_repeated_property("label", "other")
312        );
313        let decoded = from_ndjson(&text).expect("distinct names decode");
314        match &decoded.ops[0] {
315            Op::Create(CreateOp {
316                target: CreateTarget::Entity(fields),
317                ..
318            }) => assert_eq!(fields.properties.len(), 2),
319            other => panic!("expected an entity create, got {other:?}"),
320        }
321    }
322
323    #[test]
324    fn a_repeated_nested_member_is_malformed_and_the_error_names_no_bytes() {
325        let text = format!(
326            "{}\n{}\n",
327            env_line(),
328            create_line_with_repeated_property("label", "label")
329        );
330        let err = from_ndjson(&text).unwrap_err();
331        let shown = err.to_string();
332        match err {
333            ChangeSetError::MalformedLine { line, .. } => assert_eq!(line, 2),
334            other => panic!("expected MalformedLine, got {other:?}"),
335        }
336        assert!(shown.contains("repeats a member name"), "{shown}");
337        assert!(!shown.contains("label"), "{shown}");
338        assert!(!shown.contains("kept-out"), "{shown}");
339        assert!(!shown.contains("benign"), "{shown}");
340    }
341
342    #[test]
343    fn member_names_are_compared_after_unescaping() {
344        // `label` decodes to `label`, so the two spellings are one member name.
345        let text = format!(
346            "{}\n{}\n",
347            env_line(),
348            create_line_with_repeated_property("\\u006cabel", "label")
349        );
350        assert!(matches!(
351            from_ndjson(&text),
352            Err(ChangeSetError::MalformedLine { line: 2, .. })
353        ));
354    }
355
356    #[test]
357    fn a_repeated_top_level_or_envelope_member_is_malformed() {
358        let op = create_line_with_repeated_property("label", "other");
359        let repeated_op = op.replacen(
360            "{\"op\":\"create\",",
361            "{\"op\":\"create\",\"op\":\"create\",",
362            1,
363        );
364        assert_ne!(repeated_op, op);
365        let text = format!("{}\n{repeated_op}\n", env_line());
366        assert!(matches!(
367            from_ndjson(&text),
368            Err(ChangeSetError::MalformedLine { line: 2, .. })
369        ));
370
371        let env = env_line();
372        let repeated_env = env.replacen('{', "{\"schema_version\":1,", 1);
373        assert!(env.contains("\"schema_version\""), "{env}");
374        let text = format!("{repeated_env}\n{op}\n");
375        assert!(matches!(
376            from_ndjson(&text),
377            Err(ChangeSetError::MalformedLine { line: 1, .. })
378        ));
379    }
380
381    #[test]
382    fn unsupported_schema_version_is_rejected() {
383        let mut envelope = Envelope::new("agent:test", "family:sonnet", Timestamp::from_secs(1));
384        envelope.schema_version = 99;
385        let env_line = serde_json::to_string(&envelope).unwrap();
386        let err = from_ndjson(&env_line).unwrap_err();
387        assert!(matches!(
388            err,
389            ChangeSetError::UnsupportedSchemaVersion {
390                found: 99,
391                expected: CURRENT_SCHEMA_VERSION
392            }
393        ));
394    }
395
396    #[test]
397    fn op_order_is_preserved() {
398        let envelope = Envelope::new("agent:test", "family:sonnet", Timestamp::from_secs(1));
399        let mk_create = |n: u128| {
400            Op::Create(CreateOp {
401                id: Id128::from_u128(n),
402                namespace: Namespace::local(),
403                target: CreateTarget::Entity(EntityCreateFields {
404                    entity_kind: EntityKind::Concept,
405                    entity_type: None,
406                    name: format!("entity-{n}"),
407                    description: None,
408                    properties: Default::default(),
409                    tags: vec![],
410                    key: None,
411                    aliases: vec![],
412                }),
413            })
414        };
415        let ops = vec![mk_create(3), mk_create(1), mk_create(2)];
416        let cs = ChangeSet::new(envelope, ops);
417        let text = to_ndjson(&cs).unwrap();
418        let decoded = from_ndjson(&text).unwrap();
419        let ids: Vec<u128> = decoded
420            .ops
421            .iter()
422            .map(|op| match op {
423                Op::Create(c) => c.id.to_u128(),
424                _ => unreachable!(),
425            })
426            .collect();
427        assert_eq!(ids, vec![3, 1, 2]);
428    }
429}