#6315 [Engine] serve: a tool call written in a shape its schema does not declare is read as the call it meant (#6285)

open · @MegaRushX · 0 comentários · No GitHub

Setup & installServer & APIWindowsLinux

Descrição

## Problem and result

From #6285 (erycsonero, measured over two agentic runs: 7 refusals in 270 tool calls and 9 in 330): a tool call whose
arguments do not match the tool's declared schema is passed through unchanged, so a client that validates the
arguments against the schema drops the call, one lost turn each. The dominant shape is the parameter name written
without its plural `s`: `run_commands` missing `commands`, `read_files` missing `files`.

Before (upstream main `58ee32277`, the issue's own probe, no engine and no model):

```text
<tool_call><function=run_commands><parameter=command>ls -la</parameter></function></tool_call>
-> {"command":"ls -la"}          the client drops it
```

After:

```text
-> {"commands":["ls -la"]}       the declared, required array of strings
```

## Related discussion

#6285. A focused compatibility fix like #4670, so straight to a PR per CONTRIBUTING.md.

## Approach

`repair_arguments(args, schema, notes)` runs where a call's arguments are final: at the end of `parse_tool_call`, and
on the held parameters in `_close_scan` so the streamed arguments equal the final call's. Three slips are read, each
only when the schema leaves one reading:

- the call's parameters inside one JSON object under an undeclared `arguments` (the OpenAI call shape), when every key
  is declared;
- a parameter named without its plural `s`, when the tool declares exactly one required parameter, that parameter is
  an array of strings, and the write is its name minus the `s` (`command` for `commands`, `file` for `files`, `query`
  for `queries`); its text becomes the array of one string;
- an array of strings written as an array of objects: each object's field named the parameter's singular, or the one
  string field it carries.

Anything the schema does not settle stays exactly as written: a tool that declares `command` itself has no slip to
guess, one with two required parameters is left alone, an object with two strings and no singular field is not
flattened, and a tool that really declares `arguments` keeps it. `tool_arguments()` (a history turn's arguments)
is untouched: this is the live parser only. Each repair appends one line to the parser's `notes`, so the existing
`[strata] N tool call argument(s) repaired: ...` line (#4427, server.py) names it.

Nothing outside `serve/frontend.py` changes; no default, no route, no schema. A call the schema does declare parses
byte for byte as before.

## Validation

Environment: Linux x86_64 sandbox, Python 3.11.6, source tree of `58ee32277`; no engine, no GPU, no model.

**Passed:**

- `cd terminal && python3 -m unittest serve.test_tool_corpus -v` → `Ran 4 tests ... OK`. The corpus reads every
  specimen whole, in pieces of 1, 3 and 7 bytes, and cut in two at every byte, with `stream_tools` on and off, and
  compares the streamed `tool_args` JSON with the final call's arguments. Seven new specimens: the issue's four rows
  and three guards (a declared `command` on a tool with two required parameters, a declared `arguments`, an object
  with two strings and no singular field).
- The issue's probe, before this change → `{"command": "ls -la"}`; after → `{"commands": ["ls -la"]}`, in the final
  call and in the streamed `tool_args`.
- The same corpus on the previous `frontend.py` (this change to the test only) → `FAILED (failures=2)`
  (`test_every_specimen_whole_and_in_pieces`, `test_every_specimen_cut_in_two_at_every_byte`), with
  `- [('run_commands', {'command': 'ls -la'})]` / `+ [('run_commands', {'commands': ['ls -la']})]`.
- 32 adjacent modules of `terminal/serve` as single modules, all `OK`: `test_frontend`, `test_bool_args`,
  `test_cut_arguments`, `test_history_tools`, `test_param_end_options`, `test_quoted_call_values`,
  `test_native_tool_parser`, `test_structured`, `test_responses`, `test_responses_agent_message`, `test_totals`,
  `test_request_hardening`, `test_stream_errors`, `test_stream_terminal`, `test_anthropic_tool_results`,
  `test_chat_tool_results`, `test_env_to_user`, `test_tool_routes`, `test_session_save_retry`, `test_detok`,
  `test_first_chunk`, and the `test_parser_*` / `test_tool_*` families.

**Failed:**

- On the unpatched file, the two corpus tests above (the expected failure, data in the diff above). None with the
  change in place.

**Not run:**

- `python tools/check --only py-serve` and `pytest terminal/serve/`: pytest is not installed in this sandbox, so the
  modules above ran as single `python3 -m unittest` units instead. A full `unittest discover` over `terminal/serve`
  timed out at 300 s here (it is large), so I did not claim the whole folder.
- Any engine, GPU, Windows or macOS path: this change touches only `terminal/serve/frontend.py`, which is plain text
  parsing, but I have no GPU and no Windows/macOS machine to run anything there.

## Limits

- Row 4's objects may carry more than the string that survives: `{"query": "x", "path": "y"}` becomes `["x"]` and the
  `path` member is not expressible in a declared array of strings. The issue's table asks for exactly this; a member
  the schema cannot carry is dropped with the repair noted, not sent under an undeclared key.
- The repair needs the tool's schema in the request, so a call for a tool whose schema the request does not declare
  is unchanged (that case is #6120's, not this one).
- In the streamed form the repaired parameters arrive with the call's other held (non-string) parameters, at the
  close of the call; a client reading the stream sees the repaired JSON from then, not incrementally.

No site

Links install, modelos, releases.