Skip to content

👾 Open Computer Use – Open-Source Alternative to Codex Computer Use

js repl: attribute errors and output to the call that produced them

ATG51f3a590e734
diff --git a/docs/histories/2026-09/20260922-1330-js-repl-error-propagation-and-read-back.md b/docs/histories/2026-09/20260922-1330-js-repl-error-propagation-and-read-back.md
index 328a56c..59f191d 100644
--- a/docs/histories/2026-09/20260922-1330-js-repl-error-propagation-and-read-back.md
+++ b/docs/histories/2026-09/20260922-1330-js-repl-error-propagation-and-read-back.md
@@ -13,10 +13,11 @@
**Key Actions:**
- **Error propagation**: A throw inside evaluated code (sync, or after an `await`) never reached the REPL eval callback; Node prints it and moves on, so the call waited for `timeout_ms` and then reset the session. The session now settles the active evaluation from the REPL's error path (`handleError` on Node 26, the domain `error` event on Node 22) and returns `isError` with the message; bindings survive.
+- **Error ownership (review follow-up)**: settling "the active call" let a late throw from a finished call (`setTimeout(() => { throw ... }, 50)`) reject whichever call ran next, and the early-rejected call kept running and wrote into the following call's output. Each evaluation now runs inside an `AsyncLocalStorage` owner that timers and promises carry; the error path settles only the call that threw, and `nodeRepl` output is refused to any code whose call has returned. Late errors from finished calls are dropped, as the REPL did before.
- **Cross-realm error messages**: errors thrown inside the REPL context fail `instanceof Error`; `asError` now uses their `message` instead of `String(error)`, avoiding `Error: Error: ...`.
- **Read-back switch**: `OPEN_COMPUTER_USE_ACTION_READ_BACK=0` makes macOS action tools return a short status and skip the post-action settle and snapshot. All action-result sites route through one `actionResult(for:)` helper; the settles that exist only for the read-back go through `pauseBeforeReadBack(_:)`. The flag is read per call so the app agent's per-request environment forwarding applies. Default behaviour is unchanged.
- **Adapter**: launches the native runtime with the variable set, since `app.click()` and friends discard the native result and state is read explicitly with `getAXState()`.
-- **Tests**: Node test for thrown errors settling the call and keeping bindings (fails with a 1 s timeout before the fix); XCTest for the flag parsing.
+- **Tests**: Node test for thrown errors settling the call and keeping bindings (fails with a 1 s timeout before the fix); a late-throw regression test for both the in-process and Worker sessions; XCTest for the flag parsing.
### 🧠 Design Intent (Why)
The adapter's value is batching actions and one final observation in a single round trip. Each native action was still paying a 150 ms settle plus a full accessibility snapshot that the adapter threw away, so a batch cost more than the discrete tools it replaced. Skipping the read-back only when a program asks for it keeps the nine-tool surface identical for direct MCP clients. The error fix keeps a model's mistake at one failed call instead of a timeout plus a lost session.
diff --git a/docs/references/js-repl.md b/docs/references/js-repl.md
index 3dff960..99c5a2d 100644
--- a/docs/references/js-repl.md
+++ b/docs/references/js-repl.md
@@ -67,7 +67,10 @@ Top-level bindings persist across calls. Prefer `var` for reusable names, or use
REPL does not close applications or erase their UI state.
A thrown exception ends the `js` call with `isError: true` and the message;
-bindings survive. Only a timeout resets the session.
+bindings survive. Only a timeout resets the session. Errors and output belong to
+the call whose code produced them: a timer or promise callback from a call that
+has already returned cannot fail or write into a later call; its late error is
+dropped.
## Read-back
diff --git a/scripts/node-repl/open-computer-use-repl.mjs b/scripts/node-repl/open-computer-use-repl.mjs
index dacd360..7c79d70 100755
--- a/scripts/node-repl/open-computer-use-repl.mjs
+++ b/scripts/node-repl/open-computer-use-repl.mjs
@@ -1,5 +1,6 @@
#!/usr/bin/env node
+import { AsyncLocalStorage } from "node:async_hooks";
import { spawn } from "node:child_process";
import { EventEmitter } from "node:events";
import { existsSync, realpathSync } from "node:fs";
@@ -10,6 +11,9 @@ import { fileURLToPath } from "node:url";
import { Worker } from "node:worker_threads";
const require = createRequire(import.meta.url);
+// The js call whose code is running. Timers and promises carry it, so a callback
+// scheduled by a finished call still names that call, not the one running now.
+const evaluationOwner = new AsyncLocalStorage();
const repl = require("node:repl");
const { PassThrough } = require("node:stream");
@@ -326,8 +330,9 @@ export class PersistentJavaScriptSession {
}
#currentOutput() {
- if (!this.active) throw new Error("nodeRepl output is only available while js is executing");
- return this.active;
+ const owner = evaluationOwner.getStore();
+ if (!owner || owner !== this.active) throw new Error("nodeRepl output is only available while js is executing");
+ return owner;
}
reset() {
@@ -336,10 +341,14 @@ export class PersistentJavaScriptSession {
const output = new PassThrough();
output.resume();
// A throw inside evaluated code (sync, or after an await) never reaches the eval
- // callback: the REPL prints it and moves on. Settle the active evaluation from the
- // REPL's error path instead. Node 26 exposes `handleError`; Node 22 routes through
- // a domain.
- const settle = error => { this.rejectActive?.(error); return "ignore"; };
+ // callback: the REPL prints it and moves on. Settle the evaluation from the REPL's
+ // error path instead (`handleError` on Node 26, a domain on Node 22), but only the
+ // call that threw: a late throw from a finished call is dropped, as the REPL would.
+ const settle = error => {
+ const owner = evaluationOwner.getStore();
+ if (owner && owner === this.active) owner.reject(error);
+ return "ignore";
+ };
this.repl = repl.start({ prompt: "", input, output, terminal: false, useGlobal: false, ignoreUndefined: true, breakEvalOnSigint: true, handleError: settle });
this.repl.on("error", () => {});
this.repl._domain?.on("error", settle);
@@ -370,8 +379,8 @@ export class PersistentJavaScriptSession {
const enforceTimeout = Number.isFinite(timeoutMs) && timeoutMs > 0;
try {
const evaluation = new Promise((resolve, reject) => {
- this.rejectActive = reject;
- this.repl.eval(code, this.repl.context, "open-computer-use-repl", (error, result) => error ? reject(error) : resolve(result));
+ active.reject = reject;
+ evaluationOwner.run(active, () => this.repl.eval(code, this.repl.context, "open-computer-use-repl", (error, result) => error ? reject(error) : resolve(result)));
});
const value = enforceTimeout
? await Promise.race([evaluation, new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(`js execution timed out after ${timeoutMs} ms; session reset`)), timeoutMs); })])
@@ -386,7 +395,6 @@ export class PersistentJavaScriptSession {
} finally {
clearTimeout(timer);
this.active = null;
- this.rejectActive = null;
}
}
}
diff --git a/scripts/node-repl/open-computer-use-repl.test.mjs b/scripts/node-repl/open-computer-use-repl.test.mjs
index e0ea570..8293c7f 100644
--- a/scripts/node-repl/open-computer-use-repl.test.mjs
+++ b/scripts/node-repl/open-computer-use-repl.test.mjs
@@ -125,6 +125,23 @@ test("thrown errors settle the call and keep bindings", async () => {
assert.equal(result.content.at(-1).text, "1");
});
+for (const Session of [PersistentJavaScriptSession, WorkerJavaScriptSession]) {
+ test(`${Session.name}: a late throw from a finished call does not reach the next call`, async () => {
+ const session = new Session({ native: mockNative() });
+ try {
+ let result = await session.run(`var kept = 1; setTimeout(() => { throw new Error("late"); }, 20);
+ setTimeout(() => nodeRepl.write("late write"), 20);`, 1000);
+ assert.equal(result.isError, false);
+ result = await session.run(`await new Promise(resolve => setTimeout(resolve, 100)); nodeRepl.write("next " + kept);`, 1000);
+ assert.deepEqual(result, { content: [{ type: "text", text: "next 1" }], isError: false });
+ result = await session.run(`nodeRepl.write("after");`, 1000);
+ assert.deepEqual(result, { content: [{ type: "text", text: "after" }], isError: false });
+ } finally {
+ await session.close?.();
+ }
+ });
+}
+
test("screenshots emit binary image content", async () => {
const session = new PersistentJavaScriptSession({ native: mockNative() });
const result = await session.run(`