Skip to content

Commit 26af072

Browse files
committed
fix(threads): anchor merge settling on user activity, not turn lifecycle stamps
1 parent 187e7b0 commit 26af072

2 files changed

Lines changed: 50 additions & 9 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@ describe("changeRequestAutoSettles", () => {
8282
).toBe(true);
8383
});
8484

85+
it("still settles when the merge lands during an in-flight turn", () => {
86+
// Anchor is user-initiated activity only: the agent finishing a turn
87+
// after the merge must not block the settle the merge earned.
88+
const midTurnMerge = {
89+
createdAt: THREAD_CREATED_AT,
90+
latestUserMessageAt: "2026-04-02T00:00:00.000Z",
91+
latestTurn: {
92+
turnId: TurnId.make("turn-mid"),
93+
state: "completed" as const,
94+
requestedAt: "2026-04-02T00:00:00.000Z",
95+
startedAt: "2026-04-02T00:00:05.000Z",
96+
completedAt: "2026-04-02T00:20:00.000Z",
97+
assistantMessageId: null,
98+
},
99+
};
100+
expect(
101+
changeRequestAutoSettles(
102+
{ state: "merged", updatedAt: "2026-04-02T00:10:00.000Z" },
103+
{ thread: midTurnMerge },
104+
),
105+
).toBe(true);
106+
});
107+
85108
it("falls back to settling when either timestamp is missing or malformed", () => {
86109
expect(changeRequestAutoSettles({ state: "merged" }, { thread: idleThread })).toBe(true);
87110
expect(

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,34 @@ export type ThreadActivitySource = Pick<
1919
"createdAt" | "latestUserMessageAt" | "latestTurn"
2020
>;
2121

22+
/**
23+
* Latest USER-initiated activity: messages and the turn requests they start,
24+
* deliberately not the agent-side started/completed stamps. The settle-on-
25+
* merge anchor uses this so a merge landing mid-turn still settles the
26+
* thread when that turn finishes, while a user re-engaging after the merge
27+
* blocks it for good. Falls back to creation time for untouched threads.
28+
*/
29+
function threadUserActivityAnchorAt(thread: ThreadActivitySource): string {
30+
const messageAt = thread.latestUserMessageAt;
31+
const requestedAt = thread.latestTurn?.requestedAt;
32+
let anchor = thread.createdAt;
33+
for (const candidate of [messageAt, requestedAt]) {
34+
if (candidate != null && Date.parse(candidate) > Date.parse(anchor)) {
35+
anchor = candidate;
36+
}
37+
}
38+
return anchor;
39+
}
40+
2241
/**
2342
* Returns whether the change request settles the thread immediately. A
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.
43+
* terminal request settles the thread only while it postdates every user-
44+
* initiated event in it: settling on a merge happens ONCE. A request last
45+
* touched before the thread was created is inherited branch history (a new
46+
* thread started at a worktree root whose PR already merged), and one older
47+
* than the user's latest engagement was already adjudicated — re-engaging a
48+
* thread whose PR merged is the user saying the conversation outlived the
49+
* PR. Unknown timestamps keep the old always-settle behavior.
3150
*/
3251
export function changeRequestAutoSettles(
3352
changeRequest: ChangeRequestSettleSource | null | undefined,
@@ -43,8 +62,7 @@ export function changeRequestAutoSettles(
4362
if (!terminal) return false;
4463
if (changeRequest.updatedAt == null || options.thread == null) return true;
4564
const updatedAtMs = Date.parse(changeRequest.updatedAt);
46-
const anchorAt = threadLastActivityAt(options.thread) ?? options.thread.createdAt;
47-
const anchorAtMs = Date.parse(anchorAt);
65+
const anchorAtMs = Date.parse(threadUserActivityAnchorAt(options.thread));
4866
// Malformed timestamps fall back to settling, matching servers that never
4967
// report updatedAt.
5068
if (Number.isNaN(updatedAtMs) || Number.isNaN(anchorAtMs)) return true;

0 commit comments

Comments
 (0)