@@ -100,6 +100,33 @@ function shell(overrides: Partial<SettlementShell> = {}): SettlementShell {
100100 } ;
101101}
102102
103+ function persistedLink (
104+ state : "open" | null ,
105+ linkedAt : DateTime . Utc ,
106+ ) : NonNullable < SettlementShell [ "pullRequests" ] > [ number ] {
107+ return {
108+ host : "example.test" ,
109+ repository : "owner/repository" ,
110+ number : 42 ,
111+ url : "https://example.test/owner/repository/pull/42" ,
112+ source : "manual" ,
113+ linkedAt : DateTime . formatIso ( linkedAt ) ,
114+ snapshot :
115+ state === null
116+ ? null
117+ : {
118+ state,
119+ title : "Pull request" ,
120+ headBranch : "feature" ,
121+ baseBranch : "main" ,
122+ isDraft : false ,
123+ updatedAt : DateTime . formatIso ( linkedAt ) ,
124+ syncedAt : DateTime . formatIso ( linkedAt ) ,
125+ } ,
126+ stack : null ,
127+ } ;
128+ }
129+
103130describe ( "isAutoSettlementCandidate" , ( ) => {
104131 it ( "settles a thread whose only background work is a command left running" , ( ) => {
105132 expect (
@@ -186,6 +213,51 @@ describe("resolveAutoSettlementAt", () => {
186213 ) . toEqual ( at ( - 4 * DAY_MS ) ) ;
187214 } ) ;
188215
216+ it ( "settles an idle thread with an open persisted link only while retention is off" , ( ) => {
217+ const input = {
218+ thread : shell ( {
219+ latestUserMessageAt : at ( - 4 * DAY_MS ) ,
220+ pullRequests : [ persistedLink ( "open" , at ( - 5 * DAY_MS ) ) ] ,
221+ } ) ,
222+ pullRequest : null ,
223+ nowMs : NOW_MS ,
224+ autoSettleAfterDays : 3 ,
225+ autoSettleOnMerge : true ,
226+ autoSettleKeepPullRequests : false ,
227+ } ;
228+ expect ( ThreadSettlementService . resolveAutoSettlementAt ( input ) ) . toEqual ( at ( - 4 * DAY_MS ) ) ;
229+ expect (
230+ ThreadSettlementService . resolveAutoSettlementAt ( {
231+ ...input ,
232+ autoSettleKeepPullRequests : true ,
233+ } ) ,
234+ ) . toBeNull ( ) ;
235+ // The open link outranks a merged lookup, so only inactivity can settle it.
236+ expect (
237+ ThreadSettlementService . resolveAutoSettlementAt ( {
238+ ...input ,
239+ pullRequest : { state : "merged" , mergedAt : DateTime . formatIso ( at ( - 1 ) ) } ,
240+ autoSettleAfterDays : null ,
241+ } ) ,
242+ ) . toBeNull ( ) ;
243+ } ) ;
244+
245+ it ( "keeps an idle thread whose persisted link was never synced" , ( ) => {
246+ expect (
247+ ThreadSettlementService . resolveAutoSettlementAt ( {
248+ thread : shell ( {
249+ latestUserMessageAt : at ( - 4 * DAY_MS ) ,
250+ pullRequests : [ persistedLink ( null , at ( - 5 * DAY_MS ) ) ] ,
251+ } ) ,
252+ pullRequest : null ,
253+ nowMs : NOW_MS ,
254+ autoSettleAfterDays : 3 ,
255+ autoSettleOnMerge : true ,
256+ autoSettleKeepPullRequests : false ,
257+ } ) ,
258+ ) . toBeNull ( ) ;
259+ } ) ;
260+
189261 it ( "uses the latest activity time when the inactivity window elapses" , ( ) => {
190262 const idle = shell ( {
191263 latestUserMessageAt : at ( - 4 * DAY_MS ) ,
@@ -697,6 +769,32 @@ describe("ThreadSettlementServiceV2 worker", () => {
697769 ) ,
698770 ) ;
699771
772+ it . effect ( "settles an idle thread with an open persisted link on the inactivity sweep" , ( ) =>
773+ Effect . scoped (
774+ Effect . gen ( function * ( ) {
775+ yield * TestClock . setTime ( Date . parse ( NOW ) ) ;
776+ const thread = makeThread ( "open-link" , {
777+ pullRequests : [ persistedLink ( "open" , DateTime . makeUnsafe ( "2026-08-20T00:00:00.000Z" ) ) ] ,
778+ } ) ;
779+ const fixture = yield * makeHarness ( {
780+ snapshot : makeSnapshot ( [ thread ] ) ,
781+ settings : {
782+ ...DEFAULT_SERVER_SETTINGS ,
783+ sidebarAutoSettleAfterDays : 3 ,
784+ sidebarAutoSettleKeepPullRequests : false ,
785+ } ,
786+ } ) ;
787+ yield * Effect . gen ( function * ( ) {
788+ const service = yield * ThreadSettlementService . ThreadSettlementServiceV2 ;
789+ yield * startHarness ( service , fixture . activation , fixture . snapshotReads ) ;
790+ expect ( ( yield * Ref . get ( fixture . commands ) ) . map ( ( command ) => command . threadId ) ) . toEqual ( [
791+ thread . id ,
792+ ] ) ;
793+ } ) . pipe ( Effect . provide ( fixture . layer ) ) ;
794+ } ) ,
795+ ) ,
796+ ) ;
797+
700798 it . effect ( "settles only the project opted in while environment settlement is disabled" , ( ) =>
701799 Effect . scoped (
702800 Effect . gen ( function * ( ) {
0 commit comments