Skip to content

Commit 187e7b0

Browse files
committed
fix(threads): settle on a terminal PR only while it is the thread's latest event
1 parent 2de10f5 commit 187e7b0

4 files changed

Lines changed: 66 additions & 33 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,7 +4201,7 @@ function ChatViewContent(props: ChatViewProps) {
42014201
if (
42024202
changeRequestAutoSettles(activeThreadChangeRequest, {
42034203
autoSettleOnMerge,
4204-
threadCreatedAt: activeThreadShell?.createdAt ?? null,
4204+
thread: activeThreadShell,
42054205
})
42064206
) {
42074207
return false;
@@ -4226,7 +4226,7 @@ function ChatViewContent(props: ChatViewProps) {
42264226
activeLatestTurn?.completedAt,
42274227
activeThreadLastVisitedAt,
42284228
activeThreadChangeRequest,
4229-
activeThreadShell?.createdAt,
4229+
activeThreadShell,
42304230
activeThreadWokeAt,
42314231
autoSettleOnMerge,
42324232
]);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
820820
(lastVisitedDate === null || lastVisitedDate < wokeAtDate) &&
821821
!changeRequestAutoSettles(pr, {
822822
autoSettleOnMerge: props.autoSettleOnMerge,
823-
threadCreatedAt: thread.createdAt,
823+
thread,
824824
});
825825
// In-flight rows (working, or waiting on approval/input) fade as a whole:
826826
// there is nothing for the user to do yet, so prominence is reserved for

‎packages/client-runtime/src/state/threadSettled.test.ts‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,66 @@ describe("changeRequestAutoSettles", () => {
3434
});
3535

3636
const THREAD_CREATED_AT = "2026-04-01T00:00:00.000Z";
37+
const idleThread = {
38+
createdAt: THREAD_CREATED_AT,
39+
latestUserMessageAt: null,
40+
latestTurn: null,
41+
};
3742

3843
it("ignores a terminal change request last touched before the thread existed", () => {
3944
for (const state of ["merged", "closed"] as const) {
4045
expect(
4146
changeRequestAutoSettles(
4247
{ state, updatedAt: "2026-03-31T23:59:59.999Z" },
43-
{ threadCreatedAt: THREAD_CREATED_AT },
48+
{ thread: idleThread },
4449
),
4550
).toBe(false);
4651
}
4752
});
4853

49-
it("settles on a terminal change request touched at or after thread creation", () => {
54+
it("settles on a terminal change request touched at or after the thread's latest event", () => {
5055
for (const updatedAt of [THREAD_CREATED_AT, "2026-04-02T00:00:00.000Z"]) {
51-
expect(
52-
changeRequestAutoSettles(
53-
{ state: "merged", updatedAt },
54-
{ threadCreatedAt: THREAD_CREATED_AT },
55-
),
56-
).toBe(true);
56+
expect(changeRequestAutoSettles({ state: "merged", updatedAt }, { thread: idleThread })).toBe(
57+
true,
58+
);
5759
}
5860
});
5961

60-
it("falls back to settling when either timestamp is missing or malformed", () => {
62+
it("never re-settles a thread revived after the merge", () => {
63+
// Settling on a merge happens once: a user message newer than the PR's
64+
// last activity means the conversation outlived the PR.
65+
const revived = {
66+
createdAt: THREAD_CREATED_AT,
67+
latestUserMessageAt: "2026-04-05T00:00:00.000Z",
68+
latestTurn: null,
69+
};
6170
expect(
62-
changeRequestAutoSettles({ state: "merged" }, { threadCreatedAt: THREAD_CREATED_AT }),
63-
).toBe(true);
71+
changeRequestAutoSettles(
72+
{ state: "merged", updatedAt: "2026-04-03T00:00:00.000Z" },
73+
{ thread: revived },
74+
),
75+
).toBe(false);
76+
// A merge landing after the revival still settles.
6477
expect(
6578
changeRequestAutoSettles(
66-
{ state: "merged", updatedAt: null },
67-
{ threadCreatedAt: THREAD_CREATED_AT },
79+
{ state: "merged", updatedAt: "2026-04-06T00:00:00.000Z" },
80+
{ thread: revived },
6881
),
6982
).toBe(true);
83+
});
84+
85+
it("falls back to settling when either timestamp is missing or malformed", () => {
86+
expect(changeRequestAutoSettles({ state: "merged" }, { thread: idleThread })).toBe(true);
87+
expect(
88+
changeRequestAutoSettles({ state: "merged", updatedAt: null }, { thread: idleThread }),
89+
).toBe(true);
7090
expect(
7191
changeRequestAutoSettles({ state: "merged", updatedAt: "2026-03-01T00:00:00.000Z" }, {}),
7292
).toBe(true);
7393
expect(
7494
changeRequestAutoSettles(
7595
{ state: "merged", updatedAt: "not-a-date" },
76-
{ threadCreatedAt: THREAD_CREATED_AT },
96+
{ thread: idleThread },
7797
),
7898
).toBe(true);
7999
});
@@ -238,9 +258,10 @@ describe("effectiveSettled", () => {
238258
}
239259
});
240260

241-
it("ignores a change request that merged before the thread was created", () => {
261+
it("ignores a change request that merged before the thread's latest event", () => {
242262
// A new thread started at a worktree root inherits the branch's old
243-
// merged PR; that history must not settle the fresh conversation.
263+
// merged PR, and a revived thread outlives its merge; neither settles
264+
// the live conversation.
244265
const fresh = makeShell({ activityAt: FRESH });
245266
for (const state of ["merged", "closed"] as const) {
246267
expect(

‎packages/client-runtime/src/state/threadSettled.ts‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,49 @@ export interface ChangeRequestSettleSource {
1313
readonly updatedAt?: string | null | undefined;
1414
}
1515

16+
/** What the settle rules need to know about the thread's own timeline. */
17+
export type ThreadActivitySource = Pick<
18+
OrchestrationThreadShell,
19+
"createdAt" | "latestUserMessageAt" | "latestTurn"
20+
>;
21+
1622
/**
1723
* Returns whether the change request settles the thread immediately. A
18-
* terminal request last touched BEFORE the thread was created is history the
19-
* thread merely inherited from its branch (a new thread started at a
20-
* worktree root whose PR already merged), not this thread's outcome — it
21-
* never settles. Unknown timestamps keep the old always-settle behavior.
24+
* terminal request settles the thread only while it is the thread's latest
25+
* event: settling on a merge happens ONCE. A request last touched before the
26+
* thread was created is inherited branch history (a new thread started at a
27+
* worktree root whose PR already merged), and one older than the thread's
28+
* latest activity was already adjudicated — re-engaging a thread whose PR
29+
* merged is the user saying the conversation outlived the PR. Unknown
30+
* timestamps keep the old always-settle behavior.
2231
*/
2332
export function changeRequestAutoSettles(
2433
changeRequest: ChangeRequestSettleSource | null | undefined,
2534
options: {
2635
readonly autoSettleOnMerge?: boolean | undefined;
27-
readonly threadCreatedAt?: string | null | undefined;
36+
readonly thread?: ThreadActivitySource | null | undefined;
2837
} = {},
2938
): boolean {
3039
if (changeRequest == null) return false;
3140
const terminal =
3241
changeRequest.state === "closed" ||
3342
(changeRequest.state === "merged" && options.autoSettleOnMerge !== false);
3443
if (!terminal) return false;
35-
if (changeRequest.updatedAt == null || options.threadCreatedAt == null) return true;
44+
if (changeRequest.updatedAt == null || options.thread == null) return true;
3645
const updatedAtMs = Date.parse(changeRequest.updatedAt);
37-
const createdAtMs = Date.parse(options.threadCreatedAt);
46+
const anchorAt = threadLastActivityAt(options.thread) ?? options.thread.createdAt;
47+
const anchorAtMs = Date.parse(anchorAt);
3848
// Malformed timestamps fall back to settling, matching servers that never
3949
// report updatedAt.
40-
if (Number.isNaN(updatedAtMs) || Number.isNaN(createdAtMs)) return true;
41-
return updatedAtMs >= createdAtMs;
50+
if (Number.isNaN(updatedAtMs) || Number.isNaN(anchorAtMs)) return true;
51+
return updatedAtMs >= anchorAtMs;
4252
}
4353

4454
const DAY_MS = 24 * 60 * 60 * 1_000;
4555

46-
export function threadLastActivityAt(shell: OrchestrationThreadShell): string | null {
56+
export function threadLastActivityAt(
57+
shell: Pick<OrchestrationThreadShell, "latestUserMessageAt" | "latestTurn">,
58+
): string | null {
4759
const candidates = [
4860
shell.latestUserMessageAt,
4961
shell.latestTurn?.requestedAt,
@@ -260,9 +272,9 @@ export function threadWokeAt(
260272
* override. Past the blockers, the explicit user override (thread.settle /
261273
* thread.unsettle commands, projected into settledOverride + settledAt)
262274
* wins in both directions; without one, a thread can auto-settle on a
263-
* merged PR or always on a closed PR (both only when the terminal state is
264-
* not older than the thread itself, see changeRequestAutoSettles), or
265-
* settles on inactivity past the window.
275+
* merged PR or always on a closed PR (both only while the terminal state is
276+
* the thread's latest event, see changeRequestAutoSettles), or settles on
277+
* inactivity past the window.
266278
* An open PR blocks the inactivity path entirely. The server
267279
* un-settles on real activity (user message, session start, approval/
268280
* user-input request), so an override never goes stale silently.
@@ -302,7 +314,7 @@ export function effectiveSettled(
302314
if (
303315
changeRequestAutoSettles(options.changeRequest, {
304316
autoSettleOnMerge: options.autoSettleOnMerge,
305-
threadCreatedAt: shell.createdAt,
317+
thread: shell,
306318
})
307319
) {
308320
return true;

0 commit comments

Comments
 (0)