Raise the invariants that were only asserted - #405
Merged
Conversation
`python -O` and PYTHONOPTIMIZE=1 strip assert statements entirely, taking
whatever they guarded with them - silently, with no error and no log. Seven
shipped modules enforced real invariants that way.
Two of them were the event bus's duplicate-registration guards:
assert name not in self._topics, "topic registered twice"
assert key not in self._intents, "intent registered twice"
backend/app.py's _configure_session registers 12 topics and roughly 90
intents in an order its own comments call load-bearing. Under -O a
duplicate registration silently REPLACES the previous handler - exactly
the failure those checks exist to make loud, made silent by an
interpreter flag. Two more guarded a subprocess pipe against None in the
MCP and plugin-worker read loops, where the alternative to the assert is
an AttributeError surfacing deep inside a reader thread with no hint that
the process was simply never connected.
All seven are now explicit raises: ValueError for a duplicate or
malformed registration, TypeError for a non-dataclass args_schema,
RuntimeError for a read loop started before its process connected.
Nothing in this repo runs under -O today. That is the point - it is cheap
to keep true and expensive to discover is not, so a gate pins it.
Two existing tests were asserting the mechanism rather than the invariant
(`pytest.raises(AssertionError)`) and now name the real exception types.
That they existed at all is why this was a small change.
Test plan:
- tests/test_no_production_asserts.py: AST scan of all four shipped
packages plus the loose root modules, with a guards-the-guard check so
a wrong root cannot make it pass over an empty file list. Grep would
not do - backend/events.py has the word "assert" inside a docstring.
- Full suite: 3202 passed, 19 skipped. ruff clean, mypy clean (events.py
is inside the mypy scope).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
python -OandPYTHONOPTIMIZE=1stripassertstatements entirely, taking whatever they guarded with them — silently, with no error and no log. Seven shipped modules enforced real invariants that way.Two of them were the event bus's duplicate-registration guards:
backend/app.py's_configure_sessionregisters 12 topics and roughly 90 intents in an order its own comments describe as load-bearing. Under-Oa duplicate registration silently replaces the previous handler — exactly the failure those checks exist to make loud, made silent by an interpreter flag.Two more guarded a subprocess pipe against
Nonein the MCP and plugin-worker read loops, where the alternative to the assert is anAttributeErrorsurfacing deep inside a reader thread with no hint that the process was simply never connected.Nothing in this repo runs under
-Otoday. That is the point: it is cheap to keep true and expensive to discover is not.Change
All seven become explicit raises —
ValueErrorfor a duplicate or malformed registration,TypeErrorfor a non-dataclassargs_schema,RuntimeErrorfor a read loop started before its process connected — and a gate pins it.Two existing tests were asserting the mechanism rather than the invariant (
pytest.raises(AssertionError)) and now name the real exception types. That they existed at all is why this was a small change.Test plan
tests/test_no_production_asserts.py: AST scan of all four shipped packages plus the loose root modules, with a guards-the-guard check so a wrong root cannot make it pass over an empty file list. Grep would not do here —backend/events.pyhas the word "assert" inside a docstring, which is why this is AST-based liketest_domain_purity.pyandtest_node_state_migration.py.ruffclean,mypyclean (events.pyis inside the mypy scope).🤖 Generated with Claude Code