dejadb-core 1.0.0

Core .mg format, canonical serialization, content addressing, and grain types for DejaDB.
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use std::collections::HashMap;
use std::sync::LazyLock;

/// Core FIELD_MAP: maps long field names to short (compact) names.
/// OMS 1.2 canonical names — writers MUST emit these.
static FIELD_MAP: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
    let mut m = HashMap::new();
    // Core fields
    m.insert("type", "t");
    m.insert("subject", "s");
    m.insert("relation", "r");
    m.insert("object", "o");
    m.insert("confidence", "c");
    m.insert("source_type", "st");
    m.insert("created_at", "ca");
    m.insert("temporal_type", "tt");
    m.insert("valid_from", "vf");
    m.insert("valid_to", "vt");
    m.insert("system_valid_from", "svf");
    m.insert("system_valid_to", "svt");
    m.insert("context", "ctx");
    m.insert("superseded_by", "sb");
    m.insert("importance", "im");
    m.insert("author_did", "adid");
    m.insert("namespace", "ns");
    m.insert("user_id", "user");
    m.insert("structural_tags", "tags");
    m.insert("derived_from", "df");
    m.insert("consolidation_level", "cl");
    m.insert("success_count", "sc");
    m.insert("failure_count", "fc");
    m.insert("provenance_chain", "pc");
    m.insert("origin_did", "odid");
    m.insert("origin_namespace", "ons");
    m.insert("content_refs", "cr");
    m.insert("embedding_refs", "er");
    m.insert("related_to", "rt");
    m.insert("_elided", "_e");
    m.insert("_disclosure_of", "_do");
    m.insert("invalidation_policy", "ip");
    m.insert("supersession_justification", "sj");
    m.insert("supersession_auth", "sa");

    // verification_status
    m.insert("verification_status", "vstatus");

    // OMS 1.2 new common fields (§6.1)
    m.insert("owner", "own");
    m.insert("category", "cat");
    m.insert("run_id", "rid");
    m.insert("role", "role");
    m.insert("access_count", "ac");
    m.insert("last_accessed_at", "laa");
    m.insert("timestamp_ms", "tms");
    m.insert("observer_did", "obsdid");
    m.insert("subject_did", "sdid");
    m.insert("session_id", "sid2");
    m.insert("entity_id", "eid");
    m.insert("epistemic_status", "epstat");
    m.insert("requires_human_review", "rhr");
    m.insert("processing_basis", "pbasis");
    m.insert("identity_state", "idst");
    m.insert("license", "lic");
    m.insert("trusted_timestamp", "tts");
    m.insert("invalidation_type", "itype");
    m.insert("invalidation_reason", "ireason");
    m.insert("invalidation_initiator", "iinit");
    m.insert("retention_policy", "rpol");
    m.insert("recall_priority", "rpri");

    // Tool fields
    m.insert("tool_name", "tn");
    m.insert("input", "inp"); // was: arguments → args
    m.insert("tool_content", "cnt"); // Tool's content field (distinct from Event's uncompacted "content")
    m.insert("is_error", "iserr"); // was: success → ok (inverted polarity)
    m.insert("error", "err");
    m.insert("duration_ms", "dur");
    m.insert("parent_task_id", "ptid");
    // OMS 1.2 new Tool fields
    m.insert("tool_phase", "aphase");
    m.insert("tool_call_id", "tcid");
    m.insert("call_batch_id", "cbid");
    m.insert("tool_type", "ttype");
    m.insert("tool_version", "tver");
    m.insert("execution_mode", "emode");
    m.insert("code", "code");
    m.insert("stdout", "stdout");
    m.insert("stderr", "stderr");
    m.insert("exit_code", "xcode");
    m.insert("interpreter_id", "iid");
    m.insert("error_type", "etype");
    m.insert("tool_description", "tdesc");
    m.insert("input_schema", "ischema");
    m.insert("strict", "strict");
    m.insert("async_mode", "amode");
    // OMS 1.3 new Tool field
    m.insert("output_schema", "osch");

    // Phase 1 (2026-04-19) — Tool definition fields promoted from extra_fields.
    m.insert("kind", "aknd");
    m.insert("executor_uri", "axu");
    m.insert("locked_params", "lprm");
    m.insert("examples", "exmp");
    m.insert("annotations", "anno");
    m.insert("spec_hash", "shsh");
    // HPL Phase 4.1 (2026-04-22): per-binding executor classifier.
    // Default Host is omitted from the serialized blob (see
    // `serialize.rs`) so pre-HPL grains stay byte-identical.
    m.insert("executor_kind", "exk");

    // Async execution + EU AI Act / failure typing.
    // All Option-typed on Tool; absent on legacy grains. Defaults at
    // deserialize: status=None reads as Completed; the rest stay None.
    m.insert("status", "ast");
    m.insert("correlation_id", "acid");
    m.insert("expires_at_sec", "axp");
    m.insert("transient_definition_hash", "tdh");
    m.insert("failure_cause", "afc");
    m.insert("failure_detail", "afd");
    m.insert("actor_execution_environment", "aex");

    // OMS 1.3 Integration Domain Profile (int:* context map keys)
    // Tool fields (REST API connectors)
    m.insert("int:base_url", "ib");
    m.insert("int:http_method", "ihm"); // "im" collides with "importance"
    m.insert("int:http_path", "ihp"); // "ip" collides with "invalidation_policy"
    m.insert("int:path_params", "ipp");
    m.insert("int:query_params", "iqp");
    m.insert("int:body_params", "ibp");
    m.insert("int:response_mapping", "irm");
    m.insert("int:auth_type", "iat");
    m.insert("int:auth_scopes", "ias");
    m.insert("int:read_only", "iro");
    m.insert("int:connector", "ic");
    m.insert("int:docs_url", "idu");
    m.insert("int:rate_limit", "irl");
    m.insert("int:category", "icat");
    m.insert("int:sunset_date", "isd");
    m.insert("int:content_type", "ict");
    // Trigger-specific fields
    m.insert("int:poll_interval_secs", "ipis");
    m.insert("int:cursor_field", "icf");
    m.insert("int:cursor_type", "icft");
    m.insert("int:webhook_path", "iwp");
    m.insert("int:webhook_secret_header", "iwsh");
    m.insert("int:cron_expression", "icron");
    m.insert("int:timezone", "itz");
    m.insert("int:config_schema", "icfg");
    m.insert("int:event_schema", "ievt");

    // Observation fields
    m.insert("observer_id", "oid");
    m.insert("observer_type", "otype");
    m.insert("frame_id", "fid");
    m.insert("sync_group", "sg");
    m.insert("observation_mode", "omode");
    m.insert("observation_scope", "oscope");
    m.insert("observer_model", "omdl");
    m.insert("compression_ratio", "ocmp");

    // Goal fields
    m.insert("description", "desc");
    m.insert("goal_state", "gs");
    m.insert("criteria", "crit");
    m.insert("criteria_structured", "crs");
    m.insert("priority", "pri");
    m.insert("parent_goals", "pgs");
    m.insert("state_reason", "sr");
    m.insert("satisfaction_evidence", "se");
    m.insert("progress", "prog");
    m.insert("delegate_to", "dto");
    m.insert("delegate_from", "dfo");
    m.insert("expiry_policy", "ep");
    m.insert("recurrence", "rec");
    m.insert("evidence_required", "evreq");
    m.insert("rollback_on_failure", "rof");
    m.insert("allowed_transitions", "atr");
    // OMS 1.2 new Goal fields
    m.insert("depends_on", "depg");
    m.insert("assigned_agent", "asgn");
    m.insert("expected_output", "expout");
    m.insert("output_grain", "outg");
    m.insert("deadline", "dline");

    // Event fields — "content" stays uncompacted
    m.insert("content_blocks", "cblocks");
    m.insert("model_id", "mdl");
    m.insert("stop_reason", "stopr");
    m.insert("token_usage", "toku");
    m.insert("parent_message_id", "pmid");

    // Reasoning fields (OMS 1.2 new grain type)
    m.insert("premises", "prem");
    m.insert("conclusion", "conc");
    m.insert("inference_method", "imethod");
    m.insert("alternatives_considered", "altc");
    m.insert("thinking_content", "think");
    m.insert("thinking_redacted", "tredact");
    m.insert("statistical_context", "statctx");
    m.insert("software_environment", "swenv");
    m.insert("parameter_set", "params");
    m.insert("random_seed", "rseed");

    // Consensus fields (OMS 1.2 new grain type)
    m.insert("participating_observers", "pobs");
    m.insert("threshold", "thr");
    m.insert("agreement_count", "agrc");
    m.insert("dissent_count", "disc");
    m.insert("dissent_grains", "dgrains");
    m.insert("agreed_content", "acnt");

    // Consent fields (OMS 1.2 new grain type)
    // subject_did already mapped above as "sdid"
    m.insert("grantee_did", "gdid");
    m.insert("scope", "scope");
    m.insert("is_withdrawal", "isw");
    m.insert("basis", "basis");
    m.insert("jurisdiction", "jur");
    m.insert("prior_consent", "pcon");
    m.insert("witness_dids", "wdids");

    // Skill fields (OMS 1.4 new grain type, 0x0B). `description` reuses the
    // shared `desc` key (mapped above with Goal); `proficiency` aliases
    // `confidence` (D3) but still carries a dedicated `prof` key on the wire
    // for held instances. 16 keys, collision-audited clean (design §11).
    m.insert("name", "skname");
    m.insert("instructions", "instr");
    m.insert("when_to_use", "wtu");
    m.insert("version", "sver");
    m.insert("allowed_tools", "atls");
    m.insert("resources", "res");
    m.insert("dependencies", "deps");
    m.insert("input_modalities", "imod");
    m.insert("output_modalities", "omod");
    m.insert("domain", "dom");
    m.insert("holder_did", "hdid");
    m.insert("proficiency", "prof");
    m.insert("practice_count", "prcnt");
    m.insert("last_practiced_at", "lpa");
    m.insert("strategies", "strat");
    m.insert("transferable", "xfer");

    // Embedding text override
    m.insert("embedding_text", "et");

    // Scoped memory fields
    m.insert("scope_path", "scp");
    m.insert("scope_depth", "scd");

    // Delegation fields (§6.10)
    m.insert("authorized_namespaces", "ans");
    m.insert("authorized_types", "atypes");
    m.insert("authorized_tools", "atools");
    m.insert("delegation_depth", "ddepth");
    m.insert("delegation_expiry", "dexp");
    m.insert("context_grains", "cgrains");
    m.insert("return_to", "retdid");

    // Event/State/Workflow fields — not compacted per spec
    // "content" stays as "content" (Event, uncompacted)
    // "consolidated" stays as "consolidated"
    // "plan" stays as "plan"
    // "history" stays as "history"
    // "trigger" stays as "trigger"
    // "nodes" stays as "nodes"
    // "edges" stays as "edges"
    // "retries" stays as "retries"
    m.insert("bindings", "bind");

    m
});

/// Reverse map: short name → long name.
static REVERSE_FIELD_MAP: LazyLock<HashMap<&'static str, &'static str>> =
    LazyLock::new(|| FIELD_MAP.iter().map(|(&k, &v)| (v, k)).collect());

/// Content reference nested field map.
static CONTENT_REF_FIELD_MAP: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
    let mut m = HashMap::new();
    m.insert("uri", "u");
    m.insert("modality", "m");
    m.insert("mime_type", "mt");
    m.insert("size_bytes", "sz");
    m.insert("checksum", "ck");
    m.insert("metadata", "md");
    m
});

/// Embedding reference nested field map.
static EMBEDDING_REF_FIELD_MAP: LazyLock<HashMap<&'static str, &'static str>> =
    LazyLock::new(|| {
        let mut m = HashMap::new();
        m.insert("vector_id", "vi");
        m.insert("model", "mo");
        m.insert("dimensions", "dm");
        m.insert("modality_source", "ms");
        m.insert("distance_metric", "di");
        // OMS 1.2 new embedding ref fields
        m.insert("chunk_index", "ci");
        m.insert("chunk_text", "ct");
        m.insert("chunk_strategy", "cs");
        m.insert("chunk_overlap", "co");
        m
    });

/// Related-to nested field map.
static RELATED_TO_FIELD_MAP: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
    let mut m = HashMap::new();
    m.insert("hash", "h");
    m.insert("relation_type", "rl");
    m.insert("weight", "w");
    m
});

/// Workflow edge nested field map.
static WORKFLOW_EDGE_FIELD_MAP: LazyLock<HashMap<&'static str, &'static str>> =
    LazyLock::new(|| {
        let mut m = HashMap::new();
        // src, dst, cond stay as-is (already short)
        m.insert("max_cycles", "mxc");
        m
    });

/// Compact a field name using the FIELD_MAP.
pub fn compact_field(name: &str) -> &str {
    FIELD_MAP.get(name).copied().unwrap_or(name)
}

/// Expand a short field name back to its full name.
pub fn expand_field(short: &str) -> &str {
    REVERSE_FIELD_MAP.get(short).copied().unwrap_or(short)
}

/// Compact content_ref nested fields.
pub fn compact_content_ref_field(name: &str) -> &str {
    CONTENT_REF_FIELD_MAP.get(name).copied().unwrap_or(name)
}

/// Compact embedding_ref nested fields.
pub fn compact_embedding_ref_field(name: &str) -> &str {
    EMBEDDING_REF_FIELD_MAP.get(name).copied().unwrap_or(name)
}

/// Compact related_to nested fields.
pub fn compact_related_to_field(name: &str) -> &str {
    RELATED_TO_FIELD_MAP.get(name).copied().unwrap_or(name)
}

/// Compact workflow edge nested fields.
pub fn compact_workflow_edge_field(name: &str) -> &str {
    WORKFLOW_EDGE_FIELD_MAP.get(name).copied().unwrap_or(name)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compact_field() {
        assert_eq!(compact_field("subject"), "s");
        assert_eq!(compact_field("relation"), "r");
        assert_eq!(compact_field("object"), "o");
        assert_eq!(compact_field("confidence"), "c");
        assert_eq!(compact_field("type"), "t");
        assert_eq!(compact_field("created_at"), "ca");
        assert_eq!(compact_field("namespace"), "ns");
        assert_eq!(compact_field("author_did"), "adid");
        assert_eq!(compact_field("source_type"), "st");
        assert_eq!(compact_field("unknown_field"), "unknown_field");
        // OMS 1.2 fields
        assert_eq!(compact_field("input"), "inp");
        assert_eq!(compact_field("is_error"), "iserr");
        assert_eq!(compact_field("verification_status"), "vstatus");
        assert_eq!(compact_field("premises"), "prem");
        assert_eq!(compact_field("conclusion"), "conc");
        // OMS 1.3 fields
        assert_eq!(compact_field("output_schema"), "osch");
        assert_eq!(compact_field("int:base_url"), "ib");
        assert_eq!(compact_field("int:http_method"), "ihm");
        assert_eq!(compact_field("int:connector"), "ic");
        assert_eq!(compact_field("int:poll_interval_secs"), "ipis");
        assert_eq!(compact_field("int:webhook_path"), "iwp");
        // Async-execution fields
        assert_eq!(compact_field("status"), "ast");
        assert_eq!(compact_field("correlation_id"), "acid");
        assert_eq!(compact_field("expires_at_sec"), "axp");
        assert_eq!(compact_field("transient_definition_hash"), "tdh");
        assert_eq!(compact_field("failure_cause"), "afc");
        assert_eq!(compact_field("failure_detail"), "afd");
        assert_eq!(compact_field("actor_execution_environment"), "aex");
    }

    #[test]
    fn test_expand_field() {
        assert_eq!(expand_field("s"), "subject");
        assert_eq!(expand_field("r"), "relation");
        assert_eq!(expand_field("o"), "object");
        assert_eq!(expand_field("t"), "type");
        assert_eq!(expand_field("unknown"), "unknown");
        // OMS 1.2 fields
        assert_eq!(expand_field("inp"), "input");
        assert_eq!(expand_field("iserr"), "is_error");
        assert_eq!(expand_field("vstatus"), "verification_status");
        // OMS 1.1 aliases removed — these should pass through unchanged
        assert_eq!(expand_field("args"), "args");
        assert_eq!(expand_field("ok"), "ok");
        assert_eq!(expand_field("ct"), "ct");
        // OMS 1.3 fields
        assert_eq!(expand_field("osch"), "output_schema");
        assert_eq!(expand_field("ib"), "int:base_url");
        assert_eq!(expand_field("ihm"), "int:http_method");
        assert_eq!(expand_field("ic"), "int:connector");
        assert_eq!(expand_field("ipis"), "int:poll_interval_secs");
        // Async-execution fields
        assert_eq!(expand_field("ast"), "status");
        assert_eq!(expand_field("acid"), "correlation_id");
        assert_eq!(expand_field("axp"), "expires_at_sec");
        assert_eq!(expand_field("tdh"), "transient_definition_hash");
        assert_eq!(expand_field("afc"), "failure_cause");
        assert_eq!(expand_field("afd"), "failure_detail");
        assert_eq!(expand_field("aex"), "actor_execution_environment");
    }

    #[test]
    fn test_nested_compaction() {
        assert_eq!(compact_content_ref_field("uri"), "u");
        assert_eq!(compact_content_ref_field("mime_type"), "mt");
        assert_eq!(compact_embedding_ref_field("vector_id"), "vi");
        assert_eq!(compact_related_to_field("hash"), "h");
        assert_eq!(compact_related_to_field("relation_type"), "rl");
        // OMS 1.2 new embedding ref fields
        assert_eq!(compact_embedding_ref_field("chunk_index"), "ci");
        assert_eq!(compact_embedding_ref_field("chunk_text"), "ct");
    }

    #[test]
    fn test_no_compact_key_collisions() {
        // Verify every compact key maps to exactly one long name.
        // A collision means REVERSE_FIELD_MAP lost an entry.
        let map = &*FIELD_MAP;
        let reverse = &*REVERSE_FIELD_MAP;
        assert_eq!(map.len(), reverse.len(),
            "FIELD_MAP has {} entries but REVERSE_FIELD_MAP has {} — compact key collision detected",
            map.len(), reverse.len());
    }

    #[test]
    fn test_integration_profile_roundtrip() {
        // All 25 int:* keys must roundtrip through compact → expand
        let int_keys = [
            "int:base_url",
            "int:http_method",
            "int:http_path",
            "int:path_params",
            "int:query_params",
            "int:body_params",
            "int:response_mapping",
            "int:auth_type",
            "int:auth_scopes",
            "int:read_only",
            "int:connector",
            "int:docs_url",
            "int:rate_limit",
            "int:category",
            "int:sunset_date",
            "int:content_type",
            "int:poll_interval_secs",
            "int:cursor_field",
            "int:cursor_type",
            "int:webhook_path",
            "int:webhook_secret_header",
            "int:cron_expression",
            "int:timezone",
            "int:config_schema",
            "int:event_schema",
        ];
        for key in &int_keys {
            let short = compact_field(key);
            assert_ne!(short, *key, "int:* key '{}' has no compact mapping", key);
            let expanded = expand_field(short);
            assert_eq!(
                expanded, *key,
                "roundtrip failed for '{}' → '{}' → '{}'",
                key, short, expanded
            );
        }
    }
}