GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): copy models.dev snapshot without structuredClone by kitlangton · Pull Request #46710 · anomalyco/opencode

Conversation

@kitlangton

@kitlangton kitlangton commented Sep 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Why

The models.dev plugin registers every snapshot model into the catalog, and the catalog owns and mutates those records, so each run needs fresh copies. It made them with structuredClone per model — about 6,300 calls over 3.6 MB of data — costing roughly 40 ms every time the catalog callback runs. During a cold start that callback runs more than once, so this was the single most expensive piece of catalog work on the startup path.

What Changes

Snapshot data is Schema-decoded plain JSON (strings, branded numbers, nested records, arrays), so a small recursive copy produces identical fresh objects without structuredClone's general graph walk. The same helper replaces the one-time whole-snapshot clone in snapshots(...).

The copy walks own keys only and defines a literal __proto__ key explicitly, since plain assignment would set the prototype instead of an own property and a second copy would then hoist those nested fields into the request body. This matches structuredClone's behavior for JSON bodies that contain that key.

per catalog callback run
Before 39–55 ms (median ≈ 41)
After 6–11 ms (median ≈ 7)

Measured inside a real opencode serve process with an isolated HOME, five servers per revision, each running the callback twice during startup.

Isolated comparison on the bundled snapshot (6,279 models): structuredClone per model 28.8 ms; whole-snapshot structuredClone 18.0 ms; Object.fromEntries copy 9.7 ms; recursive plain copy with the __proto__ guard 2.9 ms. The cost is data volume, not call overhead, which is why cloning once was not enough.

Scope

One file. Catalog semantics are unchanged: every rebuild still gets fresh, independently mutable copies of the snapshot. No public interface changes. Independent of the State materialization stack (#46610 → #46682 → #46631); it reduces catalog rebuild cost under any State design.

Verification

# packages/core
bun typecheck
bun run test test/catalog.test.ts test/plugin/models-dev.test.ts test/provider.test.ts
  • Typecheck passes; catalog, models.dev, and provider tests: 24 passed, 0 failed.
  • New regression copies model request bodies without reinterpreting literal __proto__ keys fails on the initial for...in copy and passes with the guard.
  • In-process timing above was collected with a temporary performance.now() around the callback on both revisions; the timing code is not in the diff.
  • The pre-push hook passed all 33 repository typecheck tasks.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kitlangton
fix(core): copy models.dev snapshot without structuredClone
3dafe78
The models.dev catalog callback cloned each of the ~6,300 snapshot models with structuredClone on every run, costing about 40 ms per catalog rebuild during startup. Snapshot data is plain JSON, so a direct recursive copy preserves the same fresh-copy semantics at roughly 7 ms per run.
@github-actions github-actions Bot added the contributor label Sep 2, 2026
@kitlangton kitlangton mentioned this pull request Sep 2, 2026
fix(core): rebuild registry state on read #46631
Closed
@kitlangton
fix(core): preserve literal __proto__ keys when copying snapshot models
d780f69
Assigning that key into a fresh object sets its prototype instead of an own property, so a second copy hoisted the nested fields into the request body. Define it explicitly and copy own keys only, matching structuredClone. Adds a regression covering both copy stages.
@kitlangton
kitlangton merged commit e327f93 into v2 Sep 2, 2026
10 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@kitlangton
kitlangton deleted the models-dev-copy branch September 2, 2026 03:07
This was referenced Sep 2, 2026
fix(core): rebuild registry state on read #46725
Closed
fix(core): rebuild registry state on read #46825
Merged
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 2026
@kitlangton
fix(core): copy models.dev snapshot without structuredClone (anomalyc…
96c18d8
…o#46710)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

contributor

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@kitlangton
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.