velra 0.1.1

Lossless compaction for Claude Code: task continuity across context compactions.
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! E1–E4: the Continuation Capsule (§16).

mod common;

use common::{golden_capsule, Log};
use proptest::prelude::*;
use velra_core::git::GitInfo;
use velra_core::model::{CommandKind, Mechanism, Outcome, Trigger};
use velra_core::render::{
    self, AttemptView, CommandRef, DeadEndView, FailureView, IntentView, NextTarget, RenderConfig,
    Snapshot, WorkingFileView, ABSOLUTE_MAX_CHARS, DEFAULT_BUDGET_TOKENS, HARD_CEILING_TOKENS,
};

const CAUSAL_WORDS: [&str; 4] = ["caused", "because", "due to", "led to"];

fn check(capsule: &str) {
    assert!(
        capsule.starts_with("<VELRA_CONTINUATION v=\"1\""),
        "{capsule}"
    );
    assert!(capsule.ends_with("</VELRA_CONTINUATION>"), "{capsule}");
    assert!(!capsule.contains('\r'), "line endings must be \\n only");
    assert!(
        !capsule.contains('\\'),
        "paths render with forward slashes: {capsule}"
    );
    // E3: no causal language.
    for word in CAUSAL_WORDS {
        assert!(
            !capsule.contains(word),
            "capsule must not contain {word:?}:\n{capsule}"
        );
    }
    let tokens = velra_core::text::estimate_tokens(capsule);
    assert!(
        tokens <= HARD_CEILING_TOKENS,
        "{tokens} tokens exceeds the hard ceiling"
    );
    assert!(capsule.chars().count() <= ABSOLUTE_MAX_CHARS);
}

// ------------------------------------------------------------- E1: goldens

#[test]
fn e1_empty_state() {
    let mut log = Log::new();
    log.env.write_file("src/a.rs", "v0\n");
    log.read("src/a.rs");
    let capsule = log.capsule();
    check(&capsule);
    golden_capsule("empty_state", &capsule);
}

#[test]
fn e1_objective_only() {
    let mut log = Log::new();
    log.prompt("fix the flaky logout test and keep the session cookie behaviour intact");
    let capsule = log.capsule();
    check(&capsule);
    golden_capsule("objective_only", &capsule);
}

#[test]
fn e1_failure_only() {
    let mut log = Log::new();
    log.prompt("fix the flaky logout test and keep the session cookie behaviour intact");
    log.command_fail(
        "pytest tests/test_auth.py -x",
        1,
        "E   assert response.cookies[\"session\"] is None\ntests/test_auth.py:88: AssertionError\n1 failed, 11 passed",
    );
    let capsule = log.capsule();
    check(&capsule);
    golden_capsule("failure_only", &capsule);
}

#[test]
fn e1_dead_end_inverse_edit() {
    let mut log = Log::new();
    log.env
        .write_file("src/auth/session.py", "max_age = None\n");
    log.prompt("stop the session cookie from surviving logout");
    log.edit("src/auth/session.py", "max_age = 0\n");
    log.edit("src/auth/session.py", "max_age = None\n");
    let capsule = log.capsule();
    check(&capsule);
    golden_capsule("dead_end_inverse_edit", &capsule);
}

#[test]
fn e1_dead_end_git_command() {
    let mut log = Log::new();
    log.env
        .write_file("src/auth/session.py", "max_age = None\n");
    log.prompt("stop the session cookie from surviving logout");
    log.edit("src/auth/session.py", "max_age = 0\n");
    log.command_fail(
        "pytest tests/test_auth.py -x",
        1,
        "FAILED tests/test_auth.py::test_logout\n1 failed",
    );
    log.git_restore(
        "git restore src/auth/session.py",
        &[("src/auth/session.py", "max_age = None\n")],
    );
    let capsule = log.capsule();
    check(&capsule);
    golden_capsule("dead_end_git_command", &capsule);
}

#[test]
fn e1_dead_end_rewrite() {
    let mut log = Log::new();
    log.env
        .write_file("src/config.ts", "export const retries = 1;\n");
    log.prompt("increase the retry count and see whether the suite stabilises");
    log.edit("src/config.ts", "export const retries = 5;\n");
    log.write_tool("src/config.ts", "export const retries = 1;\n");
    let capsule = log.capsule();
    check(&capsule);
    golden_capsule("dead_end_rewrite", &capsule);
}

#[test]
fn e1_dead_end_external() {
    let mut log = Log::new();
    log.env
        .write_file("src/config.ts", "export const retries = 1;\n");
    log.prompt("increase the retry count and see whether the suite stabilises");
    log.edit("src/config.ts", "export const retries = 5;\n");
    log.reduce();
    log.env
        .write_file("src/config.ts", "export const retries = 1;\n");
    log.stop();
    let capsule = log.capsule();
    check(&capsule);
    golden_capsule("dead_end_external", &capsule);
}

#[test]
fn e1_subagent() {
    let mut log = Log::new();
    log.env.write_file("src/parser.rs", "v0\n");
    log.prompt("have a subagent explore the parser rewrite while I review tests");
    log.edit_as("src/parser.rs", "v1\n", "agent-7");
    log.edit_as("src/parser.rs", "v0\n", "agent-7");
    let capsule = log.capsule();
    check(&capsule);
    assert!(capsule.contains("(subagent)"));
    golden_capsule("subagent", &capsule);
}

#[test]
fn e1_git_repository() {
    let mut log = Log::new();
    log.env.init_git(
        "feat/auth-refactor",
        "4f2a1c9e0b3d5a7c9e1f3a5b7d9f1e3a5c7b9d1f",
    );
    log.env.write_file("src/a.rs", "v0\n");
    log.prompt("refactor the auth module without changing behaviour");
    log.edit("src/a.rs", "v1\n");
    let capsule = log.capsule();
    check(&capsule);
    assert!(
        capsule.contains("feat/auth-refactor @ 4f2a1c9"),
        "{capsule}"
    );
    golden_capsule("git_repository", &capsule);
}

#[test]
fn e1_partial_capture() {
    let mut log = Log::new();
    log.env.write_file("src/a.rs", "v0\n");
    log.prompt("refactor the auth module without changing behaviour");
    log.edit("src/a.rs", "v1\n");
    let mut snapshot = log.snapshot();
    snapshot.partial = true;
    let capsule = render::render(&snapshot, &RenderConfig::default()).text;
    check(&capsule);
    assert!(capsule.contains("(partial capture)"));
    golden_capsule("partial_capture", &capsule);
}

#[test]
fn e1_sensitive_path() {
    let mut log = Log::new();
    log.env.write_file(".env", "API_KEY=old\n");
    log.prompt("rotate the API key in the environment file and redeploy");
    log.edit(".env", "API_KEY=sk-ant-api03-abcdefghijklmnopqrstuvwx\n");
    log.edit(".env", "API_KEY=old\n");
    let capsule = log.capsule();
    check(&capsule);
    assert!(capsule.contains(".env"), "the path is shown");
    assert!(
        !capsule.contains("sk-ant"),
        "no excerpt for sensitive paths:\n{capsule}"
    );
    assert!(
        !capsule.contains("API_KEY="),
        "no excerpt for sensitive paths:\n{capsule}"
    );
    golden_capsule("sensitive_path", &capsule);
}

#[test]
fn e1_long_prompts_are_truncated() {
    let mut log = Log::new();
    log.prompt(&format!(
        "refactor the authentication stack {}",
        "and keep every existing behaviour intact ".repeat(20)
    ));
    log.prompt(&format!(
        "now also check {}",
        "the session middleware and its tests ".repeat(20)
    ));
    let capsule = log.capsule();
    check(&capsule);
    golden_capsule("long_prompts", &capsule);
}

#[test]
fn e1_many_files() {
    let mut log = Log::new();
    log.prompt("touch every module in the package and keep the suite green");
    for i in 0..12 {
        let path = format!("src/mod{i}.rs");
        log.env.write_file(&path, "v0\n");
        log.edit(&path, &format!("v{i}\n"));
        log.read(&path);
    }
    log.command_fail(
        "cargo test",
        101,
        "error[E0308]: mismatched types\n  --> src/mod3.rs:1:1\n",
    );
    // Rendered at the spec's 800: this asserts what the ladder keeps at the
    // documented budget, not how much the shipped default's safety margin
    // trims. See Log::capsule_at.
    let capsule = log.capsule_at(800);
    check(&capsule);
    assert_eq!(
        capsule.matches("\n- src/mod").count(),
        4 + 8,
        "4 attempts + 8 working files"
    );
    golden_capsule("many_files", &capsule);
}

#[test]
fn e1_nested_paths() {
    let mut log = Log::new();
    log.prompt("fix the nested module the windows build complains about");
    log.env
        .write_file("src/deeply/nested/module/handler.rs", "v0\n");
    log.edit("src/deeply/nested/module/handler.rs", "v1\n");
    let capsule = log.capsule();
    check(&capsule);
    assert!(capsule.contains("src/deeply/nested/module/handler.rs"));
    golden_capsule("nested_paths", &capsule);
}

#[test]
fn e1_full_state() {
    let mut log = Log::new();
    log.env
        .init_git("main", "0123456789abcdef0123456789abcdef01234567");
    log.env
        .write_file("src/auth/session.py", "max_age = None\n");
    log.env
        .write_file("src/auth/cookies.py", "secure = False\n");
    log.env
        .write_file("tests/test_auth.py", "def test_logout(): ...\n");
    log.prompt("fix the flaky logout test and keep the session cookie behaviour intact");
    log.prompt("subtask: start with the cookie helper");
    log.read("tests/test_auth.py");
    log.edit("src/auth/session.py", "max_age = 0\n");
    log.command_fail(
        "pytest tests/test_auth.py -x",
        1,
        "E   assert response.cookies[\"session\"] is None\ntests/test_auth.py:88: AssertionError\n1 failed, 11 passed",
    );
    log.git_restore(
        "git restore src/auth/session.py",
        &[("src/auth/session.py", "max_age = None\n")],
    );
    log.edit("src/auth/cookies.py", "secure = True\n");
    log.prompt("what else could be keeping the cookie alive?");
    // Rendered at the spec's 800 for the same reason as e1_many_files: the
    // claim under test is that a full state yields every section, which is a
    // statement about the ladder, not about the default's margin.
    let capsule = log.capsule_at(800);
    check(&capsule);
    for section in [
        "[ROOT_TASK_OBJECTIVE]",
        "[ACTIVE_SUBTASK]",
        "[LATEST_REQUEST]",
        "[STATUS]",
        "[ACTIVE_FAILURE]",
        "[DEAD_ENDS]",
        "[RECENT_ATTEMPTS]",
        "[WORKING_FILES]",
        "[NEXT_KNOWN_TARGET]",
        "[RECOVERY]",
    ] {
        assert!(capsule.contains(section), "missing {section}:\n{capsule}");
    }
    golden_capsule("full_state", &capsule);
}

// -------------------------------------------------------------- E4: sources

#[test]
fn e4_every_line_traces_to_a_row() {
    let mut log = Log::new();
    log.env.write_file("src/a.rs", "v0\n");
    log.prompt("fix the flaky logout test and keep the cookie behaviour intact");
    log.edit("src/a.rs", "v1\n");
    log.command_fail("pytest -x", 1, "FAILED tests/test_a.py::test_x\n1 failed");
    log.edit("src/a.rs", "v0\n");
    let snapshot = log.snapshot();
    let traced = render::render_traced(&snapshot, &RenderConfig::default()).text;

    let exempt = |line: &str| {
        line.starts_with("<VELRA_CONTINUATION")
            || line == "</VELRA_CONTINUATION>"
            || line == "[CONTEXT]"
            || line.starts_with("Velra is a local tool")
            || line == "[RECOVERY]"
            || line.starts_with("Full detail for any section:")
    };
    for line in traced.lines() {
        if exempt(line) {
            continue;
        }
        assert!(line.contains("#src="), "untraceable line: {line}");
    }
}

// --------------------------------------------------------------- E2: budget

fn arb_text(max: usize) -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop_oneof![Just('a'), Just(' '), Just('/'), Just('.'), Just('9')],
        0..max,
    )
    .prop_map(|c| c.into_iter().collect())
}

prop_compose! {
    fn arb_snapshot()(
        root in prop::option::of(arb_text(600)),
        subtask in prop::option::of(arb_text(400)),
        latest in prop::option::of(arb_text(400)),
        branch in prop::option::of(arb_text(120)),
        edits in 0u32..9999,
        failure_lines in prop::collection::vec(arb_text(300), 0..12),
        command in arb_text(400),
        dead_ends in 0usize..6,
        attempts in 0usize..6,
        files in 0usize..12,
        partial in any::<bool>(),
    ) -> Snapshot {
        let intent = |text: Option<String>, id: i64| text.map(|t| IntentView { id, text: t, ts_ms: common::BASE_MS });
        Snapshot {
            checkpoint_id: "ckpt_01PROPTEST".into(),
            created_ms: common::BASE_MS,
            trigger: Trigger::Auto,
            partial,
            preview: false,
            session_id: "s".into(),
            project_id: "p".into(),
            epoch: 1,
            root: intent(root, 1),
            subtask: intent(subtask, 2),
            latest: intent(latest, 3),
            git: branch.map(|b| GitInfo { branch: Some(b), head: Some("a".repeat(40)) }),
            edit_count: edits,
            last_test: Some(CommandRef { id: 1, command: "pytest".into(), outcome: Outcome::Fail }),
            failure: Some(FailureView {
                id: 1,
                kind: CommandKind::Test,
                command,
                exit_code: Some(1),
                excerpt: failure_lines,
                ts_ms: common::BASE_MS,
            }),
            failing_count: 1,
            dead_ends: (0..dead_ends).map(|i| DeadEndView {
                id: i as i64,
                path: format!("src/{}.rs", "d".repeat(60)),
                subagent: i % 2 == 0,
                edit_ids: vec![1, 2],
                mechanism: Mechanism::GitCommand,
                command: Some("git restore ".to_string() + &"x".repeat(200)),
                resolved_ms: common::BASE_MS,
                minus: Some("-".repeat(160)),
                plus: Some("+".repeat(160)),
                observed_after: Some(CommandRef { id: 2, command: "y".repeat(200), outcome: Outcome::Fail }),
            }).collect(),
            dead_end_total: dead_ends as u32,
            attempts: (0..attempts).map(|i| AttemptView {
                edit_id: i as i64,
                path: format!("src/attempt{}.rs", "a".repeat(60)),
                subagent: false,
                added: Some(12),
                removed: None,
                ts_ms: common::BASE_MS,
                afterward: None,
            }).collect(),
            working_files: (0..files).map(|i| WorkingFileView {
                path: format!("src/file{i}{}.rs", "w".repeat(60)),
                edits: 3,
                reads: 9,
                in_failure: true,
            }).collect(),
            next_target: Some(NextTarget { rule: "failure-location", target: "src/x.rs:12".into(), source: "commands:1".into() }),
            tz_offset_secs: 0,
        }
    }
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(256))]

    /// E2: the hard ceiling and character cap hold for every state.
    #[test]
    fn e2_budget_always_holds(snapshot in arb_snapshot()) {
        let rendered = render::render(&snapshot, &RenderConfig::default());
        prop_assert!(rendered.tokens <= HARD_CEILING_TOKENS, "{} tokens", rendered.tokens);
        prop_assert!(rendered.text.chars().count() <= ABSOLUTE_MAX_CHARS);
        prop_assert!(rendered.text.ends_with("</VELRA_CONTINUATION>"));
        // Sections that are never removed.
        prop_assert!(rendered.text.contains("[CONTEXT]"));
        prop_assert!(rendered.text.contains("[STATUS]"));
        prop_assert!(rendered.text.contains("[RECOVERY]"));
    }

    /// E2: a state that fits the target is rendered in full.
    #[test]
    fn e2_small_states_stay_under_target(root in arb_text(120)) {
        let mut snapshot = arb_snapshot_minimal();
        snapshot.root = Some(IntentView { id: 1, text: root, ts_ms: common::BASE_MS });
        let rendered = render::render(&snapshot, &RenderConfig::default());
        prop_assert!(
            rendered.tokens <= DEFAULT_BUDGET_TOKENS,
            "{} tokens",
            rendered.tokens
        );
        prop_assert_eq!(rendered.steps, 0);
    }
}

fn arb_snapshot_minimal() -> Snapshot {
    Snapshot {
        checkpoint_id: "ckpt_01MIN".into(),
        created_ms: common::BASE_MS,
        trigger: Trigger::Manual,
        partial: false,
        preview: false,
        session_id: "s".into(),
        project_id: "p".into(),
        epoch: 1,
        root: None,
        subtask: None,
        latest: None,
        git: None,
        edit_count: 0,
        last_test: None,
        failure: None,
        failing_count: 0,
        dead_ends: vec![],
        dead_end_total: 0,
        attempts: vec![],
        working_files: vec![],
        next_target: None,
        tz_offset_secs: 0,
    }
}

#[test]
fn e2_truncation_ladder_runs_in_order() {
    let mut snapshot = arb_snapshot_minimal();
    snapshot.working_files = (0..8)
        .map(|i| WorkingFileView {
            path: format!("src/file{i}.rs"),
            edits: 2,
            reads: 3,
            in_failure: false,
        })
        .collect();
    snapshot.attempts = (0..4)
        .map(|i| AttemptView {
            edit_id: i,
            path: format!("src/attempt{i}.rs"),
            subagent: false,
            added: Some(4),
            removed: Some(2),
            ts_ms: common::BASE_MS,
            afterward: None,
        })
        .collect();
    snapshot.root = Some(IntentView {
        id: 1,
        text: "r".repeat(240),
        ts_ms: common::BASE_MS,
    });

    let full = render::render(&snapshot, &RenderConfig::default());
    assert_eq!(full.steps, 0, "fits the default budget");
    assert_eq!(full.text.matches("\n- src/file").count(), 8);

    // A tight budget drops working files first, then attempts.
    let tight = render::render(&snapshot, &RenderConfig { budget_tokens: 120 });
    assert!(tight.steps > 0);
    assert!(tight.text.matches("\n- src/file").count() <= 4);
    assert!(tight.tokens <= HARD_CEILING_TOKENS);
}

/// E2/H3: the budget estimator must stay at or above what the real tokenizer
/// charges, or the truncation ladder stops while the block is still over
/// budget.
///
/// The fixtures are two `<VELRA_CONTINUATION>` blocks Claude Code actually
/// received during the v0.1 benchmark, alongside the token count each one
/// really cost — measured, not modelled, by differencing billed input tokens
/// between two otherwise identical minimal sessions. With the original
/// `ceil(chars / 3.2)` estimator those blocks read as 643 and 743 tokens
/// against a declared budget of 800, so nothing was ever trimmed and Velra
/// shipped 951 and 1,147.
#[test]
fn estimator_is_above_real_tokenizer_counts() {
    let dir =
        std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tokenizer");
    let measured: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(dir.join("measured.json")).expect("measured"),
    )
    .expect("json");
    let mut checked = 0;
    for (name, entry) in measured.as_object().expect("object") {
        let text = std::fs::read_to_string(dir.join(name)).expect("fixture");
        let real = entry["measured_tokens"].as_u64().expect("count") as u32;
        let est = velra_core::text::estimate_tokens(&text);
        assert!(
            est >= real,
            "{name}: estimate {est} is below the {real} tokens it really costs"
        );
        assert!(
            est <= real + real / 4,
            "{name}: estimate {est} overshoots {real} by more than a quarter, \
             which spends budget on content the capsule could have carried"
        );
        checked += 1;
    }
    assert_eq!(checked, 2, "both fixtures were checked");
}

/// H2: a session in which an approach was tried and discarded must render a
/// `[DEAD_ENDS]` section. This is the product feature the benchmark found
/// missing from a delivered capsule, so it is asserted end to end rather than
/// only at the unit level.
#[test]
fn a_discarded_approach_always_reaches_the_capsule() {
    let mut log = Log::new();
    log.env.write_file("src/money.py", "ROUND_HALF_UP\n");
    log.prompt("the gap is one cent, so try the rounding hypothesis first");
    log.edit("src/money.py", "ROUND_HALF_EVEN\n");
    log.command_fail(
        "python -m pytest -q",
        1,
        "FAILED tests/test_engine.py::test_exact_payment\n2 failed, 4 passed",
    );
    log.git_restore(
        "git restore src/money.py",
        &[("src/money.py", "ROUND_HALF_UP\n")],
    );

    let capsule = log.capsule();
    assert!(capsule.contains("[DEAD_ENDS]"), "{capsule}");
    assert!(capsule.contains("src/money.py"), "{capsule}");
    assert!(
        capsule.contains("reverted via `git restore src/money.py`"),
        "the mechanism and the command that caused it:\n{capsule}"
    );
}

/// The `[WORKING_FILES]` defect the benchmark found, reproduced offline.
///
/// In `saturated-velra-r1` the agent spent eight turns auditing 84 unrelated
/// modules after finding the failure. Every one of those reads scored the same
/// single point as the three modules the bug actually lived in, ties went to
/// whatever was touched most recently, and the capsule listed
/// `validation/invoice_number_gb.py` and friends while `engine.py` and
/// `rules.py` did not appear at all.
#[test]
fn an_unrelated_read_sweep_does_not_evict_the_files_the_task_is_about() {
    let mut log = Log::new();
    for f in ["src/money.py", "src/rules.py", "src/engine.py"] {
        log.env.write_file(f, "x = 1\n");
    }
    log.prompt("a test is failing by one cent; find out why and fix it");
    log.command_fail(
        "python -m pytest -q",
        1,
        "E       AssertionError: assert 'UNDERPAID' == 'SETTLED'\n\
         tests/test_engine.py:54: AssertionError\n1 failed, 5 passed",
    );
    // The three modules the task is about, read early.
    for f in ["src/money.py", "src/rules.py", "src/engine.py"] {
        log.read(f);
    }
    // The rounding hypothesis, tried and discarded.
    log.edit("src/money.py", "x = 2\n");
    log.git_restore("git restore src/money.py", &[("src/money.py", "x = 1\n")]);

    // Then an audit: forty unrelated modules, each read exactly once, each of
    // them more recently than anything above.
    for i in 0..40 {
        let path = format!("src/validation/rule_{i:02}.py");
        log.env.write_file(&path, "y = 1\n");
        log.read(&path);
    }

    let capsule = log.capsule();
    let working: Vec<&str> = capsule
        .lines()
        .skip_while(|l| !l.starts_with("[WORKING_FILES]"))
        .skip(1)
        .take_while(|l| l.starts_with("- "))
        .collect();
    assert!(!working.is_empty(), "{capsule}");
    for f in ["src/money.py", "src/engine.py", "src/rules.py"] {
        assert!(
            working.iter().any(|l| l.contains(f)),
            "{f} must survive the sweep, got {working:#?}\n{capsule}"
        );
    }
    // The sweep may fill what is left, but not the front of the list.
    assert!(
        working[0].contains("src/money.py"),
        "the discarded file ranks first: {working:#?}"
    );
}