Skip to content

fix: guard countMarkdownHeadings against non-string input#11882

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/markdown-heading-type-guard
Draft

fix: guard countMarkdownHeadings against non-string input#11882
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/markdown-heading-type-guard

Conversation

@roomote-v0
Copy link
Contributor

@roomote-v0 roomote-v0 bot commented Mar 7, 2026

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
@roomote-v0 roomote-v0 bot force-pushed the fix/markdown-heading-type-guard branch from 10ac9ff to dbc1d29 Compare March 7, 2026 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant