clm-kernel 0.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (333) hide show
  1. clm/__init__.py +230 -0
  2. clm/__main__.py +25 -0
  3. clm/cross_cutting/__init__.py +50 -0
  4. clm/cross_cutting/_test_runner.py +36 -0
  5. clm/cross_cutting/async_utils.py +25 -0
  6. clm/cross_cutting/cel_builder.py +123 -0
  7. clm/cross_cutting/cel_converter.py +697 -0
  8. clm/cross_cutting/cel_engine.py +252 -0
  9. clm/cross_cutting/cel_ops.py +72 -0
  10. clm/cross_cutting/cel_ops_db.py +157 -0
  11. clm/cross_cutting/cel_ops_files.py +99 -0
  12. clm/cross_cutting/cel_ops_state.py +71 -0
  13. clm/cross_cutting/cel_ops_strings.py +64 -0
  14. clm/cross_cutting/cel_resolver.py +63 -0
  15. clm/cross_cutting/cel_slices.py +56 -0
  16. clm/cross_cutting/cel_store.py +330 -0
  17. clm/cross_cutting/clm_logger.py +140 -0
  18. clm/cross_cutting/config/__init__.py +155 -0
  19. clm/cross_cutting/config/config_constants.py +392 -0
  20. clm/cross_cutting/config/env_parameters.py +90 -0
  21. clm/cross_cutting/config/logging.py +378 -0
  22. clm/cross_cutting/config/settings.py +504 -0
  23. clm/cross_cutting/context.py +5 -0
  24. clm/cross_cutting/domain_models.py +55 -0
  25. clm/cross_cutting/domain_types.py +61 -0
  26. clm/cross_cutting/effects.py +67 -0
  27. clm/cross_cutting/errors.py +153 -0
  28. clm/cross_cutting/governance.py +331 -0
  29. clm/cross_cutting/guards.py +41 -0
  30. clm/cross_cutting/mcard.py +321 -0
  31. clm/cross_cutting/native.py +185 -0
  32. clm/cross_cutting/observability.py +289 -0
  33. clm/cross_cutting/pcard.py +434 -0
  34. clm/cross_cutting/registry.py +71 -0
  35. clm/cross_cutting/resources.py +84 -0
  36. clm/cross_cutting/secrets.py +44 -0
  37. clm/cross_cutting/subprocess_utils.py +118 -0
  38. clm/cross_cutting/telemetry.py +164 -0
  39. clm/cross_cutting/testing/__init__.py +8 -0
  40. clm/cross_cutting/testing/comparator.py +187 -0
  41. clm/cross_cutting/testing/datasets.py +87 -0
  42. clm/cross_cutting/timeutil.py +20 -0
  43. clm/cross_cutting/types/new_type_check.py +0 -0
  44. clm/cross_cutting/utils/__init__.py +1 -0
  45. clm/cross_cutting/utils/url_safety.py +50 -0
  46. clm/cross_cutting/vcard.py +560 -0
  47. clm/cross_cutting/writer.py +118 -0
  48. clm/layer0/README.md +50 -0
  49. clm/layer0/__init__.py +223 -0
  50. clm/layer0/algebra.py +60 -0
  51. clm/layer0/algebra_baldwin.py +133 -0
  52. clm/layer0/algebra_baldwin_generators.py +36 -0
  53. clm/layer0/algebra_equivalence.py +262 -0
  54. clm/layer0/algebra_ingest.py +306 -0
  55. clm/layer0/algebra_manifest_io.py +156 -0
  56. clm/layer0/assembler.py +481 -0
  57. clm/layer0/cel/__init__.py +23 -0
  58. clm/layer0/cel_eval.py +491 -0
  59. clm/layer0/codec.py +106 -0
  60. clm/layer0/context.py +122 -0
  61. clm/layer0/db.py +160 -0
  62. clm/layer0/exceptions.py +73 -0
  63. clm/layer0/fibration.py +106 -0
  64. clm/layer0/gates.py +50 -0
  65. clm/layer0/hash.py +80 -0
  66. clm/layer0/loader.py +826 -0
  67. clm/layer0/math_utils.py +163 -0
  68. clm/layer0/mcard.py +96 -0
  69. clm/layer0/model/__init__.py +0 -0
  70. clm/layer0/model/action.py +223 -0
  71. clm/layer0/model/card.py +280 -0
  72. clm/layer0/model/card_triad.py +427 -0
  73. clm/layer0/model/compatibility.py +60 -0
  74. clm/layer0/model/dictionary.py +46 -0
  75. clm/layer0/model/dots.py +331 -0
  76. clm/layer0/model/event_producer.py +88 -0
  77. clm/layer0/model/g_time.py +89 -0
  78. clm/layer0/model/handle.py +163 -0
  79. clm/layer0/model/hash/__init__.py +6 -0
  80. clm/layer0/model/hash/algorithms/__init__.py +3 -0
  81. clm/layer0/model/hash/algorithms/custom_hash.py +7 -0
  82. clm/layer0/model/hash/algorithms/local_sha256.py +178 -0
  83. clm/layer0/model/hash/constants.py +53 -0
  84. clm/layer0/model/hash/enums.py +60 -0
  85. clm/layer0/model/hash/validator.py +307 -0
  86. clm/layer0/model/interpreter.py +275 -0
  87. clm/layer0/model/pagination.py +45 -0
  88. clm/layer0/model/pcard.py +716 -0
  89. clm/layer0/model/schema.py +44 -0
  90. clm/layer0/model/utils/__init__.py +1 -0
  91. clm/layer0/model/utils/content_analyzer.py +152 -0
  92. clm/layer0/model/validators/__init__.py +1 -0
  93. clm/layer0/model/validators/base_validator.py +44 -0
  94. clm/layer0/model/validators/binary_validator.py +66 -0
  95. clm/layer0/model/validators/text_validator.py +82 -0
  96. clm/layer0/model/validators/validation_registry.py +49 -0
  97. clm/layer0/model/vcard.py +823 -0
  98. clm/layer0/model/vcard_ext/__init__.py +39 -0
  99. clm/layer0/model/vcard_ext/core.py +60 -0
  100. clm/layer0/model/vcard_ext/network.py +23 -0
  101. clm/layer0/model/vcard_ext/observability.py +63 -0
  102. clm/layer0/model/vcard_ext/storage.py +60 -0
  103. clm/layer0/model/vcard_ext/vendors.py +211 -0
  104. clm/layer0/model/vcard_sandwich.py +119 -0
  105. clm/layer0/model/vcard_vocabulary.py +463 -0
  106. clm/layer0/model/workflow.py +274 -0
  107. clm/layer0/narrative.py +431 -0
  108. clm/layer0/ontology/collection.py +125 -0
  109. clm/layer0/parser.py +429 -0
  110. clm/layer0/relations.py +73 -0
  111. clm/layer0/rule_evaluator.py +251 -0
  112. clm/layer0/schema/__init__.py +38 -0
  113. clm/layer0/schema/ddl.py +97 -0
  114. clm/layer0/templates/abstract.yaml +99 -0
  115. clm/layer0/templates/balanced.yaml +253 -0
  116. clm/layer0/templates/concrete.yaml +172 -0
  117. clm/layer0/type_registry.py +223 -0
  118. clm/layer0/types.py +204 -0
  119. clm/layer0/utils.py +886 -0
  120. clm/layer0/verifier_core.py +262 -0
  121. clm/layer1/__init__.py +31 -0
  122. clm/layer1/action_dispatcher.py +275 -0
  123. clm/layer1/adapter_registry.py +233 -0
  124. clm/layer1/algebra_baldwin_generators.py +192 -0
  125. clm/layer1/arrow_compiler.py +525 -0
  126. clm/layer1/batch_processor.py +152 -0
  127. clm/layer1/cel/__init__.py +27 -0
  128. clm/layer1/cel/builder.py +173 -0
  129. clm/layer1/engine.py +759 -0
  130. clm/layer1/engine_async.py +89 -0
  131. clm/layer1/eval_callback.py +49 -0
  132. clm/layer1/gate_composer.py +229 -0
  133. clm/layer1/hoare_sandwich.py +298 -0
  134. clm/layer1/identity_guard.py +348 -0
  135. clm/layer1/matrix.py +138 -0
  136. clm/layer1/neural_runtime.py +221 -0
  137. clm/layer1/operations/__init__.py +123 -0
  138. clm/layer1/operations/augment.py +156 -0
  139. clm/layer1/operations/base.py +109 -0
  140. clm/layer1/operations/builtins.py +338 -0
  141. clm/layer1/operations/dispatch.py +144 -0
  142. clm/layer1/operations/exclude.py +107 -0
  143. clm/layer1/operations/handle.py +197 -0
  144. clm/layer1/operations/identity.py +139 -0
  145. clm/layer1/operations/invert.py +66 -0
  146. clm/layer1/operations/loader.py +116 -0
  147. clm/layer1/operations/manipulators/__init__.py +23 -0
  148. clm/layer1/operations/manipulators/document.py +211 -0
  149. clm/layer1/operations/manipulators/media.py +134 -0
  150. clm/layer1/operations/manipulators/structured.py +159 -0
  151. clm/layer1/operations/manipulators/tabular.py +334 -0
  152. clm/layer1/operations/port.py +65 -0
  153. clm/layer1/operations/services.py +529 -0
  154. clm/layer1/operations/split.py +117 -0
  155. clm/layer1/operations/substitute.py +75 -0
  156. clm/layer1/petri.py +1052 -0
  157. clm/layer1/pocketflow_orchestrator.py +408 -0
  158. clm/layer1/pocketflow_scheduler.py +519 -0
  159. clm/layer1/protocols.py +197 -0
  160. clm/layer1/runtime_adapter.py +718 -0
  161. clm/layer1/runtimes/__init__.py +71 -0
  162. clm/layer1/runtimes/_generated_assertions.py +125 -0
  163. clm/layer1/runtimes/_generated_types.py +42 -0
  164. clm/layer1/runtimes/base.py +265 -0
  165. clm/layer1/runtimes/binary.py +101 -0
  166. clm/layer1/runtimes/factory.py +203 -0
  167. clm/layer1/runtimes/javascript.py +321 -0
  168. clm/layer1/runtimes/javascript_runtime.js +33 -0
  169. clm/layer1/runtimes/js_scripts/isomorphism.mjs +22 -0
  170. clm/layer1/runtimes/js_scripts/loader.mjs +52 -0
  171. clm/layer1/runtimes/lambda_calc.py +126 -0
  172. clm/layer1/runtimes/python.py +288 -0
  173. clm/layer1/runtimes/python_runtime.py +41 -0
  174. clm/layer1/runtimes/script.py +114 -0
  175. clm/layer1/sandbox.py +334 -0
  176. clm/layer1/savepoint_guard.py +136 -0
  177. clm/layer1/sparse_net.py +122 -0
  178. clm/layer1/spec_tester.py +165 -0
  179. clm/layer1/store.py +295 -0
  180. clm/layer1/test_runner.py +498 -0
  181. clm/layer1/type_inspector.py +166 -0
  182. clm/layer1/verifier.py +772 -0
  183. clm/layer1/vm.py +91 -0
  184. clm/layer2/__init__.py +78 -0
  185. clm/layer2/card_collection.py +678 -0
  186. clm/layer2/classifier.py +479 -0
  187. clm/layer2/engine/__init__.py +1 -0
  188. clm/layer2/engine/abstract_sql_engine.py +320 -0
  189. clm/layer2/engine/base.py +193 -0
  190. clm/layer2/engine/duckdb_engine.py +576 -0
  191. clm/layer2/engine/sqlite_engine.py +518 -0
  192. clm/layer2/fiber/__init__.py +31 -0
  193. clm/layer2/fiber/context.py +89 -0
  194. clm/layer2/fiber/disposable.py +163 -0
  195. clm/layer2/fiber/fiber.py +125 -0
  196. clm/layer2/fiber/hoare.py +191 -0
  197. clm/layer2/file_io.py +376 -0
  198. clm/layer2/file_registrar.py +77 -0
  199. clm/layer2/ingest.py +202 -0
  200. clm/layer2/lineage.py +322 -0
  201. clm/layer2/mcard_fs.py +262 -0
  202. clm/layer2/merkle.py +11 -0
  203. clm/layer2/mime.py +244 -0
  204. clm/layer2/mime_detector.py +245 -0
  205. clm/layer2/private_collection.py +118 -0
  206. clm/layer2/shared_store.py +11 -0
  207. clm/layer2/storage/__init__.py +190 -0
  208. clm/layer2/storage/db_bitemporal.py +141 -0
  209. clm/layer2/storage/db_driver.py +331 -0
  210. clm/layer2/storage/db_hyperlinks.py +214 -0
  211. clm/layer2/storage/db_pool.py +139 -0
  212. clm/layer2/storage/db_repository.py +229 -0
  213. clm/layer2/storage/savepoint.py +78 -0
  214. clm/layer2/storage/sqlite_wal.py +74 -0
  215. clm/layer2/tridb.py +245 -0
  216. clm/layer2/vcard_receipt.py +71 -0
  217. clm/layer2/vfs.py +17 -0
  218. clm/layer3/__init__.py +141 -0
  219. clm/layer3/agency/__init__.py +19 -0
  220. clm/layer3/agency/config.py +220 -0
  221. clm/layer3/agency/providers/__init__.py +15 -0
  222. clm/layer3/agency/providers/base.py +91 -0
  223. clm/layer3/agency/providers/mlc_llm.py +169 -0
  224. clm/layer3/agency/providers/ollama.py +203 -0
  225. clm/layer3/agency/router.py +71 -0
  226. clm/layer3/agency/runtime.py +524 -0
  227. clm/layer3/gateway.py +353 -0
  228. clm/layer3/open_interpreter_adapter.py +159 -0
  229. clm/layer3/rag/__init__.py +70 -0
  230. clm/layer3/rag/cli.py +228 -0
  231. clm/layer3/rag/config.py +153 -0
  232. clm/layer3/rag/embeddings/__init__.py +16 -0
  233. clm/layer3/rag/embeddings/base.py +79 -0
  234. clm/layer3/rag/embeddings/ollama.py +211 -0
  235. clm/layer3/rag/embeddings/vision.py +290 -0
  236. clm/layer3/rag/engine.py +276 -0
  237. clm/layer3/rag/graph/__init__.py +22 -0
  238. clm/layer3/rag/graph/community.py +198 -0
  239. clm/layer3/rag/graph/engine.py +433 -0
  240. clm/layer3/rag/graph/extractor.py +255 -0
  241. clm/layer3/rag/graph/schema.py +115 -0
  242. clm/layer3/rag/graph/store.py +641 -0
  243. clm/layer3/rag/indexer.py +333 -0
  244. clm/layer3/rag/llm_providers.json +52 -0
  245. clm/layer3/rag/semantic_versioning.py +313 -0
  246. clm/layer3/rag/vector/__init__.py +28 -0
  247. clm/layer3/rag/vector/handle_vector_store.py +749 -0
  248. clm/layer3/rag/vector/schema.py +151 -0
  249. clm/layer3/rag/vector/store.py +632 -0
  250. clm/layer3/reticulum/__init__.py +55 -0
  251. clm/layer3/reticulum/bridge.py +74 -0
  252. clm/layer3/reticulum/daemon.py +88 -0
  253. clm/layer3/reticulum/discovery.py +164 -0
  254. clm/layer3/reticulum/identity.py +107 -0
  255. clm/layer3/reticulum/media.py +148 -0
  256. clm/layer3/reticulum/transport.py +133 -0
  257. clm/layer3/satori/__init__.py +209 -0
  258. clm/layer3/satori/alpha_conversion.py +168 -0
  259. clm/layer3/satori/beta_reduction.py +359 -0
  260. clm/layer3/satori/continuation.py +38 -0
  261. clm/layer3/satori/eta_conversion.py +205 -0
  262. clm/layer3/satori/free_variables.py +163 -0
  263. clm/layer3/satori/io_effects.py +370 -0
  264. clm/layer3/satori/lambda_runtime.py +726 -0
  265. clm/layer3/satori/lambda_term.py +230 -0
  266. clm/layer3/satori/sheaf_audit.py +261 -0
  267. clm/layer3/satori/speech_act.py +279 -0
  268. clm/layer4/README.md +9 -0
  269. clm/layer4/__init__.py +67 -0
  270. clm/layer4/_sovereign_executor.py +580 -0
  271. clm/layer4/action_commands.py +719 -0
  272. clm/layer4/api.py +236 -0
  273. clm/layer4/bootstrap/__init__.py +13 -0
  274. clm/layer4/bootstrap/bootstrapper.py +118 -0
  275. clm/layer4/bootstrap/cleanup.py +44 -0
  276. clm/layer4/bootstrap/clm_hooks.py +49 -0
  277. clm/layer4/bootstrap/genesis.py +77 -0
  278. clm/layer4/bootstrap/injection.py +125 -0
  279. clm/layer4/cli.py +549 -0
  280. clm/layer4/collection_manager.py +553 -0
  281. clm/layer4/commands/__init__.py +81 -0
  282. clm/layer4/commands/action_cmd.py +42 -0
  283. clm/layer4/commands/check_cmd.py +82 -0
  284. clm/layer4/commands/envelope.py +73 -0
  285. clm/layer4/commands/evaluate_cmd.py +224 -0
  286. clm/layer4/commands/mcard_cmd.py +24 -0
  287. clm/layer4/commands/petri_cmd.py +92 -0
  288. clm/layer4/commands/tridb_cmd.py +319 -0
  289. clm/layer4/commands/version_cmd.py +27 -0
  290. clm/layer4/diff_commands.py +167 -0
  291. clm/layer4/eoa_sponsor.py +232 -0
  292. clm/layer4/events.py +69 -0
  293. clm/layer4/flux_gateway.py +133 -0
  294. clm/layer4/gateway.py +91 -0
  295. clm/layer4/loader.py +307 -0
  296. clm/layer4/mcard_commands.py +166 -0
  297. clm/layer4/membrane.py +273 -0
  298. clm/layer4/navigation.py +50 -0
  299. clm/layer4/protocol/protocol_entry_python.py +108 -0
  300. clm/layer4/protocol/python_sync.py +126 -0
  301. clm/layer4/runner.py +150 -0
  302. clm/layer4/satori.py +262 -0
  303. clm/layer4/server/python_sync_client.py +150 -0
  304. clm/layer4/server/python_sync_server.py +139 -0
  305. clm/layer4/server/service_api.py +315 -0
  306. clm/layer4/server/start_servers.py +107 -0
  307. clm/layer4/server/stop_servers.py +24 -0
  308. clm/layer4/simulated_optimization_fix.py +0 -0
  309. clm/layer4/storage.py +463 -0
  310. clm/layer4/tridb_admin.py +268 -0
  311. clm/layer4/websocket/test_ws_client.py +104 -0
  312. clm/layer4/websocket/test_ws_server.py +138 -0
  313. clm/layer4/websocket/ws_server_local.py +160 -0
  314. clm/layer5/__init__.py +51 -0
  315. clm/layer5/async_batch_writer.py +133 -0
  316. clm/layer5/learning.py +276 -0
  317. clm/layer5/telemetry_hook.py +107 -0
  318. clm/layer5/type_lattice.py +238 -0
  319. clm/py.typed +0 -0
  320. clm/schemas/__init__.py +652 -0
  321. clm/schemas/abstract_face_schema.yaml +77 -0
  322. clm/schemas/cel_schema.yaml +416 -0
  323. clm/schemas/lifecycle/__init__.py +45 -0
  324. clm/schemas/lifecycle/models.py +123 -0
  325. clm/schemas/lifecycle/service.py +364 -0
  326. clm/schemas/mcard_schema.sql +192 -0
  327. clm/schemas/mcard_vector_schema.sql +283 -0
  328. clm/schemas/mlp-context-v1.json +91 -0
  329. clm_kernel-0.0.0.dist-info/METADATA +121 -0
  330. clm_kernel-0.0.0.dist-info/RECORD +333 -0
  331. clm_kernel-0.0.0.dist-info/WHEEL +4 -0
  332. clm_kernel-0.0.0.dist-info/entry_points.txt +3 -0
  333. clm_kernel-0.0.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,580 @@
1
+ import contextlib
2
+ import json
3
+ import os
4
+ import subprocess
5
+ import sys
6
+
7
+ if sys.platform == "win32":
8
+ git_bin = r"C:\Program Files\Git\bin"
9
+ if os.path.exists(git_bin) and git_bin not in os.environ.get("PATH", ""):
10
+ os.environ["PATH"] = git_bin + os.pathsep + os.environ.get("PATH", "")
11
+
12
+ from clm.cross_cutting.clm_logger import get_logger
13
+ from clm.cross_cutting.pcard import run_balanced_expectations
14
+ from clm.layer0.utils import clm_hash_file, get_bash_command, resolve_clm_path
15
+ from clm.layer1.gate_composer import compose_conditions_cmd
16
+ from clm.layer1.runtime_adapter import evaluate_compose, evaluate_polyglot, run_internal_reference_test
17
+ from clm.layer2.classifier import classify_clm, detect_dialect
18
+ from clm.layer2.file_registrar import register_file_mcard_py
19
+ from clm.layer2.storage import (
20
+ resolve_handle_content,
21
+ )
22
+
23
+ _logger = get_logger("clm.layer4.cli")
24
+
25
+ # ---------------------------------------------------------------------------
26
+ # Runtime dispatch helpers — extracted from evaluate_cmd for modularity
27
+ # ---------------------------------------------------------------------------
28
+
29
+ def _validate_signatures(clm_root, clm_file):
30
+ """Validates strict cryptographic signatures via dependency graph. Returns 0 on success, 1 on failure."""
31
+ bash_cmd = f"""
32
+ export CLM_ROOT="{clm_root}"
33
+ export PATH="$CLM_ROOT/kernel/scripts:$PATH"
34
+ clm_root() {{ echo "$CLM_ROOT"; }}
35
+ export -f clm_root
36
+ while IFS= read -r f; do
37
+ if [[ "$f" != *"clmEval.sh"* && "$f" != *"main.sh"* && "$f" != *"evaluator.sh"* ]]; then
38
+ source "$f" 2>/dev/null || true
39
+ fi
40
+ done < <(find "$CLM_ROOT/computable_knowledge/Concrete/layer5_system/bootstrap" -name "node_modules" -prune -o -mindepth 2 -name "*.sh" -print)
41
+ source "$CLM_ROOT/computable_knowledge/Concrete/layer5_system/bootstrap/layer1_cpn_vm/clm_dependency_graph.sh"
42
+ build_dependency_graph "$1"
43
+ """
44
+ proc = subprocess.Popen([get_bash_command(), "-c", bash_cmd, "--", clm_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding="utf-8")
45
+ stdout_data, stderr_data = proc.communicate()
46
+ if proc.returncode != 0:
47
+ _logger.error("CRITICAL: Sovereign cryptographic verification failed: build_dependency_graph execution failed.")
48
+ _logger.info(f"Subprocess stderr: {stderr_data}")
49
+ _logger.info("Strict signatures mode active. Aborting.")
50
+ return 1
51
+
52
+ lines = stdout_data.strip().splitlines()
53
+ untrusted_nodes = []
54
+ for line in lines:
55
+ if not line.strip():
56
+ continue
57
+ try:
58
+ node_data = json.loads(line)
59
+ if node_data.get("trusted") is False:
60
+ untrusted_nodes.append(node_data)
61
+ except Exception:
62
+ pass
63
+ if untrusted_nodes:
64
+ trust_reason = untrusted_nodes[0].get("trust_reason") or "Unknown trust failure"
65
+ _logger.error(f"CRITICAL: Sovereign cryptographic verification failed: {trust_reason}")
66
+ _logger.info("Strict signatures mode active. Aborting.")
67
+ return 1
68
+ return 0
69
+
70
+
71
+ def _run_python_block(clm_root, clm_file, block, concrete, extra_args):
72
+ """Executes a Python process block. Returns exit code."""
73
+ _logger.debug(f"[DEBUG_PYTHON_RUN] Executing python block: {block}")
74
+ script = block.get("script", concrete.get("script", concrete.get("code", "")))
75
+ script_handle = block.get("script_handle", concrete.get("script_handle", ""))
76
+ body = ""
77
+ if script_handle:
78
+ body = resolve_handle_content(clm_root, script_handle)
79
+ if not body and script:
80
+ body = script
81
+
82
+ if not body:
83
+ _logger.error(f"[RUNNER] ❌ Empty Python script block for {clm_file}")
84
+ return 1
85
+
86
+ nested_val = os.environ.pop("CLM_NESTED_EXEC", None)
87
+ nested_file_val = os.environ.pop("CLM_NESTED_FILE", None)
88
+ try:
89
+ env = globals().copy()
90
+ env.update({
91
+ "clm_root": clm_root,
92
+ "clm_file": clm_file,
93
+ "extra_args": extra_args,
94
+ "sys": sys,
95
+ "os": os,
96
+ "__name__": "__main__"
97
+ })
98
+ exec(body, env)
99
+ return 0
100
+ except SystemExit as e:
101
+ return e.code if isinstance(e.code, int) else 0
102
+ except Exception as e:
103
+ _logger.error(f"[RUNNER] ❌ Python execution error: {e}")
104
+ _logger.error("Exception in CLI", exc_info=True)
105
+ return 1
106
+ finally:
107
+ if nested_val is not None:
108
+ os.environ["CLM_NESTED_EXEC"] = nested_val
109
+ if nested_file_val is not None:
110
+ os.environ["CLM_NESTED_FILE"] = nested_file_val
111
+
112
+
113
+ def _run_node_library(library, block, concrete, env, extra_args):
114
+ """Executes a declarative Node.js library call. Returns exit code.
115
+
116
+ Story 139.1.5: All Node.js library execution routes through Rust core dispatch-runtime.
117
+ """
118
+ lib_name = library.get("name")
119
+ lib_entry = library.get("entry", "")
120
+ lib_signature = library.get("signature", "")
121
+
122
+ arguments = block.get("arguments", concrete.get("arguments", []))
123
+ args_json = json.dumps(arguments)
124
+
125
+ rust_bin = _resolve_rust_binary(os.environ.get("CLM_ROOT", ""))
126
+ if not rust_bin:
127
+ _logger.error("[PAL:node] ❌ clm_rust_core binary not found for library dispatch")
128
+ return 1
129
+ script = f"library:{lib_name}:{lib_entry}:{lib_signature}:{args_json}"
130
+ cmd = [rust_bin, "dispatch-runtime", "--runtime", "node", "--script", script]
131
+ proc = subprocess.Popen(cmd, env=env)
132
+ proc.wait()
133
+ return proc.returncode
134
+
135
+
136
+ def _resolve_rust_binary(clm_root):
137
+ """Find the clm_rust_core binary if it exists. Returns path or None.
138
+
139
+ Also searches the real CLM root derived from this script's location, so
140
+ mock-root tests can dispatch to the built binary even when CLM_ROOT
141
+ points to a temporary fixture.
142
+ """
143
+ import platform as _platform
144
+
145
+ script_dir = os.path.dirname(os.path.abspath(__file__))
146
+ # cli.py is at kernel/scripts/clm/layer3/cli.py → CLM root is 4 levels up
147
+ script_clm_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(script_dir))))
148
+
149
+ roots = [clm_root]
150
+ if os.path.normpath(script_clm_root) != os.path.normpath(clm_root):
151
+ roots.append(script_clm_root)
152
+
153
+ candidates = []
154
+ for root in roots:
155
+ candidates.extend([
156
+ os.path.join(root, "kernel/clm_rust_core/target/release/clm_rust_core"),
157
+ os.path.join(root, "kernel/clm_rust_core/target/debug/clm_rust_core"),
158
+ ])
159
+ if _platform.system() == "Darwin":
160
+ machine = _platform.machine()
161
+ if machine == "arm64":
162
+ candidates.insert(0, os.path.join(root, "kernel/clm_rust_core/target/aarch64-apple-darwin/release/clm_rust_core"))
163
+ candidates.insert(1, os.path.join(root, "kernel/clm_rust_core/target/aarch64-apple-darwin/debug/clm_rust_core"))
164
+ elif machine == "x86_64":
165
+ candidates.insert(0, os.path.join(root, "kernel/clm_rust_core/target/x86_64-apple-darwin/release/clm_rust_core"))
166
+ candidates.insert(1, os.path.join(root, "kernel/clm_rust_core/target/x86_64-apple-darwin/debug/clm_rust_core"))
167
+
168
+ if sys.platform == "win32":
169
+ candidates = [p + ".exe" for p in candidates]
170
+ valid = [p for p in candidates if os.path.isfile(p) and os.access(p, os.X_OK)]
171
+ if valid:
172
+ return max(valid, key=lambda p: os.path.getmtime(p))
173
+ return None
174
+
175
+
176
+ def _run_node_script(body, clm_root, env, extra_args):
177
+ """Executes an inline Node.js script via Rust core RuntimeDispatcher. Returns exit code.
178
+
179
+ Story 139.1.5: All Node.js execution routes through
180
+ `clm_rust_core dispatch-runtime --runtime node --script <body>`.
181
+ Falls back to `node -e <body>` when Rust binary is not compiled.
182
+ """
183
+ rust_bin = _resolve_rust_binary(clm_root)
184
+ if rust_bin:
185
+ cmd = [rust_bin, "dispatch-runtime", "--runtime", "node", "--script", body]
186
+ else:
187
+ cmd = ["node", "-e", body]
188
+ proc = subprocess.Popen(cmd, env=env)
189
+ proc.wait()
190
+ return proc.returncode
191
+
192
+
193
+ def _run_node_jscubed(clm_root, clm_file, concrete, env, payload):
194
+ """Executes via jsCubed dispatcher. Returns exit code.
195
+
196
+ Story 139.1.5: All jsCubed execution routes through Rust core dispatch-runtime.
197
+ Falls back to clm_eval_dispatcher.cjs when Rust binary is not compiled.
198
+ """
199
+ rust_bin = _resolve_rust_binary(clm_root)
200
+ if rust_bin:
201
+ entry_point = concrete.get("entry_point", concrete.get("operation", ""))
202
+ script = f"jsCubed:{clm_file}:{entry_point}:{payload}"
203
+ cmd = [rust_bin, "dispatch-runtime", "--runtime", "node", "--script", script]
204
+ else:
205
+ dispatcher = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/layer1_cpn_vm/clm_eval_dispatcher.cjs")
206
+ cmd = ["node", dispatcher, clm_file, payload]
207
+ proc = subprocess.Popen(cmd, env=env)
208
+ proc.wait()
209
+ return proc.returncode
210
+
211
+
212
+ def _run_node_block(clm_root, clm_file, block, concrete, extra_args):
213
+ """Executes a JavaScript/Node.js process block. Returns exit code."""
214
+ _logger.debug(f"[DEBUG_NODE_RUN] Executing node/javascript block: {block}")
215
+ library = block.get("library")
216
+ if not library and concrete:
217
+ library = concrete.get("library")
218
+ has_library = library is not None and isinstance(library, dict) and "name" in library
219
+
220
+ script = block.get("script", concrete.get("script", concrete.get("code", "")))
221
+ script_handle = block.get("script_handle", concrete.get("script_handle", ""))
222
+ body = ""
223
+ if script_handle:
224
+ body = resolve_handle_content(clm_root, script_handle)
225
+ if not body and script:
226
+ body = script
227
+ has_script = bool(body)
228
+
229
+ if has_library and has_script:
230
+ _logger.error("[PAL:node] ❌ Integrity Breach: manifest cannot define both inline/handle script AND a declarative library!")
231
+ return 1
232
+
233
+ env = os.environ.copy()
234
+ payload = " ".join(extra_args)
235
+ if payload:
236
+ env["CLM_PAYLOAD"] = payload
237
+ env["TARGET_FILE"] = payload
238
+ env["CLM_ROOT"] = clm_root
239
+
240
+ if has_library:
241
+ return _run_node_library(library, block, concrete, env, extra_args)
242
+ elif has_script:
243
+ return _run_node_script(body, clm_root, env, extra_args)
244
+ else:
245
+ return _run_node_jscubed(clm_root, clm_file, concrete, env, payload)
246
+
247
+
248
+ def _run_internal_reference(clm_root, clm_file, block, extra_args):
249
+ """Executes an internal reference test. Returns exit code."""
250
+
251
+ # In-process PyO3 evaluation fast-path (INV-418)
252
+ from clm.cross_cutting.native import evaluate_in_process
253
+ payload = extra_args[0] if extra_args else None
254
+ in_proc_res = evaluate_in_process(clm_root, clm_file, payload)
255
+ if in_proc_res is not None:
256
+ rc, out = in_proc_res
257
+ if out:
258
+ print(out, end="")
259
+ return rc
260
+
261
+ rust_core_release = os.path.join(clm_root, "kernel/clm_rust_core/target/release/clm_rust_core")
262
+ rust_core_debug = os.path.join(clm_root, "kernel/clm_rust_core/target/debug/clm_rust_core")
263
+ if sys.platform == "win32":
264
+ rust_core_release += ".exe"
265
+ rust_core_debug += ".exe"
266
+
267
+ has_rust_binary = os.path.exists(rust_core_release) or os.path.exists(rust_core_debug)
268
+
269
+ if os.environ.get("CLM_RUNTIME", "").lower() == "python" or not has_rust_binary:
270
+ module_name = block.get("module", "")
271
+ return run_internal_reference_test(clm_root, clm_file, module_name)
272
+ else:
273
+ rs_binary = os.path.join(clm_root, "kernel/scripts/clmEval-rs")
274
+ if not os.path.exists(rs_binary):
275
+ # Check release first, then debug (or pick newest if both exist)
276
+ release_bin = os.path.join(clm_root, "kernel/clm_rust_core/target/release/clm_rust_core")
277
+ debug_bin = os.path.join(clm_root, "kernel/clm_rust_core/target/debug/clm_rust_core")
278
+ if sys.platform == "win32":
279
+ release_bin += ".exe"
280
+ debug_bin += ".exe"
281
+
282
+ if os.path.exists(release_bin) and os.path.exists(debug_bin):
283
+ if os.path.getmtime(release_bin) >= os.path.getmtime(debug_bin):
284
+ rs_binary = release_bin
285
+ else:
286
+ rs_binary = debug_bin
287
+ elif os.path.exists(release_bin):
288
+ rs_binary = release_bin
289
+ elif os.path.exists(debug_bin):
290
+ rs_binary = debug_bin
291
+
292
+ if os.path.exists(rs_binary):
293
+ cmd = [rs_binary, "evaluate", clm_file] + extra_args
294
+ env = os.environ.copy()
295
+ env["CLM_NESTED_EXEC"] = "1"
296
+ env["CLM_NESTED_FILE"] = os.path.abspath(clm_file)
297
+ proc = subprocess.Popen(cmd, env=env)
298
+ proc.wait()
299
+ return proc.returncode
300
+ else:
301
+ _logger.error("[RUNNER] ❌ clmEval-rs binary not found!")
302
+ return 1
303
+
304
+
305
+ def _run_shell_block(clm_root, clm_file, block, concrete, extra_args):
306
+ """Executes a shell process block. Returns exit code.
307
+
308
+ Story 139.1.5: Routes through Rust core dispatch-runtime when binary is available.
309
+ Falls back to bash when Rust binary is not compiled.
310
+ """
311
+ script = block.get("script", concrete.get("script", concrete.get("code", "")))
312
+ script_handle = block.get("script_handle", concrete.get("script_handle", ""))
313
+ body = ""
314
+ if script_handle:
315
+ body = resolve_handle_content(clm_root, script_handle)
316
+ if not body and script:
317
+ body = script
318
+
319
+ if not body:
320
+ _logger.error(f"[RUNNER] ❌ Empty shell script block for {clm_file}")
321
+ return 1
322
+
323
+ operation = concrete.get("operation")
324
+ if operation and f"{operation}()" in body and not body.strip().endswith(f"{operation}") and not body.strip().endswith(f'{operation} "$@"'):
325
+ body += f'\n\n{operation} "$@"\n'
326
+
327
+ if extra_args:
328
+ import shlex
329
+ args_str = " ".join(shlex.quote(str(a)) for a in extra_args)
330
+ body = f"set -- {args_str}\n\n" + body
331
+
332
+ rust_bin = _resolve_rust_binary(clm_root)
333
+ env = os.environ.copy()
334
+ env["CLM_ROOT"] = clm_root
335
+ if rust_bin:
336
+ cmd = [rust_bin, "dispatch-runtime", "--runtime", "shell", "--script", body]
337
+ proc = subprocess.Popen(cmd, env=env)
338
+ proc.wait()
339
+ return proc.returncode
340
+ else:
341
+ bootstrap_sh = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/evaluator/shell_utils.sh").replace("\\", "/")
342
+ core_boot = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/clm_bootstrap.sh").replace("\\", "/")
343
+ mcard_store = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/mcard_storage.sh").replace("\\", "/")
344
+ mcard_verify = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/mcard_verification.sh").replace("\\", "/")
345
+ type_reg = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/type_registry.sh").replace("\\", "/")
346
+ grammar_ops = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/governance/grammar_ops.sh").replace("\\", "/")
347
+ define_grammar = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/governance/define_grammar.sh").replace("\\", "/")
348
+ yaml_assert = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/governance/yaml_assert_contains.sh").replace("\\", "/")
349
+ proc_disp = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/layer1_cpn_vm/process_dispatch.sh").replace("\\", "/")
350
+ rt_adapters = os.path.join(clm_root, "computable_knowledge/Concrete/layer5_system/bootstrap/layer1_cpn_vm/runtime_adapters.sh").replace("\\", "/")
351
+ init_script = (
352
+ f"source '{bootstrap_sh}' 2>/dev/null || true;\n"
353
+ f"source '{core_boot}' 2>/dev/null || true;\n"
354
+ f"source '{mcard_store}' 2>/dev/null || true;\n"
355
+ f"source '{mcard_verify}' 2>/dev/null || true;\n"
356
+ f"source '{type_reg}' 2>/dev/null || true;\n"
357
+ f"source '{grammar_ops}' 2>/dev/null || true;\n"
358
+ f"source '{define_grammar}' 2>/dev/null || true;\n"
359
+ f"source '{yaml_assert}' 2>/dev/null || true;\n"
360
+ f"source '{proc_disp}' 2>/dev/null || true;\n"
361
+ f"source '{rt_adapters}' 2>/dev/null || true;\n"
362
+ + body
363
+ )
364
+ proc = subprocess.Popen([get_bash_command(), "-s"], stdin=subprocess.PIPE, env=env)
365
+ proc.communicate(input=init_script.encode("utf-8"))
366
+ return proc.returncode
367
+
368
+
369
+ # ---------------------------------------------------------------------------
370
+ # Orchestrator — evaluate_cmd now delegates to helpers above
371
+ # ---------------------------------------------------------------------------
372
+
373
+ def evaluate_cmd(clm_root, clm_file, extra_args):
374
+ clm_file = resolve_clm_path(clm_root, clm_file)
375
+ if not os.path.exists(clm_file):
376
+ sys.exit(f"FATAL: CLM manifest missing at {clm_file}")
377
+
378
+ def print_clm_trace(transition_name):
379
+ if os.environ.get("CLM_NESTED_EXEC", "0") == "1":
380
+ return
381
+ if "CLM_DISPATCH_SOCK" not in os.environ:
382
+ return
383
+ trace_data = {
384
+ "type": "trace",
385
+ "transition": transition_name,
386
+ "agent": "did:pkc:ca:darwin:root-agent",
387
+ "agent_class": "root-agent",
388
+ "sponsor": "self",
389
+ "pcard": os.environ.get("CLM_LAST_MCARD_HASH", ""),
390
+ "cert": "",
391
+ "merkle": "",
392
+ "places": {},
393
+ "pinv_held": True
394
+ }
395
+ sys.stdout.write(f"CLM_TRACE_JSON:{json.dumps(trace_data)}\n")
396
+ sys.stdout.flush()
397
+
398
+ print_clm_trace("verify_precondition")
399
+
400
+ if os.environ.get("CLM_STRICT_SIGNATURES", "0") == "1":
401
+ rc = _validate_signatures(clm_root, clm_file)
402
+ if rc != 0:
403
+ return rc
404
+
405
+ # Lazy load algebra to prevent circular imports
406
+ from clm.layer0.algebra import load_normalized_manifest
407
+ try:
408
+ yaml_data = load_normalized_manifest(clm_root, clm_file)
409
+ except Exception as e:
410
+ sys.exit(f"FATAL: Failed to parse CLM YAML: {e}")
411
+
412
+ classification = classify_clm(yaml_data)
413
+ inner = yaml_data.get("clm", yaml_data)
414
+ has_process = False
415
+ if isinstance(inner, dict):
416
+ concrete = inner.get("concrete_impl", {})
417
+ if concrete and ("process" in concrete or "process_behavior_exec" in concrete or "runtime" in concrete):
418
+ has_process = True
419
+ elif "process" in inner or "runtime" in inner:
420
+ has_process = True
421
+
422
+ if has_process:
423
+ classification = "Function"
424
+
425
+ # Face B manifests (with pending_cases) are always "Function" — they have
426
+ # executable behavior even without a concrete_impl block.
427
+ if isinstance(inner, dict) and ("pending_cases" in inner or inner.get("face") == "B"):
428
+ classification = "Function"
429
+
430
+ if classification == "Number":
431
+ h = clm_hash_file(clm_file)
432
+ print(h)
433
+ return 0
434
+
435
+ dialect = detect_dialect(yaml_data, classification)
436
+
437
+ register_file_mcard_py(clm_root, clm_file)
438
+
439
+ skip_conditions = os.environ.get("CLM_SKIP_CONDITIONS", "0") == "1"
440
+ nested_exec = False
441
+ if os.environ.get("CLM_NESTED_EXEC", "0") == "1":
442
+ nested_file = os.environ.get("CLM_NESTED_FILE")
443
+ if nested_file and os.path.abspath(clm_file) == os.path.abspath(nested_file):
444
+ nested_exec = True
445
+
446
+ if not skip_conditions and not nested_exec:
447
+ rc = compose_conditions_cmd(clm_root, clm_file, "pre")
448
+ if rc != 0:
449
+ _logger.info("CRITICAL: Pre-Gate Failed.")
450
+ return rc
451
+
452
+ inner = yaml_data.get("clm", yaml_data)
453
+ concrete = inner.get("concrete_impl", {})
454
+
455
+ # --- Face B auto-dispatch: manifests with pending_cases run the
456
+ # Balanced Expectations runner automatically, no concrete_impl needed.
457
+ face = inner.get("face", "")
458
+ has_pending = "pending_cases" in inner
459
+ if face == "B" or has_pending:
460
+ eval_rc = run_balanced_expectations(clm_root, clm_file)
461
+ if not skip_conditions and not nested_exec:
462
+ print_clm_trace("validate_postcondition")
463
+ rc = compose_conditions_cmd(clm_root, clm_file, "post", eval_rc)
464
+ if rc != 0:
465
+ return rc
466
+ print_clm_trace("commit_witness")
467
+ return eval_rc
468
+
469
+ process = concrete.get("process", {})
470
+
471
+ process_blocks = []
472
+ if isinstance(process, dict):
473
+ process_blocks = [process]
474
+ elif isinstance(process, list):
475
+ process_blocks = process
476
+
477
+ print_clm_trace("execute_payload")
478
+ eval_rc = 0
479
+ for block in process_blocks:
480
+ p_type = block.get("type", concrete.get("runtime", "shell"))
481
+ if dialect.startswith("M:"):
482
+ p_type = dialect.split("M:")[1]
483
+
484
+ if p_type == "polyglot":
485
+ eval_rc = evaluate_polyglot(clm_root, clm_file, yaml_data, extra_args)
486
+ elif p_type == "compose":
487
+ eval_rc = evaluate_compose(clm_root, clm_file, block, extra_args)
488
+ elif p_type == "triad":
489
+ from clm.layer1.runtime_adapter import evaluate_triad
490
+ eval_rc = evaluate_triad(clm_root, clm_file, block, extra_args)
491
+ elif p_type == "triad_pipeline":
492
+ from clm.layer1.runtime_adapter import evaluate_triad_pipeline
493
+ eval_rc = evaluate_triad_pipeline(clm_root, clm_file, block, extra_args)
494
+ elif p_type == "open_interpreter":
495
+ from .open_interpreter_adapter import evaluate_open_interpreter
496
+ eval_rc = evaluate_open_interpreter(clm_root, clm_file, block, extra_args)
497
+ elif p_type == "python":
498
+ eval_rc = _run_python_block(clm_root, clm_file, block, concrete, extra_args)
499
+ elif p_type in ("javascript", "node"):
500
+ eval_rc = _run_node_block(clm_root, clm_file, block, concrete, extra_args)
501
+ elif p_type == "internal_reference":
502
+ eval_rc = _run_internal_reference(clm_root, clm_file, block, extra_args)
503
+ else:
504
+ eval_rc = _run_shell_block(clm_root, clm_file, block, concrete, extra_args)
505
+
506
+ if eval_rc != 0:
507
+ break
508
+
509
+ if not skip_conditions and not nested_exec:
510
+ print_clm_trace("validate_postcondition")
511
+ rc = compose_conditions_cmd(clm_root, clm_file, "post", eval_rc)
512
+ if rc != 0:
513
+ _logger.info("CRITICAL: Post-Gate Failed.")
514
+ return rc
515
+ print_clm_trace("commit_witness")
516
+
517
+ return eval_rc
518
+
519
+
520
+ # ---------------------------------------------------------------------------
521
+ # CLI entry point — main()
522
+ # ---------------------------------------------------------------------------
523
+
524
+ def main():
525
+ import sys
526
+ if sys.platform == "win32":
527
+ try:
528
+ sys.stdout.reconfigure(encoding="utf-8")
529
+ sys.stderr.reconfigure(encoding="utf-8")
530
+ except AttributeError:
531
+ pass
532
+
533
+ clm_root = os.environ.get("CLM_ROOT") or os.getcwd()
534
+ if clm_root:
535
+ # Normalize Git Bash / MSYS2 Unix-style paths on Windows
536
+ if sys.platform == "win32":
537
+ if clm_root.startswith('/') and len(clm_root) >= 3 and clm_root[2] == '/':
538
+ drive = clm_root[1].upper()
539
+ clm_root = drive + ":" + clm_root[2:]
540
+ elif clm_root.startswith('/') and len(clm_root) == 2:
541
+ drive = clm_root[1].upper()
542
+ clm_root = drive + ":\\"
543
+ clm_root = clm_root.replace('/', '\\')
544
+ os.environ["CLM_ROOT"] = clm_root
545
+
546
+ cache_compiler = os.path.join(clm_root, "scripts", "bootstrap", "layer0_core", "compile_cache.py")
547
+ if os.path.exists(cache_compiler):
548
+ with contextlib.suppress(Exception):
549
+ proc = subprocess.Popen([sys.executable, cache_compiler], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
550
+ proc.wait()
551
+
552
+ args = sys.argv[1:]
553
+
554
+ if "-q" in args:
555
+ args.remove("-q")
556
+ if "--quiet" in args:
557
+ args.remove("--quiet")
558
+
559
+ if not args:
560
+ print("Usage: clm_runner.py <command> [args...]")
561
+ sys.exit(1)
562
+
563
+ command = args[0]
564
+
565
+ from clm.layer4.commands import KNOWN_COMMANDS, dispatch_command
566
+
567
+ if command not in KNOWN_COMMANDS:
568
+ args = ["evaluate"] + args
569
+ command = "evaluate"
570
+
571
+ try:
572
+ rc = dispatch_command(command, args[1:])
573
+ sys.exit(rc)
574
+ except SystemExit as e:
575
+ sys.exit(e.code)
576
+ except Exception as e:
577
+ _logger.error(f"Error executing command '{command}': {e}")
578
+ _logger.error("Exception in CLI", exc_info=True)
579
+ sys.exit(1)
580
+