-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: extension file reload — watch for plugin changes and hot-reload runtime #6347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ZijianZhang989
wants to merge
10
commits into
QwenLM:main
Choose a base branch
from
ZijianZhang989:feat/extension-file-reload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2a15d1a
feat: extension file reload — watch for plugin changes and hot-reload…
f2c7831
Merge branch 'main' into feat/extension-file-reload
yiliang114 b561072
fix: address extension reload review feedback
e7c97c9
docs: expand extension file reload design
4fb9d98
fix: harden extension reload watcher state
9a1380c
fix(core): tag extension refresh legs
b7482bd
fix(cli): harden extension reload state handling
6075048
fix(cli): clarify extension reload failure state
d79c005
fix(cli): tighten extension reload boundaries
6c38cae
merge: integrate origin/main into feat/extension-file-reload
qwen-code-dev-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,304 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import { watch as watchFs, type FSWatcher } from 'chokidar'; | ||
| import { | ||
| createDebugLogger, | ||
| isSubpath, | ||
| Storage, | ||
| type Config, | ||
| type ExtensionMutationEvent, | ||
| } from '@qwen-code/qwen-code-core'; | ||
| import { ExtensionRefreshState } from './extension-refresh-state.js'; | ||
|
|
||
| const debugLogger = createDebugLogger('EXTENSION_FILE_WATCHER'); | ||
|
|
||
| const TOP_LEVEL_FILES = new Set([ | ||
| 'extension-enablement.json', | ||
| 'extension-preferences.json', | ||
| 'marketplaces.json', | ||
| ]); | ||
|
|
||
| const EXTENSION_FILES = new Set([ | ||
| 'qwen-extension.json', | ||
| '.qwen-extension-install.json', | ||
| ]); | ||
|
|
||
| const AUTO_REFRESH_DIRS = new Set(['commands', 'skills', 'agents']); | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| const STALE_DIRS = new Set(['hooks']); | ||
|
|
||
| type WatchEvent = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'; | ||
| type RefreshAction = 'auto' | 'stale'; | ||
|
|
||
| export class ExtensionFileWatcher { | ||
| private watcher?: FSWatcher; | ||
| private bootstrapWatcher?: FSWatcher; | ||
| private mutationListenerDisposer?: () => void; | ||
| private mutationSuppressionEnds = new Map<number, () => void>(); | ||
| private contextFiles = new Set<string>(); | ||
| private watching = false; | ||
| private watchGeneration = 0; | ||
|
|
||
| constructor( | ||
| private readonly config: Config, | ||
| private readonly extensionsDir = Storage.getUserExtensionsDir(), | ||
| private readonly refreshState = new ExtensionRefreshState(), | ||
| ) {} | ||
|
|
||
| startWatching(): void { | ||
| this.stopWatching(); | ||
| this.watching = true; | ||
| const generation = ++this.watchGeneration; | ||
| this.subscribeExtensionManagerMutations(); | ||
| this.contextFiles = this.getContextFiles(); | ||
| const roots = this.getWatchRoots(); | ||
|
|
||
| if (roots.length > 0) { | ||
| this.watcher = watchFs(roots, { | ||
| ignoreInitial: true, | ||
| followSymlinks: false, | ||
| awaitWriteFinish: { | ||
| stabilityThreshold: 200, | ||
| pollInterval: 50, | ||
| }, | ||
| ignored: (filePath: string) => this.isIgnored(filePath), | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| }) | ||
| .on('all', (event: string, changedPath: string) => { | ||
| if (generation !== this.watchGeneration) return; | ||
| const action = this.getRefreshAction( | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| event as WatchEvent, | ||
| path.resolve(changedPath), | ||
| ); | ||
| if (action === 'auto') { | ||
| this.refreshState.markExtensionContentChanged( | ||
| 'extension content files changed', | ||
| ); | ||
| } else if (action === 'stale') { | ||
| this.refreshState.markExtensionsChanged('extension files changed'); | ||
| } | ||
| }) | ||
| .on('error', (error: unknown) => { | ||
| debugLogger.warn('Extension file watcher error:', error); | ||
| }); | ||
| } | ||
|
|
||
| if (!fs.existsSync(this.extensionsDir)) { | ||
| this.watchExtensionsParent(); | ||
| } | ||
| } | ||
|
|
||
| stopWatching(): void { | ||
| const watcher = this.watcher; | ||
| const bootstrapWatcher = this.bootstrapWatcher; | ||
| this.watcher = undefined; | ||
| this.bootstrapWatcher = undefined; | ||
| this.watching = false; | ||
| this.watchGeneration++; | ||
| this.mutationListenerDisposer?.(); | ||
| this.mutationListenerDisposer = undefined; | ||
| this.endPendingMutationSuppressions(); | ||
| watcher?.close().catch((error: unknown) => { | ||
| debugLogger.warn('Extension file watcher close error:', error); | ||
| }); | ||
| bootstrapWatcher?.close().catch((error: unknown) => { | ||
| debugLogger.warn('Extension bootstrap watcher close error:', error); | ||
| }); | ||
| } | ||
|
|
||
| restartWatching(): void { | ||
| this.startWatching(); | ||
| } | ||
|
|
||
| private getWatchRoots(): string[] { | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| const roots = new Set<string>(); | ||
| if (fs.existsSync(this.extensionsDir)) { | ||
| roots.add(this.extensionsDir); | ||
| } | ||
| for (const extension of this.config.getExtensions()) { | ||
| if (extension.installMetadata?.type === 'link') { | ||
| const rawSource = extension.installMetadata.source; | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| const source = rawSource ? path.resolve(rawSource) : undefined; | ||
| if (source && fs.existsSync(source)) { | ||
| roots.add(source); | ||
| } | ||
| } | ||
| } | ||
| return [...roots]; | ||
| } | ||
|
|
||
| private getContextFiles(): Set<string> { | ||
| const files = new Set<string>(); | ||
| for (const extension of this.config.getExtensions()) { | ||
| for (const filePath of extension.contextFiles) { | ||
| files.add(path.resolve(filePath)); | ||
| } | ||
| const configured = extension.config.contextFileName; | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| const names = | ||
| configured === undefined | ||
| ? ['QWEN.md'] | ||
| : Array.isArray(configured) | ||
| ? configured | ||
| : [configured]; | ||
| for (const name of names) { | ||
| files.add(path.resolve(extension.path, name)); | ||
| } | ||
| } | ||
| return files; | ||
| } | ||
|
|
||
| private watchExtensionsParent(): void { | ||
| const parentDir = path.dirname(this.extensionsDir); | ||
| const dirBasename = path.basename(this.extensionsDir); | ||
| const generation = this.watchGeneration; | ||
| this.bootstrapWatcher = watchFs(parentDir, { | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| ignoreInitial: true, | ||
| followSymlinks: false, | ||
| depth: 0, | ||
| ignored: (filePath: string) => | ||
| filePath !== parentDir && path.basename(filePath) !== dirBasename, | ||
| }) | ||
| .on('all', (_event: string, changedPath: string) => { | ||
| if (generation !== this.watchGeneration) return; | ||
| if (path.basename(changedPath) !== dirBasename) return; | ||
| if (!fs.existsSync(this.extensionsDir)) return; | ||
| this.refreshState.markExtensionsChanged('extension directory created'); | ||
| queueMicrotask(() => { | ||
| if (this.watching) { | ||
| this.restartWatching(); | ||
| } | ||
| }); | ||
| }) | ||
| .on('error', (error: unknown) => { | ||
| debugLogger.warn('Extension bootstrap watcher error:', error); | ||
| }); | ||
| } | ||
|
|
||
| private getRefreshAction( | ||
| event: WatchEvent, | ||
| changedPath: string, | ||
| ): RefreshAction | false { | ||
| if (this.contextFiles.has(changedPath)) { | ||
| return 'stale'; | ||
| } | ||
| if (changedPath === path.resolve(this.extensionsDir)) { | ||
| if (event === 'unlinkDir') { | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| this.watchExtensionsParent(); | ||
| return 'stale'; | ||
| } | ||
| return false; | ||
| } | ||
| if (isSubpath(this.extensionsDir, changedPath)) { | ||
| return this.getUserExtensionRefreshAction(event, changedPath); | ||
| } | ||
| return this.getLinkedExtensionRefreshAction(changedPath); | ||
| } | ||
|
|
||
| private getUserExtensionRefreshAction( | ||
| event: WatchEvent, | ||
| changedPath: string, | ||
| ): RefreshAction | false { | ||
| const relative = path.relative(this.extensionsDir, changedPath); | ||
| const parts = relative.split(path.sep).filter(Boolean); | ||
| if (parts.length === 1) { | ||
| if (TOP_LEVEL_FILES.has(parts[0])) return 'stale'; | ||
| if (event === 'addDir' || event === 'unlinkDir') return 'stale'; | ||
| return false; | ||
| } | ||
| if ( | ||
| !fs.existsSync( | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| path.join(this.extensionsDir, parts[0], 'qwen-extension.json'), | ||
| ) | ||
| ) { | ||
| return 'stale'; | ||
| } | ||
| const runtimePath = parts.slice(1); | ||
| return this.getRuntimePathRefreshAction(runtimePath); | ||
| } | ||
|
|
||
| private getLinkedExtensionRefreshAction( | ||
| changedPath: string, | ||
| ): RefreshAction | false { | ||
| for (const extension of this.config.getExtensions()) { | ||
| if (extension.installMetadata?.type !== 'link') continue; | ||
| const rawSource = extension.installMetadata.source; | ||
| const source = rawSource ? path.resolve(rawSource) : undefined; | ||
| if (!source || !isSubpath(source, changedPath)) continue; | ||
| const relative = path.relative(source, changedPath); | ||
| const parts = relative.split(path.sep).filter(Boolean); | ||
| return parts.length === 0 | ||
| ? 'stale' | ||
| : this.getRuntimePathRefreshAction(parts); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private getRuntimePathRefreshAction(parts: string[]): RefreshAction | false { | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| if (EXTENSION_FILES.has(parts[0]) || STALE_DIRS.has(parts[0])) { | ||
| return 'stale'; | ||
| } | ||
| if (AUTO_REFRESH_DIRS.has(parts[0])) { | ||
| return 'auto'; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private isIgnored(filePath: string): boolean { | ||
| const normalized = filePath.replace(/\\/g, '/'); | ||
| const searchablePath = `/${normalized}/`; | ||
| if ( | ||
| searchablePath.includes('/node_modules/') || | ||
| searchablePath.includes('/.git/') | ||
| ) { | ||
| return true; | ||
| } | ||
| const basename = normalized.split('/').pop() ?? ''; | ||
| return ( | ||
| basename === '.DS_Store' || | ||
| basename.endsWith('~') || | ||
| basename.endsWith('.swp') || | ||
| basename.endsWith('.tmp') | ||
| ); | ||
| } | ||
|
|
||
| private subscribeExtensionManagerMutations(): void { | ||
| const manager = this.config.getExtensionManager(); | ||
| this.mutationListenerDisposer = manager.addMutationListener( | ||
| (event: ExtensionMutationEvent) => { | ||
| if (event.phase === 'start') { | ||
| this.mutationSuppressionEnds.set( | ||
| event.id, | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| this.refreshState.beginSuppression(() => | ||
| this.restartAfterMutation(), | ||
|
ZijianZhang989 marked this conversation as resolved.
|
||
| ), | ||
| ); | ||
| return; | ||
| } | ||
| const endSuppression = this.mutationSuppressionEnds.get(event.id); | ||
| if (!endSuppression) { | ||
| return; | ||
| } | ||
| this.mutationSuppressionEnds.delete(event.id); | ||
| endSuppression(); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| private endPendingMutationSuppressions(): void { | ||
| const endSuppressions = [...this.mutationSuppressionEnds.values()]; | ||
| this.mutationSuppressionEnds.clear(); | ||
| for (const endSuppression of endSuppressions) { | ||
| endSuppression(); | ||
| } | ||
| } | ||
|
|
||
| private restartAfterMutation(): void { | ||
| if (this.watching) { | ||
| this.restartWatching(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.