@@ -15,8 +15,11 @@ import (
1515 "github.com/entireio/cli/redact"
1616)
1717
18- func TestWrite_ImportedSurfacesOnList (t * testing.T ) {
19- t .Parallel ()
18+ // newImportedTestStore builds a GitStore over a fresh temp repo with one
19+ // commit (so HEAD exists) and returns it with a redacted one-line transcript,
20+ // shared setup for the imported-checkpoint tests below.
21+ func newImportedTestStore (t * testing.T ) (* GitStore , redact.RedactedBytes ) {
22+ t .Helper ()
2023 tempDir := t .TempDir ()
2124 testutil .InitRepo (t , tempDir )
2225 repo , err := git .PlainOpen (tempDir )
@@ -38,12 +41,17 @@ func TestWrite_ImportedSurfacesOnList(t *testing.T) {
3841 t .Fatal (err )
3942 }
4043
41- store := NewGitStore (repo , DefaultV1Refs ())
4244 red , err := redact .JSONLBytes ([]byte (`{"type":"user","uuid":"u1","message":{"role":"user","content":"hi"}}` + "\n " ))
4345 if err != nil {
4446 t .Fatal (err )
4547 }
46- err = store .Write (context .Background (), Session {
48+ return NewGitStore (repo , DefaultV1Refs ()), red
49+ }
50+
51+ func TestWrite_ImportedSurfacesOnList (t * testing.T ) {
52+ t .Parallel ()
53+ store , red := newImportedTestStore (t )
54+ err := store .Write (context .Background (), Session {
4755 CheckpointID : id .MustCheckpointID ("aabbccddeeff" ),
4856 SessionID : "s1" ,
4957 Strategy : "import" ,
@@ -68,37 +76,12 @@ func TestWrite_ImportedSurfacesOnList(t *testing.T) {
6876
6977func TestImportedCheckpoint_CommitSHAPersisted (t * testing.T ) {
7078 t .Parallel ()
71- tempDir := t .TempDir ()
72- testutil .InitRepo (t , tempDir )
73- repo , err := git .PlainOpen (tempDir )
74- if err != nil {
75- t .Fatalf ("open repo: %v" , err )
76- }
77- // Initial commit so HEAD exists.
78- wt , err := repo .Worktree ()
79- if err != nil {
80- t .Fatal (err )
81- }
82- testutil .WriteFile (t , tempDir , "f.txt" , "x" )
83- if _ , err := wt .Add ("f.txt" ); err != nil {
84- t .Fatal (err )
85- }
86- if _ , err := wt .Commit ("init" , & git.CommitOptions {
87- Author : & object.Signature {Name : "Test" , Email : "test@test.com" },
88- }); err != nil {
89- t .Fatal (err )
90- }
91-
92- store := NewGitStore (repo , DefaultV1Refs ())
93- red , err := redact .JSONLBytes ([]byte (`{"type":"user","uuid":"u1","message":{"role":"user","content":"hi"}}` + "\n " ))
94- if err != nil {
95- t .Fatal (err )
96- }
79+ store , red := newImportedTestStore (t )
9780
9881 const commitSHA = "b01b59663fd4860fd15a9939499be44a14dbf168"
9982 ctx := context .Background ()
10083 cid := id .MustCheckpointID ("aabbccddeeff" )
101- err = store .Write (ctx , Session {
84+ err : = store .Write (ctx , Session {
10285 CheckpointID : cid ,
10386 SessionID : "s1" ,
10487 Strategy : "import" ,
@@ -161,3 +144,57 @@ func TestImportedCheckpoint_CommitSHAPersisted(t *testing.T) {
161144 t .Fatalf ("expected marshaled metadata to omit commit_sha when unset, got %s" , rawMD )
162145 }
163146}
147+
148+ // TestImportedCheckpoint_CommitSHASurvivesSummaryRewrite proves the root
149+ // summary's preserve-on-rewrite: a later write to the SAME checkpoint that
150+ // carries no CommitSHA (e.g. a review session attached to it) must not clear
151+ // the stamped anchor from the root CheckpointSummary. Session-level Metadata
152+ // is deliberately NOT preserved the same way — each session's metadata.json
153+ // records what its own write carried.
154+ func TestImportedCheckpoint_CommitSHASurvivesSummaryRewrite (t * testing.T ) {
155+ t .Parallel ()
156+ store , red := newImportedTestStore (t )
157+
158+ const commitSHA = "b01b59663fd4860fd15a9939499be44a14dbf168"
159+ ctx := context .Background ()
160+ cid := id .MustCheckpointID ("ddeeff001122" )
161+ err := store .Write (ctx , Session {
162+ CheckpointID : cid ,
163+ SessionID : "s1" ,
164+ Strategy : "import" ,
165+ Kind : "imported" ,
166+ Agent : agent .AgentTypeClaudeCode ,
167+ Transcript : red ,
168+ Prompts : []string {"hi" },
169+ CheckpointsCount : 1 ,
170+ CommitSHA : commitSHA ,
171+ })
172+ if err != nil {
173+ t .Fatalf ("write imported checkpoint: %v" , err )
174+ }
175+
176+ // Second write to the same checkpoint, different session, no CommitSHA.
177+ err = store .Write (ctx , Session {
178+ CheckpointID : cid ,
179+ SessionID : "s2" ,
180+ Strategy : "manual-commit" ,
181+ Agent : agent .AgentTypeClaudeCode ,
182+ Transcript : red ,
183+ Prompts : []string {"review it" },
184+ CheckpointsCount : 1 ,
185+ })
186+ if err != nil {
187+ t .Fatalf ("second write to same checkpoint: %v" , err )
188+ }
189+
190+ summary , err := store .Read (ctx , cid )
191+ if err != nil {
192+ t .Fatalf ("read checkpoint summary: %v" , err )
193+ }
194+ if summary .CommitSHA != commitSHA {
195+ t .Fatalf ("root summary commit_sha must survive a CommitSHA-less rewrite: expected %q, got %q" , commitSHA , summary .CommitSHA )
196+ }
197+ if len (summary .Sessions ) != 2 {
198+ t .Fatalf ("expected both sessions in the rewritten summary, got %d" , len (summary .Sessions ))
199+ }
200+ }
0 commit comments