codewhale-tui 0.9.2

Terminal UI for open-source and open-weight coding models
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
//! Off-event-loop Lane control submission (#1888, #4022).
//!
//! `/lane interrupt` performs Runtime teardown — `tmux kill-session`, worktree
//! TTL cleanup, an advisory lock — none of which may run on the TUI composer
//! thread. Making the verb CLI-only would have been a capability regression, so
//! the slash surface *submits* instead: validation and availability are decided
//! synchronously (so a bad id is still refused immediately), the work is handed
//! to a dedicated worker thread, and the caller gets a typed `queued` receipt
//! carrying a ticket. The terminal receipt — `transitioned`, `no_change`,
//! `conflict`, or `failed` — arrives under that same ticket and is drained by
//! the UI on its next tick.
//!
//! A `queued` receipt is never reported as success. It says exactly what has
//! happened: nothing yet.

use std::collections::VecDeque;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex};

use codewhale_lane::control::{
    ControlContext, ControlFailure, ControlFailureKind, ControlOperation, ControlReceipt,
    ControlSurface, execute_lane_control_in, parse_target,
};

/// Maximum submissions that may be in flight before the queue refuses.
///
/// Interrupts are operator-initiated keystrokes, not a stream: a handful of
/// pending teardowns is already pathological, and an unbounded queue would let
/// a stuck `tmux` call accumulate work the operator can neither see nor cancel.
pub const MAX_PENDING: usize = 16;

#[derive(Debug)]
struct Submission {
    ticket: String,
    operation: ControlOperation,
    raw_target: Option<String>,
    registry_root: Option<PathBuf>,
}

#[derive(Debug, Default)]
struct Shared {
    pending: VecDeque<Submission>,
    completed: Vec<ControlReceipt>,
    /// Tickets accepted but not yet completed. Bounds the queue and lets a
    /// duplicate interrupt of the same Lane be answered without re-queuing.
    in_flight: Vec<(String, String)>,
    shutdown: bool,
    /// Whether the draining thread exists yet. Spawned on first submission so
    /// that constructing an `App` (which tests do many times) does not create a
    /// thread that will never be used.
    worker_started: bool,
}

/// A bounded, off-loop worker for durable Lane control verbs.
#[derive(Debug, Clone)]
pub struct LaneControlQueue {
    shared: Arc<(Mutex<Shared>, Condvar)>,
    tickets: Arc<AtomicU64>,
}

impl Default for LaneControlQueue {
    fn default() -> Self {
        Self::new()
    }
}

impl LaneControlQueue {
    /// Create a queue with no worker attached. Submissions accumulate until a
    /// worker drains them, which is what the tests use to observe the queue
    /// deterministically.
    #[must_use]
    pub fn new() -> Self {
        Self {
            shared: Arc::new((Mutex::new(Shared::default()), Condvar::new())),
            tickets: Arc::new(AtomicU64::new(1)),
        }
    }

    /// Start the draining thread if it is not already running.
    ///
    /// A plain OS thread, not a tokio task: the work is blocking by nature
    /// (subprocess + file lock) and must not occupy an async executor slot.
    /// Called on first submission so an idle `App` costs nothing.
    fn ensure_worker(&self, shared: &mut Shared) {
        if shared.worker_started {
            return;
        }
        shared.worker_started = true;
        let worker = self.clone();
        if std::thread::Builder::new()
            .name("lane-control".to_string())
            .spawn(move || worker.run())
            .is_err()
        {
            // Could not spawn: leave the flag clear so a later submission can
            // retry rather than queueing into a queue nothing will drain.
            shared.worker_started = false;
        }
    }

    /// Submit a Lane control verb for off-loop execution.
    ///
    /// Everything that can be decided without blocking is decided here and
    /// returned synchronously: unknown verb, malformed target, unavailable
    /// backend, saturated queue. Only the blocking teardown is deferred.
    #[must_use]
    pub fn submit(
        &self,
        operation: ControlOperation,
        raw_target: Option<&str>,
        registry_root: Option<PathBuf>,
    ) -> ControlReceipt {
        let descriptor = operation.descriptor();
        let surface = ControlSurface::Slash;

        // #1888: a surface must never advertise a backend that does not exist.
        // A verb that is declared but unbuilt (`restart`, `resume`) or not
        // offered on this surface can never succeed no matter what the durable
        // store looks like, so it is refused here with its typed reason rather
        // than answered `queued` for work that will never run. The stores are
        // deliberately assumed present for this check: whether the registry
        // exists is a *runtime* fact the executor probes on the worker thread,
        // and probing it here would put a filesystem answer on the composer
        // thread and make an empty workspace look like an unbuilt feature.
        let availability = descriptor.availability(surface, ControlContext::new(true, true));
        if !availability.is_available() {
            return ControlReceipt::unavailable(descriptor, surface, availability);
        }

        // Validate the target on the calling thread: a typo must be refused
        // now, not silently queued and refused a tick later.
        let target = match parse_target(descriptor, raw_target) {
            Ok(target) => target,
            Err(failure) => {
                return ControlReceipt::rejected(descriptor, surface, None, failure);
            }
        };

        let (lock, condvar) = &*self.shared;
        let mut shared = match lock.lock() {
            Ok(shared) => shared,
            // A poisoned queue means the worker panicked mid-teardown. Refuse
            // rather than submitting into a queue nothing will drain.
            Err(_) => {
                return ControlReceipt::failed(
                    descriptor,
                    surface,
                    target,
                    ControlFailure::backend("Lane control worker is not running"),
                );
            }
        };

        if shared.pending.len() >= MAX_PENDING {
            return ControlReceipt::rejected(
                descriptor,
                surface,
                target,
                ControlFailure::new(
                    ControlFailureKind::Saturated,
                    format!(
                        "Lane control queue is full ({MAX_PENDING} pending); \
                         nothing was submitted"
                    ),
                ),
            );
        }

        // Re-submitting the same Lane while a teardown is in flight is a
        // conflict, not a second teardown.
        if let Some(target) = target.as_ref()
            && let Some((ticket, _)) = shared
                .in_flight
                .iter()
                .find(|(_, id)| *id == target.id)
                .cloned()
        {
            return ControlReceipt::rejected(
                descriptor,
                surface,
                Some(target.clone()),
                ControlFailure::conflict(format!(
                    "{} is already in flight under ticket {ticket}",
                    target.id
                )),
            );
        }

        let ticket = format!("lane-ctl-{}", self.tickets.fetch_add(1, Ordering::Relaxed));
        if let Some(target) = target.as_ref() {
            shared.in_flight.push((ticket.clone(), target.id.clone()));
        }
        shared.pending.push_back(Submission {
            ticket: ticket.clone(),
            operation,
            raw_target: raw_target.map(str::to_string),
            registry_root,
        });
        // Tests drive `run_once` directly and never start the thread; in
        // production the first submission starts it.
        if cfg!(not(test)) {
            self.ensure_worker(&mut shared);
        }
        condvar.notify_one();
        drop(shared);

        ControlReceipt::queued(descriptor, surface, target, ticket)
    }

    /// Take every terminal receipt produced since the last drain.
    #[must_use]
    pub fn drain_completed(&self) -> Vec<ControlReceipt> {
        let (lock, _) = &*self.shared;
        match lock.lock() {
            Ok(mut shared) => std::mem::take(&mut shared.completed),
            Err(_) => Vec::new(),
        }
    }

    /// How many submissions are waiting. Test-only: nothing in the UI reads
    /// backpressure yet, and an unused public accessor would be a claim the
    /// build does not back.
    #[cfg(test)]
    #[must_use]
    pub fn pending_len(&self) -> usize {
        let (lock, _) = &*self.shared;
        lock.lock().map(|shared| shared.pending.len()).unwrap_or(0)
    }

    /// Execute one queued submission if there is one, on the calling thread.
    ///
    /// This is the worker's body, exposed so tests can drain deterministically
    /// without a thread. Returns `false` when the queue was empty.
    pub fn run_once(&self) -> bool {
        let (lock, _) = &*self.shared;
        let Some(submission) = ({
            let Ok(mut shared) = lock.lock() else {
                return false;
            };
            shared.pending.pop_front()
        }) else {
            return false;
        };

        // The blocking call. Deliberately outside the lock so a slow teardown
        // never blocks a submission or a drain.
        let receipt = execute_lane_control_in(
            ControlSurface::Cli,
            submission.operation,
            submission.raw_target.as_deref(),
            submission.registry_root.as_deref(),
        );
        // The work ran off the composer thread, but it was *requested* from the
        // slash surface; the receipt must say so rather than impersonating the
        // CLI.
        let mut receipt = receipt.with_ticket(submission.ticket.clone());
        receipt.surface = ControlSurface::Slash;

        if let Ok(mut shared) = lock.lock() {
            shared
                .in_flight
                .retain(|(ticket, _)| *ticket != submission.ticket);
            shared.completed.push(receipt);
        }
        true
    }

    fn run(&self) {
        let (lock, condvar) = &*self.shared;
        loop {
            {
                let Ok(mut shared) = lock.lock() else {
                    return;
                };
                while shared.pending.is_empty() && !shared.shutdown {
                    let Ok(next) = condvar.wait(shared) else {
                        return;
                    };
                    shared = next;
                }
                if shared.shutdown && shared.pending.is_empty() {
                    return;
                }
            }
            self.run_once();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use codewhale_lane::{LaneRegistry, LaneStatus, LifecycleOutcome, RuntimeBackendKind};

    fn seeded() -> (tempfile::TempDir, String) {
        let dir = tempfile::tempdir().unwrap();
        let registry = LaneRegistry::open(dir.path()).unwrap();
        let record = registry
            .create_pending(
                Some("stopship".into()),
                Some("stopship".into()),
                Some("4022".into()),
                None,
                RuntimeBackendKind::Inline,
                None,
            )
            .unwrap();
        let id = record.id.clone();
        (dir, id)
    }

    /// #4022: the production slash path returns immediately with a typed
    /// `queued` receipt and only then performs the blocking teardown.
    #[test]
    fn slash_interrupt_returns_queued_then_completes_off_loop() {
        let (dir, id) = seeded();
        let queue = LaneControlQueue::new();

        let submitted = queue.submit(
            ControlOperation::LaneInterrupt,
            Some(id.as_str()),
            Some(dir.path().to_path_buf()),
        );
        assert_eq!(submitted.outcome, LifecycleOutcome::Queued);
        assert_eq!(submitted.surface, ControlSurface::Slash);
        assert!(submitted.ticket.is_some());
        assert!(
            !submitted.is_error(),
            "queued is not an error, but it is also not success"
        );
        // Nothing has happened to durable state yet.
        assert_eq!(
            LaneRegistry::open(dir.path())
                .unwrap()
                .load(&id)
                .unwrap()
                .status,
            LaneStatus::Pending
        );

        assert!(queue.run_once());
        let completed = queue.drain_completed();
        assert_eq!(completed.len(), 1);
        assert_eq!(completed[0].ticket, submitted.ticket);
        assert_eq!(completed[0].outcome, LifecycleOutcome::Transitioned);
        assert_eq!(
            completed[0].surface,
            ControlSurface::Slash,
            "the receipt reports who asked, not which thread ran it"
        );
        assert_eq!(
            LaneRegistry::open(dir.path())
                .unwrap()
                .load(&id)
                .unwrap()
                .status,
            LaneStatus::Stopped
        );
    }

    /// A malformed id is refused on the calling thread, not queued.
    #[test]
    fn invalid_targets_are_refused_synchronously() {
        let queue = LaneControlQueue::new();
        for bad in [None, Some(""), Some("../escape"), Some("a b")] {
            let receipt = queue.submit(ControlOperation::LaneInterrupt, bad, None);
            assert_eq!(
                receipt.failure.as_ref().map(|failure| failure.kind),
                Some(ControlFailureKind::InvalidTarget),
                "{bad:?}"
            );
            assert!(receipt.ticket.is_none());
        }
        assert_eq!(queue.pending_len(), 0);
    }

    /// #1888: a verb with no backend must be refused on the calling thread.
    /// Queueing it would answer `queued` — a receipt that implies work is
    /// under way — for work that can never run.
    #[test]
    fn declared_but_unbuilt_verbs_are_refused_without_queueing() {
        let queue = LaneControlQueue::new();
        for operation in [ControlOperation::LaneRestart, ControlOperation::LaneResume] {
            let receipt = queue.submit(operation, Some("lane-a1b2c3d4"), None);
            assert_eq!(receipt.outcome, LifecycleOutcome::Rejected);
            assert_eq!(
                receipt.availability.reason(),
                Some(codewhale_lane::UnavailableReason::BackendNotImplemented),
                "{operation:?}"
            );
            assert!(receipt.ticket.is_none());
            assert!(!receipt.retryable);
        }
        assert_eq!(queue.pending_len(), 0, "nothing may have been enqueued");
    }

    /// #1888: a saturated queue refuses with a typed, retryable receipt rather
    /// than growing without bound or blocking the composer.
    #[test]
    fn a_saturated_queue_refuses_without_submitting() {
        let queue = LaneControlQueue::new();
        for index in 0..MAX_PENDING {
            let receipt = queue.submit(
                ControlOperation::LaneInterrupt,
                Some(format!("lane-{index:08}").as_str()),
                None,
            );
            assert_eq!(receipt.outcome, LifecycleOutcome::Queued);
        }
        assert_eq!(queue.pending_len(), MAX_PENDING);

        let refused = queue.submit(ControlOperation::LaneInterrupt, Some("lane-overflow"), None);
        assert_eq!(refused.outcome, LifecycleOutcome::Rejected);
        assert_eq!(
            refused.failure.as_ref().map(|failure| failure.kind),
            Some(ControlFailureKind::Saturated)
        );
        assert!(refused.retryable, "saturation clears on its own");
        assert!(refused.ticket.is_none());
        assert_eq!(
            queue.pending_len(),
            MAX_PENDING,
            "the refused submission must not have been enqueued"
        );
    }

    /// Re-pressing interrupt while a teardown is in flight is a conflict, not
    /// a second teardown of the same Lane.
    #[test]
    fn a_duplicate_submission_for_one_lane_is_a_conflict() {
        let queue = LaneControlQueue::new();
        let first = queue.submit(ControlOperation::LaneInterrupt, Some("lane-a1b2c3d4"), None);
        assert_eq!(first.outcome, LifecycleOutcome::Queued);

        let second = queue.submit(ControlOperation::LaneInterrupt, Some("lane-a1b2c3d4"), None);
        assert_eq!(
            second.failure.as_ref().map(|failure| failure.kind),
            Some(ControlFailureKind::Conflict)
        );
        assert_eq!(queue.pending_len(), 1);
    }

    /// #4022 (TUI responsiveness): submission must not perform the blocking
    /// work. A queue with no worker still returns promptly, which is only
    /// possible if `submit` never touches the Runtime.
    #[test]
    fn submission_does_not_block_on_runtime_teardown() {
        let (dir, id) = seeded();
        let queue = LaneControlQueue::new();
        let started = std::time::Instant::now();
        let receipt = queue.submit(
            ControlOperation::LaneInterrupt,
            Some(id.as_str()),
            Some(dir.path().to_path_buf()),
        );
        let elapsed = started.elapsed();
        assert_eq!(receipt.outcome, LifecycleOutcome::Queued);
        assert!(
            elapsed < std::time::Duration::from_millis(250),
            "submission took {elapsed:?}; it must not run teardown inline"
        );
        // Proof it really was deferred: durable state is untouched until a
        // worker runs, and no worker has.
        assert_eq!(
            LaneRegistry::open(dir.path())
                .unwrap()
                .load(&id)
                .unwrap()
                .status,
            LaneStatus::Pending
        );
    }

    /// The fence still refuses under the registry lock when the work finally
    /// runs off-loop.
    #[test]
    fn a_stale_fence_conflicts_when_the_queued_work_runs() {
        let (dir, id) = seeded();
        let queue = LaneControlQueue::new();
        let submitted = queue.submit(
            ControlOperation::LaneInterrupt,
            Some(format!("{id}@99").as_str()),
            Some(dir.path().to_path_buf()),
        );
        assert_eq!(submitted.outcome, LifecycleOutcome::Queued);

        assert!(queue.run_once());
        let completed = queue.drain_completed();
        assert_eq!(completed.len(), 1);
        assert_eq!(completed[0].outcome, LifecycleOutcome::Rejected);
        assert_eq!(
            completed[0].failure.as_ref().map(|failure| failure.kind),
            Some(ControlFailureKind::Conflict)
        );
        assert_eq!(
            LaneRegistry::open(dir.path())
                .unwrap()
                .load(&id)
                .unwrap()
                .status,
            LaneStatus::Pending,
            "a refused fence must not have torn anything down"
        );
    }

    /// A completed ticket frees its slot, so the queue does not leak capacity.
    #[test]
    fn completing_a_submission_frees_its_in_flight_slot() {
        let (dir, id) = seeded();
        let queue = LaneControlQueue::new();
        let _ = queue.submit(
            ControlOperation::LaneInterrupt,
            Some(id.as_str()),
            Some(dir.path().to_path_buf()),
        );
        assert!(queue.run_once());
        let _ = queue.drain_completed();

        let again = queue.submit(
            ControlOperation::LaneInterrupt,
            Some(id.as_str()),
            Some(dir.path().to_path_buf()),
        );
        assert_eq!(
            again.outcome,
            LifecycleOutcome::Queued,
            "the slot must be reusable once the ticket completes"
        );
    }
}