speccoder-runtime 0.9.6

Application services of speccoder: the only crate that executes anything.
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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! Anchored comments: drafts in the tray, one scoped
//! revision turn of the revising skill, sections outside the comments
//! flagged, a byte-exact undo, and Code-view comments as one nudge.

// Test code: panicking on unexpected input is the failure report.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;

use agentplane::model::ModelProvider;
use agentplane::model::fake::FakeProvider;
use serde_json::json;
use speccoder_core::actions::{CommentOp, Via};
use speccoder_core::oracle::{CodeComment, CommentAnchor, CommentsView, DraftState};
use speccoder_core::{CommandData, CommandResult, CommandStatus};
use speccoder_runtime::RunOptions;
use speccoder_runtime::comments;

const SPEC: &str = "# Spec\n\n### User Story 1 - Login (Priority: P1)\n\nUsers log in.\n\n**Acceptance Scenarios**:\n\n1. **Given** a user, **When** they log in, **Then** a `login` function exists.\n\n## Requirements\n\n- **FR-001**: Users MUST log in.\n- **FR-002**: Users MUST be refused with an empty password.\n\n## Edge Cases\n\n- An empty name is refused.\n";

const SKILL: &str = "---\nname: \"speckit-clarify\"\ndescription: \"Revise the spec.\"\nuser-invocable: true\n---\n\n## User Input\n\n$ARGUMENTS\n\nRevise specs/001-graph/spec.md as the input says.\n";

fn git(dir: &Path, args: &[&str]) -> String {
    let out = Command::new("git")
        .args(["-c", "maintenance.auto=false", "-c", "gc.auto=0"])
        .args(["-c", "user.name=Ada", "-c", "user.email=ada@example.com"])
        .args([
            "-c",
            "commit.gpgsign=false",
            "-c",
            "init.defaultBranch=main",
        ])
        .args(["-c", "core.hooksPath=/dev/null"])
        .args(args)
        .current_dir(dir)
        .output()
        .unwrap();
    assert!(out.status.success(), "git {args:?}");
    String::from_utf8_lossy(&out.stdout).into_owned()
}

fn repo() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    let r = dir.path();
    let fixture =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../speccoder-speckit/tests/fixtures/graph");
    assert!(
        Command::new("cp")
            .arg("-R")
            .arg(fixture.join("."))
            .arg(r)
            .status()
            .unwrap()
            .success()
    );
    fs::write(r.join("specs/001-graph/spec.md"), SPEC).unwrap();
    fs::write(r.join("specs/001-graph/plan.md"), "# Plan\n\nRust.\n").unwrap();
    fs::create_dir_all(r.join(".claude/skills/speckit-clarify")).unwrap();
    fs::write(r.join(".claude/skills/speckit-clarify/SKILL.md"), SKILL).unwrap();
    fs::create_dir_all(r.join("src")).unwrap();
    fs::write(r.join("src/lib.rs"), "pub fn ok() {}\n").unwrap();
    fs::write(
        r.join("speccoder.toml"),
        "[model]\nprovider = \"openai\"\nmodel = \"gpt-5.4-mini\"\n",
    )
    .unwrap();
    git(r, &["init", "-q"]);
    git(r, &["config", "user.name", "Ada"]);
    git(r, &["config", "user.email", "ada@example.com"]);
    git(r, &["add", "-A"]);
    git(r, &["commit", "-q", "-m", "init"]);
    dir
}

fn view(r: &CommandResult) -> &CommentsView {
    match r.data() {
        Some(CommandData::Comments(c)) => c,
        other => panic!("{} → {other:?}", r.message()),
    }
}

fn passage(start: usize, quote: &str) -> CommentAnchor {
    CommentAnchor::Passage {
        path: "specs/001-graph/spec.md".to_owned(),
        start_line: start,
        end_line: start,
        quote: quote.to_owned(),
        quote_sha256: String::new(),
        section: String::new(),
    }
}

fn add(r: &Path, state: &Path, anchor: CommentAnchor, text: &str) -> CommandResult {
    comments::comment(
        r,
        None,
        CommentOp::Add,
        Some(&anchor),
        None,
        Some(text),
        Via::Web,
        Some(state),
    )
}

fn sha(p: &Path) -> Vec<u8> {
    fs::read(p).unwrap()
}

#[test]
fn us5_drafts_persist_go_out_as_one_scoped_turn_and_undo_restores_bytes() {
    let r = repo();
    let state = tempfile::tempdir().unwrap();
    // Drafts on two sections, anchored and listed.
    let a = add(
        r.path(),
        state.path(),
        passage(13, "Users MUST log in."),
        "say who logs in",
    );
    assert_eq!(a.status(), CommandStatus::Ok, "{}", a.message());
    let b = add(
        r.path(),
        state.path(),
        passage(18, "An empty name is refused."),
        "and a long one",
    );
    assert_eq!(b.status(), CommandStatus::Ok, "{}", b.message());
    let listed = comments::list(r.path(), None, Some(state.path()));
    let v = view(&listed);
    assert_eq!(v.drafts.len(), 2);
    let CommentAnchor::Passage {
        section,
        quote_sha256,
        ..
    } = &v.drafts[0].anchor
    else {
        panic!()
    };
    assert_eq!(section, "Spec › Requirements");
    assert!(quote_sha256.starts_with("sha256:"));
    // An anchor outside the feature's documents is refused.
    let refused = add(
        r.path(),
        state.path(),
        CommentAnchor::Passage {
            path: "specs/001-graph/review.json".to_owned(),
            start_line: 1,
            end_line: 1,
            quote: "{".to_owned(),
            quote_sha256: String::new(),
            section: String::new(),
        },
        "no",
    );
    assert_eq!(refused.status(), CommandStatus::UsageError);
    let wrong = add(r.path(), state.path(), passage(3, "not there"), "no");
    assert_eq!(wrong.status(), CommandStatus::UsageError);

    // One skill call: both quotes, their sections; it also changes a
    // section nobody commented on.
    let spec = r.path().join("specs/001-graph/spec.md");
    let before = sha(&spec);
    let revised = SPEC
        .replace("Users MUST log in.", "Registered users MUST log in.")
        .replace("Users log in.", "People log in with email.");
    let f = FakeProvider::new();
    let d: Arc<dyn ModelProvider> = f.clone();
    f.will_call_tool(
        "w",
        "write_file",
        json!({ "path": "specs/001-graph/spec.md", "content": revised }),
    );
    f.will_structure(json!({ "summary": "revised", "status": "done", "files_written": ["specs/001-graph/spec.md"], "questions": [] }));
    let sent = comments::send_with(
        r.path(),
        None,
        &[],
        Via::Web,
        state.path(),
        Some(d),
        None,
        &RunOptions::default(),
    );
    assert_eq!(sent.status(), CommandStatus::Ok, "{}", sent.message());
    let calls: Vec<String> = f
        .asked()
        .iter()
        .map(|a| serde_json::to_string(&a.prompt).unwrap())
        .collect();
    let first = &calls[0];
    assert!(
        first.contains("say who logs in") && first.contains("and a long one"),
        "{first}"
    );
    assert!(first.contains("Every other section stays exactly as it is"));
    assert!(first.contains("Spec › Requirements") && first.contains("Spec › Edge Cases"));
    let v = view(&sent);
    assert!(v.drafts.is_empty(), "consumed");
    let turn = v.turn.clone().unwrap();
    assert_eq!(turn.outcome, "pending");
    assert!(
        turn.changed
            .iter()
            .any(|c| c == "specs/001-graph/spec.md: Spec › Requirements"),
        "{turn:?}"
    );
    assert_eq!(
        turn.outside,
        ["specs/001-graph/spec.md: Spec › User Story 1 - Login (Priority: P1)"],
        "{turn:?}"
    );
    assert!(turn.beside.is_empty(), "{turn:?}");
    // The word diff's material: each changed section before and after.
    let req = turn
        .diffs
        .iter()
        .find(|d| d.heading == "Spec › Requirements")
        .unwrap_or_else(|| panic!("{turn:?}"));
    assert!(
        req.before.contains("Users MUST log in.")
            && req.after.contains("Registered users MUST log in.")
    );
    assert!(!req.outside);
    assert!(
        turn.diffs
            .iter()
            .any(|d| d.outside && d.heading.starts_with("Spec › User Story 1"))
    );
    assert_eq!(turn.files, ["specs/001-graph/spec.md"]);
    // A second send of the same drafts is refused.
    let again = comments::send_with(
        r.path(),
        None,
        &["c1".to_owned()],
        Via::Web,
        state.path(),
        None,
        None,
        &RunOptions::default(),
    );
    assert_eq!(
        again.status(),
        CommandStatus::UsageError,
        "{}",
        again.message()
    );

    // Undo: byte for byte, the comments back in the tray.
    let undone = comments::undo(r.path(), None, Some(&turn.id), Some(state.path()));
    assert_eq!(undone.status(), CommandStatus::Ok, "{}", undone.message());
    assert!(
        undone
            .message()
            .contains("restored specs/001-graph/spec.md")
    );
    assert_eq!(sha(&spec), before);
    assert_eq!(view(&undone).drafts.len(), 2);
    assert_eq!(view(&undone).turn.as_ref().unwrap().outcome, "undone");
}

#[test]
fn us5_undo_is_refused_when_a_document_changed_since_and_outdated_drafts_wait() {
    let r = repo();
    let state = tempfile::tempdir().unwrap();
    assert_eq!(
        add(
            r.path(),
            state.path(),
            passage(13, "Users MUST log in."),
            "who?"
        )
        .status(),
        CommandStatus::Ok
    );
    let f = FakeProvider::new();
    let d: Arc<dyn ModelProvider> = f.clone();
    f.will_call_tool(
        "w",
        "write_file",
        json!({ "path": "specs/001-graph/spec.md", "content": SPEC.replace("Users MUST log in.", "Users MUST sign in.") }),
    );
    f.will_structure(
        json!({ "summary": "revised", "status": "done", "files_written": [], "questions": [] }),
    );
    let sent = comments::send_with(
        r.path(),
        None,
        &[],
        Via::Web,
        state.path(),
        Some(d),
        None,
        &RunOptions::default(),
    );
    assert_eq!(sent.status(), CommandStatus::Ok, "{}", sent.message());
    // The person edits the spec after the turn: undo would overwrite it.
    let spec = r.path().join("specs/001-graph/spec.md");
    fs::write(
        &spec,
        format!("{}\nMore.\n", fs::read_to_string(&spec).unwrap()),
    )
    .unwrap();
    let now = sha(&spec);
    let undo = comments::undo(r.path(), None, None, Some(state.path()));
    assert_eq!(
        undo.status(),
        CommandStatus::NeedsHuman,
        "{}",
        undo.message()
    );
    assert!(
        undo.message()
            .contains("specs/001-graph/spec.md changed since")
    );
    assert_eq!(sha(&spec), now, "nothing written");

    // A draft whose words moved away is outdated, and not sent unless kept.
    let c = add(
        r.path(),
        state.path(),
        passage(18, "An empty name is refused."),
        "and long",
    );
    assert_eq!(c.status(), CommandStatus::Ok, "{}", c.message());
    fs::write(&spec, "# Spec\n\nRewritten entirely.\n").unwrap();
    let listed = comments::list(r.path(), None, Some(state.path()));
    let d = &view(&listed).drafts[0];
    assert_eq!(d.state, DraftState::Outdated);
    let id = d.id.clone();
    let none = comments::send_with(
        r.path(),
        None,
        &[],
        Via::Web,
        state.path(),
        None,
        None,
        &RunOptions::default(),
    );
    assert_eq!(
        none.status(),
        CommandStatus::UsageError,
        "{}",
        none.message()
    );
    let kept = comments::comment(
        r.path(),
        None,
        CommentOp::Keep,
        None,
        Some(&id),
        None,
        Via::Web,
        Some(state.path()),
    );
    assert_eq!(kept.status(), CommandStatus::Ok, "{}", kept.message());
    assert_eq!(view(&kept).drafts[0].state, DraftState::Draft);
}

#[test]
fn us5_a_finding_anchor_must_be_one_the_review_shows() {
    let r = repo();
    let state = tempfile::tempdir().unwrap();
    let ok = add(
        r.path(),
        state.path(),
        CommentAnchor::Finding {
            source: "review".to_owned(),
            id: "FR-002".to_owned(),
            text: "Users MUST be refused with an empty password.".to_owned(),
        },
        "too vague",
    );
    assert_eq!(ok.status(), CommandStatus::Ok, "{}", ok.message());
    for (source, id) in [("review", "FR-999"), ("extension", "X1")] {
        let bad = add(
            r.path(),
            state.path(),
            CommentAnchor::Finding {
                source: source.to_owned(),
                id: id.to_owned(),
                text: "x".to_owned(),
            },
            "no",
        );
        assert_eq!(bad.status(), CommandStatus::UsageError, "{source}:{id}");
    }
}

#[test]
fn us6_code_comments_are_one_nudge_with_their_anchors() {
    let r = repo();
    let state = tempfile::tempdir().unwrap();
    let res = comments::code_nudge(
        r.path(),
        None,
        "T005",
        &[
            CodeComment {
                path: "src/a.rs".to_owned(),
                line: 12,
                quote: "let x = 1;".to_owned(),
                text: "use a constant".to_owned(),
            },
            CodeComment {
                path: "src/b.rs".to_owned(),
                line: 3,
                quote: "fn b() {}".to_owned(),
                text: "name it".to_owned(),
            },
        ],
        Some(state.path()),
    );
    assert_eq!(res.status(), CommandStatus::Ok, "{}", res.message());
    assert!(res.message().contains("the next attempt of T005"));
    let plane = speccoder_runtime::worktree::project_state_dir(state.path(), r.path());
    let d = speccoder_runtime::approvals::Decisions::load(&plane).unwrap();
    let nudges = d.for_task("001-graph", "T005").nudges;
    assert_eq!(
        nudges,
        ["src/a.rs:12 \"let x = 1;\" — use a constant\nsrc/b.rs:3 \"fn b() {}\" — name it"]
    );
    let empty = comments::code_nudge(r.path(), None, "T005", &[], Some(state.path()));
    assert_eq!(empty.status(), CommandStatus::UsageError);
}

/// The scripted model, and a person who edits a document while the
/// revision runs: before the model's `at`-th answer, `edit` is applied.
#[derive(Debug)]
struct EditsMidTurn {
    inner: Arc<FakeProvider>,
    at: usize,
    seen: std::sync::Mutex<usize>,
    file: PathBuf,
    append: &'static str,
}

#[agentplane::prelude::async_trait]
impl ModelProvider for EditsMidTurn {
    async fn complete(
        &self,
        request: agentplane::model::Request<'_>,
    ) -> Result<agentplane::model::Completion, agentplane::model::ModelError> {
        let n = {
            let mut seen = self.seen.lock().unwrap();
            *seen += 1;
            *seen
        };
        if n == self.at {
            let now = fs::read_to_string(&self.file).unwrap_or_default();
            fs::write(&self.file, format!("{now}{}", self.append)).unwrap();
        }
        self.inner.complete(request).await
    }

    fn gen_ai_provider(&self) -> Option<&str> {
        None
    }
}

fn revise_spec(f: &FakeProvider, content: &str) {
    f.will_call_tool(
        "w",
        "write_file",
        json!({ "path": "specs/001-graph/spec.md", "content": content }),
    );
    f.will_structure(
        json!({ "summary": "revised", "status": "done", "files_written": [], "questions": [] }),
    );
}

#[test]
fn us5_undo_never_reverts_what_the_person_wrote_while_the_revision_ran() {
    // P6: the person edits the spec (after the skill's write) and the plan
    // while the turn runs: those edits are not the turn's.
    for (file, at) in [
        ("specs/001-graph/spec.md", 2),
        ("specs/001-graph/plan.md", 1),
    ] {
        let r = repo();
        let state = tempfile::tempdir().unwrap();
        assert_eq!(
            add(
                r.path(),
                state.path(),
                passage(13, "Users MUST log in."),
                "who?"
            )
            .status(),
            CommandStatus::Ok
        );
        let f = FakeProvider::new();
        revise_spec(
            &f,
            &SPEC.replace("Users MUST log in.", "Users MUST sign in."),
        );
        let d: Arc<dyn ModelProvider> = Arc::new(EditsMidTurn {
            inner: f.clone(),
            at,
            seen: std::sync::Mutex::new(0),
            file: r.path().join(file),
            append: "\nMy own note.\n",
        });
        let sent = comments::send_with(
            r.path(),
            None,
            &[],
            Via::Web,
            state.path(),
            Some(d),
            None,
            &RunOptions::default(),
        );
        assert_eq!(sent.status(), CommandStatus::Ok, "{}", sent.message());
        let turn = view(&sent).turn.clone().unwrap();
        assert_eq!(turn.beside, [file], "{file}: {turn:?}");
        assert!(
            sent.message().contains("changed beside the revision"),
            "{}",
            sent.message()
        );
        let mine = fs::read_to_string(r.path().join(file)).unwrap();
        assert!(mine.contains("My own note."));
        let undo = comments::undo(r.path(), None, None, Some(state.path()));
        assert_eq!(
            undo.status(),
            CommandStatus::NeedsHuman,
            "{}",
            undo.message()
        );
        assert_eq!(
            fs::read_to_string(r.path().join(file)).unwrap(),
            mine,
            "{file}: the person's edit was reverted"
        );
    }
}

#[test]
fn us5_a_heading_is_a_section_of_its_own_document() {
    // P6: the plan gains a section named like the commented one in the spec.
    let r = repo();
    let state = tempfile::tempdir().unwrap();
    assert_eq!(
        add(
            r.path(),
            state.path(),
            passage(13, "Users MUST log in."),
            "who?"
        )
        .status(),
        CommandStatus::Ok
    );
    let f = FakeProvider::new();
    f.will_call_tool(
        "w",
        "write_file",
        json!({ "path": "specs/001-graph/plan.md", "content": "# Spec\n\n## Requirements\n\nRewritten.\n" }),
    );
    f.will_structure(
        json!({ "summary": "revised", "status": "done", "files_written": [], "questions": [] }),
    );
    let d: Arc<dyn ModelProvider> = f.clone();
    let sent = comments::send_with(
        r.path(),
        None,
        &[],
        Via::Web,
        state.path(),
        Some(d),
        None,
        &RunOptions::default(),
    );
    let turn = view(&sent).turn.clone().unwrap();
    assert!(
        turn.outside
            .iter()
            .any(|s| s == "specs/001-graph/plan.md: Spec › Requirements"),
        "{turn:?}"
    );
}

#[test]
fn an_unreadable_tray_is_refused_never_read_as_empty() {
    // C6: a corrupt tray is not overwritten by the next add.
    let r = repo();
    let state = tempfile::tempdir().unwrap();
    assert_eq!(
        add(
            r.path(),
            state.path(),
            passage(13, "Users MUST log in."),
            "who?"
        )
        .status(),
        CommandStatus::Ok
    );
    let plane = speccoder_runtime::worktree::project_state_dir(state.path(), r.path());
    let tray = plane.join("comments/001-graph.json");
    fs::write(&tray, "{ not json").unwrap();
    for res in [
        comments::list(r.path(), None, Some(state.path())),
        add(
            r.path(),
            state.path(),
            passage(18, "An empty name is refused."),
            "x",
        ),
        comments::undo(r.path(), None, None, Some(state.path())),
    ] {
        assert_eq!(
            res.code().map(|c| c.name),
            Some("REVIEW-UNREADABLE"),
            "{}",
            res.message()
        );
    }
    assert_eq!(fs::read_to_string(&tray).unwrap(), "{ not json");
}

#[test]
fn undo_writes_only_the_unit_s_documents_a_tampered_tray_names() {
    // P6: a tray naming another file (here: src/lib.rs, "absent" before)
    // never makes Undo delete or write it.
    let r = repo();
    let state = tempfile::tempdir().unwrap();
    assert_eq!(
        add(
            r.path(),
            state.path(),
            passage(13, "Users MUST log in."),
            "who?"
        )
        .status(),
        CommandStatus::Ok
    );
    let f = FakeProvider::new();
    revise_spec(
        &f,
        &SPEC.replace("Users MUST log in.", "Users MUST sign in."),
    );
    let d: Arc<dyn ModelProvider> = f.clone();
    let sent = comments::send_with(
        r.path(),
        None,
        &[],
        Via::Web,
        state.path(),
        Some(d),
        None,
        &RunOptions::default(),
    );
    assert_eq!(sent.status(), CommandStatus::Ok, "{}", sent.message());
    let plane = speccoder_runtime::worktree::project_state_dir(state.path(), r.path());
    let file = plane.join("comments/001-graph.json");
    let mut tray: serde_json::Value =
        serde_json::from_str(&fs::read_to_string(&file).unwrap()).unwrap();
    let lib = fs::read(r.path().join("src/lib.rs")).unwrap();
    let digest = format!("sha256:{}", speccoder_runtime::integrity::sha256_hex(&lib));
    let turn = &mut tray["turns"][0];
    turn["before"] = json!([{ "path": "src/lib.rs", "sha256": "absent" }]);
    turn["after"] = json!([{ "path": "src/lib.rs", "sha256": digest }]);
    fs::write(&file, serde_json::to_string(&tray).unwrap()).unwrap();
    let undo = comments::undo(r.path(), None, None, Some(state.path()));
    assert_ne!(undo.status(), CommandStatus::Ok, "{}", undo.message());
    assert_eq!(fs::read(r.path().join("src/lib.rs")).unwrap(), lib);
}

#[test]
fn us6_a_frozen_test_is_known_by_its_code_view_path_in_a_project_below_the_top() {
    // P7: the project sits in `app/` of its repository; the Code view names
    // `app/tests/…`, the frozen record `tests/…`.
    let top = tempfile::tempdir().unwrap();
    let app = top.path().join("app");
    let fixture =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../speccoder-speckit/tests/fixtures/graph");
    fs::create_dir_all(&app).unwrap();
    assert!(
        Command::new("cp")
            .arg("-R")
            .arg(fixture.join("."))
            .arg(&app)
            .status()
            .unwrap()
            .success()
    );
    fs::write(app.join("specs/001-graph/spec.md"), SPEC).unwrap();
    fs::write(
        app.join("speccoder.toml"),
        "[model]\nprovider = \"openai\"\nmodel = \"gpt-5.4-mini\"\n",
    )
    .unwrap();
    git(top.path(), &["init", "-q"]);
    git(top.path(), &["add", "-A"]);
    git(top.path(), &["commit", "-q", "-m", "init"]);
    let state = tempfile::tempdir().unwrap();
    let plane = speccoder_runtime::worktree::project_state_dir(state.path(), &app);
    let content = "checks fn login\n".to_owned();
    let record = speccoder_core::acceptance::PreFrozenAcceptance {
        unit: "001-graph".to_owned(),
        requirement: "US1".to_owned(),
        wording_digest: "sha256:w".to_owned(),
        approval_digest: "sha256:a".to_owned(),
        visible: vec![speccoder_core::acceptance::VisibleTest {
            path: "tests/acceptance_us1.txt".to_owned(),
            sha256: format!(
                "sha256:{}",
                speccoder_runtime::integrity::sha256_hex(content.as_bytes())
            ),
            cites: vec!["US1-1".to_owned()],
        }],
        held_out_sealed: None,
        producer: speccoder_core::acceptance::Producer::Review,
        decided: speccoder_core::acceptance::Decided {
            by: "Ada".to_owned(),
            via: Via::Web,
            at: 0,
        },
    };
    speccoder_runtime::pre_frozen::store(
        &plane,
        &record,
        &[("tests/acceptance_us1.txt".to_owned(), content)],
    )
    .unwrap();
    let res = comments::code_nudge(
        &app,
        None,
        "T005",
        &[CodeComment {
            path: "app/tests/acceptance_us1.txt".to_owned(),
            line: 1,
            quote: "checks fn login".to_owned(),
            text: "loosen it".to_owned(),
        }],
        Some(state.path()),
    );
    assert_eq!(res.status(), CommandStatus::UsageError, "{}", res.message());
    assert!(
        res.message().contains("is a frozen test"),
        "{}",
        res.message()
    );
}

#[test]
fn a_comment_anchors_to_a_line_changed_since_the_last_commit() {
    // T063: the Review's "since the last commit" lines carry their place.
    let r = repo();
    let state = tempfile::tempdir().unwrap();
    let spec = r.path().join("specs/001-graph/spec.md");
    fs::write(
        &spec,
        SPEC.replace("Users MUST log in.", "Users MUST sign in."),
    )
    .unwrap();
    let rv = speccoder_runtime::review::report_in(r.path(), None, Some(state.path()));
    let Some(CommandData::Review(rv)) = rv.data() else {
        panic!("{}", rv.message())
    };
    let c = rv.changes.iter().find(|c| c.id == "FR-001").unwrap();
    assert_eq!(c.path, "specs/001-graph/spec.md");
    assert_eq!((c.then_line, c.now_line), (Some(13), Some(13)));
    let added = add(
        r.path(),
        state.path(),
        CommentAnchor::DiffLine {
            path: c.path.clone(),
            side: "new".to_owned(),
            line: c.now_line.unwrap() as usize,
            quote: c.now.clone().unwrap(),
        },
        "sign in or log in?",
    );
    assert_eq!(added.status(), CommandStatus::Ok, "{}", added.message());
}