strop-engine 0.36.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
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
//! `:explain` — why things are the way they are (0042 slice 2 follow-on,
//! handoff "explain editor decisions"). A real readonly buffer (0001 §4):
//! `/` searches it, motions walk it, `q` closes it. Renders from the
//! editor's actual decision records — the workspace registry, attach
//! records and live config — never inferred from logs.

use std::fmt::Write;

use super::lsp::attach::AttachDecision;
use super::Editor;

impl Editor {
    /// Open the explain buffer: workspace bindings, the current document's
    /// routing, language-server readiness and refusals with their reasons,
    /// and effective configuration.
    pub(crate) fn open_explain(&mut self) {
        let mut text = String::from("strop explain — why things are the way they are\n");

        text.push_str("\n[workspaces]\n");
        for (id, context) in self.workspaces.iter() {
            let root = context
                .root
                .as_ref()
                .map(|root| root.display().to_string())
                .unwrap_or_else(|| "no project root".into());
            let _ = writeln!(
                text,
                "  {} #{}  {}  (incarnation {})",
                context.filesystem.label(),
                id.index(),
                root,
                context.incarnation
            );
        }

        text.push_str("\n[workers]\n");
        for (_, context) in self.workspaces.iter() {
            match &context.filesystem {
                strop_workspace::Filesystem::Local => {
                    let state = match self.filesystem.worker().session() {
                        Some(session) => format!("ready (incarnation {})", session.incarnation),
                        None => "configured; not connected or worker lost".into(),
                    };
                    let _ = writeln!(text, "  local       {state}");
                }
                strop_workspace::Filesystem::Remote(endpoint) => {
                    let state = match self.remote.workers.get_ready(endpoint) {
                        Some(ready) => {
                            let connection = match ready.worker.worker().session() {
                                Some(session) => {
                                    format!("connected (incarnation {})", session.incarnation)
                                }
                                None => "artifact verified; service not connected".into(),
                            };
                            format!(
                                "{connection}; {} / {} / sha256:{} at {}",
                                env!("CARGO_PKG_VERSION"),
                                ready.target,
                                ready.artifact.sha256,
                                strop_core::layout::printable_text(&ready.artifact.path)
                            )
                        }
                        None if self.remote.worker_requested(endpoint) => {
                            "worker admission requested; SFTP browsing remains available".into()
                        }
                        None if self.remote.workers.admitting(endpoint) => {
                            "worker deployment in progress; SFTP browsing remains available".into()
                        }
                        None => "SFTP browsing only; :remote worker [URI] for services".into(),
                    };
                    let _ = writeln!(text, "  {endpoint}  {state}");
                }
                strop_workspace::Filesystem::Container(id) => {
                    let admitted = self
                        .containers
                        .attached
                        .get(id.as_str())
                        .and_then(|identity| self.containers.workers.get(identity));
                    match admitted {
                        Some(lease) => {
                            let state = match lease.worker().session() {
                                Some(session) => {
                                    format!("connected (incarnation {})", session.incarnation)
                                }
                                None => "artifact verified; service not connected".into(),
                            };
                            let _ = writeln!(text, "  container {}  {state}", id);
                            let _ = writeln!(
                                text,
                                "    target {} / {}",
                                env!("CARGO_PKG_VERSION"),
                                lease.target()
                            );
                            let _ =
                                writeln!(text, "    artifact sha256:{}", lease.artifact_sha256());
                            let _ = writeln!(
                                text,
                                "    path {}",
                                strop_core::layout::printable_text(lease.artifact_path())
                            );
                        }
                        None => {
                            let _ = writeln!(
                                text,
                                "  container {}  read-only browse; :container-worker for services",
                                id
                            );
                        }
                    }
                }
            }
        }

        text.push_str("\n[current document]\n");
        match self.lsp_current_doc_path() {
            Some(resource) => {
                let _ = writeln!(text, "  resource  {}", resource.label());
            }
            None => text.push_str("  resource  (scratch — no filesystem identity)\n"),
        }
        let _ = writeln!(
            text,
            "  state     {}, revision {}",
            if self.buf().dirty { "dirty" } else { "clean" },
            self.buf().revision().get()
        );
        if self.buf().readonly {
            let _ = writeln!(text, "  readonly  yes — {}", self.readonly_explanation());
        }
        match self.lsp_state.bindings.get(&self.current()) {
            Some(binding) => {
                let _ = writeln!(
                    text,
                    "  lsp       bound to server {} (root {}, on {})",
                    binding.server.get(),
                    binding.root.display(),
                    binding.target.label()
                );
            }
            None => text.push_str("  lsp       not bound\n"),
        }

        text.push_str("\n[language servers]\n");
        if self.lsp_servers.is_empty()
            && self.lsp_state.attach.pending.is_empty()
            && self.lsp_state.attach.refused.is_empty()
        {
            text.push_str("  none attached or attempted\n");
        }
        for server in &self.lsp_servers {
            let _ = writeln!(
                text,
                "  server {}  {}",
                server.id.get(),
                if server.ready { "ready" } else { "starting" }
            );
        }
        for (key, ticket) in &self.lsp_state.attach.pending {
            let _ = writeln!(
                text,
                "  {} on {}  attaching (request {})",
                key.language,
                key.target.label(),
                ticket.get()
            );
        }
        for (key, decision) in &self.lsp_state.attach.refused {
            let _ = writeln!(
                text,
                "  {} on {}  refused: {}",
                key.language,
                key.target.label(),
                explain_decision(decision)
            );
        }

        text.push_str("\n[configuration]\n");
        for knob in crate::config::KNOBS {
            // 0051 R10: real values from the one typed access path —
            // a knob that resolves to nothing is a test failure, never
            // a "?" in front of the user.
            let Some(value) = self.config.knob_value(knob.key) else {
                continue;
            };
            // 0056 AR14: the winning layer with its origin, per knob.
            let layer = match self.config.knob_layer(knob.key) {
                crate::config::ConfigLayer::Default => "default".to_string(),
                crate::config::ConfigLayer::User => match self.config.user_layer_path() {
                    Some(path) => format!("user {}", path.display()),
                    None => "user config.toml".into(),
                },
            };
            let _ = writeln!(
                text,
                "  {} = {}  [{}]  — {}",
                knob.key, value, layer, knob.desc
            );
        }
        text.push_str(
            "  (user config.toml layers over embedded defaults; project layering is languages.toml under :trust)\n",
        );

        // 0051 R08: the current document's effective indent with its
        // per-side provenance, the override state, and what detection
        // concluded (or why it stayed Unknown).
        let doc = self.cur();
        let indent = doc.indent;
        text.push_str("\n[indentation]\n");
        let _ = writeln!(
            text,
            "  effective: {} (style {}, width {})",
            indent.label(),
            indent.style_source.label(),
            indent.width_source.label(),
        );
        match (doc.indent_override.style, doc.indent_override.width) {
            (None, None) => text.push_str("  override: none (:tab-size / :indent-style set one)\n"),
            (style, width) => {
                let mut parts = Vec::new();
                if let Some(style) = style {
                    parts.push(format!("style {style:?}"));
                }
                if let Some(width) = width {
                    parts.push(format!("width {width}"));
                }
                let _ = writeln!(text, "  override: {}", parts.join(", "));
            }
        }
        match doc.detection {
            None => text.push_str("  detection: disabled (indent_detect = false)\n"),
            Some(crate::editor::document::Detection::Unknown(reason)) => {
                let _ = writeln!(
                    text,
                    "  detection: unknown — {} (configured fallback applies)",
                    reason.reason()
                );
            }
            Some(crate::editor::document::Detection::Tabs {
                evidence,
                confidence,
            }) => {
                let _ = writeln!(
                    text,
                    "  detection: tabs — {} confidence ({evidence} evidence lines)",
                    confidence.label()
                );
            }
            Some(crate::editor::document::Detection::Spaces {
                width,
                evidence,
                confidence,
            }) => {
                let _ = writeln!(
                    text,
                    "  detection: spaces, width {width} — {} confidence ({evidence} evidence lines)",
                    confidence.label()
                );
            }
        }

        // 0056 AR04: recovery tells the truth — policy, the durable
        // watermark, consent and failures; never a bare "enabled" flag.
        let status = self.recovery_status();
        text.push_str("\n[recovery]\n");
        if status.memory_only {
            text.push_str(
                "  policy      memory-only — drafts are NOT durable; nothing is persisted\n",
            );
        } else {
            text.push_str("  policy      automatic — dirty local documents and scratch drafts checkpoint to private state storage\n");
        }
        match status.durable_cohort {
            Some((cohort, ms)) => {
                let _ = writeln!(
                    text,
                    "  checkpoint  durable cohort {cohort} captured at {ms} ({} draft(s))",
                    status.durable_records
                );
            }
            None => text.push_str("  checkpoint  none completed this session\n"),
        }
        if status.in_flight || status.queued {
            text.push_str("  checkpoint  publication in progress — the guarantee is the last COMPLETED cohort\n");
        }
        let _ = writeln!(
            text,
            "  remote      {}",
            if status.consent_remote {
                "session consent granted — remote drafts persist"
            } else {
                "no consent — remote/sensitive drafts are not persisted (:recover consent remote)"
            }
        );
        if !status.over_bound.is_empty() {
            let _ = writeln!(
                text,
                "  over-limit  {} draft(s) exceed the 16 MiB capture limit — not durable",
                status.over_bound.len()
            );
        }
        if let Some(error) = &status.last_error {
            let _ = writeln!(text, "  last error  {error}");
        }

        // 0056 AR08: the effect/privacy classification actually in force
        // for the current document's target — from the typed classifier,
        // never a restated policy file.
        let policy = self.effect_policy();
        let target = super::privacy::target_of(&self.cur().source);
        text.push_str("\n[effects]\n");
        let _ = writeln!(text, "  target        {}", target.label());
        for family in super::privacy::EffectFamily::ALL {
            let class = super::privacy::classify(&policy, family, target);
            let capture = match class.capture {
                strop_trace::ContentPolicy::Full => "full content",
                strop_trace::ContentPolicy::Metadata => "metadata only",
            };
            let _ = writeln!(
                text,
                "  {:<13} {:<14} {}",
                family.label(),
                capture,
                class.reason
            );
        }
        let admitted = super::privacy::persistence_admitted(&policy, target);
        let _ = writeln!(
            text,
            "  persistence   {}",
            if admitted {
                "drafts admitted to private state storage".to_string()
            } else if target == super::privacy::EffectTarget::Ssh {
                "remote drafts not persisted without :recover consent remote".to_string()
            } else {
                "container bytes are never draft-persisted".to_string()
            }
        );
        if !strop_trace::enabled() {
            text.push_str("  (no trace active — capture policy applies when recording)\n");
        }

        let mut buffer = strop_core::Buffer::from_text(&text);
        buffer.name = Some("explain".into());
        // a temporary surface (0051 §7 R07): ctrl-o AND `:q` restore
        // the exact view the user came from, like :help
        let _ = self.open_temporary_output(buffer);
    }
    /// The true source of the current buffer's readonly policy (0056
    /// AR14): the typed terminal owner first, then the recorded reason
    /// — never a generic hint.
    fn readonly_explanation(&self) -> &'static str {
        if self.terminal_document(self.current()).is_some() {
            return "terminal session — the child program owns the output; i enters child input";
        }
        match self.buf().readonly_reason {
            Some(strop_core::ReadonlyReason::Filesystem) => {
                "filesystem permissions report not writable — :set noro to edit anyway, :w! to force a write"
            }
            Some(strop_core::ReadonlyReason::Command) => {
                "set by :set ro / :view — :set noro restores editing"
            }
            Some(strop_core::ReadonlyReason::RemoteAuthority) => {
                "remote snapshot without write authority — :remote edit grants it"
            }
            Some(strop_core::ReadonlyReason::Container) => {
                "container file — container bytes have no local write path"
            }
            Some(strop_core::ReadonlyReason::GitSurface) => {
                "git surface — content is derived from history"
            }
            Some(strop_core::ReadonlyReason::Output) => "transient output view — not a source",
            Some(strop_core::ReadonlyReason::DirectoryListing) => {
                "directory listing — :fs edit opens filename drafts"
            }
            Some(strop_core::ReadonlyReason::DirectoryOperation) => {
                "directory operation in flight — editing resumes when it lands"
            }
            Some(strop_core::ReadonlyReason::CollectionProjection) => {
                "collection projection failed — this stale view refuses edits"
            }
            Some(strop_core::ReadonlyReason::RecoveryCheckpoint) => {
                "recovery checkpoint surface — :recover restore N opens a checked draft"
            }
            Some(strop_core::ReadonlyReason::OutsideWorkspace) => {
                "outside the workspace root — :set noro to edit"
            }
            None => "set directly — :set noro restores editing",
        }
    }
}

/// A refusal with its actionable reason — the install hint or the trust
/// command, not just a label.
pub(crate) fn explain_decision(decision: &AttachDecision) -> String {
    match decision {
        AttachDecision::TrustRequired { command } => {
            format!("project config wants `{command}` — :trust to allow")
        }
        AttachDecision::TrustError { error } => format!("trust check failed: {error}"),
        AttachDecision::NotExecutable {
            command, reason, ..
        } => {
            format!("`{command}` not executable: {reason}")
        }
        AttachDecision::SpawnFailed { reason } => format!("spawn failed: {reason}"),
        AttachDecision::RemoteIo { reason } => format!("remote discovery failed: {reason}"),
        other => other.label().replace('_', " "),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use strop_core::Buffer;

    #[test]
    fn explain_lists_workspaces_and_config() {
        let mut e = Editor::new(Buffer::from_text("x\n"));
        e.feed_text(":explain\r");
        let text = e.buf().text().to_string();
        assert!(text.contains("[workspaces]"), "{text}");
        assert!(text.contains("local"), "{text}");
        assert!(text.contains("tab_size = 4"), "{text}");
        assert!(text.contains("lsp       not bound"), "{text}");
        assert!(e.buf().readonly, "explain is a real readonly buffer");
        e.feed_text("/[language servers]\r");
        assert!(e.head() > 0, "searchable like any buffer");
    }
    /// 0051 R08: the indentation section shows the effective setting,
    /// per-side provenance, the override, and detection's confidence —
    /// and the once-placeholder knobs show real values.
    #[test]
    fn explain_shows_indent_provenance_and_detection() {
        let mut e = Editor::new(Buffer::from_text(
            "fn f() {\n  let x = 1;\n  let y = 2;\n  let z = 3;\n  let w = 4;\n}\n",
        ));
        e.reresolve_indents();
        e.tab_size_command("8");
        e.open_explain();
        let text = e.buf().text().to_string();
        assert!(text.contains("[indentation]"), "{text}");
        assert!(text.contains("effective: Spaces:8"), "{text}");
        assert!(text.contains("style detected"), "{text}");
        assert!(text.contains("width manual"), "{text}");
        assert!(text.contains("override: width 8"), "{text}");
        assert!(text.contains("detection: spaces, width 2"), "{text}");
        assert!(text.contains("high confidence"), "{text}");
        assert!(text.contains("indent_style = spaces"), "{text}");
        assert!(text.contains("indent_detect = true"), "{text}");
        assert!(text.contains("auto_format = true"), "{text}");
        assert!(text.contains("search_show_hidden = true"), "{text}");
        assert!(text.contains("search_respect_ignore = true"), "{text}");
        assert!(!text.contains("= ?"), "no placeholder values: {text}");
    }
    /// 0056 AR14: a file whose permissions report not writable opens
    /// readonly, and :explain names the filesystem as the source.
    #[test]
    fn explain_names_filesystem_readonly_source() {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("locked.txt");
        std::fs::write(&path, "locked\n").unwrap();
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o444)).unwrap();
        let buffer = Buffer::open(&path).unwrap();
        assert_eq!(
            buffer.readonly_reason,
            Some(strop_core::ReadonlyReason::Filesystem)
        );
        let mut e = Editor::new(buffer);
        e.open_explain();
        let text = e.buf().text().to_string();
        assert!(
            text.contains("filesystem permissions report not writable"),
            "{text}"
        );
        assert!(!text.contains(":remote edit grants"), "{text}");
    }

    /// 0056 AR14: a remote snapshot names its authority source; AR08:
    /// the effects section follows the document's ssh target.
    #[test]
    fn explain_names_remote_readonly_source_and_ssh_target() {
        use crate::editor::document::RemoteDocument;
        let file = strop_workspace::RemoteFile::parse("ssh://fixture/work/file.txt").unwrap();
        let endpoint = file.endpoint().clone();
        let selection = strop_remote::ReadSelection::Full;
        let mut e = Editor::new(Buffer::from_text("local\n"));
        let document = e
            .docs
            .try_insert(crate::editor::Document::remote(
                Buffer::from_text("before\n"),
                RemoteDocument {
                    file,
                    window: strop_remote::RemoteWindow::resolve(
                        &selection,
                        strop_remote::RemoteSize::new(7),
                    ),
                    selection,
                    connection: None,
                    return_to: None,
                    write: None,
                },
            ))
            .unwrap();
        e.workspaces
            .bind(strop_workspace::Filesystem::Remote(endpoint), None)
            .unwrap();
        e.switch_to(document);
        e.open_explain();
        let text = e.buf().text().to_string();
        assert!(
            text.contains("remote snapshot without write authority — :remote edit grants it"),
            "{text}"
        );
        assert!(text.contains("target        ssh"), "{text}");
        assert!(
            text.contains("SFTP browsing only; :remote worker [URI] for services"),
            "{text}"
        );
        assert!(
            text.contains("remote drafts not persisted without :recover consent remote"),
            "{text}"
        );
    }

    /// 0056 AR14: `:set ro` is its own source — not a remote hint.
    #[test]
    fn explain_names_command_readonly_source() {
        let mut e = Editor::new(Buffer::from_text("x\n"));
        e.feed_text(":set ro\r");
        e.open_explain();
        let text = e.buf().text().to_string();
        assert!(text.contains("set by :set ro / :view"), "{text}");
        assert!(!text.contains(":remote edit grants"), "{text}");
    }

    /// 0056 AR14: the recovery checkpoint surface names itself.
    #[test]
    fn explain_names_recovery_surface_readonly_source() {
        let dir = tempfile::tempdir().unwrap();
        let mut e = Editor::new_in(Buffer::from_text(""), dir.path().to_path_buf());
        e.feed_text(":recover\r");
        assert_eq!(
            e.buf().readonly_reason,
            Some(strop_core::ReadonlyReason::RecoveryCheckpoint)
        );
        e.open_explain();
        let text = e.buf().text().to_string();
        assert!(text.contains("recovery checkpoint surface"), "{text}");
    }

    /// 0056 AR14: a terminal session names the child program as the
    /// owner of its output — the typed terminal source, not the generic
    /// output reason the document was built with.
    #[test]
    fn explain_names_terminal_readonly_source() {
        let dir = tempfile::tempdir().unwrap();
        let mut e = Editor::new_in(Buffer::from_text("origin"), dir.path().to_path_buf());
        e.terminal_fixture(
            &[("safe", strop_terminal::model::Style::default())],
            strop_terminal::model::Phase::Exited {
                code: Some(0),
                signal: None,
            },
        );
        e.open_explain();
        let text = e.buf().text().to_string();
        assert!(
            text.contains("terminal session — the child program owns the output"),
            "{text}"
        );
    }

    /// 0056 AR14: config provenance shows the actual winning layer per
    /// knob — the user file with its path where it won, default else.
    #[test]
    fn explain_shows_config_provenance_layers() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "tab_size = 8\n").unwrap();
        let (config, error) = crate::config::Config::load_from(&path);
        assert!(error.is_none());
        let mut e = Editor::new(Buffer::from_text("x\n"));
        e.config = config;
        e.open_explain();
        let text = e.buf().text().to_string();
        assert!(
            text.contains(&format!("tab_size = 8  [user {}]", path.display())),
            "{text}"
        );
        assert!(text.contains("indent_guides = true  [default]"), "{text}");
        assert!(text.contains("cursor_fade = true  [default]"), "{text}");
    }

    /// 0056 AR08: the effects section renders the classifier's actual
    /// decisions for the local target with no opt-ins granted.
    #[test]
    fn explain_lists_effect_classification() {
        let mut e = Editor::new(Buffer::from_text("x\n"));
        e.open_explain();
        let text = e.buf().text().to_string();
        assert!(text.contains("[effects]"), "{text}");
        assert!(text.contains("target        local"), "{text}");
        assert!(
            text.contains("clipboard     metadata only  clipboard payloads never enter"),
            "{text}"
        );
        assert!(
            text.contains("process       metadata only  command lines and environment"),
            "{text}"
        );
        assert!(
            text.contains("persistence   drafts admitted to private state storage"),
            "{text}"
        );
        assert!(text.contains("no trace active"), "{text}");
    }
}