Skip to content

fix(agents): keep phaseless OpenAI WS text buffered until phase resolves - #61968

Merged
steipete merged 4 commits into
mainfrom
steipete/61954-ws-phase-gate-land
Apr 6, 2026
Merged

steipete merged 4 commits into
mainfrom
steipete/61954-ws-phase-gate-land

Conversation

@steipete

@steipete steipete commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #61477.

Replacement for #61954 because the contributor fork would not accept maintainer pushes from this account.

This keeps the original narrow fix and adds the missing flush guard plus stronger regression coverage:

  • gate response.output_text.delta and response.output_text.done on a resolved phase value
  • do not flush buffered text from output_item.added / output_item.done while phase is still undefined
  • cover all relevant orderings:
    • output_text.delta -> phaseless output_item.added -> output_item.done
    • phaseless output_item.added -> output_text.delta -> output_item.done
    • phaseless output_item.added -> output_text.done -> output_item.done

Verification

  • pnpm test src/agents/openai-ws-stream.test.ts -t "keeps buffering text deltas until item phase is defined"
  • pnpm test src/agents/openai-ws-stream.test.ts -t "buffers text when output_item.added arrives without phase metadata"
  • pnpm test src/agents/openai-ws-stream.test.ts -t "buffers output_text.done until item phase is defined"
  • pnpm test src/agents/openai-ws-stream.test.ts

Repo-wide gate note

Current rebased main is already red on unrelated untouched files:

  • pnpm check fails in extensions/discord/src/monitor/message-utils.ts and extensions/xai/*
  • pnpm build fails in extensions/acpx/src/runtime.ts

@cursor

cursor Bot commented Apr 6, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches WebSocket streaming event ordering/partial emission logic, which can affect user-visible output and streaming behavior, but the change is narrowly scoped and covered by new regression tests.

Overview
Prevents premature emission of assistant text in the OpenAI Responses WebSocket stream by only flushing buffered output_text.delta/output_text.done once the corresponding output item has a defined assistant phase (avoids phaseless output_item.added causing early flushes).

Adds stronger regression coverage in openai-ws-stream.test.ts for multiple event orderings (delta-before-added, phaseless-added, and output_text.done before phase), and updates the changelog entry for this behavior. Also adjusts session-store.test.ts to import updateSessionStoreAfterAgentRun from its new module location.

Reviewed by Cursor Bugbot for commit 3b290c5. Bugbot is set up for automated code reviews on this repo. Configure here.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M maintainer Maintainer-authored PR labels Apr 6, 2026
@openclaw-barnacle openclaw-barnacle Bot added the commands Command implementations label Apr 6, 2026
@greptile-apps

greptile-apps Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Straightforward, well-scoped fix that gates response.output_text.delta and response.output_text.done emissions behind a resolved phase value, and defers the buffered-text flush from output_item.added/output_item.done until the phase is known. Three new regression tests cover all described orderings. The only note is that the CHANGELOG entry references #61954 (the original contributor PR) rather than #61968 (this replacement PR), which is worth updating for traceability since this PR adds the flush guard that was absent from #61954.

Confidence Score: 5/5

Safe to merge — no logic errors or data-integrity issues found.

Only a P2 style note on the changelog PR number reference; the implementation logic is correct, normalizeAssistantPhase(undefined) correctly returns undefined so the !== undefined gate works as expected, and all three new tests cover the described orderings.

CHANGELOG.md — minor PR number attribution (P2 only)

Prompt To Fix All With AI
This is a comment left during a code review.
Path: CHANGELOG.md
Line: 39

Comment:
**Changelog references prior PR instead of this one**

The entry links `#61954` (the original contributor PR) rather than `#61968` (this replacement PR). Since #61968 extends the fix beyond #61954 — adding the flush guard for `output_item.added` — linking the merged PR number keeps the changelog traceable to the actual change.

```suggestion
- Providers/OpenAI: keep WebSocket text buffered until a real assistant phase arrives, even when text deltas land before a phaseless `output_item.added` announcement. (#61968) Thanks @100yenadmin.
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "test(commands): fix session-store import..." | Re-trigger Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f2a889dd10

ℹ️ 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".

@@ -1059,7 +1061,7 @@ export function createOpenAIWebSocketStreamFn(
const key = getOutputTextKey(event.item_id, event.content_index);
const nextText = `${outputTextByPart.get(key) ?? ""}${event.delta}`;
outputTextByPart.set(key, nextText);
if (outputItemPhaseById.has(event.item_id)) {
if (outputItemPhaseById.get(event.item_id) !== undefined) {

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.

P1 Badge Emit buffered text when phase metadata never arrives

Gating response.output_text.delta emission on outputItemPhaseById.get(event.item_id) !== undefined means message text is never streamed for items that stay unphased for the entire turn. OutputItem.phase is optional in the WS schema, so when a provider/model omits phase on both response.output_item.added and response.output_item.done, this path now buffers forever and emits no text_delta events until completion, regressing incremental output for those sessions.

Useful? React with 👍 / 👎.

Comment thread CHANGELOG.md
@@ -37,6 +37,7 @@ Docs: https://docs.openclaw.ai
- iOS/Watch exec approvals: keep Apple Watch review and approval recovery working while the iPhone is locked or backgrounded, including background-safe reconnects, persisted pending approvals, notification cleanup, and APNs-backed watch refresh recovery. (#61757) Thanks @ngutman.
- Gateway/status: probe local TLS gateways over `wss://`, forward the local cert fingerprint for self-signed loopback probes, and warn when the local TLS runtime cannot load the configured cert. (#61935) Thanks @ThanhNguyxn07.
- Agents/history: keep truly legacy unsigned replay text unphased when mixed with phased OpenAI WS assistant blocks, while still inheriting message phase for id-only replay signatures. (#61529) Thanks @100yenadmin.

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 Changelog references prior PR instead of this one

The entry links #61954 (the original contributor PR) rather than #61968 (this replacement PR). Since #61968 extends the fix beyond #61954 — adding the flush guard for output_item.added — linking the merged PR number keeps the changelog traceable to the actual change.

Suggested change
- Agents/history: keep truly legacy unsigned replay text unphased when mixed with phased OpenAI WS assistant blocks, while still inheriting message phase for id-only replay signatures. (#61529) Thanks @100yenadmin.
- Providers/OpenAI: keep WebSocket text buffered until a real assistant phase arrives, even when text deltas land before a phaseless `output_item.added` announcement. (#61968) Thanks @100yenadmin.
Prompt To Fix With AI
This is a comment left during a code review.
Path: CHANGELOG.md
Line: 39

Comment:
**Changelog references prior PR instead of this one**

The entry links `#61954` (the original contributor PR) rather than `#61968` (this replacement PR). Since #61968 extends the fix beyond #61954 — adding the flush guard for `output_item.added` — linking the merged PR number keeps the changelog traceable to the actual change.

```suggestion
- Providers/OpenAI: keep WebSocket text buffered until a real assistant phase arrives, even when text deltas land before a phaseless `output_item.added` announcement. (#61968) Thanks @100yenadmin.
```

How can I resolve this? If you propose a fix, please make it concise.

100yenadmin and others added 4 commits April 6, 2026 16:33
…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
@steipete
steipete force-pushed the steipete/61954-ws-phase-gate-land branch from 605bd37 to 3b290c5 Compare April 6, 2026 15:33
@steipete
steipete merged commit 0b9993d into main Apr 6, 2026
10 checks passed
@steipete
steipete deleted the steipete/61954-ws-phase-gate-land branch April 6, 2026 15:35
@steipete

steipete commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

Landed via replacement PR.

  • Verification: pnpm test src/agents/openai-ws-stream.test.ts -t "keeps buffering text deltas until item phase is defined"; pnpm test src/agents/openai-ws-stream.test.ts -t "buffers text when output_item.added arrives without phase metadata"; pnpm test src/agents/openai-ws-stream.test.ts -t "buffers output_text.done until item phase is defined"; pnpm test src/agents/openai-ws-stream.test.ts; pnpm test src/commands/agent/session-store.test.ts
  • Source head before merge: 3b290c5
  • Merged commit: 0b9993d

Thanks @100yenadmin!

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3b290c588c

ℹ️ 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".

Comment on lines +1064 to 1067
if (outputItemPhaseById.get(event.item_id) !== undefined) {
emitBufferedTextDelta({
itemId: event.item_id,
contentIndex: event.content_index,

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.

P1 Badge Restore streaming for permanently phaseless WS items

OutputItem.phase is optional, but this gate only emits when outputItemPhaseById.get(event.item_id) !== undefined. If an item stays phaseless for the whole turn (both response.output_item.added and response.output_item.done omit phase), buffered text is never flushed and users get no text_delta events before completion. Fresh evidence in this commit is that the new tests only exercise paths where a phase eventually appears, so the optional-phase-forever path remains unprotected and regresses incremental output.

Useful? React with 👍 / 👎.

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 commands Command implementations maintainer Maintainer-authored PR size: M

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

2 participants