lazyspec 0.8.0

A little TUI & CLI for project documentation.
Documentation
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
use crate::engine::config::{Config, StoreBackend};
use crate::engine::document::split_frontmatter;
use crate::engine::gh::GhCli;
use crate::engine::git_ref::GitCli;
use crate::engine::git_ref_store::GitRefStore;
use crate::engine::issue_cache::IssueCache;
use crate::engine::issue_map::IssueMap;
use crate::engine::store::Store;
use crate::engine::store_dispatch::{DocumentStore, GithubIssuesStore};
use crate::tui::content;
use crate::tui::infra::{perf_log, terminal_caps};
use crate::tui::state::App;
use crate::tui::state::AppEvent;
use crate::tui::views;
use anyhow::Result;
use crossterm::{
    event::{Event, KeyEventKind},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use notify::{EventKind, RecursiveMode, Watcher};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use std::path::Path;
use std::process::Command;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

// Discard any pending crossterm events buffered in stdin. Called after a child
// process (editor, agent) exits to drop bytes that may have arrived during the
// subprocess but were not consumed by it. Caller must hold the stdin lock so the
// input thread does not race the reads.
fn drain_stdin() {
    while let Ok(true) = crossterm::event::poll(Duration::from_millis(0)) {
        let _ = crossterm::event::read();
    }
}

fn run_editor(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, path: &Path) -> Result<()> {
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    disable_raw_mode()?;

    let editor = crate::tui::state::resolve_editor();
    let status = Command::new(&editor).arg(path).status();

    enable_raw_mode()?;
    execute!(terminal.backend_mut(), EnterAlternateScreen)?;
    terminal.clear()?;

    if let Err(e) = status {
        eprintln!("Failed to launch editor '{}': {}", editor, e);
    }

    Ok(())
}

fn try_push_gh_edit(
    root: &Path,
    relative: &Path,
    config: &Config,
    shared_store: &Arc<Mutex<GithubIssuesStore<GhCli>>>,
) -> Result<(), String> {
    let content = std::fs::read_to_string(root.join(relative))
        .map_err(|e| format!("failed to read edited file: {e}"))?;

    let (_yaml, body) =
        split_frontmatter(&content).map_err(|e| format!("failed to parse edited file: {e}"))?;

    let store = Store::load(root, config).map_err(|e| e.to_string())?;
    let doc = store
        .get(relative)
        .ok_or_else(|| "document not found in store".to_string())?;
    let doc_id = doc.id.clone();
    let type_name = doc.doc_type.as_str().to_string();

    let type_def = config
        .type_by_name(&type_name)
        .ok_or_else(|| format!("type '{}' not found in config", type_name))?;

    if type_def.store != StoreBackend::GithubIssues {
        return Ok(());
    }

    let body_trimmed = body.trim();
    let mut gh_store = shared_store
        .lock()
        .map_err(|e| format!("lock poisoned: {e}"))?;
    gh_store
        .update(type_def, &doc_id, &[("body", body_trimmed)])
        .map_err(|e| e.to_string())
}

fn try_push_git_ref_edit(root: &Path, relative: &Path, config: &Config) -> Result<(), String> {
    let store = Store::load(root, config).map_err(|e| e.to_string())?;
    let doc = store
        .get(relative)
        .ok_or_else(|| "document not found in store".to_string())?;
    let doc_id = doc.id.clone();
    let type_name = doc.doc_type.as_str().to_string();

    let type_def = config
        .type_by_name(&type_name)
        .ok_or_else(|| format!("type '{}' not found in config", type_name))?;

    if type_def.store != StoreBackend::GitRef {
        return Ok(());
    }

    let mut git_store = GitRefStore {
        git: GitCli,
        root: root.to_path_buf(),
        config: config.clone(),
        reserved_number: None,
    };
    git_store
        .update(type_def, &doc_id, &[])
        .map_err(|e| e.to_string())
}

fn handle_app_event(app: &mut App, event: AppEvent, root: &Path, config: &Config) {
    match event {
        AppEvent::Terminal(key) => {
            app.handle_key(key.code, key.modifiers, root, config);
        }
        AppEvent::FileChange(event) => match event.kind {
            EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) => {
                let mut has_non_md = false;
                for path in &event.paths {
                    if path.extension().and_then(|e| e.to_str()) == Some("md") {
                        if let Ok(relative) = path.strip_prefix(root) {
                            let _ = app.store.reload_file(root, relative, &*app.fs);
                            app.expanded_body_cache.remove(relative);
                            app.disk_cache.invalidate(relative);
                        }
                    } else {
                        has_non_md = true;
                    }
                }
                if has_non_md {
                    app.expanded_body_cache.clear();
                    app.disk_cache.clear();
                }
                app.refresh_validation(config);
                app.git_status_cache.invalidate();
            }
            _ => {}
        },
        AppEvent::ExpansionResult {
            path,
            body,
            body_hash,
        } => {
            if app.expansion_in_flight.as_ref() == Some(&path) {
                app.expansion_in_flight = None;
            }
            app.disk_cache.write(&path, body_hash, &body);
            app.expanded_body_cache.insert(path, body);
        }
        AppEvent::DiagramRendered { source_hash, entry } => {
            app.diagram_cache.insert(source_hash, entry);
        }
        AppEvent::CacheRefresh => {
            let root = app.store.root().to_path_buf();
            if let Ok(refreshed) = Store::load(&root, config) {
                app.store = refreshed;
            }
            app.last_sync = Some(Instant::now());
            app.filtered_docs_cache = None;
            app.rebuild_search_index();
            app.refresh_validation(config);
        }
        AppEvent::GhPushResult(result) => {
            app.gh_push_in_flight.store(false, Ordering::Relaxed);
            match result {
                Ok(()) => {
                    let root = app.store.root().to_path_buf();
                    if let Ok(refreshed) = Store::load(&root, config) {
                        app.store = refreshed;
                    }
                    app.filtered_docs_cache = None;
                    app.rebuild_search_index();
                    app.refresh_validation(config);
                    app.expanded_body_cache.clear();
                    app.disk_cache.clear();
                }
                Err(msg) => {
                    app.gh_conflict_message = Some(msg);
                }
            }
        }
        AppEvent::CreateStarted => {}
        AppEvent::CreateProgress { message } => {
            if app.create_form.active && app.create_form.loading {
                app.create_form.status_message = Some(message);
            }
        }
        AppEvent::CreateComplete { result } => {
            if !app.create_form.active {
                return;
            }
            match result {
                Ok(create_result) => {
                    let _ = app.store.reload_file(root, &create_result.path, &*app.fs);
                    app.filtered_docs_cache = None;
                    app.rebuild_search_index();
                    if let Some(type_idx) = app
                        .doc_types
                        .iter()
                        .position(|t| *t == create_result.doc_type)
                    {
                        app.selected_type = type_idx;
                        app.build_doc_tree();
                        if let Some(doc_idx) = app
                            .doc_tree
                            .iter()
                            .position(|n| n.path == create_result.path)
                        {
                            app.selected_doc = doc_idx;
                        }
                    }
                    app.close_create_form();
                    app.refresh_validation(config);
                    app.git_status_cache.invalidate();
                    app.gh_issue_map_stale = true;
                }
                Err(msg) => {
                    app.create_form.loading = false;
                    app.create_form.error = Some(msg);
                    app.create_form.status_message = None;
                }
            }
        }
        #[cfg(feature = "agent")]
        AppEvent::AgentFinished => {}
    }
}

pub fn run(store: Store, config: &Config) -> Result<()> {
    // Probe terminal capabilities BEFORE entering raw mode and spawning the input thread.
    // `Picker::from_query_stdio` reads stdin directly to capture terminal capability responses;
    // running it concurrently with the crossterm input thread races for stdin and silently
    // consumes user keystrokes (the parser eats bytes looking for the DSR Status terminator).
    let picker = terminal_caps::create_picker();
    let protocol = terminal_caps::TerminalImageProtocol::from(picker.protocol_type());
    let tool_availability = content::diagram::ToolAvailability::detect();

    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let mut app = App::new(
        store,
        config,
        picker,
        Box::new(crate::engine::fs::RealFileSystem),
    );
    app.terminal_image_protocol = protocol;
    app.tool_availability = tool_availability;
    app.refresh_validation(config);

    let (tx, rx) = crossbeam_channel::unbounded();
    app.event_tx = tx.clone();

    let has_gh_types = config
        .documents
        .types
        .iter()
        .any(|t| t.store == StoreBackend::GithubIssues);

    let shared_gh_store: Option<Arc<Mutex<GithubIssuesStore<GhCli>>>> = if has_gh_types {
        let gh_config = config.documents.github.as_ref();
        let repo = gh_config.and_then(|g| g.repo.clone());
        repo.map(|repo| {
            let root = app.store.root();
            Arc::new(Mutex::new(GithubIssuesStore {
                client: GhCli::new(),
                root: root.to_path_buf(),
                repo,
                config: config.clone(),
                issue_map: IssueMap::load(root)
                    .unwrap_or_else(|_| serde_json::from_str("{}").unwrap()),
                issue_cache: IssueCache::new(root),
            }))
        })
    } else {
        None
    };

    let cache_ttl = config
        .documents
        .github
        .as_ref()
        .map(|g| g.cache_ttl)
        .unwrap_or(60);
    let mut next_poll = if shared_gh_store.is_some() {
        Some(Instant::now())
    } else {
        None
    };
    let refresh_in_flight = Arc::new(AtomicBool::new(false));

    let root = app.store.root().to_path_buf();
    let fs_tx = tx.clone();
    let mut _watcher = notify::recommended_watcher(move |res: notify::Result<notify::Event>| {
        if let Ok(event) = res {
            let _ = fs_tx.send(AppEvent::FileChange(event));
        }
    })?;

    let dirs: Vec<&str> = config
        .documents
        .types
        .iter()
        .map(|t| t.dir.as_str())
        .collect();
    for dir in &dirs {
        let full = root.join(dir);
        if full.exists() {
            _watcher.watch(&full, RecursiveMode::NonRecursive)?;
        }
    }

    // Dedicated terminal input thread: sends key events through the unified channel.
    // The mutex enforces single-reader ownership of stdin: the main thread acquires it
    // before disabling raw mode for an external editor / subprocess, guaranteeing that
    // no concurrent crossterm poll consumes bytes meant for the child process.
    let stdin_lock = Arc::new(Mutex::new(()));
    let term_tx = tx.clone();
    let thread_stdin_lock = stdin_lock.clone();
    std::thread::spawn(move || loop {
        let _guard = thread_stdin_lock.lock().unwrap();
        if let Ok(true) = crossterm::event::poll(Duration::from_millis(50)) {
            if let Ok(Event::Key(key)) = crossterm::event::read() {
                if key.kind == KeyEventKind::Press {
                    perf_log::log(&format!("input_thread: read key {:?}", key.code));
                    let _ = term_tx.send(AppEvent::Terminal(key));
                    perf_log::log("input_thread: sent to channel");
                }
            }
        }
        drop(_guard);
        std::thread::yield_now();
    });

    let mut loop_count: u64 = 0;
    loop {
        let loop_start = Instant::now();

        let t = Instant::now();
        terminal.draw(|f| views::draw(f, &mut app, config))?;
        perf_log::log_duration("draw", t);

        let t = Instant::now();
        app.request_expansion(&tx);

        if let Some(meta) = app.selected_doc_meta() {
            if let Some(body) = app.expanded_body_cache.get(&meta.path) {
                let body_hash = crate::engine::cache::DiskCache::body_hash(body);
                let blocks = match &app.diagram_blocks_cache {
                    Some((p, h, b)) if p == &meta.path && *h == body_hash => b.clone(),
                    _ => {
                        let b = content::diagram::extract_diagram_blocks(body);
                        app.diagram_blocks_cache = Some((meta.path.clone(), body_hash, b.clone()));
                        b
                    }
                };
                for block in &blocks {
                    app.request_diagram_render(block, &tx);
                }
            }
        }
        perf_log::log_duration("between_frames", t);

        #[cfg(feature = "agent")]
        app.agent_spawner.poll_finished();

        let t = Instant::now();
        match rx.recv_timeout(Duration::from_millis(16)) {
            Ok(event) => {
                perf_log::log_duration("recv_wait", t);
                let t2 = Instant::now();
                let mut event_count = 1u32;
                handle_app_event(&mut app, event, &root, config);
                while let Ok(event) = rx.try_recv() {
                    event_count += 1;
                    handle_app_event(&mut app, event, &root, config);
                }
                perf_log::log_duration(&format!("handle_events({})", event_count), t2);
            }
            Err(_) => {
                perf_log::log_duration("recv_timeout", t);
            }
        }

        if app.gh_issue_map_stale {
            if let Some(ref shared_store) = shared_gh_store {
                if let Ok(mut guard) = shared_store.lock() {
                    if let Ok(map) = IssueMap::load(&root) {
                        guard.issue_map = map;
                    }
                }
            }
            app.gh_issue_map_stale = false;
        }

        if let (Some(deadline), Some(ref shared_store)) = (next_poll, &shared_gh_store) {
            if Instant::now() >= deadline && !refresh_in_flight.load(Ordering::Relaxed) {
                refresh_in_flight.store(true, Ordering::Relaxed);
                next_poll = Some(Instant::now() + Duration::from_secs(cache_ttl));
                let poll_tx = tx.clone();
                let poll_root = root.clone();
                let poll_config = config.clone();
                let poll_flag = refresh_in_flight.clone();
                let poll_store = Arc::clone(shared_store);
                std::thread::spawn(move || {
                    let gh_types: Vec<_> = poll_config
                        .documents
                        .types
                        .iter()
                        .filter(|t| t.store == StoreBackend::GithubIssues)
                        .collect();
                    let all_type_names: Vec<String> = poll_config
                        .documents
                        .types
                        .iter()
                        .map(|t| t.name.clone())
                        .collect();
                    let client = GhCli::new();
                    let mut guard = poll_store.lock().unwrap();
                    let store = &mut *guard;
                    for type_def in &gh_types {
                        if let Err(e) = store.issue_cache.fetch_all(
                            &poll_root,
                            type_def,
                            &client,
                            &store.repo,
                            &mut store.issue_map,
                            &all_type_names,
                        ) {
                            eprintln!("cache refresh failed for {}: {}", type_def.name, e);
                        }
                    }
                    let _ = store.issue_map.save(&poll_root);
                    drop(guard);
                    poll_flag.store(false, Ordering::Relaxed);
                    let _ = poll_tx.send(AppEvent::CacheRefresh);
                });
            }
        }

        loop_count += 1;
        if perf_log::enabled() && loop_count.is_multiple_of(60) {
            perf_log::log(&format!("--- loop #{} ---", loop_count));
        }
        perf_log::log_duration("loop_total", loop_start);

        if let Some(path) = app.editor_request.take() {
            let _stdin_guard = stdin_lock.lock().unwrap();
            while rx.try_recv().is_ok() {}
            run_editor(&mut terminal, &path)?;
            drain_stdin();
            while rx.try_recv().is_ok() {}
            drop(_stdin_guard);
            let root = app.store.root().to_path_buf();
            if let Ok(relative) = path.strip_prefix(&root) {
                let _ = app.store.reload_file(&root, relative, &*app.fs);
                app.expanded_body_cache.remove(relative);
                app.disk_cache.invalidate(relative);
                if let Some(ref shared_store) = shared_gh_store {
                    let push_root = root.clone();
                    let push_relative = relative.to_path_buf();
                    let push_config = config.clone();
                    let push_tx = tx.clone();
                    let push_flag = app.gh_push_in_flight.clone();
                    let push_store = Arc::clone(shared_store);
                    push_flag.store(true, Ordering::Relaxed);
                    std::thread::spawn(move || {
                        let result =
                            try_push_gh_edit(&push_root, &push_relative, &push_config, &push_store);
                        push_flag.store(false, Ordering::Relaxed);
                        let _ = push_tx.send(AppEvent::GhPushResult(result));
                    });
                }
                {
                    let push_root = root.clone();
                    let push_relative = relative.to_path_buf();
                    let push_config = config.clone();
                    let push_tx = tx.clone();
                    std::thread::spawn(move || {
                        let result =
                            try_push_git_ref_edit(&push_root, &push_relative, &push_config);
                        if let Err(msg) = result {
                            let _ = push_tx.send(AppEvent::GhPushResult(Err(msg)));
                        }
                    });
                }
            }
            app.refresh_validation(config);
        }

        #[cfg(feature = "agent")]
        if let Some(session_id) = app.resume_request.take() {
            let _stdin_guard = stdin_lock.lock().unwrap();
            while rx.try_recv().is_ok() {}

            execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
            disable_raw_mode()?;
            let _ = Command::new("claude")
                .args(["--resume", &session_id])
                .status();
            enable_raw_mode()?;
            execute!(terminal.backend_mut(), EnterAlternateScreen)?;
            terminal.clear()?;

            drain_stdin();
            while rx.try_recv().is_ok() {}
            drop(_stdin_guard);
            let root = app.store.root().to_path_buf();
            app.store = Store::load(&root, config)?;
            app.refresh_validation(config);
        }

        if app.fix_request {
            app.fix_request = false;
            let root = app.store.root().to_path_buf();
            let paths: Vec<String> = app
                .store
                .parse_errors()
                .iter()
                .map(|e| e.path.to_string_lossy().to_string())
                .collect();
            let fs = crate::engine::fs::RealFileSystem;
            let output = crate::cli::fix::run_human(&root, &app.store, config, &paths, false, &fs);
            app.store = Store::load(&root, config)?;
            app.refresh_validation(config);
            app.fix_result = if output.is_empty() {
                None
            } else {
                Some(output)
            };
            app.warnings_selected = 0;
        }

        if app.should_quit {
            break;
        }
    }

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;

    Ok(())
}