fix(app): only attach comment @mentions that exist (#62242)

This commit is contained in:
DS
2026-11-06 17:27:19 -05:00
committed by GitHub
Unverified
parent 4db59339e3
commit 91732f2cf7
8 changed files with 49 additions and 4 deletions
+5
View File
@@ -83,6 +83,11 @@ export type ComposerSession = {
) => ReturnType<ServerSDK["api"]["session"]["revert"]["commit"]>
}
}
file: {
list: (
input: Parameters<ServerSDK["api"]["file"]["list"]>[0],
) => Promise<{ data: readonly { path: string }[] }>
}
data: {
location: { command: Pick<Data["location"]["command"], "list"> }
session: {
@@ -43,6 +43,7 @@ const session: ComposerSession = {
switchModel: async () => undefined,
revert: { commit: async () => undefined },
},
file: { list: async () => ({ data: [] }) },
data: {
location: { command: { list: () => [] } },
session: {
+9
View File
@@ -47,6 +47,8 @@ type BuildPromptRequestInput = {
images: (Omit<ImageAttachmentPart, "blob"> & { dataUrl: string })[]
text: string
sessionDirectory: string
// Comment mentions that name an existing file; when given, the others stay text and are not attached.
existingMentions?: ReadonlySet<string>
}
const absolute = (directory: string, path: string) => {
@@ -74,6 +76,11 @@ const parseCommentMentions = (comment: string) => {
})
}
/** The paths that comments in the context mention with `@`, as written. */
export function commentMentions(context: ContextItem[]) {
return context.flatMap((item) => (item.comment ? parseCommentMentions(item.comment) : []))
}
const isFileAttachment = (part: Prompt[number]): part is FileAttachmentPart => part.type === "file"
const isAgentAttachment = (part: Prompt[number]): part is AgentPart => part.type === "agent"
@@ -151,6 +158,8 @@ export function buildPromptRequest(input: BuildPromptRequestInput): PromptReques
const mentioned = (comment: string) =>
parseCommentMentions(comment).flatMap((path) => {
if (input.existingMentions && !input.existingMentions.has(path)) return []
const uri = `file://${encodeFilePath(absolute(input.sessionDirectory, path))}`
if (used.has(uri)) return []
+4 -3
View File
@@ -152,6 +152,7 @@ function session(input: {
},
},
},
file: { list: async () => ({ data: [{ path: "src/api.ts" }] }) },
data: {
location: { command: { list: () => [] } },
session: {
@@ -296,11 +297,11 @@ describe("Composer submission", () => {
},
)
expect(await submission.submitQuote("quote:c=selected", "Compare @src/api.ts")).toBe(true)
expect(await submission.submitQuote("quote:c=selected", "Compare @src/api.ts with @here")).toBe(true)
expect(calls[0]).toBe("keep-preview")
expect(requests).toHaveLength(1)
expect(requests[0]?.text).toBe(
"The user quoted the following from earlier in the conversation:\n> Selected passage\n\nThe user made the following comment regarding this quote: Compare @src/api.ts",
"The user quoted the following from earlier in the conversation:\n> Selected passage\n\nThe user made the following comment regarding this quote: Compare @src/api.ts with @here",
)
expect(requests[0]).toMatchObject({
delivery,
@@ -309,7 +310,7 @@ describe("Composer submission", () => {
skills: [],
metadata: {
displayText: "",
comments: [{ type: "quote", text: "Selected passage", comment: "Compare @src/api.ts" }],
comments: [{ type: "quote", text: "Selected passage", comment: "Compare @src/api.ts with @here" }],
attachments: [],
},
})
+27 -1
View File
@@ -12,7 +12,8 @@ import type {
ComposerSession,
ComposerSubmissionHandle,
} from "./adapter"
import { buildPromptRequest, noteComment, quoteComment } from "./request"
import { buildPromptRequest, commentMentions, noteComment, quoteComment } from "./request"
import { getDirectory, getFilename } from "@opencode/util/path"
import { setCursorPosition } from "./editor/dom"
import { blobDataUrl, resolveBlobUrl } from "@/runtime/persistence/drafts"
import type { ModelSelection } from "@/providers/models/selection"
@@ -827,15 +828,40 @@ async function buildSubmissionRequest(session: ComposerSession, value: ComposerS
})),
)
const mentions = commentMentions(value.context)
return buildPromptRequest({
prompt: value.prompt,
context: value.context,
images,
text: value.text,
sessionDirectory: session.directory,
existingMentions: mentions.length > 0 ? await existingMentions(session, mentions) : undefined,
})
}
// A comment's `@word` may be plain text such as `@here`, and the server rejects a prompt that attaches a missing
// file. Lists each mention's folder instead of reading the file, so the check transfers no content.
async function existingMentions(session: ComposerSession, mentions: string[]) {
const existing = new Set<string>()
await Promise.all(
[...new Set(mentions)].map(async (path) => {
const target = path.replace(/[\\/]+$/, "")
const parent = /[\\/]/.test(target) ? getDirectory(target) : undefined
const found = await session.file
.list({ path: parent, location: { directory: session.directory } })
.then((result) => result.data.some((entry) => getFilename(entry.path) === getFilename(target)))
.catch(() => false)
if (found) existing.add(path)
}),
)
return existing
}
function failSubmission(
input: ComposerSubmitInput,
session: ComposerSession,
@@ -280,6 +280,7 @@ export function createNewSessionComposerAdapter(props: {
directory,
handoff: server.ctx.handoff.forSession(id),
api: serverSDK.api.session,
file: serverSDK.api.file,
data,
current: () => data.session.get(id),
admitted: (messageID) => data.session.input.has(id, messageID) || !!data.session.message.get(id, messageID),
@@ -47,6 +47,7 @@ export function createActiveComposerAdapter(input: {
directory: location().directory,
handoff,
api: server.api.session,
file: server.api.file,
data,
current: () => data.session.get(id),
admitted: (messageID) => data.session.input.has(id, messageID) || !!data.session.message.get(id, messageID),
@@ -137,6 +137,7 @@ test.each<{
switchModel: async () => undefined,
revert: { commit: async () => undefined },
},
file: { list: async () => ({ data: [] }) },
data: {
location: { command: { list: () => [] } },
session: {