feat(install): install TokenEconomizer by default at a pinned, patched commit (021)
make install clones microsoft/TokenEconomizer at the commit in plugins-lock.json, applies patches/tokeneconomizer.patch (child CLI under PYTHONSAFEPATH, temp files opened with O_EXCL|O_NOFOLLOW; upstream issue microsoft/TokenEconomizer#260), installs the plugin for Claude Code and, with codex on the PATH, for Codex, and sets toolprune=propose with a protect list and microskills=envfacts globally. A dirty clone stays untouched and fails the install; a host without git or python3 skips the step. Requirement 021, README and six install tests; the council-root install test stubs the step. 🎯 Fix: 91 % 💥 Break: 4 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 87 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e522e94b16
commit
41cdf0c40b
10 files changed
+429
-2
No files matched your search
@@ -527,6 +527,7 @@ class TestInstall(Settings):
|
||||
for part in ("scripts/install.sh", "scripts/orchestrator.sh", "skills/kvb-org-organize/scripts/councilroot.py", "skills/kvb-org-organize/scripts/nofollow.py"):
|
||||
(self.repo / part).parent.mkdir(parents=True, exist_ok=True)
|
||||
(self.repo / part).write_bytes((REPO_ROOT / part).read_bytes())
|
||||
(self.repo / "scripts" / "tokeneconomizer.sh").write_text("exit 0\n", encoding="utf-8")
|
||||
stubs = self.base / "bin"
|
||||
stubs.mkdir()
|
||||
(stubs / "npx").write_text(NPX_STUB, encoding="utf-8")
|
||||
|
||||
+11
-1
@@ -24,7 +24,10 @@ SHIPPED = ORGANIZE / "config.default.json"
|
||||
DEFAULT = json.loads(SHIPPED.read_text(encoding="utf-8"))
|
||||
ROW_MEMORY = DEFAULT["memory_limits"]
|
||||
HOME_TOOLS = ("bash", "cp", "dirname", "find", "install", "ln", "mkdir", "python3", "rm")
|
||||
COUNCIL_PARTS = ("scripts/orchestrator.sh", "skills/kvb-org-organize/scripts/councilroot.py", "skills/kvb-org-organize/scripts/nofollow.py")
|
||||
COUNCIL_PARTS = (
|
||||
"scripts/orchestrator.sh", "scripts/tokeneconomizer.sh", "plugins-lock.json",
|
||||
"skills/kvb-org-organize/scripts/councilroot.py", "skills/kvb-org-organize/scripts/nofollow.py",
|
||||
)
|
||||
AGENT_ROOTS = {"sandbox": {"filesystem": {"allowWrite": ["~/.local/state/agents-shared", "~/.local/state/agents-release/**"]}}}
|
||||
CGROUP_KILL = """
|
||||
case "$*" in *try-restart*kvb-code-graph*)
|
||||
@@ -209,6 +212,13 @@ class TestInstall(unittest.TestCase):
|
||||
self.assertFalse([call for call in calls if "kvb-code-graph" in call])
|
||||
self.assertIn("graphs keep their watch only while a session runs", done.stderr)
|
||||
|
||||
def test_the_home_install_runs_the_tokeneconomizer_step_and_a_host_without_git_skips_it(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
done, _, _ = self.home_install(Path(tmp), binary=True)
|
||||
self.assertEqual(done.returncode, 0, done.stderr)
|
||||
self.assertIn("skills: git or python3 not found; skipped TokenEconomizer.", done.stderr)
|
||||
self.assertFalse((Path(tmp) / "home" / "Repositories").exists())
|
||||
|
||||
def test_reinstall_empties_the_skill_directories_in_place(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
"""Exercise scripts/tokeneconomizer.sh: the pinned clone, both agents and the global settings."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
SCRIPT = REPO_ROOT / "scripts" / "tokeneconomizer.sh"
|
||||
TOOLS = ("git", "python3", "mkdir", "dirname", "cat")
|
||||
GIT_ENV = {
|
||||
"GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "test@example.invalid", "GIT_COMMITTER_NAME": "Test",
|
||||
"GIT_COMMITTER_EMAIL": "test@example.invalid", "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_NOSYSTEM": "1",
|
||||
}
|
||||
PROTECT = (
|
||||
"AskUserQuestion,Artifact,Skill,Agent,ToolSearch,mcp__kvb-orchestrator__*,"
|
||||
"mcp__codebase-memory-mcp__*,mcp__plugin_codebase-memory_*"
|
||||
)
|
||||
TE = '#!/bin/sh\necho "$*" >> "$CALLS/te.log"\n'
|
||||
UNSAFE = 'env = {"PYTHONUTF8": "1"}\n'
|
||||
SAFE = 'env = {"PYTHONUTF8": "1", "PYTHONSAFEPATH": "1"}\n'
|
||||
CLAUDE = '#!/bin/sh\necho "$*" >> "$CALLS/claude.log"\n'
|
||||
CODEX = """#!/bin/sh
|
||||
echo "$*" >> "$CALLS/codex.log"
|
||||
case "$*" in
|
||||
"plugin marketplace list") cat "$CALLS/codex.marketplaces" 2>/dev/null ;;
|
||||
"plugin marketplace add "*) echo "$4" >> "$CALLS/codex.marketplaces" ;;
|
||||
esac
|
||||
exit 0
|
||||
"""
|
||||
|
||||
|
||||
def git(cwd: Path, *args: str) -> str:
|
||||
done = subprocess.run(["git", *args], cwd=cwd, env={**os.environ, **GIT_ENV}, capture_output=True, text=True, check=True)
|
||||
return done.stdout.strip()
|
||||
|
||||
|
||||
def lines(path: Path) -> list[str]:
|
||||
return path.read_text(encoding="utf-8").splitlines() if path.is_file() else []
|
||||
|
||||
|
||||
class TestTokenEconomizerInstall(unittest.TestCase):
|
||||
def setUp(self):
|
||||
work = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(work.cleanup)
|
||||
self.root = Path(work.name)
|
||||
upstream = self.root / "upstream"
|
||||
(upstream / "plugin" / "scripts").mkdir(parents=True)
|
||||
git(upstream, "init", "-q", "-b", "main")
|
||||
te = upstream / "plugin" / "scripts" / "te"
|
||||
te.write_text(TE, encoding="utf-8")
|
||||
te.chmod(0o755)
|
||||
(upstream / "context.py").write_text(UNSAFE, encoding="utf-8")
|
||||
(upstream / "notes.txt").write_text("notes\n", encoding="utf-8")
|
||||
git(upstream, "add", "-A")
|
||||
git(upstream, "commit", "-q", "-m", "pinned")
|
||||
self.pin = git(upstream, "rev-parse", "HEAD")
|
||||
(self.root / "patches").mkdir()
|
||||
(upstream / "context.py").write_text(SAFE, encoding="utf-8")
|
||||
git(upstream, "diff", f"--output={self.root / 'patches' / 'tokeneconomizer.patch'}")
|
||||
git(upstream, "checkout", "-q", "--", "context.py")
|
||||
(upstream / "later.txt").write_text("later\n", encoding="utf-8")
|
||||
git(upstream, "add", "-A")
|
||||
git(upstream, "commit", "-q", "-m", "later")
|
||||
self.later = git(upstream, "rev-parse", "HEAD")
|
||||
self.upstream = upstream
|
||||
self.lock = self.root / "plugins-lock.json"
|
||||
self.lock.write_text(json.dumps({"tokeneconomizer": {
|
||||
"source": str(upstream), "commit": self.pin, "patch": "patches/tokeneconomizer.patch",
|
||||
}}), encoding="utf-8")
|
||||
self.clone = self.root / "home" / "Repositories" / "github.com" / "microsoft" / "TokenEconomizer"
|
||||
self.calls = self.root / "calls"
|
||||
self.calls.mkdir()
|
||||
|
||||
def run_script(self, agents: tuple[str, ...]) -> subprocess.CompletedProcess:
|
||||
stubs = self.root / f"bin-{'-'.join(agents) or 'none'}"
|
||||
if not stubs.exists():
|
||||
stubs.mkdir()
|
||||
for tool in TOOLS:
|
||||
(stubs / tool).symlink_to(shutil.which(tool))
|
||||
for name, body in (("claude", CLAUDE), ("codex", CODEX)):
|
||||
if name in agents:
|
||||
(stubs / name).write_text(body, encoding="utf-8")
|
||||
(stubs / name).chmod(0o755)
|
||||
env = {"PATH": str(stubs), "HOME": str(self.root / "home"), "CALLS": str(self.calls), **GIT_ENV}
|
||||
return subprocess.run(["/bin/bash", str(SCRIPT), str(self.lock), str(self.clone)], env=env, capture_output=True, text=True, check=False)
|
||||
|
||||
def test_a_fresh_host_gets_the_clone_at_the_pin_and_the_plugin_for_both_agents_with_the_chosen_settings(self):
|
||||
done = self.run_script(("claude", "codex"))
|
||||
self.assertEqual(done.returncode, 0, done.stderr)
|
||||
self.assertEqual(git(self.clone, "rev-parse", "HEAD"), self.pin)
|
||||
self.assertEqual(lines(self.calls / "claude.log"), [f"plugin install tokeneconomizer --marketplace {self.clone} --scope user"])
|
||||
self.assertEqual(lines(self.calls / "codex.log"), [
|
||||
"plugin marketplace list", f"plugin marketplace add {self.clone}", "plugin add tokeneconomizer@tokeneconomizer",
|
||||
])
|
||||
self.assertEqual(lines(self.calls / "te.log"), [f"config set toolprune=propose microskills=envfacts toolprune_protect={PROTECT} --global"])
|
||||
self.assertIn("run /hooks, choose Trust all", done.stdout)
|
||||
self.assertEqual(self.context(), SAFE)
|
||||
|
||||
def context(self) -> str:
|
||||
return (self.clone / "context.py").read_text(encoding="utf-8")
|
||||
|
||||
def test_a_second_run_keeps_the_pin_and_adds_the_codex_marketplace_only_once(self):
|
||||
self.assertEqual(self.run_script(("claude", "codex")).returncode, 0)
|
||||
done = self.run_script(("claude", "codex"))
|
||||
self.assertEqual(done.returncode, 0, done.stderr)
|
||||
self.assertEqual(git(self.clone, "rev-parse", "HEAD"), self.pin)
|
||||
self.assertEqual([call for call in lines(self.calls / "codex.log") if call.startswith("plugin marketplace add")], [f"plugin marketplace add {self.clone}"])
|
||||
self.assertEqual(len(lines(self.calls / "codex.log")), 5)
|
||||
self.assertEqual(self.context(), SAFE)
|
||||
|
||||
def test_a_clean_clone_on_another_commit_moves_to_the_pin(self):
|
||||
self.clone.parent.mkdir(parents=True)
|
||||
git(self.root, "clone", "-q", str(self.upstream), str(self.clone))
|
||||
self.assertEqual(git(self.clone, "rev-parse", "HEAD"), self.later)
|
||||
done = self.run_script(("claude",))
|
||||
self.assertEqual(done.returncode, 0, done.stderr)
|
||||
self.assertEqual(git(self.clone, "rev-parse", "HEAD"), self.pin)
|
||||
self.assertEqual(self.context(), SAFE)
|
||||
|
||||
def test_a_patched_clone_with_a_foreign_edit_keeps_both_and_the_step_fails(self):
|
||||
self.assertEqual(self.run_script(("claude",)).returncode, 0)
|
||||
(self.clone / "notes.txt").write_text("operator notes\n", encoding="utf-8")
|
||||
done = self.run_script(("claude",))
|
||||
self.assertEqual(done.returncode, 1)
|
||||
self.assertIn("has local changes; TokenEconomizer stays off its pin", done.stderr)
|
||||
self.assertEqual((self.context(), (self.clone / "notes.txt").read_text(encoding="utf-8")), (SAFE, "operator notes\n"))
|
||||
self.assertEqual(len(lines(self.calls / "claude.log")), 1)
|
||||
|
||||
def test_a_clone_with_local_changes_stays_where_it_is_and_the_step_fails(self):
|
||||
self.clone.parent.mkdir(parents=True)
|
||||
git(self.root, "clone", "-q", str(self.upstream), str(self.clone))
|
||||
(self.clone / "later.txt").write_text("edited\n", encoding="utf-8")
|
||||
done = self.run_script(("claude", "codex"))
|
||||
self.assertEqual(done.returncode, 1)
|
||||
self.assertIn("has local changes; TokenEconomizer stays off its pin", done.stderr)
|
||||
self.assertEqual(git(self.clone, "rev-parse", "HEAD"), self.later)
|
||||
self.assertEqual(self.context(), UNSAFE)
|
||||
self.assertEqual((lines(self.calls / "claude.log"), lines(self.calls / "codex.log"), lines(self.calls / "te.log")), ([], [], []))
|
||||
|
||||
def test_a_host_without_codex_installs_for_claude_only_and_says_so(self):
|
||||
done = self.run_script(("claude",))
|
||||
self.assertEqual(done.returncode, 0, done.stderr)
|
||||
self.assertEqual(len(lines(self.calls / "claude.log")), 1)
|
||||
self.assertEqual(lines(self.calls / "codex.log"), [])
|
||||
self.assertIn("codex not found; TokenEconomizer skipped for Codex.", done.stdout)
|
||||
self.assertEqual(len(lines(self.calls / "te.log")), 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user