truth-mirror 0.9.0

Truthfulness gate and adversarial reviewer harness for AI coding agents.
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
//! Best-effort source-control provenance for external checkpoint systems.

use std::{
    path::Path,
    process::{Command, Output},
};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

const ENTIRE_CHECKPOINTS_V1_REF: &str = "entire/checkpoints/v1";

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EntireCheckpointRef {
    pub ref_name: String,
    pub object_sha: String,
}

/// Best-effort compatibility lookup for Entire-created refs.
///
/// Absence of Entire, missing refs, or Git lookup failures all resolve to
/// `None`; provenance must never be required for core truth-mirror behavior.
pub fn entire_checkpoint_for_commit(
    repo_root: &Path,
    commit_sha: &str,
) -> Option<EntireCheckpointRef> {
    let commit_sha = commit_sha.trim();
    if !looks_like_commit_sha(commit_sha) {
        return None;
    }
    let commit_object = git_stdout(
        repo_root,
        &[
            "rev-parse",
            "--verify",
            "--quiet",
            "--end-of-options",
            &format!("{commit_sha}^{{commit}}"),
        ],
    )
    .ok()?;
    let message = git_stdout(
        repo_root,
        &["show", "-s", "--format=%B", "--end-of-options", commit_sha],
    )
    .ok()?;
    let checkpoint = entire_checkpoint_trailer(&message)?;
    resolve_entire_checkpoint(repo_root, checkpoint, commit_object.trim())
        .ok()
        .flatten()
}

fn looks_like_commit_sha(value: &str) -> bool {
    let value = value.trim();
    (6..=64).contains(&value.len()) && value.chars().all(|character| character.is_ascii_hexdigit())
}

fn resolve_entire_checkpoint(
    repo_root: &Path,
    checkpoint: &str,
    commit_object: &str,
) -> Result<Option<EntireCheckpointRef>> {
    if let Some(checkpoint_id) = normalize_entire_checkpoint_id(checkpoint) {
        return resolve_entire_checkpoint_id(repo_root, &checkpoint_id);
    }
    let Some(ref_name) = normalize_entire_ref(checkpoint) else {
        return Ok(None);
    };
    resolve_legacy_entire_ref(repo_root, &ref_name, commit_object)
}

fn resolve_entire_checkpoint_id(
    repo_root: &Path,
    checkpoint_id: &str,
) -> Result<Option<EntireCheckpointRef>> {
    let full_ref = format!("refs/heads/{ENTIRE_CHECKPOINTS_V1_REF}");
    let output = git_output(
        repo_root,
        &[
            "rev-parse",
            "--verify",
            "--quiet",
            "--end-of-options",
            &format!("{full_ref}^{{commit}}"),
        ],
    )?;
    if !output.status.success() {
        return Ok(None);
    }
    if !checkpoint_metadata_mentions_id(repo_root, checkpoint_id, &full_ref)? {
        return Ok(None);
    }
    let object_sha = String::from_utf8_lossy(&output.stdout).trim().to_owned();
    if object_sha.is_empty() {
        return Ok(None);
    }
    Ok(Some(EntireCheckpointRef {
        ref_name: ENTIRE_CHECKPOINTS_V1_REF.to_owned(),
        object_sha,
    }))
}

/// Check whether the metadata file committed to the Entire checkpoints branch
/// mentions `checkpoint_id` as a substring.
///
/// **Empirical Entire format assumption**: Entire commits checkpoint metadata
/// (JSON files keyed by checkpoint ID) to `refs/heads/entire/checkpoints/v1`.
/// The 12-character hex checkpoint ID appears verbatim in those files, so a
/// `git grep -F` is sufficient and safe. If Entire ever changes its metadata
/// format so the ID is no longer present as a plain substring, this function
/// returns `false`, which causes the lookup to degrade to `None` — no core
/// truth-mirror behaviour is affected. The metadata grep is intentionally
/// preserved (not removed) because skipping it could allow a stale checkpoint
/// ref to be incorrectly attributed to a different commit.
fn checkpoint_metadata_mentions_id(
    repo_root: &Path,
    checkpoint_id: &str,
    full_ref: &str,
) -> Result<bool> {
    let output = git_output(
        repo_root,
        &["grep", "-F", "--quiet", checkpoint_id, full_ref, "--", "."],
    )?;
    if output.status.success() {
        return Ok(true);
    }
    if output.status.code() == Some(1) {
        return Ok(false);
    }
    anyhow::bail!(
        "git grep Entire checkpoint metadata failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

fn resolve_legacy_entire_ref(
    repo_root: &Path,
    ref_name: &str,
    commit_object: &str,
) -> Result<Option<EntireCheckpointRef>> {
    let full_ref = format!("refs/heads/{ref_name}");
    let output = git_output(
        repo_root,
        &[
            "rev-parse",
            "--verify",
            "--quiet",
            "--end-of-options",
            &format!("{full_ref}^{{commit}}"),
        ],
    )?;
    if !output.status.success() {
        return Ok(None);
    }
    let checkpoint = EntireCheckpointRef {
        ref_name: ref_name.to_owned(),
        object_sha: String::from_utf8_lossy(&output.stdout).trim().to_owned(),
    };
    Ok((checkpoint.object_sha == commit_object).then_some(checkpoint))
}

fn entire_checkpoint_trailer(message: &str) -> Option<&str> {
    let lines = message.lines().collect::<Vec<_>>();
    let end = lines
        .iter()
        .rposition(|line| !line.trim().is_empty())
        .map(|index| index + 1)?;
    let start = lines[..end]
        .iter()
        .rposition(|line| line.trim().is_empty())
        .map_or(0, |index| index + 1);

    lines[start..end]
        .iter()
        .rev()
        .find_map(|line| {
            let (name, value) = line.split_once(':')?;
            name.trim()
                .eq_ignore_ascii_case("Entire-Checkpoint")
                .then(|| value.trim())
        })
        .filter(|value| !value.is_empty())
}

/// Normalise an Entire checkpoint ID to a canonical lowercase 12-character hex
/// string, returning `None` for anything that does not match that shape.
///
/// **Empirical Entire format assumption**: Entire represents checkpoint IDs as
/// exactly 12 lowercase hexadecimal characters (e.g. `a1b2c3d4e5f6`). The IDs
/// appear verbatim inside the files committed to the
/// `refs/heads/entire/checkpoints/v1` branch. If Entire ever changes the length
/// or character set of its checkpoint IDs this function returns `None`, which
/// degrades gracefully: the caller falls through to the legacy ref path and, if
/// that also misses, returns `None` overall — no core truth-mirror behaviour is
/// affected.
fn normalize_entire_checkpoint_id(value: &str) -> Option<String> {
    let value = value.trim();
    (value.len() == 12 && value.chars().all(|character| character.is_ascii_hexdigit()))
        .then(|| value.to_ascii_lowercase())
}

fn normalize_entire_ref(value: &str) -> Option<String> {
    let value = value.trim();
    if value.is_empty()
        || value.contains("..")
        || value.chars().any(|character| {
            character.is_whitespace() || matches!(character, '~' | '^' | ':' | '@')
        })
    {
        return None;
    }
    if let Some(short) = value.strip_prefix("refs/heads/") {
        return short.starts_with("entire/").then(|| short.to_owned());
    }
    value.starts_with("entire/").then(|| value.to_owned())
}

fn git_stdout(repo_root: &Path, args: &[&str]) -> Result<String> {
    let output = git_output(repo_root, args)?;
    if !output.status.success() {
        anyhow::bail!(
            "git {} failed: {}",
            args.join(" "),
            String::from_utf8_lossy(&output.stderr)
        );
    }
    Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}

fn git_output(repo_root: &Path, args: &[&str]) -> Result<Output> {
    Command::new("git")
        .args(args)
        .current_dir(repo_root)
        .output()
        .context("failed to run git for Entire provenance")
}

#[cfg(test)]
mod tests {
    use std::{fs, path::Path, process::Command};

    use super::{
        ENTIRE_CHECKPOINTS_V1_REF, entire_checkpoint_for_commit, entire_checkpoint_trailer,
        looks_like_commit_sha, normalize_entire_checkpoint_id, normalize_entire_ref,
    };

    fn git(repo: &Path, args: &[&str]) {
        let status = Command::new("git")
            .args(args)
            .current_dir(repo)
            .status()
            .unwrap();
        assert!(status.success(), "git {args:?} failed");
    }

    fn git_stdout(repo: &Path, args: &[&str]) -> String {
        let output = Command::new("git")
            .args(args)
            .current_dir(repo)
            .output()
            .unwrap();
        assert!(output.status.success(), "git {args:?} failed");
        String::from_utf8(output.stdout).unwrap().trim().to_owned()
    }

    fn init_test_repo(repo: &Path) {
        git(repo, &["init"]);
        git(repo, &["config", "commit.gpgsign", "false"]);
        git(repo, &["config", "user.email", "truth@example.invalid"]);
        git(repo, &["config", "user.name", "Truth Mirror Test"]);
    }

    fn write_checkpoint_metadata(repo: &Path, checkpoint_id: &str) {
        let path = repo
            .join(".entire/checkpoints")
            .join(format!("{checkpoint_id}.json"));
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, format!(r#"{{"id":"{checkpoint_id}"}}"#)).unwrap();
    }

    #[test]
    fn parses_explicit_entire_checkpoint_trailer() {
        let trailer = entire_checkpoint_trailer(
            "feat: work\n\nCLAIM: done | verified: cargo test | evidence: tests:x\nEntire-Checkpoint: a1b2c3d4e5f6\n",
        );

        assert_eq!(trailer, Some("a1b2c3d4e5f6"));
    }

    #[test]
    fn ignores_entire_checkpoint_outside_final_trailer_block() {
        let trailer = entire_checkpoint_trailer(
            "feat: work\n\nThe body mentions:\nEntire-Checkpoint: entire/body-only\n\nCLAIM: done | verified: cargo test | evidence: tests:x\n",
        );

        assert_eq!(trailer, None);
    }

    #[test]
    fn normalizes_only_entire_refs() {
        assert_eq!(
            normalize_entire_ref("refs/heads/entire/session-abcdef"),
            Some("entire/session-abcdef".to_owned())
        );
        assert_eq!(
            normalize_entire_ref("entire/session-abcdef"),
            Some("entire/session-abcdef".to_owned())
        );
        assert_eq!(normalize_entire_ref("main"), None);
        assert_eq!(normalize_entire_ref("entire/session:bad"), None);
        assert_eq!(normalize_entire_ref("entire/@{-1}"), None);
    }

    #[test]
    fn normalizes_entire_checkpoint_ids() {
        assert_eq!(
            normalize_entire_checkpoint_id("A1B2C3D4E5F6"),
            Some("a1b2c3d4e5f6".to_owned())
        );
        assert_eq!(normalize_entire_checkpoint_id("a1b2c3"), None);
        assert_eq!(normalize_entire_checkpoint_id("a1b2c3d4e5fx"), None);
        assert_eq!(
            normalize_entire_checkpoint_id("entire/session-abcdef"),
            None
        );
    }

    #[test]
    fn rejects_non_sha_commit_values_before_git_lookup() {
        assert!(!looks_like_commit_sha("--all"));
        assert!(!looks_like_commit_sha("HEAD"));
        assert!(!looks_like_commit_sha("abc12"));
        assert!(looks_like_commit_sha("abcdef1234567890"));
        let sha256 = "a".repeat(64);
        let too_long = "a".repeat(65);
        assert!(looks_like_commit_sha(&sha256));
        assert!(!looks_like_commit_sha(&too_long));
    }

    #[test]
    fn resolves_entire_checkpoint_id_from_metadata_ref() {
        let temp = tempfile::tempdir().unwrap();
        let repo = temp.path().join("repo");
        fs::create_dir(&repo).unwrap();
        init_test_repo(&repo);
        fs::write(repo.join("file.txt"), "hello\n").unwrap();
        write_checkpoint_metadata(&repo, "a1b2c3d4e5f6");
        git(&repo, &["add", "file.txt"]);
        git(
            &repo,
            &["add", "-f", ".entire/checkpoints/a1b2c3d4e5f6.json"],
        );
        git(
            &repo,
            &[
                "commit",
                "-m",
                "feat: base",
                "-m",
                "CLAIM: base | verified: cargo test | evidence: tests:provenance\n\nEntire-Checkpoint: a1b2c3d4e5f6",
            ],
        );
        let commit = git_stdout(&repo, &["rev-parse", "HEAD"]);
        git(
            &repo,
            &[
                "update-ref",
                &format!("refs/heads/{ENTIRE_CHECKPOINTS_V1_REF}"),
                "HEAD",
            ],
        );

        let checkpoint = entire_checkpoint_for_commit(&repo, &commit).unwrap();

        assert_eq!(checkpoint.ref_name, ENTIRE_CHECKPOINTS_V1_REF);
        assert_eq!(checkpoint.object_sha, commit);
    }

    #[test]
    fn resolves_legacy_entire_ref_from_commit_trailer() {
        let temp = tempfile::tempdir().unwrap();
        let repo = temp.path().join("repo");
        fs::create_dir(&repo).unwrap();
        init_test_repo(&repo);
        fs::write(repo.join("file.txt"), "hello\n").unwrap();
        git(&repo, &["add", "file.txt"]);
        git(
            &repo,
            &[
                "commit",
                "-m",
                "feat: base",
                "-m",
                "CLAIM: base | verified: cargo test | evidence: tests:provenance\n\nEntire-Checkpoint: entire/session-abcdef",
            ],
        );
        let commit = git_stdout(&repo, &["rev-parse", "HEAD"]);
        git(
            &repo,
            &["update-ref", "refs/heads/entire/session-abcdef", "HEAD"],
        );

        let checkpoint = entire_checkpoint_for_commit(&repo, &commit).unwrap();

        assert_eq!(checkpoint.ref_name, "entire/session-abcdef");
        assert_eq!(checkpoint.object_sha, commit);
    }

    #[test]
    fn missing_entire_ref_for_trailer_returns_none() {
        let temp = tempfile::tempdir().unwrap();
        let repo = temp.path().join("repo");
        fs::create_dir(&repo).unwrap();
        init_test_repo(&repo);
        fs::write(repo.join("file.txt"), "hello\n").unwrap();
        git(&repo, &["add", "file.txt"]);
        git(
            &repo,
            &[
                "commit",
                "-m",
                "feat: base",
                "-m",
                "CLAIM: base | verified: cargo test | evidence: tests:provenance\n\nEntire-Checkpoint: a1b2c3d4e5f6",
            ],
        );
        let commit = git_stdout(&repo, &["rev-parse", "HEAD"]);

        assert_eq!(
            entire_checkpoint_for_commit(&repo, &format!(" {commit}\n")),
            None
        );
    }

    #[test]
    fn checkpoint_id_missing_from_metadata_returns_none() {
        let temp = tempfile::tempdir().unwrap();
        let repo = temp.path().join("repo");
        fs::create_dir(&repo).unwrap();
        init_test_repo(&repo);
        fs::write(repo.join("file.txt"), "hello\n").unwrap();
        git(&repo, &["add", "file.txt"]);
        git(
            &repo,
            &[
                "commit",
                "-m",
                "feat: base",
                "-m",
                "CLAIM: base | verified: cargo test | evidence: tests:provenance\n\nEntire-Checkpoint: a1b2c3d4e5f6",
            ],
        );
        let commit = git_stdout(&repo, &["rev-parse", "HEAD"]);
        git(
            &repo,
            &[
                "update-ref",
                &format!("refs/heads/{ENTIRE_CHECKPOINTS_V1_REF}"),
                "HEAD",
            ],
        );

        assert_eq!(entire_checkpoint_for_commit(&repo, &commit), None);
    }

    #[test]
    fn stale_entire_ref_for_trailer_returns_none() {
        let temp = tempfile::tempdir().unwrap();
        let repo = temp.path().join("repo");
        fs::create_dir(&repo).unwrap();
        init_test_repo(&repo);

        fs::write(repo.join("file.txt"), "base\n").unwrap();
        git(&repo, &["add", "file.txt"]);
        git(
            &repo,
            &[
                "commit",
                "-m",
                "feat: base",
                "-m",
                "CLAIM: base | verified: cargo test | evidence: tests:provenance",
            ],
        );
        let stale_commit = git_stdout(&repo, &["rev-parse", "HEAD"]);
        git(
            &repo,
            &[
                "update-ref",
                "refs/heads/entire/session-stale",
                &stale_commit,
            ],
        );

        fs::write(repo.join("file.txt"), "next\n").unwrap();
        git(&repo, &["add", "file.txt"]);
        git(
            &repo,
            &[
                "commit",
                "-m",
                "feat: next",
                "-m",
                "CLAIM: next | verified: cargo test | evidence: tests:provenance\n\nEntire-Checkpoint: entire/session-stale",
            ],
        );
        let reviewed_commit = git_stdout(&repo, &["rev-parse", "HEAD"]);

        assert_eq!(entire_checkpoint_for_commit(&repo, &reviewed_commit), None);
    }
}