Skip to content

feat: build a notebook and a percent-format script from a chat's successful runs (E4.1) - #684

Merged
neuromechanist merged 13 commits into
developfrom
feature/issue-667-notebook-builder
Nov 11, 2026
Merged

neuromechanist merged 13 commits into
developfrom
feature/issue-667-notebook-builder

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Adds buildConversationNotebook (frontend/osa-notebook-builder.js): a pure builder that turns a conversation's stored runs into an nbformat 4.5 notebook and a percent-format script with the same cells, plus stats: {steps, leftOut}. It is exposed as OSARuntime.buildConversationNotebook and shipped in frontend/osa-runtime.bundle.js. No UI, bridge message or entry point: those are E4.2 to E4.4. The widget changes only in its generated RUNTIME_BUNDLE_INTEGRITY line.

Cells, in order: title and origin line; one setup cell (%pip install, %matplotlib inline, imports hoisted from the steps once each in first-seen order, import eegprep_lean, read_index, with index_url when zarrBase is given); a ## Step N · title heading and a code cell per successful run (title from stepTitle, then description, then just Step N; steps the reader ran carry "(run by the reader, not the assistant)"); a closing "Left out" note when a run was not successful, with each run's position, title, status and, for errors, the first line of stderr. Cells carry no outputs: stored runs lose their figures, and Run all regenerates them (stated in the module header). Ids are deterministic (title, setup, step-N-title, step-N-code, left-out).

Fixtures: frontend/test-support/notebook-conversations.js. The recorded RUN_CARD_FIXTURES runs are real Pyodide runs. The ERP conversation shaped like board B1 is hand-authored: I could not export a real chat (that needs a browser session on develop-demo.osc.earth). Its code compiles (checked by the Python test) but was not run.

Closes #667

Tests

  • bun frontend/test-notebook-builder.js: 106 passed
  • uv run pytest tests/test_frontend -q --no-cov: 142 passed (includes the new test_notebook_builder.py: nbformat 4.5 validation, every code cell compiles with top-level await after magics are stripped, jupytext library and jupytext --to notebook CLI restore the same cell types and sources, a # %% line does not split a cell)
  • bun scripts/build-runtime-bundle.js && bun scripts/build-runtime-bundle.js --check (Bun 1.4.3): current
  • bun frontend/test-runtime-bundle.js: 32 passed (the minified builder's output equals the source's for every conversation); bun frontend/test-workspace.js: 81 passed
  • uv run ruff check . && uv run ruff format --check .: clean
  • bun frontend/test-notebook-builder.js is wired into the frontend-tests job next to "Run workspace tests"; jupytext is a new dev dependency (pyproject.toml and uv.lock, uv sync --frozen --extra dev verified)

Notes for review

  • Jupytext does not treat # %% as escapable: a line such as # %% x (indented one space) still splits a cell, and Jupytext itself does not round-trip such a line. The builder therefore rewrites a code line that begins # %% at any indent to # # %%, in the notebook source as well, so both outputs stay equal.
  • Magics follow Jupytext's own rules (jupytext/magics.py, ported in the builder): the setup cell's %pip is written # %pip, and a comment that Jupytext would read as a magic (# %pip x, # why?) is written with an extra # , so reading the script back restores each source exactly.
  • The header comment is written as comments inside Jupytext's front matter. A plain comment before the first # %% becomes an extra code cell when Jupytext reads the file. The same front matter restores the kernelspec.

Builds an nbformat 4.5 notebook and a percent-format script from a chat's
successful runs. Tested with bun, nbformat and jupytext over recorded and
hand-authored conversations.
… the bundle

Regenerates the runtime bundle and its integrity line with Bun 1.4.3.
Tested: bundle check and the bundle test, which compares the minified
builder with the source.
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://feature-issue-667-notebook-b-demo.osc.earth
Branch feature/issue-667-notebook-builder
Commit babaefd

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

…in their step

Tested: bun frontend/test-notebook-builder.js (125 passed), new cases for sys.path.insert before an import, a written helper module, two imports of one name.
…thout an article

Tested: bun frontend/test-notebook-builder.js (129 passed).
…book builder changes

Tested: bun scripts/build-runtime-bundle.js --check
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 1: outcomes (pushed to feature/issue-667-notebook-builder).

  • Import hoisting moves an import above the statements that make it valid (important): fixed in f848f95. Only the leading block of a run is hoisted (imports after blank lines, comments or a module docstring); an import after other code stays in its step. Added fixtures for sys.path.insert before an import and for a helper module the run writes then imports, both asserting the import stays in its step. from __future__ imports still stay in place and do not end the leading block; star imports also stay.
  • Two different imports binding the same name (important): fixed in f848f95. Each import's bound names are tracked (alias, top package, or each from name). A statement that binds a name already bound to something else, by setup or by an earlier step's import, stays in its step. Tests cover from a import f / from b import f across four runs, import a.b next to import a (not a collision), and a collision with eegprep_lean in setup. Not covered, and noted in the file header: a name a step assigns in plain code (f = 1) that a hoisted import also binds.
  • Fixture names read from the JS source with a regex (suggestion): fixed in the test commit. New test_every_built_conversation_is_checked asserts set(NAMES) == set(built).
  • Titles placed into markdown unescaped (suggestion): fixed in 85cf40b. Step titles, left-out titles and the notebook title are backslash-escaped for markdown and HTML metacharacters, and :// is escaped so a bare URL does not autolink. The error line stays in its code span. Test uses a|b *x* [l](http://e.com) <b>bold</b> ....
  • "From a OSA chat" (suggestion): fixed in 85cf40b. The line now reads From a chat with <community> on <date>.; both test assertions updated.
  • Jupytext rules mirrored in the script writer (suggestion): fixed. The comment block now names Jupytext 1.19.x and says a pin bump means re-running test_notebook_builder.py; the dev pin is capped to jupytext>=1.19.6,<1.20 (uv.lock specifier updated).

Also: the hoisting change made frontend/osa-runtime.bundle.js stale (scripts/build-runtime-bundle.js --check failed), so it and the widget's integrity hash are rebuilt in the last commit.

Tested: bun frontend/test-notebook-builder.js (129 passed), bun frontend/test-widget-notebook-tab.js (168 passed), bun frontend/test-runtime-bundle.js (32 passed), bun scripts/build-runtime-bundle.js --check (current), uv run pytest tests/test_frontend (143 passed), uv run ruff check . and uv run ruff format --check . clean.

…e; define index before the hoisted imports

Tested: bun frontend/test-notebook-builder.js
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 2: outcomes.

  • escapeMarkdown misses $ (and www.): fixed. $ is now in the escaped class and www. is written www\. so it no longer autolinks; the nasty-input test gained $x$ www.e.com. Bare e-mail addresses are left as written (no reliable, harmless escape; the renderers that autolink them only make a mailto link).
  • Unnamed left-out run read Run 4 · Run 4 (timeout): fixed. The note now prints - Run 4 (timeout) when the run has no name; stats.leftOut[].title keeps the Run N fallback. The closing-note test expectations were updated.
  • Hoisted imports ahead of read_index could leave index undefined: fixed by reordering. The setup cell is now %pip, %matplotlib, import eegprep_lean, index = await ...read_index(...), then the hoisted step imports. A missing package now fails only the steps that need it. The ordering assertion in the hoisting test was rewritten to match.
  • "Two new OSARuntime members" wording: rejected, no code change. The PR adds one member (buildConversationNotebook), which is what the issue asks for; the bundle check passes with it. The criterion wording is the mismatch, not the code.

Tested: bun frontend/test-notebook-builder.js (130 passed), test-runtime-bundle.js (32), test-widget-notebook-tab.js (168), test-data-lane.js (106), test-controller.js (144), test-workspace.js (81), test-widget-run-card.js (261), bun scripts/build-runtime-bundle.js --check (current, rebuilt in its own commit), pytest tests/test_frontend/test_notebook_builder.py (30 passed), ruff check clean.

@neuromechanist

Copy link
Copy Markdown
Member Author

PM verdict: ready to merge. Criteria checked against the diff: builder tests wired into frontend-tests, Python nbformat/Jupytext tests present, runtime bundle test and bundle check covered (one OSARuntime member, buildConversationNotebook, as the issue asks; the criterion's "two members" wording was a mismatch). All review findings from both rounds have recorded outcomes. All checks pass (Lint, Test 3.11/3.12, Frontend Tests, Docker build, deploy; Integration skipping). Branch is up to date with develop. Squash merging.

@neuromechanist
neuromechanist merged commit 9934484 into develop Nov 11, 2026
10 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-667-notebook-builder branch November 11, 2026 01:57
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