Skip to content

Raise the invariants that were only asserted - #405

Merged
dovvnloading merged 1 commit into
mainfrom
fix/invariants-not-enforced-by-assert
Sep 4, 2026
Merged

Raise the invariants that were only asserted#405
dovvnloading merged 1 commit into
mainfrom
fix/invariants-not-enforced-by-assert

Conversation

@dovvnloading

Copy link
Copy Markdown
Owner

Problem

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,  f"topic {name!r} registered twice"
assert key  not in self._intents, f"intent {topic}/{intent} registered twice"

backend/app.py's _configure_session registers 12 topics and roughly 90 intents in an order its own comments describe as 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.

Nothing in this repo runs under -O today. That is the point: it is cheap to keep true and expensive to discover is not.

Change

All seven become 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 — 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.py has the word "assert" inside a docstring, which is why this is AST-based like test_domain_purity.py and test_node_state_migration.py.
  • Full suite: 3202 passed, 19 skipped. ruff clean, mypy clean (events.py is inside the mypy scope).

🤖 Generated with Claude Code

`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>
@dovvnloading
dovvnloading merged commit 955bb36 into main Sep 4, 2026
5 checks passed
@dovvnloading
dovvnloading deleted the fix/invariants-not-enforced-by-assert branch September 4, 2026 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant