The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 17:00 UTC
perf(shared): avoid repeated date parsing when selecting PRs (#22891)
Utkarsh Patil committed
Oct 25, 2026 at 19:33 UTC
87d22b00f75a5d45edd219cd68220313c6bb7edd
2 files changed
+71
-11
packages/client-runtime/src/threadPullRequests.test.ts
+49
@@ -104,6 +104,55 @@ describe("threadPullRequestKeysEqual", () => {
104
});
105
106
describe("resolveThreadCurrentPullRequest", () => {
107
+ it("selects the same terminal link as stable sorting across input orders", () => {
108
+ const dates = ["invalid", "1969-01-01T00:00:00.000Z", "2026-01-01T00:00:00.000Z"];
109
+ for (let offset = 0; offset < dates.length; offset++) {
110
+ const links = Array.from({ length: 12 }, (_, index) =>
111
+ link(index + 1, {
112
+ snapshot: snapshot({
113
+ state: "merged",
114
+ updatedAt: dates[(index + offset) % dates.length]!,
115
+ }),
116
+ }),
117
+ );
118
+ for (const input of [links, [...links].sort((left, right) => right.number - left.number)]) {
119
+ const expected = [...input].sort(
120
+ (left, right) =>
121
+ (Date.parse(right.snapshot?.updatedAt ?? right.linkedAt) || 0) -
122
+ (Date.parse(left.snapshot?.updatedAt ?? left.linkedAt) || 0),
123
+ )[0];
124
+ expect(resolveThreadCurrentPullRequest(input)).toEqual({ kind: "single", link: expected });
125
+ }
126
+ }
127
+ });
128
+
129
+ it("preserves input order when open chains have equal link dates", () => {
130
+ const linkedAt = "2026-01-01T00:00:00.000Z";
131
+ const current = resolveThreadCurrentPullRequest([
132
+ link(3, { linkedAt }),
133
+ link(1, { linkedAt }),
134
+ link(2, { linkedAt }),
135
+ ]);
136
+ expect(current?.kind === "stack" ? current.open.map((entry) => entry.number) : null).toEqual([
137
+ 3, 1, 2,
138
+ ]);
139
+ });
140
+
141
+ it("keeps the first terminal link on ties and treats invalid update dates as zero", () => {
142
+ const first = link(3, { snapshot: snapshot({ state: "merged", updatedAt: "invalid" }) });
143
+ const second = link(1, {
144
+ snapshot: snapshot({ state: "closed", updatedAt: "1970-01-01T00:00:00.000Z" }),
145
+ });
146
+ expect(resolveThreadCurrentPullRequest([first, second])).toEqual({
147
+ kind: "single",
148
+ link: first,
149
+ });
150
+ expect(resolveThreadCurrentPullRequest([second, first])).toEqual({
151
+ kind: "single",
152
+ link: second,
153
+ });
154
+ });
155
+
156
it("prefers the single open link over terminal ones", () => {
157
const current = resolveThreadCurrentPullRequest([
158
link(1, { snapshot: snapshot({ state: "merged" }) }),
packages/source-control-core/src/shared/threadPullRequests.ts
+22
-11
@@ -151,22 +151,33 @@ export function makeThreadPullRequests(
151
if (open.length > 1) {
152
const openChains = chains
153
.map((chain) => layersFromTop(chain).filter(isOpen))
154
- .filter((layers) => layers.length > 0)
155
- .sort(
156
- (left, right) =>
157
- Math.max(...right.map((link) => Date.parse(link.linkedAt))) -
158
- Math.max(...left.map((link) => Date.parse(link.linkedAt))),
159
- );
160
- const ordered = openChains.flat();
154
+ .filter((layers) => layers.length > 0);
155
+ const ordered =
156
+ openChains.length === 1
157
+ ? openChains[0]!
158
+ : openChains
159
+ .map((layers) => ({
160
+ layers,
161
+ newestLinkedAt: Math.max(...layers.map((link) => Date.parse(link.linkedAt))),
162
+ }))
163
+ .sort((left, right) => right.newestLinkedAt - left.newestLinkedAt)
164
+ .flatMap((chain) => chain.layers);
165
return { kind: "stack", open: ordered, top: ordered[0]! };
166
}
167
if (chains.length === 1) {
168
return { kind: "single", link: layersFromTop(chains[0]!)[0]! };
169
}
166
- const terminal = [...visible].sort(
167
- (left, right) => latestUpdatedAt(right) - latestUpdatedAt(left),
168
- );
169
- return { kind: "single", link: terminal[0]! };
170
+ let latest = visible[0]!;
171
+ let latestTimestamp = latestUpdatedAt(latest);
172
+ for (let index = 1; index < visible.length; index++) {
173
+ const link = visible[index]!;
174
+ const timestamp = latestUpdatedAt(link);
175
+ if (timestamp > latestTimestamp) {
176
+ latest = link;
177
+ latestTimestamp = timestamp;
178
+ }
179
+ }
180
+ return { kind: "single", link: latest };
181
}
182
183
function resolveThreadCurrentPullRequestLink(