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
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
// `add` is crate::session-visible so the store migrator can call the content-id
// builder (`build_observation_id`) and its `ObservationIdMaterial` directly.
pub(in crate::session) mod add;
mod list;
mod target;
mod util;
mod view;

pub use self::add::{ObservationAddOptions, ObservationAddResult, record_observation};
pub use self::list::{ObservationListOptions, ObservationListResult, list_observations};
pub use self::target::ObservationTargetSelector;
pub(crate) use self::target::{
    CurrentRevisionContext, ResolvedRevision, RevisionScope, RevisionSelection,
    resolve_observation_target, resolve_revision, revision_ids_in_worktree,
};
pub(crate) use self::util::{required_title, staged_body, validated_track_id};
#[cfg(test)]
use self::view::sort_observation_views;
pub(crate) use self::view::{
    ObservationProjectionOptions, project_observations, target_matches_file,
};
pub use self::view::{ObservationStatus, ObservationView};

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

    use super::*;
    use crate::model::{
        EngagementId, EventId, JournalId, ObjectId, ObservationId, ReviewEndpoint, ReviewTargetRef,
        RevisionId, RevisionSource, Side, TrackId, WorktreeCaptureMode,
    };
    use crate::session::event::{
        EventTarget, EventType, GitProvenance, Revision, ShoreEvent, WorkObjectProposal,
        WorkObjectProposedPayload, Writer,
    };
    use crate::session::store::content::ContentArtifacts;
    use crate::session::{
        CaptureOptions, CaptureResult, EventStore, SessionState, capture_worktree_review,
    };

    #[test]
    fn track_policy_accepts_lowercase_local_and_namespaced_ids() {
        assert_eq!(validated_track_id("codex").unwrap().as_str(), "codex");
        assert_eq!(
            validated_track_id("agent:codex").unwrap().as_str(),
            "agent:codex"
        );
        assert_eq!(
            validated_track_id("human:kevin").unwrap().as_str(),
            "human:kevin"
        );
    }

    #[test]
    fn track_policy_rejects_reserved_or_unsafe_ids() {
        for bad in [
            "",
            "All",
            "all",
            "*",
            "none",
            "null",
            "default",
            "agent/codex",
            "agent codex",
            "system:shore",
            "import:hunk",
        ] {
            assert!(validated_track_id(bad).is_err(), "{bad} should be rejected");
        }
    }

    #[test]
    fn track_policy_rejects_overlong_ids() {
        let too_long = "a".repeat(129);

        assert!(validated_track_id(&too_long).is_err());
    }

    #[test]
    fn resolves_single_current_revision_when_not_explicit() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let event_store = EventStore::open(resolved_store_dir(repo.path()));
        let events = event_store.list_events().unwrap();

        let context = CurrentRevisionContext::for_repo(repo.path()).unwrap();
        let resolved = resolve_revision(
            &events,
            RevisionSelection::Current,
            &context,
            RevisionScope::CurrentWorktree,
        )
        .unwrap();

        assert_eq!(resolved.revision_id, capture.revision_id);
        assert_eq!(resolved.object_id, capture.object_id);
    }

    #[test]
    fn resolving_current_revision_errors_when_none_captured() {
        let events = Vec::new();

        let error = resolve_revision(
            &events,
            RevisionSelection::Current,
            &any_context(),
            RevisionScope::All,
        )
        .unwrap_err();

        assert!(error.to_string().contains("no captured revision"));
    }

    #[test]
    fn resolving_current_revision_errors_when_ambiguous() {
        let events = vec![
            revision_captured_event_with_ids("rev:one", "snap:one"),
            revision_captured_event_with_ids("rev:two", "snap:two"),
        ];

        let error = resolve_revision(
            &events,
            RevisionSelection::Current,
            &any_context(),
            RevisionScope::All,
        )
        .unwrap_err();

        assert!(error.to_string().contains("multiple captured revisions"));
    }

    #[test]
    fn explicit_unknown_revision_is_rejected() {
        let events = vec![revision_captured_event_with_ids("rev:one", "snap:one")];

        let error = resolve_revision(
            &events,
            RevisionSelection::Exact(&RevisionId::new("review-unit:sha256:missing")),
            &any_context(),
            RevisionScope::All,
        )
        .unwrap_err();

        assert!(error.to_string().contains("unknown revision"));
    }

    #[test]
    fn head_seed_that_is_a_current_head_resolves_exactly() {
        // A <- B (B supersedes A): B is the head; seeding B resolves B.
        let events = vec![revision_event("a", &[]), revision_event("b", &["a"])];

        let resolved = resolve_revision(
            &events,
            RevisionSelection::Head(&rev("b")),
            &any_context(),
            RevisionScope::All,
        )
        .unwrap();

        assert_eq!(resolved.revision_id, rev("b"));
    }

    #[test]
    fn head_seed_on_a_superseded_revision_resolves_its_thread_head() {
        // Seeding the superseded A resolves to its thread's current head, B.
        let events = vec![revision_event("a", &[]), revision_event("b", &["a"])];

        let resolved = resolve_revision(
            &events,
            RevisionSelection::Head(&rev("a")),
            &any_context(),
            RevisionScope::All,
        )
        .unwrap();

        assert_eq!(resolved.revision_id, rev("b"));
    }

    #[test]
    fn head_seed_under_a_fork_force_disambiguates_thread_scoped() {
        // A <- {B, C} (competing heads) plus an unrelated thread Z. A non-head seed
        // lists the thread's competing heads (B, C), never the unrelated head Z.
        let events = vec![
            revision_event("a", &[]),
            revision_event("b", &["a"]),
            revision_event("c", &["a"]),
            revision_event("z", &[]),
        ];

        let error = resolve_revision(
            &events,
            RevisionSelection::Head(&rev("a")),
            &any_context(),
            RevisionScope::All,
        )
        .unwrap_err();
        let message = error.to_string();
        assert!(message.contains("rev:sha256:b"));
        assert!(message.contains("rev:sha256:c"));
        assert!(!message.contains("rev:sha256:z"));

        // A head seed escapes the fork by resolving to itself exactly.
        let resolved = resolve_revision(
            &events,
            RevisionSelection::Head(&rev("b")),
            &any_context(),
            RevisionScope::All,
        )
        .unwrap();
        assert_eq!(resolved.revision_id, rev("b"));
    }

    #[test]
    fn target_selector_builds_review_wide_file_and_range_refs() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let resolved = resolved_from_capture(&capture);

        let review_wide = resolve_observation_target(
            repo.path(),
            &resolved,
            &ObservationTargetSelector::revision(),
        )
        .unwrap();
        let file = resolve_observation_target(
            repo.path(),
            &resolved,
            &ObservationTargetSelector::file("src/lib.rs"),
        )
        .unwrap();
        let range = resolve_observation_target(
            repo.path(),
            &resolved,
            &ObservationTargetSelector::range("src/lib.rs", Side::New, 2, Some(3)),
        )
        .unwrap();

        assert!(matches!(review_wide, ReviewTargetRef::Revision { .. }));
        assert!(matches!(file, ReviewTargetRef::File { .. }));
        assert!(matches!(
            range,
            ReviewTargetRef::Range {
                start_line: 2,
                end_line: 3,
                ..
            }
        ));
    }

    #[test]
    fn target_selector_rejects_file_not_in_captured_snapshot() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let resolved = resolved_from_capture(&capture);

        let error = resolve_observation_target(
            repo.path(),
            &resolved,
            &ObservationTargetSelector::file("missing.rs"),
        )
        .unwrap_err();

        assert!(
            error
                .to_string()
                .contains("not present in captured snapshot")
        );
    }

    #[test]
    fn target_selector_rejects_invalid_range_shape() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let resolved = resolved_from_capture(&capture);

        let zero = resolve_observation_target(
            repo.path(),
            &resolved,
            &ObservationTargetSelector::range("src/lib.rs", Side::New, 0, Some(1)),
        )
        .unwrap_err();
        let reversed = resolve_observation_target(
            repo.path(),
            &resolved,
            &ObservationTargetSelector::range("src/lib.rs", Side::New, 3, Some(2)),
        )
        .unwrap_err();

        assert!(zero.to_string().contains("start line"));
        assert!(reversed.to_string().contains("end line"));
    }

    #[test]
    fn record_observation_writes_event_and_updates_state() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();

        let result = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Check return value")
                .with_target(ObservationTargetSelector::file("src/lib.rs")),
        )
        .unwrap();

        assert_eq!(result.revision_id, capture.revision_id);
        assert!(result.observation_id.as_str().starts_with("obs:sha256:"));
        assert_eq!(result.track_id.as_str(), "agent:codex");
        assert_eq!(result.events_created, 1);
        assert_eq!(result.events_existing, 0);
        assert_eq!(
            result.events_created_by_type["review_observation_recorded"],
            1
        );
        assert!(result.body_content_hash.is_none());

        let events = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        let state = SessionState::from_events(&events).unwrap();
        assert_eq!(state.observation_count, 1);
    }

    #[test]
    fn record_observation_with_actor_id_attributes_override_and_changes_derived_id() {
        use crate::model::ActorId;

        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();

        let with_a = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Check return value")
                .with_actor_id(ActorId::new("actor:agent:obs-a")),
        )
        .unwrap();
        let with_b = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Check return value")
                .with_actor_id(ActorId::new("actor:agent:obs-b")),
        )
        .unwrap();

        // The override flows into the content-addressed observation id.
        assert_ne!(with_a.observation_id, with_b.observation_id);

        let events = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        let actor_for = |id: &crate::model::ObservationId| {
            events
                .iter()
                .filter(|event| event.event_type == EventType::ReviewObservationRecorded)
                .find(|event| event.payload["observationId"] == serde_json::json!(id.as_str()))
                .map(|event| event.writer.actor_id.as_str().to_owned())
                .unwrap()
        };
        assert_eq!(actor_for(&with_a.observation_id), "actor:agent:obs-a");
        assert_eq!(actor_for(&with_b.observation_id), "actor:agent:obs-b");
    }

    #[test]
    fn record_observation_without_actor_id_uses_git_identity() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Check return value"),
        )
        .unwrap();

        let events = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        let observation = events
            .iter()
            .find(|event| event.event_type == EventType::ReviewObservationRecorded)
            .unwrap();
        assert_eq!(
            observation.writer.actor_id.as_str(),
            "actor:git-email:shore-tests@example.com"
        );
    }

    #[test]
    fn record_observation_is_idempotent_for_same_logical_input() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let options = ObservationAddOptions::new(repo.path())
            .with_track("agent:codex")
            .with_title("Same finding")
            .with_body("same body")
            .with_target(ObservationTargetSelector::revision());

        let first = record_observation(options.clone()).unwrap();
        let second = record_observation(options).unwrap();

        assert_eq!(first.observation_id, second.observation_id);
        assert_eq!(first.events_created, 1);
        assert_eq!(second.events_created, 0);
        assert_eq!(second.events_existing, 1);
    }

    #[test]
    fn record_observation_state_json_equals_full_replay_after_created_and_existing_paths() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();

        let options = ObservationAddOptions::new(repo.path())
            .with_track("agent:codex")
            .with_title("equal-after-write")
            .with_body("same body");

        let first = record_observation(options.clone()).unwrap();
        assert_eq!(first.events_created, 1);
        assert_eq!(first.events_existing, 0);
        let on_disk: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(resolved_store_dir(repo.path()).join("state.json")).unwrap(),
        )
        .unwrap();
        let events = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        let replay = serde_json::to_value(SessionState::from_events(&events).unwrap()).unwrap();
        assert_eq!(on_disk, replay, "Created path drifted from full replay");

        let second = record_observation(options).unwrap();
        assert_eq!(second.events_created, 0);
        assert_eq!(second.events_existing, 1);
        let on_disk: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(resolved_store_dir(repo.path()).join("state.json")).unwrap(),
        )
        .unwrap();
        let events = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        let replay = serde_json::to_value(SessionState::from_events(&events).unwrap()).unwrap();
        assert_eq!(on_disk, replay, "Existing path drifted from full replay");
    }

    #[test]
    fn explicit_same_idempotency_key_with_different_payload_conflicts() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();

        record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("First")
                .with_idempotency_key("retry-key"),
        )
        .unwrap();
        let error = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Second")
                .with_idempotency_key("retry-key"),
        )
        .unwrap_err();

        assert!(error.to_string().contains("event conflict"));
    }

    #[test]
    fn large_observation_body_is_stored_as_internal_body_artifact() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let body = "x".repeat(crate::session::body_artifact::BODY_INLINE_LIMIT + 1);

        let result = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Large body")
                .with_body(body),
        )
        .unwrap();

        assert!(
            result
                .body_content_hash
                .as_deref()
                .unwrap()
                .starts_with("sha256:")
        );
        assert!(
            !format!("{result:?}").contains("artifacts/notes/"),
            "workflow result must not expose internal artifact paths"
        );

        let artifacts = ContentArtifacts::local(&resolved_store_dir(repo.path()))
            .list_refs("artifacts/notes")
            .unwrap();
        assert_eq!(artifacts.len(), 1);
    }

    #[test]
    fn correction_records_new_observation_with_supersedes_link() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();

        let original = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Original"),
        )
        .unwrap();
        let correction = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Correction")
                .superseding(original.observation_id.clone()),
        )
        .unwrap();

        assert_ne!(original.observation_id, correction.observation_id);

        let events = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        let correction_event = events
            .iter()
            .find(|event| event.event_id == correction.event_id)
            .unwrap();
        assert_eq!(
            correction_event.payload["supersedesObservationIds"][0],
            original.observation_id.as_str()
        );
    }

    #[test]
    fn observation_records_responds_to_link_in_payload() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();

        let base = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("A"),
        )
        .unwrap();
        let ack = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("noted — tracking as issue #N")
                .responding_to(base.observation_id.clone()),
        )
        .unwrap();

        // The payload records the responds-to link. (These ids differ only because the titles
        // differ; that responds_to itself changes the id is a separate concern, exercised elsewhere.)
        let events = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        let ack_event = events
            .iter()
            .find(|event| event.event_id == ack.event_id)
            .unwrap();
        assert_eq!(
            ack_event.payload["respondsToObservationIds"][0],
            base.observation_id.as_str()
        );
    }

    #[test]
    fn responds_to_yields_a_distinct_observation_id() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let plain = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("x"),
        )
        .unwrap();
        let acking = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("x")
                .responding_to(ObservationId::new("obs:sha256:deadbeef")),
        )
        .unwrap();
        assert_ne!(plain.observation_id, acking.observation_id);
    }

    #[test]
    fn reordered_responds_to_links_dedupe_instead_of_conflicting() {
        // A set-equal but reordered fact-pointer re-write must converge to the
        // stored event, not hard-conflict on payload_hash. The content id already
        // folds a sorted set, so the two writes share an idempotency key;
        // normalizing the *stored* payload the same way lets the retry classify as
        // Existing rather than raising an "event conflict".
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let a = ObservationId::new("obs:sha256:aaa");
        let b = ObservationId::new("obs:sha256:bbb");

        let first = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("ack")
                .responding_to(a.clone())
                .responding_to(b.clone()),
        )
        .unwrap();
        assert_eq!(first.events_created, 1);

        let reordered = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("ack")
                .responding_to(b)
                .responding_to(a),
        )
        .unwrap();

        assert_eq!(reordered.observation_id, first.observation_id);
        assert_eq!(reordered.events_created, 0);
        assert_eq!(reordered.events_existing, 1);
    }

    #[test]
    fn duplicate_responds_to_links_dedupe_to_one_stored_link() {
        // A duplicate-bearing fact-pointer re-write must dedupe, proving the stored
        // payload is `sorted_unique`-normalized (sort *and* dedup), not merely
        // sorted: a bare sort would keep `[a, a, b]` distinct from `[a, b]` and
        // spawn a second observation.
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let a = ObservationId::new("obs:sha256:aaa");
        let b = ObservationId::new("obs:sha256:bbb");

        let first = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("ack")
                .responding_to(a.clone())
                .responding_to(b.clone()),
        )
        .unwrap();

        let duplicated = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("ack")
                .responding_to(a.clone())
                .responding_to(a.clone())
                .responding_to(b.clone()),
        )
        .unwrap();

        assert_eq!(duplicated.observation_id, first.observation_id);
        assert_eq!(duplicated.events_created, 0);
        assert_eq!(duplicated.events_existing, 1);

        // The single stored payload carries the deduped set, in sorted order.
        let events = EventStore::open(resolved_store_dir(repo.path()))
            .list_events()
            .unwrap();
        let stored = events
            .iter()
            .find(|event| event.event_id == first.event_id)
            .unwrap();
        assert_eq!(
            stored.payload["respondsToObservationIds"],
            serde_json::json!([a.as_str(), b.as_str()])
        );
    }

    #[test]
    fn list_observations_returns_observations_for_current_revision() {
        let repo = modified_repo();
        let capture = capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let first = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("First"),
        )
        .unwrap();
        let second = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:claude")
                .with_title("Second"),
        )
        .unwrap();

        let result = list_observations(ObservationListOptions::new(repo.path())).unwrap();

        assert_eq!(result.revision_id, capture.revision_id);
        let mut actual_ids = result
            .observations
            .iter()
            .map(|observation| observation.id.as_str().to_owned())
            .collect::<Vec<_>>();
        actual_ids.sort();
        let mut expected_ids = vec![
            first.observation_id.as_str().to_owned(),
            second.observation_id.as_str().to_owned(),
        ];
        expected_ids.sort();
        assert_eq!(actual_ids, expected_ids);
    }

    #[test]
    fn list_observations_collapses_duplicate_semantic_events() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let first = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Same finding")
                .with_body("same body")
                .with_idempotency_key("retry-a"),
        )
        .unwrap();
        let second = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Same finding")
                .with_body("same body")
                .with_idempotency_key("retry-b"),
        )
        .unwrap();

        let result =
            list_observations(ObservationListOptions::new(repo.path()).with_include_body(true))
                .unwrap();

        assert_eq!(first.observation_id, second.observation_id);
        assert_eq!(first.events_created, 1);
        assert_eq!(second.events_created, 1);
        assert_eq!(result.observations.len(), 1);
        assert_eq!(result.observations[0].id, first.observation_id);
        assert_eq!(result.observations[0].body.as_deref(), Some("same body"));
        assert!(result.diagnostics.iter().any(|diagnostic| {
            diagnostic.code == crate::session::state::DUPLICATE_SEMANTIC_OBSERVATION_EVENT_CODE
        }));
    }

    #[test]
    fn list_observations_resolves_the_store_from_a_subdirectory() {
        // Listing from a nested path resolves the worktree root and reads the same
        // resolved (shared common-dir) store the write landed in.
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let added = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("subdir read"),
        )
        .unwrap();

        let result = list_observations(ObservationListOptions::new(repo.path().join("src")))
            .expect("observations load from subdirectory");

        assert_eq!(result.observations[0].id, added.observation_id);
    }

    #[test]
    fn list_observations_filters_by_track_and_file() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("File")
                .with_target(ObservationTargetSelector::file("src/lib.rs")),
        )
        .unwrap();
        record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:claude")
                .with_title("Review wide"),
        )
        .unwrap();

        let result = list_observations(
            ObservationListOptions::new(repo.path())
                .with_track("agent:codex")
                .with_file("src/lib.rs"),
        )
        .unwrap();

        assert_eq!(result.observations.len(), 1);
        assert_eq!(result.observations[0].track_id.as_str(), "agent:codex");
    }

    #[test]
    fn list_observations_omits_body_by_default_and_hydrates_when_requested() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Body")
                .with_body("large ".repeat(1000)),
        )
        .unwrap();

        let without_body = list_observations(ObservationListOptions::new(repo.path())).unwrap();
        let with_body =
            list_observations(ObservationListOptions::new(repo.path()).with_include_body(true))
                .unwrap();

        assert!(without_body.observations[0].body.is_none());
        assert!(
            with_body.observations[0]
                .body
                .as_deref()
                .unwrap()
                .starts_with("large ")
        );
        assert!(
            !format!("{with_body:?}").contains("artifacts/notes/"),
            "list result must not expose internal artifact paths"
        );
    }

    #[test]
    fn list_observations_marks_superseded_observations() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        let original = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Original"),
        )
        .unwrap();
        record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:codex")
                .with_title("Correction")
                .superseding(original.observation_id.clone()),
        )
        .unwrap();

        let result = list_observations(ObservationListOptions::new(repo.path())).unwrap();
        let original_view = result
            .observations
            .iter()
            .find(|observation| observation.id == original.observation_id)
            .unwrap();

        assert_eq!(original_view.status, ObservationStatus::Superseded);
    }

    #[test]
    fn responded_by_reverse_map_is_derived_cross_track_and_target_stays_active() {
        let repo = modified_repo();
        capture_worktree_review(CaptureOptions::new(repo.path())).unwrap();
        // author-track observation A
        let a = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:author")
                .with_title("A"),
        )
        .unwrap();
        // reviewer-track observation B responds to A (CROSS-TRACK)
        let b = record_observation(
            ObservationAddOptions::new(repo.path())
                .with_track("agent:reviewer")
                .with_title("B")
                .responding_to(a.observation_id.clone()),
        )
        .unwrap();

        // Project filtered to A's track, which EXCLUDES B from the returned set. If responds_to edges
        // were collected AFTER the track filter, B's edge would be dropped and A.responded_by would be
        // empty — so this filtered projection is what proves collection happens before the filter.
        let filtered =
            list_observations(ObservationListOptions::new(repo.path()).with_track("agent:author"))
                .unwrap();
        let a_view = filtered
            .observations
            .iter()
            .find(|v| v.id == a.observation_id)
            .unwrap();

        // (1) derived reverse-map names B, even though B's track was filtered out of the returned set
        assert!(a_view.responded_by.iter().any(|id| id == &b.observation_id));
        // (2) target A stays Active — no status flip
        assert_eq!(a_view.status, ObservationStatus::Active);

        // (3) B carries the forward pointer — check via an unfiltered list (B is filtered out above)
        let all = list_observations(ObservationListOptions::new(repo.path())).unwrap();
        let b_view = all
            .observations
            .iter()
            .find(|v| v.id == b.observation_id)
            .unwrap();
        assert!(b_view.responds_to.iter().any(|id| id == &a.observation_id));
    }

    #[test]
    fn list_observations_sorts_by_occurred_at_then_event_id() {
        let mut observations = vec![
            observation_view_for_sort("obs:sha256:b", "evt:sha256:b", "unix-ms:2"),
            observation_view_for_sort("obs:sha256:c", "evt:sha256:c", "unix-ms:1"),
            observation_view_for_sort("obs:sha256:a", "evt:sha256:a", "unix-ms:1"),
        ];

        sort_observation_views(&mut observations);

        assert_eq!(
            observations
                .iter()
                .map(|observation| observation.id.as_str())
                .collect::<Vec<_>>(),
            vec!["obs:sha256:a", "obs:sha256:c", "obs:sha256:b"]
        );
    }

    fn resolved_from_capture(capture: &CaptureResult) -> ResolvedRevision {
        ResolvedRevision {
            journal_id: capture.journal_id.clone(),
            revision_id: capture.revision_id.clone(),
            object_id: capture.object_id.clone(),
            object_artifact_content_hash: capture.object_artifact_content_hash.clone(),
        }
    }

    /// A context for selection tests that do not exercise worktree scoping (they
    /// widen to `RevisionScope::All` or resolve `Exact`/`LineageHead`).
    fn any_context() -> CurrentRevisionContext {
        CurrentRevisionContext {
            worktree_root: "/repo".to_owned(),
            head_ref: None,
        }
    }

    fn revision_captured_event_with_ids(revision_id: &str, object_id: &str) -> ShoreEvent {
        // The envelope subject and the payload revision address one and the same
        // revision, as a real capture stamps both from one minted id.
        let revision_id = RevisionId::new(revision_id);
        let object_id = ObjectId::new(object_id);
        ShoreEvent::new(
            EventType::WorkObjectProposed,
            format!("work_object_proposed:{}", revision_id.as_str()),
            EventTarget::for_revision(JournalId::new("journal:default"), revision_id.clone(), None)
                .unwrap(),
            Writer::shore_local("0.1.0"),
            WorkObjectProposedPayload {
                engagement_id: EngagementId::new(format!(
                    "engagement:sha256:{}",
                    crate::canonical_hash::sha256_bytes_hex(revision_id.as_str().as_bytes())
                )),
                work_object: WorkObjectProposal::Revision {
                    revision: Revision {
                        id: revision_id.clone(),
                        object_id: object_id.clone(),
                        git_provenance: Some(GitProvenance {
                            source: RevisionSource::GitWorktree {
                                mode: WorktreeCaptureMode::CombinedHeadToWorkingTree,
                                include_untracked: true,
                                pathspecs: Vec::new(),
                            },
                            base: ReviewEndpoint::GitCommit {
                                commit_oid: "abc".to_owned(),
                                tree_oid: "def".to_owned(),
                            },
                            target: ReviewEndpoint::GitWorkingTree {
                                worktree_root: "/repo".to_owned(),
                            },
                        }),
                    },
                    object_artifact_content_hash: "sha256:artifact".to_owned(),
                    supersedes: vec![],
                },
            },
            "2026-05-12T00:00:00Z",
        )
        .unwrap()
    }

    fn rev(suffix: &str) -> RevisionId {
        RevisionId::new(format!("rev:sha256:{suffix}"))
    }

    /// A review-domain generative move proposing a revision that supersedes the
    /// given predecessors (by suffix). Feeds the supersession head-selection.
    fn revision_event(suffix: &str, supersedes: &[&str]) -> ShoreEvent {
        let revision_id = rev(suffix);
        ShoreEvent::new(
            EventType::WorkObjectProposed,
            format!("work_object_proposed:{}", revision_id.as_str()),
            EventTarget::for_revision(JournalId::new("journal:default"), revision_id.clone(), None)
                .unwrap(),
            Writer::shore_local("0.1.0"),
            WorkObjectProposedPayload {
                engagement_id: EngagementId::new(format!("engagement:sha256:{suffix}")),
                work_object: WorkObjectProposal::Revision {
                    revision: Revision {
                        id: revision_id,
                        object_id: ObjectId::new(format!("obj:sha256:{suffix}")),
                        git_provenance: Some(GitProvenance {
                            source: RevisionSource::GitWorktree {
                                mode: WorktreeCaptureMode::CombinedHeadToWorkingTree,
                                include_untracked: true,
                                pathspecs: Vec::new(),
                            },
                            base: ReviewEndpoint::GitCommit {
                                commit_oid: "abc".to_owned(),
                                tree_oid: "def".to_owned(),
                            },
                            target: ReviewEndpoint::GitWorkingTree {
                                worktree_root: "/repo".to_owned(),
                            },
                        }),
                    },
                    object_artifact_content_hash: "sha256:artifact".to_owned(),
                    supersedes: supersedes.iter().map(|s| rev(s)).collect(),
                },
            },
            "2026-05-12T00:00:00Z",
        )
        .unwrap()
    }

    fn observation_view_for_sort(
        observation_id: &str,
        event_id: &str,
        created_at: &str,
    ) -> ObservationView {
        let revision_id = RevisionId::new("review-unit:sha256:one");
        ObservationView {
            id: crate::model::ObservationId::new(observation_id),
            event_id: EventId::new(event_id),
            track_id: TrackId::new("agent:codex"),
            target: ReviewTargetRef::Revision { revision_id },
            title: "sort".to_owned(),
            body: None,
            body_content_type: Default::default(),
            tags: vec![],
            confidence: None,
            status: ObservationStatus::Active,
            supersedes: vec![],
            responds_to: vec![],
            responded_by: vec![],
            body_content_hash: None,
            body_content_state: Default::default(),
            created_at: created_at.to_owned(),
            writer: Writer::shore_local("test"),
        }
    }

    fn modified_repo() -> TestRepo {
        let repo = TestRepo::new();
        repo.write("src/lib.rs", "pub fn value() -> u32 {\n    1\n}\n");
        repo.commit_all("base");
        repo.write("src/lib.rs", "pub fn value() -> u32 {\n    2\n}\n");
        repo
    }

    /// The store a workflow actually lands in for `repo` — the shared common-dir
    /// store by default. Reads that follow a workflow resolve here, not the raw
    /// worktree-local `.shore/data`.
    fn resolved_store_dir(repo: &Path) -> std::path::PathBuf {
        crate::git::git_common_dir(repo).unwrap().join("shore")
    }

    struct TestRepo {
        root: tempfile::TempDir,
    }

    impl TestRepo {
        fn new() -> Self {
            let root = tempfile::tempdir().expect("create temp git repository directory");
            let repo = Self { root };

            repo.git(["init"]);
            repo.git(["config", "user.name", "Shore Tests"]);
            repo.git(["config", "user.email", "shore-tests@example.com"]);
            repo.git(["config", "commit.gpgsign", "false"]);

            repo
        }

        fn path(&self) -> &Path {
            self.root.path()
        }

        fn write(&self, path: &str, contents: &str) {
            let path = self.path().join(path);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(path, contents).unwrap();
        }

        fn commit_all(&self, message: &str) {
            self.git(["add", "."]);
            self.git(["commit", "-m", message]);
        }

        fn git<I, S>(&self, args: I)
        where
            I: IntoIterator<Item = S>,
            S: AsRef<std::ffi::OsStr>,
        {
            let output = Command::new("git")
                .args(args)
                .current_dir(self.path())
                .output()
                .expect("run git command");
            assert!(
                output.status.success(),
                "git failed\nstdout:\n{}\nstderr:\n{}",
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr)
            );
        }
    }
}