A Python tool adapter checks permission before a write. Its negative test passes: an unauthorised request raises AssertionError, and the write never happens. Launch the same adapter with python -O, and the request reaches the write without evaluating the permission function.
I would reject assert as the enforcement mechanism for an agent tool’s authorisation boundary. The deployed interpreter must execute the refusal path regardless of whether debugging assertions are enabled.
This is ordinary Python behaviour, not a model exploit. The Python language reference says the code generator emits no code for an assert statement when optimisation is requested at compile time. If that statement contains the only permission check, the check disappears with it.
The downloadable example uses a synthetic permission decision and an in-memory effect list. It demonstrates a faulty adapter pattern, not a vulnerability finding against Claude Code, MCP or a named service. It does not contact a model or exercise a real authorisation server.
The permission function never runs
Consider this deliberately unsafe wrapper:
def write_document(request, principal):
assert permitted(principal, request), "not authorised"
return store.write(request)
In normal execution, a false result from permitted raises AssertionError before store.write. A happy-path test and a denied-path test can both pass. That result describes the code under that interpreter configuration.
The CPython command-line documentation defines -O as removing assert statements and code conditional on __debug__. -OO also discards docstrings. Neither setting preserves an assertion because its message says “not authorised”.
There are two consequences. The wrapper no longer refuses the request, and permitted is not called at all. If someone put an audit event, quota reservation or approval-consumption operation inside that function, those effects would also be skipped. Such operations should not depend on evaluating a debugging assertion.
Moving the function call outside assert is an incomplete repair:
allowed = permitted(principal, request)
assert allowed, "not authorised"
return store.write(request)
This evaluates the permission function under optimisation, but removes the branch that prevents the write. A recorded denial followed by an executed action is still an enforcement failure.
Run the actual interpreter modes
Download and inspect the standard-library fixture. It runs on Python 3.9 or later:
python3 agent-assert-authorisation.py
python3 -O agent-assert-authorisation.py
Each run starts separate child processes. The test launcher explicitly selects normal, -O and -OO execution rather than assuming that an optimised test runner automatically starts optimised children. It removes any inherited PYTHONOPTIMIZE setting before selecting a mode, then reads sys.flags.optimize from the child result.
The demonstration printed these results on Python 3.9.6:
{"mode": "weak", "optimisation": 0, "outcome": "denied", "checks": 1, "effects": []}
{"mode": "weak", "optimisation": 1, "outcome": "completed", "checks": 0, "effects": ["synthetic_write"]}
{"mode": "fixed", "optimisation": 1, "outcome": "denied", "checks": 1, "effects": []}
The checks count records calls to the permission function. The effect is an append to a list in the child, not a filesystem or remote write. The second result therefore demonstrates both removed enforcement and removed predicate evaluation without modifying production state.
Eight test methods passed with the runner in normal mode and again under -O. They cover the weak refusal in normal mode, its bypass under both optimisation levels, environment-selected optimisation, and the repaired allowed and denied paths. Several methods test multiple interpreter modes. These are eight unique tests, not a claim that every invocation is a separate test.
The weak-path tests deliberately require the bypass to occur. Their green result confirms a reproduced defect. The repaired-path tests require an empty effect list for denied requests and exactly one synthetic write for authorised requests.
Raise explicitly before the effect
The repaired wrapper uses an ordinary conditional and exception:
def write_document(request, principal):
if not permitted(principal, request):
raise PermissionError("not authorised")
return store.write(request)
Python optimisation does not remove this branch. The fixture uses the same permission function in both versions, so the comparison isolates the choice of enforcement construct rather than changing the policy.
The real permitted function still needs a precise contract. Obtain the principal from authenticated service context, resolve the resource’s tenant and evaluate the requested action against that scope. Do not accept a model-supplied allowed: true as a permission decision. The fixture’s Boolean is a controlled test input, not a suggested authentication design.
A policy-service error should also stop this path before the write. Do not catch PermissionError or a broader exception and then fall through to store.write as a recovery measure. The example catches refusal only at its outer reporting boundary, after control has left the action path.
This correction does not solve approval expiry or a resource changing between check and write. Where those races matter, the receiving service needs to enforce the current scope and relevant version condition at the effect boundary. Keep the tool permission budget and resource-level policy separate from the Python mechanism that invokes them.
Inspect the launch configuration, not only the function
The documented PYTHONOPTIMIZE environment variable can enable optimisation without an explicit -O in the command you are reading. The fixture includes a child started with PYTHONOPTIMIZE=1 and confirms the same bypass.
Review the service entrypoint, container command and worker launcher. A developer’s local shell may differ from a scheduled worker or packaged deployment. The subprocess environment example explains why inherited process settings deserve their own contract; here, a Python setting changes which checks execute.
Turning optimisation off can contain this particular bypass while a repair is prepared. It leaves the source dependent on deployment configuration, however. Replace the enforcement assertion and keep the optimised negative test so a later launch change cannot recreate the defect unnoticed.
A source search for assert gives you review candidates. Classify each occurrence. Debugging invariants and test assertions have different roles from checks on an untrusted request, a spending limit or a write permission. Do not replace every assertion mechanically, and do not describe a clean search as a complete security review. An alias, decorator or downstream wrapper may contain the effective boundary.
Make the denied-path test survive optimisation
The checker itself must retain its checks. A standalone script built entirely from bare assert statements may report success under -O after Python has removed its verification code. This fixture uses unittest.TestCase methods such as assertEqual; these are method calls, not Python assert statements.
For an integration test, launch the actual tool service in a disposable environment using its deployment command. Send a denied request, inspect the response, and independently query the effect store. A denial response alone does not prove the service performed no write. Run an authorised control case too, so a broken endpoint cannot pass every refusal test by doing nothing.
That service-level exercise remains work for your adapter. The local fixture establishes the interpreter behaviour and the repaired branch; it does not establish identity provenance, remote policy correctness or absence of effects in another process.
For the broader review and operating workflow, see my Claude Code book, available as a Kindle edition on Amazon UK.
Pick one write-capable Python adapter. Replace any enforcement assertion with an explicit refusal, then run its denied-path test under the same interpreter flags and environment as the deployed worker.