Skip to content

fix(tooldefer): never add our search to a Responses request that has its own (#876) - #897

Merged
amiddavid merged 2 commits into
mainfrom
feat/codex-native-876
Nov 4, 2026
Merged

amiddavid merged 2 commits into
mainfrom
feat/codex-native-876

Conversation

@amiddavid

@amiddavid amiddavid commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #876.

Behavior change

  • Who: Codex users (Responses wire) on ultrahigh. Any Responses client that declares a tool_search tool.
  • Old: the proxy adds context_guru_tools and removes some tools from tools. Codex's own search indexes only the tools Codex deferred itself, so a removed tool is out of its reach.
  • New: the proxy adds nothing and defers nothing on such a request. It sends the request as the client built it.
  • Off: leave ultrahigh.

Summary

  • Rule: a native search tool is used when there is one. Our search is added only when there is none.
  • Codex's own search indexes only tools that Codex itself deferred. A tool the proxy removed would be out of its reach. So the proxy defers nothing here.
  • No extra token saving for Codex. Real deferral for Codex needs a larger design (see Open questions).

What changed

  • ApplySearch returns native_search when a Responses request declares a tool_search tool.
  • NativeLoads reads the tool_search_output items Codex echoes in input. A load counts as a use in the history.
  • Chat Completions is unchanged and never gets defer_loading.

Data dependence

None new. Tool names only. No row in docs/data-dependence.md changes.

How it was tested

  • Unit: native request keeps its bytes; the same body without a native search gets today's rewrite; Chat wire not affected; loads read from echoed items (namespace and tools).
  • Proxy test: request passes unchanged; the load is a use in the history.
  • Mutation: NativeSearch forced to false. Both new tests fail. Restored.
  • Box 2: go test -race ./tooldefer/... ok. go test -race ./proxy/... ok (510 s).

Open questions

  • No model gate was added: nothing is deferred on this path, so a gate has nothing to guard.
  • Real deferral for Codex (answer its tool_search_call in band) is being measured separately.

🤖 Generated with Claude Code

…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 amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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:

  1. ApplySearch order (tooldefer/clientsearch.go:327). The native_search case sits after the wire, session, store, account and model checks, and before the gate and the no_tools check. On a native-search request the function returns Body unchanged and writes no record. So no 400 round-trip, no gate poisoning (the old path returned 400 "Only one tool_search tool is allowed", then deferRetry sent the original). Fail open holds: the pristine body goes out.
  2. The load booking (proxy/tooldefer.go:470). NativeLoads reads names from tool_search_output items in the pristine input. NativeSearch gates 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. Observe writes sets, so a full transcript re-observed each turn stays idempotent.
  3. The namespace name works. A namespace result adds both mcp__w__ and its tools as uses. inSet treats a name ending in __ as a server prefix (tooldefer/tooldefer.go:237), so the namespace entry vetoes the whole server unit in Decide. That is the intended "a load counts as a use".
  4. The response side. With native_search the handler returns di == nil, so searchOn stays 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.
  5. NativeSearch and NativeLoads cannot panic. gjson reads are nil-safe, and no index arithmetic runs on the results. A tools_search_output string 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 before NativeSearch(wire, orig). Every Chat and Responses request pays one full body copy (string(body) inside the prefilter) before the cheap tools scan could say no. Swap the two tests and use bytes.Contains on the raw bytes. repairSearchResults shares 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 at native_search and skips repairSearchResults. A leaked search call's "unknown tool" answer then stays in the transcript. The client must add tool_search mid-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:182 is now incomplete. The ultrahigh row says the tool is removed and loaded through context_guru_tools on 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 in proxy/proxy.go.
  • That orig is 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: session n1 is newest, and LoadView puts Loaded1 in both declared and used.
  • That no Why consumer outside tooldefer switches on the new native_search value (grep over proxy, dash, components).
  • The PR body names the behavior change (who, old, new, how to turn it off) as CLAUDE.md requires, and the data-dependence claim holds: tool names only, already covered by the toolfilter and history rows in docs/data-dependence.md.
  • Issue #876 for the mechanism and the OpenAI 400.

Live tests were not approved, so I read the code only.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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, a type: "function" tool, and kept the client's one tool_search. Nothing in this repo ever added a second tool_search entry. The measured 400 in #876 ("Only one tool_search tool is allowed") came from a test shim that added a literal second tool_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 tools is not observed. NativeSearch needs a tools array. A Responses turn with previous_response_id and no tools, carrying a new tool_search_output item, records no use. Effect: the history can under-count a tool the user loads. Narrow case: a stateful client that omits tools. Codex sends its tools every turn (issue #876, step 2). The same turn is declined by ApplySearch as no_tools today, so nothing else changes for it.
  • proxy/tooldefer_search_test.go:513 — the test does not assert the tool_search tool itself survived. It checks that Workflow stayed and our tool is absent. It does not check that the client's tool_search entry 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 no SearchRecord and consumes no pending opt-out. Correct: nothing is deferred on that path.
  • Traced the history observe at proxy/tooldefer.go:470: LoadView marks the loaded names as declared and used; Mem.Observe dedupes by name, so the same echo on every request of a thread adds one use, not many. A namespace name such as mcp__w__ works with inSet's server-unit matching, so a namespace load counts as a use of that server. The gate (NativeSearch) needs tools, 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 ApplySearch is 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/responses maps to provider OpenAI, wire responses; applyToolDefer reaches applyClientSearch only there, inside the not-observe, not-bypassed gate, as before.
  • Verified both new tests are mutation-sensitive: with NativeSearch forced off, the proxy test's request would lose Workflow and gain context_guru_tools, and TestApplySearchNativeSearchWins would report a rewrite. The history assertion (Used["Loaded1"]) fails only through the new NativeLoads observe.
  • Read issue #876 and its two probe comments: Codex 0.160.0 on gpt-5.5 sends {"type":"tool_search","execution":"client"}, echoes tool_search_call/tool_search_output items, and its search indexes only its own deferred tools. The measured output shape (functions with name, namespaces with name + tools) matches what NativeLoads reads. 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_test needs 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]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Round 1 notes, fixed in the new head:

  1. Comment reworded. The old path added only the context_guru_tools function, never a second tool_search, so the 400 is not named. The true reason is that Codex's search indexes only tools Codex deferred itself. The PR body says the same.
  2. docs/reference/presets.md ultrahigh row has the new sentence.
  3. The test asserts the client's tool_search entry reaches the upstream, with a Responses-shaped answer.
  4. bytes.Contains and NativeSearch now run before NativeLoads.
  5. Left as is. Chained turn with no tools: Codex sends tools on every turn. Mid-thread switch: no realistic path to it.

Box 2: gofmt and vet clean, -race ./tooldefer/... ok. ./proxy/... had one failure, TestALateFirstByteOnTheRealTransportNeverReachesTheClient (a 5 ms timing test, not in my files). It passes 3 of 3 on rerun with the new tests.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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:328 now 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 tools is not observed): rejected by the author, and I accept the reason. The author says Codex sends tools on every turn. Issue #876's step-2 probe confirms it: gpt-5.5 requests hold 10+ function tools and one tool_search each turn. The gap needs a stateful client that omits tools, and the same turn already gets no deferral decision today.
  • Mine 3 (the test did not assert the client's tool_search survived): 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 runs bytes.Contains on the raw bytes first, then NativeSearch, and calls NativeLoads last. bytes was 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_search mid-thread after a mixed search. Even then, every tool stays declared and the request stays valid.
  • Reviewer 2, presets.md missing the exception: fixed. The ultrahigh row ends with one sentence: a Responses request that declares its own tool_search keeps 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 if branch. The new gate keeps its "no" case as an empty block with a comment, then puts the work in else if. Valid Go, and the behavior is the same as round 1. A plain if bytes.Contains(...) && tooldefer.NativeSearch(...) { if loads := ...; len(loads) > 0 { ... } } would read without the inversion. Style only.
  • NativeLoads keeps its own strings.Contains prefilter (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 applyClientSearch is semantically identical to round 1's: loads are booked only when the body holds a tool_search_output, the request declares a tool_search, and NativeLoads returns 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_search bytes come from its tools array, so the assertion is strict and mutation-sensitive.
  • The presets.md sentence matches the code: NativeSearch gates only the Responses wire, and ApplySearch defers 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 amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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):

  1. Condition order / body copy — fixed. proxy/tooldefer.go:470 now runs bytes.Contains(orig, ...) and NativeSearch before NativeLoads. 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, NativeSearch is true, and the loads are non-empty. That is the old condition, because NativeLoads returns non-empty only when its own prefilter matches. bytes was already imported.
  2. 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_search mid-thread after a mixed search. The impact stays one stale error text.
  3. docs/reference/presets.md incomplete — fixed. The ultrahigh row ends with: a Responses request that declares its own tool_search keeps it, and nothing is deferred on it (#876). Accurate.
  4. Test shape — fixed. The upstream answer is now a Responses object, and the test also asserts the client's tool_search entry reaches the upstream byte for byte.

Reviewer 1 (round 1):

  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.
  2. A load on a chained turn that omits tools — accept the author's reason. NativeSearch needs a tools array, and Codex sends tools every turn (issue #876). The same turn is declined as no_tools by ApplySearch today, so this observe adds no new gap. Kept out.
  3. The test did not assert tool_search survived — fixed. The new bytes.Contains(sent[0], '{"type":"tool_search","execution":"client"}') covers it.

The new commit, reviewed as a first review

  • proxy/tooldefer.go restructure. Empty if branch, then else if with 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: format rewrites only tool-role message content (components/reformat/format.go:40), never the tools array, 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.go comment. Only the comment text changed; the native_search case and its position in the switch are untouched from the head I approved in round 1.
  • docs/reference/presets.md. One sentence added to the ultrahigh row. Matches the code: NativeSearch on the Responses wire, nothing deferred, nothing added.

Non-blocking

  • proxy/tooldefer.go:470 — the empty if branch. 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, the native_search case, and Format.Reformat for the byte-survival question.
  • That bytes is imported in proxy/tooldefer.go and that the new condition is equivalent to the old one.

Live tests were not approved, so I read the code only.

amiddavid added a commit that referenced this pull request Nov 4, 2026
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]>
@amiddavid
amiddavid merged commit dd3cfa6 into main Nov 4, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 4, 2026
amiddavid added a commit that referenced this pull request Nov 4, 2026
…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]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Summary

Live check of #897 passed. Codex keeps its own tool_search. The proxy defers nothing on it.

Setup

Result

Run Route Status Time (ms) Tools Codex sent
1 /v1/responses 200 5782 10 tools incl. tool_search
2 /v1/responses 200 4732 same
3 /v1/responses 200 17408 same (captured)
  • tool_search kept. The captured request holds 8 function/custom tools, tool_search and web_search.
  • No context_guru_tools. No defer_loading in the body either.
  • Nothing deferred. /stats tool_defer: decisions 0, requests 0, add_backs 0, retries_400 0.
  • No 400. All requests answered 200.

Limits

  • The capture shows the request as Codex sent it. It is not a dump of the upstream bytes. The "nothing deferred" proof is the /stats counters plus the 200 answers.
  • Captured bodies were deleted after I read the tool names.

🤖 Generated with Claude Code

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant