Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix(filesystem): keep glob tokens and code-span stars literal in PDFs

lei_leibeb7494caf7d
diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.py
index f008eee19..5864af92f 100644
--- a/browser_use/filesystem/file_system.py
+++ b/browser_use/filesystem/file_system.py
@@ -99,15 +99,27 @@ def _markdown_inline_to_rml(text: str) -> str:
injected ``<b>`` / ``<i>`` / ``<font>`` tags are unambiguous.
Underscore emphasis is intentionally unsupported so ``snake_case`` identifiers
- survive unchanged.
+ survive unchanged. Inline code is stashed before emphasis so markers inside
+ backticks stay Courier-only. Bold content cannot start with ``/``, so globs
+ like ``**/foo/**`` stay literal.
"""
text = html.escape(text)
+ # Stash code spans first — Markdown renders their contents literally.
+ code_spans: list[str] = []
+
+ def _stash_code(match: re.Match[str]) -> str:
+ code_spans.append(match.group(1))
+ return f'\x00C{len(code_spans) - 1}\x00'
+
+ text = re.sub(r'`([^`]+)`', _stash_code, text)
# Bold before italic so ``**`` is not treated as two italic markers.
# Content cannot contain ``*`` — otherwise globs like ``*.txt and **/*.py`` pair across tokens.
- text = re.sub(r'\*\*([^\s*](?:[^*]*[^\s*])?)\*\*', r'<b>\1</b>', text)
+ # Content cannot start with ``/`` — otherwise ``**/foo/**`` is treated as bold.
+ text = re.sub(r'\*\*([^\s*/](?:[^*]*[^\s*])?)\*\*', r'<b>\1</b>', text)
# Non-space boundaries keep ``2 * 3 * 4`` literal; leading ``* `` is a bullet, not italic
text = re.sub(r'(?<!\*)\*([^\s*](?:[^*]*[^\s*])?)\*(?!\*)', r'<i>\1</i>', text)
- text = re.sub(r'`([^`]+)`', r'<font face="Courier">\1</font>', text)
+ for i, span in enumerate(code_spans):
+ text = text.replace(f'\x00C{i}\x00', f'<font face="Courier">{span}</font>')
return text
diff --git a/tests/ci/infrastructure/test_filesystem.py b/tests/ci/infrastructure/test_filesystem.py
index f5cd81937..0bf457482 100644
--- a/tests/ci/infrastructure/test_filesystem.py
+++ b/tests/ci/infrastructure/test_filesystem.py
@@ -17,6 +17,7 @@ from browser_use.filesystem.file_system import (
JsonlFile,
MarkdownFile,
TxtFile,
+ _markdown_inline_to_rml,
_split_heading,
)
@@ -53,6 +54,16 @@ class TestBaseFile:
assert _split_heading('plain') == ('plain', None)
assert _split_heading('#### too deep') == ('#### too deep', None)
+ def test_markdown_inline_glob_is_not_bold(self):
+ """Recursive globs must not be treated as bold delimiters."""
+ assert _markdown_inline_to_rml('**/foo/**') == '**/foo/**'
+ assert _markdown_inline_to_rml('**bold** and **/foo/**') == '<b>bold</b> and **/foo/**'
+
+ def test_markdown_inline_code_protects_emphasis(self):
+ """Emphasis markers inside backticks stay Courier, not italic/bold."""
+ assert _markdown_inline_to_rml('`*literal*`') == '<font face="Courier">*literal*</font>'
+ assert _markdown_inline_to_rml('`**bold**`') == '<font face="Courier">**bold**</font>'
+
def test_txt_file_creation(self):
"""Test TxtFile creation and basic properties."""
txt_file = TxtFile(name='notes', content='Hello\nWorld')
@@ -308,6 +319,7 @@ class TestFileSystem:
[
'Arithmetic 2 * 3 * 4 stays literal',
'Glob pattern *.txt and **/*.py stay literal',
+ 'Recursive glob **/foo/** stays literal',
]
)
result = await empty_filesystem.write_file('stars.pdf', source)
@@ -316,6 +328,7 @@ class TestFileSystem:
assert '2 * 3 * 4' in blob
assert '*.txt' in blob
assert '**/*.py' in blob
+ assert '**/foo/**' in blob
async def test_write_pdf_underscore_is_not_italic(self, empty_filesystem):
"""Underscores are identifiers, not emphasis."""
@@ -342,6 +355,17 @@ class TestFileSystem:
assert 'Run $(cmd) inline.' in blob
assert 'echo `$(cmd)`' in blob
+ async def test_write_pdf_inline_code_protects_emphasis_markers(self, empty_filesystem):
+ """Stars inside inline code stay literal instead of becoming italic/bold."""
+ source = 'Keep `*literal*` and `**bold**` as code.'
+ result = await empty_filesystem.write_file('code_stars.pdf', source)
+ assert result == 'Data written to file code_stars.pdf successfully.'
+ blob, _ = _extract_pdf_text(empty_filesystem.data_dir / 'code_stars.pdf')
+ assert '*literal*' in blob
+ assert '**bold**' in blob
+ assert '`*literal*`' not in blob
+ assert '`**bold**`' not in blob
+
def test_filesystem_initialization(self, temp_filesystem):
"""Test FileSystem initialization with default files."""
fs = temp_filesystem