Skip to content

fix(agents): completions-API models leak tool-call preambles into chats — tag pre-tool narration as commentary - #109057

Merged
steipete merged 1 commit into
openclaw:mainfrom
aspalagin:fix/completions-commentary-tagging
Jul 22, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
aspalagin:fix/completions-commentary-tagging

Conversation

@aspalagin

@aspalagin aspalagin commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes #108945
Related: #25592, #80458

What Problem This Solves

Providers using api: "openai-completions" could deliver model narration such as “I’ll check that…” as an ordinary chat reply before a tool call. Chat Completions streaming exposes content deltas, tool-call deltas, and finish reasons, but no commentary phase, so OpenClaw must resolve that phase at the tool boundary.

Why This Change Was Made

Both completions processors now provisionally tag buffered text immediately before the first consumer-visible tool-call event. A shared helper also serves the Anthropic transport, assigns deterministic v1 commentary signatures, and tracks the exact blocks/signatures generated by the current turn.

If the provider later resolves to finish_reason: "stop" and tool calls are stripped as spurious, only those exact provisional signatures are rolled back. Empty tool_calls: [] arrays never trigger tagging. Confirmed tool turns receive a final backstop tag for text arriving after the last tool-call delta.

The embedded subscriber withholds unphased Chat Completions text from durable block replies until the authoritative message_end: commentary is suppressed, while ordinary and rolled-back spurious-tool answers use the existing final delivery path. Valid v1 phase signatures are preserved through transcript redaction for Completions and Anthropic routes, without broadening reasoning-replay exemptions.

No new config or stored-data shape is introduced; textSignature is an existing optional text-block field.

User Impact

Tool-call preambles stay in the transient commentary/progress lane and never become durable chat bubbles. Final answers, ordinary no-tool replies, and text from stripped spurious tool calls remain deliverable in full and in order.

Evidence

  • Focused Vitest: 8 files, 348 tests passed across provider, transport, subscriber, DeepSeek shaping, Anthropic, helper, and transcript-redaction surfaces.
  • Blacksmith changed gate: formatting, guards, core production/test types, full core/package lint, schema/storage guards, and import-cycle checks passed.
  • Source-blind CLI contract against a deterministic OpenAI-compatible SSE server:
    • tool turn executed one real read, delivered only FINAL_RESULT, and omitted PREAMBLE_SHOULD_NOT_DELIVER;
    • plain turn delivered PLAIN_RESULT;
    • finish_reason=stop plus a spurious tool call delivered SPURIOUS_RESULT with no tool execution.
  • Final AutoReview: clean, no accepted/actionable findings.
  • git diff --check origin/main...HEAD: passed.

Credit is retained with Co-authored-by: Arseniy Palagin <[email protected]>.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M labels Jul 16, 2026
@aspalagin
aspalagin force-pushed the fix/completions-commentary-tagging branch from f325daf to ffadaa2 Compare July 16, 2026 14:14
@aspalagin

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper

clawsweeper Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 18, 2026, 7:49 AM ET / 11:49 UTC.

Summary
The branch tags Chat Completions pre-tool narration as commentary, withholds unresolved completions text from durable block replies, and preserves commentary phase signatures during transcript redaction.

PR surface: Source +155, Tests +385. Total +540 across 16 files.

Reproducibility: yes. The PR provides a concrete current-main baseline and an after-fix real OpenAI-compatible SSE scenario with a tool-call preamble, final reply, persisted transcript output, and rendered webchat result.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: serialized state: packages/ai/src/providers/openai-completions.test.ts, serialized state: packages/ai/src/providers/openai-completions.ts, serialized state: src/agents/openai-transport-stream.inline-reasoning-and-tool-calls.test.ts, serialized state: src/agents/transcript-redact.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Mantis proof suggestion
A redacted Telegram Desktop run would directly confirm that completions pre-tool narration is withheld while the final reply still reaches a real chat surface. A maintainer can ask Mantis to capture proof by posting this exact PR comment:

@openclaw-mantis telegram desktop proof: reproduce a Chat Completions tool-call turn and capture that pre-tool narration is absent while the final reply is delivered.

Risk before merge

  • [P1] This changes the message-delivery boundary for every OpenAI Completions provider: an incorrect phase classification could either leak pre-tool narration or suppress a legitimate ordinary reply. The branch covers tool-use, stripped-spurious-tool, and no-tool outcomes, but merge review should retain that channel-delivery scrutiny.

Maintainer options:

  1. Merge with scoped delivery coverage (recommended)
    Accept the bounded message-delivery risk once exact-head checks complete, because the branch has focused transport, subscriber, transcript, and live-provider evidence for both suppression and normal reply delivery.
  2. Request a channel-specific confirmation
    Before merging, obtain one redacted Telegram or comparable channel run that shows a tool preamble stays absent while the final reply is delivered.

Next step before merge

  • [P2] No repair lane is needed because no discrete current-head defect was identified; the remaining action is normal exact-head maintainer review and merge gating.

Security
Cleared: The diff changes stream classification, delivery gating, and redaction validation without adding dependencies, execution paths, credentials access, workflow permissions, or supply-chain inputs.

Review details

Best possible solution:

Keep the shared phase-tagging contract at the stream boundary, preserve it through redaction, and land only after exact-head checks confirm the covered tool-use and ordinary-reply paths remain intact.

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

Yes. The PR provides a concrete current-main baseline and an after-fix real OpenAI-compatible SSE scenario with a tool-call preamble, final reply, persisted transcript output, and rendered webchat result.

Is this the best way to solve the issue?

Yes. Resolving the phase before tool-call events and reusing the existing commentary delivery gate is narrower and more maintainable than adding per-channel suppression logic; the redaction preservation closes the corresponding reload/resume path.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P1: The PR addresses narration leaking into active chats and groups during tool-calling turns for configured Chat Completions providers.
  • merge-risk: 🚨 message-delivery: The patch deliberately changes which streamed assistant text is eligible for durable and channel-facing reply delivery.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body includes a redacted real-provider streaming run with before/after persisted transcript events and a rendered Control UI result showing narration suppression, final-answer delivery, and an ordinary no-tool reply.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes a redacted real-provider streaming run with before/after persisted transcript events and a rendered Control UI result showing narration suppression, final-answer delivery, and an ordinary no-tool reply.
  • mantis: telegram-visible-proof: Mantis should capture Telegram visible proof. The fix changes user-visible channel reply content and can be directly demonstrated in a short Telegram Desktop tool-call recording.
Evidence reviewed

PR surface:

Source +155, Tests +385. Total +540 across 16 files.

View PR surface stats
Area Files Added Removed Net
Source 10 258 103 +155
Tests 6 393 8 +385
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 16 651 111 +540

What I checked:

Likely related people:

  • steipete: Merged the prior phase-buffering repair for the same embedded delivery boundary; that work is the closest merged-history precedent for this PR’s unresolved-text handling. (role: recent adjacent owner; confidence: medium; commits: 0b9993df95df; files: src/agents/embedded-agent-subscribe.handlers.messages.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.
Review history (7 earlier review cycles)
  • reviewed 2026-07-16T14:21:52.282Z sha ffadaa209889ee2b94d27535f2e85c07404adcf7 :: needs real behavior proof before merge. :: [P1] Classify commentary before emitting stream events
  • reviewed 2026-07-16T16:15:10.925Z sha cd286477bcce4f9aa572107d2842762a187af879 :: needs real behavior proof before merge. :: [P1] Restore final delivery for permanently unphased completions
  • reviewed 2026-07-16T18:29:38.282Z sha dceccef5d15006d15ff7e14bdafbe1d6a13f05ec :: needs real behavior proof before merge. :: [P1] Flush the withheld prefix before the text_end suffix
  • reviewed 2026-07-16T20:09:38.071Z sha a47f2bc156de447b20499db1a48d83d71a37b5ce :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-16T22:07:55.361Z sha c22c0b8633be5868d84e655668b38e1b82800a7a :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-16T23:51:36.296Z sha c22c0b8633be5868d84e655668b38e1b82800a7a :: needs maintainer review before merge. :: none
  • reviewed 2026-07-17T01:19:37.982Z sha 9826b15741aea9550f2fb76cb31966042ef931ff :: needs maintainer review before merge. :: none

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. mantis: telegram-visible-proof Mantis should capture Telegram visible proof. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. labels Jul 16, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 16, 2026
@aspalagin
aspalagin force-pushed the fix/completions-commentary-tagging branch 2 times, most recently from 3778f8d to c22c0b8 Compare July 16, 2026 22:03
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 17, 2026
@aspalagin
aspalagin force-pushed the fix/completions-commentary-tagging branch from c22c0b8 to 9826b15 Compare July 17, 2026 01:13
@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Jul 17, 2026
@aspalagin
aspalagin force-pushed the fix/completions-commentary-tagging branch from 9826b15 to c78009d Compare July 18, 2026 11:45
@steipete steipete self-assigned this Jul 21, 2026
@steipete
steipete force-pushed the fix/completions-commentary-tagging branch from c78009d to e18d1e8 Compare July 21, 2026 23:55
@clawsweeper

clawsweeper Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: fix(agents): completions-API models leak tool-call preambles into chats — tag pre-tool narration as commentary This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@steipete
steipete force-pushed the fix/completions-commentary-tagging branch from e18d1e8 to a44f89c Compare July 22, 2026 00:03
@clawsweeper

clawsweeper Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: fix(agents): completions-API models leak tool-call preambles into chats — tag pre-tool narration as commentary This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@steipete
steipete merged commit 5451f48 into openclaw:main Jul 22, 2026
119 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling mantis: telegram-visible-proof Mantis should capture Telegram visible proof. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: L status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat Completions transport never tags mid-turn commentary — tool-call preambles are delivered to chats (Responses transport tags them correctly)

2 participants