Skip to content

Commit 7fff5d5

Browse files
Zeus-Deusclaude
andcommitted
fix(server): threads no longer settle after merging PRs they were asked to merge
A thread asked to merge pull requests would link each one after merging it. Settle-on-merge then read those links as the thread's own work shipping and settled it while the agent was still working. A link made after its pull request had already merged or closed is now a record of work, not an outcome the thread waits on, so it no longer settles it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent ed0f7b9 commit 7fff5d5

2 files changed

Lines changed: 78 additions & 9 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,53 @@ describe("resolveAutoSettlementAt", () => {
358358
).toBeNull();
359359
});
360360

361+
it("does not settle on pull requests that were already merged when linked", () => {
362+
const link = (number: number, linkedAt: DateTime.Utc, mergedAt: DateTime.Utc) => ({
363+
host: "example.test",
364+
repository: "owner/repository",
365+
number,
366+
url: `https://example.test/owner/repository/pull/${number}`,
367+
source: "agent" as const,
368+
linkedAt: DateTime.formatIso(linkedAt),
369+
snapshot: {
370+
state: "merged" as const,
371+
title: "Pull request",
372+
headBranch: `feature-${number}`,
373+
baseBranch: "main",
374+
isDraft: false,
375+
updatedAt: DateTime.formatIso(mergedAt),
376+
syncedAt: DateTime.formatIso(linkedAt),
377+
mergedAt: DateTime.formatIso(mergedAt),
378+
},
379+
stack: null,
380+
});
381+
// The user asked the agent to merge pull requests; it merged one, then
382+
// linked it to the thread as a record of the work.
383+
const recorded = link(1, at(-50 * 60 * 1_000), at(-55 * 60 * 1_000));
384+
const input = {
385+
thread: shell({
386+
latestUserMessageAt: at(-2 * 60 * 60 * 1_000),
387+
latestRunCompletedAt: at(-45 * 60 * 1_000),
388+
pullRequests: [recorded],
389+
}),
390+
pullRequest: null,
391+
nowMs: NOW_MS,
392+
autoSettleAfterDays: null,
393+
autoSettleOnMerge: true,
394+
};
395+
expect(ThreadSettlementService.resolveAutoSettlementAt(input)).toBeNull();
396+
// A pull request linked while open still settles the thread when it merges.
397+
expect(
398+
ThreadSettlementService.resolveAutoSettlementAt({
399+
...input,
400+
thread: {
401+
...input.thread,
402+
pullRequests: [recorded, link(2, at(-90 * 60 * 1_000), at(-40 * 60 * 1_000))],
403+
},
404+
}),
405+
).toEqual(at(-45 * 60 * 1_000));
406+
});
407+
361408
it("settles inactive threads even when their pull request remains open", () => {
362409
const input = {
363410
thread: shell({ latestUserMessageAt: at(-30 * DAY_MS) }),

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ function pullRequestMatchesProject(
7777
);
7878
}
7979

80+
type SettlementPullRequestLink = NonNullable<
81+
ProjectionStore.ProjectionSettlementCandidate["pullRequests"]
82+
>[number];
83+
84+
/** When the linked pull request merged or closed, or -Infinity if it has not. */
85+
function linkTerminalAtMs(link: SettlementPullRequestLink): number {
86+
const snapshot = link.snapshot;
87+
const value = snapshot?.state === "merged" ? snapshot.mergedAt : snapshot?.closedAt;
88+
const timestamp = Date.parse(value ?? "");
89+
return Number.isNaN(timestamp) ? Number.NEGATIVE_INFINITY : timestamp;
90+
}
91+
92+
/**
93+
* The links whose outcome the thread waits on. A link made after its pull
94+
* request had already merged or closed records work the thread did, such as an
95+
* agent merging pull requests it was asked to merge, so it cannot settle the
96+
* thread.
97+
*/
98+
function settlementPullRequestLinks(
99+
links: ReadonlyArray<SettlementPullRequestLink>,
100+
): ReadonlyArray<SettlementPullRequestLink> {
101+
return visibleThreadPullRequests(links).filter((link) => {
102+
if (link.snapshot === null || link.snapshot.state === "open") return true;
103+
const linkedAtMs = Date.parse(link.linkedAt);
104+
return Number.isNaN(linkedAtMs) || linkTerminalAtMs(link) >= linkedAtMs;
105+
});
106+
}
107+
80108
/**
81109
* A recent user message stays queued until a run adopts its timestamp.
82110
* Absolute age bounds client clock skew in both directions and stops stale
@@ -205,17 +233,11 @@ export function resolveAutoSettlementAt(input: {
205233
}): DateTime.Utc | null {
206234
const { thread } = input;
207235
let pullRequest = input.pullRequest;
208-
const links = visibleThreadPullRequests(thread.pullRequests ?? []);
236+
const links = settlementPullRequestLinks(thread.pullRequests ?? []);
209237
if (links.some((link) => link.snapshot === null || link.snapshot.state === "open")) return null;
210238
if (links.length > 0) {
211-
const terminalAt = (link: (typeof links)[number]) => {
212-
const snapshot = link.snapshot;
213-
const value = snapshot?.state === "merged" ? snapshot.mergedAt : snapshot?.closedAt;
214-
const timestamp = Date.parse(value ?? "");
215-
return Number.isNaN(timestamp) ? Number.NEGATIVE_INFINITY : timestamp;
216-
};
217239
const latest = links.reduce((current, candidate) =>
218-
terminalAt(candidate) > terminalAt(current) ? candidate : current,
240+
linkTerminalAtMs(candidate) > linkTerminalAtMs(current) ? candidate : current,
219241
);
220242
pullRequest =
221243
latest.snapshot === null
@@ -376,7 +398,7 @@ export const make = Effect.gen(function* () {
376398
{ concurrency: 8 },
377399
))
378400
.filter((thread) => thread !== null)
379-
.filter((thread) => visibleThreadPullRequests(thread.pullRequests ?? []).length === 0);
401+
.filter((thread) => settlementPullRequestLinks(thread.pullRequests ?? []).length === 0);
380402
// Use the same cwd as the sidebar so both paths share GitManager's PR cache.
381403
const lookupCwdByThreadId = new Map<string, string>();
382404
yield* Effect.forEach(

0 commit comments

Comments
 (0)