Skip to content

fix: don't let the unambiguous fast path rewrite the word destructively - #597

Open
luochen1990 wants to merge 2 commits into
Aloxaf:masterfrom
luochen1990:fix-unambiguous-fast-path-truncation
Open

luochen1990 wants to merge 2 commits into
Aloxaf:masterfrom
luochen1990:fix-unambiguous-fast-path-truncation

Conversation

@luochen1990

@luochen1990 luochen1990 commented Sep 13, 2026 •

Copy link
Copy Markdown

What

Two guards on the unambiguous fast path in -ftb-complete, both rejecting
shapes where compstate[unambiguous] cannot be trusted to extend the word
safely:

  1. skip the fast path when compstate[unambiguous] is a proper prefix of
    the current word (compared as "$compstate[quote]$IPREFIX$PREFIX");
  2. skip the fast path when compstate[unambiguous] is not a prefix of any
    captured candidate's full insertion form.

Both guards only fall back to opening the picker; nothing else changes.

Why

The fast path trusts compstate[unambiguous]: when it differs from the typed
word, it sets compstate[insert]=unambiguous, letting zsh rewrite the word in
place and skipping the picker entirely. That trust is misplaced in two
specific shapes.

Guard 1 — destructive truncation. When candidates come from mixed
sources — e.g. a user completer feeding bare full-path candidate words
(compadd ... dir/a/mod.nix dir/b/mod.nix) alongside the _files group — zsh
can compute an unambiguous that is cut at a path-segment boundary and ends
up as a proper prefix of the word. Inserting it then deletes already-typed
characters
:

  • cmd dir/<TAB> → the buffer silently becomes cmd dir — the trailing slash
    is gone, no menu, just a beep
  • inside quotes it is worse: cmd "dir/<TAB> → the whole path after the
    opening quote disappears

In this shape the word is strictly more specific than the common prefix, so
the insertion has zero information gain — the sensible behavior here is to
open the picker. Normal extension insertion (word shorter than
unambiguous, e.g. d → dir) is unaffected, and an unquoted empty word
falls through to the fast path unchanged.

Guard 2 — fabricated strings. Zsh computes compstate[unambiguous] over
matcher alignments. With substring matchers (l:|=* r:|=*, part of the
default matcher-list in Oh My Zsh and a popular fuzzy-completion setting) and
candidates aligning the word at inconsistent offsets — one candidate ending
with the word, another containing it mid-string — the computation fabricates
a string with a duplicated leading character of the word. Observed live:
typing cmd forgejo-actions<TAB> with candidates
git.lambda.lc/lc-studio/forgejo-actions and
codeberg.org/nix/forgejo-actions-runner rewrote the buffer to
cmd fforgejo-actions (and /forgejo-actions in the comptest run — the
fabricated character varies with alignment/order). Such a string prefixes
no candidate: no menu selection could ever reach it, so inserting it
corrupts the word with characters the user never typed. Requiring that at
least one candidate's full insertion form starts with unambiguous keeps the
fast path exactly for the reachable cases (common-prefix extension, unique
full expansion, path-prefixed and quoted completion) and routes the
fabricated ones to the picker.

The two guards are complementary: a fabricated string is typically not a
prefix of the word (guard 1 misses it), and a truncating unambiguous may
still be a genuine candidate prefix (guard 2 misses it).

This bug family overlaps #560 / #589 (the fast path acting on a misleading
compstate[unambiguous] value); #596 is a related-but-different request
(opt-in picker after unambiguous insertion). This change only carves out the
clearly-wrong subsets — destructive rewrites of the word; it intentionally
does not change behavior for the misleading-value cases in #560/#589 or
implement #596. Compared to #576, which requires unambiguous to be
contiguous in every candidate word to also address the bias reported in
#560, the reachability check here is deliberately narrower — it only rejects
strings no menu selection could ever produce.

How

Two extra conditions in the existing guard chain, right after the current
"differs from the word" test:

&& [[ "$compstate[quote]$IPREFIX$PREFIX" != "$compstate[unambiguous]"* ]] \
&& (( _ftb_amb_is_cand )) \

For guard 1, unquoted equality is already excluded by the preceding
condition, so the prefix test only rejects the proper-prefix (truncation)
shape. Inside quotes the preceding condition compares against a form with a
trailing quote, so the new condition additionally catches the exact-equality
shape — where master performs a no-op rewrite, the patched behavior opens
the picker, which matches what master already does for the same word
unquoted. The comparison is a plain case-sensitive string prefix test, so it
does not touch the case-folding scenario of #589. The leading
compstate[quote] matters: inside quotes compstate[unambiguous] itself
begins with the quote character, while on the word side the quote lives in
compstate[quote], not in $IPREFIX$PREFIX — omitting it would misalign
the two sides and quoted words would silently lose the guard. Any closing
quote on the line is not part of the constructed form, which is harmless for
a prefix test.

For guard 2, _ftb_amb_is_cand is computed just above by scanning
_ftb_compcap: each entry is split on NUL, its insertion form is
reconstructed as compstate[quote] + IPREFIX + hpre (the compadd -p
path prefix) + word, and the flag is set when any entry's form starts with
compstate[unambiguous]. The comparison must use the full insertion form,
not the bare word segment: compstate[unambiguous] lives in the full
command-line word space (carrying the leading quote and the path prefix), so
comparing against bare words would block the legitimate fast path for
path-prefixed file completion and for quoted words. The reconstruction
concatenates the kv segments in entry order (hpre precedes IPREFIX
there), which diverges from zsh's true insertion space (IPREFIX first) only
when both are non-empty — a corner where the mismatch is always
over-blocking (falls back to the picker), never permissive. apre (-P)
and -i are deliberately omitted: shortening the reconstruction form only
makes the prefix test harder to pass, i.e. also safely over-blocking. A loop
cannot be inlined into the \\ continuation chain, hence the flag; the outer
conditions of the scan gate it to fast-path-relevant cases only.

Testing

Three comptest cases in test/fzftab.ztst:

  • bare and quoted variants of an external bare full-path candidate group
    mixed with _files (guard 1) — both fail on current master with exactly
    the destructive rewrites described above;
  • a substring-matcher case with misaligned candidates (guard 2): typing
    forgejo-actions against the two URLs above opens the picker instead of
    silently rewriting the buffer — it fails on current master (and on master
    • guard 1 alone, confirming the guards are complementary) and passes with
      both guards.

The full ztst suite passes (zsh +Z -f test/ztst.zsh test/fzftab.ztst).

Also verified in live interactive shells (zsh 5.9.2, fzf 0.74.3) and deployed
via a downstream NixOS packaging patch.

luochen1990 and others added 2 commits September 13, 2026 12:30
When an external completer feeds bare full-path candidate words alongside
the file group (e.g. user-defined completers injecting candidates from
their own source), compstate[unambiguous] can be cut at a path-segment
boundary and become a proper prefix of the current word. The fast path
then honors insert=unambiguous and zsh rewrites the word, silently
deleting already-typed characters -- e.g. 'cmd dir/' becomes 'cmd dir'
with no menu, or worse, inside quotes the whole path after the opening
quote disappears.

Skip the fast path when unambiguous is a proper prefix of the current
word: the word is already more specific than the common prefix, so
inserting it has no information gain. Normal extension insertion (word
is shorter than unambiguous) is unaffected.
@luochen1990 luochen1990 changed the title fix: don't let the unambiguous fast path truncate the already-typed word fix: don't let the unambiguous fast path rewrite the word destructively Oct 26, 2026
@luochen1990

luochen1990 commented Oct 26, 2026 •

Copy link
Copy Markdown
Author

While dogfooding the first guard downstream, I ran into a second way the same fast path can rewrite the word destructively, so I've added a second, complementary guard in edd2329 (title updated to cover both).

Where the first guard catches the fast path deleting already-typed characters, the new one catches zsh fabricating characters: with substring matchers (l:|=* r:|=*) and candidates aligning the word at inconsistent offsets (one candidate ending with the word, another containing it mid-string), compstate[unambiguous] can be a string no candidate starts with — e.g. forgejo-actions → fforgejo-actions — so no menu selection could ever reach it and inserting it corrupts the word. The new condition simply requires that at least one captured candidate's full insertion form start with compstate[unambiguous], otherwise the picker opens.

A comptest regression is included; it fails on current master and also on master + the first guard alone, confirming the two guards are complementary. Happy to split this into a separate PR if you prefer reviewing them independently.

njhoffman added a commit to njhoffman/fzf-tab that referenced this pull request Nov 22, 2026
…ing no candidate has (upstream PRs Aloxaf#597, Aloxaf#576, Aloxaf#600, combined)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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