@alexgreensh/token-optimizer 0.0.0-stage → 5.13.51
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 +448 -0
- package/hooks/posttooluse_runner.py +1110 -0
- package/hooks/python-launcher.sh +816 -0
- package/hooks/run.py +788 -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 +149 -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 +3633 -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 +141 -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 +93 -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 +379 -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 +1011 -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 +288 -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 +426 -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 +400 -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 +716 -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 +59531 -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 +1923 -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,320 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Quality-cache throttle gate — lightweight pre-check before importing measure.py.
|
|
3
|
+
|
|
4
|
+
The PostToolUse ``quality-cache --throttle-only`` hook fires on EVERY tool call.
|
|
5
|
+
In the common case (throttle window active, ~97% of calls), it does nothing but
|
|
6
|
+
one stat() of a throttle marker file. But importing measure.py (44k+ lines, 2MB)
|
|
7
|
+
costs 682ms cold — paid on every single tool call before the throttle check
|
|
8
|
+
even runs. In a read-only-scripts container (the benchmark steady state),
|
|
9
|
+
__pycache__ never persists, so every call pays the cold import.
|
|
10
|
+
|
|
11
|
+
This gate mirrors the throttle-marker stat() that measure.py's
|
|
12
|
+
``_quality_cache_tick_due`` performs, but WITHOUT importing measure.py. If the
|
|
13
|
+
throttle window is still active (the common case), it exits 0 immediately.
|
|
14
|
+
Only when the throttle has EXPIRED does it fall through to import measure.py
|
|
15
|
+
and run the full quality-cache computation — exactly as today.
|
|
16
|
+
|
|
17
|
+
Mirrors the pattern every other hook script already uses to stay measure-free:
|
|
18
|
+
read_cache.py, bash_hook.py, bash_compress_hook.py, archive_result.py,
|
|
19
|
+
context_intel.py, refetch_guard.py all avoid importing measure.py deliberately.
|
|
20
|
+
|
|
21
|
+
HARD INVARIANT preserved: a throttle-only cache MISS (missing marker) never
|
|
22
|
+
parses a transcript. The gate treats a missing marker as "not due" (OSError →
|
|
23
|
+
False, mirroring ``_quality_cache_tick_due``) and exits 0 without importing
|
|
24
|
+
measure.py at all.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
import json
|
|
30
|
+
import os
|
|
31
|
+
import re
|
|
32
|
+
import sys
|
|
33
|
+
import time
|
|
34
|
+
from functools import lru_cache
|
|
35
|
+
from pathlib import Path
|
|
36
|
+
|
|
37
|
+
# --- Lightweight path resolution (mirrors measure.py's _STATE_BASE / QUALITY_CACHE_DIR) ---
|
|
38
|
+
# These imports are deliberately from lightweight stdlib-only modules that do
|
|
39
|
+
# NOT import measure.py. This is the same pattern refetch_guard.py and
|
|
40
|
+
# archive_result.py use. If they fail to load, _ENV_RESOLVED is False and the
|
|
41
|
+
# gate falls through to the full measure.py path (correctness over speed) rather
|
|
42
|
+
# than silently guessing a marker path that might never exist.
|
|
43
|
+
try:
|
|
44
|
+
from runtime_env import runtime_home, is_cowork
|
|
45
|
+
from plugin_env import resolve_plugin_data_dir
|
|
46
|
+
|
|
47
|
+
_ENV_RESOLVED = True
|
|
48
|
+
except Exception:
|
|
49
|
+
_ENV_RESOLVED = False
|
|
50
|
+
runtime_home = None
|
|
51
|
+
is_cowork = None
|
|
52
|
+
resolve_plugin_data_dir = None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@lru_cache(maxsize=1)
|
|
56
|
+
def _resolve_quality_cache_dir() -> Path:
|
|
57
|
+
"""Resolve QUALITY_CACHE_DIR exactly as measure.py does.
|
|
58
|
+
|
|
59
|
+
Mirrors measure.py lines ~33616:
|
|
60
|
+
_STATE_BASE = _RESOLVED_PLUGIN_DATA if (cowork + plugin_data + no override) else RUNTIME_DIR
|
|
61
|
+
QUALITY_CACHE_DIR = _STATE_BASE / "token-optimizer"
|
|
62
|
+
|
|
63
|
+
Cached so repeated in-process calls (via the runner's delegation) do not
|
|
64
|
+
re-resolve. ``resolve_plugin_data_dir`` is called LAZILY — only after
|
|
65
|
+
``is_cowork()`` is True and no snapshot override is set — so the common
|
|
66
|
+
desktop/local case never pays the ``installed_plugins.json`` stat/read cost.
|
|
67
|
+
"""
|
|
68
|
+
runtime_dir = runtime_home()
|
|
69
|
+
snapshot_override = os.environ.get("TOKEN_OPTIMIZER_SNAPSHOT_DIR", "").strip()
|
|
70
|
+
|
|
71
|
+
# Lazy: only resolve plugin_data when it could actually be used.
|
|
72
|
+
# resolve_plugin_data_dir() can stat/read installed_plugins.json, which is
|
|
73
|
+
# wasted work in the common non-Cowork case.
|
|
74
|
+
if (
|
|
75
|
+
not snapshot_override
|
|
76
|
+
and is_cowork is not None
|
|
77
|
+
and is_cowork()
|
|
78
|
+
and resolve_plugin_data_dir is not None
|
|
79
|
+
):
|
|
80
|
+
plugin_data = resolve_plugin_data_dir()
|
|
81
|
+
if plugin_data is not None:
|
|
82
|
+
return plugin_data / "token-optimizer"
|
|
83
|
+
|
|
84
|
+
return runtime_dir / "token-optimizer"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _sanitize_session_id(sid: str) -> str:
|
|
88
|
+
"""Mirror measure.py.sanitize_session_id for the throttle marker path."""
|
|
89
|
+
if not sid:
|
|
90
|
+
return "unknown"
|
|
91
|
+
sanitized = re.sub(r"[^a-zA-Z0-9_-]", "", sid)
|
|
92
|
+
return sanitized if len(sanitized) >= 6 else "unknown"
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _coerce_str(value) -> str | None:
|
|
96
|
+
"""Coerce a stdin payload value to str, or None if not string-like.
|
|
97
|
+
|
|
98
|
+
The hook payload is untrusted JSON. transcript_path and session_id should
|
|
99
|
+
be strings, but a malformed or malicious payload could send int, list, null,
|
|
100
|
+
etc. Path() and re.sub() raise TypeError on non-string input, which would
|
|
101
|
+
crash the hook. This guard prevents that.
|
|
102
|
+
"""
|
|
103
|
+
if value is None:
|
|
104
|
+
return None
|
|
105
|
+
if isinstance(value, str):
|
|
106
|
+
return value
|
|
107
|
+
return None
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _throttle_marker(
|
|
111
|
+
filepath=None, session_id=None, cache_dir: Path | None = None
|
|
112
|
+
) -> Path:
|
|
113
|
+
"""Mirror measure.py._quality_cache_throttle_marker.
|
|
114
|
+
|
|
115
|
+
Returns the throttle marker path for one session (legacy global if unknown).
|
|
116
|
+
"""
|
|
117
|
+
if cache_dir is None:
|
|
118
|
+
cache_dir = _resolve_quality_cache_dir()
|
|
119
|
+
|
|
120
|
+
identity = None
|
|
121
|
+
if filepath:
|
|
122
|
+
identity = Path(filepath).stem
|
|
123
|
+
elif session_id:
|
|
124
|
+
identity = _sanitize_session_id(session_id)
|
|
125
|
+
|
|
126
|
+
if identity:
|
|
127
|
+
identity = re.sub(r"[^a-zA-Z0-9_-]", "_", str(identity))[:128]
|
|
128
|
+
return cache_dir / f".quality-cache-throttle-{identity}"
|
|
129
|
+
return cache_dir / ".quality-cache-throttle"
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _tick_due(
|
|
133
|
+
throttle_seconds: int,
|
|
134
|
+
filepath=None,
|
|
135
|
+
session_id=None,
|
|
136
|
+
cache_dir: Path | None = None,
|
|
137
|
+
) -> bool:
|
|
138
|
+
"""Mirror measure.py._quality_cache_tick_due.
|
|
139
|
+
|
|
140
|
+
One-stat gate: True when the marker is older than throttle_seconds.
|
|
141
|
+
A missing marker is a cache miss, NOT permission to parse a transcript.
|
|
142
|
+
Returns False (not due) on OSError, preserving the invariant that a
|
|
143
|
+
throttle-only cache MISS never parses a transcript.
|
|
144
|
+
|
|
145
|
+
Also catches TypeError/ValueError from non-string payload values, so a
|
|
146
|
+
malformed payload (e.g. transcript_path as int) fails open as "not due"
|
|
147
|
+
rather than crashing the hook.
|
|
148
|
+
"""
|
|
149
|
+
try:
|
|
150
|
+
marker = _throttle_marker(filepath, session_id, cache_dir)
|
|
151
|
+
age = time.time() - marker.stat().st_mtime
|
|
152
|
+
return age >= max(0, throttle_seconds)
|
|
153
|
+
except (OSError, TypeError, ValueError):
|
|
154
|
+
return False
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _read_stdin_payload() -> dict:
|
|
158
|
+
"""Read the hook payload from stdin (transcript_path, session_id).
|
|
159
|
+
|
|
160
|
+
Mirrors measure.py._read_throttle_only_hook_input: on Windows, avoids
|
|
161
|
+
reading an open stdin pipe (PeekNamedPipe first); on POSIX, uses the
|
|
162
|
+
shared hook_io reader with select-based non-blocking semantics.
|
|
163
|
+
"""
|
|
164
|
+
if os.name == "nt":
|
|
165
|
+
try:
|
|
166
|
+
import ctypes
|
|
167
|
+
import msvcrt
|
|
168
|
+
|
|
169
|
+
stream = getattr(sys.stdin, "buffer", sys.stdin)
|
|
170
|
+
fd = stream.fileno()
|
|
171
|
+
available = ctypes.c_ulong()
|
|
172
|
+
handle = msvcrt.get_osfhandle(fd)
|
|
173
|
+
ok = ctypes.windll.kernel32.PeekNamedPipe(
|
|
174
|
+
handle, None, 0, None, ctypes.byref(available), None
|
|
175
|
+
)
|
|
176
|
+
if not ok or not available.value:
|
|
177
|
+
return {}
|
|
178
|
+
raw = os.read(fd, min(1_000_000, available.value))
|
|
179
|
+
return json.loads(raw.decode("utf-8")) if raw else {}
|
|
180
|
+
except (
|
|
181
|
+
AttributeError,
|
|
182
|
+
ImportError,
|
|
183
|
+
OSError,
|
|
184
|
+
ValueError,
|
|
185
|
+
UnicodeDecodeError,
|
|
186
|
+
json.JSONDecodeError,
|
|
187
|
+
):
|
|
188
|
+
return {}
|
|
189
|
+
try:
|
|
190
|
+
from hook_io import read_stdin_hook_input
|
|
191
|
+
|
|
192
|
+
return read_stdin_hook_input(max_bytes=1_000_000)
|
|
193
|
+
except Exception:
|
|
194
|
+
return {}
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def main() -> int:
|
|
198
|
+
args = sys.argv[1:]
|
|
199
|
+
quiet = "--quiet" in args or "-q" in args
|
|
200
|
+
warn = "--warn" in args
|
|
201
|
+
force = "--force" in args
|
|
202
|
+
throttle = 120
|
|
203
|
+
warn_threshold = 70
|
|
204
|
+
for i, a in enumerate(args):
|
|
205
|
+
if a == "--throttle" and i + 1 < len(args):
|
|
206
|
+
try:
|
|
207
|
+
throttle = int(args[i + 1])
|
|
208
|
+
except ValueError:
|
|
209
|
+
pass
|
|
210
|
+
if a == "--warn-threshold" and i + 1 < len(args):
|
|
211
|
+
try:
|
|
212
|
+
warn_threshold = int(args[i + 1])
|
|
213
|
+
except ValueError:
|
|
214
|
+
pass
|
|
215
|
+
|
|
216
|
+
# Read the hook payload from stdin (transcript_path, session_id)
|
|
217
|
+
payload = _read_stdin_payload()
|
|
218
|
+
|
|
219
|
+
# Coerce payload values to strings — the hook payload is untrusted JSON and
|
|
220
|
+
# non-string values (int, list, null) would crash Path() or re.sub().
|
|
221
|
+
filepath = _coerce_str(payload.get("transcript_path"))
|
|
222
|
+
session_id = _coerce_str(payload.get("session_id"))
|
|
223
|
+
|
|
224
|
+
# Force bypasses the throttle check — always fall through to full computation.
|
|
225
|
+
# Also, if the lightweight env modules failed to import, we cannot resolve
|
|
226
|
+
# the marker path safely — fall through to measure.py (correctness over speed)
|
|
227
|
+
# rather than guessing a path that might never exist and silently freezing.
|
|
228
|
+
if not force and _ENV_RESOLVED:
|
|
229
|
+
cache_dir = _resolve_quality_cache_dir()
|
|
230
|
+
|
|
231
|
+
if not _tick_due(throttle, filepath, session_id, cache_dir):
|
|
232
|
+
# Throttle ACTIVE (common case, ~97% of calls) — exit 0 immediately.
|
|
233
|
+
# No measure.py import, no transcript parse, no cache write.
|
|
234
|
+
# This is the entire point of the gate: a stat() instead of 682ms.
|
|
235
|
+
return 0
|
|
236
|
+
# Throttle EXPIRED — fall through to the full quality-cache computation.
|
|
237
|
+
# This is the rare case (~3% of calls), so the 682ms cold import is
|
|
238
|
+
# acceptable here, exactly as today.
|
|
239
|
+
|
|
240
|
+
# --- Throttle expired (or forced, or env unresolved) — full computation path ---
|
|
241
|
+
# Import measure.py and run the full quality-cache computation. This
|
|
242
|
+
# mirrors the original dispatch's quality-cache --throttle-only path:
|
|
243
|
+
# quality_cache() + evaluate_cohort_tripwire() piggyback.
|
|
244
|
+
try:
|
|
245
|
+
import measure
|
|
246
|
+
|
|
247
|
+
# Mid-session dashboard-daemon liveness pulse (best-effort, same as
|
|
248
|
+
# the original dispatch).
|
|
249
|
+
try:
|
|
250
|
+
measure._daemon_midsession_pulse()
|
|
251
|
+
except Exception:
|
|
252
|
+
pass
|
|
253
|
+
|
|
254
|
+
# Self-heal: if the quality-cache hook is missing from settings.json
|
|
255
|
+
# and this is NOT a plugin install and quality_bar_disabled is unset,
|
|
256
|
+
# reinstall it. Mirrors the original dispatch's self-heal block and
|
|
257
|
+
# the runner's _quality_cache_self_heal. Only runs on the expired
|
|
258
|
+
# path (after the throttle check), exactly as in the dispatch.
|
|
259
|
+
try:
|
|
260
|
+
_quality_cache_self_heal(measure)
|
|
261
|
+
except Exception:
|
|
262
|
+
pass
|
|
263
|
+
|
|
264
|
+
measure.quality_cache(
|
|
265
|
+
throttle_seconds=throttle,
|
|
266
|
+
warn_threshold=warn_threshold,
|
|
267
|
+
quiet=quiet,
|
|
268
|
+
pure_time_throttle=True,
|
|
269
|
+
session_jsonl=filepath,
|
|
270
|
+
session_id=session_id,
|
|
271
|
+
force=force,
|
|
272
|
+
warn=warn,
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
# Tripwire piggyback: the --throttle-only invocation fires on the
|
|
276
|
+
# PostToolUse Edit/Write path, so refresh the per-cohort live edit-rate
|
|
277
|
+
# verdict. Mtime-gated to 5 min, so the common case is a single sidecar
|
|
278
|
+
# stat() — near-zero added cost.
|
|
279
|
+
try:
|
|
280
|
+
measure.evaluate_cohort_tripwire()
|
|
281
|
+
except Exception:
|
|
282
|
+
pass
|
|
283
|
+
except Exception:
|
|
284
|
+
# Fail-open: never crash a hook
|
|
285
|
+
pass
|
|
286
|
+
|
|
287
|
+
return 0
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def _quality_cache_self_heal(measure) -> None:
|
|
291
|
+
"""Replicate the quality-cache dispatch's self-healing block.
|
|
292
|
+
|
|
293
|
+
If the quality-cache hook is missing from settings.json and this is NOT a
|
|
294
|
+
plugin install and quality_bar_disabled is unset, reinstall it. Mirrors
|
|
295
|
+
hooks/posttooluse_runner.py:_quality_cache_self_heal and the original
|
|
296
|
+
measure.py dispatch block. Fail-open: never raises.
|
|
297
|
+
"""
|
|
298
|
+
if measure._is_running_from_plugin_cache() or measure._is_plugin_installed():
|
|
299
|
+
return
|
|
300
|
+
if not measure.CONFIG_PATH.exists():
|
|
301
|
+
return
|
|
302
|
+
try:
|
|
303
|
+
_qb_cfg = json.loads(measure.CONFIG_PATH.read_text(encoding="utf-8"))
|
|
304
|
+
except Exception:
|
|
305
|
+
_qb_cfg = {}
|
|
306
|
+
if _qb_cfg.get("quality_bar_disabled"):
|
|
307
|
+
return
|
|
308
|
+
# a FIFO at settings.json would block read_text() on the per-turn hot path
|
|
309
|
+
if not measure._is_regular_file(measure.SETTINGS_PATH):
|
|
310
|
+
return
|
|
311
|
+
try:
|
|
312
|
+
_sh_hooks = json.loads(measure.SETTINGS_PATH.read_text(encoding="utf-8"))
|
|
313
|
+
except Exception:
|
|
314
|
+
return
|
|
315
|
+
if not measure._quality_cache_hook_present(_sh_hooks):
|
|
316
|
+
measure.setup_quality_bar(quiet=True)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
if __name__ == "__main__":
|
|
320
|
+
sys.exit(main())
|