Physically move the pure-code subtrees out of .specify into the package, leaving compat symlinks at the old .specify/<dir> paths so every existing reference (internal CASAN_HARNESS_ROOT + external CI/docker/mjs) keeps resolving. Runtime state stays put. Moved (git mv): scripts/ tests/ security/ templates/ config/ governance/ memory/ .specify/<dir> -> packages/casan-harness/<dir> (+ .specify/<dir> symlink) Stays in .specify (state/governance/domain, handled later): logs/ agentops/ level5/ init-options.json traceability-map.json Python `.resolve()` self-location followed the compat symlink into packages and lost the app root; generate-casan-demo-context.py, generate-agentops-dashboard.py and dashboard-server.py now walk UP for the `.specify` state marker instead of a fixed parent depth (fixes "missing trace files" in run-casan4). Full gate: PASS=64 FAIL=0 SKIP=3 (CASAN_CI_STEP_TIMEOUT_SEC=1200 — track-a ~450s runs close to the 600s default and can tip over under load; this is timing variance, not a regression — it passed cleanly with headroom). Runtime log/audit artifacts kept unstaged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
175 lines
6.6 KiB
Python
175 lines
6.6 KiB
Python
#!/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 <jsonl> [--drift <json>] -> proposals JSON on stdout (no writes)
|
|
apply --proposals <json> --id <ID> [--approval <tok>] -> 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())
|