The best way to control Claude Code, Codex and more from any computer or phone
fix(shared): PRs built on the same PR stay in one stack under their parent (#20160)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Oct 16, 2026 at 12:35 UTC
a9c9c7cdb15e695ce72c4afc55fde93f994f6134
7 files changed
+128
-28
apps/mobile/src/features/threads/git/GitOverviewSheet.tsx
+5
-1
@@ -375,7 +375,11 @@ function GitOverviewSheetContent(props: GitOverviewSheetProps) {
375
</View>
376
) : null}
377
{chain.layers.map((link, index) => (
378
- <View key={threadPullRequestKeyOf(link)}>
378
+ <View
379
+ key={threadPullRequestKeyOf(link)}
380
+ // Steps in per layer like the web list, capped so deep stacks keep their width.
381
+ style={{ paddingLeft: Math.min(chain.depths[index]!, 3) * 16 }}
382
+ >
383
{index > 0 && Platform.OS !== "android" ? (
384
<View className="ml-12 h-px bg-border" />
385
) : null}
apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts
+16
@@ -574,6 +574,22 @@ describe("listThreadPullRequests", () => {
574
]);
575
expect(result.chains).toEqual([{ kind: "native", numbers: [1, 2] }]);
576
});
577
+
578
+ it("gives pull requests built on the same one the same position", () => {
579
+ const result = listThreadPullRequests({
580
+ pullRequests: [
581
+ makeLink(1, { headBranch: "feat-a", baseBranch: "main" }),
582
+ makeLink(2, { headBranch: "feat-b", baseBranch: "feat-a" }),
583
+ makeLink(3, { headBranch: "feat-c", baseBranch: "feat-a" }),
584
+ ],
585
+ });
586
+ expect(result.pullRequests.map((entry) => [entry.number, entry.stack])).toEqual([
587
+ [1, { kind: "derived", position: 1, size: 3 }],
588
+ [2, { kind: "derived", position: 2, size: 3 }],
589
+ [3, { kind: "derived", position: 2, size: 3 }],
590
+ ]);
591
+ expect(result.chains).toEqual([{ kind: "derived", numbers: [1, 2, 3] }]);
592
+ });
593
});
594
595
describe("pull request guide tools", () => {
apps/server/src/mcp/toolkits/pullRequests/handlers.ts
+1
-1
@@ -114,7 +114,7 @@ function entryOf(
114
if (chain.layers.length < 2) continue;
115
const index = chain.layers.findIndex((layer) => threadPullRequestKeyOf(layer) === key);
116
if (index !== -1) {
117
- stack = { kind: chain.kind, position: index + 1, size: chain.layers.length };
117
+ stack = { kind: chain.kind, position: chain.depths[index]! + 1, size: chain.layers.length };
118
break;
119
}
120
}
apps/server/src/mcp/toolkits/pullRequests/tools.ts
+2
-2
@@ -250,7 +250,7 @@ export const ThreadPullRequestEntry = Schema.Struct({
250
stack: Schema.NullOr(
251
Schema.Struct({
252
kind: Schema.Literals(["native", "derived"]),
253
- /** 1-based, bottom of the stack first. */
253
+ /** 1-based layer, bottom of the stack first. Pull requests built on the same one share it. */
254
position: Schema.Int,
255
size: Schema.Int,
256
}),
@@ -263,7 +263,7 @@ export const ListThreadPullRequestsResult = Schema.Struct({
263
chains: Schema.Array(
264
Schema.Struct({
265
kind: Schema.Literals(["native", "derived"]),
266
- /** Bottom to top. */
266
+ /** Bottom to top; each pull request comes before the ones built on it. */
267
numbers: Schema.Array(Schema.Int),
268
}),
269
),
apps/web/src/components/pullRequest/pullRequestListLines.ts
+3
-3
@@ -27,12 +27,12 @@ export function pullRequestListLines(
27
);
28
return ordered.flatMap((chain) => {
29
const chainKey = chainKeyOf(chain);
30
- return chain.layers.map((link, depth) => ({
30
+ return chain.layers.map((link, index) => ({
31
link,
32
- depth,
32
+ depth: chain.depths[index]!,
33
chainKey,
34
stack:
35
- depth === 0 && chain.layers.length > 1
35
+ index === 0 && chain.layers.length > 1
36
? { kind: chain.kind, size: chain.layers.length }
37
: null,
38
}));
packages/shared/src/threadPullRequests.test.ts
+56
@@ -270,6 +270,51 @@ describe("resolveThreadPullRequestChains", () => {
270
]);
271
});
272
273
+ it("keeps every link built on the same parent under it", () => {
274
+ const links = [
275
+ link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "a" }) }),
276
+ link(5, { snapshot: snapshot({ headBranch: "p", baseBranch: "main" }) }),
277
+ link(6, { snapshot: snapshot({ headBranch: "q", baseBranch: "p" }) }),
278
+ link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "p" }) }),
279
+ link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
280
+ link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
281
+ ];
282
+ const chains = resolveThreadPullRequestChains(links);
283
+ expect(
284
+ chains.map((chain) => [chain.layers.map((layer) => layer.number), chain.depths]),
285
+ ).toEqual([
286
+ // #6 and #1 both build on #5; #3 and #2 both build on #1.
287
+ [
288
+ [5, 6, 1, 3, 2],
289
+ [0, 1, 1, 2, 2],
290
+ ],
291
+ [[9], [0]],
292
+ ]);
293
+ expect(resolveThreadPullRequestBadge(links.slice(0, 5))).toEqual({
294
+ kind: "stack",
295
+ layers: 5,
296
+ state: "open",
297
+ });
298
+ });
299
+
300
+ it("points at the highest layer of a branched stack, not the last sibling listed", () => {
301
+ // #1 → #2 → #3, and #4 also builds on #1.
302
+ const branched = (state: ThreadPullRequestSnapshot["state"]) => [
303
+ link(1, { snapshot: snapshot({ state, headBranch: "a" }) }),
304
+ link(2, { snapshot: snapshot({ state, headBranch: "b", baseBranch: "a" }) }),
305
+ link(3, { snapshot: snapshot({ state, headBranch: "c", baseBranch: "b" }) }),
306
+ link(4, { snapshot: snapshot({ state, headBranch: "d", baseBranch: "a" }) }),
307
+ ];
308
+ const open = resolveThreadCurrentPullRequest(branched("open"));
309
+ expect(open?.kind === "stack" ? open.open.map((entry) => entry.number) : null).toEqual([
310
+ 3, 4, 2, 1,
311
+ ]);
312
+ expect(resolveThreadCurrentPullRequest(branched("merged"))).toMatchObject({
313
+ kind: "single",
314
+ link: { number: 3 },
315
+ });
316
+ });
317
+
318
it("uses the native stack order when the host provides one", () => {
319
const stack = {
320
kind: "native" as const,
@@ -432,6 +477,17 @@ describe("chain selection and badge state", () => {
477
});
478
});
479
480
+ it("lists a pull request once when two links share its identity", () => {
481
+ const links = [
482
+ link(1, { snapshot: snapshot({ headBranch: "a" }) }),
483
+ link(1, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
484
+ link(1, { snapshot: snapshot({ headBranch: "c" }) }),
485
+ ];
486
+ expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual([
487
+ [links[0]],
488
+ ]);
489
+ });
490
+
491
it("does not guess a parent when a head branch was reused", () => {
492
const links = [
493
link(1, { snapshot: snapshot({ state: "merged", headBranch: "reused" }) }),
packages/shared/src/threadPullRequests.ts
+45
-21
@@ -100,6 +100,15 @@ export type ThreadCurrentPullRequest =
100
readonly top: ThreadPullRequestLink;
101
};
102
103
+function layersFromTop(chain: ThreadPullRequestChain): Array<ThreadPullRequestLink> {
104
+ // `.sort()` on a fresh array, not `.toSorted()`: this runs on Hermes, which has no ES2023
105
+ // array methods, and a TypeError here is fatal on every mobile launch that renders a stack.
106
+ return chain.layers
107
+ .map((link, index) => ({ link, index, depth: chain.depths[index]! }))
108
+ .sort((left, right) => right.depth - left.depth || right.index - left.index)
109
+ .map((entry) => entry.link);
110
+}
111
+
112
export function resolveThreadCurrentPullRequest(
113
links: ReadonlyArray<ThreadPullRequestLink>,
114
): ThreadCurrentPullRequest | null {
@@ -109,10 +118,8 @@ export function resolveThreadCurrentPullRequest(
118
if (open.length === 1) return { kind: "single", link: open[0]! };
119
const chains = resolveThreadPullRequestChains(visible);
120
if (open.length > 1) {
112
- // `.reverse()` on a copy, not `.toReversed()`: this runs on Hermes, which has no ES2023
113
- // array methods, and a TypeError here is fatal on every mobile launch that renders a stack.
121
const openChains = chains
115
- .map((chain) => [...chain.layers].reverse().filter(isOpen))
122
+ .map((chain) => layersFromTop(chain).filter(isOpen))
123
.filter((layers) => layers.length > 0)
124
.sort(
125
(left, right) =>
@@ -123,7 +130,7 @@ export function resolveThreadCurrentPullRequest(
130
return { kind: "stack", open: ordered, top: ordered[0]! };
131
}
132
if (chains.length === 1) {
126
- return { kind: "single", link: chains[0]!.layers.at(-1)! };
133
+ return { kind: "single", link: layersFromTop(chains[0]!)[0]! };
134
}
135
const terminal = [...visible].sort(
136
(left, right) => latestUpdatedAt(right) - latestUpdatedAt(left),
@@ -189,6 +196,8 @@ export function legacyLinkedPullRequestOf(
196
export interface ThreadPullRequestChain {
197
readonly kind: "native" | "derived";
198
readonly layers: ReadonlyArray<ThreadPullRequestLink>;
199
+ /** One per layer: 0 for the base, parent's depth + 1 above it. Siblings share a depth. */
200
+ readonly depths: ReadonlyArray<number>;
201
}
202
203
export function resolveThreadPullRequestChains(
@@ -211,7 +220,7 @@ export function resolveThreadPullRequestChains(
220
const order = new Map(members[0]!.stack!.layers.map((layer, index) => [layer.number, index]));
221
members.sort((left, right) => (order.get(left.number) ?? 0) - (order.get(right.number) ?? 0));
222
for (const member of members) placed.add(threadPullRequestKeyOf(member));
214
- chains.push({ kind: "native", layers: members });
223
+ chains.push({ kind: "native", layers: members, depths: members.map((_, index) => index) });
224
}
225
226
const remaining = visible.filter((link) => !placed.has(threadPullRequestKeyOf(link)));
@@ -225,29 +234,44 @@ export function resolveThreadPullRequestChains(
234
const key = branchKey(link, link.snapshot.headBranch);
235
byHead.set(key, byHead.has(key) ? null : link);
236
}
228
- const hasChild = new Set<string>();
229
- for (const link of remaining) {
230
- if (link.snapshot === null) continue;
237
+ const parentOf = (link: ThreadPullRequestLink) => {
238
+ if (link.snapshot === null) return undefined;
239
const parent = byHead.get(branchKey(link, link.snapshot.baseBranch));
232
- if (parent != null && parent !== link) hasChild.add(threadPullRequestKeyOf(parent));
240
+ return parent == null || parent === link ? undefined : parent;
241
+ };
242
+ const children = new Map<string, Array<ThreadPullRequestLink>>();
243
+ for (const link of remaining) {
244
+ const parent = parentOf(link);
245
+ if (parent === undefined) continue;
246
+ const key = threadPullRequestKeyOf(parent);
247
+ const siblings = children.get(key) ?? [];
248
+ siblings.push(link);
249
+ children.set(key, siblings);
250
}
234
- for (const top of remaining) {
235
- if (hasChild.has(threadPullRequestKeyOf(top))) continue;
251
+ for (const base of remaining) {
252
+ if (parentOf(base) !== undefined) continue;
253
const layers: Array<ThreadPullRequestLink> = [];
237
- let cursor: ThreadPullRequestLink | undefined = top;
238
- while (cursor !== undefined && !placed.has(threadPullRequestKeyOf(cursor))) {
239
- placed.add(threadPullRequestKeyOf(cursor));
240
- layers.unshift(cursor);
241
- cursor =
242
- cursor.snapshot === null
243
- ? undefined
244
- : (byHead.get(branchKey(cursor, cursor.snapshot.baseBranch)) ?? undefined);
254
+ const depths: Array<number> = [];
255
+ const pending: Array<{ readonly link: ThreadPullRequestLink; readonly depth: number }> = [
256
+ { link: base, depth: 0 },
257
+ ];
258
+ for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
259
+ const key = threadPullRequestKeyOf(next.link);
260
+ // Two links with one identity would otherwise share a child list and expand forever.
261
+ if (placed.has(key)) continue;
262
+ placed.add(key);
263
+ layers.push(next.link);
264
+ depths.push(next.depth);
265
+ const below = children.get(key) ?? [];
266
+ for (let index = below.length - 1; index >= 0; index -= 1) {
267
+ pending.push({ link: below[index]!, depth: next.depth + 1 });
268
+ }
269
}
246
- if (layers.length > 0) chains.push({ kind: "derived", layers });
270
+ if (layers.length > 0) chains.push({ kind: "derived", layers, depths });
271
}
272
for (const link of remaining) {
273
if (!placed.has(threadPullRequestKeyOf(link))) {
250
- chains.push({ kind: "derived", layers: [link] });
274
+ chains.push({ kind: "derived", layers: [link], depths: [0] });
275
}
276
}
277
return chains;