Skip to content

Stop index-writing commands when the sparse-index cone cannot be prepared - #2208

Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
tyrielv/sparse-index/prune-dehydratesfrom
tyrielv/sparse-index/fail-closed-index-writes
Draft

Tyrie Vella (tyrielv) wants to merge 1 commit into
tyrielv/sparse-index/prune-dehydratesfrom
tyrielv/sparse-index/fail-closed-index-writes

Conversation

@tyrielv

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

Copy link
Copy Markdown
Contributor

Note

This PR is part of a stack of PRs into vnext. It builds on #2202, and its base branch is the branch of #2202, 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 44433540852 on a8595035: 136 passed, 2 failed, 0 ignored.

Problem and Context

With gvfs.auto-sparse-index, the pre-command hook asks the mount to widen the sparse-index cone for the paths that a Git command names. It also asks the mount to apply pending working-tree changes outside the cone (#2197). A request can fail, for example under load, after a mount timeout, or when it waits too long behind earlier requests. In that case the hook let Git run, and Git expanded the index in memory.

That is not correct. Git has no index entry for a file inside a directory that the cone does not include. When the mount refuses the widen and the apply, the commands behave as follows:

  • git add <path>, add ., add -A, mv, checkout -- <path>, and restore <path> fail. git add checks the sparse-checkout patterns and reports that the path is outside the sparse-checkout definition. checkout and restore report that the pathspec does not match a file.
  • git commit -a and commit <path> report "nothing to commit, working tree clean". With other staged files, commit -a commits them without the local change.
  • git add -u, stash, and stash push -- <path> succeed and do nothing for the file. stash reports "No local changes to save".
  • git checkout <branch>, switch, merge, cherry-pick, rebase, revert, and reset --keep succeed, although Git must refuse because of the local change. HEAD and the index move, and the file keeps the old content and the local change. A later commit -a then reverts the change of the new commits to that file without a warning.
  • git status, diff, and ls-files -m do not show the change. They change nothing, but the user does not see the change.

A plain git commit of the staged files is correct.

The unstage commands of #2200 stop only when a widen times out or an apply fails. This PR makes every command that writes the index fail closed, including the unstage commands, and adds a warning for the other commands.

Changes

  • Add ConeCommandClassifier in the hooks, next to UnstageCommandParser. It sorts a command into one of three kinds:
    • Unstage: the unstage commands of Keep a change visible when unstaging after reset --soft (sparse index) #2200 (for example restore --staged and checkout HEAD -- <path>). An unstage also writes the index, so it uses the same stop rules as WritesIndex (below), with the message of Keep a change visible when unstaging after reset --soft (sparse index) #2200.
    • WritesIndex: add and stage, am, apply with --index, --cached, or --3way (not with --check, --stat, --numstat, or --summary alone), checkout, cherry-pick, commit with -a, -i, -o, -p, --interactive, --pathspec-from-file, or paths, merge, mv, pull, rebase, reset --hard, --merge, or --keep, restore, revert, rm, stash (not list, show, drop, clear, or store), and switch.
    • Other: every other command, including read-only commands, plumbing such as update-index and read-tree, and a commit of the staged files only.
  • PreCommandConeRequests.Send takes the kind and returns a ConeRequestFailure (the request and its outcome).
    • For WritesIndex and Unstage, it returns a failure when the widen was sent and is not Applied, or when the apply was sent and is not Applied, NothingPending, or FeatureDisabled. For Unstage, a failed widen now stops the command before the unstage preparation; before, only a widen that timed out did.
    • Before it returns a failure for WritesIndex or Unstage, it checks if GVFS manages a sparse-index cone. When the hook knows that GVFS does not manage it, Git runs. When the hook cannot read the cone file or the feature flag, the command stops.
    • For Other, it returns a failure only for an apply that was sent and did not succeed. It ignores a failed widen: the mount refuses every widen of a command that does not hold the GVFS lock (for example git diff -- <path>), and a named path without a local change is correct after the in-memory expansion.
  • The pre-command hook acts on the failure:
    • It stops a WritesIndex command before Git runs, with GVFS could not prepare the sparse index (<request>: <outcome>). Run the command again. A second line names gvfs status, gvfs unmount, and gvfs mount for a failure that repeats. For NotLockHolder and LockUnavailable, the message says that this command does not hold the GVFS lock, and that usually another Git command holds it. Like the other hook errors, the message goes to stdout, and Git exits with code 1.
    • For an Other command, it writes one line to stderr and lets Git run: GVFS: the sparse index is not up to date (apply: <outcome>); this command can miss changes outside the sparse-checkout cone. Run it again.
    • Before it stops a command after a ReplyTimedOut widen or apply, it waits (up to 60 seconds, as the unlocked-reader path of Widen the sparse-index cone for working-tree changes #2197 does) for the mount to finish the request. Otherwise the mount's Git can still hold index.lock and write the index after the GVFS lock of the stopped command is released.
  • The mount replies with the new NotLockHolderResult (NL) when the requesting Git process does not hold the GVFS lock, so that the hook can name the lock. AutoSparseIndexConeManager.HandleRequest maps the internal result to the reply. A hook that does not know NL treats it as "not applied", as for every other reply. TryWiden and TryApply are now internal; the mount uses HandleRequest.
  • When the hook cannot check the mount's pending-change file (an error other than "not found"), it counts the change as pending and sends an apply. Before, it treated the change as not pending, and the command could miss it.
  • The hook maps a widen reply NE (the mount has no cone manager) to FeatureDisabled, as the apply already did. Before, it was Rejected, which would now stop every named-path writer when the mount does not manage the cone.
  • The hook does not send a widen with more uncovered paths than AutoSparseIndexConeManager.MaxWidenPaths, and returns the new outcome TooManyPaths. The mount refuses such a widen every time, so a stop would stop the command at every attempt. A pending apply still runs.
  • Add a test-only fault seam in the mount, ConeRequestFaultInjection. The mount reads the environment variable GVFS_TEST_CONE_REQUEST_FAULT_FILE once when it starts. When the variable names a file, the mount reads that file (at most 4 KiB, not a reparse point) at each widen and apply request. Each line names a request (ConeWiden or ConeApply) and a reply (F or NL). The mount sends that reply and does not change the cone or the index. A missing or unreadable file means no fault. When the variable is not set, the mount reads no file. The mount logs a warning when the variable is set, and an event for each forced reply.
  • Update the remarks and comments of ConeManagementHook, PendingConeChangeHook, AutoSparseIndexConeManager, and the cone messages, which said that every failure lets Git run.
  • Add unit tests for the classification (including option clusters such as -qam, -Sdeadbeef, -uno, apply -v3 and -p3, and stash -- list), for the decision of PreCommandConeRequests for each kind and each outcome (including the order of the requests and no stop when GVFS does not manage the cone), for the stop messages and the warning, for the NL and NE replies in both hook paths, for the abandoned request after a reply timeout in both hook paths, for the pending-file check (present, not found, and cannot check), for TooManyPaths at and above the limit, and for the parser of the fault file.
  • Add functional tests (AutoSparseIndexFailClosedTests) on a live mount with the fault file. In each test, the test makes the mount refuse the requests, edits a file outside the cone, and waits until the mount makes the cone change pending.
    • For add -- <path>, add ., add -A, add -u, rm --cached, commit -a, commit -- <path>, stash, stash push -- <path>, checkout -- <path>, and restore -- <path>, the test asserts that the hook stopped the command with the message, and that Git did not run: no index.lock, the same index file, the same HEAD and stash list, and the local change is still in the file. Then the test removes the fault, runs the command again, and asserts that it staged, removed, committed, stashed, or discarded the file.
    • For checkout <branch>, switch, merge, cherry-pick, rebase, reset --keep, and revert, the test creates a commit that changes the file and makes the same checks. After the fault is removed, the test asserts that Git (not the hook) refuses the command because of the local change.
    • For restore --staged -- <path> and checkout HEAD -- <path>, the test asserts that a failed widen stops the unstage before Git runs, with the same checks. After the fault is removed, the test asserts that the command runs.
    • The test makes the mount reply NL for the apply of commit -a, and asserts that the message names the GVFS lock.
    • For status, diff, diff --cached, ls-files, and blame, the test asserts that the command runs, that the hook writes the warning, and that the change stays pending. For log -p -- <path>, which does not read the index, the test asserts that the hook writes no warning.
    • The test stages a file in the cone, and asserts that a commit of the staged files only runs with the warning and commits only that file.
  • Update ConeManagementHookTests and AutoSparseIndexConeTests for the NE and NL replies.

Functional tests to run with the preview git

  • GVFS.FunctionalTests.Tests.EnlistmentPerFixture.AutoSparseIndexFailClosedTests (added by this PR)
  • GVFS.FunctionalTests.Tests.EnlistmentPerFixture.AutoSparseIndexConeTests (changed)
  • GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests (changed; also runs in CI)

Known gaps and deliberate decisions

  • Stop instead of a silent skip. A failed request now stops a command that writes the index, and the user must run it again. The requests fail rarely: under heavy load, after a mount timeout, or when a request waits too long behind earlier requests. A git-side change can later make the fallback a partial expansion of the index. Then some of these stops can become slower successes, and the classification can shrink.
  • Read-only commands only warn. status, diff, and ls-files can miss a local change after a failed apply, and the hook only writes a warning. Editors run status very often, and a stop there would break them. A command that writes the index stops, so the missed change is not lost.
  • Plumbing is not stopped. update-index -- <path> skips the file in the same way as add -u, and read-tree -m -u can write the index. Tools call plumbing in scripts, and a stop there is harder to recover from. These commands get the warning when their apply fails.
  • More than MaxWidenPaths uncovered paths. The hook does not send the widen, and the command runs. A pending apply still puts every local change in the cone. A named path without a change stays outside the cone. git add, rm, and mv then fail with Git's message about the sparse-checkout definition.
  • No telemetry event for a stop. The hooks have no telemetry. The mount traces each request that it refused, and with GIT_TRACE the hook traces each outcome.
  • Option parsing. The classifier does not match abbreviated long options (for example --interactiv for --interactive). A commit with such an option runs as a commit of the staged files.
  • Fail-closed read check is not unit-tested. The check that stops a command when the cone file or the feature flag cannot be read lives in the hook's Program, which the unit-test project does not compile. The unit tests cover the decision through a delegate.

@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/fail-closed-index-writes branch from 7982a60 to c200b3c Compare November 18, 2026 22:40
With a sparse index, Git has no index entry for a file inside a
directory that the sparse-checkout cone does not include. The
pre-command hook asks the mount to widen the cone for the paths that a
command names. It also asks the mount to apply the pending changes in
the working tree. A request can fail, for example under load or after
a mount timeout. Then the hook let Git run, and Git expanded the index
in memory.

That is not correct for commands that write the index. "git add",
"rm", "mv", "checkout -- <path>", and "restore <path>" check the paths
against the sparse-checkout patterns and fail. "git stash", "add -u",
"commit -a", "checkout <branch>", "switch", "merge", "cherry-pick",
"rebase", "revert", and "reset --keep" do not see a local change to a
file outside the cone, and they report success. The stash saves
nothing. The history commands move HEAD, and the file keeps the old
content.

Classify the commands in the pre-command hook. When a widen or an apply
that the hook sent is not applied, stop a command that writes the index
before Git runs. The message is "GVFS could not prepare the sparse
index (<request>: <outcome>). Run the command again." When the hook
cannot read the cone file or the feature flag, it also stops the
command. When it knows that GVFS does not manage the cone, it never
stops a command.

An unstage command also writes the index. It now stops for every failed
widen as well, not only after a timeout, and before the unstage
preparation runs. When the hook cannot check the pending-change marker,
it counts the change as pending.

Other commands run. When their apply failed, they can miss a change
outside the cone, so the hook writes a warning to stderr.

Before the hook stops a command after a reply timeout, it waits for the
mount to finish the request. Otherwise the next Git command can start
while the mount still writes the index.

The mount now replies "NL" when the requesting Git process does not
hold the GVFS lock, so that the message can name the lock. A widen
reply "NE" (no cone management in the mount) no longer counts as a
failure. The hook does not send a widen with more paths than the mount
accepts, because the mount refuses it every time.

For the functional tests, the mount reads a fault file that the
GVFS_TEST_CONE_REQUEST_FAULT_FILE environment variable names. The mount
refuses the widen and apply requests that the file lists. Without the
variable, the mount reads no file.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>
@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/fail-closed-index-writes branch from c200b3c to a859503 Compare November 19, 2026 00:17
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