strop-engine 0.35.0

strop editor engine: documents, grammar, services and sessions — no frontend UI dependencies
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
//! 0056 AR04 crash/EOF/restart journeys: dirty documents, scratch drafts,
//! coherent cohorts, rename/delete/conflict source states, over-bound
//! cohorts, memory-only mode, remote consent and failed persistence.
//! Disk content stays unchanged until an explicit checked save.

use super::record::{DraftOrigin, Snapshot};
use super::{store, Editor};
use std::path::PathBuf;
use strop_core::Buffer;

struct Fixture {
    _dir: tempfile::TempDir,
    cwd: PathBuf,
    state: PathBuf,
}

fn fixture() -> Fixture {
    let dir = tempfile::tempdir().unwrap();
    let cwd = dir.path().join("project");
    std::fs::create_dir(&cwd).unwrap();
    Fixture {
        state: dir.path().join("state"),
        cwd,
        _dir: dir,
    }
}

impl Fixture {
    fn editor(&self) -> Editor {
        let mut editor = Editor::new_in(Buffer::from_text(""), self.cwd.clone());
        editor.state_dir = Some(self.state.clone());
        editor
    }
    fn checkpoint(&self) -> PathBuf {
        store::checkpoint_path(&self.state, &self.cwd)
    }
    fn stored(&self) -> Option<store::StoredCohort> {
        store::load(&self.checkpoint()).unwrap()
    }
    fn write(&self, name: &str, text: &str) -> PathBuf {
        let path = self.cwd.join(name);
        std::fs::write(&path, text).unwrap();
        path
    }
}

fn settle(editor: &mut Editor) {
    editor.wait_io().unwrap();
}

fn surface(editor: &Editor) -> String {
    editor.buf().text().to_string()
}

#[test]
fn dirty_document_checkpoints_and_restores_after_crash() {
    let fixture = fixture();
    let file = fixture.write("a.txt", "alpha\n");
    let mut editor = fixture.editor();
    editor.open_fixture(&file).unwrap();
    editor.feed_text("x");
    settle(&mut editor);
    // Checkpointing never marks text clean or advances its saved baseline.
    assert!(editor.buf().dirty);
    assert!(fixture.checkpoint().exists());
    // Crash: the editor drops without an orderly close.
    drop(editor);

    let mut restarted = fixture.editor();
    restarted.feed_text(":recover<cr>");
    settle(&mut restarted);
    let rows = surface(&restarted);
    assert!(rows.contains("a.txt"), "{rows}");
    assert!(rows.contains("[current]"), "{rows}");
    restarted.feed_text(":recover restore 1<cr>");
    settle(&mut restarted);
    assert_eq!(restarted.buf().text().to_string(), "lpha\n");
    let name = restarted.buf().name.clone().unwrap_or_default();
    assert!(name.starts_with("recovered: "), "{name}");
    assert!(
        restarted.buf().path.is_none(),
        "a checked draft has no target"
    );
    assert!(restarted.buf().dirty);
    // The disk was never touched by checkpoint or restore.
    assert_eq!(std::fs::read_to_string(&file).unwrap(), "alpha\n");
}

#[test]
fn scratch_draft_checkpoints_and_restores_after_crash() {
    let fixture = fixture();
    let mut editor = fixture.editor();
    editor.feed_text("idraft words<esc>");
    settle(&mut editor);
    assert!(fixture.checkpoint().exists());
    drop(editor);

    let mut restarted = fixture.editor();
    restarted.feed_text(":recover<cr>");
    settle(&mut restarted);
    let rows = surface(&restarted);
    assert!(rows.contains("[scratch]"), "{rows}");
    restarted.feed_text(":recover restore 1<cr>");
    settle(&mut restarted);
    assert_eq!(restarted.buf().text().to_string(), "draft words");
}

#[test]
fn a_multi_document_change_captures_one_coherent_cohort() {
    let fixture = fixture();
    let a = fixture.write("a.txt", "one\n");
    let b = fixture.write("b.txt", "two\n");
    let mut editor = fixture.editor();
    let a_doc = editor.open_fixture(&a).unwrap();
    editor.open_fixture(&b).unwrap();
    editor.feed_text("x");
    editor.switch_to(a_doc);
    editor.feed_text("x");
    settle(&mut editor);

    let stored = fixture.stored().expect("a durable checkpoint");
    assert_eq!(stored.header.records.len(), 2, "{stored:?}");
    let cohort = stored.header.cohort;
    assert!(cohort > 0);
    let text_of = |name: &str| -> String {
        let (index, record) = stored
            .header
            .records
            .iter()
            .enumerate()
            .find(|(_, record)| record.origin.label().ends_with(name))
            .unwrap_or_else(|| panic!("a record for {name}"));
        assert!(matches!(record.snapshot, Snapshot::Captured { .. }));
        String::from_utf8(stored.texts[index].clone().unwrap()).unwrap()
    };
    assert_eq!(text_of("a.txt"), "ne\n");
    assert_eq!(text_of("b.txt"), "wo\n");
}

#[test]
fn a_live_rename_reports_the_renamed_state() {
    let fixture = fixture();
    let file = fixture.write("old.txt", "alpha\n");
    let mut editor = fixture.editor();
    let document = editor.open_fixture(&file).unwrap();
    editor.feed_text("x");
    settle(&mut editor);

    let renamed = fixture.cwd.join("renamed.txt");
    std::fs::rename(&file, &renamed).unwrap();
    let stamp = std::fs::metadata(&renamed).and_then(|m| m.modified()).ok();
    let canonical = std::fs::canonicalize(&renamed).unwrap();
    editor
        .doc_mut(document)
        .buf
        .relocate_file_binding(canonical, stamp);

    editor.feed_text(":recover<cr>");
    settle(&mut editor);
    let rows = surface(&editor);
    assert!(rows.contains("[renamed]"), "{rows}");
    assert!(rows.contains("renamed.txt"), "{rows}");
}

#[test]
fn a_deleted_source_restores_without_recreating_it() {
    let fixture = fixture();
    let file = fixture.write("gone.txt", "alpha\n");
    let mut editor = fixture.editor();
    editor.open_fixture(&file).unwrap();
    editor.feed_text("x");
    settle(&mut editor);
    drop(editor);

    std::fs::remove_file(&file).unwrap();
    let mut restarted = fixture.editor();
    restarted.feed_text(":recover<cr>");
    settle(&mut restarted);
    let rows = surface(&restarted);
    assert!(rows.contains("[missing]"), "{rows}");
    restarted.feed_text(":recover restore 1<cr>");
    settle(&mut restarted);
    assert_eq!(restarted.buf().text().to_string(), "lpha\n");
    assert!(restarted.buf().path.is_none());
    assert!(!file.exists(), "restore never recreates a deleted target");
}

#[test]
fn an_external_change_reports_conflict_and_never_overwrites() {
    let fixture = fixture();
    let file = fixture.write("a.txt", "alpha\n");
    let mut editor = fixture.editor();
    editor.open_fixture(&file).unwrap();
    editor.feed_text("x");
    settle(&mut editor);
    drop(editor);

    // Different length: the source observation mismatch is deterministic.
    std::fs::write(&file, "externally changed content\n").unwrap();
    let mut restarted = fixture.editor();
    restarted.feed_text(":recover<cr>");
    settle(&mut restarted);
    let rows = surface(&restarted);
    assert!(rows.contains("[conflict]"), "{rows}");
    restarted.feed_text(":recover restore 1<cr>");
    settle(&mut restarted);
    assert_eq!(restarted.buf().text().to_string(), "lpha\n");
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "externally changed content\n",
        "restore never overwrites changed disk content"
    );
}

#[test]
fn an_over_bound_draft_is_reported_not_truncated() {
    let fixture = fixture();
    let small = fixture.write("small.txt", "one\n");
    let huge = fixture.write("huge.txt", &"x".repeat(17 * 1024 * 1024));
    let mut editor = fixture.editor();
    editor.open_fixture(&small).unwrap();
    editor.feed_text("x");
    settle(&mut editor);
    editor.open_fixture(&huge).unwrap();
    editor.feed_text("x");
    settle(&mut editor);

    let stored = fixture
        .stored()
        .expect("the fitting cohort still publishes");
    let huge_record = stored
        .header
        .records
        .iter()
        .find(|record| record.origin.label().ends_with("huge.txt"))
        .expect("the over-bound draft is reported in the cohort");
    assert!(
        matches!(huge_record.snapshot, Snapshot::OverBound { .. }),
        "{huge_record:?}"
    );
    let small_record = stored
        .header
        .records
        .iter()
        .find(|record| record.origin.label().ends_with("small.txt"))
        .expect("the fitting draft stays protected");
    assert!(matches!(small_record.snapshot, Snapshot::Captured { .. }));
    assert_eq!(editor.recovery_status().over_bound.len(), 1);

    editor.feed_text(":recover<cr>");
    settle(&mut editor);
    let rows = surface(&editor);
    assert!(rows.contains("[over-bound]"), "{rows}");
    let huge_index = stored
        .header
        .records
        .iter()
        .position(|record| record.origin.label().ends_with("huge.txt"))
        .unwrap()
        + 1;
    editor.feed_text(&format!(":recover restore {huge_index}<cr>"));
    settle(&mut editor);
    assert!(
        editor.message.contains("over the capture limit"),
        "{}",
        editor.message
    );
}

#[test]
fn memory_only_mode_persists_nothing_and_says_so() {
    let fixture = fixture();
    // No state directory: memory-only by construction.
    let mut editor = Editor::new_in(Buffer::from_text(""), fixture.cwd.clone());
    editor.feed_text("idraft<esc>");
    settle(&mut editor);
    assert!(editor.recovery_memory_only());
    editor.feed_text(":recover<cr>");
    settle(&mut editor);
    let rows = surface(&editor);
    assert!(rows.contains("memory-only"), "{rows}");
    assert!(rows.contains("NOT durable"), "{rows}");

    // A disabled session policy is memory-only even with a state directory.
    let mut disabled = fixture.editor();
    disabled.session_policy = crate::session::SessionPolicy::Disabled;
    disabled.feed_text("idraft<esc>");
    settle(&mut disabled);
    assert!(disabled.recovery_memory_only());
    assert!(!fixture.checkpoint().exists());
}

#[test]
fn remote_drafts_persist_only_with_explicit_session_consent() {
    let fixture = fixture();
    let mut editor = fixture.editor();
    let document = editor
        .docs
        .try_insert(crate::editor::test_support::remote::document(
            "remote bytes\n",
            strop_remote::ReadSelection::Full,
        ))
        .unwrap();
    editor.doc_mut(document).buf.dirty = true;
    editor.recovery_after_event();
    settle(&mut editor);
    assert!(
        !fixture.checkpoint().exists(),
        "no consent: remote drafts are never persisted"
    );

    editor.feed_text(":recover consent remote<cr>");
    settle(&mut editor);
    let stored = fixture.stored().expect("the consented cohort");
    let remote = stored
        .header
        .records
        .iter()
        .find(|record| matches!(record.origin, DraftOrigin::RemoteFile { .. }))
        .expect("consent persists the remote draft");
    assert!(remote.origin.label().contains("ssh://fixture"));

    editor.feed_text(":recover<cr>");
    settle(&mut editor);
    let rows = surface(&editor);
    assert!(rows.contains("[remote]"), "{rows}");
    editor.feed_text(":recover restore 1<cr>");
    settle(&mut editor);
    assert_eq!(editor.buf().text().to_string(), "remote bytes\n");
    assert!(
        editor.buf().path.is_none(),
        "restoring never re-grants remote authority"
    );
}

#[test]
fn a_confirmed_save_retires_only_the_checkpoint_it_supersedes() {
    let fixture = fixture();
    let file = fixture.write("a.txt", "alpha\n");
    let mut editor = fixture.editor();
    editor.open_fixture(&file).unwrap();
    editor.feed_text("x");
    settle(&mut editor);
    assert!(fixture
        .stored()
        .expect("checkpoint")
        .header
        .records
        .iter()
        .any(|record| record.origin.label().ends_with("a.txt")));

    editor.feed_text(":w<cr>");
    settle(&mut editor);
    assert!(!editor.buf().dirty);
    let stored = fixture.stored().expect("the retired cohort republishes");
    assert!(
        stored
            .header
            .records
            .iter()
            .all(|record| !record.origin.label().ends_with("a.txt")),
        "the confirmed save retired its checkpoint"
    );

    // Edits after that save still need recovery: the next capture is back.
    editor.feed_text("x");
    settle(&mut editor);
    assert!(fixture
        .stored()
        .expect("checkpoint")
        .header
        .records
        .iter()
        .any(|record| record.origin.label().ends_with("a.txt")));
}

#[test]
fn orderly_close_checkpoints_eligible_drafts() {
    let fixture = fixture();
    let file = fixture.write("a.txt", "alpha\n");
    let mut editor = fixture.editor();
    editor.open_fixture(&file).unwrap();
    editor.feed_text("x");
    editor.finish_background_work();
    settle(&mut editor);
    assert!(editor.take_shutdown_error().is_none());
    assert!(fixture
        .stored()
        .expect("the orderly-close checkpoint")
        .header
        .records
        .iter()
        .any(|record| record.origin.label().ends_with("a.txt")));
}

#[test]
fn a_persistence_failure_is_visible_and_writes_nothing() {
    let fixture = fixture();
    // A regular file where the state directory must be: publication fails.
    let blocked = fixture.cwd.join("blocked-state");
    std::fs::write(&blocked, b"not a directory").unwrap();
    let file = fixture.write("a.txt", "alpha\n");
    let mut editor = Editor::new_in(Buffer::from_text(""), fixture.cwd.clone());
    editor.state_dir = Some(blocked);
    editor.open_fixture(&file).unwrap();
    editor.feed_text("x");
    settle(&mut editor);
    let status = editor.recovery_status();
    assert!(status.last_error.is_some(), "the failure reaches status");
    assert!(
        editor.message.contains("checkpoint failed") || status.last_error.is_some(),
        "{}",
        editor.message
    );
    assert_eq!(std::fs::read_to_string(&file).unwrap(), "alpha\n");
    assert!(editor.buf().dirty, "recovery failure is not a save");
}

#[test]
fn a_deliberate_discard_drops_the_record() {
    let fixture = fixture();
    let file = fixture.write("a.txt", "alpha\n");
    let mut editor = fixture.editor();
    editor.open_fixture(&file).unwrap();
    editor.feed_text("x");
    settle(&mut editor);
    drop(editor);

    let mut restarted = fixture.editor();
    restarted.feed_text(":recover discard 1<cr>");
    settle(&mut restarted);
    let stored = fixture.stored().expect("the cohort survives the discard");
    assert!(
        stored
            .header
            .records
            .iter()
            .all(|record| !record.origin.label().ends_with("a.txt")),
        "the discarded record's bytes are gone"
    );
    let rows = surface(&restarted);
    assert!(
        rows.contains("no drafts") || !rows.contains("a.txt"),
        "{rows}"
    );
}

#[test]
fn a_suppressed_tape_never_launches_recovery_natively() {
    // 0057 VF15: capture/replay/recovery admission — under a fixture or
    // replay tape the native side is suppressed (tape.request answers
    // false): the request is RECORDED, but no worker is spawned and no
    // checkpoint bytes reach the disk. Replay and recovery never
    // acquire fresh process or filesystem authority.
    let fixture = fixture();
    let file = fixture.write("a.txt", "alpha\n");
    let mut editor = fixture.editor();
    editor.open_fixture(&file).unwrap();
    editor.tape = std::rc::Rc::new(strop_trace::replay::Tape::fixture(|operation, _| {
        panic!("no synchronous native query belongs to recovery: {operation}")
    }));
    editor.feed_text("x");
    editor.recovery_after_event();
    assert!(editor.buf().dirty, "the draft itself is untouched");
    let pending = editor
        .recovery
        .in_flight
        .expect("the publication was admitted onto the tape");
    assert!(
        !editor.worker_handles.contains_key(&pending),
        "suppressed: no native worker holds the request"
    );
    assert!(
        editor.recovery.queued.is_none(),
        "nothing pretends to be in progress"
    );
    assert!(
        !fixture.checkpoint().exists(),
        "no checkpoint bytes on disk"
    );
    assert!(
        editor.tape.fixture_nodes().iter().any(|node| matches!(
            node,
            strop_trace::replay::Node::Request { operation, .. }
                if operation == "io.recovery"
        )),
        "the request was recorded, never launched"
    );
}