Skip to content

Commit 7ee6e92

Browse files
saphidcodexgithub-actions[bot]juliusmarminge
authored
fix(web): reject file suggestions from an older composer query (#18845)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <[email protected]>
1 parent 502ce3c commit 7ee6e92

6 files changed

Lines changed: 63 additions & 19 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import {
9090
detectComposerTrigger,
9191
expandCollapsedComposerCursor,
9292
formatAssistantCitationForComposer,
93+
isPathSuggestionCurrent,
9394
replaceTextRange,
9495
} from "../../composer-logic";
9596
import { DISCONNECTED_COMPOSER_PLACEHOLDER } from "../../composerPlaceholder";
@@ -203,7 +204,6 @@ import {
203204
type TerminalContextDraft,
204205
type TerminalContextSelection,
205206
} from "../../lib/terminalContext";
206-
import { useComposerPathSearch } from "../../lib/composerPathSearchState";
207207
import {
208208
replaceComposerContextReferences,
209209
toKindScopedComposerContextId,
@@ -284,7 +284,7 @@ import {
284284
type EnvironmentQueryTarget,
285285
} from "~/state/pullRequests";
286286
import { useEnvironmentQuery } from "~/state/query";
287-
import { useDebouncedValue } from "~/state/queries";
287+
import { useComposerPathSearch, useDebouncedValue } from "~/state/queries";
288288
import { ProviderModelPicker } from "./ProviderModelPicker";
289289
import { resolveModelPickerSelectedModel } from "./ModelPickerContent";
290290
import {
@@ -2636,6 +2636,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
26362636
type: "path" as const,
26372637
path: entry.path,
26382638
pathKind: entry.kind,
2639+
searchTarget: workspaceEntries.searchedTarget,
26392640
label: basenameOfPath(entry.path),
26402641
description: entry.path.slice(0, Math.max(0, entry.path.lastIndexOf("/"))),
26412642
})),
@@ -3809,6 +3810,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38093810
const { snapshot, trigger } = resolveActiveComposerTrigger();
38103811
if (!trigger) return;
38113812
if (item.type === "path") {
3813+
if (!isPathSuggestionCurrent(item.searchTarget, trigger, environmentId, gitCwd)) return;
38123814
const replacement = `${serializeComposerFileLink(item.path)} `;
38133815
const replacementRangeEnd = extendReplacementRangeForTrailingSpace(
38143816
snapshot.value,

‎apps/web/src/components/chat/ComposerCommandMenu.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
2+
import type { ComposerPathSearchTarget } from "@t3tools/client-runtime/state/threads";
23
import {
34
resolveProviderSkillSourceKind,
45
type ProviderSkillSourceKind,
@@ -36,6 +37,7 @@ export type ComposerCommandItem =
3637
type: "path";
3738
path: string;
3839
pathKind: ProjectEntry["kind"];
40+
searchTarget: ComposerPathSearchTarget;
3941
label: string;
4042
description: string;
4143
}

‎apps/web/src/composer-logic.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
expandCollapsedComposerCursor,
2323
formatAssistantCitationForComposer,
2424
isCollapsedCursorAdjacentToInlineToken,
25+
isPathSuggestionCurrent,
2526
parseStandaloneComposerSlashCommand,
2627
replaceTextRange,
2728
} from "./composer-logic";
@@ -48,6 +49,41 @@ const citation: AssistantCitation = {
4849
};
4950
const citationSource = serializeAssistantCitation(citation).replaceAll("+", "%20");
5051

52+
describe("isPathSuggestionCurrent", () => {
53+
const searchedTarget = {
54+
environmentId: EnvironmentId.make("local"),
55+
cwd: "/repo",
56+
query: "alpha",
57+
};
58+
59+
it("allows a file suggestion matching the live search", () => {
60+
expect(
61+
isPathSuggestionCurrent(
62+
searchedTarget,
63+
detectComposerTrigger("@alpha", 6),
64+
searchedTarget.environmentId,
65+
searchedTarget.cwd,
66+
),
67+
).toBe(true);
68+
});
69+
70+
it.each([
71+
["query", { ...searchedTarget, query: "beta" }],
72+
["cwd", { ...searchedTarget, cwd: "/other-repo" }],
73+
["environment", { ...searchedTarget, environmentId: EnvironmentId.make("remote") }],
74+
] as const)("rejects a visible file suggestion after the live %s changes", (_change, current) => {
75+
const prompt = `@${current.query}`;
76+
expect(
77+
isPathSuggestionCurrent(
78+
searchedTarget,
79+
detectComposerTrigger(prompt, prompt.length),
80+
current.environmentId,
81+
current.cwd,
82+
),
83+
).toBe(false);
84+
});
85+
});
86+
5187
describe("formatAssistantCitationForComposer", () => {
5288
it.each([undefined, "", " \n\t "])(
5389
"keeps citation-only insertion for a blank comment %j",

‎apps/web/src/composer-logic.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { ClientSettings } from "@t3tools/contracts/settings";
2-
import type { AssistantCitation, ResolvedKeybindingsConfig } from "@t3tools/contracts";
2+
import type {
3+
AssistantCitation,
4+
EnvironmentId,
5+
ResolvedKeybindingsConfig,
6+
} from "@t3tools/contracts";
7+
import type { ComposerPathSearchTarget } from "@t3tools/client-runtime/state/threads";
38
import {
49
serializeAssistantCitation,
510
withAssistantCitationComment,
@@ -23,6 +28,20 @@ export interface ComposerTrigger {
2328
rangeEnd: number;
2429
}
2530

31+
export function isPathSuggestionCurrent(
32+
searchTarget: ComposerPathSearchTarget,
33+
trigger: ComposerTrigger | null,
34+
environmentId: EnvironmentId | null,
35+
cwd: string | null,
36+
) {
37+
return (
38+
trigger?.kind === "path" &&
39+
searchTarget.environmentId === environmentId &&
40+
searchTarget.cwd === cwd &&
41+
searchTarget.query === trigger.query.trim()
42+
);
43+
}
44+
2645
export function formatAssistantCitationForComposer(citation: AssistantCitation, comment = "") {
2746
return `${serializeAssistantCitation(withAssistantCitationComment(citation, comment))} `;
2847
}

‎apps/web/src/lib/composerPathSearchState.ts‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎apps/web/src/state/queries.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ export function useProjectPathSearch(
272272
isPending:
273273
!areProjectPathSearchTargetsEqual(normalizedTarget, debouncedTarget) ||
274274
(hasTarget && (fileAccess.isPending || result.isPending)),
275+
searchedTarget: debouncedTarget,
275276
searchedQuery: debouncedTarget.query ?? "",
276277
truncated: result.data?.truncated ?? false,
277278
refresh: result.refresh,

0 commit comments

Comments
 (0)