lex-vcs 0.11.34

Agent-native version control: typed op log + attestation graph.
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
//! AST-level structural diff between two sets of `FnDecl`s.
//!
//! Moved from `lex-cli/src/diff.rs` so both the CLI (`lex diff`
//! command) and the HTTP API (`lex serve`) can compute a [`DiffReport`]
//! without introducing a circular dependency. `lex-vcs` is the right
//! home because it already owns `DiffReport` and `diff_to_ops`.

use crate::diff_report::{
    AddRemove, BodyPatch, DiffReport, EffectChanges, Modified, Renamed,
};
use lex_ast::{stage_canonical_hash_hex, CExpr, Effect, EffectArg, FnDecl, Stage, TypeDecl, TypeExpr};
use std::collections::{BTreeMap, BTreeSet, HashMap};

/// Compute a structural diff between two named fn-decl maps.
///
/// `body_patches` controls whether body-level expression diffs are
/// emitted inside each `Modified` entry. Pass `true` for rich output
/// (CLI / review); `false` for a signature-only diff (faster).
///
/// Function-only: type declarations are not considered. Use
/// [`compute_diff_with_types`] on the publish path, where the op log
/// must capture `type` declarations too (else `export-git` cannot
/// reproduce a compilable module — see alpibrusl/lex-lang#895).
pub fn compute_diff(
    a: &BTreeMap<String, FnDecl>,
    b: &BTreeMap<String, FnDecl>,
    body_patches: bool,
) -> DiffReport {
    compute_diff_with_types(a, b, &BTreeMap::new(), &BTreeMap::new(), body_patches)
}

/// Like [`compute_diff`], but also diffs top-level `type` declarations,
/// emitting added / removed / modified entries for them so `diff_to_ops`
/// produces `AddType` / `RemoveType` / `ModifyType` ops. A type's
/// `signature` is rendered `type Name = …` (the `"type "` prefix is how
/// `diff_to_ops` tells a type removal from a function removal), and its
/// `old_sig_id` is the `SigId` of the old `TypeDecl` stage. Rename
/// detection is intentionally function-only; a renamed type reads as a
/// remove + add, which reproduces it correctly.
pub fn compute_diff_with_types(
    a: &BTreeMap<String, FnDecl>,
    b: &BTreeMap<String, FnDecl>,
    a_types: &BTreeMap<String, TypeDecl>,
    b_types: &BTreeMap<String, TypeDecl>,
    body_patches: bool,
) -> DiffReport {
    let mut report = DiffReport::default();
    let names_a: BTreeSet<&String> = a.keys().collect();
    let names_b: BTreeSet<&String> = b.keys().collect();

    let only_a: Vec<&String> = names_a.difference(&names_b).copied().collect();
    let only_b: Vec<&String> = names_b.difference(&names_a).copied().collect();

    // Detect renames: for each name only-in-A, check if any only-in-B
    // has a body whose canonical-AST hash matches (modulo the fn
    // name itself). sig_id over the FnDecl with name normalized
    // serves as the structural-identity key.
    //
    // `body_hash` clones the FnDecl and does a full canonical
    // serialize + SHA-256 — not O(1). The naive nested loop below
    // used to recompute `body_hash(fb)` on every outer (`only_a`)
    // iteration even though `fb` never changes, making this
    // O(|only_a| * |only_b|) hashes instead of O(|only_a| + |only_b|).
    // Harmless at the tiny scale these sets used to have, but
    // `pkg_publish_handler` calls this once per uploaded file with
    // `only_a` sized to the *entire* tenant's historical function
    // set — that combination hung the server for 38+ minutes on a
    // real publish (alpibrusl/lex-lang#813). Precomputing each
    // `only_b` hash once turns the nested loop into O(1) lookups.
    let mut hash_to_bs: HashMap<String, Vec<&String>> = HashMap::new();
    for &bn in &only_b {
        hash_to_bs.entry(body_hash(&b[bn])).or_default().push(bn);
    }
    let mut renamed_pairs: Vec<(String, String)> = Vec::new();
    let mut consumed_a: BTreeSet<String> = BTreeSet::new();
    let mut consumed_b: BTreeSet<String> = BTreeSet::new();
    for &an in &only_a {
        let fa = &a[an];
        let fa_norm_id = body_hash(fa);
        let Some(candidates) = hash_to_bs.get(&fa_norm_id) else { continue };
        let Some(&bn) = candidates.iter().find(|bn| !consumed_b.contains(**bn)) else { continue };
        renamed_pairs.push((an.clone(), bn.clone()));
        consumed_a.insert(an.clone());
        consumed_b.insert(bn.clone());
    }

    for &n in &only_a {
        if consumed_a.contains(n) { continue; }
        let fd = &a[n];
        report.removed.push(AddRemove {
            name: n.clone(),
            signature: render_signature(fd),
            old_sig_id: lex_ast::sig_id(&Stage::FnDecl(fd.clone())),
        });
    }
    for &n in &only_b {
        if consumed_b.contains(n) { continue; }
        let fd = &b[n];
        report.added.push(AddRemove {
            name: n.clone(),
            signature: render_signature(fd),
            old_sig_id: None,
        });
    }
    for (an, bn) in &renamed_pairs {
        let fa = &a[an];
        let fd = &b[bn];
        report.renamed.push(Renamed {
            from: an.clone(),
            to: bn.clone(),
            signature: render_signature(fd),
            old_sig_id: lex_ast::sig_id(&Stage::FnDecl(fa.clone())).unwrap_or_default(),
        });
    }

    // Modified: same name on both sides; compare bodies.
    for n in names_a.intersection(&names_b) {
        let fa = &a[*n];
        let fb = &b[*n];
        let sig_a = render_signature(fa);
        let sig_b = render_signature(fb);
        if body_hash(fa) == body_hash(fb) && sig_a == sig_b { continue; }

        let patches = if body_patches {
            let mut patches = Vec::new();
            diff_expr(&fa.body, &fb.body, "body", &mut patches, 4);
            patches
        } else { Vec::new() };

        let effect_changes = effect_diff(&fa.effects, &fb.effects);
        report.modified.push(Modified {
            name: (*n).clone(),
            signature_before: sig_a.clone(),
            signature_after: sig_b.clone(),
            signature_changed: sig_a != sig_b,
            effect_changes,
            body_patches: patches,
            old_sig_id: lex_ast::sig_id(&Stage::FnDecl(fa.clone())).unwrap_or_default(),
        });
    }

    // Types. Added / removed by name; a same-name pair whose canonical
    // hash differs is a modification (its `SigId` — name + type params,
    // not the definition — stays put across a body change). A pair whose
    // `SigId` differs too (e.g. gained a type parameter) reads as a
    // remove + add, since the old sig can't be `ModifyType`d into the new
    // one. No rename detection for types (rare; remove + add reproduces).
    let tnames_a: BTreeSet<&String> = a_types.keys().collect();
    let tnames_b: BTreeSet<&String> = b_types.keys().collect();
    for n in tnames_a.difference(&tnames_b) {
        let td = &a_types[*n];
        report.removed.push(AddRemove {
            name: (*n).clone(),
            signature: render_type_signature(td),
            old_sig_id: lex_ast::sig_id(&Stage::TypeDecl(td.clone())),
        });
    }
    for n in tnames_b.difference(&tnames_a) {
        let td = &b_types[*n];
        report.added.push(AddRemove {
            name: (*n).clone(),
            signature: render_type_signature(td),
            old_sig_id: None,
        });
    }
    for n in tnames_a.intersection(&tnames_b) {
        let ta = &a_types[*n];
        let tb = &b_types[*n];
        let hash_a = stage_canonical_hash_hex(&Stage::TypeDecl(ta.clone()));
        let hash_b = stage_canonical_hash_hex(&Stage::TypeDecl(tb.clone()));
        if hash_a == hash_b { continue; }
        let sig_a = lex_ast::sig_id(&Stage::TypeDecl(ta.clone()));
        let sig_b = lex_ast::sig_id(&Stage::TypeDecl(tb.clone()));
        if sig_a != sig_b {
            // Structural identity changed (type params): can't modify in
            // place — drop the old, introduce the new.
            report.removed.push(AddRemove {
                name: (*n).clone(),
                signature: render_type_signature(ta),
                old_sig_id: sig_a,
            });
            report.added.push(AddRemove {
                name: (*n).clone(),
                signature: render_type_signature(tb),
                old_sig_id: None,
            });
        } else {
            report.modified.push(Modified {
                name: (*n).clone(),
                signature_before: render_type_signature(ta),
                signature_after: render_type_signature(tb),
                signature_changed: true,
                effect_changes: EffectChanges::default(),
                body_patches: Vec::new(),
                old_sig_id: sig_a.unwrap_or_default(),
            });
        }
    }
    report
}

/// Hash of the function's structural identity, used for rename
/// detection. Excludes the function's name (so `fn foo -> Int { 1 }`
/// and `fn bar -> Int { 1 }` share a hash) but includes everything
/// else: params, effects, return type, body.
fn body_hash(fd: &FnDecl) -> String {
    let mut anon = fd.clone();
    anon.name = String::new();
    let stage = Stage::FnDecl(anon);
    stage_canonical_hash_hex(&stage)
}

/// Walk two CExprs in parallel; record the first divergence at each
/// child position. `depth` caps recursion so a tiny per-fn diff
/// doesn't degenerate into hundreds of micro-changes.
fn diff_expr(a: &CExpr, b: &CExpr, path: &str, out: &mut Vec<BodyPatch>, depth: u32) {
    if depth == 0 { return; }
    let kind_a = node_kind(a);
    let kind_b = node_kind(b);
    if kind_a != kind_b {
        out.push(BodyPatch {
            op: "Replace".into(), node_path: path.into(),
            from_kind: kind_a.into(), to_kind: kind_b.into(),
        });
        return;
    }
    // Same kind: recurse into structurally-equivalent children.
    match (a, b) {
        (CExpr::Literal { value: la }, CExpr::Literal { value: lb }) => {
            if la != lb {
                out.push(BodyPatch {
                    op: "Replace".into(), node_path: path.into(),
                    from_kind: "Literal".into(), to_kind: "Literal".into(),
                });
            }
        }
        (CExpr::Var { name: na }, CExpr::Var { name: nb }) => {
            if na != nb {
                out.push(BodyPatch {
                    op: "Replace".into(), node_path: path.into(),
                    from_kind: format!("Var({na})"), to_kind: format!("Var({nb})"),
                });
            }
        }
        (CExpr::Call { callee: ca, args: aa },
         CExpr::Call { callee: cb, args: ab }) => {
            diff_expr(ca, cb, &format!("{path}.callee"), out, depth - 1);
            diff_args(aa, ab, &format!("{path}.args"), out, depth);
        }
        (CExpr::Let { name: na, value: va, body: ba, .. },
         CExpr::Let { name: nb, value: vb, body: bb, .. }) => {
            if na != nb {
                out.push(BodyPatch {
                    op: "Replace".into(),
                    node_path: format!("{path}.name"),
                    from_kind: format!("Let({na})"),
                    to_kind:   format!("Let({nb})"),
                });
            }
            diff_expr(va, vb, &format!("{path}.value"), out, depth - 1);
            diff_expr(ba, bb, &format!("{path}.body"),  out, depth - 1);
        }
        (CExpr::Match { scrutinee: sa, arms: ams },
         CExpr::Match { scrutinee: sb, arms: bms }) => {
            diff_expr(sa, sb, &format!("{path}.scrutinee"), out, depth - 1);
            let n = ams.len().max(bms.len());
            for i in 0..n {
                let p = format!("{path}.arms[{i}]");
                match (ams.get(i), bms.get(i)) {
                    (Some(a), Some(b)) =>
                        diff_expr(&a.body, &b.body, &p, out, depth - 1),
                    (Some(_), None) => out.push(BodyPatch {
                        op: "Deleted".into(), node_path: p,
                        from_kind: "MatchArm".into(), to_kind: "(removed)".into(),
                    }),
                    (None, Some(_)) => out.push(BodyPatch {
                        op: "Inserted".into(), node_path: p,
                        from_kind: "(none)".into(), to_kind: "MatchArm".into(),
                    }),
                    (None, None) => break,
                }
            }
        }
        (CExpr::Block { statements: sa, result: ra },
         CExpr::Block { statements: sb, result: rb }) => {
            diff_args(sa, sb, &format!("{path}.statements"), out, depth);
            diff_expr(ra, rb, &format!("{path}.result"), out, depth - 1);
        }
        (CExpr::FieldAccess { value: va, field: fa },
         CExpr::FieldAccess { value: vb, field: fb }) => {
            diff_expr(va, vb, &format!("{path}.value"), out, depth - 1);
            if fa != fb {
                out.push(BodyPatch {
                    op: "Replace".into(), node_path: format!("{path}.field"),
                    from_kind: format!("Field({fa})"), to_kind: format!("Field({fb})"),
                });
            }
        }
        (CExpr::Lambda { body: ba, .. }, CExpr::Lambda { body: bb, .. }) => {
            diff_expr(ba, bb, &format!("{path}.body"), out, depth - 1);
        }
        // For shapes we don't unfold further, mark the node itself
        // as edited (same kind, content differs) — finer detail can
        // come in a follow-up.
        _ => {
            out.push(BodyPatch {
                op: "Replace".into(), node_path: path.into(),
                from_kind: kind_a.into(), to_kind: kind_b.into(),
            });
        }
    }
}

fn diff_args(a: &[CExpr], b: &[CExpr], path: &str, out: &mut Vec<BodyPatch>, depth: u32) {
    let n = a.len().max(b.len());
    for i in 0..n {
        let p = format!("{path}[{i}]");
        match (a.get(i), b.get(i)) {
            (Some(x), Some(y)) => diff_expr(x, y, &p, out, depth - 1),
            (Some(x), None) => out.push(BodyPatch {
                op: "Deleted".into(), node_path: p,
                from_kind: node_kind(x).into(), to_kind: "(removed)".into(),
            }),
            (None, Some(y)) => out.push(BodyPatch {
                op: "Inserted".into(), node_path: p,
                from_kind: "(none)".into(), to_kind: node_kind(y).into(),
            }),
            (None, None) => break,
        }
    }
}

fn node_kind(e: &CExpr) -> &'static str {
    match e {
        CExpr::Literal { .. }     => "Literal",
        CExpr::Var { .. }         => "Var",
        CExpr::Call { .. }        => "Call",
        CExpr::Let { .. }         => "Let",
        CExpr::Match { .. }       => "Match",
        CExpr::Block { .. }       => "Block",
        CExpr::Constructor { .. } => "Constructor",
        CExpr::RecordLit { .. }   => "RecordLit",
        CExpr::TupleLit { .. }    => "TupleLit",
        CExpr::ListLit { .. }     => "ListLit",
        CExpr::FieldAccess { .. } => "FieldAccess",
        CExpr::Lambda { .. }      => "Lambda",
        CExpr::BinOp { .. }       => "BinOp",
        CExpr::UnaryOp { .. }     => "UnaryOp",
        CExpr::Return { .. }      => "Return",
    }
}

pub fn render_signature(fd: &FnDecl) -> String {
    let params: Vec<String> = fd.params.iter()
        .map(|p| format!("{} :: {}", p.name, render_type(&p.ty))).collect();
    let eff = if fd.effects.is_empty() { String::new() } else {
        let labels: Vec<String> = fd.effects.iter().map(effect_label).collect();
        format!("[{}] ", labels.join(", "))
    };
    format!("fn {}({}) -> {}{}", fd.name, params.join(", "),
        eff, render_type(&fd.return_type))
}

/// Render a type declaration's signature. The leading `type ` is
/// load-bearing: `diff_to_ops` classifies a removal as `RemoveType`
/// vs `RemoveFunction` by testing `signature.starts_with("type ")`.
pub fn render_type_signature(td: &TypeDecl) -> String {
    let params = if td.params.is_empty() {
        String::new()
    } else {
        format!("[{}]", td.params.join(", "))
    };
    format!("type {}{} = {}", td.name, params, render_type(&td.definition))
}

/// Render an effect with its arg if present: `fs_read("/tmp")`,
/// `net("api.example.com")`, or just `io`. Used by both signature
/// rendering and effect-diff so the same string identifies the
/// same effect in either context.
pub fn effect_label(e: &Effect) -> String {
    match &e.arg {
        Some(EffectArg::Str { value })   => format!("{}({:?})", e.name, value),
        Some(EffectArg::Int { value })   => format!("{}({})",   e.name, value),
        Some(EffectArg::Ident { value }) => format!("{}({})",   e.name, value),
        None => e.name.clone(),
    }
}

/// Set-style diff over two effect lists. Order-insensitive within
/// the lists; ordering of the output is sorted-by-label so the
/// JSON is stable.
fn effect_diff(a: &[Effect], b: &[Effect]) -> EffectChanges {
    let labels_a: BTreeSet<String> = a.iter().map(effect_label).collect();
    let labels_b: BTreeSet<String> = b.iter().map(effect_label).collect();
    let added:   Vec<String> = labels_b.difference(&labels_a).cloned().collect();
    let removed: Vec<String> = labels_a.difference(&labels_b).cloned().collect();
    EffectChanges {
        before:  labels_a.into_iter().collect(),
        after:   labels_b.into_iter().collect(),
        added,
        removed,
    }
}

fn render_type(t: &TypeExpr) -> String {
    match t {
        TypeExpr::Named { name, args } => {
            if args.is_empty() { name.clone() }
            else {
                let parts: Vec<String> = args.iter().map(render_type).collect();
                format!("{name}[{}]", parts.join(", "))
            }
        }
        TypeExpr::Tuple { items } => {
            let parts: Vec<String> = items.iter().map(render_type).collect();
            format!("({})", parts.join(", "))
        }
        TypeExpr::Record { fields } => {
            let parts: Vec<String> = fields.iter()
                .map(|f| format!("{} :: {}", f.name, render_type(&f.ty))).collect();
            format!("{{ {} }}", parts.join(", "))
        }
        TypeExpr::RecordWithSpreads { spreads, fields } => {
            let mut parts: Vec<String> = spreads.iter().map(|s| format!("...{}", s)).collect();
            parts.extend(fields.iter().map(|f| format!("{} :: {}", f.name, render_type(&f.ty))));
            format!("{{ {} }}", parts.join(", "))
        }
        TypeExpr::Function { params, effects, effect_row_var, ret } => {
            let parts: Vec<String> = params.iter().map(render_type).collect();
            let eff = if effects.is_empty() && effect_row_var.is_none() { String::new() } else {
                let mut names: Vec<String> = effects.iter().map(|e| e.name.clone()).collect();
                if let Some(v) = effect_row_var { names.push(format!("| {}", v)); }
                format!("[{}] ", names.join(", "))
            };
            format!("({}) -> {}{}", parts.join(", "), eff, render_type(ret))
        }
        TypeExpr::Union { variants } => variants.iter().map(|v| match &v.payload {
            Some(p) => format!("{}({})", v.name, render_type(p)),
            None => v.name.clone(),
        }).collect::<Vec<_>>().join(" | "),
        TypeExpr::Refined { base, binding, .. } => {
            // Render compactly: `Base{x | …}`. The full predicate is
            // captured in the canonical AST and contributes to
            // OpId hashing via lex-vcs's content-addressing — this
            // string is for diagnostics only. (#209 slice 1)
            format!("{}{{{} | …}}", render_type(base), binding)
        }
    }
}