@@ -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 */
3251export 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