Skip to content

Fix streamed chat completions dropping leading less-than - #82355

Closed
honor2030 wants to merge 1 commit into
openclaw:mainfrom
honor2030:fix/openai-sse-final-fragment-flush
Closed

honor2030 wants to merge 1 commit into
openclaw:mainfrom
honor2030:fix/openai-sse-final-fragment-flush

Conversation

@honor2030

@honor2030 honor2030 commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Preserve cumulative assistant text payloads when translating agent events into OpenAI-compatible SSE deltas, so leading < characters are not dropped when delta starts after a buffered tag prefix.
  • Defer chat-completion stream finalization by one microtask after lifecycle end/result resolution so same-turn assistant deltas are flushed before [DONE].
  • Add regression coverage for split literal XML openers and chat-completions SSE final-fragment flushing.

Fixes #81991

Real behavior proof

  • Behavior or issue addressed: /v1/chat/completions streaming responses now keep a literal leading < in assistant content and flush the last assistant text fragment before the OpenAI-compatible SSE [DONE] frame.

  • Real environment tested: Local OpenClaw source checkout on macOS with Node v23.11.0 and pnpm 11.1.0, rebased on current origin/main (3156d94bca), exercising the repository's OpenAI-compatible HTTP SSE e2e harness for both gateway-server and gateway-client projects plus the embedded subscribe stream filter path.

  • Exact steps or command run after this patch: pnpm exec node --no-maglev node_modules/vitest/vitest.mjs run src/gateway/openai-http.test.ts src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.filters-final-suppresses-output-without-start-tag.test.ts --reporter=dot

  • Evidence after fix: Terminal output captured after the refreshed patch:

    RUN  v4.1.6 /private/tmp/openclaw-sse-refresh
    
    Test Files  4 passed (4)
         Tests  70 passed (70)
      Duration  52.38s (transform 25.88s, setup 1.41s, import 16.87s, tests 33.71s, environment 0ms)
    
  • Observed result after fix: The OpenAI-compatible HTTP SSE e2e path emitted the full streamed assistant content in both gateway modes, including the leading < case and the final finish fragment before stream completion; the embedded subscribe path preserved <xiaohai-banli>... when the literal tag opener was split after a bare <.

  • What was not tested: A live external OpenAI/Codex provider call was not used; the proof exercises OpenClaw's local HTTP SSE gateway behavior and stream filtering path without sending provider credentials.

Tests

  • git diff --cached --check && git diff --check
  • node scripts/run-vitest.mjs run src/gateway/openai-http.test.ts src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.filters-final-suppresses-output-without-start-tag.test.ts --reporter=dot
  • pnpm exec node --no-maglev node_modules/vitest/vitest.mjs run src/gateway/openai-http.test.ts src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.filters-final-suppresses-output-without-start-tag.test.ts --reporter=dot — 4 files / 70 tests passed
  • pnpm check:test-types

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime agents Agent runtime and tooling size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 15, 2026
@clawsweeper

clawsweeper Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 19, 2026, 4:05 PM ET / 20:05 UTC.

ClawSweeper review

What this changes

The branch derives OpenAI-compatible SSE content from cumulative assistant text, delays finalization by a microtask, and adds regression tests for a split leading &lt; and final-fragment flushing.

Merge readiness

⛔ Blocked until real behavior proof from a real setup is added - 4 items remain

Keep this PR open: it targets the still-open HTTP SSE truncation report and has focused regression coverage, but its head is conflicted with current main and the added proof is test-harness output rather than an after-fix request/response artifact from the real SSE surface.

Priority: P2
Reviewed head: f33c2379e1b8e99f55d4b282afacc02347a3b337

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The patch is focused and its tests track the reported failure, but proof remains mock-only and the branch must be refreshed before it can be evaluated as merge-ready.
Proof confidence 🦪 silver shellfish (2/6) Needs real behavior proof before merge: The PR body contains copied terminal output from focused tests, but the stated tests exercise mocked agent-command behavior and do not show an after-fix HTTP SSE response from the real runtime surface. Add a redacted terminal capture, live output, or response transcript; redact private endpoints, tokens, IP addresses, and customer data. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🦐 gold shrimp (3/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Needs proof Needs real behavior proof before merge: The PR body contains copied terminal output from focused tests, but the stated tests exercise mocked agent-command behavior and do not show an after-fix HTTP SSE response from the real runtime surface. Add a redacted terminal capture, live output, or response transcript; redact private endpoints, tokens, IP addresses, and customer data. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 6 items Linked canonical bug: The PR explicitly targets the same two observable HTTP SSE failures tracked by the still-open canonical report: lost leading < content and final content arriving after [DONE].
Proposed implementation: The branch adds cumulative assistant-text tracking before translating events into SSE deltas, which directly addresses the incremental-delta mismatch described in the linked report.
Focused regression coverage: The branch adds coverage for a literal XML opener split after a bare < and for the OpenAI-compatible HTTP SSE stream retaining cumulative content and its final fragment.
Findings None None.
Security None None.

Before merge

  • Add real behavior proof - Needs real behavior proof before merge: The PR body contains copied terminal output from focused tests, but the stated tests exercise mocked agent-command behavior and do not show an after-fix HTTP SSE response from the real runtime surface. Add a redacted terminal capture, live output, or response transcript; redact private endpoints, tokens, IP addresses, and customer data. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Resolve merge risk (P1) - Merging without a real after-fix HTTP SSE artifact leaves the message-delivery fix unverified outside mocked test plumbing.
  • Resolve merge risk (P1) - The branch is dirty against current main, so its three-way merge behavior and current-line applicability need a rebase and refreshed review before landing.
  • Complete next step (P2) - The remaining blockers are contributor-owned real-behavior evidence and a rebase/review refresh, not a safe automated repair on this stale conflicting branch.
Agent review details

Security

None.

PR surface

Source +24, Tests +91. Total +115 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 40 16 +24
Tests 2 91 0 +91
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 131 16 +115

Review metrics

None.

Stored data model

Persistent data-model change detected: serialized state: src/gateway/openai-http.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #81991
Summary: This PR is a candidate fix for the canonical HTTP SSE truncation report; the other open streaming PR overlaps only around completion framing.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Rebase and provide an SSE frame capture (recommended)
    Refresh the branch against current main and add a redacted after-fix HTTP SSE transcript or terminal capture that visibly includes the leading <, final content, and trailing [DONE] frame.
  2. Pause the stale branch
    If the contributor cannot rebase and demonstrate the live response path, leave the canonical bug open for a fresh narrowly scoped fix.

Technical review

Best possible solution:

Rebase onto current main, preserve the focused cumulative-text and final-flush regression coverage if it still applies, and attach a redacted live /v1/chat/completions SSE transcript or terminal capture showing the leading <, final content chunk, and [DONE] ordering.

Do we have a high-confidence way to reproduce the issue?

No high-confidence live reproduction was established in this review. The linked report gives a concrete split-tag HTTP SSE scenario and the branch adds matching source-level regression tests, so the path is source-reproducible but still needs a real post-fix stream capture.

Is this the best way to solve the issue?

Unclear until the rebased branch is compared with current main, but preserving cumulative event text and delaying stream completion is a narrow, maintainable direction for the two reported SSE ordering failures rather than adding a parallel API path.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against cb0bbcb1772c.

Labels

Label justifications:

  • P2: The PR addresses a bounded but user-visible HTTP SSE content-truncation problem for OpenAI-compatible clients.
  • merge-risk: 🚨 message-delivery: The changed completion and delta paths determine whether streamed assistant bytes reach HTTP SSE consumers in the correct order.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body contains copied terminal output from focused tests, but the stated tests exercise mocked agent-command behavior and do not show an after-fix HTTP SSE response from the real runtime surface. Add a redacted terminal capture, live output, or response transcript; redact private endpoints, tokens, IP addresses, and customer data. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

What I checked:

  • Linked canonical bug: The PR explicitly targets the same two observable HTTP SSE failures tracked by the still-open canonical report: lost leading < content and final content arriving after [DONE]. (f33c2379e1b8)
  • Proposed implementation: The branch adds cumulative assistant-text tracking before translating events into SSE deltas, which directly addresses the incremental-delta mismatch described in the linked report. (src/gateway/openai-http.ts:103, f33c2379e1b8)
  • Focused regression coverage: The branch adds coverage for a literal XML opener split after a bare < and for the OpenAI-compatible HTTP SSE stream retaining cumulative content and its final fragment. (src/gateway/openai-http.test.ts:1620, f33c2379e1b8)
  • Proof remains harness-only: The updated PR body reports 70 passing tests, but its stated validation uses mocked agentCommand paths and does not include a redacted after-fix HTTP SSE response transcript, terminal capture, or runtime log showing the emitted frames. (src/gateway/openai-http.test.ts:1620, f33c2379e1b8)
  • Current merge state: GitHub reports the branch as dirty against main; this needs a rebase and review refresh, but stale-base drift alone is not a patch correctness finding. (f33c2379e1b8)
  • Prior review context: A proof request was posted on July 16, 2026, and the PR body was updated on July 19, 2026; the new text merits review but does not yet demonstrate the unmocked user-visible SSE frames. (f33c2379e1b8)

Likely related people:

  • yfge: Requested concrete HTTP SSE proof in the PR discussion and is the clearest available routing contact for the remaining evidence requirement. (role: prior reviewer; confidence: medium; files: src/gateway/openai-http.ts, src/gateway/openai-http.test.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Rebase the branch onto current main and resolve the reported merge conflict.
  • Add a redacted after-fix HTTP SSE transcript or terminal capture that shows complete content before [DONE].

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 15, 2026
@yfge

This comment was marked as spam.

@honor2030
honor2030 force-pushed the fix/openai-sse-final-fragment-flush branch from 8c6ca10 to f33c237 Compare May 21, 2026 05:46
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. labels May 21, 2026
@clawsweeper

clawsweeper Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg

🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat.

Where did the egg go?
  • The egg game starts only after the PR passes the real-behavior proof check.
  • Before that, no creature or rarity is rolled. The treat waits for real proof.
  • This is still just collectible flavor: proof affects review readiness, not creature quality.

@clawsweeper

clawsweeper Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

@honor2030 thanks for the PR. ClawSweeper is still waiting on real behavior proof before this can move forward.

Useful proof can be a screenshot, short video, terminal output, copied live output, linked artifact, or redacted logs that show the changed behavior after the fix. Please redact private tokens, phone numbers, private endpoints, customer data, and anything else sensitive.

Once proof is added to the PR body or a comment, ClawSweeper or a maintainer can re-check it.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jul 16, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 24, 2026
steipete added a commit that referenced this pull request Jul 29, 2026
Preserve cumulative assistant snapshot prefixes and flush same-turn text before terminating OpenAI-compatible Gateway SSE.

Fixes #81991

Refs: #82355

Co-authored-by: honor2030 <[email protected]>
@steipete

Copy link
Copy Markdown
Contributor

Thank you, @honor2030, for identifying and implementing the two Chat Completions streaming fixes in this PR. Both defects are now fixed on main by #115652 and 1425a6c. The landed version preserves your snapshot and final-fragment solution, carries your actual Co-authored-by commit credit, and adds fail-first proof through the pinned official OpenAI SDK, real Gateway SSE, literal split-tag/filter regressions, 110 passing tests, and successful exact-commit CI. The linked issue #81991 was automatically closed as completed. Closing as fixed with sincere thanks for your work.

@steipete steipete closed this Jul 29, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 30, 2026
…15652)

Preserve cumulative assistant snapshot prefixes and flush same-turn text before terminating OpenAI-compatible Gateway SSE.

Fixes openclaw#81991

Refs: openclaw#82355

Co-authored-by: honor2030 <[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 gateway Gateway runtime merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP SSE (/v1/chat/completions) drops leading '<' from streamed content and closes connection before final chunk

3 participants