cortex-loop 0.1.0a1__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 (52) hide show
  1. cortex/__init__.py +7 -0
  2. cortex/adapters.py +339 -0
  3. cortex/blocklist.py +51 -0
  4. cortex/challenges.py +210 -0
  5. cortex/cli.py +7 -0
  6. cortex/core.py +601 -0
  7. cortex/core_helpers.py +190 -0
  8. cortex/data/identity_preamble.md +5 -0
  9. cortex/data/layer1_part_a.md +65 -0
  10. cortex/data/layer1_part_b.md +17 -0
  11. cortex/executive.py +295 -0
  12. cortex/foundation.py +185 -0
  13. cortex/genome.py +348 -0
  14. cortex/graveyard.py +226 -0
  15. cortex/hooks/__init__.py +27 -0
  16. cortex/hooks/_shared.py +167 -0
  17. cortex/hooks/post_tool_use.py +13 -0
  18. cortex/hooks/pre_tool_use.py +13 -0
  19. cortex/hooks/session_start.py +13 -0
  20. cortex/hooks/stop.py +13 -0
  21. cortex/invariants.py +258 -0
  22. cortex/packs.py +118 -0
  23. cortex/repomap.py +6 -0
  24. cortex/requirements.py +497 -0
  25. cortex/retry.py +312 -0
  26. cortex/stop_contract.py +217 -0
  27. cortex/stop_payload.py +122 -0
  28. cortex/stop_policy.py +100 -0
  29. cortex/stop_runtime.py +400 -0
  30. cortex/stop_signals.py +75 -0
  31. cortex/store.py +793 -0
  32. cortex/templates/__init__.py +10 -0
  33. cortex/utils.py +58 -0
  34. cortex_loop-0.1.0a1.dist-info/METADATA +121 -0
  35. cortex_loop-0.1.0a1.dist-info/RECORD +52 -0
  36. cortex_loop-0.1.0a1.dist-info/WHEEL +5 -0
  37. cortex_loop-0.1.0a1.dist-info/entry_points.txt +3 -0
  38. cortex_loop-0.1.0a1.dist-info/licenses/LICENSE +21 -0
  39. cortex_loop-0.1.0a1.dist-info/top_level.txt +3 -0
  40. cortex_ops_cli/__init__.py +3 -0
  41. cortex_ops_cli/_adapter_validation.py +119 -0
  42. cortex_ops_cli/_check_report.py +454 -0
  43. cortex_ops_cli/_check_report_output.py +270 -0
  44. cortex_ops_cli/_openai_bridge_probe.py +241 -0
  45. cortex_ops_cli/_openai_bridge_protocol.py +469 -0
  46. cortex_ops_cli/_runtime_profile_templates.py +341 -0
  47. cortex_ops_cli/_runtime_profiles.py +445 -0
  48. cortex_ops_cli/gemini_hooks.py +301 -0
  49. cortex_ops_cli/main.py +911 -0
  50. cortex_ops_cli/openai_app_server_bridge.py +375 -0
  51. cortex_repomap/__init__.py +1 -0
  52. cortex_repomap/engine.py +1201 -0
@@ -0,0 +1,341 @@
1
+ from __future__ import annotations
2
+
3
+ import shlex
4
+ from pathlib import Path
5
+
6
+
7
+ def render_claude_settings_json(*, python_executable: str | None, schema_version: str) -> str:
8
+ text = _load_repo_template(
9
+ "claude/settings.json",
10
+ f"""{{
11
+ "hooks": {{
12
+ "PreToolUse": [
13
+ {{
14
+ "hooks": [
15
+ {{
16
+ "type": "command",
17
+ "command": "python3 -m cortex.hooks.pre_tool_use --schema-version {schema_version}"
18
+ }}
19
+ ]
20
+ }}
21
+ ],
22
+ "PostToolUse": [
23
+ {{
24
+ "hooks": [
25
+ {{
26
+ "type": "command",
27
+ "command": "python3 -m cortex.hooks.post_tool_use --schema-version {schema_version}"
28
+ }}
29
+ ]
30
+ }}
31
+ ],
32
+ "Stop": [
33
+ {{
34
+ "hooks": [
35
+ {{
36
+ "type": "command",
37
+ "command": "python3 -m cortex.hooks.stop --schema-version {schema_version}"
38
+ }}
39
+ ]
40
+ }}
41
+ ]
42
+ }}
43
+ }}
44
+ """,
45
+ )
46
+ if not python_executable:
47
+ return text
48
+ exe = shlex.quote(str(Path(python_executable)))
49
+ return text.replace("python3 -m cortex.hooks.", f"{exe} -m cortex.hooks.")
50
+
51
+
52
+ def render_gemini_settings_json(*, python_executable: str | None) -> str:
53
+ text = _load_repo_template(
54
+ "gemini/settings.json",
55
+ """{
56
+ "hooksConfig": {
57
+ "enabled": true
58
+ },
59
+ "model": {
60
+ "maxSessionTurns": 50,
61
+ "compressionThreshold": 0.35
62
+ },
63
+ "hooks": {
64
+ "SessionStart": [
65
+ {
66
+ "hooks": [
67
+ {
68
+ "type": "command",
69
+ "command": "python3 -m cortex_ops_cli.gemini_hooks SessionStart",
70
+ "name": "cortex-session-start",
71
+ "timeout": 30000
72
+ }
73
+ ]
74
+ }
75
+ ],
76
+ "BeforeTool": [
77
+ {
78
+ "hooks": [
79
+ {
80
+ "type": "command",
81
+ "command": "python3 -m cortex_ops_cli.gemini_hooks BeforeTool",
82
+ "name": "cortex-before-tool",
83
+ "timeout": 30000
84
+ }
85
+ ]
86
+ }
87
+ ],
88
+ "AfterTool": [
89
+ {
90
+ "hooks": [
91
+ {
92
+ "type": "command",
93
+ "command": "python3 -m cortex_ops_cli.gemini_hooks AfterTool",
94
+ "name": "cortex-after-tool",
95
+ "timeout": 30000
96
+ }
97
+ ]
98
+ }
99
+ ],
100
+ "BeforeAgent": [
101
+ {
102
+ "hooks": [
103
+ {
104
+ "type": "command",
105
+ "command": "python3 -m cortex_ops_cli.gemini_hooks BeforeAgent",
106
+ "name": "cortex-before-agent",
107
+ "timeout": 30000
108
+ }
109
+ ]
110
+ }
111
+ ],
112
+ "AfterAgent": [
113
+ {
114
+ "hooks": [
115
+ {
116
+ "type": "command",
117
+ "command": "python3 -m cortex_ops_cli.gemini_hooks AfterAgent",
118
+ "name": "cortex-after-agent",
119
+ "timeout": 60000
120
+ }
121
+ ]
122
+ }
123
+ ]
124
+ }
125
+ }
126
+ """,
127
+ )
128
+ if not python_executable:
129
+ return text
130
+ exe = shlex.quote(str(Path(python_executable)))
131
+ return text.replace("python3 -m cortex_ops_cli.gemini_hooks", f"{exe} -m cortex_ops_cli.gemini_hooks")
132
+
133
+
134
+ def render_claude_md() -> str:
135
+ return _load_repo_template(
136
+ "claude/CLAUDE.md",
137
+ """# Cortex Runtime Instructions (Claude Code)
138
+
139
+ ## Purpose
140
+
141
+ Cortex is enforcing completion-time quality gates on this project through Claude Code hooks.
142
+
143
+ ## Files Written by `cortex runtime install --profile claude`
144
+
145
+ - `.claude/settings.json`
146
+ - `.claude/CLAUDE.md`
147
+
148
+ ## Enforcement Expectations
149
+
150
+ - Treat hook output as policy, not optional advice.
151
+ - Keep changes small and load-bearing.
152
+ - Prefer structured stop payload fields (or a `stop_fields` object) over message-trailer fallback.
153
+
154
+ Cortex expects challenge coverage at stop:
155
+ - `null_inputs`
156
+ - `boundary_values`
157
+ - `error_handling`
158
+ - `graveyard_regression`
159
+
160
+ In strict mode, invariant failure should be treated as a revert signal.
161
+
162
+ If an approach failed, include `failed_approach` with:
163
+ - `summary`
164
+ - `reason`
165
+ - `files`
166
+
167
+ If requirement traceability is configured, include `requirement_audit` and all required IDs.
168
+
169
+ When claiming completion facts, include `truth_claims`:
170
+ - `modified_files`: files you actually changed
171
+ - `tests_ran`: test commands you actually ran
172
+
173
+ Fallback trailer format:
174
+
175
+ `STOP_FIELDS_JSON: {"challenge_coverage":{"null_inputs":true,"boundary_values":true,"error_handling":true,"graveyard_regression":true},"truth_claims":{"modified_files":["src/app.py"],"tests_ran":["pytest -q"]},"failed_approach":{"summary":"...","reason":"...","files":["path/to/file"]}}`
176
+
177
+ Use valid one-line JSON.
178
+
179
+ ## Adapter Contract
180
+
181
+ - Adapter path in `cortex.toml`: `cortex.adapters.claude:ClaudeAdapter`
182
+ - Required hook commands: `PreToolUse`, `PostToolUse`, `Stop`
183
+ - Runtime hook schema is pinned in `.claude/settings.json` (`claude_native_v1`, rollback `legacy_json_v0`)
184
+
185
+ ## Known Caveats
186
+
187
+ - `SessionStart` is not consistently emitted by every Claude Code build.
188
+ - If `SessionStart` is unavailable, rely on `PreToolUse`, `PostToolUse`, and `Stop`, and run `cortex repomap --root .` explicitly when needed.
189
+ - Each hook module also accepts `--root` and `--config` for manual testing.
190
+ """,
191
+ )
192
+
193
+
194
+ def render_gemini_md() -> str:
195
+ return _load_repo_template(
196
+ "gemini/GEMINI.md",
197
+ """# Cortex Runtime Instructions (Gemini CLI)
198
+
199
+ ## Purpose
200
+
201
+ Cortex is enforcing completion-time quality gates on this project through Gemini CLI hooks.
202
+
203
+ ## Files Written by `cortex runtime install --profile gemini`
204
+
205
+ - `.gemini/settings.json`
206
+ - `.gemini/GEMINI.md`
207
+
208
+ ## Enforcement Expectations
209
+
210
+ When producing final responses (`AfterAgent`), include Cortex stop markers
211
+ directly in the final response body using one-line `STOP_FIELDS_JSON:` with
212
+ valid JSON. Do not emit the marker through a shell command or by writing it to
213
+ another file.
214
+
215
+ In strict mode, each `challenge_coverage` category must be an object with
216
+ `covered: true` and a non-empty `evidence` list. Bare booleans are not enough.
217
+ For passing `requirement_audit.items`, use exact `status: "pass"` or
218
+ `status: "fail"` and include `evidence` for every pass item.
219
+
220
+ Example:
221
+
222
+ `STOP_FIELDS_JSON: {"challenge_coverage":{"null_inputs":{"covered":true,"evidence":["src/module.py"]},"boundary_values":{"covered":true,"evidence":["cmd:python -m pytest -q tests/test_normalize_port.py"]},"error_handling":{"covered":true,"evidence":["cmd:python -m pytest -q tests/test_normalize_port.py"]},"graveyard_regression":{"covered":true,"evidence":["tests/test_normalize_port.py"]}},"truth_claims":{"tests_ran":["python -m pytest -q tests/test_normalize_port.py"],"modified_files":["src/module.py"]},"requirement_audit":{"items":[{"id":"BUGFIX","status":"pass","evidence":["src/module.py"]},{"id":"TEST","status":"pass","evidence":["cmd:python -m pytest -q tests/test_normalize_port.py"]}],"completeness_verdict":"pass"}}`
223
+
224
+ If markers are omitted or malformed, Cortex will treat the completion claim as incomplete and return correction feedback.
225
+
226
+ ## Adapter And Bridge Contract
227
+
228
+ - Hook bridge entrypoint: `python3 -m cortex_ops_cli.gemini_hooks <HookEvent>`
229
+ - Required hook events: `SessionStart`, `BeforeTool`, `AfterTool`, `BeforeAgent`, `AfterAgent`
230
+ - Adapter path in `cortex.toml`: `cortex.adapters.gemini:GeminiAdapter`
231
+
232
+ ## Known Caveats And Status
233
+
234
+ - Non-blocking Cortex warnings are surfaced back to Gemini as `systemMessage` text.
235
+ - Blocking outcomes return hook `decision=deny` with a reason.
236
+ - Per-turn executive anchor delivery is handled by the `BeforeAgent` hook (`hookSpecificOutput.additionalContext`).
237
+ - Runtime-specific watchlist details are tracked in `docs/ADAPTERS.md`.
238
+ """,
239
+ )
240
+
241
+
242
+ def render_openai_bridge_profile_json(*, python_executable: str | None, schema_version: str) -> str:
243
+ text = _load_repo_template(
244
+ "openai/cortex_openai_bridge.json",
245
+ f"""{{
246
+ "schema_version": "{schema_version}",
247
+ "bridge": {{
248
+ "command": "python3 -m cortex_ops_cli.openai_app_server_bridge run --schema-version {schema_version}",
249
+ "codex_bin": "codex",
250
+ "listen": "stdio://"
251
+ }}
252
+ }}
253
+ """,
254
+ )
255
+ if not python_executable:
256
+ return text
257
+ exe = shlex.quote(str(Path(python_executable)))
258
+ return text.replace(
259
+ "python3 -m cortex_ops_cli.openai_app_server_bridge",
260
+ f"{exe} -m cortex_ops_cli.openai_app_server_bridge",
261
+ )
262
+
263
+
264
+ def render_openai_md() -> str:
265
+ return _load_repo_template(
266
+ "openai/OPENAI.md",
267
+ """# Cortex Runtime Instructions (OpenAI/Codex App Server)
268
+
269
+ ## Purpose
270
+
271
+ Cortex uses the OpenAI App Server bridge to enforce pre-tool, post-tool, and stop gates.
272
+
273
+ ## Files Written by `cortex runtime install --profile openai`
274
+
275
+ - `.codex/cortex_openai_bridge.json`
276
+ - `.codex/OPENAI.md`
277
+
278
+ ## Enforcement Expectations
279
+
280
+ When producing final responses through Codex App Server, include Cortex stop
281
+ markers directly in the final response body using the exact one-line literal
282
+ prefix `STOP_FIELDS_JSON:` followed by valid JSON. Do not emit the marker
283
+ through a shell command or by writing it to another file.
284
+
285
+ In strict mode, each `challenge_coverage` category must be an object with
286
+ `covered: true` and a non-empty `evidence` list. Bare booleans are not enough.
287
+ Use repo-verifiable evidence tokens such as repo-relative file paths with line
288
+ references (`src/module.py:8`, `tests/test_module.py:12`) or executed-command
289
+ markers (`cmd:python -m pytest -q tests/test_normalize_port.py`). Do not use pytest node ids,
290
+ narrative summaries, or other evidence strings the kernel cannot verify from
291
+ the workspace.
292
+ For every `requirement_audit.items` entry, use exact `status: "pass"` or
293
+ `status: "fail"`. Do not use custom statuses such as `blocked` or `not_done`.
294
+ Include `evidence` for every pass item, and for any fail item used to justify a
295
+ truthful incomplete outcome.
296
+
297
+ If completion cannot be claimed honestly, still end with one-line
298
+ `STOP_FIELDS_JSON` carrying a structured truthful failure such as
299
+ `stuck_declaration`, `failed_approach`, real `truth_claims` gaps, or real
300
+ `requirement_audit` gaps. Do not rely on prose-only refusal text.
301
+
302
+ Example:
303
+
304
+ `STOP_FIELDS_JSON: {"challenge_coverage":{"null_inputs":{"covered":true,"evidence":["src/module.py"]},"boundary_values":{"covered":true,"evidence":["cmd:python -m pytest -q tests/test_normalize_port.py"]},"error_handling":{"covered":true,"evidence":["cmd:python -m pytest -q tests/test_normalize_port.py"]},"graveyard_regression":{"covered":true,"evidence":["tests/test_normalize_port.py"]}},"truth_claims":{"tests_ran":["python -m pytest -q tests/test_normalize_port.py"],"modified_files":["src/module.py"]},"requirement_audit":{"items":[{"id":"BUGFIX","status":"pass","evidence":["src/module.py"]},{"id":"TEST","status":"pass","evidence":["cmd:python -m pytest -q tests/test_normalize_port.py"]}],"completeness_verdict":"pass"}}`
305
+
306
+ ## Enforcement-Ready Mode
307
+
308
+ - `cortex init` starts with advisory hooks because that is the safe starter state.
309
+ - A user who wants blocking enforcement can opt in through the shipped `cortex.toml` hooks surface after `cortex runtime install --profile openai`:
310
+
311
+ ```toml
312
+ [hooks]
313
+ mode = "strict"
314
+ fail_on_missing_challenge_coverage = true
315
+ require_requirement_audit = true
316
+ fail_on_requirement_audit_gap = true
317
+ require_structured_stop_payload = true
318
+ allow_message_stop_fallback = false
319
+ ```
320
+
321
+ ## Adapter And Bridge Contract
322
+
323
+ - Bridge entrypoint: `python3 -m cortex_ops_cli.openai_app_server_bridge run`
324
+ - Schema pin: `--schema-version openai_app_server_v1`
325
+ - Profile file: `.codex/cortex_openai_bridge.json`
326
+ - Adapter path in `cortex.toml`: `cortex.adapters.openai:OpenAIAdapter`
327
+
328
+ ## Known Caveats And Status
329
+
330
+ - If command approval decline cannot be proven to block execution, the bridge must report `pre_tool_use_nonblocking_approval`.
331
+ - Runs with unresolved coverage gaps are diagnostic and non-promotable.
332
+ - OpenAI remains an experimental runtime surface until current live gates prove the boundary-critical lanes on the tested stable release.
333
+ """,
334
+ )
335
+
336
+
337
+ def _load_repo_template(rel_path: str, fallback: str) -> str:
338
+ repo_template = Path(__file__).resolve().parents[1] / rel_path
339
+ if repo_template.exists():
340
+ return repo_template.read_text(encoding="utf-8")
341
+ return fallback