Skip to content

Commit 9dc8702

Browse files
fix(composer): harden mention index and PR context ids
Close Macroscope/CodeRabbit review gaps: cold-scope hot race, atomic sync in-flight, host-scoped Jira ids, Figma trailing punctuation, exact Co-authored-by: Cursor <[email protected]> #N ranking, indexed thread select, and pull-request draft context ids.
1 parent b9cc92f commit 9dc8702

13 files changed

Lines changed: 129 additions & 37 deletions

‎apps/server/src/mention/MentionIndex.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ export const make = Effect.gen(function* () {
198198
});
199199
return published;
200200
}
201+
// Disk miss can race a concurrent markSyncing/replaceScope; prefer any hot publish.
202+
const raced = (yield* Ref.get(hot)).get(scopeKey);
203+
if (raced) return raced;
201204
return {
202205
rows: [],
203206
meta: { syncedAt: 0, status: "empty" },

‎apps/server/src/mention/MentionIndexPrSyncer.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,14 @@ const make = Effect.gen(function* () {
121121
if (!stale) return;
122122

123123
const key = String(projectId);
124-
const running = yield* Ref.get(inFlight);
125-
if (running.has(key)) return;
124+
const claimed = yield* Ref.modify(inFlight, (set) => {
125+
if (set.has(key)) return [false, set] as const;
126+
const next = new Set(set);
127+
next.add(key);
128+
return [true, next] as const;
129+
});
130+
if (!claimed) return;
126131

127-
yield* Ref.update(inFlight, (set) => new Set(set).add(key));
128132
yield* syncProject(projectId).pipe(
129133
Effect.ensuring(
130134
Ref.update(inFlight, (set) => {

‎apps/server/src/mention/MentionIndexService.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const make = Effect.gen(function* () {
4141
}),
4242

4343
ensureIndexed: Effect.fn("MentionIndexService.ensureIndexed")(function* (projectId) {
44-
yield* pullRequests.syncProject(projectId);
44+
// Honor freshness / in-flight guards; do not force a blocking full sync.
45+
yield* pullRequests.ensureIndexed(projectId);
4546
return yield* index.search({
4647
projectId,
4748
providers: ["pull-request"],

‎apps/server/src/mention/MentionIndexThreadSyncer.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const make = Effect.gen(function* () {
5959
label: title.slice(0, 200),
6060
description: title.slice(0, 500),
6161
url: `t3-thread://${environmentId}/${threadId}`,
62-
tokens: foldTokens([title, String(threadId), String(environmentId), String(projectId)]),
62+
tokens: foldTokens([title]),
6363
thread: {
6464
environmentId,
6565
threadId,
@@ -87,10 +87,14 @@ const make = Effect.gen(function* () {
8787
if (!stale) return;
8888

8989
const key = String(projectId);
90-
const running = yield* Ref.get(inFlight);
91-
if (running.has(key)) return;
90+
const claimed = yield* Ref.modify(inFlight, (set) => {
91+
if (set.has(key)) return [false, set] as const;
92+
const next = new Set(set);
93+
next.add(key);
94+
return [true, next] as const;
95+
});
96+
if (!claimed) return;
9297

93-
yield* Ref.update(inFlight, (set) => new Set(set).add(key));
9498
yield* syncProject(projectId).pipe(
9599
Effect.ensuring(
96100
Ref.update(inFlight, (set) => {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ import {
172172
uploadedAttachmentContextRecord,
173173
previewAnnotationContextId,
174174
previewAnnotationContextRecord,
175-
reviewCommentContextId,
175+
reviewCommentDraftContextId,
176176
reviewCommentContextReference,
177177
reviewCommentContextRecord,
178178
terminalContextReference,
@@ -1340,7 +1340,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
13401340
...composerJiraContexts.filter((record) => wanted.has(record.contextId)),
13411341
...composerFigmaContexts.filter((record) => wanted.has(record.contextId)),
13421342
...composerReviewComments
1343-
.filter((c) => wanted.has(reviewCommentContextId(c.id)))
1343+
.filter((c) => wanted.has(reviewCommentDraftContextId(c)))
13441344
.map(reviewCommentContextRecord),
13451345
...composerPreviewAnnotations
13461346
.filter((a) => wanted.has(previewAnnotationContextId(a.id)))
@@ -1727,14 +1727,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
17271727
}
17281728

17291729
for (const comment of composerReviewComments) {
1730-
const contextId = reviewCommentContextId(comment.id);
1730+
const contextId = reviewCommentDraftContextId(comment);
17311731
if (!referenced.has(contextId)) {
17321732
retained.reviewComments.set(contextId, comment);
17331733
removeComposerDraftReviewComment(composerDraftTarget, comment.id);
17341734
}
17351735
}
17361736
const liveReviewIds = new Set<string>(
1737-
composerReviewComments.map((comment) => reviewCommentContextId(comment.id)),
1737+
composerReviewComments.map((comment) => reviewCommentDraftContextId(comment)),
17381738
);
17391739
for (const contextId of referenced) {
17401740
if (liveReviewIds.has(contextId)) continue;
@@ -2016,8 +2016,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
20162016
if (item.type === "thread") {
20172017
if (trigger.kind !== "path") return;
20182018
const shell = readThreadShell(item.thread);
2019-
if (!shell) return;
2020-
const record = threadContextRecord(item.thread, shell.title);
2019+
const record = threadContextRecord(item.thread, shell?.title ?? item.label);
20212020
const replacement = `${formatInlineContextReference(threadContextReference(record))} `;
20222021
const replacementRangeEnd = extendReplacementRangeForTrailingSpace(
20232022
snapshot.value,

‎apps/web/src/components/chat/useComposerContextImport.ts‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import {
2929
previewAnnotationContextId,
3030
previewAnnotationContextRecord,
3131
previewAnnotationFromRecord,
32-
reviewCommentContextId,
32+
reviewCommentDraftContextId,
3333
reviewCommentContextRecord,
34+
reviewCommentContextReference,
3435
reviewCommentFromRecord,
3536
terminalContextDraftFromRecord,
3637
terminalContextRecord,
@@ -52,6 +53,7 @@ import {
5253
type ComposerDraftContextRecords,
5354
uploadedContextRecordFromDraft,
5455
} from "../composerContextPresentation";
56+
import { buildPullRequestReferenceContext } from "../pullRequest/pullRequestDetail.logic";
5557
import { useAtomCommand } from "../../state/use-atom-command";
5658
import { useAtomQueryRunner } from "~/state/use-atom-query-runner";
5759
import { useStableCallback } from "../../hooks/useStableCallback";
@@ -339,14 +341,16 @@ export function useComposerContextImport(input: {
339341
? existing.record.id
340342
: undefined,
341343
})
342-
: existing?.kind === "thread" ||
343-
existing?.kind === "environment" ||
344-
existing?.kind === "jira" ||
345-
existing?.kind === "figma"
346-
? existing.record
347-
: existing
348-
? (uploadedContextRecordFromDraft(existing) ?? undefined)
349-
: undefined;
344+
: existing?.kind === "pull-request"
345+
? reviewCommentContextRecord(existing.record)
346+
: existing?.kind === "thread" ||
347+
existing?.kind === "environment" ||
348+
existing?.kind === "jira" ||
349+
existing?.kind === "figma"
350+
? existing.record
351+
: existing
352+
? (uploadedContextRecordFromDraft(existing) ?? undefined)
353+
: undefined;
350354
if (
351355
existingRecord &&
352356
isSameComposerContextPayload(existingRecord, record) &&
@@ -376,7 +380,26 @@ export function useComposerContextImport(input: {
376380
addComposerDraftReviewComment(composerDraftTarget, comment, {
377381
appendReference: false,
378382
});
379-
rewritten.set(record.contextId, reviewCommentContextId(comment.id));
383+
rewritten.set(record.contextId, reviewCommentDraftContextId(comment));
384+
break;
385+
}
386+
case "pull-request": {
387+
// Question answers carry no PR chips; keep them off the main draft while answering.
388+
if (questionAttachmentTarget !== null) break;
389+
const imported = buildPullRequestReferenceContext({
390+
number: record.number,
391+
title: record.title,
392+
url: record.url,
393+
headBranch: record.headBranch,
394+
baseBranch: record.baseBranch,
395+
state: record.state,
396+
isDraft: record.isDraft,
397+
});
398+
const comment = conflicts ? { ...imported, id: randomUUID() } : imported;
399+
addComposerDraftReviewComment(composerDraftTarget, comment, {
400+
appendReference: false,
401+
});
402+
rewritten.set(record.contextId, reviewCommentContextReference(comment).contextId);
380403
break;
381404
}
382405
case "element":

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
} from "@t3tools/client-runtime/thread-pull-requests";
3939

4040
import { inferReviewCommentFenceLanguage, type ReviewCommentContext } from "~/reviewCommentContext";
41-
import { reviewCommentContextId } from "~/lib/composerContextRecords";
41+
import { reviewCommentDraftContextId } from "~/lib/composerContextRecords";
4242
import { removeInlineContextReference } from "~/lib/composerContextReferences";
4343

4444
export const PULL_REQUEST_MERGE_METHOD_LABELS: Record<PullRequestMergeMethod, string> = {
@@ -835,7 +835,7 @@ export function stripPullRequestHandoffReferences(
835835
let next = prompt;
836836
for (const comment of comments) {
837837
if (!comment.id.startsWith(HANDOFF_COMMENT_ID_PREFIX) || retainedIds.has(comment.id)) continue;
838-
next = removeInlineContextReference(next, reviewCommentContextId(comment.id)).prompt;
838+
next = removeInlineContextReference(next, reviewCommentDraftContextId(comment)).prompt;
839839
}
840840
return next;
841841
}

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import {
8787
fileContextReference,
8888
previewAnnotationContextId,
8989
previewAnnotationContextReference,
90-
reviewCommentContextId,
90+
reviewCommentDraftContextId,
9191
reviewCommentContextReference,
9292
terminalContextReference,
9393
threadContextReference,
@@ -3879,7 +3879,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
38793879
if (retainedIds.has(previous.id)) continue;
38803880
prompt = removeInlineContextReference(
38813881
prompt,
3882-
reviewCommentContextId(previous.id),
3882+
reviewCommentDraftContextId(previous),
38833883
).prompt;
38843884
}
38853885
prompt = ensureInlineContextReferences(
@@ -4162,13 +4162,16 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
41624162
set((state) => {
41634163
const current = state.draftsByThreadKey[threadKey];
41644164
if (!current) return state;
4165+
const removed = current.reviewComments.find((entry) => entry.id === commentId);
41654166
const reviewComments = current.reviewComments.filter((entry) => entry.id !== commentId);
4166-
if (reviewComments.length === current.reviewComments.length) return state;
4167+
if (reviewComments.length === current.reviewComments.length || removed === undefined) {
4168+
return state;
4169+
}
41674170
const nextDraft = {
41684171
...current,
41694172
prompt: removeInlineContextReference(
41704173
current.prompt,
4171-
reviewCommentContextId(commentId),
4174+
reviewCommentDraftContextId(removed),
41724175
).prompt,
41734176
reviewComments,
41744177
};

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ export function reviewCommentContextReference(
204204
};
205205
}
206206

207+
/** Draft lookup id for a review comment, including pull-request summary chips. */
208+
export function reviewCommentDraftContextId(comment: ReviewCommentContext): ComposerContextId {
209+
if (isPullRequestSummaryContext(comment) && comment.pullRequest !== undefined) {
210+
return pullRequestContextId(comment.id);
211+
}
212+
return reviewCommentContextId(comment.id);
213+
}
214+
207215
export function previewAnnotationContextReference(
208216
annotation: PreviewAnnotationPayload,
209217
): ComposerContextReference {

‎packages/shared/src/composerContextExternalLinks.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ describe("composerContextExternalLinks", () => {
2525
});
2626
});
2727

28+
it("keeps trailing prose punctuation outside a Figma URL rewrite", () => {
29+
const rewritten = externalLinkContextReferences(
30+
"See https://www.figma.com/design/AbCdEf123/Flow?node-id=1-2).",
31+
);
32+
expect(rewritten.records).toHaveLength(1);
33+
expect(rewritten.records[0]).toMatchObject({ kind: "figma", nodeId: "1:2" });
34+
expect(rewritten.text.endsWith(").")).toBe(true);
35+
});
36+
37+
it("scopes Jira context ids by site host", () => {
38+
const rewritten = externalLinkContextReferences(
39+
"https://acme.atlassian.net/browse/APP-1 https://other.atlassian.net/browse/APP-1",
40+
);
41+
expect(rewritten.records).toHaveLength(2);
42+
expect(new Set(rewritten.records.map((record) => record.contextId)).size).toBe(2);
43+
});
44+
2845
it("rejects Figma URLs with malformed percent escapes", () => {
2946
expect(parseFigmaDesignUrl("https://www.figma.com/design/AbCdEf123/%")).toBeNull();
3047
expect(

0 commit comments

Comments
 (0)