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>
68 lines
2.7 KiB
Python
68 lines
2.7 KiB
Python
"""Exercise context.py: one count per message id, days, compactions and the threshold shares."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
SCRIPT = REPO_ROOT / "skills" / "kvb-skill-silence" / "scripts" / "context.py"
|
|
|
|
|
|
def turn(day: str, mid: str, cached: int) -> str:
|
|
usage = {"input_tokens": 100, "cache_read_input_tokens": cached, "cache_creation_input_tokens": 0}
|
|
return json.dumps({"timestamp": f"{day}T10:00:00Z", "message": {"id": mid, "usage": usage}})
|
|
|
|
|
|
class TestContext(unittest.TestCase):
|
|
def setUp(self):
|
|
tmp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(tmp.cleanup)
|
|
self.root = Path(tmp.name)
|
|
today = datetime.datetime.now(datetime.UTC).date()
|
|
self.today, self.yesterday = today.isoformat(), (today - datetime.timedelta(days=1)).isoformat()
|
|
lines = [
|
|
turn(self.today, "m1", 400_000),
|
|
turn(self.today, "m1", 400_000),
|
|
turn(self.today, "m2", 49_900),
|
|
turn(self.yesterday, "m3", 599_900),
|
|
json.dumps({"timestamp": f"{self.today}T11:00:00Z", "type": "system", "subtype": "compact_boundary"}),
|
|
"not json",
|
|
]
|
|
(self.root / "project").mkdir()
|
|
(self.root / "project" / "session.jsonl").write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
subagents = self.root / "project" / "session" / "subagents"
|
|
subagents.mkdir(parents=True)
|
|
(subagents / "agent-a1.jsonl").write_text(turn(self.today, "s1", 0) + "\n", encoding="utf-8")
|
|
|
|
def _run(self, *args: str) -> subprocess.CompletedProcess:
|
|
return subprocess.run(
|
|
[sys.executable, str(SCRIPT), "--root", str(self.root), *args], capture_output=True, text=True, check=False
|
|
)
|
|
|
|
def test_days_turns_compactions_and_shares(self):
|
|
result = self._run("--days", "2", "--threshold", "300000", "--json")
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
yesterday, today = json.loads(result.stdout)
|
|
self.assertEqual(
|
|
(yesterday["day"], yesterday["turns"], yesterday["compactions"], yesterday["over"]["300000"]),
|
|
(self.yesterday, 1, 0, {"turns": 100, "tokens": 50}),
|
|
)
|
|
self.assertEqual(
|
|
(today["day"], today["turns"], today["tokens"], today["compactions"], today["over"]["300000"]),
|
|
(self.today, 3, 450_200, 1, {"turns": 33, "tokens": 22}),
|
|
)
|
|
|
|
def test_usage_errors_exit_2(self):
|
|
self.assertEqual(self._run("--days", "0", "--threshold", "1").returncode, 2)
|
|
self.assertEqual(self._run("--days", "1").returncode, 2)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|