pointbreak 0.6.0

Durable terminal code review for changes humans and coding agents collaborate on together
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
mod support;

use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::process::Command;

use serde_json::Value;
use support::git_repo::GitRepo;
use support::{shore, shore_env};

/// A repo with a committed base and an uncommitted change, so `shore capture`
/// has a HEAD -> working-tree diff to capture.
fn modified_repo() -> GitRepo {
    let repo = GitRepo::new();
    repo.write("README.md", "base\n");
    repo.commit_all("base");
    repo.write("README.md", "changed\n");
    repo
}

fn parse_json(stdout: &[u8]) -> Value {
    serde_json::from_slice(stdout).expect("stdout is json")
}

fn capture(repo: &Path) -> Value {
    let output = shore(["capture", "--repo", repo.to_str().unwrap()]);
    assert!(
        output.status.success(),
        "capture stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    parse_json(&output.stdout)
}

/// Read back the single `artifact_removed` event JSON the CLI wrote to the
/// shared common-dir store (an integration test cannot reach the `pub(crate)`
/// `EventStore`, so it reads the event files directly).
fn artifact_removed_event(repo: &Path) -> Value {
    let events_dir = support::common_dir_store(repo).join("events");
    for entry in fs::read_dir(&events_dir).expect("events dir exists") {
        let path = entry.unwrap().path();
        if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
            continue;
        }
        let value: Value = parse_json(&fs::read(&path).unwrap());
        if value["eventType"] == "t:16" {
            return value;
        }
    }
    panic!("no artifact_removed event written under {events_dir:?}");
}

#[test]
fn store_remove_by_snapshot_emits_removed_document() {
    let repo = modified_repo();
    let captured = capture(repo.path());
    let snapshot_id = captured["revision"]["objectId"].as_str().unwrap();
    let content_hash = captured["revision"]["objectArtifactContentHash"]
        .as_str()
        .unwrap();

    let output = shore([
        "store",
        "remove",
        "--repo",
        repo.path().to_str().unwrap(),
        "--snapshot",
        snapshot_id,
    ]);

    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.starts_with("{\"schema\":\"pointbreak.store-remove\""));
    let json = parse_json(stdout.as_bytes());
    assert_eq!(json["schema"], "pointbreak.store-remove");
    assert_eq!(json["version"], 1);
    let removed = json["removed"].as_array().unwrap();
    assert_eq!(removed.len(), 1);
    assert_eq!(removed[0]["contentHash"], content_hash);
    assert_eq!(removed[0]["created"], true);
    assert_eq!(json["eventsCreated"], 1);

    // Path-free contract, matching `store status`.
    assert!(!stdout.contains(".shore"));
    assert!(!stdout.contains(".git"));
    assert!(!stdout.contains("state.json"));
    assert!(!stdout.contains("artifacts/"));
}

#[test]
fn store_remove_by_revision_reports_co_referencing_units() {
    // Two worktrees capturing the SAME working-tree change share one snapshot
    // content hash under distinct review unit ids: the working-tree target carries
    // each worktree's own root, so the two captures stay distinct even though their
    // snapshot bytes are identical (the cross-worktree coexistence case).
    // Both worktrees write through to the shared common-dir store by default.
    let main = GitRepo::new();
    main.write("README.md", "base\n");
    main.commit_all("base");

    let parent = tempfile::tempdir().unwrap();
    let wt1 = parent.path().join("wt1");
    let wt2 = parent.path().join("wt2");
    add_worktree(main.path(), &wt1, "wt1");
    add_worktree(main.path(), &wt2, "wt2");

    // The same uncommitted change in each worktree yields byte-identical snapshots.
    std::fs::write(wt1.join("README.md"), "change\n").unwrap();
    std::fs::write(wt2.join("README.md"), "change\n").unwrap();

    let cap1 = capture_worktree(&wt1);
    let cap2 = capture_worktree(&wt2);

    let unit1 = cap1["revision"]["id"].as_str().unwrap();
    let unit2 = cap2["revision"]["id"].as_str().unwrap().to_owned();
    let hash1 = cap1["revision"]["objectArtifactContentHash"]
        .as_str()
        .unwrap();
    let hash2 = cap2["revision"]["objectArtifactContentHash"]
        .as_str()
        .unwrap();
    assert_eq!(
        hash1, hash2,
        "the same change yields one shared content hash"
    );

    let output = shore([
        "store",
        "remove",
        "--repo",
        wt1.to_str().unwrap(),
        "--revision",
        unit1,
    ]);
    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json = parse_json(&output.stdout);
    let entry = json["removed"]
        .as_array()
        .unwrap()
        .iter()
        .find(|blob| blob["contentHash"] == hash1)
        .expect("the shared snapshot hash is removed");
    let co_referencing: Vec<&str> = entry["coReferencingUnits"]
        .as_array()
        .unwrap()
        .iter()
        .map(|id| id.as_str().unwrap())
        .collect();
    assert!(
        co_referencing.contains(&unit2.as_str()),
        "the sibling unit still names the shared blob: {co_referencing:?}"
    );
}

#[test]
fn store_remove_has_no_idempotency_key_flag() {
    let repo = modified_repo();
    let captured = capture(repo.path());
    let snapshot_id = captured["revision"]["objectId"].as_str().unwrap();

    let output = shore([
        "store",
        "remove",
        "--repo",
        repo.path().to_str().unwrap(),
        "--snapshot",
        snapshot_id,
        "--idempotency-key",
        "x",
    ]);

    assert!(
        !output.status.success(),
        "the removal key is non-overridable; an --idempotency-key flag must not exist"
    );
}

#[test]
fn store_remove_is_idempotent() {
    let repo = modified_repo();
    let captured = capture(repo.path());
    let snapshot_id = captured["revision"]["objectId"].as_str().unwrap();

    let first = shore([
        "store",
        "remove",
        "--repo",
        repo.path().to_str().unwrap(),
        "--snapshot",
        snapshot_id,
    ]);
    assert!(first.status.success());

    let second = shore([
        "store",
        "remove",
        "--repo",
        repo.path().to_str().unwrap(),
        "--snapshot",
        snapshot_id,
    ]);
    assert!(second.status.success());
    let json = parse_json(&second.stdout);
    assert_eq!(json["eventsCreated"], 0);
    assert_eq!(json["eventsExisting"], 1);
    assert_eq!(json["removed"][0]["created"], false);
}

#[test]
fn store_compact_deletes_removed_blob_and_emits_document() {
    let repo = modified_repo();
    let captured = capture(repo.path());
    let snapshot_id = captured["revision"]["objectId"].as_str().unwrap();
    let content_hash = captured["revision"]["objectArtifactContentHash"]
        .as_str()
        .unwrap();

    shore([
        "store",
        "remove",
        "--repo",
        repo.path().to_str().unwrap(),
        "--snapshot",
        snapshot_id,
    ]);
    // Erasure is consent-gated: `--yes` performs the delete (a bare compact
    // previews and refuses).
    let output = shore([
        "store",
        "compact",
        "--repo",
        repo.path().to_str().unwrap(),
        "--yes",
    ]);

    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.starts_with("{\"schema\":\"pointbreak.store-compact\""));
    let json = parse_json(stdout.as_bytes());
    assert!(
        json["swept"]
            .as_array()
            .unwrap()
            .iter()
            .any(|blob| blob["contentHash"] == content_hash && blob["outcome"] == "removed")
    );
    assert!(json["bytesReclaimed"].as_u64().unwrap() > 0);

    // The snapshot blob is physically gone.
    let snapshots = repo.path().join(".shore/data/artifacts/objects");
    let remaining = fs::read_dir(&snapshots)
        .map(|entries| entries.count())
        .unwrap_or(0);
    assert_eq!(remaining, 0, "the removed blob was physically deleted");
}

#[test]
fn store_gc_is_alias_of_compact() {
    let repo = modified_repo();
    let captured = capture(repo.path());
    let snapshot_id = captured["revision"]["objectId"].as_str().unwrap();

    shore([
        "store",
        "remove",
        "--repo",
        repo.path().to_str().unwrap(),
        "--snapshot",
        snapshot_id,
    ]);

    // `gc` is an alias of `compact` and inherits the consent gate.
    let gc = shore([
        "store",
        "gc",
        "--repo",
        repo.path().to_str().unwrap(),
        "--yes",
    ]);
    assert!(
        gc.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&gc.stderr)
    );
    let stdout = String::from_utf8(gc.stdout).unwrap();
    assert!(stdout.starts_with("{\"schema\":\"pointbreak.store-compact\""));

    // A second sweep finds the blob already gone (idempotent).
    let second = shore([
        "store",
        "gc",
        "--repo",
        repo.path().to_str().unwrap(),
        "--yes",
    ]);
    let json = parse_json(&second.stdout);
    assert!(
        json["swept"]
            .as_array()
            .unwrap()
            .iter()
            .all(|blob| blob["outcome"] == "missing")
    );
}

#[test]
fn store_remove_unknown_snapshot_errors() {
    let repo = modified_repo();
    capture(repo.path());

    let output = shore([
        "store",
        "remove",
        "--repo",
        repo.path().to_str().unwrap(),
        "--snapshot",
        &format!("obj:sha256:{}", "0".repeat(64)),
    ]);

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("unknown snapshot"),
        "stderr should name the unknown snapshot:\n{stderr}"
    );
}

#[test]
fn store_remove_signs_the_event_when_a_sign_key_is_given() {
    let home = tempfile::tempdir().unwrap();
    let env_home = home.path().to_str().unwrap();
    let init = shore_env(
        ["key", "init", "--name", "mykey"],
        &[("SHORE_HOME", env_home)],
    );
    assert!(init.status.success());

    let repo = modified_repo();
    let captured = parse_json(
        &shore_env(
            [
                "capture",
                "--repo",
                repo.path().to_str().unwrap(),
                "--sign-key",
                "mykey",
            ],
            &[("SHORE_HOME", env_home)],
        )
        .stdout,
    );
    let snapshot_id = captured["revision"]["objectId"].as_str().unwrap();

    let output = shore_env(
        [
            "store",
            "remove",
            "--repo",
            repo.path().to_str().unwrap(),
            "--snapshot",
            snapshot_id,
            "--sign-key",
            "mykey",
        ],
        &[("SHORE_HOME", env_home)],
    );
    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );

    // The emitted removal event is signed — removal never writes an unsigned
    // event into a signed store.
    let event = artifact_removed_event(repo.path());
    assert!(
        event.get("signature").is_some(),
        "artifact_removed event must be signed: {event}"
    );
    assert!(event.get("signer").is_some());
}

fn capture_worktree(repo: &Path) -> Value {
    let output = shore(["capture", "--repo", repo.to_str().unwrap()]);
    assert!(
        output.status.success(),
        "working-tree capture stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    parse_json(&output.stdout)
}

fn add_worktree(repo: &Path, path: &Path, branch: &str) {
    let output = Command::new("git")
        .args([
            OsString::from("worktree"),
            OsString::from("add"),
            OsString::from("-b"),
            OsString::from(branch),
            path.as_os_str().to_owned(),
        ])
        .current_dir(repo)
        .output()
        .unwrap_or_else(|error| panic!("run git worktree add in {}: {error}", repo.display()));
    assert!(
        output.status.success(),
        "git worktree add failed:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn store_remove_selectors_resolve_short_ids() {
    // --snapshot: a prefixed short obj id resolves to the full artifact claim.
    let repo = modified_repo();
    let captured = capture(repo.path());
    let object_id = captured["revision"]["objectId"].as_str().unwrap();
    let content_hash = captured["revision"]["objectArtifactContentHash"]
        .as_str()
        .unwrap();
    let digest = object_id.rsplit_once("sha256:").unwrap().1;
    let short_snapshot = format!("obj:{}", &digest[..8]);

    let output = shore([
        "store",
        "remove",
        "--repo",
        repo.path().to_str().unwrap(),
        "--snapshot",
        &short_snapshot,
    ]);
    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json = parse_json(&output.stdout);
    assert_eq!(json["removed"][0]["contentHash"], content_hash);

    // --revision: a bare fragment resolves (the flag implies exactly one kind).
    let rev_repo = modified_repo();
    let rev_captured = capture(rev_repo.path());
    let revision_id = rev_captured["revision"]["id"].as_str().unwrap();
    let rev_digest = revision_id.rsplit_once("sha256:").unwrap().1;

    let rev_output = shore([
        "store",
        "remove",
        "--repo",
        rev_repo.path().to_str().unwrap(),
        "--revision",
        &rev_digest[..8],
    ]);
    assert!(
        rev_output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&rev_output.stderr)
    );
    assert_eq!(parse_json(&rev_output.stdout)["eventsCreated"], 1);
}