assay-cli 8.0.0

Policy-as-code gate for MCP agent tool calls, with verifiable evidence and Linux kernel enforcement.
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
use crate::cli::args::MigrateArgs;
use crate::exit_codes;
use anyhow::{Context, Result};
use std::{fs, io::Write, path::Path};

pub fn cmd_migrate(args: MigrateArgs) -> Result<i32> {
    let config_path = args.config;
    println!("Migrating configuration: {:?}", config_path);

    if !config_path.exists() {
        anyhow::bail!("Config file not found: {:?}", config_path);
    }

    let input = assay_core::config::load_config_for_migration(&config_path)
        .context("failed to load config (strict check failed)")?;
    let config = input.config;

    println!(
        "Loaded config version: {} (model: {}, suite: {})",
        config.version, config.model, config.suite
    );

    let old_json = serde_json::to_value(&config)?;
    let mut migrated = assay_core::config::resolve::resolve_authored_policies(config, &config_path)
        .context("failed to resolve policies")?;
    if migrated.version == 0 {
        migrated.version = assay_core::config::SUPPORTED_CONFIG_VERSION;
    }
    let modified = input.legacy_expected || old_json != serde_json::to_value(&migrated)?;

    if args.check {
        if modified {
            eprintln!(
                "Migration required for {:?}. Run 'assay migrate' to update configuration.",
                config_path
            );
            return Ok(exit_codes::CONFIG_ERROR);
        } else {
            println!("Config {:?} is clean (already migrated).", config_path);
            return Ok(exit_codes::OK);
        }
    }

    if !modified {
        println!("No legacy policies found. Config is already up to date.");
        return Ok(exit_codes::OK);
    }

    let new_yaml = serde_yaml::to_string(&migrated)?;
    assay_core::config::check_config_size(&config_path, new_yaml.len() as u64)
        .context("migrated config would be too large to read")?;

    if args.dry_run {
        println!("\n--- Migrated Config (Dry Run) ---");
        println!("{}", new_yaml);
    } else {
        // Backup
        let backup_path = {
            let mut p = config_path.clone();
            if let Some(ext) = p.extension() {
                let mut s = ext.to_os_string();
                s.push(".bak");
                p.set_extension(s);
            } else {
                p.set_extension("bak");
            }
            p
        };

        let directory = backup_path
            .parent()
            .filter(|p| !p.as_os_str().is_empty())
            .unwrap_or(Path::new("."));
        let mut backup = tempfile::NamedTempFile::new_in(directory)
            .with_context(|| format!("failed to stage backup for {:?}", config_path))?;
        backup
            .write_all(input.original.as_bytes())
            .with_context(|| format!("failed to write backup for {:?}", config_path))?;
        backup.persist_noclobber(&backup_path).with_context(|| {
            format!(
                "failed to create backup without overwriting {:?}",
                backup_path
            )
        })?;
        println!("Original config backed up to: {:?}", backup_path);

        fs::write(&config_path, new_yaml)
            .with_context(|| format!("failed to write migrated config: {:?}", config_path))?;
        println!("✅ Migration complete! Updated {:?}", config_path);
    }

    Ok(exit_codes::OK)
}

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

    #[test]
    fn migrating_refuses_duplicate_expected_keys_before_writing() -> Result<()> {
        for expected in [
            "{must_contain: [first], must_contain: [last]}",
            "{schema: {Search: {type: string, type: integer}}}",
            "{schema: {Search: {properties: {1: {}, '1': {type: string}}}}}",
        ] {
            let dir = tempfile::tempdir()?;
            let path = dir.path().join("eval.yaml");
            let source = format!(
                "suite: migration\nmodel: trace\ntests:\n  - id: case\n    input: hello\n    expected: {expected}\n"
            );
            fs::write(&path, &source)?;
            let runtime_error = assay_core::config::load_config(&path, false, true)
                .expect_err("the runtime reader must also refuse duplicate keys");
            assert!(
                runtime_error.to_string().contains("duplicate"),
                "{runtime_error}"
            );
            let error = cmd_migrate(MigrateArgs {
                config: path.clone(),
                check: false,
                dry_run: false,
            })
            .expect_err("migration must refuse duplicate keys");
            assert!(format!("{error:#}").contains("duplicate"), "{error:#}");
            assert_eq!(fs::read_to_string(&path)?, source);
            assert!(!path.with_extension("yaml.bak").exists());
        }
        Ok(())
    }

    #[test]
    fn migration_keeps_distinct_property_names_that_yaml_types_alike() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().join("eval.yaml");
        let source = "suite: migration\nmodel: trace\ntests:\n  - id: case\n    input: hello\n    expected: {schema: {Search: {properties: {true: {}, True: {type: string}}}}}\n";
        fs::write(&path, source)?;
        assert_eq!(
            cmd_migrate(MigrateArgs {
                config: path.clone(),
                check: false,
                dry_run: false,
            })?,
            exit_codes::OK
        );
        let config = assay_core::config::load_config(&path, false, true)?;
        let expected = serde_json::to_value(&config.tests[0].expected)?;
        let properties = &expected["schema"]["schemas"]["Search"]["properties"];
        assert_eq!(properties["true"], serde_json::json!({}));
        assert_eq!(properties["True"], serde_json::json!({"type": "string"}));
        Ok(())
    }

    #[test]
    fn test_migrate_logic() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let config_path = dir.path().join("mcp-eval.yaml");
        let policy_path = dir.path().join("args.yaml");

        std::fs::write(
            &policy_path,
            r#"
Search:
  type: object
  properties:
    foo:
      type: string
"#,
        )?;

        std::fs::write(
            &config_path,
            r#"
suite: legacy-migration
model: dummy
tests:
  - id: t1
    input:
       prompt: "hi"
    expected:
       type: args_valid
       policy: args.yaml
"#,
        )?;

        // unused imports removed
        let args = MigrateArgs {
            check: false,
            config: config_path.clone(),
            dry_run: false,
        };

        // We run the cmd logic directly
        let code = cmd_migrate(args)?;
        assert_eq!(code, 0);

        let content = std::fs::read_to_string(&config_path)?;
        println!("Migrated content:\n{}", content);

        assert!(content.contains("configVersion: 1"));
        assert!(!content.contains("policy: args.yaml"));
        assert!(content.contains("schema:"));
        assert!(content.contains("Search:"));
        assert!(content.contains("type: object"));

        // Check backup
        let mut bak = config_path.clone();
        bak.set_extension("yaml.bak"); // Logic in code: ext + ".bak" -> .yaml.bak
                                       // Wait, logic is: let mut s = ext.to_os_string(); s.push(".bak"); p.set_extension(s);
                                       // "yaml" -> "yaml.bak". So "mcp-eval.yaml.bak". Correct.
        if !bak.exists() {
            // Fallback check if extension replacement happened differently
            let bak2 = config_path.with_extension("bak");
            if bak2.exists() {
                bak = bak2;
            }
        }
        assert!(bak.exists(), "Backup file should exist at {:?}", bak);

        Ok(())
    }
}

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

    #[test]
    fn duplicate_inline_expectation_keys_are_refused_before_migration() -> Result<()> {
        let directory = tempfile::tempdir()?;
        let config = directory.path().join("eval.yaml");
        for expected in [
            "{must_contain: [first], must_contain: [second]}",
            "{schema: {Search: {type: string, type: integer}}}",
            "{schema: {Search: {properties: {1: {}, '1': {type: string}}}}}",
            "[{type: must_contain, must_contain: [first], must_contain: [second]}]",
            "{type: args_valid, schema: {version: '2', tools: {deny: [rm], deny: [other]}}}",
        ] {
            let original = format!("suite: migration\nmodel: trace\ntests:\n- id: t\n  input: p\n  expected: {expected}\n");
            fs::write(&config, &original)?;
            let error = cmd_migrate(MigrateArgs {
                config: config.clone(),
                check: false,
                dry_run: false,
            })
            .expect_err("migration dropped an inline expectation key");
            assert!(
                format!("{error:#}").contains("duplicate member"),
                "{error:#}"
            );
            assert_eq!(fs::read_to_string(&config)?, original);
            assert!(!directory.path().join("eval.yaml.bak").exists());
        }
        Ok(())
    }

    #[test]
    fn migrating_a_sibling_assertion_keeps_schema_files_relative_to_the_config() -> Result<()> {
        let directory = tempfile::tempdir()?;
        let config = directory.path().join("eval.yaml");
        fs::write(directory.path().join("answer.json"), r#"{"type":"string"}"#)?;
        fs::write(
            &config,
            r#"configVersion: 1
suite: migration
model: trace
tests:
- id: legacy
  input: capital
  expected: {must_contain: [Paris]}
- id: schema
  input: answer
  expected: {type: json_schema, json_schema: "", schema_file: absent/../answer.json}
"#,
        )?;
        assert_eq!(
            cmd_migrate(MigrateArgs {
                config: config.clone(),
                check: false,
                dry_run: false
            })?,
            exit_codes::OK
        );
        let migrated = fs::read_to_string(&config)?;
        assert!(
            migrated.contains("schema_file: absent/../answer.json"),
            "{migrated}"
        );
        assert!(!migrated.contains("schema_file_resolved"), "{migrated}");
        Ok(())
    }

    #[test]
    fn versioned_config_still_migrates_untagged_expectations() -> Result<()> {
        let directory = tempfile::tempdir()?;
        let config = directory.path().join("eval.yaml");
        let backup = directory.path().join("eval.yaml.bak");
        let migrate = |check, dry_run| {
            cmd_migrate(MigrateArgs {
                config: config.clone(),
                check,
                dry_run,
            })
        };

        for expected in ["{must_contain: [Paris]}", "[{must_contain: [Paris]}]"] {
            let original = format!(
                "configVersion: 1\nsuite: migration\nmodel: trace\ntests:\n  - id: capital\n    input: {{prompt: capital}}\n    expected: {expected}\n"
            );
            fs::write(&config, &original)?;
            assert_eq!(
                migrate(true, false)?,
                exit_codes::CONFIG_ERROR,
                "{expected}"
            );
            assert_eq!(migrate(false, true)?, exit_codes::OK);
            assert_eq!(fs::read_to_string(&config)?, original);
            assert!(!backup.exists());

            assert_eq!(migrate(false, false)?, exit_codes::OK);
            let migrated = fs::read_to_string(&config)?;
            assert!(migrated.contains("type: must_contain"));
            assert!(migrated.contains("Paris"));
            assert_eq!(fs::read_to_string(&backup)?, original);
            assert_eq!(migrate(true, false)?, exit_codes::OK);
            assert_eq!(migrate(false, false)?, exit_codes::OK);
            assert_eq!(fs::read_to_string(&config)?, migrated);
            assert_eq!(fs::read_to_string(&backup)?, original);
            fs::remove_file(&backup)?;
        }
        Ok(())
    }
}

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

    #[test]
    fn an_existing_backup_can_be_a_referenced_input() -> Result<()> {
        let directory = tempfile::tempdir()?;
        let config = directory.path().join("eval.yaml");
        let backup = directory.path().join("eval.yaml.bak");
        let original = "suite: migration\nmodel: trace\ntests:\n- id: t\n  input: p\n  expected: {$ref: eval.yaml.bak}\n";
        let assertion = "must_contain: [sentinel]\n";
        fs::write(&config, original)?;
        fs::write(&backup, assertion)?;
        let migrate = |check, dry_run| {
            cmd_migrate(MigrateArgs {
                config: config.clone(),
                check,
                dry_run,
            })
        };
        assert_eq!(migrate(true, false)?, exit_codes::CONFIG_ERROR);
        assert_eq!(migrate(false, true)?, exit_codes::OK);
        let error = migrate(false, false).expect_err("migration overwrote its referenced input");
        assert!(format!("{error:#}").contains("eval.yaml.bak"), "{error:#}");
        assert_eq!(fs::read_to_string(config)?, original);
        assert_eq!(fs::read_to_string(backup)?, assertion);
        Ok(())
    }

    #[test]
    fn duplicate_and_oversized_references_leave_inputs_intact() -> Result<()> {
        let directory = tempfile::tempdir()?;
        let config = directory.path().join("eval.yaml");
        let reference = directory.path().join("part.yaml");
        let backup = directory.path().join("eval.yaml.bak");
        let original = "suite: migration\nmodel: trace\ntests:\n- id: t\n  input: p\n  expected: {$ref: part.yaml}\n";
        let migrate = || {
            cmd_migrate(MigrateArgs {
                config: config.clone(),
                check: false,
                dry_run: false,
            })
        };
        let mut at_limit = String::from("must_contain: [first]\n#");
        let limit = assay_core::model::MAX_POLICY_INPUT_BYTES as usize;
        at_limit.push_str(&"x".repeat(limit - at_limit.len()));
        for (source, reason) in [
            (
                "must_contain: [first]\nmust_contain: [second]".to_owned(),
                "duplicate member",
            ),
            (format!("{at_limit}x"), "source bytes limit"),
        ] {
            fs::write(&config, original)?;
            fs::write(&reference, &source)?;
            let error = migrate().expect_err("invalid reference was migrated");
            let message = format!("{error:#}");
            assert!(
                message.contains(reason) && message.contains("part.yaml"),
                "{message}"
            );
            assert_eq!(fs::read_to_string(&config)?, original);
            assert_eq!(fs::read_to_string(&reference)?, source);
            assert!(!backup.exists());
        }
        fs::write(&reference, &at_limit)?;
        assert_eq!(migrate()?, exit_codes::OK);
        assert!(fs::read_to_string(&config)?.contains("first"));
        assert_eq!(fs::read_to_string(backup)?, original);
        assert_eq!(fs::read_to_string(reference)?, at_limit);
        Ok(())
    }
}