thirdshift 0.22.0

Turn a GitHub issue into a ready-for-review pull request, unattended
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
//! One native filesystem observer for the Console, shared by all streams.
//! Watch directories so atomic replacements keep working. The slow fallback
//! recovers lost notifications.
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, RwLock, Weak};
use std::time::{Duration, SystemTime};

use anyhow::Result;
use notify::{Event, EventKind, RecursiveMode, Watcher};
use tokio::sync::watch;

#[derive(Clone, Copy, Default, PartialEq)]
pub(super) struct Revision {
    pub commands: u64,
    pub accounts: u64,
    pub styles: u64,
    pub scripts: u64,
    executable: u64,
}

pub(super) type Receiver = watch::Receiver<Revision>;

#[derive(Clone)]
pub(super) struct Notifier {
    pending: watch::Sender<Revision>,
    following: Arc<Mutex<Weak<super::watching::Following>>>,
}

impl Notifier {
    pub fn commands(&self) {
        self.pending.send_modify(|revision| revision.commands += 1);
    }

    pub fn follow(&self, changes: Receiver) -> Arc<super::watching::Following> {
        let mut current = self.following.lock().unwrap();
        if let Some(following) = current.upgrade() {
            return following;
        }
        let following = Arc::new(super::watching::follow(self.clone(), changes));
        *current = Arc::downgrade(&following);
        following
    }
}

#[derive(PartialEq)]
struct Registration {
    mode: RecursiveMode,
    created: Option<SystemTime>,
    inode: (u64, u64),
}

impl Registration {
    fn at(path: &std::path::Path, mode: RecursiveMode) -> std::io::Result<Self> {
        let metadata = std::fs::metadata(path)?;
        Ok(Self {
            mode,
            created: metadata.created().ok(),
            inode: {
                use std::os::unix::fs::MetadataExt;
                (metadata.dev(), metadata.ino())
            },
        })
    }
}

struct Paths {
    root: PathBuf,
    logs: PathBuf,
    other_logs: Vec<PathBuf>,
    executable: PathBuf,
    assets: Option<PathBuf>,
}

impl Paths {
    fn new(root: PathBuf, logs: PathBuf, executable: PathBuf) -> Self {
        Self {
            root: observed_path(&root),
            logs: observed_path(&logs),
            other_logs: Vec::new(),
            executable: observed_path(&executable),
            assets: None,
        }
    }

    fn notice(&self, result: notify::Result<Event>, revision: &mut Revision) {
        let event = match result {
            Ok(event) if matches!(event.kind, EventKind::Access(_)) => return,
            Ok(event) if !event.need_rescan() => event,
            error => {
                if let Err(error) = error {
                    eprintln!("thirdshift Console: filesystem observer: {error}");
                }
                revision.commands += 1;
                revision.accounts += 1;
                revision.executable += 1;
                if self.assets.is_some() {
                    revision.styles += 1;
                    revision.scripts += 1;
                }
                return;
            }
        };
        for path in event.paths {
            if crate::logs::github::bookkeeping(&path, &self.logs) {
                continue;
            }
            // Parent-directory mtimes change for unrelated children and our
            // own account/log writes. File events carry the actual change.
            if matches!(
                event.kind,
                EventKind::Modify(notify::event::ModifyKind::Metadata(_))
            ) && path.is_dir()
            {
                continue;
            }
            if let Some(directory) = &self.assets
                && (path.starts_with(directory) || directory.starts_with(&path))
            {
                match path.extension().and_then(|ext| ext.to_str()) {
                    Some("js") => revision.scripts += 1,
                    Some("css" | "woff2" | "woff" | "otf" | "ttf") => revision.styles += 1,
                    _ if path == *directory
                        || path.is_dir()
                        || matches!(
                            event.kind,
                            EventKind::Remove(notify::event::RemoveKind::Folder)
                        ) =>
                    {
                        revision.styles += 1;
                        revision.scripts += 1;
                    }
                    _ => {}
                }
            }
            if path == self.root.join("config.toml") || self.root.starts_with(&path) {
                revision.commands += 1;
                revision.accounts += 1;
            }
            if path == self.root.join("console-accounts.json") {
                revision.accounts += 1;
            }
            if path.starts_with(&self.logs)
                || self.logs.starts_with(&path)
                || self
                    .other_logs
                    .iter()
                    .any(|root| path.starts_with(root) || root.starts_with(&path))
                || path.starts_with(self.root.join("console"))
            {
                revision.commands += 1;
            }
            if path == self.executable {
                revision.executable += 1;
            }
        }
    }

    fn watches(&self) -> BTreeMap<PathBuf, RecursiveMode> {
        let mut paths = BTreeMap::new();
        for (path, recursive) in [
            (self.root.clone(), RecursiveMode::NonRecursive),
            (self.root.join("console"), RecursiveMode::Recursive),
            (self.logs.clone(), RecursiveMode::Recursive),
            (
                self.executable.parent().unwrap().to_path_buf(),
                RecursiveMode::NonRecursive,
            ),
        ]
        .into_iter()
        .chain(
            self.other_logs
                .iter()
                .map(|path| (path.clone(), RecursiveMode::Recursive)),
        )
        .chain(
            self.assets
                .iter()
                .map(|path| (path.clone(), RecursiveMode::Recursive)),
        ) {
            // An absent log root is normal. Observe its nearest existing parent
            // until it is created, then move the recursive watch onto the root.
            let ancestor = path.ancestors().find(|path| path.is_dir()).unwrap();
            let mode = if ancestor == path {
                recursive
            } else {
                RecursiveMode::NonRecursive
            };
            paths
                .entry(ancestor.to_path_buf())
                .and_modify(|old| {
                    if mode == RecursiveMode::Recursive {
                        *old = mode;
                    }
                })
                .or_insert(mode);
            // Retain a parent watch when the root itself is replaced.
            if ancestor == path
                && let Some(parent) = path.parent()
            {
                paths
                    .entry(parent.to_path_buf())
                    .or_insert(RecursiveMode::NonRecursive);
            }
        }
        paths
    }
}

// Native backends may report canonical paths.
// Resolve the existing ancestor so roots created later use the same spelling.
pub(super) fn observed_path(path: &std::path::Path) -> PathBuf {
    for ancestor in path.ancestors() {
        if let Ok(canonical) = ancestor.canonicalize() {
            return canonical.join(path.strip_prefix(ancestor).unwrap());
        }
    }
    path.to_path_buf()
}

pub(super) fn observe(
    config: &crate::config::UserConfig,
    executable: &super::restart::Executable,
    assets: &super::assets::Development,
) -> Result<(Receiver, Notifier, tokio::task::JoinHandle<()>)> {
    let mut paths = Paths::new(
        crate::config::home()?.join(".thirdshift"),
        config.logs_dir.clone(),
        executable.path.clone(),
    );
    paths.assets = assets.directory().map(PathBuf::from);
    paths.other_logs = crate::logs::accounts::discover(config)
        .logs
        .into_iter()
        .skip(1)
        .map(|source| observed_path(&source.root))
        .collect();
    let paths = Arc::new(RwLock::new(paths));
    let (pending, mut notifications) = watch::channel(Revision::default());
    let notifier = Notifier {
        pending: pending.clone(),
        following: Arc::default(),
    };
    let observed = paths.clone();
    let enqueue = move |event| {
        pending.send_if_modified(|revision| {
            let before = *revision;
            observed.read().unwrap().notice(event, revision);
            before != *revision
        });
    };
    let mut watcher = notify::recommended_watcher(enqueue.clone())?;
    // Recovery compares metadata rather than making every viewer render again
    // each minute. Its scan runs on notify's thread and never reads contents.
    let mut recovery =
        notify::PollWatcher::new(enqueue, notify::Config::default().with_manual_polling())?;
    let mut watched = BTreeMap::new();
    let mut recovered = BTreeMap::new();
    reconcile(&mut watcher, &mut watched, &paths.read().unwrap());
    reconcile(&mut recovery, &mut recovered, &paths.read().unwrap());
    let (sender, changes) = watch::channel(Revision::default());
    let source_changes = notifier.clone();
    let observer = tokio::spawn(async move {
        let mut previous = Revision::default();
        let mut recovery_tick = tokio::time::interval(Duration::from_secs(60));
        recovery_tick.tick().await;
        loop {
            let fallback = tokio::select! {
                _ = recovery_tick.tick() => true,
                result = notifications.changed() => {
                    if result.is_err() { return; }
                    false
                }
            };
            // Coalesce an atomic save and bursts of log appends into one notice.
            if !fallback {
                tokio::time::sleep(Duration::from_millis(100)).await;
            }
            if fallback && let Err(error) = recovery.poll() {
                eprintln!("thirdshift Console: filesystem recovery: {error}");
            }
            let revision = *notifications.borrow_and_update();
            if (fallback || revision.accounts != previous.accounts)
                && let Ok(config) = crate::config::UserConfig::load()
            {
                let mut paths = paths.write().unwrap();
                paths.logs = observed_path(&config.logs_dir);
                let sources = crate::logs::accounts::discover(&config)
                    .logs
                    .into_iter()
                    .skip(1)
                    .map(|source| observed_path(&source.root))
                    .collect::<Vec<_>>();
                if paths.other_logs != sources {
                    paths.other_logs = sources;
                    source_changes.commands();
                }
            }
            reconcile(&mut watcher, &mut watched, &paths.read().unwrap());
            reconcile(&mut recovery, &mut recovered, &paths.read().unwrap());
            sender.send_if_modified(|current| {
                let before = *current;
                if revision.commands != previous.commands {
                    current.commands += 1;
                }
                // Session expiry follows wall time even when no file changes.
                if fallback || revision.accounts != previous.accounts {
                    current.accounts += 1;
                }
                if revision.executable != previous.executable {
                    current.executable += 1;
                }
                if revision.styles != previous.styles {
                    current.styles += 1;
                }
                if revision.scripts != previous.scripts {
                    current.scripts += 1;
                }
                *current != before
            });
            previous = revision;
        }
    });
    Ok((changes, notifier, observer))
}

fn reconcile(
    watcher: &mut impl Watcher,
    watched: &mut BTreeMap<PathBuf, Registration>,
    paths: &Paths,
) {
    let desired = paths.watches();
    for (path, mode) in &desired {
        let Ok(registration) = Registration::at(path, *mode) else {
            continue;
        };
        if watched.get(path) != Some(&registration) {
            if watched.remove(path).is_some() {
                let _ = watcher.unwatch(path);
            }
            match watcher.watch(path, *mode) {
                Ok(()) => {
                    watched.insert(path.clone(), registration);
                }
                Err(error) => eprintln!(
                    "thirdshift Console: can't watch {} (using 60s fallback): {error}",
                    path.display()
                ),
            }
        }
    }
    watched.retain(|path, _| {
        if desired.contains_key(path) {
            return true;
        }
        let _ = watcher.unwatch(path);
        false
    });
}

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

    #[test]
    fn canonical_backend_events_match_paths_under_a_symlinked_home() {
        let temp = tempfile::tempdir().unwrap();
        let real = temp.path().join("real");
        std::fs::create_dir(&real).unwrap();
        let alias = temp.path().join("alias");
        std::os::unix::fs::symlink(&real, &alias).unwrap();
        let root = alias.join(".thirdshift");
        std::fs::create_dir(&root).unwrap();
        // The log root need not exist yet. Canonical events must still match
        // paths beneath the symlinked home.
        let paths = Paths::new(root.clone(), root.join("logs"), alias.join("thirdshift"));
        let canonical = real.canonicalize().unwrap().join(".thirdshift");
        let mut revision = Revision::default();
        for file in [
            "config.toml",
            "console-accounts.json",
            "logs/new.command.json",
        ] {
            paths.notice(
                Ok(Event::new(EventKind::Any).add_path(canonical.join(file))),
                &mut revision,
            );
        }
        assert_eq!(revision.accounts, 2);
        assert_eq!(revision.commands, 2);
    }

    #[test]
    fn directory_metadata_alone_does_not_invalidate_the_inbox() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join(".thirdshift");
        std::fs::create_dir(&root).unwrap();
        let paths = Paths::new(
            root.clone(),
            root.join("logs"),
            temp.path().join("thirdshift"),
        );
        let mut revision = Revision::default();
        for path in [
            temp.path().canonicalize().unwrap(),
            root.canonicalize().unwrap(),
        ] {
            paths.notice(
                Ok(
                    Event::new(EventKind::Modify(notify::event::ModifyKind::Metadata(
                        notify::event::MetadataKind::WriteTime,
                    )))
                    .add_path(path),
                ),
                &mut revision,
            );
        }
        assert!(revision == Revision::default());
    }
}