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
//! App Lifecycle Methods
//!
//! Contains constructors, initialization, cleanup, and background task management.
//!
//! # Functions
//!
//! - `spawn_tracked()` - Spawn a background task with abort tracking
//! - `spawn_provider_verification()` - Verify LLM providers async
//! - `spawn_provider_verification_timeout()` - Timeout watcher for provider verification
//! - `spawn_mcp_verification()` - Verify MCP servers async
//! - `save_current_session()` - Save chat session to disk
//! - `cancel_background_tasks()` - Abort all tracked background tasks
//! - `cleanup()` - Restore terminal state
use std::sync::Arc;
use std::time::Duration;
use crossterm::{
event::DisableMouseCapture,
execute,
terminal::{disable_raw_mode, LeaveAlternateScreen},
};
use crate::error::{NikaError, Result};
use crate::mcp::{McpClient, McpConfig};
use crate::provider::rig::RigProvider;
use super::super::session::save_session;
use super::super::verification::VerificationEntry;
use super::App;
use crate::provider::rig::StreamChunk;
impl App {
/// Spawn a tracked background task that will be cancelled on cleanup
///
/// Use this instead of raw `tokio::spawn()` for tasks that should be
/// cleaned up when the TUI exits. The task's AbortHandle is stored for
/// later cancellation via `cancel_background_tasks()`.
pub(crate) fn spawn_tracked<F>(&self, future: F)
where
F: std::future::Future<Output = ()> + Send + 'static,
{
let handle = tokio::spawn(future);
// PERF: parking_lot::Mutex doesn't poison, so this is infallible
self.background_handles.lock().push(handle.abort_handle());
}
/// Spawn async provider verification tasks
///
/// Verifies all configured LLM providers in parallel, sending StreamChunk events
/// to update the session context display in real-time.
///
/// Uses TTL-based caching to avoid redundant API calls (30s default).
pub(crate) fn spawn_provider_verification(&self) {
let tx = self.stream_chunk_tx.clone();
let provider_ids = [
("claude", "claude-sonnet-4-6"),
("openai", "gpt-4o"),
("mistral", "mistral-large-latest"),
("groq", "llama-3.3-70b-versatile"),
("deepseek", "deepseek-chat"),
];
let cache = Arc::clone(&self.verification_cache);
// Check cache and send events for cached providers, mark uncached as verifying
for (provider_id, default_model) in &provider_ids {
let model = default_model.to_string();
// Check if we have a valid cached result
let cached = {
let cache_guard = cache.lock();
cache_guard.get_provider(provider_id).and_then(|entry| {
if cache_guard.is_valid(entry) {
Some(entry.clone())
} else {
None
}
})
};
if let Some(entry) = cached {
// Send cached result directly
match entry.status {
super::super::widgets::VerifyStatus::Verified => {
let _ = tx.try_send(StreamChunk::ProviderVerified {
provider: provider_id.to_string(),
model: entry.model.unwrap_or(model),
latency_ms: entry.latency.map(|d| d.as_millis() as u64).unwrap_or(0),
});
}
super::super::widgets::VerifyStatus::Failed => {
let _ = tx.try_send(StreamChunk::ProviderVerifyFailed {
provider: provider_id.to_string(),
error: entry.error.unwrap_or_else(|| "Unknown error".to_string()),
});
}
_ => {
// Verifying or Unknown - re-verify
let _ = tx.try_send(StreamChunk::ProviderVerifying {
provider: provider_id.to_string(),
model,
});
}
}
} else {
// Mark as verifying - will spawn task below
let _ = tx.try_send(StreamChunk::ProviderVerifying {
provider: provider_id.to_string(),
model,
});
}
}
// Spawn verification tasks only for providers NOT in valid cache
for (provider_id_str, _) in provider_ids {
let provider_id = provider_id_str.to_string();
// Skip if cached
{
let cache_guard = cache.lock();
if cache_guard.has_valid_provider(&provider_id) {
continue;
}
}
let tx = tx.clone();
let cache = Arc::clone(&cache);
self.spawn_tracked(async move {
// Create provider and check if configured
let provider_opt: Option<RigProvider> = match provider_id.as_str() {
"claude" => {
if std::env::var("ANTHROPIC_API_KEY").is_ok() {
let p = RigProvider::claude();
if p.is_configured() {
Some(p)
} else {
None
}
} else {
None
}
}
"openai" => {
if std::env::var("OPENAI_API_KEY").is_ok() {
let p = RigProvider::openai();
if p.is_configured() {
Some(p)
} else {
None
}
} else {
None
}
}
"mistral" => {
if std::env::var("MISTRAL_API_KEY").is_ok() {
let p = RigProvider::mistral();
if p.is_configured() {
Some(p)
} else {
None
}
} else {
None
}
}
"groq" => {
if std::env::var("GROQ_API_KEY").is_ok() {
let p = RigProvider::groq();
if p.is_configured() {
Some(p)
} else {
None
}
} else {
None
}
}
"deepseek" => {
if std::env::var("DEEPSEEK_API_KEY").is_ok() {
let p = RigProvider::deepseek();
if p.is_configured() {
Some(p)
} else {
None
}
} else {
None
}
}
"native" => {
#[cfg(feature = "native-inference")]
{
Some(RigProvider::native())
}
#[cfg(not(feature = "native-inference"))]
{
None
}
}
_ => None,
};
const SINGLE_PROVIDER_TIMEOUT: Duration = Duration::from_secs(10);
match provider_opt {
Some(provider) => {
match tokio::time::timeout(SINGLE_PROVIDER_TIMEOUT, provider.verify()).await
{
Ok(Ok(result)) => {
// Cache the successful result
{
let mut cache_guard = cache.lock();
cache_guard.set_provider(
provider_id.clone(),
VerificationEntry::verified(
result.latency,
Some(result.model.clone()),
),
);
}
let _ = tx
.send(StreamChunk::ProviderVerified {
provider: provider_id,
model: result.model,
latency_ms: result.latency.as_millis() as u64,
})
.await;
}
Ok(Err(e)) => {
// Cache the failure
{
let mut cache_guard = cache.lock();
cache_guard.set_provider(
provider_id.clone(),
VerificationEntry::failed(e.to_string()),
);
}
let _ = tx
.send(StreamChunk::ProviderVerifyFailed {
provider: provider_id,
error: e.to_string(),
})
.await;
}
Err(_timeout) => {
tracing::warn!(
provider = %provider_id,
"Provider verification timed out after 10s"
);
{
let mut cache_guard = cache.lock();
cache_guard.set_provider(
provider_id.clone(),
VerificationEntry::failed(
"Verification timeout (10s)".to_string(),
),
);
}
let _ = tx
.send(StreamChunk::ProviderVerifyFailed {
provider: provider_id,
error: "Verification timeout (10s)".to_string(),
})
.await;
}
}
}
None => {
tracing::debug!(provider = %provider_id, "Provider not configured");
let _ = tx
.send(StreamChunk::ProviderNotConfigured {
provider: provider_id,
})
.await;
}
}
});
}
}
/// Spawn provider verification timeout watcher
///
/// After 5 seconds, checks if ANY provider has been verified.
/// If not, sends ProviderVerificationTimeout event to show fallback UI.
pub(crate) fn spawn_provider_verification_timeout(&self) {
const PROVIDER_VERIFICATION_TIMEOUT: Duration = Duration::from_secs(5);
let tx = self.stream_chunk_tx.clone();
let cache = Arc::clone(&self.verification_cache);
self.spawn_tracked(async move {
tokio::time::sleep(PROVIDER_VERIFICATION_TIMEOUT).await;
// Check if ANY provider is verified
let has_verified = {
let cache_guard = cache.lock();
cache_guard.has_any_verified_provider()
};
if !has_verified {
// Send timeout event
let _ = tx.try_send(StreamChunk::ProviderVerificationTimeout);
}
});
}
/// Spawn async MCP server verification tasks
///
/// Pings all configured MCP servers in parallel, sending StreamChunk events
/// to update the session context display in real-time.
///
/// Uses TTL-based caching to avoid redundant MCP pings (30s default).
pub(crate) fn spawn_mcp_verification(&self) {
let pool_configs = self.mcp_pool.configs();
if pool_configs.is_empty() {
tracing::debug!("No MCP servers configured, skipping verification");
return;
}
let tx = self.stream_chunk_tx.clone();
let cache = Arc::clone(&self.verification_cache);
let configs: Vec<_> = pool_configs
.iter()
.map(|(name, config)| (name.clone(), config.clone()))
.collect();
// Drop the read guard before spawning async work
drop(pool_configs);
// Check cache and send events for cached servers, mark uncached as pinging
for (server_name, _config) in &configs {
// Check if we have a valid cached result
let cached = {
let cache_guard = cache.lock();
cache_guard.get_mcp(server_name).and_then(|entry| {
if cache_guard.is_valid(entry) {
Some(entry.clone())
} else {
None
}
})
};
if let Some(entry) = cached {
// Send cached result directly
match entry.status {
super::super::widgets::VerifyStatus::Verified => {
let _ = tx.try_send(StreamChunk::McpPinged {
server: server_name.clone(),
latency_ms: entry.latency.map(|d| d.as_millis() as u64).unwrap_or(0),
tool_count: entry.tool_count.unwrap_or(0),
});
}
super::super::widgets::VerifyStatus::Failed => {
let _ = tx.try_send(StreamChunk::McpError {
server_name: server_name.clone(),
error: entry.error.unwrap_or_else(|| "Unknown error".to_string()),
});
}
_ => {
// Verifying or Unknown - re-ping
let _ = tx.try_send(StreamChunk::McpPinging {
server: server_name.clone(),
});
}
}
} else {
// Mark as pinging - will spawn task below
let _ = tx.try_send(StreamChunk::McpPinging {
server: server_name.clone(),
});
}
}
// Spawn ping tasks only for servers NOT in valid cache
for (server_name, config) in configs {
// Skip if cached
{
let cache_guard = cache.lock();
if cache_guard.has_valid_mcp(&server_name) {
continue;
}
}
let tx = tx.clone();
let cache = Arc::clone(&cache);
let server_name_for_config = server_name.clone();
self.spawn_tracked(async move {
// Build MCP config from inline config
let mut mcp_config = McpConfig::new(&server_name_for_config, &config.command);
for arg in &config.args {
mcp_config = mcp_config.with_arg(arg);
}
for (key, value) in &config.env {
mcp_config = mcp_config.with_env(key, value);
}
if let Some(cwd) = &config.cwd {
mcp_config = mcp_config.with_cwd(cwd);
}
// Create client and ping
match McpClient::new(mcp_config) {
Ok(client) => match client.ping().await {
Ok(result) => {
// Cache the successful result
{
let mut cache_guard = cache.lock();
cache_guard.set_mcp(
server_name.clone(),
VerificationEntry::verified_mcp(
result.latency,
result.tool_count,
),
);
}
let _ = tx
.send(StreamChunk::McpPinged {
server: server_name,
latency_ms: result.latency.as_millis() as u64,
tool_count: result.tool_count,
})
.await;
}
Err(e) => {
// Cache the failure
{
let mut cache_guard = cache.lock();
cache_guard.set_mcp(
server_name.clone(),
VerificationEntry::failed(e.to_string()),
);
}
let _ = tx
.send(StreamChunk::McpError {
server_name,
error: e.to_string(),
})
.await;
}
},
Err(e) => {
// Cache the failure
{
let mut cache_guard = cache.lock();
cache_guard.set_mcp(
server_name.clone(),
VerificationEntry::failed(e.to_string()),
);
}
let _ = tx
.send(StreamChunk::McpError {
server_name,
error: e.to_string(),
})
.await;
}
}
});
}
}
/// Save current chat session to disk
///
/// Only saves in standalone mode (chat mode). Skips empty sessions.
pub(crate) fn save_current_session(&mut self) {
// Only save in standalone mode (chat mode)
if self.standalone_state.is_none() {
return;
}
// Get chat state from the chat view
let chat_state = self.command_view.chat.get_chat_state();
// Save session (guard: won't save empty sessions)
match save_session(&chat_state) {
Ok(session) => {
self.session_id = Some(session.id.clone());
tracing::info!("Chat session saved: {}", session.id);
}
Err(e) if e.kind() == std::io::ErrorKind::InvalidInput => {
// Empty session - not an error, just skip
tracing::debug!("Skipping save: {}", e);
}
Err(e) => {
tracing::warn!("Failed to save session: {}", e);
}
}
}
/// Request workflow retry (TIER 1.2)
///
/// Resets failed tasks and signals that caller should re-run the workflow.
/// Only works when workflow is in failed state.
///
/// Note: Will be called from routing.rs when Action::RetryWorkflow is handled.
pub(crate) fn retry_workflow(&mut self) {
if self.state.is_running() {
self.set_status("⚠ Cannot retry: workflow is still running");
return;
}
if self.state.is_success() {
self.set_status("⚠ Cannot retry: workflow completed successfully");
return;
}
if !self.state.is_failed() {
self.set_status("⚠ Nothing to retry");
return;
}
// Reset state for retry
let reset_tasks = self.state.reset_for_retry();
self.retry_requested = true;
self.workflow_done = false;
if reset_tasks.is_empty() {
self.set_status("✓ Ready to retry (no failed tasks found)");
} else {
self.set_status(&format!(
"✓ Ready to retry: {} task(s) reset ({})",
reset_tasks.len(),
reset_tasks.join(", ")
));
}
}
/// Cancel all background tasks
///
/// Should be called during cleanup to ensure graceful shutdown.
/// Tasks are aborted immediately; no waiting for completion.
pub(crate) fn cancel_background_tasks(&self) {
// PERF: parking_lot::Mutex doesn't poison, so this is simple
let handles = self.background_handles.lock();
let count = handles.len();
for handle in handles.iter() {
handle.abort();
}
tracing::debug!("Aborted {} background tasks", count);
}
/// Cleanup terminal state
///
/// Restores terminal to normal mode, disables raw mode, and shows cursor.
pub(crate) fn cleanup(&mut self) -> Result<()> {
// Note: Background tasks are cancelled in run_unified() after this,
// because cleanup() is not async. Use cancel_background_tasks() separately.
if let Some(ref mut terminal) = self.terminal {
disable_raw_mode().map_err(|e| NikaError::TuiError {
reason: format!("Failed to disable raw mode: {}", e),
})?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)
.map_err(|e| NikaError::TuiError {
reason: format!("Failed to leave alternate screen: {}", e),
})?;
terminal.show_cursor().map_err(|e| NikaError::TuiError {
reason: format!("Failed to show cursor: {}", e),
})?;
}
tracing::info!("TUI cleanup complete");
Ok(())
}
/// Toggle theme (cycle through Dark → Light → Solarized)
pub(crate) fn toggle_theme(&mut self) {
use super::super::tokens::CosmicVariant;
let next = match self.cosmic_theme.variant() {
CosmicVariant::CosmicDark => CosmicVariant::CosmicLight,
CosmicVariant::CosmicLight => CosmicVariant::CosmicViolet,
CosmicVariant::CosmicViolet => CosmicVariant::CosmicDark,
};
self.cosmic_theme = super::super::cosmic_theme::CosmicTheme::new(next);
self.theme = self.cosmic_theme.as_theme();
}
/// Set status message (shown in status bar)
pub(crate) fn set_status(&mut self, message: &str) {
self.status_message = Some((message.to_string(), std::time::Instant::now()));
}
}