fix(ai): prevent async suggestion duplication

- Add buffer validation to check suggestion starts with buffer
- Discard stale suggestions that don't match current content

Fixes text duplication bug when accepting async suggestions after
buffer changes during suggestion fetch.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Frad LEE 2026-02-12 17:01:15 +08:00
commit 55cd4ec23d
3 changed files with 85 additions and 1 deletions

View file

@ -405,3 +405,29 @@ EOFCURL
wait_for { session.content }.to eq('temp 0.3')
end
end
context 'suggestion buffer validation' do
let(:options) { ["ZSH_AUTOSUGGEST_AI_API_KEY=test-key", "ZSH_AUTOSUGGEST_STRATEGY=(ai)"] }
context 'when suggestion does not match current buffer' do
let(:before_sourcing) do
-> {
session.run_command('curl() {
if [[ "$*" == *"max-time"* ]]; then
cat <<EOFCURL
{"choices":[{"message":{"content":"git status --long"}}]}
200
EOFCURL
fi
}')
}
end
it 'discards suggestion that does not match buffer' do
# Suggestion is "git status --long" but buffer is unrelated
session.send_string('ls')
wait_for { session.content }.to_not match(/git status/)
end
end
end
end