@alexgreensh/token-optimizer 0.0.0-stage → 5.13.50
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.
- package/.claude-plugin/marketplace.json +76 -0
- package/.claude-plugin/plugin.json +21 -0
- package/LICENSE +130 -0
- package/LICENSE-SMALL-BUSINESS.md +62 -0
- package/PRIVACY.md +200 -0
- package/README.md +13 -2
- package/hooks/codex-hooks.json +4 -0
- package/hooks/hooks.json +182 -0
- package/hooks/module_runner.py +149 -0
- package/hooks/posttooluse_runner.py +1110 -0
- package/hooks/python-launcher.sh +816 -0
- package/hooks/run.py +776 -0
- package/hooks/sessionstart_runner.py +629 -0
- package/hooks/stop_runner.py +408 -0
- package/hooks/userpromptsubmit_runner.py +722 -0
- package/hooks/windows-launcher.py +163 -0
- package/install-cloud.sh +23 -0
- package/package.json +29 -4
- package/skills/fleet-auditor/SKILL.md +150 -0
- package/skills/fleet-auditor/references/fleet-systems.md +167 -0
- package/skills/fleet-auditor/references/report-template.md +50 -0
- package/skills/fleet-auditor/references/waste-patterns.md +126 -0
- package/skills/fleet-auditor/scripts/fleet.py +3612 -0
- package/skills/fleet-auditor/scripts/shared.py +277 -0
- package/skills/resume-checkpoint/SKILL.md +52 -0
- package/skills/resume-checkpoint/scripts/pull_checkpoint.py +188 -0
- package/skills/token-coach/SKILL.md +129 -0
- package/skills/token-coach/assets/coach-dashboard-tab.html +45 -0
- package/skills/token-coach/examples/coaching-session-agentic.md +62 -0
- package/skills/token-coach/examples/coaching-session-heavy-setup.md +46 -0
- package/skills/token-coach/examples/coaching-session-new-project.md +38 -0
- package/skills/token-coach/references/agentic-systems.md +147 -0
- package/skills/token-coach/references/coach-patterns.md +120 -0
- package/skills/token-coach/references/coaching-scripts.md +146 -0
- package/skills/token-coach/references/deterministic-stages.md +102 -0
- package/skills/token-coach/references/keepwarm-consent.md +48 -0
- package/skills/token-coach/references/quick-reference.md +155 -0
- package/skills/token-dashboard/SKILL.md +72 -0
- package/skills/token-optimizer/SKILL.md +150 -0
- package/skills/token-optimizer/assets/dashboard.html +7251 -0
- package/skills/token-optimizer/examples/claude-md-optimized.md +40 -0
- package/skills/token-optimizer/examples/hooks-starter.json +179 -0
- package/skills/token-optimizer/examples/permissions-deny-template.json +24 -0
- package/skills/token-optimizer/pricing/prices.json +695 -0
- package/skills/token-optimizer/references/agent-prompts.md +687 -0
- package/skills/token-optimizer/references/cli-reference.md +92 -0
- package/skills/token-optimizer/references/codex-workflow.md +188 -0
- package/skills/token-optimizer/references/cursor-workflow.md +74 -0
- package/skills/token-optimizer/references/error-recovery.md +37 -0
- package/skills/token-optimizer/references/implementation-playbook.md +416 -0
- package/skills/token-optimizer/references/keepwarm-consent.md +48 -0
- package/skills/token-optimizer/references/opencode-workflow.md +72 -0
- package/skills/token-optimizer/references/optimization-checklist.md +714 -0
- package/skills/token-optimizer/references/phase0-setup.md +142 -0
- package/skills/token-optimizer/references/presentation-workflow.md +72 -0
- package/skills/token-optimizer/references/session-continuity.md +19 -0
- package/skills/token-optimizer/references/token-flow-architecture.md +629 -0
- package/skills/token-optimizer/scripts/activity_tracker.py +143 -0
- package/skills/token-optimizer/scripts/antigravity_doctor.py +548 -0
- package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +511 -0
- package/skills/token-optimizer/scripts/antigravity_install.py +376 -0
- package/skills/token-optimizer/scripts/antigravity_proto.py +413 -0
- package/skills/token-optimizer/scripts/antigravity_session.py +346 -0
- package/skills/token-optimizer/scripts/antigravity_state.py +486 -0
- package/skills/token-optimizer/scripts/archive_result.py +1725 -0
- package/skills/token-optimizer/scripts/bash_compress.py +2490 -0
- package/skills/token-optimizer/scripts/bash_compress_hook.py +766 -0
- package/skills/token-optimizer/scripts/bash_hook.py +61 -0
- package/skills/token-optimizer/scripts/bash_whitelist.py +243 -0
- package/skills/token-optimizer/scripts/benchmark.py +1765 -0
- package/skills/token-optimizer/scripts/build_output_compress.py +776 -0
- package/skills/token-optimizer/scripts/cloud_kit.py +991 -0
- package/skills/token-optimizer/scripts/codex_command_compress.py +434 -0
- package/skills/token-optimizer/scripts/codex_compact_prompt.py +273 -0
- package/skills/token-optimizer/scripts/codex_doctor.py +454 -0
- package/skills/token-optimizer/scripts/codex_hook_bridge.py +311 -0
- package/skills/token-optimizer/scripts/codex_install.py +810 -0
- package/skills/token-optimizer/scripts/codex_io.py +119 -0
- package/skills/token-optimizer/scripts/codex_log_index.py +219 -0
- package/skills/token-optimizer/scripts/codex_models.py +34 -0
- package/skills/token-optimizer/scripts/codex_session.py +1186 -0
- package/skills/token-optimizer/scripts/codex_state.py +707 -0
- package/skills/token-optimizer/scripts/codex_statusline.py +303 -0
- package/skills/token-optimizer/scripts/command_filters.py +386 -0
- package/skills/token-optimizer/scripts/compression_backfill.py +600 -0
- package/skills/token-optimizer/scripts/compression_log.py +230 -0
- package/skills/token-optimizer/scripts/context_intel.py +331 -0
- package/skills/token-optimizer/scripts/context_pressure.py +115 -0
- package/skills/token-optimizer/scripts/copilot_doctor.py +406 -0
- package/skills/token-optimizer/scripts/copilot_hook_bridge.py +709 -0
- package/skills/token-optimizer/scripts/copilot_install.py +277 -0
- package/skills/token-optimizer/scripts/copilot_session.py +496 -0
- package/skills/token-optimizer/scripts/copilot_state.py +569 -0
- package/skills/token-optimizer/scripts/copilot_vscode.py +832 -0
- package/skills/token-optimizer/scripts/cowork_doctor.py +270 -0
- package/skills/token-optimizer/scripts/cowork_install.py +380 -0
- package/skills/token-optimizer/scripts/credential_patterns.py +1514 -0
- package/skills/token-optimizer/scripts/cursor_doctor.py +602 -0
- package/skills/token-optimizer/scripts/cursor_hook_bridge.py +843 -0
- package/skills/token-optimizer/scripts/cursor_install.py +423 -0
- package/skills/token-optimizer/scripts/cursor_session.py +303 -0
- package/skills/token-optimizer/scripts/cursor_state.py +348 -0
- package/skills/token-optimizer/scripts/delta_diff.py +110 -0
- package/skills/token-optimizer/scripts/detectors/__init__.py +2 -0
- package/skills/token-optimizer/scripts/detectors/bad_decomposition.py +78 -0
- package/skills/token-optimizer/scripts/detectors/cache_instability.py +504 -0
- package/skills/token-optimizer/scripts/detectors/looping.py +88 -0
- package/skills/token-optimizer/scripts/detectors/output_waste.py +143 -0
- package/skills/token-optimizer/scripts/detectors/overpowered.py +116 -0
- package/skills/token-optimizer/scripts/detectors/pdf_ingestion.py +52 -0
- package/skills/token-optimizer/scripts/detectors/registry.py +65 -0
- package/skills/token-optimizer/scripts/detectors/respond_to_bash.py +76 -0
- package/skills/token-optimizer/scripts/detectors/retry_churn.py +83 -0
- package/skills/token-optimizer/scripts/detectors/tool_cascade.py +62 -0
- package/skills/token-optimizer/scripts/detectors/wasteful_thinking.py +75 -0
- package/skills/token-optimizer/scripts/detectors/weak_model.py +55 -0
- package/skills/token-optimizer/scripts/detectors/websearch_routing.py +72 -0
- package/skills/token-optimizer/scripts/deterministic_candidates.py +1458 -0
- package/skills/token-optimizer/scripts/devin_compress.py +790 -0
- package/skills/token-optimizer/scripts/devin_doctor.py +403 -0
- package/skills/token-optimizer/scripts/devin_hooks.py +453 -0
- package/skills/token-optimizer/scripts/devin_install.py +1086 -0
- package/skills/token-optimizer/scripts/devin_source.py +119 -0
- package/skills/token-optimizer/scripts/devin_state.py +330 -0
- package/skills/token-optimizer/scripts/devin_verify.py +67 -0
- package/skills/token-optimizer/scripts/grok_doctor.py +560 -0
- package/skills/token-optimizer/scripts/grok_hook_bridge.py +699 -0
- package/skills/token-optimizer/scripts/grok_install.py +397 -0
- package/skills/token-optimizer/scripts/grok_session.py +317 -0
- package/skills/token-optimizer/scripts/grok_state.py +299 -0
- package/skills/token-optimizer/scripts/hermes_doctor.py +479 -0
- package/skills/token-optimizer/scripts/hermes_hook_bridge.py +285 -0
- package/skills/token-optimizer/scripts/hermes_install.py +668 -0
- package/skills/token-optimizer/scripts/hermes_session.py +691 -0
- package/skills/token-optimizer/scripts/hermes_state.py +448 -0
- package/skills/token-optimizer/scripts/hook_io.py +120 -0
- package/skills/token-optimizer/scripts/hook_runtime.py +713 -0
- package/skills/token-optimizer/scripts/injection.py +156 -0
- package/skills/token-optimizer/scripts/install_reconcile.py +952 -0
- package/skills/token-optimizer/scripts/measure.py +58739 -0
- package/skills/token-optimizer/scripts/outline.py +53 -0
- package/skills/token-optimizer/scripts/pipeline_analyzer.py +723 -0
- package/skills/token-optimizer/scripts/plugin_env.py +562 -0
- package/skills/token-optimizer/scripts/py_trust.py +80 -0
- package/skills/token-optimizer/scripts/quality_cache_gate.py +320 -0
- package/skills/token-optimizer/scripts/read_cache.py +2489 -0
- package/skills/token-optimizer/scripts/recovery_output.py +110 -0
- package/skills/token-optimizer/scripts/refetch_fingerprint.py +236 -0
- package/skills/token-optimizer/scripts/refetch_guard.py +244 -0
- package/skills/token-optimizer/scripts/routing_advisor.py +389 -0
- package/skills/token-optimizer/scripts/runtime_env.py +1893 -0
- package/skills/token-optimizer/scripts/session_store.py +979 -0
- package/skills/token-optimizer/scripts/spawn_utils.py +122 -0
- package/skills/token-optimizer/scripts/statusline.js +660 -0
- package/skills/token-optimizer/scripts/structure_map.py +2880 -0
- package/skills/token-optimizer/scripts/structure_map_ts.py +676 -0
- package/skills/token-optimizer/scripts/structure_replay.py +1556 -0
- package/skills/token-optimizer/scripts/thrash_guard.py +604 -0
- package/skills/token-optimizer/scripts/token_estimate.py +80 -0
- package/skills/token-optimizer/scripts/tool_search_diagnostic.py +61 -0
- package/skills/token-optimizer/scripts/utf8_io.py +219 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Codex-specific install readiness checks for Token Optimizer."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from runtime_env import codex_home, detect_runtime, runtime_home
|
|
14
|
+
import codex_state
|
|
15
|
+
import codex_statusline
|
|
16
|
+
|
|
17
|
+
SUPPORTED_HOOK_EVENTS = {
|
|
18
|
+
"PreToolUse",
|
|
19
|
+
"PermissionRequest",
|
|
20
|
+
"PostToolUse",
|
|
21
|
+
"SessionStart",
|
|
22
|
+
"UserPromptSubmit",
|
|
23
|
+
"Stop",
|
|
24
|
+
"SubagentStart",
|
|
25
|
+
"SubagentStop",
|
|
26
|
+
}
|
|
27
|
+
BASH_ONLY_EVENTS = {"PreToolUse", "PermissionRequest", "PostToolUse"}
|
|
28
|
+
REQUIRED_FILES = (
|
|
29
|
+
".codex-plugin/plugin.json",
|
|
30
|
+
"hooks/python-launcher.sh",
|
|
31
|
+
"hooks/run.py",
|
|
32
|
+
"skills/token-optimizer/scripts/codex_hook_bridge.py",
|
|
33
|
+
"skills/token-optimizer/scripts/codex_session.py",
|
|
34
|
+
"skills/token-optimizer/scripts/codex_compact_prompt.py",
|
|
35
|
+
"skills/token-optimizer/scripts/codex_statusline.py",
|
|
36
|
+
"skills/token-optimizer/scripts/codex_install.py",
|
|
37
|
+
"skills/token-optimizer/scripts/bash_hook.py",
|
|
38
|
+
"skills/token-optimizer/scripts/bash_compress.py",
|
|
39
|
+
"skills/token-optimizer/scripts/context_intel.py",
|
|
40
|
+
"skills/token-optimizer/scripts/archive_result.py",
|
|
41
|
+
"skills/token-optimizer/scripts/measure.py",
|
|
42
|
+
"skills/token-optimizer/scripts/outline.py",
|
|
43
|
+
"skills/token-optimizer/scripts/runtime_env.py",
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
SKILL_INSTALL_FILES = (
|
|
47
|
+
"SKILL.md",
|
|
48
|
+
"scripts/codex_hook_bridge.py",
|
|
49
|
+
"scripts/codex_session.py",
|
|
50
|
+
"scripts/codex_compact_prompt.py",
|
|
51
|
+
"scripts/codex_statusline.py",
|
|
52
|
+
"scripts/codex_install.py",
|
|
53
|
+
"scripts/measure.py",
|
|
54
|
+
"scripts/runtime_env.py",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _repo_root() -> Path:
|
|
59
|
+
return Path(__file__).resolve().parents[3]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _skill_root() -> Path:
|
|
63
|
+
return Path(__file__).resolve().parents[1]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _is_plugin_repo(root: Path) -> bool:
|
|
67
|
+
return (root / ".codex-plugin" / "plugin.json").exists()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _check(status: str, name: str, detail: str) -> dict[str, str]:
|
|
71
|
+
return {"status": status, "name": name, "detail": detail}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _load_json(path: Path) -> tuple[Any | None, str | None]:
|
|
75
|
+
try:
|
|
76
|
+
return json.loads(path.read_text(encoding="utf-8")), None
|
|
77
|
+
except (OSError, ValueError) as exc:
|
|
78
|
+
return None, str(exc)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _codex_home_warnings() -> list[dict[str, str]]:
|
|
82
|
+
checks: list[dict[str, str]] = []
|
|
83
|
+
home = codex_home()
|
|
84
|
+
raw = os.environ.get("CODEX_HOME", "").strip()
|
|
85
|
+
|
|
86
|
+
if raw:
|
|
87
|
+
requested = Path(raw).expanduser()
|
|
88
|
+
try:
|
|
89
|
+
requested_resolved = requested.resolve(strict=False)
|
|
90
|
+
except (OSError, ValueError):
|
|
91
|
+
requested_resolved = requested
|
|
92
|
+
if requested_resolved != home.resolve(strict=False):
|
|
93
|
+
checks.append(_check("FAIL", "CODEX_HOME", f"ignored unsafe CODEX_HOME={raw!r}; using {home}"))
|
|
94
|
+
else:
|
|
95
|
+
checks.append(_check("OK", "CODEX_HOME", str(home)))
|
|
96
|
+
else:
|
|
97
|
+
checks.append(_check("OK", "CODEX_HOME", f"default {home}"))
|
|
98
|
+
|
|
99
|
+
if home.exists():
|
|
100
|
+
checks.append(_check("OK", "Codex home exists", str(home)))
|
|
101
|
+
else:
|
|
102
|
+
checks.append(_check("WARN", "Codex home exists", f"{home} does not exist yet"))
|
|
103
|
+
|
|
104
|
+
parent = home if home.exists() else home.parent
|
|
105
|
+
if os.access(parent, os.W_OK):
|
|
106
|
+
checks.append(_check("OK", "Codex storage writable", str(parent)))
|
|
107
|
+
else:
|
|
108
|
+
checks.append(_check("FAIL", "Codex storage writable", f"{parent} is not writable"))
|
|
109
|
+
|
|
110
|
+
return checks
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _manifest_checks(root: Path) -> list[dict[str, str]]:
|
|
114
|
+
path = root / ".codex-plugin" / "plugin.json"
|
|
115
|
+
data, error = _load_json(path)
|
|
116
|
+
if error:
|
|
117
|
+
return [_check("FAIL", "Plugin manifest", error)]
|
|
118
|
+
if not isinstance(data, dict):
|
|
119
|
+
return [_check("FAIL", "Plugin manifest", "manifest is not a JSON object")]
|
|
120
|
+
|
|
121
|
+
checks = []
|
|
122
|
+
name = data.get("name")
|
|
123
|
+
version = data.get("version")
|
|
124
|
+
if name == "token-optimizer":
|
|
125
|
+
checks.append(_check("OK", "Plugin name", name))
|
|
126
|
+
else:
|
|
127
|
+
checks.append(_check("FAIL", "Plugin name", f"expected token-optimizer, got {name!r}"))
|
|
128
|
+
if isinstance(version, str) and version.strip():
|
|
129
|
+
checks.append(_check("OK", "Plugin version", version))
|
|
130
|
+
else:
|
|
131
|
+
checks.append(_check("FAIL", "Plugin version", "missing or blank version"))
|
|
132
|
+
return checks
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _hook_config_checks(root: Path) -> list[dict[str, str]]:
|
|
136
|
+
path = root / ".codex" / "hooks.json"
|
|
137
|
+
data, error = _load_json(path)
|
|
138
|
+
if error:
|
|
139
|
+
return [_check("FAIL", "Codex hooks", error)]
|
|
140
|
+
if not isinstance(data, dict) or not isinstance(data.get("hooks"), dict):
|
|
141
|
+
return [_check("FAIL", "Codex hooks", "expected top-level hooks object")]
|
|
142
|
+
|
|
143
|
+
checks = []
|
|
144
|
+
hooks = data["hooks"]
|
|
145
|
+
unsupported = sorted(set(hooks) - SUPPORTED_HOOK_EVENTS)
|
|
146
|
+
if unsupported:
|
|
147
|
+
checks.append(_check("FAIL", "Hook events", f"unsupported events: {', '.join(unsupported)}"))
|
|
148
|
+
else:
|
|
149
|
+
checks.append(_check("OK", "Hook events", ", ".join(sorted(hooks))))
|
|
150
|
+
|
|
151
|
+
for event_name, groups in hooks.items():
|
|
152
|
+
if not isinstance(groups, list):
|
|
153
|
+
checks.append(_check("FAIL", f"{event_name} groups", "expected list"))
|
|
154
|
+
continue
|
|
155
|
+
for group_index, group in enumerate(groups):
|
|
156
|
+
if not isinstance(group, dict):
|
|
157
|
+
checks.append(_check("FAIL", f"{event_name}[{group_index}]", "expected object"))
|
|
158
|
+
continue
|
|
159
|
+
matcher = group.get("matcher")
|
|
160
|
+
if event_name in BASH_ONLY_EVENTS and matcher not in (None, "", "Bash", "^Bash$"):
|
|
161
|
+
checks.append(
|
|
162
|
+
_check("FAIL", f"{event_name} matcher", f"Codex currently supports Bash hook payloads, got {matcher!r}")
|
|
163
|
+
)
|
|
164
|
+
for hook_index, hook in enumerate(group.get("hooks", [])):
|
|
165
|
+
if not isinstance(hook, dict):
|
|
166
|
+
checks.append(_check("FAIL", f"{event_name} hook", "expected object"))
|
|
167
|
+
continue
|
|
168
|
+
if hook.get("type") != "command":
|
|
169
|
+
checks.append(_check("FAIL", f"{event_name} hook", f"unsupported type {hook.get('type')!r}"))
|
|
170
|
+
if hook.get("async"):
|
|
171
|
+
checks.append(_check("FAIL", f"{event_name} hook", "async hooks are skipped by current Codex"))
|
|
172
|
+
timeout = hook.get("timeout")
|
|
173
|
+
if isinstance(timeout, (int, float)) and timeout > 300:
|
|
174
|
+
checks.append(_check("WARN", f"{event_name} hook timeout", f"timeout={timeout} may be in milliseconds (Codex expects seconds)"))
|
|
175
|
+
command = hook.get("command", "")
|
|
176
|
+
if not isinstance(command, str) or not command.strip():
|
|
177
|
+
checks.append(_check("FAIL", f"{event_name} hook", "missing command"))
|
|
178
|
+
elif _command_mentions_missing_path(root, command):
|
|
179
|
+
checks.append(_check("FAIL", f"{event_name} hook {hook_index}", f"missing file in command: {command}"))
|
|
180
|
+
|
|
181
|
+
if not any(c["status"] == "FAIL" and c["name"].startswith("Hook") for c in checks):
|
|
182
|
+
checks.append(_check("OK", "Hook commands", "all referenced repo files exist"))
|
|
183
|
+
return checks
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _command_mentions_missing_path(root: Path, command: str) -> bool:
|
|
187
|
+
# Absolute paths (e.g. a stale version-pinned marketplace install path whose
|
|
188
|
+
# version dir was deleted on upgrade) must be checked as-is, NOT re-rooted
|
|
189
|
+
# under the doctor's own repo root — re-rooting always "finds" the trailing
|
|
190
|
+
# hooks/... tail and masks the real missing path. Capture the FULL
|
|
191
|
+
# absolute path (including the /.../<version>/ prefix), not just the tail.
|
|
192
|
+
for match in re.findall(r"/[A-Za-z0-9_./-]+/(?:hooks|skills)/[A-Za-z0-9_./-]+", command):
|
|
193
|
+
if not Path(match).exists():
|
|
194
|
+
return True
|
|
195
|
+
# Relative paths (dev/install.sh installs use legitimate relative paths and
|
|
196
|
+
# must not become false positives) are re-rooted under root as before.
|
|
197
|
+
for match in re.findall(r"(?:(?:\\.|/)?(?:hooks|skills)/[A-Za-z0-9_./-]+)", command):
|
|
198
|
+
if match.startswith("/"):
|
|
199
|
+
continue # already handled by the absolute-path pass above
|
|
200
|
+
candidate = root / match.lstrip("./")
|
|
201
|
+
if not candidate.exists():
|
|
202
|
+
return True
|
|
203
|
+
return False
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _required_file_checks(root: Path) -> list[dict[str, str]]:
|
|
207
|
+
missing = [rel for rel in REQUIRED_FILES if not (root / rel).exists()]
|
|
208
|
+
if missing:
|
|
209
|
+
return [_check("FAIL", "Required files", ", ".join(missing))]
|
|
210
|
+
return [_check("OK", "Required files", f"{len(REQUIRED_FILES)} present")]
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _skill_install_checks() -> list[dict[str, str]]:
|
|
214
|
+
root = _skill_root()
|
|
215
|
+
missing = [rel for rel in SKILL_INSTALL_FILES if not (root / rel).exists()]
|
|
216
|
+
if missing:
|
|
217
|
+
return [_check("FAIL", "Installed skill files", ", ".join(missing))]
|
|
218
|
+
return [
|
|
219
|
+
_check("OK", "Install shape", f"standalone Codex skill at {root}"),
|
|
220
|
+
_check("OK", "Installed skill files", f"{len(SKILL_INSTALL_FILES)} present"),
|
|
221
|
+
]
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _compact_prompt_check() -> dict[str, str]:
|
|
225
|
+
config_path = codex_home() / "config.toml"
|
|
226
|
+
try:
|
|
227
|
+
text = config_path.read_text(encoding="utf-8")
|
|
228
|
+
except OSError:
|
|
229
|
+
return _check("FAIL", "Compact prompt", f"{config_path} not found; run measure.py codex-compact-prompt --install")
|
|
230
|
+
except UnicodeDecodeError:
|
|
231
|
+
return _check("FAIL", "Compact prompt", f"{config_path} is not valid UTF-8; run measure.py codex-compact-prompt --install")
|
|
232
|
+
expected = codex_home() / "token-optimizer" / "codex-compact-prompt.md"
|
|
233
|
+
if str(expected) in text and expected.exists():
|
|
234
|
+
return _check("OK", "Compact prompt", str(expected))
|
|
235
|
+
if "compact_prompt" in text or "experimental_compact_prompt_file" in text:
|
|
236
|
+
return _check("WARN", "Compact prompt", "custom compact prompt configured")
|
|
237
|
+
return _check("FAIL", "Compact prompt", "not configured yet; run measure.py codex-compact-prompt --install")
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _status_line_check() -> dict[str, str]:
|
|
241
|
+
try:
|
|
242
|
+
state = codex_statusline.status()
|
|
243
|
+
except Exception:
|
|
244
|
+
return _check("WARN", "Codex CLI status line", "status check failed")
|
|
245
|
+
if state.startswith("configured: Token Optimizer"):
|
|
246
|
+
return _check("OK", "Codex CLI status line", state)
|
|
247
|
+
if state.startswith("configured: custom"):
|
|
248
|
+
return _check("WARN", "Codex CLI status line", state)
|
|
249
|
+
return _check("WARN", "Codex CLI status line", "not configured; rerun codex-install with --enable-status-line")
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _global_hook_check() -> dict[str, str]:
|
|
253
|
+
hooks_path = codex_home() / "hooks.json"
|
|
254
|
+
data, error = _load_json(hooks_path)
|
|
255
|
+
if error:
|
|
256
|
+
return _check("FAIL", "Global hooks", f"{hooks_path} not found; run measure.py codex-install")
|
|
257
|
+
if not isinstance(data, dict) or not isinstance(data.get("hooks"), dict):
|
|
258
|
+
return _check("FAIL", "Global hooks", f"{hooks_path} has no hooks object")
|
|
259
|
+
if "token-optimizer/scripts" in json.dumps(data, sort_keys=True):
|
|
260
|
+
return _check("OK", "Global hooks", str(hooks_path))
|
|
261
|
+
return _check("WARN", "Global hooks", f"no Token Optimizer hooks in {hooks_path}; run measure.py codex-install")
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _global_hook_config_checks() -> list[dict[str, str]]:
|
|
265
|
+
hooks_path = codex_home() / "hooks.json"
|
|
266
|
+
data, error = _load_json(hooks_path)
|
|
267
|
+
if error:
|
|
268
|
+
return []
|
|
269
|
+
if not isinstance(data, dict) or not isinstance(data.get("hooks"), dict):
|
|
270
|
+
return []
|
|
271
|
+
return _validate_hook_structure(data, hooks_path)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _validate_hook_structure(data: dict[str, Any], source_path: Path) -> list[dict[str, str]]:
|
|
275
|
+
checks: list[dict[str, str]] = []
|
|
276
|
+
hooks = data["hooks"]
|
|
277
|
+
unsupported = sorted(set(hooks) - SUPPORTED_HOOK_EVENTS)
|
|
278
|
+
if unsupported:
|
|
279
|
+
checks.append(_check("FAIL", f"Hook events ({source_path.name})", f"unsupported events: {', '.join(unsupported)}"))
|
|
280
|
+
|
|
281
|
+
for event_name, groups in hooks.items():
|
|
282
|
+
if not isinstance(groups, list):
|
|
283
|
+
continue
|
|
284
|
+
for group_index, group in enumerate(groups):
|
|
285
|
+
if not isinstance(group, dict):
|
|
286
|
+
continue
|
|
287
|
+
for hook in group.get("hooks", []):
|
|
288
|
+
if not isinstance(hook, dict):
|
|
289
|
+
continue
|
|
290
|
+
if hook.get("async"):
|
|
291
|
+
checks.append(_check("FAIL", f"{event_name} hook", "async hooks are skipped by current Codex"))
|
|
292
|
+
timeout = hook.get("timeout")
|
|
293
|
+
if isinstance(timeout, (int, float)) and timeout > 300:
|
|
294
|
+
checks.append(_check("WARN", f"{event_name} hook timeout", f"timeout={timeout} may be in milliseconds (Codex expects seconds)"))
|
|
295
|
+
return checks
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _project_hook_check(project: Path) -> dict[str, str]:
|
|
299
|
+
hooks_path = project / ".codex" / "hooks.json"
|
|
300
|
+
data, error = _load_json(hooks_path)
|
|
301
|
+
if error:
|
|
302
|
+
return _check("WARN", "Project hooks", f"{hooks_path} not found; per-project hooks are optional when global hooks are installed")
|
|
303
|
+
if not isinstance(data, dict) or not isinstance(data.get("hooks"), dict):
|
|
304
|
+
return _check("FAIL", "Project hooks", f"{hooks_path} has no hooks object")
|
|
305
|
+
if "token-optimizer/scripts" in json.dumps(data, sort_keys=True):
|
|
306
|
+
return _check("OK", "Project hooks", str(hooks_path))
|
|
307
|
+
return _check("WARN", "Project hooks", f"no Token Optimizer hooks installed in {hooks_path}; manual refresh mode avoids visible Codex hook rows")
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def _project_feature_checks(project: Path) -> list[dict[str, str]]:
|
|
311
|
+
hooks_path = project / ".codex" / "hooks.json"
|
|
312
|
+
data, error = _load_json(hooks_path)
|
|
313
|
+
if error or not isinstance(data, dict) or not isinstance(data.get("hooks"), dict):
|
|
314
|
+
return []
|
|
315
|
+
|
|
316
|
+
hooks = data.get("hooks", {})
|
|
317
|
+
checks = []
|
|
318
|
+
if _has_project_hook(hooks, "PreToolUse", "Bash", "bash_hook.py"):
|
|
319
|
+
checks.append(_check("OK", "Feature: Bash compression", "enabled for PreToolUse(Bash)"))
|
|
320
|
+
else:
|
|
321
|
+
checks.append(_check("OK", "Feature: Bash compression", "off by default to avoid visible Codex PreToolUse hook spam"))
|
|
322
|
+
|
|
323
|
+
required_features = (
|
|
324
|
+
("Session continuity and dashboard refresh", "Stop", None, "session-end-flush"),
|
|
325
|
+
)
|
|
326
|
+
optional_noisy_features = (
|
|
327
|
+
("Prompt quality nudges", "UserPromptSubmit", None, "codex_hook_bridge.py"),
|
|
328
|
+
("Subagent sprawl tracking", "SubagentStart", None, "subagent-start"),
|
|
329
|
+
("Tool output archive", "PostToolUse", "Bash", "archive_result.py"),
|
|
330
|
+
("Context intelligence", "PostToolUse", "Bash", "context_intel.py"),
|
|
331
|
+
)
|
|
332
|
+
for feature, event, matcher, needle in required_features:
|
|
333
|
+
if _has_project_hook(hooks, event, matcher, needle):
|
|
334
|
+
checks.append(_check("OK", f"Feature: {feature}", "available in current Codex adapter"))
|
|
335
|
+
else:
|
|
336
|
+
checks.append(_check("WARN", f"Feature: {feature}", f"manual refresh mode; install low-noise Stop hook with measure.py codex-install --project {project}"))
|
|
337
|
+
for feature, event, matcher, needle in optional_noisy_features:
|
|
338
|
+
if _has_project_hook(hooks, event, matcher, needle):
|
|
339
|
+
checks.append(_check("OK", f"Optional feature: {feature}", "enabled; Codex Desktop will show visible hook rows"))
|
|
340
|
+
else:
|
|
341
|
+
checks.append(_check("OK", f"Optional feature: {feature}", "off by default to avoid visible Codex hook rows"))
|
|
342
|
+
|
|
343
|
+
repo_root = _repo_root()
|
|
344
|
+
parser_path = (
|
|
345
|
+
repo_root / "skills/token-optimizer/scripts/codex_session.py"
|
|
346
|
+
if _is_plugin_repo(repo_root)
|
|
347
|
+
else _skill_root() / "scripts/codex_session.py"
|
|
348
|
+
)
|
|
349
|
+
if parser_path.exists():
|
|
350
|
+
checks.append(_check("OK", "Feature: Dashboard session parsing", "available in current Codex adapter"))
|
|
351
|
+
else:
|
|
352
|
+
checks.append(_check("FAIL", "Feature: Dashboard session parsing", f"missing {parser_path}"))
|
|
353
|
+
|
|
354
|
+
return checks
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def _has_project_hook(hooks: dict[str, Any], event: str, matcher: str | None, command_needle: str) -> bool:
|
|
358
|
+
groups = hooks.get(event)
|
|
359
|
+
if not isinstance(groups, list):
|
|
360
|
+
return False
|
|
361
|
+
for group in groups:
|
|
362
|
+
if not isinstance(group, dict):
|
|
363
|
+
continue
|
|
364
|
+
if matcher is not None and group.get("matcher") not in (matcher, f"^{matcher}$"):
|
|
365
|
+
continue
|
|
366
|
+
for hook in group.get("hooks", []):
|
|
367
|
+
if not isinstance(hook, dict) or hook.get("type") != "command":
|
|
368
|
+
continue
|
|
369
|
+
command = hook.get("command", "")
|
|
370
|
+
if isinstance(command, str) and command_needle in command:
|
|
371
|
+
return True
|
|
372
|
+
return False
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def _state_db_checks() -> list[dict[str, str]]:
|
|
376
|
+
"""Report readability of Codex's versioned state/goals SQLite databases.
|
|
377
|
+
|
|
378
|
+
These power subagent/memory/goal measurement (codex-state). Absence is a
|
|
379
|
+
WARN, not a FAIL: Codex creates them lazily once it records runtime state.
|
|
380
|
+
"""
|
|
381
|
+
if detect_runtime() != "codex":
|
|
382
|
+
return []
|
|
383
|
+
status = codex_state.state_db_status()
|
|
384
|
+
checks: list[dict[str, str]] = []
|
|
385
|
+
if status["state_db"]:
|
|
386
|
+
if status["state_readable"]:
|
|
387
|
+
checks.append(_check("OK", "Codex state DB", status["state_db"]))
|
|
388
|
+
else:
|
|
389
|
+
checks.append(_check("WARN", "Codex state DB", f"{status['state_db']} present but no thread rows yet"))
|
|
390
|
+
else:
|
|
391
|
+
checks.append(_check("WARN", "Codex state DB", "no state_*.sqlite yet; subagent/memory metrics unavailable until Codex writes one"))
|
|
392
|
+
if status["goals_present"]:
|
|
393
|
+
checks.append(_check("OK", "Codex goals DB", status["goals_db"]))
|
|
394
|
+
else:
|
|
395
|
+
checks.append(_check("OK", "Codex goals DB", "no goals_*.sqlite yet (goals are opt-in)"))
|
|
396
|
+
return checks
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def run_checks(project: Path | None = None) -> list[dict[str, str]]:
|
|
400
|
+
root = _repo_root()
|
|
401
|
+
project = project or Path.cwd()
|
|
402
|
+
checks = [
|
|
403
|
+
_check("OK", "Repo root", str(root)),
|
|
404
|
+
_check("OK", "Detected runtime", detect_runtime()),
|
|
405
|
+
_check("OK", "Runtime home", str(runtime_home())),
|
|
406
|
+
]
|
|
407
|
+
checks.extend(_codex_home_warnings())
|
|
408
|
+
checks.extend(_state_db_checks())
|
|
409
|
+
if _is_plugin_repo(root):
|
|
410
|
+
checks.extend(_required_file_checks(root))
|
|
411
|
+
checks.extend(_manifest_checks(root))
|
|
412
|
+
checks.append(_check("OK", "Codex hook template", "generated by codex_install.py"))
|
|
413
|
+
else:
|
|
414
|
+
checks.extend(_skill_install_checks())
|
|
415
|
+
checks.append(_compact_prompt_check())
|
|
416
|
+
checks.append(_status_line_check())
|
|
417
|
+
checks.append(_global_hook_check())
|
|
418
|
+
checks.extend(_global_hook_config_checks())
|
|
419
|
+
checks.append(_project_hook_check(project))
|
|
420
|
+
checks.extend(_project_feature_checks(project))
|
|
421
|
+
return checks
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def _print_text(checks: list[dict[str, str]]) -> None:
|
|
425
|
+
print("\nToken Optimizer Codex Doctor")
|
|
426
|
+
print("=" * 28)
|
|
427
|
+
for check in checks:
|
|
428
|
+
print(f"[{check['status']}] {check['name']}: {check['detail']}")
|
|
429
|
+
counts = {status: sum(1 for check in checks if check["status"] == status) for status in ("OK", "WARN", "FAIL")}
|
|
430
|
+
print(f"\nSummary: {counts['OK']} OK, {counts['WARN']} WARN, {counts['FAIL']} FAIL")
|
|
431
|
+
print("\nThis checks Token Optimizer's Codex integration (token/optimization focus).")
|
|
432
|
+
print("For generic Codex runtime/auth/network health, run the native `codex doctor`.")
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
436
|
+
parser = argparse.ArgumentParser(description="Check Token Optimizer Codex adapter readiness.")
|
|
437
|
+
parser.add_argument("--json", action="store_true", help="Emit machine-readable output")
|
|
438
|
+
parser.add_argument("--project", default=".", help="Project directory whose .codex/hooks.json should be checked")
|
|
439
|
+
return parser
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def main(argv: list[str] | None = None) -> int:
|
|
443
|
+
args = build_parser().parse_args(argv)
|
|
444
|
+
project = Path(args.project).expanduser().resolve(strict=False)
|
|
445
|
+
checks = run_checks(project=project)
|
|
446
|
+
if args.json:
|
|
447
|
+
print(json.dumps({"project": str(project), "checks": checks}, indent=2))
|
|
448
|
+
else:
|
|
449
|
+
_print_text(checks)
|
|
450
|
+
return 1 if any(check["status"] == "FAIL" for check in checks) else 0
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
if __name__ == "__main__":
|
|
454
|
+
raise SystemExit(main())
|