Repository navigation
Expand file tree
/
Copy pathcopy_test.go
More file actions
285 lines (273 loc) · 10.1 KB
/
Copy pathcopy_test.go
File metadata and controls
285 lines (273 loc) · 10.1 KB
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
package cell
import (
"context"
"crypto/sha256"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
type fileStamp struct {
Mode fs.FileMode
Time int64
Sum [32]byte
}
func copyFingerprint(t *testing.T, root string) map[string]fileStamp {
t.Helper()
out := map[string]fileStamp{}
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
info, err := d.Info()
if err != nil {
return err
}
rel, _ := filepath.Rel(root, path)
var data []byte
if !d.IsDir() {
data, err = os.ReadFile(path)
if err != nil {
return err
}
}
out[rel] = fileStamp{info.Mode(), info.ModTime().UnixNano(), sha256.Sum256(data)}
return nil
})
if err != nil {
t.Fatal(err)
}
return out
}
func olderFixture(t *testing.T) string {
t.Helper()
root := filepath.Join(t.TempDir(), "older-chat")
for path, body := range map[string]string{"transcript.jsonl": "whole conversation\n", "state.json": "dials", "tasks/a.jsonl": "task journal"} {
name := filepath.Join(root, path)
if err := os.MkdirAll(filepath.Dir(name), 0750); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(name, []byte(body), 0640); err != nil {
t.Fatal(err)
}
at := time.Unix(1234567890, 987654321)
if err := os.Chtimes(name, at, at); err != nil {
t.Fatal(err)
}
}
at := time.Unix(1234567890, 987654300)
for _, dir := range []string{filepath.Join(root, "tasks"), root} {
if err := os.Chtimes(dir, at, at); err != nil {
t.Fatal(err)
}
}
return root
}
// TestC29CopyNeverTouchesTheOriginal proves C29.1/C29.3: making and using the
// copy leaves every source file, mode and mtime unchanged and retries reuse it.
func TestC29CopyNeverTouchesTheOriginal(t *testing.T) {
original := olderFixture(t)
before := copyFingerprint(t, original)
c, err := CopyLegacy(t.Context(), original, func(ctx context.Context, c Cell) error { return nil })
if err != nil {
t.Fatal(err)
}
f, err := os.OpenFile(filepath.Join(c.Root, TranscriptPath), os.O_APPEND|os.O_WRONLY, 0)
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString("a new turn\n")
if err != nil {
t.Fatal(err)
}
_ = f.Close()
again, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error { t.Fatal("prepared twice"); return nil })
if err != nil || again.Root != c.Root {
t.Fatalf("duplicate copy: %v %v", again, err)
}
if !reflect.DeepEqual(before, copyFingerprint(t, original)) {
t.Fatal("the original changed")
}
if !ValidID(c.ID) {
t.Fatal("copy identity cannot move")
}
}
// TestC29CopyFailureIsInvisibleAndRetryCompletes proves C29.6 at each public
// preparation boundary: failure leaves no visible copy and retry publishes one.
func TestC29CopyFailureIsInvisibleAndRetryCompletes(t *testing.T) {
original := olderFixture(t)
before := copyFingerprint(t, original)
_, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error { return fmt.Errorf("interrupted before durable seal") })
if err == nil || Copied(original) != original {
t.Fatal("partial copy became visible")
}
if !reflect.DeepEqual(before, copyFingerprint(t, original)) {
t.Fatal("original changed on interruption")
}
if _, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error { return nil }); err != nil {
t.Fatal(err)
}
if Copied(original) == original {
t.Fatal("retry did not finish")
}
}
// TestC29KilledCopyRestarts proves C29.6 with an actual process kill between
// private construction and publication, including native Windows Process.Kill.
func TestC29KilledCopyRestarts(t *testing.T) {
if root := os.Getenv("C6_COPY_KILL_ROOT"); root != "" {
copyCheckpoint = func(step string) {
if step == os.Getenv("C6_COPY_KILL_STEP") {
p, _ := os.FindProcess(os.Getpid())
_ = p.Kill()
select {}
}
}
_, _ = CopyLegacy(context.Background(), root, func(context.Context, Cell) error { return nil })
os.Exit(3)
}
for round := 0; round < 3; round++ {
for _, step := range []string{"source-locked", "file-created", "file-written", "copied", "migrated", "prepared", "published"} {
t.Run(fmt.Sprintf("%d/%s", round, step), func(t *testing.T) {
original := olderFixture(t)
before := copyFingerprint(t, original)
cmd := exec.Command(os.Args[0], "-test.run=^TestC29KilledCopyRestarts$")
cmd.Env = append(os.Environ(), "C6_COPY_KILL_ROOT="+original, "C6_COPY_KILL_STEP="+step)
if err := cmd.Run(); err == nil {
t.Fatal("child was not killed")
}
after := copyFingerprint(t, original)
if !reflect.DeepEqual(before, after) {
for name, was := range before {
if now := after[name]; now != was {
t.Errorf("changed %s: before=%+v after=%+v", name, was, now)
}
}
t.Fatal("kill changed original")
}
if _, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error { return nil }); err != nil {
t.Fatal(err)
}
if Copied(original) == original {
t.Fatal("retry did not publish a complete copy")
}
})
}
}
}
// TestC29LinkedFolderCannotAliasTheOriginal proves C29.1: a supplied folder
// link must never become the destination of migration writes through the copy.
func TestC29LinkedFolderCannotAliasTheOriginal(t *testing.T) {
original := olderFixture(t)
alias := filepath.Join(t.TempDir(), "linked-chat")
if err := os.Symlink(original, alias); err != nil {
t.Skipf("folder link fixture unavailable: %v", err)
}
before := copyFingerprint(t, original)
if _, err := CopyLegacy(t.Context(), alias, func(context.Context, Cell) error { t.Fatal("linked original was prepared"); return nil }); err == nil {
t.Fatal("folder link was copied as an alias")
}
if !reflect.DeepEqual(before, copyFingerprint(t, original)) {
t.Fatal("copy through a folder link changed the original")
}
}
// TestTheCopyFolderSaysItIsTheLiveStore pins #2072: the folder holding moved
// chats is named for a move in progress, so it says, to anyone who looks, that
// it holds the chats themselves and must not be removed.
func TestTheCopyFolderSaysItIsTheLiveStore(t *testing.T) {
original := olderFixture(t)
if _, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error { return nil }); err != nil {
t.Fatal(err)
}
raw, err := os.ReadFile(filepath.Join(filepath.Dir(CopyRoot(original)), CopyFolderReadme))
if err != nil || !strings.Contains(string(raw), "Do not delete it") {
t.Fatalf("the copy folder does not explain itself: %q, %v", raw, err)
}
}
// TestCopyLegacyRefusesToWipePopulatedDestination verifies the second line of
// defence when a destination copy already holds sealed turns (either in
// turns.jsonl or adopted.json) without copy-ready: CopyLegacy opens and
// re-stamps a valid cell without rerunning prepare or removing the destination,
// and fails loudly without removing the destination if OpenAt fails.
func TestCopyLegacyRefusesToWipePopulatedDestination(t *testing.T) {
for _, marker := range []string{"turns.jsonl", "adopted.json"} {
t.Run(marker, func(t *testing.T) {
original := olderFixture(t)
c, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error { return nil })
if err != nil {
t.Fatal(err)
}
// Append precious post-copy content and record a sealed turn marker.
transcriptPath := filepath.Join(c.Root, filepath.FromSlash(TranscriptPath))
if err := os.WriteFile(transcriptPath, []byte("whole conversation\nprecious turn\n"), 0o600); err != nil {
t.Fatal(err)
}
switch marker {
case "turns.jsonl":
if err := os.WriteFile(filepath.Join(c.Root, StateDir, "turns.jsonl"),
[]byte(`{"V":1,"id":"1111111111111111111111111111111111111111111111111111111111111111"}`+"\n"), 0o600); err != nil {
t.Fatal(err)
}
case "adopted.json":
if err := os.WriteFile(filepath.Join(c.Root, StateDir, "adopted.json"),
[]byte(`{"V":1,"head":"2222222222222222222222222222222222222222222222222222222222222222"}`+"\n"), 0o600); err != nil {
t.Fatal(err)
}
}
// Remove copy-ready to simulate an older build's restore or take-back stripping it.
readyFile := filepath.Join(c.Root, StateDir, "copy-ready")
if err := os.Remove(readyFile); err != nil {
t.Fatal(err)
}
reopened, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error {
t.Fatal("CopyLegacy re-ran prepare on an already-populated copy")
return nil
})
if err != nil {
t.Fatalf("CopyLegacy failed instead of opening populated copy: %v", err)
}
if reopened.Root != c.Root {
t.Fatalf("CopyLegacy returned root %s, want %s", reopened.Root, c.Root)
}
after, err := os.ReadFile(transcriptPath)
if err != nil || !strings.Contains(string(after), "precious turn") {
t.Fatalf("populated copy transcript lost precious turn: err=%v body=%q", err, string(after))
}
if Copied(original) != c.Root {
t.Fatalf("Copied(original) = %s, want %s", Copied(original), c.Root)
}
})
}
t.Run("unopenable_populated_destination_fails_loudly_without_removal", func(t *testing.T) {
original := olderFixture(t)
c, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error { return nil })
if err != nil {
t.Fatal(err)
}
transcriptPath := filepath.Join(c.Root, filepath.FromSlash(TranscriptPath))
if err := os.WriteFile(transcriptPath, []byte("precious unopenable turn\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(c.Root, StateDir, "turns.jsonl"),
[]byte(`{"V":1,"id":"1111111111111111111111111111111111111111111111111111111111111111"}`+"\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Remove(filepath.Join(c.Root, StateDir, "copy-ready")); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(c.Root, filepath.FromSlash(MetaPath)), []byte("{corrupt"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := CopyLegacy(t.Context(), original, func(context.Context, Cell) error { return nil }); err == nil {
t.Fatal("expected CopyLegacy to fail loudly when populated destination cannot be opened")
}
after, err := os.ReadFile(transcriptPath)
if err != nil || !strings.Contains(string(after), "precious unopenable turn") {
t.Fatalf("CopyLegacy deleted or altered populated unopenable destination: err=%v body=%q", err, string(after))
}
})
}