fix(chat): make task-notification parsing tolerant of field order and whitespace#862
fix(chat): make task-notification parsing tolerant of field order and whitespace#862bourgois wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThe task-notification parsing logic in ChangesChat message task-notification parsing
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
… whitespace The task-notification regex required an exact field sequence (task-id, output-file, status, summary) with no leading/trailing content, and ran with the /g flag against the raw string. Notifications whose fields arrive in a different order, omit optional fields, or carry surrounding whitespace failed to match and rendered as raw XML in the chat instead of a formatted notification. Anchor the match to the trimmed content and use non-greedy [\s\S]*? between the status and summary captures so field order and incidental whitespace no longer break parsing.
cf4c1f6 to
c98abb4
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/components/chat/hooks/useChatMessages.ts`:
- Around line 58-59: The current single regex (taskNotifRegex) requires <status>
before <summary> and is order-dependent; change the logic to first extract the
whole <task-notification> block (e.g. via a regex matching
<task-notification>[\s\S]*?<\/task-notification>) then separately extract fields
from that inner content with two independent regexes for <status> and <summary>
so either order is accepted; update the code that uses
taskNotifRegex/taskNotifMatch in useChatMessages.ts to use the two-step
extraction and handle missing fields gracefully.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 03137d6a-112e-4806-8178-74e2161bc7fb
📒 Files selected for processing (1)
src/components/chat/hooks/useChatMessages.ts
| const taskNotifRegex = /^<task-notification>[\s\S]*?<status>([^<]*)<\/status>[\s\S]*?<summary>([^<]*)<\/summary>[\s\S]*?<\/task-notification>$/; | ||
| const taskNotifMatch = taskNotifRegex.exec(content.trim()); |
There was a problem hiding this comment.
Regex is still order-dependent for status vs summary
This pattern still requires <status> to appear before <summary>, so notifications with reversed order won’t parse and will fall back to raw XML rendering. That contradicts the PR’s field-order tolerance goal.
Suggested fix
-const taskNotifRegex = /^<task-notification>[\s\S]*?<status>([^<]*)<\/status>[\s\S]*?<summary>([^<]*)<\/summary>[\s\S]*?<\/task-notification>$/;
-const taskNotifMatch = taskNotifRegex.exec(content.trim());
+const trimmed = content.trim();
+const isTaskNotif = /^<task-notification>[\s\S]*<\/task-notification>$/.test(trimmed);
+const statusMatch = /<status>([^<]*)<\/status>/.exec(trimmed);
+const summaryMatch = /<summary>([^<]*)<\/summary>/.exec(trimmed);
+const taskNotifMatch = isTaskNotif && statusMatch && summaryMatch
+ ? [trimmed, statusMatch[1], summaryMatch[1]]
+ : null;🧰 Tools
🪛 OpenGrep (1.22.0)
[ERROR] 59-59: Dynamic command passed to child_process.exec/execSync. Use child_process.execFile or spawn with an argument array instead.
(coderabbit.command-injection.exec-js)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/components/chat/hooks/useChatMessages.ts` around lines 58 - 59, The
current single regex (taskNotifRegex) requires <status> before <summary> and is
order-dependent; change the logic to first extract the whole <task-notification>
block (e.g. via a regex matching
<task-notification>[\s\S]*?<\/task-notification>) then separately extract fields
from that inner content with two independent regexes for <status> and <summary>
so either order is accepted; update the code that uses
taskNotifRegex/taskNotifMatch in useChatMessages.ts to use the two-step
extraction and handle missing fields gracefully.
Small, self-contained fix extracted from #855 at the maintainer's request.
Issue
normalizedToChatMessagesparses background<task-notification>messages with a regex that:task-id→output-file→status→summary,/gflag against the raw (untrimmed) string.When a notification's fields arrive in a different order, omit an optional field, or carry surrounding whitespace, the match fails and the message renders as raw XML in the chat instead of a formatted notification.
Reproduction
task-id, output-file, status, summaryorder.<task-notification>…</task-notification>markup.Fix
Anchor the match to the trimmed content and use non-greedy
[\s\S]*?between thestatusandsummarycaptures, so field order and incidental whitespace no longer break parsing.npm run typecheckandeslintpass. One-line change inuseChatMessages.ts.Summary by CodeRabbit