← Back to Pull requests

fix(openaimodel): preserve refusals and completed stream content #1467

Closed
unknown closed this pull request without merging on Sep 08, 2026 07:16 UTC
View on GitHub
unknown unknown commented on Sep 02, 2026 03:10 UTC
Author

Link to Issue or Description of Change

Closes #1466. Closes #1476.

The OpenAI stream translator ignores response.refusal.delta. A refusal-only
response loses incremental delivery, and a refusal following reasoning can be
omitted entirely because the non-empty aggregator suppresses terminal-response
fallback. The resulting thought-only turn may cause up to 10 model calls without
surfacing an answer.

Translate non-empty refusal deltas into ordinary, non-thought text, matching the
blocking representation. Explicitly classify response.refusal.done as an
informational event; this documents its existing behavior and avoids emitting
the complete refusal again after its deltas.

When response.completed contains non-thought text or a function call, use
its content for the final response only if it retains the visible text and
function calls already collected from the stream. Otherwise, keep the aggregate.
When replacement is allowed, final reasoning follows the completed response;
thoughts already delivered as partials are not added back.

This does not merge content from both sources. For example, if visible text
streams first and the completed response contains only a tool call, the text
is retained and the call is not recovered. Calls whose streamed ID fell back
to item_id can also prevent recovery when the completed response uses a
different call_id. Both remain limitations of the current approach.

Preserve aggregated content when the completed payload is empty or cannot be
converted, and when the stream ends without a terminal event or its first
terminal event is response.incomplete. With no aggregated response, the
existing terminal fallback and conversion errors are unchanged. Final metadata
continues through the existing finalization path.

The partial-delta case was reproduced with synthetic SSE events. This validates
the adapter's behavior for that input; it does not establish how often a live
provider emits such a stream.

Testing Plan

  • Direct tests cover reasoning filtering, usable output, text containment,

  • argument and ID differences, and one-to-one call matching. Each of the five
    mutations identified in the review makes a test fail.
  • Full-stream tests cover reasoning that contains the visible answer, streamed

  • thoughts omitted by the completed response, and recovery of completed text
    after a function call with null or {} arguments. The null case failed
    before argument normalization and passes afterward.
  • Translator tests cover non-empty and empty refusal deltas.

  • Full-stream tests compare final refusal content directly with blocking output,

  • including partial deltas with and without refusal.done, and partial refusal
    after reasoning. They also verify that incremental text contains only the
    deltas actually received.
  • Regression tests cover text completion, mixed output order, empty and

  • unsupported terminal content, incomplete responses, and first-terminal-event
    precedence.
  • TestModel_GenerateStream_TerminalOnlyMessageContent preserves the original

  • #1476 reproduction: reasoning is streamed, while the visible answer appears
    only in response.completed. It fails against the pre-fix implementation and
    passes with the completed-content fix, comparing final content with blocking.
  • Tests cover reasoning-only and shorter completed responses, completed

  • responses that omit a streamed function call, and function calls that appear
    only in response.completed.
  • Updated the existing function-call fixture: response.output_item.added

  • previously omitted its type, and response.completed contained a message
    instead of the function call from the stream. Both events now describe the
    same call.

    Before the terminal-content fix, all three new partial-refusal cases failed:

    expected final text: "I cannot help with that."
    actual final text:   "I cannot "
    

    They pass after the fix. Local validation on Go 1.26.6:

    go build -mod=readonly work
    passed
    
    go test -race -mod=readonly -count=1 -shuffle=on work
    all packages passed, including plugin/agentanalytics
    
    golangci-lint run
    0 issues in both modules
    
    go mod tidy -diff
    no differences in either module
    

    Tests use a local HTTP server and synthetic SSE; no live model calls are made.

    Checklist

  • Read CONTRIBUTING.md and performed self-review.

  • Added regression tests and verified they fail before the fix.

  • Build, race tests, lint, and tidy checks pass locally.

  • No public API or dependency changes.

  • No dependent downstream changes.
  • Diff: 571 lines (+428 / -143) across 7 files Reviewer: ktsoator Review duration: 8886m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.55) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 571 lines diff across 7 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.380
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #1467"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 54.8% (MEDIUM) · Higher = larger blast radius
    54.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 23.7% · Safe to merge
    23.7%
    Repository