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
@@ -464,7 +464,7 @@ Copy the skills into a specific project (`<repo>/.agents/skills` and `<repo>/.cl
|
||||
make project TARGET=/path/to/repo
|
||||
```
|
||||
|
||||
Both commands also patch the target's `.claude/settings.local.json` — the machine-local, untracked file, so a project's reviewed `.claude/settings.json` is never rewritten by an install: the caveman and ponytail plugins are enabled so their modes auto-activate on session start, and two reminder hooks are registered — `kvb-dream` points each agent at `/kvb-dream` at most once every twelve hours (stamped per session in `~/.claude/.dream-stamps/`), and `kvb-coach` runs its short form at the first prompt of every full hour, once across all agents (stamped in `~/.claude/.coach-hours/`); the global install also enables the `kvb-dream-notify.timer` systemd user timer, which recommends `/kvb-dream` in a GNOME notification daily at 17:00 local time and catches up once after a missed run, and the `kvb-org-queue-cleanup.timer` and `kvb-org-queue-cleanup.path` user units, which start a `kvb-org-queue-cleanup` round at 00:00 and 12:00 local time without catch-up and whenever an agent runs `cleanup.py request`. A PreToolUse gate from the `kvb-org-organize` skill blocks edits to files in a git worktree until the session holds a claim (`skills/kvb-org-organize/scripts/council.py`), and its resource watch (`skills/kvb-org-organize/scripts/watch.py`) measures disk, Docker and caches, cleans up low-risk data and blocks new heavy jobs before the disk runs out; it stays inactive until `~/.claude/agents-council/config.json` exists, and because its hooks run that config's commands outside the sandbox, the install adds `Edit(~/.claude/agents-council/**)` to `permissions.deny`, which also covers Write in every permission mode; `Edit(~/.local/state/agents-council/**)` joins it, so the state the skills' scripts keep there changes only through those scripts. The global install writes both rules into `~/.claude/settings.json` too (`scripts/council-deny.js`), the only user-wide settings file, so they bind sessions in every repository. The `kvb-org-radio` skill registers five hooks: its prompt hook and a PostToolBatch hook deliver the immediate radio (`call`, `broadcast`, `security`, `pan-pan`, `mayday`) mid-turn, a Stop hook delivers the post (`post`, `newsletter` and call receipts) when the turn ends, an `asyncRewake` Stop hook wakes an idle session for the radio, and a PreToolUse hook refuses every writing action while a mayday holds, except those of its sender and of the agents that join it with `radio.py assist`. The PreToolUse hook of the `kvb-org-queue` skill sends heavy commands through `queue.py hold` once the config sets `queue_enforce`, and holds a held command to the settings' Bash deny and ask rules, which see only `queue.py hold`. The global install also installs the MEDUSA security scanner with pipx for `kvb-check-security`, unless `medusa` is already on the PATH. Two hooks of the `kvb-org-perform` skill (`skills/kvb-org-perform/scripts/log.py`) log every Agent spawn with its task class, model and effort, and store the quality feedback a sandboxed session prints, in `~/.local/state/agents-council/subagent-model.jsonl`. Every hook runs `/usr/bin/python3 -I`, so neither the `python3` of a sandbox-writable virtualenv on the `PATH` nor `PYTHONPATH` reaches the hooks, which run outside the sandbox; older bare `python3` hook entries are rewritten. The reminder hooks run `/usr/bin/bash` and pin `PATH=/usr/bin:/bin`, and the council scripts keep only the `PATH` directories this uid cannot write (`skills/kvb-org-organize/scripts/hookpath.py`), so no binary an agent plants on the inherited `PATH` runs outside the sandbox. Existing settings are preserved.
|
||||
Both commands also patch the target's `.claude/settings.local.json` — the machine-local, untracked file, so a project's reviewed `.claude/settings.json` is never rewritten by an install: the caveman and ponytail plugins are enabled so their modes auto-activate on session start, and two reminder hooks are registered — `kvb-dream` points each agent at `/kvb-dream` at most once every twelve hours (stamped per session in `~/.claude/.dream-stamps/`), and `kvb-coach` runs its short form at the first prompt of every full hour, once across all agents (stamped in `~/.claude/.coach-hours/`); the global install also enables the `kvb-dream-notify.timer` systemd user timer, which recommends `/kvb-dream` in a GNOME notification daily at 17:00 local time and catches up once after a missed run, and the `kvb-org-queue-cleanup.timer` and `kvb-org-queue-cleanup.path` user units, which start a `kvb-org-queue-cleanup` round at 00:00 and 12:00 local time without catch-up and whenever an agent runs `cleanup.py request`. A PreToolUse gate from the `kvb-org-organize` skill blocks edits to files in a git worktree until the session holds a claim (`skills/kvb-org-organize/scripts/council.py`), and its resource watch (`skills/kvb-org-organize/scripts/watch.py`) measures disk, Docker and caches, cleans up low-risk data and blocks new heavy jobs before the disk runs out; it stays inactive until `~/.claude/agents-council/config.json` exists, and because its hooks run that config's commands outside the sandbox, the install adds `Edit(~/.claude/agents-council/**)` to `permissions.deny`, which also covers Write in every permission mode; `Edit(~/.local/state/agents-council/**)` joins it, so the state the skills' scripts keep there changes only through those scripts. The global install writes both rules into `~/.claude/settings.json` too (`scripts/council-deny.js`), the only user-wide settings file, so they bind sessions in every repository. The `kvb-org-radio` skill registers five hooks: its prompt hook and a PostToolBatch hook deliver the immediate radio (`call`, `broadcast`, `security`, `pan-pan`, `mayday`) mid-turn, a Stop hook delivers the post (`post`, `newsletter` and call receipts) when the turn ends, an `asyncRewake` Stop hook wakes an idle session for the radio, and a PreToolUse hook refuses every writing action while a mayday holds, except those of its sender and of the agents that join it with `radio.py assist`. The PreToolUse hook of the `kvb-org-queue` skill sends heavy commands through `queue.py hold` once the config sets `queue_enforce`, and holds a held command to the settings' Bash deny and ask rules, which see only `queue.py hold`. The global install also installs the MEDUSA security scanner with pipx for `kvb-check-security`, unless `medusa` is already on the PATH. It clones microsoft/TokenEconomizer at the commit pinned in `plugins-lock.json` to `~/Repositories/github.com/microsoft/TokenEconomizer` and installs it for Claude Code and, when `codex` is on the PATH, for Codex, which runs its hooks once they are approved with `/hooks`; tool pruning only proposes deny lists and never drops `AskUserQuestion`, `Artifact`, `Skill`, `Agent`, `ToolSearch` or the hub and code graph MCP tools, micro-skills learn only from repeated failures, and the loop guard stays on (requirement 021). Two hooks of the `kvb-org-perform` skill (`skills/kvb-org-perform/scripts/log.py`) log every Agent spawn with its task class, model and effort, and store the quality feedback a sandboxed session prints, in `~/.local/state/agents-council/subagent-model.jsonl`. Every hook runs `/usr/bin/python3 -I`, so neither the `python3` of a sandbox-writable virtualenv on the `PATH` nor `PYTHONPATH` reaches the hooks, which run outside the sandbox; older bare `python3` hook entries are rewritten. The reminder hooks run `/usr/bin/bash` and pin `PATH=/usr/bin:/bin`, and the council scripts keep only the `PATH` directories this uid cannot write (`skills/kvb-org-organize/scripts/hookpath.py`), so no binary an agent plants on the inherited `PATH` runs outside the sandbox. Existing settings are preserved.
|
||||
|
||||
The council root is the one directory every `kvb-org` script, hook and the hub share: the register, radio, queue, claims, operator rows, score journal, gate receipts, security cache and night state, plus `pages/` (the `kvb-git-diff` review pages the hub reads), `approvals/` (the replay stores of `approval.py verify`) and `gates/` (gate logs). `skills/kvb-org-organize/scripts/councilroot.py` alone resolves it: `$AGENTS_COUNCIL_ROOT`, else `$XDG_STATE_HOME/agents-shared`, else `~/.local/state/agents-shared`, so it survives a reboot; `councilroot.py root` prints it and `councilroot.py dir <part>` creates and prints a part. It and `agents-release` below are the only state paths a sandbox's `allowWrite` may name. Its sibling `~/.local/state/agents-council` (`councilroot.py durable`) is host-only and never belongs in `allowWrite`: only hooks, the runner and the systemd units write it (`gates.db`, the watch hook's copies of the receipt, parent, acceptance and queue-history journals, `queue-cleanup.json`, `resources/`, `subagent-model.jsonl`), and the hub mounts it read-only; no directory is both agent-writable and host-written except the council root, where every host writer opens files through `nofollow.py`. The `kvb-release` drafts lie in `agents-release/<version>` beside both (`councilroot.py release <version>`). The global install first runs `councilroot.py preflight` and stops before it copies anything unless `~/.claude/settings.json` lists both `agents-shared` and `agents-release` in `sandbox.filesystem.allowWrite`, no `allowWrite` entry reaches `agents-council` in the managed file, its `managed-settings.d` drop-ins, the user or user-local file (the list `councilroot.py` shares with the queue's permission check), in a project `.claude/settings.json` or `.claude/settings.local.json` below the pkgmgr repositories and worktrees directories (directory and file links followed), or in the start directory of any session listed in `~/.claude/projects`, and the move below would succeed; it warns while the agents are not suspended. Fixing those entries is the operator's step. It then builds the skills, stops the hub, the runner, the offices, resources and queue-cleanup units, runs `councilroot.py migrate`, which moves an earlier council root in `/tmp/agents-council` into the council root, copies the skills, runs `migrate --catch-up` for what the old skills wrote meanwhile, and starts the units again; the hub image is built before the container is replaced, and a failed build leaves the hub down and fails the install until `bash scripts/orchestrator.sh install` succeeds. Migrate refuses while a lock there is held or a queue job waits, and, checked under those locks, moves nothing (exit 4) while a file cannot be moved or a whole file both roots hold differs; the install then stops with the earlier root intact. Each file is renamed to a `.migrating` name before it is copied, so a writer that opens the old name meanwhile makes a new file the catch-up moves. An identical file leaves one copy, a `.jsonl` journal gets the rows it lacks (by id, else each exact line as often as the earlier root holds it more often, in `ts` order), links are skipped, and a second run moves nothing and duplicates no row. The catch-up keeps a differing whole file beside its namesake as `<name>.legacy-<hash>` (exit 3); a file that fails after the check stays behind (exit 5). After exit 3 or 5 the install goes on so the new skills read the new root, names every such file and exits 1 at the end. `~/.local/state/agents-council/migrate.started` exists from the first move to the end of a run; when a crash leaves it, the install copies the new skills anyway, names it and exits 1, and the next completed migrate removes it. Because the council root survives reboots, each `kvb-org-queue-cleanup` round first runs `councilroot.py retire`, which moves gate logs and review pages older than 30 days into `archive/<date>/`, and the radio appends the rows it drops past its last 500 to `archive/news-<date>.jsonl`; no reader opens `archive/`, which `kvb-cleanup` offers for deletion. Run the install with every agent suspended (`kvb-org-suspend`) and the queue empty, so nothing writes either root during the move.
|
||||
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
# 021 - TokenEconomizer by Default
|
||||
|
||||
## User Story
|
||||
|
||||
As the operator running many agent sessions across repositories, I want
|
||||
microsoft/TokenEconomizer installed and on by default for Claude Code and
|
||||
Codex in every repository through the skills `make install`, so that sessions
|
||||
stop paying for failing loops and rediscovered repository facts, without any
|
||||
tool silently disappearing from an agent.
|
||||
|
||||
## Decisions (operator-confirmed 2026-11-17; do not re-litigate)
|
||||
|
||||
1. **Source and pin**: microsoft/TokenEconomizer (MIT), pinned to commit
|
||||
`b57d03f81585a18b936a0eaecbd1eb51cdcecf71`. The pin is one constant in
|
||||
the skills repository; raising it is a reviewed change. Rejected: tracking
|
||||
`main` (unreviewed hook code would run in every session outside the
|
||||
sandbox).
|
||||
2. **Supply-chain gate**: a MEDUSA scan of the pinned commit and a
|
||||
kvb-check-legal pass come before the first install; a critical finding
|
||||
stops the rollout. The README badge says Apache 2.0, the LICENSE file says
|
||||
MIT; the LICENSE file governs.
|
||||
3. **Install path**: the skills `make install` (TARGET = HOME) clones it to
|
||||
`~/Repositories/github.com/microsoft/TokenEconomizer` (pkgmgr layout) at
|
||||
the pin, enables it for Claude Code in `~/.claude/settings.local.json`
|
||||
(the caveman/ponytail mechanism of `scripts/enable-plugins.js`) and, when
|
||||
`codex` is on PATH, adds it to Codex (`codex plugin marketplace add`,
|
||||
`codex plugin add`). Rejected: one-off operator rows (not reproducible on
|
||||
another machine).
|
||||
4. **Tool pruning: propose + protect**: `toolprune=propose` globally; deny
|
||||
lists are only proposed, the operator applies one per repository with
|
||||
`/tokeneconomizer:toolprune apply`. `toolprune_protect` names the tools
|
||||
that are rare but critical here: `AskUserQuestion`, `Artifact`, `Skill`,
|
||||
`Agent`, `ToolSearch`, `mcp__kvb-orchestrator__*`,
|
||||
`mcp__codebase-memory-mcp__*`, `mcp__plugin_codebase-memory_*`. Rejected:
|
||||
`auto` (writes deny rules into `.claude/settings.local.json` after 60 idle
|
||||
minutes and could silently drop the hub approval tools), `off`.
|
||||
5. **Micro-skills: envfacts**: facts only from failures that repeat across
|
||||
sessions, no model calls. Rejected: `on` (paid proposer every 3 sessions
|
||||
per repository), `off`.
|
||||
6. **Loop guard: plugin default (on)**: the judge runs through the agent only
|
||||
when a turn shows loop signs; it redirects once and never stops a turn.
|
||||
7. **Global settings** are written with the clone's own launcher
|
||||
(`plugin/scripts/te config set ... --global`), idempotent on every
|
||||
install.
|
||||
8. **Codex hook approval stays manual**: Codex asks once per hook change
|
||||
(`/hooks`, Trust all); the install prints that step in one line.
|
||||
Rejected: editing Codex's trust store by script.
|
||||
9. **Local security patch until upstream fixes it** (operator pick
|
||||
2026-11-17 "1 und 2", after the review in decision 2): the pin carries
|
||||
two holes. `context.py` starts `python -m tokeneconomizer.plugin.cli`
|
||||
with the repository as cwd, so a `json.py` or `tokeneconomizer/`
|
||||
planted there runs outside the sandbox; `write_json` and `_write_text`
|
||||
in `denylist.py` follow a symlink planted at the `.te.tmp` name.
|
||||
`patches/tokeneconomizer.patch` sets `PYTHONSAFEPATH=1` for that child
|
||||
and creates the temporary file with `O_CREAT|O_EXCL|O_NOFOLLOW`; the
|
||||
install applies it on top of the pin, and a clone that differs from pin
|
||||
plus patch is refused, left as found. Both holes go to Microsoft through
|
||||
its private channel (aka.ms/SECURITY.md), never a public issue; the
|
||||
patch is dropped once a fixed upstream commit is pinned. Rejected:
|
||||
parking the rollout until upstream fixes it; installing unpatched.
|
||||
|
||||
## Approach
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
subgraph P["🧭 0 · Preparation"]
|
||||
P1["📋 Write requirement"] --> P2{"✅ Operator approves?"}
|
||||
P2 -- "✏️ corrections" --> P1
|
||||
end
|
||||
P2 -- "🤖 yes" --> S1
|
||||
subgraph S["🛡️ S · Supply-chain gate"]
|
||||
S1["🔎 MEDUSA scan of the pinned commit"] --> S2["⚖️ kvb-check-legal"]
|
||||
S2 --> S3{"🚦 No critical finding?"}
|
||||
S3 -- "🔴 critical" --> SX["⛔ Stop, report to operator"]
|
||||
end
|
||||
S3 -- "🟢 clean" --> A1
|
||||
subgraph A["🛠️ A · make install"]
|
||||
A1["📥 Clone or move to the pin"] --> A2["🔌 Enable for Claude Code"]
|
||||
A2 --> A3["🔌 Add to Codex when present"]
|
||||
A3 --> A4["⚙️ Global settings: propose, protect, envfacts"]
|
||||
A4 --> A5{"🧪 Install tests green?"}
|
||||
A5 -- "🔴 no: fix" --> A1
|
||||
end
|
||||
A5 -- "🟢 yes" --> B1
|
||||
subgraph B["🧪 B · Live check"]
|
||||
B1["🖥️ Operator runs make install"] --> B2["✅ /tokeneconomizer:status in a repo"]
|
||||
B2 --> B3["🤝 Codex /hooks trust"]
|
||||
B3 --> BF{"💬 Operator feedback?"}
|
||||
BF -- "✏️ changes" --> A1
|
||||
end
|
||||
BF -- "👍 fits" --> D1["🏁 Review, commit with operator OK, hand-over"]
|
||||
```
|
||||
|
||||
## Acceptance Criteria
|
||||
|
||||
### Supply chain
|
||||
|
||||
- [ ] A MEDUSA scan of the pinned commit is saved under
|
||||
`.state/evidence/tokeneconomizer/` and reports no critical finding, or the
|
||||
rollout stopped with the finding reported.
|
||||
- [ ] kvb-check-legal names the MIT licence of the pinned commit.
|
||||
- [ ] Under `.state/evidence/tokeneconomizer/`, a probe shows the planted
|
||||
`json.py` running under plain `python -m` and not under
|
||||
`PYTHONSAFEPATH=1`, and the unpatched `write_json` overwriting a
|
||||
symlinked victim while the patched one leaves it untouched.
|
||||
- [ ] The private vulnerability report is drafted for the operator to send.
|
||||
|
||||
### Install
|
||||
|
||||
- [ ] `make install` with an empty clone path leaves
|
||||
`~/Repositories/github.com/microsoft/TokenEconomizer` with `HEAD` equal to
|
||||
the pin.
|
||||
- [ ] A clone at another commit is moved to the pin and patched; a clone
|
||||
with changes beyond the patch is left as found, patch included, with a
|
||||
warning and a non-zero install status.
|
||||
- [ ] `~/.claude/settings.local.json` holds
|
||||
`enabledPlugins["tokeneconomizer@tokeneconomizer"] = true` and the
|
||||
`tokeneconomizer` marketplace pointing at the clone; a second install
|
||||
changes nothing.
|
||||
- [ ] With `codex` on PATH, `codex plugin list` shows tokeneconomizer
|
||||
enabled; without `codex`, the install skips it with one line.
|
||||
- [ ] `te config show --global` (or its equivalent) reports
|
||||
`toolprune=propose`, `microskills=envfacts` and the protect list of
|
||||
decision 4.
|
||||
- [ ] The install prints the Codex `/hooks` approval in one line.
|
||||
|
||||
### Live
|
||||
|
||||
- [ ] `/tokeneconomizer:status` in a fresh Claude Code session in this
|
||||
repository reports the plugin active with the settings above.
|
||||
- [ ] After 10 prompts no file `.claude/settings.local.json` in any
|
||||
repository gained a deny rule written by the plugin.
|
||||
|
||||
### Tests
|
||||
|
||||
- Unit: `tests/test_install.py` covers the settings merge (marketplace,
|
||||
enabled plugin, idempotence) and the pin logic (fresh, moved, dirty)
|
||||
against a local bare repository standing in for GitHub.
|
||||
- Integration: `make install` into a temporary HOME with a fake `codex` and
|
||||
a local clone source asserts the settings file, the clone pin and the
|
||||
global TokenEconomizer settings.
|
||||
- E2E: n/a: needs a signed-in agent; covered by the operator's live check.
|
||||
@@ -22,3 +22,4 @@ skill defines the format.
|
||||
- [016 - Agents Controls in the View Row](016-agents-controls-row.md)
|
||||
- [017 - Legend Toggle](017-legend-toggle.md)
|
||||
- [018 - Gibberlink](018-gibberlink.md)
|
||||
- [021 - TokenEconomizer by default](021-tokeneconomizer-by-default.md)
|
||||
@@ -0,0 +1,48 @@
|
||||
diff --git a/tokeneconomizer/plugin/context.py b/tokeneconomizer/plugin/context.py
|
||||
index 85865d3..431115b 100644
|
||||
--- a/tokeneconomizer/plugin/context.py
|
||||
+++ b/tokeneconomizer/plugin/context.py
|
||||
@@ -81,6 +81,9 @@ class LiveContext:
|
||||
else "")
|
||||
# It reads session logs and the agents' output, which are UTF-8 whatever the locale.
|
||||
env["PYTHONUTF8"] = "1"
|
||||
+ # `-m` with cwd at the repository root would put that root first on sys.path, so a
|
||||
+ # json.py or tokeneconomizer/ planted in the repository would run in this process.
|
||||
+ env["PYTHONSAFEPATH"] = "1"
|
||||
cmd = [sys.executable, "-m", "tokeneconomizer.plugin.cli", *argv,
|
||||
"--host", self.host.name, "--cwd", self.repo.root]
|
||||
self.store.ensure()
|
||||
diff --git a/tokeneconomizer/plugin/denylist.py b/tokeneconomizer/plugin/denylist.py
|
||||
index 003891a..98c77d3 100644
|
||||
--- a/tokeneconomizer/plugin/denylist.py
|
||||
+++ b/tokeneconomizer/plugin/denylist.py
|
||||
@@ -121,10 +121,19 @@ def _guard_target(repo_root, path):
|
||||
"leads out of the repository")
|
||||
|
||||
|
||||
+def _open_tmp(tmp):
|
||||
+ # A planted symlink at the temporary name would carry the write out of the repository:
|
||||
+ # drop whatever is there and create the file new, never following a link.
|
||||
+ if os.path.lexists(tmp):
|
||||
+ os.unlink(tmp)
|
||||
+ flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0)
|
||||
+ return os.fdopen(os.open(tmp, flags, 0o666), "w", encoding="utf-8")
|
||||
+
|
||||
+
|
||||
def write_json(path, obj):
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
tmp = path + ".te.tmp"
|
||||
- with open(tmp, "w", encoding="utf-8") as fh:
|
||||
+ with _open_tmp(tmp) as fh:
|
||||
json.dump(obj, fh, indent=2)
|
||||
fh.write("\n")
|
||||
os.replace(tmp, path)
|
||||
@@ -133,7 +142,7 @@ def write_json(path, obj):
|
||||
def _write_text(path, text):
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
tmp = path + ".te.tmp"
|
||||
- with open(tmp, "w", encoding="utf-8") as fh:
|
||||
+ with _open_tmp(tmp) as fh:
|
||||
fh.write(text)
|
||||
os.replace(tmp, path)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"tokeneconomizer": {
|
||||
"source": "https://github.com/microsoft/TokenEconomizer.git",
|
||||
"commit": "b57d03f81585a18b936a0eaecbd1eb51cdcecf71",
|
||||
"patch": "patches/tokeneconomizer.patch"
|
||||
}
|
||||
}
|
||||
@@ -168,6 +168,10 @@ if [[ "${TARGET}" == "${HOME}" ]]; then
|
||||
else
|
||||
warn "skills: docker not found; skipped the Agent Orchestrator."
|
||||
fi
|
||||
if ! bash "${REPO_ROOT}/scripts/tokeneconomizer.sh" "${REPO_ROOT}/plugins-lock.json" "${HOME}/Repositories/github.com/microsoft/TokenEconomizer"; then
|
||||
warn "skills: TokenEconomizer is not installed on its pin (see above); make install again once that is fixed."
|
||||
status=1
|
||||
fi
|
||||
log "skills: installing or updating the codebase-memory-mcp code graph for every detected agent"
|
||||
if ! curl -fsSL --proto '=https' https://raw.githubusercontent.com/DeusData/codebase-memory-mcp/main/install.sh | bash; then
|
||||
warn "skills: the codebase-memory-mcp installer failed; the code graph keeps its previous setup."
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
# Param: $1 plugins-lock.json, holding tokeneconomizer.source, .commit and .patch (relative to the lock).
|
||||
# Param: $2 the clone directory; created at the pin, or moved to it while clean apart from the patch.
|
||||
set -euo pipefail
|
||||
|
||||
lock=$1
|
||||
clone=$2
|
||||
PROTECT="AskUserQuestion,Artifact,Skill,Agent,ToolSearch,mcp__kvb-orchestrator__*,mcp__codebase-memory-mcp__*,mcp__plugin_codebase-memory_*"
|
||||
|
||||
log() { printf '%s\n' "$*"; }
|
||||
warn() { printf '%s\n' "$*" >&2; }
|
||||
|
||||
if ! command -v git &>/dev/null || ! command -v python3 &>/dev/null; then
|
||||
warn "skills: git or python3 not found; skipped TokenEconomizer."
|
||||
exit 0
|
||||
fi
|
||||
{ read -r source; read -r commit; read -r patch; } < <(python3 -I -c 'import json, os, sys; entry = json.load(open(sys.argv[1]))["tokeneconomizer"]; print(entry["source"], entry["commit"], os.path.join(os.path.dirname(os.path.abspath(sys.argv[1])), entry["patch"]), sep="\n")' "${lock}")
|
||||
|
||||
if [[ ! -e "${clone}" ]]; then
|
||||
mkdir -p "$(dirname -- "${clone}")"
|
||||
git clone --quiet "${source}" "${clone}"
|
||||
fi
|
||||
reversed=0
|
||||
if [[ -n "$(git -C "${clone}" status --porcelain)" ]] && git -C "${clone}" apply --reverse --check "${patch}" 2>/dev/null; then
|
||||
git -C "${clone}" apply --reverse "${patch}"
|
||||
reversed=1
|
||||
fi
|
||||
if [[ -n "$(git -C "${clone}" status --porcelain)" ]]; then
|
||||
if ((reversed)); then
|
||||
git -C "${clone}" apply "${patch}"
|
||||
fi
|
||||
warn "skills: ${clone} has local changes; TokenEconomizer stays off its pin ${commit}."
|
||||
exit 1
|
||||
fi
|
||||
if [[ "$(git -C "${clone}" rev-parse HEAD)" != "${commit}" ]]; then
|
||||
git -C "${clone}" cat-file -e "${commit}^{commit}" 2>/dev/null || git -C "${clone}" fetch --quiet origin "${commit}"
|
||||
git -C "${clone}" -c advice.detachedHead=false checkout --quiet --detach "${commit}"
|
||||
fi
|
||||
git -C "${clone}" apply "${patch}"
|
||||
log "skills: TokenEconomizer at ${commit} with the local security patch ${patch##*/} in ${clone}"
|
||||
|
||||
if command -v claude &>/dev/null; then
|
||||
claude plugin install tokeneconomizer --marketplace "${clone}" --scope user
|
||||
else
|
||||
log "skills: claude not found; TokenEconomizer skipped for Claude Code."
|
||||
fi
|
||||
if command -v codex &>/dev/null; then
|
||||
listed="$(codex plugin marketplace list 2>/dev/null || true)"
|
||||
if [[ "${listed}" != *"${clone}"* ]]; then
|
||||
codex plugin marketplace add "${clone}"
|
||||
fi
|
||||
codex plugin add tokeneconomizer@tokeneconomizer
|
||||
log "skills: TokenEconomizer for Codex takes effect once its hooks are approved: start codex, run /hooks, choose Trust all."
|
||||
else
|
||||
log "skills: codex not found; TokenEconomizer skipped for Codex."
|
||||
fi
|
||||
"${clone}/plugin/scripts/te" config set toolprune=propose microskills=envfacts "toolprune_protect=${PROTECT}" --global
|
||||
log "skills: TokenEconomizer settings: tool pruning only proposes, micro-skills from repeated failures, loop guard on"
|
||||
@@ -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