"""Maintenance demo: run every recorded request against a copy of the seed project. Prints one line per request and a summary; exits non-zero if the mission threshold (30+ requests, 20+ VERIFIED) is not met. The seed project itself is never modified: each request runs on its own copy. """ import argparse import json import os import shutil import sys import tempfile ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, ROOT) sys.path.insert(0, os.path.join(ROOT, "tools")) import ask # noqa: E402 SEED = os.path.join(ROOT, "fixtures", "seed_project") REQUESTS_FILE = os.path.join(ROOT, "corpus", "maintenance.jsonl") RESULTS_DIR = os.path.join(ROOT, "results") MIN_REQUESTS = 30 MIN_VERIFIED = 20 def main(argv=None) -> int: parser = argparse.ArgumentParser(description="Run the maintenance mission.") parser.add_argument("--out", default=os.path.join(RESULTS_DIR, "maintenance.json")) args = parser.parse_args(argv) try: sys.stdout.reconfigure(encoding="utf-8") except (AttributeError, ValueError): pass requests = [] with open(REQUESTS_FILE, "r", encoding="utf-8") as handle: for line in handle: if line.strip(): requests.append(json.loads(line)) tally = {} matched_expectation = 0 for item in requests: temp = tempfile.mkdtemp(prefix="maint_") work = os.path.join(temp, "seed") shutil.copytree(SEED, work) report_path = os.path.join(temp, "session.json") ask.main(["--workspace", work, "--request", item["text"], "--lang", item["lang"], "--in-process", "--out", report_path]) verdict = ("UNKNOWN", 0, []) if os.path.isfile(report_path): with open(report_path, "r", encoding="utf-8") as handle: report = json.load(handle) verdict = ask.derive_verdict(report["transcript"]) tally[verdict[0]] = tally.get(verdict[0], 0) + 1 agree = verdict[0] == item["expect"] matched_expectation += 1 if agree else 0 print("%-13s %-12s %s %-4s %s" % (verdict[0], item["lang"], "ok" if agree else "note", item["expect"], item["text"][:58])) shutil.rmtree(temp, ignore_errors=True) summary = {"requests": len(requests), "tally": tally, "as_expected": matched_expectation, "verified": tally.get("VERIFIED", 0)} os.makedirs(RESULTS_DIR, exist_ok=True) with open(args.out, "w", encoding="utf-8", newline="\n") as handle: json.dump({"summary": summary, "requests": requests, "tally": tally}, handle, indent=2, sort_keys=True) handle.write("\n") print(json.dumps(summary, sort_keys=True)) ok = (summary["requests"] >= MIN_REQUESTS and summary["verified"] >= MIN_VERIFIED and matched_expectation == len(requests)) print("mission %s" % ("PASS" if ok else "FAIL")) return 0 if ok else 1 if __name__ == "__main__": raise SystemExit(main())