@@ -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 */
2332export 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
4454const 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