Skip to content

Commit 3eb8366

Browse files
fix(server): settle inactive threads with persisted open PR links (pingdotgg#24617)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 97548db commit 3eb8366

2 files changed

Lines changed: 109 additions & 3 deletions

File tree

‎apps/server/src/orchestration-v2/ThreadSettlementService.test.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
103130
describe("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* () {

‎apps/server/src/orchestration-v2/ThreadSettlementService.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,13 @@ export function resolveAutoSettlementAt(input: {
192192
const { thread } = input;
193193
let pullRequest = input.pullRequest;
194194
const links = settlementPullRequestLinks(thread.pullRequests ?? []);
195-
if (links.some((link) => link.snapshot === null || link.snapshot.state === "open")) return null;
196-
if (links.length > 0) {
195+
if (links.some((link) => link.snapshot === null)) return null;
196+
// An open link means the pull request has not finished, so only inactivity
197+
// can settle the thread, never a merged or closed lookup.
198+
const hasOpenLink = links.some((link) => link.snapshot?.state === "open");
199+
if (hasOpenLink) {
200+
pullRequest = null;
201+
} else if (links.length > 0) {
197202
const latest = links.reduce((current, candidate) =>
198203
linkTerminalAtMs(candidate) > linkTerminalAtMs(current) ? candidate : current,
199204
);
@@ -218,7 +223,10 @@ export function resolveAutoSettlementAt(input: {
218223
return DateTime.makeUnsafe(activityAtMs);
219224
}
220225
const hasPullRequest =
221-
pullRequest !== null || thread.linkedPullRequest != null || thread.branchPullRequest != null;
226+
hasOpenLink ||
227+
pullRequest !== null ||
228+
thread.linkedPullRequest != null ||
229+
thread.branchPullRequest != null;
222230
if (input.autoSettleKeepPullRequests === true && hasPullRequest) return null;
223231
if (input.autoSettleAfterDays === null) return null;
224232
const snoozedUntilMs = toMillis(thread.snoozedUntil);

0 commit comments

Comments
 (0)