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
//! Event replay onto the sparse buffer: snapshots, splices, segments.
use super::*;
// ── (4) sparse-buffer replay ──
#[test]
fn replay_full_snapshot_resets_then_partial_splices_without_padding() {
let mut buf = SparseBuffer::default();
buf.reset_to_full("a\nb\nc", 3, 10);
assert_eq!(
buf.known_lines(),
vec![(1, "a".into()), (2, "b".into()), (3, "c".into())]
);
// A windowed read at line 6 splices WITHOUT padding lines 4-5 (they stay gaps).
buf.splice(6, &["f".to_string()], 6, 20);
let ranges = buf.covered_ranges();
assert_eq!(
ranges,
vec![(1, 3), (6, 6)],
"no fabricated padding for the 4-5 gap"
);
assert_eq!(buf.seen_total_lines, Some(6));
}
#[test]
fn reset_to_full_drops_phantom_trailing_line_from_separator_count_total() {
// CC's Read / file-attachment `totalLines` is a SEPARATOR count: a 2-line file ending in
// `\n` reports totalLines=3. We hold the full content, so split_lines (2) is authoritative -
// the phantom 3rd line must NOT inflate seen_total (else restore/salvage/at/coverage report a
// spurious unknown trailing line). Regression for the node24-migrate.sh 96/97 (98%) bug.
let mut buf = SparseBuffer::default();
buf.reset_to_full("a\nb\n", 3, 10);
assert_eq!(buf.known_lines(), vec![(1, "a".into()), (2, "b".into())]);
assert_eq!(
buf.seen_total_lines,
Some(2),
"separator-count total 3 must normalise to the terminator count 2"
);
// A non-newline-terminated full snapshot keeps its total verbatim (there is no phantom line).
let mut buf2 = SparseBuffer::default();
buf2.reset_to_full("a\nb", 2, 10);
assert_eq!(buf2.seen_total_lines, Some(2));
}
#[test]
fn replay_full_read_after_write_does_not_invent_trailing_gap() {
// The real node24-migrate.sh sequence: a Write creates the file (terminator count), then a
// later full READ / file-attachment re-observes it with a SEPARATOR-count `totalLines` (N+1
// for a newline-terminated file). The read is the LAST event, so it sets the final
// seen_total - which must stay N, not N+1, so `recover` (restore) sees a COMPLETE file.
let events = vec![
FileEvent {
line_no: 1,
turn_index: 0,
timestamp_utc: Some("2026-06-09T00:00:00.000Z".into()),
kind: EventKind::FullSnapshot {
content: "l1\nl2\nl3\n".into(),
total_lines: 3, // Write: terminator count
source: SnapSource::Write,
},
},
FileEvent {
line_no: 2,
turn_index: 1,
timestamp_utc: Some("2026-06-09T00:01:00.000Z".into()),
kind: EventKind::FullSnapshot {
content: "l1\nl2\nl3\n".into(),
total_lines: 4, // Read / file-attachment: SEPARATOR count (the phantom +1)
source: SnapSource::FileAttachment,
},
},
];
let rep = replay(&events, None);
assert_eq!(rep.final_buffer.known_lines().len(), 3);
assert_eq!(
rep.final_buffer.seen_total_lines,
Some(3),
"the separator-count read must not invent a phantom 4th line"
);
// complete == (known.len() == seen_total) → restore succeeds instead of hard-failing.
assert_eq!(
rep.final_buffer.known_lines().len(),
rep.final_buffer.seen_total_lines.unwrap()
);
}
#[test]
fn replay_edit_applies_to_running_buffer_not_originalfile() {
// The buffer holds a\nb\nc; an edit's structuredPatch changes line 2 b→B. We apply to
// the BUFFER (structured patch), not the originalFile field.
let events = vec![
FileEvent {
line_no: 1,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::FullSnapshot {
content: "a\nb\nc".into(),
total_lines: 3,
source: SnapSource::Write,
},
},
FileEvent {
line_no: 2,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::Edit {
hunks: vec![EditHunk {
old_string: "b".into(),
new_string: "B".into(),
replace_all: false,
}],
original_file: None,
structured_patch: Some(vec![PatchHunk {
old_start: 2,
old_lines: 1,
new_lines: 1,
lines: vec!["-b".into(), "+B".into()],
}]),
},
},
];
let rep = replay(&events, None);
assert_eq!(
rep.final_buffer.known_lines(),
vec![(1, "a".into()), (2, "B".into()), (3, "c".into())]
);
}
#[test]
fn replay_unanchorable_edit_is_a_hole_not_a_fabrication() {
// An edit whose old region falls in an unknown gap (no anchor) is un-anchorable: it
// becomes a coverage hole and does NOT invent island lines.
let events = vec![
// A windowed read of lines 1-2 only.
FileEvent {
line_no: 1,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::PartialRead {
start_line: 1,
lines: vec!["a".into(), "b".into()],
total_lines: 100,
},
},
// An edit at line 50 (deep in the unknown gap).
FileEvent {
line_no: 2,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::Edit {
hunks: vec![EditHunk {
old_string: "zzz".into(),
new_string: "Z".into(),
replace_all: false,
}],
original_file: None,
structured_patch: Some(vec![PatchHunk {
old_start: 50,
old_lines: 1,
new_lines: 1,
lines: vec!["-zzz".into(), "+Z".into()],
}]),
},
},
];
let rep = replay(&events, None);
assert_eq!(
rep.counts.edit_unanchorable, 1,
"the drifted edit is a hole"
);
// No island line was created at 50.
assert!(
!rep.final_buffer.known.contains_key(&50),
"no fabricated island at line 50"
);
assert_eq!(
rep.final_buffer.known_lines(),
vec![(1, "a".into()), (2, "b".into())]
);
}
#[test]
fn replay_string_edit_fallback_when_no_structured_patch() {
// No structuredPatch → string replacement over the contiguous-from-1 known text.
let events = vec![
FileEvent {
line_no: 1,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::FullSnapshot {
content: "hello\nworld".into(),
total_lines: 2,
source: SnapSource::Write,
},
},
FileEvent {
line_no: 2,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::Edit {
hunks: vec![EditHunk {
old_string: "world".into(),
new_string: "café🛠".into(),
replace_all: false,
}],
original_file: None,
structured_patch: None,
},
},
];
let rep = replay(&events, None);
assert_eq!(
rep.final_buffer.known_lines(),
vec![(1, "hello".into()), (2, "café🛠".into())]
);
}
#[test]
fn replay_edit_into_already_open_segment_does_not_reopen() {
// When an Edit arrives with a segment already open (a windowed read opened it, no full
// anchor yet), the `seg_open.is_none()` guard takes its FALSE side - the edit extends
// the open segment instead of opening a new one. With no full anchor, the string-edit
// fallback cannot anchor a non-contiguous-from-1 buffer, so it is a hole - but the
// single-segment shape proves the open segment was reused, not reopened.
let events = vec![
FileEvent {
line_no: 10,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::PartialRead {
start_line: 1,
lines: vec!["a".into(), "b".into()],
total_lines: 2,
},
},
FileEvent {
line_no: 11,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::Edit {
hunks: vec![EditHunk {
old_string: "a".into(),
new_string: "A".into(),
replace_all: false,
}],
original_file: None,
structured_patch: Some(vec![PatchHunk {
old_start: 1,
old_lines: 1,
new_lines: 1,
lines: vec!["-a".into(), "+A".into()],
}]),
},
},
];
let rep = replay(&events, None);
assert_eq!(
rep.segments.len(),
1,
"the edit extended the read's open segment (seg_open was already Some)"
);
// The structured patch anchored on the known contiguous lines 1-2 → applied.
assert_eq!(
rep.final_buffer.known.get(&1).map(|c| c.text.as_str()),
Some("A"),
"structured patch anchored on the read's known lines"
);
}
#[test]
fn replay_edit_as_first_event_opens_a_segment() {
// An Edit arriving with NO prior read/anchor (seg_open == None) takes the TRUE side of
// `if seg_open.is_none()` and OPENS a fresh segment. With no known content it cannot
// anchor (un-anchorable hole), but a segment is opened so the op is timeline-visible.
let events = vec![FileEvent {
line_no: 1,
turn_index: 0,
timestamp_utc: None,
kind: EventKind::Edit {
hunks: vec![EditHunk {
old_string: "x".into(),
new_string: "y".into(),
replace_all: false,
}],
original_file: None,
structured_patch: None,
},
}];
let rep = replay(&events, None);
assert_eq!(
rep.segments.len(),
1,
"the lone edit opened a segment (seg_open was None → the if-body ran)"
);
assert_eq!(
rep.counts.edit_unanchorable, 1,
"with no known content the edit is an honest hole"
);
}