Skip to content

computer: Give host module calls an execution id, log and defer - #247

Draft
mattzcarey wants to merge 1 commit into
mainfrom
computer/host-module-lifecycle
Draft

mattzcarey wants to merge 1 commit into
mainfrom
computer/host-module-lifecycle

Conversation

@mattzcarey

Copy link
Copy Markdown
Member

Every host module call's context now has executionId, log(text) and defer(fn). A module can keep state for one run, clean it up when the run ends, and tell the caller things its return value can't carry.

agents/browser is moving off @cloudflare/codemode to become a host module (ws:browser) that exec code imports. Its CDP connector needs three things host modules didn't have:

  • an id to keep one browser socket per run;
  • a hook to save the active tab and close the socket when the run ends;
  • a place to say "the browser was restarted" or "the page opened a new tab", which codemode added to the tool result. exec's result belongs to Computer, so the module writes those lines to standard error.
modules: {
  "ws:remote": {
    async send([message], context) {
      let socket = sockets.get(context.executionId);
      if (!socket) {
        socket = await openSocket();
        sockets.set(context.executionId, socket);
        context.defer(() => {
          sockets.delete(context.executionId);
          socket.close();
        });
      }
      return socket.send(message);
    },
  },
}

Deferred functions run however the run ends. They start after the code has finished and its host calls have settled, and finish before the result is published, so the next run sees what they saved. They run one at a time, last registered first, each within the host call deadline. A failure is reported on standard error and the rest still run. Log lines have their own maxStdioBytes budget. docs/17_isolate_javascript.md covers this under host modules.

npm test in packages/computer and npm run check ran. New workerd tests cover the id, logging, and deferred functions on success, failure, cancellation and a throwing deferred function. The deferred tests fail with runDeferred turned off.

This touches bridge.ts and the hand-built test contexts that #238 also changes, so whichever lands second needs a small rebase. Under #238's replay, a deferred function should run once, on the attempt that settles.

A host module that holds something for the length of a run, such as a
connection to a browser, had no way to tell which run a call came from
or when that run ended. It also had no way to tell the caller about
something its return value can't carry, like the remote end having
restarted since the last run.

Every host module call's context now has the run's executionId, a log
function that writes a line to the run's standard error, and a defer
function that registers cleanup. Deferred functions run however the
run ends, after its code has finished and its host calls have settled,
and before its result is published, so the next run sees whatever they
saved. They run one at a time, last registered first, each within the
host call deadline. A failure is reported on standard error and the
rest still run. Log lines have their own maxStdioBytes budget.
@changeset-bot

changeset-bot Bot commented Oct 17, 2026

Copy link
Copy Markdown

馃 Changeset detected

Latest commit: 2dbb36a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@cloudflare/computer Minor
@cloudflare/dofs Minor
@cloudflare/computer-rpc Minor
@cloudflare/computerd Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

expect(start.status).toBe(200);
const probe = (name: string) =>
runtime({
source: `import { ${name} } from "ws:lifecycle"; export default () => ${name}(${JSON.stringify(id)});`,
@pkg-pr-new

pkg-pr-new Bot commented Oct 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@cloudflare/computer@247

commit: 2dbb36a

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.

2 participants