-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: stricter file caching #2844
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -549,42 +549,20 @@ export class SandboxManager { | |||||||||||||||||||||||||||||||||||||||
| async handleFileChangedEvent(normalizedPath: string) { | ||||||||||||||||||||||||||||||||||||||||
| const cachedFile = this.fileSync.readCache(normalizedPath); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (isImageFile(normalizedPath)) { | ||||||||||||||||||||||||||||||||||||||||
| if (!cachedFile || cachedFile.content === null) { | ||||||||||||||||||||||||||||||||||||||||
| // If the file was not cached, we need to write an empty file | ||||||||||||||||||||||||||||||||||||||||
| this.fileSync.writeEmptyFile(normalizedPath, 'binary'); | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| // If the file was already cached, we need to read the remote file and update the cache | ||||||||||||||||||||||||||||||||||||||||
| const remoteFile = await this.readRemoteFile(normalizedPath); | ||||||||||||||||||||||||||||||||||||||||
| if (!remoteFile || remoteFile.content === null) { | ||||||||||||||||||||||||||||||||||||||||
| console.error(`File content for ${normalizedPath} not found in remote`); | ||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| this.fileSync.updateCache(remoteFile); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| // If the file is not an image, we need to read the remote file and update the cache | ||||||||||||||||||||||||||||||||||||||||
| const remoteFile = await this.readRemoteFile(normalizedPath); | ||||||||||||||||||||||||||||||||||||||||
| if (!remoteFile || remoteFile.content === null) { | ||||||||||||||||||||||||||||||||||||||||
| console.error(`File content for ${normalizedPath} not found in remote`); | ||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| if (remoteFile.type === 'text') { | ||||||||||||||||||||||||||||||||||||||||
| // If the file is a text file, we need to process it for mapping | ||||||||||||||||||||||||||||||||||||||||
| this.fileSync.updateCache({ | ||||||||||||||||||||||||||||||||||||||||
| type: 'text', | ||||||||||||||||||||||||||||||||||||||||
| path: normalizedPath, | ||||||||||||||||||||||||||||||||||||||||
| content: remoteFile.content, | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| if (remoteFile.content !== cachedFile?.content) { | ||||||||||||||||||||||||||||||||||||||||
| await this.processFileForMapping(remoteFile); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| this.fileSync.updateCache({ | ||||||||||||||||||||||||||||||||||||||||
| type: 'binary', | ||||||||||||||||||||||||||||||||||||||||
| path: normalizedPath, | ||||||||||||||||||||||||||||||||||||||||
| content: remoteFile.content, | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| // Always read the remote file and update the cache, regardless of file type | ||||||||||||||||||||||||||||||||||||||||
| const remoteFile = await this.readRemoteFile(normalizedPath); | ||||||||||||||||||||||||||||||||||||||||
| if (!remoteFile) { | ||||||||||||||||||||||||||||||||||||||||
| console.error(`File content for ${normalizedPath} not found in remote`); | ||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Always update the cache with the fresh remote file content | ||||||||||||||||||||||||||||||||||||||||
| this.fileSync.updateCache(remoteFile); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // For text files, also process for mapping if content has changed | ||||||||||||||||||||||||||||||||||||||||
| if (remoteFile.type === 'text' && this.isJsxFile(normalizedPath)) { | ||||||||||||||||||||||||||||||||||||||||
| if (remoteFile.content !== cachedFile?.content) { | ||||||||||||||||||||||||||||||||||||||||
| await this.processFileForMapping(remoteFile); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -678,6 +656,12 @@ export class SandboxManager { | |||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Read and cache the copied file | ||||||||||||||||||||||||||||||||||||||||
| const copiedFile = await this.readRemoteFile(normalizedTargetPath); | ||||||||||||||||||||||||||||||||||||||||
| if (copiedFile) { | ||||||||||||||||||||||||||||||||||||||||
| this.fileSync.updateCache(copiedFile); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+659
to
+664
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copying directories leaves cache stale; handle directory targets After Apply this diff: - // Read and cache the copied file
- const copiedFile = await this.readRemoteFile(normalizedTargetPath);
- if (copiedFile) {
- this.fileSync.updateCache(copiedFile);
- }
+ // Sync cache for the copy target (file or directory)
+ const stat = await this.session.provider.statFile({
+ args: { path: normalizedTargetPath },
+ });
+ if (stat?.type === 'directory') {
+ this.fileSync.updateDirectoryCache(normalizedTargetPath);
+ // Optional: list + seed child entries if provider doesn't emit add events for children.
+ // (Rely on watcher by default.)
+ } else {
+ const copiedFile = await this.readRemoteFile(normalizedTargetPath);
+ if (copiedFile) {
+ this.fileSync.updateCache(copiedFile);
+ }
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||
| console.error(`Error copying ${path} to ${targetPath}:`, error); | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -744,6 +728,7 @@ export class SandboxManager { | |||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Note: Cache update handled by file watcher rename event | ||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||
| console.error(`Error renaming file ${oldPath} to ${newPath}:`, error); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic error: The code returns early if
remoteFileis falsy, but the comment on line 552 states it should 'always update cache regardless of file type'. This creates inconsistent behavior where some file change events are silently ignored. The function should either handle the null case appropriately (perhaps by removing the file from cache) or the early return should be reconsidered to match the stated behavior.Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.