Skip to content

Ml driver poc#254

Closed
ti3x wants to merge 2 commits into
mozilla-firefox:mainfrom
ti3x:ml_driver_poc
Closed

Ml driver poc#254
ti3x wants to merge 2 commits into
mozilla-firefox:mainfrom
ti3x:ml_driver_poc

Conversation

@ti3x
Copy link
Copy Markdown
Contributor

@ti3x ti3x commented May 14, 2026

No description provided.

ti3x and others added 2 commits May 8, 2026 16:03
Lands the eval-pipeline ml_driver runner inside Firefox so eval suites can
invoke ML APIs through an in-tree, CI-tested entry point instead of
re-injecting JavaScript into the browser via executeAsyncScript every run.

Source mapping (eval-pipeline ml_driver/src/ml_driver/firefox/scripts -> MC):
  runner.js               -> src/firefox_inference/runner.js
                             (12-line executeAsyncScript shim) +
                             src/firefox_inference/Runner.sys.mjs
                             (handleMessage / run / serializeError)
  imports.js              -> folded into per-module
                             ChromeUtils.defineESModuleGetters
  ml_engine.js            -> src/firefox_inference/MlEngineRunner.sys.mjs
                             (createMlEngine, runMlEngine, destroyMlEngine,
                              getExistingEngine, plus serializeEntry and
                              serializeInferenceResponse - referenced but
                              never defined upstream)
  translations.js         -> src/firefox_inference/TranslationsRunner.sys.mjs
                             (createTranslationsSession,
                              runTranslationsSession,
                              destroyTranslationsSession,
                              normalizeLanguagePair, createDeferred)
  page_extractor.js       -> src/firefox_inference/PageExtractorRunner.sys.mjs
                             (getPageText, getPageInfo, getReaderModeContent,
                              getSelectionText, runHeadlessExtractor)

Module wiring (already committed in the parent change):
  - src/firefox_inference/moz.build registers the four .sys.mjs modules
    under EXTRA_JS_MODULES.firefox_inference, exposing them as
    resource://gre/modules/firefox_inference/<Name>.sys.mjs.
  - toolkit/components/ml/jar.mn ships runner.js at
    chrome://global/content/ml/firefox_inference/runner.js.
  - toolkit/components/ml/moz.build adds src/firefox_inference to DIRS and
    registers the new browser.toml under BROWSER_CHROME_MANIFESTS.

New mochitest suite (tests/browser/browser_ml_driver/):
  - browser.toml: pulls parent head.js + shared-head.js as support files.
  - head.js: loadSubScript the parent ML browser-test head so setup(),
    EngineProcess, remoteClients, etc. are available.
  - browser_ml_driver.js: 6 add_task blocks (19 subtests) that all route
    through `run(done, args)` via a `dispatch(...)` Promise wrapper.
    Covers happy-path lifecycle (create -> run -> destroy moz-echo),
    unknown-message error envelope, unknown/blank engineId error envelope,
    and translations missing-source/target validation. Internal helpers
    (serializeEntry/serializeError/normalizeLanguagePair/...) are verified
    implicitly through the observable envelope contract.

Result: ./mach test toolkit/components/ml/tests/browser/browser_ml_driver
prints `Passed: 19 / Failed: 0 / Unexpected results: 0`.

TODO / deferred (not in this change):
  - chat.js, memories.js, search_browsing_history.js from ml_driver are
    NOT ported. They depend on moz-src:///browser/components/aiwindow/
    (Smart Window), which is browser/, not toolkit/, and is still
    actively changing. Defer until aiwindow stabilizes or a parallel
    runner shell lands under browser/.
  - PageExtractorRunner is wired but its actor (PageExtractorParent) is
    not in upstream toolkit yet; lazy import means the module loads,
    invocation throws clearly until the actor lands.
  - Per-handler happy-path mochitests for translations sessions and
    PageExtractor handlers; engine lifecycle is the only end-to-end
    success path covered today.
  - eval-pipeline FirefoxDriver (ml_driver/src/ml_driver/firefox/driver.py)
    still bundles its own runner.js via script_utils.get_runner_script()
    and ships it over Marionette per session. Switching it to load
    chrome://global/content/ml/firefox_inference/runner.js directly is a
    follow-up patch in eval-pipeline, gated on this landing upstream.

Differential Revision: https://phabricator.services.mozilla.com/D299465
Continues d051a08 (toolkit port) by landing the six handlers that
were deferred because they depend on browser/components/aiwindow/ —
Smart Window code, which sits in browser/ and cannot be reached from
toolkit. A parallel runner shell under aiwindow/scripts/ now owns these
handlers and falls through to the toolkit Runner.sys.mjs for the other
twelve, so ml_driver gets one chrome:// URL that responds to all
~18 message names with an identical wire envelope.

Files:
- scripts/runner.js — 19-line executeAsyncScript shim, mirror of the
  toolkit shim.
- scripts/AiwindowRunner.sys.mjs — dispatcher. 8-case switch over the
  smart-window handlers; default branch forwards to toolkit
  Runner.handleMessage. Exports run + serializeError matching the
  toolkit envelope contract bit-for-bit.
- scripts/ChatRunner.sys.mjs — setRealTimeInfo, initConversation,
  submitTurn (5-round tool loop, DDG search fallback, runWithGenerator
  streaming). Lazy imports of ChatConversation, Utils, Tools,
  PageExtractorParent.
- scripts/MemoriesRunner.sys.mjs — setMemories, getAllMemories,
  getRelevantMemories, clearMemories. Normalization defaults preserved
  (Hobbies & Leisure, Research / Learn, score 5).
- scripts/SearchBrowsingHistoryRunner.sys.mjs — thin wrapper over the
  aiwindow searchBrowsingHistory model export.
- scripts/moz.build, scripts/jar.mn — register MOZ_SRC_FILES, the
  chrome://browser/content/aiwindow/scripts/runner.js URL, and the
  mochitest manifest.
- aiwindow/moz.build — one-line addition of "scripts" to DIRS, the only
  edit outside the new directory.

Behavior change worth flagging: chat/conversation state moves from the
chrome window (chromeWindow.__realTimeInfo, __conversationState) to
module-level singletons. Per-process scope instead of per-window;
ml_driver drives one window per process so eval-pipeline sees no
difference, but the contract is documented.

Tests: scripts/tests/browser/browser_aiwindow_runner.js covers the
dispatcher envelope shape for set_real_time_info, init_conversation,
the memories CRUD roundtrip, search_browsing_history, the toolkit
fallthrough error envelope (unknown handler), and submit_turn input
validation. Heavier submit_turn end-to-end with the aiwindow mock
OpenAI fixture is a follow-up.

Out of scope: ml_driver eval-pipeline migration to load this URL
directly (gated on this landing, same staging pattern as
d051a08); toolkit Runner.sys.mjs registry refactor (rejected as
inverted layering — toolkit shouldn't acquire an extension point for a
single browser-layer consumer).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@firefoxci-taskcluster
Copy link
Copy Markdown

No Taskcluster jobs started for this pull request

The allowPullRequests configuration for this repository (in .taskcluster.yml on the default branch) does not allow starting tasks for this pull request.

@github-actions
Copy link
Copy Markdown
Contributor

(Automated Close) Please do not file pull requests here, see https://firefox-source-docs.mozilla.org/contributing/how_to_submit_a_patch.html

@github-actions github-actions Bot closed this May 14, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators May 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant