Repository navigation
[CS146S Stanford] Studio web fetch: a bare & stays as written, and escaped code stays in its code block - #17200
Conversation
…n its code block The HTML-to-Markdown converter ran HTMLParser with convert_charrefs=False and decoded each reference itself. HTMLParser reports "&name" whether or not a ";" follows, and the handler always decoded "&name;", so "AT&T." became "AT&T.;" and "?key=abc&format=json&lang=en" became "?key=abc&format;=json⟨=en". The decoded text also went through _emit(), which picks an open link or table cell before an open <pre>, so the escaped characters of a line-numbered code block (a <pre> in a table cell) landed outside the code. And depending on the Python version, a malformed "&#" (©Copyright, or a "&#" without digits) ended the parse and left the rest of the page as raw HTML. Let HTMLParser decode references inside their text run, and drop the handlers and replay branches it no longer calls. Keep U+00A0 out of the whitespace collapse of emitted text, as browsers do, so text indented with keeps its indent, while the header link-density tally still counts it collapsed. Shorten decimal references longer than 640 digits before parsing, since html.unescape raises past int()'s digit limit.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f4954306ff
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ) | ||
| if site_links is not None: | ||
| scored = site_links.clean(scored) | ||
| scored = _fold_kept_spaces(scored) |
There was a problem hiding this comment.
Re-clean folded spacer lines before ranking scopes
For two sibling <article> elements without a <main>, NBSP-only block spacers still inflate scope ranking. The added fold runs after _cleanup, so those lines are never cleaned up after becoming ASCII whitespace. A 240-character teaser followed by 400 <p>\xa0</p> spacers and <p>More information</p> scores 1,457 and displaces a 999-character real article; the parent selects the real article instead. Unlike the fixed contiguous-run case, this leaves hundreds of blank lines in len(scored). Re-clean the folded scoring view before ranking it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: it needs hundreds of empty spacer paragraphs, and the merge base already ranks the same page this way when the spacers are written as .
| # Collapse all whitespace (including newlines) per HTML rules. | ||
| text = _WHITESPACE_RUN.sub(" ", data) | ||
| # Collapse whitespace (including newlines) per HTML rules, which keep U+00A0 and other Unicode spaces. | ||
| text = _COLLAPSIBLE_RUN.sub(" ", data) |
There was a problem hiding this comment.
Fold kept whitespace before the boilerplate length check
When a boilerplate notice is padded with literal U+00A0, preserving those characters can now push its raw line length past the 300-character check in _strip_boilerplate_lines before _line_is_boilerplate gets to normalize it. For example, a <main> containing <p>This website uses cookies followed by 17,000 NBSPs, .</p>, and a body paragraph previously returned just the body, but now retains the notice and padding. tools._render_page consequently truncates at its 16,000-character cap before reaching any body text. Apply the folded whitespace view to the boilerplate length check as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: 17,000 no-break spaces inside a cookie notice is not a real page, and the merge base already keeps that notice when the padding is .
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Review + reproduction evidencePinned review: I reproduced a chat model fetching the Google Maps URLs docs with the web search tool on the merge base
Conclusion: the bug is real and fixed on this branch, with no regression on the pages checked. Ready once CI on the current head is green. |
NilayYadav
left a comment
There was a problem hiding this comment.
Thank you for the PR! The goal of this PR is to make Studio's web fetch give the model the text a browser shows, so a bare & keeps its meaning and escaped characters stay inside their code block. As a summary, this PR lets HTMLParser decode character references inside each text run instead of in separate handlers, keeps no-break and other Unicode spaces when collapsing whitespace, and shortens very long decimal references so html.unescape cannot raise.
| Reviewers | Severity | Finding |
|---|---|---|
| - | - | No open findings on the current head. The issues found during review (caption separators,   indentation, and no-break space padding changing which part of a page is kept) are fixed in the commits I pushed to this branch. |
[skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |

Fixes #17199.
Problem
When the model fetches a page, Studio's HTML-to-Markdown converter:
;to a bare&followed by a letter, and decodes names that are references only with their;. For example,AT&T.becomesAT&T.;and?key=abc&format=json&lang=enbecomes?key=abc&format;=json⟨=en.<pre>in a table cell. That is the layout of line-numbered code from Pygments' table formatter, Rouge and Chroma, soif a < b and c > 0:readsif a b and c 0:inside the fence. Inside a link in a<pre>, it moves the escaped characters into the link text and drops a .;and runs into a letter a–f (©Copyright);&#with no digits when no;follows it.The issue lists live pages where this happens, such as Alpha Vantage's API samples, Google's Maps URLs docs and RFC 6749.
Root cause
The renderer ran
HTMLParserwithconvert_charrefs=Falseand decoded references itself inhandle_entityrefandhandle_charref:HTMLParserreports&namewhether or not a;follows, and the handler always decoded&name;._emit(), which picks an open link or table cell before an open<pre>, while the text around it went to the<pre>.convert_charrefs=False,HTMLParser's handling of a malformed&#depends on the Python version, and on each version some form of it ends the parse early.Changes
HTMLParser(convert_charrefs=True), the stdlib default.html.unescape, as a browser decodes text, and the decoded run reacheshandle_data, so it is routed with the text around it.HTMLParserthen never callshandle_entityreforhandle_charref. They, their two replay branches and thehtmlimport are removed._TokenLogdocstring now names the three handlers its replay contract covers. used to bypass the whitespace collapse inhandle_data. Decoded inside the text run, it would be collapsed, and code indented with (W3Schools writes its examples that way) would lose its indentation._COLLAPSIBLE_RUN), as a browser does. is stripped as before.html.unescaperaisesValueErroronce a decimal reference has more digits thanint()'s limit. Python never sets that limit below 640, and leading zeros count toward it.html_to_markdowndrops the leading zeros of such a reference. If 8 or more digits remain, it writes�, which is the U+FFFD thathtml.unescapegives past U+10FFFF.;or a space, or inside an attribute. On 3.13 and 3.14 it also raised for one followed by a hex letter or cut off at the end of the page. Withconvert_charrefs=Truealone, 3.11 and 3.12 would raise for those two as well.Other behavior changes
already was, instead of becoming a space. , , , ,	, and in flowing text now collapse like the characters they decode to, as those literal characters already did. Main emitted them as is, including a raw tab and carriage return.\xa0| c | d |).<caption>:All Methods\xa0Static MethodsbecomesAll MethodsStatic Methods.* "Quoted" title).;.¶m=xwas¶m;=xand is now¶m=x.Testing
All runs were on macOS 26 (arm64).
test_web_fetch_extraction.py, which make 28 tests with themain_contentvariants.origin/main's converter, 22 fail on Python 3.11.16 and 3.12.7, and 18 fail on 3.13.11 and 3.14.7, where CPython already parses a&#without digits as text. -indented text keeps its indentation, and a -padded link header is still stripped.test_html_to_md_*.pyandtest_web_fetch_extraction.py): 1567 passed on main and 1595 (1567 + 28) on this branch, on each of 3.11.16, 3.12.7, 3.13.11 and 3.14.7.origin/main: 8204 passed, 9 failed, 3 skipped. The 28 extra passes are the new tests.test_sandbox_files_and_storage_roots.pyfail on both on this machine.unsloth_zoois not installed and a tokenizer file was not fetched.main_content=True:
no longer emits a raw carriage return.;or keep a bare&that main decoded.<pre>. inside a link in a<pre>, which main dropped (RFC section links, Cloudflare-protected email addresses in code samples).python scripts/run_ruff_format.py(ruff-unsloth 0.6.9.post1),ruff check(0.17.3) andgit diff --checkpass.Not covered
<caption>rendering:<caption>has no rendering rule. On both main and this branch, a caption runs into its table's header row._HTMLBodyLocatorintools.py: it also runsHTMLParserwithconvert_charrefs=False. It only finds where<body>starts, so the download can stop a window after it, and it produces no text, so it is left as is.This contribution is part of Stanford's CS146S: The Modern Software Developer.