strop-engine 0.43.1

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
//! Subscription lifecycle: state, guarded local/remote subscribe jobs,
//! and the checked read a guarded reload performs.
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::Arc;

use strop_core::id::DocumentId;
use strop_core::worker::{self, CancelReason, CancelToken, FailureKind, Outcome};
use strop_worker_protocol::message::NotifyCoverage;
use strop_worker_protocol::{Event, NotifyHint, Session, Subscription};
use strop_workspace::{Filesystem, ResourceLocation};

use super::super::directory::DirectoryTask;
use super::super::events::AppEvent;
use super::super::io::Opened;
use super::super::{Document, DocumentSource, Editor};
use super::{NotifyQueue, Record, ReloadKey, SubscribedScope};

/// The editor's notification state: subscription identity, coverage,
/// the in-flight guarded reloads and the honest coverage note.
pub(crate) struct NotifyState {
    pub(crate) queue: Arc<NotifyQueue>,
    pub(super) tx: Sender<Event>,
    pub(crate) rx: Option<Receiver<Event>>,
    /// The live subscription identity; events stamped otherwise are dead.
    pub(crate) subscription: Option<Subscription>,
    /// The worker session the subscription belongs to.
    pub(super) session: Option<Session>,
    pub(super) coverage: Option<NotifyCoverage>,
    pub(super) subscribing: bool,
    /// The subscribe job's cancel handle; retained until settle
    /// (dropping it cancels the job).
    pub(super) subscribe_handle: Option<worker::CancelHandle>,
    pub(super) reloads: HashMap<strop_core::worker::WorkerId, ReloadKey>,
    pub(super) reloading: HashMap<DocumentId, strop_core::worker::WorkerId>,
    /// Hints arrived while a reload was in flight: one re-observation
    /// owed at completion so a raced write is never stranded.
    pub(super) reload_again: HashSet<DocumentId>,
    /// Remote workspace scopes (WK07): one subscription per opened
    /// remote directory root on a worker-admitted endpoint.
    pub(super) remote: HashMap<ResourceLocation, RemoteScope>,
}

/// One remote workspace scope's subscription state. The subscription's
/// authority is its (session, scope, generation) identity exactly as
/// for the local scope; a dead worker incarnation's events are dropped.
pub(crate) struct RemoteScope {
    pub(super) endpoint: strop_workspace::RemoteEndpoint,
    pub(super) subscription: Option<Subscription>,
    pub(super) session: Option<Session>,
    pub(super) subscribing: bool,
    /// The subscribe job's cancel handle, retained until settle.
    pub(super) subscribe_handle: Option<worker::CancelHandle>,
}

impl Default for NotifyState {
    fn default() -> Self {
        let (tx, rx) = channel();
        Self {
            queue: Arc::new(NotifyQueue::new()),
            tx,
            rx: Some(rx),
            subscription: None,
            session: None,
            subscribe_handle: None,
            coverage: None,
            subscribing: false,
            reloads: HashMap::new(),
            reloading: HashMap::new(),
            reload_again: HashSet::new(),
            remote: HashMap::new(),
        }
    }
}

impl NotifyState {
    pub(crate) fn take_rx(&mut self) -> Option<Receiver<Event>> {
        self.rx.take()
    }

    /// Finite notify work in flight (guarded reloads). The subscribe
    /// attempt settles onto the notify queue, not the io channel, so it
    /// never parks an io drain.
    pub(crate) fn pending(&self) -> bool {
        !self.reloads.is_empty()
    }

    /// Push coverage over the scope, honestly reported (CoverageHonest):
    /// native/polling push, on-demand, or nothing.
    pub(crate) fn push_coverage(&self) -> bool {
        self.subscription.is_some()
            && matches!(
                self.coverage,
                Some(NotifyCoverage::Native) | Some(NotifyCoverage::Polling)
            )
    }
}

/// Observe and read one local file for a guarded reload: the same
/// checked observation/read family as open (WK09), riding the lease.
/// `None` means the resource vanished or stopped being a regular file —
/// an outcome, never a clobber.
pub(super) fn reload_run(
    worker: &strop_worker_client::Worker,
    path: &std::path::Path,
    cancel: &CancelToken,
) -> Outcome<Option<Opened>> {
    if cancel.is_cancelled() {
        return Outcome::Cancelled(CancelReason::OwnerClosed);
    }
    let location = ResourceLocation::local(path.to_path_buf());
    let observed = match worker.observe(cancel, vec![location.clone()]) {
        Ok(mut observations) => observations.pop().and_then(|located| located.value),
        Err(error) if error.is_cancellation() => {
            return Outcome::Cancelled(CancelReason::OwnerClosed)
        }
        Err(error) => return Outcome::failed(FailureKind::Io, error.to_string()),
    };
    let Some(observation) = observed else {
        return Outcome::Success(None);
    };
    if observation.kind != strop_workspace::EntryKind::File {
        return Outcome::Success(None);
    }
    let payload = match worker.read(cancel, location, 0, None) {
        Ok(payload) => payload,
        Err(error) if error.is_cancellation() => {
            return Outcome::Cancelled(CancelReason::OwnerClosed)
        }
        Err(error) => return Outcome::failed(FailureKind::Io, error.to_string()),
    };
    let rope = match ropey::Rope::from_reader(payload) {
        Ok(rope) => rope,
        Err(error) => return Outcome::failed(FailureKind::Io, error.to_string()),
    };
    let writable = observation
        .permissions
        .is_none_or(|permissions| permissions.bits() & 0o222 != 0);
    let stamp = observation
        .modified
        .map(super::super::io::open::filetime_to_systemtime);
    let canonical = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    Outcome::Success(Some(Opened {
        document: Document::new(strop_core::Buffer::from_read(
            path.to_path_buf(),
            rope,
            stamp,
            canonical.clone(),
            writable,
        )),
        canonical: crate::files::FileTarget::Local(canonical),
    }))
}

impl Editor {
    /// Subscribe the opened scope root (recursive; the worker adds the
    /// parent/name guards of the root itself) through the local worker
    /// lease. No-op under replay, while subscribed/subscribing, or when
    /// finishing. Never blocks input: the subscribe round trip rides a
    /// job, and the settle lands on the notify queue.
    pub(crate) fn start_notifications(&mut self) {
        if self.finishing
            || self.notify.subscribing
            || self.notify.subscription.is_some()
            || self.tape.is_replay()
        {
            return;
        }
        self.notify.subscribing = true;
        let worker = self.filesystem.worker().clone();
        worker.set_event_sink(self.notify.tx.clone());
        let scope = ResourceLocation::local(self.cwd.clone());
        let queue = Arc::clone(&self.notify.queue);
        let wake = self.app_tx.clone();
        let handle = worker::spawn(
            "fs-notify-subscribe",
            move |outcome| {
                queue.push_record(Record::Settled(outcome));
                if let Some(wake) = &wake {
                    let _ = wake.send(AppEvent::Notify);
                }
            },
            move |cancel| {
                let owned_trace = strop_trace::active_path().and_then(|path| {
                    std::fs::canonicalize(&scope.path).ok().and_then(|root| {
                        path.strip_prefix(root).ok().map(|relative| {
                            strop_workspace::addr::uri::path_bytes(relative).to_vec()
                        })
                    })
                });
                match worker.subscribe(&cancel, scope, true) {
                    Ok((subscription, coverage)) => Outcome::Success(SubscribedScope {
                        subscription,
                        coverage,
                        owned_trace,
                    }),
                    Err(error) if error.is_cancellation() => {
                        Outcome::Cancelled(CancelReason::OwnerClosed)
                    }
                    Err(error) => Outcome::failed(FailureKind::Unavailable, error.to_string()),
                }
            },
        );
        self.notify.subscribe_handle = Some(handle);
    }

    pub(crate) fn start_remote_notifications(&mut self) {
        // Hint timing is inherently racy, so the bit-exact full-content
        // tape lane (recording or replay) keeps remote freshness on
        // demand — hints are advisory and the feature is untraced by
        // design. Default sessions (no content capture) subscribe.
        if self.finishing || self.tape.observes() {
            return;
        }
        let roots: Vec<(ResourceLocation, strop_workspace::RemoteEndpoint)> = self
            .docs
            .iter()
            .filter_map(|(_, document)| {
                let directory = document.directory_metadata_ref()?;
                match &directory.location.filesystem {
                    Filesystem::Remote(endpoint) => {
                        Some((directory.location.clone(), endpoint.clone()))
                    }
                    _ => None,
                }
            })
            .collect();
        for (root, endpoint) in roots {
            if self
                .notify
                .remote
                .get(&root)
                .is_some_and(|scope| scope.subscribing || scope.subscription.is_some())
            {
                continue;
            }
            let Some(worker) = self.remote.workers.get(&endpoint) else {
                continue;
            };
            let tx = self.notify.tx.clone();
            let queue = Arc::clone(&self.notify.queue);
            let wake = self.app_tx.clone();
            let scope = self
                .notify
                .remote
                .entry(root.clone())
                .or_insert_with(|| RemoteScope {
                    endpoint,
                    subscription: None,
                    session: None,
                    subscribing: false,
                    subscribe_handle: None,
                });
            scope.subscribing = true;
            worker.worker().set_event_sink(tx);
            let scope_root = root.clone();
            let work_root = root.clone();
            let handle = worker::spawn(
                "remote-fs-notify-subscribe",
                move |outcome| {
                    queue.push_record(Record::RemoteSettled {
                        root: scope_root,
                        outcome,
                    });
                    if let Some(wake) = &wake {
                        let _ = wake.send(AppEvent::Notify);
                    }
                },
                move |cancel| match worker.subscribe(&cancel, work_root, true) {
                    Ok((subscription, coverage)) => Outcome::Success(SubscribedScope {
                        subscription,
                        coverage,
                        owned_trace: None,
                    }),
                    Err(error) if error.is_cancellation() => {
                        Outcome::Cancelled(CancelReason::OwnerClosed)
                    }
                    Err(error) => Outcome::failed(FailureKind::Unavailable, error.to_string()),
                },
            );
            if let Some(scope) = self.notify.remote.get_mut(&root) {
                scope.subscribe_handle = Some(handle);
            }
        }
    }

    /// Hints against one remote scope (WK07): remote Directory buffers
    /// reobserve through the same checked listing path (which routes
    /// through the endpoint's worker lease); a dirty remote document
    /// gains external-change state and is never clobbered; clean remote
    /// documents keep their follow-poll freshness until WK09's guarded
    /// remote reload.
    pub(super) fn apply_remote_hints(&mut self, root: &std::path::Path, hints: Vec<NotifyHint>) {
        use std::os::unix::ffi::OsStrExt;
        let mut directories = Vec::new();
        let mut dirty = Vec::new();
        for hint in hints {
            let relative = PathBuf::from(std::ffi::OsStr::from_bytes(&hint.path));
            if relative.as_os_str().is_empty() {
                // The scope root itself moved/was replaced: the whole
                // remote baseline is suspect.
                self.remote_notify_rescan(root);
                return;
            }
            let absolute = root.join(&relative);
            for (id, document) in self.docs.iter() {
                match &document.source {
                    DocumentSource::Directory(directory) => {
                        if !matches!(directory.location.filesystem, Filesystem::Remote(_)) {
                            continue;
                        }
                        let affects_listing = absolute == directory.location.path
                            || absolute.parent() == Some(directory.location.path.as_path());
                        if affects_listing && !directories.contains(&id) {
                            directories.push(id);
                        }
                    }
                    DocumentSource::Remote(source)
                        if source.file.path() == absolute
                            && document.buf.dirty
                            && !dirty.contains(&id) =>
                    {
                        dirty.push(id);
                    }
                    DocumentSource::Remote(_) => {}
                    _ => {}
                }
            }
        }
        for id in dirty {
            self.notify_mark_external(id);
        }
        for id in directories {
            let _ = self.start_directory_task(id, DirectoryTask::Reload, None);
        }
    }

    /// A conservative remote rescan obligation (overflow, queue bound,
    /// root replacement, lease loss): invalidate the remote baseline
    /// and reobserve before freshness is claimed again.
    pub(super) fn remote_notify_rescan(&mut self, root: &std::path::Path) {
        let mut directories = Vec::new();
        let mut dirty = Vec::new();
        for (id, document) in self.docs.iter() {
            match &document.source {
                DocumentSource::Directory(directory)
                    if matches!(directory.location.filesystem, Filesystem::Remote(_))
                        && directory.location.path.starts_with(root) =>
                {
                    directories.push(id);
                }
                DocumentSource::Remote(source)
                    if source.file.path().starts_with(root) && document.buf.dirty =>
                {
                    dirty.push(id);
                }
                DocumentSource::Remote(_) => {}
                _ => {}
            }
        }
        for id in dirty {
            self.notify_mark_external(id);
        }
        for id in directories {
            let _ = self.start_directory_task(id, DirectoryTask::Reload, None);
        }
    }

    /// Lease observation, per app event (cheap mutex reads, no I/O): a
    /// changed/absent session incarnation means the subscriptions died
    /// with it. Invalidate conservatively, surface, and reestablish
    /// coverage before freshness is claimed again (LOSS).
    /// Remote lease observation (WK07): a changed or dead incarnation
    /// kills its scopes' subscriptions; the scope baseline is
    /// invalidated, rescanned and reestablished before freshness is
    /// claimed again — identical to the local LOSS rule.
    pub(super) fn observe_remote_leases(&mut self) {
        let lost: Vec<ResourceLocation> = self
            .notify
            .remote
            .iter()
            .filter_map(|(root, scope)| {
                let session = scope.session?;
                let alive = self
                    .remote
                    .workers
                    .get(&scope.endpoint)
                    .and_then(|worker| worker.worker().session());
                (alive != Some(session)).then(|| root.clone())
            })
            .collect();
        let reestablish = !lost.is_empty();
        for root in lost {
            let path = root.path.clone();
            if let Some(scope) = self.notify.remote.get_mut(&root) {
                scope.subscription = None;
                scope.session = None;
            }
            self.remote_notify_rescan(&path);
            self.message =
                "remote filesystem notifications lost with the worker; reestablishing coverage"
                    .into();
        }
        if reestablish {
            self.start_remote_notifications();
        }
    }
}