perf(skills): cut skill tokens and load hooks from cached bytecode

Hooks start their script as __main__ from cached bytecode instead of recompiling it on every call, and the installer migrates registered old hook commands to that form. A disabled plugin is written as false instead of deleted, because a deleted key in settings.local.json lets a true in settings.json win; the auto-compact window drops to 300000. The coach hour prints one ready line chosen by the time of day instead of asking the agent to load the skill, kvb-cleanup moves its procedure into plan.md, and the token counter reads subagent transcripts too and counts with Counter and defaultdict instead of .get defaults.

🎯 Fix: 88 %
💥 Break: 8 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 81
🚦 Gate: ⛔ fix 88 % < 90, break 8 % > 5

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-18 19:40:37 +00:00
1 parent d695bb2b58
commit 6c63c364bd
13 files changed
+250 -130

No files matched your search

+3 -2
View File
@@ -27,8 +27,9 @@ per day and never changes the exit code; pass its quotient or `?` to
`kvb-check-score --value`.
- **Cost**: listing words delta x 1.33 x requests x 0.1 (cached) plus each
changed `SKILL.md`'s tokens delta x its invocations, per day of the last
7 days' transcripts (other runtimes: JSONL of that shape). A new skill
changed `SKILL.md`'s tokens delta x (its invocations + 0.1 x the later
turns it stays resident), per day of the last 7 days' session and
subagent transcripts (other runtimes: JSONL of that shape). A new skill
takes its category's median, named in the line.
- **Value**: tokens/day saved, derived by one `--evidence` line, e.g.
`"3829 SendMessage wakes/day x 10 % avoided x 43k"`; else `?:?`.
+71 -34
View File
@@ -4,9 +4,12 @@
Args:
--repo: skills repository root; its scripts/budget.py counts the listing.
--base: git ref the change starts from; the working tree is the after side.
--transcripts: Claude Code transcripts root, e.g. ~/.claude/projects; every */*.jsonl of the
last 7 days gives the requests (unique assistant message ids) and the invocations per skill
(unique Skill tool_use ids, a `<scope>:` prefix stripped). Read only; no text is printed.
--transcripts: Claude Code transcripts root, e.g. ~/.claude/projects; every session and
subagent transcript of the last 7 days (context.py of kvb-skill-silence lists them) gives
the requests (unique assistant message ids), the invocations per skill (unique Skill
tool_use ids, a `<scope>:` prefix stripped) and per skill its resident turns: every later
request of the same transcript, once per invocation, as Claude Code keeps an invoked
skill in context and re-injects it after a compaction. Read only; no text is printed.
--value: tokens per day the change saves; needs --evidence.
--evidence: one line that derives --value.
--json: print the figures as JSON instead of the lines.
@@ -23,10 +26,11 @@ Output:
Line 2: `💰 value 1:<n> (cost <c>/day, value <v>/day; <evidence>)` when the value is at least
the cost, `<n>:1` below it, `0:1` without a cost, `1:0` without a value; without --value
`💰 value ?:? (cost <c>/day; pass --value with evidence)`. Cost per day = listing words delta
* 1.33 tokens * requests per day * 0.1 (cached) + per changed SKILL.md its tokens delta * the
skill's invocations per day. A skill without a base SKILL.md takes the median invocations of
its category's base skills (the name up to its second dash; all base skills when it has
none), and the line names it.
* 1.33 tokens * requests per day * 0.1 (cached) + per changed SKILL.md its tokens delta *
(the skill's invocations per day + 0.1 * its resident turns per day). A skill without a base
SKILL.md takes the median invocations and the median resident turns of its category's base
skills (the name up to its second dash; all base skills when it has none), and the line
names them.
--json prints efficiency (null when new), the skills and listing totals, the tokens per skill
and the value figures.
value: `💰 measured value <v>/day (median daily context <b>→<a> tokens over <n> active days
@@ -49,6 +53,7 @@ import math
import statistics
import subprocess
import sys
from collections import Counter, defaultdict
from pathlib import Path, PurePosixPath
from types import ModuleType
@@ -192,55 +197,86 @@ def skill_uses(message: dict) -> list[tuple[str, str]]:
return found
def usage(root: Path, since: float) -> tuple[int, dict[str, int]]:
def entries(path: Path, since: float):
"""Args:
path: one transcript.
since: the epoch second the window starts.
Returns:
The assistant messages of the window, in transcript order.
"""
with path.open(encoding="utf-8", errors="ignore") as handle:
for line in handle:
if '"assistant"' not in line:
continue
try:
entry = json.loads(line)
stamp = datetime.datetime.fromisoformat(entry["timestamp"]).timestamp()
except (ValueError, KeyError, TypeError):
continue
message = entry.get("message")
if entry.get("type") == "assistant" and stamp >= since and isinstance(message, dict):
yield message
def usage(root: Path, since: float) -> tuple[int, dict[str, int], dict[str, int]]:
"""Args:
root: the transcripts root.
since: the epoch second the window starts.
Returns:
The requests, per skill its invocations, and per skill its resident turns.
"""
listed = load(CONTEXT, "context", "lists the transcripts").transcripts(root)
requests: set[str] = set()
invocations: dict[str, set[str]] = {}
for path in root.glob("*/*.jsonl"):
resident: Counter[str] = Counter()
for path in listed:
if path.stat().st_mtime < since:
continue
with path.open(encoding="utf-8", errors="ignore") as handle:
for line in handle:
if '"assistant"' not in line:
continue
try:
entry = json.loads(line)
stamp = datetime.datetime.fromisoformat(entry["timestamp"]).timestamp()
except (ValueError, KeyError, TypeError):
continue
message = entry.get("message")
if entry.get("type") != "assistant" or stamp < since or not isinstance(message, dict):
continue
if message.get("id"):
requests.add(str(message["id"]))
for skill, use in skill_uses(message):
invocations.setdefault(skill, set()).add(use)
return len(requests), {skill: len(uses) for skill, uses in invocations.items()}
live: Counter[str] = Counter()
for message in entries(path, since):
request = str(message.get("id") or "")
if request and request not in requests:
requests.add(request)
resident.update(live)
for skill, use in skill_uses(message):
seen = invocations.setdefault(skill, set())
if use not in seen:
seen.add(use)
live[skill] += 1
return len(requests), {skill: len(uses) for skill, uses in invocations.items()}, resident
def weigh(root: Path, listing_delta: int, body: dict[str, int], new: set[str], base_skills: list[str]) -> dict:
requests, invocations = usage(root, datetime.datetime.now(datetime.UTC).timestamp() - DAYS * 86400)
per_day = {skill: count / DAYS for skill, count in invocations.items()}
rates, assumed = {}, {}
requests, invocations, resident = usage(root, datetime.datetime.now(datetime.UTC).timestamp() - DAYS * 86400)
per_day = defaultdict(float, {skill: count / DAYS for skill, count in invocations.items()})
held = defaultdict(float, {skill: turns / DAYS for skill, turns in resident.items()})
rates, kept, assumed = {}, {}, {}
for skill in body:
if skill in new:
peers = [peer for peer in base_skills if category(peer) == category(skill)]
assumed[skill] = category(skill) if peers else ALL_SKILLS
pool = [per_day.get(peer, 0.0) for peer in peers or base_skills]
rates[skill] = statistics.median(pool) if pool else 0.0
pool = peers or base_skills
rates[skill] = statistics.median([per_day[peer] for peer in pool]) if pool else 0.0
kept[skill] = statistics.median([held[peer] for peer in pool]) if pool else 0.0
else:
rates[skill] = per_day.get(skill, 0.0)
rates[skill] = per_day[skill]
kept[skill] = held[skill]
listing = listing_delta * TOKENS_PER_WORD * (requests / DAYS) * CACHE_FACTOR
return {
"cost": listing + sum(delta * rates[skill] for skill, delta in body.items()),
"cost": listing + sum(delta * (rates[skill] + CACHE_FACTOR * kept[skill]) for skill, delta in body.items()),
"requests_per_day": requests / DAYS,
"invocations_per_day": rates,
"resident_turns_per_day": kept,
"median_for_new": assumed,
}
def value_line(weighed: dict, value: float | None, evidence: str | None) -> tuple[str, str]:
notes = [
f"{skill} new: {group} median {figure(weighed['invocations_per_day'][skill])} invocations/day"
f"{skill} new: {group} median {figure(weighed['invocations_per_day'][skill])} invocations/day, "
f"{figure(weighed['resident_turns_per_day'][skill])} resident turns/day"
for skill, group in sorted(weighed["median_for_new"].items())
]
cost = weighed["cost"]
@@ -331,6 +367,7 @@ def check(argv: list[str]) -> int:
"evidence": args.evidence,
"requests_per_day": round(weighed["requests_per_day"], 2),
"invocations_per_day": {s: round(r, 2) for s, r in weighed["invocations_per_day"].items()},
"resident_turns_per_day": {s: round(r, 2) for s, r in weighed["resident_turns_per_day"].items()},
"median_for_new": weighed["median_for_new"],
},
}))
+2 -63
View File
@@ -17,70 +17,9 @@ request and stop, or, ordered here by the operator, first
`${CLAUDE_SKILL_DIR}/../kvb-org-radio/scripts/radio.py post --to <chair>
"<plan>"`.
## Step 1: inventory
## Steps 1 to 4: inventory, owners, classes, proposal
Read-only, parallel, sized, only in the agent write scope (Claude Code:
`sandbox.filesystem.allowWrite`) and session scratch dirs, never another
user's files. Prefer project cleanup and teardown targets; obey raw-command
bans.
- **/tmp**: `pkgmgr gc --report --docker --root /tmp --root <write dir>`,
else `du -sh` per own entry; never system entries (`.*-unix`,
`systemd-private-*`, `ssh-*`, `tmux-*`, sockets).
- **Docker**, **worktrees**: read `docker.md` (its `⚠ Volumes` checklist
joins Step 4) and `worktrees.md` here when present.
- **Agent dirs**: pip, npm, pre-commit, act, gh, buildx caches; Ansible and
build temp dirs.
- **Council root** (`~/.local/state/agents-shared`): `archive/` only.
- **Register**: `council.py list --long`; `rasci.py artifacts`
(`kvb-org-artefacts`, `--ended` for candidates).
- **Queue**: `${CLAUDE_SKILL_DIR}/../kvb-org-queue-cleanup/scripts/cleanup.py
request`; go on meanwhile.
- **Live use**: `fuser` (exit 1 or 123: free), `lsof`, `ps -o
pid,etime,comm`; never full command lines or environments (credentials).
Sandboxed, an empty result proves nothing.
- **Archive duties**: memory or project rules requiring a copy before
deletion (logs, reports, generated maps).
## Step 2: ask the owners
Owner: register worker, path session id, worktree or compose project,
container label, open handle, else `unknown`. Once per local peer owning
unregistered candidates, `radio.py send call --to <peer>` the `ask.md` text;
go on meanwhile.
## Step 3: classify
First match wins: claimed by a peer, `keep`; in a register task not `done`,
`stopped` or `stale` (a branch merged into the default makes it
`stale`; an ended task's items are its worker's candidates), `keep (task <task>)`; a
running deploy, gate, test, swarm or build, `keep (running)`; a silent peer's,
`keep (no answer from <peer>)`, silence is not consent; sandboxed, touched
within 24 hours (`find -mmin -1440`; bfs fails on `-newermt`) and neither
owner-released nor operator-picked, `keep (recent, possibly in use)`; archive
duty, `archive first`, never a plain delete. Else a candidate:
- **low**, cheap to regenerate: stopped containers without volumes, dangling
images, unused networks, ended sessions' scratch and logs.
- **medium**, slow, rate-limited or networked to regenerate: unused tagged
images, build and package caches, clean merged worktrees.
- **high**, data loss or a broken run: volumes, stopped containers with
volumes, running unclaimed containers or stacks, dirty or unpushed
worktrees, unread reports, anything `unknown`.
## Step 4: propose
One table, biggest first: area, item, size, owner, risk, proposal, what
breaks if wrong. Below it reclaimable size per risk and every `keep` by
reason. Then `AskUserQuestion`:
1. `multiSelect: true`, up to four questions: `/tmp`, `Docker`, `Other
dirs`, `High risk` (every high-risk candidate); each option names size,
risk and worst case. Over four options, group by owner or risk or call
again; merge single items into one question: one-option questions fail.
Only ticks select, not an empty `Other` answer; nothing preselected.
2. `multiSelect: false`: `Execute: <n> items, <size> (Recommended)` naming
the ticked items' worst case, `Revise the selection` (repeats 1), `Abort`.
Read `plan.md` here and follow it to its `Execute` pick.
## Step 5: execute
+66
View File
@@ -0,0 +1,66 @@
# Plan: inventory, owners, classes, proposal
## Step 1: inventory
Read-only, parallel, sized, only in the agent write scope (Claude Code:
`sandbox.filesystem.allowWrite`) and session scratch dirs, never another
user's files. Prefer project cleanup and teardown targets; obey raw-command
bans.
- **/tmp**: `pkgmgr gc --report --docker --root /tmp --root <write dir>`,
else `du -sh` per own entry; never system entries (`.*-unix`,
`systemd-private-*`, `ssh-*`, `tmux-*`, sockets).
- **Docker**, **worktrees**: read `docker.md` (its `⚠ Volumes` checklist
joins Step 4) and `worktrees.md` here when present.
- **Agent dirs**: pip, npm, pre-commit, act, gh, buildx caches; Ansible and
build temp dirs.
- **Council root** (`~/.local/state/agents-shared`): `archive/` only.
- **Register**: `council.py list --long`; `rasci.py artifacts`
(`kvb-org-artefacts`, `--ended` for candidates).
- **Queue**: `${CLAUDE_SKILL_DIR}/../kvb-org-queue-cleanup/scripts/cleanup.py
request`; go on meanwhile.
- **Live use**: `fuser` (exit 1 or 123: free), `lsof`, `ps -o
pid,etime,comm`; never full command lines or environments (credentials).
Sandboxed, an empty result proves nothing.
- **Archive duties**: memory or project rules requiring a copy before
deletion (logs, reports, generated maps).
## Step 2: ask the owners
Owner: register worker, path session id, worktree or compose project,
container label, open handle, else `unknown`. Once per local peer owning
unregistered candidates, `radio.py send call --to <peer>` the `ask.md` text;
go on meanwhile.
## Step 3: classify
First match wins: claimed by a peer, `keep`; in a register task not `done`,
`stopped` or `stale` (a branch merged into the default makes it
`stale`; an ended task's items are its worker's candidates), `keep (task <task>)`; a
running deploy, gate, test, swarm or build, `keep (running)`; a silent peer's,
`keep (no answer from <peer>)`, silence is not consent; sandboxed, touched
within 24 hours (`find -mmin -1440`; bfs fails on `-newermt`) and neither
owner-released nor operator-picked, `keep (recent, possibly in use)`; archive
duty, `archive first`, never a plain delete. Else a candidate:
- **low**, cheap to regenerate: stopped containers without volumes, dangling
images, unused networks, ended sessions' scratch and logs.
- **medium**, slow, rate-limited or networked to regenerate: unused tagged
images, build and package caches, clean merged worktrees.
- **high**, data loss or a broken run: volumes, stopped containers with
volumes, running unclaimed containers or stacks, dirty or unpushed
worktrees, unread reports, anything `unknown`.
## Step 4: propose
One table, biggest first: area, item, size, owner, risk, proposal, what
breaks if wrong. Below it reclaimable size per risk and every `keep` by
reason. Then `AskUserQuestion`:
1. `multiSelect: true`, up to four questions: `/tmp`, `Docker`, `Other
dirs`, `High risk` (every high-risk candidate); each option names size,
risk and worst case. Over four options, group by owner or risk or call
again; merge single items into one question: one-option questions fail.
Only ticks select, not an empty `Other` answer; nothing preselected.
2. `multiSelect: false`: `Execute: <n> items, <size> (Recommended)` naming
the ticked items' worst case, `Revise the selection` (repeats 1), `Abort`.
+4 -4
View File
@@ -21,10 +21,10 @@ coach.py --profile <same> show [--json]
Areas: `psyche`, `body`, `sleep`, `food`, `tasks`, `delegate`. Every coaching
skill beyond this one is named `kvb-coach-<name>`.
`hooks/reminder.sh` triggers the short form at the first prompt of every full
hour, once across all agents (stamped in `~/.claude/.coach-hours/`): at most
three lines, the most urgent area first, then the agent continues with the
operator's request.
`hooks/reminder.sh` prints one fixed tip at the first prompt of every full
hour, once across all agents (stamped in `~/.claude/.coach-hours/`), chosen
by the hour: sleep late, food at meal times, else moving. The agent passes
it on in one line without loading this skill.
## Step 1: read the situation
+7 -5
View File
@@ -9,8 +9,10 @@ mkdir -p "${hours}"
mkdir "${hours}/${hour}" 2>/dev/null || exit 0
find "${hours}" -mindepth 1 -maxdepth 1 -type d ! -name "${hour}" -exec rmdir {} +
cat <<'HINT'
COACH HOUR: a new full hour began. Before answering the operator's request,
run the kvb-coach skill in its short form: at most three lines, the most
urgent area first, then continue with the request.
HINT
case "${hour##*T}" in
22 | 23 | 0[0-5]) tip="😴 Late: wind down, screens off soon, seven to nine hours of sleep." ;;
1[2-3] | 1[8-9]) tip="🥗 Meal time: a real meal with vegetables and protein, and a glass of water." ;;
*) tip="🚶 Stand up, stretch neck, hips and back for two minutes, and drink a glass of water." ;;
esac
printf 'COACH HOUR: open your answer with this one line for the operator, word for word, then continue with the request; load no skill for it:\n%s (More: /kvb-coach)\n' "${tip}"
+14 -2
View File
@@ -2,7 +2,8 @@
"""Report how much context agent sessions re-read per turn, from Claude Code's session transcripts.
Args:
--root: transcripts root, e.g. ~/.claude/projects (every */*.jsonl below).
--root: transcripts root, e.g. ~/.claude/projects (every session transcript
*/*.jsonl and every subagent transcript */*/subagents/*.jsonl below).
--days: how many days back, one row per day, newest last.
--threshold: a context size in tokens; per threshold the report adds the
share of turns above it and the share of context tokens above it.
@@ -34,10 +35,21 @@ def fail(message: str) -> None:
sys.exit(2)
def transcripts(root: Path) -> list[Path]:
"""Args:
root: the transcripts root, e.g. ~/.claude/projects.
Returns:
Every session transcript <project>/<session>.jsonl and every subagent
transcript <project>/<session>/subagents/<agent>.jsonl below it.
"""
return [*root.glob("*/*.jsonl"), *root.glob("*/*/subagents/*.jsonl")]
def scan(root: Path, since: float) -> tuple[dict[str, tuple[str, int]], dict[str, int]]:
turns: dict[str, tuple[str, int]] = {}
compactions: dict[str, int] = {}
for path in root.glob("*/*.jsonl"):
for path in transcripts(root):
if path.stat().st_mtime < since:
continue
with path.open(encoding="utf-8", errors="ignore") as handle: