@alexgreensh/token-optimizer 5.13.66 → 5.13.68
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 +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/hooks/hooks.json +0 -10
- package/package.json +1 -1
- package/skills/token-optimizer/examples/hooks-starter.json +0 -10
- package/skills/token-optimizer/scripts/_measure/_compat.py +2 -2
- package/skills/token-optimizer/scripts/_measure/_vendor/README.md +15 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/__init__.py +63 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/LICENSE +21 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/__init__.py +39 -0
- package/skills/token-optimizer/scripts/_measure/_vendor/tomli/py.typed +1 -0
- package/skills/token-optimizer/scripts/_measure/audit.py +5 -21
- package/skills/token-optimizer/scripts/_measure/cache_policy.py +54 -1
- package/skills/token-optimizer/scripts/_measure/daemon.py +21 -51
- package/skills/token-optimizer/scripts/_measure/lifecycle.py +81 -327
- package/skills/token-optimizer/scripts/_tomli.py +916 -0
- package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +11 -3
- package/skills/token-optimizer/scripts/antigravity_install.py +3 -0
- package/skills/token-optimizer/scripts/bash_compress.py +3 -3
- package/skills/token-optimizer/scripts/bash_compress_hook.py +6 -7
- package/skills/token-optimizer/scripts/cloud_kit.py +21 -21
- package/skills/token-optimizer/scripts/codex_compact_prompt.py +2 -2
- package/skills/token-optimizer/scripts/codex_doctor.py +33 -7
- package/skills/token-optimizer/scripts/codex_hook_provenance.py +268 -0
- package/skills/token-optimizer/scripts/codex_install.py +33 -69
- package/skills/token-optimizer/scripts/command_filters.py +359 -18
- package/skills/token-optimizer/scripts/copilot_install.py +5 -0
- package/skills/token-optimizer/scripts/cursor_install.py +5 -0
- package/skills/token-optimizer/scripts/devin_install.py +6 -12
- package/skills/token-optimizer/scripts/devin_source.py +6 -5
- package/skills/token-optimizer/scripts/devin_verify.py +16 -0
- package/skills/token-optimizer/scripts/grok_install.py +5 -0
- package/skills/token-optimizer/scripts/heal_report.py +92 -0
- package/skills/token-optimizer/scripts/hook_heal.py +138 -0
- package/skills/token-optimizer/scripts/hook_provenance.py +878 -0
- package/skills/token-optimizer/scripts/measure.py +2 -2
- package/skills/token-optimizer/scripts/owned_hooks.py +420 -0
- package/skills/token-optimizer/scripts/read_cache.py +20 -21
- package/skills/token-optimizer/scripts/reset_epoch.py +177 -0
- package/skills/token-optimizer/scripts/session_store.py +11 -48
- package/skills/token-optimizer/scripts/shipped_codex_hook_templates.json +5815 -0
- package/skills/token-optimizer/scripts/shipped_hook_templates.py +252 -0
- package/skills/token-optimizer/scripts/shipped_starter_hooks.py +55 -0
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"name": "token-optimizer",
|
|
14
14
|
"source": "./",
|
|
15
15
|
"description": "Audit, fix, and monitor Claude Code context window usage. Find the ghost tokens.",
|
|
16
|
-
"version": "5.13.
|
|
16
|
+
"version": "5.13.68",
|
|
17
17
|
"author": {
|
|
18
18
|
"name": "Alex Greenshpun",
|
|
19
19
|
"url": "https://linkedin.com/in/alexgreensh"
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"name": "token-optimizer-cowork",
|
|
56
56
|
"source": "./cowork/token-optimizer",
|
|
57
57
|
"description": "Token Optimizer for Claude Cowork \u2014 full skills + Cowork-native hooks (beta). Install THIS in Cowork; the standard token-optimizer entry is for desktop Claude Code.",
|
|
58
|
-
"version": "5.13.
|
|
58
|
+
"version": "5.13.68",
|
|
59
59
|
"author": {
|
|
60
60
|
"name": "Alex Greenshpun",
|
|
61
61
|
"url": "https://linkedin.com/in/alexgreensh"
|
package/hooks/hooks.json
CHANGED
|
@@ -14,16 +14,6 @@
|
|
|
14
14
|
}
|
|
15
15
|
]
|
|
16
16
|
},
|
|
17
|
-
{
|
|
18
|
-
"matcher": "Bash",
|
|
19
|
-
"hooks": [
|
|
20
|
-
{
|
|
21
|
-
"type": "command",
|
|
22
|
-
"command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%[/\\\\]*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/bash_hook.py --quiet; done; exit 0",
|
|
23
|
-
"timeout": 10
|
|
24
|
-
}
|
|
25
|
-
]
|
|
26
|
-
},
|
|
27
17
|
{
|
|
28
18
|
"matcher": "Agent|Task",
|
|
29
19
|
"hooks": [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexgreensh/token-optimizer",
|
|
3
|
-
"version": "5.13.
|
|
3
|
+
"version": "5.13.68",
|
|
4
4
|
"description": "Token Optimizer for Claude Code cloud sessions: the real plugin, installed by your cloud environment's setup script.",
|
|
5
5
|
"license": "PolyForm-Noncommercial-1.0.0",
|
|
6
6
|
"author": "Alex Greenshpun (https://linkedin.com/in/alexgreensh)",
|
|
@@ -11,16 +11,6 @@
|
|
|
11
11
|
}
|
|
12
12
|
]
|
|
13
13
|
},
|
|
14
|
-
{
|
|
15
|
-
"matcher": "Bash",
|
|
16
|
-
"hooks": [
|
|
17
|
-
{
|
|
18
|
-
"type": "command",
|
|
19
|
-
"command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT:-$HOME/.claude/token-optimizer}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT:-$HOME/.claude/token-optimizer}/hooks/run.py\" skills/token-optimizer/scripts/bash_hook.py --quiet; done; exit 0",
|
|
20
|
-
"timeout": 10
|
|
21
|
-
}
|
|
22
|
-
]
|
|
23
|
-
},
|
|
24
14
|
{
|
|
25
15
|
"matcher": "Agent|Task",
|
|
26
16
|
"hooks": [
|
|
@@ -27,7 +27,7 @@ def read_global(module, name):
|
|
|
27
27
|
try:
|
|
28
28
|
return builtins[name]
|
|
29
29
|
except KeyError:
|
|
30
|
-
raise NameError(f"name '{name}' is not defined", name
|
|
30
|
+
raise NameError(f"name '{name}' is not defined", **({'name': name} if sys.version_info >= (3, 10) else {})) from None
|
|
31
31
|
|
|
32
32
|
|
|
33
33
|
def prepare(module):
|
|
@@ -147,7 +147,7 @@ def _lookups(namespace, providers):
|
|
|
147
147
|
providers.load(binding.domain)
|
|
148
148
|
if name in namespace:
|
|
149
149
|
return namespace[name]
|
|
150
|
-
|
|
150
|
+
error = AttributeError(f"module {namespace.get('__name__')!r} has no attribute {name!r}"); error.name = name; raise error # name= is 3.10+
|
|
151
151
|
|
|
152
152
|
def __dir__():
|
|
153
153
|
return sorted(set(namespace) | set(providers.pending))
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Vendored TOML reader
|
|
2
|
+
|
|
3
|
+
The parser source is the flat `../../../_tomli.py` module (Tomli 2.2.1, MIT,
|
|
4
|
+
Taneli Hukkinen; SPDX headers in the file, full text in `tomli/LICENSE`).
|
|
5
|
+
It is the single copy: Devin's closed payload ships `_tomli.py` because it
|
|
6
|
+
forbids `_measure` imports, and `_measure._vendor.tomli` re-exports it so
|
|
7
|
+
repo code keeps one lazy entry point. Python 3.11+ uses stdlib `tomllib`.
|
|
8
|
+
|
|
9
|
+
Source: https://pypi.org/project/tomli/2.2.1/
|
|
10
|
+
Archive: https://files.pythonhosted.org/packages/18/87/302344fed471e44a87289cf4967697d07e532f2421fdaf868a303cbae4ff/tomli-2.2.1.tar.gz
|
|
11
|
+
Archive SHA256: `cd45e1dc79c835ce60f7404ec8119f2eb06d38b1deba146f07ced3bbc44505ff`
|
|
12
|
+
|
|
13
|
+
`tomli/__init__.py` is a compatibility shim over `_tomli`; `LICENSE` and
|
|
14
|
+
`py.typed` are kept from the upstream package. It is not an extraction
|
|
15
|
+
provider or public API.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Private, license-preserving runtime dependencies; no runtime installation."""
|
|
2
|
+
import importlib.util
|
|
3
|
+
import os as _os
|
|
4
|
+
import sys as _sys
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _lazy(name):
|
|
8
|
+
"""Return module ``name`` deferred to first attribute access.
|
|
9
|
+
|
|
10
|
+
The module object is registered in sys.modules immediately, but its code
|
|
11
|
+
runs only on the first attribute miss. No lock is held across that exec:
|
|
12
|
+
the shim's own __getattr__ keeps resolving parser attributes lazily, and
|
|
13
|
+
a fork child detects the inherited in-flight exec by pid and binds the
|
|
14
|
+
parser module directly instead of re-entering dead import state.
|
|
15
|
+
"""
|
|
16
|
+
module = _sys.modules.get(name)
|
|
17
|
+
if module is not None:
|
|
18
|
+
return module
|
|
19
|
+
spec = importlib.util.find_spec(name)
|
|
20
|
+
module = importlib.util.module_from_spec(spec)
|
|
21
|
+
pid = _os.getpid()
|
|
22
|
+
|
|
23
|
+
def _getattr(attr):
|
|
24
|
+
if _os.getpid() == pid:
|
|
25
|
+
spec.loader.exec_module(module)
|
|
26
|
+
try:
|
|
27
|
+
return module.__dict__[attr]
|
|
28
|
+
except KeyError:
|
|
29
|
+
pass
|
|
30
|
+
# The shim installed its own lazy __getattr__ during exec; it
|
|
31
|
+
# resolves parser attributes on demand. Anything else missing is
|
|
32
|
+
# a genuine AttributeError from that resolver.
|
|
33
|
+
module_getattr = module.__dict__.get('__getattr__')
|
|
34
|
+
if module_getattr is not None and module_getattr is not _getattr:
|
|
35
|
+
return module_getattr(attr)
|
|
36
|
+
else:
|
|
37
|
+
# Fork child: the parent's first exec of ``name`` was in flight,
|
|
38
|
+
# so its import lock is inherited held by a thread that does not
|
|
39
|
+
# exist here and exec_module could never finish. Bind the parser
|
|
40
|
+
# module's public names directly; _tomli's own lock is free.
|
|
41
|
+
impl = importlib.import_module('_tomli')
|
|
42
|
+
for key in ('TOMLDecodeError', '__version__', 'load', 'loads', '__all__'):
|
|
43
|
+
try:
|
|
44
|
+
module.__dict__[key] = getattr(impl, key)
|
|
45
|
+
except AttributeError:
|
|
46
|
+
pass
|
|
47
|
+
module.__dict__.pop('__getattr__', None)
|
|
48
|
+
try:
|
|
49
|
+
return module.__dict__[attr]
|
|
50
|
+
except KeyError:
|
|
51
|
+
raise AttributeError(
|
|
52
|
+
"module {!r} has no attribute {!r}".format(name, attr)) from None
|
|
53
|
+
|
|
54
|
+
module.__dict__['__getattr__'] = _getattr
|
|
55
|
+
_sys.modules[name] = module
|
|
56
|
+
return module
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# The Python 3.9/3.10 ``tomllib`` fallback. Importers bind it at module load
|
|
60
|
+
# (``from _measure._vendor import tomli as tomllib``), but Tomli itself, with
|
|
61
|
+
# its imports and regex compiles, runs only when a caller first parses TOML,
|
|
62
|
+
# so hooks that never read TOML pay nothing for it.
|
|
63
|
+
tomli = _lazy(__name__ + ".tomli")
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Taneli Hukkinen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
|
|
3
|
+
# Licensed to PSF under a Contributor Agreement.
|
|
4
|
+
|
|
5
|
+
# Compatibility shim: the canonical flat copy ships as the _tomli payload
|
|
6
|
+
# module so Devin's closed set can use it without _measure imports. This
|
|
7
|
+
# package exists only so `from _measure._vendor import tomli` keeps working;
|
|
8
|
+
# _vendor's lazy loader still defers the real parser to first TOML access.
|
|
9
|
+
import importlib as _importlib
|
|
10
|
+
|
|
11
|
+
_EXPORTS = ("TOMLDecodeError", "__version__", "load", "loads")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def __getattr__(attr):
|
|
15
|
+
if attr not in _EXPORTS:
|
|
16
|
+
raise AttributeError(
|
|
17
|
+
"module '_measure._vendor.tomli' has no attribute {!r}".format(attr))
|
|
18
|
+
mod = _importlib.import_module("_tomli")
|
|
19
|
+
try:
|
|
20
|
+
value = getattr(mod, attr)
|
|
21
|
+
except AttributeError:
|
|
22
|
+
# _tomli is still executing its own first import on this thread (an
|
|
23
|
+
# import-time callback re-entered the facade). Hand back a resolver
|
|
24
|
+
# that delegates once the parser module finishes; it is not cached,
|
|
25
|
+
# so the real attribute binds on the next access.
|
|
26
|
+
def value(*args, **kwargs):
|
|
27
|
+
target = getattr(_importlib.import_module("_tomli"), attr, None)
|
|
28
|
+
if target is None:
|
|
29
|
+
raise RuntimeError(
|
|
30
|
+
"_tomli is still executing its first import; "
|
|
31
|
+
"tomli.{}() cannot run re-entrantly".format(attr))
|
|
32
|
+
globals()[attr] = target
|
|
33
|
+
return target(*args, **kwargs)
|
|
34
|
+
return value
|
|
35
|
+
globals()[attr] = value
|
|
36
|
+
return value
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
__all__ = ("loads", "load", "TOMLDecodeError")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Marker file for PEP 561
|
|
@@ -3110,28 +3110,12 @@ def doctor(as_json=None):
|
|
|
3110
3110
|
checks.append(("OK", "Dashboard daemon", "status unknown (probe failed)"))
|
|
3111
3111
|
score += 1
|
|
3112
3112
|
|
|
3113
|
-
# Settings self-heal history
|
|
3113
|
+
# Settings self-heal history, plus one line per hook the malformed-hook
|
|
3114
|
+
# healer left alone because Token Optimizer cannot prove it wrote it.
|
|
3115
|
+
# Informational rows: they never change the score (heal_report.py).
|
|
3114
3116
|
try:
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
_heal_entries = [
|
|
3118
|
-
_measure_facade.json.loads(l) for l in _heal_log.read_text(encoding="utf-8").strip().split("\n")
|
|
3119
|
-
if l.strip()
|
|
3120
|
-
]
|
|
3121
|
-
_restores = [e for e in _heal_entries if e.get("action") == "restore"]
|
|
3122
|
-
if _restores:
|
|
3123
|
-
_last = _restores[-1]
|
|
3124
|
-
_keys = ", ".join(_last.get("restored_keys", [])[:5])
|
|
3125
|
-
if len(_last.get("restored_keys", [])) > 5:
|
|
3126
|
-
_keys += f" (+{len(_last['restored_keys']) - 5} more)"
|
|
3127
|
-
checks.append((
|
|
3128
|
-
"OK", "Settings self-heal",
|
|
3129
|
-
f"{len(_restores)} restore(s), last: {_keys} at {_last.get('timestamp', '?')[:19]}",
|
|
3130
|
-
))
|
|
3131
|
-
else:
|
|
3132
|
-
checks.append(("OK", "Settings self-heal", "no restores needed"))
|
|
3133
|
-
else:
|
|
3134
|
-
checks.append(("OK", "Settings self-heal", "no heal history yet"))
|
|
3117
|
+
import heal_report
|
|
3118
|
+
checks.extend(heal_report.doctor_rows(_measure_facade))
|
|
3135
3119
|
except Exception:
|
|
3136
3120
|
pass # doctor must never fail on heal history
|
|
3137
3121
|
|
|
@@ -614,7 +614,60 @@ def write_keepwarm_arm_record(session_id, transcript_path, now=None):
|
|
|
614
614
|
"state": "armed",
|
|
615
615
|
}
|
|
616
616
|
line = _measure_facade.json.dumps(record) + "\n"
|
|
617
|
-
|
|
617
|
+
# Idempotency (state-machine invariant: ONE active arm per
|
|
618
|
+
# session+transcript-snapshot). A double-registered Stop hook (the
|
|
619
|
+
# settings-fossil case) or two Stop events with no transcript growth
|
|
620
|
+
# between them would otherwise append a second record with an identical
|
|
621
|
+
# (session_id, transcript_mtime, byte_size) snapshot, and the tick then
|
|
622
|
+
# fires one probe ping PER RECORD for the same paused session -- the
|
|
623
|
+
# per-pause probe cap is tracked per record, and the ping's
|
|
624
|
+
# --no-session-persistence never bumps the mtime the classifier watches
|
|
625
|
+
# (regression: tests/test_keepwarm_arm_dedup.py). The check and the
|
|
626
|
+
# append must share ONE lock hold, or a concurrent arm/compaction can
|
|
627
|
+
# slip between them; the lease is not reentrant, so the append body is
|
|
628
|
+
# inlined here instead of calling _keepwarm_append_line (which takes
|
|
629
|
+
# the same lock itself).
|
|
630
|
+
try:
|
|
631
|
+
with _measure_facade._keepwarm_lock() as acquired:
|
|
632
|
+
if not acquired:
|
|
633
|
+
return None
|
|
634
|
+
for existing in _measure_facade.load_keepwarm_records():
|
|
635
|
+
if existing.get("session_id") != record["session_id"]:
|
|
636
|
+
continue
|
|
637
|
+
try:
|
|
638
|
+
same_snapshot = (
|
|
639
|
+
abs(float(existing.get("transcript_mtime", 0) or 0)
|
|
640
|
+
- record["transcript_mtime"]) < 0.001
|
|
641
|
+
and existing.get("byte_size") is not None
|
|
642
|
+
and int(existing.get("byte_size")) == record["byte_size"])
|
|
643
|
+
except (TypeError, ValueError):
|
|
644
|
+
same_snapshot = False
|
|
645
|
+
if same_snapshot:
|
|
646
|
+
# Already armed for this exact pause state; keep the
|
|
647
|
+
# original record (and its pings_fired history).
|
|
648
|
+
return existing
|
|
649
|
+
# The append body of _keepwarm_append_line, inlined so it runs
|
|
650
|
+
# inside this lock hold (same O_NOFOLLOW / 0600 semantics).
|
|
651
|
+
try:
|
|
652
|
+
_measure_facade.SNAPSHOT_DIR.mkdir(parents=True, exist_ok=True)
|
|
653
|
+
except OSError:
|
|
654
|
+
return None
|
|
655
|
+
try:
|
|
656
|
+
flags = _measure_facade.os.O_APPEND | _measure_facade.os.O_CREAT | _measure_facade.os.O_WRONLY
|
|
657
|
+
if hasattr(_measure_facade.os, "O_NOFOLLOW"):
|
|
658
|
+
flags |= _measure_facade.os.O_NOFOLLOW
|
|
659
|
+
try:
|
|
660
|
+
fd = _measure_facade.os.open(str(_measure_facade._keepwarm_sidecar_path()), flags, 0o600)
|
|
661
|
+
except OSError:
|
|
662
|
+
return None
|
|
663
|
+
try:
|
|
664
|
+
with _measure_facade.os.fdopen(fd, "a", encoding="utf-8") as fh:
|
|
665
|
+
fh.write(line)
|
|
666
|
+
except OSError:
|
|
667
|
+
return None
|
|
668
|
+
except OSError:
|
|
669
|
+
return None
|
|
670
|
+
except Exception:
|
|
618
671
|
return None
|
|
619
672
|
return record
|
|
620
673
|
except Exception:
|
|
@@ -1734,18 +1734,13 @@ def session_health():
|
|
|
1734
1734
|
print("\nSession health check is not supported on this platform.")
|
|
1735
1735
|
return
|
|
1736
1736
|
|
|
1737
|
-
installed_version =
|
|
1738
|
-
|
|
1739
|
-
automated = health["automated"]
|
|
1740
|
-
recommendations = health["recommendations"]
|
|
1737
|
+
installed_version, running_sessions, automated, recommendations = (
|
|
1738
|
+
health[k] for k in ("installed_version", "running_sessions", "automated", "recommendations"))
|
|
1741
1739
|
|
|
1742
1740
|
print("\nSESSION HEALTH CHECK")
|
|
1743
1741
|
print("=" * 55)
|
|
1744
1742
|
|
|
1745
|
-
|
|
1746
|
-
print(f"\n Installed version: {installed_version}")
|
|
1747
|
-
else:
|
|
1748
|
-
print("\n Installed version: unknown (could not run 'claude --version')")
|
|
1743
|
+
print(f"\n Installed version: {installed_version or 'unknown (could not run ' + repr('claude --version') + ')'}")
|
|
1749
1744
|
|
|
1750
1745
|
if not running_sessions:
|
|
1751
1746
|
print("\n No running Claude Code CLI sessions found.")
|
|
@@ -1770,6 +1765,11 @@ def session_health():
|
|
|
1770
1765
|
print("\nAUTOMATED PROCESSES")
|
|
1771
1766
|
for proc in automated:
|
|
1772
1767
|
print(f" {proc}")
|
|
1768
|
+
try: # hooks the malformed-hook healer left alone (heal_report.py)
|
|
1769
|
+
for _line in __import__("heal_report").health_lines(_measure_facade):
|
|
1770
|
+
print(_line)
|
|
1771
|
+
except Exception:
|
|
1772
|
+
pass # health must never fail on this optional section
|
|
1773
1773
|
|
|
1774
1774
|
print()
|
|
1775
1775
|
|
|
@@ -5430,51 +5430,19 @@ def _backup_settings_file(dest_dir: Path) -> Path | None:
|
|
|
5430
5430
|
return None
|
|
5431
5431
|
|
|
5432
5432
|
|
|
5433
|
-
def _is_our_hook_entry(entry) -> bool:
|
|
5434
|
-
"""True when a settings.json hook entry is one
|
|
5433
|
+
def _is_our_hook_entry(entry, event="", group=None) -> bool:
|
|
5434
|
+
"""True when a settings.json hook entry is one Token Optimizer wrote (cleanup).
|
|
5435
5435
|
|
|
5436
|
-
|
|
5437
|
-
|
|
5438
|
-
|
|
5439
|
-
|
|
5440
|
-
|
|
5441
|
-
|
|
5442
|
-
* it carries one of our distinctive subcommands (quality-cache,
|
|
5443
|
-
compact-capture, compact-restore, ensure-health, clear-compacted,
|
|
5444
|
-
session-end-flush, dynamic-compact-instructions).
|
|
5445
|
-
|
|
5446
|
-
Conservative by construction: unrecognized shapes are NOT ours, so a hook
|
|
5447
|
-
we cannot positively identify is left in place. The cost of a false
|
|
5448
|
-
negative is a dangling entry the user can delete; the cost of a false
|
|
5449
|
-
positive is silently deleting their automation.
|
|
5436
|
+
Proof only (owned_hooks.cleanup_owner): recorded in the provenance ledger,
|
|
5437
|
+
a shipped hooks.json entry for an install root, or byte-identical to a
|
|
5438
|
+
command setup_hook / setup_smart_compact / setup_quality_bar generated for
|
|
5439
|
+
an install's measure.py. A command that merely names our subcommands or
|
|
5440
|
+
scripts proves nothing and stays: a dangling entry the user can delete
|
|
5441
|
+
beats silently deleting their automation.
|
|
5450
5442
|
"""
|
|
5451
5443
|
_measure_facade = _compat.facade()
|
|
5452
|
-
|
|
5453
|
-
|
|
5454
|
-
cmd = entry.get("command")
|
|
5455
|
-
if not isinstance(cmd, str) or not cmd:
|
|
5456
|
-
return False
|
|
5457
|
-
low = cmd.lower()
|
|
5458
|
-
our_subcommands = (
|
|
5459
|
-
"quality-cache", "compact-capture", "compact-restore", "ensure-health",
|
|
5460
|
-
"clear-compacted", "session-end-flush", "dynamic-compact-instructions",
|
|
5461
|
-
)
|
|
5462
|
-
if any(sub in low for sub in our_subcommands):
|
|
5463
|
-
return True
|
|
5464
|
-
our_scripts = (
|
|
5465
|
-
"measure.py", "read_cache.py", "statusline.js", "archive_result.py",
|
|
5466
|
-
"context_intel.py", "activity_tracker.py",
|
|
5467
|
-
# the UserPromptSubmit dispatcher. Without it a hook that
|
|
5468
|
-
# runs userpromptsubmit_runner.py (+ token-optimizer) is not recognised
|
|
5469
|
-
# as ours, so ownership-gated repair/reconciliation skips it. Whitelisting
|
|
5470
|
-
# the script name here is inert until the dispatcher ships (it is not yet
|
|
5471
|
-
# in this branch) and never matches a foreign hook (the token-optimizer
|
|
5472
|
-
# substring is still required alongside it).
|
|
5473
|
-
"userpromptsubmit_runner.py",
|
|
5474
|
-
)
|
|
5475
|
-
if "token-optimizer" in low and any(s in low for s in our_scripts):
|
|
5476
|
-
return True
|
|
5477
|
-
return False
|
|
5444
|
+
import owned_hooks
|
|
5445
|
+
return owned_hooks.cleanup_owner(_measure_facade)(event, {"hooks": [entry]} if group is None else group, entry)
|
|
5478
5446
|
|
|
5479
5447
|
|
|
5480
5448
|
def _remove_our_settings_entries(settings: dict) -> list[str]:
|
|
@@ -5522,6 +5490,8 @@ def _remove_our_settings_entries(settings: dict) -> list[str]:
|
|
|
5522
5490
|
# filtered with the same ownership predicate.
|
|
5523
5491
|
hooks = settings.get("hooks")
|
|
5524
5492
|
if isinstance(hooks, dict):
|
|
5493
|
+
import owned_hooks
|
|
5494
|
+
owns = owned_hooks.cleanup_owner(_measure_facade) # proof only, as _is_our_hook_entry
|
|
5525
5495
|
for event in list(hooks.keys()):
|
|
5526
5496
|
groups = hooks.get(event)
|
|
5527
5497
|
if not isinstance(groups, list):
|
|
@@ -5536,7 +5506,7 @@ def _remove_our_settings_entries(settings: dict) -> list[str]:
|
|
|
5536
5506
|
if not isinstance(entries, list):
|
|
5537
5507
|
new_groups.append(group)
|
|
5538
5508
|
continue
|
|
5539
|
-
kept = [h for h in entries if not
|
|
5509
|
+
kept = [h for h in entries if not owns(event, group, h)]
|
|
5540
5510
|
n_ours = len(entries) - len(kept)
|
|
5541
5511
|
if n_ours == 0:
|
|
5542
5512
|
# Nothing of ours here -- leave the group exactly as-is,
|