TypeScript
fix(server): settle inactive threads with persisted open PR links (#24617)
Julius Marminge3eb836664eb1
diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.test.tsindex 10caf988e47..f17aaad4b86 100644--- a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts@@ -100,6 +100,33 @@ function shell(overrides: Partial<SettlementShell> = {}): SettlementShell {};}+function persistedLink(+ state: "open" | null,+ linkedAt: DateTime.Utc,+): NonNullable<SettlementShell["pullRequests"]>[number] {+ return {+ host: "example.test",+ repository: "owner/repository",+ number: 42,+ url: "https://example.test/owner/repository/pull/42",+ source: "manual",+ linkedAt: DateTime.formatIso(linkedAt),+ snapshot:+ state === null+ ? null+ : {+ state,+ title: "Pull request",+ headBranch: "feature",+ baseBranch: "main",+ isDraft: false,+ updatedAt: DateTime.formatIso(linkedAt),+ syncedAt: DateTime.formatIso(linkedAt),+ },+ stack: null,+ };+}+describe("isAutoSettlementCandidate", () => {it("settles a thread whose only background work is a command left running", () => {expect(@@ -186,6 +213,51 @@ describe("resolveAutoSettlementAt", () => {).toEqual(at(-4 * DAY_MS));});+ it("settles an idle thread with an open persisted link only while retention is off", () => {+ const input = {+ thread: shell({+ latestUserMessageAt: at(-4 * DAY_MS),+ pullRequests: [persistedLink("open", at(-5 * DAY_MS))],+ }),+ pullRequest: null,+ nowMs: NOW_MS,+ autoSettleAfterDays: 3,+ autoSettleOnMerge: true,+ autoSettleKeepPullRequests: false,+ };+ expect(ThreadSettlementService.resolveAutoSettlementAt(input)).toEqual(at(-4 * DAY_MS));+ expect(+ ThreadSettlementService.resolveAutoSettlementAt({+ ...input,+ autoSettleKeepPullRequests: true,+ }),+ ).toBeNull();+ // The open link outranks a merged lookup, so only inactivity can settle it.+ expect(+ ThreadSettlementService.resolveAutoSettlementAt({+ ...input,+ pullRequest: { state: "merged", mergedAt: DateTime.formatIso(at(-1)) },+ autoSettleAfterDays: null,+ }),+ ).toBeNull();+ });++ it("keeps an idle thread whose persisted link was never synced", () => {+ expect(+ ThreadSettlementService.resolveAutoSettlementAt({+ thread: shell({+ latestUserMessageAt: at(-4 * DAY_MS),+ pullRequests: [persistedLink(null, at(-5 * DAY_MS))],+ }),+ pullRequest: null,+ nowMs: NOW_MS,+ autoSettleAfterDays: 3,+ autoSettleOnMerge: true,+ autoSettleKeepPullRequests: false,+ }),+ ).toBeNull();+ });+it("uses the latest activity time when the inactivity window elapses", () => {const idle = shell({latestUserMessageAt: at(-4 * DAY_MS),@@ -697,6 +769,32 @@ describe("ThreadSettlementServiceV2 worker", () => {),);+ it.effect("settles an idle thread with an open persisted link on the inactivity sweep", () =>+ Effect.scoped(+ Effect.gen(function* () {+ yield* TestClock.setTime(Date.parse(NOW));+ const thread = makeThread("open-link", {+ pullRequests: [persistedLink("open", DateTime.makeUnsafe("2026-08-20T00:00:00.000Z"))],+ });+ const fixture = yield* makeHarness({+ snapshot: makeSnapshot([thread]),+ settings: {+ ...DEFAULT_SERVER_SETTINGS,+ sidebarAutoSettleAfterDays: 3,+ sidebarAutoSettleKeepPullRequests: false,+ },+ });+ yield* Effect.gen(function* () {+ const service = yield* ThreadSettlementService.ThreadSettlementServiceV2;+ yield* startHarness(service, fixture.activation, fixture.snapshotReads);+ expect((yield* Ref.get(fixture.commands)).map((command) => command.threadId)).toEqual([+ thread.id,+ ]);+ }).pipe(Effect.provide(fixture.layer));+ }),+ ),+ );+it.effect("settles only the project opted in while environment settlement is disabled", () =>Effect.scoped(Effect.gen(function* () {diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.tsindex 2efe9e3e896..d218205fbc5 100644--- a/apps/server/src/orchestration-v2/ThreadSettlementService.ts+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.ts@@ -192,8 +192,13 @@ export function resolveAutoSettlementAt(input: {const { thread } = input;let pullRequest = input.pullRequest;const links = settlementPullRequestLinks(thread.pullRequests ?? []);- if (links.some((link) => link.snapshot === null || link.snapshot.state === "open")) return null;- if (links.length > 0) {+ if (links.some((link) => link.snapshot === null)) return null;+ // An open link means the pull request has not finished, so only inactivity+ // can settle the thread, never a merged or closed lookup.+ const hasOpenLink = links.some((link) => link.snapshot?.state === "open");+ if (hasOpenLink) {+ pullRequest = null;+ } else if (links.length > 0) {const latest = links.reduce((current, candidate) =>linkTerminalAtMs(candidate) > linkTerminalAtMs(current) ? candidate : current,);@@ -218,7 +223,10 @@ export function resolveAutoSettlementAt(input: {return DateTime.makeUnsafe(activityAtMs);}const hasPullRequest =- pullRequest !== null || thread.linkedPullRequest != null || thread.branchPullRequest != null;+ hasOpenLink ||+ pullRequest !== null ||+ thread.linkedPullRequest != null ||+ thread.branchPullRequest != null;if (input.autoSettleKeepPullRequests === true && hasPullRequest) return null;if (input.autoSettleAfterDays === null) return null;const snoozedUntilMs = toMillis(thread.snoozedUntil);