Implement CCLite 0.1.6 diagnostics checker

This commit is contained in:
Codex
2026-07-05 17:13:31 +08:00
parent a9f40635b6
commit 731e27df67
15 changed files with 555 additions and 49 deletions
@@ -0,0 +1,159 @@
from __future__ import annotations
import argparse
import json
from pathlib import Path
from common_csv import as_float, as_int, read_required_csv
IPC_HEADERS = [
"step_id",
"solver_id",
"command_id",
"command_seq",
"ack_seq",
"worker_state",
"heartbeat_seq",
"elapsed_ms",
"error_code",
]
FORCE_HEADERS = [
"step_id",
"sph_fx",
"sph_fy",
"sph_fz",
"fem_fx",
"fem_fy",
"fem_fz",
"total_fx",
"total_fy",
"total_fz",
]
def build_report(args: argparse.Namespace) -> dict:
output_dir = Path(args.output_dir)
ipc_path = output_dir / "ipc_report.csv"
force_path = output_dir / "force_balance.csv"
log_path = output_dir / "run.log"
failed: list[str] = []
metrics: dict[str, int | float] = {}
try:
_, ipc_rows = read_required_csv(ipc_path, IPC_HEADERS)
_, force_rows = read_required_csv(force_path, FORCE_HEADERS)
except AssertionError as exc:
ipc_rows = []
force_rows = []
failed.append(str(exc))
if not log_path.exists():
failed.append(f"missing log file: {log_path}")
if args.headers_only:
return make_report(args, failed, metrics, ipc_path, force_path, log_path)
if not ipc_rows:
failed.append("ipc_report.csv has no data rows")
if not force_rows:
failed.append("force_balance.csv has no data rows")
stop_rows = 0
for index, row in enumerate(ipc_rows, start=1):
try:
command_seq = as_int(row, "command_seq")
ack_seq = as_int(row, "ack_seq")
command_id = as_int(row, "command_id")
worker_state = as_int(row, "worker_state")
heartbeat_seq = as_int(row, "heartbeat_seq")
elapsed_ms = as_float(row, "elapsed_ms")
error_code = as_int(row, "error_code")
except AssertionError as exc:
failed.append(f"ipc row {index}: {exc}")
continue
if command_seq != ack_seq:
failed.append(f"ipc row {index}: seq/ack mismatch {command_seq}/{ack_seq}")
if error_code != 0:
failed.append(f"ipc row {index}: nonzero error_code {error_code}")
if elapsed_ms < 0.0:
failed.append(f"ipc row {index}: negative elapsed_ms")
if heartbeat_seq < 0:
failed.append(f"ipc row {index}: negative heartbeat")
if command_id == 99:
stop_rows += 1
if worker_state != 6:
failed.append(f"ipc row {index}: STOP did not leave worker STOPPED")
if stop_rows != 2:
failed.append(f"expected two STOP rows, got {stop_rows}")
seen_steps: set[int] = set()
for index, row in enumerate(force_rows, start=1):
try:
step_id = as_int(row, "step_id")
values = [as_float(row, key) for key in FORCE_HEADERS if key != "step_id"]
except AssertionError as exc:
failed.append(f"force row {index}: {exc}")
continue
seen_steps.add(step_id)
for key, value in zip(FORCE_HEADERS[1:], values):
if abs(value) > 1.0e-12:
failed.append(f"force row {index}: {key} expected 0, got {value}")
expected_steps = set(range(1, args.expected_steps + 1))
if seen_steps != expected_steps:
failed.append(f"force steps mismatch: expected 1..{args.expected_steps}, got {sorted(seen_steps)}")
metrics["ipc_rows"] = len(ipc_rows)
metrics["force_rows"] = len(force_rows)
metrics["stop_rows"] = stop_rows
metrics["expected_steps"] = args.expected_steps
return make_report(args, failed, metrics, ipc_path, force_path, log_path)
def make_report(
args: argparse.Namespace,
failed: list[str],
metrics: dict,
ipc_path: Path,
force_path: Path,
log_path: Path,
) -> dict:
return {
"case_id": args.case_id,
"status": "PASS" if not failed else "FAIL",
"metrics": metrics,
"failed_checks": failed,
"input": str(Path(args.input)),
"output_files": {
"run_log": str(log_path),
"ipc_report": str(ipc_path),
"force_balance": str(force_path),
},
}
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--case-id", required=True)
parser.add_argument("--input", required=True)
parser.add_argument("--output-dir", required=True)
parser.add_argument("--report", required=True)
parser.add_argument("--expected-steps", type=int, default=100)
parser.add_argument("--headers-only", action="store_true")
args = parser.parse_args()
report = build_report(args)
report_path = Path(args.report)
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(f"{report['case_id']} {report['status']} -> {report_path}")
return 0 if report["status"] == "PASS" else 1
if __name__ == "__main__":
raise SystemExit(main())
+33
View File
@@ -0,0 +1,33 @@
from __future__ import annotations
import csv
from pathlib import Path
def read_required_csv(path: Path, required_headers: list[str]) -> tuple[list[str], list[dict[str, str]]]:
if not path.exists():
raise AssertionError(f"missing CSV file: {path}")
with path.open("r", encoding="utf-8", newline="") as stream:
reader = csv.DictReader(stream)
headers = list(reader.fieldnames or [])
missing = [name for name in required_headers if name not in headers]
if missing:
raise AssertionError(f"{path.name} missing headers: {', '.join(missing)}")
rows = list(reader)
return headers, rows
def as_int(row: dict[str, str], key: str) -> int:
try:
return int(row[key])
except Exception as exc:
raise AssertionError(f"invalid integer in {key}: {row.get(key)!r}") from exc
def as_float(row: dict[str, str], key: str) -> float:
try:
return float(row[key])
except Exception as exc:
raise AssertionError(f"invalid float in {key}: {row.get(key)!r}") from exc
@@ -0,0 +1,25 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "CCLite verification report",
"type": "object",
"required": ["case_id", "status", "metrics", "failed_checks", "input", "output_files"],
"properties": {
"case_id": { "type": "string" },
"status": { "type": "string", "enum": ["PASS", "FAIL"] },
"metrics": { "type": "object" },
"failed_checks": {
"type": "array",
"items": { "type": "string" }
},
"input": { "type": "string" },
"output_files": {
"type": "object",
"required": ["run_log", "ipc_report", "force_balance"],
"properties": {
"run_log": { "type": "string" },
"ipc_report": { "type": "string" },
"force_balance": { "type": "string" }
}
}
}
}