Repository navigation
Description
Issue Title
Bug: Subagent announce-delivery echo messages inherit wrong provider/model metadata, causing persistent "thinking blocks cannot be modified" errors after gateway restart
GitHub Issue
Bug: Echo assistant messages from subagent announce-delivery bypass isTranscriptOnlyOpenClawAssistantMessage filter, causing persistent Anthropic API errors after gateway restart
Summary
When a gateway restart occurs while a session has active thinking blocks, the subagent announce-delivery mechanism writes echo assistant messages to the parent session that inherit the original API response metadata (provider="anthropic", model="claude-sonnet-4-6"). Because these messages are not tagged as internal echoes (provider="openclaw", model="delivery-mirror"), they bypass the existing isTranscriptOnlyOpenClawAssistantMessage filter and appear as real consecutive assistant turns in subsequent API requests. This causes Anthropic to reject every subsequent user message with "thinking or redacted_thinking blocks in the latest assistant message cannot be modified" — an error that repeats on every turn for up to 23 minutes until the offending thinking block naturally rotates out of the message window.
Steps to Reproduce
- Enable thinking in a session:
/thinking medium - Spawn one or more subagents that will deliver results back to the parent session.
- While a turn is in progress (subagent running), restart the OpenClaw gateway.
- After the gateway comes back, send any user message.
- Observe the error immediately:
LLM request rejected: messages.25.content.1: thinking or redacted_thinking blocks
in the latest assistant message cannot be modified. These blocks must remain as
they were in the original response.
- Send another message. The error fires again. Repeat for every message for approximately 20–23 minutes.
Expected Behavior
Subagent announce-delivery echo messages should be transparently filtered from the API message replay. The session transcript should include them as internal bookkeeping entries only, invisible to the Anthropic API, with no impact on the conversation flow. Users should be able to send messages immediately after a gateway restart without error.
Actual Behavior
Every user message after the gateway restart triggers the Anthropic "thinking or redacted_thinking blocks cannot be modified" error. The error self-resolves only once the offending thinking block rotates out of the active message context window (~23 minutes, 8 failed attempts observed in a single session).
Root Cause Analysis
The failure is a chain of three distinct issues. RC-1 is the root cause; RC-2 and RC-3 amplify the damage.
RC-1 (Root of Roots): Echo messages are tagged with the wrong provider/model
When the announce-delivery path writes an echo of the assistant response to the parent session, it copies the original API response metadata verbatim. The resulting session entries look like this (from live session 38ff0e59):
{"id": "8a80757e", "message": {"role": "assistant", "provider": "anthropic", "model": "claude-sonnet-4-6", "api": "anthropic-messages", "content": [{"type": "text", "text": "..."}]}}
{"id": "7e1f9a8e", "message": {"role": "assistant", "provider": "anthropic", "model": "claude-sonnet-4-6", "api": "anthropic-messages", "content": [{"type": "text", "text": "..."}]}}An existing filter (isTranscriptOnlyOpenClawAssistantMessage) is specifically designed to identify internal echo messages and exclude them from API replay:
// selection-DZnXuqgh.js / normalizeAssistantReplayContent
function isTranscriptOnlyOpenClawAssistantMessage(message) {
const provider = normalizeOptionalString(message.provider) ?? "";
const model = normalizeOptionalString(message.model) ?? "";
return provider === "openclaw" && (model === "delivery-mirror" || model === "gateway-injected");
}Because the echo messages carry provider="anthropic", this predicate returns false. The echoes pass through as real assistant turns, creating consecutive assistant turns in the API payload — with the earlier turn containing a thinking block that Anthropic refuses to allow in any position other than the final assistant message.
RC-2 (Amplifier): validateAnthropicTurns does not merge consecutive assistant turns
The Gemini validation path already handles consecutive assistant turns correctly:
// pi-embedded-helpers-seljzB3q.js
function validateGeminiTurns(messages) {
return validateTurnsWithConsecutiveMerge({
messages,
role: "assistant", // merges consecutive assistant turns
merge: mergeConsecutiveAssistantTurns,
});
}The Anthropic path does not:
function validateAnthropicTurns(messages) {
return validateTurnsWithConsecutiveMerge({
messages: stripDanglingAnthropicToolUses(messages),
role: "user", // only merges user turns — no assistant merge
merge: mergeConsecutiveUserTurns,
});
}mergeConsecutiveAssistantTurns exists and is battle-tested. It is simply not wired into the Anthropic path. If it were, a leaking echo that slips past RC-1 would be silently merged rather than triggering an API error.
RC-3 (Amplifier): Recovery is per-request, not per-session
wrapAnthropicStreamWithRecovery fires when it detects a thinking-block error, strips the thinking blocks, and retries. However, recoveredAnthropicThinking is a per-request flag. The next user message creates a fresh request object with recoveredAnthropicThinking = false. The same error fires, recovery fires again, and this cycle repeats N times until the original thinking block organically scrolls past position messages[25].
This is not the proximate cause, but it is what turns a single structural bug into a 23-minute outage.
Proposed Fixes
Fix 1 — PRIMARY: Tag echo messages correctly at write time
Location: The announce-delivery message writer (wherever it appends the echo assistant message to the parent session transcript).
Change:
// Before writing the echo to the parent session, override metadata:
const echoMessage = {
...originalAssistantMessage,
provider: "openclaw", // was: inherited "anthropic"
model: "delivery-mirror", // was: inherited "claude-sonnet-4-6"
api: undefined, // clear — this is not an API response
};
// write echoMessage to session, not originalAssistantMessageWhy this is safe: The delivery-mirror model value and the isTranscriptOnlyOpenClawAssistantMessage filter are already present in production. This fix simply ensures the tag is applied at write time instead of relying on external consumers to detect echoes by other means. Risk: LOW.
Fix 2 — DEFENSE IN DEPTH: Merge consecutive assistant turns in validateAnthropicTurns
Location: src/agents/pi-embedded-helpers/turns.ts (or equivalent bundled source)
Change:
export function validateAnthropicTurns(messages: AgentMessage[]): AgentMessage[] {
// Step 1: strip dangling tool uses (existing)
const stripped = stripDanglingAnthropicToolUses(messages);
// Step 2: merge consecutive assistant turns (NEW — mirrors validateGeminiTurns)
const withMergedAssistant = validateTurnsWithConsecutiveMerge({
messages: stripped,
role: "assistant",
merge: mergeConsecutiveAssistantTurns,
});
// Step 3: merge consecutive user turns (existing)
return validateTurnsWithConsecutiveMerge({
messages: withMergedAssistant,
role: "user",
merge: mergeConsecutiveUserTurns,
});
}Why this is safe: mergeConsecutiveAssistantTurns is already exercised by the Gemini path on every request. Merging [thinking, text] + [text] produces [thinking, text, text], which is valid Anthropic content. This provides a structural guarantee: even if a future bug allows a mis-tagged echo to reach the validation layer, it will be silently resolved rather than surfaced as an API error. Risk: LOW.
Fix 3 — OPTIONAL: Persist recovery state at the session level
Location: wrapAnthropicStreamWithRecovery
When recovery fires, write a custom marker entry to the session:
await session.appendCustomEntry({
type: "anthropic-thinking-recovery",
firedAt: Date.now(),
strippedUpToMessageIndex: messages.length,
});On the next turn, before building the API request, check for this marker and apply stripAllThinkingBlocks proactively to avoid the N-retry cascade.
Why this is lower priority: If Fix 1 and Fix 2 are applied, this error path should never be reached in the echo scenario. This fix targets the edge case where recovery is needed for reasons other than mis-tagged echoes (e.g., a genuine thinking block persisted across an unclean shutdown). Risk: MEDIUM (requires session state schema change).
Environment
| Field | Value |
|---|---|
| OpenClaw version | 2026.5.26 |
| Model | anthropic/claude-sonnet-4-6 |
| Channel | webchat (TUI) |
| Thinking level | medium |
| Error first observed | 2026-05-27T13:49 UTC |
| Error duration | ~23 minutes |
| Failed attempts before self-resolution | 8 |
| Session ID (for log correlation) | 38ff0e59 |
Additional Context
The 2026.5.26 release improved THINKING_BLOCK_ERROR_PATTERN to catch a related error (Invalid signature in thinking block). That fix correctly targets the thinking recovery logic. This bug is distinct: it lives in the session transcript writer, not the recovery path. The two bugs are orthogonal and can both be present simultaneously — the signature error triggers recovery, and the echo-tagging bug causes recovery to fire on every subsequent turn.
A quick test to confirm the fix for RC-1 is working: after applying it, inspect a new session file where subagent echo messages have been written and verify the entries show "provider": "openclaw" and "model": "delivery-mirror". If isTranscriptOnlyOpenClawAssistantMessage is evaluated on those entries, it should return true.
Investigated and reported after live observation during a 23-minute error window on 2026-05-27. Root causes confirmed by direct session file inspection and source analysis of the bundled JS. All proposed fixes reference existing production code — no new infrastructure required for Fix 1 or Fix 2.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
clawsweeper commentedon May 27, 2026
Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed May 27, 2026, 11:21 AM ET / 15:21 UTC.
Summary
Keep open: current main filters correctly tagged delivery mirrors, but the dormant/restarted completion path still direct-runs the requester agent and Anthropic replay validation does not merge consecutive assistant turns, so the reported failure chain is still plausible and needs a narrow fix.
Reproducibility: no. live high-confidence reproduction was performed in this read-only review. Source inspection supports the reported chain through dormant announce delivery, provider-owned assistant replay, Anthropic turn validation, and request-local thinking recovery.
Ways to help us reproduce this
Next step
A narrow repair lane is reasonable because the source path is concrete, but the fix should first capture the restart/dormant completion transcript shape in a regression test.
Review details
Best possible solution:
Add a focused replay-safety fix that preserves current delivery routing while ensuring announce-delivery artifacts cannot enter Anthropic replay as a separate provider-owned assistant turn after restart.
Do we have a high-confidence way to reproduce the issue?
No live high-confidence reproduction was performed in this read-only review. Source inspection supports the reported chain through dormant announce delivery, provider-owned assistant replay, Anthropic turn validation, and request-local thinking recovery.
Is this the best way to solve the issue?
Unclear that retagging all direct announce output is the best fix, because the dormant requester-agent handoff is an existing behavior. The safer repair is to add a regression test for the restart/dormant completion case and then make the replay path resilient without changing delivery semantics broadly.
AGENTS.md: found and applied where relevant.
Remaining risk / open question:
Codex review notes: model gpt-5.5, reasoning high; reviewed against 20eab65ff4fc.
Label changes
Label changes:
P1: The report describes a latest-release user session that repeatedly rejects every follow-up message after gateway restart, breaking an active agent workflow.impact:session-state: The affected state is the persisted session transcript and replay window, which can keep poisoning later turns until the bad assistant entry rotates out.impact:auth-provider: The failure depends on provider/model metadata and Anthropic replay policy for thinking blocks.issue-rating: 🐚 platinum hermit: Current issue advisory state selects this label.clawsweeper:needs-live-repro: Current issue advisory state selects this label.Label justifications:
P1: The report describes a latest-release user session that repeatedly rejects every follow-up message after gateway restart, breaking an active agent workflow.impact:session-state: The affected state is the persisted session transcript and replay window, which can keep poisoning later turns until the bad assistant entry rotates out.impact:auth-provider: The failure depends on provider/model metadata and Anthropic replay policy for thinking blocks.Evidence reviewed
Acceptance criteria:
What I checked:
AGENTS.md:1, 20eab65ff4fc)src/config/sessions/transcript.ts:112, 20eab65ff4fc)src/config/sessions/transcript.ts:240, 20eab65ff4fc)src/agents/subagent-announce-delivery.ts:1072, 20eab65ff4fc)src/agents/subagent-announce-delivery.test.ts:941, 20eab65ff4fc)src/agents/pi-embedded-helpers/turns.ts:320, 20eab65ff4fc)Likely related people:
src/config/sessions/transcript.ts,src/agents/subagent-announce-delivery.ts,src/agents/pi-embedded-helpers/turns.ts)src/agents/pi-embedded-runner/thinking.ts,src/config/sessions/transcript.ts)src/agents/pi-embedded-runner/replay-history.ts)How this review workflow works
@clawsweeper re-reviewcomments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.@clawsweeper re-reviewor@clawsweeper re-runon an open PR or issue to request a fresh review only.@clawsweeper reviewto request a fresh review only.@clawsweeper autofix,@clawsweeper automerge,@clawsweeper fix ci, or@clawsweeper address review.@clawsweeper explainto ask for more context, or@clawsweeper stopto stop active automation.WarrenJones commentedon May 27, 2026
travellingsoldier85 commentedon May 27, 2026
Fix Implementation — Patch Attached
The PAT used for this account lacks the
workflowscope needed to push branches containing.github/workflows/files (which come from the upstream fork). Attaching the patch here for the maintainer to apply directly.Files changed (2 files, 57 insertions, 5 deletions)
src/agents/pi-embedded-helpers/turns.ts— AddmergeConsecutiveAssistantTurnspass tovalidateAnthropicTurnssrc/agents/pi-embedded-helpers.validate-turns.test.ts— Update existing test (behavior changed) + add 2 new regression testsHow to apply
Patch
Jefsky commentedon May 27, 2026
I submitted a fix for RC-2 in PR #87346.
The function was only merging consecutive user turns but not consecutive assistant turns. The helper already existed and was used in — this fix wires it into the Anthropic path as well.
Consecutive assistant turns (from subagent announce-delivery echo messages) are now silently merged before being sent to the Anthropic API, preventing the 'thinking blocks cannot be modified' error.
bryanbaer commentedon May 30, 2026
Update — 2026-05-30, observed against
openclaw@2026.5.27(and verified against2026.5.28-beta.4).The bug is still reproducing with the symptom
messages.N.content.M: thinking or redacted_thinking blocks in the latest assistant messagedespite RC-1 being fully fixed in the installed code (transcript.appendAssistantMessageToSessionTranscriptcorrectly hardcodesprovider="openclaw", model="delivery-mirror"and all live callers use it; no mistagged echoes are reaching the transcript).Investigation finding: the current source of consecutive assistant turns is not the announce-delivery mirror — it's the dormant-requester direct-agent path in
src/agents/subagent-announce-delivery.ts(directAgentParams~line 1012 in the bundled output). When a subagent completes while the requester (e.g. a cron run or a TUI in another tab) is dormant, the announce-delivery path runs the requester agent inside the parent session viarunAnnounceAgentCall. This produces a realprovider="anthropic"assistant turn thatisTranscriptOnlyOpenClawAssistantMessagecorrectly does not filter, because it is not a mirror — it is a real agent run.The structural problem RC-2 (no assistant-turn merge in
validateAnthropicTurns) is what turns this into the user-visible "thinking blocks cannot be modified" rejection. PR #87346 is exactly the right fix and is sufficient to neutralise both this Mode B (dormant-completion direct agent run) and the original Mode A.Notes on PR #87346 CI status: every Real behavior proof / build-artifacts / dependency-guard job has been red for two days. The patch itself is a 4-line surgical edit using helpers that are already exercised by
validateGeminiTurns. CI failures look infrastructural rather than caused by the patch. Requesting maintainer help to land this — the diff is mechanically sound and matches the Gemini path's existing behaviour exactly.Also tangentially relevant: PR #88025 (treat stale thinking-block signature as
replay_invalid) is needed to close a separate failure mode that hits long-running cron sessions when Anthropic's signature window expires. That one is not the same issue, but a session that gets RC-2 right but not RC-3 (request-local recovery flag) will still see a recovery cascade until the offending block scrolls off. TherequestMeta = { id: sessionMeta.id }line inwrapAnthropicStreamWithRecoverycreates a fresh recovery context per request; promoting the flag to session-level would convert the cascade into a one-shot recovery.Local mitigation while #87346 lands:
Both helpers are already defined and exported in the same file. Tested locally against a subagent-heavy workload and resolves the symptom.
fix(anthropic): merge consecutive assistant replay turns (#87346)
fix(anthropic): merge consecutive assistant replay turns (openclaw#87346
fix(runtime): catch up openclaw upstream to d1b33a6 (openclaw#1733) (#99
fix(anthropic): merge consecutive assistant replay turns (openclaw#87346