termproof 0.4.0

Evidence-first verification for TUI and terminal applications: recipe model, schema, steps, assertions, orchestration, terminal sessions and the evidence pipeline
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
//! Typed configuration with cascading precedence and extension preservation.
//!
//! Precedence (lowest to highest, per spec ยง4.1 and Python `config.py`):
//! builtin < legacy user config < current user config < legacy project config
//! < current project config < explicit `--config` file.
//!
//! Values are merged via recursive deep-merge: dicts are merged key-wise,
//! scalars and lists are replaced by the overlay. This matches the Python
//! `_deep_merge` behavior and ensures a later source only overrides values it
//! provides (so user defaults do not wipe `steps` when only `docker` is set).

use std::collections::HashMap;
use std::path::{Path, PathBuf};

#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Legacy user config directory (`~/.config/tui-verifier/config.yaml`).
pub const LEGACY_USER_CONFIG_DIR: &str = "tui-verifier";
/// Current user config directory (`~/.config/termproof/config.yaml`).
pub const CURRENT_USER_CONFIG_DIR: &str = "termproof";
/// Legacy project config directory (`.tui-verifier/config.yaml`).
pub const LEGACY_PROJECT_CONFIG_DIR: &str = ".tui-verifier";
/// Current project config directory (`.termproof/config.yaml`).
pub const CURRENT_PROJECT_CONFIG_DIR: &str = ".termproof";

/// Docker backend settings.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct DockerBackendConfig {
    /// Container image name (empty means host-native / no Docker).
    #[serde(default)]
    pub image: String,

    /// Workspace mount point inside the container.
    #[serde(default = "default_docker_workdir")]
    pub workdir: String,

    /// Volume mounts.
    #[serde(default = "default_docker_volumes")]
    pub volumes: Vec<serde_json::Value>,

    /// Environment variables passed into the container.
    #[serde(default)]
    pub env: HashMap<String, String>,

    /// Preserve any extra Docker fields.
    #[serde(default, flatten)]
    #[cfg_attr(feature = "schema", schemars(flatten))]
    pub extension: HashMap<String, serde_json::Value>,
}

fn default_docker_workdir() -> String {
    "/workspace".to_string()
}

fn default_docker_volumes() -> Vec<serde_json::Value> {
    vec![serde_json::json!({"host": ".", "container": "/workspace"})]
}

impl Default for DockerBackendConfig {
    fn default() -> Self {
        Self {
            image: String::new(),
            workdir: default_docker_workdir(),
            volumes: default_docker_volumes(),
            env: HashMap::new(),
            extension: HashMap::new(),
        }
    }
}

/// Global defaults (post-script idle wait cap).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct GlobalDefaults {
    /// Cap for the post-script idle wait in PTY mode; `None` means wait up to
    /// the recipe timeout for quiescence.
    #[serde(
        default = "default_idle_cap",
        deserialize_with = "deserialize_idle_cap",
        serialize_with = "serialize_idle_cap"
    )]
    #[cfg_attr(feature = "schema", schemars(with = "Option<f64>"))]
    pub idle_cap_seconds: Option<f64>,

    /// Preserve unknown defaults keys.
    #[serde(default, flatten)]
    #[cfg_attr(feature = "schema", schemars(flatten))]
    pub extension: HashMap<String, serde_json::Value>,
}

fn default_idle_cap() -> Option<f64> {
    Some(3.0)
}

fn deserialize_idle_cap<'de, D>(deserializer: D) -> Result<Option<f64>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let opt = Option::<serde_json::Value>::deserialize(deserializer)?;
    match opt {
        None => Ok(Some(3.0)),
        Some(serde_json::Value::Null) => Ok(None),
        Some(serde_json::Value::Number(n)) => {
            let f = n
                .as_f64()
                .ok_or_else(|| serde::de::Error::custom("idle_cap_seconds must be a number"))?;
            if !f.is_finite() || f < 0.0 {
                return Err(serde::de::Error::custom(format!(
                    "idle_cap_seconds must be a finite nonnegative number, got {f:?}"
                )));
            }
            Ok(Some(f))
        }
        Some(other) => Err(serde::de::Error::custom(format!(
            "idle_cap_seconds must be a finite nonnegative number, got {other:?}"
        ))),
    }
}

fn serialize_idle_cap<S>(value: &Option<f64>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    match value {
        Some(v) => serializer.serialize_some(v),
        None => serializer.serialize_none(),
    }
}

impl Default for GlobalDefaults {
    fn default() -> Self {
        Self {
            idle_cap_seconds: Some(3.0),
            extension: HashMap::new(),
        }
    }
}

/// Typed verifier configuration.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct VerifierConfig {
    /// Step plugin map: action name โ†’ `module:Class`.
    #[serde(default = "default_steps")]
    pub steps: HashMap<String, String>,

    /// Assertion plugin map: type name โ†’ `module:Class`.
    #[serde(default = "default_assertions")]
    pub assertions: HashMap<String, String>,

    /// Agent runner plugins.
    #[serde(default = "default_agent_runners")]
    pub agent_runners: HashMap<String, String>,

    /// Execution mode plugins.
    #[serde(default = "default_execution_modes")]
    pub execution_modes: HashMap<String, String>,

    /// Reporter plugins.
    #[serde(default = "default_reporters")]
    pub reporters: HashMap<String, String>,

    /// Screen renderer plugins.
    #[serde(default = "default_screen_renderers")]
    pub screen_renderers: HashMap<String, String>,

    /// Video backend plugins.
    #[serde(default = "default_video_backends")]
    pub video_backends: HashMap<String, String>,

    /// Session backend plugin identifier.
    #[serde(default = "default_session_backend")]
    pub session_backend: String,

    /// Docker backend configuration.
    #[serde(default)]
    pub docker: DockerBackendConfig,

    /// Global defaults.
    #[serde(default)]
    pub defaults: GlobalDefaults,

    /// Extension map for unknown top-level keys.
    #[serde(default, flatten)]
    #[cfg_attr(feature = "schema", schemars(flatten))]
    pub extension: HashMap<String, serde_json::Value>,
}

fn default_steps() -> HashMap<String, String> {
    let mut m = HashMap::new();
    m.insert(
        "wait_for_text".to_string(),
        "termproof.builtin_steps:WaitForText".to_string(),
    );
    m.insert(
        "wait_for_idle".to_string(),
        "termproof.builtin_steps:WaitForIdle".to_string(),
    );
    m.insert(
        "send_text".to_string(),
        "termproof.builtin_steps:SendText".to_string(),
    );
    m.insert(
        "send_line".to_string(),
        "termproof.builtin_steps:SendLine".to_string(),
    );
    m.insert(
        "press".to_string(),
        "termproof.builtin_steps:Press".to_string(),
    );
    m.insert(
        "sleep".to_string(),
        "termproof.builtin_steps:Sleep".to_string(),
    );
    m.insert(
        "wait_for_regex".to_string(),
        "termproof.builtin_steps:WaitForRegex".to_string(),
    );
    m
}

fn default_assertions() -> HashMap<String, String> {
    let mut m = HashMap::new();
    m.insert(
        "output_contains".to_string(),
        "termproof.builtin_assertions:OutputContains".to_string(),
    );
    m.insert(
        "output_not_contains".to_string(),
        "termproof.builtin_assertions:OutputNotContains".to_string(),
    );
    m.insert(
        "screen_contains".to_string(),
        "termproof.builtin_assertions:ScreenContains".to_string(),
    );
    m.insert(
        "screen_not_contains".to_string(),
        "termproof.builtin_assertions:ScreenNotContains".to_string(),
    );
    m.insert(
        "exit_code".to_string(),
        "termproof.builtin_assertions:ExitCode".to_string(),
    );
    m.insert(
        "file_exists".to_string(),
        "termproof.builtin_assertions:FileExists".to_string(),
    );
    m.insert(
        "file_contains".to_string(),
        "termproof.builtin_assertions:FileContains".to_string(),
    );
    m.insert(
        "json_schema".to_string(),
        "termproof.builtin_assertions:JsonSchema".to_string(),
    );
    m
}

fn default_agent_runners() -> HashMap<String, String> {
    let mut m = HashMap::new();
    m.insert(
        "codex".to_string(),
        "termproof.agent_driven:CodexCliAgentRunner".to_string(),
    );
    m
}

fn default_execution_modes() -> HashMap<String, String> {
    let mut m = HashMap::new();
    m.insert(
        "scripted_pty".to_string(),
        "termproof.builtin_modes:ScriptedPtyMode".to_string(),
    );
    m.insert(
        "scripted_process".to_string(),
        "termproof.builtin_modes:ScriptedProcessMode".to_string(),
    );
    m.insert(
        "agent_driven".to_string(),
        "termproof.builtin_modes:AgentDrivenMode".to_string(),
    );
    m
}

fn default_reporters() -> HashMap<String, String> {
    let mut m = HashMap::new();
    m.insert(
        "markdown".to_string(),
        "termproof.builtin_reporters:MarkdownReporter".to_string(),
    );
    m.insert(
        "junit_xml".to_string(),
        "termproof.builtin_reporters:JUnitXmlReporter".to_string(),
    );
    m
}

fn default_screen_renderers() -> HashMap<String, String> {
    let mut m = HashMap::new();
    m.insert(
        "svg".to_string(),
        "termproof.builtin_renderers:SvgRenderer".to_string(),
    );
    m.insert(
        "png".to_string(),
        "termproof.builtin_renderers:PngRenderer".to_string(),
    );
    m
}

fn default_video_backends() -> HashMap<String, String> {
    let mut m = HashMap::new();
    m.insert(
        "agg_ffmpeg".to_string(),
        "termproof.builtin_video:AggFfmpegBackend".to_string(),
    );
    m
}

fn default_session_backend() -> String {
    "termproof.builtin_session:PexpectAsciinemaBackend".to_string()
}

impl Default for VerifierConfig {
    fn default() -> Self {
        Self {
            steps: default_steps(),
            assertions: default_assertions(),
            agent_runners: default_agent_runners(),
            execution_modes: default_execution_modes(),
            reporters: default_reporters(),
            screen_renderers: default_screen_renderers(),
            video_backends: default_video_backends(),
            session_backend: default_session_backend(),
            docker: DockerBackendConfig::default(),
            defaults: GlobalDefaults::default(),
            extension: HashMap::new(),
        }
    }
}

impl VerifierConfig {
    /// Return a config populated entirely from built-in defaults.
    pub fn builtin() -> Self {
        Self::default()
    }

    /// Load configuration with full cascading precedence.
    ///
    /// When `user_path` is `Some`, exactly that file is used for user config
    /// and implicit user-location discovery is skipped (mirrors Python
    /// `load_config(user_path=...)`). When `config_path` is `Some`, it is
    /// applied last and wins.
    pub fn load(
        project_path: Option<&Path>,
        user_path: Option<&Path>,
        config_path: Option<&Path>,
    ) -> Result<Self, crate::error::CoreError> {
        // Start from builtin deep-merged empty dict.
        let mut merged = serde_json::to_value(Self::default()).expect("builtin serializes");

        let project_root = project_path
            .map(PathBuf::from)
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));

        let user_files: Vec<PathBuf> = if let Some(p) = user_path {
            vec![p.to_path_buf()]
        } else {
            let home = dirs_home();
            if let Some(home) = home {
                vec![
                    home.join(".config")
                        .join(LEGACY_USER_CONFIG_DIR)
                        .join("config.yaml"),
                    home.join(".config")
                        .join(CURRENT_USER_CONFIG_DIR)
                        .join("config.yaml"),
                ]
            } else {
                vec![]
            }
        };

        let project_files = [
            project_root
                .join(LEGACY_PROJECT_CONFIG_DIR)
                .join("config.yaml"),
            project_root
                .join(CURRENT_PROJECT_CONFIG_DIR)
                .join("config.yaml"),
        ];

        let explicit_files: Vec<PathBuf> = config_path
            .map(|p| vec![p.to_path_buf()])
            .unwrap_or_default();

        for path in user_files
            .iter()
            .chain(project_files.iter())
            .chain(explicit_files.iter())
        {
            if path.exists() {
                let overlay = load_yaml_as_value(path)?;
                merged = deep_merge(merged, overlay);
            }
        }

        // Now deserialize back, with strict idle_cap validation already applied
        // via custom deserializer. If it fails, surface as InvalidConfig.
        let config: Self =
            serde_json::from_value(merged).map_err(|e| crate::error::CoreError::InvalidConfig {
                field: "config".to_string(),
                message: e.to_string(),
            })?;
        Ok(config)
    }

    /// Convenience wrapper matching Python `load_config(project_path, user_path, config_path)`.
    pub fn from_paths(
        project_path: Option<&Path>,
        user_path: Option<&Path>,
        config_path: Option<&Path>,
    ) -> Result<Self, crate::error::CoreError> {
        Self::load(project_path, user_path, config_path)
    }
}

fn dirs_home() -> Option<PathBuf> {
    std::env::var_os("HOME").map(PathBuf::from)
}

fn load_yaml_as_value(path: &Path) -> Result<serde_json::Value, crate::error::CoreError> {
    let content = std::fs::read_to_string(path).map_err(|e| crate::error::CoreError::Io {
        path: path.display().to_string(),
        source: e,
    })?;
    if content.trim().is_empty() {
        return Ok(serde_json::Value::Object(Default::default()));
    }
    let yaml_value: serde_yaml::Value =
        serde_yaml::from_str(&content).map_err(|e| crate::error::CoreError::Parse {
            path: path.display().to_string(),
            message: e.to_string(),
        })?;
    // Convert YAML value to JSON value for uniform merging.
    let json_value =
        serde_json::to_value(yaml_value).map_err(|e| crate::error::CoreError::Parse {
            path: path.display().to_string(),
            message: e.to_string(),
        })?;
    // If the file top-level is not an object, treat as empty (mirrors Python's `return data if isinstance(data, dict) else {}`).
    if json_value.is_object() {
        Ok(json_value)
    } else {
        Ok(serde_json::Value::Object(Default::default()))
    }
}

/// Recursively deep-merge `overlay` into `base`; overlay leaves win.
fn deep_merge(base: serde_json::Value, overlay: serde_json::Value) -> serde_json::Value {
    match (base, overlay) {
        (serde_json::Value::Object(mut base_map), serde_json::Value::Object(overlay_map)) => {
            for (k, v) in overlay_map {
                let base_val = base_map.remove(&k).unwrap_or(serde_json::Value::Null);
                // Recurse only when both sides are objects.
                let merged = if base_val.is_object() && v.is_object() {
                    deep_merge(base_val, v)
                } else {
                    v
                };
                base_map.insert(k, merged);
            }
            serde_json::Value::Object(base_map)
        }
        (_, overlay) => overlay,
    }
}

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

    #[test]
    fn builtin_has_all_fields() {
        let c = VerifierConfig::builtin();
        assert!(c.steps.contains_key("wait_for_text"));
        assert!(c.assertions.contains_key("output_contains"));
        assert_eq!(c.docker.workdir, "/workspace");
        assert_eq!(c.defaults.idle_cap_seconds, Some(3.0));
    }

    #[test]
    fn deep_merge_overlay_wins_at_leaf() {
        let base = serde_json::json!({"a": {"x": 1, "y": 2}, "b": 3});
        let overlay = serde_json::json!({"a": {"y": 99, "z": 100}, "c": 4});
        let merged = deep_merge(base, overlay);
        assert_eq!(merged["a"]["x"], 1);
        assert_eq!(merged["a"]["y"], 99);
        assert_eq!(merged["a"]["z"], 100);
        assert_eq!(merged["b"], 3);
        assert_eq!(merged["c"], 4);
    }

    #[test]
    fn load_cascades_project_over_user() {
        let tmp = std::env::temp_dir().join(format!("termproof-test-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();
        let project_dir = tmp.join("project");
        std::fs::create_dir_all(project_dir.join(".termproof")).unwrap();
        std::fs::write(
            project_dir.join(".termproof/config.yaml"),
            "docker:\n  image: project-image\n  workdir: /project\n",
        )
        .unwrap();
        let user_yaml = tmp.join("user.yaml");
        std::fs::write(
            &user_yaml,
            "docker:\n  image: user-image\n  env:\n    FROM_USER: '1'\n",
        )
        .unwrap();

        let cfg = VerifierConfig::load(Some(&project_dir), Some(&user_yaml), None).unwrap();
        assert_eq!(cfg.docker.image, "project-image");
        assert_eq!(cfg.docker.workdir, "/project");
        assert_eq!(cfg.docker.env.get("FROM_USER").unwrap(), "1");
    }
}