interplane.Get Omega
Read in your language

interplane-mv

INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.

Brought by drakestapleton

branch main Branches Tags History

interplane-mv / provenance / src / gojson.rs

136 lines · 4280 bytes Raw Blame History
1//! WALDO record identity, read from retained bytes.
2//!
3//! WALDO pins its records (`run_bom_sha256`, `bom_sha256`, `source_bom_sha256`, `plan_sha256`,
4//! `corpus_bom_sha256`) as SHA-256 over Go `json.Marshal` of the record, while it writes the file
5//! with `json.MarshalIndent` of the same value (openwaldo/waldo `internal/model/records.go`
6//! `hashJSON` / `writeJSONAtomic`). Removing the insignificant whitespace of the file bytes gives
7//! the `json.Marshal` bytes back, so the pin can be checked without re-encoding (and without
8//! reordering keys). Checked against real WALDO run bytes: see provenance/README.md.
9
10use sha2::{Digest, Sha256};
11
12/// Lower-case hex SHA-256 of raw bytes.
13pub fn sha256_hex(b: &[u8]) -> String {
14 // sha2 0.11's digest output does not implement LowerHex.
15 hex::encode(Sha256::digest(b))
16}
17
18/// Drop JSON whitespace outside strings. Bytes inside strings are kept exactly.
19pub fn compact(b: &[u8]) -> Vec<u8> {
20 let mut out = Vec::with_capacity(b.len());
21 let (mut in_str, mut esc) = (false, false);
22 for &c in b {
23 if in_str {
24 out.push(c);
25 if esc {
26 esc = false;
27 } else if c == b'\\' {
28 esc = true;
29 } else if c == b'"' {
30 in_str = false;
31 }
32 } else if !matches!(c, b' ' | b'\t' | b'\r' | b'\n') {
33 out.push(c);
34 if c == b'"' {
35 in_str = true;
36 }
37 }
38 }
39 out
40}
41
42/// WALDO record digest of a retained JSON file: SHA-256 of its compact form.
43pub fn waldo_sha256(b: &[u8]) -> String {
44 sha256_hex(&compact(b))
45}
46
47/// Index just past the JSON value that starts at `i` in compact bytes, or None if malformed.
48fn value_end(c: &[u8], i: usize) -> Option<usize> {
49 match *c.get(i)? {
50 b'"' => {
51 let mut j = i + 1;
52 while j < c.len() {
53 match c[j] {
54 b'\\' => j += 2,
55 b'"' => return Some(j + 1),
56 _ => j += 1,
57 }
58 }
59 None
60 }
61 b'{' | b'[' => {
62 let mut depth = 0usize;
63 let mut j = i;
64 while j < c.len() {
65 match c[j] {
66 b'"' => {
67 j = value_end(c, j)?;
68 continue;
69 }
70 b'{' | b'[' => depth += 1,
71 b'}' | b']' => {
72 depth -= 1;
73 if depth == 0 {
74 return Some(j + 1);
75 }
76 }
77 _ => {}
78 }
79 j += 1;
80 }
81 None
82 }
83 _ => {
84 let mut j = i;
85 while j < c.len() && !matches!(c[j], b',' | b'}' | b']') {
86 j += 1;
87 }
88 Some(j)
89 }
90 }
91}
92
93/// The compact bytes of a top-level member's value, by exact key, or None.
94pub fn top_level_member<'a>(compact_obj: &'a [u8], key: &str) -> Option<&'a [u8]> {
95 let c = compact_obj;
96 if c.first() != Some(&b'{') {
97 return None;
98 }
99 let want = format!("\"{key}\"");
100 let mut i = 1;
101 while i < c.len() && c[i] != b'}' {
102 let key_end = value_end(c, i)?;
103 if c.get(key_end) != Some(&b':') {
104 return None;
105 }
106 let v_start = key_end + 1;
107 let v_end = value_end(c, v_start)?;
108 if &c[i..key_end] == want.as_bytes() {
109 return Some(&c[v_start..v_end]);
110 }
111 i = v_end;
112 if c.get(i) == Some(&b',') {
113 i += 1;
114 }
115 }
116 None
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn compact_keeps_string_bytes_and_order() {
125 let b = b"{\n \"b\": \"x y\\\" z\",\n \"a\": [1, 2]\n}\n";
126 assert_eq!(compact(b), b"{\"b\":\"x y\\\" z\",\"a\":[1,2]}".to_vec());
127 }
128
129 #[test]
130 fn member_is_top_level_only() {
131 let c = compact(b"{\"x\": {\"k\": 1}, \"k\": {\"z\": \"}\"}, \"k2\": 3}");
132 assert_eq!(top_level_member(&c, "k"), Some(&b"{\"z\":\"}\"}"[..]));
133 assert_eq!(top_level_member(&c, "k2"), Some(&b"3"[..]));
134 assert_eq!(top_level_member(&c, "missing"), None);
135 }
136}