Skip to content

Commit 18b2132

Browse files
t3dotggclaude
andauthored
feat(server): agents can watch a PR and get woken when checks, reviews, or conflicts need them (pingdotgg#15057)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e8545b2 commit 18b2132

31 files changed

Lines changed: 1531 additions & 39 deletions

‎apps/mobile/src/features/threads/git/GitOverviewSheet.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ export function GitOverviewSheet(props: GitOverviewSheetProps) {
339339
<SheetListRow
340340
icon="arrow.triangle.pull"
341341
title={`#${link.number} ${link.snapshot?.title ?? "Pull request"}`}
342-
subtitle={`${link.repository} · ${link.snapshot === null ? "Status pending" : link.snapshot.isDraft && link.snapshot.state === "open" ? "Draft" : link.snapshot.state}`}
342+
subtitle={`${link.repository} · ${link.snapshot === null ? "Status pending" : link.snapshot.isDraft && link.snapshot.state === "open" ? "Draft" : link.snapshot.state}${link.watch === undefined ? "" : " · Watching"}`}
343343
onPress={() => {
344344
void tryOpenExternalUrl(link.url, "pull-request").then((opened) => {
345345
if (!opened)

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,8 @@ function toolGroupSummarySymbolName(kind: ToolGroupSummaryKind): AppSymbolName {
13681368
case "link-pr":
13691369
case "unlink-pr":
13701370
case "list-prs":
1371+
case "watch-pr":
1372+
case "unwatch-pr":
13711373
return "arrow.triangle.pull";
13721374
case "read":
13731375
return { ios: "eye", android: "visibility" };

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export const make = Effect.gen(function* () {
240240
threadTitleRegeneration: true,
241241
threadVisitedTracking: true,
242242
threadPullRequests: true,
243+
threadPullRequestWatch: true,
243244
pullRequestStackActions: true,
244245
threadPullRequestLinking: true,
245246
serverResolvedCommandContext: true,

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,58 @@ describe("pull request toolkit handlers", () => {
222222
}),
223223
);
224224

225+
it.effect("watching an unlinked pull request links it first", () =>
226+
Effect.gen(function* () {
227+
const harness = yield* makeHarness();
228+
const result = yield* harness.call("watch_pull_request", {
229+
url: "https://github.com/t3tools/t3code/pull/9",
230+
});
231+
// The harness thread never changes, so the result reports what it still holds.
232+
expect(result).toMatchObject({ number: 9, watching: false, wasWatching: false });
233+
expect(yield* Ref.get(harness.commands)).toMatchObject([
234+
{
235+
type: "thread.pull-request.watch",
236+
number: 9,
237+
watching: true,
238+
link: { url: "https://github.com/t3tools/t3code/pull/9", source: "agent" },
239+
},
240+
]);
241+
}),
242+
);
243+
244+
it.effect("refuses to watch a merged pull request and stops an existing watch", () =>
245+
Effect.gen(function* () {
246+
const watch = {
247+
startedAt: "2026-08-20T00:00:00.000Z",
248+
headSha: null,
249+
failedChecks: [],
250+
passed: false,
251+
remarksThrough: "2026-08-20T00:00:00.000Z",
252+
remarkIds: [],
253+
conflicting: false,
254+
wakes: 0,
255+
};
256+
const merged = makeLink(1, { headBranch: "done" });
257+
const harness = yield* makeHarness({
258+
thread: makeThread([
259+
{ ...merged, snapshot: merged.snapshot && { ...merged.snapshot, state: "merged" } },
260+
makeLink(2, { headBranch: "idle" }),
261+
makeLink(3, { headBranch: "watched", watch }),
262+
]),
263+
});
264+
const error = yield* harness
265+
.call("watch_pull_request", { repository: "t3tools/t3code", number: 1 })
266+
.pipe(Effect.flip);
267+
expect(error).toMatchObject({ _tag: "PullRequestNotOpenError", state: "merged" });
268+
expect(
269+
yield* harness.call("unwatch_pull_request", { repository: "t3tools/t3code", number: 3 }),
270+
).toMatchObject({ wasWatching: true });
271+
expect(yield* Ref.get(harness.commands)).toMatchObject([
272+
{ type: "thread.pull-request.watch", number: 3, watching: false },
273+
]);
274+
}),
275+
);
276+
225277
it.effect("links by repository and number, defaulting the host to the project's", () =>
226278
Effect.gen(function* () {
227279
const harness = yield* makeHarness();
@@ -396,6 +448,7 @@ describe("pull request toolkit handlers", () => {
396448
number: 3,
397449
url: "https://github.com/t3tools/t3code/pull/3",
398450
source: "agent",
451+
watching: false,
399452
state: "open",
400453
title: "PR 3",
401454
headBranch: "feat-c",

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

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ import {
3434
PullRequestHostRequiredError,
3535
PullRequestUnlinkFailedError,
3636
PullRequestListFailedError,
37+
PullRequestNotOpenError,
3738
type PullRequestTargetInput,
39+
PullRequestWatchFailedError,
3840
PullRequestThreadNotFoundError,
3941
PullRequestsToolkit,
4042
type ThreadPullRequestEntry,
@@ -122,6 +124,7 @@ function entryOf(
122124
number: link.number,
123125
url: link.url,
124126
source: link.source,
127+
watching: link.watch !== undefined,
125128
state: link.snapshot?.state ?? null,
126129
title: link.snapshot?.title ?? null,
127130
headBranch: link.snapshot?.headBranch ?? null,
@@ -163,7 +166,8 @@ const make = Effect.gen(function* () {
163166
Failure:
164167
| typeof PullRequestLinkFailedError
165168
| typeof PullRequestUnlinkFailedError
166-
| typeof PullRequestListFailedError,
169+
| typeof PullRequestListFailedError
170+
| typeof PullRequestWatchFailedError,
167171
) {
168172
const scope = yield* McpInvocationContext.requireMcpCapability("pull-requests");
169173
const thread = yield* engine
@@ -178,22 +182,76 @@ const make = Effect.gen(function* () {
178182

179183
const projectOf = (
180184
thread: OrchestrationV2ThreadShell,
181-
Failure: typeof PullRequestLinkFailedError | typeof PullRequestUnlinkFailedError,
185+
Failure:
186+
| typeof PullRequestLinkFailedError
187+
| typeof PullRequestUnlinkFailedError
188+
| typeof PullRequestWatchFailedError,
182189
) =>
183190
projects.getShell(thread.projectId).pipe(
184191
Effect.map(Option.getOrUndefined),
185192
Effect.mapError((cause) => new Failure({ cause })),
186193
);
187194

188195
const dispatchFailure =
189-
(Failure: typeof PullRequestLinkFailedError | typeof PullRequestUnlinkFailedError) =>
196+
(
197+
Failure:
198+
| typeof PullRequestLinkFailedError
199+
| typeof PullRequestUnlinkFailedError
200+
| typeof PullRequestWatchFailedError,
201+
) =>
190202
<E>(
191203
cause: Cause.Cause<E>,
192-
): Effect.Effect<never, PullRequestLinkFailedError | PullRequestUnlinkFailedError> =>
204+
): Effect.Effect<
205+
never,
206+
PullRequestLinkFailedError | PullRequestUnlinkFailedError | PullRequestWatchFailedError
207+
> =>
193208
Cause.hasInterruptsOnly(cause)
194209
? Effect.failCause(cause as Cause.Cause<never>)
195210
: Effect.fail(new Failure({ cause }));
196211

212+
/**
213+
* Starts or stops a watch. One command links an unlinked pull request and watches it, and the
214+
* result reports the state the thread holds afterwards.
215+
*/
216+
const setWatching = Effect.fn("PullRequestsToolkit.setWatching")(function* (
217+
input: PullRequestTargetInput,
218+
watching: boolean,
219+
) {
220+
const thread = yield* requireThread(PullRequestWatchFailedError);
221+
const project = yield* projectOf(thread, PullRequestWatchFailedError);
222+
const target = yield* resolveTarget(input, project);
223+
const watchedLink = (shell: OrchestrationV2ThreadShell) =>
224+
threadPullRequestsOf(shell).find(
225+
(link) => link.source !== "stack-dismissed" && threadPullRequestKeysEqual(link, target),
226+
);
227+
const before = watchedLink(thread);
228+
const state = before?.snapshot?.state;
229+
if (watching && state !== undefined && state !== "open") {
230+
return yield* new PullRequestNotOpenError({ state });
231+
}
232+
yield* engine
233+
.dispatch({
234+
type: "thread.pull-request.watch",
235+
commandId: yield* commandId("mcp-pr-watch", thread.id),
236+
threadId: thread.id,
237+
host: target.host,
238+
repository: target.repository,
239+
number: target.number,
240+
watching,
241+
...(watching ? { link: { url: target.url, source: "agent" as const } } : {}),
242+
})
243+
.pipe(Effect.catchCause(dispatchFailure(PullRequestWatchFailedError)));
244+
const after = yield* requireThread(PullRequestWatchFailedError);
245+
return {
246+
host: target.host,
247+
repository: target.repository,
248+
number: target.number,
249+
url: target.url,
250+
watching: watchedLink(after)?.watch !== undefined,
251+
wasWatching: before?.watch !== undefined,
252+
};
253+
});
254+
197255
return PullRequestsToolkit.of({
198256
link_pull_request: (input) =>
199257
Effect.gen(function* () {
@@ -260,6 +318,8 @@ const make = Effect.gen(function* () {
260318
}),
261319
list_thread_pull_requests: () =>
262320
requireThread(PullRequestListFailedError).pipe(Effect.map(listThreadPullRequests)),
321+
watch_pull_request: (input) => setWatching(input, true),
322+
unwatch_pull_request: (input) => setWatching(input, false),
263323
});
264324
});
265325

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ export class PullRequestUnlinkFailedError extends Schema.TaggedError<PullRequest
108108
}
109109
}
110110

111+
export class PullRequestWatchFailedError extends Schema.TaggedError<PullRequestWatchFailedError>()(
112+
"PullRequestWatchFailedError",
113+
{ cause: Schema.Defect() },
114+
) {
115+
override get message(): string {
116+
return "Could not change whether the pull request is watched.";
117+
}
118+
}
119+
120+
export class PullRequestNotOpenError extends Schema.TaggedError<PullRequestNotOpenError>()(
121+
"PullRequestNotOpenError",
122+
{ state: Schema.String },
123+
) {
124+
override get message(): string {
125+
return `The pull request is ${this.state}, so there is nothing to watch.`;
126+
}
127+
}
128+
111129
export class PullRequestListFailedError extends Schema.TaggedError<PullRequestListFailedError>()(
112130
"PullRequestListFailedError",
113131
{ cause: Schema.Defect() },
@@ -126,6 +144,8 @@ export const PullRequestToolError = Schema.Union([
126144
PullRequestLinkFailedError,
127145
PullRequestUnlinkFailedError,
128146
PullRequestListFailedError,
147+
PullRequestWatchFailedError,
148+
PullRequestNotOpenError,
129149
]);
130150
export type PullRequestToolError = typeof PullRequestToolError.Type;
131151

@@ -154,9 +174,21 @@ export const UnlinkPullRequestResult = Schema.Struct({
154174
});
155175
export type UnlinkPullRequestResult = typeof UnlinkPullRequestResult.Type;
156176

177+
export const WatchPullRequestResult = Schema.Struct({
178+
...PullRequestIdentity,
179+
watching: Schema.Boolean.annotate({
180+
description: "Whether T3 Code now watches the pull request for this thread.",
181+
}),
182+
wasWatching: Schema.Boolean.annotate({
183+
description: "Whether it was already watched before the call.",
184+
}),
185+
});
186+
export type WatchPullRequestResult = typeof WatchPullRequestResult.Type;
187+
157188
export const ThreadPullRequestEntry = Schema.Struct({
158189
...PullRequestIdentity,
159190
source: ThreadPullRequestLinkSource,
191+
watching: Schema.Boolean,
160192
state: Schema.NullOr(PullRequestState),
161193
title: Schema.NullOr(Schema.String),
162194
headBranch: Schema.NullOr(Schema.String),
@@ -224,8 +256,38 @@ const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
224256
.annotate(Tool.Idempotent, true)
225257
.annotate(Tool.OpenWorld, false);
226258

259+
const WatchPullRequestTool = Tool.make("watch_pull_request", {
260+
description:
261+
"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. Watching ends when the pull request merges or closes, when T3 Code cannot read it for 15 minutes, or when you call unwatch_pull_request.",
262+
parameters: PullRequestTargetInput,
263+
success: WatchPullRequestResult,
264+
failure: PullRequestToolError,
265+
dependencies,
266+
})
267+
.annotate(Tool.Title, "Watch pull request")
268+
.annotate(Tool.Readonly, false)
269+
.annotate(Tool.Destructive, false)
270+
.annotate(Tool.Idempotent, true)
271+
.annotate(Tool.OpenWorld, false);
272+
273+
const UnwatchPullRequestTool = Tool.make("unwatch_pull_request", {
274+
description:
275+
"Stop T3 Code from watching a pull request for this thread. The pull request stays linked. Pass the URL, or repository plus number.",
276+
parameters: PullRequestTargetInput,
277+
success: WatchPullRequestResult,
278+
failure: PullRequestToolError,
279+
dependencies,
280+
})
281+
.annotate(Tool.Title, "Stop watching pull request")
282+
.annotate(Tool.Readonly, false)
283+
.annotate(Tool.Destructive, false)
284+
.annotate(Tool.Idempotent, true)
285+
.annotate(Tool.OpenWorld, false);
286+
227287
export const PullRequestsToolkit = Toolkit.make(
228288
LinkPullRequestTool,
229289
UnlinkPullRequestTool,
230290
ListThreadPullRequestsTool,
291+
WatchPullRequestTool,
292+
UnwatchPullRequestTool,
231293
);

0 commit comments

Comments
 (0)