dv-review 0.34.26

A terminal diff viewer for reviewing GitHub PRs: Helix keys, rust-analyzer, inline review comments, and Claude Code reviews
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
//! Replacements: PRs Claude drafts locally to replace others (low-effort ones, say), kept
//! unpublished until you've reviewed them. Each is a branch `dv/replace-N` (N: the first PR it
//! replaces) in a worktree `<repo>-replace-N`, and a record in dv's directory,
//! `replacements/N.json`: the PRs it replaces, its base, and Claude's drafts of its title and
//! description and of the note closing the originals. A new PR (replacing nothing: what you
//! asked for) is the same, as `dv/new-K` in `<repo>-new-K`, its record keeping your request.
use anyhow::{Context, Result};
use serde_json::{Value, json};
use std::path::{Path, PathBuf};

/// A PR a replacement replaces, as it was when the replacement was drafted.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Original {
    pub number: u64,
    pub title: String,
    pub author: String,
    pub url: String,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Record {
    /// The first PR it replaces: its name (`dv/replace-N`, `<repo>-replace-N`, `N.json`).
    pub id: u64,
    pub replaces: Vec<Original>,
    /// The branch it's based on (the originals'), on origin.
    pub base: String,
    /// Claude's drafts: the PR's title and description, and the note for closing the originals.
    pub title: String,
    pub body: String,
    pub closing_note: String,
    /// The Claude Code session that drafted it, to carry on in.
    pub session: String,
    /// Its PR, once published (a draft).
    pub published: Option<u64>,
    /// The originals closed (with the note) since.
    pub closed: Vec<u64>,
    /// The process drafting it now, if one is (`drafting_now` checks it's still running).
    pub drafting: Option<u32>,
    /// A new PR's: what you asked it to do (a replacement's is what it replaces).
    pub task: String,
}

impl Record {
    /// What to draft it again from: the PRs it replaces, or (a new PR) itself.
    pub fn ids(&self) -> Vec<u64> {
        match self.replaces.is_empty() {
            true => vec![self.id],
            false => self.replaces.iter().map(|o| o.number).collect(),
        }
    }

    /// Claude is drafting it now (from any dv): its process is still running.
    pub fn drafting_now(&self) -> bool {
        self.drafting.is_some_and(|pid| {
            let alive = std::process::Command::new("kill").args(["-0", &pid.to_string()]).stderr(std::process::Stdio::null()).status();
            alive.is_ok_and(|s| s.success())
        })
    }
}

/// New PRs' ids start here (`new_id`), above any PR number: new PR K is `NEW + K`.
const NEW: u64 = 1 << 40;

/// Whether `id` is a new PR's (not a replacement's).
pub fn is_new(id: u64) -> bool {
    id >= NEW
}

/// Its name: its branch's (`dv/<name>`) and its worktree's (`<repo>-<name>`).
pub fn name(id: u64) -> String {
    match is_new(id) {
        true => format!("new-{}", id - NEW),
        false => format!("replace-{id}"),
    }
}

/// How it's said: "#N" (the PR a replacement replaces first) or "new K".
pub fn shown(id: u64) -> String {
    match is_new(id) {
        true => format!("new {}", id - NEW),
        false => format!("#{id}"),
    }
}

/// What it is, in words: "the replacement of #N" or "new PR K".
pub fn label(id: u64) -> String {
    match is_new(id) {
        true => format!("new PR {}", id - NEW),
        false => format!("the replacement of #{id}"),
    }
}

pub fn branch(id: u64) -> String {
    format!("dv/{}", name(id))
}

/// The replacement (or new PR) checked out here, by its branch.
pub fn current() -> Option<u64> {
    let b = crate::diff::git(&["branch", "--show-current"]).ok()?;
    let b = b.trim();
    match b.strip_prefix("dv/new-") {
        Some(k) => k.parse::<u64>().ok().map(|k| NEW + k),
        None => b.strip_prefix("dv/replace-")?.parse().ok(),
    }
}

/// The next new PR's id: one past the last there's a record of.
pub fn new_id() -> u64 {
    all().iter().map(|r| r.id).filter(|&id| is_new(id)).max().map_or(NEW + 1, |m| m + 1)
}

fn dir() -> Result<PathBuf> {
    Ok(crate::claude::dv_dir()?.join("replacements"))
}

pub fn path(id: u64) -> Result<PathBuf> {
    Ok(dir()?.join(format!("{id}.json")))
}

pub fn load(id: u64) -> Result<Record> {
    let p = path(id)?;
    parse(id, &std::fs::read_to_string(&p).with_context(|| format!("reading {}", p.display()))?)
}

fn parse(id: u64, text: &str) -> Result<Record> {
    let v: Value = serde_json::from_str(text)?;
    let s = |v: &Value| v.as_str().unwrap_or("").to_owned();
    let replaces = v["replaces"].as_array().map(Vec::as_slice).unwrap_or(&[]);
    Ok(Record {
        id,
        replaces: replaces
            .iter()
            .map(|o| Original { number: o["number"].as_u64().unwrap_or(0), title: s(&o["title"]), author: s(&o["author"]), url: s(&o["url"]) })
            .collect(),
        base: s(&v["base"]),
        title: s(&v["title"]),
        body: s(&v["body"]),
        closing_note: s(&v["closing_note"]),
        session: s(&v["session"]),
        published: v["published"].as_u64(),
        closed: v["closed"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect(),
        drafting: v["drafting"].as_u64().map(|p| p as u32),
        task: s(&v["task"]),
    })
}

fn to_json(r: &Record) -> String {
    let replaces: Vec<Value> =
        r.replaces.iter().map(|o| json!({ "number": o.number, "title": o.title, "author": o.author, "url": o.url })).collect();
    let v = json!({
        "replaces": replaces, "base": r.base, "title": r.title, "body": r.body,
        "closing_note": r.closing_note, "session": r.session, "published": r.published, "closed": r.closed,
        "drafting": r.drafting, "task": r.task,
    });
    serde_json::to_string_pretty(&v).unwrap_or_default() + "\n"
}

pub fn save(r: &Record) -> Result<()> {
    let p = path(r.id)?;
    std::fs::create_dir_all(p.parent().context("no dir")?)?;
    Ok(std::fs::write(p, to_json(r))?)
}

/// When any replacement's record last changed (or one came or went), to notice it.
pub fn changed() -> Option<std::time::SystemTime> {
    let d = dir().ok()?;
    let entries = std::fs::read_dir(&d).ok()?.flatten().filter_map(|e| e.metadata().ok()?.modified().ok());
    entries.chain(std::fs::metadata(&d).ok()?.modified().ok()).max()
}

/// Every replacement in this repo, by number.
pub fn all() -> Vec<Record> {
    let Ok(entries) = dir().and_then(|d| Ok(std::fs::read_dir(d)?)) else { return vec![] };
    let mut out: Vec<Record> = entries
        .flatten()
        .filter_map(|e| e.file_name().to_str()?.strip_suffix(".json")?.parse().ok())
        .filter_map(|id| load(id).ok())
        .collect();
    out.sort_by_key(|r| r.id);
    out
}

/// Set up a replacement of `prs` (the first names it) from the repo at `root`: its record
/// (kept if it exists: drafting again carries on), and its branch, from the originals' base,
/// in its worktree. Returns the record and the worktree.
pub fn prepare(prs: &[u64], root: &Path) -> Result<(Record, PathBuf)> {
    let id = *prs.first().context("no PR to replace")?;
    let git = |args: &[&str]| crate::diff::git(&[&["-C", &root.to_string_lossy()], args].concat());
    let mut r = load(id).unwrap_or(Record { id, ..Record::default() });
    anyhow::ensure!(!r.drafting_now(), "Claude is already drafting the replacement of #{id}");
    let mut originals = vec![];
    let mut bases: Vec<(u64, String)> = vec![];
    for &n in prs {
        let p = crate::gh::pr(Some(n))?;
        bases.push((n, p.base));
        originals.push(Original { number: n, title: p.title, author: p.author, url: p.url });
    }
    // One replacement can only target one branch.
    if let Some((n, b)) = bases.iter().find(|b| b.1 != bases[0].1) {
        anyhow::bail!("#{} targets {}, #{n} targets {b}: one replacement can't replace both", bases[0].0, bases[0].1);
    }
    r.base = bases.first().map(|b| b.1.clone()).unwrap_or_default();
    r.replaces = originals;
    if r.session.is_empty() {
        r.session = crate::claude::new_session_id();
    }
    let dir = crate::worktree_dir_named(root, &name(id))?;
    if dir.exists() {
        crate::ensure_worktree_of(&dir, root)?;
        // Carrying on: from the base as it is now, if that goes cleanly (else Claude sees why).
        _ = rebase(&r, &dir);
    } else {
        _ = git(&["fetch", "-q", &crate::diff::remote(), &r.base]);
        let (b, d) = (branch(id), dir.to_string_lossy().into_owned());
        match git(&["rev-parse", "--verify", "-q", &format!("refs/heads/{b}")]) {
            Ok(_) => git(&["worktree", "add", &d, &b])?,
            Err(_) => git(&["worktree", "add", "-b", &b, &d, &crate::diff::remote_branch(&r.base)])?,
        };
    }
    save(&r)?;
    Ok((r, dir))
}

/// Set up a new PR doing `task` from the repo at `root`: its record, and its branch from the
/// repo's default branch (on its remote), in its worktree. Its id.
pub fn create_new(task: &str, root: &Path) -> Result<u64> {
    let git = |args: &[&str]| crate::diff::git(&[&["-C", &root.to_string_lossy()], args].concat());
    let base = crate::diff::default_branch();
    let id = new_id();
    let r = Record { id, base: base.clone(), session: crate::claude::new_session_id(), task: task.trim().to_owned(), ..Record::default() };
    let dir = crate::worktree_dir_named(root, &name(id))?;
    _ = git(&["fetch", "-q", &crate::diff::remote(), &base]);
    git(&["worktree", "add", "-b", &branch(id), &dir.to_string_lossy(), &crate::diff::remote_branch(&base)])?;
    save(&r)?;
    Ok(id)
}

/// A new PR's record and worktree, to draft it (again): its branch brought up to its base.
fn prepare_new(id: u64, root: &Path) -> Result<(Record, PathBuf)> {
    let r = load(id)?;
    anyhow::ensure!(!r.drafting_now(), "Claude is already drafting {}", label(id));
    let dir = crate::worktree_dir_named(root, &name(id))?;
    anyhow::ensure!(dir.exists(), "its worktree {} is gone", dir.display());
    crate::ensure_worktree_of(&dir, root)?;
    _ = rebase(&r, &dir); // carrying on: from its base as it is now, if that goes cleanly
    Ok((r, dir))
}

/// Bring replacement `r`'s branch (in `dir`) up to its base on origin: fetch, then rebase onto
/// it. A rebase that conflicts is undone, and said so.
pub fn rebase(r: &Record, dir: &Path) -> Result<()> {
    let git = |args: &[&str]| crate::diff::git(&[&["-C", &dir.to_string_lossy()], args].concat());
    git(&["fetch", "-q", &crate::diff::remote(), &r.base])?;
    if let Err(e) = git(&["rebase", "-q", &crate::diff::remote_branch(&r.base)]) {
        _ = git(&["rebase", "--abort"]);
        anyhow::bail!("it doesn't rebase cleanly onto {} ({e:#}): ask Claude to redraft it on the new base", crate::diff::remote_branch(&r.base));
    }
    Ok(())
}

/// What Claude is asked, to draft (or redraft) replacement `r`. Your notes on the repo come
/// with it, to read: a draft reads untrusted PRs, so it gets no access to them (nor to dv's
/// other files), only to its record.
pub fn prompt(r: &Record, extra: &str) -> String {
    let prs: Vec<String> = r.replaces.iter().map(|o| format!("#{}", o.number)).collect();
    let now = crate::claude::now();
    let notes = crate::claude::notes_path().ok().and_then(|p| std::fs::read_to_string(p).ok()).unwrap_or_default();
    let notes = match notes.trim() {
        "" => String::new(),
        n => format!("\n\nThe user's notes on this repo (read-only here; follow them):\n<notes>\n{n}\n</notes>"),
    };
    let p = match is_new(r.id) {
        // What it's to do is the user's request (in `dv replace-info` too); `extra`, if any, is
        // more, for carrying on.
        true => format!(
            "Use the dv-review skill to draft a new PR doing what the user asks (its section 7: \"Draft a new PR\"). \
             First run `dv replace-info`. (It's {now} now.){notes}\n\nThe user asks:\n{}",
            r.task
        ),
        false => format!(
            "Use the dv-review skill to draft a replacement for PR {} (its section 6: \"Draft a replacement\"). \
             First run `dv replace-info`. (It's {now} now.){notes}",
            prs.join(", ")
        ),
    };
    crate::view::with_instructions(&p, extra)
}

/// The directory a draft may write to (its record): `--add-dir`.
pub fn writable() -> Result<PathBuf> {
    dir()
}

/// Draft replacement `prs` with Claude, in its worktree, in the foreground of this thread:
/// what it took ("12 turns, $0.40").
pub fn draft(prs: &[u64], root: &Path, extra: &str, prepared: impl FnOnce()) -> Result<String> {
    let (mut r, dir) = match prs {
        [id] if is_new(*id) => prepare_new(*id, root)?,
        _ => prepare(prs, root)?,
    };
    // Others (dv list, another dv) see it's under way, and don't start another.
    r.drafting = Some(std::process::id());
    save(&r)?;
    let res = run_claude(&r, &dir, extra, prepared);
    // Claude has written to the record meanwhile: only the mark comes off.
    if let Ok(mut now) = load(r.id) {
        now.drafting = None;
        _ = save(&now);
    }
    res
}

fn run_claude(r: &Record, dir: &Path, extra: &str, prepared: impl FnOnce()) -> Result<String> {
    prepared();
    // A record that exists from before has a session that saw it: carry on there.
    let fresh = r.title.is_empty() && r.body.is_empty();
    let session = match fresh {
        true => ["--session-id".to_owned(), r.session.clone()],
        false => ["--resume".to_owned(), r.session.clone()],
    };
    let log = std::env::temp_dir().join(format!("dv-claude-replace-{}-{}.log", std::process::id(), r.id));
    let st = crate::view::claude_command(&prompt(r, extra), dir, &session, crate::view::REPLACE_TOOLS, Some(&writable()?))
        .stdin(std::process::Stdio::null())
        .stdout(std::fs::File::create(&log)?)
        .stderr(std::fs::File::create(log.with_extension("err"))?)
        .status()
        .context("is Claude Code (`claude`) installed?")?;
    let v: Value = std::fs::read_to_string(&log).ok().and_then(|t| serde_json::from_str(&t).ok()).unwrap_or_default();
    if !st.success() || v["is_error"] != false {
        anyhow::bail!("Claude stopped ({st}): see {}", log.display());
    }
    Ok(format!("{} turns, ${:.2}", v["num_turns"].as_u64().unwrap_or(0), v["total_cost_usd"].as_f64().unwrap_or(0.0)))
}

/// The remote replacement branches are pushed to (`git config dv.replaceRemote`): not origin,
/// but yours (a fork). `None` until it's set.
pub fn remote() -> Option<String> {
    crate::diff::git(&["config", "--get", "dv.replaceRemote"]).ok().map(|r| r.trim().to_owned()).filter(|r| !r.is_empty())
}

pub fn set_remote(name: &str) -> Result<()> {
    crate::diff::git(&["config", "dv.replaceRemote", name]).map(drop)
}

/// The remotes it could be (all but origin).
pub fn remote_choices() -> Vec<(String, String)> {
    let names = crate::diff::git(&["remote"]).unwrap_or_default();
    names
        .lines()
        .filter(|n| *n != crate::diff::remote())
        .map(|n| (n.to_owned(), crate::diff::git(&["remote", "get-url", n]).unwrap_or_default().trim().to_owned()))
        .collect()
}

/// The GitHub owner of a remote's repo, from its URL (`git@github.com:owner/repo.git`,
/// `https://github.com/owner/repo`).
fn owner(url: &str) -> Option<&str> {
    let path = url.split_once("github.com")?.1.trim_start_matches([':', '/']);
    path.split('/').next().filter(|o| !o.is_empty())
}

/// Publish replacement `r` (checked out in `dir`) as a draft PR (ready for review once CI
/// passes), with `title` and `body` (what
/// you made of Claude's drafts): rebase it onto its base as it is on origin now, push it to
/// your replacements remote, then open the PR (from there, into origin's repo). Its number.
pub fn publish(r: &mut Record, dir: &Path, title: &str, body: &str) -> Result<u64> {
    anyhow::ensure!(r.published.is_none(), "already published, as #{}", r.published.unwrap_or(0));
    let remote = remote().context("no remote for replacement branches (dv.replaceRemote)")?;
    rebase(r, dir)?;
    let b = branch(r.id);
    let url = crate::diff::git(&["remote", "get-url", &remote])?;
    let owner = owner(url.trim()).with_context(|| format!("{remote} ({}) isn't a GitHub repo", url.trim()))?;
    crate::diff::git(&["-C", &dir.to_string_lossy(), "push", "-q", "-f", "-u", &remote, &format!("{b}:refs/heads/{b}")])?;
    let n = crate::gh::create_pr(&r.base, &format!("{owner}:{b}"), title, body)?;
    (r.published, r.title, r.body) = (Some(n), title.to_owned(), body.to_owned());
    save(r)?;
    Ok(n)
}

/// The closing note, with the replacement's PR where Claude left `{replacement}` (or, if it
/// didn't, said at the end).
pub fn note(r: &Record) -> String {
    let link = r.published.map_or("the replacement".to_owned(), |n| format!("#{n}"));
    match r.closing_note.contains("{replacement}") {
        true => r.closing_note.replace("{replacement}", &link),
        false => format!("{}\n\nSuperseded by {link}.", r.closing_note.trim_end()).trim_start().to_owned(),
    }
}

/// Close the originals not closed yet, each with `note`: what was closed (an error stops at
/// the PR it's about, keeping those before).
pub fn close_originals(r: &mut Record, note: &str) -> Result<Vec<u64>> {
    let mut done = vec![];
    for n in r.replaces.iter().map(|o| o.number).filter(|n| !r.closed.contains(n)).collect::<Vec<_>>() {
        let res = crate::gh::close(n, Some(note)).with_context(|| format!("closing #{n}"));
        if res.is_ok() {
            r.closed.push(n);
            done.push(n);
        }
        save(r)?;
        res?;
    }
    Ok(done)
}

/// Remove replacement `id`: its worktree (not with local changes) and its record. Its branch
/// stays, in case there's something on it you want.
pub fn remove(id: u64, root: &Path) -> Result<String> {
    let dir = crate::worktree_dir_named(root, &name(id))?;
    if dir.exists() {
        crate::ensure_worktree_of(&dir, root)?;
        crate::diff::git(&["-C", &root.to_string_lossy(), "worktree", "remove", &dir.to_string_lossy()]).map_err(|e| {
            match format!("{e:#}").contains("modified or untracked") {
                true => e.context(format!("{} has local changes: commit or discard them first", dir.display())),
                false => e,
            }
        })?;
    }
    _ = std::fs::remove_file(path(id)?);
    let b = branch(id);
    Ok(format!("removed {}; kept branch {b} (`git branch -D {b}` deletes it)", label(id)))
}

/// `dv replace-info`: what Claude needs to draft the replacement checked out here.
pub fn info() -> Result<()> {
    let id = current().context("not on a replacement's branch (dv/replace-N) or a new PR's (dv/new-K)")?;
    let r = load(id)?;
    let base = crate::diff::merge_base(Some(&crate::diff::remote_branch(&r.base)))?;
    match is_new(id) {
        true => {
            println!("record: {}    (write title and body here)", path(id)?.display());
            println!("task: {}", r.task.replace('\n', "\n      "));
        }
        false => println!("record: {}    (write title, body and closing_note here)", path(id)?.display()),
    }
    for o in &r.replaces {
        println!("replaces: #{} {} (@{}) {}", o.number, o.title, o.author, o.url);
    }
    println!("branch: {}    (commit your work here; never push)", branch(id));
    println!("base: {base}    ({}: `git diff {base}` is what dv shows)", crate::diff::remote_branch(&r.base));
    println!("notes: the user's notes on this repo are in your prompt (read-only for a replacement)");
    Ok(())
}

#[test]
fn records_round_trip() {
    let r = Record {
        id: 12,
        replaces: vec![Original { number: 12, title: "fix it".into(), author: "bot".into(), url: "u".into() }, Original { number: 15, ..Original::default() }],
        base: "master".into(),
        title: "Fix it properly".into(),
        body: "Replaces #12 and #15.\n\nFixes #9.".into(),
        closing_note: "Thanks! Handled in the replacement.".into(),
        session: "s".into(),
        published: Some(20),
        closed: vec![12],
        drafting: Some(7),
        task: String::new(),
    };
    assert_eq!(parse(12, &to_json(&r)).unwrap(), r);
    // As Claude may leave it: fields missing are empty.
    let bare = parse(3, r#"{ "replaces": [{ "number": 3 }], "base": "main" }"#).unwrap();
    assert_eq!((bare.replaces[0].number, bare.title.as_str(), bare.base.as_str()), (3, "", "main"));
    assert!(prompt(&r, "keep the old API").contains("PR #12, #15") && prompt(&r, "keep the old API").ends_with("keep the old API"));
    let r = Record { closing_note: "Thanks! See {replacement}.".into(), ..r };
    assert_eq!(note(&r), "Thanks! See #20.");
    assert_eq!(note(&Record { published: None, ..r.clone() }), "Thanks! See the replacement.");
    // Without the placeholder, the link goes at the end.
    assert_eq!(note(&Record { closing_note: "Thanks!".into(), ..r.clone() }), "Thanks!\n\nSuperseded by #20.");
    assert_eq!(owner("git@github.com:evan/diffview.git"), Some("evan"));
    assert_eq!(owner("https://github.com/evan/diffview"), Some("evan"));
    assert_eq!(owner("/srv/git/x.git"), None);
    // A draft from a process that's gone isn't under way.
    assert!(Record { drafting: Some(std::process::id()), ..r.clone() }.drafting_now());
    assert!(!Record { drafting: None, ..r }.drafting_now());
}

#[test]
fn new_prs() {
    let id = NEW + 3;
    assert!(is_new(id) && !is_new(2390));
    assert_eq!((name(id), branch(id), shown(id), label(id)), ("new-3".into(), "dv/new-3".into(), "new 3".into(), "new PR 3".into()));
    assert_eq!((name(12), branch(12), shown(12)), ("replace-12".into(), "dv/replace-12".into(), "#12".into()));
    // Its record: no PRs it replaces, its task kept; the prompt asks for a new PR doing it.
    let r = parse(id, r#"{ "base": "master", "task": "Fix #9: the panic on empty input" }"#).unwrap();
    assert!(r.replaces.is_empty() && r.task.starts_with("Fix #9"));
    assert_eq!(parse(id, &to_json(&r)).unwrap(), r);
    let p = prompt(&r, "");
    assert!(p.contains("draft a new PR") && p.ends_with("Fix #9: the panic on empty input"), "{p}");
}