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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use parking_lot::Mutex;
use tokio::sync::Semaphore;
use crate::agents::{AgentMode, AgentStatus, ApprovalType};
use crate::audit::AuditEventSender;
use crate::command_sender::CommandSender;
use crate::config::AutoApproveSettings;
use crate::detectors;
use crate::state::SharedState;
use super::judge::{ClaudeHaikuJudge, JudgmentProvider};
use super::rules::RuleEngine;
use super::types::{
AutoApproveMode, AutoApprovePhase, JudgmentDecision, JudgmentRequest, JudgmentResult,
};
/// Number of screen context lines to capture for judgment
const SCREEN_CONTEXT_LINES: usize = 30;
/// Tracks whether a target is being judged or in cooldown after judgment
enum FlightStatus {
/// Judgment is currently in progress
InFlight,
/// Judgment completed; cooldown started at this instant
Cooldown(Instant),
}
/// Auto-approve service that monitors agents and auto-approves safe actions
pub struct AutoApproveService {
settings: AutoApproveSettings,
app_state: SharedState,
command_sender: Arc<CommandSender>,
audit_tx: Option<AuditEventSender>,
}
impl AutoApproveService {
/// Create a new AutoApproveService
pub fn new(
settings: AutoApproveSettings,
app_state: SharedState,
command_sender: CommandSender,
audit_tx: Option<AuditEventSender>,
) -> Self {
Self {
settings,
app_state,
command_sender: Arc::new(command_sender),
audit_tx,
}
}
/// Start the service as a background tokio task
pub fn start(self) -> tokio::task::JoinHandle<()> {
// Validate custom_command path at startup
if let Some(ref cmd) = self.settings.custom_command {
let path = std::path::Path::new(cmd);
if path.is_absolute() {
if !path.exists() {
tracing::warn!(
custom_command = %cmd,
"Auto-approve custom_command not found at absolute path"
);
}
} else {
// For relative command names, check via `which`
match std::process::Command::new("which").arg(cmd).output() {
Ok(output) if output.status.success() => {}
_ => {
tracing::warn!(
custom_command = %cmd,
"Auto-approve custom_command not found in PATH"
);
}
}
}
}
tokio::spawn(async move {
self.run().await;
})
}
/// Main loop
async fn run(self) {
let mode = self.settings.effective_mode();
// Initialize providers based on mode
let rule_engine: Option<Arc<RuleEngine>> =
if mode == AutoApproveMode::Rules || mode == AutoApproveMode::Hybrid {
Some(Arc::new(RuleEngine::new(self.settings.rules.clone())))
} else {
None
};
let ai_judge: Option<Arc<ClaudeHaikuJudge>> =
if mode == AutoApproveMode::Ai || mode == AutoApproveMode::Hybrid {
Some(Arc::new(ClaudeHaikuJudge::new(
self.settings.model.clone(),
self.settings.timeout_secs,
self.settings.custom_command.clone(),
)))
} else {
None
};
// Legacy compatibility: if mode is Ai but no ai_judge, fall back
let judge = ai_judge.clone().unwrap_or_else(|| {
Arc::new(ClaudeHaikuJudge::new(
self.settings.model.clone(),
self.settings.timeout_secs,
self.settings.custom_command.clone(),
))
});
// Suppress unused variable warning for `judge` when not in Ai mode
let _ = &judge;
let flight_tracker: Arc<Mutex<HashMap<String, FlightStatus>>> =
Arc::new(Mutex::new(HashMap::new()));
let semaphore = Arc::new(Semaphore::new(self.settings.max_concurrent));
let interval = Duration::from_millis(self.settings.check_interval_ms);
let cooldown_duration = Duration::from_secs(self.settings.cooldown_secs);
tracing::info!(
"Auto-approve service started (mode={:?}, model={}, interval={}ms, max_concurrent={})",
mode,
self.settings.model,
self.settings.check_interval_ms,
self.settings.max_concurrent,
);
loop {
tokio::time::sleep(interval).await;
// Collect candidates from shared state, marking skip reasons
let candidates = {
let mut state = self.app_state.write();
if !state.running {
tracing::info!("Auto-approve service shutting down (app stopped)");
break;
}
let mut candidates = Vec::new();
let targets: Vec<String> = state.agents.keys().cloned().collect();
for target in &targets {
let agent = match state.agents.get(target) {
Some(a) => a,
None => continue,
};
// Skip non-approval agents
let (approval_type, details) = match &agent.status {
AgentStatus::AwaitingApproval {
approval_type,
details,
} => (approval_type.clone(), details.clone()),
_ => continue,
};
// Skip genuine UserQuestion (requires human judgment),
// but allow standard yes/no permission prompts through
if is_genuine_user_question(&approval_type) {
if let Some(a) = state.agents.get_mut(target) {
if a.auto_approve_phase.is_none() {
a.auto_approve_phase = Some(AutoApprovePhase::ManualRequired(
"genuine user question".to_string(),
));
}
}
continue;
}
// Skip agents already in AutoApprove mode
if agent.mode == AgentMode::AutoApprove {
if let Some(a) = state.agents.get_mut(target) {
if a.auto_approve_phase.is_none() {
a.auto_approve_phase = Some(AutoApprovePhase::ManualRequired(
"agent in auto-approve mode".to_string(),
));
}
}
continue;
}
// Skip virtual agents (no pane to send keys to)
if agent.is_virtual {
continue;
}
// Check allowed_types filter
if !self.settings.allowed_types.is_empty() {
let type_str = approval_type_to_string(&approval_type);
if !self.settings.allowed_types.contains(&type_str) {
if let Some(a) = state.agents.get_mut(target) {
if a.auto_approve_phase.is_none() {
a.auto_approve_phase = Some(AutoApprovePhase::ManualRequired(
format!("not in allowed_types ({})", type_str),
));
}
}
continue;
}
}
// Skip if in flight or in cooldown (single lock check)
{
let tracker = flight_tracker.lock();
match tracker.get(target) {
Some(FlightStatus::InFlight) => continue,
Some(FlightStatus::Cooldown(since)) => {
if since.elapsed() < cooldown_duration {
continue;
}
}
None => {}
}
}
candidates.push((
target.clone(),
approval_type,
details,
agent.cwd.clone(),
agent.agent_type.clone(),
agent.last_content.clone(),
));
}
candidates
};
// Spawn judgment tasks for each candidate
for (target, approval_type, details, cwd, agent_type, last_content) in candidates {
// Check semaphore availability (non-blocking)
let permit = match semaphore.clone().try_acquire_owned() {
Ok(permit) => permit,
Err(_) => continue, // Max concurrent reached
};
flight_tracker
.lock()
.insert(target.clone(), FlightStatus::InFlight);
// Mark agent as being judged
{
let mut state = self.app_state.write();
if let Some(agent) = state.agents.get_mut(&target) {
agent.auto_approve_phase = Some(AutoApprovePhase::Judging);
}
}
let request = JudgmentRequest {
target: target.clone(),
approval_type: approval_type_to_string(&approval_type),
details: details.clone(),
screen_context: extract_screen_context(&last_content, SCREEN_CONTEXT_LINES),
cwd: cwd.clone(),
agent_type: agent_type.short_name().to_string(),
};
let rule_engine_ref = rule_engine.clone();
let ai_judge_ref = ai_judge.clone();
let flight_tracker = flight_tracker.clone();
let app_state = self.app_state.clone();
let command_sender = self.command_sender.clone();
let audit_tx = self.audit_tx.clone();
let agent_type_clone = agent_type.clone();
tokio::spawn(async move {
// Dispatch based on mode
let result = match mode {
AutoApproveMode::Rules => {
rule_engine_ref
.as_ref()
.expect("rule engine initialized")
.judge(&request)
.await
}
AutoApproveMode::Ai => {
ai_judge_ref
.as_ref()
.expect("ai judge initialized")
.judge(&request)
.await
}
AutoApproveMode::Hybrid => {
let rule_result = rule_engine_ref
.as_ref()
.expect("rule engine initialized")
.judge(&request)
.await;
match rule_result {
Ok(ref r) if r.decision == JudgmentDecision::Uncertain => {
// Rules abstained → escalate to AI
ai_judge_ref
.as_ref()
.expect("ai judge initialized")
.judge(&request)
.await
}
other => other,
}
}
AutoApproveMode::Off => unreachable!("Off mode should not reach here"),
};
let _permit = permit; // Keep permit alive until task completes
match result {
Ok(result) => {
let approval_sent = handle_judgment_result(
&request,
&result,
&app_state,
&command_sender,
&agent_type_clone,
);
// Determine phase: rule-based vs AI-based approval
let phase = if approval_sent {
if result.model.starts_with("rules:") {
Some(AutoApprovePhase::ApprovedByRule)
} else {
Some(AutoApprovePhase::ApprovedByAi)
}
} else {
Some(AutoApprovePhase::ManualRequired(format!(
"{}: {}",
result.decision, result.reasoning
)))
};
// Update phase
{
let mut state = app_state.write();
if let Some(agent) = state.agents.get_mut(&request.target) {
agent.auto_approve_phase = phase;
}
}
// Emit audit event
emit_audit_event(&audit_tx, &request, &result, approval_sent);
tracing::info!(
target = %request.target,
decision = %result.decision,
model = %result.model,
elapsed_ms = result.elapsed_ms,
approval_sent = approval_sent,
"Auto-approve judgment: {}",
result.reasoning,
);
}
Err(e) => {
// Update phase to manual required on error
{
let mut state = app_state.write();
if let Some(agent) = state.agents.get_mut(&request.target) {
agent.auto_approve_phase = Some(
AutoApprovePhase::ManualRequired(format!("error: {}", e)),
);
}
}
tracing::warn!(
target = %request.target,
"Auto-approve judgment error: {}",
e,
);
emit_audit_event(
&audit_tx,
&request,
&JudgmentResult {
decision: JudgmentDecision::Uncertain,
reasoning: format!("Error: {}", e),
model: "unknown".to_string(),
elapsed_ms: 0,
usage: None,
},
false,
);
}
}
// Atomically transition from InFlight to Cooldown (single lock)
flight_tracker
.lock()
.insert(request.target, FlightStatus::Cooldown(Instant::now()));
});
}
}
}
}
/// Handle the judgment result: send approval keys if approved and state is still valid
fn handle_judgment_result(
request: &JudgmentRequest,
result: &JudgmentResult,
app_state: &SharedState,
command_sender: &CommandSender,
agent_type: &crate::agents::AgentType,
) -> bool {
if result.decision != JudgmentDecision::Approve {
return false;
}
// Re-check state before sending keys (race condition protection)
{
let state = app_state.read();
let agent = match state.agents.get(&request.target) {
Some(a) => a,
None => {
tracing::debug!(
target = %request.target,
"Agent disappeared before approval could be sent"
);
return false;
}
};
// Only send keys if still awaiting approval (user might have approved manually)
if !matches!(agent.status, AgentStatus::AwaitingApproval { .. }) {
tracing::debug!(
target = %request.target,
"Agent no longer awaiting approval (likely manually handled)"
);
return false;
}
}
// Get the correct approval keys for this agent type
let detector = detectors::get_detector(agent_type);
let keys = detector.approval_keys();
// Send approval keys
match command_sender.send_keys(&request.target, keys) {
Ok(()) => {
tracing::info!(
target = %request.target,
keys = keys,
"Auto-approve: sent approval keys"
);
true
}
Err(e) => {
tracing::warn!(
target = %request.target,
"Auto-approve: failed to send keys: {}",
e
);
false
}
}
}
/// Emit an AutoApproveJudgment audit event
fn emit_audit_event(
audit_tx: &Option<AuditEventSender>,
request: &JudgmentRequest,
result: &JudgmentResult,
approval_sent: bool,
) {
if let Some(tx) = audit_tx {
let event = crate::audit::AuditEvent::AutoApproveJudgment {
ts: chrono::Utc::now().timestamp_millis() as u64,
pane_id: request.target.clone(),
agent_type: request.agent_type.clone(),
approval_type: request.approval_type.clone(),
approval_details: request.details.clone(),
decision: result.decision.to_string(),
reasoning: result.reasoning.clone(),
model: result.model.clone(),
elapsed_ms: result.elapsed_ms,
approval_sent,
usage: result.usage.clone(),
screen_context: Some(request.screen_context.clone()),
};
let _ = tx.send(event);
}
}
/// Check if an ApprovalType is a genuine user question (not a standard permission prompt).
///
/// Claude Code's standard permission prompts (file edit, shell command, etc.) appear as
/// numbered choices like "1. Yes / 2. Yes, and always allow... / 3. No", which the detector
/// classifies as UserQuestion. However, these are standard approval prompts that can be
/// auto-approved, not genuine AskUserQuestion prompts requiring human judgment.
fn is_genuine_user_question(approval_type: &ApprovalType) -> bool {
match approval_type {
ApprovalType::UserQuestion {
choices,
multi_select,
..
} => {
// Multi-select questions always require human judgment
if *multi_select {
return true;
}
// Standard permission prompts have choices that are all variations of Yes/No
// e.g., ["Yes", "Yes, and always allow...", "No"]
// Genuine questions have choices like ["Option A", "Option B", "Other"]
let is_standard_approval = !choices.is_empty()
&& choices.iter().all(|choice| {
let lower = choice.to_lowercase();
lower.starts_with("yes")
|| lower.starts_with("no")
|| lower == "other"
|| lower == "__other__"
});
// If all choices are yes/no variants, this is a standard prompt → NOT genuine
!is_standard_approval
}
_ => false, // Non-UserQuestion types are never genuine user questions
}
}
/// Convert ApprovalType to a string for filtering and logging
fn approval_type_to_string(approval_type: &ApprovalType) -> String {
match approval_type {
ApprovalType::FileEdit => "file_edit".to_string(),
ApprovalType::FileCreate => "file_create".to_string(),
ApprovalType::FileDelete => "file_delete".to_string(),
ApprovalType::ShellCommand => "shell_command".to_string(),
ApprovalType::McpTool => "mcp_tool".to_string(),
ApprovalType::UserQuestion { .. } => "user_question".to_string(),
ApprovalType::Other(s) => s.clone(),
}
}
/// Mask sensitive data patterns (API keys, tokens, etc.) before sending to AI
fn sanitize_sensitive_data(text: &str) -> String {
use crate::wrap::exfil_detector::SENSITIVE_PATTERNS;
let mut result = text.to_string();
for sp in SENSITIVE_PATTERNS.iter() {
result = sp
.pattern
.replace_all(&result, format!("[REDACTED:{}]", sp.name))
.to_string();
}
result
}
/// Extract the last N lines from screen content, with sensitive data sanitized
fn extract_screen_context(content: &str, max_lines: usize) -> String {
let lines: Vec<&str> = content.lines().collect();
let start = lines.len().saturating_sub(max_lines);
sanitize_sensitive_data(&lines[start..].join("\n"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_screen_context() {
let content = (1..=50)
.map(|i| format!("line {}", i))
.collect::<Vec<_>>()
.join("\n");
let context = extract_screen_context(&content, 30);
let lines: Vec<&str> = context.lines().collect();
assert_eq!(lines.len(), 30);
assert_eq!(lines[0], "line 21");
assert_eq!(lines[29], "line 50");
}
#[test]
fn test_extract_screen_context_short() {
let content = "line 1\nline 2\nline 3";
let context = extract_screen_context(content, 30);
assert_eq!(context, content);
}
#[test]
fn test_approval_type_to_string() {
assert_eq!(
approval_type_to_string(&ApprovalType::FileEdit),
"file_edit"
);
assert_eq!(
approval_type_to_string(&ApprovalType::ShellCommand),
"shell_command"
);
assert_eq!(
approval_type_to_string(&ApprovalType::Other("custom".to_string())),
"custom"
);
}
#[test]
fn test_is_genuine_user_question_standard_yes_no() {
// Standard permission prompt: "1. Yes / 2. No" → NOT genuine
let approval = ApprovalType::UserQuestion {
choices: vec!["Yes".to_string(), "No".to_string()],
multi_select: false,
cursor_position: 1,
};
assert!(!is_genuine_user_question(&approval));
}
#[test]
fn test_is_genuine_user_question_yes_always_no() {
// Standard permission prompt with "always allow" → NOT genuine
let approval = ApprovalType::UserQuestion {
choices: vec![
"Yes".to_string(),
"Yes, and always allow access to tmp/ from this project".to_string(),
"No".to_string(),
],
multi_select: false,
cursor_position: 1,
};
assert!(!is_genuine_user_question(&approval));
}
#[test]
fn test_is_genuine_user_question_custom_choices() {
// Actual AskUserQuestion with custom choices → genuine
let approval = ApprovalType::UserQuestion {
choices: vec![
"Use TypeScript".to_string(),
"Use JavaScript".to_string(),
"Other".to_string(),
],
multi_select: false,
cursor_position: 1,
};
assert!(is_genuine_user_question(&approval));
}
#[test]
fn test_is_genuine_user_question_multi_select() {
// Multi-select is always genuine (requires human selection)
let approval = ApprovalType::UserQuestion {
choices: vec!["Yes".to_string(), "No".to_string()],
multi_select: true,
cursor_position: 1,
};
assert!(is_genuine_user_question(&approval));
}
#[test]
fn test_is_genuine_user_question_non_user_question() {
// Non-UserQuestion types are never genuine user questions
assert!(!is_genuine_user_question(&ApprovalType::FileEdit));
assert!(!is_genuine_user_question(&ApprovalType::ShellCommand));
}
#[test]
fn test_sanitize_sensitive_data_openai_key() {
let text = "export OPENAI_API_KEY=sk-test1234567890abcdefghij";
let result = sanitize_sensitive_data(text);
assert!(result.contains("[REDACTED:OpenAI API Key]"));
assert!(!result.contains("sk-test1234567890abcdefghij"));
}
#[test]
fn test_sanitize_sensitive_data_github_token() {
let text = "GITHUB_TOKEN=ghp_1234567890abcdefghijklmnopqrstuvwxyz";
let result = sanitize_sensitive_data(text);
assert!(result.contains("[REDACTED:GitHub Token]"));
assert!(!result.contains("ghp_1234567890abcdefghijklmnopqrstuvwxyz"));
}
#[test]
fn test_sanitize_sensitive_data_no_sensitive() {
let text = "Hello world\nnormal output\nno secrets here";
let result = sanitize_sensitive_data(text);
assert_eq!(result, text);
}
#[test]
fn test_extract_screen_context_sanitizes() {
let content = "line 1\nline 2\napi_key=sk-secret1234567890abcdefghij\nline 4";
let context = extract_screen_context(content, 30);
assert!(context.contains("[REDACTED:"));
assert!(!context.contains("sk-secret1234567890abcdefghij"));
}
}