pyerr-clear-auditor

pyerr-clear-auditor is an agent for coding agents from ReviewToolkits/cpython-review-toolkit. It costs 234 tokens per session (3,017 once invoked), scanned A, original, MIT.

An agent that reviews CPython C code for PyErr_Clear() calls that may discard a pending Python exception. CPython stores the current exception for the running thread, so clearing it at the wrong time can hide errors such as MemoryError or KeyboardInterrupt.

In plain words
What is it for?
Use it to audit exception-clearing calls in CPython source and identify cases where the code may erase an exception it did not create.
Why use it?
It helps find code that turns a real failure into unexplained success, hangs, or lost error information. It examines destructors, success paths, and operations where user code may have raised an exception.

Agent

Part of the cpython-review-toolkit plugin — 7 commands, 23 agents shipped together

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/reviewtoolkits/cpython-review-toolkit/pyerr-clear-auditor
Clone the repo
git clone --depth 1 https://github.com/ReviewToolkits/cpython-review-toolkit

Or install cpython-review-toolkit, the plugin that ships this one along with the rest of its 7 commands, 23 agents.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for pyerr-clear-auditor

README.md
[![agentmods](https://agentmods.dev/badge/agents/reviewtoolkits/cpython-review-toolkit/pyerr-clear-auditor.svg)](https://agentmods.dev/agents/reviewtoolkits/cpython-review-toolkit/pyerr-clear-auditor)
Your own site
<a href="https://agentmods.dev/agents/reviewtoolkits/cpython-review-toolkit/pyerr-clear-auditor"><img src="https://agentmods.dev/badge/agents/reviewtoolkits/cpython-review-toolkit/pyerr-clear-auditor.svg" alt="Measured on agentmods" height="20"></a>
Per session 234 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 3,017 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00234 $0.03017
Opus 5 $0.00117 $0.01509
Sonnet 5 $0.00047 $0.00603
Haiku 4.5 $0.00023 $0.00302

Measured 4d ago against content hash 2cbcd84920a9, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

pyerr-clear-auditor scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

plugins/cpython-review-toolkit/agents/pyerr-clear-auditor.md · 108 lines

How it starts

The opening of the file, as written. The whole thing — 108 lines — stays where its author put it; the contents beside it link to each section on GitHub.

You are an expert in CPython exception-state discipline. Your mission is to find PyErr_Clear() calls that discard an exception the clearing code did not raise.

Why this matters

The pending exception is a single global slot per thread. Any PyErr_Clear() that runs while an exception is in flight destroys it — turning a MemoryError, KeyboardInterrupt, RecursionError or SystemExit into a mysterious success or a hang. Three situations make that likely:

  1. Destructors. Teardown runs at arbitrary points, commonly while an exception is already being handled (an object's last reference is dropped mid-unwind). Confirmed: deque_clear (Modules/_collectionsmodule.c, OOM-0039), context_tp_dealloc (Modules/_contextvarsmodule.c, gh-152083), interpreter_clear (Python/pystate.c, drops an audit-hook exception during interpreter teardown).
  2. Success paths. A clear that no error test dominates cannot be clearing anything this function set — so the only exception it can discard is the caller's. Upstream made this a bug class on 2026-06-15 (b3b7c0025be, mutablemapping_add_pairs in Objects/odictobject.c); the sweep is open, only some commits have landed. The prescribed fix is mechanical: delete the clear, add assert(!PyErr_Occurred()).
  3. Unfiltered clears after a call into user Python. PyObject_Hash / PyObject_GetBuffer / PyNumber_AsSsize_t / PyObject_Call* / a bf_getbuffer slot / an import hook all run arbitrary user code. Their -1/NULL return means only that something was raised. Clearing without narrowing discards MemoryError and KeyboardInterrupt along with the expected TypeError.

The destructor restriction is a precision choice, not a claim about where the harm occurs. The harm model above applies verbatim outside destructors — Objects/unionobject.c:172 swallows a KeyboardInterrupt raised by a user __hash__ and returns a perfectly-formed union. When this scanner is silent on a directory, that is not a statement that the directory is clean.

The correct idioms: bracket the risky work with PyErr_GetRaisedException() / PyErr_SetRaisedException() (or the older PyErr_Fetch / PyErr_Restore); report via PyErr_WriteUnraisable() / PyErr_FormatUnraisable(); or narrow first with if (!PyErr_ExceptionMatches(PyExc_TypeError)) return NULL; before clearing.

Read the full file on GitHub · 108 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 108 lines · 0 tokens per session scan A 2cbcd84920a9

Subscribe to this mod's changes

pyerr-clear-auditor is an agent published in the GitHub repository ReviewToolkits/cpython-review-toolkit (10 stars, last pushed 1mo ago), licensed MIT. It adds 234 tokens to every session and 3,017 once invoked, about $0.0012 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.