Skip to content

Commit 5bf0ed4

Browse files
cursoragentDJLougen
andcommitted
fix(rule_fast): retain body literals when comments/URLs contain parens
The skeleton regex added in 6fb595a used [^(]*$ to exclude computed assignments, but it also dropped innocent literals like `limit = 42 # note (v2)` and URL strings containing parentheses. Tighten the pattern to literal numbers/strings only and keep trailing comments. Also fix _compress_command dropping the exit-code tail on 7-9 line logs when len(lines) <= 9 left tail empty so remainder lines were discarded. Co-authored-by: Daniel <DJLougen@users.noreply.github.com>
1 parent 6fb595a commit 5bf0ed4

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

hive/rule_fast/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ def _classify(role: str, content_type: str, content: str = "") -> str:
175175
r"^\s*(?:async\s+def|def|class|pub fn|fn|function|export)\b"
176176
r"|^[A-Za-z_]\w*\s*=\s"
177177
r"|^(?:import|from)\s"
178-
r"|^\s+[A-Z_a-z]\w*\s*(?::[^=]+)?=\s*(?:-?\d|[\"'])[^(]*$"
178+
# Literal body assignments only — allow trailing comments and quoted
179+
# strings that contain parens; still reject computed rhs (`payload.get(...)`).
180+
r"|^\s+[A-Z_a-z]\w*\s*(?::[^=]+)?=\s*"
181+
r"(?:-?\d+\s*(?:#.*)?|\"[^\"]*\"|'[^']*')\s*$"
179182
)
180183

181184

@@ -229,8 +232,14 @@ def _compress_command(content: str) -> str:
229232
"""
230233
lines = content.splitlines()
231234
head = lines[:6]
232-
tail = lines[-3:] if len(lines) > 9 else []
233-
middle = lines[6 : len(lines) - len(tail)]
235+
# Keep the tail (exit code) for short logs too; the >9 guard only avoids
236+
# head/tail overlap on longer output.
237+
if len(lines) > 9:
238+
tail = lines[-3:]
239+
middle = lines[6 : len(lines) - len(tail)]
240+
else:
241+
tail = lines[6:]
242+
middle = []
234243
errors = [line for line in middle if _RE_ERRORISH.search(line)][:_MAX_ERROR_LINES]
235244
parts = head + (["..."] if middle else []) + errors + (["..."] if tail else []) + tail
236245
return f"[cmd] ({len(content)} chars)\n" + "\n".join(parts)

tests/test_stack.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
Label,
1111
Message,
1212
RuleFastHoneyComb,
13+
_compact_file,
14+
_compress_command,
1315
_compress_test_output,
1416
_infer_content_type,
1517
)
@@ -54,6 +56,40 @@ def test_compress_test_output_collapses_lines():
5456
assert "12 passed" not in out # we didn't claim that
5557

5658

59+
def test_compact_file_keeps_literal_assignments_with_paren_comments():
60+
"""Body literals must survive comments/URLs containing parentheses."""
61+
content = "\n".join(
62+
[
63+
"def handler(payload, *, timeout=30):",
64+
' """Handler."""',
65+
" limit = 4321 # see RFC (section 3)",
66+
' url = "http://registry.internal/path(v2)/push"',
67+
" x = payload.get('retry')",
68+
" return limit",
69+
]
70+
)
71+
out = _compact_file(content)
72+
assert "limit = 4321" in out
73+
assert 'url = "http://registry.internal/path(v2)/push"' in out
74+
assert "payload.get" not in out
75+
76+
77+
def test_compress_command_keeps_exit_code_on_short_logs():
78+
short = "\n".join(
79+
[
80+
"$ make deploy",
81+
"step 1 ok",
82+
"step 2 ok",
83+
"step 3 ok",
84+
"step 4 ok",
85+
"step 5 ok",
86+
"step 6 ok",
87+
"exit 1",
88+
]
89+
)
90+
assert "exit 1" in _compress_command(short)
91+
92+
5793
def test_route_falls_back_when_no_policy():
5894
stack = HiveStack()
5995
decision = stack.route({"goal": "x", "state": {}, "available_tools": []})

0 commit comments

Comments
 (0)