#!/usr/bin/env python3 """CASAN self-improve core (Plan-04, harness-owned). Reads real telemetry (provider-usage / metrics JSONL + optional drift report) and emits improvement PROPOSALS (dry-run, never writes). Applying a proposal requires human approval (`--approval`) and goes through the governed settings store (control-plane-settings.py) so every change is audited. Loosen / security-sensitive proposals always require approval. Subcommands: propose --metrics [--drift ] -> proposals JSON on stdout (no writes) apply --proposals --id [--approval ] -> governed set (needs approval) """ import argparse import json import os import statistics import subprocess import sys def read_jsonl(path): rows = [] if not path or not os.path.isfile(path): return rows with open(path, encoding="utf-8") as fh: for line in fh: line = line.strip() if not line: continue try: rows.append(json.loads(line)) except ValueError: continue return rows def build_proposals(metrics_rows, drift): proposals = [] costs = [r["cost_usd"] for r in metrics_rows if isinstance(r.get("cost_usd"), (int, float))] if costs: cap = round(max(costs) * 1.5, 4) proposals.append({ "id": "P-COST-CAP", "type": "calibrate_cost_cap", "key": "cost.absolute_cap_usd", "value": cap, "direction": "tighten", "security_sensitive": False, "reason": f"observed max cost {max(costs)}; set cap to 1.5x = {cap}", }) if drift and (drift.get("drift") is True or drift.get("entries")): proposals.append({ "id": "P-GOLDEN", "type": "update_golden", "key": None, "value": None, "direction": "loosen", "security_sensitive": True, "reason": "drift detected; updating golden may hide real regressions — needs review", }) return proposals def verify_metrics_integrity(path, sig, pub): """True only if the metrics file has a valid detached signature (openssl). ARCH-08: proposals from telemetry that is not integrity-verified are marked untrusted so a reviewer (and the apply gate) treats them with suspicion.""" if not (path and sig and pub): return False if not (os.path.isfile(path) and os.path.isfile(sig) and os.path.isfile(pub)): return False try: r = subprocess.run( ["openssl", "dgst", "-sha256", "-verify", pub, "-signature", sig, path], capture_output=True, ) return r.returncode == 0 except Exception: return False def cmd_propose(args): metrics = read_jsonl(args.metrics) drift = None if args.drift and os.path.isfile(args.drift): try: drift = json.load(open(args.drift, encoding="utf-8")) except ValueError: drift = None # ARCH-08 telemetry-poisoning defence: tag every proposal with the trust level # of its source telemetry. Unsigned/unverifiable metrics -> untrusted. trusted = verify_metrics_integrity(args.metrics, args.metrics_sig, args.metrics_pub) source_trust = "verified" if trusted else "untrusted" proposals = build_proposals(metrics, drift) for p in proposals: p["source_trust"] = source_trust print(json.dumps({"proposals": proposals, "count": len(proposals), "source_trust": source_trust}, ensure_ascii=False, indent=2)) return 0 def cmd_apply(args): try: data = json.load(open(args.proposals, encoding="utf-8")) except (OSError, ValueError): print(f"IMPROVE_DENY PROPOSALS_UNREADABLE {args.proposals}", file=sys.stderr) return 1 proposal = next((p for p in data.get("proposals", []) if p.get("id") == args.id), None) if proposal is None: print(f"IMPROVE_DENY UNKNOWN_PROPOSAL {args.id}", file=sys.stderr) return 1 # ARCH-08: in enforced mode (prod / CASAN_SELFIMPROVE_STRICT=1) refuse to apply a # proposal derived from unverified telemetry unless explicitly allowed with # justification. Dev default only tags (backward compatible). Missing tag = # untrusted (fail-closed). enforced = (os.environ.get("CASAN_PROFILE") == "prod" or os.environ.get("CASAN_SELFIMPROVE_STRICT") == "1") if (proposal.get("source_trust", "untrusted") == "untrusted" and enforced and not args.allow_untrusted): print(f"IMPROVE_DENY UNTRUSTED_SOURCE {args.id} (telemetry not integrity-verified; " f"re-run propose with --metrics-sig/--metrics-pub, or pass --allow-untrusted)", file=sys.stderr) return 1 # Proposal != application: applying ALWAYS requires human approval (Plan-04). if not (args.approval or "").strip(): print(f"IMPROVE_DENY APPROVAL_REQUIRED {args.id}", file=sys.stderr) return 1 if proposal.get("key") is None: # Non-settings proposal (e.g. update_golden) — record intent, no auto-apply. print(f"IMPROVE_MANUAL {args.id} type={proposal.get('type')} (no auto-apply; do it under review)") return 0 cps = os.path.join(os.path.dirname(__file__), "control-plane-settings.py") cmd = [ sys.executable, cps, "set", proposal["key"], json.dumps(proposal["value"]), "--actor", "casan-improve", "--reason", f"auto-improve {args.id}", ] if proposal.get("security_sensitive"): cmd += ["--approval", args.approval] result = subprocess.run(cmd, capture_output=True, text=True) if result.returncode != 0: print(f"IMPROVE_DENY GOVERNED_SET_FAILED {result.stderr.strip()}", file=sys.stderr) return 1 print(f"IMPROVE_APPLIED {args.id} key={proposal['key']} value={proposal['value']}") return 0 def main() -> int: ap = argparse.ArgumentParser() sub = ap.add_subparsers(dest="cmd", required=True) pr = sub.add_parser("propose") pr.add_argument("--metrics", default="") pr.add_argument("--drift", default="") pr.add_argument("--metrics-sig", default="") pr.add_argument("--metrics-pub", default="") ap_ = sub.add_parser("apply") ap_.add_argument("--proposals", required=True) ap_.add_argument("--id", required=True) ap_.add_argument("--approval", default="") ap_.add_argument("--allow-untrusted", action="store_true") args = ap.parse_args() if args.cmd == "propose": return cmd_propose(args) if args.cmd == "apply": return cmd_apply(args) return 2 if __name__ == "__main__": raise SystemExit(main())