Repository navigation
Mount: widen and narrow the sparse-index cone - #2196
Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
Tyrie Vella (tyrielv) wants to merge 1 commit into
Conversation
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/mount-cone-handlers
branch
from
November 17, 2026 02:18
c39c7a3 to
6636993
Compare
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/mount-cone-handlers
branch
from
November 17, 2026 17:08
6636993 to
46fe831
Compare
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/mount-cone-handlers
branch
from
November 18, 2026 07:23
46fe831 to
d9af964
Compare
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/mount-cone-handlers
branch
2 times, most recently
from
November 19, 2026 19:06
216f5a1 to
e07b58c
Compare
The pre-command hook asks the mount to widen the sparse-checkout cone before a Git command that names paths outside the cone. The post-command hook asks it to narrow the cone after the command. Until now the mount answered both requests with UnknownRequest, and Git expanded the whole index in memory to find those paths. Add AutoSparseIndexConeManager. It handles ConeWiden and ConeNarrow when gvfs.auto-sparse-index was true when the projection was created, and core.sparseCheckout and index.sparse are true. Otherwise the mount traces a warning and replies NotEnabled. The mount refuses to start when the enlistment uses the sparse index and its Git does not report "feature: vfs-sparse-index" in "git version --build-options". Such a Git leaves a full index full in "sparse-checkout reapply", and with a cone-mode sparse-checkout file "git add -- <path outside the cone>" exits 0 and stages nothing. The triggers are gvfs.auto-sparse-index and a sparse-checkout file with the cone-mode header. core.sparseCheckout, index.sparse, and a non-cone file are not triggers: VFS for Git set and wrote the first and the last before 2018, and Git does not use index.sparse without a cone file. The error names the Git and gives the steps that turn the feature off. The cone is the cone of the modified paths plus the paths of every Git command that widened and did not narrow yet. TransientConeState keeps those paths, keyed by the Git process id and its start time. The manager removes the paths of a process that ended without a narrow. It refuses a late widen for a command that already narrowed. A widen writes the cone file, then runs "git sparse-checkout reapply" with the virtual file system on. Thus the index has entries for the new directories before Git runs. "git update-index --force-write-index" does not do this. It collapses directories that leave the cone, but it never expands a sparse-directory entry that the cone now includes. With the virtual file system on, reapply expands only those directories and keeps the skip-worktree bits. Its post-index-change hook reports no change, so the projection stays valid. "-c index.deleteSparseDirectories=false" stops Git from deleting working-tree directories. Only the Git process that holds the GVFS lock can widen. It waits in its pre-command hook, so no other Git command writes the index. Just before the child Git process starts, the manager checks again that the requester holds the GVFS lock and that at most half of WidenReplyCeilingMs passed since the request. The same limit applies to the wait for earlier requests. Thus reapply ends while the hook still waits. A refusal is costly, because Git checks a pathspec against the cone, so the mount refuses only when reapply could not finish in time. The manager refuses a widen that makes Git check out a deleted file: when reapply expands a directory, Git checks out each modified path that is missing from the working directory. Git also reports success when it cannot replace the index, so the mount checks that the index file changed. If the apply fails, the manager restores the previous cone file. A narrow writes only the cone file. It arrives after the hook released the GVFS lock, and the mount blocks a replacement of .git\index while no Git command holds the lock. The index keeps the expanded entries until the next widen collapses them. The manager holds the lock file of Git's own sparse-checkout writers while it changes the cone. It does not take the GVFS lock, because the requesting Git command holds it. Assisted-by: Claude Opus 5.5 Signed-off-by: Tyrie Vella <[email protected]>
Tyrie Vella (tyrielv)
force-pushed
the
tyrielv/sparse-index/mount-cone-handlers
branch
from
November 19, 2026 20:10
e07b58c to
2390ad1
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Note
This PR is part of a stack of PRs into
vnext. It builds on #2192, and its base branch is the branch of #2192, so the diff shows only this layer.Important
Test with the git changes. This stack needs the git-side changes in microsoft/git#1027, #1025, #1028, #1024, and #1026. VFS for Git CI uses a released git without them, so CI skips the AutoSparseIndex functional tests (NUnit reports them as ignored). To run the tests locally, install the unsigned preview git from sparse-index-preview-1. Its release notes give the install, test, and removal steps.
Latest preview validation: run 44421685610 on
216f5a1b: 16 passed, 0 failed, 0 ignored.Functional-test fixtures of this layer (
--test=):GVFS.FunctionalTests.Tests.EnlistmentPerFixture.AutoSparseIndexConeTests(new)GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests(summary changed)GVFS.FunctionalTests.Tests.EnlistmentPerFixture.AutoSparseIndexWithoutGitSupportTests(new; runs only with a Git withoutfeature: vfs-sparse-index, so it runs in CI and is ignored with the preview)Problem and Context
With
gvfs.auto-sparse-indexon, the pre-command hook sendsConeWidenbefore a Git command that names paths outside the sparse-checkout cone, and the post-command hook sendsConeNarrowafter the command (#2192). The mount answers both withUnknownRequesttoday, so Git expands the whole sparse index in memory to find those paths. On a repository with about 2.2 million index entries, that expansion costs several seconds for each command.This PR adds the mount side. A Git command that holds the GVFS lock and names an out-of-cone path finds that path inside the cone before Git starts. The cone goes back to its persistent state (the cone of the modified paths) after the command. The index stays sparse.
Why the mount runs
sparse-checkout reapply, notupdate-index --force-write-indexgit update-index --force-write-indexcollapses directories that leave the cone, but it never expands a sparse-directory entry that the cone now includes.update-indexdoes not require a full index, sodo_read_index()callsensure_correct_sparsity(), andconvert_to_sparse()returns at once for an index that is already collapsed. After a cone-file change andupdate-index, the widened directory is still one sparse-directory entry, and a followinggit add -- <path>still runsensure_full_index. Onlyupdate_sparsity()(git sparse-checkout reapply,set,add) callsexpand_index()with the cone patterns.With
core.virtualfilesystemactive,reapplyexpands only the directories that the cone now includes and collapses the directories that left it. It keeps the skip-worktree bits that the virtual file system hook reports, and its post-index-change hook reports no working-directory or skip-worktree change. The mount runs it with-c index.deleteSparseDirectories=false, so Git never deletes a working-tree directory that leaves the cone.Two hazards of
reapplythat the mount guards againstreapplyexpands the directory of a deleted file, the new entry starts with skip-worktree, and the virtual file system hook clears that bit because the path is modified.update_sparsity()takes that as a path that enters the cone and checks the file out again. A modified file that exists keeps its content. Thus the mount refuses a widen whose cone includes the directory of a deleted file (or a directory in a modified folder) that is still inside a sparse-directory entry. It finds those entries withgit ls-files --sparse, and only when the modified paths contain a deleted file or a folder.reapplyreads the index before it takesindex.lock, and it writes without a freshness check. Thus only the Git process that holds the GVFS lock can widen: it waits in its pre-command hook, so no other Git command writes the index.update_working_directory()also ignores the result ofwrite_locked_index(), soreapplyexits 0 when it cannot replace the index. The mount checks that the index file changed.The projection does not change
A cone change only changes how the index represents a directory: one sparse-directory entry, or one entry for each file. The projection expands sparse-directory entries from their trees, so the projected set is the same. The mount does not invalidate the projection after a cone change.
Changes
AutoSparseIndexConeManagerinGVFS.Common.Sparse. It computes the cone from the modified paths plus the paths of every Git command that widened and did not narrow yet, withConeBuilder, and writes it withConeFileWriter.git sparse-checkout reapplythroughIConeManagerEnvironment.ApplyConeToIndex. If Git fails, throws, or does not replace the index, restore the previous cone file (or delete it, if there was none before). If the restore fails, keep the backup. If the cone file does not change and the index already matches it, do not run Git. Refuse a widen with more than 1,000 paths. Just before Git starts, check again that the requester holds the GVFS lock. Do not start Git later than 5,000 ms (half ofWidenReplyCeilingMs) after the request, either after the wait for earlier requests or after the checks before Git. That leaves the other half of the hook's reply ceiling forreapply. A refusal makes a path-limited command fail (see Known gaps), so the mount refuses only whenreapplycould not finish in time.WindowsFileSystemVirtualizerblocks a rename to.git\indexwhile no Git command holds that lock. The index keeps the expanded entries (a valid sparse index, all skip-worktree) until the next widen, whosereapplycollapses every directory outside the cone. If a narrow cannot write the cone file, the next request writes it. The first request after a mount (or after an in-process remount) also writes it.F, and the hook lets Git run without the widen. Git then expands the index in memory. A path-limited command that changes the index, such asgit add -- <path>, still refuses a modified path outside the cone (see Known gaps). A modified path that a cone file cannot contain makesConeBuilderthrowArgumentException. The manager then keeps the current cone file and index. Failure events and widen events overWidenWaitBudgetMsare warnings with the telemetry keyword, and they record full exception text.info/sparse-checkout.lock, which is the lock file that Git's ownsparse-checkoutwriters create withO_EXCL. The mount opens it withDeleteOnClose, so the lock file goes away with the handle. The manager does not take the GVFS lock, because the requesting Git command holds it.SparseDirectoryExpansionGuard. It finds the modified paths inside a sparse-directory entry, and the first unsafe path that a cone reaches. A cone that includes only a parent directory is safe, because the deeper directory stays a sparse-directory entry.TransientConeState. It keeps the paths of each Git command, keyed by the Git process id and the process start time (Windows reuses process ids). The paths of a command stay in the cone until that command narrows, so two commands that need the same directory do not remove it for each other. The manager removes the paths of a process that ended, or that it can no longer open, at the next request. A narrow for a command without paths records the command, and the manager refuses a later widen from the same process. The manager refuses a widen from a process whose start time it cannot read.GitProcess.ReapplySparseCheckout()andGitProcess.ListSparseDirectories(). Both keep the virtual file system and the hooks path on, and setCOMMAND_HOOK_LOCK, so Git skips the pre-command and post-command hooks.ConeWidenandConeNarrowinInProcessMountthrough the staticAutoSparseIndexConeManager.HandleRequest. ReplyMountNotReadywhile the mount is not ready, andNEwhen the feature is off. The mount creates the manager only when the projection was created withgvfs.auto-sparse-indexon (newGitIndexProjection.IsSparseIndexSupportEnabled) andcore.sparseCheckoutandindex.sparseare true, and when the Git that the mount runs reportsfeature: vfs-sparse-indexingit version --build-options. A Git without that line runssparse-checkout reapplybut leaves a full index full. Otherwise the mount traces a warning with the telemetry keyword. AddGitProcess.VersionBuildOptions()andGitProcess.HasBuildOption(). The mount no longer logs an error event for these requests.feature: vfs-sparse-index. With such a Git and a cone-mode sparse-checkout file,git add -- <path outside the cone>exits 0 and stages nothing.SparseIndexGitSupportGuardnames the triggers:gvfs.auto-sparse-indextrue, or a sparse-checkout file with the cone-mode header. Git applies a cone, and writes a sparse index, only with such a file.core.sparseCheckout,index.sparse, and a non-cone sparse-checkout file are not triggers. VFS for Git setcore.sparseCheckoutand wrote a non-cone file before 2018, and such enlistments work with any supported Git. The mount checks this right after the Git version, before it writes the required config or builds the projection, and runsgit version --build-optionsonly when a trigger is set. The error names the Git path and version, and the commands that turn the feature off. The mount sends aSparseIndexGitNotSupportedwarning event with the telemetry keyword.MountConeManagerEnvironment, the mount implementation ofIConeManagerEnvironment, and thecore.ignorecase,core.sparsecheckout, andindex.sparsenames inGitConfigSetting. The hooks now use the sharedcore.ignorecasename. Update the contract remarks inConeManagementNamedPipeMessagesfor the new replies and the lock-holder rule.AutoSparseIndexConeManagerwith a mock environment, so that no test starts Git. These tests check widen and narrow round trips, two commands that share a directory, a missing narrow, a reused or no longer accessible process id, a process in an unknown state, a widen that arrives after its narrow, a request without a command id, a widen from a process that does not hold the GVFS lock, too many paths, a long wait for earlier requests, a requester that loses the GVFS lock before Git starts, a widen that cannot start Git in time, the cone file lock held by Git, a modified path that a cone file cannot contain, a deleted file or modified folder inside a sparse-directory entry, a sibling directory of a deleted file, a cone file write failure, a Git failure and a Git exception (the previous cone file is restored, or deleted when there was none), a failed restore (the backup stays), a failed narrow that the next request repairs, parallel widens and narrows, and the replies ofHandleRequest.TransientConeStateandSparseDirectoryExpansionGuard, and forAutoSparseIndexConeManager.GitSupportsSparseIndex(the feature line with LF and CRLF, a missing line, a similar line, a failed command, no output).SparseIndexGitSupportGuard: each trigger, a legacy or empty sparse-checkout file, all triggers together, and the content of the error.git add -- <out-of-cone paths>does not runensure_full_index, that the file is staged, that the index stays sparse, and that the cone file goes back to the cone of the modified paths. The tests check that the next widen collapses the stale expansion, and that applying the cone wrote no working-tree file and deleted nothing. A second test runs twogit addcommands and agit diff -- <path>at the same time, and waits for late narrows. A third test checks thatgit diff -- <path>(no GVFS lock) does not change the cone. A fourth test deletes a file in a collapsed directory, runsgit addon another file in that directory, and checks that the deleted file does not come back. A fifth test writes a full index with-c index.sparse=false update-index --force-write-index, as a clone leaves it. The test then runsgit addon a modified file outside the cone. It checks that the entry count in the index file is smaller than the full count, and that the file that the fourth test deleted does not come back. The tests check that no cone request other than a narrow printed "not applied", and they wait for a narrow that ends after the command. The tests run only with a Git that reportsfeature: vfs-sparse-index.feature: vfs-sparse-index, which is the Git of the PR validation build. The first test turns the feature on, writes a cone file and a sparse index on disk, and checks that the mount refuses with the error. The test then runs the printed recovery commands, checks that the projection refuses the sparse index, and runs the steps of that error. After that, the test checks that the mount works, thatgit status,git addof a deep file, andgit commitwork, thatgit statusis clean, that the index header is full, and that no cone file exists. A second test setscore.sparseCheckoutto true, writes the non-cone sparse-checkout file of older versions, and checks that the mount,git add, andgit commitwork.ConeManagementHookTests: the flag is turned on after the mount started, so the mount now answersNE.Measurements
On a copy of the sparse index of a repository with about 2.2 million index entries (1,229 entries, 1,039 sparse directories), with the same
core.gvfsflags and a virtual file system hook:git sparse-checkout reapplyafter adding one directory to the cone (25 samples)reapplywith the original conegit ls-files --sparse(only when a deleted file or modified folder exists)git ls-files -- <path>)reapplydid not runensure_full_index. Over 20 widen/narrow cycles with the mount's behavior (the narrow writes only the cone file), the index stayed between 133.0 and 134.5 KB (133.0 KB before), because each widen collapses the expansion of the previous command. Without the missing-objects flag ofcore.gvfs, which GVFS sets,expand_index()falls back to a full expansion when a blob is not local.On a live mount, a dump of the complete projection (every path, blob id, and folder inclusion flag) was byte-identical before and after a widen, before and after a collapse-only
reapply, and before and after a mixedreapply. Moving the index to another commit changed the dump, so the comparison is not vacuous.Known gaps and deliberate decisions
git diff -- <path>) getF, and Git expands the index in memory, as today.WidenReplyCeilingMs(10,000 ms). Ifreapplytakes longer, it keeps running and holdsindex.lock, and the Git command can fail with "index.lock: File exists". The mount does not stopreapply, because a stopped Git process can leaveindex.lockbehind. The measured widen takes less than 500 ms. The mount startsreapplyat most 5,000 ms after the request, and it traces a warning for a widen overWidenWaitBudgetMs(1,000 ms).git addof a modified path outside the cone. After any refused or timed-out widen, Git checks its pathspec against the cone patterns.git add -- <path>then reports "paths ... outside of your sparse-checkout definition" and exits 1. In-memory expansion does not change that check. Refusals come from the cases below, from the request limits, and from load.reapplychange only the index under a virtual file system removes this limit.git sparse-checkout setandaddfail while the mount changes the cone, because the mount holds their lock file.reapplyuses the repository'score.sparseCheckout,core.sparseCheckoutCone, andindex.sparsevalues. The mount does not force them with-c. It does not create the manager whencore.sparseCheckoutorindex.sparseis not true.sdircheck before the projection. The mount guard does not read the index for sparse-directory entries, because that needs a parse of the whole index. Whengvfs.auto-sparse-indexis false, the projection refuses a sparse-directory entry with its own recovery steps. When it is true, the guard refuses first. The enable path, in a later PR of this stack, collapses the index when it turns the feature on. Until a widen or another Git command writes the index, a full index stays full.