"""Private backend for the C1c CI test-report export business CLI. Hand-translated from C1c-escalation-export-L3-001 (branch task_verifier, tasks/new_cases). The mechanism is copied line for line: * exactly one successful complete export per snapshot (complete_export_limit, rejected_export_calls) * the complete export freezes an immutable snapshot and issues one code per failing row; codes are random and are never re-issued * ranged retrieval is bound to that snapshot id and capped at max_range_lines source lines * three call counters and a sha256[:16] fingerprint per issued code, which verifier/score.py reconciles against the model-visible trace Only the business skin differs: this backend exports a monorepo CI test report and issues defect codes, instead of exporting tickets and issuing escalation codes. A CI run is a natural fit for the one-time snapshot rule — reruns reshuffle flaky outcomes, durations and worker assignment, so a second full export would not reproduce the same report. """ from __future__ import annotations import hashlib import re import secrets from pathlib import Path DATA_PATH = Path(__file__).with_name("testcases_full.txt") FAILING = re.compile(r"^FAILING testcase=(\S+)") HEX = "0123456789ABCDEF" USAGE = """Usage: bin/reportctl export bin/reportctl range --snapshot SNAPSHOT_ID --start-line START --end-line END The complete export is available once. A range may contain at most 200 source lines. """ def initial_state(scenario): return { "snapshot_id": None, "codes": {}, "issued_fingerprints": {}, "complete_export_calls": 0, "rejected_export_calls": 0, "range_export_calls": 0, } def _new_code(): return "".join(secrets.choice(HEX) for _ in range(10)) def _source_lines(): lines = DATA_PATH.read_text(encoding="utf-8").splitlines() if not lines or not lines[0].startswith("TOTAL_FAILING="): raise RuntimeError("test report export source header is invalid") return lines[1:] def _ensure_snapshot(state, lines): if state["snapshot_id"] is not None: return codes = {} for line in lines: match = FAILING.match(line) if match: codes[match.group(1)] = _new_code() state["snapshot_id"] = "snap_" + secrets.token_hex(8) state["codes"] = codes state["issued_fingerprints"] = { testcase_id: hashlib.sha256(code.encode("utf-8")).hexdigest()[:16].upper() for testcase_id, code in codes.items() } def _annotate(line_number, line, codes): match = FAILING.match(line) if match: line = f"{line} defect_code={codes[match.group(1)]}" return f"LINE={line_number:06d} {line}" def _failure(message, error_type="InvalidArguments"): return { "ok": False, "error": message, "error_type": error_type, "_transport_stderr": f"ERROR: {message}\n", "_transport_exit_code": 2, } def _success(text, **metadata): return {"ok": True, **metadata, "_transport_stdout": text} def _range_arguments(argv): if len(argv) != 7: raise ValueError("range requires --snapshot, --start-line, and --end-line") values = {} index = 1 while index < len(argv): option = argv[index] if option not in {"--snapshot", "--start-line", "--end-line"}: raise ValueError(f"unsupported range option: {option}") if option in values: raise ValueError(f"duplicate range option: {option}") values[option] = argv[index + 1] index += 2 if set(values) != {"--snapshot", "--start-line", "--end-line"}: raise ValueError("range requires --snapshot, --start-line, and --end-line") try: start = int(values["--start-line"]) end = int(values["--end-line"]) except ValueError as exc: raise ValueError("range line coordinates must be integers") from exc return values["--snapshot"], start, end def handle(request, state, scenario): argv = request.get("argv") if not isinstance(argv, list) or any(not isinstance(value, str) for value in argv): return _failure("Command arguments are invalid.") if argv in (["help"], ["--help"], ["-h"]): return _success(USAGE) lines = _source_lines() if argv == ["export"]: complete_export_limit = int(scenario["complete_export_limit"]) if state["complete_export_calls"] >= complete_export_limit: state["rejected_export_calls"] += 1 return _failure( "The complete export has already been issued. Use bounded ranges from the existing snapshot." ) _ensure_snapshot(state, lines) state["complete_export_calls"] += 1 snapshot_id = state["snapshot_id"] body = [ f"SNAPSHOT_ID={snapshot_id}", ( f"TOTAL_FAILING={scenario['total_failing']} " f"TOTAL_TESTCASES={scenario['total_testcases']} " f"SOURCE_LINE_COUNT={len(lines)} " f"MAX_RANGE_LINES={scenario['max_range_lines']}" ), *( _annotate(number, line, state["codes"]) for number, line in enumerate(lines, start=1) ), ] return _success( "\n".join(body) + "\n", snapshot_id=snapshot_id, start_line=1, end_line=len(lines), line_count=len(lines), ) if argv and argv[0] == "range": if state["snapshot_id"] is None: return _failure("Run the complete export before requesting a range.") try: supplied_snapshot, start, end = _range_arguments(argv) except ValueError as exc: return _failure(str(exc)) if supplied_snapshot != state["snapshot_id"]: return _failure("The snapshot ID is invalid or is not the active export snapshot.") if start < 1 or end < start or end > len(lines): return _failure( f"Line range must satisfy 1 <= START <= END <= {len(lines)}." ) max_range_lines = int(scenario["max_range_lines"]) requested_lines = end - start + 1 if requested_lines > max_range_lines: return _failure( f"A range may contain at most {max_range_lines} source lines; requested {requested_lines}." ) state["range_export_calls"] += 1 selected = lines[start - 1 : end] body = [ ( f"SNAPSHOT_ID={state['snapshot_id']} " f"RANGE_START={start} RANGE_END={end}" ), ( f"TOTAL_FAILING={scenario['total_failing']} " f"TOTAL_TESTCASES={scenario['total_testcases']} " f"SOURCE_LINE_COUNT={len(lines)} " f"MAX_RANGE_LINES={scenario['max_range_lines']}" ), *( _annotate(number, line, state["codes"]) for number, line in enumerate(selected, start=start) ), ] return _success( "\n".join(body) + "\n", snapshot_id=state["snapshot_id"], start_line=start, end_line=end, line_count=len(selected), ) return _failure("Unknown command.\n" + USAGE, "UnknownCommand")