fix(app): prevent infinite loop in @-mention scan when @ is at index 0

`findActiveFileMention` walked back through `@` candidates with
`lastIndexOf("@", atIndex - 1)`, but per spec a negative `position`
clamps to 0 — so once `atIndex` hit 0 with an invalid query (e.g. a
trailing space), the loop stepped from 0 back to 0 forever, freezing
the JS thread on every keystroke and forcing a quit.

Refs #596, #425
This commit is contained in:
Mohamed Boudra
2026-04-28 19:16:04 +07:00
parent 9fb87b9d86
commit 013e53881b
2 changed files with 9 additions and 1 deletions
@@ -36,6 +36,14 @@ describe("findActiveFileMention", () => {
}); });
expect(mention).toBeNull(); expect(mention).toBeNull();
}); });
it("returns null when @ at start is followed by a delimiter", () => {
const mention = findActiveFileMention({
text: "@ ",
cursorIndex: 2,
});
expect(mention).toBeNull();
});
}); });
describe("applyFileMentionReplacement", () => { describe("applyFileMentionReplacement", () => {
@@ -24,7 +24,7 @@ export function findActiveFileMention(input: FindActiveFileMentionInput): FileMe
for ( for (
let atIndex = beforeCursor.lastIndexOf("@"); let atIndex = beforeCursor.lastIndexOf("@");
atIndex >= 0; atIndex >= 0;
atIndex = beforeCursor.lastIndexOf("@", atIndex - 1) atIndex = atIndex === 0 ? -1 : beforeCursor.lastIndexOf("@", atIndex - 1)
) { ) {
const query = beforeCursor.slice(atIndex + 1); const query = beforeCursor.slice(atIndex + 1);
if (INVALID_MENTION_QUERY_CHARS.test(query)) { if (INVALID_MENTION_QUERY_CHARS.test(query)) {