heddle-object-model 0.15.1

Heddle's content-addressed object model and stable codecs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;

use chrono::{TimeZone, Utc};
use sley::{ObjectFormat as GitObjectFormat, ObjectId as GitObjectId};

use super::{
    CompactError, decode_blob_frame, decode_state_frame, decode_tree_frame, encode_blob_frame,
    encode_state_frame, encode_tree_frame, extract_state, extract_tree, is_state_frame,
    state::{STATE_MAGIC, STATE_MAGIC_V1, encode_state_frame_hcs1},
};
use crate::object::{
    Agent, Attribution, ChangeId, ChangeLineage, ChangeLineageKind, ContentHash, Principal,
    SpoolId, State, StateId, Status, Tree, TreeEntry, Verification,
};

#[test]
fn tree_frame_round_trips_every_entry_kind_and_native_bytes() {
    let content = ContentHash::from_bytes([1; 32]);
    let nested = ContentHash::from_bytes([2; 32]);
    let spool_state = StateId::from_bytes([3; 32]);
    let sha1 = GitObjectId::from_raw(GitObjectFormat::Sha1, &[4; 20]).unwrap();
    let sha256 = GitObjectId::from_raw(GitObjectFormat::Sha256, &[5; 32]).unwrap();
    let trees = vec![
        Tree::from_entries(vec![
            TreeEntry::file("a", content, false).unwrap(),
            TreeEntry::file("b", content, true).unwrap(),
            TreeEntry::directory("c", nested).unwrap(),
            TreeEntry::symlink("d", content).unwrap(),
            TreeEntry::gitlink("e", sha1).unwrap(),
            TreeEntry::gitlink("f", sha256).unwrap(),
            TreeEntry::spoollink("g", SpoolId::parse("acme/child").unwrap(), spool_state).unwrap(),
        ]),
        Tree::from_entries(vec![TreeEntry::file("same", content, false).unwrap()]),
    ];

    let encoded = encode_tree_frame(&trees).unwrap();
    let decoded = decode_tree_frame(&encoded).unwrap();

    assert_eq!(decoded, trees);
    for (actual, expected) in decoded.iter().zip(&trees) {
        assert_eq!(actual.hash(), expected.hash());
        assert_eq!(
            rmp_serde::to_vec_named(actual).unwrap(),
            rmp_serde::to_vec_named(expected).unwrap()
        );
    }
}

#[test]
fn state_frame_round_trips_every_fidelity_field_and_recomputes_id() {
    let principal = Principal::new("Author", "author@example.com");
    let committer = Principal::new("Committer", "committer@example.com");
    let agent = Agent::new("openai", "gpt-test")
        .with_session("session", "segment")
        .with_policy("policy")
        .with_thought_level("high")
        .with_parent("agent-1");
    let mut custom = BTreeMap::new();
    custom.insert(
        "nested".to_string(),
        serde_json::json!({"bytes": [1, 2, 3]}),
    );
    let mut first = State::new(
        ContentHash::from_bytes([10; 32]),
        vec![StateId::from_bytes([11; 32])],
        Attribution::with_agent(principal, agent),
    );
    first.change_id = ChangeId::from_bytes([12; 16]);
    first.intent = Some("intent".to_string());
    first.confidence = Some(f32::from_bits(0x7fc0_0123));
    first.created_at = Utc.timestamp_opt(1_700_000_000, 123_456_789).unwrap();
    first.authored_at = Some(Utc.timestamp_opt(1_699_999_900, 987_654_321).unwrap());
    first.verification = Some(Verification {
        tests_passed: Some(false),
        tests_failed: Some(7),
        coverage_pct: Some(92.5),
        coverage_delta: Some(f32::from_bits(0xffc0_0456)),
        lint_warnings: Some(3),
        custom,
    });
    first.status = Status::Published;
    first.provenance = Some(ContentHash::from_bytes([13; 32]));
    first.committer = Some(committer);
    first.authored_tz_offset = -7 * 3600;
    first.committer_tz_offset = 12 * 3600 + 45 * 60;
    first.raw_message = Some(b"raw\0message\xff".to_vec());
    first.git_lossy = true;
    first.extra_headers = vec![
        (b"x-custom".to_vec(), b"first\xff".to_vec()),
        (b"gpgsig".to_vec(), b"signed\n continuation".to_vec()),
    ];
    first.lineage = vec![ChangeLineage {
        kind: ChangeLineageKind::GitProjection,
        source_change: ChangeId::from_bytes([14; 16]),
        source_state: StateId::from_bytes([15; 32]),
    }];
    first.state_id = first.id();
    let mut second = State::new(
        ContentHash::from_bytes([20; 32]),
        vec![first.state_id],
        Attribution::human(Principal::new("Author", "author@example.com")),
    );
    second.created_at = Utc.timestamp_opt(1_700_000_001, 0).unwrap();
    second.state_id = second.id();
    let states = vec![first, second];

    let encoded = encode_state_frame(&states).unwrap();
    let decoded = decode_state_frame(&encoded).unwrap();

    for (actual, expected) in decoded.iter().zip(&states) {
        assert_eq!(actual.id(), expected.id());
        assert_eq!(
            rmp_serde::to_vec_named(actual).unwrap(),
            rmp_serde::to_vec_named(expected).unwrap()
        );
    }
    assert_eq!(
        decoded[0]
            .attribution
            .agent
            .as_ref()
            .unwrap()
            .thought_level
            .as_deref(),
        Some("high")
    );
    assert_eq!(
        decoded[0]
            .attribution
            .agent
            .as_ref()
            .unwrap()
            .parent
            .as_deref(),
        Some("agent-1")
    );
    assert_eq!(decoded[0].confidence.unwrap().to_bits(), 0x7fc0_0123);
    assert_eq!(
        decoded[0]
            .verification
            .as_ref()
            .unwrap()
            .coverage_delta
            .unwrap()
            .to_bits(),
        0xffc0_0456
    );
}

#[test]
fn compact_and_hash_include_thought_level_and_parent() {
    let principal = Principal::new("Author", "author@example.com");
    let created_at = Utc.timestamp_opt(1_700_000_000, 0).unwrap();
    let tree = ContentHash::from_bytes([70; 32]);
    let mut without = State::new(
        tree,
        Vec::new(),
        Attribution::with_agent(principal.clone(), Agent::new("anthropic", "opus")),
    );
    without.created_at = created_at;
    without.state_id = without.id();
    let mut with = State::new(
        tree,
        Vec::new(),
        Attribution::with_agent(
            principal,
            Agent::new("anthropic", "opus")
                .with_thought_level("high")
                .with_parent("agent-1"),
        ),
    );
    with.created_at = created_at;
    with.state_id = with.id();
    assert_ne!(
        without.id(),
        with.id(),
        "thought_level and parent must participate in the state hash"
    );
    let mut thought_only = State::new(
        tree,
        Vec::new(),
        Attribution::with_agent(
            Principal::new("Author", "author@example.com"),
            Agent::new("anthropic", "opus").with_thought_level("high"),
        ),
    );
    thought_only.created_at = created_at;
    thought_only.state_id = thought_only.id();
    let mut parent_only = State::new(
        tree,
        Vec::new(),
        Attribution::with_agent(
            Principal::new("Author", "author@example.com"),
            Agent::new("anthropic", "opus").with_parent("agent-1"),
        ),
    );
    parent_only.created_at = created_at;
    parent_only.state_id = parent_only.id();
    assert_ne!(
        without.id(),
        thought_only.id(),
        "changing thought_level must change the state id"
    );
    assert_ne!(
        without.id(),
        parent_only.id(),
        "changing parent must change the state id"
    );
    assert_ne!(
        thought_only.id(),
        parent_only.id(),
        "thought_level and parent must not be interchangeable in the hash"
    );
    let encoded = encode_state_frame(&[with.clone()]).unwrap();
    assert!(
        encoded.starts_with(STATE_MAGIC),
        "current encode must use the HCS2 cursor dictionary"
    );
    let decoded = decode_state_frame(&encoded).unwrap();
    let agent = decoded[0].attribution.agent.as_ref().unwrap();
    assert_eq!(agent.thought_level.as_deref(), Some("high"));
    assert_eq!(agent.parent.as_deref(), Some("agent-1"));
    assert_eq!(decoded[0].id(), with.id());
}

#[test]
fn hcs1_agent_frames_keep_the_five_field_dictionary() {
    let created_at = Utc.timestamp_opt(1_700_000_000, 0).unwrap();
    let mut state = State::new(
        ContentHash::from_bytes([70; 32]),
        Vec::new(),
        Attribution::with_agent(
            Principal::new("Author", "author@example.com"),
            Agent::new("anthropic", "opus"),
        ),
    );
    state.created_at = created_at;
    state.state_id = state.pre_cursor_id();
    let encoded = encode_state_frame_hcs1(&[state.clone()]).unwrap();
    assert!(
        encoded.starts_with(STATE_MAGIC_V1),
        "format-4 packs keep HCS1 magic, got {:x?}",
        &encoded[..4.min(encoded.len())]
    );
    assert!(is_state_frame(&encoded));
    let decoded = decode_state_frame(&encoded).unwrap();
    let agent = decoded[0].attribution.agent.as_ref().unwrap();
    assert_eq!(agent.provider, "anthropic");
    assert_eq!(agent.model, "opus");
    assert!(agent.thought_level.is_none());
    assert!(agent.parent.is_none());
    assert!(
        decoded[0].accepts_stored_id(&state.pre_cursor_id()),
        "HCS1 decode must not desync into the seven-field dictionary"
    );
    let extracted = extract_state(&encoded, state.pre_cursor_id()).unwrap();
    assert_eq!(extracted.state_id, state.pre_cursor_id());
}

#[test]
fn hcs2_repack_accepts_format4_agent_stored_id() {
    let created_at = Utc.timestamp_opt(1_700_000_000, 0).unwrap();
    let mut state = State::new(
        ContentHash::from_bytes([71; 32]),
        Vec::new(),
        Attribution::with_agent(
            Principal::new("Author", "author@example.com"),
            Agent::new("anthropic", "opus"),
        ),
    );
    state.created_at = created_at;
    let stored_id = state.pre_cursor_id();
    state.state_id = stored_id;
    let encoded = encode_state_frame(&[state.clone()]).unwrap();
    assert!(
        encoded.starts_with(STATE_MAGIC),
        "current encode must stay HCS2, got {:x?}",
        &encoded[..4.min(encoded.len())]
    );
    let decoded = decode_state_frame(&encoded).unwrap();
    assert!(
        decoded[0].accepts_stored_id(&stored_id),
        "HCS2 decode must keep the accepted format-4 stored id"
    );
    let extracted = extract_state(&encoded, stored_id).unwrap();
    assert_eq!(extracted.state_id, stored_id);
}

#[test]
fn corrupt_frame_byte_rejects_every_contained_object() {
    let hash = ContentHash::from_bytes([21; 32]);
    let trees = vec![
        Tree::from_entries(vec![TreeEntry::file("a", hash, false).unwrap()]),
        Tree::from_entries(vec![TreeEntry::file("b", hash, true).unwrap()]),
    ];
    let mut encoded = encode_tree_frame(&trees).unwrap();
    let corrupt_at = encoded.len() / 2;
    encoded[corrupt_at] ^= 0x01;

    for _ in &trees {
        let error = decode_tree_frame(&encoded).unwrap_err();
        assert!(error.to_string().contains("checksum mismatch"));
    }
}

#[test]
fn extract_matches_decode_all_canonical_bytes() {
    let content = ContentHash::from_bytes([31; 32]);
    let trees = vec![
        Tree::from_entries(vec![TreeEntry::file("first", content, false).unwrap()]),
        Tree::from_entries(vec![TreeEntry::file("second", content, true).unwrap()]),
        Tree::from_entries(vec![TreeEntry::directory("nested", content).unwrap()]),
    ];
    let encoded = encode_tree_frame(&trees).unwrap();
    let decoded = decode_tree_frame(&encoded).unwrap();

    for (expected, decoded) in trees.iter().zip(&decoded) {
        let extracted = extract_tree(&encoded, expected.hash()).unwrap();
        assert_eq!(extracted, *decoded);
        assert_eq!(extracted.hash(), expected.hash());
        assert_eq!(
            rmp_serde::to_vec_named(&extracted).unwrap(),
            rmp_serde::to_vec_named(expected).unwrap()
        );
    }
}

#[test]
fn extract_state_matches_decode_all_canonical_bytes() {
    let mut first = State::new(
        ContentHash::from_bytes([41; 32]),
        Vec::new(),
        Attribution::human(Principal::new("Author", "author@example.com")),
    );
    first.intent = Some("first".to_string());
    first.state_id = first.id();
    let mut second = State::new(
        ContentHash::from_bytes([42; 32]),
        vec![first.state_id],
        Attribution::human(Principal::new("Author", "author@example.com")),
    );
    second.intent = Some("second".to_string());
    second.state_id = second.id();
    let states = vec![first, second];
    let encoded = encode_state_frame(&states).unwrap();
    let decoded = decode_state_frame(&encoded).unwrap();

    for (expected, decoded) in states.iter().zip(&decoded) {
        let extracted = extract_state(&encoded, expected.id()).unwrap();
        assert_eq!(extracted.id(), decoded.id());
        assert_eq!(
            rmp_serde::to_vec_named(&extracted).unwrap(),
            rmp_serde::to_vec_named(expected).unwrap()
        );
    }
}

#[test]
fn extract_rejects_a_forged_typed_hash() {
    let content = ContentHash::from_bytes([51; 32]);
    let tree = Tree::from_entries(vec![TreeEntry::file("only", content, false).unwrap()]);
    let encoded = encode_tree_frame(std::slice::from_ref(&tree)).unwrap();
    let forged = ContentHash::compute_typed("tree", b"not this tree");

    let error = extract_tree(&encoded, forged).unwrap_err();
    assert!(matches!(error, CompactError::Missing));
    assert_ne!(forged, tree.hash());
}

#[test]
fn extract_rejects_every_object_after_one_corrupt_frame_byte() {
    let hash = ContentHash::from_bytes([61; 32]);
    let trees = vec![
        Tree::from_entries(vec![TreeEntry::file("a", hash, false).unwrap()]),
        Tree::from_entries(vec![TreeEntry::file("b", hash, true).unwrap()]),
    ];
    let mut encoded = encode_tree_frame(&trees).unwrap();
    let corrupt_at = encoded.len() / 2;
    encoded[corrupt_at] ^= 0x01;

    for tree in &trees {
        let error = extract_tree(&encoded, tree.hash()).unwrap_err();
        assert!(
            error.to_string().contains("checksum mismatch"),
            "corrupt frame must fail typed extraction, got {error}"
        );
    }
}

#[test]
fn blob_frame_round_trips_offsets_lengths_and_typed_hashes() {
    let bodies = [b"newest body".as_slice(), b"older body".as_slice(), &[]];
    let encoded = encode_blob_frame(&bodies).unwrap();
    let decoded = decode_blob_frame(&encoded).unwrap();

    assert_eq!(decoded.len(), bodies.len());
    for ((hash, actual), expected) in decoded.iter().zip(bodies) {
        assert_eq!(*actual, expected);
        assert_eq!(*hash, ContentHash::compute_typed("blob", expected));
    }
}

#[test]
fn corrupt_blob_frame_byte_rejects_every_contained_object() {
    let bodies = [b"first".as_slice(), b"second".as_slice()];
    let mut encoded = encode_blob_frame(&bodies).unwrap();
    let corrupt_at = encoded.len() / 2;
    encoded[corrupt_at] ^= 0x01;

    for _ in bodies {
        let error = decode_blob_frame(&encoded).unwrap_err();
        assert!(error.to_string().contains("checksum mismatch"));
    }
}