Skip to content

fix(agents): gate WS text delta on valid phase value — prevent commentary leak from phaseless output_item.added - #61954

Closed
100yenadmin wants to merge 1 commit into
openclaw:mainfrom
electricsheephq:fix/61477-ws-phase-gate-v2
Closed

100yenadmin wants to merge 1 commit into
openclaw:mainfrom
electricsheephq:fix/61477-ws-phase-gate-v2

Conversation

@100yenadmin

Copy link
Copy Markdown
Contributor

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 phaseless output_item.added, then .has(id) returns true for both response.output_text.delta and response.output_text.done handlers.

Fix: Change .has(event.item_id) → .get(event.item_id) !== undefined on both emission gates (2 lines).

Regression test: Sends phaseless output_item.added → text delta → asserts no emission → output_item.done with phase → asserts correct emission.

Files changed

  • src/agents/openai-ws-stream.ts — 2-line gate fix
  • src/agents/openai-ws-stream.test.ts — regression test for phaseless scenario

Related

Test plan

  • Phaseless output_item.added does not flush text
  • Text buffered until valid phase arrives via output_item.done
  • Existing 92 tests still pass

🤖 Generated with Claude Code

…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
Copilot AI review requested due to automatic review settings April 6, 2026 15:01
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Apr 6, 2026
@greptile-apps

greptile-apps Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a subtle buffering gate bug in src/agents/openai-ws-stream.ts. When output_item.added arrives without phase metadata, normalizeAssistantPhase(undefined) returns undefined, which is stored as outputItemPhaseById.set(id, undefined). The old .has(id) guard on both the response.output_text.delta and response.output_text.done handlers returned true (the key exists), allowing text to be emitted before a valid phase was known — potentially leaking internal commentary as user-visible text.

The fix correctly changes both guards to .get(id) !== undefined, suppressing emission until output_item.done arrives with an actual phase value and the buffer is flushed by the existing output_item.added/done flush loop. A dedicated regression test covering the phaseless output_item.added → text delta → output_item.done (with phase) sequence is also added and validates both the suppression and the eventual flush.

  • Two-line source fix in src/agents/openai-ws-stream.ts: .has(event.item_id) → .get(event.item_id) !== undefined on both emission gates (lines 1062 and 1076)
  • Regression test in src/agents/openai-ws-stream.test.ts: covers the exact race order described in the bug — no premature emission while phase is unknown, correct flush once phase arrives
  • Minor: the new test uses gpt-5.2 as the model constant; CLAUDE.md testing guidelines prefer gpt-5.4 for new tests"

Confidence Score: 5/5

Safe 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 (.has vs .get !== undefined on a Map with explicit undefined values), the regression test covers the exact failure sequence, and all 92 existing tests continue to pass. No architectural changes, no new dependencies, no impact on the public plugin SDK surface.

Only src/agents/openai-ws-stream.ts and its test file are changed; both are straightforward. The sole minor note is the gpt-5.2 model constant in the new test.

Prompt To Fix All 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.

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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 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).

Suggested change
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!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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-undefined phase value (not just a Map key).
  • Add a regression test covering output_item.added arriving 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/done from emitting when outputItemPhaseById is present-but-undefined, but the response.output_item.added/done handler still flushes buffered text parts unconditionally right after outputItemPhaseById.set(...), even when the resolved itemPhase is undefined (phaseless item). If an output_text.delta arrives before the (phaseless) output_item.added event, the buffered text will be emitted as unphased during that flush, which can still leak commentary. Consider only flushing buffered text when the newly resolved itemPhase is 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,
                  });
                }

steipete added a commit that referenced this pull request Apr 6, 2026
…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]>
@steipete

steipete commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Landed via replacement PR #61968 because the contributor fork would not accept maintainer pushes from this account.

Thanks @100yenadmin!

@steipete steipete closed this Apr 6, 2026
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…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]>
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…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]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…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]>
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
…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]>
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(agents): OpenAI WS late-map buffering gates on key existence not phase validity — unphased commentary can leak to UI

3 participants