Skip to content

feat: import a chat notebook as a named tab and notebook tabs (E4.2, E4.4) - #690

Merged
neuromechanist merged 13 commits into
developfrom
feature/issue-673-notebook-import-tabs
Nov 11, 2026
Merged

neuromechanist merged 13 commits into
developfrom
feature/issue-673-notebook-import-tabs

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

The widget can hand a chat's notebook to the notebook frame, which writes it as <community>/<dataset> · <title>.ipynb, opens it, and offers Run all and Undo; and the Notebook surface has a strip of named notebooks (the dataset's playground, imported ones, "+" for a blank one) with rename, close and reopen. Board B1 (.context/designs/nemar-ai-ux/Notebook.dc.html). The starter notebook is never changed. The design's Download menu and the chat's "Continue in notebook" card (E4.3) are out of scope; importNotebookToTab is what E4.3 will call.

Two commits:

  1. feat: import a chat's notebook into the notebook tab (#673): the pure module notebook/osa-notebook-files.js (naming, import validation, message validation), the bridge handlers import-notebook, run-all, undo-import and the result and notebooks answers, and the widget's request machinery, importNotebookToTab and banner.
  2. feat: named notebook tabs in the notebook surface (#676): the strip (tablist, arrow keys, inline rename, close, "+" menu), the bridge's new-notebook, open-notebook, rename-notebook and close-notebook, the per-dataset open list in the page's localStorage, the protocol in notebook/README.md, ADR 0017 and the changelog line.

Why the bridge: the frame is cross-origin, so only notebook/osa-bridge.js can write JupyterLite's storage (serviceManager.contents) and open a document. The page trusts nothing in a message: a path is acted on only if it is in the page's own listing under <community>/ for the dataset; an import is checked (dataset against starters/index.json via datasetProblem, nbformat 4, markdown and code cells only, at most 200 cells and 2 MB), the osa-autorun tag is stripped, and a name that exists gets " 2", " 3". Run all runs the cells one at a time (run-all-cells never settles, ADR 0012).

Two JupyterLite facts that shaped it, both measured and recorded in ADR 0017: the notebooks page is a single-document shell (docmanager:open goes to a browser tab unless called with options: {ref: '_noref'}, and NotebookShell.add refuses a second widget), so a tab switch closes the panel on screen and opens the next in its place; and the kernel of a closed panel keeps running and is reconnected on reopen, so Python state survives a switch.

Closes #673
Closes #676

Tests

  • bun notebook/test-notebook-files.js && bun notebook/test-open.js && bun notebook/test-bridge.js: 211, 52 and 14 checks pass.
  • bun frontend/test-widget-notebook-import.js && bun frontend/test-widget-notebook-strip.js && bun frontend/test-widget-notebook-tab.js && bun frontend/test-notebook-builder.js: 95, 121, 168 and 130 checks pass.
  • bun notebook/e2e-check.js --environment develop: all checks pass in real Chrome on a develop build, including the framed import from the real builder (file exists, is open, no autorun tag, the starter's last_modified unchanged), Run all on passing and failing notebooks, Undo, create, rename, close, a reload with the open list restored and a closed notebook reopened, a second import as a new tab, Python kept across a tab switch, and a foreign target ignored.
  • uv run pytest tests/test_scripts/test_build_notebook_site.py tests/test_frontend -q: 205 passed.
  • node --check frontend/osa-chat-widget.js
  • uv run ruff check . && uv run ruff format --check .: clean.
  • CI wiring: Run notebook files tests, Run the notebook import tests on a real DOM and Run the notebook strip tests on a real DOM in tests.yml; notebook-site-check.yml also triggers on the builder and its fixtures, with a 20-minute timeout since the develop run is longer.

The widget's importNotebookToTab hands a notebook to the notebook frame; the
bridge (the only code that can write the cross-origin storage) validates it,
writes <community>/<dataset> . <title>.ipynb without overwriting, opens it,
and answers; the widget shows a banner with Run all (cell by cell) and Undo
(until the reader edits it). The starter is never changed.

- notebook/osa-notebook-files.js: pure naming, import validation (dataset
  pattern, nbformat 4, markdown and code only, 200 cells, 2 MB, autorun tag
  stripped) and message validation; built next to the bridge.
- Bridge: import-notebook, run-all, undo-import, result and notebooks.
- open.js runs main() only on a page with #app, so it can be imported.

Tested: bun notebook/test-notebook-files.js, test-widget-notebook-import.js,
the existing notebook tab and build tests, and notebook/e2e-check.js against
a develop build in Chrome.
A strip under the surface tabs lists the dataset's notebooks (the playground,
imports, blank ones) from the notebook page's notebooks messages: role
tablist/tab with aria-selected and arrow keys, inline rename (Enter commits,
Escape cancels; the file keeps its dataset prefix), close, and a + menu for a
blank notebook and the closed ones to reopen. The bridge handles new-notebook,
open-notebook, rename-notebook and close-notebook, keeps the open list in its
own localStorage per community and dataset, and restores it after a reload;
a closed notebook stays a file.

Also the protocol in notebook/README.md, ADR 0017 and the changelog line.

Tested: bun frontend/test-widget-notebook-strip.js, the import and tab suites,
and notebook/e2e-check.js against a develop build in Chrome (create, rename,
close, reload and reopen, a second import as a new tab, a foreign message).
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://feature-issue-673-notebook-i-demo.osc.earth
Branch feature/issue-673-notebook-import-tabs
Commit f2f060a

This preview will be updated automatically when you push new commits.

…lapsing whitespace (tested: test-notebook-files.js)
… and ignore unknown messages (tested: test-notebook-files.js)
…cannot hold the tab queue (not tested: needs the e2e browser run)
…t on strip redraw; strip is a labelled group, not a tablist; tab label contrast; stats stays out of import-notebook (tested: widget notebook import and strip suites, other widget suites)
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 1: outcome for each finding. Fix commits are on this branch (da14bce, 40e1927, b5e713b, 1ded192, 8d6a932).

  • handle() check outside try/catch; unknown messages answered when tabs are down (important): fixed. The listNotebooks() and validateTabMessage step is wrapped, and a request that is one gets result {ok:false, error}. New tabRequestId() in osa-notebook-files.js is the one rule (target, known type, well-formed id) shared by validateTabMessage and the tabs-unavailable branch, so ignored messages stay unanswered. Only when osa-notebook-files.js itself failed to load is the id pattern checked locally, since the type list is not available. Tested: test-notebook-files.js (tabRequestId cases).
  • Undo offered while Run all runs; unguarded result callbacks (important): fixed. No Undo button while running, undoImportedNotebook refuses in that state, and the import, run and undo callbacks apply only from the state that started them (importing, running, undoing). The refused-undo branch now restores the prior state. Tested: test-widget-notebook-import.js (no Undo while running, and a late failed run-all result after removal leaves the banner removed).
  • Focus lost on strip rebuild (important): fixed. Focus is recorded before the strip is cleared and given back to the same tab, Rename or Close (or the open tab if its control is gone, including after a rename commit); a menu choice returns focus to the "+" button; the menu also closes on focusout to a target outside it and on window blur (a click in the cross-origin frame). A null relatedTarget is deliberately not treated as leaving, because some browsers do not focus a button on click. Tested: test-widget-notebook-strip.js (focus across list updates for tab, Rename, Close, a removed tab, rename Enter; menu focus and close). Confirmed the new focus tests fail with the restore disabled.
  • Tab label contrast (suggestion): fixed. Labels use --osa-text; the selected tab stays distinguished by weight and background. Tested: a computed AA assertion in the strip suite for light and dark.
  • tablist holding non-tab controls (suggestion): fixed by dropping the tablist pattern. The strip is role="group" named "Notebooks", tabs are plain buttons, the open notebook has aria-current="true"; arrow keys and the roving tab stop are kept. There is no tab panel, so the tab pattern did not fit. Tests updated.
  • importedName order, zero-width characters (suggestion): fixed. Whitespace becomes spaces, unsafe characters (now including U+200B to U+200F and U+2060) are removed, whitespace is collapsed again, then trimmed, so a / b is a b and cleaning is idempotent. Tested: test-notebook-files.js (a / b, ERPs - a : b, zero-width, idempotence over several titles).
  • Unbounded wait for setup in show() (suggestion): fixed. The wait is bounded at 120 s (shorter than the widget's 180 s) and then answers ok: false; an import whose file is already written says it was saved but not opened. ADR and README say so. Not covered by a unit test (needs the e2e browser run against a hung setup); node --check only.
  • "Starter is never changed" claim (suggestion): fixed by rewording in the ADR, CHANGELOG and README: the starter's source is never changed, but it may be saved with the outputs its setup produced. I did not add the optional e2e step.
  • stats posted to the frame (suggestion): fixed. stats stays in the widget's banner record and is no longer in the import-notebook message, the ADR table, the README table or the validateImport docstring. Tested: the import suite checks the posted message has only the fields the page reads.
  • NOTEBOOK_KINDS and message types duplicated; void lines (suggestion): fixed. The strip suite imports NOTEBOOK_KINDS and TAB_REQUESTS from osa-notebook-files.js, asserts the widget draws every kind and rejects an unknown one, that every message type the widget posted in the run is one the page validates, and that the widget source knows each request type. The void statements and the unused destructured names are removed.
  • Frontend Tests failure at "Check the paced reveal in headless Chrome" (suggestion): the failing check is the reader saw the reply grow through 3 different lengths (all other checks in that step passed), a timing assertion in code this PR does not touch, filed as Flaky: paced-reveal-check.mjs fails 'the reader saw the reply grow through 3 different lengths' on the CI runner #691. The push of these fixes re-runs every job; the final head's checks (Frontend Tests, Test 3.11 and 3.12, Build and open the notebook site) still have to be green before merging.

Local runs on the new head: bun on notebook/test-notebook-files.js (241 passed), all frontend/test-widget-*.js, frontend/test-notebook-builder.js and notebook/test-open.js (all pass), and uv run pytest tests/test_frontend (144 passed). notebook/e2e-check.js was not run locally.

…imer; an import saved but not shown keeps its path, Run all and Undo (tested: widget notebook import suite)
…vives a redraw and a window blur (tested: widget notebook strip suite)
…undo-import is refused while the cells run; setup wait bound is 150 s (tested: notebook e2e check, develop environment)
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 2 outcomes (fix commits on this branch: bb1e9f4, 8199303, 73d8acd).

  • Run all shares the 180 s timer (important): fixed in bb1e9f4. Run all now has its own 30 minute wait (NOTEBOOK_RUN_ALL_MS); other requests wait 240 s (was 180 s, so it covers the page's setup bound plus the 30 s open). The banner stays in running until the page answers, and the frame-replaced path still settles it. Widget test added: a run-all that outlives the ordinary timer stays running and shows the real result when it arrives; the ordinary timer still settles an unanswered Undo. Tested: widget notebook import suite.
  • Menu focus (suggestion): fixed in 8199303. renderNotebookTabs records the focused menu entry and refocuses the entry at that index after the rebuild. A window blur with focus inside the menu now closes it with focus restored to "+"; a blur caused by a click into the frame (focus not in the menu) does not take focus back. Strip-suite cases added for a redraw (same index, and a shorter menu) and for blur. Tested: widget notebook strip suite.
  • Roving tab stop with no hint (suggestion): fixed in 8199303 by taking the first option: every tab is a normal tab stop and the arrow keys stay as an extra. tabIndex assertions updated. Tested: widget notebook strip suite.
  • new-notebook and slow setup; widget drops the saved path (suggestion): fixed. In 73d8acd newNotebook has the same try/catch as import and answers ok:false with the path ("saved but not opened"); the setup wait bound is 150 s, above the 120 s a healthy cold setup is allowed in the e2e check, and below the widget's 240 s wait. In bb1e9f4 the widget treats an ok:false answer that carries a path as imported but not shown: it keeps the path and shows the message beside Run all and Undo (an answer with no path is still an error). Tests: widget import suite. Not added: a test for the setup-wait timeout path itself, which needs a hung setup in a real JupyterLite page (rejected as not reproducible in the e2e check without a test-only hook); the longer bound is the mitigation.
  • Overstated undo protection (suggestion): fixed in 73d8acd. undoImport now refuses with 'the cells are running' while runningAll is that path. New e2e case (develop environment): undo-import during a slow run-all is refused and the file stays, the run finishes ok, and Undo works afterwards. The widget test comment now says the widget must not trust the page's refusal. Tested: bun notebook/e2e-check.js --environment develop passes.

All widget and notebook JS suites pass; uv run ruff check . and ruff format --check . clean.

@neuromechanist

Copy link
Copy Markdown
Member Author

PM verdict: ready to merge. All three test files exist and are wired in tests.yml (bun notebook tests after the bootstrap tests, both widget suites after the notebook tab suite); e2e-check covers import, undo, create/rename/close, reload, second import, foreign target; both review rounds have outcomes; all checks pass on head 73d8acd and the branch is current with develop. Merging.

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