fix(agent): replace assert with runtime guard in secret_sources command parser - #72539
Open
JonthanaHanh wants to merge 1 commit into
Open
JonthanaHanh wants to merge 1 commit into
JonthanaHanh wants to merge 1 commit into
Conversation
…nd parser The `assert m is not None` at agent/secret_sources/command.py:116 is stripped by `python -O`, silently removing the invariant check. If the regex match were to fail (e.g. due to a future regex engine change), the subsequent `m.group(1)` would raise an unhelpful AttributeError instead of gracefully skipping the line. Replace with `if m is None: continue` — a defensive guard that preserves the intent (skip non-matching lines) without relying on assert. Fixes a production assert not covered by prior PRs (NousResearch#56866, NousResearch#62659, NousResearch#64818).
teknium1
reviewed
Jul 30, 2026
teknium1
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the defensive cleanup. The target assertion is still present on current main, but the claimed optimized-mode failure is not established by the current parser flow.
Problems
agent/secret_sources/command.py:109-113only appends lines for which_ENV_LINE.match(line)succeeds;agent/secret_sources/command.py:115immediately repeats that same match before the assertion at line 116. Underpython -O, removing the assertion does not itself makemnullable or change normal behavior.- The diff adds no regression test for the new branch; existing parsing coverage at
tests/test_command_secret_source.py:88-91covers the base64 fallback only.
Suggested changes
- Please provide a reachable production condition where the second match can fail, together with a behavior-level test. If none exists, reframe this as a defensive cleanup rather than an optimized-mode bug fix.
Automated hermes-sweeper review.
| for line in dotenv_lines: | ||
| m = _ENV_LINE.match(line) | ||
| assert m is not None # filtered above | ||
| if m is None: |
Collaborator
There was a problem hiding this comment.
The preceding comprehension retains only lines for which this same _ENV_LINE.match succeeded. Under python -O, removing the assertion does not itself make this second match fail; please provide a reachable condition and regression test for this guard, or reframe it as defensive cleanup.
This branch has not been deployed
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.
Summary
Replace
assert m is not Nonewith a defensiveif m is None: continueguard inagent/secret_sources/command.py:116.Problem
The
assertstatement atagent/secret_sources/command.py:116is stripped bypython -O(optimized mode), silently removing the invariant check. While the regex match is logically guaranteed by the upstream filter in the list comprehension, relying onassertfor production code is a well-known anti-pattern:python -O, the assert is removed entirelym.group(1)would raise an unhelpfulAttributeError: 'NoneType' object has no attribute 'group'Fix
Replace:
With:
This is a no-op in the normal case (the regex always matches lines that already passed the filter) but provides a graceful degradation path instead of an AssertionError crash.
Test Plan
ast.parse())python -O -c "import agent.secret_sources.command"— no AssertionErrorNotes
This assert was not covered by prior assert-fixing PRs (#56866, #62659, #64818). The
agent/secret_sources/module was recently added and was not included in the earlier assert sweeps.