Skip to content

fix(sdk): surface backend errors in SandboxBackend.grep - #3471

Closed
Nikhil Reddy (Nik-Reddy) wants to merge 3 commits into
langchain-ai:mainfrom
Nik-Reddy:fix/grep-error-propagation
Closed

fix(sdk): surface backend errors in SandboxBackend.grep#3471
Nikhil Reddy (Nik-Reddy) wants to merge 3 commits into
langchain-ai:mainfrom
Nik-Reddy:fix/grep-error-propagation

Conversation

@Nik-Reddy

@Nik-Reddy Nikhil Reddy (Nik-Reddy) commented May 19, 2026

Copy link
Copy Markdown

Closes #3441


grep() masks its own non-zero exit codes with 2>/dev/null || true, but when the backend (e.g., a container exec) fails before the shell starts, that redirection never takes effect: the backend's error text lands in result.output with a non-zero exit_code, and the match parser then crashes with ValueError: invalid literal for int() with base 10: ' exec failed' while reading the second :-delimited field as a line number.

This mirrors the convention already used by ls/read/edit/glob (after #3359): check exit_code first and return a structured GrepResult(error=...) instead of raising.

As defense in depth, the match parser also rejects any line that doesn't fit the path:line:text shape — whether the line-number field isn't an integer or the line has no colons at all — surfacing a structured error instead of silently dropping the line (which would mask the failure as a no-match). When this happens, any matches accumulated before the malformed line are discarded, matching glob()'s preference for a clean error over ambiguous partial output.

LocalSubprocessSandbox is intentionally unaffected: its shell always runs, so || true continues to force exit 0 on grep-side failures (no behavior change for the no-match-on-missing-path case there). The fix targets exactly the failure surface described in the issue — backend exec failures that produce output before the shell is reached.

Note: Pierre Larochelle (@pierrel) mentioned on the issue that they had a fix ready. Happy to defer if their work is further along — closing this PR if so. Posting in parallel given the issue is two weeks old and the bug is currently blocking grep against any Docker-style sandbox with an unreachable path.

`grep()` builds a shell pipeline that appends `2>/dev/null || true`,
which masks grep's own non-zero exit codes. When the backend (e.g., a
container exec) fails before the shell starts, the `||` never runs;
the backend's error text lands in `result.output` with a non-zero
`exit_code`, and the parser then crashes with `ValueError: invalid
literal for int() with base 10: ' exec failed'` while trying to read
the second `:`-delimited field as a line number.

This mirrors the fix landed for ls/read/edit/glob: check `exit_code`
first and surface a structured `GrepResult(error=...)` consistent with
the other backend operations. As a defense-in-depth measure, the
match-line parser also catches `ValueError` from the int conversion
and returns the same structured error rather than raising.

Closes langchain-ai#3441
@github-actions github-actions Bot added deepagents Related to the `deepagents` SDK / agent harness fix A bug fix (PATCH) size: S 50-199 LOC labels May 19, 2026
@langchain-oss-automated-triage langchain-oss-automated-triage Bot added new-contributor First-time contributor to this repo external User is not a member of the `langchain-ai` GitHub organization labels May 19, 2026
@github-actions github-actions Bot added the missing-issue-link PR missing a linked issue label May 19, 2026
@github-actions

This comment has been minimized.

@Nik-Reddy

Copy link
Copy Markdown
Author

Closing this in favor of #3637, which landed on main with the same exit_code-first plus structured GrepResult(error=...) approach. The additional grep -Z NUL-delimited filename change in #3637 is a nice orthogonal hardening for paths containing colons.

Thanks for picking up the design and for the in-branch refinements on 7480fd6. The original root-cause writeup and test plan from 2026-05-19 lives on issue #3441 for the audit trail.

Nothing left to merge here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepagents Related to the `deepagents` SDK / agent harness external User is not a member of the `langchain-ai` GitHub organization fix A bug fix (PATCH) new-contributor First-time contributor to this repo size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SandboxBackend.grep crashes with ValueError when container exec fails

2 participants