"""Build a run report from the harness's per-instance reports, for a run whose own final report was not written. python eval/instance_report.py [--ids id1 id2 ...] The SWE-bench harness scores each instance into logs/run_evaluation////report.json and then, as its last step, lists every container on the machine to write one summary file. On a machine where hundreds of short-lived containers of other runs come and go, that listing can fail (a container disappears between being listed and being read) and the summary is never written. The per-instance reports are complete by then; this adds them up the same way. An instance with a patch and no report counts as an error, to be scored again (eval/rescore.sh). """ import glob import json import sys def main(): folder, run_id, preds_path, out = sys.argv[1:5] only = set(sys.argv[sys.argv.index("--ids") + 1:]) if "--ids" in sys.argv else None preds = json.load(open(preds_path)) resolved, completed, errors, empty = [], [], [], [] for iid, p in sorted(preds.items()): if only is not None and iid not in only: continue if not (p.get("model_patch") or "").strip(): empty.append(iid) continue found = glob.glob(f"{folder}/logs/run_evaluation/{run_id}/*/{iid}/report.json") if not found: errors.append(iid) continue completed.append(iid) try: if json.load(open(found[0])).get(iid, {}).get("resolved"): resolved.append(iid) except ValueError: completed.pop() errors.append(iid) n = len(resolved) + (len(completed) - len(resolved)) + len(errors) + len(empty) report = {"built_from": "the per-instance reports (the harness's own run report was not written)", "total_instances": n, "submitted_instances": n, "completed_instances": len(completed), "resolved_instances": len(resolved), "unresolved_instances": len(completed) - len(resolved), "empty_patch_instances": len(empty), "error_instances": len(errors), "completed_ids": completed, "resolved_ids": resolved, "unresolved_ids": sorted(set(completed) - set(resolved)), "empty_patch_ids": empty, "error_ids": errors} json.dump(report, open(out, "w"), indent=1) print(f"built {out}: resolved {len(resolved)}, completed {len(completed)}, errors {len(errors)}, empty {len(empty)}") if __name__ == "__main__": main()