From 6ff7f878e2deb8962c9aaa0e11e4e311a86af90d Mon Sep 17 00:00:00 2001 From: Raunak Raj <71929976+bajrangCoder@users.noreply.github.com> Date: Sun, 27 Sep 2026 17:35:54 +0530 Subject: [PATCH] fix(console): restore isolated worker execution by default Restore upstream behaviour from Acode-Foundation/Acode#2808, which was reverted as part of the iOS terminal work. The console runs in the isolated worker by default, page execution is opt-in and labelled "Page (unsafe)", and the standalone menu console always uses the worker. Co-Authored-By: Claude Opus 5.5 --- src/lib/console.js | 25 ++++++++++++++-------- src/lib/consoleRuntime.js | 11 ++++++++-- tests/unit/consoleExecutionContext.test.js | 11 ++++++++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/lib/console.js b/src/lib/console.js index 72615db..35a32cb 100644 --- a/src/lib/console.js +++ b/src/lib/console.js @@ -8,6 +8,7 @@ import ConsoleExecutor, { applyConsoleViewport, executeConsoleCommand, executeConsoleScript, + resolveConsoleExecutionContext, } from "./consoleRuntime"; (function () { @@ -69,14 +70,14 @@ import ConsoleExecutor, { title: "Choose isolated Worker execution or live page access", }, children: [ - tag("option", { - textContent: "Page", - attr: { value: "page" }, - }), tag("option", { textContent: "Worker", attr: { value: "worker" }, }), + tag("option", { + textContent: "Page (unsafe)", + attr: { value: "page" }, + }), ], onchange() { if (this.value !== "page") return; @@ -97,7 +98,10 @@ import ConsoleExecutor, { children: [ $input, tag("c-input-actions", { - children: [$executionContext, $stopExecution], + children: [ + ...(isStandaloneConsole ? [] : [$executionContext]), + $stopExecution, + ], }), ], }); @@ -209,7 +213,7 @@ import ConsoleExecutor, { async function runStartupScript() { if (!startupScriptUrl) return; - setExecutionState(true, "worker"); + setExecutionState(true); const result = await executeConsoleScript({ scriptUrl: startupScriptUrl, workerExecutor: executor, @@ -448,10 +452,10 @@ import ConsoleExecutor, { $input.focus(); } - function setExecutionState(running, context = $executionContext.value) { + function setExecutionState(running) { isExecuting = running; $console.toggleAttribute("running", running); - $stopExecution.hidden = !running || context !== "worker"; + $stopExecution.hidden = !running || $executionContext.value !== "worker"; $input.disabled = running; $executionContext.disabled = running; } @@ -861,7 +865,10 @@ import ConsoleExecutor, { function executeCommand(code) { return executeConsoleCommand({ - context: $executionContext.value, + context: resolveConsoleExecutionContext( + isStandaloneConsole, + $executionContext.value, + ), code, workerExecutor: executor, pageExecutor: execute, diff --git a/src/lib/consoleRuntime.js b/src/lib/consoleRuntime.js index 6c9944f..5e45e25 100644 --- a/src/lib/consoleRuntime.js +++ b/src/lib/consoleRuntime.js @@ -164,12 +164,19 @@ export function applyConsoleViewport(element, windowObject = window) { element.style.setProperty("--console-viewport-left", `${left}px`); } +export function resolveConsoleExecutionContext( + isStandaloneConsole, + selectedContext, +) { + return isStandaloneConsole ? "worker" : selectedContext; +} + /** * Routes a console command to the isolated worker or the live preview page. - * Page execution keeps browser objects inspectable; workers isolate scripts. + * Page execution is deliberately opt-in because it runs on the preview thread. */ export function executeConsoleCommand({ - context = "page", + context, code, workerExecutor, pageExecutor, diff --git a/tests/unit/consoleExecutionContext.test.js b/tests/unit/consoleExecutionContext.test.js index aafbd1a..3d6a651 100644 --- a/tests/unit/consoleExecutionContext.test.js +++ b/tests/unit/consoleExecutionContext.test.js @@ -2,10 +2,16 @@ import { describe, expect, it } from "vitest"; import { executeConsoleCommand, executeConsoleScript, + resolveConsoleExecutionContext, } from "lib/consoleRuntime"; describe("console execution context", () => { - it("uses isolated worker execution when selected", async () => { + it("forces standalone menu consoles to use the worker", () => { + expect(resolveConsoleExecutionContext(true, "page")).toBe("worker"); + expect(resolveConsoleExecutionContext(false, "page")).toBe("page"); + }); + + it("uses isolated worker execution by default", async () => { const calls = []; const result = await executeConsoleCommand({ context: "worker", @@ -25,9 +31,10 @@ describe("console execution context", () => { expect(result.value).toBe("isolated"); }); - it("evaluates interactive commands in the live page by default", async () => { + it("restores live page execution when explicitly selected", async () => { const calls = []; const result = await executeConsoleCommand({ + context: "page", code: 'document.querySelector("main")', workerExecutor: { execute(code) {