Skip to content

fix: propagate HTTP error statuses in API tool#3739

Open
Piyush0049 wants to merge 1 commit into
docker:mainfrom
Piyush0049:fix/api-http-errors
Open

fix: propagate HTTP error statuses in API tool#3739
Piyush0049 wants to merge 1 commit into
docker:mainfrom
Piyush0049:fix/api-http-errors

Conversation

@Piyush0049

Copy link
Copy Markdown
Contributor

Overview

This PR addresses a logic bug in the api tool where HTTP error responses (4xx and 5xx status codes) were previously swallowed and treated as successful executions. Additionally, it resolves a couple of test suite regressions related to Windows compatibility and unordered slice assertions.

Changes Included

Bug Fixes

  • API Tool Error Propagation: Updated pkg/tools/builtin/api/api.go to explicitly check resp.StatusCode. If the status code is >= 400, the tool now properly returns a tools.ResultError containing the status code, the status text, and the truncated response body.
  • API Tool Tests: Added TestAPITool_ErrorStatusCode to verify that 500 Internal Server Error responses correctly generate an error result instead of a success object.

Testing and CI Improvements

  • Windows Compatibility for Mkfifo: Extracted the TestAgentRefsInDirSkipsNonRegularFiles test (which uses syscall.Mkfifo) out of agent_picker_test.go and into a dedicated agent_picker_unix_test.go file with a //go:build !windows directive. This cleanly resolves cross-platform compilation and test execution issues on Windows.
  • Cross-Platform Paths: Updated hardcoded Unix-style paths in agent_picker_test.go to use filepath.FromSlash to ensure the tests pass smoothly on Windows environments.
  • Unordered Matching: Switched from assert.Equal to assert.ElementsMatch in sandbox_cmd_test.go for SandboxAllowlist assertions, preventing flaky test failures caused by non-deterministic map or slice ordering.

This fixes a logic bug where the API tool incorrectly swallows 4xx/5xx HTTP errors and returns them as successful tool executions. Also includes fixes for upstream Windows test regressions (syscall.Mkfifo and unordered map matching).
@Piyush0049
Piyush0049 requested a review from a team as a code owner July 18, 2026 15:43
@aheritier aheritier added area/cli CLI commands, flags, output formatting area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Jul 18, 2026

@aheritier aheritier left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Automated implementer agentthis comment was posted by the implementer bot from Docker Agentic Platform, not by a human developer

The core fix is correct. Returning tools.ResultError (not a Go error) is the right choice — it surfaces the HTTP failure to the LLM with status code, reason phrase, and truncated body so the model can react rather than silently treating an error response as success.

A few observations:


  • Threshold >= 400 is correct; 3xx redirects are already resolved by Go's HTTP client before this point.
  • limitOutput on the error body is appropriate, though the body is already capped at 1 MB by io.LimitReader; the 30 KB limit from limitOutput makes the effective cap 30 KB in both paths — fine.
  • http.StatusText for unregistered status codes returns "", producing a slightly odd "status 999 : ..." string. Negligible in practice.

Tests

  • TestAPITool_ErrorStatusCode covers the 500 path correctly; a 4xx case (e.g. 401, 404) would round out coverage, but absence isn't a blocker given the code path is identical for all >= 400.

Supporting changes

  • Extracting TestAgentRefsInDirSkipsNonRegularFiles to agent_picker_unix_test.go with //go:build !windows is the right approach.
  • filepath.FromSlash and assert.ElementsMatch changes are appropriate cross-platform cleanups.

Overall: clean, minimal, correct.

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

Labels

area/cli CLI commands, flags, output formatting area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants