Description
Phase 5 of #324.
Base: epic/324-pyodide-browser.
Depends on: phase 4 for the artifact, phase 2 for the Pyodide harness.
Parallel with: phase 6.
Why
onnxruntime has no WebAssembly build, so the browser path has to go through ONNX Runtime Web, which is JavaScript and asynchronous.
That asynchrony is the real design problem in this phase, and it is why it is separated from phase 4.
Everything in eegprep that reaches ICLabel today is synchronous.
The actual design question
Under Pyodide, calling into ONNX Runtime Web returns a JavaScript promise that must be awaited.
iclabel() is synchronous, pop_iclabel() is synchronous, and both are reachable from the GUI menus and from eegprep-console.
So this phase has to decide and document how the await-able entry point reaches users without forking the API:
- what the async entry point is called and where it lives,
- whether the synchronous
pop_iclabelraises a clear error under Emscripten rather than silently blocking, - how the console and the GUI menu action reach the async path, given the
EEGPrepSessioncontract inAGENTS.mdthat GUI and console must stay synchronized, - whether
return_com=Truehistory still produces a command a user can replay.
Present the options on the issue before implementing.
Do not invent a parallel async copy of the whole pop_* surface; AGENTS.md rules out parallel implementations of existing helpers.
Grounding, verified against the current tree
pop_iclabel at src/eegprep/plugins/ICLabel/pop_iclabel.py:16 already supports return_com and a GUI path through inputgui, and it recurses over lists of datasets at :38.
Any async seam has to preserve all three behaviors.
iclabel.py is the single place the network is actually invoked, which is the natural seam.
Scope
- ONNX Runtime Web backend selected when
sys.platform == "emscripten", resolved once, consistent with phase 3's helper pattern. - An
await-able entry point, designed per the decision above. - Clear, early failure if the synchronous path is called under Emscripten, naming the async entry point.
- Parity test under the phase 2 Pyodide harness: browser backend against the native onnxruntime backend on the same input.
Non-goals
No quantization; phase 6.
No server-side execution, per the epic.
No async variants of pop_* functions other than what ICLabel strictly needs.
Acceptance criteria
- Design options for the async entry point posted on this issue and resolved before implementation.
- Backend resolved once from the platform, not per call, and not via an environment variable.
- ONNX Runtime Web path classifies correctly under the phase 2 Pyodide harness.
- Browser and native backends agree on the same input to a stated, justified tolerance.
- Synchronous entry under Emscripten fails immediately with a message naming the async entry point.
-
return_com=Truestill yields a replayable history command. - List-of-datasets recursion at
pop_iclabel.py:38still works on the async path. - GUI plus
eegprep-consolesynchronization preserved; add or updatetests/test_console_workspace.pycoverage, perAGENTS.md. - CI runs the browser ICLabel parity test under Pyodide.
-
docs/source/workflow docs explain the browser path and how a user invokes it from both the GUI and the console. -
src/eegprep/resources/help/pop_iclabel.mdupdated if user-facing behavior changed. - Pre-commit, ruff, ruff format, ty clean.
/goal prompt
/goal Make ICLabel run in the browser through ONNX Runtime Web, with an await-able entry point that does not fork eegprep's public API.
Source of truth: src/eegprep/plugins/ICLabel/iclabel.py and pop_iclabel.py, phase 4's ONNX artifact, phase 2's Pyodide harness, and the EEGPrepSession GUI-plus-console contract in AGENTS.md. Read AGENTS.md first.
Constraints: no server-side execution; no parallel async copy of the pop_* surface; resolve the backend once from the platform rather than from an environment variable; preserve return_com, the GUI path, and list-of-datasets recursion.
Start by posting the async entry point design options on the issue and getting an answer. Do not implement before that is resolved.
Finish line: the browser backend classifies under the Pyodide harness and agrees with native onnxruntime at a justified tolerance; the synchronous path fails fast under Emscripten with a message naming the async entry point; console and GUI stay synchronized with tests/test_console_workspace.py coverage; CI runs the browser parity test; workflow docs and pop_iclabel.md updated.
Report any review finding you chose not to fix, with the reason.
Activity
neuromechanist commentedon Sep 17, 2026
🤖 Phase 5 async-seam design gate — proposal for approval before implementation
I inspected the current ICLabel, GUI, console, session, Pyodide, test, and help contracts on
feature/issue-378-phase5-iclabel-webat9057bd0e. The load-bearing facts are:iclabel()prepares the four-way augmented feature batch, transposes to the three NCHW inputs, makes one synchronous network call, then performs the existing output reshape/mean and attaches the seven-class result (src/eegprep/plugins/ICLabel/iclabel.py:58-98). That is the natural inference seam.pop_iclabel()owns the GUI dialog, version normalization, list-of-datasets recursion, ICA validation, andreturn_comhistory command (src/eegprep/plugins/ICLabel/pop_iclabel.py:15-44, :47-69, :72-79).return_com=True, calls the function, and passes the result to the session bridge (src/eegprep/functions/adminfunc/console.py:197-259). The bridge stores EEG/history once and suppresses duplicate post-cell history (:383-397, :462-520).gui_actionaround a synchronous dispatch (src/eegprep/functions/guifunc/menu_actions.py:278-287);pop_iclabelis a normal multi-dataset pop action (:164-175, :233-255, :1082-1085, :1147-1150). The existing long-task helper runs a callable in a Qt worker thread (src/eegprep/functions/guifunc/long_task.py:35-45, :57-74), so it should not be assumed to be the ONNX Runtime Web Promise scheduler.EEGPrepSessionis the source of truth for GUI and console state;CURRENTSETis an ordered 1-based selection, andstore_current(..., index=...)supports committing a result back to explicit one-based slots (src/eegprep/functions/guifunc/session.py:129-140, :218-220, :222-260). The current docs require one shared session, one history append, and replayable commands (docs/source/user_guide/gui_console_session.rst:95-124).emfs:and runs the target script withawait pyodide.runPythonAsync(...)(tools/pyodide/run_pyodide.mjs:123-141, :148-166). It does not yet provide an ONNX Runtime Web bridge. Existing ICLabel parity uses measuredrtol=1e-4, atol=1e-5(tests/test_iclabel.py:113-129).Option A — paired pure async APIs (recommended)
Public entry points, in the existing modules:
async def iclabel_async(EEG, algorithm="default", engine=None)insrc/eegprep/plugins/ICLabel/iclabel.py.async def pop_iclabel_async(EEG, icversion=None, *, gui=None, renderer=None, engine=None, return_com=False)insrc/eegprep/plugins/ICLabel/pop_iclabel.py.eegpreplazy API. Keep the existing native synchronous APIs unchanged except for the Emscripten guard below.Synchronous Emscripten contract:
Put the guard at the first executable line of both synchronous functions, before GUI display,
EEG is Nonehandling, deepcopy, ICA validation, or engine dispatch. This is intentionally an immediate, deterministic failure; MATLAB/Octave engine arguments do not bypass a browser execution boundary. The owner still needs to approve whether theEEG is Noneno-op should be the one exception, but the acceptance wording favors the strict guard.Behavior and session flow:
pop_iclabel_asynckeeps the current dialog/version behavior and awaitsiclabel_async; for a list it awaits each dataset sequentially, preserving input order and onereturn_comcommand.EEG = await pop_iclabel_async(EEG, 'default');. The native sync wrapper continues to emit its currentEEG = pop_iclabel(EEG, 'default');. This means history is entry-point/context specific rather than one byte-for-byte command across native and browser runtimes.EEGPrepSessioninto it. The browser GUI dispatcher capturessession.selected_dataset_indices()before the await, begins the GUI action, schedules the coroutine on the browser/GUI event loop, and ends the action only in success/error/finally. On success it calls the existing session storage path with the captured explicitindexlist, thenecho_command,store_current,add_history,notify_changed, and refresh. No session mutation or history append occurs before inference completes. Native GUI dispatch remains synchronous.pop_iclabel_async(not an async fork of everypop_*). It awaits the function, feeds the resolved(EEG, command)to the existingaccept_pop_resultpath, and returns the same compact unpackable console result. Thus bareawait pop_iclabel_async(EEG)auto-stores once, preserves scalar/listCURRENTSET, updatesEEG/ALLEEG/LASTCOM/ALLCOM, and the existing post-cell hook does not append history twice. Because the emitted command containsawait,execute_history_command()must gain an async-capable replay branch; its current rawexecimplementation is synchronous (src/eegprep/functions/adminfunc/console.py:588-592). IPython top-level await can be the console-facing execution boundary.Backend and Promise boundary:
sys.platform == "emscripten": native selects the existing ONNX Runtime backend; Emscripten selects a browser adapter. Do not inspect an environment variable or reselect on every call. The browser adapter owns one cached model/session promise and exposes an awaitable run; it must await both ONNX Runtime Web session creation and inference, rather than trying to make either Promise synchronous.iclabel.onnxartifact as the model source and must not import nativeonnxruntimeon Emscripten. The exact JS bridge registration and model URL-versus-bytes transport remain open.InferenceSession.createandInferenceSession.run: https://onnxruntime.ai/docs/api/js/interfaces/InferenceSessionFactory.html and https://onnxruntime.ai/docs/api/js/interfaces/InferenceSession.html. Pyodide Promise interop must be exercised from an async Python/JS stack: https://pyodide.org/en/stable/usage/api/python-api/ffi.html.Test plan:
iclabel_asyncandpop_iclabel_async; exact Emscripten fast-fail tests for both sync APIs; GUI dialog/list/return-com tests; async console tests covering one shared session, ordered list selection, one history append, and failed inference with no mutation.onnxruntime-webbridge in the Node/browser harness, compare the same input/model against native, and report measured output differences using the existing tolerance rather than assuming parity. Add docs andsrc/eegprep/resources/help/pop_iclabel.mdexamples for the async command.Tradeoffs: this adds two narrowly scoped public names and one async-aware console/replay path, but preserves the current pure
pop_*/session boundary and minimizes GUI/console drift. It has the clearest test seam for the existing one-network-call implementation.Option B — only expose
iclabel_async; adapters own pop orchestrationPublic entry point:
async def iclabel_async(...)insrc/eegprep/plugins/ICLabel/iclabel.py; no publicpop_iclabel_async. Both synchronous functions use this exact contract:The GUI and console each call a shared private async coordinator in
pop_iclabel.py, but the dialog, ICA check, list recursion, async history string, explicitCURRENTSETslot commit, andreturn_comhandling are assembled outside the public pop wrapper. The console needs a special awaitable adapter; the GUI needs a main-loop completion callback and a long-livedbegin_gui_action/end_gui_actionpair. Backend selection and Promise handling are otherwise the same one-timesys.platform == "emscripten"design as Option A. The harness tests the private coordinator through GUI/console integration and testsiclabel_asyncdirectly at the Promise boundary.Tradeoff: only one new public symbol, but it moves the user-facing pop contract into two callers and makes it easier for GUI, console, and normal scripts to disagree about list order,
return_com, or history replay. This is the smallest API but the highest drift risk.Option C — session-aware async pop coordinator
Public entry point:
async def pop_iclabel_async(EEG, icversion=None, *, session: EEGPrepSession, gui=None, renderer=None, engine=None, return_com=False)insrc/eegprep/plugins/ICLabel/pop_iclabel.py. The low-level async model runner remains private iniclabel.py. The existing sync functions fail immediately with:The coordinator captures
session.CURRENTSET, awaits sequential list inference, and commits throughsession.store_current(..., index=...)plus one history/notification transaction. GUI and console both pass their one shared session, soCURRENTSET, list recursion, and failure atomicity are centralized. The async command remainsEEG = await pop_iclabel_async(EEG, 'default', session=session);; the console must avoid applying the returned EEG a second time.Backend resolution is still a one-time
sys.platform == "emscripten"choice and the harness tests the session coordinator plus the real ORT Web Promise bridge. The tradeoff is architectural coupling: an EEGLAB-style processing wrapper currently returns a dataset and command without knowing the GUI/session, while this option imports and mutates the interactive session from the plugin layer and requires a special console path. It is attractive only if atomic session commit is more important than keeping processing and interactive state separated.Recommendation and decisions needed
Recommend Option A. It follows the current separation:
iclabel.pyowns the single inference seam,pop_iclabel.pyowns GUI/list/history semantics,EEGPrepSessionremains the single state boundary, and the console bridge remains the single auto-store boundary. It also lets native callers keep today's synchronous API while browser callers use an explicitly awaitable API.Before implementation, please choose/approve:
RuntimeErrormessage.return_comcommands containingawait, with an async history-replay branch, versus a different platform-aware replay design.@arnodelorme please choose the option and approve the contract before Phase 5 implementation begins.
neuromechanist commentedon Sep 17, 2026
🤖 Phase 5 is implemented in PR #384. It adds
iclabel_async/pop_iclabel_async, fail-fast synchronous Emscripten entry points, the ONNX Runtime Web bridge, async GUI/console synchronization with stale-selection protection, and CI parity coverage. Local native-vs-Pyodide float32 parity isallclose: truefor shape [32, 7] with max absolute difference 4.3213e-06 at rtol=1e-4/atol=1e-5. The corrected CI run has browser parity green and is finishing the existing one-thread ICA/platform jobs.