Skip to content

feat(plugin): /context-guru:save-more and /context-guru:remove-mcp-and-skills (replace adapt) - #526

Merged
amiddavid merged 24 commits into
mainfrom
feat/adapt-command
Oct 15, 2026
Merged

amiddavid merged 24 commits into
mainfrom
feat/adapt-command

Conversation

@OsherElhadad

@OsherElhadad OsherElhadad commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

What

Three plugin commands for Claude Code and Codex. They replace /context-guru:adapt, which stays for one release as a redirect.

Command Does
/context-guru:save-more runs config and cache together; one combined summary and one question at the end
/context-guru:save-more config replays past sessions under every shipped preset and shows what each would have saved; --custom also searches single components, their combinations and their order
/context-guru:save-more cache every keep-alive strategy, with a day-of-week × hour view of where the savings come from
/context-guru:remove-mcp-and-skills unused MCP servers, MCP tools and skills (never built-in tools), plus what earlier removals measurably saved; asks one question, "Remove these N items, saving ≈$X/week?"
... apply <kind> <option>@<basis> / ... revert <kind> make or undo a change the screen offered

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 on tvlib.

Codex: $context-guru-save-more and $context-guru-remove-mcp-and-skills, running the same script.

Safety

  • Reads are pre-approved. A change is a separate top-level command (cg.py apply / cg.py revert), so Claude Code asks permission for it behind the skill's own yes/no question.
  • A change typed under a read command is not run; the command prints the exact command to run instead.
  • Applying an option whose go-live check failed needs a second, separate yes (--force).
  • Every skill pre-approves only its own read command. The allowed-tools patterns are quoted to match the quoted command, since the match is a literal prefix. A test scans all skill frontmatter (Claude Code and Codex): it rejects a cg.py wildcard and any pre-approved apply/revert, and checks that each skill's own command lines match its patterns.
  • On hosted, the account's own token is sent only by these commands.

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

  • Goldens for every screen, apply/revert/redirect path and error, against a stub proxy. The docs pages embed the goldens verbatim. A width/ASCII/colour test checks 80 columns, ASCII only, and that colour adds no text.
  • The plugin suite and the Codex tests pass.
  • The screens in docs/plugin/img (asciinema .cast plus 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.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 vet is clean and go test ./context-guru-plugin passes. The plugin and Codex copies of cg.py are 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 the apply argument, or from the terminal prompt on y/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 error sentence. That covers #523's 400/409/501 answers, and a 409 re-renders the report.

Blocking:

  1. The contract no longer matches the tools endpoint (#523), which now requires basis.
    • docs/plugin/adapt.md says "basis may be omitted (an undo, for example); then no staleness check is made". #523 now answers 400 without it.
    • The Undo: line is rendered as adapt <kind> apply <undo.option> with no basis, so the printed undo for tools fails with 400. #523's apply answer carries undo.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: basis is required (400 without it), undo carries basis, and 501 means the proxy has no configuration to write.
  2. Missing numbers are shown as $0 on the tools screen. #523's items carry priced: false with usd: 0 when a request's model had no price. The item rows render dollars(i.get("usd", 0)), so an unpriced item reads as costing $0.00. Show n/a when priced is false. The same applies to saved_usd/spend_usd defaulting 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.

Osher-Elhadad added 2 commits October 13, 2026 19:52
… 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 OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 basis is required (400 without it), undo carries a basis, and 501 means there is no writable config. The Undo: line renders <option>@<basis>. An option typed without a basis reads the current report and sends its basis_id.
  • n/a, not $0. money() renders n/a for an unpriced item, an absent saved or spend figure, and a missing overview net_delta_usd. With money() changed back to dollars(x or 0), TestCGGoldens/adapt-tools fails.
  • net_usd: null is now pinned in the adapt-cache goldens.
  • The write is a separate command. cg.py apply <kind> <token> is the write, and cg.py adapt <kind> apply … and cg.py adapt apply … both refuse with exit 1. The Claude Code skill pre-approves only cg.py adapt:*, which does not prefix-match cg.py apply. Restoring the old branch, or widening the pattern back to cg.py:*, makes TestAdaptSkillPreApprovesOnlyTheRead fail.
  • Codex. The skill applies only after an explicit yes and with require_escalated, and the two copies of cg.py are 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:

  1. 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.
  2. The write prompt depends on which skills are loaded. 14 other skills pre-approve python3 ${CLAUDE_PLUGIN_ROOT}/scripts/cg.py:*, which would also cover cg.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.
  3. 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.

Osher-Elhadad added 2 commits October 13, 2026 20:36
… 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]>
Osher-Elhadad added 3 commits October 13, 2026 21:27
…ow; a change typed kind-first prints the exact command

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 OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Bash prefix:* rule matches the exact prefix or startsWith(prefix + " "), so it respects word boundaries. That means cg.py cache:* does NOT pre-approve cg.py cache-campaign build/mode, and session:*, home:* and the others cannot reach a longer command name either.
  • Writes under a read prefix are refused. cg.py adapt:* does match any adapt … 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, and apply/revert (with and without --force) under waste, cache, home, session, doctor and savings. Every one exited 1 and sent no POST. The adapt ones print the exact separate apply/revert command. adapt tools --force only reads.
  • Skill pre-approvals. Every Claude skill pre-approves only cg.py <read command>:*. With cg.py:* put back in the cache skill, TestNoSkillPreApprovesAChange fails. The Codex skills pre-approve nothing, and the change path asks for require_escalated.
  • Revert. It sends {"option":"revert"} with no basis, which matches #528's compaction route (which takes "revert" without a basis), the shared adaptRevert that #523 uses, and #530's revert option.
  • --force. It is a second decision: the shell prints the Apply anyway … --force line only after a refused apply that offers it, and the skill asks a separate yes/no and never adds --force itself.
  • 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 own CG_URL/CG_TOKEN.
  • Rendering. With ☆ replaced by ★ on a non-recommended top row, the adapt-compaction and adapt-compaction-details goldens fail.
  • gofmt and go vet are clean, the full plugin suite passes, and the Codex pytest suite passes (40). The two copies of cg.py are 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.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 equals X or starts with X followed by a space. A rule without :* must equal the whole trimmed command. Prefix rules are never applied to compound commands. So the quoted cg.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.py pattern names one read command. adapt:* covers adapt <kind> apply… lines, but cg.py refuses those: it exits 1 without a POST, and TestAdaptSkillPreApprovesOnlyTheRead pins that. The $ARGUMENTS apply/revert line matches no pattern. The settings.py rules (preset-picker, cache-strategy-picker) are exact, unquoted and argument-less, so they never match preset set or strategy 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":*, and apply:*; TestNoSkillPreApprovesAChange fails on each.
  • CI is green (build-test, purego, trivy, DCO). go vet is 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]>
@OsherElhadad OsherElhadad changed the title feat(plugin): /context-guru:adapt command (cache, compaction, tools) with confirm-and-apply feat(plugin): /context-guru:save-more and /context-guru:remove-mcp-and-skills (replace adapt) Oct 14, 2026
Osher-Elhadad added 2 commits October 14, 2026 22:06
…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 OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 exact settings.py record-run <name>, which only writes a local timestamp. Codex skills pre-approve nothing, and their writes use require_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 revert and save-more apply config …;
    • remove-mcp-and-skills apply … and remove-mcp-and-skills revert;
    • adapt tools apply … and save-more tools apply ….
  • Every one exited non-zero and sent no POST/PUT/DELETE. The adapt-style ones print the separate apply or revert command to run.
  • Widening save-more:* to cg.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_range names a 95% CI "95% CI" and a bound "range", never one for the other.
  • Built-ins are never listed: with the ITEM_KINDS filter removed, the remove-mcp-and-skills goldens 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:

  1. Payback wording. It uses round() while #537 uses ceil, so a payback of 1.2 weeks reads "It pays back in about 1 weeks". Use ceil and singular/plural.
  2. Question with no weekly figure. With no weekly figure the question reads "saving ~n/a/week?". Drop the clause when per_week is None.
  3. Wording conflict. The question and card say "saving ≈$X/week" and "would add about $X a week" (forward-looking), while #537's own recommendation.question uses hindsight wording ("would have saved"). If the owner mandated the exact form, that's fine; otherwise prefer the backend's.
  4. Lost basis. The remove-mcp-and-skills apply remove@b redirect prints apply tools remove, dropping the @basis. That fails safe (400, basis required), but it should keep the token as typed.
  5. Tools "So far" line. It says "nothing removed yet" whenever to_date.items is 0, even if saved_usd (the filter's measured total, including the unattributed share) is above zero.

This approval covers 8b9e667.

Osher-Elhadad added 2 commits October 14, 2026 22:32
… 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 OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-more is pre-approved by the two separate rules: the save-more:* prefix, which covers every argument variant (config, cache, --custom, details), and the exact record-run save-more rule. 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-more still prompts, because the apply subcommand matches no rule. A bare record-run save-more with 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". Use ceil (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.items is 0, even if a measured saving is reported.

This approval covers dded586.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. With round() 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), and adapt tools apply X@b. Each prints apply tools X@b and 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 vet is 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.

@amiddavid
amiddavid merged commit ed6bd6a into main Oct 15, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants