planwarden 0.1.0

CLI planning enforcer for AI agents
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
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlanKind {
    Roadmap,
    Task,
}

impl PlanKind {
    fn id_prefix(self) -> &'static str {
        match self {
            Self::Roadmap => "R",
            Self::Task => "T",
        }
    }
}

impl From<PlanKind> for PlanDocumentKind {
    fn from(value: PlanKind) -> Self {
        match value {
            PlanKind::Roadmap => Self::Roadmap,
            PlanKind::Task => Self::Task,
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReviewRequest {
    #[serde(default)]
    pub title: Option<String>,
    pub goal: String,
    #[serde(default)]
    pub facts: Vec<String>,
    #[serde(default)]
    pub constraints: Vec<String>,
    #[serde(default)]
    pub acceptance_criteria: Vec<String>,
    #[serde(default)]
    pub unknowns: Vec<String>,
    #[serde(default)]
    pub risks: Vec<String>,
    pub signals: ReviewSignals,
    #[serde(default)]
    pub proposed_slices: Vec<ProposedSlice>,
    pub concerns: ReviewConcerns,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReviewSignals {
    #[serde(default)]
    pub bugfix: bool,
    #[serde(default)]
    pub user_visible: bool,
    #[serde(default)]
    pub touches_authentication: bool,
    #[serde(default)]
    pub touches_authorization: bool,
    #[serde(default)]
    pub touches_sensitive_data: bool,
    #[serde(default)]
    pub touches_external_boundary: bool,
    #[serde(default)]
    pub touches_database_schema: bool,
    #[serde(default)]
    pub cross_cutting_change: bool,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProposedSlice {
    pub title: String,
    pub summary: String,
    pub estimated_minutes: u32,
    #[serde(default)]
    pub dependencies: Vec<String>,
    #[serde(default)]
    pub acceptance_criteria: Vec<String>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct Concern {
    pub applicable: bool,
    #[serde(default)]
    pub reason: Option<String>,
    #[serde(default)]
    pub approach: Option<String>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct TestConcerns {
    pub unit: Concern,
    pub integration: Concern,
    pub regression: Concern,
    pub smoke: Concern,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct ReviewConcerns {
    pub rollback: Concern,
    pub security: Concern,
    pub authentication: Concern,
    pub authorization: Concern,
    pub decoupling: Concern,
    pub tests: TestConcerns,
    pub bugfix_red: Concern,
}

#[derive(Debug, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ReviewDecision {
    Blocked,
    NeedsInput,
    Ready,
}

#[derive(Debug, Serialize, PartialEq, Eq)]
pub struct Issue {
    pub code: String,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub field: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct ReviewQuestion {
    pub code: String,
    pub prompt: String,
}

#[derive(Debug, Serialize)]
pub struct ReviewResponse {
    pub decision: ReviewDecision,
    pub missing: Vec<Issue>,
    pub questions: Vec<ReviewQuestion>,
    pub pushback: Vec<Issue>,
    pub normalized_plan: NormalizedPlan,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PlanDocumentKind {
    Roadmap,
    Task,
}

impl PlanDocumentKind {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Roadmap => "roadmap",
            Self::Task => "task",
        }
    }

    pub fn directory(&self) -> &'static str {
        match self {
            Self::Roadmap => "roadmaps",
            Self::Task => "tasks",
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NormalizedPlan {
    pub kind: PlanDocumentKind,
    pub plan_status: PlanLifecycleStatus,
    pub title: String,
    pub goal: String,
    pub facts: Vec<String>,
    pub constraints: Vec<String>,
    pub acceptance_criteria: Vec<String>,
    pub risks: Vec<String>,
    pub concerns: ReviewConcerns,
    #[serde(default)]
    pub open_questions: Vec<ReviewQuestion>,
    pub items: Vec<NormalizedPlanItem>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct NormalizedPlanItem {
    pub id: String,
    pub status: PlanItemStatus,
    pub title: String,
    pub summary: String,
    pub estimated_minutes: u32,
    pub dependencies: Vec<String>,
    pub acceptance_criteria: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PlanItemStatus {
    Todo,
    InProgress,
    Done,
}

impl PlanItemStatus {
    pub fn checkbox(self) -> &'static str {
        match self {
            Self::Todo => "[ ]",
            Self::InProgress => "[-]",
            Self::Done => "[x]",
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PlanLifecycleStatus {
    Draft,
    Approved,
    InProgress,
    Done,
}

impl PlanLifecycleStatus {
    pub fn label(self) -> &'static str {
        match self {
            Self::Draft => "draft",
            Self::Approved => "approved",
            Self::InProgress => "in_progress",
            Self::Done => "done",
        }
    }
}

pub fn review_request(kind: PlanKind, request: ReviewRequest) -> ReviewResponse {
    let mut missing = Vec::new();
    let mut pushback = Vec::new();
    let mut questions = Vec::new();

    if request.goal.trim().is_empty() {
        missing.push(issue("goal_missing", "Goal is required.", Some("goal")));
    }

    if request.acceptance_criteria.is_empty() {
        missing.push(issue(
            "acceptance_criteria_missing",
            "At least one top-level acceptance criterion is required.",
            Some("acceptance_criteria"),
        ));
    }

    if request.proposed_slices.is_empty() {
        missing.push(issue(
            "proposed_slices_missing",
            "At least one proposed slice is required.",
            Some("proposed_slices"),
        ));
    }

    validate_concern(
        "rollback",
        "concerns.rollback",
        &request.concerns.rollback,
        &mut missing,
    );
    validate_concern(
        "security",
        "concerns.security",
        &request.concerns.security,
        &mut missing,
    );
    validate_concern(
        "authentication",
        "concerns.authentication",
        &request.concerns.authentication,
        &mut missing,
    );
    validate_concern(
        "authorization",
        "concerns.authorization",
        &request.concerns.authorization,
        &mut missing,
    );
    validate_concern(
        "decoupling",
        "concerns.decoupling",
        &request.concerns.decoupling,
        &mut missing,
    );
    validate_concern(
        "unit_tests",
        "concerns.tests.unit",
        &request.concerns.tests.unit,
        &mut missing,
    );
    validate_concern(
        "integration_tests",
        "concerns.tests.integration",
        &request.concerns.tests.integration,
        &mut missing,
    );
    validate_concern(
        "regression_tests",
        "concerns.tests.regression",
        &request.concerns.tests.regression,
        &mut missing,
    );
    validate_concern(
        "smoke_tests",
        "concerns.tests.smoke",
        &request.concerns.tests.smoke,
        &mut missing,
    );
    validate_concern(
        "bugfix_red",
        "concerns.bugfix_red",
        &request.concerns.bugfix_red,
        &mut missing,
    );

    if request.signals.touches_database_schema && !request.concerns.rollback.applicable {
        pushback.push(issue(
            "rollback_inconsistent",
            "Rollback cannot be marked not applicable when the change touches database schema.",
            Some("concerns.rollback"),
        ));
    }

    if request.signals.touches_authentication && !request.concerns.authentication.applicable {
        pushback.push(issue(
            "authentication_inconsistent",
            "Authentication review cannot be marked not applicable when authentication is affected.",
            Some("concerns.authentication"),
        ));
    }

    if request.signals.touches_authorization && !request.concerns.authorization.applicable {
        pushback.push(issue(
            "authorization_inconsistent",
            "Authorization review cannot be marked not applicable when permissions are affected.",
            Some("concerns.authorization"),
        ));
    }

    if (request.signals.touches_sensitive_data || request.signals.touches_external_boundary)
        && !request.concerns.security.applicable
    {
        pushback.push(issue(
            "security_inconsistent",
            "Security review cannot be marked not applicable when the change touches sensitive data or an external boundary.",
            Some("concerns.security"),
        ));
    }

    if request.signals.cross_cutting_change && !request.concerns.decoupling.applicable {
        pushback.push(issue(
            "decoupling_inconsistent",
            "Decoupling cannot be marked not applicable for a cross-cutting change.",
            Some("concerns.decoupling"),
        ));
    }

    if request.signals.bugfix && !request.concerns.bugfix_red.applicable {
        pushback.push(issue(
            "bugfix_red_inconsistent",
            "Bugfix work must include explicit red-proof handling.",
            Some("concerns.bugfix_red"),
        ));
    }

    if request.signals.user_visible
        && !request.concerns.tests.regression.applicable
        && !request.concerns.tests.smoke.applicable
    {
        pushback.push(issue(
            "user_visible_test_gap",
            "User-visible work must include regression or smoke coverage, or both.",
            Some("concerns.tests"),
        ));
    }

    for (index, slice) in request.proposed_slices.iter().enumerate() {
        let slice_number = index + 1;
        if slice.title.trim().is_empty() {
            missing.push(issue(
                format!("slice_{slice_number}_title_missing"),
                format!("Slice {slice_number} is missing a title."),
                Some(format!("proposed_slices[{index}].title")),
            ));
        }

        if slice.summary.trim().is_empty() {
            missing.push(issue(
                format!("slice_{slice_number}_summary_missing"),
                format!("Slice {slice_number} is missing a summary."),
                Some(format!("proposed_slices[{index}].summary")),
            ));
        }

        if slice.acceptance_criteria.is_empty() {
            missing.push(issue(
                format!("slice_{slice_number}_acceptance_missing"),
                format!("Slice {slice_number} needs at least one acceptance criterion."),
                Some(format!("proposed_slices[{index}].acceptance_criteria")),
            ));
        }

        if slice.estimated_minutes > 90 {
            pushback.push(issue(
                format!("slice_{slice_number}_too_large"),
                format!(
                    "Slice {slice_number} is estimated at {} minutes; split it into a smaller chunk.",
                    slice.estimated_minutes
                ),
                Some(format!("proposed_slices[{index}].estimated_minutes")),
            ));
        }
    }

    for (index, unknown) in request.unknowns.iter().enumerate() {
        if !unknown.trim().is_empty() {
            questions.push(ReviewQuestion {
                code: format!("unknown_{}", index + 1),
                prompt: format!("Resolve this before finalizing the plan: {unknown}"),
            });
        }
    }

    let normalized_plan = normalize_plan(kind, &request, &questions);

    let decision = if !pushback.is_empty() {
        ReviewDecision::Blocked
    } else if !missing.is_empty() || !questions.is_empty() {
        ReviewDecision::NeedsInput
    } else {
        ReviewDecision::Ready
    };

    ReviewResponse {
        decision,
        missing,
        questions,
        pushback,
        normalized_plan,
    }
}

fn normalize_plan(
    kind: PlanKind,
    request: &ReviewRequest,
    questions: &[ReviewQuestion],
) -> NormalizedPlan {
    let title = request
        .title
        .as_deref()
        .filter(|value| !value.trim().is_empty())
        .unwrap_or(&request.goal)
        .to_string();

    let items = request
        .proposed_slices
        .iter()
        .into_iter()
        .enumerate()
        .map(|(index, slice)| NormalizedPlanItem {
            id: format!("{}{}", kind.id_prefix(), index + 1),
            status: PlanItemStatus::Todo,
            title: slice.title.clone(),
            summary: slice.summary.clone(),
            estimated_minutes: slice.estimated_minutes,
            dependencies: slice.dependencies.clone(),
            acceptance_criteria: slice.acceptance_criteria.clone(),
        })
        .collect();

    NormalizedPlan {
        kind: kind.into(),
        plan_status: PlanLifecycleStatus::Draft,
        title,
        goal: request.goal.clone(),
        facts: request.facts.clone(),
        constraints: request.constraints.clone(),
        acceptance_criteria: request.acceptance_criteria.clone(),
        risks: request.risks.clone(),
        concerns: request.concerns.clone(),
        open_questions: questions.to_vec(),
        items,
    }
}

fn validate_concern(name: &str, field: &str, concern: &Concern, missing: &mut Vec<Issue>) {
    if concern.applicable {
        if blank(&concern.approach) {
            missing.push(issue(
                format!("{name}_approach_missing"),
                format!("{name} is marked applicable but has no approach."),
                Some(field),
            ));
        }
    } else if blank(&concern.reason) {
        missing.push(issue(
            format!("{name}_reason_missing"),
            format!("{name} is marked not applicable but has no justification."),
            Some(field),
        ));
    }
}

fn blank(value: &Option<String>) -> bool {
    value
        .as_deref()
        .map(|item| item.trim().is_empty())
        .unwrap_or(true)
}

fn issue<C, M, F>(code: C, message: M, field: Option<F>) -> Issue
where
    C: Into<String>,
    M: Into<String>,
    F: Into<String>,
{
    Issue {
        code: code.into(),
        message: message.into(),
        field: field.map(Into::into),
    }
}

#[cfg(test)]
mod tests {
    use super::{PlanKind, ReviewDecision, ReviewRequest, review_request};

    fn ready_request() -> ReviewRequest {
        serde_json::from_str(
            r#"
            {
              "title": "Add billing portal",
              "goal": "Add a billing portal entry point under settings.",
              "facts": ["Stripe integration already exists."],
              "constraints": ["Must be rollbackable."],
              "acceptance_criteria": ["Users can open the billing portal from settings."],
              "unknowns": [],
              "risks": ["Incorrect tenant mapping could expose the wrong portal session."],
              "signals": {
                "bugfix": false,
                "user_visible": true,
                "touches_authentication": false,
                "touches_authorization": true,
                "touches_sensitive_data": true,
                "touches_external_boundary": true,
                "touches_database_schema": false,
                "cross_cutting_change": true
              },
              "proposed_slices": [
                {
                  "title": "Wire settings action",
                  "summary": "Add the settings action that creates a portal session through the existing backend endpoint.",
                  "estimated_minutes": 45,
                  "dependencies": [],
                  "acceptance_criteria": ["The settings page shows a billing portal action for eligible users."]
                }
              ],
              "concerns": {
                "rollback": {
                  "applicable": true,
                  "approach": "Guard the new entry point behind a feature flag that can be disabled."
                },
                "security": {
                  "applicable": true,
                  "approach": "Reuse the existing server-side portal session creation path and keep Stripe keys server-only."
                },
                "authentication": {
                  "applicable": false,
                  "reason": "The change reuses the existing authenticated session model without altering login flows."
                },
                "authorization": {
                  "applicable": true,
                  "approach": "Only render the action for owners and enforce the same role check on the server."
                },
                "decoupling": {
                  "applicable": true,
                  "approach": "Keep billing wiring inside the settings and billing modules without spreading Stripe calls into unrelated views."
                },
                "tests": {
                  "unit": {
                    "applicable": true,
                    "approach": "Cover the visibility predicate for billing portal access."
                  },
                  "integration": {
                    "applicable": true,
                    "approach": "Add an integration test for portal session creation and redirect handling."
                  },
                  "regression": {
                    "applicable": true,
                    "approach": "Add a regression test for owner visibility on the settings page."
                  },
                  "smoke": {
                    "applicable": true,
                    "approach": "Exercise the happy path in a smoke check against the settings flow."
                  }
                },
                "bugfix_red": {
                  "applicable": false,
                  "reason": "This is feature work, not a bug fix."
                }
              }
            }
            "#,
        )
        .expect("request should deserialize")
    }

    #[test]
    fn review_ready_when_contract_is_satisfied() {
        let response = review_request(PlanKind::Roadmap, ready_request());

        assert_eq!(response.decision, ReviewDecision::Ready);
        assert!(response.missing.is_empty());
        assert!(response.questions.is_empty());
        assert!(response.pushback.is_empty());
        assert_eq!(response.normalized_plan.items[0].id, "R1");
        assert_eq!(response.normalized_plan.kind.label(), "roadmap");
    }

    #[test]
    fn review_needs_input_for_missing_approach_and_unknowns() {
        let mut request = ready_request();
        request.unknowns =
            vec!["Confirm whether billing is owner-only or admin-accessible.".into()];
        request.concerns.security.approach = None;

        let response = review_request(PlanKind::Task, request);

        assert_eq!(response.decision, ReviewDecision::NeedsInput);
        assert_eq!(response.questions.len(), 1);
        assert_eq!(response.normalized_plan.open_questions.len(), 1);
        assert!(
            response
                .missing
                .iter()
                .any(|issue| issue.code == "security_approach_missing")
        );
    }

    #[test]
    fn review_blocks_inconsistent_auth_and_oversized_slice() {
        let mut request = ready_request();
        request.signals.touches_authorization = true;
        request.concerns.authorization.applicable = false;
        request.concerns.authorization.reason = Some("Not needed.".into());
        request.proposed_slices[0].estimated_minutes = 180;

        let response = review_request(PlanKind::Task, request);

        assert_eq!(response.decision, ReviewDecision::Blocked);
        assert!(
            response
                .pushback
                .iter()
                .any(|issue| issue.code == "authorization_inconsistent")
        );
        assert!(
            response
                .pushback
                .iter()
                .any(|issue| issue.code == "slice_1_too_large")
        );
    }

    #[test]
    fn review_blocks_bugfix_without_red_proof() {
        let mut request = ready_request();
        request.signals.bugfix = true;
        request.concerns.bugfix_red.applicable = false;
        request.concerns.bugfix_red.reason = Some("We can skip this.".into());

        let response = review_request(PlanKind::Task, request);

        assert_eq!(response.decision, ReviewDecision::Blocked);
        assert!(
            response
                .pushback
                .iter()
                .any(|issue| issue.code == "bugfix_red_inconsistent")
        );
    }

    #[test]
    fn review_request_requires_declared_signals_and_concerns() {
        let invalid = serde_json::from_str::<ReviewRequest>(
            r#"
            {
              "goal": "Missing required structures."
            }
            "#,
        );

        assert!(invalid.is_err());
    }

    #[test]
    fn review_request_rejects_unknown_fields() {
        let invalid = serde_json::from_str::<ReviewRequest>(
            r#"
            {
              "goal": "Test unknown field handling.",
              "facts": [],
              "constraints": [],
              "acceptance_criteria": ["It works."],
              "unknowns": [],
              "risks": [],
              "signals": {
                "bugfix": false,
                "user_visible": false,
                "touches_authentication": false,
                "touches_authorization": false,
                "touches_sensitive_data": false,
                "touches_external_boundary": false,
                "touches_database_schema": false,
                "cross_cutting_change": false
              },
              "proposed_slices": [{
                "title": "One slice",
                "summary": "Do one thing.",
                "estimated_minutes": 30,
                "acceptance_criteria": ["Still works."]
              }],
              "concerns": {
                "rollback": {"applicable": true, "approach": "Revert the commit."},
                "security": {"applicable": false, "reason": "No boundary changes."},
                "authentication": {"applicable": false, "reason": "No auth changes."},
                "authorization": {"applicable": false, "reason": "No permission changes."},
                "decoupling": {"applicable": true, "approach": "Keep it isolated."},
                "tests": {
                  "unit": {"applicable": true, "approach": "Unit test it."},
                  "integration": {"applicable": false, "reason": "No integration boundary."},
                  "regression": {"applicable": false, "reason": "No user-visible change."},
                  "smoke": {"applicable": false, "reason": "No smoke needed."}
                },
                "bugfix_red": {"applicable": false, "reason": "Not a bug fix."}
              },
              "surprise": true
            }
            "#,
        );

        assert!(invalid.is_err());
    }
}