Skip to content

Commit 9e5229b

Browse files
t3dotggclaude
andauthored
fix(server): PR watches stop burning GitHub's rate limit and giving up (#16208)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent f33b060 commit 9e5229b

13 files changed

Lines changed: 577 additions & 147 deletions

‎apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,41 @@ describe("pull request toolkit handlers", () => {
279279
}),
280280
);
281281

282+
it.effect("watches a pull request saved as closed, since it may have reopened", () =>
283+
Effect.gen(function* () {
284+
const closed = makeLink(1, { headBranch: "closed" });
285+
const harness = yield* makeHarness({
286+
thread: makeThread([
287+
{ ...closed, snapshot: closed.snapshot && { ...closed.snapshot, state: "closed" } },
288+
]),
289+
});
290+
yield* harness.call("watch_pull_request", { repository: "t3tools/t3code", number: 1 });
291+
expect(yield* Ref.get(harness.commands)).toMatchObject([
292+
{ type: "thread.pull-request.watch", number: 1, watching: true },
293+
]);
294+
}),
295+
);
296+
297+
it.effect("refuses a watch from a subagent thread, whose parent owns the pull request", () =>
298+
Effect.gen(function* () {
299+
const harness = yield* makeHarness({
300+
thread: {
301+
...makeThread([makeLink(1, { headBranch: "feature" })]),
302+
lineage: {
303+
rootThreadId: ThreadId.make("parent"),
304+
parentThreadId: ThreadId.make("parent"),
305+
relationshipToParent: "subagent",
306+
},
307+
},
308+
});
309+
const error = yield* harness
310+
.call("watch_pull_request", { repository: "t3tools/t3code", number: 1 })
311+
.pipe(Effect.flip);
312+
expect(error).toMatchObject({ _tag: "PullRequestWatchFromSubagentError" });
313+
expect(yield* Ref.get(harness.commands)).toEqual([]);
314+
}),
315+
);
316+
282317
it.effect("links by repository and number, defaulting the host to the project's", () =>
283318
Effect.gen(function* () {
284319
const harness = yield* makeHarness();

‎apps/server/src/mcp/toolkits/pullRequests/handlers.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
PullRequestNotOpenError,
3939
type PullRequestTargetInput,
4040
PullRequestWatchFailedError,
41+
PullRequestWatchFromSubagentError,
4142
PullRequestThreadNotFoundError,
4243
PullRequestThreadAboveLimitsError,
4344
PullRequestThreadRequiredError,
@@ -273,10 +274,14 @@ const make = Effect.gen(function* () {
273274
threadPullRequestsOf(shell).find(
274275
(link) => link.source !== "stack-dismissed" && threadPullRequestKeysEqual(link, target),
275276
);
277+
if (watching && thread.lineage.relationshipToParent === "subagent") {
278+
return yield* new PullRequestWatchFromSubagentError();
279+
}
276280
const before = watchedLink(thread);
277-
const state = before?.snapshot?.state;
278-
if (watching && state !== undefined && state !== "open") {
279-
return yield* new PullRequestNotOpenError({ state });
281+
// A merged pull request cannot reopen. A closed one can, and its saved state may be stale,
282+
// so the watch starts and its first read ends it if the host still says closed.
283+
if (watching && before?.snapshot?.state === "merged") {
284+
return yield* new PullRequestNotOpenError({ state: "merged" });
280285
}
281286
yield* engine
282287
.dispatch({

‎apps/server/src/mcp/toolkits/pullRequests/tools.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ export class PullRequestNotOpenError extends Schema.TaggedError<PullRequestNotOp
150150
}
151151
}
152152

153+
export class PullRequestWatchFromSubagentError extends Schema.TaggedError<PullRequestWatchFromSubagentError>()(
154+
"PullRequestWatchFromSubagentError",
155+
{},
156+
) {
157+
override get message(): string {
158+
return "This thread is a subagent, so it cannot watch pull requests. Its parent thread owns the pull request: finish your task and report back instead.";
159+
}
160+
}
161+
153162
export class PullRequestListFailedError extends Schema.TaggedError<PullRequestListFailedError>()(
154163
"PullRequestListFailedError",
155164
{ cause: Schema.Defect() },
@@ -172,6 +181,7 @@ export const PullRequestToolError = Schema.Union([
172181
PullRequestListFailedError,
173182
PullRequestWatchFailedError,
174183
PullRequestNotOpenError,
184+
PullRequestWatchFromSubagentError,
175185
]);
176186
export type PullRequestToolError = typeof PullRequestToolError.Type;
177187

@@ -289,7 +299,7 @@ const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
289299

290300
const WatchPullRequestTool = Tool.make("watch_pull_request", {
291301
description:
292-
"Have T3 Code watch an open pull request for this thread, linking it first if needed. T3 Code checks it every minute and wakes you with a message when a check fails, the required checks pass, someone else comments or reviews, or the branch starts to conflict with its base. Use this to monitor or babysit a pull request instead of polling, sleeping, or running a watcher. Only comments posted after this call wake you, so handle the existing ones first, then end your turn. A wake is news, not a merge decision: check readiness yourself before merging. While T3 Code watches, the thread stays in the user's Working list, not their inbox. When you hand the work back to the user, call unwatch_pull_request first so the thread returns to their inbox. Watching ends when the pull request merges or closes, when its thread settles, when T3 Code cannot read it for 15 minutes, when the user stops this thread, or when you call unwatch_pull_request. Unsettle the thread before starting a new watch.",
302+
"Have T3 Code watch an open pull request for this thread, linking it first if needed. T3 Code checks it every two minutes and wakes you with a message when a check fails, the required checks pass, someone else comments or reviews, or the branch starts to conflict with its base. Use this to monitor or babysit a pull request instead of polling, sleeping, or running a watcher. Only comments posted after this call wake you, so handle the existing ones first, then end your turn. A wake is news, not a merge decision: check readiness yourself before merging. While T3 Code watches, the thread stays in the user's Working list, not their inbox. When you hand the work back to the user, call unwatch_pull_request first so the thread returns to their inbox. Watching ends when the pull request merges or closes, when its thread settles or is archived, when T3 Code fails to read it 8 times in a row (a host rate limit only delays it), when the user stops this thread, or when you call unwatch_pull_request. Unsettle the thread before starting a new watch. A subagent cannot watch: its parent thread owns the pull request.",
293303
parameters: PullRequestTargetInput,
294304
success: WatchPullRequestResult,
295305
failure: PullRequestToolError,

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,12 +2382,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23822382
if (
23832383
command.type === "thread.pull-request.watch" &&
23842384
command.watching &&
2385-
(thread.settledOverride === "settled" || thread.settledAt !== null)
2385+
(thread.settledOverride === "settled" ||
2386+
thread.settledAt !== null ||
2387+
thread.archivedAt !== null)
23862388
) {
23872389
return yield* new OrchestratorDispatchError({
23882390
commandId: command.commandId,
23892391
commandType: command.type,
2390-
cause: `Thread ${command.threadId} is settled and cannot watch pull requests.`,
2392+
cause: `Thread ${command.threadId} is settled or archived and cannot watch pull requests.`,
2393+
});
2394+
}
2395+
// A subagent or delegated task reports to its parent thread, which owns the pull request.
2396+
// Its own watch would wake it on every change of a PR it was not asked to babysit.
2397+
if (
2398+
command.type === "thread.pull-request.watch" &&
2399+
command.watching &&
2400+
thread.lineage.relationshipToParent === "subagent"
2401+
) {
2402+
return yield* new OrchestratorDispatchError({
2403+
commandId: command.commandId,
2404+
commandType: command.type,
2405+
cause: `Thread ${command.threadId} is a subagent; its parent thread watches pull requests.`,
23912406
});
23922407
}
23932408
// Only the agent's watch_pull_request links as "agent". A call that raced a Stop may land
@@ -2713,7 +2728,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
27132728
const updatedThread: OrchestrationV2AppThread = (() => {
27142729
switch (command.type) {
27152730
case "thread.archive":
2716-
return { ...thread, archivedAt: now, titleRegeneration: null, updatedAt: now };
2731+
// An archived thread takes no wakes, so its watches end like a settled thread's.
2732+
return {
2733+
...thread,
2734+
archivedAt: now,
2735+
titleRegeneration: null,
2736+
pullRequests: thread.pullRequests?.map((link) => withPullRequestWatch(link, undefined)),
2737+
updatedAt: now,
2738+
};
27172739
case "thread.unarchive":
27182740
return { ...thread, archivedAt: null, updatedAt: now };
27192741
case "thread.settle": {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export type ProjectionLimitRecoveryCandidate = Pick<
156156
/** The thread fields pull request sync reads, for a thread with at least one link. */
157157
export type ProjectionThreadPullRequests = Pick<
158158
OrchestrationV2AppThread,
159-
"id" | "projectId" | "settledOverride" | "settledAt" | "pullRequests"
159+
"id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pullRequests"
160160
>;
161161

162162
/**
@@ -5247,6 +5247,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52475247
Effect.map((thread): ProjectionThreadPullRequests => ({
52485248
id: thread.id,
52495249
projectId: thread.projectId,
5250+
lineage: thread.lineage,
52505251
settledOverride: thread.settledOverride,
52515252
settledAt: thread.settledAt,
52525253
pullRequests: thread.pullRequests ?? [],
@@ -5718,6 +5719,7 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
57185719
.map((thread): ProjectionThreadPullRequests => ({
57195720
id: thread.id,
57205721
projectId: thread.projectId,
5722+
lineage: thread.lineage,
57215723
settledOverride: thread.settledOverride,
57225724
settledAt: thread.settledAt,
57235725
pullRequests: thread.pullRequests ?? [],

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
235235
.map((thread) => ({
236236
id: thread.id,
237237
projectId: thread.projectId,
238+
lineage: {
239+
parentThreadId: null,
240+
relationshipToParent: null,
241+
rootThreadId: thread.id,
242+
},
238243
settledOverride: thread.settledOverride,
239244
settledAt: thread.settledAt === null ? null : DateTime.makeUnsafe(thread.settledAt),
240245
pullRequests: thread.pullRequests,

0 commit comments

Comments
 (0)