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 21, 03:37 UTC
fix(server): watch_pull_request on a merged PR reports it instead of failing (#39899)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 20, 2026 at 14:57 UTC
b076d8c8bf62e9780be6ab0b0c2b671ab697b37f
4 files changed
+54
-39
apps/server/src/mcp/toolkits/pullRequests/handlers.ts
+6
@@ -61,6 +61,7 @@ import {
61
PullRequestProjectNotFoundError,
62
PullRequestsToolkit,
63
type ThreadPullRequestEntry,
64
+ alreadyEndedWatchNext,
65
CHILD_PULL_REQUEST_REPORT_NEXT,
66
} from "./tools.ts";
67
@@ -326,6 +327,11 @@ const make = Effect.gen(function* () {
327
return yield* watches
328
.setWatching({ threadId, target: input, watching, until: input.until })
329
.pipe(
330
+ Effect.map((result) =>
331
+ result.state === undefined
332
+ ? result
333
+ : { ...result, next: alreadyEndedWatchNext(result.state) },
334
+ ),
335
Effect.catchTags({
336
PullRequestWatchThreadNotFoundError: (error) =>
337
threadNotFound(engine, scope, error.threadId, error.message),
apps/server/src/mcp/toolkits/pullRequests/tools.ts
+9
-1
@@ -186,7 +186,6 @@ export const PullRequestToolError = Schema.Union([
186
PullRequestListFailedError,
187
PullRequestWatchService.PullRequestWatchFailedError,
188
PullRequestUnavailableError,
189
- PullRequestWatchService.PullRequestNotOpenError,
189
PullRequestWatchService.PullRequestWatchFromSubagentError,
190
PullRequestProjectNotFoundError,
191
PullRequestGuideFailedError,
@@ -258,9 +257,18 @@ export const WatchPullRequestResult = Schema.Struct({
257
...PullRequestIdentity,
258
watching: Schema.Boolean,
259
wasWatching: Schema.Boolean,
260
+ state: Schema.optional(
261
+ Schema.Literals(["merged", "closed"]).annotate({
262
+ description: "The PR already ended, so nothing was watched.",
263
+ }),
264
+ ),
265
+ next: Schema.optional(Schema.String),
266
});
267
export type WatchPullRequestResult = typeof WatchPullRequestResult.Type;
268
269
+export const alreadyEndedWatchNext = (state: "merged" | "closed") =>
270
+ `This PR is already ${state}, so there is nothing to watch. Report that and end your turn.`;
271
+
272
export const ThreadPullRequestEntry = Schema.Struct({
273
...PullRequestIdentity,
274
source: ThreadPullRequestLinkSource,
apps/server/src/pullRequest/PullRequestWatchService.test.ts
+18
-16
@@ -225,13 +225,12 @@ describe("PullRequestWatchService", () => {
225
}),
226
);
227
228
- it.effect("rejects merged PRs and keeps an active watch usable without hosting access", () =>
228
+ it.effect("reports merged PRs without watching and keeps an active watch usable offline", () =>
229
Effect.gen(function* () {
230
const freshMerged = yield* makeHarness({ readState: "merged" });
231
- const freshError = yield* freshMerged
232
- .setWatching(true, { repository: "t3tools/t3code", number: 1 })
233
- .pipe(Effect.flip);
234
- expect(freshError).toMatchObject({ _tag: "PullRequestNotOpenError", state: "merged" });
231
+ expect(
232
+ yield* freshMerged.setWatching(true, { repository: "t3tools/t3code", number: 1 }),
233
+ ).toMatchObject({ number: 1, watching: false, wasWatching: false, state: "merged" });
234
expect(yield* Ref.get(freshMerged.commands)).toEqual([]);
235
236
const watch = {
@@ -254,10 +253,9 @@ describe("PullRequestWatchService", () => {
253
makeLink(3, { headBranch: "watched", watch }),
254
]),
255
});
257
- const error = yield* harness
258
- .setWatching(true, { repository: "t3tools/t3code", number: 1 })
259
- .pipe(Effect.flip);
260
- expect(error).toMatchObject({ _tag: "PullRequestNotOpenError", state: "merged" });
256
+ expect(
257
+ yield* harness.setWatching(true, { repository: "t3tools/t3code", number: 1 }),
258
+ ).toMatchObject({ number: 1, watching: false, state: "merged" });
259
expect(
260
yield* harness.setWatching(true, { repository: "t3tools/t3code", number: 3 }),
261
).toMatchObject({ watching: true, wasWatching: true });
@@ -271,15 +269,19 @@ describe("PullRequestWatchService", () => {
269
}),
270
);
271
274
- it.effect("watches a pull request saved as closed, since it may have reopened", () =>
272
+ it.effect("re-reads a pull request saved as closed, and watches it only if it reopened", () =>
273
Effect.gen(function* () {
274
const closed = makeLink(1, { headBranch: "closed" });
277
- const harness = yield* makeHarness({
278
- readState: "closed",
279
- thread: makeThread([
280
- { ...closed, snapshot: closed.snapshot && { ...closed.snapshot, state: "closed" } },
281
- ]),
282
- });
275
+ const closedThread = makeThread([
276
+ { ...closed, snapshot: closed.snapshot && { ...closed.snapshot, state: "closed" } },
277
+ ]);
278
+ const stillClosed = yield* makeHarness({ readState: "closed", thread: closedThread });
279
+ expect(
280
+ yield* stillClosed.setWatching(true, { repository: "t3tools/t3code", number: 1 }),
281
+ ).toMatchObject({ watching: false, state: "closed" });
282
+ expect(yield* Ref.get(stillClosed.commands)).toEqual([]);
283
+
284
+ const harness = yield* makeHarness({ readState: "open", thread: closedThread });
285
yield* harness.setWatching(true, { repository: "t3tools/t3code", number: 1 });
286
expect(yield* Ref.get(harness.commands)).toMatchObject([
287
{ type: "thread.pull-request.watch", number: 1, watching: true },
apps/server/src/pullRequest/PullRequestWatchService.ts
+21
-22
@@ -36,15 +36,6 @@ export class PullRequestWatchFailedError extends Schema.TaggedError<PullRequestW
36
}
37
}
38
39
-export class PullRequestNotOpenError extends Schema.TaggedError<PullRequestNotOpenError>()(
40
- "PullRequestNotOpenError",
41
- { state: Schema.String },
42
-) {
43
- override get message(): string {
44
- return `The pull request is ${this.state}, so there is nothing to watch.`;
45
- }
46
-}
47
-
39
export class PullRequestWatchFromSubagentError extends Schema.TaggedError<PullRequestWatchFromSubagentError>()(
40
"PullRequestWatchFromSubagentError",
41
{},
@@ -72,9 +63,13 @@ export class PullRequestWatchService extends Context.Service<
63
readonly watching: boolean;
64
readonly until?: PullRequestWatchUntil | undefined;
65
}) => Effect.Effect<
75
- ResolvedTarget & { readonly watching: boolean; readonly wasWatching: boolean },
66
+ ResolvedTarget & {
67
+ readonly watching: boolean;
68
+ readonly wasWatching: boolean;
69
+ /** Set when the PR already ended, so no watch was registered. */
70
+ readonly state?: "merged" | "closed";
71
+ },
72
| PullRequestWatchFailedError
77
- | PullRequestNotOpenError
73
| PullRequestWatchFromSubagentError
74
| PullRequestWatchThreadNotFoundError
75
| PullRequestTargetError
@@ -123,13 +118,20 @@ const make = Effect.gen(function* () {
118
return yield* new PullRequestWatchFromSubagentError();
119
}
120
const before = watchedLink(thread);
126
- if (watching && before?.snapshot?.state === "merged") {
127
- return yield* new PullRequestNotOpenError({ state: "merged" });
128
- }
121
+ const wasWatching = before?.watch !== undefined;
122
+ // A watch on an ended PR could only report that it ended, so report it now instead.
123
+ const alreadyEnded = (state: "merged" | "closed") => ({
124
+ ...target,
125
+ watching: false,
126
+ wasWatching,
127
+ state,
128
+ });
129
+ // A closed snapshot still gets a read below, since the PR may have reopened.
130
+ if (watching && before?.snapshot?.state === "merged") return alreadyEnded("merged");
131
// A pending rollup has no failure yet, so any failure the first read finds is news.
132
let checksPendingAtStart: boolean | undefined;
131
- if (watching && before?.watch === undefined) {
132
- checksPendingAtStart = yield* pullRequests
133
+ if (watching && !wasWatching) {
134
+ const read = yield* pullRequests
135
.summary(
136
{
137
projectId: thread.projectId,
@@ -141,17 +143,14 @@ const make = Effect.gen(function* () {
143
{ recoverTransientFailure: false, bypassCache: true },
144
)
145
.pipe(
144
- Effect.flatMap((summary) =>
145
- summary.state === "merged"
146
- ? Effect.fail(new PullRequestNotOpenError({ state: "merged" }))
147
- : Effect.succeed(summary.checksState === "pending"),
148
- ),
146
Effect.catchTags({
147
// Host and transport errors can recover while the watch is active.
148
// A failed read leaves the seed to the link's last sync.
149
PullRequestOperationError: () => Effect.undefined,
150
}),
151
);
152
+ if (read?.state === "merged" || read?.state === "closed") return alreadyEnded(read.state);
153
+ checksPendingAtStart = read === undefined ? undefined : read.checksState === "pending";
154
}
155
const uuid = yield* random.uuidV4().pipe(Effect.orDie);
156
yield* engine
@@ -180,7 +179,7 @@ const make = Effect.gen(function* () {
179
return {
180
...target,
181
watching: watchedLink(after)?.watch !== undefined,
183
- wasWatching: before?.watch !== undefined,
182
+ wasWatching,
183
};
184
});
185