Skip to content

Phase 4a: a browser workspace, osa.save_script and save_artifact, and export - #455

Merged
neuromechanist merged 34 commits into
feature/issue-429-epic-browser-executionfrom
feature/issue-433-phase4-workspace
Sep 23, 2026
Merged

neuromechanist merged 34 commits into
feature/issue-429-epic-browser-executionfrom
feature/issue-433-phase4-workspace

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Phase 4's workspace (#433): what a run leaves behind now survives, in the reader's own browser, and nothing leaves it unless the reader exports it.

  • Every run is kept in IndexedDB under <community>/<session>/: scripts/run-NNN.py (the code as it ran) and results/run-NNN/ (its stdout, stderr and summary, bounded at 262,144 characters as get_full_output already keeps them, and each figure as a PNG). A per-session manifest.json is derived from the stored records whenever it is needed, never stored, so it cannot drift.
  • osa.save_script(name, code) and osa.save_artifact(path, data) in the sealed runtime. Only these explicit saves are listed back to the model in the result's artifacts (sorted and deterministic, so the prompt cache holds). The bytes travel back to the host the way full output already does, are stripped before anything is sent, and never reach the server.
  • "Saved equals announced." Paths are relative, [A-Za-z0-9._-] segments, no . or .., depth at most 4, at most 200 characters, checked on the caller's own argument in Python and again on the host. At most 10 MB per file, 25 MB and 32 files of explicit saves per run, 250 MB per community. Any write that fails (quota, private browsing, IndexedDB unavailable, a hang past the deadline) is reported in that run's result as whole, sorted stderr lines, and the file is dropped from artifacts: the model is never told a file exists when it does not.
  • Bounded and race-free. Every store call has a deadline, so a stuck IndexedDB can no longer stall a chat turn; a failed open does not disable the workspace for the page's life; a run's number is reserved atomically in one IndexedDB transaction, so two tabs of the same session cannot overwrite each other's runs.
  • Export and delete from a new workspace section in Settings (shown only for communities with client tools): one zip of the community's workspace, each session with its manifest and a generated notebook.ipynb (nbformat 4.5, one code cell per run with its outputs), which opens in Jupyter, JupyterLite, VS Code or Colab. Delete is confirmed inline. The zip writer is one module (frontend/osa-zip.js) that the test wheel builder now shares.
  • NEMAR's execute_code description and docs/community-browser-runtime.md describe the workspace, its limits, and that it is per origin (the popout window on another origin sees neither the chat history nor the workspace).

Test plan

  • uv run pytest -m "not network": 2716 passed, 50 skipped; the one failure is test_version_shows_version fails locally and passes in CI, because rich colorizes on a TTY #434. ruff check and ruff format --check are clean.
  • Every Bun suite CI runs, green: test-worker-core.js 190 (real Pyodide), test-widget-tools.js 149, test-runtime-lifecycle.js 177, test-controller.js 95, test-workspace.js 77 (new), test-output.js 76, test-egress.js 80, test-data-lane.js 57, test-runtime-bundle.js 25, and the rest. The bundle check is current.
  • test_workspace_export.py writes a real export through the real modules and validates it with zipfile and nbformat, across two sessions.
  • chrome.js on real IndexedDB in Chrome 153: the workspace write page (36 checks, including two concurrent writers, the host-side run budget and 32-file cap, and a genuinely malformed file) and a second page load reading it back (12 checks, including a run continuing at run-003); all other pages pass.
  • Mutation-checked, each caught: the atomic ordinal reservation, the deadline, whole-line notes, the 32-file cap on the host, the zip entry limit and UTF-8 flag, the drift test pinning the JavaScript limits to the generated Python, and the path, size and artifact guards in both languages.
  • Reviewed locally by four Sonnet agents; the findings and what was done are in a comment below.

Part of #433 and epic #429.

E5: a run may save at most 32 distinct explicit files, matching
ClientToolResult.artifacts' own cap, so saved always equals
announced. D7: path validation runs on the caller's raw name/path
before scripts/ or artifacts/ is prefixed, so an absolute-looking
argument is refused for what it actually is.
save_script/save_artifact now check the caller's raw name/path for
being relative and well-formed before scripts/ or artifacts/ is
added, so an absolute path is refused as "must be relative" rather
than as a confusing empty path segment. Docstrings state the numeric
limits (D3) and point to the Settings panel's whole-workspace
download/delete, never a per-file delete (D5).
E1: every public method races a deadline (default 10s, overridable
per instance like PyodideRuntime's bootTimeoutMs), and _open reacts
to onblocked and resets its cached promise on rejection, so a stuck
IndexedDB request reports failure instead of hanging a chat turn.

C1: ordinal assignment is now atomic. _reserveOrdinal reads a
session's highest ordinal and writes a RESERVED placeholder in ONE
readwrite transaction, relying on IndexedDB serializing overlapping
transactions across tabs and connections, so two tabs recording at
once never collide on run-NNN and silently overwrite each other.
deriveManifest and exportZip leave a still-reserved run out.

E2: the run-record and deleteAll transactions gain onabort, matching
putFile's existing three-handler pattern.

D4/C2/E5(host): the 25MB per-run budget and the 32-file cap apply
only to explicit saves, both re-checked on the host; automatic files
are bounded by their own caps (_FULL_CHARS, image limits) and never
compete with a path Python already accepted for the same budget.
C3: buildStoredZip throws rather than writing a wrapped entry count
past the 16-bit end-of-central-directory field's 65,535-entry limit.
C4: local and central headers set the general-purpose flag's UTF-8
bit (0x0800), so a future caller passing a non-ASCII name gets a
correctly labeled archive.
E3: a genuine (non-timeout) rejection from recordRun -- WorkspaceStore's
own failures always resolve to a value -- is logged with
console.error and every explicit save for that run is dropped from
artifacts, with a deterministic stderr line naming the failure, so a
bug here never lets the model believe an unverifiable save succeeded.

E4: the workspace failure note is appended AFTER the run's own stderr
is clipped to the community's limit, keeping only whole lines (with
an "and N more" trailer) rather than letting toClientToolResult's
own clipping cut the note away or truncate it mid-line.
Covers E3 (a genuine recordRun rejection is logged and drops every
explicit save) and E4 (a note is never clipped mid-line even when
stderr_chars is set to its 256-character floor). Also drops a stale
reference to a plan document in a comment (D2).
E1: a real dbFactory whose open() never settles proves the deadline
reports failure rather than hanging, and a fast rejecting one proves
_open resets its cached promise rather than replaying it.
T2: a second session in the zip-writer test proves exportZip scopes
each session's manifest and notebook to only its own runs.
T5: the exact per-file byte cap passes, mirroring the existing
community-cap pair.
C3/C4: buildStoredZip's entry-count refusal and its UTF-8 flag bit,
the latter checked against Python's zipfile since the system unzip
on this machine mis-decodes UTF-8 names regardless of the flag.
Adds setWorkspaceStore, formatWorkspaceBytes, refreshWorkspacePanel,
downloadWorkspace, handleWorkspaceDeleteClick and closeSettings to
the __OSA_TEST__ surface, so a test can exercise the Settings
workspace panel against a real WorkspaceStore without booting a
runtime (T3).
Size formatting across B/KB/MB/GB boundaries, hidden when there is
no store, the first Delete click only arms it while the second
attempts the delete, reopening Settings disarms a pending confirm,
and a failed size read or delete shows an error rather than nothing
-- all against a real WorkspaceStore, genuinely unavailable under
happy-dom exactly as it is under Bun.
BADFILE reports one artifact whose base64 is genuinely invalid,
something real encoding can never produce, so a real recordRun call
against it makes atob() throw for real -- the trigger the browser
harness needs to exercise ClientToolController#persist's catch path.
Against real IndexedDB, since none of these are reachable under Bun:
E3 (a genuine recordRun failure logs and reports, via a protocol-
speaking worker rather than real Pyodide), E5 host-side (the 32-file
cap), T1 (the per-run byte budget), T4 (ordinals survive a reload),
T6 (two sessions in one community), and C1 (two connections
recording concurrently for one session never collide on an ordinal).
E6 bounds each phase by its own deadline so a hang fails loudly.
A second session in the fixture, with its own run and its own
scripts/run-001.py, proves each session's manifest and notebook in
the exported zip hold only that session's own content, never
leaking a call_id across the shared archive.
WORKSPACE_LIMITS in osa-workspace.js mirrors five constants declared
in the Python buildOutputCaptureSource generates; this test reads
them out of that generated text and compares, the same drift-guard
pattern already used for SERVER_LIMITS.
results/run-NNN's stdout/stderr/summary are bounded at 262,144
characters (_FULL_CHARS), the same copy get_full_output keeps, not
literally the full, untruncated text as the doc previously claimed.
Regenerated from the review-fix changes to osa-workspace.js,
osa-controller.js, osa-egress.js, osa-output.js and osa-worker-core.js.
Labels like E3 and T6 named review items, not anything in the repository;
each comment now states its rule. Suites and chrome.js rerun green.
@neuromechanist

Copy link
Copy Markdown
Member Author

Local review, and what was done with each finding

Four Sonnet reviewers: code and security, tests, error handling, and comments and docs. Every finding was addressed.

Error handling

  • Critical: no deadline anywhere in the persist path, so a hung IndexedDB call (a blocked upgrade, a transaction that never settles) stalled the chat turn forever. Every store call is now bounded; onblocked rejects; a rejected open resets rather than disabling the workspace for the page's life; the Settings calls are bounded with visible feedback. A test whose open never settles fails by deadline, and removing the deadline makes it hang.
  • The run-record write and deleteAll lacked onabort; both have it.
  • An unexpected throw returned the result unchanged and logged nothing, which could tell the model about files never saved. It now logs, drops every explicit save from artifacts, and adds one deterministic line. It is tested through a real trigger: a malformed file reaching the real atob.
  • The failure note could be clipped mid-line by stderr_chars. The run's own stderr is clipped first, and the note is kept as whole lines with an "and N more" line.
  • More than 32 saves were persisted but only 32 announced. A run's explicit saves are now capped at 32 in Python and on the host: saved equals announced.
  • The harness page could hang with no verdict; each phase is bounded.

Code and security

  • Two tabs of one session could take the same run number and silently overwrite each other. The number is now reserved atomically in one IndexedDB transaction; two concurrent real connections get distinct numbers and both runs survive.
  • The zip entry count could silently wrap past 65,535 entries; it is refused. Entries set the UTF-8 flag.
  • Verified clean: bytes never reach the server (traced end to end), path traversal, the generated Python, determinism, the Settings panel's escaping, the zip and notebook formats, and the CI edits.

Tests

  • The host-side run budget, multi-session export (a manifest mix-up passed before), the Settings panel, run numbers across a reload, the exact 10 MB and 25 MB boundaries, multi-session size and manifests, and the helpers' closure being unreachable from executed code are now all tested, each proven by a mutation.

Comments and docs

  • "Full, untruncated" output is bounded at 262,144 characters; the guide says so.
  • Plan and review labels in comments and check names (including two older ones) were replaced by the rule each states.
  • The helpers' docstrings, which the model reads through help(), state the numbers.
  • The two budget layers now agree: the per-run cap counts explicit saves only, on both sides.
  • "Delete" removes the whole workspace, and the docstrings say so.
  • A drift test pins the JavaScript limits to the generated Python.
  • Paths are validated as the caller wrote them, before the scripts/ or artifacts/ prefix, so an absolute path gets the right message.

@github-actions

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://feature-issue-433-phase4-wor-demo.osc.earth
Branch feature/issue-433-phase4-workspace
Commit 8582c31

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

@neuromechanist
neuromechanist merged commit 2bb13a8 into feature/issue-429-epic-browser-execution Sep 23, 2026
14 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-433-phase4-workspace branch September 23, 2026 15:54
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