Skip to content

[staging, do not merge] CI for PR #10319 connect-layer address walk - #13

Closed
Lyxot wants to merge 5 commits into
mainfrom
staging-ci-10319
Closed

Lyxot wants to merge 5 commits into
mainfrom
staging-ci-10319

Conversation

@Lyxot

@Lyxot Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Owner

Staging PR to run CI against the head of unslothai/unsloth#10319 (e195035). Not for merge — will be closed once CI reports.

NilayYadav and others added 5 commits September 4, 2026 22:15
Retrying `opener.open` once per address divided the caller's timeout between them, and `create_connection` leaves whatever timeout it was given on the socket it returns, so that division bounded time to first byte rather than just the connect. Measured against the merge base on a host whose first record is unroutable: main answered in 9.0s, the retry loop failed at 30.0s.

Walking inside `HTTPConnection._create_connection` instead keeps the fallback in the connect phase, so the caller's timeout still covers one whole response, and it reaches the Gemini image fetch through the same handler.

The walk spends one budget across two passes: a brief probe of every address, capped at 1s each and at a quarter of the timeout in total, so an address that answers neither way cannot hold up the reachable ones; then an equal share of what is left, so no address strands the ones behind it. Every address is dialled in both passes, a dial that overruns still leaves the rest an attempt, and with no timeout this collapses to the single uncapped pass `create_connection` performs. Only a direct request carries the addresses: a proxied one reaches the origin through the proxy.

Validated with a 24-mutation sweep over the walk, the handlers and the routing decision, each mutation caught by a named test, and with live fetches against hosts given unroutable addresses ahead of their real ones, asserting the walk actually dialled them: 30 blackholed addresses first still connects, in 7.7s over 31 dials.
@Lyxot

Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Owner Author

CI complete on e195035: 33 pass, 1 fail, 2 skipped.

The single failure is Repo tests (CPU) -> tests/studio/test_model_picker_contracts.py, two static-string assertions against frontend TypeScript. Verified pre-existing on main: checked out ad67389 with nothing from unslothai#10319 and both fail there too. The rest of that job is 18173 passed.

Chat UI Tests (Windows lane) failed first time on a Playwright Page.goto 60s timeout with the server killed at 143; re-ran the job and it passed. Flake. All four Linux UI lanes passed both times.

Closing -- this was only here to get CI on the head.

@Lyxot Lyxot closed this Sep 9, 2026
Lyxot pushed a commit that referenced this pull request Nov 16, 2026
…caped code stays in its code block (unslothai#17200)

Fixes unslothai#17199.

## Problem

When the model fetches a page, Studio's HTML-to-Markdown converter:

- appends a `;` to a bare `&` followed by a letter, and decodes names
that are references only with their `;`. For example, `AT&T.` becomes
`AT&T.;` and `?key=abc&format=json&lang=en` becomes
`?key=abc&format;=json⟨=en`.
- moves escaped characters out of a `<pre>` in a table cell. That is the
layout of line-numbered code from Pygments' table formatter, Rouge and
Chroma, so `if a < b and c > 0:` reads `if 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 `&nbsp;`.
- can pass the rest of the page through as raw HTML:
- on Python 3.13 and 3.14, after a decimal reference that has no `;` and
runs into a letter a–f (`&#169Copyright`);
- on 3.11 and 3.12, after a `&#` 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 `HTMLParser` with `convert_charrefs=False` and decoded
references itself in `handle_entityref` and `handle_charref`:

- `HTMLParser` reports `&name` whether or not a `;` follows, and the
handler always decoded `&name;`.
- The decoded text went through `_emit()`, which picks an open link or
table cell before an open `<pre>`, while the text around it went to the
`<pre>`.
- With `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.
- References decode inside their text run with `html.unescape`, as a
browser decodes text, and the decoded run reaches `handle_data`, so it
is routed with the text around it.
- `HTMLParser` then never calls `handle_entityref` or `handle_charref`.
They, their two replay branches and the `html` import are removed.
- The `_TokenLog` docstring now names the three handlers its replay
contract covers.
- **U+00A0 is not collapsed.**
- `&nbsp;` used to bypass the whitespace collapse in `handle_data`.
Decoded inside the text run, it would be collapsed, and code indented
with `&nbsp;` (W3Schools writes its examples that way) would lose its
indentation.
- Emitted text now collapses whitespace other than U+00A0
(`_COLLAPSIBLE_RUN`), as a browser does.
- The header link-density tally still counts the text with U+00A0
collapsed, so a link header padded with `&nbsp;` is stripped as before.
- **Decimal references longer than 640 digits are shortened before
parsing.**
- `html.unescape` raises `ValueError` once a decimal reference has more
digits than `int()`'s limit. Python never sets that limit below 640, and
leading zeros count toward it.
- `html_to_markdown` drops the leading zeros of such a reference. If 8
or more digits remain, it writes `&#65533`, which is the U+FFFD that
`html.unescape` gives past U+10FFFF.
- On every version, main raised for such a reference ended by `;` 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. With
`convert_charrefs=True` alone, 3.11 and 3.12 would raise for those two
as well.

## Other behavior changes

- **Literal U+00A0:** a literal U+00A0 in flowing text is now kept, as
`&nbsp;` already was, instead of becoming a space.
- **Other whitespace references:** `&ensp;`, `&emsp;`, `&thinsp;`,
`&unslothai#8239;`, `&#9;`, `&#10;` and `&#13;` 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.
- **U+00A0-only text between table elements:** this is now dropped like
other whitespace there.
  - Main put it in front of the next Markdown row (`\xa0| c | d |`).
- It also drops Javadoc 8's method-summary tab separators, which sit
inside a `<caption>`: `All Methods\xa0Static Methods` becomes `All
MethodsStatic Methods`.
- **List markers:** a list item whose first text is a reference after
source indentation now gets the two spaces after its marker that literal
text already gets (`* "Quoted" title`).
- **Legacy reference names in query parameters:** a parameter that
starts with a legacy reference name still decodes, as in a browser, but
without the stray `;`. `&param=x` was `¶m;=x` and is now `¶m=x`.

## Testing

All runs were on macOS 26 (arm64).

- **New tests:** 12 cases in `test_web_fetch_extraction.py`, which make
28 tests with the `main_content` variants.
- Against `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.
- The 6 that pass on main are guards: references still decode,
`&nbsp;`-indented text keeps its indentation, and a `&nbsp;`-padded link
header is still stripped.
  - All 28 pass on this branch on 3.11.16, 3.12.7, 3.13.11 and 3.14.7.
- **Converter test files** (`test_html_to_md_*.py` and
`test_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.
- **Related backend tests:** 47 backend test files around web fetch and
the tool loop, on Python 3.12.7 with the backend CI dependencies.
- This branch: 8232 passed, 9 failed, 3 skipped. `origin/main`: 8204
passed, 9 failed, 3 skipped. The 28 extra passes are the new tests.
- The same 9 deferred workspace-deletion tests in
`test_sandbox_files_and_storage_roots.py` fail on both on this machine.
- The same 3 tests skip on both, because `unsloth_zoo` is not installed
and a tokenizer file was not fetched.
- **Live pages:** 155 saved responses (153 distinct URLs) from
documentation, code-heavy, prose and data sites. They are 133 pages (7
of which give empty Markdown with both converters), 17 Cloudflare
challenge pages, 4 "Access Denied" pages and 1 empty file. Converted
with `main_content=True`:
- **112 are identical**, including all 29 non-pages and empty
conversions.
- **25 differ only in whitespace.** 24 come from a literal U+00A0 that
is now kept. The other is legislation.gov.uk, where `&#xD;` no longer
emits a raw carriage return.
- **18 differ in content.** A script sorted the differing hunks on those
pages:
    - 619 drop a stray `;` or keep a bare `&` that main decoded.
    - 53 put characters back in their `<pre>`.
- 12 keep a U+00A0 from `&nbsp;` inside a link in a `<pre>`, which main
dropped (RFC section links, Cloudflare-protected email addresses in code
samples).
    - 1 is the Javadoc caption change above.
- Across these outputs, U+00A0 goes from 2,822 to 12,253 characters,
0.15% of the branch's 8.26M.
- **Robustness and speed:**
- 3,000 generated documents in both modes, on each of 3.11.16, 3.12.7,
3.13.11 and 3.14.7: 0 exceptions on this branch, 18 on main.
- CPU time over the 155 responses (49 MB) on 3.13.11, as the sum of
per-page minimums over 5 interleaved runs: 4.52 s on main and 4.44 s on
this branch. The pre-pass costs 14 ms over the whole 49 MB.
- **Lint:** `python scripts/run_ruff_format.py` (ruff-unsloth
0.6.9.post1), `ruff check` (0.17.3) and `git diff --check` pass.

## Not covered

- **`<caption>` rendering:** `<caption>` has no rendering rule. On both
main and this branch, a caption runs into its table's header row.
- **`_HTMLBodyLocator` in `tools.py`:** it also runs `HTMLParser` with
`convert_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.

---------

Co-authored-by: Nilay Yadav <[email protected]>
Co-authored-by: Daniel Han <[email protected]>
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.

2 participants