-
Notifications
You must be signed in to change notification settings - Fork 53k
fix(execute_code): tolerate tool payloads with a trailing hint #74100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -416,6 +416,47 @@ def retry(fn, max_attempts=3, delay=2): | |
| time.sleep(delay * (2 ** attempt)) | ||
| raise last_err | ||
|
|
||
|
|
||
| def _parse_tool_result(raw): | ||
| """Parse an RPC tool-result payload into Python data. | ||
|
|
||
| Some Hermes tools append a human-readable hint AFTER the JSON payload -- | ||
| e.g. search_files emits a JSON object, a blank line, then | ||
| "[Hint: Results truncated. Use offset=50 ...]" whenever results were | ||
| truncated. A strict json.loads() raises JSONDecodeError("Extra data") on | ||
| those, which used to surface inside execute_code as a spurious parse | ||
| failure even though the tool call itself succeeded. Decode the first JSON | ||
| value and keep any trailing text under the "_hint" key so nothing is lost. | ||
|
|
||
| A payload can also be doubly encoded (the outer JSON value is itself a JSON | ||
| string), and the trailing text can sit after either the outer or the inner | ||
| payload. Unwrap iteratively and carry the suffix through the unwrap instead | ||
| of dropping it. | ||
| """ | ||
| def _decode(text): | ||
| """Decode the first JSON value in `text` -> (value, trailing_text).""" | ||
| try: | ||
| return json.loads(text), "" | ||
| except json.JSONDecodeError: | ||
| value, end = json.JSONDecoder().raw_decode(text) | ||
| return value, text[end:].strip() | ||
|
|
||
| text = raw.strip() if isinstance(raw, str) else raw | ||
| result, trailing = _decode(text) | ||
| while isinstance(result, str): | ||
| # Doubly-encoded payload: the outer JSON value is itself a JSON string. | ||
| try: | ||
| inner, inner_trailing = _decode(result.strip()) | ||
| except (json.JSONDecodeError, TypeError): | ||
| break | ||
| result = inner | ||
| # A hint attached to the inner payload is the more specific one; keep | ||
| # the outer suffix when the inner level had none. | ||
| trailing = inner_trailing or trailing | ||
| if trailing and isinstance(result, dict) and "_hint" not in result: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This recursive return drops
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — fixed in adc79ba. The recursion discarded the outer result, trailing = _decode(text)
while isinstance(result, str):
try:
inner, inner_trailing = _decode(result.strip())
except (json.JSONDecodeError, TypeError):
break
result = inner
trailing = inner_trailing or trailingAn inner hint wins when both levels carry one (it is the more specific attachment); otherwise the outer suffix survives. Two regression tests cover both placements — Green with the fix: |
||
| result["_hint"] = trailing | ||
| return result | ||
|
|
||
| ''' | ||
|
|
||
| # ---- UDS transport (local backend) --------------------------------------- | ||
|
|
@@ -476,13 +517,7 @@ def _call(tool_name, args): | |
| if buf.endswith(b"\\n"): | ||
| break | ||
| raw = buf.decode().strip() | ||
| result = json.loads(raw) | ||
| if isinstance(result, str): | ||
| try: | ||
| return json.loads(result) | ||
| except (json.JSONDecodeError, TypeError): | ||
| return result | ||
| return result | ||
| return _parse_tool_result(raw) | ||
|
|
||
| ''' | ||
|
|
||
|
|
@@ -542,12 +577,7 @@ def _call(tool_name, args): | |
| except OSError: | ||
| pass | ||
|
|
||
| result = json.loads(raw) | ||
| if isinstance(result, str): | ||
| try: | ||
| return json.loads(result) | ||
| except (json.JSONDecodeError, TypeError): | ||
| return result | ||
| result = _parse_tool_result(raw) | ||
| return result | ||
|
|
||
| ''' | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid generated-source substring assertions here. The exec()-based tests below can exercise both transport parsers directly and comply with the repository's behavioral-test rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed — that assertion tested the generator's source text rather than behavior, and
assertNotIn("result = json.loads(raw)", src)would have gone green on a rename while the bug came back. Removed in adc79ba.The behavioral coverage it was standing in for is now real: every case in
TestGeneratedToolResultParsing(tests/tools/test_code_execution.py:143) loops over("uds", "file")and exercises the parser compiled out of the generated module, so both transports are asserted by execution rather than by substring. That closes the gap the source assertion was hedging against — a transport regressing to a strictjson.loadsnow failstest_trailing_hint_is_preserved_not_raisedon that transport.Cases in the class, all per-transport: plain object roundtrip, trailing hint preserved, existing
_hintnot clobbered, double-encoded unwrap, double-encoded with an outer trailing hint, double-encoded with an inner trailing hint, non-JSON trailing on a JSON list, plain string payload returned as-is, and truly malformed input still raisingJSONDecodeError.scripts/run_tests.sh tests/tools/test_code_execution.py -q→ 85 tests passed, 0 failed.