Skip to content

fix(core): invalidate AISDK caches when plugin hooks change - #67129

Open
imarshallwidjaja wants to merge 1 commit into
anomalyco:devfrom
imarshallwidjaja:aisdk-stale-cache
Open

imarshallwidjaja wants to merge 1 commit into
anomalyco:devfrom
imarshallwidjaja:aisdk-stale-cache

Conversation

@imarshallwidjaja

@imarshallwidjaja imarshallwidjaja commented Nov 29, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #67116

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

After a plugin hot reload, the AISDK location layer kept serving SDKs and language models built by the disposed plugin generation. The sdks/languages caches in packages/core/src/aisdk.ts lived for the life of the location layer, and disposing an sdk/language hook only removed the callback, so language() kept hitting the stale entries and never called the new generation's hook. (A full location reload rebuilds the layer, so this only showed up on in-place plugin reloads.)

The fix treats a cache entry as valid only for the hook set that was current when it was created:

  • The two maps are now one replaceable pair. Registering or disposing an sdk or language hook swaps in fresh maps in the same step as the hook-list update. Registering is included so a hook that loads late (plugins load asynchronously) supersedes entries cached before it existed.
  • language() captures the pair at entry and uses it for all reads and writes. A lookup that is in flight while hooks change still returns its result to its own caller, but writes into the retired pair instead of repopulating the live caches.

No feature flag: this is a bug fix for stale resolution, and the only behavior change is that model resolution re-runs the current hooks after they change.

How did you verify your code works?

Added five tests to packages/core/test/aisdk.test.ts: sdk hook disposal observed with no registration in between (falls back to an earlier hook), language hook disposal, disposal through scope close (the plugin reload finalizer path), a late-registered hook superseding a cached SDK, and an in-flight Deferred-gated lookup that must not re-cache (checked with both the same model and a second model sharing the SDK cache key). The new tests fail against the unmodified aisdk.ts and pass with the fix. I also checked they bite under targeted mutations: removing the dispose-time cache replacement fails the disposal tests, and writing through the live caches instead of the captured pair fails the in-flight test.

From packages/core:

  • bun run script/test.ts test/aisdk.test.ts — 49 pass
  • bun run script/test.ts test/plugin/supervisor.test.ts — 11 pass
  • bun run typecheck:tsc — clean

The push also ran the pre-push bun run check under Bun 1.4.4 (37/37 tasks).

Screenshots / recordings

N/A, no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #62068, #52333

Resolved SDKs and language models were cached for the life of the location layer, so after a plugin hot reload disposed its sdk or language hook, language() kept returning the disposed generation's entries and never called the replacement hook.

Every hook register or dispose now replaces the cache pair with fresh maps, and each language() call reads and writes only the pair it saw at entry, so an in-flight lookup from the old hook set cannot repopulate the live caches.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I ran the hot-reload reproduction from #67116 from source. On the base branch, after editing the plugin file, the new plugin's sdk hook was never called, and both the existing session and a new one failed with generation … was disposed (stale SDK reused). With this PR, the new plugin's hook runs and both turns are served by it. The five new tests fail against the base aisdk.ts and pass with the fix, and typecheck:tsc is clean.

Public API: unchanged. The only behaviour change is internal: the sdk/language model caches are reset whenever one of those hooks is registered or disposed.

I have no concerns that should block a merge.

Related: #62068, #52333

This branch has not been deployed

No deployments
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.

core: AISDK cache reuses the SDK from a disposed plugin generation after location reload

1 participant