fix(ai): cancel the deferred startup model sync when the plugin unloads - #1823
Conversation
The 5 s post-layout-ready auto-sync timer was never cleared, so disabling, reloading or updating QuickAdd within that window still ran a full model sync (including the models.dev download) from the unloaded instance. Register a cleanup that cancels it, and skip scheduling if the plugin unloads before layout-ready. Co-authored-by: Christian Bager Bach Houmann <christian@bagerbach.com>
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (3)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review. 📝 WalkthroughWalkthroughThe startup model sync now uses a scheduler that waits for layout readiness and then delays the sync by 5,000 ms. The scheduler clears a pending timer when the plugin unloads. Tests cover layout readiness, unload, and reload behavior. ChangesStartup model-sync scheduling
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to The pending startup sync is canceled on unload, with no supported issue remaining that would prevent merging after normal checks. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change reduces model-sync requests after the plugin is disabled or reloaded. It does not appear to add a new way to trigger synchronization, but work already running when the plugin unloads can still finish. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. A rabbit checks the layout light Comment |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Summary
QuickAdd schedules its background AI model sync 5 s after layout-ready with a bare
window.setTimeout.onunloadnever cancelled that timer. So if the plugin was disabled, reloaded or updated within those 5 s, the unloaded instance still ran a full model sync against the network, including a ~5 MB models.dev download. That happened even when the user had just turned QuickAdd off.Changes
src/ai/startupModelSync.ts:scheduleStartupModelSync(host, sync)keeps the same behaviour (5 s after layout-ready). It registers a cleanup through Obsidian'sComponent.register, so unloading the plugin cancels a pending run. If the plugin unloads before layout-ready, nothing is scheduled at all.src/main.tscalls it instead of the inlineonLayoutReady+setTimeout.Testing / validation
master. I used a temporary build that tagged each plugin instance and logged whether it was still the loaded one when its timer fired. Afterplugin:reloadand thenplugin:disable1 s later, the console showedfired; loaded=falsefollowed by a models.dev request, 4–5 s after QuickAdd was disabled (app.plugins.plugins.quickaddwasfalse).request-urlIPC boundary. Each plugin sync pass makes exactly one request, and the sync throttle was cleared before each step. The runs used the rawobsidianCLI, because theobsidian:e2ewrapper reloads the plugin before every command, which would add instances of its own.masterplugin:reloadplugin:reloads 1 s apartplugin:reload, thenplugin:disable1 s laterThe first row shows the live instance still syncs as before.
src/ai/startupModelSync.test.ts(4 tests, fake timers) covers: exactly one sync at 5 s after layout-ready (none before layout-ready, none at 4,999 ms), no sync when unloaded during the delay, no scheduling when unloaded before layout-ready, and only the live instance syncing after a reload. Mutation check: removing either theclearTimeoutor the unloaded guard fails 3 of the 4 tests.pnpm run test: 455 files passed, 5887 tests passed.pnpm run build-with-lint: clean.Checklist
Note
Cancel the deferred startup model sync on plugin unload
QuickAdd.onloadwithscheduleStartupModelSync, which registers unload cleanup that cancels the pending sync.STARTUP_MODEL_SYNC_DELAY_MS = 5000), and the delay is injectable for tests. See startupModelSync.ts and main.ts.Macroscope summarized e8923bc.
Summary by CodeRabbit