Reload working-tree combined diff views on external file changes (#6769)

* Reload working-tree combined diff views on external file changes

Combined diff views (e.g. "Changes" and "All Changes" tabs) display the
working-tree diffs for all modified files but do not match any single
file path. As a result, the file system watcher previously did not
notify them when individual files were updated.

Check for active working-tree combined diff tabs once per event payload,
and trigger a reload notification on file changes so the diff sections
refresh automatically when files are modified externally.

* Reload combined diff view when matching single-file tab is dirty

When an external file change occurs and the matching open file is dirty,
reloading the individual file tab is skipped. However, if a combined
diff view (like the uncommitted Changes tab) is open, it still needs
to be refreshed to reflect the external changes.

Ensure we schedule a debounced external reload for the combined diff
consumer even when there are dirty matching files.
This commit is contained in:
Jinjing
2026-06-29 11:27:09 -07:00
committed by GitHub
parent 5dd58d052d
commit eb9f0efb21
3 changed files with 123 additions and 0 deletions
@@ -57,6 +57,18 @@ export function isExternalReloadableEditorTab(file: OpenFile): boolean {
)
}
// Why: combined "Changes"/"All Changes" tabs render working-tree diffs but
// match no single path (their relativePath is a label, filePath the worktree
// root). The fs watcher must still notify them so terminal/agent edits reload
// the affected section. Branch/commit combined diffs compare committed refs, so
// working-tree changes don't affect them and are intentionally excluded.
export function isWorkingTreeCombinedDiffTab(file: OpenFile): boolean {
return (
file.mode === 'diff' &&
(file.diffSource === 'combined-uncommitted' || file.diffSource === 'combined-all')
)
}
export function canAutoSaveOpenFile(file: OpenFile): boolean {
// Why: single-file editors and one-file unstaged diffs have an unambiguous
// write target. Combined diff and conflict-review tabs can represent multiple
@@ -456,6 +456,96 @@ describe('createExternalWatchEventHandler tombstone coalescing', () => {
dispose()
})
it('reloads a combined Changes tab even though no single-file tab matches', () => {
const combinedDiffTab = {
id: 'wt-1::all-diffs::uncommitted',
worktreeId: 'wt-1',
worktreePath: '/repo',
filePath: '/repo',
relativePath: 'Changes',
mode: 'diff' as const,
diffSource: 'combined-uncommitted' as const,
isDirty: false
}
vi.mocked(useAppStore.getState).mockReturnValue({
openFiles: [combinedDiffTab],
setExternalMutation
} as never)
// Why: combined tabs match no single path, so the per-path lookup stays empty.
vi.mocked(getOpenFilesForExternalFileChange).mockReturnValue([])
const { handleFsChanged, dispose } = createExternalWatchEventHandler(findTarget)
handleFsChanged(payload([{ kind: 'update', absolutePath: '/repo/src/foo.ts' }]))
vi.advanceTimersByTime(100)
expect(notifyEditorExternalFileChange).toHaveBeenCalledWith({
worktreeId: 'wt-1',
worktreePath: '/repo',
relativePath: 'src/foo.ts',
runtimeEnvironmentId: null
})
dispose()
})
it('reloads a combined Changes tab when the matching single-file tab is dirty', () => {
const dirtyFile = {
...fileNotes,
isDirty: true
}
const combinedDiffTab = {
id: 'wt-1::all-diffs::uncommitted',
worktreeId: 'wt-1',
worktreePath: '/repo',
filePath: '/repo',
relativePath: 'Changes',
mode: 'diff' as const,
diffSource: 'combined-uncommitted' as const,
isDirty: false
}
vi.mocked(useAppStore.getState).mockReturnValue({
openFiles: [dirtyFile, combinedDiffTab],
setExternalMutation
} as never)
vi.mocked(getOpenFilesForExternalFileChange).mockReturnValue([dirtyFile] as never)
const { handleFsChanged, dispose } = createExternalWatchEventHandler(findTarget)
handleFsChanged(payload([{ kind: 'update', absolutePath: '/repo/notes.md' }]))
vi.advanceTimersByTime(100)
expect(notifyEditorExternalFileChange).toHaveBeenCalledWith({
worktreeId: 'wt-1',
worktreePath: '/repo',
relativePath: 'notes.md',
runtimeEnvironmentId: null
})
dispose()
})
it('does not reload a branch-compare combined diff for working-tree changes', () => {
const branchDiffTab = {
id: 'wt-1::all-diffs::branch',
worktreeId: 'wt-1',
worktreePath: '/repo',
filePath: '/repo',
relativePath: 'Branch Changes',
mode: 'diff' as const,
diffSource: 'combined-branch' as const,
isDirty: false
}
vi.mocked(useAppStore.getState).mockReturnValue({
openFiles: [branchDiffTab],
setExternalMutation
} as never)
vi.mocked(getOpenFilesForExternalFileChange).mockReturnValue([])
const { handleFsChanged, dispose } = createExternalWatchEventHandler(findTarget)
handleFsChanged(payload([{ kind: 'update', absolutePath: '/repo/src/foo.ts' }]))
vi.advanceTimersByTime(100)
expect(notifyEditorExternalFileChange).not.toHaveBeenCalled()
dispose()
})
it('tombstones only the matching owner for same-path delete events', () => {
const localFile = fileNotes
const runtimeFile = {
@@ -10,6 +10,7 @@ import { normalizeRuntimePathForComparison } from '../../../shared/cross-platfor
import {
getOpenFilesForExternalFileChange,
isExternalReloadableEditorTab,
isWorkingTreeCombinedDiffTab,
notifyEditorExternalFileChange
} from '@/components/editor/editor-autosave'
import {
@@ -608,6 +609,16 @@ export function createExternalWatchEventHandler(
// `useFileExplorerHandlers`. Read `openFiles` once per payload to avoid
// N store reads for large batched events.
const openFilesSnapshot = useAppStore.getState().openFiles
// Why: a combined "Changes" tab matches no single path but renders every
// changed file's working-tree diff. This is per-worktree, not per-path, so
// compute it once instead of rescanning openFiles for each changed file in
// a large batched payload (e.g. a branch switch touching hundreds of files).
const hasCombinedDiffConsumer = openFilesSnapshot.some(
(f) =>
f.worktreeId === target.worktreeId &&
openFileRuntimeOwner(f) === target.runtimeEnvironmentId &&
isWorkingTreeCombinedDiffTab(f)
)
for (const relativePath of changedFiles) {
const notification = {
worktreeId: target.worktreeId,
@@ -617,9 +628,19 @@ export function createExternalWatchEventHandler(
}
const matching = getOpenFilesForExternalFileChange(openFilesSnapshot, notification)
if (matching.length === 0) {
// Why: notify the combined-diff tab so its section reloads. Its own
// dirty/section guards make a blanket reload safe, and there is no
// in-memory editor content to clobber, so self-write suppression is
// unnecessary here.
if (hasCombinedDiffConsumer) {
scheduleDebouncedExternalReload(notification)
}
continue
}
if (matching.some((f) => f.isDirty)) {
if (hasCombinedDiffConsumer) {
scheduleDebouncedExternalReload(notification)
}
continue
}
const absolutePath = joinPath(notification.worktreePath, notification.relativePath)