Repository navigation
Widget: show a run's code in place, with Copy and Download - #503
Merged
Merged
Conversation
Contributor
Preview Deployment
This preview will be updated automatically when you push new commits. |
This was referenced Sep 24, 2026
Each run's code is now its own disclosure (Code, N lines) beside the run's output and figures, closed until opened and kept open across re-renders. Its bar copies the exact code (Clipboard API, then the copy command, then selecting the code for the reader; never a dialog) and downloads it as <dataset>-run-<n>.py built as a Blob. The permission gate gets the same Copy. The workspace export now shares the download helper. Tested: bun frontend/test-widget-tools.js (385 passed, 73 new), test-widget-color-scheme.js (128); 22 mutations each caught and restored; Copy and Download checked by hand in headless Chrome.
Tested: docs only.
A download's blob: address still serves the file right after the click and is revoked after the wait (the wait is now a test-hook variable); a community id with unsafe characters or only dots names the file safely; Settings' Download workspace reports why it cannot build a zip, through init's wiring. Tested: bun frontend/test-widget-tools.js (397 passed); never revoking, revoking at once, dropping the sanitizer or the fallback name, and unwiring or silencing the workspace button each fail it.
The pop-out shows a run's code block with the exact code, closed even when a later save happened while it was open on the page, and its Copy and Download work there. Tested: bun frontend/test-widget-popout.js (140 passed); storing and reading back the open state fails it.
Tested: docs only.
neuromechanist
force-pushed
the
feature/issue-491-run-code
branch
from
September 25, 2026 00:53
5b0c894 to
ef106ee
Compare
Member
Author
Review responseRebased onto current
Skipped: the "Copied" label can go stale if a re-render lands inside its 2-second window. It is a temporary indicator, the re-rendered button is correct, and no state is lost. Every widget suite passes: tools 397 (385 before), pop-out 140 (132), capsule 133, color scheme 128, notebook tab 168, first paint 82, dataset questions 60. The new checks' asserts now tally rather than being swallowed by a captured |
3 tasks
neuromechanist
added a commit
that referenced
this pull request
Sep 25, 2026
Brings in #502, #503 and #508. The generated runtime bundle and its integrity line are rebuilt from the merged sources; CHANGELOG keeps both Added entries. Tested: bun scripts/build-runtime-bundle.js --check; test-worker-core (220), test-data-lane (106), test-runtime-bundle (25), test-widget-tools (397), test-runtime-lifecycle and test-egress all passed.
This was referenced Sep 25, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #491
Each run's code is now its own disclosure, shown in place in the run's entry, with Copy and Download; the permission gate gets the same Copy.
What the reader sees
Each run is two disclosures, one under the other, joined by a rule down their left:
Ran Python: Plot a 10 Hz sine ...): its output, its figures and "Edit and run". It opens by default exactly when it did before: a figure, a reader's own run, or an open editor.Code · 8 lines), closed until the reader opens it, so it opens without the output, and a figure is not pushed down by the script that drew it. Whether it is open is kept on the run record (_codeOpen, via thetoggleevent), so a streamed chunk or runtime progress re-rendering the reply does not close it; it is stripped insaveHistorylike the other transient fields, so a reload shows it closed.Opening the code shows a bar over it with two icon buttons,
type="button",aria-labeled, with arole="status"line beside them:clipboard-write), it triesdocument.execCommand('copy')on an off-screen textarea, handing focus back afterward; where that fails too, it opens the code, selects it, and says "This page cannot copy for you: the code is selected, so press Ctrl+C" (Cmd+C on Apple platforms). Never a browser dialog.copyToClipboard, which the chat's markdown code blocks and message copy use, is untouched, so no other community's behavior changes.text/x-pythonBlob throughsaveFile(the workspace export's link-and-revoke steps, now shared by both), named<dataset>-run-<n>.py: the dataset recorded on the question the reply answers (setDatasetwhen the reader sent it, Dataset pages: suggested questions about the dataset on screen #477), or the community id when there was none;nis the run's place among the conversation's runs, from 1. Only[A-Za-z0-9._-]survive in the name, and a leading dot is dropped.The gate's code has the same bar with Copy only (nothing has run yet), and copying decides nothing.
Everything the model wrote is escaped as before: the code goes through
highlightOrEscape, and the file name and line count are ours.Done when
frontend/test-widget-tools.js, 73 new checks (385 in all). The block's structure and label, closed by default, the output not inside it; hostile code (the suite'sHOSTILEset) leaving no foreign markup and reading back exactly, escaped and highlighted; Copy through happy-dom's own Clipboard, read back withreadText, from highlighted code, and "Copied" clearing; Copy when the browser refuses (happy-dom'sclipboard-writepermission set todenied, which makes its realwriteTextreject): the code opened and selected in full, the keys named, noalert/confirm/prompt; Copy with nonavigator.clipboard, through the copy command (happy-dom has none, so a recorder stands in and records what is selected when it runs): the exact code, the textarea removed, focus back; a refusing command falling through to the selection; Download's Blob text, type and file name for a dataset question and a no-dataset question, viaURL.createObjectURLand the clicked link, both recorded on the way through; the gate's Copy; the open state across a re-render and not stored; a reply with no runs rendering none of it, the chat's own code-block Copy unchanged.frontend/test-widget-color-scheme.jsadds the darkCopiedcolor (128).docs/community-widget.mdgains "A run in the chat";docs/community-browser-runtime.mdlinks to it from "The editable re-run panel"; CHANGELOG entry under[Unreleased].Checked in Chrome
A one-off script against
widget_e2e.py(the scripted model's "plot", real Pyodide from the CDN) in headless Chrome 153, with clipboard permission granted and downloads allowed: the gate's Copy put the gate's code on the clipboard; after Run, the run's code block held the code that ran; clickingCodeopened it; the run's Copy put its exact code on the clipboard; Download savednm000132-run-1.pywhose bytes equal the code; and the block was still open after the reply finished rendering. Not committed: it needs the network for Pyodide, which is why the committed tests are the happy-dom ones.Mutation checks
Each applied alone to
frontend/osa-chat-widget.js, the suite run, then restored, with the restore confirmed by comparinggit diff --statbefore and after. All 22 caught, each by the assertion that names it:writeTextnot caughttext/plainCopiedcolorNot built: Send to notebook (stretch)
Not clean, for three reasons, so none of it is here:
notebook/osa-bridge.jsinserting a cell drives JupyterLite's notebook model throughwindow.jupyterapp. Its only real test isnotebook/e2e-check.js, which builds the whole site, serves it, and reads live Zarr data, so it needs the network and is not in CI. A Bun test would need a stand-in JupyterLite app, a mock of exactly the API the code would be written against.osa.fetch,osa.save_scriptandosa.save_artifactexist only there, and eegprep-lean's transport is set by the runtime's prelude. The notebook has noosamodule and sets up eegprep-lean in its own setup cell, so a copied cell that callsosa.fails. A clean version needs a decision on translating or flagging those calls.thememessage from its parent. Accepting code means any page the notebook site'sframe-ancestorsallows could write cells into a reader's stored notebook. That is plausibly fine, but it is a decision fordocs/adr/0012(or its successor) to make, not a stretch goal. There is also plumbing to settle: the frame exists only on a capsule page, only for the dataset on screen, after the notebook reports ready, and the pop-out has a frame of its own.Copy and Download cover the same need for now: the reader can paste the code into the notebook, or upload the
.py.Other suites
Based on current
develop(with #499). Every Bun suitetests.ymlruns passes (widget suites: tools 385, capsule 133, color scheme 128, notebook tab 168, first paint 82, dataset questions 60, pop-out 132; the runtime bundle is current);uv run ruff check .is clean;uv run pytest --ignore=tests/test_integration -m "not network"passes, 2922 tests. Locally I deselectedtests/test_knowledge/test_papers_sync.py::TestPapersSync, whose two tests call the live OpenAlex API without anetworkmark and hung on this machine's connection twice (unrelated to this change; CI runs them).