The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 18:15 UTC
fix(server): settle inactive threads with persisted open PR links (#24617)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 24, 2026 at 17:20 UTC
3eb836664eb1a5d0102055fe1e4a7fc5c1d8a9c7
2 files changed
+109
-3
apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+98
@@ -100,6 +100,33 @@ function shell(overrides: Partial<SettlementShell> = {}): SettlementShell {
100
};
101
}
102
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
+
130
describe("isAutoSettlementCandidate", () => {
131
it("settles a thread whose only background work is a command left running", () => {
132
expect(
@@ -186,6 +213,51 @@ describe("resolveAutoSettlementAt", () => {
213
).toEqual(at(-4 * DAY_MS));
214
});
215
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
+
261
it("uses the latest activity time when the inactivity window elapses", () => {
262
const idle = shell({
263
latestUserMessageAt: at(-4 * DAY_MS),
@@ -697,6 +769,32 @@ describe("ThreadSettlementServiceV2 worker", () => {
769
),
770
);
771
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
+
798
it.effect("settles only the project opted in while environment settlement is disabled", () =>
799
Effect.scoped(
800
Effect.gen(function* () {
apps/server/src/orchestration-v2/ThreadSettlementService.ts
+11
-3
@@ -192,8 +192,13 @@ export function resolveAutoSettlementAt(input: {
192
const { thread } = input;
193
let pullRequest = input.pullRequest;
194
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) {
202
const latest = links.reduce((current, candidate) =>
203
linkTerminalAtMs(candidate) > linkTerminalAtMs(current) ? candidate : current,
204
);
@@ -218,7 +223,10 @@ export function resolveAutoSettlementAt(input: {
223
return DateTime.makeUnsafe(activityAtMs);
224
}
225
const hasPullRequest =
221
- pullRequest !== null || thread.linkedPullRequest != null || thread.branchPullRequest != null;
226
+ hasOpenLink ||
227
+ pullRequest !== null ||
228
+ thread.linkedPullRequest != null ||
229
+ thread.branchPullRequest != null;
230
if (input.autoSettleKeepPullRequests === true && hasPullRequest) return null;
231
if (input.autoSettleAfterDays === null) return null;
232
const snoozedUntilMs = toMillis(thread.snoozedUntil);