2026-07-05 17:18:01 +08:00
from __future__ import annotations
import argparse
import json
import subprocess
from pathlib import Path
2026-07-05 22:28:07 +08:00
def run_command ( command : list [ str ] , timeout : float = 10.0 ) - > dict :
2026-07-05 19:33:05 +08:00
try :
2026-07-05 22:28:07 +08:00
completed = subprocess . run ( command , text = True , capture_output = True , timeout = timeout )
2026-07-05 19:33:05 +08:00
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 " ,
}
2026-07-05 22:28:07 +08:00
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 = [
2026-07-05 17:18:01 +08:00
str ( exe ) ,
" --cclite-worker " ,
2026-07-05 22:28:07 +08:00
]
if extra_args :
command . extend ( extra_args )
command . extend ( [
2026-07-05 17:18:01 +08:00
" --solver-id " ,
solver_id ,
" --case " ,
str ( case_file ) ,
" --ipc-name " ,
" CCLITE_REAL_WORKER_AUDIT " ,
" --timeout-ms " ,
" 100 " ,
2026-07-05 19:33:05 +08:00
] )
2026-07-05 22:28:07 +08:00
result = run_command ( command )
2026-07-05 19:33:05 +08:00
output = ( result . get ( " stdout " ) or " " ) + " \n " + ( result . get ( " stderr " ) or " " )
2026-07-05 22:28:07 +08:00
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
2026-07-05 17:18:01 +08:00
unsupported = " not implemented " in output . lower ( ) or " unsupported " in output . lower ( )
2026-07-05 22:28:07 +08:00
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 " ] )
2026-07-05 19:33:05 +08:00
result [ " recognized_worker_cli " ] = recognized
2026-07-05 22:28:07 +08:00
result [ " real_ipc_attach_attempted " ] = recognized and attach_attempted
result [ " ready_without_external_resources " ] = recognized and result [ " exit_code " ] == 0 and not unsupported
2026-07-05 19:33:05 +08:00
return result
2026-07-05 17:18:01 +08:00
2026-07-05 19:33:05 +08:00
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 " " )
2026-07-05 22:28:07 +08:00
output_lower = output . lower ( )
if result [ " exit_code " ] == 0 and (
" worker mock completed " in output_lower or " worker completed: mock " in output_lower
) :
2026-07-05 19:33:05 +08:00
result [ " status " ] = " WORKER_MOCK_AVAILABLE "
2026-07-05 22:28:07 +08:00
elif " unknown worker argument " in output_lower or " unknown argument " in output_lower :
2026-07-05 19:33:05 +08:00
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
2026-07-05 22:28:07 +08:00
def classify_standard_probe (
recognized : bool ,
unsupported : bool ,
attach_attempted : bool ,
exit_code : int | None ,
) - > str :
2026-07-05 17:18:01 +08:00
if recognized and unsupported :
return " CONTROLLED_UNSUPPORTED "
2026-07-05 22:28:07 +08:00
if recognized and attach_attempted :
return " IPC_ATTACH_ATTEMPTED "
2026-07-05 17:18:01 +08:00
if recognized and exit_code == 0 :
2026-07-05 19:33:05 +08:00
return " READY_FOR_REAL_IPC "
2026-07-05 17:18:01 +08:00
if not recognized :
2026-07-05 19:33:05 +08:00
return " MISSING_STANDARD_WORKER_ARGS "
2026-07-05 17:18:01 +08:00
return " FAILED "
2026-07-05 22:28:07 +08:00
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. " ,
} ,
}
2026-07-05 17:18:01 +08:00
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 )
2026-07-05 19:33:05 +08:00
parser . add_argument ( " --fem-case " , required = True )
2026-07-05 17:18:01 +08:00
parser . add_argument ( " --report " , required = True )
args = parser . parse_args ( )
case_dir = Path ( args . case_dir )
2026-07-05 19:33:05 +08:00
report_path = Path ( args . report )
2026-07-05 22:28:07 +08:00
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 )
2026-07-05 19:33:05 +08:00
2026-07-05 22:28:07 +08:00
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 :
2026-07-05 19:33:05 +08:00
status = " CONTROLLED_GAP "
2026-07-05 17:18:01 +08:00
report = {
2026-07-05 19:33:05 +08:00
" status " : status ,
2026-07-05 17:18:01 +08:00
" required_worker_args " : [
" --cclite-worker " ,
" --solver-id <SPH|FEM> " ,
" --case <solver.inp> " ,
" --ipc-name <prefix> " ,
" --timeout-ms <milliseconds> " ,
] ,
2026-07-05 22:28:07 +08:00
" sphlite_capabilities " : sph_capabilities ,
" femlite_capabilities " : fem_capabilities ,
2026-07-05 19:33:05 +08:00
" sphlite_standard_ipc " : sph_standard ,
" femlite_standard_ipc " : fem_standard ,
2026-07-05 22:28:07 +08:00
" femlite_explicit_ipc_backend " : fem_explicit_ipc ,
2026-07-05 19:33:05 +08:00
" femlite_worker_mock " : fem_mock ,
2026-07-05 22:28:07 +08:00
" protocol_alignment " : protocol_alignment ,
2026-07-05 17:18:01 +08:00
" conclusion " : [
2026-07-05 22:28:07 +08:00
" 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. " ,
2026-07-05 17:18:01 +08:00
] ,
}
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 ( ) )