courant-dev 0.5.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 (152) hide show
  1. courant/__init__.py +26 -0
  2. courant/case/__init__.py +0 -0
  3. courant/case/_foam_reader.py +269 -0
  4. courant/case/config.py +134 -0
  5. courant/case/mesh.py +135 -0
  6. courant/case/reads.py +90 -0
  7. courant/case/schemes.py +99 -0
  8. courant/case/solvers.py +170 -0
  9. courant/cli/__init__.py +0 -0
  10. courant/cli/commands/__init__.py +6 -0
  11. courant/cli/commands/audit.py +40 -0
  12. courant/cli/commands/convergence.py +68 -0
  13. courant/cli/commands/ctest.py +76 -0
  14. courant/cli/commands/doctor.py +162 -0
  15. courant/cli/commands/gate.py +88 -0
  16. courant/cli/commands/live.py +73 -0
  17. courant/cli/commands/mcp.py +57 -0
  18. courant/cli/commands/preflight.py +36 -0
  19. courant/cli/commands/spec.py +231 -0
  20. courant/cli/commands/tour.py +122 -0
  21. courant/cli/commands/update.py +213 -0
  22. courant/cli/commands/verify.py +222 -0
  23. courant/cli/config.py +244 -0
  24. courant/cli/guard.py +69 -0
  25. courant/cli/main.py +130 -0
  26. courant/cli/tail.py +351 -0
  27. courant/config.py +155 -0
  28. courant/converge/__init__.py +57 -0
  29. courant/converge/assess.py +823 -0
  30. courant/converge/exact.py +112 -0
  31. courant/converge/render.py +182 -0
  32. courant/converge/scale.py +87 -0
  33. courant/converge/sentences.py +82 -0
  34. courant/converge/series.py +413 -0
  35. courant/converge/steady.py +261 -0
  36. courant/converge/tables.py +1019 -0
  37. courant/converge/transient.py +314 -0
  38. courant/ctest/__init__.py +6 -0
  39. courant/ctest/build.py +82 -0
  40. courant/ctest/data/judge-v0.labels.jsonl +31 -0
  41. courant/ctest/data/judge-v0.tasks.jsonl.gz +0 -0
  42. courant/ctest/judges/__init__.py +27 -0
  43. courant/ctest/judges/constant.py +18 -0
  44. courant/ctest/judges/courant.py +23 -0
  45. courant/ctest/judges/llm.py +166 -0
  46. courant/ctest/run.py +74 -0
  47. courant/ctest/schema.py +46 -0
  48. courant/ctest/score.py +91 -0
  49. courant/ctest/splits.py +20 -0
  50. courant/data/__init__.py +46 -0
  51. courant/data/certification.json +1 -0
  52. courant/data/certified_codes.json +18 -0
  53. courant/data/demo/icoFoam_cavity__cfl_blowup.case.json +68 -0
  54. courant/data/demo/icoFoam_cavity__cfl_blowup.log.gz +0 -0
  55. courant/data/demo/icoFoam_cavity__healthy.case.json +68 -0
  56. courant/data/demo/icoFoam_cavity__healthy.log.gz +0 -0
  57. courant/data/demo/icoFoam_cavity__transport_units.case.json +68 -0
  58. courant/data/demo/icoFoam_cavity__transport_units.log.gz +0 -0
  59. courant/data/demo/pitzDaily_fixed_budget.tar.gz +0 -0
  60. courant/data/schemas/audit.1.json +129 -0
  61. courant/data/schemas/cache.1.json +77 -0
  62. courant/data/schemas/convergence.1.json +112 -0
  63. courant/data/schemas/error-rates.1.json +53 -0
  64. courant/data/schemas/preflight.1.json +250 -0
  65. courant/data/schemas/record.1.json +155 -0
  66. courant/data/schemas/release-signature.1.json +39 -0
  67. courant/data/schemas/spec.1.json +258 -0
  68. courant/data/schemas/support.1.json +80 -0
  69. courant/data/schemas/verdict.1.json +716 -0
  70. courant/data/schemas/verdict.2.json +752 -0
  71. courant/diagnosis/__init__.py +0 -0
  72. courant/diagnosis/bundle.py +168 -0
  73. courant/diagnosis/checkers.py +919 -0
  74. courant/diagnosis/deterministic.py +110 -0
  75. courant/diagnosis/findings.py +107 -0
  76. courant/diagnosis/kb.py +188 -0
  77. courant/diagnosis/kb_data.py +253 -0
  78. courant/diagnosis/llm.py +299 -0
  79. courant/diagnosis/redact.py +425 -0
  80. courant/diagnosis/schema.py +83 -0
  81. courant/events/__init__.py +35 -0
  82. courant/events/core.py +194 -0
  83. courant/events/diverged.py +55 -0
  84. courant/gate/__init__.py +1 -0
  85. courant/gate/config.py +150 -0
  86. courant/gate/run.py +295 -0
  87. courant/live/__init__.py +16 -0
  88. courant/live/engine.py +155 -0
  89. courant/live/watch.py +164 -0
  90. courant/llm/__init__.py +59 -0
  91. courant/llm/allowlist.py +60 -0
  92. courant/llm/anthropic.py +137 -0
  93. courant/llm/budget.py +491 -0
  94. courant/llm/door.py +1071 -0
  95. courant/llm/errors.py +105 -0
  96. courant/llm/openrouter.py +118 -0
  97. courant/llm/policy.py +268 -0
  98. courant/llm/transport.py +48 -0
  99. courant/mcp/__init__.py +7 -0
  100. courant/mcp/server.py +113 -0
  101. courant/mcp/tools.py +177 -0
  102. courant/record/__init__.py +22 -0
  103. courant/record/filter.py +99 -0
  104. courant/record/record.py +149 -0
  105. courant/release.py +205 -0
  106. courant/signing.py +99 -0
  107. courant/spec/__init__.py +31 -0
  108. courant/spec/check.py +353 -0
  109. courant/spec/deck.py +455 -0
  110. courant/spec/extract.py +181 -0
  111. courant/spec/impossible.py +197 -0
  112. courant/spec/model.py +194 -0
  113. courant/taxonomy.py +219 -0
  114. courant/verification/__init__.py +30 -0
  115. courant/verification/_fluid_reader.py +228 -0
  116. courant/verification/bounding.py +84 -0
  117. courant/verification/checks.py +369 -0
  118. courant/verification/convergence.py +257 -0
  119. courant/verification/fields.py +38 -0
  120. courant/verification/grade.py +101 -0
  121. courant/verification/suite.py +30 -0
  122. courant/verify/__init__.py +37 -0
  123. courant/verify/about.py +100 -0
  124. courant/verify/audit.py +209 -0
  125. courant/verify/batch.py +141 -0
  126. courant/verify/cache.py +329 -0
  127. courant/verify/checks.py +37 -0
  128. courant/verify/html.py +330 -0
  129. courant/verify/ingest.py +115 -0
  130. courant/verify/junit.py +48 -0
  131. courant/verify/outcome.py +74 -0
  132. courant/verify/pack.py +106 -0
  133. courant/verify/pipeline.py +396 -0
  134. courant/verify/policy.py +167 -0
  135. courant/verify/preflight.py +166 -0
  136. courant/verify/render.py +174 -0
  137. courant/verify/schema.py +398 -0
  138. courant/verify/sentences.py +137 -0
  139. courant/verify/support.py +142 -0
  140. courant/verify/verdict2.py +235 -0
  141. courant/watcher/__init__.py +13 -0
  142. courant/watcher/anomaly.py +244 -0
  143. courant/watcher/exitcode.py +23 -0
  144. courant/watcher/parser.py +435 -0
  145. courant/watcher/watcher.py +49 -0
  146. courant_dev-0.5.0.dist-info/METADATA +146 -0
  147. courant_dev-0.5.0.dist-info/RECORD +152 -0
  148. courant_dev-0.5.0.dist-info/WHEEL +5 -0
  149. courant_dev-0.5.0.dist-info/entry_points.txt +3 -0
  150. courant_dev-0.5.0.dist-info/licenses/LICENSE +202 -0
  151. courant_dev-0.5.0.dist-info/licenses/NOTICE +4 -0
  152. courant_dev-0.5.0.dist-info/top_level.txt +1 -0
courant/config.py ADDED
@@ -0,0 +1,155 @@
1
+ """Central configuration + capability detection (env-driven, .env-aware).
2
+
3
+ One place decides what is available. ``real_key`` is the one definition of "a key
4
+ is configured", and the model door (``courant/llm/``) reuses it; which model
5
+ answers, and whether the LLM diagnoser runs at all, is the door's decision, not
6
+ this module's. Start the Docker daemon and the real-OpenFOAM runner auto-engages.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import os
12
+ from functools import lru_cache
13
+
14
+ # The solver image, pinned by digest (TRD OPS-2). packaging/images.lock is the record: it names
15
+ # the tag this was read from (opencfd/openfoam-default:2406), the platform manifests and where
16
+ # the digest came from, and tests/unit/test_ops.py fails when the two disagree. The tag moves;
17
+ # the digest does not. COURANT_OPENFOAM_IMAGE overrides it for a machine that mirrors the image.
18
+ OPENFOAM_IMAGE = "opencfd/openfoam-default@sha256:dd5aa20630a55722663bf83ba0cb74870cba130081303e32e3865007fa2aa35a"
19
+
20
+ # Values that look like a key but are not one. A copied-but-unedited .env is
21
+ # the common case, and treating its placeholder as real makes every request 401
22
+ # with an error nobody reads as "you forgot to fill this in".
23
+ _PLACEHOLDERS = {"sk-ant-...", "sk-or-...", "sk-...", "your-key-here", ""}
24
+
25
+
26
+ def real_key(name: str) -> str | None:
27
+ value = (os.environ.get(name) or "").strip()
28
+ return value if value not in _PLACEHOLDERS else None
29
+
30
+
31
+ def _parse_env_file(path: str) -> None:
32
+ """Set KEY=VALUE lines into the environment without overriding existing vars."""
33
+ with open(path) as fh:
34
+ for line in fh:
35
+ line = line.strip()
36
+ if not line or line.startswith("#") or "=" not in line:
37
+ continue
38
+ key, val = line.split("=", 1)
39
+ os.environ.setdefault(key.strip(), val.strip().strip('"').strip("'"))
40
+
41
+
42
+ def load_dotenv(explicit: str | None = None) -> str | None:
43
+ """Load the first ``.env`` found (explicit path, repo root, or cwd).
44
+
45
+ ``COURANT_NO_DOTENV=1`` disables it entirely. The test suite sets that,
46
+ because `.env` is developer-specific state sitting in the repository root:
47
+ a test asserting "no provider is configured" passed on a machine with no
48
+ keys and failed the moment a real key was added to `.env` — which is the
49
+ worst possible time to discover a test was never checking what it claimed.
50
+ """
51
+ if os.environ.get("COURANT_NO_DOTENV") == "1":
52
+ return None
53
+
54
+ here = os.path.dirname(os.path.abspath(__file__))
55
+ for path in [explicit, os.path.join(here, "..", ".env"),
56
+ os.path.join(os.getcwd(), ".env")]:
57
+ if path and os.path.exists(path):
58
+ _parse_env_file(path)
59
+ return path
60
+ return None
61
+
62
+
63
+ class Settings:
64
+ def __init__(self, load_env: bool = True):
65
+ if load_env:
66
+ load_dotenv()
67
+ # Keys only. Model choice belongs to the model door (courant/llm/): the
68
+ # allowlist, the data class and the measurements decide which model may
69
+ # answer, so nothing here names one.
70
+ self.anthropic_api_key = real_key("ANTHROPIC_API_KEY")
71
+ self.openrouter_api_key = real_key("OPENROUTER_API_KEY")
72
+ self.llm_max_tokens = int(os.environ.get("COURANT_LLM_MAX_TOKENS", "4096"))
73
+ self.openfoam_image = os.environ.get("COURANT_OPENFOAM_IMAGE", OPENFOAM_IMAGE)
74
+ # runner: auto (docker if available else offline) | docker | replay
75
+ self.runner_mode = os.environ.get("COURANT_RUNNER", "auto")
76
+
77
+ @property
78
+ def llm_ready(self) -> bool:
79
+ """Whether a model provider's key is configured. Not whether the LLM
80
+ diagnoser runs: that is ``capabilities()["llm"]``, the model door's answer."""
81
+ return bool(self.anthropic_api_key or self.openrouter_api_key)
82
+
83
+ @property
84
+ def providers(self) -> list[str]:
85
+ """Providers with a key configured, in the model door's registry order.
86
+ A report only: it is not a fallback order (ARC-11)."""
87
+ from courant.llm import REGISTRY
88
+
89
+ keys = {"openrouter": self.openrouter_api_key, "anthropic": self.anthropic_api_key}
90
+ return [name for name in REGISTRY if keys.get(name)]
91
+
92
+ def docker_ready(self) -> bool:
93
+ return docker_available()
94
+
95
+ @property
96
+ def docker_detail(self) -> str:
97
+ """Why Docker is unusable, or "" when it is fine."""
98
+ return docker_status()[1]
99
+
100
+ def capabilities(self) -> dict:
101
+ # Whether the LLM diagnoser runs, and on which model, is the model door's
102
+ # answer, so the console and `courant doctor` report what actually answers.
103
+ from courant.diagnosis.llm import DIAGNOSIS_CALL
104
+ from courant.llm import default_door
105
+
106
+ candidates = default_door().eligible(**DIAGNOSIS_CALL)
107
+ return {
108
+ "llm": bool(candidates),
109
+ "llm_model": candidates[0].id if candidates else None,
110
+ "llm_providers": self.providers,
111
+ "docker": self.docker_ready(),
112
+ "docker_detail": self.docker_detail,
113
+ "openfoam_image": self.openfoam_image,
114
+ }
115
+
116
+
117
+ @lru_cache(maxsize=1)
118
+ def docker_status() -> tuple[bool, str]:
119
+ """``(available, why_not)``. Cached per process.
120
+
121
+ Two failures wear the same face and need opposite fixes, so they are
122
+ reported separately:
123
+
124
+ * the ``docker`` **Python SDK** is not installed — `pip install
125
+ 'courant-dev[sandbox]'`. Nothing is wrong with the machine.
126
+ * the **daemon** is unreachable — start Docker Desktop, or check
127
+ DOCKER_HOST.
128
+
129
+ Collapsing both into "docker: false" sent me looking for a broken daemon on
130
+ a machine where `docker run` worked perfectly and only the Python package
131
+ was missing. A tool whose whole purpose is diagnosis should not misdiagnose
132
+ its own dependencies.
133
+ """
134
+ try:
135
+ import docker # type: ignore
136
+ except ImportError:
137
+ return False, "the docker Python SDK is not installed (pip install " \
138
+ "'courant-dev[sandbox]')"
139
+
140
+ try:
141
+ docker.from_env().ping()
142
+ except Exception as exc: # noqa: BLE001 — any failure means unusable
143
+ return False, f"the Docker daemon is not reachable ({type(exc).__name__})"
144
+
145
+ return True, ""
146
+
147
+
148
+ def docker_available() -> bool:
149
+ """True if a Docker daemon is reachable."""
150
+ return docker_status()[0]
151
+
152
+
153
+ @lru_cache(maxsize=1)
154
+ def get_settings() -> Settings:
155
+ return Settings()
@@ -0,0 +1,57 @@
1
+ """Courant convergence: where the engineering answer stopped changing (TRD §18).
2
+
3
+ from courant.converge import convergence_path
4
+ conv = convergence_path("./airfoil")
5
+ conv.status # PASS | WARN | FAIL | NOT_ASSESSABLE
6
+ conv.data["converged_at_step"] # the last monitor to settle
7
+ conv.data["waste"] # iterations, wall clock and core-hours after it
8
+
9
+ Read-only (INV-5). The arithmetic is pure and exact (`exact.py`). Only `series.py` reads files.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from pathlib import Path
15
+
16
+ from courant.converge.assess import Convergence, Options, assess
17
+ from courant.converge.render import render_markdown, render_text, summary
18
+ from courant.converge.series import assess_case
19
+
20
+ SCHEMA = "courant.convergence/1"
21
+
22
+
23
+ def from_run_inputs(inputs, options: Options = Options()) -> Convergence:
24
+ """Assess a run the verifier has already ingested (one parse of the log)."""
25
+ from courant.case.config import CaseConfig
26
+ from courant.watcher.exitcode import infer_exit_code
27
+ from courant.watcher.parser import LogParser
28
+
29
+ exit_code = inputs.exit_code if inputs.exit_code is not None else infer_exit_code(inputs.log_text)
30
+ events = list(LogParser(run_id=inputs.run_id).parse_text(inputs.log_text, return_code=exit_code))
31
+ config = None
32
+ if inputs.config_view:
33
+ try:
34
+ config = CaseConfig.model_validate(inputs.config_view)
35
+ except Exception: # noqa: BLE001
36
+ config = None
37
+ return assess_case(inputs.case_dir, log_text=inputs.log_text, events=events, config=config,
38
+ solver=inputs.solver_hint, options=options, read=inputs.inputs)
39
+
40
+
41
+ def convergence_path(path: str | Path, case_json: str | Path | None = None,
42
+ options: Options = Options()) -> Convergence:
43
+ from courant.verify.ingest import from_path
44
+
45
+ return from_run_inputs(from_path(path, case_json), options)
46
+
47
+
48
+ def to_document(conv: Convergence, run_id: str, solver: str | None) -> dict:
49
+ """The `--json` document: canonical, versioned, and carrying its own inputs."""
50
+ from courant import __version__
51
+
52
+ return {"schema": SCHEMA, "tool": {"name": "courant", "version": __version__},
53
+ "run": {"id": run_id, "solver": solver}, "convergence": conv.as_dict()}
54
+
55
+
56
+ __all__ = ["SCHEMA", "Convergence", "Options", "assess", "assess_case", "convergence_path",
57
+ "from_run_inputs", "render_markdown", "render_text", "summary", "to_document"]