Files
CCLite/Python/checkers/audit_real_workers.py
T
2026-07-05 23:40:12 +08:00

307 lines
12 KiB
Python

from __future__ import annotations
import argparse
import json
import subprocess
from pathlib import Path
def run_command(command: list[str], timeout: float = 10.0) -> dict:
try:
completed = subprocess.run(command, text=True, capture_output=True, timeout=timeout)
except subprocess.TimeoutExpired as exc:
return {
"command": command,
"exit_code": None,
"stdout": exc.stdout or "",
"stderr": exc.stderr or "",
"status": "TIMEOUT",
}
return {
"command": command,
"exit_code": completed.returncode,
"stdout": completed.stdout,
"stderr": completed.stderr,
"status": "RAW",
}
def parse_key_value_output(text: str) -> dict[str, str]:
values: dict[str, str] = {}
for raw_line in text.splitlines():
line = raw_line.strip()
if not line or line.startswith("["):
continue
if "=" not in line:
continue
key, value = line.split("=", 1)
values[key.strip()] = value.strip()
return values
def is_yes(values: dict[str, str], key: str) -> bool:
return values.get(key, "").strip().lower() in {"yes", "true", "1", "on"}
def run_capabilities_probe(exe: Path) -> dict:
result = run_command([str(exe), "--cclite-capabilities"])
output = (result.get("stdout") or "") + "\n" + (result.get("stderr") or "")
capabilities = parse_key_value_output(output)
result["capabilities"] = capabilities
if result["exit_code"] == 0 and capabilities.get("cclite_worker", "").lower() == "yes":
result["status"] = "CAPABILITIES_AVAILABLE"
elif "unknown" in output.lower() and "capabilities" in output.lower():
result["status"] = "MISSING_CAPABILITIES_CLI"
else:
result["status"] = "CAPABILITIES_FAILED"
result["supports_real_ipc"] = is_yes(capabilities, "supports_real_ipc")
return result
def run_standard_worker_probe(
exe: Path,
solver_id: str,
case_file: Path,
extra_args: list[str] | None = None,
) -> dict:
command = [
str(exe),
"--cclite-worker",
]
if extra_args:
command.extend(extra_args)
command.extend([
"--solver-id",
solver_id,
"--case",
str(case_file),
"--ipc-name",
"CCLITE_REAL_WORKER_AUDIT",
"--timeout-ms",
"100",
])
result = run_command(command)
output = (result.get("stdout") or "") + "\n" + (result.get("stderr") or "")
output_lower = output.lower()
recognized = "--solver-id" not in output and "unknown argument" not in output_lower and "unknown worker argument" not in output_lower
unsupported = "not implemented" in output.lower() or "unsupported" in output.lower()
attach_attempted = (
"ipc attach failed" in output_lower
or "failed to open cclite ipc transport" in output_lower
or ("failed to open" in output_lower and "ipc" in output_lower)
)
result["status"] = classify_standard_probe(recognized, unsupported, attach_attempted, result["exit_code"])
result["recognized_worker_cli"] = recognized
result["real_ipc_attach_attempted"] = recognized and attach_attempted
result["ready_without_external_resources"] = recognized and result["exit_code"] == 0 and not unsupported
return result
def run_femlite_mock_probe(exe: Path, case_file: Path, report_path: Path) -> dict:
output_dir = report_path.parent / "femlite_worker_mock"
log_file = report_path.parent / "femlite_worker_mock.log"
result = run_command([
str(exe),
"--cclite-worker",
"--case",
str(case_file),
"--ipc-name",
"CCLITE_FEMLITE_WORKER_MOCK_AUDIT",
"--log",
str(log_file),
"--output-dir",
str(output_dir),
"--mock-steps",
"1",
])
output = (result.get("stdout") or "") + "\n" + (result.get("stderr") or "")
output_lower = output.lower()
if result["exit_code"] == 0 and (
"worker mock completed" in output_lower or "worker completed: mock" in output_lower
):
result["status"] = "WORKER_MOCK_AVAILABLE"
elif "unknown worker argument" in output_lower or "unknown argument" in output_lower:
result["status"] = "MISSING_WORKER_MOCK_CLI"
else:
result["status"] = "WORKER_MOCK_FAILED"
result["log_file"] = str(log_file)
result["output_dir"] = str(output_dir)
return result
def classify_standard_probe(
recognized: bool,
unsupported: bool,
attach_attempted: bool,
exit_code: int | None,
) -> str:
if recognized and unsupported:
return "CONTROLLED_UNSUPPORTED"
if recognized and attach_attempted:
return "IPC_ATTACH_ATTEMPTED"
if recognized and exit_code == 0:
return "READY_FOR_REAL_IPC"
if not recognized:
return "MISSING_STANDARD_WORKER_ARGS"
return "FAILED"
def build_protocol_alignment(fem_exe: Path) -> dict:
fem_include = fem_exe.parent.parent / "include" / "femlite_cclite_ipc.h"
fem_native_header = False
if fem_include.exists():
text = fem_include.read_text(encoding="utf-8", errors="ignore")
fem_native_header = (
"femlite_cclite_command_block" in text
and "femlite_cclite_data_header" in text
)
return {
"status": "CCLITE_PROTOCOL_ADAPTER_REQUIRED",
"cclite_current_protocol": {
"command_block": "CCLite generic command block v1",
"data_header": "CCLite generic data block header v0.1",
"control_names": [
"<prefix>_CCL_COMMAND_<solver>",
"<prefix>_EVENT_COMMAND_<solver>",
"<prefix>_EVENT_ACK_<solver>",
],
"data_names": [
"<prefix>_SPH_TO_CCL_PARTICLE",
"<prefix>_FEM_TO_CCL_FACET",
"<prefix>_CCL_TO_SPH_FORCE",
"<prefix>_CCL_TO_FEM_LOAD",
],
},
"sphlite_observed_protocol": {
"default_control_names": [
"<ipc_name>.command",
"<ipc_name>.command_event",
"<ipc_name>.done_event",
],
"supports_name_overrides": ["--command-block", "--data-prefix"],
"note": "SPHLite reports real IPC support, but true external CCLite process interop still requires a CCLite-side protocol/name adapter test.",
},
"femlite_observed_protocol": {
"native_header_detected": fem_native_header,
"header": str(fem_include),
"requires_worker_backend": "--worker-backend ipc",
"control_names": [
"<prefix>_COMMAND_BLOCK",
"<prefix>_COMMAND_EVENT",
"<prefix>_DONE_EVENT",
],
"data_names": [
"<data-prefix>_FEM_TO_CCL_FACET",
"<data-prefix>_CCL_TO_FEM_LOAD",
],
"note": "The current FEMLite package uses a native compact command block/data header that is not the same binary layout as CCLite 0.1.x generic blocks.",
},
}
def build_v020_adapter_gate() -> dict:
return {
"gate_id": "V020_SINGLE_REAL_WORKER_SMOKE",
"status": "CONTROLLED_PROTOCOL_GAP_UNTIL_ADAPTER_TESTS_PASS",
"mock_algorithm_acceptance_independent": True,
"required_paths": [
"real SPHLite + mock FEMLite, 1 step empty coupling",
"mock SPHLite + real FEMLite, 1 step empty coupling",
"at least one single-real-worker path, 100 step empty coupling",
"real-worker payload header and stride validation",
"real-worker empty or controlled load import validation",
],
"pass_conditions": [
"worker process starts through the CCLite worker CLI",
"command seq/ack stays matched for all commands",
"worker exports an empty or controlled interface block that passes CCLite validation",
"worker consumes an empty or controlled load block and acknowledges IMPORT_LOADS",
"worker responds to ADVANCE and STOP without timeout or residual process",
],
"controlled_gap_conditions": [
"published command block layout differs from CCLite generic command block",
"published data header layout differs from CCLite generic data block header",
"resource naming cannot be mapped without an adapter",
"record stride or block role cannot be validated by the current CCLite reader",
],
}
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--sph-exe", required=True)
parser.add_argument("--fem-exe", required=True)
parser.add_argument("--case-dir", required=True)
parser.add_argument("--fem-case", required=True)
parser.add_argument("--report", required=True)
args = parser.parse_args()
case_dir = Path(args.case_dir)
report_path = Path(args.report)
sph_exe = Path(args.sph_exe)
fem_exe = Path(args.fem_exe)
sph_capabilities = run_capabilities_probe(sph_exe)
fem_capabilities = run_capabilities_probe(fem_exe)
sph_standard = run_standard_worker_probe(sph_exe, "SPH", case_dir / "mock_sph.inp")
fem_standard = run_standard_worker_probe(fem_exe, "FEM", Path(args.fem_case))
fem_explicit_ipc = run_standard_worker_probe(
fem_exe,
"FEM",
Path(args.fem_case),
["--worker-backend", "ipc", "--data-prefix", "CCLITE_REAL_WORKER_AUDIT"],
)
fem_mock = run_femlite_mock_probe(fem_exe, Path(args.fem_case), report_path)
protocol_alignment = build_protocol_alignment(fem_exe)
both_report_real_ipc = sph_capabilities["supports_real_ipc"] and fem_capabilities["supports_real_ipc"]
both_attach_attempted = (
sph_standard["real_ipc_attach_attempted"]
and (fem_standard["real_ipc_attach_attempted"] or fem_explicit_ipc["real_ipc_attach_attempted"])
)
if both_report_real_ipc and both_attach_attempted:
status = "EXTERNAL_WORKERS_READY_CCLITE_PROTOCOL_ADAPTER_REQUIRED"
elif both_report_real_ipc:
status = "EXTERNAL_WORKERS_REPORT_REAL_IPC"
elif fem_mock["status"] == "WORKER_MOCK_AVAILABLE":
status = "PARTIAL_EXTERNAL_WORKER_SUPPORT"
else:
status = "CONTROLLED_GAP"
report = {
"status": status,
"required_worker_args": [
"--cclite-worker",
"--solver-id <SPH|FEM>",
"--case <solver.inp>",
"--ipc-name <prefix>",
"--timeout-ms <milliseconds>",
],
"sphlite_capabilities": sph_capabilities,
"femlite_capabilities": fem_capabilities,
"sphlite_standard_ipc": sph_standard,
"femlite_standard_ipc": fem_standard,
"femlite_explicit_ipc_backend": fem_explicit_ipc,
"femlite_worker_mock": fem_mock,
"protocol_alignment": protocol_alignment,
"v020_adapter_gate": build_v020_adapter_gate(),
"conclusion": [
"The current SPHLite package reports CCLite worker and Windows shared memory/named event support.",
"The current FEMLite package reports CCLite worker, worker mock, and Windows shared memory/named event support.",
"Both workers now attempt real IPC attach when launched without pre-created CCLite resources.",
"CCLite 0.1.10 still treats true external process interop as requiring a CCLite-side protocol/name adapter, because the published FEMLite IPC structures and names differ from CCLite 0.1.x generic blocks.",
"Mock-worker IPC integration remains the executable acceptance path until the adapter-level process tests pass.",
],
}
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"real worker audit -> {report_path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())