GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing github.com — fix(core): rebuild registry state on read by kitlangton · Pull Request #46725 · anomalyco/opencode Conversation
|
| Operation | Behavior |
|---|---|
| Register a transform | Append it to the ordered pipeline and mark the State changed |
| Read after any change, including inside a batch | Rebuild into a fresh value from all active transforms; return it |
| Read again without changes | Return the same value, no replay |
Remove a registration or reload() |
Mark changed; the next read rebuilds from the remaining transforms |
| Callback throws during a rebuild | Propagate the error; the previous complete value stays visible and the State stays changed |
| Finish a batch | Materialize once, then notify each affected domain once |
Every rebuild produces a new value and never touches earlier ones, so a caller may hold what it read across awaits without copying. Tool request snapshots and MCP reconciliation, which retain registry values across time, no longer need their own copies.
sequenceDiagram
participant Plugin
participant State
participant Observer as Domain observer
Note over Plugin,State: Inside State.batch
Plugin->>State: transform(A)
Plugin->>State: get()
State-->>Plugin: rebuild {A} → value₁
Plugin->>State: transform(B)
Plugin->>State: get()
State-->>Plugin: rebuild {A, B} → value₂ (value₁ unchanged)
Note over Plugin,State: Batch finishes
State->>State: get() — unchanged, returns value₂
State->>Observer: notify once
Reads never run notifications or resource I/O. Outside a batch, registration and disposal notify immediately; batches coalesce those notifications; reload keeps its 500 ms trailing-edge debounce. Notification always materializes first, so a broken callback fails the batch that admitted it rather than a later reader.
A batch is not a transaction: cancellation can stop observer work without rolling back accepted registrations. Ordinary observer failures are aggregated while the other domains are still attempted.
An earlier revision of this PR kept an applied-prefix cursor and ran only newly registered callbacks on each read. That made the returned value a live object the State kept mutating, which forced explicit copies at every consumer that retains a read across an await and a contract reviewers had to know.
Measured on a real cold start with an isolated HOME (17 States, 50 reads, 27 of them after changes), the incremental design saved about 20 ms of a ~360 ms activation once #46710 removes the one expensive catalog callback, and about 10 ms per rare background model-discovery event. Transform counts are per plugin, not per configuration item (max 23 on the catalog), so the rebuild cost does not scale with user configuration. Full rebuild removes the cursor, retained-draft semantics, a retry-from-partial-state path, and both consumer copies for that cost.
finalize(draft) with a notify: Effect<void> value. Materialization is now get()'s job, so the hook only observes; no production finalizer needed to mutate its draft. Effects are lazy, so the value form drops a thunk and lets a batch dedupe one notification per State by identity. Hooks that read mutable closure state in argument position wrap in Effect.suspend (tool.ts).State.batch(effect, { flush: false }) into State.shutdown(effect). It was a one-way door (States close permanently and never notify again, including debounced reloads already waiting), which a boolean named flush did not convey. batch no longer takes options.Fresh reads made an existing ACP boundary observable in CI: ACP accepted any available model and primary agent as a complete catalog and cached that partial result per directory, so it could pin an ambient built-in model before configured providers had activated. That fix is its own PR, #46682, which this PR is stacked on. The Windows-flake test fixes in #46610 (shell retention and slow npm install cases) sit beneath both because the same Windows job blocked this stack's CI.
v2
└─ #46610 test(core): make shell retention regression deterministic
└─ #46682 fix(cli): await plugin activation before caching ACP catalog
└─ #46725 fix(core): rebuild registry state on read ← this PR
A mechanical Draft → Editor rename of the State editing interfaces was prepared but is deliberately left out so this diff stays reviewable; it can follow as a separate PR if wanted.
This is a new alternative to #45810 and #45822, based on v2. Registry reads rebuild from the ordered transform history when something changed since the last read. Plugin registration signatures remain unchanged; captured source changes still require reload(). Notification equality and a new plugin startup phase are not part of this change.
Review also reproduced two pre-existing lifecycle limits outside this change: an MCP status listener awaiting another MCP transform can deadlock under the lifecycle permit, and a blocked filesystem-watcher target lookup can delay shutdown. This PR does not claim to fix either behavior.
# packages/core bun run test bun run test test/state-replay.test.ts bun run test test/state.test.ts test/integration.test.ts test/plugin.test.ts --rerun-each 10 bun typecheck # packages/server bun run ../core/script/test.ts bun typecheck # packages/cli bun run test bun run test test/acp/config-options.subprocess.test.ts test/acp/prompt-content.subprocess.test.ts test/acp/skills.subprocess.test.ts --rerun-each 5 bun typecheck # packages/plugin bun run test bun typecheck
Plugin.activate OAuth regression fails on clean base ce6247bd2f with the expired credential and zero refreshes; the identical test passes here.Two independent diff reviews both found the model-resolution freezing issue; a follow-up review verified the correction. Effect-specific reviews compared the pinned 4.0.0-rc.112 implementation with freshly fetched upstream 5641ad333; the relevant primitives have equivalent semantics, and no dependency upgrade was needed.
The property suite uses noncommutative operations and checks the real State implementation against a full-fold specification. FastCheck shrinking reports minimal counterexamples. The second property checks that unchanged reads do no work, that each change replays exactly the active callbacks once, and that retained values are never mutated.
Additional local CLI smoke verification uses normal server startup, device login, a naturally expired token, and a fresh server process. The loopback Console fixture records refresh before the first cold-start config request, followed by config 200, a visible fixture model, and an advanced expiry under the same credential ID. Tokens and endpoints are synthetic; no live service or real account is involved.
An additional isolated SDK suite has 22 passed and 4 failed on both this branch and clean base fd73a85de4. The first-generation explicit/default model tests, plugin-backed web search, and Promise event-cancellation expectation fail identically on the base; normalized logs match and each case reproduces in repeated baseline runs. They are pre-existing and are not changed by this PR.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Registry reads returned the value from the last completed fold, so a plugin reading a registry during activation saw the previous batch. An OAuth method could be registered but unreadable until the batch ended, and resolving an expired credential during setup skipped its refresh implementation.
State.get() now rebuilds synchronously whenever registrations changed, replaying every active transform onto a fresh value from initial(). Unchanged States return the previous value. Each rebuild is a new object and earlier values are never mutated, so consumers may retain what they read; Tool request snapshots and MCP reconciliation drop their copies.
finalize(draft) becomes a notify effect that only observes; materialization is get()'s job, so a throwing callback fails the batch that admitted it. State.batch(effect, { flush: false }) becomes State.shutdown(effect). Model resolution stops using Immer, whose freezing broke later mutation of catalog-owned objects.
Measured on a cold start, an incremental-prefix variant saved about 20 ms of a ~360 ms activation; full rebuild removes the cursor, retained-draft semantics, and consumer copies for that cost.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.