Repository navigation
fix(/v1/responses): drop the extra phantom assistant turn on client-tool calls - #71062
PopFlamingo wants to merge 3 commits into
Conversation
Greptile SummaryThis PR fixes two independent bugs in
Confidence Score: 4/5Safe to merge after fixing the stale The two primary fixes ( src/gateway/openresponses-http.ts — line 1103:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d06063b487
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
d06063b to
0e92a44
Compare
The wrapped execute() callback that the adapter builds for each client-tool definition returns `terminate: true` on its handoff stub. That flag is Pi's native shouldTerminateToolBatch signal, so the agent loop stops after this batch instead of feeding the placeholder back as a normal tool result and running another model turn that would narrate the stub alongside the real function_call. The outer runner still observes clientToolCallDetected and surfaces the real function_call through pendingToolCalls. Piggybacked on top of that, the gateway's SSE event sequence for streamed function_call output items is tightened to follow the OpenAI Responses-API streaming convention more closely: response.output_item.added (with item.arguments = "" as an in-progress placeholder), response.function_call_arguments.delta and response.function_call_arguments.done carrying the args string, and response.output_item.done with item.arguments populated as the finalization snapshot. The dedicated args events are the source the streaming convention reserves for incremental args; the empty placeholder on .added leaves seed-and-append accumulators no string to double. The upstream Responses-stream parser in openai-transport-stream.ts also gains a handler for response.function_call_arguments.done that assigns event.arguments to currentBlock.partialJson and emits a synthetic toolcall_delta for any tail not carried by intermediate deltas, so the toolcall_end event carries the parsed args even when an upstream delivers them only via .done. Regression tests pin the terminate-true flag on the handoff stub (pi-tool-definition-adapter.test.ts), the SSE event sequence for streamed function_call output items (openresponses-http.test.ts), and the .done branch's args recovery (openai-transport-stream.test.ts).
0e92a44 to
ea31069
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ea31069edb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…em.type The four-event-sequence regression test used `eventTypes.findIndex(t => t === 'response.output_item.added')` to locate the function_call's bookend events. But `output_item.added` and `output_item.done` each fire twice per turn — once for the assistant message that precedes the function_call, and once for the function_call itself. `findIndex` matched the first occurrence (the assistant message), so the ordering assertion `fcArgsDeltaIdx > fcAddedIdx` passed trivially against the assistant message's added event. The function_call's bookend events could have been missing entirely or reordered after the args events, and the test would still pass. Filter events by the embedded `item.type === 'function_call'` so the indices point at the function_call's own bookend events. The ordering assertion now meaningfully pins the four-event sequence. Caught by Codex review on PR openclaw#71062.
Model<"openai-responses"> requires `baseUrl` (added on main between when this branch first opened and the rebase). The .done-handler regression test's model fixture didn't carry it, so check-test-types on CI failed with TS2741 "Property 'baseUrl' is missing". Local pnpm tsgo:prod (production graph) doesn't include .test.ts files and so didn't catch this; pnpm tsgo:test (the gate CI runs) does. Caught by CI on PR openclaw#71062.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Thanks for the context here. I swept through the related work, and this is now duplicate or superseded. Keep open: this is a useful bug-fix direction, but it is not merge-ready because current main only partially covers it, the PR now conflicts with current main, and the external-contributor real behavior proof is still missing. Canonical path: Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review. So I’m closing this here because the remaining work is already tracked in the canonical issue. Review detailsBest possible solution: Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review. Do we have a high-confidence way to reproduce the issue? Yes, source inspection shows current main still does not emit the dedicated Is this the best way to solve the issue? No, not as submitted: the fix direction is plausible and backed by the OpenAI Node stream accumulator contract, but the branch needs to be ported onto current main and the Security review: Security review cleared: No concrete security or supply-chain regression was found; the diff changes gateway/agent API behavior and tests without new dependencies, credentials, permissions, or execution sources. AGENTS.md: found and applied where relevant. What I checked:
Likely related people:
Codex review notes: model internal, reasoning high; reviewed against 8ed6c78b7891. |
|
ClawSweeper PR egg 🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat. Where did the egg go?
|
|
This pull request has been automatically marked as stale due to inactivity. |
|
This pull request has been automatically marked as stale due to inactivity. |
|
ClawSweeper status: review started. I am starting a fresh review of this pull request: fix(/v1/responses): drop the extra phantom assistant turn on client-tool calls This is item 1/1 in the current shard. Shard 19/22. This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking. Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted. |
|
Closing due to inactivity. |
Summary
Describe the problem and fix in 2–5 bullets:
/v1/responsesclient-tool calls (tools the API caller passes intools: [...]for the agent to delegate back to them):function_call. Symptom: the model first denies it can run the tool, then seconds later in the same SSE response claims it did — a self-contradictory mini-transcript every caller saw, regardless of how their parser was built.item.argumentson theoutput_item.added/output_item.doneevents, instead of using the dedicatedresponse.function_call_arguments.delta/.doneevents that the Responses-API streaming convention reserves for incremental function-call arguments. Clients whose parsers follow that convention strictly — only reading args from those dedicated events — sawarguments == "{}"for every call, because the dedicated events simply weren't being emitted. More permissive clients (e.g. OpenAI's Node SDK, which also readsitem.argumentsdirectly) recovered the args from the bundled-in shape and worked anyway.Two unrelated root causes combined to produce these symptoms. (1) The Pi-side adapter for client-tool definitions returned a placeholder stub from its
execute()callback withoutterminate: true, so Pi treated the stub as a normal tool result and ran another model turn before the runner observed the handoff — that extra turn is what leaked into the stream as the contradictory phantom narration. (2) The/v1/responsesSSE emitter never emitted the dedicatedresponse.function_call_arguments.delta/.doneevents./v1/responsescouldn't be relied on for client-tool calls — every caller (lenient or strict) saw the conversation contaminated by the contradictory phantom narration, and on top of that, strict clients lost the args entirely.terminate: trueon its handoff stub, so Pi stops the agent loop after this batch and the placeholder stub does not produce another model turn. Piggybacked on top of that, the gateway's SSE event sequence for streamed function_call output items is tightened to follow the Responses-API streaming convention more closely (output_item.addedwith empty placeholder →function_call_arguments.delta→function_call_arguments.done→output_item.donewith populated args), and the upstream Responses-stream parser also gains a handler forresponse.function_call_arguments.doneto cover the case where an upstream delivers the full args only via.done./v1/responsesendpoints. No changes to authentication, secrets handling, command/tool execution surface, or data access scope. No changes toDefaultConfigurationExtension.threadTransformersseeding or to any plugin contract.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
Root Cause (if applicable)
src/agents/pi-tool-definition-adapter.tsreturned a placeholder stub for client-tool calls without setting Pi'sterminate: trueflag, so Pi treated the stub as a normal tool result and ran an extra model turn before the outer runner observedclientToolCallDetected. That extra turn is what produced the contradictory phantom narration in the SSE stream./v1/responsesSSE emitter atsrc/gateway/openresponses-http.tsshipped streamed function-call args by populatingitem.argumentson theoutput_item.added/output_item.doneevents instead of using the dedicatedresponse.function_call_arguments.delta/.doneevents. The Responses-API streaming convention reserves those dedicated events for incremental args; some client parsers (those that read args only from those events) saw nothing because they weren't emitted.execute()callback returnedterminate: true, and no regression test pinned the four-event SSE sequence for streamed function_calls. Both are now covered.output_item.{added,done}events with dedicatedfunction_call_arguments.{delta,done}events that carry the streamed args;openresponses-http.tshad only ever emitted the bookends. The OpenAI Node SDK and pi-ai both seed their args buffer fromoutput_item.added.item.argumentsand then append eachdelta, so they recovered the args from the bundled-in shape; parsers that read args only from the dedicated events did not.Regression Test Plan (if applicable)
src/agents/pi-tool-definition-adapter.test.ts— pins theterminate: trueflag on the handoff stub returned by the wrapped client-toolexecute()callback.src/gateway/openresponses-http.test.ts— pins the SSE event sequence for streamed function_call output items.src/agents/openai-transport-stream.test.ts— pins the.done-only-args recovery branch in the upstream Responses-stream parser.execute()callback returnsterminate: trueso the agent loop stops after the handoff; the/v1/responsesSSE stream for a streamedfunction_callcarries the dedicatedfunction_call_arguments.{delta,done}events between theoutput_item.addedandoutput_item.donebookends; andprocessResponsesStreamrecovers args fromfunction_call_arguments.donewhen no intermediate deltas were emitted.User-visible / Behavior Changes
External clients consuming
/v1/responsesfor client-tool calls will no longer see the conversation contaminated by a self-contradictory phantom assistant turn (where the model first claimed it could not run the tool, then seconds later claimed it did). On top of that, clients whose parsers read streamed args only from the dedicatedfunction_call_arguments.{delta,done}events will now receive the parsed function-call arguments through those events instead of an empty{}. No CLI/UI/default-config changes.Diagram (if applicable)
Pi agent loop (the main fix):
/v1/responsesSSE event sequence (the piggybacked wire-shape improvement):Security Impact (required)
NoNoNoNo— same client-tool handoff contract, just delivered through a more usual SSE event sequence.NoYes, explain risk + mitigation: N/A.Repro + Verification
Environment
/v1/responsesHTTP endpoint, client-tool definition handed off to the API caller/v1/responsesenabled and at least one agent configuredSteps
/v1/responsesendpoint./v1/responseswithstream: trueand atools: [...]array including a client-tool definition.Expected
function_call. The transcript is exactly what the caller asked for: the model emits the tool call and the agent loop yields control back for the client to handle it.function_call_arguments.{delta,done}SSE events for parsers that read from those, or viaitem.argumentson the bookend events for parsers that read from there).Actual
function_call(the model first declares it cannot run the tool, then a few seconds later claims it did), and clients that read args only from the dedicated streaming events seearguments == "{}".Evidence
Attach at least one:
openresponses-http.test.ts,pi-tool-definition-adapter.test.ts,openai-transport-stream.test.ts).Human Verification (required)
What I personally verified (not just CI), and how:
/v1/responsesrequest from a local client that triggered a client-tool call. The client tool received its parsed arguments correctly, and the conversation contained no phantom narration of the placeholder stub alongside the realfunction_call.Review Conversations
Compatibility / Migration
Yes— additive on the wire (the dedicatedfunction_call_arguments.{delta,done}events are new;item.argumentsonoutput_item.donestill carries the populated args; onlyoutput_item.added'sitem.argumentsfield changes from<full args>to"").NoNoRisks and Mitigations
item.argumentsfromoutput_item.addedas its sole source (and ignored both the dedicated args events andoutput_item.done'sitem.arguments) will now read"".output_item.done.item.arguments. The populated args remain available via both of those alternative sources for any client that follows either pattern.AI-assisted PR
pnpm build && pnpm check && pnpm testrun on a Linux box matching CI conditions; touched files clean forpnpm lintandpnpm format:check).