File size: 3,775 Bytes
4d393e8 332cc17 4d393e8 332cc17 4d393e8 332cc17 | 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 | """
`dw validate` command.
Evaluates active validation policy rules against extracted knowledge proposals.
"""
from __future__ import annotations
from typing import Optional
import typer
from rich.table import Table
from rich.panel import Panel
from diffweave.cli.config import get_active_workspace
from diffweave.cli.output import print_error, print_json, console
from diffweave.mcp.client import DiffWeaveMCPClient
def validate_command(
workspace_id: Optional[str] = typer.Option(None, "--workspace", "-w", help="Workspace UUID override"),
proposal_id: Optional[str] = typer.Option(None, "--proposal", "-p", help="Validate a specific proposal UUID"),
as_json: bool = typer.Option(False, "--json", help="Output machine-readable JSON"),
strict: bool = typer.Option(True, "--strict/--no-strict", help="Exit with non-zero code on violations (ideal for CI/CD)"),
):
"""
Run policy rule validation (CI linter) on knowledge proposals.
"""
try:
active_ws_id = get_active_workspace(workspace_id)
except Exception as e:
print_error(str(e))
raise typer.Exit(code=1)
client = DiffWeaveMCPClient()
try:
val_res = client.call_tool_sync(
"validate_proposals",
{"workspace_id": active_ws_id, "proposal_id": proposal_id},
)
except Exception as e:
print_error(f"Validation failed: {e}")
raise typer.Exit(code=1)
if as_json:
print_json(val_res)
if strict and not compliant:
raise typer.Exit(code=1)
return
rules_total = val_res.get("rules_total", 0)
rules_enabled = val_res.get("rules_enabled", 0)
props_count = val_res.get("proposals_evaluated", 0)
compliant = val_res.get("compliant", False)
summary = val_res.get("summary", {})
results = val_res.get("results", [])
status_badge = "[bold green]COMPLIANT (0 VIOLATIONS)[/bold green]" if compliant else "[bold red]NON-COMPLIANT (VIOLATIONS DETECTED)[/bold red]"
console.print(
Panel(
f"[bold white]Policy Compliance Status:[/bold white] {status_badge}\n"
f"[bold white]Active Rules:[/bold white] {rules_enabled} of {rules_total} enabled\n"
f"[bold white]Proposals Evaluated:[/bold white] {props_count}\n"
f"[bold white]Passed Rules:[/bold white] [green]{summary.get('passed', 0)}[/green] | "
f"[bold white]Violations:[/bold white] [red]{summary.get('violations', 0)}[/red]",
title="[bold blue]DiffWeave Policy Validation Report[/bold blue]",
border_style="blue",
)
)
if not results:
console.print("[dim]No proposals or rules to evaluate.[/dim]")
return
table = Table(title="Rule Evaluation Details", header_style="bold cyan")
table.add_column("Rule Name", style="white")
table.add_column("Operator", style="dim")
table.add_column("Result", style="bold")
table.add_column("Severity", style="dim")
table.add_column("Details", style="white")
for r in results:
res_status = r.get("status", "INFO")
if res_status == "PASS":
styled_status = "[green]✔ PASS[/green]"
elif res_status == "FAIL":
styled_status = "[bold red]✖ FAIL[/bold red]"
elif res_status == "WARNING":
styled_status = "[bold yellow]▲ WARN[/bold yellow]"
else:
styled_status = f"[dim]{res_status}[/dim]"
table.add_row(
r.get("rule_name") or "Unnamed Rule",
r.get("operator") or "n/a",
styled_status,
r.get("severity") or "LOW",
r.get("message") or "",
)
console.print(table)
if strict and not compliant:
raise typer.Exit(code=1)
|