codewhale-tui 0.9.8

Terminal UI for open-source and open-weight coding models
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
//! Invariant validation — fail closed.
//!
//! [`validate`] checks every whole-snapshot invariant (V1–V8 below, plus
//! structural well-formedness). The reducer calls it on the candidate
//! snapshot after every change and rejects the change on any violation,
//! leaving the input snapshot untouched. There is no fail-open path: if a
//! node cannot be verified, it does not become Verified — verification
//! infrastructure trouble must surface as a rejection (callers then mark the
//! node Blocked), never as silently-assumed success.
//!
//! Invariants:
//! - V1  `DependsOn` edges are acyclic.
//! - V2  every live (`Initializing`/`Active`/`Waiting`) Operation reaches an
//!   Objective/PlanStep via `Contains` ancestry — no orphaned live work.
//! - V3  `binding.is_some()` ⇒ `kind == Operation`.
//! - V4  `Verified` ⇒ acceptance non-empty ⇒ a `Verifies`-edge evidence path
//!   satisfies every requirement. Completion is never verification.
//! - V5  `Blocked` ⇒ an incoming `Blocks` edge, an unmet `DependsOn`, or a
//!   pending `RequiresApproval` path exists.
//! - V6  each binding's `external` matches exactly one identity scheme and no
//!   two operations bind the same external identity.
//! - V7  `RuntimeRef`/`LaneRef` nodes never carry liveness state — the
//!   owning subsystems are the only liveness source.
//! - V8  history is bounded and its revisions strictly increase.
//! - V9  terminal states are never overwritten except via explicit
//!   `Supersede` (enforced in the reducer, which sees the predecessor
//!   snapshot; single-snapshot validation cannot observe overwrites).
//! - V10 compat projections are pure functions of the snapshot — enforced at
//!   the type level: projection functions take `&WorkGraphSnapshot` (see
//!   `compat.rs`); nothing hands them mutable graph access.

use std::collections::{HashMap, HashSet};

use serde::{Deserialize, Serialize};

use super::ids::WorkNodeId;
use super::model::{
    ACTIVITY_CAP, EdgeKind, HISTORY_CAP, NodeKind, NodeState, SCHEMA_VERSION, WorkActivityEvent,
    WorkGraphSnapshot, WorkNode, external_identity_is_well_formed,
};

/// Which rule a violation belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ValidationCode {
    /// Basic well-formedness (unique IDs, resolvable endpoints, schema).
    Structural,
    V1,
    V2,
    V3,
    V4,
    V5,
    V6,
    V7,
    V8,
    V9,
    /// Never emitted at runtime: enforced by projection function signatures.
    V10,
}

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

/// Result of a failed validation. A change producing any violation is
/// rejected wholesale; the pre-change snapshot is returned to the caller
/// untouched.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ValidationReport {
    pub violations: Vec<Violation>,
}

impl ValidationReport {
    #[must_use]
    pub fn single(code: ValidationCode, message: impl Into<String>) -> Self {
        ValidationReport {
            violations: vec![Violation {
                code,
                message: message.into(),
            }],
        }
    }

    #[must_use]
    pub fn contains_code(&self, code: ValidationCode) -> bool {
        self.violations.iter().any(|v| v.code == code)
    }
}

impl std::fmt::Display for ValidationReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "work graph validation failed:")?;
        for v in &self.violations {
            write!(f, " [{:?}] {};", v.code, v.message)?;
        }
        Ok(())
    }
}

impl std::error::Error for ValidationReport {}

/// Validate a whole snapshot. `Ok(())` or every violation found.
pub fn validate(snapshot: &WorkGraphSnapshot) -> Result<(), ValidationReport> {
    let mut violations = Vec::new();

    check_structural(snapshot, &mut violations);
    check_v1_depends_on_acyclic(snapshot, &mut violations);
    check_v2_live_operations_rooted(snapshot, &mut violations);
    check_v3_binding_only_on_operations(snapshot, &mut violations);
    check_v4_verified_requires_evidence(snapshot, &mut violations);
    check_v5_blocked_has_cause(snapshot, &mut violations);
    check_v6_binding_identity(snapshot, &mut violations);
    check_v7_refs_inert(snapshot, &mut violations);
    check_v8_history_bounded_monotonic(snapshot, &mut violations);

    if violations.is_empty() {
        Ok(())
    } else {
        Err(ValidationReport { violations })
    }
}

fn check_structural(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    if snapshot.schema != SCHEMA_VERSION {
        out.push(Violation {
            code: ValidationCode::Structural,
            message: format!("unknown schema {}", snapshot.schema),
        });
    }
    let mut node_ids = HashSet::new();
    for node in &snapshot.nodes {
        if !node_ids.insert(&node.id) {
            out.push(Violation {
                code: ValidationCode::Structural,
                message: format!("duplicate node id {}", node.id),
            });
        }
        if node.evidence.is_some() && !matches!(node.kind, NodeKind::Evidence) {
            out.push(Violation {
                code: ValidationCode::Structural,
                message: format!(
                    "node {} carries evidence but is not an Evidence node",
                    node.id
                ),
            });
        }
    }
    let mut edge_ids = HashSet::new();
    for edge in &snapshot.edges {
        if !edge_ids.insert(&edge.id) {
            out.push(Violation {
                code: ValidationCode::Structural,
                message: format!("duplicate edge id {}", edge.id),
            });
        }
        for endpoint in [&edge.from, &edge.to] {
            if !node_ids.contains(endpoint) {
                out.push(Violation {
                    code: ValidationCode::Structural,
                    message: format!("edge {} references missing node {}", edge.id, endpoint),
                });
            }
        }
    }

    let mut plan_ids = HashSet::new();
    for id in &snapshot.compat.plan_order {
        if !plan_ids.insert(id) {
            out.push(Violation {
                code: ValidationCode::Structural,
                message: format!("duplicate plan projection node {id}"),
            });
        }
        match snapshot.node(id) {
            Some(node) if matches!(node.kind, NodeKind::PlanStep) => {}
            Some(_) => out.push(Violation {
                code: ValidationCode::Structural,
                message: format!("plan projection node {id} is not a PlanStep"),
            }),
            None => out.push(Violation {
                code: ValidationCode::Structural,
                message: format!("plan projection references missing node {id}"),
            }),
        }
    }

    let mut todo_ids = HashSet::new();
    let mut active_todos = 0usize;
    for binding in &snapshot.compat.todos {
        if binding.legacy_id == 0 || !todo_ids.insert(binding.legacy_id) {
            out.push(Violation {
                code: ValidationCode::Structural,
                message: format!("invalid or duplicate legacy To-do id {}", binding.legacy_id),
            });
        }
        match snapshot.node(&binding.node) {
            Some(node) => {
                if node.kind != NodeKind::PlanStep {
                    out.push(Violation {
                        code: ValidationCode::Structural,
                        message: format!(
                            "To-do projection {} node {} is not a PlanStep",
                            binding.legacy_id, binding.node
                        ),
                    });
                }
                if matches!(node.state, NodeState::Active) {
                    active_todos += 1;
                }
            }
            None => out.push(Violation {
                code: ValidationCode::Structural,
                message: format!(
                    "To-do projection {} references missing node {}",
                    binding.legacy_id, binding.node
                ),
            }),
        }
        if let Some(index) = binding.plan_index {
            let aliased = usize::try_from(index)
                .ok()
                .and_then(|index| snapshot.compat.plan_order.get(index));
            if aliased != Some(&binding.node) {
                out.push(Violation {
                    code: ValidationCode::Structural,
                    message: format!(
                        "To-do projection {} has an invalid plan alias",
                        binding.legacy_id
                    ),
                });
            }
        }
    }
    if active_todos > 1 {
        out.push(Violation {
            code: ValidationCode::Structural,
            message: "legacy To-do projection has more than one active row".to_string(),
        });
    }

    if snapshot.activities.len() > ACTIVITY_CAP {
        out.push(Violation {
            code: ValidationCode::Structural,
            message: format!(
                "activity length {} exceeds bound {ACTIVITY_CAP}",
                snapshot.activities.len()
            ),
        });
    }
    for activity in snapshot.activities.iter() {
        let (requested, effective, provider_kind, provider, endpoint_identity, model, operation) =
            match activity {
                WorkActivityEvent::ReasoningEffortChanged {
                    requested,
                    effective,
                    provider_kind,
                    provider,
                    endpoint_identity,
                    model,
                    operation,
                    ..
                } => (
                    requested,
                    effective,
                    provider_kind,
                    provider,
                    endpoint_identity,
                    model,
                    operation,
                ),
            };
        if matches!(
            requested,
            super::ReasoningEffortTier::ThinkingEnabledGranularityUnavailable
                | super::ReasoningEffortTier::Unavailable
        ) {
            out.push(Violation {
                code: ValidationCode::Structural,
                message: "requested reasoning effort is not an operator-selectable tier"
                    .to_string(),
            });
        }
        if provider.is_empty()
            || provider.chars().count() > 128
            || provider
                .chars()
                .any(|ch| ch.is_whitespace() || ch.is_control())
        {
            out.push(Violation {
                code: ValidationCode::Structural,
                message: "activity provider is not a bounded route identity".to_string(),
            });
        }
        let provenance_is_bounded = provider_kind.is_some()
            && endpoint_identity.as_ref().is_some_and(|endpoint| {
                !endpoint.is_empty()
                    && endpoint.chars().count() <= 512
                    && !endpoint.chars().any(char::is_control)
            })
            && model.as_ref().is_some_and(|model| {
                !model.trim().is_empty()
                    && model.chars().count() <= 256
                    && !model.chars().any(char::is_control)
            });
        if !provenance_is_bounded {
            if *effective != super::ReasoningEffortTier::Unavailable {
                out.push(Violation {
                    code: ValidationCode::Structural,
                    message:
                        "activity without bounded route provenance must be effective unavailable"
                            .to_string(),
                });
            }
        } else {
            let api_provider = provider_kind.expect("provenance bounded above");
            if api_provider != crate::config::ApiProvider::Custom
                && provider != api_provider.as_str()
            {
                out.push(Violation {
                    code: ValidationCode::Structural,
                    message: "activity provider identity does not match its recorded kind"
                        .to_string(),
                });
                continue;
            }
            let constrained = match api_provider {
                crate::config::ApiProvider::Custom => Some(super::ReasoningEffortTier::Unavailable),
                api_provider => super::model::constrained_effective_reasoning_for_route(
                    *requested,
                    api_provider,
                    endpoint_identity.as_deref().expect("bounded above"),
                    model.as_deref().expect("bounded above"),
                ),
            };
            if constrained.is_some_and(|expected| *effective != expected) {
                out.push(Violation {
                    code: ValidationCode::Structural,
                    message: "activity effective reasoning is impossible for its recorded route"
                        .to_string(),
                });
            } else if constrained.is_none()
                && matches!(
                    effective,
                    super::ReasoningEffortTier::ThinkingEnabledGranularityUnavailable
                )
            {
                out.push(Violation {
                    code: ValidationCode::Structural,
                    message: "granularity-unavailable receipt is not valid for this recorded route"
                        .to_string(),
                });
            }
        }
        if let Some(operation) = operation {
            match snapshot.node(operation) {
                Some(node) if node.kind == NodeKind::Operation => {}
                Some(_) => out.push(Violation {
                    code: ValidationCode::Structural,
                    message: format!("activity operation {operation} is not an Operation node"),
                }),
                None => out.push(Violation {
                    code: ValidationCode::Structural,
                    message: format!("activity references missing operation {operation}"),
                }),
            }
        }
    }
}

/// V1: DFS three-color cycle detection over `DependsOn` edges.
fn check_v1_depends_on_acyclic(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    let mut adjacency: HashMap<&WorkNodeId, Vec<&WorkNodeId>> = HashMap::new();
    for edge in &snapshot.edges {
        if matches!(edge.kind, EdgeKind::DependsOn) {
            adjacency.entry(&edge.from).or_default().push(&edge.to);
        }
    }
    let mut done: HashSet<&WorkNodeId> = HashSet::new();
    let mut in_progress: HashSet<&WorkNodeId> = HashSet::new();

    fn visit<'a>(
        node: &'a WorkNodeId,
        adjacency: &HashMap<&'a WorkNodeId, Vec<&'a WorkNodeId>>,
        done: &mut HashSet<&'a WorkNodeId>,
        in_progress: &mut HashSet<&'a WorkNodeId>,
    ) -> bool {
        if done.contains(node) {
            return true;
        }
        if !in_progress.insert(node) {
            return false; // back edge → cycle
        }
        let acyclic = adjacency
            .get(node)
            .map(|next| next.iter().all(|n| visit(n, adjacency, done, in_progress)))
            .unwrap_or(true);
        in_progress.remove(node);
        done.insert(node);
        acyclic
    }

    for node in &snapshot.nodes {
        if !visit(&node.id, &adjacency, &mut done, &mut in_progress) {
            out.push(Violation {
                code: ValidationCode::V1,
                message: format!("depends_on cycle reachable from node {}", node.id),
            });
            return; // one report is enough; graph is already invalid
        }
    }
}

/// V2: every live Operation climbs `Contains` ancestry to an
/// Objective/PlanStep. `Contains` points parent → child, so we walk incoming
/// edges upward with a visited set (defensive against malformed cycles).
fn check_v2_live_operations_rooted(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    for node in &snapshot.nodes {
        if !(matches!(node.kind, NodeKind::Operation) && node.state.is_live()) {
            continue;
        }
        let mut visited: HashSet<&WorkNodeId> = HashSet::new();
        let mut frontier: Vec<&WorkNodeId> = vec![&node.id];
        let mut rooted = false;
        while let Some(current) = frontier.pop() {
            if !visited.insert(current) {
                continue;
            }
            for edge in &snapshot.edges {
                if matches!(edge.kind, EdgeKind::Contains)
                    && &edge.to == current
                    && let Some(parent) = snapshot.node(&edge.from)
                {
                    if matches!(parent.kind, NodeKind::Objective | NodeKind::PlanStep) {
                        rooted = true;
                    }
                    frontier.push(&parent.id);
                }
            }
            if rooted {
                break;
            }
        }
        if !rooted {
            out.push(Violation {
                code: ValidationCode::V2,
                message: format!(
                    "live operation {} has no Objective/PlanStep ancestry",
                    node.id
                ),
            });
        }
    }
}

fn check_v3_binding_only_on_operations(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    for node in &snapshot.nodes {
        if node.binding.is_some() && !matches!(node.kind, NodeKind::Operation) {
            out.push(Violation {
                code: ValidationCode::V3,
                message: format!("non-operation node {} carries a binding", node.id),
            });
        }
    }
}

/// V4: `Verified` demands non-empty acceptance and, for every requirement, at
/// least one Evidence node linked by a `Verifies` edge whose payload
/// satisfies it. There is no fail-open branch: absence of satisfying
/// evidence — for any reason, including verification infrastructure being
/// unavailable — is a rejection.
fn check_v4_verified_requires_evidence(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    for node in &snapshot.nodes {
        if !matches!(node.state, NodeState::Verified) {
            continue;
        }
        if node.acceptance.is_empty() {
            out.push(Violation {
                code: ValidationCode::V4,
                message: format!("verified node {} has no acceptance requirements", node.id),
            });
            continue;
        }
        let evidence: Vec<&WorkNode> = snapshot
            .edges
            .iter()
            .filter(|e| matches!(e.kind, EdgeKind::Verifies) && e.to == node.id)
            .filter_map(|e| snapshot.node(&e.from))
            .filter(|n| matches!(n.kind, NodeKind::Evidence))
            .collect();
        for requirement in &node.acceptance {
            let satisfied = evidence.iter().any(|ev| {
                ev.evidence
                    .as_ref()
                    .is_some_and(|payload| requirement.is_satisfied_by(payload))
            });
            if !satisfied {
                out.push(Violation {
                    code: ValidationCode::V4,
                    message: format!(
                        "verified node {} lacks satisfying evidence for {:?}",
                        node.id, requirement
                    ),
                });
            }
        }
    }
}

/// V5: `Blocked` must have a visible cause.
fn check_v5_blocked_has_cause(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    for node in &snapshot.nodes {
        if !matches!(node.state, NodeState::Blocked) {
            continue;
        }
        let blocked_by_edge = snapshot
            .edges
            .iter()
            .any(|e| matches!(e.kind, EdgeKind::Blocks) && e.to == node.id);
        let unmet_dependency = snapshot.edges.iter().any(|e| {
            matches!(e.kind, EdgeKind::DependsOn)
                && e.from == node.id
                && snapshot
                    .node(&e.to)
                    .is_some_and(|dep| !WorkGraphSnapshot::node_is_done(dep))
        });
        let pending_approval = snapshot.edges.iter().any(|e| {
            matches!(e.kind, EdgeKind::RequiresApproval)
                && e.from == node.id
                && snapshot
                    .node(&e.to)
                    .is_some_and(|approval| !WorkGraphSnapshot::node_is_done(approval))
        });
        if !(blocked_by_edge || unmet_dependency || pending_approval) {
            out.push(Violation {
                code: ValidationCode::V5,
                message: format!("blocked node {} has no blocking cause", node.id),
            });
        }
    }
}

/// V6: binding externals are well-formed under exactly one scheme prefix and
/// unique across operations. (Cross-checking against the owners' live
/// registries is the liveness slice's job; within the snapshot this is the
/// enforceable core.)
fn check_v6_binding_identity(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    let mut seen: HashMap<&str, &WorkNodeId> = HashMap::new();
    for node in &snapshot.nodes {
        let Some(binding) = &node.binding else {
            continue;
        };
        if !external_identity_is_well_formed(&binding.external) {
            out.push(Violation {
                code: ValidationCode::V6,
                message: format!(
                    "node {} binding external {:?} matches no identity scheme",
                    node.id, binding.external
                ),
            });
        }
        if let Some(previous) = seen.insert(binding.external.as_str(), &node.id) {
            out.push(Violation {
                code: ValidationCode::V6,
                message: format!(
                    "external {:?} bound by both {} and {}",
                    binding.external, previous, node.id
                ),
            });
        }
    }
}

/// V7: reference nodes are inert — they never carry liveness state, because
/// the owning subsystems are the only source of liveness truth.
fn check_v7_refs_inert(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    for node in &snapshot.nodes {
        if matches!(node.kind, NodeKind::RuntimeRef | NodeKind::LaneRef)
            && !matches!(node.state, NodeState::Ready)
        {
            out.push(Violation {
                code: ValidationCode::V7,
                message: format!(
                    "reference node {} carries liveness state {:?}",
                    node.id, node.state
                ),
            });
        }
    }
}

/// V8: bounded history with strictly increasing revisions. (The exactly-once
/// revision increment itself is a reducer property, covered by tests.)
fn check_v8_history_bounded_monotonic(snapshot: &WorkGraphSnapshot, out: &mut Vec<Violation>) {
    if snapshot.history.len() > HISTORY_CAP {
        out.push(Violation {
            code: ValidationCode::V8,
            message: format!(
                "history length {} exceeds bound {HISTORY_CAP}",
                snapshot.history.len()
            ),
        });
    }
    let mut previous: Option<u64> = None;
    for receipt in snapshot.history.iter() {
        if let Some(prev) = previous
            && receipt.revision <= prev
        {
            out.push(Violation {
                code: ValidationCode::V8,
                message: format!(
                    "history revisions not strictly increasing ({} then {})",
                    prev, receipt.revision
                ),
            });
            break;
        }
        previous = Some(receipt.revision);
    }
    if let Some(last) = snapshot.history.last() {
        // During apply, validation runs before the increment, so the newest
        // receipt may equal the current revision but never exceed it by >1.
        if last.revision > snapshot.revision.saturating_add(1) {
            out.push(Violation {
                code: ValidationCode::V8,
                message: format!(
                    "history revision {} ahead of snapshot revision {}",
                    last.revision, snapshot.revision
                ),
            });
        }
    }
}