@devries-cameron20 / t3code Mirror · read-only / commits / ca2afd8aea
Fork 0

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 23, 04:15 UTC

fix(server): threads no longer settle after merging PRs they were asked to merge (#18232)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Zeus-Deus committed Oct 18, 2026 at 21:21 UTC ca2afd8aea1a452098da09e1e8792f50f0705595
2 files changed +135 -8
apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+105
@@ -380,6 +380,53 @@ describe("resolveAutoSettlementAt", () => {
380 ).toBeNull();
381 });
382
383 + it("does not settle on pull requests that were already merged when linked", () => {
384 + const link = (number: number, linkedAt: DateTime.Utc, mergedAt: DateTime.Utc) => ({
385 + host: "example.test",
386 + repository: "owner/repository",
387 + number,
388 + url: `https://example.test/owner/repository/pull/${number}`,
389 + source: "agent" as const,
390 + linkedAt: DateTime.formatIso(linkedAt),
391 + snapshot: {
392 + state: "merged" as const,
393 + title: "Pull request",
394 + headBranch: `feature-${number}`,
395 + baseBranch: "main",
396 + isDraft: false,
397 + updatedAt: DateTime.formatIso(mergedAt),
398 + syncedAt: DateTime.formatIso(linkedAt),
399 + mergedAt: DateTime.formatIso(mergedAt),
400 + },
401 + stack: null,
402 + });
403 + // The user asked the agent to merge pull requests; it merged one, then
404 + // linked it to the thread as a record of the work.
405 + const recorded = link(1, at(-50 * 60 * 1_000), at(-55 * 60 * 1_000));
406 + const input = {
407 + thread: shell({
408 + latestUserMessageAt: at(-2 * 60 * 60 * 1_000),
409 + latestRunCompletedAt: at(-45 * 60 * 1_000),
410 + pullRequests: [recorded],
411 + }),
412 + pullRequest: null,
413 + nowMs: NOW_MS,
414 + autoSettleAfterDays: null,
415 + autoSettleOnMerge: true,
416 + };
417 + expect(ThreadSettlementService.resolveAutoSettlementAt(input)).toBeNull();
418 + // A pull request linked while open still settles the thread when it merges.
419 + expect(
420 + ThreadSettlementService.resolveAutoSettlementAt({
421 + ...input,
422 + thread: {
423 + ...input.thread,
424 + pullRequests: [recorded, link(2, at(-90 * 60 * 1_000), at(-40 * 60 * 1_000))],
425 + },
426 + }),
427 + ).toEqual(at(-45 * 60 * 1_000));
428 + });
429 +
430 it("settles inactive threads even when their pull request remains open", () => {
431 const input = {
432 thread: shell({ latestUserMessageAt: at(-30 * DAY_MS) }),
@@ -852,6 +899,64 @@ describe("ThreadSettlementServiceV2 worker", () => {
899 ),
900 );
901
902 + it.effect("does not look up a pull request that was already merged when linked", () =>
903 + Effect.scoped(
904 + Effect.gen(function* () {
905 + yield* TestClock.setTime(Date.parse(NOW));
906 + const reference = {
907 + projectId: PROJECT_ID,
908 + repository: "owner/repository",
909 + number: 42,
910 + url: "https://example.test/owner/repository/pull/42",
911 + };
912 + const fixture = yield* makeHarness({
913 + snapshot: makeSnapshot([
914 + makeThread("merged-then-linked", {
915 + linkedPullRequest: reference,
916 + pullRequests: [
917 + {
918 + host: "example.test",
919 + repository: reference.repository,
920 + number: reference.number,
921 + url: reference.url,
922 + source: "agent",
923 + linkedAt: "2026-08-28T00:00:00.000Z",
924 + snapshot: {
925 + state: "merged",
926 + title: "Pull request",
927 + headBranch: "feature",
928 + baseBranch: "main",
929 + isDraft: false,
930 + updatedAt: "2026-08-27T00:00:00.000Z",
931 + syncedAt: "2026-08-28T00:00:00.000Z",
932 + mergedAt: "2026-08-27T00:00:00.000Z",
933 + },
934 + stack: null,
935 + },
936 + ],
937 + }),
938 + ]),
939 + settings: {
940 + ...DEFAULT_SERVER_SETTINGS,
941 + sidebarAutoSettleAfterDays: null,
942 + sidebarAutoSettleOnMerge: true,
943 + },
944 + pullRequestSummary: (input) =>
945 + Effect.succeed({
946 + ...makePullRequestSummary({ ...input, state: "merged" }),
947 + mergedAt: "2026-08-27T00:00:00.000Z",
948 + }),
949 + });
950 + yield* Effect.gen(function* () {
951 + const reactor = yield* ThreadSettlementService.ThreadSettlementServiceV2;
952 + yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
953 + assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);
954 + assert.deepStrictEqual(yield* Ref.get(fixture.summaryCalls), []);
955 + }).pipe(Effect.provide(fixture.layer));
956 + }),
957 + ),
958 + );
959 +
960 it.effect("reevaluates immediately after a pull request merge", () =>
961 Effect.scoped(
962 Effect.gen(function* () {
apps/server/src/orchestration-v2/ThreadSettlementService.ts
+30 -8
@@ -77,6 +77,34 @@ function pullRequestMatchesProject(
77 );
78 }
79
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 +
108 /**
109 * Absolute age bounds client clock skew in both directions and stops stale
110 * pre-adoption data from blocking the thread forever.
@@ -187,17 +215,11 @@ export function resolveAutoSettlementAt(input: {
215 }): DateTime.Utc | null {
216 const { thread } = input;
217 let pullRequest = input.pullRequest;
190 - const links = visibleThreadPullRequests(thread.pullRequests ?? []);
218 + const links = settlementPullRequestLinks(thread.pullRequests ?? []);
219 if (links.some((link) => link.snapshot === null || link.snapshot.state === "open")) return null;
220 if (links.length > 0) {
193 - const terminalAt = (link: (typeof links)[number]) => {
194 - const snapshot = link.snapshot;
195 - const value = snapshot?.state === "merged" ? snapshot.mergedAt : snapshot?.closedAt;
196 - const timestamp = Date.parse(value ?? "");
197 - return Number.isNaN(timestamp) ? Number.NEGATIVE_INFINITY : timestamp;
198 - };
221 const latest = links.reduce((current, candidate) =>
200 - terminalAt(candidate) > terminalAt(current) ? candidate : current,
222 + linkTerminalAtMs(candidate) > linkTerminalAtMs(current) ? candidate : current,
223 );
224 pullRequest =
225 latest.snapshot === null