Skip to content

Let the widget embed the notebook as a tab - #471

Merged
neuromechanist merged 6 commits into
developfrom
feature/issue-470-notebook-embed
Sep 24, 2026
Merged

neuromechanist merged 6 commits into
developfrom
feature/issue-470-notebook-embed

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Part of #470. The notebook site's side of showing the notebook as a tab inside the chat widget; the widget's own tabs, capsule and pop-out come in a separate PR. ADR 0012 records the decisions and amends 0011.

What changes

  • Who may embed it. frame-ancestors was 'none'. The build now writes it per environment (embed_origins): 'self', the platform's widget hosts (demo.osc.earth, osa-demo.pages.dev), then every notebook community's cors_origins (for NEMAR: nemar.org, www.nemar.org, test.nemar.org). The develop build adds *.osc.earth, *.osa-demo.pages.dev and loopback. A frame-ancestors source cannot take a partial-label wildcard, so a community origin written that way fails the build.
  • notebook/osa-bridge.js, added by the build to notebooks/index.html. It runs a starter's code cells tagged osa-autorun when the notebook opens and again after a kernel restart. Embedded, it reports ready and setup status to the widget and applies the widget's light or dark theme. It listens only to window.parent, whose origin frame-ancestors has already vetted, and posts no reader data.
  • Every build exposes the app (exposeAppInBrowser), because the bridge drives it. The --expose-app flag is gone, so e2e-check.js checks the same build a deployment ships.
  • Settings overrides: adaptive-theme on (a notebook opened on its own follows the device), and autosaveInterval 5 s instead of 120 s (so the widget's pop-out reopens the latest edits).
  • The NEMAR starter opens with the instructions block agreed on in the design review ("Before you start", %pip install scipy, File > Download), and its first code cell (install, import, Ready line) is tagged to run on open.
  • _headers also covers /osa/open, the path Cloudflare Pages serves open.html at, and the bridge script, with no-cache.

Measured before building

In Chrome against JupyterLite 0.8.4: notebook:run-cell settles when the cell finishes (unlike notebook:run-all-cells); apputils:change-theme only turns adaptive-theme off and returns when it is on, leaving the theme in place (also read in JupyterLab's own command), so the bridge turns that setting off first; settingsOverrides in the root jupyter-lite.json reach the notebooks app. Autosave was checked with a control: with the override, an unsaved edit reached storage within a second; without it, not within 20 seconds.

Tests

  • Build unit tests: the frame-ancestors fill, the no-cache paths, a template without the token refused, embed_origins per environment (loopback and previews develop-only, an inexpressible wildcard refused, the shipped NEMAR sites present), inject_bridge (once, before </head>, refusing a page without one or with the tag already), the settings overrides merged into existing ones, and the shipped starter's only auto-run cell being its first code cell.
  • e2e-check.js now serves the build's own _headers, and checks, in Chrome: the setup cell runs with no clicks; autosave, and the autosaved edit surviving a reopen; a kernel restart reruns setup; framed on an allowed cross-site origin, the notebook reports ready and setup done, and each light and dark message is applied inside the frame; framed on an origin not on the list, it never loads. Production checks that loopback is refused.
  • Mutation check of the new Chrome checks, seven mutations, each failing its own check: headers not sent (the refused origin loads), bridge not added (setup never runs), follow-the-device left on, no wait for it to turn off (both drop the first dark message), restarts ignored, autosave override dropped. The first version of the theme check sent light first and let a mutation survive; the reworked check, a first dark message on a light device, then caught a real race in the bridge: apputils:adaptive-theme does not return its settings write, so apputils:change-theme saw the setting still on and switched it back. The bridge now reads the setting back before changing the theme.
  • Timings, develop in Chrome 153: setup ran by itself 5.5 s after opening; cold run of every cell 8.5 s; embedded, ready and setup done 5.8 s.

Not in this PR

  • The widget's notebook tab, capsule redesign and pop-out tabs (Capsule: open the notebook as a tab in the widget #470, next PR).
  • Safari: the framed run covers Chrome's partitioned storage and third-party service worker; Safari is checked by hand on staging once the widget tab exists.

frame-ancestors was 'none'. The build now writes it per environment
from the platform's widget hosts and each notebook community's
cors_origins (develop adds previews and loopback), refusing an origin
frame-ancestors cannot express.

notebook/osa-bridge.js, added to the notebook page, runs a starter's
osa-autorun cells on open and after a kernel restart, and when
embedded reports ready and setup status to the widget and applies its
light or dark theme. Every build now exposes the app it drives, so the
--expose-app flag is gone. Settings overrides: follow the device theme
when opened alone, autosave every 5 s. NEMAR's starter gains the
agreed instructions and its setup cell runs on open. _headers covers
/osa/open, the path Pages serves open.html at.

Tested: build unit tests (47 offline); e2e-check.js in Chrome, next
commit.
e2e-check.js serves the build's own _headers and now checks that the
setup cell runs with no clicks, an unsaved edit autosaves, a kernel
restart reruns setup, the notebook works framed on an allowed
cross-site origin (a first dark message on a light device, then
light, then the device switching back must not override it), and an
origin not on the list is refused; production checks loopback is.

Tested: develop and production pass in Chrome 153. Seven mutations
(no headers sent, no bridge, follow-the-device left on, no wait for
it to turn off, restarts ignored, autosave override dropped) each
fail their check. The first theme check found a real race in the
bridge, fixed in the previous commit.
ADR 0012 amends 0011: the widget's sites may embed the notebook, the
bridge and its messages, why not a JupyterLab extension, and the two
settings overrides, with what was measured. The adopter guide
documents the osa-autorun tag, who may embed the notebook and the
frame-src embedders need; the notebook README the bridge and the
removed flag.

Tested: docs only.
From review of #471: a bridge that failed to start only logged to the
console, so the widget would wait for ready forever; it now posts
{type: 'error', phase: 'startup'}. The whole setup run is inside its
try, so every path posts a setup status. A setup cell counts as run
only if it gets an execution count, cleared first because a reopened
notebook shows its saved one. The theme reply is always sent, with
applied true or false, after up to 10 s. A new kernel reruns setup,
as a restart does.

Tested: develop e2e-check.js passes in Chrome, including the restart
rerun (prompt [2] to [1]) and every theme check.
From review of #471: X-Frame-Options SAMEORIGIN for a browser too old
to read frame-ancestors (a current one ignores it when both are sent,
measured: the allowed embed still loads in Chrome). The origin check is
tested with quote, semicolon, newline and path payloads, and the config
loader is shown to refuse them first. embed_origins raises clearly for
an environment without platform hosts, and a test keeps the two lists
in step. The refused-origin Chrome check now first proves its host page
loaded and pointed at the notebook, so silence means refusal.

Tested: 56 build tests; loosening the origin pattern fails 3; pointing
the refused host at a dead port fails the Chrome check; develop passes.
From review of #471: the adopter guide described the widget's notebook
tab as shipped, but the widget still opens a new tab until #470; it now
says which is which. The build docstring cited ADR 0011 for the bridge,
which is 0012's subject. ADR 0012 records the X-Frame-Options fallback,
the bridge's failure messages, and that the config loader refuses an
inexpressible origin before the build re-checks it.

Tested: docs and a docstring.
@neuromechanist

Copy link
Copy Markdown
Member Author

Review response (49a0b15, 6f8978a, 3b29ca6)

Four Sonnet reviewers (code and security, tests, silent failures, docs) reviewed 3ad30ae. Every finding is addressed below; the two not acted on say why.

Security (all five questions answered sound)

  • Listening only to window.parent is sound: frame-ancestors constrains every ancestor, and a top-level window (including a popup) has no parent, so the listener never engages.
  • targetOrigin: '*' is safe: no message carries reader data.
  • The exposed app is reachable only same-origin; an embedder can only post the theme message.
  • A cors_origins entry cannot inject into the header: the config loader's own anchored rule refuses it first, and embed_origins re-checks.
  • Autorun cells have no more power than a cell run by hand.
  • Suggestion: no X-Frame-Options fallback for a browser that does not read frame-ancestors. Added X-Frame-Options: SAMEORIGIN; a browser that reads frame-ancestors ignores it when both are sent, measured in Chrome (the allowed embed still loads).
  • Noted, intentional: the widget does not embed the notebook yet. This PR is the site half of Capsule: open the notebook as a tab in the widget #470; the widget PR follows.

Silent failures (49a0b15)

  • A bridge that fails to start only logged; the widget would wait for ready forever. It now posts {type: 'error', phase: 'startup'}. The widget PR will also time out on its own.
  • autorunIndices ran outside the try, so a failure there posted no setup status, and on the restart path became an unhandled rejection. The whole run is inside the try now.
  • "done" did not prove a cell ran. A setup cell's execution count is cleared first (a reopened notebook shows its saved one), and the run counts only if every tagged cell gets a new one; the run also refuses if another widget became current.
  • Theme failures were console-only. The theme reply is always sent, applied: true or false.
  • 5 s theme waits were tight for a slow phone: now 10 s.
  • Restart detection matched two status strings only: a new kernel (kernelChanged) now reruns setup too.
  • PLATFORM_EMBED_ORIGINS[environment] would raise a bare KeyError if the environment lists drifted: now a clear build error, and a test keeps the two lists equal.
  • Not acted on: a setup cell that catches its own exception and prints an error string is reported done. That is the starter author's choice, and the bridge cannot tell it from success.

Tests (6f8978a)

  • The refused-origin Chrome check could pass because its host page never loaded. It now first proves the host page loaded and pointed its frame at the notebook; pointing the refused host at a dead port fails the check (mutation run).
  • No test fed a quote, semicolon, newline or path through the origin check: parametrized, plus a test that the config loader refuses those shapes first. Loosening the pattern to ^https?://\S+$ fails 3 tests.
  • The >1 </head> branch of inject_bridge: tested.
  • Not acted on: a build-level offline test that embed_origins fires before the JupyterLite build. No origin can pass the config loader and fail embed_origins (their grammars match), so the test cannot be written honestly; the environment wiring in build() is proven by the develop Chrome run in this PR's notebook check.

Docs (3b29ca6)

  • The build docstring cited ADR 0011 for the bridge; it is 0012's.
  • docs/community-notebook.md described the widget tab as shipped; it now says the widget opens a new tab until Capsule: open the notebook as a tab in the widget #470.
  • ADR 0012 also records the X-Frame-Options fallback, the failure messages, and that the loader and the build both refuse an inexpressible origin.

Also found and fixed before review (see the description): the first theme check let a mutation survive; the reworked check found a real race in the bridge, which now reads adaptive-theme back before changing the theme.

Verification: develop e2e-check.js in Chrome 153 passes after every fix (setup by itself 5.8 s, embedded ready and set up 5.2 s); production passes, refusing loopback. 56 offline build tests pass.

@neuromechanist
neuromechanist merged commit 2e805cc into develop Sep 24, 2026
16 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-470-notebook-embed branch September 24, 2026 09:53
neuromechanist added a commit that referenced this pull request Sep 24, 2026
Release review: add the widget's Notebook tab (#471, #474), color_scheme auto (#472), the first-paint fix (#476) and the dark-page field colors; the capsule and notebook-site bullets described the notebook opening in a new tab, which #474 replaced.

Tested: docs-only; each claim checked against origin/develop (config.yaml color_scheme, osa-widget-config- key, ADR 0012 path).
neuromechanist added a commit that referenced this pull request Sep 24, 2026
* Title the changelog's release as 0.8.14

Tested: docs only.

* Complete the 0.8.14 changelog

Release review: add the widget's Notebook tab (#471, #474), color_scheme auto (#472), the first-paint fix (#476) and the dark-page field colors; the capsule and notebook-site bullets described the notebook opening in a new tab, which #474 replaced.

Tested: docs-only; each claim checked against origin/develop (config.yaml color_scheme, osa-widget-config- key, ADR 0012 path).
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.

1 participant