-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocessor.py
More file actions
1481 lines (1294 loc) · 57.6 KB
/
processor.py
File metadata and controls
1481 lines (1294 loc) · 57.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Execute Processor module for GNN Processing Pipeline.
This module provides execute processing capabilities for rendered implementations.
"""
import copy
import json
import logging
import os
import subprocess # nosec B404 -- subprocess calls with controlled/trusted input
import sys
from concurrent.futures import ProcessPoolExecutor
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union
from utils.logging.logging_utils import (
log_step_error,
log_step_start,
log_step_success,
log_step_warning,
)
try:
from utils.logging.logging_utils import PipelineLogger
except ImportError:
PipelineLogger = None
logger = logging.getLogger(__name__)
def check_julia_dependencies(
verbose: bool, log: Optional[logging.Logger] = None
) -> bool:
"""Check if required Julia packages are available.
Args:
verbose: Enable verbose logging.
log: Optional logger instance; defaults to module logger if not provided.
Returns:
True if dependencies ok, False otherwise.
"""
if log is None:
log = logger
try:
# check basic julia availability
subprocess.run(
["julia", "--version"], capture_output=True, check=True, timeout=10
) # nosec B607 B603 -- subprocess calls with controlled/trusted input
# Check for key packages
check_script = (
'using Pkg; Pkg.status(["RxInfer", "ActiveInference", "GraphPPL"])'
)
result = subprocess.run( # nosec B607 B603 -- subprocess calls with controlled/trusted input
["julia", "-e", check_script], capture_output=True, text=True, timeout=30
)
if result.returncode != 0:
if verbose:
log.warning(f"Julia package check failed: {result.stderr}")
return False
return True
except (subprocess.CalledProcessError, FileNotFoundError):
return False
def determine_script_framework(
script_path: Path, render_output_dir: Path, framework_dirs: Dict[str, str]
) -> str:
"""
Determine the framework for a script based on its directory path.
Args:
script_path: Path to the script
render_output_dir: Base render output directory
framework_dirs: Mapping of directory names to framework names
Returns:
Framework name or 'unknown'
"""
try:
# Get relative path from render output directory
relative_path = script_path.relative_to(render_output_dir)
# Render outputs use model/framework/script.ext. Match framework
# directories exactly so model names like "bnlearn_causal_model" do
# not override the actual framework directory.
for part in relative_path.parts[:-1]:
if part.lower() in framework_dirs:
return framework_dirs[part.lower()]
script_name = relative_path.name.lower()
for framework_name in framework_dirs.values():
if script_name.endswith(f"_{framework_name}.py") or script_name.endswith(
f"_{framework_name}.jl"
):
return framework_name
# Default recovery
return "unknown"
except Exception as e:
logging.getLogger(__name__).debug(
f"Error determining framework for script: {e}"
)
return "unknown"
# Phase 2.3: framework-availability helpers moved to utils.framework_availability
# so execute and render stay in sync. The import-check dict and predicate are
# re-exported here via thin aliases to preserve any external callers that
# previously imported them from execute.processor.
from utils.framework_availability import ( # noqa: E402
FRAMEWORK_IMPORT_CHECK as _FRAMEWORK_IMPORT_CHECK,
)
from utils.framework_availability import (
is_framework_available as _is_framework_available_by_name,
)
def _is_python_framework_dependency_available(
framework: str, executor: str, logger
) -> bool:
"""Return True if the framework's required Python module is importable.
Delegates to ``utils.framework_availability.is_framework_available``, passing
``executor`` so the check targets the subprocess-invoked interpreter rather
than the caller's. Preserves the pre-Phase-2.3 call-site signature.
"""
return _is_framework_available_by_name(framework, executor=executor, logger=logger)
def _make_skipped_result(
script_info: Dict[str, Any], framework: str, model_name: str, executor: str, logger
) -> Dict[str, Any]:
"""Build an execution result dict for a script skipped due to missing dependency."""
module_name, install_hint = _FRAMEWORK_IMPORT_CHECK.get(framework, ("", ""))
reason = (
f"Dependency not installed: {module_name}"
if module_name
else "Dependency not installed"
)
if install_hint and not logger.isEnabledFor(logging.DEBUG):
logger.info(
f"Skipping {script_info['name']} ({framework}): {module_name} not installed. Install with: {install_hint}"
)
return {
"script_path": str(script_info["path"]),
"script_name": script_info["name"],
"framework": framework,
"model_name": model_name,
"executor": executor,
"success": False,
"skipped": True,
"return_code": None,
"stdout": "",
"stderr": "",
"execution_time": 0,
"timestamp": datetime.now().isoformat(),
"error": reason,
"error_type": "DependencyNotInstalled",
}
def _coerce_execution_workers(value: Any) -> int:
"""Normalize the configured local/distributed worker count."""
try:
workers = int(value)
except (TypeError, ValueError):
workers = 1
return max(1, workers)
def _execute_script_worker(
bundle: Tuple[Dict[str, Any], Path, bool, int, int],
) -> Dict[str, Any]:
"""Process-pool entry point for a single rendered script."""
script_info, results_dir, verbose, timeout, repeats = bundle
worker_logger = logging.getLogger("execute.worker")
worker_logger.setLevel(logging.INFO)
result = execute_single_script(
script_info,
results_dir,
verbose,
worker_logger,
timeout,
execution_benchmark_repeats=repeats,
)
result.setdefault("skipped", False)
return result
def _run_scripts_with_local_workers(
executable_scripts: List[Dict[str, Any]],
results_dir: Path,
verbose: bool,
logger: logging.Logger,
timeout: int,
execution_workers: int,
execution_benchmark_repeats: int,
) -> List[Dict[str, Any]]:
"""Execute rendered scripts locally, using multiple processes when requested."""
repeats = max(1, int(execution_benchmark_repeats))
if execution_workers <= 1 or len(executable_scripts) <= 1:
details = []
for script_info in executable_scripts:
exec_result = execute_single_script(
script_info,
results_dir,
verbose,
logger,
timeout,
execution_benchmark_repeats=repeats,
)
exec_result.setdefault("skipped", False)
details.append(exec_result)
return details
bounded_workers = min(execution_workers, len(executable_scripts))
logger.info(
"Dispatching %s executable scripts with %s local workers",
len(executable_scripts),
bounded_workers,
)
bundles = [
(info, results_dir, verbose, timeout, repeats) for info in executable_scripts
]
with ProcessPoolExecutor(max_workers=bounded_workers) as pool:
return list(pool.map(_execute_script_worker, bundles))
def parse_frameworks_parameter(frameworks: str, logger) -> List[str]:
"""
Parse the frameworks parameter into a list of framework names.
Args:
frameworks: Comma-separated string of framework names or preset
logger: Logger instance
Returns:
List of framework names to include
"""
if not frameworks or frameworks.lower() == "all":
return [
"pymdp",
"jax",
"discopy",
"rxinfer",
"activeinference_jl",
"pytorch",
"numpyro",
"bnlearn",
]
if frameworks.lower() == "lite":
return ["pymdp", "jax", "discopy", "bnlearn"]
# Parse comma-separated list
framework_list = [f.strip() for f in frameworks.split(",")]
valid_frameworks = [
"pymdp",
"jax",
"discopy",
"rxinfer",
"activeinference_jl",
"pytorch",
"numpyro",
"bnlearn",
]
# Filter out invalid frameworks
valid_list = [f for f in framework_list if f in valid_frameworks]
if len(valid_list) != len(framework_list):
invalid = [f for f in framework_list if f not in valid_frameworks]
logger.warning(
f"Invalid frameworks specified: {invalid}. Valid options: {valid_frameworks}"
)
return valid_list if valid_list else ["pymdp"] # Default to pymdp if nothing valid
def _resolve_render_output_dir(
target_dir: Path,
kwargs: dict,
output_dir: Optional[Path] = None,
) -> Optional[Path]:
"""Resolve the render output directory from kwargs and filesystem heuristics.
Resolution priority:
1. Explicit ``--render-output-dir`` kwarg.
2. Sibling of the current step's output dir: when ``output_dir`` is
``<base>/12_execute_output``, use ``<base>/11_render_output`` (and nested layout).
3. target_dir itself if it looks like a render output directory.
4. Common pipeline and test output locations (searched in order).
Returns the first existing, non-empty directory found, or None.
"""
def _if_nonempty(p: Path) -> Optional[Path]:
if p.exists() and any(p.rglob("*")):
return p
return None
# Priority 1: explicit kwarg
if kwargs.get("render_output_dir"):
p = Path(kwargs["render_output_dir"])
return _if_nonempty(p) or p
# Priority 2: same pipeline base as step 12 (target often remains GNN input dir)
if output_dir is not None:
base = output_dir.parent
for rel in (
"11_render_output/11_render_output",
"11_render_output",
):
found = _if_nonempty(base / rel)
if found is not None:
return found
# Priority 3: target_dir is already the render output
if "11_render_output" in str(target_dir) or target_dir.name == "11_render_output":
return _if_nonempty(target_dir) or target_dir
# Priority 4: search common cwd-relative locations.
candidates: List[Path] = [
target_dir.parent / "output" / "11_render_output",
target_dir / "11_render_output",
Path("output/test_render/11_render_output/11_render_output"),
Path("output/test_render_improved/11_render_output/11_render_output"),
*list(Path("output").glob("*/11_render_output/11_render_output")),
*list(Path("output").glob("**/11_render_output")),
]
for candidate in candidates:
found = _if_nonempty(candidate)
if found is not None:
return found
return None
def _load_render_summary_contract(
render_output_dir: Path,
requested_frameworks: List[str],
logger: logging.Logger,
) -> Tuple[Optional[set[Path]], List[Dict[str, str]]]:
"""Load the latest Step 11 render contract for script filtering and failures."""
summary_file = render_output_dir / "render_processing_summary.json"
if not summary_file.exists():
return None, []
try:
summary = json.loads(summary_file.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as exc:
logger.warning("Could not read render summary %s: %s", summary_file, exc)
return None, []
requested = set(requested_frameworks)
allowed_scripts: set[Path] = set()
render_failures: List[Dict[str, str]] = []
file_results = summary.get("file_results")
if not isinstance(file_results, dict):
return None, []
for source_file, file_result in file_results.items():
framework_results = (
file_result.get("framework_results", {})
if isinstance(file_result, dict)
else {}
)
if not isinstance(framework_results, dict):
continue
for framework, framework_result in framework_results.items():
if framework not in requested or not isinstance(framework_result, dict):
continue
if framework_result.get("success"):
for output_file in framework_result.get("output_files") or []:
allowed_scripts.add(Path(output_file).resolve())
else:
render_failures.append(
{
"file": Path(str(source_file)).name,
"framework": str(framework),
"message": str(framework_result.get("message", "")),
}
)
return allowed_scripts, render_failures
def _summarize_collected_outputs(coll: Any) -> Any:
"""Replace bulky collected_outputs with counts safe for aggregate JSON."""
if coll is None:
return None
if isinstance(coll, dict):
out: Dict[str, Any] = {}
for k, v in coll.items():
if isinstance(v, list):
out[str(k)] = {"count": len(v)}
elif isinstance(v, dict):
out[str(k)] = {"n_keys": len(v)}
else:
out[str(k)] = v
return out
if isinstance(coll, list):
return {"count": len(coll)}
return coll
def _slim_execution_detail(detail: Dict[str, Any]) -> Dict[str, Any]:
"""Strip heavy fields from a per-script execution result for aggregate summaries."""
keys_keep = (
"script_path",
"script_name",
"framework",
"model_name",
"executor",
"success",
"skipped",
"return_code",
"error",
"error_type",
"execution_time",
"timestamp",
"execution_benchmark_repeats",
"execution_time_mean",
"execution_time_std",
"execution_time_samples",
"structured_result_file",
"output_file",
"implementation_directory",
)
slim: Dict[str, Any] = {}
for k in keys_keep:
if k in detail:
slim[k] = detail[k]
if isinstance(detail.get("stdout"), str):
slim["stdout_length"] = len(detail["stdout"])
if isinstance(detail.get("stderr"), str):
slim["stderr_length"] = len(detail["stderr"])
if "collected_outputs" in detail:
slim["collected_outputs_summary"] = _summarize_collected_outputs(
detail["collected_outputs"]
)
return slim
def process_execute(
target_dir: Path,
output_dir: Path,
verbose: bool = False,
frameworks: str = "all",
**kwargs: Any,
) -> Union[bool, int]:
"""
Execute rendered implementations from 11_render_output directory.
This function searches for executable scripts generated by 11_render.py
and executes them using subprocess, capturing their outputs and results.
Args:
target_dir: Directory containing rendered executable scripts (typically 11_render_output)
output_dir: Directory to save execution results
verbose: Enable verbose output
**kwargs: Additional arguments
Returns:
True if processing successful, False otherwise
"""
logger = logging.getLogger("execute")
try:
log_step_start(
logger, "Processing execute - searching for rendered implementations"
)
# Phase 1.3: validate frameworks arg before parsing. Rejects non-string
# input and fully-unknown framework lists early with a clear error.
try:
from utils.validation_schemas import validate_frameworks_arg
frameworks = validate_frameworks_arg(frameworks, context="process_execute")
except ValueError as _verr:
log_step_error(logger, f"Invalid frameworks argument: {_verr}")
return False
# Parse frameworks parameter
requested_frameworks = parse_frameworks_parameter(frameworks, logger)
strict_requested_frameworks = str(frameworks).lower() not in {"all", "lite"}
logger.info(f"Requested frameworks: {requested_frameworks}")
results_dir = output_dir
results_dir.mkdir(parents=True, exist_ok=True)
execution_benchmark_repeats = max(
1, int(kwargs.get("execution_benchmark_repeats", 1))
)
execution_summary_detail = bool(kwargs.get("execution_summary_detail", False))
# Initialize execution results
execution_results = {
"timestamp": datetime.now().isoformat(),
"target_directory": str(target_dir),
"output_directory": str(output_dir),
"total_scripts_found": 0,
"successful_executions": 0,
"failed_executions": 0,
"skipped_executions": 0,
"execution_details": [],
"framework_status": {},
"execution_mode": "local",
"execution_workers": 1,
"backend": None,
"execution_benchmark_repeats": execution_benchmark_repeats,
"execution_summary_detail": execution_summary_detail,
"success": True,
}
# Look for rendered implementations from render output
render_output_dir = _resolve_render_output_dir(
target_dir, kwargs, output_dir=results_dir
)
if render_output_dir is not None and render_output_dir != target_dir:
logger.info(f"Found render output directory: {render_output_dir}")
if verbose:
logger.info(f"Searching for executable scripts in: {render_output_dir}")
if not render_output_dir or not render_output_dir.exists():
log_step_warning(
logger, f"Render output directory not found: {render_output_dir}"
)
execution_results["success"] = True # Not a hard error
execution_results["skipped_reason"] = "no_render_output"
execution_results["message"] = "No rendered implementations found"
else:
allowed_render_scripts, render_failures = _load_render_summary_contract(
render_output_dir,
requested_frameworks,
logger,
)
execution_results["render_failures"] = render_failures
# Find executable scripts, filtered by requested frameworks
executable_scripts = find_executable_scripts(
render_output_dir, verbose, logger, requested_frameworks
)
if allowed_render_scripts is not None:
before_filter = len(executable_scripts)
executable_scripts = [
script
for script in executable_scripts
if script["path"].resolve() in allowed_render_scripts
]
found_allowed_scripts = {
script["path"].resolve() for script in executable_scripts
}
missing_render_scripts = sorted(
str(path) for path in allowed_render_scripts - found_allowed_scripts
)
execution_results["missing_render_scripts"] = missing_render_scripts
if missing_render_scripts:
logger.error(
"Latest render summary references %d requested scripts not discoverable for execution",
len(missing_render_scripts),
)
filtered_count = before_filter - len(executable_scripts)
if filtered_count:
logger.warning(
"Ignoring %d rendered scripts not present in the latest render summary",
filtered_count,
)
execution_results["total_scripts_found"] = len(executable_scripts)
execution_results["requested_frameworks"] = requested_frameworks
if not executable_scripts:
log_step_warning(logger, "No executable scripts found in render output")
execution_results["message"] = "No executable scripts found"
execution_results["success"] = True
execution_results["skipped_reason"] = "no_executable_scripts"
else:
logger.info(
f"Found {len(executable_scripts)} executable scripts to run"
)
# Extract args
timeout = kwargs.get("timeout", 3600)
is_distributed = kwargs.get("distributed", False)
execution_workers = _coerce_execution_workers(
kwargs.get("execution_workers", 1)
)
execution_results["execution_mode"] = (
"distributed" if is_distributed else "local"
)
execution_results["execution_workers"] = execution_workers
execution_results["backend"] = (
kwargs.get("backend", "ray") if is_distributed else None
)
details = []
if is_distributed:
from .distributed import Dispatcher
backend = kwargs.get("backend", "ray")
dispatcher = Dispatcher(backend=backend, num_cpus=execution_workers)
def ray_script_runner(info, **kws):
"""Execute a rendered simulation script using Ray for distributed processing."""
import logging
local_logger = logging.getLogger("execute.worker")
local_logger.setLevel(logging.INFO)
return execute_single_script(
info,
kws["results_dir"],
kws["verbose"],
local_logger,
kws["timeout"],
execution_benchmark_repeats=kws.get(
"execution_benchmark_repeats", 1
),
)
details = dispatcher.run_scripts_parallel(
executable_scripts,
ray_script_runner,
results_dir=results_dir,
verbose=verbose,
timeout=timeout,
execution_benchmark_repeats=execution_benchmark_repeats,
)
else:
details = _run_scripts_with_local_workers(
executable_scripts,
results_dir,
verbose,
logger,
timeout,
execution_workers,
execution_benchmark_repeats,
)
# Update aggregated results
for exec_result in details:
execution_results["execution_details"].append(exec_result)
# Update framework status
framework = exec_result.get("framework", "unknown")
if framework not in execution_results["framework_status"]:
execution_results["framework_status"][framework] = {
"status": "unknown",
"executions": 0,
}
execution_results["framework_status"][framework]["executions"] += 1
if exec_result.get("skipped"):
execution_results["skipped_executions"] = (
execution_results.get("skipped_executions", 0) + 1
)
execution_results["framework_status"][framework]["status"] = (
"skipped"
)
if "error" in exec_result:
execution_results["framework_status"][framework][
"error"
] = exec_result["error"]
elif exec_result["success"]:
execution_results["successful_executions"] += 1
execution_results["framework_status"][framework]["status"] = (
"success"
)
else:
execution_results["failed_executions"] += 1
execution_results["framework_status"][framework]["status"] = (
"failed"
)
if "error" in exec_result:
execution_results["framework_status"][framework][
"error"
] = exec_result["error"]
# Populate summary counters before saving.
total_found = execution_results["total_scripts_found"]
successful = execution_results["successful_executions"]
skipped = execution_results.get("skipped_executions", 0)
attempted = total_found - skipped
execution_results["total_scripts"] = total_found
execution_results["success_rate"] = (
round(successful / attempted * 100, 2) if attempted > 0 else 100.0
)
# Save detailed results to summaries subfolder (slim aggregate + optional full detail file)
summaries_dir = results_dir / "summaries"
summaries_dir.mkdir(parents=True, exist_ok=True)
results_file = summaries_dir / "execution_summary.json"
full_details_snapshot = copy.deepcopy(execution_results["execution_details"])
execution_results["execution_details"] = [
_slim_execution_detail(d) for d in full_details_snapshot
]
execution_results["execution_summary_format"] = "slim_v1"
with open(results_file, "w") as f:
json.dump(execution_results, f, indent=2, default=str)
if execution_summary_detail:
detail_path = summaries_dir / "execution_summary_detail.json"
detail_payload = dict(execution_results)
detail_payload["execution_details"] = full_details_snapshot
detail_payload["execution_summary_format"] = "detail_v1"
with open(detail_path, "w") as f:
json.dump(detail_payload, f, indent=2, default=str)
# Generate execution report (uses slim execution_details)
generate_execution_report(execution_results, results_dir, logger)
# Restore full details in-memory for any downstream callers of this function
execution_results["execution_details"] = full_details_snapshot
# Determine overall success: only count real failures (not skipped) toward critical threshold
total_scripts = execution_results["total_scripts_found"]
failed_scripts = execution_results["failed_executions"]
skipped_scripts = execution_results.get("skipped_executions", 0)
attempted_scripts = total_scripts - skipped_scripts
render_failures = execution_results.get("render_failures", [])
missing_render_scripts = execution_results.get("missing_render_scripts", [])
if strict_requested_frameworks and render_failures:
failure_preview = "; ".join(
f"{item['file']}:{item['framework']}" for item in render_failures[:5]
)
log_step_error(
logger,
f"Execute blocked by requested-framework render failures: {failure_preview}",
)
return False
if strict_requested_frameworks and missing_render_scripts:
log_step_error(
logger,
"Execute blocked because requested rendered scripts were not discoverable",
)
return False
if total_scripts == 0:
log_step_warning(logger, "No executable scripts found to run")
if strict_requested_frameworks:
return False
# Exit-code 2: step completed without doing work. Distinguishes
# "nothing to do" from "did work successfully".
return 2
elif strict_requested_frameworks and (
failed_scripts > 0 or skipped_scripts > 0
):
log_step_error(
logger,
f"Execute failed for requested frameworks: {successful} succeeded, "
f"{failed_scripts} failed, {skipped_scripts} skipped",
)
return False
elif failed_scripts == 0:
if skipped_scripts:
log_step_success(
logger,
f"Execute completed: {execution_results['successful_executions']} succeeded, {skipped_scripts} skipped (dependency not installed)",
)
else:
log_step_success(logger, "Execute processing completed successfully")
elif attempted_scripts > 0 and failed_scripts < attempted_scripts * 0.5:
log_step_warning(
logger,
f"Execute completed with {failed_scripts}/{attempted_scripts} failures (partial success)"
+ (f", {skipped_scripts} skipped" if skipped_scripts else ""),
)
elif attempted_scripts > 0:
log_step_error(
logger,
f"Execute completed with {failed_scripts}/{attempted_scripts} failures (critical)"
+ (f", {skipped_scripts} skipped" if skipped_scripts else ""),
)
return False
return failed_scripts == 0
except Exception as e:
log_step_error(logger, f"Execute processing failed: {e}")
return False
def find_executable_scripts(
render_output_dir: Path, verbose: bool, logger, requested_frameworks: List[str]
) -> List[Dict[str, Any]]:
"""
Find executable scripts in the render output directory.
Searches for Python (.py) and Julia (.jl) scripts in the render output
directory structure. Scripts are filtered by the requested frameworks
and excluded if they match common non-executable patterns (test files,
__init__.py, etc.).
Args:
render_output_dir: Directory containing rendered scripts from Step 11.
verbose: Enable verbose logging of discovered scripts.
logger: Logger instance for output messages.
requested_frameworks: List of framework names to include (e.g.,
["pymdp", "jax", "discopy"]). Scripts from other frameworks
will be skipped.
Returns:
List of dictionaries, each containing:
- path: Path to the script file
- name: Script filename
- framework: Detected framework name
- executor: Command to execute the script (python/julia)
- relative_path: Path relative to render_output_dir
- size_bytes: File size in bytes
"""
executable_scripts = []
# Define supported script types and their executors
script_types = {
"*.py": {"executor": sys.executable, "framework": "python"},
"*.jl": {"executor": "julia", "framework": "julia"},
}
# Map framework directories to framework names
framework_dirs = {
"pymdp": "pymdp",
"jax": "jax",
"discopy": "discopy",
"rxinfer": "rxinfer",
"activeinference_jl": "activeinference_jl",
"activeinference.jl": "activeinference_jl",
"pytorch": "pytorch",
"numpyro": "numpyro",
"bnlearn": "bnlearn",
}
for pattern, config in script_types.items():
scripts = list(render_output_dir.rglob(pattern))
for script_path in scripts:
# Skip test files and other non-executable scripts
if any(
skip in script_path.name.lower() for skip in ["test_", "__", "readme"]
):
continue
# Determine framework from directory path
framework = determine_script_framework(
script_path, render_output_dir, framework_dirs
)
# Filter by requested frameworks
if framework not in requested_frameworks:
if verbose:
logger.debug(
f"Skipping {framework} script: {script_path.name} (not in requested frameworks)"
)
continue
# Check if script is executable or can be made executable
script_info = {
"path": script_path,
"name": script_path.name,
"framework": framework,
"executor": config["executor"],
"relative_path": script_path.relative_to(render_output_dir),
"size_bytes": script_path.stat().st_size if script_path.exists() else 0,
}
executable_scripts.append(script_info)
if verbose:
logger.info(
f"Found {config['framework']} script: {script_info['relative_path']}"
)
return executable_scripts
def _aggregate_benchmark_samples(samples: List[float]) -> Dict[str, Any]:
"""Aggregate repeated execution durations (median + population std)."""
import statistics
if not samples:
return {}
med = float(statistics.median(samples))
mean = float(statistics.mean(samples))
std = float(statistics.pstdev(samples)) if len(samples) > 1 else 0.0
return {
"execution_time": med,
"execution_time_mean": mean,
"execution_time_std": std,
"execution_time_samples": list(samples),
}
def execute_single_script(
script_info: Dict[str, Any],
results_dir: Path,
verbose: bool,
logger,
timeout: int = 3600,
*,
execution_benchmark_repeats: int = 1,
) -> Dict[str, Any]:
"""
Execute a single script using subprocess.
Args:
script_info: Dictionary containing script information
results_dir: Directory to save execution results (will create implementation-specific subfolders)
verbose: Enable verbose logging
logger: Logger instance
Returns:
Dictionary with execution results
"""
script_path = script_info["path"]
executor = script_info["executor"]
# Extract model name and framework from script path for organization
# Expected path: .../11_render_output/model_name/framework/script.ext
path_parts = script_path.parts
if len(path_parts) >= 3:
model_name = path_parts[-3] # e.g., 'actinf_pomdp_agent'
framework = path_parts[-2] # e.g., 'pymdp'
else:
model_name = "unknown_model"
framework = script_info["framework"]
# Pre-flight skip: do not run Python frameworks when optional dependency is missing
if executor == sys.executable and not _is_python_framework_dependency_available(
framework, executor, logger
):
return _make_skipped_result(
script_info, framework, model_name, executor, logger
)
# Prepare execution result
exec_result = {
"script_path": str(script_path),
"script_name": script_info["name"],
"framework": framework,
"model_name": model_name,
"executor": executor,
"success": False,
"return_code": None,
"stdout": "",
"stderr": "",
"execution_time": 0,
"timestamp": datetime.now().isoformat(),
}
try:
if verbose:
logger.info(
f"Executing {script_info['framework']} script: {script_info['name']}"
)
# Check if the executor is available
try:
# For Python scripts, check if Python is available (most are Python scripts)
if executor in ["python", "python3"]:
subprocess.run(
[executor, "--version"], # nosec B603 -- subprocess calls with controlled/trusted input
capture_output=True,
text=True,
timeout=5,
check=True,
)
# For PyMDP, specifically check if it's importable
if framework == "pymdp":
try:
import_check = subprocess.run( # nosec B603 -- subprocess calls with controlled/trusted input
[executor, "-c", 'import pymdp; print("ok")'],
capture_output=True,
text=True,
timeout=5,
)
if import_check.returncode != 0:
logger.warning(
f"PyMDP package appears missing or broken: {import_check.stderr}"
)
exec_result["error"] = (
f"PyMDP dependency missing: {import_check.stderr}"
)
# Continue anyway as it might be a local import, but log warning
except Exception as e:
logger.debug(f"Error checking PyMDP importability: {e}")
# For Julia scripts, check availability and dependencies
elif executor == "julia":
if not check_julia_dependencies(verbose, logger):
logger.warning(