gemel 0.10.0

Evidence-native version control for agentic software development: canonical object encoding, content-addressed identity, immutable object store, change workflow, Git-carried exchange rollups, and CLI.
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! Phase 3 integration tests (SPECIFICATION.md §44, OBJECT_MODEL.md §6.17,
//! AGENT_PROTOCOL.md §5.10).
//!
//! These tests prove the Phase 3 exit criteria: `gemel reconcile` using
//! textual changes, path changes, explicit Claims, Evidence, Residuals;
//! producing a Reconciliation object (adopted / rejected / unresolved /
//! verification required / resulting State); concurrent agents working from
//! the same base State; uncertainty exposed, never invented.

// Test closures return the rich store error type; boxed variants would
// obscure construction sites for no measurable gain.
#![allow(clippy::result_large_err)]

use gemel::reconcile::{self, ReconcileInput, ReconcileOptions};
use gemel::store::{InitOptions, Repo, REF_HEAD, REF_STATE_HEAD};
use gemel::value::{Field, Value};
use gemel::workflow::{self, BeginOptions, ClaimSpec, EvidenceSpec, FinishOptions, ResidualSpec};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

static COUNTER: AtomicU64 = AtomicU64::new(0);

fn temp_root(tag: &str) -> PathBuf {
    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    let dir = std::env::temp_dir().join(format!("gemel-p3-{tag}-{}-{n}", std::process::id()));
    std::fs::remove_dir_all(&dir).ok();
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

fn write_file(root: &Path, rel: &str, content: &str) {
    let path = root.join(rel);
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    std::fs::write(path, content).unwrap();
}

/// Seeds a repository at a common base state S1: two files, snapshot only
/// (no changes yet). Returns the repo and the base state.
fn seed_base(root: &Path) -> (Repo, gemel::gid::Gid) {
    write_file(root, "a.txt", "a1\n");
    write_file(root, "b.txt", "b1\n");
    let repo = Repo::init(root, &InitOptions::default()).unwrap();
    let ignore = gemel::ignore::Ignore::from_root(root);
    let snap = gemel::content::build_state(&repo, root, &ignore).unwrap();
    repo.with_write_lock(|| {
        let ops = vec![gemel::store::refs::RefOp::set(
            &format!("{}/S1", gemel::store::REF_NAMES),
            snap.state,
        )];
        repo.apply_refs_unlocked(&gemel::store::refs::RefTransaction { ops })
            .unwrap();
        workflow::set_workspace_state(&repo, snap.state).unwrap();
        Ok::<(), gemel::store::Error>(())
    })
    .unwrap();
    (repo, snap.state)
}

/// Concurrent agent A: a change touching `a.txt` from the base state, in its
/// own workspace and working directory (brief §34: no filesystem
/// serialization between agents).
fn agent_a_change(repo: &Repo, _root: &Path, base: &gemel::gid::Gid) -> PathBuf {
    let wa = temp_root("wa");
    gemel::content::materialize_overlay(repo, base, &wa).unwrap();
    workflow::begin_change(
        repo,
        &BeginOptions {
            from_state: Some(*base),
            intent_summary: Some("Agent A work".into()),
            workspace: Some("wa".into()),
            worktree: Some(wa.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&wa, "a.txt", "a2 from A\n");
    workflow::finish_change(
        repo,
        &FinishOptions {
            summary: "A modifies a.txt".into(),
            claims: vec![ClaimSpec {
                subject: Some("a.txt".into()),
                predicate: "a.txt is stable under A".into(),
                kind: "correctness".into(),
                evidence: Vec::new(),
            }],
            workspace: Some("wa".into()),
            worktree: Some(wa.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    wa
}

/// Concurrent agent B: a change touching `b.txt` from the base state, in its
/// own workspace and working directory.
fn agent_b_change(repo: &Repo, _root: &Path, base: &gemel::gid::Gid) -> PathBuf {
    let wb = temp_root("wb");
    gemel::content::materialize_overlay(repo, base, &wb).unwrap();
    workflow::begin_change(
        repo,
        &BeginOptions {
            from_state: Some(*base),
            intent_summary: Some("Agent B work".into()),
            workspace: Some("wb".into()),
            worktree: Some(wb.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&wb, "b.txt", "b2 from B\n");
    workflow::finish_change(
        repo,
        &FinishOptions {
            summary: "B modifies b.txt".into(),
            workspace: Some("wb".into()),
            worktree: Some(wb.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    wb
}

fn input(repo: &Repo, name: &str) -> ReconcileInput {
    ReconcileInput {
        name: name.to_string(),
        gid: repo.resolve(name).unwrap(),
    }
}

#[test]
fn concurrent_agents_same_base_no_conflict() {
    let root = temp_root("concurrent");
    let (repo, base) = seed_base(&root);
    agent_a_change(&repo, &root, &base);
    agent_b_change(&repo, &root, &base);

    // Both trajectories derive from the same base state.
    assert_eq!(
        gemel::query::trajectory_detail(&repo, "T1")
            .unwrap()
            .base_state,
        Some(base)
    );
    assert_eq!(
        gemel::query::trajectory_detail(&repo, "T2")
            .unwrap()
            .base_state,
        Some(base)
    );

    // Reconcile: disjoint paths → adopt everything, no conflicts.
    let plan = reconcile::analyze(&repo, &[input(&repo, "T1"), input(&repo, "T2")]).unwrap();
    assert!(
        plan.textual_conflicts.is_empty(),
        "{:?}",
        plan.textual_conflicts
    );
    assert_eq!(plan.adopted.len(), 2);
    assert!(plan.rejected.is_empty());
    // Merged file map contains both changes (the plan's state identity is
    // in-memory; the file map is the ground truth).
    let read_blob = |repo: &Repo, g: &gemel::gid::Gid| {
        String::from_utf8_lossy(repo.load(g).unwrap().blob_bytes().unwrap_or(&[])).into_owned()
    };
    assert_eq!(
        read_blob(&repo, &plan.merged_files.get("a.txt").unwrap().1),
        "a2 from A\n"
    );
    assert_eq!(
        read_blob(&repo, &plan.merged_files.get("b.txt").unwrap().1),
        "b2 from B\n"
    );
    // The retained claim from A is present; it is unverified (no evidence).
    assert_eq!(plan.claims_retained.len(), 1);
    assert_eq!(plan.verification_required, plan.claims_retained);
}

#[test]
fn textual_conflict_first_input_wins_and_is_recorded() {
    let root = temp_root("conflict");
    let (repo, base) = seed_base(&root);
    agent_a_change(&repo, &root, &base);
    // Agent C also modifies a.txt from the same base, in its own workspace.
    let wc = temp_root("wc");
    gemel::content::materialize_overlay(&repo, &base, &wc).unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            from_state: Some(base),
            intent_summary: Some("Agent C work".into()),
            workspace: Some("wc".into()),
            worktree: Some(wc.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&wc, "a.txt", "a2 from C\n");
    let c_change = workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "C modifies a.txt".into(),
            evidence: vec![EvidenceSpec {
                subject: Some("a.txt".into()),
                outcome: "pass".into(),
                kind: "test_result".into(),
            }],
            workspace: Some("wc".into()),
            worktree: Some(wc.clone()),
            ..Default::default()
        },
    )
    .unwrap();

    let plan = reconcile::analyze(&repo, &[input(&repo, "T1"), input(&repo, "T2")]).unwrap();
    assert_eq!(plan.textual_conflicts.len(), 1);
    assert_eq!(plan.textual_conflicts[0].path, "a.txt");
    assert_eq!(plan.textual_conflicts[0].changes.len(), 2);
    // First input (T1) wins the path: A's change adopted, C's rejected.
    assert_eq!(plan.adopted.len(), 1);
    assert_eq!(plan.rejected, vec![c_change.change]);
    assert!(plan
        .interactions
        .iter()
        .any(|i| i.kind == "textual" && i.certainty == "observed"));
    // Resulting state carries the adopted version.
    let a = plan.merged_files.get("a.txt").unwrap().1;
    let content =
        String::from_utf8_lossy(repo.load(&a).unwrap().blob_bytes().unwrap_or(&[])).into_owned();
    assert_eq!(content, "a2 from A\n");
    assert!(plan.rationale.contains("first-input-trajectory-wins"));
    // C's claim about a.txt is invalidated (subject touched by adopted work).
    assert_eq!(plan.claims_invalidated.len(), 0); // C declared no claim
                                                  // C's evidence is not retained.
    assert!(plan.evidence_retained.is_empty());
}

#[test]
fn reconciliation_object_records_the_decision() {
    let root = temp_root("object");
    let (repo, base) = seed_base(&root);
    agent_a_change(&repo, &root, &base);
    agent_b_change(&repo, &root, &base);

    let out = reconcile::reconcile(
        &repo,
        &[input(&repo, "T1"), input(&repo, "T2")],
        &ReconcileOptions::default(),
    )
    .unwrap();
    assert_eq!(out.reconciliation_name, "Re1");
    assert!(out.plan.textual_conflicts.is_empty());

    let obj = repo.load(&out.reconciliation).unwrap();
    assert_eq!(obj.family, gemel::family::Family::Reconciliation);
    let fs = obj.field_sequence().unwrap();
    // input_trajectories (0x03) lists both inputs.
    let inputs = gemel::query::gid_list(fs, 0x03);
    assert_eq!(inputs.len(), 2);
    // adopted (0x05) = both changes; rejected (0x06) absent.
    assert_eq!(gemel::query::gid_list(fs, 0x05).len(), 2);
    assert!(gemel::query::gid_list(fs, 0x06).is_empty());
    // resulting_state (0x0E) is the merged state; resulting_change (0x0F).
    assert_eq!(gemel::query::gid_field(fs, 0x0E), Some(out.state));
    assert_eq!(gemel::query::gid_field(fs, 0x0F), Some(out.change));
    // rationale (0x10) is present.
    let rationale = gemel::query::str_field(fs, 0x10).unwrap();
    assert!(rationale.contains("per-path first-input-trajectory-wins"));

    // The resulting change has the merged state and causal parents.
    let cobj = repo.load(&out.change).unwrap();
    let cfs = cobj.field_sequence().unwrap();
    assert_eq!(gemel::query::gid_field(cfs, 0x05), Some(out.state));
    assert_eq!(gemel::query::gid_list(cfs, 0x11).len(), 2);
    // Names resolve.
    assert_eq!(repo.resolve("Re1").unwrap(), out.reconciliation);
    assert_eq!(repo.resolve(&out.state_name).unwrap(), out.state);
    assert_eq!(repo.resolve(&out.change_name).unwrap(), out.change);
    // Without --apply the head is untouched: it still points at the last
    // agent finish (B's change), not the reconciliation's change.
    assert_ne!(repo.read_ref(REF_HEAD).unwrap(), Some(out.change));
    assert_eq!(
        repo.read_ref(REF_STATE_HEAD).unwrap(),
        Some(
            gemel::query::gid_field(
                repo.load(&repo.read_ref(REF_HEAD).unwrap().unwrap())
                    .unwrap()
                    .field_sequence()
                    .unwrap(),
                0x05
            )
            .unwrap()
        )
    );
    let _ = base;
}

#[test]
fn plan_is_read_only_and_deterministic() {
    let root = temp_root("plan");
    let (repo, base) = seed_base(&root);
    agent_a_change(&repo, &root, &base);
    agent_b_change(&repo, &root, &base);
    let inputs = [input(&repo, "T1"), input(&repo, "T2")];

    let p1 = reconcile::analyze(&repo, &inputs).unwrap();
    let p2 = reconcile::analyze(&repo, &inputs).unwrap();
    assert_eq!(p1.resulting_state, p2.resulting_state);
    assert_eq!(p1.adopted, p2.adopted);
    assert_eq!(p1.rationale, p2.rationale);
    // The plan publishes nothing: refs are exactly as the agent finishes
    // left them (head = B's change, state/head = B's resulting state).
    let head_before = repo.read_ref(REF_HEAD).unwrap();
    let state_before = repo.read_ref(REF_STATE_HEAD).unwrap();
    assert!(head_before.is_some());
    // The planned resulting state identity matches the executed one.
    let out = reconcile::reconcile(&repo, &inputs, &ReconcileOptions::default()).unwrap();
    assert_eq!(out.plan.resulting_state, p1.resulting_state);
    assert_eq!(out.state, p1.resulting_state);
    // Reconcile without --apply leaves refs untouched.
    assert_eq!(repo.read_ref(REF_HEAD).unwrap(), head_before);
    assert_eq!(repo.read_ref(REF_STATE_HEAD).unwrap(), state_before);
}

#[test]
fn apply_advances_head_and_workspace() {
    let root = temp_root("apply");
    let (repo, base) = seed_base(&root);
    agent_a_change(&repo, &root, &base);
    agent_b_change(&repo, &root, &base);

    let out = reconcile::reconcile(
        &repo,
        &[input(&repo, "T1"), input(&repo, "T2")],
        &ReconcileOptions {
            apply: true,
            producer: None,
        },
    )
    .unwrap();
    assert_eq!(repo.read_ref(REF_HEAD).unwrap(), Some(out.change));
    assert_eq!(repo.read_ref(REF_STATE_HEAD).unwrap(), Some(out.state));
    assert_eq!(workflow::workspace_state(&repo).unwrap(), Some(out.state));
    // Materialize the merged state into the default working tree; status is
    // then clean (workspace matches head state).
    gemel::content::materialize_overlay(&repo, &out.state, &root).unwrap();
    let st = gemel::query::status(&repo).unwrap();
    assert!(
        st.changed.is_empty(),
        "workspace matches merged state: {:?}",
        st.changed
    );
}

#[test]
fn base_mismatch_is_refused() {
    let root = temp_root("mismatch");
    let (repo, base) = seed_base(&root);
    agent_a_change(&repo, &root, &base);
    // The sequential change explicitly bases on A's resulting state.
    let a_head = repo.read_ref(REF_HEAD).unwrap().unwrap();
    let a_result =
        gemel::query::gid_field(repo.load(&a_head).unwrap().field_sequence().unwrap(), 0x05)
            .unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            from_state: Some(a_result),
            intent_summary: Some("sequential".into()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "b.txt", "b2\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "sequential change".into(),
            ..Default::default()
        },
    )
    .unwrap();
    // T1 bases on S1; T2 bases on A's result → refuse, fail closed.
    let err = reconcile::analyze(&repo, &[input(&repo, "T1"), input(&repo, "T2")]).unwrap_err();
    assert!(
        err.to_string().contains("do not share a base state"),
        "unexpected error: {err}"
    );
}

#[test]
fn residual_carry_forward_and_resolution() {
    let root = temp_root("residuals");
    let (repo, base) = seed_base(&root);
    // Agent A leaves an open residual; Agent B resolves one. Each works in
    // its own workspace (brief §34).
    let wa = temp_root("ra");
    gemel::content::materialize_overlay(&repo, &base, &wa).unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            from_state: Some(base),
            intent_summary: Some("A".into()),
            workspace: Some("ra".into()),
            worktree: Some(wa.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&wa, "a.txt", "a2\n");
    let fa = workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "A".into(),
            residuals: vec![ResidualSpec {
                summary: "FreeBSD still diverges".into(),
                severity: "high".into(),
                classification: "platform_divergence".into(),
                affected_claims: Vec::new(),
                origin_evidence: None,
                affected_changes: Vec::new(),
            }],
            workspace: Some("ra".into()),
            worktree: Some(wa.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    let wb = temp_root("rb");
    gemel::content::materialize_overlay(&repo, &base, &wb).unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            from_state: Some(base),
            intent_summary: Some("B".into()),
            workspace: Some("rb".into()),
            worktree: Some(wb.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&wb, "b.txt", "b2\n");
    let fb = workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "B".into(),
            residuals: vec![ResidualSpec {
                summary: "fixed on FreeBSD".into(),
                severity: "medium".into(),
                classification: "platform_divergence".into(),
                affected_claims: Vec::new(),
                origin_evidence: None,
                affected_changes: Vec::new(),
            }],
            workspace: Some("rb".into()),
            worktree: Some(wb.clone()),
            ..Default::default()
        },
    )
    .unwrap();
    workflow::resolve_residual(
        &repo,
        &workflow::ResolveResidualOptions {
            residual: fb.residuals[0].to_string(),
            disposition: "resolved".into(),
            reason: Some("court now matches".into()),
            producer: None,
        },
    )
    .unwrap();

    let plan = reconcile::analyze(&repo, &[input(&repo, "T1"), input(&repo, "T2")]).unwrap();
    assert_eq!(plan.unresolved_residuals, vec![fa.residuals[0]]);
    assert_eq!(plan.resolved_residuals.len(), 1);
    // The resulting change carries the open residual forward.
    let out = reconcile::reconcile(
        &repo,
        &[input(&repo, "T1"), input(&repo, "T2")],
        &ReconcileOptions::default(),
    )
    .unwrap();
    let cobj = repo.load(&out.change).unwrap();
    let cfs = cobj.field_sequence().unwrap();
    assert!(gemel::query::gid_list(cfs, 0x0E).contains(&fa.residuals[0]));
}

#[test]
fn cli_reconcile_plan_and_execute() {
    let root = temp_root("cli-recon");
    let (repo, base) = seed_base(&root);
    agent_a_change(&repo, &root, &base);
    agent_b_change(&repo, &root, &base);
    let bin = env!("CARGO_BIN_EXE_gemel");
    let run = |args: &[&str]| -> (i32, serde_json::Value, String) {
        let mut cmd = std::process::Command::new(bin);
        cmd.arg("--repo").arg(&root);
        cmd.args(args);
        let out = cmd.output().expect("run gemel");
        (
            out.status.code().unwrap_or(-1),
            serde_json::from_slice(&out.stdout).unwrap_or(serde_json::Value::Null),
            String::from_utf8_lossy(&out.stderr).into_owned(),
        )
    };
    let (code, plan, err) = run(&["reconcile", "T1", "T2", "--plan", "--json"]);
    assert_eq!(code, 0, "plan failed: {err}");
    assert_eq!(plan["schema"], "gemel.query.v1");
    assert_eq!(plan["result"]["mode"], "plan");
    assert!(plan["result"]["textual_conflicts"]
        .as_array()
        .unwrap()
        .is_empty());
    assert_eq!(plan["result"]["adopted"].as_array().unwrap().len(), 2);

    let (code, out, err) = run(&["reconcile", "T1", "T2", "--apply", "--json"]);
    assert_eq!(code, 0, "reconcile failed: {err}");
    assert_eq!(out["result"]["reconciliation_name"], "Re1");
    assert_eq!(out["result"]["applied"], serde_json::Value::Bool(true));
    assert_eq!(out["result"]["mode"], "executed");
    // Head advanced; fsck stays clean (canonical + derived consistent).
    let (code, _, _) = run(&["fsck"]);
    assert_eq!(code, 0, "fsck must be clean after reconcile");
    let _ = repo;
}

// ---------------------------------------------------------------------------
// Schema-order regression: the reconciliation object must encode with
// strictly ascending tags (the fail-closed encoder rejects out-of-order).
// ---------------------------------------------------------------------------

#[test]
fn reconciliation_object_encodes_with_ascending_tags() {
    let root = temp_root("tags");
    let (repo, base) = seed_base(&root);
    agent_a_change(&repo, &root, &base);
    agent_b_change(&repo, &root, &base);
    let out = reconcile::reconcile(
        &repo,
        &[input(&repo, "T1"), input(&repo, "T2")],
        &ReconcileOptions::default(),
    )
    .unwrap();
    let obj = repo.load(&out.reconciliation).unwrap();
    let fs = obj.field_sequence().unwrap();
    let mut prev = 0u8;
    for field in fs {
        assert!(
            field.tag > prev,
            "reconciliation tags must be ascending: {field:?}"
        );
        prev = field.tag;
    }
    // The interaction record shape round-trips.
    let interactions = gemel::query::value_at(fs, 0x09);
    assert!(interactions.is_none() || matches!(interactions, Some(Value::Array(_))));
    let _ = Field::new(0, Value::B(false)); // Field is in scope
}