@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,406 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Token Optimizer — GitHub Copilot readiness doctor.
|
|
3
|
+
|
|
4
|
+
Per-source health report with fix-it hints. Copilot's data surfaces are
|
|
5
|
+
version-volatile (weekly CLI releases; hook fields break and regress
|
|
6
|
+
upstream), so the doctor names exactly WHICH source works and which broke
|
|
7
|
+
rather than a single pass/fail.
|
|
8
|
+
|
|
9
|
+
Checks:
|
|
10
|
+
P0 copilot binary on PATH + version
|
|
11
|
+
P0 ~/.copilot exists; hooks dir present, a directory, and writable
|
|
12
|
+
P1 Token Optimizer hook config installed + parseable
|
|
13
|
+
P1 capabilities.json freshness vs installed CLI version + matrix age
|
|
14
|
+
P1 session-state presence (CLI data plane)
|
|
15
|
+
P2 VS Code debug-logs / OTel presence (editor data plane)
|
|
16
|
+
P2 daemon port availability
|
|
17
|
+
|
|
18
|
+
Usage:
|
|
19
|
+
python3 copilot_doctor.py [--json]
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import argparse
|
|
25
|
+
import calendar
|
|
26
|
+
import json
|
|
27
|
+
import os
|
|
28
|
+
import shutil
|
|
29
|
+
import sys
|
|
30
|
+
import time
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
|
|
33
|
+
_SCRIPT_DIR = Path(__file__).resolve().parent
|
|
34
|
+
sys.path.insert(0, str(_SCRIPT_DIR))
|
|
35
|
+
|
|
36
|
+
from runtime_env import copilot_home, measure_cli as _measure_cli, with_measure_cli as _hint # noqa: E402
|
|
37
|
+
|
|
38
|
+
_MATRIX_STALE_DAYS = 60
|
|
39
|
+
DAEMON_PORT = 24845
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _check(status: str, name: str, detail: str, hint: str = "") -> dict:
|
|
43
|
+
# Intentional extension over codex_doctor/hermes_doctor._check: Copilot's
|
|
44
|
+
# data surfaces are version-volatile, so every failure carries a fix hint.
|
|
45
|
+
out = {"status": status, "name": name, "detail": detail}
|
|
46
|
+
if hint:
|
|
47
|
+
out["hint"] = hint
|
|
48
|
+
return out
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _binary_checks() -> list:
|
|
52
|
+
checks = []
|
|
53
|
+
exe = os.environ.get("TOKEN_OPTIMIZER_COPILOT_BIN") or "copilot"
|
|
54
|
+
path = shutil.which(exe)
|
|
55
|
+
if not path:
|
|
56
|
+
checks.append(
|
|
57
|
+
_check(
|
|
58
|
+
"fail",
|
|
59
|
+
"copilot binary",
|
|
60
|
+
f"`{exe}` not found on PATH.",
|
|
61
|
+
"Install the Copilot CLI (https://docs.github.com/copilot/copilot-cli), "
|
|
62
|
+
"or set TOKEN_OPTIMIZER_COPILOT_BIN to its location.",
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
return checks
|
|
66
|
+
try:
|
|
67
|
+
import copilot_hook_bridge
|
|
68
|
+
|
|
69
|
+
version, raw = copilot_hook_bridge._copilot_cli_version()
|
|
70
|
+
except Exception:
|
|
71
|
+
version, raw = None, ""
|
|
72
|
+
if version:
|
|
73
|
+
checks.append(_check("ok", "copilot binary", f"{path} (v{'.'.join(map(str, version))})"))
|
|
74
|
+
else:
|
|
75
|
+
checks.append(
|
|
76
|
+
_check(
|
|
77
|
+
"warn",
|
|
78
|
+
"copilot binary",
|
|
79
|
+
f"{path} found but version not detected ({raw or 'no output'}).",
|
|
80
|
+
"Run `copilot --version` manually; auth may be required.",
|
|
81
|
+
)
|
|
82
|
+
)
|
|
83
|
+
return checks
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _home_checks() -> list:
|
|
87
|
+
checks = []
|
|
88
|
+
root = copilot_home()
|
|
89
|
+
if not root.exists():
|
|
90
|
+
checks.append(
|
|
91
|
+
_check(
|
|
92
|
+
"fail",
|
|
93
|
+
"~/.copilot",
|
|
94
|
+
f"{root} does not exist.",
|
|
95
|
+
"Run the Copilot CLI once (`copilot`) so it creates its home, then re-run install.",
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
return checks
|
|
99
|
+
checks.append(_check("ok", "~/.copilot", str(root)))
|
|
100
|
+
|
|
101
|
+
hooks_dir = root / "hooks"
|
|
102
|
+
if hooks_dir.exists() and not hooks_dir.is_dir():
|
|
103
|
+
checks.append(
|
|
104
|
+
_check(
|
|
105
|
+
"fail",
|
|
106
|
+
"hooks dir",
|
|
107
|
+
f"{hooks_dir} exists but is NOT a directory.",
|
|
108
|
+
"Move the file aside; hooks must live in a directory of *.json files.",
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
elif hooks_dir.exists() and not os.access(str(hooks_dir), os.W_OK):
|
|
112
|
+
checks.append(
|
|
113
|
+
_check("fail", "hooks dir", f"{hooks_dir} is not writable.", "Fix permissions (chmod u+w).")
|
|
114
|
+
)
|
|
115
|
+
elif not hooks_dir.exists():
|
|
116
|
+
checks.append(
|
|
117
|
+
_check(
|
|
118
|
+
"warn",
|
|
119
|
+
"hooks dir",
|
|
120
|
+
f"{hooks_dir} missing (created on install).",
|
|
121
|
+
_hint("Run `python3 measure.py copilot-install`."),
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
else:
|
|
125
|
+
checks.append(_check("ok", "hooks dir", f"{hooks_dir} (writable)"))
|
|
126
|
+
return checks
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _hook_config_checks() -> list:
|
|
130
|
+
checks = []
|
|
131
|
+
hook_path = copilot_home() / "hooks" / "token-optimizer.json"
|
|
132
|
+
if not hook_path.exists():
|
|
133
|
+
checks.append(
|
|
134
|
+
_check(
|
|
135
|
+
"warn",
|
|
136
|
+
"TO hook config",
|
|
137
|
+
"Not installed.",
|
|
138
|
+
_hint("Run `python3 measure.py copilot-install`."),
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
return checks
|
|
142
|
+
try:
|
|
143
|
+
config = json.loads(hook_path.read_text(encoding="utf-8"))
|
|
144
|
+
events = sorted((config.get("hooks") or {}).keys())
|
|
145
|
+
checks.append(_check("ok", "TO hook config", f"{hook_path} (events: {', '.join(events)})"))
|
|
146
|
+
except (OSError, json.JSONDecodeError, ValueError) as exc:
|
|
147
|
+
checks.append(
|
|
148
|
+
_check(
|
|
149
|
+
"fail",
|
|
150
|
+
"TO hook config",
|
|
151
|
+
f"{hook_path} unreadable/invalid: {exc}",
|
|
152
|
+
_hint("Re-run `python3 measure.py copilot-install` to rewrite it."),
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
# Payload integrity: the bridge needs codex_io (atomic_write_json) or its
|
|
157
|
+
# tally + capability writes silently no-op (crash recovery dies).
|
|
158
|
+
plugin_dir = copilot_home() / "token-optimizer" / "plugin"
|
|
159
|
+
if plugin_dir.is_dir():
|
|
160
|
+
missing = [
|
|
161
|
+
m for m in ("copilot_hook_bridge.py", "codex_io.py", "bash_compress.py")
|
|
162
|
+
if not (plugin_dir / m).exists()
|
|
163
|
+
]
|
|
164
|
+
if missing:
|
|
165
|
+
checks.append(
|
|
166
|
+
_check(
|
|
167
|
+
"fail",
|
|
168
|
+
"hook payload",
|
|
169
|
+
f"Installed bridge is missing modules: {', '.join(missing)}.",
|
|
170
|
+
_hint("Re-run `python3 measure.py copilot-install` to refresh the payload."),
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
else:
|
|
174
|
+
checks.append(_check("ok", "hook payload", f"{plugin_dir} (complete)"))
|
|
175
|
+
return checks
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _capability_checks() -> list:
|
|
179
|
+
checks = []
|
|
180
|
+
cap_path = copilot_home() / "token-optimizer" / "capabilities.json"
|
|
181
|
+
if not cap_path.exists():
|
|
182
|
+
checks.append(
|
|
183
|
+
_check(
|
|
184
|
+
"warn",
|
|
185
|
+
"capabilities",
|
|
186
|
+
"capabilities.json missing (seeded on install / first sessionStart).",
|
|
187
|
+
_hint("Run `python3 measure.py copilot-install`."),
|
|
188
|
+
)
|
|
189
|
+
)
|
|
190
|
+
return checks
|
|
191
|
+
try:
|
|
192
|
+
data = json.loads(cap_path.read_text(encoding="utf-8"))
|
|
193
|
+
except (OSError, json.JSONDecodeError, ValueError):
|
|
194
|
+
checks.append(_check("fail", "capabilities", "capabilities.json is corrupt.", "Delete it; it reseeds on next session."))
|
|
195
|
+
return checks
|
|
196
|
+
|
|
197
|
+
caps = data.get("caps") or {}
|
|
198
|
+
on = sorted(k for k, v in caps.items() if v)
|
|
199
|
+
off = sorted(k for k, v in caps.items() if not v)
|
|
200
|
+
checks.append(
|
|
201
|
+
_check(
|
|
202
|
+
"ok",
|
|
203
|
+
"capabilities",
|
|
204
|
+
f"CLI {data.get('cli_version', '?')} — enabled: {', '.join(on) or 'none'}"
|
|
205
|
+
+ (f"; gated off: {', '.join(off)}" if off else ""),
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
# Version drift: installed CLI vs the version the caps were seeded for.
|
|
210
|
+
try:
|
|
211
|
+
import copilot_hook_bridge
|
|
212
|
+
|
|
213
|
+
version, raw = copilot_hook_bridge._copilot_cli_version()
|
|
214
|
+
if version:
|
|
215
|
+
current = ".".join(map(str, version))
|
|
216
|
+
if data.get("cli_version") not in (current, None):
|
|
217
|
+
# The doctor runs in the native shell where `copilot` IS on PATH,
|
|
218
|
+
# so it can resolve the real version even when the WSL-root
|
|
219
|
+
# sessionStart hook could only seed "unknown". Rather
|
|
220
|
+
# than tell the user to "start a session" (which, in that WSL-root
|
|
221
|
+
# context, would just re-seed "unknown" again), self-heal now:
|
|
222
|
+
# persist the correct matrix so postToolUse/allow/etc. stop being
|
|
223
|
+
# gated off on a capable CLI.
|
|
224
|
+
healed = False
|
|
225
|
+
try:
|
|
226
|
+
new_caps = copilot_hook_bridge.reseed_capabilities(version, raw)
|
|
227
|
+
healed = isinstance(new_caps, dict) and bool(new_caps)
|
|
228
|
+
except Exception:
|
|
229
|
+
healed = False
|
|
230
|
+
if healed:
|
|
231
|
+
checks.append(
|
|
232
|
+
_check(
|
|
233
|
+
"ok",
|
|
234
|
+
"capabilities freshness",
|
|
235
|
+
f"Reseeded {data.get('cli_version')} -> {current} "
|
|
236
|
+
"(matrix now matches the installed CLI).",
|
|
237
|
+
)
|
|
238
|
+
)
|
|
239
|
+
else:
|
|
240
|
+
checks.append(
|
|
241
|
+
_check(
|
|
242
|
+
"warn",
|
|
243
|
+
"capabilities freshness",
|
|
244
|
+
f"Seeded for {data.get('cli_version')}, installed CLI is {current}.",
|
|
245
|
+
_hint("Re-run `python3 measure.py copilot-install` to reseed the matrix."),
|
|
246
|
+
)
|
|
247
|
+
)
|
|
248
|
+
except Exception:
|
|
249
|
+
pass
|
|
250
|
+
|
|
251
|
+
research = data.get("matrix_research_date", "")
|
|
252
|
+
try:
|
|
253
|
+
# timegm parses as UTC; mktime would skew the age by the local offset.
|
|
254
|
+
age_days = (time.time() - calendar.timegm(time.strptime(research, "%Y-%m-%d"))) / 86400
|
|
255
|
+
if age_days > _MATRIX_STALE_DAYS:
|
|
256
|
+
checks.append(
|
|
257
|
+
_check(
|
|
258
|
+
"warn",
|
|
259
|
+
"capability matrix age",
|
|
260
|
+
f"Matrix research is {int(age_days)} days old; upstream hook bugs may have been fixed since.",
|
|
261
|
+
"Update Token Optimizer, or override via TOKEN_OPTIMIZER_COPILOT_CAPS_JSON.",
|
|
262
|
+
)
|
|
263
|
+
)
|
|
264
|
+
except (ValueError, OverflowError):
|
|
265
|
+
pass
|
|
266
|
+
return checks
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _cli_data_checks() -> list:
|
|
270
|
+
checks = []
|
|
271
|
+
state_dir = copilot_home() / "session-state"
|
|
272
|
+
if not state_dir.exists():
|
|
273
|
+
checks.append(
|
|
274
|
+
_check(
|
|
275
|
+
"warn",
|
|
276
|
+
"CLI session data",
|
|
277
|
+
f"{state_dir} missing — no Copilot CLI sessions recorded yet.",
|
|
278
|
+
"Run a Copilot CLI session; analytics activate automatically.",
|
|
279
|
+
)
|
|
280
|
+
)
|
|
281
|
+
return checks
|
|
282
|
+
try:
|
|
283
|
+
sessions = [p for p in state_dir.iterdir() if p.is_dir()]
|
|
284
|
+
except OSError:
|
|
285
|
+
sessions = []
|
|
286
|
+
incomplete = 0
|
|
287
|
+
try:
|
|
288
|
+
for s in sessions[:200]:
|
|
289
|
+
events = s / "events.jsonl"
|
|
290
|
+
if not events.exists():
|
|
291
|
+
continue
|
|
292
|
+
# Seek the tail only — events.jsonl can be tens of MB on long
|
|
293
|
+
# sessions; reading the whole file 200x would stall or OOM.
|
|
294
|
+
try:
|
|
295
|
+
with events.open("rb") as fh:
|
|
296
|
+
fh.seek(0, 2)
|
|
297
|
+
size = fh.tell()
|
|
298
|
+
fh.seek(-min(65536, size), 2)
|
|
299
|
+
tail = fh.read()
|
|
300
|
+
except OSError:
|
|
301
|
+
continue
|
|
302
|
+
# Quote the marker so a log line that merely mentions the phrase
|
|
303
|
+
# ("waiting for session.shutdown") can't read as a real event.
|
|
304
|
+
if b'"session.shutdown"' not in tail:
|
|
305
|
+
incomplete += 1
|
|
306
|
+
except Exception:
|
|
307
|
+
incomplete = -1
|
|
308
|
+
detail = f"{len(sessions)} session(s)"
|
|
309
|
+
if incomplete > 0:
|
|
310
|
+
detail += f"; {incomplete} without a shutdown event (crash/kill — recovered via partial data)"
|
|
311
|
+
checks.append(_check("ok", "CLI session data", detail))
|
|
312
|
+
return checks
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def _vscode_data_checks() -> list:
|
|
316
|
+
checks = []
|
|
317
|
+
try:
|
|
318
|
+
import copilot_vscode
|
|
319
|
+
|
|
320
|
+
status = copilot_vscode.enablement_status()
|
|
321
|
+
except Exception as exc:
|
|
322
|
+
checks.append(_check("warn", "VS Code data plane", f"reader unavailable: {exc}"))
|
|
323
|
+
return checks
|
|
324
|
+
if status.get("debug_logs_found"):
|
|
325
|
+
checks.append(_check("ok", "VS Code debug-logs", "found (per-request cost active — authoritative source)"))
|
|
326
|
+
elif status.get("otel_db_found"):
|
|
327
|
+
checks.append(
|
|
328
|
+
_check(
|
|
329
|
+
"ok",
|
|
330
|
+
"VS Code OTel DB",
|
|
331
|
+
"agent-traces.db found (fallback source; token metadata only)",
|
|
332
|
+
)
|
|
333
|
+
)
|
|
334
|
+
else:
|
|
335
|
+
checks.append(
|
|
336
|
+
_check(
|
|
337
|
+
"warn",
|
|
338
|
+
"VS Code data plane",
|
|
339
|
+
"No debug-logs or OTel DB found.",
|
|
340
|
+
'Enable both "github.copilot.chat.agentDebugLog" settings in VS Code '
|
|
341
|
+
"(note: debug logs store full prompt text on disk).",
|
|
342
|
+
)
|
|
343
|
+
)
|
|
344
|
+
return checks
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _daemon_check() -> dict:
|
|
348
|
+
import socket
|
|
349
|
+
|
|
350
|
+
try:
|
|
351
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
352
|
+
sock.settimeout(0.5)
|
|
353
|
+
in_use = sock.connect_ex(("127.0.0.1", DAEMON_PORT)) == 0
|
|
354
|
+
except OSError:
|
|
355
|
+
in_use = False
|
|
356
|
+
if in_use:
|
|
357
|
+
return _check("ok", "dashboard daemon", f"port {DAEMON_PORT} serving")
|
|
358
|
+
return _check("ok", "dashboard daemon", f"port {DAEMON_PORT} free (daemon not running — optional)")
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def run_checks() -> list:
|
|
362
|
+
checks = []
|
|
363
|
+
checks.extend(_binary_checks())
|
|
364
|
+
checks.extend(_home_checks())
|
|
365
|
+
checks.extend(_hook_config_checks())
|
|
366
|
+
checks.extend(_capability_checks())
|
|
367
|
+
checks.extend(_cli_data_checks())
|
|
368
|
+
checks.extend(_vscode_data_checks())
|
|
369
|
+
checks.append(_daemon_check())
|
|
370
|
+
return checks
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
_BADGES = {"ok": "[OK] ", "warn": "[WARN]", "fail": "[FAIL]"}
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def _print_text(checks: list) -> None:
|
|
377
|
+
print("Token Optimizer — GitHub Copilot doctor")
|
|
378
|
+
print()
|
|
379
|
+
for c in checks:
|
|
380
|
+
print(f" {_BADGES.get(c['status'], '[?] ')} {c['name']}: {c['detail']}")
|
|
381
|
+
if c.get("hint"):
|
|
382
|
+
print(f" fix: {c['hint']}")
|
|
383
|
+
fails = sum(1 for c in checks if c["status"] == "fail")
|
|
384
|
+
warns = sum(1 for c in checks if c["status"] == "warn")
|
|
385
|
+
print()
|
|
386
|
+
print(f" {len(checks)} checks — {fails} fail, {warns} warn")
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
390
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
391
|
+
parser.add_argument("--json", action="store_true")
|
|
392
|
+
return parser
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def main(argv=None) -> int:
|
|
396
|
+
args = build_parser().parse_args(argv)
|
|
397
|
+
checks = run_checks()
|
|
398
|
+
if args.json:
|
|
399
|
+
print(json.dumps(checks, indent=1))
|
|
400
|
+
else:
|
|
401
|
+
_print_text(checks)
|
|
402
|
+
return 1 if any(c["status"] == "fail" for c in checks) else 0
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
if __name__ == "__main__":
|
|
406
|
+
sys.exit(main())
|