assay-cli 6.8.0

Policy-as-code gate for MCP agent tool calls, with verifiable evidence and Linux kernel enforcement.
use crate::cli::args::DemoArgs;
use anyhow::Context;
use assay_core::config::path_resolver::PathResolver;
use assay_core::validate::{validate, ValidateOptions};
use std::fs;

pub async fn cmd_demo(args: DemoArgs) -> anyhow::Result<i32> {
    let demo_dir = args.out;
    fs::create_dir_all(&demo_dir)?;

    // 1. Create Policy File (The Rules)
    let policy_path = demo_dir.join("policy.yaml");
    // ToolsPolicy is deny_unknown_fields: allow / deny / require_args / arg_constraints.
    // validate() serializes arg_constraints into evaluate_tool_args, which compiles
    // each tool entry as a JSON Schema (Map<Tool, Schema>), not the RFC arg-map dialect.
    let policy_content = r#"version: "1"
name: demo-policy
tools:
  arg_constraints:
    Search:
      type: object
      properties:
        query:
          type: string
          pattern: "^[a-zA-Z0-9 ]+$"
    Calculate:
      type: object
      properties:
        operation:
          type: string
          enum: ["add", "subtract"]
"#;
    fs::write(&policy_path, policy_content)
        .with_context(|| format!("failed to write demo file {}", policy_path.display()))?;

    // 2. Create Config File (The Test Runner)
    let config_path = demo_dir.join("assay.yaml");
    let config_content = r#"version: 1
suite: demo
model: gpt-4o-mini
tests:
  - id: demo_trace_1
    input: "find assay rules"
    expected:
      type: args_valid
      policy: policy.yaml
  - id: demo_trace_2
    input: "calculate 1 + 2"
    expected:
      type: args_valid
      policy: policy.yaml
"#;
    fs::write(&config_path, config_content)
        .with_context(|| format!("failed to write demo file {}", config_path.display()))?;

    // 2. Create Traces
    let trace_path = demo_dir.join("traces.jsonl");
    // We add an 'id' or 'prompt' to match the test case
    let trace_content = r#"{"id": "demo_trace_1", "tool": "Search", "args": {"query": "assay rules"}, "prompt": "find assay rules", "response": "detecting 123"}
{"id": "demo_trace_2", "tool": "Calculate", "args": {"operation": "add", "x": 1, "y": 2}, "prompt": "calculate 1 + 2", "response": "3"}
"#;
    fs::write(&trace_path, trace_content)
        .with_context(|| format!("failed to write demo file {}", trace_path.display()))?;

    println!("✓ Created demo environment in {}", demo_dir.display());
    println!("  - Config: {}", config_path.display());
    println!("  - Policy: {}", policy_path.display());
    println!("  - Traces: {}", trace_path.display());
    println!();
    println!("Running validation...");
    println!();

    // 3. Run Validation
    let cfg = assay_core::config::load_config(&config_path, false, true)?;
    let resolver = PathResolver::new(&config_path);

    let opts = ValidateOptions {
        trace_file: Some(trace_path.clone()),
        baseline_file: None,
        replay_strict: false,
    };

    let report = validate(&cfg, &opts, &resolver).await?;

    // Print success
    if report.diagnostics.is_empty() {
        println!("✅ Validation Passed!");
        println!("  Rules checked:");
        println!("    - Search.query");
        println!("    - Calculate.operation");
        println!();
        println!("Next steps:");
        println!("  1. Edit the policy: vim {}", policy_path.display());
        println!(
            "  2. Run validation:  assay validate --config {} --trace-file {}",
            config_path.display(),
            trace_path.display()
        );
        Ok(0)
    } else {
        // Should not happen in demo, but just in case
        for d in report.diagnostics {
            println!("{}", d.format_terminal());
        }
        Ok(1)
    }
}