Repository navigation
feat(plugin): /context-guru:save-more and /context-guru:remove-mcp-and-skills (replace adapt) - #526
Conversation
…n, tools) Signed-off-by: Osher-Elhadad <[email protected]>
…r when every report fails Signed-off-by: Osher-Elhadad <[email protected]>
…ins a missing apply route Signed-off-by: Osher-Elhadad <[email protected]>
…the screen is shown Signed-off-by: Osher-Elhadad <[email protected]>
…t as long as the slowest route Signed-off-by: Osher-Elhadad <[email protected]>
…ows without details Signed-off-by: Osher-Elhadad <[email protected]>
Signed-off-by: Osher-Elhadad <[email protected]>
Signed-off-by: Osher-Elhadad <[email protected]>
Signed-off-by: Osher-Elhadad <[email protected]>
Signed-off-by: Osher-Elhadad <[email protected]>
OsherElhadad
left a comment
There was a problem hiding this comment.
Review: REQUEST CHANGES at c1a60e0. (Posted as a comment: GitHub does not let the author account request changes.)
Verified:
- The branch is up to date with main. All 10 commits are by Osher-Elhadad and signed off, with no AI attribution. The PR text has no experiment numbers; the docs say their examples are illustrative.
go vetis clean andgo test ./context-guru-pluginpasses. The plugin and Codex copies ofcg.pyare byte-identical. Nothing is on the status-line path.- Confirm before apply. The read paths, both the overview and
adapt <kind>, only GET. Apply runs only from theapplyargument, or from the terminal prompt ony/yes. Both skills order it as "print the screen, ask once, apply only on yes, otherwise say Nothing changed", and never apply from the read. - Errors. Every failure is one line and exits 1: unreachable (0), timeout (-1), older proxy (404 with no body), sign-in (401/403), and otherwise the server's own
errorsentence. That covers #523's 400/409/501 answers, and a 409 re-renders the report.
Blocking:
- The contract no longer matches the tools endpoint (#523), which now requires
basis.docs/plugin/adapt.mdsays "basismay be omitted (an undo, for example); then no staleness check is made". #523 now answers 400 without it.- The
Undo:line is rendered asadapt <kind> apply <undo.option>with no basis, so the printed undo for tools fails with 400. #523's apply answer carriesundo.basis(the basis of the post-write report), so render<undo.option>@<undo.basis>when it is present. - The Claude Code skill tells the model to apply a different option from the table "as
adapt <kind> apply <option>(no@basis)". The basis belongs to the report, not to one option, so use<option>@<basis_id>there too. - Update the contract section:
basisis required (400 without it),undocarriesbasis, and 501 means the proxy has no configuration to write.
- Missing numbers are shown as $0 on the tools screen. #523's items carry
priced: falsewithusd: 0when a request's model had no price. The item rows renderdollars(i.get("usd", 0)), so an unpriced item reads as costing $0.00. Shown/awhenpricedis false. The same applies tosaved_usd/spend_usddefaulting to 0 when absent from an option.
Non-blocking:
3. The "not evaluated" branch is untested. With if x.get("net_usd") is None: replaced by if False:, every golden still passes, because no fixture option has net_usd: null. Add one to adapt_cache.json so the "missing is not zero" rule is pinned.
4. The overview can print $0 for a missing figure. It renders rec.get("net_delta_usd", 0.0), so a recommendation without that field shows $0.00. Render it blank or n/a instead.
5. The confirm guarantee rests on the model alone. allowed-tools: Bash(python3 ${CLAUDE_PLUGIN_ROOT}/scripts/cg.py:*) pre-approves the apply invocation as well as the read, so Claude Code never prompts for it. Giving apply a first token outside the pre-approved prefix would add a harness permission prompt as a second gate, for example a top-level cg.py apply <kind> ... with allowed-tools narrowed to cg.py adapt:*. Note that adapt:* alone still matches adapt cache apply.
… current report's basis Signed-off-by: Osher-Elhadad <[email protected]>
…for a missing figure, undo carries its basis Signed-off-by: Osher-Elhadad <[email protected]>
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-review: APPROVE at 04f139b. (Posted as a comment: GitHub does not let the author account approve.)
All five earlier findings are fixed:
- basis. The contract says
basisis required (400 without it),undocarries abasis, and 501 means there is no writable config. TheUndo:line renders<option>@<basis>. An option typed without a basis reads the current report and sends itsbasis_id. - n/a, not $0.
money()rendersn/afor an unpriced item, an absent saved or spend figure, and a missing overviewnet_delta_usd. Withmoney()changed back todollars(x or 0), TestCGGoldens/adapt-tools fails. net_usd: nullis now pinned in the adapt-cache goldens.- The write is a separate command.
cg.py apply <kind> <token>is the write, andcg.py adapt <kind> apply …andcg.py adapt apply …both refuse with exit 1. The Claude Code skill pre-approves onlycg.py adapt:*, which does not prefix-matchcg.py apply. Restoring the old branch, or widening the pattern back tocg.py:*, makes TestAdaptSkillPreApprovesOnlyTheRead fail. - Codex. The skill applies only after an explicit yes and with
require_escalated, and the two copies ofcg.pyare byte-identical.
go vet is clean, and the full go test ./context-guru-plugin run passes (nice 10). All commits are signed off with no AI attribution, and the PR text has no numbers.
Non-blocking:
- The read pre-approval probably never matches. The skill runs
python3 "${CLAUDE_PLUGIN_ROOT}/scripts/cg.py" adapt …with quotes, but the pattern has none. That likely explains why the read still prompted in your live run. It fails safe, and the other skills have the same quoting, so it predates this PR. Either drop the quotes in the command, or confirm how Claude Code matches quoted paths. - The write prompt depends on which skills are loaded. 14 other skills pre-approve
python3 ${CLAUDE_PLUGIN_ROOT}/scripts/cg.py:*, which would also covercg.py apply …while one of them is the active skill (if its pattern matches at all, see 1). Narrow those patterns to their own subcommands, or accept that and note it. - A hand-typed option without a basis applies against the current report, so the stale check cannot protect it. That is acceptable because the user typed the command, but the docs could say so in one line.
…check Signed-off-by: Osher-Elhadad <[email protected]>
… token; no skill pre-approves a change Every skill now pre-approves only its own cg.py read command (a test scans all skill frontmatter, Claude Code and Codex). Words of apply and revert may come in any order; a change typed under adapt prints the exact command. The highest net that is not recommended is marked as such. Signed-off-by: Osher-Elhadad <[email protected]>
…ow; a change typed kind-first prints the exact command Signed-off-by: Osher-Elhadad <[email protected]>
Signed-off-by: Osher-Elhadad <[email protected]>
…dapt prints the paired differences first A pattern is a literal prefix of the command, quotes included, so the unquoted patterns never matched the quoted invocations and every read prompted. The scan test now checks each skill's own command lines against its own patterns, and that the apply/revert line matches none. Signed-off-by: Osher-Elhadad <[email protected]>
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-review: APPROVE at 10e470b. (Posted as a comment: GitHub does not let the author account approve.)
Verified:
- How
:*matches. In the installed Claude Code 2.1.296 CLI, a Bashprefix:*rule matches the exact prefix orstartsWith(prefix + " "), so it respects word boundaries. That meanscg.py cache:*does NOT pre-approvecg.py cache-campaign build/mode, andsession:*,home:*and the others cannot reach a longer command name either. - Writes under a read prefix are refused.
cg.py adapt:*does match anyadapt …arguments, so I ran cg.py against a local stub that records request methods, with 13 write-shaped command lines under read prefixes:adapt tools apply,adapt apply tools,adapt compaction revert,compaction revert,cache revert,adapt tools revert, andapply/revert(with and without--force) underwaste,cache,home,session,doctorandsavings. Every one exited 1 and sent no POST. The adapt ones print the exact separateapply/revertcommand.adapt tools --forceonly reads. - Skill pre-approvals. Every Claude skill pre-approves only
cg.py <read command>:*. Withcg.py:*put back in thecacheskill, TestNoSkillPreApprovesAChange fails. The Codex skills pre-approve nothing, and the change path asks forrequire_escalated. - Revert. It sends
{"option":"revert"}with no basis, which matches #528's compaction route (which takes "revert" without a basis), the sharedadaptRevertthat #523 uses, and #530's revert option. --force. It is a second decision: the shell prints theApply anyway … --forceline only after a refused apply that offers it, and the skill asks a separate yes/no and never adds--forceitself.- Token. The token is attached only on adapt/apply/revert calls. The hosted base comes from the base URL that already carries the
cg_live_token, or from the user's ownCG_URL/CG_TOKEN. - Rendering. With ☆ replaced by ★ on a non-recommended top row, the
adapt-compactionandadapt-compaction-detailsgoldens fail. gofmtandgo vetare clean, the full plugin suite passes, and the Codex pytest suite passes (40). The two copies ofcg.pyare byte-identical. All 4 commits are by Osher-Elhadad and signed off, with no AI attribution.
Non-blocking:
- Still unchanged from my earlier note: the skills run
python3 "${CLAUDE_PLUGIN_ROOT}/scripts/cg.py"in quotes, while the patterns have no quotes. If the matcher compares the quoted string, the reads simply prompt. That fails safe; a live check would settle it.
…-rendering a report Signed-off-by: Osher-Elhadad <[email protected]>
c6ecbaa to
10e470b
Compare
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-review: APPROVE at c6ecbaa (3190de0 quoted patterns and paired differences first; c6ecbaa refused revert prints the proxy's reason). (Posted as a comment: GitHub does not let the author account approve.)
- Matcher (Claude Code 2.1.299, read from the binary). A
Bash(X:*)rule matches when the trimmed command equalsXor starts withXfollowed by a space. A rule without:*must equal the whole trimmed command. Prefix rules are never applied to compound commands. So the quotedcg.py" waste:*now matches the quoted command the skill runs, and the unquoted form never did. - No change is pre-approved. Every
cg.py/tv.pypattern names one read command.adapt:*coversadapt <kind> apply…lines, but cg.py refuses those: it exits 1 without a POST, and TestAdaptSkillPreApprovesOnlyTheRead pins that. The$ARGUMENTSapply/revert line matches no pattern. Thesettings.pyrules (preset-picker, cache-strategy-picker) are exact, unquoted and argument-less, so they never matchpreset setorstrategy set(or any call the skills make): they fail safe to a prompt. - The scan test catches regressions. On waste's pattern I tried an unquoted form, a widening to
cg.py":*, andapply:*; TestNoSkillPreApprovesAChange fails on each. - CI is green (build-test, purego, trivy, DCO).
go vetis clean and the plugin adapt, golden and revert tests pass. Both commits are signed off with no AI attribution.
Non-blocking: the scan's read set includes cache-campaign, so on its own it would accept a widened cg.py" cache-campaign:*, which covers build and mode. #522's TestCacheCampaignSkillPreApprovesOnlyReads explicitly bans cache-campaign:*, so both trees together are covered. Treating cache-campaign here as exact-or-show:* only would close it in one place.
…d-skills replace adapt save-more runs the config report (every shipped preset; --custom adds searched combinations) and the cache report (every keep-alive strategy, with a day-by-hour view) with a combined summary and one question. remove-mcp-and-skills lists unused MCP servers, MCP tools and skills (never built-in tools) with what earlier removals measurably saved, and asks one question. Each screen opens with a card saying what the current setting saved so far and what the recommended change would add a week. Width-aware, colour on a terminal, ASCII fallback. adapt stays as a redirect for one release. apply and revert are unchanged and still never pre-approved. Signed-off-by: Osher-Elhadad <[email protected]>
…from a real Claude Code session on synthetic data; skills run the read first Signed-off-by: Osher-Elhadad <[email protected]>
…he tools question keeps its one exact form; screens regenerated Signed-off-by: Osher-Elhadad <[email protected]>
…5% CI and a range are named for what they are The skills call settings.py record-run after a read that printed a report, never after a failure, an apply or a revert. Removals measured by the proxy and estimated client-side removals are listed apart, the unattributed row included; the tools question says the payback when it is over a week; plans and the account's time zone show under details. Signed-off-by: Osher-Elhadad <[email protected]>
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-review: APPROVE at 8b9e667 (the head; it is one commit past 8915153). (Posted as a comment: GitHub does not let the author account approve.)
Pre-approval safety holds.
- Every Claude skill pre-approves only its quoted read (
cg.py" save-more:*,cg.py" remove-mcp-and-skills:*) and the exactsettings.py record-run <name>, which only writes a local timestamp. Codex skills pre-approve nothing, and their writes userequire_escalated. - I ran 12 write-shaped commands under the read prefixes against a stub that logs request methods:
save-more config apply …,save-more cache revertandsave-more apply config …;remove-mcp-and-skills apply …andremove-mcp-and-skills revert;adapt tools apply …andsave-more tools apply ….
- Every one exited non-zero and sent no POST/PUT/DELETE. The adapt-style ones print the separate
applyorrevertcommand to run. - Widening
save-more:*tocg.py":*, or unquoting it, makes TestNoSkillPreApprovesAChange fail.
Skill order rules. The skill prints first and asks one question with "yes" only. --force needs a second, separate yes, and the model never adds it itself. After an apply or a revert it prints and stops. record-run runs only after a successful read, never for an apply or a revert.
Honesty.
- Missing figures render
n/a(money()).week_rangenames a 95% CI "95% CI" and a bound "range", never one for the other. - Built-ins are never listed: with the
ITEM_KINDSfilter removed, theremove-mcp-and-skillsgoldens fail. There is no summing across kinds. - The field names used match the backends: tools from #537, config from #538, cache from #530.
Publication. No internal paths, ids or real figures are in the PR body, the docs or the docs/plugin/img casts and SVGs.
Checks. go vet is clean. The full plugin suite passes, as do TestSaveMoreWidthASCIIAndColour and the Codex tests (40). The two cg.py copies are identical. CI is green, and all commits are signed off with no AI attribution.
Non-blocking:
- Payback wording. It uses
round()while #537 uses ceil, so a payback of 1.2 weeks reads "It pays back in about 1 weeks". Useceiland singular/plural. - Question with no weekly figure. With no weekly figure the question reads "saving ~n/a/week?". Drop the clause when
per_weekis None. - Wording conflict. The question and card say "saving ≈$X/week" and "would add about $X a week" (forward-looking), while #537's own
recommendation.questionuses hindsight wording ("would have saved"). If the owner mandated the exact form, that's fine; otherwise prefer the backend's. - Lost basis. The
remove-mcp-and-skills apply remove@bredirect printsapply tools remove, dropping the@basis. That fails safe (400, basis required), but it should keep the token as typed. - Tools "So far" line. It says "nothing removed yet" whenever
to_date.itemsis 0, even ifsaved_usd(the filter's measured total, including the unattributed share) is above zero.
This approval covers 8b9e667.
… with no report exits non-zero In a live run the model skipped a separate record-run step, so the record is chained onto the read with &&: it runs only when the read exited 0, and every failed or refused read exits 1, no-proxy included. Signed-off-by: Osher-Elhadad <[email protected]>
…yback rounds up The tools question and the save-more cards are in hindsight: over the last D days the items would have saved about $X a week, with the payback rounded up and its plural right. With no weekly figure the question has no money part. A measured saving with no removals still counted reads as such. waste is understood as tools in a change typed under it. Screens regenerated. Signed-off-by: Osher-Elhadad <[email protected]>
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-review: APPROVE at dded586 (since 8b9e667). (Posted as a comment: GitHub does not let the author account approve.)
How Claude Code (2.1.299, read from the binary) matches allowed-tools against A && B.
- The command is split into its subcommands, and each subcommand is checked on its own against the rules.
- The whole command is allowed only when every subcommand is allowed. Otherwise the user is asked. A prefix rule never matches a string that is itself compound.
- So
cg.py" save-more $ARGUMENTS && settings.py" record-run save-moreis pre-approved by the two separate rules: thesave-more:*prefix, which covers every argument variant (config, cache, --custom, details), and the exactrecord-run save-morerule. The same holds for remove-mcp-and-skills. - Nothing beyond that is pre-approved.
cg.py" apply config x@y && settings.py" record-run save-morestill prompts, because the apply subcommand matches no rule. A barerecord-run save-morewith no read is allowed, but it only writes a local timestamp.
&& behaviour. Every failed or refused read exits 1, so record-run does not run after one. I checked save-more, save-more cache, save-more config --custom and remove-mcp-and-skills against a stub that answered with no proxy, 401, 404, 409, 503 and 500. Every case exits 1. Write-shaped args under a read (save-more config apply x@y, remove-mcp-and-skills apply x@y, save-more cache revert) also exit 1, and the redirect now keeps the @basis token.
Tests. With the record-run rule widened to settings.py":*, or the chained record-run removed, TestSaveMoreSkillsRecordTheirRuns fails. gofmt and go vet are clean. The full plugin suite passes, and so do the Codex tests (34 + 6, unittest). CI is green and the commit is signed off. The PR body has no figures.
Non-blocking, still open from before:
- Payback uses
round(pb)with a fixed "weeks", so 1.2 reads "about 1 weeks". Useceil(as #537 does) and get the plural right. - With no weekly figure the question reads "saving ~n/a/week?".
- The tools "So far" line says "nothing removed yet" whenever
to_date.itemsis 0, even if a measured saving is reported.
This approval covers dded586.
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-check: APPROVE at a9b9523 (since dded586). (Posted as a comment: GitHub does not let the author account approve.)
- (1) Payback.
weeks()rounds up (ceil, floor 1) and gets the plural right. Withround()swapped in, the remove-mcp-and-skills and -details goldens fail. - (2) No weekly figure. The question is just "Remove these N items?" (pinned by remove-mcp-and-skills-edge), and "n/a/week" appears nowhere in the code, skills, docs or goldens.
- (3) Hindsight wording. The question, the save-more cards, the skills, the docs and the README say "Over the last D days … would have saved about $X a week", with D from the report window (
days_of(w)). There are no forward-looking leftovers in code, skills or docs. - (4) @Basis. It is kept in every redirect I tried:
remove-mcp-and-skills apply X@b,… apply tools X@b,… X@b apply, a quoted'X@b'(re-quoted for the shell), andadapt tools apply X@b. Each printsapply tools X@band exits 1. I could not reproduce the earlier report of a dropped basis. - (5) Earlier removals. With items 0 but a measured saving, the card reads "So far: earlier removals saved $X (observed, since …)".
- Pre-approval. A local stub logged the methods for 9 write-shaped commands under read prefixes (save-more config/cache apply/revert, remove-mcp-and-skills apply/revert, adapt). All exited 1 and none sent a POST, PUT or DELETE. TestNoSkillPreApprovesAChange and TestSaveMoreSkillsRecordTheirRuns pass.
go vetis clean, the plugin goldens/adapt/save-more/remove tests and the 40 Codex tests pass, and CI is green. Signed off.
Nit, non-blocking: docs/plugin/img/cc-remove-mcp-and-skills.cast still records the old question ("Remove these 6 items, saving ≈$7.33/week?"). No doc references it now, and the re-captured SVGs carry the new wording. Delete the cast or re-record it.
This approval covers a9b9523.
What
Three plugin commands for Claude Code and Codex. They replace
/context-guru:adapt, which stays for one release as a redirect./context-guru:save-moreconfigandcachetogether; one combined summary and one question at the end/context-guru:save-more config--customalso searches single components, their combinations and their order/context-guru:save-more cache/context-guru:remove-mcp-and-skills... apply <kind> <option>@<basis>/... revert <kind>Each screen opens with a card that says, in plain words, what the current setting saved so far and what the recommended change would add per week, with its likely range. A ranked table, confidence ranges, a before → after line and the caveats follow. The output adapts to the terminal width (80 or 120 columns), uses colour on a terminal and falls back to ASCII (
CG_ASCII=1). It is built ontvlib.Codex:
$context-guru-save-moreand$context-guru-remove-mcp-and-skills, running the same script.Safety
cg.py apply/cg.py revert), so Claude Code asks permission for it behind the skill's own yes/no question.--force).cg.pywildcard and any pre-approved apply/revert, and checks that each skill's own command lines match its patterns.Contract
docs/plugin/save-more.md("The report format"). The backend routes keep their/api/adapt/*paths; only the user-facing names change. The analysis lives in the backend PRs (config, cache, tools).Tests and screens
docs/plugin/img(asciinema.castplus dark and light SVG) come from a real Claude Code session against synthetic data. They show each command's summary, the question, Claude Code's permission prompt for the change, the applied result and the revert.