You find a skill for your coding agent. You run it through a scanner first, because you've read the supply-chain stories. The scanner reads SKILL.md, reads every .py in the folder, finds nothing and passes it. Then your agent runs the skill, and Python executes code that is in none of the files the scanner read.

That is the attack in PyCache Trap: The Inspection-Execution Gap in Agent Skill Scanners, a paper submitted to arXiv on October 7, 2026. The authors paired benign Python source with a substituted bytecode cache and tested it on 100 skills against seven skill scanners. The attack succeeded 94% to 100% of the time, and the paper's results table reports that no scanner recognised what the cache actually did. Adversa AI's November 4 roundup put it among the month's agent-skill security findings.

The paper says the trick needs "neither a hash collision nor a modified interpreter." The easiest version uses a feature that has shipped since Python 3.7, and you can rebuild it in about a minute.

What a skill is, and what a scanner reads

In Claude Code, a skill is a folder with a SKILL.md and, optionally, scripts. The skills docs show a layout with scripts/helper.py and the note "executed, not loaded": the agent runs the script instead of reading it into context. Personal skills live in ~/.claude/skills/, project skills in .claude/skills/, and plugins can bring their own.

A skill scanner looks at what a reviewer would look at: the instructions and the source. The seven tools in the paper are Cisco's skill scanner, HSS, Auditor, SlowMist, Vetter, SkillFortify and ExecScan. Per the paper's Table 1, the cache swap got past them at rates from 94% (ExecScan) to 100% (Auditor, SlowMist, Vetter).

Say helper.py imports a utils.py. The scanner inspects both. Python runs helper.py from source, but import utils may load __pycache__/utils.cpython-311.pyc instead.

How Python decides to trust a pyc

When you import a module, Python looks for a cached, compiled copy in __pycache__. PEP 3147 set up that directory and its file names, which carry an interpreter tag such as foo.cpython-32.pyc. The Python tutorial says the tag "generally contains the Python version number", so a planted cache only fires on the Python version it was built for. The paper's corpus targets Python 3.11.

Before using the cache, Python checks it is still current. The import system docs describe the default: Python stores "the source's last-modified timestamp and size in the cache file," and at import it compares those two numbers with the .py on disk. It never compares content. Make the numbers match and the cache wins.

PEP 552, in Python 3.7, added a second kind. The .pyc header grew to four 32-bit words, and a bit field says how to validate the file. A hash-based pyc stores a hash of the source. If its check_source flag is set, Python hashes the .py and compares. If the flag is not set, the PEP says Python loads the pyc "without checking the hash of the source file." The import docs put it more bluntly: for unchecked hash-based files, Python "assumes the cache file is valid if it exists."

The mode is meant for caches "kept up to date by some system external to Python like a build system," per the py_compile docs. In a folder a stranger sent you, nothing external is keeping anything up to date.

Editorial illustration of a magnifying glass inspecting a clean sheet of paper while a hidden orange cartridge slides into a machine behind it
The scanner reads the page. The interpreter runs the cartridge.

Two rules from the docs shape the attack. First, the .py has to stay. PEP 3147 says "if the py source file is missing, the pyc file inside __pycache__ will be ignored." The paper calls these source-present substitutions, because the benign source ships next to the cache. That source is also what makes the cache load. Second, the tutorial says Python "always recompiles" the module run directly from the command line. So python scripts/helper.py runs the real helper.py, and the planted cache has to sit on a module that helper.py imports.

Rebuild the swap in a minute

Run this in an empty folder. The payload is a print statement, so nothing on your machine changes.

mkdir pyc-demo && cd pyc-demo echo 'print("PAYLOAD: not in skill.py")' > skill.py python3 - <<'EOF' import py_compile as pc mode = pc.PycInvalidationMode.UNCHECKED_HASH pc.compile("skill.py", invalidation_mode=mode) EOF echo 'print("hello from a harmless skill")' > skill.py cat skill.py ls __pycache__ python3 -c 'import skill' 

The first echo writes the "malicious" source. py_compile.compile writes its bytecode to the default PEP 3147 path in __pycache__, marked as an unchecked hash-based pyc. The second echo replaces the source with something harmless. cat shows you the harmless file, which is what a scanner would read.

Then the import. python3 -c puts the current folder on the path, Python finds the cache, sees the unchecked flag and, per the import docs, assumes it is valid. You get the PAYLOAD line, from code that is no longer in any .py file in the folder.

The timestamp kind works too, with more effort: the attacker has to make the benign file's size and modification time match what the header recorded. The unchecked kind skips all of that, which is why it is the one to worry about. A pull request on Nous Research's Hermes Agent (a different project from Hermes IDE, no relation), open as of November 8, 2026, cites the same reason: "an unchecked-hash pyc ... is imported without consulting the .py next to it." It proposes dropping shipped .pyc and __pycache__ files from every hub bundle before the scan.

Which defences close the gap, and which don't

Try each line in the same folder:

python3 -B -c 'import skill' python3 --check-hash-based-pycs always -c 'import skill' python3 -X pycache_prefix="$(mktemp -d)" -c 'import skill' 

-B and PYTHONDONTWRITEBYTECODE don't help here. The command-line docs say -B means Python "won't try to write .pyc files." It is about writing. It says nothing about reading a cache that is already there, so the planted file still loads. It's still useful for keeping your own trees clean.

--check-hash-based-pycs always closes the unchecked hole. The docs say it makes Python validate "all hash-based .pyc files, whether checked or unchecked" against their source. Your swapped cache no longer matches skill.py, so it is not used. The same page adds that "the semantics of timestamp-based .pyc files are unaffected", so a forged timestamp pyc still passes.

A pycache prefix covers both kinds. Per the sys docs, once pycache_prefix is set, "any __pycache__ directories in the source code tree will be ignored." Python reads and writes caches only under the prefix you chose, which the skill's author can't ship into. Set it with -X pycache_prefix=PATH or PYTHONPYCACHEPREFIX (Python 3.8 and later), in the shell that starts your agent so the scripts it runs inherit it. One catch: -I isolated mode ignores all PYTHON* environment variables, so a script launched with -I needs the -X form.

Editorial illustration of a funnel that catches small orange cartridges and lets only clean sheets of paper fall through into a machine
Strip the caches before anything imports the skill.

The cheapest fix is to delete the caches before review. For a skill that ships its source, nothing legitimate is lost: an honest cache is regenerated the next time Python imports the source.

find ~/.claude/skills -name '__pycache__' -o -name '*.pyc' find ~/.claude/skills -name '*.pyc' -delete find ~/.claude/skills -type d -name '__pycache__' -empty -delete 

The first command lists what you are about to remove, so read it first. Run the same commands on .claude/skills in each project, too. If a skill you didn't write came with bytecode, that alone is worth asking its author about. The paper's own proposal, execution-aware validation, goes further: it maps what the runtime will actually execute and rebuilds compiled artifacts from source to compare them. Until scanners do that, removing the artifacts yourself gets you most of the way.

Review new skills in their own worktree

I build Hermes IDE, so weigh this section with that in mind. None of it needs Hermes: a plain git worktree add gives you the same separate folder.

The idea is to look at a new skill somewhere that isn't your main checkout, before your everyday agent loads it. In Hermes, ⌘N (Ctrl+Shift+N on Windows and Linux) starts a task on its own hermes/<name> branch in its own worktree, with the task as the agent's first prompt. Copy the skill into a plain folder in that worktree (say skill-review/, not .claude/skills/, where the agent would load it) and give the agent the review as its task: list every file, including any .pyc or __pycache__, and run nothing.

The Review Desk's Worktrees tab in Hermes IDE, listing one Hermes worktree each for orders-api, web-app and orders-types under ~/code, with branch, task, age and disk size
The Review Desk's Worktrees tab: one Hermes worktree each for orders-api, web-app and orders-types under ~/code, with each worktree's hermes/ branch, task, age and disk size, and the free disk space.

Watch one trap. GitHub's standard Python.gitignore lists __pycache__/ and *.py[codz] as its first rules, so in most Python repos a planted cache is an ignored file. Hermes's Review Desk (⌘G, Ctrl+Shift+G) lists staged, unstaged and untracked files, and git doesn't count ignored files as any of those. Run git status --ignored (docs) or the find command above in the task's terminal before anything imports the skill.

Keep the agent on a mode that asks before it runs commands. Hermes never answers a permission prompt for you, so the agent waits for your answer before it runs a skill script. And be clear about what a worktree is: a separate folder, not a sandbox. Anything the agent runs there runs as you, with your files and your network. The worktree keeps the review off your main branch, and the cache checks keep the payload from running.

Before your next skill install, run the find above and see what comes back. Do you scan the skills your agents load, and did your scanner ever look inside __pycache__? Tell me in the comments.

If this was useful

Under this attack is a cache-invalidation decision: Python trusts a cache because two numbers match, or because a flag says not to check at all. Every cache you design makes the same trade between a cheap check and a correct one, and the System Design Pocket Guide: Fundamentals covers caching and the other building blocks where that trade shows up. If you want to try the worktree review, Hermes IDE is a free download for macOS, Windows and Linux, source-available on GitHub.

System Design Pocket Guide: Fundamentals — Core Building Blocks for Scalable Systems