@@ -602,6 +602,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
602602 const settingsReadCount = yield * Ref . make ( 0 ) ;
603603 const settingsChanges = yield * PubSub . unbounded < ContractServerSettings > ( ) ;
604604 const mergedPullRequests = yield * PubSub . unbounded < PullRequestService . PullRequestMergeEvent > ( ) ;
605+ const pullRequestRefreshes = yield * PubSub . unbounded < number > ( ) ;
605606 const commands = yield * Ref . make < ReadonlyArray < AutoSettleCommand > > ( [ ] ) ;
606607 const branchCalls = yield * Ref . make <
607608 ReadonlyArray < { readonly cwd : string ; readonly branch : string } >
@@ -737,6 +738,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
737738 subscribeMerges : PubSub . subscribe ( mergedPullRequests ) . pipe (
738739 Effect . map ( ( subscription ) => Stream . fromSubscription ( subscription ) ) ,
739740 ) ,
741+ subscribeRefreshes : Stream . fromPubSub ( pullRequestRefreshes ) ,
740742 } ) ,
741743 Layer . succeed ( ServerSettings . ServerSettingsService , serverSettings ) ,
742744 Layer . succeed ( ServerActivation . ServerActivation , Deferred . await ( activation ) ) ,
@@ -775,6 +777,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
775777 observedEvents,
776778 publishEvent : ( event : OrchestrationV2DomainEvent ) => PubSub . publish ( domainEvents , event ) ,
777779 updateSettings,
780+ publishRefresh : PubSub . publish ( pullRequestRefreshes , 1 ) ,
778781 publishMerge : PubSub . publish ( mergedPullRequests , {
779782 projectId : PROJECT_ID ,
780783 repository : "owner/repository" ,
@@ -1623,20 +1626,40 @@ describe("ThreadSettlementServiceV2 worker", () => {
16231626 } ) ,
16241627 ) ;
16251628
1626- it . effect ( "settles an unlinked branch thread its agent merged from a shell, past the cache" , ( ) =>
1629+ it . effect . each ( [
1630+ { kind : "an unlinked" , linked : false } ,
1631+ { kind : "a quiet linked" , linked : true } ,
1632+ ] ) ( "settles $kind branch thread its agent merged from a shell, past the cache" , ( { linked } ) =>
16271633 Effect . gen ( function * ( ) {
16281634 yield * TestClock . setTime ( Date . parse ( NOW ) ) ;
1629- const settled = yield * Deferred . make < void > ( ) ;
16301635 const thread = makeThread ( "shell-merged" , {
16311636 branch : "saved-feature" ,
16321637 latestUserMessageAt : DateTime . makeUnsafe ( "2026-08-27T00:00:00.000Z" ) ,
1638+ ...( linked
1639+ ? {
1640+ branchPullRequest : {
1641+ projectId : PROJECT_ID ,
1642+ repository : "owner/repository" ,
1643+ number : 42 ,
1644+ url : "https://example.test/owner/repository/pull/42" ,
1645+ } ,
1646+ }
1647+ : { } ) ,
16331648 } ) ;
16341649 const fixture = yield * makeHarness ( {
16351650 snapshot : makeSnapshot ( [ thread ] ) ,
16361651 // The host already merged it; only a fresh read sees that past the cached open lookup.
16371652 branchPullRequest : ( _input , lookupOptions ) =>
16381653 Effect . succeed ( makeBranchPullRequest ( lookupOptions ?. refresh ? "merged" : "open" ) ) ,
1639- onDispatch : ( ) => Deferred . succeed ( settled , undefined ) ,
1654+ pullRequestSummary : ( input , readOptions ) =>
1655+ Effect . succeed ( {
1656+ ...makePullRequestSummary ( {
1657+ ...input ,
1658+ state : readOptions ?. bypassCache ? "merged" : "open" ,
1659+ updatedAt : DateTime . formatIso ( DateTime . makeUnsafe ( Date . parse ( NOW ) - 2 * DAY_MS ) ) ,
1660+ } ) ,
1661+ ...( readOptions ?. bypassCache ? { mergedAt : NOW } : { } ) ,
1662+ } ) ,
16401663 } ) ;
16411664 const at = DateTime . makeUnsafe ( NOW ) ;
16421665 const runId = RunId . make ( "run:shell-merged" ) ;
@@ -1681,7 +1704,8 @@ describe("ThreadSettlementServiceV2 worker", () => {
16811704 input : "gh pr merge 42 --squash" ,
16821705 } ,
16831706 } ) ;
1684- yield * Deferred . await ( settled ) ;
1707+ yield * Queue . take ( fixture . snapshotReads ) ;
1708+ yield * reactor . drain ;
16851709 assert . deepStrictEqual (
16861710 ( yield * Ref . get ( fixture . commands ) ) . map ( ( command ) => command . threadId ) ,
16871711 [ thread . id ] ,
@@ -1728,6 +1752,122 @@ describe("ThreadSettlementServiceV2 worker", () => {
17281752 } ) ,
17291753 ) ;
17301754
1755+ it . effect ( "reads a quiet open branch pull request every 5 minutes" , ( ) =>
1756+ Effect . gen ( function * ( ) {
1757+ yield * TestClock . setTime ( Date . parse ( NOW ) ) ;
1758+ const fixture = yield * makeHarness ( {
1759+ snapshot : makeSnapshot ( [
1760+ makeThread ( "active-branch" , {
1761+ branch : "feature" ,
1762+ latestUserMessageAt : DateTime . makeUnsafe ( "2026-08-27T00:00:00.000Z" ) ,
1763+ branchPullRequest : {
1764+ projectId : PROJECT_ID ,
1765+ repository : "owner/repository" ,
1766+ number : 42 ,
1767+ url : "https://example.test/owner/repository/pull/42" ,
1768+ } ,
1769+ } ) ,
1770+ ] ) ,
1771+ pullRequestSummary : ( input ) =>
1772+ Effect . succeed (
1773+ makePullRequestSummary ( {
1774+ ...input ,
1775+ state : "open" ,
1776+ updatedAt : DateTime . formatIso ( DateTime . makeUnsafe ( Date . parse ( NOW ) - 2 * DAY_MS ) ) ,
1777+ } ) ,
1778+ ) ,
1779+ } ) ;
1780+
1781+ yield * Effect . gen ( function * ( ) {
1782+ const reactor = yield * ThreadSettlementService . ThreadSettlementServiceV2 ;
1783+ yield * startHarness ( reactor , fixture . activation , fixture . snapshotReads ) ;
1784+ assert . strictEqual ( ( yield * Ref . get ( fixture . summaryCalls ) ) . length , 1 ) ;
1785+
1786+ for ( let minute = 0 ; minute < 4 ; minute ++ ) {
1787+ yield * TestClock . adjust ( "1 minute" ) ;
1788+ yield * Queue . take ( fixture . snapshotReads ) ;
1789+ yield * reactor . drain ;
1790+ }
1791+ assert . strictEqual ( ( yield * Ref . get ( fixture . summaryCalls ) ) . length , 1 ) ;
1792+
1793+ yield * TestClock . adjust ( "1 minute" ) ;
1794+ yield * Queue . take ( fixture . snapshotReads ) ;
1795+ yield * reactor . drain ;
1796+ assert . strictEqual ( ( yield * Ref . get ( fixture . summaryCalls ) ) . length , 2 ) ;
1797+
1798+ // An app merge the host completes later sends only a refresh; reads stay on the minute
1799+ // cadence while the pull request still reads open.
1800+ yield * fixture . publishRefresh ;
1801+ for ( const expected of [ 3 , 4 ] ) {
1802+ yield * TestClock . adjust ( "1 minute" ) ;
1803+ yield * Queue . take ( fixture . snapshotReads ) ;
1804+ yield * reactor . drain ;
1805+ assert . strictEqual ( ( yield * Ref . get ( fixture . summaryCalls ) ) . length , expected ) ;
1806+ }
1807+ } ) . pipe ( Effect . provide ( fixture . layer ) ) ;
1808+ } ) ,
1809+ ) ;
1810+
1811+ it . effect . each ( [
1812+ { when : "after" , mergeDuringRead : false } ,
1813+ { when : "during" , mergeDuringRead : true } ,
1814+ ] ) (
1815+ "a merge $when a quiet pull request's read, while its thread is not a candidate, rereads it next sweep" ,
1816+ ( { mergeDuringRead } ) =>
1817+ Effect . gen ( function * ( ) {
1818+ yield * TestClock . setTime ( Date . parse ( NOW ) ) ;
1819+ const thread = makeThread ( "busy-branch" , {
1820+ branch : "feature" ,
1821+ latestUserMessageAt : DateTime . makeUnsafe ( "2026-08-27T00:00:00.000Z" ) ,
1822+ branchPullRequest : {
1823+ projectId : PROJECT_ID ,
1824+ repository : "owner/repository" ,
1825+ number : 42 ,
1826+ url : "https://example.test/owner/repository/pull/42" ,
1827+ } ,
1828+ } ) ;
1829+ const readStarted = yield * Deferred . make < void > ( ) ;
1830+ const releaseRead = yield * Deferred . make < void > ( ) ;
1831+ if ( ! mergeDuringRead ) yield * Deferred . succeed ( releaseRead , undefined ) ;
1832+ const fixture = yield * makeHarness ( {
1833+ snapshot : makeSnapshot ( [ thread ] ) ,
1834+ pullRequestSummary : ( input ) =>
1835+ Deferred . succeed ( readStarted , undefined ) . pipe (
1836+ Effect . andThen ( Deferred . await ( releaseRead ) ) ,
1837+ Effect . as (
1838+ makePullRequestSummary ( {
1839+ ...input ,
1840+ state : "open" ,
1841+ updatedAt : DateTime . formatIso ( DateTime . makeUnsafe ( Date . parse ( NOW ) - 2 * DAY_MS ) ) ,
1842+ } ) ,
1843+ ) ,
1844+ ) ,
1845+ } ) ;
1846+
1847+ yield * Effect . gen ( function * ( ) {
1848+ const reactor = yield * ThreadSettlementService . ThreadSettlementServiceV2 ;
1849+ yield * reactor . start ( ) ;
1850+ yield * Deferred . succeed ( fixture . activation , undefined ) ;
1851+ yield * Queue . take ( fixture . snapshotReads ) ;
1852+ yield * Deferred . await ( readStarted ) ;
1853+ if ( ! mergeDuringRead ) yield * reactor . drain ;
1854+ assert . strictEqual ( ( yield * Ref . get ( fixture . summaryCalls ) ) . length , 1 ) ;
1855+
1856+ yield * Ref . set ( fixture . snapshots , makeSnapshot ( [ ] ) ) ;
1857+ yield * fixture . publishMerge ;
1858+ yield * Queue . take ( fixture . snapshotReads ) ;
1859+ yield * Deferred . succeed ( releaseRead , undefined ) ;
1860+ yield * reactor . drain ;
1861+
1862+ yield * Ref . set ( fixture . snapshots , makeSnapshot ( [ thread ] ) ) ;
1863+ yield * TestClock . adjust ( "1 minute" ) ;
1864+ yield * Queue . take ( fixture . snapshotReads ) ;
1865+ yield * reactor . drain ;
1866+ assert . strictEqual ( ( yield * Ref . get ( fixture . summaryCalls ) ) . length , 2 ) ;
1867+ } ) . pipe ( Effect . provide ( fixture . layer ) ) ;
1868+ } ) ,
1869+ ) ;
1870+
17311871 it . effect ( "rereads a branch without a pull request less often the longer its thread idles" , ( ) =>
17321872 Effect . gen ( function * ( ) {
17331873 yield * TestClock . setTime ( Date . parse ( NOW ) ) ;
0 commit comments