Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

fix: guard countMarkdownHeadings against non-string input - #11882

Draft
ghost wants to merge 1 commit into
mainfrom
fix/markdown-heading-type-guard
Draft

fix: guard countMarkdownHeadings against non-string input#11882
ghost wants to merge 1 commit into
mainfrom
fix/markdown-heading-type-guard

Conversation

@ghost

@ghost ghost commented Mar 7, 2026

Copy link
Copy Markdown

Related GitHub Issue

Closes: #11879

Description

This PR attempts to address Issue #11879. When returning from a subtask to the parent task, message.text can be a non-string truthy value at runtime. The existing if (!text) guard in countMarkdownHeadings() only catches falsy values, so calling .replace() on a non-string throws TypeError: n.replace is not a function.

The fix adds a typeof text !== "string" check to defensively handle non-string inputs, returning 0 instead of crashing.

Changes:

  • webview-ui/src/utils/markdown.ts: Added typeof guard to countMarkdownHeadings
  • webview-ui/src/utils/__tests__/markdown.spec.ts: Added test cases for non-string inputs (number, array, object, boolean)

Feedback and guidance are welcome.

Test Procedure

  • Added unit tests that pass non-string values (number, array, object, boolean) to countMarkdownHeadings and hasComplexMarkdown, verifying they return 0/false instead of throwing.
  • All existing tests continue to pass.
  • Run: cd webview-ui && npx vitest run src/utils/__tests__/markdown.spec.ts

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue.
  • Scope: Changes are focused on the linked issue.
  • Self-Review: Performed a self-review of the code.
  • Testing: New tests added to cover the fix.
  • Documentation Impact: No documentation updates required.
  • Contribution Guidelines: Read and agree to the Contributor Guidelines.

Screenshots / Videos

N/A (no UI changes)

Documentation Updates

  • No documentation updates are required.

Additional Notes

Minimal, defensive fix that prevents the crash without changing any upstream data flow.

Interactively review PR in Roo Code Cloud

When a subtask returns to the main task, message.text can be a
non-string truthy value (e.g. array or object). The existing falsy
check (!text) lets these through, causing a TypeError on .replace().

Add a typeof check so non-string values safely return 0.

Fixes #11881
@ghost
ghost force-pushed the fix/markdown-heading-type-guard branch from 10ac9ff to dbc1d29 Compare March 7, 2026 07:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] 子任务返回主任务时报错

1 participant