Skip to content

fix: implement boot-time patch for Anthropic thinking blocks API signature (#27825)#27965

Closed
ishan8351 wants to merge 2 commits intoopenclaw:mainfrom
ishan8351:fix/issue-27825-skip-thinking-sanitization
Closed

fix: implement boot-time patch for Anthropic thinking blocks API signature (#27825)#27965
ishan8351 wants to merge 2 commits intoopenclaw:mainfrom
ishan8351:fix/issue-27825-skip-thinking-sanitization

Conversation

@ishan8351
Copy link
Copy Markdown

@ishan8351 ishan8351 commented Feb 26, 2026

Summary

Describe the problem and fix in 2–5 bullets:

  • Problem: The sanitizeSurrogates() utility in @mariozechner/pi-ai mutates Anthropic thinking blocks, which breaks cryptographic signatures during conversation history replay and causes session stalls.
  • Why it matters: Users with long-running sessions (e.g., heartbeat agents) experience permanent data corruption when compacted context is saved with invalid signatures.
  • What changed: Added a boot-time runtime patch in openclaw.mjs. This dynamically removes the sanitizer wrapper using regex before the app boots.
  • What did NOT change (scope boundary): Sanitization remains active for standard text blocks and system prompts where signature integrity isn't a factor.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

User-visible / Behavior Changes

List user-visible changes (including defaults/config).
Displays patch applied message on boot when debug mode (OPENCLAW_DEBUG=1) is enabled, and prints a helpful warning if file permissions block the patch.

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Environment

  • OS: WSL
  • Runtime/container: pnpm (Node.js)
  • Model/provider: Anthropic
  • Integration/channel (if any): Telegram/OpenClaw session replay

Steps

1.Enable "Extended Thinking" for a Claude-based assistant.
2.Generate a response that includes a reasoning trace (thinking block).
3.Send a follow-up message to trigger a history replay.

Expected

-The session continues normally as the thinking block is sent back to the API unmodified.

Actual

-The API returns an error or stalls because the thinking block content was mutated by the surrogate sanitizer, breaking the signature.

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios: Manually verified that the patch runs and has desired effects.
  • Edge cases checked: Verified that standard text blocks still undergo sanitization.
  • What you did not verify: Did not verify performance overhead (should be negligible).

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No
  • If yes, exact upgrade steps: None. Automatic execution on initiation and works universally

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly: Remove the applyAnthropicThinkingPatch() block from the top of openclaw.mjs.
  • Known bad symptoms reviewers should watch for: If the upstream pi-ai library drastically rewrites its file structure or code in a future update, the Regex won't match. The patch will safely skip itself without crashing, but users will revert to the "stalled session" bug. Reviewers can watch the boot logs for [OpenClaw Patch] Skipped patch: to diagnose.

Risks and Mitigations

List only real risks for this PR. Add/remove entries as needed. If none, write None.

  • Risk: Deployments in strictly read-only environments (e.g., some Docker containers or serverless functions) will block the runtime patch from modifying node_modules.
    -Mitigation: Caught EROFS and EACCES errors in the boot script. It will gracefully skip the patch and log a clear warning rather than crashing the app, leaving the user no worse off than before the PR

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Feb 26, 2026

Greptile Summary

This PR applies a pnpm patch to bypass sanitizeSurrogates() for Anthropic thinking blocks during conversation history replay. The surrogate sanitizer was modifying thinking block content, breaking the API signature and causing session stalls. The fix removes sanitization only for thinking blocks (both with and without signatures) while preserving it for standard text blocks.

Key changes:

  • Added patchedDependencies entry in package.json for @mariozechner/pi-ai
  • Created patch file that removes two sanitizeSurrogates() calls in dist/providers/anthropic.js
  • Updated pnpm-lock.yaml to reference the patch with its hash

The approach is minimal and targeted - only the specific lines causing the issue are modified, and the patch correctly preserves API signature integrity for Anthropic's extended thinking feature.

Confidence Score: 4/5

  • Safe to merge with minor verification needed
  • The patch is minimal, well-scoped, and addresses a specific bug. The dependency version is correctly specified as an exact version per repository guidelines. The only considerations are: (1) this modifies a third-party dependency's dist files which could be fragile across updates, and (2) the PR author has manually verified the fix but automated testing coverage for this specific scenario is unclear.
  • No files require special attention - the changes are straightforward dependency patching

Last reviewed commit: 4c40946

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: 4c40946441

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ishan8351 ishan8351 force-pushed the fix/issue-27825-skip-thinking-sanitization branch from 4c40946 to ae2bbea Compare February 27, 2026 04:22
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: ae2bbead6a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ishan8351 ishan8351 force-pushed the fix/issue-27825-skip-thinking-sanitization branch from ae2bbea to 05717f9 Compare February 27, 2026 04:29
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: 05717f9eb0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ishan8351 ishan8351 force-pushed the fix/issue-27825-skip-thinking-sanitization branch from 05717f9 to c51587b Compare February 27, 2026 09:15
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: c51587bf5b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ishan8351 ishan8351 force-pushed the fix/issue-27825-skip-thinking-sanitization branch from c51587b to c5e6a96 Compare March 1, 2026 10:55
@ishan8351 ishan8351 changed the title fix: skip anthropic thinking block sanitization to preserve API signature (#27825) fix: implement boot-time patch for Anthropic thinking blocks API signature (#27825) Mar 1, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: c5e6a961a0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ishan8351 ishan8351 force-pushed the fix/issue-27825-skip-thinking-sanitization branch from c5e6a96 to 52d4d21 Compare March 1, 2026 11:11
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: 52d4d21f12

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ishan8351 ishan8351 force-pushed the fix/issue-27825-skip-thinking-sanitization branch from 52d4d21 to 23fce86 Compare March 1, 2026 11:27
@ishan8351
Copy link
Copy Markdown
Author

Regarding upstream @mariozechner/pi-ai
Came across this code snippet:
const client = new Anthropic({
apiKey: null,
authToken: apiKey,
baseURL: model.baseUrl,
dangerouslyAllowBrowser: true,
defaultHeaders: mergeHeaders({
accept: "application/json",
"anthropic-dangerous-direct-browser-access": "true",
"anthropic-beta": claude-code-20250219,oauth-2025-04-20,${betaFeatures.join(",")},
"user-agent": claude-cli/${claudeCodeVersion},
"x-app": "cli",
}, model.headers, optionsHeaders),
});

Looks like headers are hardcoded to spoof claude-cli. This violates Anthropic's Terms of Service, and could result in permanent OAuth/API bans for users.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: 3aea5df9be

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Meli73 pushed a commit to Meli73/openclaw that referenced this pull request Mar 20, 2026
…27825)

- Add sanitizePayload() with structural traversal
- Bypass sanitization for signed thinking blocks
- Preserve normal sanitization for other content
- Config-driven toggle for flexible deployment
- Replace runtime patching with clean code solution

Supersedes openclaw#27965
@openclaw-barnacle
Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle bot added the stale Marked as stale due to inactivity label Mar 31, 2026
@openclaw-barnacle
Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #pr-thunderdome-dangerzone on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

@openclaw-barnacle openclaw-barnacle bot closed this Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: S stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sanitizeSurrogates() corrupts thinking block content before API replay

1 participant