perf(file-provider): read a new directory inside a pinned folder in the same scan

The working-set scan only descends into a changed child directory when
something inside it is already materialised, which keeps one push from
walking whole never-visited subtrees on a sparse domain. A directory
created on the server inside a pinned folder can never pass that test,
because nothing inside it is known yet, so the extension did not report
its contents. fileproviderd still lists the pinned folder and downloads
its files on its own background cycle, which took about a minute in a
manual test and can take longer when macOS defers background work.

"Always keep downloaded" asks for the whole subtree, so the scan now
descends when the child carries the pin, and the files are reported in
the same scan. The bound holds everywhere else.

The pin is read from the stored row, because the read applies the
parent's pin to a new row on its own copy, so the metadata it hands back
still has the value from before.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Julius van der Vaart <[email protected]>
This commit is contained in:
Julius van der VaartandClaude Opus 5.5 committed 2026-11-06 13:23:50 +01:00
1 parent baf2df9aa3
commit 1e8c855a66
2 files changed
+78 -13

No files matched your search

@@ -150,12 +150,12 @@ extension Enumerator {
}
// Work queue seeded with the materialised items. A changed child directory discovered while
// scanning is appended ONLY when its subtree actually contains a materialised item, so its
// changed descendants are visited too — otherwise a depth-1 read of a visited folder surfaces
// the changed subdirectory but never the changed items inside it. A changed subdirectory whose
// subtree holds nothing materialised is NOT enqueued: nothing inside it is part of the working
// set, so there is nothing to keep in sync, and its contents are read lazily when the user
// navigates into it. Without that bound a single working-set signal on a sparse / freshly
// scanning is appended ONLY when its subtree actually contains a materialised item or the
// directory is pinned, so its changed descendants are visited too — otherwise a depth-1 read of
// a visited folder surfaces the changed subdirectory but never the changed items inside it. A
// changed, unpinned subdirectory whose subtree holds nothing materialised is NOT enqueued:
// nothing inside it is part of the working set, so there is nothing to keep in sync, and its
// contents are read lazily when the user navigates into it. Without that bound a single working-set signal on a sparse / freshly
// activated domain triggers a full recursive PROPFIND of every changed branch down to its
// leaves (every never-enumerated descendant looks "new"), hammering the server. Unchanged
// subtrees are likewise never enqueued, so the "skip unchanged directories" optimisation holds.
@@ -237,12 +237,13 @@ extension Enumerator {
// A changed child directory is scanned so its changed descendants are
// discovered, even when the directory itself is not materialised — but
// only when the working set actually tracks something inside it, i.e. it
// has a materialised descendant (a visited subfolder or a downloaded file).
// A changed-but-unmaterialised subtree is never crawled here: nothing in
// it is cached locally, so its contents are read lazily on navigation
// rather than walked now (which on a sparse domain would recurse into
// entire never-visited subtrees). Its own change is still reported above
// via `accumulatedUpdates` / `accumulatedCreations`.
// has a materialised descendant (a visited subfolder or a downloaded file),
// or when it is pinned, because "Always keep downloaded" asks for the whole
// subtree. Any other changed-but-unmaterialised subtree is never crawled
// here: nothing in it is cached locally, so its contents are read lazily on
// navigation rather than walked now (which on a sparse domain would recurse
// into entire never-visited subtrees). Its own change is still reported
// above via `accumulatedUpdates` / `accumulatedCreations`.
if changedChildOcIds.contains(childDirectory.ocId) {
let childPath = childDirectory.remotePath()
let childHasMaterialisedDescendant = materialisedItems.contains {
@@ -250,7 +251,12 @@ extension Enumerator {
&& ($0.hasSameRemotePath(as: childPath)
|| $0.isDescendant(of: childPath))
}
if childHasMaterialisedDescendant,
// The pin is read from the stored row because the read applies the parent's pin to its own copy.
let childIsPinned = dbManager
.itemMetadata(ocId: childDirectory.ocId)?
.keepDownloaded == true
if childHasMaterialisedDescendant || childIsPinned,
enqueuedDirectoryIds.insert(childDirectory.ocId).inserted
{
scanQueue[remotePathDepth(childDirectory), default: []].append(childDirectory)
@@ -95,11 +95,13 @@ final class RemoteChangePropagationTests: NextcloudFileProviderKitTestCase {
_ item: MockRemoteItem,
downloaded: Bool = false,
visitedDirectory: Bool = false,
keepDownloaded: Bool = false,
syncTime: Date? = nil
) -> SendableItemMetadata {
var metadata = item.toItemMetadata(account: Self.account)
metadata.downloaded = downloaded
metadata.visitedDirectory = visitedDirectory
metadata.keepDownloaded = keepDownloaded
metadata.syncTime = syncTime ?? oldSyncTime
Self.dbManager.addItemMetadata(metadata)
return metadata
@@ -498,6 +500,63 @@ final class RemoteChangePropagationTests: NextcloudFileProviderKitTestCase {
)
}
/// Run one working-set scan after a folder with a file in it was created on the server inside a visited folder, pinned or not.
private func scanAfterNewFolder(underPinnedParent parentIsPinned: Bool) async throws -> (observer: MockChangeObserver, enumeratedPaths: [String], newFolder: MockRemoteItem, newLeaf: MockRemoteItem) {
let parentFolder = makeFolder(name: "parentFolder", parent: rootItem, etag: "parent-v1")
// Brand new on the server, so nothing inside it is materialised to justify descending.
let newFolder = makeFolder(name: "NewFolder", parent: parentFolder, etag: "newfolder-v1")
let newLeaf = makeFile(name: "newLeaf", parent: newFolder, etag: "newleaf-v1")
seed(parentFolder, visitedDirectory: true, keepDownloaded: parentIsPinned)
parentFolder.versionIdentifier = "parent-v2"
let remoteInterface = MockRemoteInterface(account: Self.account, rootItem: rootItem)
let recorder = EnumeratePathRecorder()
remoteInterface.enumerateCallHandler = { remotePath, _, _, _, _, _, _, _ in
recorder.add(remotePath)
}
let observer = try await runWorkingSetChanges(remoteInterface)
return (observer, recorder.paths, newFolder, newLeaf)
}
/// A folder created on the server inside a pinned folder should be read in the same working-set scan.
func testNewFolderUnderAPinnedFolderIsCrawled() async throws {
let (observer, enumeratedPaths, _, newLeaf) = try await scanAfterNewFolder(underPinnedParent: true)
XCTAssertNil(observer.error)
XCTAssertTrue(
enumeratedPaths.contains { $0.hasSuffix("/NewFolder") },
"A new directory inside a pinned folder should be read in the same working-set scan."
)
XCTAssertTrue(
reportedIds(observer).contains(newLeaf.identifier),
"A file created on the server inside a new folder under a pinned folder should surface."
)
}
/// A folder created on the server inside an unpinned folder is reported but not read, because nothing inside it is part of the working set.
func testNewFolderUnderAnUnpinnedFolderIsNotCrawled() async throws {
let (observer, enumeratedPaths, newFolder, newLeaf) = try await scanAfterNewFolder(underPinnedParent: false)
XCTAssertNil(observer.error)
XCTAssertFalse(
enumeratedPaths.contains { $0.hasSuffix("/NewFolder") },
"A new directory inside an unpinned folder should not be read by the working-set scan."
)
XCTAssertFalse(
reportedIds(observer).contains(newLeaf.identifier),
"A file inside a new folder under an unpinned folder should not be reported by the working-set scan."
)
XCTAssertTrue(
reportedIds(observer).contains(newFolder.identifier),
"The new folder itself should still be reported."
)
}
/// S4: the change-detection predicate `isInSameDatabaseStoreableRemoteState` keys on ETag (+ a
/// fixed field set). If a file's content changes but its ETag is unchanged, the predicate treats
/// it as unchanged and the update is skipped. Real servers bump the ETag on content change, so