Skip to content

Commit d1bc24e

Browse files
t3dotggclaude
andcommitted
fix(prompt-links): a pull request match does not wait for later environments
The first match in project order now opens without waiting for the lookups after it, on web and mobile. Web mounts the lookup only while it decides, so "Open it again" asks again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 21ad6d5 commit d1bc24e

4 files changed

Lines changed: 40 additions & 27 deletions

File tree

‎apps/mobile/src/features/prompt-links/usePromptLinkResolver.ts‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,29 @@ export function usePromptLinkResolver(params: Readonly<Record<string, unknown>>
122122
.filter((environment) => environment.connectionState === "connected")
123123
.map((environment) => environment.environmentId),
124124
);
125-
const found = await Promise.all(
126-
projects
127-
.filter((project) => connected.has(project.environmentId))
128-
.map(async (project) => {
129-
const linked = await loadLinkedThreads({
130-
environmentId: project.environmentId,
131-
input: {
132-
projectId: project.id,
133-
repository: promptLinkRepositoryName(pullRequest),
134-
number: pullRequest.number,
135-
},
136-
});
137-
const thread =
138-
linked._tag === "Success"
139-
? linked.value.threads.find((candidate) => candidate.archivedAt === null)
140-
: undefined;
141-
return thread && { environmentId: project.environmentId, id: thread.id };
142-
}),
143-
);
144-
return found.find((thread) => thread !== undefined);
125+
const lookups = projects
126+
.filter((project) => connected.has(project.environmentId))
127+
.map(async (project) => {
128+
const linked = await loadLinkedThreads({
129+
environmentId: project.environmentId,
130+
input: {
131+
projectId: project.id,
132+
repository: promptLinkRepositoryName(pullRequest),
133+
number: pullRequest.number,
134+
},
135+
});
136+
const thread =
137+
linked._tag === "Success"
138+
? linked.value.threads.find((candidate) => candidate.archivedAt === null)
139+
: undefined;
140+
return thread && { environmentId: project.environmentId, id: thread.id };
141+
});
142+
// The most recent project wins without waiting for the lookups after it.
143+
for (const lookup of lookups) {
144+
const thread = await lookup;
145+
if (thread) return thread;
146+
}
147+
return undefined;
145148
};
146149

147150
const openProjectDraft = async (project: EnvironmentProject, request: LinkedDraftRequest) => {

‎apps/web/src/components/promptLinks/NewThreadLinkView.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,13 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
259259
ready && link.kind === "pull-request"
260260
? findPromptLinkPullRequestThread(threads, link.pullRequest)
261261
: undefined;
262+
// Mounted only while deciding, so "Open it again" asks the server again.
262263
const lookupProjects =
263-
ready && link.kind === "pull-request" && localPullRequestThread === undefined
264+
ready &&
265+
state.status === "opening" &&
266+
!started &&
267+
link.kind === "pull-request" &&
268+
localPullRequestThread === undefined
264269
? findPromptLinkProjects(recentProjects(), link.pullRequest)
265270
: [];
266271
const serverPullRequestThread = useAtomValue(

‎packages/client-runtime/src/promptLinks.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("findPromptLinkProjects", () => {
4242
});
4343

4444
describe("pickPromptLinkPullRequestThread", () => {
45-
it("waits for every fresh answer, then takes the first open thread", () => {
45+
it("takes the first open thread in order once every earlier lookup has answered", () => {
4646
const laptop = EnvironmentId.make("laptop");
4747
const server = EnvironmentId.make("server");
4848
const linked = (
@@ -61,6 +61,12 @@ describe("pickPromptLinkPullRequestThread", () => {
6161
// A value shown while it refetches is not an answer yet.
6262
expect(pick(AsyncResult.success(none, { waiting: true }))).toBeUndefined();
6363
expect(pick(AsyncResult.success(none), AsyncResult.initial(true))).toBeUndefined();
64+
expect(pick(AsyncResult.initial(true), AsyncResult.success(linked("b")))).toBeUndefined();
65+
const laptopThread = { environmentId: laptop, id: ThreadId.make("a") };
66+
expect(pick(AsyncResult.success(linked("a")), AsyncResult.initial(true))).toEqual(laptopThread);
67+
expect(pick(AsyncResult.success(linked("a")), AsyncResult.success(linked("b")))).toEqual(
68+
laptopThread,
69+
);
6470
expect(pick(AsyncResult.success(none), AsyncResult.success(linked("old")))).toEqual({
6571
environmentId: server,
6672
id: ThreadId.make("old"),

‎packages/client-runtime/src/promptLinks.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,24 @@ export function findPromptLinkPullRequestThread<
6060
}
6161

6262
/**
63-
* The first open thread from per-environment pull request lookups, in the order given. `null`
64-
* when none has one; `undefined` until every lookup has answered.
63+
* The first open thread from per-environment pull request lookups, in the order given, once
64+
* every lookup before it has answered. `null` when none has one; `undefined` until then.
6565
*/
6666
export function pickPromptLinkPullRequestThread(
6767
lookups: ReadonlyArray<{
6868
readonly environmentId: EnvironmentId;
6969
readonly result: AsyncResult.AsyncResult<PullRequestLinkedThreadsResult, unknown>;
7070
}>,
7171
): { readonly environmentId: EnvironmentId; readonly id: ThreadId } | null | undefined {
72-
let found: { readonly environmentId: EnvironmentId; readonly id: ThreadId } | null = null;
7372
for (const { environmentId, result } of lookups) {
7473
if (result._tag === "Initial" || result.waiting) return undefined;
7574
const thread =
7675
result._tag === "Success"
7776
? result.value.threads.find((candidate) => candidate.archivedAt === null)
7877
: undefined;
79-
if (found === null && thread !== undefined) found = { environmentId, id: thread.id };
78+
if (thread !== undefined) return { environmentId, id: thread.id };
8079
}
81-
return found;
80+
return null;
8281
}
8382

8483
const PROVIDER_ALIASES: Record<string, string> = { claude: "claudeagent" };

0 commit comments

Comments
 (0)