Repository navigation
fix(agents): gate WS text delta on valid phase value — prevent commentary leak from phaseless output_item.added - #61954
100yenadmin wants to merge 1 commit into
Conversation
…p key existence When output_item.added arrives without phase metadata, outputItemPhaseById stores undefined. The previous .has() check returned true for undefined values, bypassing the buffering gate and leaking commentary as unphased visible content. Fix: change .has() to .get() !== undefined on both delta and done handlers. Fixes #61477
Greptile SummaryThis PR fixes a subtle buffering gate bug in The fix correctly changes both guards to
Confidence Score: 5/5Safe to merge — minimal two-line fix with a targeted regression test and no side effects on existing behavior. The change is surgical (2 lines, same logical location), the root cause analysis is accurate ( Only Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/openai-ws-stream.test.ts
Line: 2096
Comment:
**Use preferred test model constant `gpt-5.4`**
Per the CLAUDE.md testing guidelines, new tests should prefer `gpt-5.4` over `gpt-5.2`. This applies to three occurrences in the new regression test block (the `response.completed` model field, the `item` object, and the `output` array entry).
```suggestion
model: "gpt-5.4",
```
**Context Used:** CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(agents): gate WS text delta emission..." | Re-trigger Greptile |
| object: "response", | ||
| created_at: Date.now(), | ||
| status: "completed", | ||
| model: "gpt-5.2", |
There was a problem hiding this comment.
Use preferred test model constant
gpt-5.4
Per the CLAUDE.md testing guidelines, new tests should prefer gpt-5.4 over gpt-5.2. This applies to three occurrences in the new regression test block (the response.completed model field, the item object, and the output array entry).
| model: "gpt-5.2", | |
| model: "gpt-5.4", |
Context Used: CLAUDE.md (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/openai-ws-stream.test.ts
Line: 2096
Comment:
**Use preferred test model constant `gpt-5.4`**
Per the CLAUDE.md testing guidelines, new tests should prefer `gpt-5.4` over `gpt-5.2`. This applies to three occurrences in the new regression test block (the `response.completed` model field, the `item` object, and the `output` array entry).
```suggestion
model: "gpt-5.4",
```
**Context Used:** CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Pull request overview
Fixes a WebSocket streaming edge case in the OpenAI Responses adapter where text deltas could be emitted before a valid assistant phase (commentary vs final_answer) is known, leading to internal commentary leaking into user-visible output.
Changes:
- Tighten the text-delta emission gate to require a non-
undefinedphase value (not just a Map key). - Add a regression test covering
output_item.addedarriving without phase metadata and ensuring deltas are buffered until phase becomes available.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/agents/openai-ws-stream.ts | Changes the delta/done emission gate from has() to get() !== undefined to prevent phaseless items from streaming text prematurely. |
| src/agents/openai-ws-stream.test.ts | Adds a regression test for phaseless output_item.added followed by text delta, asserting no premature emission until a phased output_item.done. |
Comments suppressed due to low confidence (1)
src/agents/openai-ws-stream.ts:1067
- The new gate prevents
response.output_text.delta/donefrom emitting whenoutputItemPhaseByIdis present-but-undefined, but theresponse.output_item.added/donehandler still flushes buffered text parts unconditionally right afteroutputItemPhaseById.set(...), even when the resolveditemPhaseisundefined(phaseless item). If anoutput_text.deltaarrives before the (phaseless)output_item.addedevent, the buffered text will be emitted as unphased during that flush, which can still leak commentary. Consider only flushing buffered text when the newly resolveditemPhaseis valid (non-undefined), and/or deferring flush until a later item event provides a valid phase; add a regression test for the out-of-order delta-before-phaseless-added case to lock this in.
if (event.type === "response.output_text.delta") {
const key = getOutputTextKey(event.item_id, event.content_index);
const nextText = `${outputTextByPart.get(key) ?? ""}${event.delta}`;
outputTextByPart.set(key, nextText);
if (outputItemPhaseById.get(event.item_id) !== undefined) {
emitBufferedTextDelta({
itemId: event.item_id,
contentIndex: event.content_index,
});
}
…ves (#61968) * fix(agents): gate WS text delta emission on valid phase value, not map key existence When output_item.added arrives without phase metadata, outputItemPhaseById stores undefined. The previous .has() check returned true for undefined values, bypassing the buffering gate and leaking commentary as unphased visible content. Fix: change .has() to .get() !== undefined on both delta and done handlers. Fixes #61477 * docs: note WS phase buffering fix (#61954) (thanks @100yenadmin) * test(agents): cover phaseless WS output_text.done buffering (#61954) * test(commands): fix session-store import path for tsgo (#61968) --------- Co-authored-by: Eva <[email protected]>
|
Landed via replacement PR #61968 because the contributor fork would not accept maintainer pushes from this account.
Thanks @100yenadmin! |
…ves (openclaw#61968) * fix(agents): gate WS text delta emission on valid phase value, not map key existence When output_item.added arrives without phase metadata, outputItemPhaseById stores undefined. The previous .has() check returned true for undefined values, bypassing the buffering gate and leaking commentary as unphased visible content. Fix: change .has() to .get() !== undefined on both delta and done handlers. Fixes openclaw#61477 * docs: note WS phase buffering fix (openclaw#61954) (thanks @100yenadmin) * test(agents): cover phaseless WS output_text.done buffering (openclaw#61954) * test(commands): fix session-store import path for tsgo (openclaw#61968) --------- Co-authored-by: Eva <[email protected]>
…ves (openclaw#61968) * fix(agents): gate WS text delta emission on valid phase value, not map key existence When output_item.added arrives without phase metadata, outputItemPhaseById stores undefined. The previous .has() check returned true for undefined values, bypassing the buffering gate and leaking commentary as unphased visible content. Fix: change .has() to .get() !== undefined on both delta and done handlers. Fixes openclaw#61477 * docs: note WS phase buffering fix (openclaw#61954) (thanks @100yenadmin) * test(agents): cover phaseless WS output_text.done buffering (openclaw#61954) * test(commands): fix session-store import path for tsgo (openclaw#61968) --------- Co-authored-by: Eva <[email protected]>
…ves (openclaw#61968) * fix(agents): gate WS text delta emission on valid phase value, not map key existence When output_item.added arrives without phase metadata, outputItemPhaseById stores undefined. The previous .has() check returned true for undefined values, bypassing the buffering gate and leaking commentary as unphased visible content. Fix: change .has() to .get() !== undefined on both delta and done handlers. Fixes openclaw#61477 * docs: note WS phase buffering fix (openclaw#61954) (thanks @100yenadmin) * test(agents): cover phaseless WS output_text.done buffering (openclaw#61954) * test(commands): fix session-store import path for tsgo (openclaw#61968) --------- Co-authored-by: Eva <[email protected]>
…ves (openclaw#61968) * fix(agents): gate WS text delta emission on valid phase value, not map key existence When output_item.added arrives without phase metadata, outputItemPhaseById stores undefined. The previous .has() check returned true for undefined values, bypassing the buffering gate and leaking commentary as unphased visible content. Fix: change .has() to .get() !== undefined on both delta and done handlers. Fixes openclaw#61477 * docs: note WS phase buffering fix (openclaw#61954) (thanks @100yenadmin) * test(agents): cover phaseless WS output_text.done buffering (openclaw#61954) * test(commands): fix session-store import path for tsgo (openclaw#61968) --------- Co-authored-by: Eva <[email protected]>
…ves (openclaw#61968) * fix(agents): gate WS text delta emission on valid phase value, not map key existence When output_item.added arrives without phase metadata, outputItemPhaseById stores undefined. The previous .has() check returned true for undefined values, bypassing the buffering gate and leaking commentary as unphased visible content. Fix: change .has() to .get() !== undefined on both delta and done handlers. Fixes openclaw#61477 * docs: note WS phase buffering fix (openclaw#61954) (thanks @100yenadmin) * test(agents): cover phaseless WS output_text.done buffering (openclaw#61954) * test(commands): fix session-store import path for tsgo (openclaw#61968) --------- Co-authored-by: Eva <[email protected]>
Fixes #61477.
Recreated from #61528 with narrower scope and dedicated regression test (as requested by @steipete).
What this fixes (plain English)
When OpenAI streams a response, it first announces each output item (
output_item.added) then streams text deltas. If the announcement arrives without phase metadata (e.g., phase is determined later), our buffering gate incorrectly let text through because it checked "does the map have a key?" rather than "does the key have a valid value?" This caused internal commentary to appear as visible user text.Technical details
Root cause:
outputItemPhaseById.set(id, undefined)on phaselessoutput_item.added, then.has(id)returnstruefor bothresponse.output_text.deltaandresponse.output_text.donehandlers.Fix: Change
.has(event.item_id)→.get(event.item_id) !== undefinedon both emission gates (2 lines).Regression test: Sends phaseless
output_item.added→ text delta → asserts no emission →output_item.donewith phase → asserts correct emission.Files changed
src/agents/openai-ws-stream.ts— 2-line gate fixsrc/agents/openai-ws-stream.test.ts— regression test for phaseless scenarioRelated
Test plan
🤖 Generated with Claude Code