From 6404ffd07b91cc0201e181efd142e3bb4f3bf06d Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Fri, 20 Nov 2026 13:19:28 -0500 Subject: [PATCH] feat(server): let PR watches include same-account comments --- .../src/mcp/toolkits/pullRequests/handlers.ts | 13 +++++- .../src/mcp/toolkits/pullRequests/tools.ts | 6 +++ .../src/orchestration-v2/Orchestrator.ts | 44 ++++++++++++++----- .../PullRequestWatchReactor.ts | 6 ++- .../orchestration-v2/pullRequestWatch.test.ts | 11 ++++- .../src/orchestration-v2/pullRequestWatch.ts | 3 +- .../src/orchestration-v2/runtimeLayer.test.ts | 28 +++++++++--- .../src/provider/agentContextBudget.json | 2 +- .../pullRequest/PullRequestWatchService.ts | 6 +++ docs/user/source-control.md | 8 ++-- packages/contracts/src/orchestrationV2.ts | 1 + packages/contracts/src/threadPullRequest.ts | 1 + 12 files changed, 102 insertions(+), 27 deletions(-) diff --git a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts index 65eaf22975c68..a8a2b94e115a3 100644 --- a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts +++ b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts @@ -317,14 +317,23 @@ const make = Effect.gen(function* () { // The domain service owns the registration span. const setWatching = Effect.fnUntraced(function* ( - input: PullRequestTargetInput & { readonly until?: PullRequestWatchUntil | undefined }, + input: PullRequestTargetInput & { + readonly until?: PullRequestWatchUntil | undefined; + readonly includeOwnComments?: boolean | undefined; + }, watching: boolean, ) { const scope = yield* McpInvocationContext.requireMcpCapability("pull-requests"); const threadId = input.threadId ?? scope.thread?.threadId; if (threadId === undefined) return yield* new PullRequestThreadRequiredError(); return yield* watches - .setWatching({ threadId, target: input, watching, until: input.until }) + .setWatching({ + threadId, + target: input, + watching, + until: input.until, + includeOwnComments: input.includeOwnComments, + }) .pipe( Effect.catchTags({ PullRequestWatchThreadNotFoundError: (error) => diff --git a/apps/server/src/mcp/toolkits/pullRequests/tools.ts b/apps/server/src/mcp/toolkits/pullRequests/tools.ts index 0b5dad39da07a..a71f936eabedf 100644 --- a/apps/server/src/mcp/toolkits/pullRequests/tools.ts +++ b/apps/server/src/mcp/toolkits/pullRequests/tools.ts @@ -380,6 +380,12 @@ const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", { const WatchPullRequestInput = Schema.Struct({ ...PullRequestTargetInput.fields, + includeOwnComments: Schema.optional( + Schema.Boolean.annotate({ + description: + "Wake on comments and reviews from the authenticated account too, including ones you post. Defaults to false for a new watch. Omit to keep the active watch's setting.", + }), + ), until: Schema.optional( PullRequestWatchUntil.annotate({ description: diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 5f57b8960d576..1c818b9000e41 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -896,8 +896,8 @@ function syncedPullRequestWatch( }, ): ThreadPullRequestWatch | undefined { if (command.watch === null) return undefined; - // Only the agent sets `until`, so a read in flight keeps what the watch has now. - const watch = withUntil(command.watch, existing?.until ?? "checks"); + // A read in flight keeps the current hold duration. + const watch = withPullRequestWatchOptions(command.watch, { until: existing?.until ?? "checks" }); // A push cleared the answer while this read was in flight, so its answer is the old head's. if (command.checksPendingBefore === undefined || existing?.checksPending !== undefined) { return watch; @@ -905,14 +905,27 @@ function syncedPullRequestWatch( return withoutChecksAnswer(watch); } -function withUntil( +function withPullRequestWatchOptions( watch: ThreadPullRequestWatch, - until: PullRequestWatchUntil | undefined, + options: { + readonly until?: PullRequestWatchUntil | undefined; + readonly includeOwnComments?: boolean | undefined; + }, ): ThreadPullRequestWatch { - if (until === undefined || until === (watch.until ?? "checks")) return watch; - if (until === "merged") return { ...watch, until }; - const { until: _checks, ...rest } = watch; - return rest; + const until = options.until ?? watch.until ?? "checks"; + const includeOwnComments = options.includeOwnComments ?? watch.includeOwnComments ?? false; + if ( + until === (watch.until ?? "checks") && + includeOwnComments === Boolean(watch.includeOwnComments) + ) { + return watch; + } + const { until: _until, includeOwnComments: _includeOwnComments, ...state } = watch; + return { + ...state, + ...(until === "merged" && { until }), + ...(includeOwnComments && { includeOwnComments: true as const }), + }; } const withoutChecksAnswer = ({ @@ -4176,7 +4189,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio pullRequestSnapshotEndsWatch(link?.snapshot ?? null, entry.startedAt), ); if ( - entries.some(({ entry, link }) => link?.watch?.startedAt !== entry.startedAt) || + entries.some(({ entry, link }) => { + const evaluated = entry.watch ?? entry.endedWatch; + return ( + link?.watch?.startedAt !== entry.startedAt || + (evaluated !== undefined && + evaluated.includeOwnComments !== link.watch.includeOwnComments) + ); + }) || (wake && inactive) || // One wake cannot drop the news of just some of its pull requests; the sender splits it. (entries.length > 1 && outdated.some(Boolean)) @@ -4184,7 +4204,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio return yield* new OrchestratorDispatchError({ commandId: command.commandId, commandType: command.type, - cause: "The pull request watch ended or its thread settled while it was read.", + cause: "The pull request watch changed or its thread settled while it was read.", }); } for (const { entry } of entries) { @@ -5302,7 +5322,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio : existing.watch : !command.watching ? undefined - : withUntil( + : withPullRequestWatchOptions( existing.watch ?? { startedAt, headSha: null, @@ -5346,7 +5366,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio remarkIds: [], wakes: 0, }, - command.until, + command, ); if (watch === existing.watch && links === linked) return thread; const final = diff --git a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts index 7590ee3c11b3e..4189cc785c82c 100644 --- a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts +++ b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts @@ -125,7 +125,8 @@ interface HeldWake { readonly reason?: WatchEndReason; } -const watchKey = ({ thread, watch }: WatchTarget) => `${thread.id} ${watch.startedAt}`; +const watchKey = ({ thread, watch }: WatchTarget) => + `${thread.id} ${watch.startedAt} ${watch.includeOwnComments === true}`; type WatchEndReason = | "merged" @@ -230,7 +231,8 @@ function watchesEqual(left: ThreadPullRequestWatch, right: ThreadPullRequestWatc left.awaitingBaseline === right.awaitingBaseline && left.checksAwaitedSince === right.checksAwaitedSince && left.checksPending === right.checksPending && - left.until === right.until + left.until === right.until && + left.includeOwnComments === right.includeOwnComments ); } diff --git a/apps/server/src/orchestration-v2/pullRequestWatch.test.ts b/apps/server/src/orchestration-v2/pullRequestWatch.test.ts index d81d2348c5d21..75063f308660c 100644 --- a/apps/server/src/orchestration-v2/pullRequestWatch.test.ts +++ b/apps/server/src/orchestration-v2/pullRequestWatch.test.ts @@ -901,7 +901,7 @@ describe("evaluatePullRequestWatch", () => { assert.deepEqual(evaluatePullRequestWatch(watch(), noViewer, [reply]).changes, []); }); - it("reports remarks from others once and never the agent's own", () => { + it("reports fresh remarks once and includes the authenticated account only when requested", () => { const comments = [ remark("agent-user", "2026-10-02T12:05:00Z", "Fixed in the latest push."), remark("macroscope-app[bot]", "2026-10-02T12:06:00Z"), @@ -911,6 +911,15 @@ describe("evaluatePullRequestWatch", () => { assert.deepEqual(report.changes, [{ kind: "remarks", remarks: [comments[1]!] }]); assert.equal(report.next.remarksThrough, "2026-10-02T12:06:00Z"); assert.deepEqual(evaluatePullRequestWatch(report.next, detail(), comments).changes, []); + + const optedIn = evaluatePullRequestWatch( + watch({ includeOwnComments: true }), + detail(), + comments, + ); + assert.deepEqual(optedIn.changes, [{ kind: "remarks", remarks: comments.slice(0, 2) }]); + assert.equal(optedIn.next.includeOwnComments, true); + assert.deepEqual(evaluatePullRequestWatch(optedIn.next, detail(), comments).changes, []); }); it("cuts a long remark in the wake message without splitting an emoji", () => { diff --git a/apps/server/src/orchestration-v2/pullRequestWatch.ts b/apps/server/src/orchestration-v2/pullRequestWatch.ts index 5999996ec5dda..73e37b5568bc6 100644 --- a/apps/server/src/orchestration-v2/pullRequestWatch.ts +++ b/apps/server/src/orchestration-v2/pullRequestWatch.ts @@ -216,7 +216,7 @@ export function evaluatePullRequestWatch( const at = Date.parse(activeAt(remark)); return ( (at > through || (at === through && !watch.remarkIds.includes(remark.id))) && - remark.author?.login.toLowerCase() !== own + (watch.includeOwnComments === true || remark.author?.login.toLowerCase() !== own) ); }); if (fresh.length > 0) changes.push({ kind: "remarks", remarks: fresh }); @@ -287,6 +287,7 @@ export function evaluatePullRequestWatch( checksAwaitedSince, checksPending, ...(watch.until !== undefined && { until: watch.until }), + ...(watch.includeOwnComments === true && { includeOwnComments: true }), }, exhausted: commentsOnly && wakes >= PULL_REQUEST_WATCH_WAKE_LIMIT, }; diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 6543193172258..913adf89c1277 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -4309,7 +4309,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { }), ); - it.effect("keeps how long a watch holds across reads until the agent changes it", () => + it.effect("keeps watch options across reads until the agent changes them", () => Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; const maintenance = yield* ProjectionReplay.ProjectionReplay; @@ -4333,7 +4333,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { orchestrator.getThreadShell(threadId), (thread) => thread?.pullRequests?.[0]?.watch, ); - const watch = (id: string, until?: PullRequestWatchUntil) => + const watch = (id: string, until?: PullRequestWatchUntil, includeOwnComments?: boolean) => orchestrator.dispatch({ type: "thread.pull-request.watch", commandId: CommandId.make(id), @@ -4342,16 +4342,32 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { watching: true, link: { url: "https://github.com/pingdotgg/t3code/pull/7", source: "agent" }, ...(until !== undefined && { until }), + ...(includeOwnComments !== undefined && { includeOwnComments }), }); yield* watch("pr-watch-until-start"); - assert.isUndefined((yield* watchOf)?.until); + const initial = yield* watchOf; + assert.isUndefined(initial?.until); + assert.isUndefined(initial?.includeOwnComments); + if (initial === undefined) return; - yield* watch("pr-watch-until-on", "merged"); + yield* watch("pr-watch-until-on", "merged", true); const started = yield* watchOf; assert.equal(started?.until, "merged"); + assert.equal(started?.includeOwnComments, true); if (started === undefined) return; + yield* orchestrator + .dispatch({ + type: "thread.pull-request-watch.sync", + commandId: CommandId.make("pr-watch-old-comment-filter"), + threadId, + ...key, + startedAt: initial.startedAt, + watch: initial, + }) + .pipe(Effect.flip); + // A read that began before the agent set it must not drop it. const { until: _read, ...readBefore } = started; yield* orchestrator.dispatch({ @@ -4369,9 +4385,11 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { assert.equal((yield* watchOf)?.until, "merged"); assert.isTrue((yield* maintenance.rebuild).valid); assert.equal((yield* watchOf)?.until, "merged"); + assert.equal((yield* watchOf)?.includeOwnComments, true); - yield* watch("pr-watch-until-off", "checks"); + yield* watch("pr-watch-until-off", "checks", false); assert.isUndefined((yield* watchOf)?.until); + assert.isUndefined((yield* watchOf)?.includeOwnComments); assert.equal((yield* watchOf)?.headSha, "abc123"); }), ); diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 028cd6cdf1fdf..272eaafd7155c 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 6245, "codexAdditionalContextBytes": 7639, - "mcpToolListJsonBytes": 57751 + "mcpToolListJsonBytes": 57973 } diff --git a/apps/server/src/pullRequest/PullRequestWatchService.ts b/apps/server/src/pullRequest/PullRequestWatchService.ts index b38075baeb7bd..43c8a5f37616e 100644 --- a/apps/server/src/pullRequest/PullRequestWatchService.ts +++ b/apps/server/src/pullRequest/PullRequestWatchService.ts @@ -71,6 +71,7 @@ export class PullRequestWatchService extends Context.Service< readonly target: PullRequestTarget; readonly watching: boolean; readonly until?: PullRequestWatchUntil | undefined; + readonly includeOwnComments?: boolean | undefined; }) => Effect.Effect< ResolvedTarget & { readonly watching: boolean; readonly wasWatching: boolean }, | PullRequestWatchFailedError @@ -107,6 +108,7 @@ const make = Effect.gen(function* () { readonly target: PullRequestTarget; readonly watching: boolean; readonly until?: PullRequestWatchUntil | undefined; + readonly includeOwnComments?: boolean | undefined; }) { const { watching } = input; const thread = yield* requireThread(input.threadId); @@ -165,6 +167,10 @@ const make = Effect.gen(function* () { watching, ...(watching ? { link: { url: target.url, source: "agent" as const } } : {}), ...(watching && input.until !== undefined && { until: input.until }), + ...(watching && + input.includeOwnComments !== undefined && { + includeOwnComments: input.includeOwnComments, + }), ...(watching && checksPendingAtStart !== undefined ? { checksPending: checksPendingAtStart } : {}), diff --git a/docs/user/source-control.md b/docs/user/source-control.md index 1bd54659eb5af..e66fd40aa1e13 100644 --- a/docs/user/source-control.md +++ b/docs/user/source-control.md @@ -342,9 +342,11 @@ check fails or waits for a maintainer's approval, the required checks pass, the conflict. On github.com it also wakes the agent, with GitHub's reason, when the merge queue drops the pull request without merging it. Threads in a project that watch the same pull request share one check. On GitHub, a check first asks whether anything changed and reads the pull request only when it did, which keeps -watching inside GitHub's rate limit. Comments from your own account do not wake it. When the pull -request merges or closes, watching ends, and the agent is woken once unless it merged or closed the -pull request itself with the host's CLI. A reopened pull request needs a new watch. Watching also ends after 10 wakes in a row that bring only comments, after 8 +watching inside GitHub's rate limit. To wake the agent on comments from your own account, ask it to +enable `includeOwnComments` for the watch. This also includes replies the agent posts through your +account. When the pull request merges or closes, watching ends, and the agent is woken once unless +it merged or closed the pull request itself with the host's CLI. A reopened pull request needs a new +watch. Watching also ends after 10 wakes in a row that bring only comments, after 8 failed reads in a row, or when you press Stop on the thread. A rate limit only pauses watching. Settling or archiving a thread also ends all its watches. Unsettle the thread before starting a new watch. Watching a pull request again does not repeat check or conflict news its last diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts index bcd262989fa84..378903264e4ee 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -3369,6 +3369,7 @@ export const OrchestrationV2Command = Schema.Union([ watching: Schema.Boolean, /** With `watching`, sets how long the watch holds its thread; absent keeps the current one. */ until: Schema.optional(PullRequestWatchUntil), + includeOwnComments: Schema.optional(Schema.Boolean), /** Whether CI was pending with no failure yet when the watch started, so any failure is news. */ checksPending: Schema.optional(Schema.Boolean), link: Schema.optional( diff --git a/packages/contracts/src/threadPullRequest.ts b/packages/contracts/src/threadPullRequest.ts index 67ffe72a41de8..a8576483644f0 100644 --- a/packages/contracts/src/threadPullRequest.ts +++ b/packages/contracts/src/threadPullRequest.ts @@ -123,6 +123,7 @@ export const ThreadPullRequestWatch = Schema.Struct({ checksPending: Schema.optional(Schema.Boolean), /** How long the watch holds its thread. Absent means until every check finished. */ until: Schema.optional(Schema.Literal("merged")), + includeOwnComments: Schema.optional(Schema.Literal(true)), }); export type ThreadPullRequestWatch = typeof ThreadPullRequestWatch.Type;