Skip to content

computer: Let runtime.exec carry metadata for host modules - #291

Open
ben-reitz wants to merge 1 commit into
browser/execution-contextfrom
browser/exec-metadata
Open

ben-reitz wants to merge 1 commit into
browser/execution-contextfrom
browser/exec-metadata

Conversation

@ben-reitz

@ben-reitz ben-reitz commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

A host module sometimes needs to know who a run is for, such as the conversation that started it, so it can keep separate state for each. The run's env and input can't carry that: the run's code reads both, and a model writing that code could change them.

runtime.exec() now takes metadata, a record of strings, and host module calls read it as context.execution.metadata. The run's code never sees it. Host code reads it as WorkspaceExecutionMetadata, whose values may be undefined, since a run may have been started without metadata, by other code, or before metadata existed.

await workspace.runtime.exec(source, {
  backend: "worker-javascript",
  metadata: { conversation: conversationId },
});

WorkerJavaScriptBackend validates it like env and caps it at a fixed 16 KiB, which is plenty for identifiers; it can become an option if someone needs more. The Workspace stub and the client pass it through, and backends without host modules ignore it.

A replayable run saves its metadata with the rest of its start, so it keeps it when it resumes. The field is optional in the saved start, and a run saved by an earlier version resumes with empty metadata, so JOURNAL_VERSION stays the same and runs in flight during a deploy still resume. runtime.pending() and onWait list each waiting call with its run's metadata, which lets an app that queues work per conversation see that a paused run holds a conversation's place.

Unit tests cover validation, the byte cap, resuming with and without saved metadata, and pending(). Workerd tests check that host modules see the metadata, that the run's code can't find it in process.env or its input, that it reaches the backend through the stub, and that it survives a restart while a run waits. docs/05_runtime_interface.md and docs/17_isolate_javascript.md describe the option.

@github-actions github-actions Bot closed this Nov 2, 2026
@changeset-bot

changeset-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: bcf2f52

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

@ben-reitz ben-reitz added the allow-pr Allow a PR to remain open. label Nov 2, 2026
@ben-reitz ben-reitz reopened this Nov 2, 2026
@ben-reitz
ben-reitz added this pull request to stack #294 November 2, 2026 17:27
@pkg-pr-new

pkg-pr-new Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: bcf2f52

@ben-reitz
ben-reitz force-pushed the browser/exec-metadata branch from b3129d3 to 075b4bd Compare November 3, 2026 10:14
@ben-reitz
ben-reitz marked this pull request as ready for review November 3, 2026 10:15
devin-ai-integration[bot]

This comment was marked as resolved.

@ben-reitz
ben-reitz force-pushed the browser/exec-metadata branch from 075b4bd to 98e2b2e Compare November 3, 2026 11:31
devin-ai-integration[bot]

This comment was marked as resolved.

@cloudflare cloudflare deleted a comment from github-actions Bot Nov 3, 2026
@ben-reitz
ben-reitz force-pushed the browser/exec-metadata branch from 98e2b2e to ae9e6d7 Compare November 3, 2026 11:50
@aron-cf
aron-cf force-pushed the browser/exec-metadata branch from ae9e6d7 to 5205a37 Compare November 3, 2026 14:17
@ben-reitz
ben-reitz removed this pull request from stack #294 November 4, 2026 10:44
@ben-reitz
ben-reitz added this pull request to stack #304 November 4, 2026 10:50
A host module sometimes needs to know who a run is for, such as the
conversation that started it, so it can keep separate state for
each. The run's `env` and `input` can't carry that: the run's code
reads both, and a model writing that code could change them.

`runtime.exec()` now takes `metadata`, a record of strings. The
JavaScript backend validates it like `env` and caps it at 16 KiB of
keys and values. Host module calls read it as
`context.execution.metadata`, and the run's code never sees it. Host
code reads it as `WorkspaceExecutionMetadata`, whose values may be
`undefined`, since a run may have been started without it. The
Workspace stub and the client pass it through, and backends without
host modules ignore it.

A replayable run saves its metadata with the rest of its start, so it
keeps it when it resumes. The field is optional in the saved start: a
run saved by an earlier version resumes with empty metadata, so
`JOURNAL_VERSION` stays as it was. `runtime.pending()` and `onWait`
list each waiting call with its run's metadata, read from the run in
memory, or from its saved start for one that isn't.
@ben-reitz
ben-reitz force-pushed the browser/exec-metadata branch 3 times, most recently from 5205a37 to bcf2f52 Compare November 4, 2026 11:39
@ben-reitz
ben-reitz removed this pull request from stack #304 November 4, 2026 13:38
@ben-reitz
ben-reitz added this pull request to stack #306 November 4, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

allow-pr Allow a PR to remain open.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant