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): Bitbucket threads settle after their pull request merges (#19937)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sebastian Pugliese committed
Oct 18, 2026 at 21:20 UTC
beadfe56277707d693101c97b0983989ecbc8682
7 files changed
+129
-6
apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts
+37
-1
@@ -630,7 +630,11 @@ describe("PullRequestSyncReactor", () => {
630
yield* TestClock.setTime(Date.parse(NOW));
631
const fixture = yield* makeHarness({
632
snapshot: makeSnapshot([
633
- makeThread("merged", { pullRequests: [makeLink(1, { state: "merged" })] }),
633
+ makeThread("merged", {
634
+ pullRequests: [
635
+ makeLink(1, { state: "merged", mergedAt: "2026-08-27T00:00:00.000Z" }),
636
+ ],
637
+ }),
638
]),
639
});
640
@@ -658,6 +662,38 @@ describe("PullRequestSyncReactor", () => {
662
),
663
);
664
665
+ it.effect("re-reads a merged pull request until the host supplies its merge time", () =>
666
+ Effect.scoped(
667
+ Effect.gen(function* () {
668
+ yield* TestClock.setTime(Date.parse(NOW));
669
+ const invalidated = yield* Ref.make<ReadonlyArray<number>>([]);
670
+ const fixture = yield* makeHarness({
671
+ snapshot: makeSnapshot([
672
+ makeThread("merged", { pullRequests: [makeLink(1, { state: "merged" })] }),
673
+ ]),
674
+ invalidate: ({ reference }) =>
675
+ Ref.update(invalidated, (numbers) => [...numbers, reference?.number ?? -1]),
676
+ summary: (input) =>
677
+ Effect.succeed(
678
+ makeSummary(input, { state: "merged", mergedAt: "2026-08-27T00:00:00.000Z" }),
679
+ ),
680
+ });
681
+
682
+ yield* Effect.gen(function* () {
683
+ const reactor = yield* startAndSweep(fixture);
684
+ const firstSweep = yield* Ref.get(fixture.syncCommands);
685
+ // The held merged summary is dropped so the read reaches the host.
686
+ assert.deepStrictEqual(yield* Ref.get(invalidated), [1]);
687
+ assert.strictEqual(firstSweep.at(-1)?.snapshot.mergedAt, "2026-08-27T00:00:00.000Z");
688
+ yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, firstSweep));
689
+
690
+ for (let index = 0; index < 15; index += 1) yield* sweepAgain(fixture, reactor);
691
+ assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
692
+ }).pipe(Effect.provide(fixture.layer));
693
+ }),
694
+ ),
695
+ );
696
+
697
it.effect("syncs a pull request a reader saw merge without waiting for the sweep", () =>
698
Effect.scoped(
699
Effect.gen(function* () {
apps/server/src/orchestration-v2/PullRequestSyncReactor.ts
+17
-2
@@ -167,7 +167,15 @@ export const make = Effect.gen(function* () {
167
if (requested.has(key) || retryStacks.has(key)) return true;
168
if (entries.some((entry) => entry.link.snapshot === null)) return true;
169
const active = entries.filter((entry) => isUnsettled(entry.thread));
170
- if (active.every((entry) => entry.link.snapshot?.state === "merged")) return false;
170
+ // A merged snapshot without its merge time cannot settle the thread, so it keeps the slow
171
+ // cadence until the host supplies one (Bitbucket snapshots saved before it was read).
172
+ if (
173
+ active.every(
174
+ (entry) => entry.link.snapshot?.state === "merged" && entry.link.snapshot.mergedAt != null,
175
+ )
176
+ ) {
177
+ return false;
178
+ }
179
if (active.some((entry) => entry.link.snapshot?.state === "open")) return true;
180
const last = lastSyncedAt.get(key);
181
return last === undefined || nowMs - last >= SLOW_SYNC_INTERVAL_MS;
@@ -271,7 +279,14 @@ export const make = Effect.gen(function* () {
279
number: first.link.number,
280
};
281
const generation = requested.get(key);
274
- if (generation !== undefined) yield* pullRequests.invalidate({ reference: ref });
282
+ // A held merged summary is served without asking the host, so a snapshot still missing its
283
+ // merge time has to drop it or the slow-cadence re-read would hand back the same gap.
284
+ const missingMergedAt = entries.some(
285
+ (entry) => entry.link.snapshot?.state === "merged" && entry.link.snapshot.mergedAt == null,
286
+ );
287
+ if (generation !== undefined || missingMergedAt) {
288
+ yield* pullRequests.invalidate({ reference: ref });
289
+ }
290
const summary = yield* pullRequests.summary(ref, { recoverTransientFailure: false });
291
const fields = snapshotFieldsOf(summary);
292
const needsStack =
packages/source-control-bitbucket/src/server/BitbucketPullRequestApi.test.ts
+18
@@ -711,6 +711,24 @@ layer("BitbucketPullRequestApi.layer", (it) => {
711
}),
712
);
713
714
+ it.effect("asks for the time a pull request closed, which Bitbucket leaves out by default", () =>
715
+ Effect.gen(function* () {
716
+ mockedRequest.mockReturnValueOnce(
717
+ Effect.succeed(
718
+ response(
719
+ pullRequestJson({ state: "MERGED", closed_on: "2025-06-16T23:40:46.222238+00:00" }),
720
+ ),
721
+ ),
722
+ );
723
+ const api = yield* BitbucketPullRequestApi.BitbucketPullRequestApi;
724
+
725
+ const pullRequest = yield* api.getPullRequest({ repository: "acme/web", number: 7 });
726
+
727
+ expect(callAt(0).url).toBe("/repositories/acme/web/pullrequests/7?fields=%2Bclosed_on");
728
+ assert.strictEqual(pullRequest.closedAt, "2025-06-16T23:40:46.222Z");
729
+ }),
730
+ );
731
+
732
it.effect("fails the read when Bitbucket answers with something unreadable", () =>
733
Effect.gen(function* () {
734
mockedRequest.mockReturnValueOnce(
packages/source-control-bitbucket/src/server/BitbucketPullRequestApi.ts
+3
-1
@@ -585,7 +585,9 @@ export const make = Effect.gen(function* () {
585
withRepository(input.repository, (path) =>
586
readPage({
587
operation: "getPullRequest",
588
- url: `${path}/pullrequests/${input.number}`,
588
+ // `closed_on` is left out of the default representation. It is the only merge or
589
+ // decline time Bitbucket states on the pull request itself, and settlement needs it.
590
+ url: `${path}/pullrequests/${input.number}?fields=%2Bclosed_on`,
591
decode: decodePullRequestJson,
592
}),
593
),
packages/source-control-bitbucket/src/server/BitbucketPullRequestProvider.test.ts
+47
@@ -70,6 +70,53 @@ it.effect.each([
70
}),
71
);
72
73
+it.effect.each([
74
+ { state: "MERGED", mergedAt: "2025-06-16T23:40:46.222Z", closedAt: null },
75
+ { state: "DECLINED", mergedAt: null, closedAt: "2025-06-16T23:40:46.222Z" },
76
+ { state: "OPEN", mergedAt: null, closedAt: null },
77
+])("reads the $state time from closed_on", ({ state, mergedAt, closedAt }) =>
78
+ Effect.gen(function* () {
79
+ const pullRequest = Result.getOrThrow(
80
+ decodePullRequestJson(
81
+ JSON.stringify({
82
+ id: 52,
83
+ title: "Release",
84
+ state,
85
+ source: { branch: { name: "release" } },
86
+ destination: { branch: { name: "master" } },
87
+ created_on: "2025-06-16T06:20:58.415108+00:00",
88
+ // Comments after a merge move this, so it is not the merge time.
89
+ updated_on: "2025-06-20T10:00:00.000000+00:00",
90
+ closed_on: state === "OPEN" ? null : "2025-06-16T23:40:46.222238+00:00",
91
+ links: { html: { href: "https://bitbucket.org/acme/web/pull-requests/52" } },
92
+ }),
93
+ ),
94
+ );
95
+ const provider = yield* make.pipe(
96
+ Effect.provide(
97
+ Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
98
+ getPullRequest: () => Effect.succeed(pullRequest),
99
+ getDiffStat: () => Effect.succeed({ additions: 0, deletions: 0, changedFiles: 0 }),
100
+ getMergeability: () => Effect.succeed("unknown"),
101
+ listChecks: () => Effect.succeed([]),
102
+ getRepositoryPermission: () => Effect.succeed(true),
103
+ }),
104
+ ),
105
+ );
106
+ const detail = yield* provider.getChangeRequest({
107
+ cwd: "/repo",
108
+ repository: "acme/web",
109
+ host: "bitbucket.org",
110
+ number: 52,
111
+ });
112
+
113
+ expect({ mergedAt: detail.mergedAt, closedAt: detail.closedAt }).toEqual({
114
+ mergedAt,
115
+ closedAt,
116
+ });
117
+ }),
118
+);
119
+
120
for (const operation of [
121
"getMergeability",
122
"listChecks",
packages/source-control-bitbucket/src/server/BitbucketPullRequestProvider.ts
+2
-2
@@ -198,8 +198,8 @@ export const make = Effect.gen(function* () {
198
deletions: diffStat.deletions,
199
changedFiles: diffStat.changedFiles,
200
body: pullRequest.body,
201
- mergedAt: null,
202
- closedAt: null,
201
+ mergedAt: pullRequest.state === "merged" ? pullRequest.closedAt : null,
202
+ closedAt: pullRequest.state === "closed" ? pullRequest.closedAt : null,
203
reviewers: pullRequest.reviewers,
204
checks,
205
// Bitbucket publishes no per-repository list of allowed strategies, so the ones it
packages/source-control-bitbucket/src/server/bitbucketPullRequestJson.ts
+5
@@ -66,6 +66,8 @@ const RawPullRequestSchema = Schema.Struct({
66
destination: RawBranchSchema,
67
created_on: Schema.String,
68
updated_on: Schema.String,
69
+ /** Only sent when asked for with `fields=+closed_on`; null while the request is open. */
70
+ closed_on: Schema.optional(Schema.NullOr(Schema.String)),
71
reviewers: Schema.optional(Schema.NullOr(Schema.Array(RawUserSchema))),
72
participants: Schema.optional(
73
Schema.NullOr(
@@ -175,6 +177,8 @@ export interface BitbucketPullRequest {
177
readonly mergeability: PullRequestMergeability;
178
readonly createdAt: string;
179
readonly updatedAt: string;
180
+ /** When the request was merged or declined, unlike `updatedAt`, which later comments move. */
181
+ readonly closedAt: string | null;
182
readonly body: string;
183
readonly reviewRequestLogins: ReadonlyArray<string>;
184
readonly reviewers: ReadonlyArray<PullRequestActor>;
@@ -280,6 +284,7 @@ function toPullRequest(raw: Schema.Schema.Type<typeof RawPullRequestSchema>): Bi
284
mergeability: "unknown",
285
createdAt: toIsoUtc(raw.created_on),
286
updatedAt: toIsoUtc(raw.updated_on),
287
+ closedAt: raw.closed_on == null ? null : toIsoUtc(raw.closed_on),
288
body: raw.description ?? "",
289
reviewRequestLogins: reviewers.map((reviewer) => reviewer.login),
290
reviewers,