Skip to content

fix: ignore stale mention search responses#272

Open
adliebe wants to merge 3 commits into
profullstack:masterfrom
adliebe:fix/mention-stale-suggestions
Open

fix: ignore stale mention search responses#272
adliebe wants to merge 3 commits into
profullstack:masterfrom
adliebe:fix/mention-stale-suggestions

Conversation

@adliebe
Copy link
Copy Markdown

@adliebe adliebe commented May 27, 2026

Summary\n- Fixes #271.\n- Adds a request sequence guard to MentionTextarea so only the latest active mention search can update suggestions.\n- Clears and invalidates pending mention searches when the cursor leaves mention context.\n- Adds regression coverage for stale response ordering and pending debounce cancellation.\n\n## Validation\n- node_modules/.bin/vitest.cmd run src/components/ui/mention-textarea.test.tsx -> 2 passed\n- node_modules/.bin/eslint.cmd --quiet -- src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed\n- node_modules/.bin/tsc.cmd --noEmit -> passed\n- node_modules/.bin/prettier.cmd --check src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed\n- git diff --check -- src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed\n\nNote: I used direct local binaries for validation because the current base branch postinstall still hits the known Windows || true issue tracked by PR #262.

@adliebe
Copy link
Copy Markdown
Author

adliebe commented May 27, 2026

Submitted for the active Ugig testing/fix gig: https://ugig.net/gigs/abd6b2a0-e728-48cf-a46f-f99e419ed94e\n\nScope: real mention-suggestion race bug, issue #271, focused fix plus regression coverage and validation listed in the PR body.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 27, 2026

Greptile Summary

This PR adds a request sequence guard to MentionTextarea to prevent stale mention search responses from overwriting results from a newer query, fixing #271. It also cancels pending debounces and invalidates in-flight requests when the cursor leaves mention context.

  • mention-textarea.tsx: Introduces requestSeqRef (a monotonically-incrementing counter); each debounce-scheduled fetchSuggestions call captures its seq at scheduling time and bails out on completion if requestSeqRef.current !== seq. The else branch (no mention context) now also clears the pending debounce, increments the counter, and calls setSuggestions([]).
  • mention-textarea.test.tsx: Adds three deterministic fake-timer tests covering stale-response ordering, pending debounce cancellation, and HTTP-error state clearing — all previous timer-flakiness concerns are addressed.

Confidence Score: 5/5

Safe to merge — the change is well-scoped, all three mutation paths (success, HTTP error, thrown exception) are guarded by the sequence check, and the debounce-cancellation path correctly invalidates in-flight requests.

The sequence guard logic is sound: requestSeqRef is incremented before each debounced fetch and again when the cursor leaves mention context, ensuring any concurrently-resolving fetch from a prior sequence finds the counter has advanced and discards its result. Both previously-flagged concerns (timer flakiness in tests and the error path not hiding the dropdown) are resolved in this PR. No incorrect state transitions or missed guard conditions were found.

No files require special attention.

Important Files Changed

Filename Overview
src/components/ui/mention-textarea.tsx Adds requestSeqRef sequence guard to fetchSuggestions; seq is captured at debounce-schedule time and checked before every state mutation on all three code paths (success, HTTP error, throw). Else branch (leaving mention context) now also clears debounce and invalidates in-flight fetches.
src/components/ui/mention-textarea.test.tsx New test file with three deterministic fake-timer tests: stale-response ordering, cursor-leaves-mention-context debounce cancellation, and HTTP-error suggestion clearing. All use vi.useFakeTimers() for determinism.

Sequence Diagram

sequenceDiagram
    participant User
    participant detectMention
    participant debounce
    participant fetchSuggestions
    participant API

    User->>detectMention: "type @a"
    detectMention->>detectMention: "seq = ++requestSeqRef (=1)"
    detectMention->>debounce: setTimeout fetchSuggestions a 1 300ms

    User->>detectMention: "type @ad"
    detectMention->>debounce: "clearTimeout cancel seq=1"
    detectMention->>detectMention: "seq = ++requestSeqRef (=2)"
    detectMention->>debounce: setTimeout fetchSuggestions ad 2 300ms

    debounce->>fetchSuggestions: fetchSuggestions ad 2
    fetchSuggestions->>API: "GET /api/users/search?q=ad"

    Note over fetchSuggestions: Old fetch seq=1 resolves late
    API-->>fetchSuggestions: "response seq=1"
    fetchSuggestions->>fetchSuggestions: "requestSeqRef 2 != seq 1 discard"

    API-->>fetchSuggestions: "response seq=2"
    fetchSuggestions->>fetchSuggestions: "requestSeqRef 2 = seq 2 apply"
    fetchSuggestions->>fetchSuggestions: setSuggestions setShowDropdown

    User->>detectMention: cursor leaves mention context
    detectMention->>debounce: clearTimeout
    detectMention->>detectMention: "requestSeqRef++ =3"
    detectMention->>fetchSuggestions: setSuggestions empty setShowDropdown false
Loading

Reviews (3): Last reviewed commit: "fix: clear mention suggestions on failed..." | Re-trigger Greptile

Comment thread src/components/ui/mention-textarea.test.tsx
Comment thread src/components/ui/mention-textarea.tsx
@adliebe
Copy link
Copy Markdown
Author

adliebe commented May 27, 2026

Addressed Greptile's follow-up notes.\n\nChanges pushed in 96ec180:\n- close the suggestions dropdown on the current error path\n- switch the stale-response regression to fake timers so it no longer waits on real debounce time\n\nValidation after the follow-up:\n- node_modules/.bin/vitest.cmd run src/components/ui/mention-textarea.test.tsx -> 2 passed\n- node_modules/.bin/eslint.cmd --quiet -- src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed\n- node_modules/.bin/tsc.cmd --noEmit -> passed\n- node_modules/.bin/prettier.cmd --check src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed\n- git diff --check -- src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed

@adliebe
Copy link
Copy Markdown
Author

adliebe commented May 27, 2026

Addressed the latest Greptile note on the non-ok HTTP path.\n\nChanges pushed in d1c3954:\n- clear stale suggestions and close the dropdown when the active mention search returns a non-2xx response\n- add a regression test that first renders a successful suggestion, then verifies a failed active search clears it\n\nValidation after the follow-up:\n- node_modules/.bin/vitest.cmd run src/components/ui/mention-textarea.test.tsx -> 3 passed\n- node_modules/.bin/eslint.cmd --quiet -- src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed\n- node_modules/.bin/tsc.cmd --noEmit -> passed\n- node_modules/.bin/prettier.cmd --check src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed\n- git diff --check -- src/components/ui/mention-textarea.tsx src/components/ui/mention-textarea.test.tsx -> passed

@adliebe
Copy link
Copy Markdown
Author

adliebe commented May 27, 2026

Final automated review state is green on d1c3954.\n\n- Greptile Review -> success, confidence 5/5, safe to merge\n- Socket Security: Project Report -> success\n- Socket Security: Pull Request Alerts -> success\n\nNo remaining review action is open from my side.

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.

Mention suggestions can show stale results after query changes

1 participant