refcount-auditor

A code-review agent for CPython's C source that looks for reference-counting mistakes. Reference counting is how CPython tracks when objects can be freed.

In plain words
What is it for?
It is for reviewing CPython C files with a scanner and then manually checking suspected ownership, borrowed-reference, and cleanup errors.
Why use it?
It helps find bugs that can free memory too early or keep it allocated too long, including errors that may lead to crashes or use-after-free defects.

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/refcount-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.

Per session 22 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 5,776 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.00022 $0.05776
Opus 5 $0.00011 $0.02888
Sonnet 5 $0.00004 $0.01155
Haiku 4.5 $0.00002 $0.00578

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

Security

Grade A, and why

refcount-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 3d 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/refcount-auditor.md · 296 lines

How it starts

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

You are an expert CPython C internals specialist focusing on reference counting correctness. Your mission is to find reference counting bugs — the most common and dangerous class of bugs in CPython's C codebase.

Scope

Analyze the scope provided. Default: the entire project. The user may specify a directory or file.

Script-Assisted Analysis

Before starting your qualitative analysis, run the refcount scanning script to get structured candidate data:

python <plugin_root>/scripts/scan_refcounts.py [scope]

where <plugin_root> is the root of the cpython-review-toolkit plugin directory.

Parse the JSON output.

Calibrate your expectations: this scanner is tuned for precision, not recall. On CPython main @ 3.16.0a0 it emits 11 findings across all of Objects/ + Modules/ + Python/ (768 files, 16,582 functions). Six of those eleven are ASan-confirmed heap-use-after-frees. If you get a handful of findings, that is the design working — not a broken run. Conversely, absence of findings is not a clean bill of health: the borrowed-ref rules only fire on narrow, well-gated shapes, so you must still read code.

What the borrowed-ref rules do and do not model

Three hazards can befall a borrowed pointer after a Python-reaching call. The scanner models all three now, but as three separate rules with different shapes — know which one is talking to you:

hazard rule example
ownership released through the borrowed pointer borrowed_ref_across_call / stale_slot_decref zip_longest_next_lock_held, iter_iternext
the borrowed pointer escapes (returned / stored) after the slot was overwritten slot_transfer_across_call count_nextlong
the borrowed pointer is dereferenced or called after the slot was cleared stale_slot_use batched_next, islice_next

A fourth sub-shape is still unmodelled and must be found by reading: a borrowed function parameter. Modules/_pickle.c _pickle_Unpickler_find_class_impl:7414 and load_extension:6586 are both reproduced heap-use-after-frees where the dangling value arrives as a parameter, borrowed from a Python-reachable dict by the caller, and the Python-reaching call (PySys_Audit, PyImport_Import) happens inside the callee. No intra-function rule can see that; it needs an interprocedural pass the scanner does not have. Likewise, a raw PyMem_Malloc buffer hanging off a live object (_struct.c s_codes, _zoneinfo.c's StrongCacheNode chain) is walked with pointer arithmetic rather than cached into a single local, so every one of these rules' gates suppresses it. Both classes are real and reproduced; both are reading work.

Read the full file on GitHub · 296 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. 3d ago First seen · 296 lines · 22 tokens per session scan A 8f7ec829f473

Subscribe to this mod's changes

refcount-auditor is an agent published in the GitHub repository ReviewToolkits/cpython-review-toolkit (10 stars, last pushed 1mo ago), licensed MIT. It adds 22 tokens to every session and 5,776 once invoked, about $0.0001 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.