Refusectomy
Find, explain and live-monitor model refusals in CLI coding agents.
By Sebastian S. Cocioba, Binomica Labs.
When a safety classifier declines legitimate work, an agent session rarely fails just once. It keeps failing, and the obvious fixes (rephrasing, switching models, trimming context) mostly don't help. Refusectomy measures what actually happened on your machine, watches for it as it happens, and tells you what the evidence supports. It identifies refused text by digest, never by printing it.
What it can see, per agent
| agent | how Refusectomy reads it | what you get |
|---|---|---|
| Claude Code | its session logs, plus a Stop hook | every refusal with the classifier's category, the model, Claude Code's switch to a fallback model, and the refused message (as a digest); full forensics |
| Any agent whose API base URL you can set (Anthropic, OpenAI or Gemini wire format) | refusectomy proxy |
every response's stop reason as it streams, request ids, and the prompt's digest |
| GitHub Copilot CLI | its session log | tool failures and session errors only: it does not log the model's stop reason |
| Antigravity CLI | its session log | error steps; a safety block only when the agent logs it as an error |
refusectomy agents prints this table for your own machine.
Install
Python 3.10 or newer, no dependencies.
pipx install refusectomy # or: uv tool install refusectomy
pipx install . # from a checkout
Claude Code
refusectomy audit # check the parser's assumptions against your transcripts
refusectomy profile # rates, stickiness, the model comparison, confounds
refusectomy remedy --category bio # the playbook, rendered from your own profile
refusectomy watch # live, in a second terminal
refusectomy report --project DIR # a support-ticket bundle: request ids and digests
To get a warning inside Claude Code the moment a session declines, add the hook to
~/.claude/settings.json:
{ "hooks": {
"Stop": [{ "hooks": [{ "type": "command", "timeout": 10,
"command": "refusectomy hook claude-code 2>/dev/null || true" }] }],
"SessionStart": [{ "hooks": [{ "type": "command", "timeout": 10,
"command": "refusectomy hook claude-code 2>/dev/null || true" }] }] } }
The warning is shown to you as a systemMessage. It is never added to the model's context:
feeding "you were just declined" back into the context that declined is how one decline turns
into a run of them.
Excise: a clean copy of a declined session
refusectomy excise --dry-run # what would be removed from the current session
refusectomy excise # write the clean copy and print its /resume command
To excise between turns, add --excise to the hook command (or put {"excise": true} in
config.json in the state directory, or set REFUSECTOMY_EXCISE=1). When a session declines,
the warning then names a clean copy to /resume. refusectomy watch --excise does the same from
the monitor, waiting until the session's log has gone quiet.
What it removes: the decline's own debris, nothing else.
- Every record of the declined response: the partial output that the refusal docs say to discard.
- The harness's placeholder notice and its refusal record.
- The hidden "meta" notice message that Claude Code otherwise sends back to the model.
- The results of any tool calls made inside the declined response. A call that had already run keeps its effects on disk, and the report says how many did.
What it keeps: everything else, byte for byte. That covers your messages, including any that quote the notice, every response that wasn't declined, and all the work material. Excision never rewords anything, and never removes content because a classifier reacts to it, so the classifier still sees everything you actually asked.
How the copy behaves. The source session is never modified. Resuming the copy starts on your default model, which also releases the refusal-fallback lock.
This was verified end to end: Claude Code resumes a cleaned copy. Asked whether any earlier message mentioned a safety classifier, the model in the cleaned copy says no, and the same session uncleaned says yes.
Whether it prevents the next decline is not yet known. The run of declines could come from the
debris, or from the material that is still in context. profile reports what happened after each
clean copy was resumed. Until there is data, abandoning the session remains the measured lever.
Any other agent, through the proxy
refusectomy proxy # loopback only; prints the URL for each provider
# then point the agent at it, e.g.
ANTHROPIC_BASE_URL=http://127.0.0.1:8787/anthropic your-agent
OPENAI_BASE_URL=http://127.0.0.1:8787/openai/v1 your-agent
refusectomy watch # in another terminal: every agent, one timeline
The proxy forwards each request unchanged to the real API over verified TLS. It relays the response back as it streams, reading the stop reason as it goes by. It recognises:
- Anthropic:
stop_reason: "refusal", with its category. - OpenAI: a
content_filterfinish, arefusalfield, or a Responses refusal or content-filter cut-off. - Gemini: a safety finish reason or a blocked prompt.
Agents that only talk to their vendor's own backend, such as Copilot CLI and Antigravity, can't be proxied without intercepting TLS, which Refusectomy doesn't do.
What the evidence says
These come from the corpus this tool was built on. profile re-measures each of them on yours.
- Declines cluster within a session. Once one lands, the next request in that session is far more likely to decline than a request in a fresh session. Abandon the session; don't rephrase inside it.
- The refused message is rarely the problem. Most refused messages are a single word, like "continue". The decline is about the work accumulated in the context, not the phrasing.
- Claude Code moves a declined session to another model. When Opus 5.5 or Fable declines,
Claude Code retries on Opus 5 and keeps the session there until
/clearor/model. The retry usually rescues the turn, but the session stays hot. - Compare models only before a session's first decline. After it, the fallback and the cascade decide which model you're on, so turn-level comparisons measure the fallback, not the model. Measured the clean way, this corpus showed no evidence that switching models helps.
- Context size and memory files weren't levers. The apparent effect of context size was survivorship. No memory file was read more often before early declines than in clean sessions.
- The real fix is institutional. For legitimate work, that means Anthropic's verification
programs for life sciences and for cyber, plus false-positive reports filed with request ids.
reportbuilds that bundle.
refusectomy profile prints the numbers behind each one for your own machine, beside the controls
that keep them honest.
Privacy and safety
- Refused text is never printed. It's identified by a sha256 digest and a character shape.
--verbatimwrites it to a mode-0600 file for you to read; it never goes to stdout. - Everything stays local. There is no telemetry and no network access, except the proxy forwarding your agent's own requests to the API it was already calling.
- The proxy records metadata only. It listens on loopback only, and never records headers, bodies, query strings (where Gemini can carry an API key) or text.
- It only observes. It never rewrites, retries, or searches for phrasings that get past a
classifier.
bisectreports what reproduces a decline, nothing more. - Where files go. Reports and state live in
$REFUSECTOMY_HOME, or by default in~/.local/state/refusectomy(Linux),~/Library/Application Support/refusectomy(macOS) or%LOCALAPPDATA%\refusectomy(Windows).
How it keeps itself honest
auditchecks the invariants its numbers depend on, against your own transcripts, and exits non-zero if any fail.- Every figure that
remedyand the hook quote comes from your lastprofile; none is typed into the source. - Each bug fix has a regression test on synthetic transcripts. Each of those tests was checked by putting the bug back and watching it fail.
Development
python3 -m unittest discover -s tests -t .
PYTHONPATH=src python3 -m refusectomy audit
The code is in src/refusectomy/:
claude_code.py: the transcript engine and forensicsproviders.py: each provider's refusal signalsproxy.py,agents.py,watch.py: the live monitoringcli.py: the command
analysis/ holds exploratory scripts that build on the package.
License
MIT, copyright 2026 Sebastian S. Cocioba, Binomica Labs. See LICENSE.
Metadata
Release files for refusectomy 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| refusectomy-0.2.0.tar.gz | 87.4 kB | With provenance | Details for refusectomy-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| refusectomy-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for refusectomy-0.2.0-py3-none-any.whl |
Total release size: 161.5 kB
Release files / refusectomy-0.2.0.tar.gz
| Download URL | refusectomy-0.2.0.tar.gz |
|---|---|
| Size | 87.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9adf09bb808f3bfea1da1217d2cac9a7350bf9e73e7f5fe5d29b5895e2c5d009
|
|
BLAKE2b-256 checksum How to use checksums |
8e6fd99c09dc740477a0628b149b50ae72a7b09991d39c335787bafbc5601cf7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logRelease files / refusectomy-0.2.0-py3-none-any.whl
| Download URL | refusectomy-0.2.0-py3-none-any.whl |
|---|---|
| Size | 74.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a0f14046daf1298b26aa16b159cc700c90d7626d2799f250df82ab79a830b48
|
|
BLAKE2b-256 checksum How to use checksums |
765b4d0f722e64177c851fbdcb549335b221d0df2a23e23bab1f3f81a23a6d4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logSecurity for refusectomy 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for refusectomy-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.