Skip to content

feat(runtime): resolve relative image sources in WebFetch Markdown output - #5598

Merged
Astro-Han merged 2 commits into
apache:mainfrom
wutongyuonce:feat/runtime-webfetch-absolute-image-urls
Sep 24, 2026
Merged

Astro-Han merged 2 commits into
apache:mainfrom
wutongyuonce:feat/runtime-webfetch-absolute-image-urls

Conversation

@wutongyuonce

Copy link
Copy Markdown
Contributor

Summary

Fixes #5596

WebFetch converts fetched HTML to Markdown for the model. <a href> values were rewritten to absolute URLs against the document base, but <img src> was not, so a page without a <base> element produced Markdown whose images the model cannot resolve:

See [the setup notes](https://docs.example.com/guide/intro/setup) before wiring this up.

![wiring](../images/wiring.png)      <- meaningless outside the page it came from

Rewrite img[src] through the same base-URL path as [href], so both Markdown link forms carry absolute URLs.

Verification

Ran from apache/maka@30c406c9e plus this change:

  • npm --workspace @maka/runtime run typecheck — passed
  • npm --workspace @maka/runtime run build — passed
  • node --test .../dist/__tests__/local-web-fetch.test.js — 14 passed, 0 failed
  • node --test .../dist/__tests__/web-fetch-tool.test.js — 19 passed, 0 failed
  • npx biome check on both changed files — clean

Measured against the built executor, page https://docs.example.com/guide/v2/install:

input before after
<img src="../images/wiring.png">, no <base> ../images/wiring.png https://docs.example.com/guide/images/wiring.png
<img src="logo.png"> with <base href="https://cdn.example/assets/"> already absolute unchanged
<a href="../intro/setup">, no <base> already absolute unchanged

One new test fails on the unmodified base (resolves relative image sources against the response URL). The other new test passes both before and after by design — it guards against the rewrite mangling already-absolute and inline data: sources rather than pinning new behavior. I dropped a <base>-relative image test after measuring that Readability already resolves those URIs, which would have made it unable to fail.

Not run: no live site was fetched; the tests drive the executor with synthetic responses.

AI use

Select exactly one:

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Pi (pi-coding-agent) traced the extraction path, wrote the change and tests, and ran the verification above. The commit carries a Generated-by: Pi (pi-coding-agent) trailer.

Checklist

  • Tests cover the change and fail without it
  • Lint, format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

Turndown renders `<img src>` as a Markdown image, but WebFetch only rewrote
`<a href>` against the document base. On a page without a `<base href>`, the
extracted Markdown kept image sources relative:

    <img alt="wiring" src="../images/wiring.png">
    -> ![wiring](../images/wiring.png)

A relative path stops meaning anything once the model reads the text apart
from the page it was extracted from, so it cannot cite the image or fetch it
again.

Rewrite `img[src]` through the same base-URL path as `[href]`, so both
Markdown link forms carry absolute URLs. Verified unchanged behaviour for
pages that declare `<base href>` (Readability already fixed those), for
`[href]` rewriting, and for absolute and inline `data:` image sources.

Generated-by: Pi (pi-coding-agent)
@github-actions github-actions Bot added the effort/S Under 100 readable lines label Sep 22, 2026

@jackwener jackwener left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review result: GO — no findings

Exact head: c732a259626f85e40761fcaddb97930d3037d6c1

The reported problem is real on the production conversion path. WebFetch passes the final response URL into htmlToMarkdown; links are normalized before Readability/Turndown produces the model-visible Markdown, but the old code left an ordinary relative <img src> unchanged. Restoring that behavior makes the new regression fail with ![wiring](../images/wiring.png).

This patch fixes the behavior at the shared HTML-to-Markdown boundary, reuses the existing document-base calculation, and does not introduce a second URL authority. Absolute HTTP URLs and inline data: sources retain their values, malformed page-authored values remain non-fatal, and existing link rewriting is unchanged. The two-file scope is the smallest complete change for this bug. The commit and PR body also contain the required generative-tool attribution.

Verification

  • Exact-head Runtime build passed.
  • Exact-head affected tests passed: 18/18 across local WebFetch conversion and the model-facing WebFetch tool.
  • Removing only the new img[src] rewrite makes the relative-image regression fail while preserving the old output, which confirms that the test distinguishes the fix.
  • Biome, ASF header audit, and git diff --check passed.
  • Current main was 6cb8c58084d043f9b87421807fbee1d1ad3bdc03; the synthetic merge was clean (b7271ccdd333a44798ded98de582b5c35c6a46b0, tree 1284f28866755f846143da5a26f9addc6e6600ff) and the same 18 affected tests passed on that merge result.
  • All hosted exact-head checks were successful.

I did not approve or merge this PR.


Automated review notice: This comment was posted by an automated review agent operated by jackwener. It is not an independent human review and does not replace one.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — verified at c732a2596. The approach is right: img[src] now shares the same base-URL resolution as [href] (page-authored <base> respected, malformed values kept as-is, data:/absolute/protocol-relative untouched). Confirmed end-to-end through the Readability→Turndown pipeline.

One coverage gap worth fixing before this lands — details inline: relative sources that Readability's _fixLazyImages synthesizes after the rewrite loop still reach the Markdown unresolved, so lazy-loaded pages (arguably the more common shape for this defect) get no benefit. The fix is a one-liner and I verified it closes every leak case including srcset.

One nuance on the dropped <base>-relative test: it does pass on base, but the mechanism is accidental — the pre-existing [href] loop happens to absolutize the <base href> element itself, which makes linkedom's baseURI absolute, which is what lets Readability's _fixRelativeUris work. Worth a comment or a pinned test so a future refactor doesn't break that chain silently.

Minor P3s, no action needed: whitespace-only src resolves to the page URL itself (matches real-browser Readability); malformed src is kept as-is and produces a broken ![x](…) — same policy href already had.

中文

方向正确、实现最小:img src 与 href 共走 base-URL 解析,<base>/malformed/absolute 行为全部正确。一个覆盖缺口建议落地前修:Readability 的 _fixLazyImages 在改写循环之后才把 data-src 等值合成 src,懒加载图片的相对路径仍会原样漏进 markdown——修法一行(无 <base> 时注入 <base href>),已验证全类泄漏闭合。另注意"<base> 相对图片已被解析"是巧合机制(href 循环顺带的副作用),建议注释或测试固定。余下两条 P3 无行动项。


Review generated with AI assistance (Devin); verified against head c732a2596.

}
}
}
const article = new Readability(document as unknown as Document).parse();

@Astro-Han Astro-Han Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 — trigger ① (normal user path): relative sources synthesized by Readability's _fixLazyImages still reach the Markdown unresolved. The rewrite loop only sees the pre-Readability DOM. During parse(), _fixLazyImages (Readability.js:~2332) copies data-src/data-original/data-srcset/figure[data-src] values into src/srcset after this loop ran, and _fixRelativeUris then cannot resolve them because linkedom's document.baseURI is null on pages without a <base> element (interface/node.js — it returns the raw <base> href or location.href, which is absent). Lazy-loading is the common shape for this exact defect, so a page whose images are all lazy-loaded still produces ![](/relative.png) — the PR delivers nothing there. Verified end-to-end on the head pipeline: <img data-src="/r.png">, <img src="/ph.png" data-src="/r.png">, <figure data-src>, and protocol-relative values through that path all leak.

Smallest fix (verified): give the document an absolute baseURI so Readability's own _fixRelativeUris does the work — e.g. when no <base> exists, document.head?.insertAdjacentHTML('afterbegin', ) before parse(). That resolves the lazy-synthesized src/srcset/poster for free, and keeps the explicit loop for everything else. A test that would have failed on base: <img data-src="/real.png"> under a page with no <base>.

中文

改写循环只能看到 Readability 之前的 DOM;_fixLazyImages 在 parse() 内把 data-src 等值合成 src/srcset 发生在循环之后,而无 <base> 页面 linkedom 的 baseURI 为 null、_fixRelativeUris 解析失败 → 懒加载图片相对路径原样漏出(已端到端验证:data-src、占位 src+data-src、figure[data-src]、协议相对全漏)。最小修法:无 <base> 时注入 <base href="${baseUrl}">,让 _fixRelativeUris 像浏览器一样工作,srcset/poster 顺带解决;补一个 base 上会失败的回归(<img data-src="/real.png"> + 无 <base>)。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Astro-Han Fixed in a14fb53. Before Readability parses the page, htmlToMarkdown now installs the resolved document base URL, including a page-authored <base> value. Readability can then resolve lazy-loaded image URLs it synthesizes after the explicit rewrite pass. Added tests for data-src with and without an authored base; all 16 local WebFetch tests pass and Biome is clean. Runtime build is blocked by four TS7006 errors in unrelated model-factory and OpenAI test files in this checkout.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at a14fb53. The lazy-image gap is closed: installing the resolved base URL into the document before Readability runs lets _fixRelativeUris handle data-src/srcset/poster per browser semantics, and it removes the accidental dependence on the href loop absolutizing . Verified end-to-end against linkedom+Readability; both regressions pinned. No remaining findings.

@Astro-Han
Astro-Han merged commit fd490b7 into apache:main Sep 24, 2026
1 check passed
@wutongyuonce
wutongyuonce deleted the feat/runtime-webfetch-absolute-image-urls branch September 24, 2026 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/S Under 100 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(runtime): resolve relative image sources in WebFetch Markdown output

3 participants