jacs 0.10.0

JACS JSON AI Communication Standard
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use crate::agent::document::JACSDocument;
use crate::error::JacsError;
use crate::search::{SearchHit, SearchMethod, SearchResults};
use serde_json::Value;

/// Parse a document key in the form `id:version`.
///
/// Versions may contain additional colons; only the first colon splits the key.
pub fn parse_document_key(key: &str) -> Result<(&str, &str), JacsError> {
    let (id, version) = key
        .split_once(':')
        .ok_or_else(|| format!("Invalid document key '{}': expected 'id:version'", key))?;
    Ok((id, version))
}

/// Reconstruct a stored document from canonical JSON text.
pub fn document_from_raw_json(raw: &str) -> Result<JACSDocument, JacsError> {
    let value: Value = serde_json::from_str(raw)?;
    document_from_value(value)
}

/// Reconstruct a stored document from canonical JSON bytes.
pub fn document_from_raw_bytes(raw: &[u8]) -> Result<JACSDocument, JacsError> {
    let value: Value = serde_json::from_slice(raw)?;
    document_from_value(value)
}

/// Extract the signing agent id from a document signature if present.
pub fn extract_signature_agent_id(value: &Value) -> Option<String> {
    value
        .get("jacsSignature")
        .and_then(|signature| {
            signature
                .get("agentID")
                .or_else(|| signature.get("jacsSignatureAgentId"))
        })
        .and_then(|v| v.as_str())
        .map(|s| s.to_string())
}

/// Traverse a JSON value using a dot-separated field path.
pub fn get_nested_field<'a>(value: &'a Value, path: &str) -> Option<&'a Value> {
    let mut current = value;
    for part in path.split('.') {
        current = current.get(part)?;
    }
    Some(current)
}

/// Check whether a nested field matches an exact string representation.
pub fn field_matches_exact(value: &Value, field_path: &str, expected: &str) -> bool {
    get_nested_field(value, field_path).is_some_and(|field_value| match field_value {
        Value::String(s) => s == expected,
        other => other.to_string().trim_matches('"') == expected,
    })
}

/// Wrap exact field-filter matches into a consistent `SearchResults` shape.
pub fn build_field_filter_search_results(
    docs: Vec<JACSDocument>,
    field_path: &str,
) -> SearchResults {
    let total_count = docs.len();
    let matched_field = field_path.to_string();
    let results = docs
        .into_iter()
        .map(|document| SearchHit {
            document,
            score: 1.0,
            matched_fields: vec![matched_field.clone()],
        })
        .collect();

    SearchResults {
        results,
        total_count,
        method: SearchMethod::FieldMatch,
    }
}

fn document_from_value(value: Value) -> Result<JACSDocument, JacsError> {
    let id = value
        .get("jacsId")
        .and_then(|v| v.as_str())
        .ok_or("Document missing required field: jacsId")?
        .to_string();
    let version = value
        .get("jacsVersion")
        .and_then(|v| v.as_str())
        .ok_or("Document missing required field: jacsVersion")?
        .to_string();
    let jacs_type = value
        .get("jacsType")
        .and_then(|v| v.as_str())
        .ok_or("Document missing required field: jacsType")?
        .to_string();

    Ok(JACSDocument {
        id,
        version,
        value,
        jacs_type,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::StorageDocumentTraits;
    use crate::testing::make_test_doc;
    use serde_json::json;
    use std::collections::{HashMap, HashSet};
    use std::sync::Mutex;

    struct MockStorage {
        docs: Mutex<HashMap<String, JACSDocument>>,
        store_failures: HashSet<String>,
        get_failures: HashSet<String>,
        stored_keys: Mutex<Vec<String>>,
        fetched_keys: Mutex<Vec<String>>,
    }

    impl MockStorage {
        fn new(docs: Vec<JACSDocument>) -> Self {
            let docs = docs
                .into_iter()
                .map(|doc| (doc.getkey(), doc))
                .collect::<HashMap<_, _>>();
            Self {
                docs: Mutex::new(docs),
                store_failures: HashSet::new(),
                get_failures: HashSet::new(),
                stored_keys: Mutex::new(Vec::new()),
                fetched_keys: Mutex::new(Vec::new()),
            }
        }

        fn with_store_failures(mut self, failures: &[&str]) -> Self {
            self.store_failures = failures.iter().map(|key| key.to_string()).collect();
            self
        }

        fn with_get_failures(mut self, failures: &[&str]) -> Self {
            self.get_failures = failures.iter().map(|key| key.to_string()).collect();
            self
        }
    }

    impl StorageDocumentTraits for MockStorage {
        fn store_document(&self, doc: &JACSDocument) -> Result<(), JacsError> {
            let key = doc.getkey();
            if self.store_failures.contains(&key) {
                return Err(JacsError::StorageError(format!("store failure: {}", key)));
            }

            self.docs
                .lock()
                .expect("lock docs")
                .insert(key.clone(), doc.clone());
            self.stored_keys.lock().expect("lock stored keys").push(key);
            Ok(())
        }

        fn get_document(&self, key: &str) -> Result<JACSDocument, JacsError> {
            self.fetched_keys
                .lock()
                .expect("lock fetched keys")
                .push(key.to_string());
            if self.get_failures.contains(key) {
                return Err(JacsError::StorageError(format!("get failure: {}", key)));
            }

            self.docs
                .lock()
                .expect("lock docs")
                .get(key)
                .cloned()
                .ok_or_else(|| JacsError::StorageError(format!("missing doc: {}", key)))
        }

        fn remove_document(&self, _key: &str) -> Result<JACSDocument, JacsError> {
            unimplemented!("not needed in these unit tests")
        }

        fn list_documents(&self, _prefix: &str) -> Result<Vec<String>, JacsError> {
            unimplemented!("not needed in these unit tests")
        }

        fn document_exists(&self, _key: &str) -> Result<bool, JacsError> {
            unimplemented!("not needed in these unit tests")
        }

        fn get_documents_by_agent(&self, _agent_id: &str) -> Result<Vec<String>, JacsError> {
            unimplemented!("not needed in these unit tests")
        }

        fn get_document_versions(&self, _document_id: &str) -> Result<Vec<String>, JacsError> {
            unimplemented!("not needed in these unit tests")
        }

        fn get_latest_document(&self, _document_id: &str) -> Result<JACSDocument, JacsError> {
            unimplemented!("not needed in these unit tests")
        }

        fn merge_documents(
            &self,
            _doc_id: &str,
            _v1: &str,
            _v2: &str,
        ) -> Result<JACSDocument, JacsError> {
            unimplemented!("not needed in these unit tests")
        }
    }

    #[test]
    fn parse_document_key_accepts_colons_in_version() {
        let (id, version) = parse_document_key("doc-1:v1:extra").expect("parse key");
        assert_eq!(id, "doc-1");
        assert_eq!(version, "v1:extra");
    }

    #[test]
    fn parse_document_key_rejects_missing_separator() {
        let err = parse_document_key("invalid-key").expect_err("key should be rejected");
        assert!(err.to_string().contains("expected 'id:version'"));
    }

    #[test]
    fn document_from_raw_json_extracts_required_fields() {
        let raw = json!({
            "jacsId": "doc-1",
            "jacsVersion": "v1",
            "jacsType": "config",
            "payload": {"ok": true}
        })
        .to_string();

        let doc = document_from_raw_json(&raw).expect("document from json");
        assert_eq!(doc.id, "doc-1");
        assert_eq!(doc.version, "v1");
        assert_eq!(doc.jacs_type, "config");
        assert_eq!(doc.value["payload"]["ok"], true);
    }

    #[test]
    fn document_from_raw_bytes_extracts_required_fields() {
        let raw = json!({
            "jacsId": "doc-2",
            "jacsVersion": "v2",
            "jacsType": "artifact",
            "content": "hello"
        })
        .to_string();

        let doc = document_from_raw_bytes(raw.as_bytes()).expect("document from bytes");
        assert_eq!(doc.id, "doc-2");
        assert_eq!(doc.version, "v2");
        assert_eq!(doc.jacs_type, "artifact");
    }

    #[test]
    fn extract_signature_agent_id_supports_both_signature_keys() {
        let legacy = json!({
            "jacsSignature": {
                "agentID": "agent-legacy"
            }
        });
        let binding = json!({
            "jacsSignature": {
                "jacsSignatureAgentId": "agent-binding"
            }
        });

        assert_eq!(
            extract_signature_agent_id(&legacy),
            Some("agent-legacy".to_string())
        );
        assert_eq!(
            extract_signature_agent_id(&binding),
            Some("agent-binding".to_string())
        );
    }

    #[test]
    fn get_nested_field_resolves_dot_paths() {
        let value = json!({
            "metadata": {
                "status": {
                    "state": "active"
                }
            }
        });

        assert_eq!(
            get_nested_field(&value, "metadata.status.state"),
            Some(&json!("active"))
        );
        assert!(get_nested_field(&value, "metadata.status.missing").is_none());
    }

    #[test]
    fn field_matches_exact_handles_strings_and_scalars() {
        let value = json!({
            "metadata": {
                "status": "active",
                "priority": 3,
                "published": true
            }
        });

        assert!(field_matches_exact(&value, "metadata.status", "active"));
        assert!(field_matches_exact(&value, "metadata.priority", "3"));
        assert!(field_matches_exact(&value, "metadata.published", "true"));
        assert!(!field_matches_exact(&value, "metadata.status", "inactive"));
    }

    #[test]
    fn build_field_filter_search_results_sets_shape_consistently() {
        let docs = vec![
            make_test_doc("search-1", "v1", "config", None),
            make_test_doc("search-2", "v1", "config", None),
        ];

        let results = build_field_filter_search_results(docs, "metadata.status");

        assert_eq!(results.total_count, 2);
        assert_eq!(results.method, SearchMethod::FieldMatch);
        assert_eq!(results.results.len(), 2);
        assert_eq!(results.results[0].matched_fields, vec!["metadata.status"]);
        assert_eq!(results.results[1].matched_fields, vec!["metadata.status"]);
    }

    #[test]
    fn default_store_documents_returns_keys_in_input_order() {
        let storage = MockStorage::new(Vec::new());
        let docs = vec![
            make_test_doc("bulk-store-2", "v1", "config", None),
            make_test_doc("bulk-store-1", "v1", "config", None),
        ];

        let keys = storage
            .store_documents(docs.clone())
            .expect("store_documents should succeed");

        assert_eq!(keys, vec!["bulk-store-2:v1", "bulk-store-1:v1"]);
        assert_eq!(
            storage
                .stored_keys
                .lock()
                .expect("lock stored keys")
                .clone(),
            vec!["bulk-store-2:v1", "bulk-store-1:v1"]
        );
    }

    #[test]
    fn default_store_documents_aggregates_partial_failures_in_input_order() {
        let storage = MockStorage::new(Vec::new())
            .with_store_failures(&["bulk-store-fail-2:v1", "bulk-store-fail-3:v1"]);
        let docs = vec![
            make_test_doc("bulk-store-fail-1", "v1", "config", None),
            make_test_doc("bulk-store-fail-2", "v1", "config", None),
            make_test_doc("bulk-store-fail-3", "v1", "config", None),
        ];

        let errors = storage
            .store_documents(docs)
            .expect_err("store_documents should aggregate failures");

        assert_eq!(errors.len(), 2);
        assert!(errors[0].to_string().contains("bulk-store-fail-2:v1"));
        assert!(errors[1].to_string().contains("bulk-store-fail-3:v1"));
        assert_eq!(
            storage
                .stored_keys
                .lock()
                .expect("lock stored keys")
                .clone(),
            vec!["bulk-store-fail-1:v1"]
        );
    }

    #[test]
    fn default_get_documents_returns_documents_in_requested_order() {
        let doc_a = make_test_doc("bulk-get-1", "v1", "config", None);
        let doc_b = make_test_doc("bulk-get-2", "v1", "config", None);
        let storage = MockStorage::new(vec![doc_a.clone(), doc_b.clone()]);

        let docs = storage
            .get_documents(vec![doc_b.getkey(), doc_a.getkey()])
            .expect("get_documents should succeed");

        assert_eq!(docs[0].getkey(), "bulk-get-2:v1");
        assert_eq!(docs[1].getkey(), "bulk-get-1:v1");
        assert_eq!(
            storage
                .fetched_keys
                .lock()
                .expect("lock fetched keys")
                .clone(),
            vec!["bulk-get-2:v1", "bulk-get-1:v1"]
        );
    }

    #[test]
    fn default_get_documents_aggregates_partial_failures_in_request_order() {
        let doc_a = make_test_doc("bulk-get-fail-1", "v1", "config", None);
        let doc_b = make_test_doc("bulk-get-fail-2", "v1", "config", None);
        let doc_c = make_test_doc("bulk-get-fail-3", "v1", "config", None);
        let storage = MockStorage::new(vec![doc_a, doc_b, doc_c])
            .with_get_failures(&["bulk-get-fail-1:v1", "bulk-get-fail-3:v1"]);

        let errors = storage
            .get_documents(vec![
                "bulk-get-fail-1:v1".to_string(),
                "bulk-get-fail-2:v1".to_string(),
                "bulk-get-fail-3:v1".to_string(),
            ])
            .expect_err("get_documents should aggregate failures");

        assert_eq!(errors.len(), 2);
        assert!(errors[0].to_string().contains("bulk-get-fail-1:v1"));
        assert!(errors[1].to_string().contains("bulk-get-fail-3:v1"));
        assert_eq!(
            storage
                .fetched_keys
                .lock()
                .expect("lock fetched keys")
                .clone(),
            vec![
                "bulk-get-fail-1:v1",
                "bulk-get-fail-2:v1",
                "bulk-get-fail-3:v1"
            ]
        );
    }
}