@@ -46,7 +46,12 @@ function at(offsetMs: number): DateTime.Utc {
4646 return DateTime . makeUnsafe ( NOW_MS + offsetMs ) ;
4747}
4848
49- function shell ( overrides : Partial < OrchestrationV2ThreadShell > = { } ) : OrchestrationV2ThreadShell {
49+ type SettlementShell = OrchestrationV2ThreadShell &
50+ Pick < ProjectionStore . ProjectionSettlementCandidate , "latestUserAuthoredMessageAt" > ;
51+
52+ // A fixture's user message is one the user wrote unless the test sets
53+ // latestUserAuthoredMessageAt on its own.
54+ function shell ( overrides : Partial < SettlementShell > = { } ) : SettlementShell {
5055 return {
5156 id : ThreadId . make ( "thread-1" ) ,
5257 projectId : ProjectId . make ( "project-1" ) ,
@@ -83,6 +88,7 @@ function shell(overrides: Partial<OrchestrationV2ThreadShell> = {}): Orchestrati
8388 latestRunStartedAt : null ,
8489 latestRunCompletedAt : null ,
8590 latestUserMessageAt : null ,
91+ latestUserAuthoredMessageAt : overrides . latestUserMessageAt ?? null ,
8692 createdAt : at ( - 30 * DAY_MS ) ,
8793 updatedAt : at ( - 10 * DAY_MS ) ,
8894 archivedAt : null ,
@@ -292,6 +298,31 @@ describe("resolveAutoSettlementAt", () => {
292298 ) . toEqual ( shell ( ) . createdAt ) ;
293299 } ) ;
294300
301+ it ( "settles on merge after agent-started runs, but not after the user writes again" , ( ) => {
302+ // A background command stopped after the merge and its notification
303+ // started a run. Only the user's own messages hold a merged thread open.
304+ const woken = shell ( {
305+ latestUserMessageAt : at ( - 30 * 60 * 1_000 ) ,
306+ latestUserAuthoredMessageAt : at ( - 2 * 60 * 60 * 1_000 ) ,
307+ latestRunRequestedAt : at ( - 30 * 60 * 1_000 ) ,
308+ latestRunCompletedAt : at ( - 29 * 60 * 1_000 ) ,
309+ } ) ;
310+ const input = {
311+ thread : woken ,
312+ pullRequest : { state : "merged" as const , mergedAt : DateTime . formatIso ( at ( - 60 * 60 * 1_000 ) ) } ,
313+ nowMs : NOW_MS ,
314+ autoSettleAfterDays : null ,
315+ autoSettleOnMerge : true ,
316+ } ;
317+ expect ( ThreadSettlementService . resolveAutoSettlementAt ( input ) ) . toEqual ( at ( - 29 * 60 * 1_000 ) ) ;
318+ expect (
319+ ThreadSettlementService . resolveAutoSettlementAt ( {
320+ ...input ,
321+ thread : { ...woken , latestUserAuthoredMessageAt : at ( - 30 * 60 * 1_000 ) } ,
322+ } ) ,
323+ ) . toBeNull ( ) ;
324+ } ) ;
325+
295326 it ( "settles inactive threads even when their pull request remains open" , ( ) => {
296327 const input = {
297328 thread : shell ( { latestUserMessageAt : at ( - 30 * DAY_MS ) } ) ,
@@ -370,10 +401,7 @@ function makeProject(
370401 } ;
371402}
372403
373- function makeThread (
374- id : string ,
375- overrides : Partial < OrchestrationV2ThreadShell > = { } ,
376- ) : OrchestrationV2ThreadShell {
404+ function makeThread ( id : string , overrides : Partial < SettlementShell > = { } ) : SettlementShell {
377405 return shell ( {
378406 id : ThreadId . make ( id ) ,
379407 projectId : PROJECT_ID ,
@@ -385,10 +413,14 @@ function makeThread(
385413 } ) ;
386414}
387415
416+ type SettlementSnapshot = Omit < OrchestrationV2ShellSnapshot , "threads" > & {
417+ readonly threads : ReadonlyArray < SettlementShell > ;
418+ } ;
419+
388420function makeSnapshot (
389- threads : ReadonlyArray < OrchestrationV2ThreadShell > ,
421+ threads : ReadonlyArray < SettlementShell > ,
390422 projects : ReadonlyArray < OrchestrationProjectShell > = [ makeProject ( ) ] ,
391- ) : OrchestrationV2ShellSnapshot {
423+ ) : SettlementSnapshot {
392424 return {
393425 schemaVersion : 1 ,
394426 snapshotSequence : 1 ,
@@ -435,7 +467,7 @@ function makeBranchPullRequest(state: "open" | "closed" | "merged") {
435467}
436468
437469interface HarnessOptions {
438- readonly snapshot : OrchestrationV2ShellSnapshot ;
470+ readonly snapshot : SettlementSnapshot ;
439471 readonly settings ?: ContractServerSettings ;
440472 readonly branchPullRequest ?: GitManager . GitManager [ "Service" ] [ "branchPullRequest" ] ;
441473 readonly pullRequestSummary ?: PullRequestService . PullRequestService [ "Service" ] [ "summary" ] ;
0 commit comments