vfa-tui 0.0.18

Enterprise-grade terminal UI for the Vanguard Frontier Agentic marketplace catalog
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
//! Verified model / reasoning-effort matrix (`catalog/model-registry.json`).
//!
//! The registry is the fail-closed allowlist `scripts/model-policy.mjs`
//! validates every policy value against. The TUI reads it for one purpose
//! only: to offer the operator the *same* choices the script would accept,
//! instead of a free-text box plus a hardcoded effort union that drifts.
//!
//! Read-first principle: this module never classifies an arbitrary value into
//! a namespace and never decides whether a pin is legal — that is the script's
//! job, and duplicating its regex evaluation here is exactly the kind of logic
//! fork the TUI is meant to avoid. It only walks the already-declared
//! structure, so a choice the operator *picks* carries its namespace with it
//! and the effort narrowing follows from that, with no matching required.
//! Free-typed values fall back to the harness vocabulary and are left for the
//! script to accept or reject.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// Whether a namespace enumerates its models (`closed`) or validates shape
/// only because the catalog is too large/volatile to list (`open`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Membership {
    Closed,
    Open,
}

/// Provider lifecycle state of a registered model. Absent means available.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ModelStatus {
    #[default]
    Available,
    Retiring,
    Retired,
}

/// One entry in a closed namespace's verified allowlist.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RegistryModel {
    pub id: String,
    /// Effort values this model supports. `None` inherits the namespace (or
    /// harness) vocabulary; `Some([])` is a non-reasoning model.
    #[serde(default)]
    pub reasoning_efforts: Option<Vec<String>>,
    pub last_verified: String,
    #[serde(default)]
    pub source: Option<String>,
    #[serde(default)]
    pub note: Option<String>,
    #[serde(default)]
    pub status: Option<ModelStatus>,
    #[serde(default)]
    pub retirement_date: Option<String>,
    #[serde(default)]
    pub successor: Option<String>,
}

impl RegistryModel {
    pub fn status(&self) -> ModelStatus {
        self.status.unwrap_or_default()
    }
}

/// A namespace groups model values that share a routing target and vocabulary.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RegistryNamespace {
    pub id: String,
    #[serde(default)]
    pub description: Option<String>,
    /// Anchored regex the script uses to classify a value into this namespace.
    /// Carried for display/provenance only — never evaluated here.
    #[serde(rename = "match")]
    pub match_pattern: String,
    pub membership: Membership,
    #[serde(default)]
    pub model_provider: Option<String>,
    #[serde(default)]
    pub requires_provider_table: Option<bool>,
    #[serde(default)]
    pub reasoning_efforts: Option<Vec<String>>,
    #[serde(default)]
    pub reasoning_note: Option<String>,
    #[serde(default)]
    pub sources: Vec<String>,
    #[serde(default)]
    pub examples: Vec<String>,
    #[serde(default)]
    pub models: Vec<RegistryModel>,
}

/// Per-harness section of the registry.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RegistryHarness {
    /// Config key the harness projects reasoning effort into; `None` means the
    /// harness has no reasoning field at all.
    pub reasoning_key: Option<String>,
    pub reasoning_efforts: Vec<String>,
    #[serde(default)]
    pub reasoning_note: Option<String>,
    pub namespaces: Vec<RegistryNamespace>,
}

impl RegistryHarness {
    /// Whether this harness can express a reasoning effort at all.
    pub fn supports_reasoning(&self) -> bool {
        self.reasoning_key.is_some() && !self.reasoning_efforts.is_empty()
    }
}

/// Top-level structure of `catalog/model-registry.json`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ModelRegistry {
    pub manifest_version: u32,
    #[serde(default)]
    pub description: Option<String>,
    pub last_refreshed: String,
    pub harnesses: HashMap<String, RegistryHarness>,
}

/// One selectable entry in the TUI's model picker, already resolved to the
/// effort vocabulary that applies to it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelChoice {
    pub model: String,
    pub namespace: String,
    /// Effort values valid for this model: the model's own list when it
    /// declares one, else the namespace's, else the harness vocabulary.
    pub efforts: Vec<String>,
    pub status: ModelStatus,
    /// True when the entry came from an open namespace's advisory `examples`
    /// rather than a verified allowlist — pickable, but not an allowlist hit.
    pub is_example: bool,
    pub retirement_date: Option<String>,
    pub successor: Option<String>,
    pub note: Option<String>,
}

impl ModelChoice {
    /// Short suffix describing lifecycle / provenance for the picker line.
    pub fn label_suffix(&self) -> String {
        let mut parts: Vec<String> = Vec::new();
        match self.status {
            ModelStatus::Retiring => parts.push(match &self.retirement_date {
                Some(d) => format!("retiring {d}"),
                None => "retiring".to_string(),
            }),
            ModelStatus::Retired => parts.push(match &self.successor {
                Some(s) => format!("retired -> {s}"),
                None => "retired".to_string(),
            }),
            ModelStatus::Available => {}
        }
        if self.is_example {
            parts.push("example".to_string());
        }
        if self.efforts.is_empty() {
            parts.push("no effort".to_string());
        }
        if parts.is_empty() {
            String::new()
        } else {
            format!(" ({})", parts.join(", "))
        }
    }
}

/// Follow a `retired` model's `successor` chain within its namespace to the
/// entry the policy engine will actually project, so effort narrowing is read
/// off that model rather than the retired one.
///
/// Bounded by the namespace size and guarded against a cycle or a dangling
/// successor: either simply stops, returning the last entry reached, which is
/// the conservative choice — the script re-validates regardless.
fn resolve_successor<'a>(ns: &'a RegistryNamespace, model: &'a RegistryModel) -> &'a RegistryModel {
    let mut current = model;
    for _ in 0..ns.models.len() {
        if current.status() != ModelStatus::Retired {
            break;
        }
        let Some(successor) = current.successor.as_deref() else {
            break;
        };
        let Some(next) = ns.models.iter().find(|m| m.id == successor) else {
            break;
        };
        if next.id == current.id {
            break;
        }
        current = next;
    }
    current
}

impl ModelRegistry {
    pub fn harness(&self, harness: &str) -> Option<&RegistryHarness> {
        self.harnesses.get(harness)
    }

    /// Complete effort vocabulary a harness accepts, used when the operator
    /// free-types a model the registry does not enumerate.
    pub fn harness_efforts(&self, harness: &str) -> Vec<String> {
        self.harness(harness)
            .map(|h| h.reasoning_efforts.clone())
            .unwrap_or_default()
    }

    pub fn supports_reasoning(&self, harness: &str) -> bool {
        self.harness(harness)
            .is_some_and(|h| h.supports_reasoning())
    }

    /// Every model the operator can pick for `harness`, in registry order:
    /// each namespace's verified allowlist first, then the advisory examples
    /// of open namespaces. Duplicate ids keep their first (most specific)
    /// occurrence, mirroring the script's first-namespace-wins ordering.
    pub fn choices_for_harness(&self, harness: &str) -> Vec<ModelChoice> {
        let Some(hdef) = self.harness(harness) else {
            return Vec::new();
        };
        let mut out: Vec<ModelChoice> = Vec::new();
        let mut seen: Vec<String> = Vec::new();

        let push = |choice: ModelChoice, out: &mut Vec<ModelChoice>, seen: &mut Vec<String>| {
            if seen.iter().any(|m| m == &choice.model) {
                return;
            }
            seen.push(choice.model.clone());
            out.push(choice);
        };

        for ns in &hdef.namespaces {
            let ns_efforts = ns
                .reasoning_efforts
                .clone()
                .unwrap_or_else(|| hdef.reasoning_efforts.clone());

            for m in &ns.models {
                // A "retired" model is not what the engine validates against:
                // resolveAll in scripts/model-policy.mjs substitutes the
                // documented successor (chain-followed) and checks the effort
                // against *that* model. Mirror it, or the picker would offer
                // the retired entry's vocabulary for a pin the script resolves
                // elsewhere — offering a value the script then rejects, or
                // hiding one it would accept.
                let effective = resolve_successor(ns, m);
                let efforts = effective
                    .reasoning_efforts
                    .clone()
                    .unwrap_or_else(|| ns_efforts.clone());
                push(
                    ModelChoice {
                        model: m.id.clone(),
                        namespace: ns.id.clone(),
                        efforts,
                        status: m.status(),
                        is_example: false,
                        retirement_date: m.retirement_date.clone(),
                        successor: m.successor.clone(),
                        note: m.note.clone(),
                    },
                    &mut out,
                    &mut seen,
                );
            }

            // Open namespaces cannot enumerate their catalog; their `examples`
            // are advisory but they are exactly the values an operator wants
            // one keypress away (local Ollama tags, OpenRouter slugs).
            if ns.membership == Membership::Open {
                for ex in &ns.examples {
                    push(
                        ModelChoice {
                            model: ex.clone(),
                            namespace: ns.id.clone(),
                            efforts: ns_efforts.clone(),
                            status: ModelStatus::Available,
                            is_example: true,
                            retirement_date: None,
                            successor: None,
                            note: None,
                        },
                        &mut out,
                        &mut seen,
                    );
                }
            }
        }
        out
    }

    /// The choice matching an exact model id, if the registry enumerates it.
    /// Exact-match only — no namespace regex is evaluated, so a free-typed
    /// value simply returns `None` and the caller falls back to the harness
    /// vocabulary rather than guessing at a classification.
    pub fn choice_for(&self, harness: &str, model: &str) -> Option<ModelChoice> {
        self.choices_for_harness(harness)
            .into_iter()
            .find(|c| c.model == model)
    }

    /// Effort values to offer for `model` under `harness`. A registered model
    /// narrows to its own vocabulary; anything else gets the full harness
    /// vocabulary and is left for `scripts/model-policy.mjs` to adjudicate.
    pub fn efforts_for(&self, harness: &str, model: &str) -> Vec<String> {
        match self.choice_for(harness, model) {
            Some(choice) => choice.efforts,
            None => self.harness_efforts(harness),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn fixture() -> ModelRegistry {
        let json = r#"{
          "manifest_version": 1,
          "description": "test",
          "last_refreshed": "2026-09-05",
          "harnesses": {
            "codex": {
              "reasoning_key": "model_reasoning_effort",
              "reasoning_efforts": ["none", "minimal", "low", "medium", "high", "xhigh", "max"],
              "namespaces": [
                {
                  "id": "openai",
                  "match": "^(gpt-|o[0-9])[a-z0-9.-]*$",
                  "membership": "closed",
                  "model_provider": null,
                  "models": [
                    { "id": "gpt-6-astra", "reasoning_efforts": ["low", "high", "max"], "last_verified": "2026-09-05" },
                    { "id": "gpt-4.1-mini", "reasoning_efforts": [], "last_verified": "2026-09-05" },
                    { "id": "gpt-old", "last_verified": "2026-09-05", "status": "retiring", "retirement_date": "2026-12-11", "successor": "gpt-6-astra" }
                  ]
                },
                {
                  "id": "ollama",
                  "match": "^[a-z0-9][a-z0-9._-]*:[a-z0-9][a-z0-9._-]*$",
                  "membership": "open",
                  "model_provider": "ollama",
                  "requires_provider_table": true,
                  "reasoning_efforts": ["none", "low", "medium", "high", "max"],
                  "examples": ["qwen3:32b", "glm-5.3:cloud"]
                }
              ]
            },
            "cursor": {
              "reasoning_key": null,
              "reasoning_efforts": [],
              "namespaces": [
                { "id": "named", "match": "^[a-z0-9][a-z0-9.-]*$", "membership": "closed",
                  "models": [{ "id": "composer-2.5", "last_verified": "2026-09-05" }] }
              ]
            }
          }
        }"#;
        serde_json::from_str(json).expect("fixture parses")
    }

    #[test]
    fn parses_registry_shape() {
        let reg = fixture();
        assert_eq!(reg.manifest_version, 1);
        assert_eq!(reg.last_refreshed, "2026-09-05");
        assert_eq!(reg.harness("codex").unwrap().namespaces.len(), 2);
    }

    #[test]
    fn choices_include_closed_models_and_open_examples() {
        let ids: Vec<String> = fixture()
            .choices_for_harness("codex")
            .into_iter()
            .map(|c| c.model)
            .collect();
        assert_eq!(
            ids,
            vec![
                "gpt-6-astra",
                "gpt-4.1-mini",
                "gpt-old",
                "qwen3:32b",
                "glm-5.3:cloud"
            ]
        );
    }

    #[test]
    fn open_namespace_entries_are_marked_as_examples() {
        let reg = fixture();
        assert!(!reg.choice_for("codex", "gpt-6-astra").unwrap().is_example);
        let tag = reg.choice_for("codex", "qwen3:32b").unwrap();
        assert!(tag.is_example);
        assert_eq!(tag.namespace, "ollama");
    }

    #[test]
    fn effort_narrowing_prefers_model_then_namespace_then_harness() {
        let reg = fixture();
        // model-level list wins
        assert_eq!(
            reg.efforts_for("codex", "gpt-6-astra"),
            vec!["low", "high", "max"]
        );
        // namespace-level list wins where the model declares none
        assert_eq!(
            reg.efforts_for("codex", "qwen3:32b"),
            vec!["none", "low", "medium", "high", "max"]
        );
        // model with no list inherits the harness vocabulary
        assert_eq!(reg.efforts_for("codex", "gpt-old").len(), 7);
    }

    #[test]
    fn non_reasoning_model_offers_no_efforts() {
        assert!(fixture().efforts_for("codex", "gpt-4.1-mini").is_empty());
    }

    #[test]
    fn free_typed_model_falls_back_to_harness_vocabulary() {
        let reg = fixture();
        // Deliberately never classified against a namespace regex.
        assert!(reg.choice_for("codex", "deepseek-r1:70b").is_none());
        assert_eq!(reg.efforts_for("codex", "deepseek-r1:70b").len(), 7);
    }

    #[test]
    fn harness_without_reasoning_key_reports_unsupported() {
        let reg = fixture();
        assert!(reg.supports_reasoning("codex"));
        assert!(!reg.supports_reasoning("cursor"));
        assert!(!reg.supports_reasoning("nonexistent"));
        assert!(reg.choices_for_harness("nonexistent").is_empty());
    }

    #[test]
    fn lifecycle_surfaces_in_the_picker_label() {
        let reg = fixture();
        let retiring = reg.choice_for("codex", "gpt-old").unwrap();
        assert_eq!(retiring.status, ModelStatus::Retiring);
        assert!(retiring.label_suffix().contains("retiring 2026-12-11"));
        assert!(reg
            .choice_for("codex", "gpt-4.1-mini")
            .unwrap()
            .label_suffix()
            .contains("no effort"));
        assert!(reg
            .choice_for("codex", "qwen3:32b")
            .unwrap()
            .label_suffix()
            .contains("example"));
        assert_eq!(
            reg.choice_for("codex", "gpt-6-astra")
                .unwrap()
                .label_suffix(),
            ""
        );
    }

    #[test]
    fn retired_model_takes_its_successors_efforts() {
        // The engine projects the successor for a retired pin and validates the
        // effort against it, so the picker must narrow the same way.
        let json = r#"{
          "manifest_version": 1,
          "last_refreshed": "2026-09-05",
          "harnesses": {
            "codex": {
              "reasoning_key": "model_reasoning_effort",
              "reasoning_efforts": ["none", "low", "medium", "high", "xhigh", "max"],
              "namespaces": [
                {
                  "id": "openai",
                  "match": "^gpt-[a-z0-9.-]*$",
                  "membership": "closed",
                  "models": [
                    { "id": "gpt-dead", "reasoning_efforts": ["low"], "last_verified": "2026-09-05", "status": "retired", "successor": "gpt-mid" },
                    { "id": "gpt-mid", "reasoning_efforts": ["medium"], "last_verified": "2026-09-05", "status": "retired", "successor": "gpt-live" },
                    { "id": "gpt-live", "reasoning_efforts": ["none", "high", "max"], "last_verified": "2026-09-05" },
                    { "id": "gpt-loop", "reasoning_efforts": ["low"], "last_verified": "2026-09-05", "status": "retired", "successor": "gpt-loop" },
                    { "id": "gpt-dangling", "reasoning_efforts": ["low"], "last_verified": "2026-09-05", "status": "retired", "successor": "gpt-missing" }
                  ]
                }
              ]
            },
            "claude-code": { "reasoning_key": "effort", "reasoning_efforts": ["high"], "namespaces": [
              { "id": "alias", "match": "^opus$", "membership": "closed", "models": [{ "id": "opus", "last_verified": "2026-09-05" }] } ] },
            "cursor": { "reasoning_key": null, "reasoning_efforts": [], "namespaces": [
              { "id": "named", "match": "^[a-z0-9-]+$", "membership": "closed", "models": [{ "id": "composer-2", "last_verified": "2026-09-05" }] } ] }
          }
        }"#;
        let reg: ModelRegistry = serde_json::from_str(json).expect("parse");
        // Chain is followed to the end, not one hop.
        assert_eq!(
            reg.efforts_for("codex", "gpt-dead"),
            vec!["none", "high", "max"]
        );
        assert_eq!(
            reg.efforts_for("codex", "gpt-mid"),
            vec!["none", "high", "max"]
        );
        // A live model is untouched.
        assert_eq!(
            reg.efforts_for("codex", "gpt-live"),
            vec!["none", "high", "max"]
        );
        // A self-referential or dangling successor must terminate, not hang.
        assert_eq!(reg.efforts_for("codex", "gpt-loop"), vec!["low"]);
        assert_eq!(reg.efforts_for("codex", "gpt-dangling"), vec!["low"]);
        // The retired entry still renders as retired in the picker.
        assert!(reg
            .choice_for("codex", "gpt-dead")
            .unwrap()
            .label_suffix()
            .contains("retired -> gpt-mid"));
    }

    #[test]
    fn unknown_fields_are_rejected() {
        // deny_unknown_fields keeps the TUI honest when the generator adds a key.
        let json = r#"{ "id": "x", "last_verified": "2026-09-05", "surprise": true }"#;
        assert!(serde_json::from_str::<RegistryModel>(json).is_err());
    }

    #[test]
    fn parses_the_committed_registry() {
        // Guards against a registry change that the TUI's strict structs
        // would reject at runtime (the catalog would silently stop loading).
        let path = concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../../catalog/model-registry.json"
        );
        let data = std::fs::read_to_string(path).expect("committed registry is readable");
        let reg: ModelRegistry = serde_json::from_str(&data).expect("committed registry parses");
        for harness in ["codex", "claude-code", "cursor"] {
            assert!(
                !reg.choices_for_harness(harness).is_empty(),
                "{harness} should offer at least one model choice"
            );
        }
    }
}