Repository navigation
fix(tooldefer): never add our search to a Responses request that has its own (#876) - #897
Conversation
…its own (#876) A Responses request that declares a tool_search tool (Codex) is left alone: our context_guru_tools is not added and no tool is deferred. OpenAI refuses a second tool_search with a 400. The tools the client's search loaded, echoed as tool_search_output items in input, count as a use in the history. Assisted-By: Claude Opus 5.5 Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.317 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 1 of 2 | f6b2a4f0d525 |
approve |
Verdict: approve
Findings
None. I could not prove a real defect. The traces I ran:
ApplySearchorder (tooldefer/clientsearch.go:327). Thenative_searchcase sits after the wire, session, store, account and model checks, and before the gate and theno_toolscheck. On a native-search request the function returnsBodyunchanged and writes no record. So no 400 round-trip, no gate poisoning (the old path returned 400 "Only one tool_search tool is allowed", thendeferRetrysent the original). Fail open holds: the pristine body goes out.- The load booking (
proxy/tooldefer.go:470).NativeLoadsreads names fromtool_search_outputitems in the pristineinput.NativeSearchgates it, so the loads are booked only when the client declares its own search. With our search active there is no native tool, so the two Observe paths never double-count one load.Observewrites sets, so a full transcript re-observed each turn stays idempotent. - The namespace name works. A namespace result adds both
mcp__w__and its tools as uses.inSettreats a name ending in__as a server prefix (tooldefer/tooldefer.go:237), so the namespace entry vetoes the whole server unit inDecide. That is the intended "a load counts as a use". - The response side. With
native_searchthe handler returnsdi == nil, sosearchOnstays false (proxy/proxy.go:1806) and the streaming and in-band paths are skipped. Nothing of ours is in flight, so nothing is held or spliced. NativeSearchandNativeLoadscannot panic. gjson reads are nil-safe, and no index arithmetic runs on the results. Atools_search_outputstring inside message text only makes the prefilter run the loop, which finds no items.
Non-blocking
proxy/tooldefer.go:470— condition order.NativeLoads(orig)runs beforeNativeSearch(wire, orig). Every Chat and Responses request pays one full body copy (string(body)inside the prefilter) before the cheaptoolsscan could say no. Swap the two tests and usebytes.Containson the raw bytes.repairSearchResultsshares the pattern, so this is consistency, not a new cost class.- Mid-thread switch is unrepaired. A thread our search already rewrote, whose client later declares a native
tool_search, bails atnative_searchand skipsrepairSearchResults. A leaked search call's "unknown tool" answer then stays in the transcript. The client must addtool_searchmid-thread after a mixed search for this; the impact is one stale error text, and every tool stays declared. I found no realistic path to it. docs/reference/presets.md:182is now incomplete. Theultrahighrow says the tool is removed and loaded throughcontext_guru_toolson GPT, with no word about the native-search exception. One sentence would fix it.- Test shape. The new proxy test's upstream answers a Responses route with a Chat Completions body. Harmless, but a Responses-shaped answer would match the route.
What I checked
- The full diff of all four files, and the code around each change:
ApplySearch,applyClientSearch,View/LoadView,Mem.Observe,UserSessions,Decide's server units, and the response-side gating inproxy/proxy.go. - That
origis the pristine inbound body (proxy/proxy.go:1318), so both new reads see the client's own declaration, not the pipeline's rewrite. - Both new tests by hand: bodies, helpers (
csHandler,respBody,sreq,env) and assertions. The proxy test's history assertion is meaningful: sessionn1is newest, andLoadViewputsLoaded1in bothdeclaredandused. - That no
Whyconsumer outsidetooldeferswitches on the newnative_searchvalue (grep overproxy,dash,components). - The PR body names the behavior change (who, old, new, how to turn it off) as
CLAUDE.mdrequires, and the data-dependence claim holds: tool names only, already covered by thetoolfilterand history rows indocs/data-dependence.md. - Issue #876 for the mechanism and the OpenAI 400.
Live tests were not approved, so I read the code only.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.317 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 1 of 2 | f6b2a4f0d525 |
approve |
Verdict: approve
Findings
None.
Non-blocking
- tooldefer/clientsearch.go:329 — the comment names a 400 mechanism the old code could not produce. The comment says "OpenAI refuses a second tool_search with a 400". The old rewrite added
context_guru_tools, atype: "function"tool, and kept the client's onetool_search. Nothing in this repo ever added a secondtool_searchentry. The measured 400 in #876 ("Only one tool_search tool is allowed") came from a test shim that added a literal secondtool_search. The fix is still right for the other stated reason: Codex's client search indexes only the tools Codex deferred itself, so a tool the proxy removed would be out of its reach. Suggest rewording the comment, or naming where the old-path 400 was observed. - proxy/tooldefer.go:470 — a load on a chained turn that omits
toolsis not observed.NativeSearchneeds atoolsarray. A Responses turn withprevious_response_idand notools, carrying a newtool_search_outputitem, records no use. Effect: the history can under-count a tool the user loads. Narrow case: a stateful client that omitstools. Codex sends its tools every turn (issue #876, step 2). The same turn is declined byApplySearchasno_toolstoday, so nothing else changes for it. - proxy/tooldefer_search_test.go:513 — the test does not assert the
tool_searchtool itself survived. It checks thatWorkflowstayed and our tool is absent. It does not check that the client'stool_searchentry reached the upstream body. A pipeline rewrite that dropped unknown tool types would pass this test. Minor: the pipeline is not this PR's change.
What I checked
- Read the full diff (4 files, one commit) and the code around each change:
applyClientSearch,ApplySearch,rewriteSearch,Mem.Observe,DeclaredNames,recentUses,usedInTranscript,inSet. - Traced the new early return in
ApplySearch: it sits among checks that all decline, so its order changes no behavior. It runs before any record is created, so a native-search thread writes noSearchRecordand consumes no pending opt-out. Correct: nothing is deferred on that path. - Traced the history observe at proxy/tooldefer.go:470:
LoadViewmarks the loaded names as declared and used;Mem.Observededupes by name, so the same echo on every request of a thread adds one use, not many. A namespace name such asmcp__w__works withinSet's server-unit matching, so a namespace load counts as a use of that server. The gate (NativeSearch) needstools, which the first observe at line 468 already requires. - Checked fail-open: the new code is two pure parsers plus one history write. No path can alter or drop a request. A panic in
ApplySearchis covered by its recover; the proxy-side observe uses gjson/sjson, which do not panic. - Checked callers: only the proxy calls
ApplySearch,NativeSearch,NativeLoads. No other host (bifrost adapter, Cortex) is affected. - Checked the routing:
/openai/v1/responsesmaps to provider OpenAI, wireresponses;applyToolDeferreachesapplyClientSearchonly there, inside the not-observe, not-bypassed gate, as before. - Verified both new tests are mutation-sensitive: with
NativeSearchforced off, the proxy test's request would loseWorkflowand gaincontext_guru_tools, andTestApplySearchNativeSearchWinswould report a rewrite. The history assertion (Used["Loaded1"]) fails only through the newNativeLoadsobserve. - Read issue #876 and its two probe comments: Codex 0.160.0 on gpt-5.5 sends
{"type":"tool_search","execution":"client"}, echoestool_search_call/tool_search_outputitems, and its search indexes only its own deferred tools. The measured output shape (functions withname, namespaces withname+tools) matches whatNativeLoadsreads. The PR is the "safe subset" the issue recommends. - Checked the repo rules: no behavior change rides unmentioned (the PR names it at the top, with old, new, and off). Data dependence: tool names only; the existing Tool deferral history row in docs/data-dependence.md already covers "used or loaded". No new metric, no new component, so
datadep_testneeds no row.
… the doc line (#876) Reword the comment: Codex's search indexes only the tools Codex deferred itself. Add a presets.md sentence. The test checks the client's tool_search reaches the upstream, with a Responses-shaped answer. The proxy checks NativeSearch before reading loads. Assisted-By: Claude Opus 5.5 Signed-off-by: DAVID AMID <[email protected]>
|
Round 1 notes, fixed in the new head:
Box 2: gofmt and vet clean, |
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.317 | offload-review skill, @amiddavid's session | resumed | 1 of 2 | 2 of 2 | b17b95a78fc7 |
approve |
Verdict: approve
Findings
None.
Status of earlier findings
- Mine 1 (comment named a 400 the old code could not produce): fixed. The comment in
tooldefer/clientsearch.go:328now states the true reason only: Codex's search indexes the tools Codex deferred itself. The PR body's Old line was rewritten the same way. Nothing claims the 400 anymore. - Mine 2 (a load on a chained turn without
toolsis not observed): rejected by the author, and I accept the reason. The author says Codex sendstoolson every turn. Issue #876's step-2 probe confirms it: gpt-5.5 requests hold 10+ function tools and onetool_searcheach turn. The gap needs a stateful client that omitstools, and the same turn already gets no deferral decision today. - Mine 3 (the test did not assert the client's
tool_searchsurvived): fixed. The test now requires the exact bytes{"type":"tool_search","execution":"client"}in the upstream body. It fails if the pipeline drops or reorders the entry. - Reviewer 2, condition order and
string(body)copy: fixed. The gate now runsbytes.Containson the raw bytes first, thenNativeSearch, and callsNativeLoadslast.byteswas already imported. - Reviewer 2, mid-thread switch leaves a stale "unknown tool" answer: rejected by the author, and I accept. Reviewer 2 also found no realistic path to it. It needs a client that adds
tool_searchmid-thread after a mixed search. Even then, every tool stays declared and the request stays valid. - Reviewer 2,
presets.mdmissing the exception: fixed. Theultrahighrow ends with one sentence: a Responses request that declares its owntool_searchkeeps it, and nothing is deferred on it. It matches the code. - Reviewer 2, the test's answer was Chat-shaped on a Responses route: fixed. The upstream now answers with a Responses object.
Non-blocking
- proxy/tooldefer.go:470 — the empty
ifbranch. The new gate keeps its "no" case as an empty block with a comment, then puts the work inelse if. Valid Go, and the behavior is the same as round 1. A plainif bytes.Contains(...) && tooldefer.NativeSearch(...) { if loads := ...; len(loads) > 0 { ... } }would read without the inversion. Style only. NativeLoadskeeps its ownstrings.Containsprefilter (tooldefer/clientsearch.go:730). The proxy gate now checks the same string first, so the inner copy of the body runs only after the outer gate passed. Redundant, harmless.- PR body, How it was tested still cites the round-1 box run. The author's comment reports the new run (
./tooldefer/...ok;./proxy/...had one unrelated timing flake, passing 3 of 3 on rerun). The body was not refreshed. Documentation only.
What I checked
- Last review: round 1, f6b2a4f, 2026-11-04T16:25:57Z.
- New commits read: 1 — b17b95a "fix(tooldefer): state the true reason for the native-search rule, add the doc line (#876)".
- Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files in my folder).
- New items since my last review: the commit b17b95a (diff read in full, all four files); the author's comment of 2026-11-04T16:37:16Z (round-1 dispositions and the new box-2 run); the updated PR body (the Old line no longer claims a 400). Reviewer 2's round-1 review was posted before my round-1 review; I read it now for the first time and checked each of its notes against the new head myself.
- The new gate in
applyClientSearchis semantically identical to round 1's: loads are booked only when the body holds atool_search_output, the request declares atool_search, andNativeLoadsreturns names. Only the check order and the byte-level prefilter changed. No behavior change rides with it. - The strengthened test: traced the assertion against the request body built in the test — the exact
tool_searchbytes come from itstoolsarray, so the assertion is strict and mutation-sensitive. - The
presets.mdsentence matches the code:NativeSearchgates only the Responses wire, andApplySearchdefers nothing on that path. - Fail-open, callers, and history semantics are unchanged from round 1; I re-read the touched regions to confirm the commit did not move anything else.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.317 | offload-review skill, @amiddavid's session | resumed | 2 of 2 | 2 of 2 | b17b95a78fc7 |
approve |
Verdict: approve
Findings
None. The new commit changes no behavior, and every fix it claims is in the diff.
Round-1 findings, status
Mine (reviewer 2, round 1):
- Condition order / body copy — fixed.
proxy/tooldefer.go:470now runsbytes.Contains(orig, ...)andNativeSearchbeforeNativeLoads. The[]byte("tool_search_output")conversion is a compile-time constant, so the per-request cost is one allocation-free scan. I traced the equivalence: the observe fires exactly when the substring is present,NativeSearchis true, and the loads are non-empty. That is the old condition, becauseNativeLoadsreturns non-empty only when its own prefilter matches.byteswas already imported. - Mid-thread switch unrepaired — left as is, and I agree. The author's reason matches mine from round 1: no realistic path to it. The client must add
tool_searchmid-thread after a mixed search. The impact stays one stale error text. docs/reference/presets.mdincomplete — fixed. Theultrahighrow ends with: a Responses request that declares its owntool_searchkeeps it, and nothing is deferred on it (#876). Accurate.- Test shape — fixed. The upstream answer is now a Responses object, and the test also asserts the client's
tool_searchentry reaches the upstream byte for byte.
Reviewer 1 (round 1):
- The 400 claim in the comment — fixed, and the reword is right. The new comment states only the reach reason: Codex's client search indexes only the tools Codex deferred itself. That reason is the one issue #876 states and reviewer 1 verified. Dropping the 400 sentence removes the claim neither of us could source.
- A load on a chained turn that omits
tools— accept the author's reason.NativeSearchneeds atoolsarray, and Codex sendstoolsevery turn (issue #876). The same turn is declined asno_toolsbyApplySearchtoday, so this observe adds no new gap. Kept out. - The test did not assert
tool_searchsurvived — fixed. The newbytes.Contains(sent[0], '{"type":"tool_search","execution":"client"}')covers it.
The new commit, reviewed as a first review
proxy/tooldefer.gorestructure. Emptyifbranch, thenelse ifwith the observe. Compiles, gofmt-clean per the author's box run, and equivalent to the old condition (trace above). No path can alter or drop a request.- The new byte assertion. I checked the risk myself:
formatrewrites only tool-role message content (components/reformat/format.go:40), never thetoolsarray, so the element's bytes survive the pipeline. The author's box run (-race ./proxy/..., one unrelated 5 ms timing failure, new tests passing) confirms it live. I could not run Go here. tooldefer/clientsearch.gocomment. Only the comment text changed; thenative_searchcase and its position in the switch are untouched from the head I approved in round 1.docs/reference/presets.md. One sentence added to theultrahighrow. Matches the code:NativeSearchon the Responses wire, nothing deferred, nothing added.
Non-blocking
proxy/tooldefer.go:470— the emptyifbranch.if !A || !B { // comment } else if ...reads backwards for a guard.if bytes.Contains(...) && tooldefer.NativeSearch(...) { if loads := ...; len(loads) > 0 { ... } }says the same without the empty branch. Cosmetic.
What I checked
- Last review: round 1, f6b2a4f, 2026-11-04T16:21:17Z.
- New commits read: 1 (
b17b95a78fc7"fix(tooldefer): state the true reason for the native-search rule, add the doc line (#876)"). - Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files
.review-in-reviews.txt,.review-in-comments.txt,.review-in-inline.txt). - New items since my last review: the round-1 review by reviewer 1 (approve, 3 non-blocking), the author's fix comment at 16:37:16Z, and commit
b17b95a78fc7. I checked each round-1 finding against the new diff, and the author's two "left as is" reasons against the code and issue #876. - The full new diff (4 files, 12 insertions, 7 deletions), plus the code around each change:
applyClientSearch, thenative_searchcase, andFormat.Reformatfor the byte-survival question. - That
bytesis imported inproxy/tooldefer.goand that the new condition is equivalent to the old one.
Live tests were not approved, so I read the code only.
Codex on gpt-5.5 declares its own tool_search. After #897 the proxy adds no context_guru_tools to such a request and defers nothing on it. Our search is only for a Responses client that declares no tool_search of its own. Assisted-By: Claude Opus 5.5 Signed-off-by: DAVID AMID <[email protected]>
…898) - A break is a deferred tool's own error that says it is not loaded. A direct call that worked is a use (direct_calls). A tool_choice tool never breaks. - Client-mode loads leave DeferredTokens and count as loaded in the ledger. - An errored or unknown result books nothing. - client_loads is its own counter; add_backs stays for the #825 path. - The list weight in client mode is the client list. Probe logs say server or client. - Matrix rows marked "after #897"; data-dependence toolfilter row extended. Assisted-By: Claude Opus 5.5 Signed-off-by: DAVID AMID <[email protected]>
SummaryLive check of #897 passed. Codex keeps its own Setup
Result
Limits
🤖 Generated with Claude Code |
Refs #876.
Behavior change
tool_searchtool.context_guru_toolsand removes some tools fromtools. Codex's own search indexes only the tools Codex deferred itself, so a removed tool is out of its reach.Summary
What changed
ApplySearchreturnsnative_searchwhen a Responses request declares atool_searchtool.NativeLoadsreads thetool_search_outputitems Codex echoes ininput. A load counts as a use in the history.defer_loading.Data dependence
None new. Tool names only. No row in docs/data-dependence.md changes.
How it was tested
NativeSearchforced to false. Both new tests fail. Restored.go test -race ./tooldefer/...ok.go test -race ./proxy/...ok (510 s).Open questions
tool_search_callin band) is being measured separately.🤖 Generated with Claude Code