khive-request 0.10.0

Request DSL — parses verb-dispatch strings (function-call, JSON, and future LNDL / pipe / bash-style syntaxes) into a transport-agnostic ParsedRequest AST
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use std::collections::{BTreeMap, HashMap};
use std::ops::Range;

use serde_json::Value;

use crate::types::{ArgValue, ParsedOp};

#[cfg(test)]
use crate::types::{DslError, ExecutionMode, ParsedRequest};

/// One cross-unit static write-key conflict: `leaf` (inside the unit this
/// entry is filed under) claims the same statically knowable write key as
/// `other_leaf`, which belongs to a *different* unit, `other_unit`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnitKeyConflict {
    pub key: String,
    pub leaf: usize,
    pub other_unit: usize,
    pub other_leaf: usize,
}

/// Finds every statically knowable write-key collision between *different*
/// units of a bracketed batch of chains (ADR-016 Amendment 2).
///
/// A key repeated by two leaves of the *same* unit is legal, because a chain's own
/// leaves are already ordered, and is never reported. A key claimed by leaves
/// in two different units is reported once per affected unit (symmetric:
/// both units get an entry), each naming the other unit's conflicting global
/// leaf position, so a caller can refuse both units before any of their
/// leaves dispatch. Units absent from the returned map share no cross-unit
/// key with any other unit.
pub fn unit_write_key_conflicts(
    ops: &[ParsedOp],
    ranges: &[Range<usize>],
) -> BTreeMap<usize, Vec<UnitKeyConflict>> {
    // Per unit, collapse repeated in-unit keys to their first-occurring leaf
    // (deterministic: ranges are walked in leaf order regardless of the
    // arbitrary hash order `write_keys_for_op_pub` or a later `HashMap`
    // iteration might otherwise introduce).
    let mut per_unit_keys: Vec<HashMap<String, usize>> = Vec::with_capacity(ranges.len());
    for range in ranges {
        let mut keys: HashMap<String, usize> = HashMap::new();
        for leaf in range.clone() {
            for key in write_keys_for_op_pub(&ops[leaf]) {
                keys.entry(key).or_insert(leaf);
            }
        }
        per_unit_keys.push(keys);
    }

    let mut first_claim: HashMap<String, (usize, usize)> = HashMap::new();
    let mut conflicts: BTreeMap<usize, Vec<UnitKeyConflict>> = BTreeMap::new();
    for (unit_index, keys) in per_unit_keys.iter().enumerate() {
        for (key, &leaf) in keys {
            match first_claim.get(key) {
                Some(&(other_unit, other_leaf)) if other_unit != unit_index => {
                    conflicts
                        .entry(unit_index)
                        .or_default()
                        .push(UnitKeyConflict {
                            key: key.clone(),
                            leaf,
                            other_unit,
                            other_leaf,
                        });
                    conflicts
                        .entry(other_unit)
                        .or_default()
                        .push(UnitKeyConflict {
                            key: key.clone(),
                            leaf: other_leaf,
                            other_unit: unit_index,
                            other_leaf: leaf,
                        });
                }
                Some(_) => {}
                None => {
                    first_claim.insert(key.clone(), (unit_index, leaf));
                }
            }
        }
    }
    for entries in conflicts.values_mut() {
        entries.sort_by_key(|c| (c.leaf, c.other_leaf));
        entries.dedup();
    }
    conflicts
}

/// Extracts statically knowable, substrate-prefixed write-conflict keys for `op`.
///
/// Missing, dynamic, or non-string targets contribute no key. The result order
/// follows the tool's argument/entry order.
/// See `crates/khive-request/docs/api/write-conflicts.md` for key formats.
pub fn write_keys_for_op_pub(op: &ParsedOp) -> Vec<String> {
    let mut keys = Vec::new();
    match op.tool.as_str() {
        "update" | "delete" => {
            if let Some(ArgValue::Value(Value::String(s))) = op.args.get("id") {
                keys.push(format!("entity:{s}"));
            }
        }
        "merge" => {
            // A dry-run merge reads the pair and returns a prediction, so it targets
            // no stored record and declares no key. Only a literal `true` reads as a
            // preview: absent, `false` and a non-boolean value keep the keys, on the
            // same static-knowability rule the rest of this builder follows, so an
            // argument this cannot read as a preview stays conservative.
            let previews_without_writing = matches!(
                op.args.get("dry_run"),
                Some(ArgValue::Value(Value::Bool(true)))
            );
            if !previews_without_writing {
                for name in &["into_id", "from_id"] {
                    if let Some(ArgValue::Value(Value::String(s))) = op.args.get(*name) {
                        keys.push(format!("entity:{s}"));
                    }
                }
            }
        }
        "link" => {
            // Edge keys must not collide with their endpoint entities.
            // `source`, `target` and `kind` are accepted spellings of `source_id`,
            // `target_id` and `relation`. Each field reads the canonical name first
            // and the alias only when the canonical name is absent, so both
            // spellings of one edge claim the same key. A call giving both
            // spellings of a field is refused by the handler, so which one this
            // reads for it does not matter.
            let link_arg = |canonical: &str, alias: &str| {
                op.args.get(canonical).or_else(|| op.args.get(alias))
            };
            if let (
                Some(ArgValue::Value(Value::String(s))),
                Some(ArgValue::Value(Value::String(t))),
                Some(ArgValue::Value(Value::String(r))),
            ) = (
                link_arg("source_id", "source"),
                link_arg("target_id", "target"),
                link_arg("relation", "kind"),
            ) {
                push_link_key(&mut keys, s, t, r);
            }

            // Bulk and singleton links share natural keys so equivalent writes collide.
            if let Some(ArgValue::Value(Value::Array(links))) = op.args.get("links") {
                for link in links {
                    let Some(obj) = link.as_object() else {
                        continue;
                    };
                    let (Some(s), Some(t), Some(r)) = (
                        obj.get("source_id").and_then(Value::as_str),
                        obj.get("target_id").and_then(Value::as_str),
                        obj.get("relation").and_then(Value::as_str),
                    ) else {
                        continue;
                    };
                    push_link_key(&mut keys, s, t, r);
                }
            }
        }
        _ => {}
    }
    keys
}

/// Adds a natural edge key, ordering endpoints for known symmetric relations.
fn push_link_key(keys: &mut Vec<String>, source: &str, target: &str, relation: &str) {
    let relation_key = canonical_relation_key(relation);
    let (source_key, target_key) = if is_static_symmetric_relation(&relation_key) && target < source
    {
        (target, source)
    } else {
        (source, target)
    };
    keys.push(format!(
        "edge-natural:{source_key}:{target_key}:{relation_key}"
    ));
}

/// Normalizes relation spelling without adding a `khive-types` dependency.
fn canonical_relation_key(relation: &str) -> String {
    let normalized: String = relation
        .chars()
        .map(|c| {
            if c == '-' {
                '_'
            } else {
                c.to_ascii_lowercase()
            }
        })
        .filter(|c| c.is_ascii_alphanumeric() || *c == '_')
        .collect();
    match normalized.as_str() {
        "competeswith" => "competes_with".to_string(),
        "composedwith" => "composed_with".to_string(),
        _ => normalized,
    }
}

/// Returns whether the deliberately conservative local relation set is symmetric.
fn is_static_symmetric_relation(relation: &str) -> bool {
    matches!(relation, "competes_with" | "composed_with")
}

/// Scans a parsed batch for duplicate write keys; ordered chains are exempt.
#[cfg(test)]
pub(crate) fn check_write_key_conflicts(req: &ParsedRequest) -> Result<(), DslError> {
    if req.mode == ExecutionMode::Chain {
        return Ok(());
    }
    let mut seen: std::collections::HashMap<String, String> = std::collections::HashMap::new();
    for op in &req.ops {
        let keys = write_keys_for_op_pub(op);
        for key in keys {
            if let Some(first) = seen.get(&key) {
                return Err(DslError::WriteKeyConflict {
                    id: key,
                    first_op: first.clone(),
                    second_op: op.tool.clone(),
                });
            }
            seen.insert(key, op.tool.clone());
        }
    }
    Ok(())
}

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

    #[test]
    fn no_conflict_on_non_write_ops() {
        let r =
            parse_request(r#"[list(kind="entity"), search(kind="entity", query="x")]"#).unwrap();
        check_write_key_conflicts(&r).unwrap();
    }

    #[test]
    fn update_and_delete_same_id_conflict() {
        let r =
            parse_request(r#"[update(id="abc-123", name="new"), delete(id="abc-123")]"#).unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, first_op, second_op }
                if id == "entity:abc-123" && first_op == "update" && second_op == "delete"),
            "expected WriteKeyConflict with entity-prefixed key, got {err:?}"
        );
    }

    #[test]
    fn two_updates_same_id_conflict() {
        let r = parse_request(
            r#"[update(id="uuid-1", name="a"), update(id="uuid-1", description="b")]"#,
        )
        .unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, .. } if id == "entity:uuid-1"),
            "expected WriteKeyConflict with entity-prefixed key, got {err:?}"
        );
    }

    #[test]
    fn merge_from_id_conflicts_with_delete() {
        let r =
            parse_request(r#"[merge(into_id="new-id", from_id="old-id"), delete(id="old-id")]"#)
                .unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, .. } if id == "entity:old-id"),
            "expected WriteKeyConflict with entity-prefixed key, got {err:?}"
        );
    }

    #[test]
    fn two_dry_run_merges_in_one_batch_do_not_conflict() {
        // The refusal this closes: two previews over the same pair were rejected as
        // overlapping writes and had to be issued as sequential singleton calls.
        let r = parse_request(
            r#"[merge(into_id="a-id", from_id="b-id", dry_run=true), merge(into_id="a-id", from_id="b-id", dry_run=true)]"#,
        )
        .unwrap();
        check_write_key_conflicts(&r).unwrap();
    }

    #[test]
    fn a_dry_run_merge_does_not_conflict_with_a_delete_of_its_from_id() {
        // The same pair as merge_from_id_conflicts_with_delete above, which is the
        // control: the only difference between the two cases is the preview flag.
        let r = parse_request(
            r#"[merge(into_id="new-id", from_id="old-id", dry_run=true), delete(id="old-id")]"#,
        )
        .unwrap();
        check_write_key_conflicts(&r).unwrap();
    }

    #[test]
    fn dry_run_false_keeps_the_merge_keys() {
        let r = parse_request(
            r#"[merge(into_id="new-id", from_id="old-id", dry_run=false), delete(id="old-id")]"#,
        )
        .unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, .. } if id == "entity:old-id"),
            "a merge that writes must keep its keys, got {err:?}"
        );
    }

    #[test]
    fn a_non_boolean_dry_run_keeps_the_merge_keys() {
        // A string is not the preview the handler acts on, so reading it as one here
        // would drop the keys of an op that goes on to write.
        let r = parse_request(
            r#"[merge(into_id="new-id", from_id="old-id", dry_run="true"), delete(id="old-id")]"#,
        )
        .unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, .. } if id == "entity:old-id"),
            "a non-boolean dry_run must stay conservative, got {err:?}"
        );
    }

    #[test]
    fn different_ids_no_conflict() {
        let r = parse_request(
            r#"[update(id="id-1", name="a"), delete(id="id-2"), update(id="id-3", name="c")]"#,
        )
        .unwrap();
        check_write_key_conflicts(&r).unwrap();
    }

    #[test]
    fn chain_mode_skips_conflict_detection() {
        let r = parse_request(r#"update(id="same-id", name="a") | delete(id="same-id")"#).unwrap();
        assert_eq!(r.mode, ExecutionMode::Chain);
        check_write_key_conflicts(&r).unwrap();
    }

    #[test]
    fn link_source_id_does_not_conflict_with_entity_update() {
        let r = parse_request(
            r#"[update(id="node-1", name="x"), link(source_id="node-1", target_id="node-2", relation="extends")]"#,
        )
        .unwrap();
        check_write_key_conflicts(&r).unwrap();
    }

    #[test]
    fn two_links_same_natural_key_conflict() {
        let r = parse_request(
            r#"[link(source_id="a", target_id="b", relation="extends"), link(source_id="a", target_id="b", relation="extends")]"#,
        )
        .unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, .. }
                if id == "edge-natural:a:b:extends"),
            "expected WriteKeyConflict on edge-natural key, got {err:?}"
        );
    }

    #[test]
    fn mixed_spelling_links_to_one_edge_conflict_like_the_canonical_spelling() {
        // Both spellings of the same edge must produce the same key: the
        // canonical pair is the control, and each mixed ordering is the case a
        // key builder that only knows the canonical names lets through.
        for ops in [
            r#"[link(source_id="a", target_id="b", relation="extends", weight=0.1), link(source_id="a", target_id="b", relation="extends", weight=0.9)]"#,
            r#"[link(source_id="a", target_id="b", relation="extends", weight=0.1), link(source="a", target="b", kind="extends", weight=0.9)]"#,
            r#"[link(source="a", target="b", kind="extends", weight=0.1), link(source_id="a", target_id="b", relation="extends", weight=0.9)]"#,
        ] {
            let r = parse_request(ops).unwrap();
            let err = check_write_key_conflicts(&r).unwrap_err();
            assert!(
                matches!(&err, DslError::WriteKeyConflict { id, first_op, second_op }
                    if id == "edge-natural:a:b:extends"
                        && first_op == "link"
                        && second_op == "link"),
                "{ops} must conflict on the edge key, got {err:?}"
            );
        }
    }

    #[test]
    fn aliased_only_links_to_one_edge_conflict() {
        let r = parse_request(
            r#"[link(source="a", target="b", kind="extends"), link(source="a", target="b", kind="extends")]"#,
        )
        .unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, .. }
                if id == "edge-natural:a:b:extends"),
            "expected WriteKeyConflict on edge-natural key, got {err:?}"
        );
    }

    #[test]
    fn each_link_field_reads_its_own_alias_into_the_canonical_key() {
        // A directional relation, so an alias read into the wrong endpoint slot
        // would yield `b:a` rather than `a:b`.
        for ops in [
            r#"link(source="a", target="b", kind="extends")"#,
            r#"link(source="a", target_id="b", relation="extends")"#,
            r#"link(source_id="a", target="b", relation="extends")"#,
            r#"link(source_id="a", target_id="b", kind="extends")"#,
        ] {
            let r = parse_request(ops).unwrap();
            assert_eq!(
                write_keys_for_op_pub(&r.ops[0]),
                vec!["edge-natural:a:b:extends".to_string()],
                "{ops}"
            );
        }
    }

    #[test]
    fn single_write_op_no_conflict() {
        let r = parse_request(r#"delete(id="solo-id")"#).unwrap();
        assert_eq!(r.mode, ExecutionMode::Single);
        check_write_key_conflicts(&r).unwrap();
    }

    #[test]
    fn bulk_link_and_singleton_same_natural_key_conflict() {
        let r = parse_request(
            r#"[link(links=[{"source_id":"a","target_id":"b","relation":"extends","weight":0.1}]), link(source_id="a", target_id="b", relation="extends", weight=0.9)]"#,
        )
        .unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, .. }
                if id == "edge-natural:a:b:extends"),
            "expected WriteKeyConflict on edge-natural key, got {err:?}"
        );
    }

    #[test]
    fn reversed_symmetric_links_conflict() {
        let r = parse_request(
            r#"[link(source_id="b", target_id="a", relation="competes_with"), link(source_id="a", target_id="b", relation="competes_with")]"#,
        )
        .unwrap();
        let err = check_write_key_conflicts(&r).unwrap_err();
        assert!(
            matches!(&err, DslError::WriteKeyConflict { id, .. }
                if id == "edge-natural:a:b:competes_with"),
            "expected WriteKeyConflict on canonicalized symmetric key, got {err:?}"
        );
    }

    #[test]
    fn reversed_non_symmetric_links_do_not_conflict() {
        let r = parse_request(
            r#"[link(source_id="b", target_id="a", relation="extends"), link(source_id="a", target_id="b", relation="extends")]"#,
        )
        .unwrap();
        check_write_key_conflicts(&r).unwrap();
    }

    #[test]
    fn write_keys_for_op_pub_bulk_link_extracts_all_edges() {
        let r = parse_request(
            r#"link(links=[{"source_id":"a","target_id":"b","relation":"extends"},{"source_id":"c","target_id":"d","relation":"extends"}])"#,
        )
        .unwrap();
        let keys = write_keys_for_op_pub(&r.ops[0]);
        assert_eq!(
            keys,
            vec![
                "edge-natural:a:b:extends".to_string(),
                "edge-natural:c:d:extends".to_string(),
            ]
        );
    }

    #[test]
    fn unit_conflict_refuses_both_units_sharing_a_key() {
        let r = parse_request(
            r#"[update(id="x", name="1") | update(id="y", name="2"), update(id="x", name="3")]"#,
        )
        .unwrap();
        assert_eq!(r.ranges, vec![0..2, 2..3]);
        let conflicts = unit_write_key_conflicts(&r.ops, &r.ranges);
        assert_eq!(
            conflicts.len(),
            2,
            "both units must be named: {conflicts:?}"
        );
        assert_eq!(
            conflicts[&0],
            vec![UnitKeyConflict {
                key: "entity:x".to_string(),
                leaf: 0,
                other_unit: 1,
                other_leaf: 2,
            }]
        );
        assert_eq!(
            conflicts[&1],
            vec![UnitKeyConflict {
                key: "entity:x".to_string(),
                leaf: 2,
                other_unit: 0,
                other_leaf: 0,
            }]
        );
    }

    #[test]
    fn same_key_repeated_within_one_unit_is_not_reported() {
        let r = parse_request(r#"[update(id="x", name="1") | update(id="x", name="2")]"#).unwrap();
        assert_eq!(r.ranges.len(), 1);
        assert_eq!(r.ranges[0], 0..2);
        let conflicts = unit_write_key_conflicts(&r.ops, &r.ranges);
        assert!(
            conflicts.is_empty(),
            "a chain's own ordered leaves must not conflict with each other: {conflicts:?}"
        );
    }

    #[test]
    fn disjoint_units_share_no_conflict() {
        let r = parse_request(r#"[update(id="x", name="1"), update(id="y", name="2")]"#).unwrap();
        assert_eq!(r.ranges, vec![0..1, 1..2]);
        assert!(unit_write_key_conflicts(&r.ops, &r.ranges).is_empty());
    }

    #[test]
    fn three_way_unit_conflict_names_every_affected_unit() {
        let r = parse_request(
            r#"[update(id="x", name="1"), update(id="x", name="2"), update(id="x", name="3")]"#,
        )
        .unwrap();
        let conflicts = unit_write_key_conflicts(&r.ops, &r.ranges);
        assert_eq!(conflicts.len(), 3, "{conflicts:?}");
    }
}