Skip to content

Commit f21b47e

Browse files
authored
fix(threads): a merged PR settles its thread only once (pingdotgg#7454)
1 parent efcf7d1 commit f21b47e

17 files changed

Lines changed: 331 additions & 75 deletions

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type EnvironmentThreadSearchMatch,
1313
} from "@t3tools/client-runtime/state/thread-search";
1414
import { sortPinnedThreadsByOrderKey } from "@t3tools/client-runtime/state/thread-sort";
15+
import type { ChangeRequestSettleSource } from "@t3tools/client-runtime/state/thread-settled";
1516
import type {
1617
EnvironmentId,
1718
SidebarProjectGroupingMode,
@@ -488,18 +489,24 @@ export function HomeScreen(props: HomeScreenProps) {
488489
// optimistic holds.
489490
// PR states stream in per-row. The next partition applies the configured
490491
// merge rule and the always-on close rule, matching web.
491-
const [changeRequestStateByKey, setChangeRequestStateByKey] = useState<
492-
ReadonlyMap<string, "open" | "closed" | "merged">
492+
const [changeRequestByKey, setChangeRequestByKey] = useState<
493+
ReadonlyMap<string, ChangeRequestSettleSource>
493494
>(() => new Map());
494495
const handleChangeRequestState = useCallback(
495-
(threadKey: string, state: "open" | "closed" | "merged" | null) => {
496-
setChangeRequestStateByKey((current) => {
497-
if ((current.get(threadKey) ?? null) === state) return current;
496+
(threadKey: string, changeRequest: ChangeRequestSettleSource | null) => {
497+
setChangeRequestByKey((current) => {
498+
const existing = current.get(threadKey) ?? null;
499+
if (
500+
(existing?.state ?? null) === (changeRequest?.state ?? null) &&
501+
(existing?.updatedAt ?? null) === (changeRequest?.updatedAt ?? null)
502+
) {
503+
return current;
504+
}
498505
const next = new Map(current);
499-
if (state === null) {
506+
if (changeRequest === null) {
500507
next.delete(threadKey);
501508
} else {
502-
next.set(threadKey, state);
509+
next.set(threadKey, changeRequest);
503510
}
504511
return next;
505512
});
@@ -667,7 +674,7 @@ export function HomeScreen(props: HomeScreenProps) {
667674
projectRefs: v2ScopedProjectGroup === null ? null : v2ScopedProjectGroup.projectRefs,
668675
searchQuery: props.searchQuery,
669676
matchedThreadKeys,
670-
changeRequestStateByKey,
677+
changeRequestByKey,
671678
autoSettleOnMerge,
672679
settlementEnvironmentIds,
673680
snoozeEnvironmentIds,
@@ -679,7 +686,7 @@ export function HomeScreen(props: HomeScreenProps) {
679686
selectedThreadKey: null,
680687
});
681688
}, [
682-
changeRequestStateByKey,
689+
changeRequestByKey,
683690
autoSettleOnMerge,
684691
nowMinute,
685692
snoozeWakeTick,

‎apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useAtomValue } from "@effect/atom-react";
1313
import { AsyncResult } from "effect/unstable/reactivity";
1414
import type { EnvironmentId } from "@t3tools/contracts";
1515
import { sortPinnedThreadsByOrderKey } from "@t3tools/client-runtime/state/thread-sort";
16+
import type { ChangeRequestSettleSource } from "@t3tools/client-runtime/state/thread-settled";
1617
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
1718
import type { LayoutChangeEvent, NativeScrollEvent, NativeSyntheticEvent } from "react-native";
1819
import { Platform, Pressable, StyleSheet, TextInput, View } from "react-native";
@@ -420,18 +421,24 @@ function ThreadNavigationSidebarPane(
420421
// (HomeScreen.tsx): flat creation-order card block + settled recency tail.
421422
// PR states stream in per-row. The next partition applies the configured
422423
// merge rule and the always-on close rule.
423-
const [changeRequestStateByKey, setChangeRequestStateByKey] = useState<
424-
ReadonlyMap<string, "open" | "closed" | "merged">
424+
const [changeRequestByKey, setChangeRequestByKey] = useState<
425+
ReadonlyMap<string, ChangeRequestSettleSource>
425426
>(() => new Map());
426427
const handleChangeRequestState = useCallback(
427-
(threadKey: string, state: "open" | "closed" | "merged" | null) => {
428-
setChangeRequestStateByKey((current) => {
429-
if ((current.get(threadKey) ?? null) === state) return current;
428+
(threadKey: string, changeRequest: ChangeRequestSettleSource | null) => {
429+
setChangeRequestByKey((current) => {
430+
const existing = current.get(threadKey) ?? null;
431+
if (
432+
(existing?.state ?? null) === (changeRequest?.state ?? null) &&
433+
(existing?.updatedAt ?? null) === (changeRequest?.updatedAt ?? null)
434+
) {
435+
return current;
436+
}
430437
const next = new Map(current);
431-
if (state === null) {
438+
if (changeRequest === null) {
432439
next.delete(threadKey);
433440
} else {
434-
next.set(threadKey, state);
441+
next.set(threadKey, changeRequest);
435442
}
436443
return next;
437444
});
@@ -552,7 +559,7 @@ function ThreadNavigationSidebarPane(
552559
projectRefs: selectedProjectScope === null ? null : selectedProjectScope.projectRefs,
553560
searchQuery: props.searchQuery,
554561
matchedThreadKeys,
555-
changeRequestStateByKey,
562+
changeRequestByKey,
556563
autoSettleOnMerge,
557564
settlementEnvironmentIds,
558565
snoozeEnvironmentIds,
@@ -564,7 +571,7 @@ function ThreadNavigationSidebarPane(
564571
selectedThreadKey: props.selectedThreadKey ?? null,
565572
});
566573
}, [
567-
changeRequestStateByKey,
574+
changeRequestByKey,
568575
autoSettleOnMerge,
569576
nowMinute,
570577
snoozeWakeTick,

‎apps/mobile/src/features/threads/thread-list-v2-items.tsx‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import type {
33
EnvironmentThreadShell,
44
} from "@t3tools/client-runtime/state/shell";
55
import type { EnvironmentThreadSearchMatch } from "@t3tools/client-runtime/state/thread-search";
6-
import { canSnooze, resolveSnoozePresets } from "@t3tools/client-runtime/state/thread-settled";
6+
import {
7+
canSnooze,
8+
resolveSnoozePresets,
9+
type ChangeRequestSettleSource,
10+
} from "@t3tools/client-runtime/state/thread-settled";
711
import type { MenuAction } from "@react-native-menu/menu";
812
import { memo, useCallback, useEffect, useMemo, useState, type ComponentProps } from "react";
913
import { Alert, Platform, Pressable, useWindowDimensions, View } from "react-native";
@@ -365,11 +369,11 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
365369
readonly canMovePinnedDown?: boolean;
366370
readonly onSwipeableWillOpen: (methods: SwipeableMethods) => void;
367371
readonly onSwipeableClose: (methods: SwipeableMethods) => void;
368-
/** Reports this row's live PR state for the partition's merge and close
369-
rules. Mirrors web's onChangeRequestState. */
372+
/** Reports this row's live PR (state + last activity) for the partition's
373+
merge and close rules. Mirrors web's onChangeRequestState. */
370374
readonly onChangeRequestState?: (
371375
threadKey: string,
372-
state: "open" | "closed" | "merged" | null,
376+
changeRequest: ChangeRequestSettleSource | null,
373377
) => void;
374378
readonly projectCwd?: string | null;
375379
readonly searchMatch?: EnvironmentThreadSearchMatch;
@@ -400,10 +404,14 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
400404

401405
const pr = useThreadPr(thread, props.projectCwd ?? props.project?.workspaceRoot ?? null);
402406
const prState = pr?.state ?? null;
407+
const prUpdatedAt = pr?.updatedAt ?? null;
403408
const threadKey = `${thread.environmentId}:${thread.id}`;
404409
useEffect(() => {
405-
onChangeRequestState?.(threadKey, prState);
406-
}, [onChangeRequestState, prState, threadKey]);
410+
onChangeRequestState?.(
411+
threadKey,
412+
prState === null ? null : { state: prState, updatedAt: prUpdatedAt },
413+
);
414+
}, [onChangeRequestState, prState, prUpdatedAt, threadKey]);
407415

408416
const screenColor = useThemeColor("--color-screen");
409417
const drawerColor = useThemeColor("--color-drawer");

‎apps/mobile/src/features/threads/threadListV2.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ describe("buildThreadListV2Items", () => {
269269
threads: [merged],
270270
environmentId: null,
271271
searchQuery: "",
272-
changeRequestStateByKey: new Map([[`${environmentId}:${merged.id}`, "merged"]]),
272+
changeRequestByKey: new Map([
273+
[`${environmentId}:${merged.id}`, { state: "merged" as const }],
274+
]),
273275
autoSettleOnMerge: false,
274276
now: NOW,
275277
});

‎apps/mobile/src/features/threads/threadListV2.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
resolveSnoozePresets,
77
snoozeWakeLabel,
88
} from "@t3tools/client-runtime/state/thread-settled";
9-
import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled";
9+
import type {
10+
ChangeRequestSettleSource,
11+
SnoozePreset,
12+
} from "@t3tools/client-runtime/state/thread-settled";
1013
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
1114
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
1215
import { sortPinnedThreadsByOrderKey } from "@t3tools/client-runtime/state/thread-sort";
@@ -318,8 +321,8 @@ export function buildThreadListV2Items(input: {
318321
}> | null;
319322
readonly searchQuery: string;
320323
readonly matchedThreadKeys?: ReadonlySet<string>;
321-
/** Per-row PR state reported up by visible rows ("env:threadId" keys). */
322-
readonly changeRequestStateByKey?: ReadonlyMap<string, "open" | "closed" | "merged">;
324+
/** Per-row PR reported up by visible rows ("env:threadId" keys). */
325+
readonly changeRequestByKey?: ReadonlyMap<string, ChangeRequestSettleSource>;
323326
/** Environments whose server supports thread.settle/unsettle. Threads on
324327
other environments never classify as settled — the user could neither
325328
un-settle nor pin them. Absent = no gating (tests). */
@@ -381,8 +384,8 @@ export function buildThreadListV2Items(input: {
381384
}
382385
const supportsSettlement = input.settlementEnvironmentIds?.has(thread.environmentId) ?? true;
383386
const supportsSnooze = input.snoozeEnvironmentIds?.has(thread.environmentId) ?? true;
384-
const changeRequestState =
385-
input.changeRequestStateByKey?.get(`${thread.environmentId}:${thread.id}`) ?? null;
387+
const changeRequest =
388+
input.changeRequestByKey?.get(`${thread.environmentId}:${thread.id}`) ?? null;
386389
// Visibility parity with web: snooze outranks everything, including a
387390
// pin — a snoozed thread leaves the list until it wakes (or raises its
388391
// hand). The pin (and its pinOrderKey) survives underneath, so a woken
@@ -410,7 +413,7 @@ export function buildThreadListV2Items(input: {
410413
now,
411414
autoSettleAfterDays,
412415
autoSettleOnMerge,
413-
changeRequestState,
416+
changeRequest,
414417
})
415418
) {
416419
settled.push(thread);

‎apps/mobile/src/state/thread-pr-presentation.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type ThreadPr = NonNullable<VcsStatusResult["pr"]>;
66
export interface ThreadPrPresentation {
77
readonly number: number;
88
readonly state: ThreadPr["state"];
9+
/** Provider-side last activity, bounding when a terminal state landed. */
10+
readonly updatedAt: string | null;
911
readonly url: string;
1012
/** Compact pull request number label, e.g. "3774". */
1113
readonly label: string;
@@ -28,6 +30,7 @@ export function presentThreadPr(
2830
return {
2931
number: pr.number,
3032
state: pr.state,
33+
updatedAt: pr.updatedAt ?? null,
3134
url: pr.url,
3235
label: String(pr.number),
3336
accessibilityLabel: `#${pr.number} ${presentation.longName} ${pr.state}`,

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
717717
baseRef: "main",
718718
headRef: "feature/status-open-pr",
719719
state: "open",
720+
updatedAt: null,
720721
});
721722
}),
722723
);
@@ -756,6 +757,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
756757
baseRef: "main",
757758
headRef: "feature/status-trimmed-pr",
758759
state: "open",
760+
updatedAt: null,
759761
});
760762
}),
761763
);
@@ -808,6 +810,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
808810
baseRef: "main",
809811
headRef: "feature/status-valid-pr-entry",
810812
state: "open",
813+
updatedAt: null,
811814
});
812815
}),
813816
);
@@ -858,6 +861,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
858861
baseRef: "main",
859862
headRef: "feature/status-lowercase-state",
860863
state: "merged",
864+
updatedAt: "2026-01-02T00:00:00.000Z",
861865
});
862866
}),
863867
);
@@ -1121,6 +1125,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
11211125
baseRef: "main",
11221126
headRef: "statemachine",
11231127
state: "open",
1128+
updatedAt: "2026-03-10T07:00:00.000Z",
11241129
});
11251130
expect(ghCalls).toContain(
11261131
"pr list --head jasonLaster:statemachine --state all --limit 20 --json number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
@@ -1186,6 +1191,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
11861191
baseRef: "main",
11871192
headRef: "main",
11881193
state: "open",
1194+
updatedAt: "2026-03-10T07:00:00.000Z",
11891195
});
11901196
expect(ghCalls).toContain(
11911197
"pr list --head contributor:main --state all --limit 20 --json number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
@@ -1294,6 +1300,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
12941300
baseRef: "main",
12951301
headRef: "effect-atom",
12961302
state: "open",
1303+
updatedAt: "2026-03-01T10:00:00.000Z",
12971304
});
12981305
expect(ghCalls.some((call) => call.includes("pr list --head upstream/effect-atom "))).toBe(
12991306
false,
@@ -1345,6 +1352,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
13451352
baseRef: "main",
13461353
headRef: "feature/status-merged-pr",
13471354
state: "merged",
1355+
updatedAt: "2026-01-30T10:00:00.000Z",
13481356
});
13491357
}),
13501358
);
@@ -1461,6 +1469,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
14611469
baseRef: "main",
14621470
headRef: "feature/status-open-over-merged",
14631471
state: "open",
1472+
updatedAt: "2026-01-30T10:00:00.000Z",
14641473
});
14651474
}),
14661475
);

‎apps/server/src/git/GitManager.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ function toStatusPr(pr: PullRequestInfo): {
538538
baseRef: string;
539539
headRef: string;
540540
state: "open" | "closed" | "merged";
541+
updatedAt: string | null;
541542
} {
542543
return {
543544
number: pr.number,
@@ -546,6 +547,10 @@ function toStatusPr(pr: PullRequestInfo): {
546547
baseRef: pr.baseRefName,
547548
headRef: pr.headRefName,
548549
state: pr.state,
550+
updatedAt: Option.match(pr.updatedAt, {
551+
onNone: () => null,
552+
onSome: (updatedAt) => DateTime.formatIso(updatedAt),
553+
}),
549554
};
550555
}
551556

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4151,6 +4151,18 @@ function ChatViewContent(props: ChatViewProps) {
41514151
}, [activeThreadPr, openThreadPullRequest]);
41524152
const pullRequestSurfaceAvailable =
41534153
supportsPullRequests && activeThreadPr !== null && threadRepository !== null;
4154+
// Primitive slice of the displayed PR for the settle-rule memos below:
4155+
// resolveDisplayedThreadPr returns a fresh object every render, so memoize
4156+
// on the fields the rules read instead of the object identity.
4157+
const activeThreadPrState = activeThreadPr?.state ?? null;
4158+
const activeThreadPrUpdatedAt = activeThreadPr?.updatedAt ?? null;
4159+
const activeThreadChangeRequest = useMemo(
4160+
() =>
4161+
activeThreadPrState === null
4162+
? null
4163+
: { state: activeThreadPrState, updatedAt: activeThreadPrUpdatedAt },
4164+
[activeThreadPrState, activeThreadPrUpdatedAt],
4165+
);
41544166
const supportsSettlement = serverConfig?.environment.capabilities.threadSettlement === true;
41554167
const supportsSnooze = serverConfig?.environment.capabilities.threadSnooze === true;
41564168
const nowMinute = useNowMinute();
@@ -4186,7 +4198,14 @@ function ChatViewContent(props: ChatViewProps) {
41864198
);
41874199
const activeThreadWokeVisible = useMemo(() => {
41884200
if (activeThreadWokeAt === null) return false;
4189-
if (changeRequestAutoSettles(activeThreadPr?.state, autoSettleOnMerge)) return false;
4201+
if (
4202+
changeRequestAutoSettles(activeThreadChangeRequest, {
4203+
autoSettleOnMerge,
4204+
thread: activeThreadShell,
4205+
})
4206+
) {
4207+
return false;
4208+
}
41904209
const wokeAtMs = Date.parse(activeThreadWokeAt);
41914210
if (Number.isNaN(wokeAtMs)) return false;
41924211
// Having the thread open counts as a visit at completedAt (the effect
@@ -4206,7 +4225,8 @@ function ChatViewContent(props: ChatViewProps) {
42064225
}, [
42074226
activeLatestTurn?.completedAt,
42084227
activeThreadLastVisitedAt,
4209-
activeThreadPr?.state,
4228+
activeThreadChangeRequest,
4229+
activeThreadShell,
42104230
activeThreadWokeAt,
42114231
autoSettleOnMerge,
42124232
]);
@@ -4216,10 +4236,10 @@ function ChatViewContent(props: ChatViewProps) {
42164236
now: `${nowMinute}:00.000Z`,
42174237
autoSettleAfterDays,
42184238
autoSettleOnMerge,
4219-
changeRequestState: activeThreadPr?.state ?? null,
4239+
changeRequest: activeThreadChangeRequest,
42204240
});
42214241
}, [
4222-
activeThreadPr?.state,
4242+
activeThreadChangeRequest,
42234243
activeThreadShell,
42244244
autoSettleAfterDays,
42254245
autoSettleOnMerge,
@@ -6245,7 +6265,7 @@ function ChatViewContent(props: ChatViewProps) {
62456265
{...(routeKind === "draft" && draftId ? { draftId } : {})}
62466266
activeThreadTitle={activeThread.title}
62476267
isServerThread={isServerThread}
6248-
changeRequestState={activeThreadPr?.state ?? null}
6268+
changeRequest={activeThreadChangeRequest}
62496269
activeProjectName={activeProject?.title}
62506270
activeProjectCwd={activeProject?.workspaceRoot ?? null}
62516271
activeProjectFaviconPath={activeProject?.faviconPath ?? null}

0 commit comments

Comments
 (0)