sysknife-cli 0.6.0

Approval-gated AI system administration CLI and MCP server
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
//! Approval policy engine — pure logic, no I/O.
//!
//! Determines whether plan steps can be auto-approved, need a human prompt,
//! or must be rejected outright based on CLI flags.
//!
//! Security invariant: `--yes` NEVER auto-approves HIGH risk steps regardless
//! of any flag combination. This is hardcoded, not configurable.

use sysknife_brain::planner::{AuthorizedPlan, PlanRiskLevel};

/// The maximum risk level that `--yes` can auto-approve.
/// `--max-risk high` with `--yes` still only auto-approves up to MEDIUM.
/// HIGH always requires a human in the loop.
const HARDCODED_MAX_AUTO_APPROVE: MaxRisk = MaxRisk::Medium;

/// CLI risk-level argument (mirrors clap value-enum).
///
/// Derives `Ord` so the ordering (Low < Medium < High) is a first-class
/// language property. `effective_auto_ceiling` uses `Ord::min` to clamp the
/// auto-approve ceiling; any future variant added above `High` is blocked
/// automatically without requiring a new named match arm.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum MaxRisk {
    Low,
    Medium,
    High,
}

impl MaxRisk {
    /// Human-readable lowercase name for error messages.
    pub fn as_str(&self) -> &'static str {
        match self {
            MaxRisk::Low => "low",
            MaxRisk::Medium => "medium",
            MaxRisk::High => "high",
        }
    }

    /// Returns `true` if this ceiling level includes the given plan risk level.
    fn includes(&self, risk: &PlanRiskLevel) -> bool {
        match self {
            MaxRisk::Low => matches!(risk, PlanRiskLevel::Low),
            MaxRisk::Medium => matches!(risk, PlanRiskLevel::Low | PlanRiskLevel::Medium),
            MaxRisk::High => true,
        }
    }
}

/// Flags that control approval behavior.
///
/// Fields are private; construct with [`ApprovalPolicy::new`] so the security
/// invariant stays encapsulated in [`decide_step`][ApprovalPolicy::decide_step]
/// and cannot be bypassed by direct field mutation at a call site.
pub struct ApprovalPolicy {
    yes: bool,
    max_risk: Option<MaxRisk>,
    non_interactive: bool,
    dry_run: bool,
}

/// The result of evaluating a step or plan against the policy.
#[derive(Debug, PartialEq, Eq)]
pub enum ApprovalDecision {
    /// No human input needed — auto-approved by policy.
    AutoApproved,
    /// Human must confirm before execution.
    RequiresPrompt,
    /// Non-interactive mode and this step would need a prompt — fail immediately.
    ///
    /// Terminal in `decide_plan`: causes early return on first occurrence.
    RequiresInteraction,
    /// Plan exceeds the `--max-risk` ceiling — abort.
    ///
    /// Carries the ceiling that was exceeded. `decide_step` has it in scope at
    /// the point of decision; returning it removes the need for call sites to
    /// re-derive it from `opts.max_risk` behind an `.expect()` that is correct
    /// only while the decision and the options come from the same place.
    ///
    /// Terminal in `decide_plan`: causes early return on first occurrence.
    ExceedsCeiling(MaxRisk),
}

impl ApprovalPolicy {
    /// Construct from parsed CLI flags.
    pub fn new(yes: bool, max_risk: Option<MaxRisk>, non_interactive: bool, dry_run: bool) -> Self {
        Self {
            yes,
            max_risk,
            non_interactive,
            dry_run,
        }
    }

    /// Effective auto-approve ceiling, clamped by the hardcoded HIGH block.
    ///
    /// Returns `None` if `--yes` was not passed (no auto-approval at all).
    /// Returns `Some(level)` indicating the max risk that will be auto-approved.
    pub fn effective_auto_ceiling(&self) -> Option<MaxRisk> {
        if !self.yes {
            return None;
        }
        // User-requested ceiling (default to Low when --yes is bare).
        let requested = self.max_risk.unwrap_or(MaxRisk::Low);

        // Clamp using Ord::min: any variant ≥ HARDCODED_MAX_AUTO_APPROVE is
        // reduced to the constant. Extension-safe — no named-arm match needed.
        Some(requested.min(HARDCODED_MAX_AUTO_APPROVE))
    }

    /// Decision for a single step's risk level.
    pub fn decide_step(&self, risk: &PlanRiskLevel) -> ApprovalDecision {
        // --dry-run: nothing executes, so everything is "approved" vacuously.
        if self.dry_run {
            return ApprovalDecision::AutoApproved;
        }

        // Check --max-risk ceiling (hard abort, independent of --yes).
        if let Some(ceiling) = self.max_risk {
            if !ceiling.includes(risk) {
                return ApprovalDecision::ExceedsCeiling(ceiling);
            }
        }

        // Check auto-approval via --yes.
        if let Some(auto_ceiling) = self.effective_auto_ceiling() {
            if auto_ceiling.includes(risk) {
                return ApprovalDecision::AutoApproved;
            }
        }

        // Step needs a human prompt — can we do that?
        if self.non_interactive {
            return ApprovalDecision::RequiresInteraction;
        }

        ApprovalDecision::RequiresPrompt
    }

    /// Decision for the whole plan (uses the highest risk across all steps).
    ///
    /// Returns `AutoApproved` only if every step is auto-approved.
    /// Returns `ExceedsCeiling` if any step exceeds the ceiling (short-circuits).
    /// Returns `RequiresInteraction` if any step needs a prompt and we're
    /// non-interactive (short-circuits).
    /// Returns `RequiresPrompt` if at least one step needs human confirmation.
    pub fn decide_plan(&self, plan: &AuthorizedPlan) -> ApprovalDecision {
        let mut worst = ApprovalDecision::AutoApproved;
        for step in plan.steps() {
            let d = self.decide_step(step.risk_level());
            match d {
                // These are terminal — return immediately.
                ApprovalDecision::ExceedsCeiling(_) => return d,
                ApprovalDecision::RequiresInteraction => return d,
                // Escalate: RequiresPrompt > AutoApproved.
                ApprovalDecision::RequiresPrompt => worst = ApprovalDecision::RequiresPrompt,
                ApprovalDecision::AutoApproved => {}
            }
        }
        worst
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sysknife_brain::action_name::ActionName;
    use sysknife_brain::planner::{Plan, PlanStep};

    fn step(risk: PlanRiskLevel) -> PlanStep {
        PlanStep::new(
            ActionName::parse("GetDiskUsage").unwrap(),
            "test step".into(),
            risk,
            serde_json::json!({}),
        )
        .unwrap()
    }

    // The policy gates on authoritative risk, so tests feed the desired risk
    // levels through `assume_authorized` (the risks here *are* the values under
    // test — no ActionSpec substitution needed).
    fn plan(risks: &[PlanRiskLevel]) -> AuthorizedPlan {
        let steps: Vec<PlanStep> = risks.iter().map(|r| step(r.clone())).collect();
        Plan::new(
            "test".into(),
            "test plan".into(),
            "test explanation".into(),
            steps,
        )
        .unwrap()
        .assume_authorized()
    }

    fn policy(
        yes: bool,
        max_risk: Option<MaxRisk>,
        non_interactive: bool,
        dry_run: bool,
    ) -> ApprovalPolicy {
        ApprovalPolicy::new(yes, max_risk, non_interactive, dry_run)
    }

    // --- Step-level: security policy ---

    // --yes alone never approves Medium
    #[test]
    fn yes_alone_requires_prompt_for_medium() {
        let p = policy(true, None, false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Medium),
            ApprovalDecision::RequiresPrompt
        );
    }

    // --yes alone never approves High
    #[test]
    fn yes_alone_requires_prompt_for_high() {
        let p = policy(true, None, false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::High),
            ApprovalDecision::RequiresPrompt
        );
    }

    // --yes alone auto-approves Low
    #[test]
    fn yes_alone_auto_approves_low() {
        let p = policy(true, None, false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Low),
            ApprovalDecision::AutoApproved
        );
    }

    // --yes --max-risk medium approves Medium
    #[test]
    fn yes_max_risk_medium_auto_approves_medium() {
        let p = policy(true, Some(MaxRisk::Medium), false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Medium),
            ApprovalDecision::AutoApproved
        );
    }

    // --yes --max-risk high does NOT auto-approve High (hardcoded ceiling)
    #[test]
    fn yes_max_risk_high_does_not_auto_approve_high() {
        let p = policy(true, Some(MaxRisk::High), false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::High),
            ApprovalDecision::RequiresPrompt
        );
    }

    // --yes --max-risk high auto-approves Medium (ceiling clamps to Medium)
    #[test]
    fn yes_max_risk_high_auto_approves_medium() {
        let p = policy(true, Some(MaxRisk::High), false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Medium),
            ApprovalDecision::AutoApproved
        );
    }

    // --max-risk medium with High step → ExceedsCeiling
    #[test]
    fn max_risk_ceiling_exceeds_for_high_step() {
        let p = policy(false, Some(MaxRisk::Medium), false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::High),
            ApprovalDecision::ExceedsCeiling(MaxRisk::Medium)
        );
    }

    // --max-risk low with Medium step → ExceedsCeiling
    #[test]
    fn max_risk_low_ceiling_exceeds_for_medium_step() {
        let p = policy(false, Some(MaxRisk::Low), false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Medium),
            // The payload must be the ceiling that was configured, not the
            // risk that breached it.
            ApprovalDecision::ExceedsCeiling(MaxRisk::Low)
        );
    }

    // --max-risk high with High step and no --yes:
    // MaxRisk::High.includes(High) = true so ceiling does NOT fire ExceedsCeiling;
    // no auto-approve ceiling (yes=false); falls through to RequiresPrompt.
    // Confirms --max-risk high never rejects — it only gates auto-approval.
    #[test]
    fn max_risk_high_no_yes_high_step_requires_prompt() {
        let p = policy(false, Some(MaxRisk::High), false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::High),
            ApprovalDecision::RequiresPrompt
        );
    }

    // --non-interactive with Medium step → RequiresInteraction
    #[test]
    fn non_interactive_no_yes_requires_interaction_for_medium() {
        let p = policy(false, None, true, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Medium),
            ApprovalDecision::RequiresInteraction
        );
    }

    // --non-interactive --yes with Low → AutoApproved
    #[test]
    fn non_interactive_yes_auto_approves_low() {
        let p = policy(true, None, true, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Low),
            ApprovalDecision::AutoApproved
        );
    }

    // --non-interactive --yes with Medium → RequiresInteraction (auto-ceiling is Low)
    #[test]
    fn non_interactive_yes_requires_interaction_for_medium() {
        let p = policy(true, None, true, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Medium),
            ApprovalDecision::RequiresInteraction
        );
    }

    // --non-interactive --yes --max-risk high with High step:
    // ceiling passes (High.includes(High)=true), auto-ceiling clamps to Medium
    // (does not include High), falls to non_interactive → RequiresInteraction.
    // Critical CI path: scripted run must hard-fail, not hang on a prompt.
    #[test]
    fn non_interactive_yes_max_risk_high_high_step_requires_interaction() {
        let p = policy(true, Some(MaxRisk::High), true, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::High),
            ApprovalDecision::RequiresInteraction
        );
    }

    // --dry-run with any risk → AutoApproved
    #[test]
    fn dry_run_auto_approves_low() {
        let p = policy(false, None, false, true);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Low),
            ApprovalDecision::AutoApproved
        );
    }

    #[test]
    fn dry_run_auto_approves_medium() {
        let p = policy(false, None, false, true);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Medium),
            ApprovalDecision::AutoApproved
        );
    }

    #[test]
    fn dry_run_auto_approves_high() {
        let p = policy(false, None, false, true);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::High),
            ApprovalDecision::AutoApproved
        );
    }

    // --dry-run overrides --yes --max-risk high even for a High step.
    // Confirms dry-run short-circuits before any security check fires.
    #[test]
    fn dry_run_overrides_yes_max_risk_high_with_high_step() {
        let p = policy(true, Some(MaxRisk::High), false, true);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::High),
            ApprovalDecision::AutoApproved
        );
    }

    // No flags at all — Low step still RequiresPrompt (no --yes)
    #[test]
    fn no_flags_low_step_requires_prompt() {
        let p = policy(false, None, false, false);
        assert_eq!(
            p.decide_step(&PlanRiskLevel::Low),
            ApprovalDecision::RequiresPrompt
        );
    }

    // --- Plan-level decisions ---

    // Single-step Low plan with --yes → AutoApproved
    #[test]
    fn plan_single_low_step_with_yes_auto_approved() {
        let p = policy(true, None, false, false);
        let pl = plan(&[PlanRiskLevel::Low]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::AutoApproved);
    }

    // All-Low plan with --yes → AutoApproved
    #[test]
    fn plan_all_low_with_yes_auto_approved() {
        let p = policy(true, None, false, false);
        let pl = plan(&[PlanRiskLevel::Low, PlanRiskLevel::Low]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::AutoApproved);
    }

    // All-Low plan without --yes → RequiresPrompt (no auto-approve without --yes)
    #[test]
    fn plan_all_low_no_yes_requires_prompt() {
        let p = policy(false, None, false, false);
        let pl = plan(&[PlanRiskLevel::Low, PlanRiskLevel::Low]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::RequiresPrompt);
    }

    // Plan with one Medium step, --yes → RequiresPrompt
    #[test]
    fn plan_mixed_low_medium_with_yes_requires_prompt() {
        let p = policy(true, None, false, false);
        let pl = plan(&[PlanRiskLevel::Low, PlanRiskLevel::Medium]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::RequiresPrompt);
    }

    // Plan with High step first, --max-risk medium → ExceedsCeiling (early return on step 1)
    #[test]
    fn plan_high_first_exceeds_ceiling() {
        let p = policy(false, Some(MaxRisk::Medium), false, false);
        let pl = plan(&[PlanRiskLevel::High, PlanRiskLevel::Low]);
        assert_eq!(
            p.decide_plan(&pl),
            ApprovalDecision::ExceedsCeiling(MaxRisk::Medium)
        );
    }

    // Plan with High step in the middle → ExceedsCeiling (early return on exceeding step)
    #[test]
    fn plan_high_middle_exceeds_ceiling() {
        let p = policy(false, Some(MaxRisk::Medium), false, false);
        let pl = plan(&[PlanRiskLevel::Low, PlanRiskLevel::High, PlanRiskLevel::Low]);
        assert_eq!(
            p.decide_plan(&pl),
            ApprovalDecision::ExceedsCeiling(MaxRisk::Medium)
        );
    }

    // Plan with High step second, --max-risk medium → ExceedsCeiling
    #[test]
    fn plan_high_step_with_max_risk_medium_exceeds_ceiling() {
        let p = policy(true, Some(MaxRisk::Medium), false, false);
        let pl = plan(&[PlanRiskLevel::Low, PlanRiskLevel::High]);
        assert_eq!(
            p.decide_plan(&pl),
            ApprovalDecision::ExceedsCeiling(MaxRisk::Medium)
        );
    }

    // Single High step with --yes --max-risk high → RequiresPrompt.
    // Plan-level anchor for the core security invariant.
    #[test]
    fn plan_single_high_step_yes_max_risk_high_requires_prompt() {
        let p = policy(true, Some(MaxRisk::High), false, false);
        let pl = plan(&[PlanRiskLevel::High]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::RequiresPrompt);
    }

    // All-High plan with --yes --max-risk high → RequiresPrompt.
    // The most dangerous plan a user can construct always requires human confirmation.
    #[test]
    fn plan_all_high_yes_max_risk_high_requires_prompt() {
        let p = policy(true, Some(MaxRisk::High), false, false);
        let pl = plan(&[PlanRiskLevel::High, PlanRiskLevel::High]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::RequiresPrompt);
    }

    // Plan with Medium step, --non-interactive --yes → RequiresInteraction
    #[test]
    fn plan_medium_step_non_interactive_yes_requires_interaction() {
        let p = policy(true, None, true, false);
        let pl = plan(&[PlanRiskLevel::Low, PlanRiskLevel::Medium]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::RequiresInteraction);
    }

    // Plan with High step, --non-interactive --yes --max-risk high → RequiresInteraction.
    // CI must fail fast rather than hang waiting for input.
    #[test]
    fn plan_non_interactive_yes_max_risk_high_high_step_requires_interaction() {
        let p = policy(true, Some(MaxRisk::High), true, false);
        let pl = plan(&[PlanRiskLevel::Low, PlanRiskLevel::High]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::RequiresInteraction);
    }

    // Plan with mixed risk levels under --dry-run → AutoApproved (vacuous approval)
    #[test]
    fn plan_dry_run_multi_step_auto_approved() {
        let p = policy(false, None, false, true);
        let pl = plan(&[
            PlanRiskLevel::Low,
            PlanRiskLevel::Medium,
            PlanRiskLevel::High,
        ]);
        assert_eq!(p.decide_plan(&pl), ApprovalDecision::AutoApproved);
    }

    // --- effective_auto_ceiling ---

    #[test]
    fn effective_ceiling_none_without_yes() {
        let p = policy(false, None, false, false);
        assert!(p.effective_auto_ceiling().is_none());
    }

    #[test]
    fn effective_ceiling_low_with_bare_yes() {
        let p = policy(true, None, false, false);
        assert_eq!(p.effective_auto_ceiling(), Some(MaxRisk::Low));
    }

    #[test]
    fn effective_ceiling_medium_with_yes_max_risk_medium() {
        let p = policy(true, Some(MaxRisk::Medium), false, false);
        assert_eq!(p.effective_auto_ceiling(), Some(MaxRisk::Medium));
    }

    #[test]
    fn effective_ceiling_clamped_to_medium_with_yes_max_risk_high() {
        let p = policy(true, Some(MaxRisk::High), false, false);
        assert_eq!(p.effective_auto_ceiling(), Some(MaxRisk::Medium));
    }
}