Skip to content
TypeScript

fix(server): automatically settle empty inactive threads (#15783)

Adamulek123d5fdfdcf0533
diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
index b591ef7a2ea..4ad315427cd 100644
--- a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
@@ -297,8 +297,28 @@ describe("resolveAutoSettlementAt", () => {
expect(
ThreadSettlementService.resolveAutoSettlementAt({ ...input, autoSettleAfterDays: null }),
).toBeNull();
+ });
+
+ it("anchors an empty thread's inactivity at its creation time", () => {
+ const input = {
+ thread: shell({ createdAt: at(-3 * DAY_MS) }),
+ pullRequest: null,
+ nowMs: NOW_MS,
+ autoSettleAfterDays: 2,
+ autoSettleOnMerge: false,
+ };
+ expect(ThreadSettlementService.resolveAutoSettlementAt(input)).toEqual(at(-3 * DAY_MS));
expect(
- ThreadSettlementService.resolveAutoSettlementAt({ ...input, thread: shell() }),
+ ThreadSettlementService.resolveAutoSettlementAt({
+ ...input,
+ thread: shell({ createdAt: at(-DAY_MS) }),
+ }),
+ ).toBeNull();
+ expect(
+ ThreadSettlementService.resolveAutoSettlementAt({
+ ...input,
+ thread: { ...input.thread, lastSnoozeWakeAt: at(-DAY_MS) },
+ }),
).toBeNull();
});
diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.ts
index 9c2eceef512..d6cfe2caec4 100644
--- a/apps/server/src/orchestration-v2/ThreadSettlementService.ts
+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.ts
@@ -231,19 +231,20 @@ export function resolveAutoSettlementAt(input: {
};
}
if (!isAutoSettlementCandidate(thread, input.nowMs)) return null;
- const activityAtMs = latestMillis([
- toMillis(thread.latestUserMessageAt),
- toMillis(thread.latestRunRequestedAt),
- toMillis(thread.latestRunStartedAt),
- toMillis(thread.latestRunCompletedAt),
- ]);
+ const activityAtMs =
+ latestMillis([
+ toMillis(thread.latestUserMessageAt),
+ toMillis(thread.latestRunRequestedAt),
+ toMillis(thread.latestRunStartedAt),
+ toMillis(thread.latestRunCompletedAt),
+ ]) ?? DateTime.toEpochMillis(thread.createdAt);
if (pullRequest !== null && pullRequestSettles(thread, pullRequest, input.autoSettleOnMerge)) {
- return activityAtMs === null ? thread.createdAt : DateTime.makeUnsafe(activityAtMs);
+ return DateTime.makeUnsafe(activityAtMs);
}
const hasPullRequest =
pullRequest !== null || thread.linkedPullRequest != null || thread.branchPullRequest != null;
if (input.autoSettleKeepPullRequests === true && hasPullRequest) return null;
- if (input.autoSettleAfterDays === null || activityAtMs === null) return null;
+ if (input.autoSettleAfterDays === null) return null;
const snoozedUntilMs = toMillis(thread.snoozedUntil);
const timerWakeAtMs =
snoozedUntilMs !== null && snoozedUntilMs <= input.nowMs ? snoozedUntilMs : null;
diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md
index f98d9e91437..63732eb4a10 100644
--- a/docs/user/thread-sidebar.md
+++ b/docs/user/thread-sidebar.md
@@ -148,7 +148,8 @@ Press `Escape` while dragging to cancel.
By default, environments settle inactive threads after three days and settle
threads whose pull request merged. A closed pull request can also settle an idle
-thread. Work in progress, pending questions or approvals, and live background work
+thread. An empty thread's inactivity counts from when it was created.
+Work in progress, pending questions or approvals, and live background work
prevent automatic settlement. An open pull request does not prevent inactivity
settlement unless you turn on **Keep threads with a PR** in **Settings → General**,
but an old closed or merged pull request does not settle work you