Skip to content

Mount: widen and narrow the sparse-index cone - #2196

Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
tyrielv/sparse-index/hooks-cone-requestsfrom
tyrielv/sparse-index/mount-cone-handlers
Draft

Tyrie Vella (tyrielv) wants to merge 1 commit into
tyrielv/sparse-index/hooks-cone-requestsfrom
tyrielv/sparse-index/mount-cone-handlers

Conversation

@tyrielv

@tyrielv Tyrie Vella (tyrielv) commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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 without feature: vfs-sparse-index, so it runs in CI and is ignored with the preview)

Problem and Context

With gvfs.auto-sparse-index on, the pre-command hook sends ConeWiden before a Git command that names paths outside the sparse-checkout cone, and the post-command hook sends ConeNarrow after the command (#2192). The mount answers both with UnknownRequest today, 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, not update-index --force-write-index

git update-index --force-write-index collapses directories that leave the cone, but it never expands a sparse-directory entry that the cone now includes. update-index does not require a full index, so do_read_index() calls ensure_correct_sparsity(), and convert_to_sparse() returns at once for an index that is already collapsed. After a cone-file change and update-index, the widened directory is still one sparse-directory entry, and a following git add -- <path> still runs ensure_full_index. Only update_sparsity() (git sparse-checkout reapply, set, add) calls expand_index() with the cone patterns.

With core.virtualfilesystem active, reapply expands 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 reapply that the mount guards against

  • A deleted file comes back. When reapply expands 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 with git ls-files --sparse, and only when the modified paths contain a deleted file or a folder.
  • Another command's index changes are lost. reapply reads the index before it takes index.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 of write_locked_index(), so reapply exits 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

  • Add AutoSparseIndexConeManager in GVFS.Common.Sparse. It computes the cone from the modified paths plus the paths of every Git command that widened and did not narrow yet, with ConeBuilder, and writes it with ConeFileWriter.
    • Widen: refuse the request unless the requesting process holds the GVFS lock. Then write the cone file and run git sparse-checkout reapply through IConeManagerEnvironment.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 of WidenReplyCeilingMs) 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 for reapply. A refusal makes a path-limited command fail (see Known gaps), so the mount refuses only when reapply could not finish in time.
    • Narrow: write the cone file only, with no Git process. The narrow arrives after the post-command hook released the GVFS lock, and WindowsFileSystemVirtualizer blocks a rename to .git\index while no Git command holds that lock. The index keeps the expanded entries (a valid sparse index, all skip-worktree) until the next widen, whose reapply collapses 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.
    • Failures: every failure replies 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 as git add -- <path>, still refuses a modified path outside the cone (see Known gaps). A modified path that a cone file cannot contain makes ConeBuilder throw ArgumentException. The manager then keeps the current cone file and index. Failure events and widen events over WidenWaitBudgetMs are warnings with the telemetry keyword, and they record full exception text.
    • Locks: a process-internal lock serializes all requests. While it changes the cone, the manager also holds info/sparse-checkout.lock, which is the lock file that Git's own sparse-checkout writers create with O_EXCL. The mount opens it with DeleteOnClose, so the lock file goes away with the handle. The manager does not take the GVFS lock, because the requesting Git command holds it.
  • Add 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.
  • Add 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.
  • Add GitProcess.ReapplySparseCheckout() and GitProcess.ListSparseDirectories(). Both keep the virtual file system and the hooks path on, and set COMMAND_HOOK_LOCK, so Git skips the pre-command and post-command hooks.
  • Handle ConeWiden and ConeNarrow in InProcessMount through the static AutoSparseIndexConeManager.HandleRequest. Reply MountNotReady while the mount is not ready, and NE when the feature is off. The mount creates the manager only when the projection was created with gvfs.auto-sparse-index on (new GitIndexProjection.IsSparseIndexSupportEnabled) and core.sparseCheckout and index.sparse are true, and when the Git that the mount runs reports feature: vfs-sparse-index in git version --build-options. A Git without that line runs sparse-checkout reapply but leaves a full index full. Otherwise the mount traces a warning with the telemetry keyword. Add GitProcess.VersionBuildOptions() and GitProcess.HasBuildOption(). The mount no longer logs an error event for these requests.
  • Refuse to mount when the enlistment uses the sparse index and the Git that the mount runs does not report 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. SparseIndexGitSupportGuard names the triggers: gvfs.auto-sparse-index true, 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 set core.sparseCheckout and 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 runs git version --build-options only when a trigger is set. The error names the Git path and version, and the commands that turn the feature off. The mount sends a SparseIndexGitNotSupported warning event with the telemetry keyword.
  • Add MountConeManagerEnvironment, the mount implementation of IConeManagerEnvironment, and the core.ignorecase, core.sparsecheckout, and index.sparse names in GitConfigSetting. The hooks now use the shared core.ignorecase name. Update the contract remarks in ConeManagementNamedPipeMessages for the new replies and the lock-holder rule.
  • Add unit tests for AutoSparseIndexConeManager with 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 of HandleRequest.
  • Add unit tests for TransientConeState and SparseDirectoryExpansionGuard, and for AutoSparseIndexConeManager.GitSupportsSparseIndex (the feature line with LF and CRLF, a missing line, a similar line, a failed command, no output).
  • Add unit tests for SparseIndexGitSupportGuard: each trigger, a legacy or empty sparse-checkout file, all triggers together, and the content of the error.
  • Add functional tests that mount an enlistment with a root-only cone and a collapsed index, and run Git with trace2. The tests check that git add -- <out-of-cone paths> does not run ensure_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 two git add commands and a git diff -- <path> at the same time, and waits for late narrows. A third test checks that git diff -- <path> (no GVFS lock) does not change the cone. A fourth test deletes a file in a collapsed directory, runs git add on 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 runs git add on 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 reports feature: vfs-sparse-index.
  • Add functional tests that run only with a Git without 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, that git status, git add of a deep file, and git commit work, that git status is clean, that the index header is full, and that no cone file exists. A second test sets core.sparseCheckout to true, writes the non-cone sparse-checkout file of older versions, and checks that the mount, git add, and git commit work.
  • Update the summary of the existing ConeManagementHookTests: the flag is turned on after the mount started, so the mount now answers NE.

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.gvfs flags and a virtual file system hook:

Operation Time
Widen: git sparse-checkout reapply after adding one directory to the cone (25 samples) 336-424 ms
Narrow and collapse: reapply with the original cone 346-396 ms
git ls-files --sparse (only when a deleted file or modified folder exists) 221-339 ms
Full in-memory expansion (git ls-files -- <path>) 6.4-23 s

reapply did not run ensure_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 of core.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 mixed reapply. Moving the index to another commit changed the dump, so the comparison is not vacuous.

Known gaps and deliberate decisions

  • Unlocked commands still expand in memory. Only the Git process that holds the GVFS lock can widen. Commands that do not take the lock (for example git diff -- <path>) get F, and Git expands the index in memory, as today.
  • A widen slower than the hook's reply ceiling. After the hook sends a widen, it waits for the reply up to WidenReplyCeilingMs (10,000 ms). If reapply takes longer, it keeps running and holds index.lock, and the Git command can fail with "index.lock: File exists". The mount does not stop reapply, because a stopped Git process can leave index.lock behind. The measured widen takes less than 500 ms. The mount starts reapply at most 5,000 ms after the request, and it traces a warning for a widen over WidenWaitBudgetMs (1,000 ms).
  • A failed widen fails git add of 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.
  • A deleted file in a collapsed directory blocks a widen of that directory. The widen is refused, with the result in the previous bullet. A Git change that makes reapply change only the index under a virtual file system removes this limit.
  • Modified files outside the cone. This PR does not widen the cone when a file outside the cone is modified. Until a Git command names the file, Git does not see the change in a collapsed directory. A later PR widens the cone on file modification.
  • The narrow does not collapse the index at once. The expanded entries stay until the next widen. They are skip-worktree entries in a valid sparse index, and the measured growth is bounded.
  • The cone file is owned by the mount. Each request rewrites the cone file from the modified paths and the live commands. A cone that someone else wrote is replaced at the next request. git sparse-checkout set and add fail while the mount changes the cone, because the mount holds their lock file.
  • Repository config. reapply uses the repository's core.sparseCheckout, core.sparseCheckoutCone, and index.sparse values. The mount does not force them with -c. It does not create the manager when core.sparseCheckout or index.sparse is not true.
  • No sdir check before the projection. The mount guard does not read the index for sparse-directory entries, because that needs a parse of the whole index. When gvfs.auto-sparse-index is 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.

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]>
@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/mount-cone-handlers branch from e07b58c to 2390ad1 Compare November 19, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant