Skip to content

fix: prevent sanitizeUserFacingText false-positives on assistant prose#12702

Closed
zerone0x wants to merge 1 commit intoopenclaw:mainfrom
zerone0x:fix/sanitize-user-facing-text-false-positive
Closed

fix: prevent sanitizeUserFacingText false-positives on assistant prose#12702
zerone0x wants to merge 1 commit intoopenclaw:mainfrom
zerone0x:fix/sanitize-user-facing-text-false-positive

Conversation

@zerone0x
Copy link
Contributor

@zerone0x zerone0x commented Feb 9, 2026

Summary

Fixes #12676

sanitizeUserFacingText applies error-detection heuristics to all outbound messaging text, including normal assistant responses. When an assistant discusses topics like billing, error handling, or HTTP status codes, the heuristic pattern-matching can misclassify the response as an API error and replace it with a generic error message.

Changes

  • Add looksLikeAssistantProse() guard that detects multi-sentence text, paragraph breaks, and markdown formatting — structural signals that distinguish assistant prose from short API error messages
  • Move the isRawApiErrorPayload check (JSON structure) before the prose guard since JSON payloads are always worth catching
  • Skip heuristic checks (isLikelyHttpErrorText, ERROR_PREFIX_RE) when text looks like prose
  • Tighten isBillingErrorMessage loose keyword branch (billing + upgrade/credits/payment/plan) to also skip prose text
  • Add tests covering false-positive scenarios (multi-paragraph, billing-topic, markdown, error-prefix prose)

Test Plan

  • All 107 existing pi-embedded-helpers tests pass
  • 5 new tests verify false-positive prevention on assistant prose
  • Existing error-catching behavior preserved for short, actual error messages
  • pnpm lint, pnpm build, pnpm format all pass

🤖 Generated with Claude Code (issue-hunter-pro)

Greptile Overview

Greptile Summary

This PR updates sanitizeUserFacingText to reduce false-positives where normal assistant prose (multi-paragraph text, markdown, multi-sentence explanations) was being misclassified as an API error and replaced with a generic error message. It introduces a looksLikeAssistantProse() guard, moves JSON error-payload detection earlier, and tightens the billing “loose keyword” heuristic to avoid classifying assistant discussions about billing as real billing errors. Tests were added to cover multi-paragraph/markdown/billing-topic prose while preserving sanitization for short, actual error-like strings.

Confidence Score: 3/5

  • This PR is mostly safe to merge, but has a couple logic edge-cases that can reintroduce false-positives and can also let long real errors through unsanitized.
  • Core approach and tests address the reported false positives, but (1) isBillingErrorMessage lowercases before applying looksLikeAssistantProse, breaking its sentence heuristic, and (2) the early prose return bypasses all heuristic sanitization for any text >500 chars, which can expose verbose real errors.
  • src/agents/pi-embedded-helpers/errors.ts

…nt prose

Add prose detection guard (looksLikeAssistantProse) so error-detection
heuristics in sanitizeUserFacingText only fire on short, single-line text
that resembles actual API errors—not multi-sentence assistant responses.

Also tighten isBillingErrorMessage loose keyword branch to skip prose,
preventing false classification of normal text discussing billing topics.

Fixes openclaw#12676

Co-Authored-By: Claude <[email protected]>
@openclaw-barnacle openclaw-barnacle bot added the agents Agent runtime and tooling label Feb 9, 2026
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +294 to +305
// JSON error payloads are always worth catching regardless of length.
if (isRawApiErrorPayload(trimmed)) {
return "The AI service returned an error. Please try again.";
}

// Skip heuristic error detection for text that looks like normal assistant
// prose - long responses, multi-paragraph, markdown-formatted, etc.
// This prevents false positives where normal responses discussing topics
// like "Error handling" or "402 payment required" get replaced.
if (looksLikeAssistantProse(trimmed)) {
return stripped;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long errors bypass sanitization

looksLikeAssistantProse() returns true for any text over 500 chars, and sanitizeUserFacingText returns early in that case. This means any real error text that happens to be verbose (>500 chars)—e.g. gateway/proxy HTML, stack traces, or multi-sentence provider errors—will skip isLikelyHttpErrorText / ERROR_PREFIX_RE handling and be shown to users un-sanitized. If the intent is only to avoid false positives on assistant prose, consider restricting the prose guard to non-error-prefixed messages (or only skipping the heuristic branches, not all sanitization) so long true errors still get replaced.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/errors.ts
Line: 294:305

Comment:
**Long errors bypass sanitization**

`looksLikeAssistantProse()` returns true for any text over 500 chars, and `sanitizeUserFacingText` returns early in that case. This means any *real* error text that happens to be verbose (>500 chars)—e.g. gateway/proxy HTML, stack traces, or multi-sentence provider errors—will skip `isLikelyHttpErrorText` / `ERROR_PREFIX_RE` handling and be shown to users un-sanitized. If the intent is only to avoid false positives on assistant prose, consider restricting the prose guard to non-error-prefixed messages (or only skipping the heuristic branches, not all sanitization) so long true errors still get replaced.

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 9, 2026

Additional Comments (1)

src/agents/pi-embedded-helpers/errors.ts
Lowercased prose guard broken

isBillingErrorMessage lowercases raw into value and then calls looksLikeAssistantProse(value). Since looksLikeAssistantProse’s sentence detection is based on [.!?]\s+[A-Z], it can’t ever trigger on a lowercased string, so long single-paragraph assistant prose about billing (no \n\n, no markdown) can still hit the loose billing + upgrade/credits/payment/plan heuristic and be misclassified. Consider calling looksLikeAssistantProse(raw) (or adjusting the sentence heuristic to be case-insensitive / not depend on capitals).

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/errors.ts
Line: 404:412

Comment:
**Lowercased prose guard broken**

`isBillingErrorMessage` lowercases `raw` into `value` and then calls `looksLikeAssistantProse(value)`. Since `looksLikeAssistantProse`’s sentence detection is based on `[.!?]\s+[A-Z]`, it can’t ever trigger on a lowercased string, so long single-paragraph assistant prose about billing (no `\n\n`, no markdown) can still hit the loose `billing` + `upgrade/credits/payment/plan` heuristic and be misclassified. Consider calling `looksLikeAssistantProse(raw)` (or adjusting the sentence heuristic to be case-insensitive / not depend on capitals).

How can I resolve this? If you propose a fix, please make it concise.

@Takhoffman
Copy link
Contributor

Fixed in #12988.

This will go out in the next OpenClaw release.

If you still see this after updating to the first release that includes #12988, please open a new issue with:

  • your OpenClaw version
  • channel (Telegram/Slack/etc)
  • the exact prompt/response that got rewritten
  • whether Web UI showed the full text vs the channel being rewritten
  • relevant logs around send/normalize (if available)

Link back here for context.

@Takhoffman
Copy link
Contributor

Closing as superseded by the merged sanitize/error-context work:

This PR’s intent appears covered by those merged changes and current mainline tests.

@Takhoffman Takhoffman closed this Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sanitizeUserFacingText false-positive: normal assistant responses about billing/pricing replaced with billing error warning

2 participants

Comments