#19868: fix: prevent media token regex from matching markdown bold text
agents
size: XS
Cluster:
Media Handling Improvements
Fixes #19847
## Problem
`extractToolResultMediaPaths` matched markdown bold markers (`**text**`) as media tokens, causing false positives like `**I` being treated as a file path.
## Changes
- **src/media/parse.ts**: Export `isValidMedia()` function that validates extracted paths against known media extensions and patterns
- **src/agents/pi-embedded-subscribe.tools.ts**: Add `isValidMedia()` validation before accepting extracted paths
- **src/media/parse.test.ts**: Add regression tests for bold text false positives
## Testing
- 2 regression tests added verifying `**text**` patterns are rejected
- Existing tests pass
---
*Replaces #19855 which was closed due to an accidentally committed log file. This PR has clean history — no PII.*
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixes false positives in `extractToolResultMediaPaths` where any non-empty string under 4096 characters after a `MEDIA:` token was accepted as a valid media path. The fix reuses the existing `isValidMedia()` validator from `src/media/parse.ts` (previously module-private, now exported) to ensure extracted strings are structurally valid media references (URLs, local file paths, or bare filenames with extensions). This aligns `extractToolResultMediaPaths` with the same validation already used in `splitMediaFromOutput`.
- Replaced weak `p.length <= 4096` guard with `isValidMedia(p, { allowSpaces: true })` in `extractToolResultMediaPaths`
- Exported `isValidMedia` from `src/media/parse.ts` (no logic changes to the function itself)
- Added 2 regression tests covering markdown bold patterns and non-path strings
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — it tightens validation using an existing, well-tested function with no behavioral regressions.
- The change is minimal and well-scoped: it replaces a weak length-only guard with an existing structural validator (`isValidMedia`) that was already battle-tested in `splitMediaFromOutput`. The function's logic is unchanged — only its visibility is widened via `export`. The new validation is strictly more restrictive than the old check (anything passing `isValidMedia` also passes `length <= 4096`), so no existing valid media paths are rejected. Regression tests are included.
- No files require special attention
<sub>Last reviewed commit: 6557c99</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#18890: fix(media): parse tool-result MEDIA directives with shared parser
by teededung · 2026-02-17
88.9%
#18811: fix(media): require file extension for ambiguous MEDIA: path detection
by aldoeliacim · 2026-02-17
85.2%
#14794: fix: parse inline MEDIA: tokens in agent replies
by explainanalyze · 2026-02-12
84.2%
#19399: telegram: fix MEDIA false positives and partial final drop
by HOYALIM · 2026-02-17
83.7%
#21110: fix(tts): deliver audio via structured mediaUrl instead of MEDIA: t...
by hydro13 · 2026-02-19
79.9%
#20735: fix: skip auto-attaching tool MEDIA: paths already sent via message t…
by anillBhoi · 2026-02-19
78.6%
#7400: media: allow temp-dir MEDIA paths for tool outputs
by grammakov · 2026-02-02
78.4%
#21513: Agents: track TTS media in duplicate filter state
by DevvGwardo · 2026-02-20
78.0%
#22088: fix(web): sanitize media errors to prevent PII leak
by ashiabbott · 2026-02-20
78.0%
#9817: fix(media): resolve relative paths before reading local files (#8759)
by lailoo · 2026-02-05
77.1%