Skip to content

Add opt-in link prediction to Speculative Loading - #1509

Draft
adamsilverstein wants to merge 7 commits into
WordPress:trunkfrom
adamsilverstein:add/ai-preload-predictions
Draft

adamsilverstein wants to merge 7 commits into
WordPress:trunkfrom
adamsilverstein:add/ai-preload-predictions

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Aug 27, 2024 •

Copy link
Copy Markdown
Member

This started as an experiment with Chrome's built-in AI. I reworked it to use a small decision engine that calculates the probability of each link being visited instead, still entirely on device.

Claude rebuilt the prototype along those lines and wrote up what changed:

Summary

Adds opt-in link prediction to Speculative Loading: a small script estimates how likely the visitor is to follow each link on the page and speculatively loads the most likely ones right away, without waiting for a hover or click.

The original prototype sent the whole page HTML to the built-in language model and parsed URLs out of its answer. That needs a multi-gigabyte model download, only runs on some desktop hardware, takes seconds to answer, and the API it used (window.ai.assistant) no longer exists. The scoring model here runs in a few milliseconds in any browser that supports speculation rules, and the script is about 4 KB minified.

Relevant technical choices

  • Probability per link. Each link gets signals from 0 to 1: in the viewport, proximity to the middle of the viewport, clickable area, inside main content / heading / navigation / footer, contains an image, goes to the next page or post, and whether other links go to the same URL. The probability is the logistic function of the weighted sum. Links are scored when the browser is idle after load and again after scrolling.
  • It adapts. When the visitor follows a link, the weights take one logistic regression step (followed link as the positive example, the other links as negatives). Only the weight offsets are kept, in localStorage. No URLs are stored and nothing leaves the browser.
  • Existing exclusions keep applying. Predicted links get a data-plsr-predicted attribute (its value is the probability, handy for debugging). The script inserts a document rule with immediate eagerness that matches a[data-plsr-predicted] and the conditions of the main speculation rule, so /wp-admin/*, nofollow, .no-prerender and plsr_speculation_rules_href_exclude_paths are all honored by the browser itself. The rule is inserted from JS because core rejects immediate for document rules.
  • Opt-in and bounded. New "Link Prediction" setting under Settings > Reading, disabled by default. Defaults are at most 2 links per page view with a probability of at least 50%. It follows the configured mode (prefetch or prerender), is skipped for visitors with Save-Data on, and is off wherever speculative loading is off.
  • Filterable. plsr_link_prediction_args adjusts threshold, max_links, learning_rate (0 turns adapting off) and the individual weights.
  • predict.js is built to predict.min.js and printed as an inline module, the same way View Transitions ships its script.

Testing

  1. npm run build, then enable Settings > Reading > Link Prediction, with pretty permalinks on.
  2. Visit the blog home logged out in Chrome. In DevTools > Application > Speculative loads, the first two post links show as prerendered without any interaction, and the links carry data-plsr-predicted.
  3. Open a single post and scroll to the post navigation: the next post link is picked up.
  4. Follow a few links and check localStorage.plsrLinkPredictionWeights to see the weights adapt.

Checked in headless Chromium against wp-env with Twenty Twenty-Five: the predicted URLs were requested with Sec-Purpose: prefetch;prerender, and nothing else was. PHPUnit covers the setting, the config and filter, and the printed output. There is no automated test of the scoring script itself yet.

Open questions

  • The default weights are hand-tuned priors, not fitted to data. Chromium has its own on-device model for this (chrome/browser/navigation_predictor) using similar anchor signals; worth comparing against real navigation data before this leaves draft.
  • Is a setting the right surface, or should this start as a filter-only experiment?
  • wp_get_speculation_rules() is marked private in core, and it is the only way to read the main rule's conditions. Would a public accessor in core be better?

AI Use

Code and description both written with 🤖 Claude Code after I asked it to swap the language model for a decision engine. I will review and test.

@SandiyosDev

Copy link
Copy Markdown
Contributor

this is what google's up to? crazy

@github-actions github-actions Bot added the [Plugin] Speculative Loading Formerly "Speculation Rules" [slug: speculative-loading] label Nov 21, 2026
@SandiyosDev

Copy link
Copy Markdown
Contributor

🥲

@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.09091% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.09%. Comparing base (f6679c1) to head (4db13f6).

Files with missing lines Patch % Lines
plugins/speculation-rules/prediction.php 86.66% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1509      +/-   ##
==========================================
+ Coverage   70.84%   71.09%   +0.24%     
==========================================
  Files          91       92       +1     
  Lines        7967     8077     +110     
==========================================
+ Hits         5644     5742      +98     
- Misses       2323     2335      +12     
Flag Coverage Δ
multisite 71.09% <89.09%> (+0.24%) ⬆️
single 34.88% <13.63%> (-0.30%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Link prediction loads pages before any interaction, which raises server
load, so it needs to be an explicit opt-in next to the other settings.

Claude-Session: https://claude.ai/code/session_018vwYjS6a4aSvRzXnBSD5N7
The language model prototype needed a multi-gigabyte model download, was
limited to a few desktop browsers, took seconds to answer and returned
free text that had to be parsed. Scoring each link with a logistic model
over layout signals gives a probability per link in a few milliseconds
in any browser that supports speculation rules.

Predicted links are marked with an attribute that a document rule
selects, so the exclusions for speculative loading keep applying.

Claude-Session: https://claude.ai/code/session_018vwYjS6a4aSvRzXnBSD5N7
@adamsilverstein adamsilverstein added the [Type] Feature A new feature within an existing module label Nov 24, 2026
@adamsilverstein adamsilverstein changed the title Use on-device AI to predict links users are most likely to visit Add opt-in link prediction to Speculative Loading Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Speculative Loading Formerly "Speculation Rules" [slug: speculative-loading] [Type] Feature A new feature within an existing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants