Commit db34bc6
fix(core, mastracode): silence provider cache corruption warning and consolidate gateway sync (#16332)
## Description
Two related fixes for the noisy `[GatewayRegistry] Detected invalid
provider-types in global cache …` / `Detected corrupted global cache …`
warnings that show up when running `mastracode` and `mastra dev` against
the same `~/.cache/mastra/` directory.
### 1. Stop logging on auto-recoverable cache corruption
(`@mastra/core`)
In `packages/core/src/llm/model/provider-registry.ts >
syncGlobalCacheToLocal()`:
- Removed the three `console.warn` calls that fired on corrupted JSON,
corrupted `.d.ts`, or any thrown error during sync.
- Behavior is otherwise unchanged — the corrupted file is still deleted
so it can't propagate into a project's `dist/`, and the next gateway
sync writes a valid file. The warnings were just noise (most commonly
triggered by an older `@mastra/core` in another project's `node_modules`
writing a cache file without the digit-quoting fix).
- Added an explicit `expect(warnSpy).not.toHaveBeenCalled()` assertion
to the existing "delete corrupted JSON" test to lock this in.
### 2. Consolidate gateway sync (`mastracode`)
`mastracode/src/utils/gateway-sync.ts` was a near-duplicate of
`@mastra/core`'s `GatewayRegistry.syncGateways` — it had its own
`fetchProvidersFromGateways`, `generateTypesContent`, `atomicWriteFile`,
and a separate `~/.cache/mastra/gateway-refresh-time` timestamp file.
Two independent code paths writing to the same global cache made it
harder to reason about and made it possible for the two implementations
to drift (e.g. only one having the digit-quoting fix).
`gateway-sync.ts` is now a thin wrapper that:
- Calls `GatewayRegistry.getInstance({ useDynamicLoading: true
}).syncGateways(true)` for the actual fetch/generate/write.
- Keeps the existing public surface (`syncGateways`, `startGatewaySync`,
`stopGatewaySync`) and the 5-minute skip-if-recently-synced behavior,
but uses `getLastRefreshTime()` from the registry instead of a separate
timestamp file.
Tests under `mastracode/src/utils/__tests__/gateway-sync.test.ts` were
rewritten to cover the wrapper behavior (delegation, force flag,
skip-if-recent, error swallowing, periodic timer).
## Related Issue(s)
Internal Slack thread; no GitHub issue.
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [x] Code refactoring
- [x] Test update
## Reviewer notes / risks to look at
- **Behavioral subtlety in mastracode:** the old code persisted
last-sync time to disk (`~/.cache/mastra/gateway-refresh-time`), so the
5-minute skip survived process restarts. The new wrapper uses
`GatewayRegistry.getLastRefreshTime()` which is in-memory per process.
In practice this means a fresh `mastracode` start will always do one
sync, then back off for 5 minutes. This matches `mastra dev`'s behavior
and seems desirable, but flagging it explicitly.
- **`MastraGateway` enable check:** the previous mastracode code passed
an explicit gateway list. Core's `GatewayRegistry.syncGateways`
constructs the same default set (`ModelsDevGateway`, `NetlifyGateway`,
`MastraGateway`) and each gateway's own `shouldEnable()` still gates
whether it runs, so the `MASTRA_GATEWAY_API_KEY` skip is preserved — but
worth double-checking against
`packages/core/src/llm/model/provider-registry.ts >
GatewayRegistry.syncGateways`.
- **No end-to-end verification:** I ran the focused unit tests
(`provider-registry.test.ts`, `registry-generator.test.ts`, new
`gateway-sync.test.ts`) and `pnpm --filter @mastra/core check`, but did
not reproduce the original "corruption" scenario with both `mastracode`
and `mastra dev` running side-by-side. The repro depends on having an
older `@mastra/core` write to the shared cache, which is awkward to set
up locally.
- **Pre-existing failures unrelated to this PR:**
`mastracode/src/__tests__/index.test.ts` and `pnpm --filter mastracode
check` fail on `main` too (unbuilt sibling packages like
`@mastra/duckdb`); not introduced here.
## Checklist
- [x] I have made corresponding changes to the documentation (if
applicable — changeset added)
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have addressed all Coderabbit comments on this PR
Link to Devin session:
https://app.devin.ai/sessions/45161f2995e142368556aad18f290a9d
Requested by: @TylerBarnes
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## ELI5
This PR stops noisy warning messages when old cache files are
auto-recovered and makes mastracode reuse the core gateway-sync logic so
there’s one canonical implementation for syncing gateways.
---
## Changes
### packages/core - Silence cache corruption warnings
- packages/core/src/llm/model/provider-registry.ts: Removed three
`console.warn` calls in syncGlobalCacheToLocal() that previously logged
cache-corruption warnings for corrupted JSON, corrupted `.d.ts`, or any
thrown error during sync. Corrupted cache files are still deleted so
they can't propagate; the next gateway sync rewrites valid files.
- packages/core/src/llm/model/provider-registry.test.ts: Updated test to
assert no warning is emitted when corrupted JSON is deleted
(`expect(warnSpy).not.toHaveBeenCalled()`).
### mastracode - Consolidate gateway sync
- mastracode/src/utils/gateway-sync.ts: Replaced the near-duplicate
standalone implementation with a thin wrapper that delegates to
GatewayRegistry.getInstance({ useDynamicLoading: true
}).syncGateways(true). Preserves public API (`syncGateways`,
`startGatewaySync`, `stopGatewaySync`) and the 5-minute skip-if-recent
behavior, but now uses the registry’s in-memory getLastRefreshTime()
instead of a ~/.cache/mastra/gateway-refresh-time file. Removed
duplicated filesystem, provider-aggregation, and type-generation logic.
- mastracode/src/utils/__tests__/gateway-sync.test.ts: Rewrote tests to
cover delegation, force flag, skip-if-recent behavior, error swallowing
(logged with `[GatewaySync]`), and periodic timer start/stop. Removed
prior tests that duplicated provider-fetch/type-generation behavior now
handled by core.
### Changesets
- .changeset/silence-provider-cache-corruption-warning.md and
.changeset/mastracode-gateway-sync-delegation.md: Patch changesets
documenting the warning silencing and delegation.
---
## Reviewer notes / behavioral impacts
- The 5-minute cooldown is now in-memory per process (no longer
persisted across restarts).
- MastraGateway enablement remains governed by core’s shouldEnable
logic.
- Corrupted cache files are still deleted automatically; warnings are
silenced for auto-recoverable cases.
- Focused unit tests were added; no end-to-end concurrent mastracode +
mastra dev reproduction was performed.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: tyler <tylerdbarnes@gmail.com>
Co-authored-by: Mastra Code (openai/gpt-5.5) <noreply@mastra.ai>1 parent 9260e01 commit db34bc6
6 files changed
Lines changed: 145 additions & 310 deletions
File tree
- .changeset
- mastracode/src/utils
- __tests__
- packages/core/src/llm/model
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
20 | 25 | | |
21 | 26 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
26 | 51 | | |
27 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
28 | 55 | | |
29 | 56 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
111 | 73 | | |
112 | 74 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
117 | 101 | | |
118 | | - | |
119 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
120 | 105 | | |
121 | 106 | | |
0 commit comments