Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions apps/server/src/mcp/toolkits/pullRequests/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CommandId,
type OrchestrationProjectShell,
type PullRequestState,
type PullRequestWatchUntil,
type OrchestrationV2ThreadShell,
type ThreadId,
type ThreadPullRequestLink,
Expand Down Expand Up @@ -199,18 +200,20 @@ const make = Effect.gen(function* () {

// The domain service owns the registration span.
const setWatching = Effect.fnUntraced(function* (
input: PullRequestTargetInput,
input: PullRequestTargetInput & { readonly until?: PullRequestWatchUntil | 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 }).pipe(
Effect.catchTags({
PullRequestWatchThreadNotFoundError: (error) =>
threadNotFound(engine, scope, error.threadId, error.message),
}),
);
return yield* watches
.setWatching({ threadId, target: input, watching, until: input.until })
.pipe(
Effect.catchTags({
PullRequestWatchThreadNotFoundError: (error) =>
threadNotFound(engine, scope, error.threadId, error.message),
}),
);
});

const locateAll = Effect.fn("PullRequestsToolkit.locateAll")(function* (
Expand Down
13 changes: 12 additions & 1 deletion apps/server/src/mcp/toolkits/pullRequests/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PullRequestReviewGuideRun,
PullRequestState,
PullRequestUnavailableError,
PullRequestWatchUntil,
ThreadPullRequestLinkSource,
TrimmedNonEmptyString,
ThreadId,
Expand Down Expand Up @@ -324,10 +325,20 @@ const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
.annotate(Tool.Idempotent, true)
.annotate(Tool.OpenWorld, false);

const WatchPullRequestInput = Schema.Struct({
...PullRequestTargetInput.fields,
until: Schema.optional(
PullRequestWatchUntil.annotate({
description:
'How long this thread stays working. Omit it to keep the current value; a new watch starts at "checks". "checks": until checks finish, for a PR you hand to the user. "merged": until it merges or closes or you unwatch it, when you will act on it yourself, such as merging after someone else approves.',
}),
),
});

const WatchPullRequestTool = Tool.make("watch_pull_request", {
description:
"Watch an open PR for this thread, linking it first if needed. T3 wakes you on failed checks, passed required checks, new comments or reviews, or conflicts. Handle old comments first. Only later ones wake you. After this call, end your turn. The watch stays on after each wake, so do not call this again on a wake. A wake is not a merge decision. Check readiness yourself.",
parameters: PullRequestTargetInput,
parameters: WatchPullRequestInput,
success: WatchPullRequestResult,
failure: PullRequestToolError,
dependencies: watchDependencies,
Expand Down
94 changes: 55 additions & 39 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
type OrchestrationV2ServerCommand,
type ThreadPullRequestLink,
type ThreadPullRequestWatch,
type PullRequestWatchUntil,
type OrchestrationV2AppThread,
type OrchestrationV2ContextHandoff,
type OrchestrationV2ContextSourcePoint,
Expand Down Expand Up @@ -892,11 +893,23 @@ 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 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 command.watch;
return watch;
}
return withoutChecksAnswer(command.watch);
return withoutChecksAnswer(watch);
}

function withUntil(
watch: ThreadPullRequestWatch,
until: PullRequestWatchUntil | 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 withoutChecksAnswer = ({
Expand Down Expand Up @@ -5077,43 +5090,46 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
: existing.watch
: !command.watching
? undefined
: (existing.watch ?? {
startedAt,
headSha: null,
failedChecks: [],
passed: false,
passedChecks: [],
conflicting: false,
// Watching again must not repeat check or conflict news the last watch told.
...(existing.endedWatch && {
headSha: existing.endedWatch.headSha,
failedChecks: existing.endedWatch.failedChecks,
passed: existing.endedWatch.passed,
passedChecks: existing.endedWatch.passedChecks,
conflicting: existing.endedWatch.conflicting,
rebaseRequired: existing.endedWatch.rebaseRequired,
}),
awaitingBaseline: existing.endedWatch
? existing.endedWatch.awaitingBaseline
: "all",
...(existing.endedWatch?.owedChecks !== undefined && {
owedChecks: existing.endedWatch.owedChecks,
}),
...(existing.endedWatch?.awaitingApproval !== undefined && {
awaitingApproval: existing.endedWatch.awaitingApproval,
}),
...(existing.endedWatch?.approvalAnnounced !== undefined && {
approvalAnnounced: existing.endedWatch.approvalAnnounced,
}),
// A finished-checks answer may be an older head's; a fresh read decides.
...(existing.endedWatch?.checksPending === true && { checksPending: true }),
...(existing.endedWatch?.checksAwaitedSince !== undefined && {
checksAwaitedSince: existing.endedWatch.checksAwaitedSince,
}),
remarksThrough: startedAt,
remarkIds: [],
wakes: 0,
});
: withUntil(
existing.watch ?? {
startedAt,
headSha: null,
failedChecks: [],
passed: false,
passedChecks: [],
conflicting: false,
// Watching again must not repeat check or conflict news the last watch told.
...(existing.endedWatch && {
headSha: existing.endedWatch.headSha,
failedChecks: existing.endedWatch.failedChecks,
passed: existing.endedWatch.passed,
passedChecks: existing.endedWatch.passedChecks,
conflicting: existing.endedWatch.conflicting,
rebaseRequired: existing.endedWatch.rebaseRequired,
}),
awaitingBaseline: existing.endedWatch
? existing.endedWatch.awaitingBaseline
: "all",
...(existing.endedWatch?.owedChecks !== undefined && {
owedChecks: existing.endedWatch.owedChecks,
}),
...(existing.endedWatch?.awaitingApproval !== undefined && {
awaitingApproval: existing.endedWatch.awaitingApproval,
}),
...(existing.endedWatch?.approvalAnnounced !== undefined && {
approvalAnnounced: existing.endedWatch.approvalAnnounced,
}),
// A finished-checks answer may be an older head's; a fresh read decides.
...(existing.endedWatch?.checksPending === true && { checksPending: true }),
...(existing.endedWatch?.checksAwaitedSince !== undefined && {
checksAwaitedSince: existing.endedWatch.checksAwaitedSince,
}),
remarksThrough: startedAt,
remarkIds: [],
wakes: 0,
},
command.until,
);
if (watch === existing.watch && links === linked) return thread;
const final =
command.type === "thread.pull-request-watch.sync" && watch === undefined
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ function watchesEqual(left: ThreadPullRequestWatch, right: ThreadPullRequestWatc
left.wakes === right.wakes &&
left.awaitingBaseline === right.awaitingBaseline &&
left.checksAwaitedSince === right.checksAwaitedSince &&
left.checksPending === right.checksPending
left.checksPending === right.checksPending &&
left.until === right.until
);
}

Expand Down
1 change: 1 addition & 0 deletions apps/server/src/orchestration-v2/pullRequestWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export function evaluatePullRequestWatch(
}),
checksAwaitedSince,
checksPending,
...(watch.until !== undefined && { until: watch.until }),
},
exhausted: commentsOnly && wakes >= PULL_REQUEST_WATCH_WAKE_LIMIT,
};
Expand Down
68 changes: 68 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
type PullRequestDetail,
type PullRequestComment,
PullRequestOperationError,
type PullRequestWatchUntil,
ProviderDriverKind,
ProviderInstanceId,
ProviderSessionId,
Expand Down Expand Up @@ -4280,6 +4281,73 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
}),
);

it.effect("keeps how long a watch holds across reads until the agent changes it", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const maintenance = yield* ProjectionReplay.ProjectionReplay;
const threadId = ThreadId.make("runtime-pull-request-watch-until-merged");
yield* orchestrator.dispatch({
type: "thread.create",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make("pr-watch-until-create"),
threadId,
projectId: ProjectId.make("pr-watch-project"),
title: "Watch until merged",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
});
const key = { host: "github.com", repository: "pingdotgg/t3code", number: 7 };
const watchOf = Effect.map(
orchestrator.getThreadShell(threadId),
(thread) => thread?.pullRequests?.[0]?.watch,
);
const watch = (id: string, until?: PullRequestWatchUntil) =>
orchestrator.dispatch({
type: "thread.pull-request.watch",
commandId: CommandId.make(id),
threadId,
...key,
watching: true,
link: { url: "https://github.com/pingdotgg/t3code/pull/7", source: "agent" },
...(until !== undefined && { until }),
});

yield* watch("pr-watch-until-start");
assert.isUndefined((yield* watchOf)?.until);

yield* watch("pr-watch-until-on", "merged");
const started = yield* watchOf;
assert.equal(started?.until, "merged");
if (started === undefined) return;

// A read that began before the agent set it must not drop it.
const { until: _read, ...readBefore } = started;
yield* orchestrator.dispatch({
type: "thread.pull-request-watch.sync",
commandId: CommandId.make("pr-watch-until-record"),
threadId,
...key,
startedAt: started.startedAt,
watch: { ...readBefore, headSha: "abc123", checksPending: false },
});
assert.equal((yield* watchOf)?.until, "merged");
assert.equal((yield* watchOf)?.headSha, "abc123");

yield* watch("pr-watch-until-again");
assert.equal((yield* watchOf)?.until, "merged");
assert.isTrue((yield* maintenance.rebuild).valid);
assert.equal((yield* watchOf)?.until, "merged");

yield* watch("pr-watch-until-off", "checks");
assert.isUndefined((yield* watchOf)?.until);
assert.equal((yield* watchOf)?.headSha, "abc123");
}),
);

it.effect.each(["manual", "automatic"])("settling ends every pull request watch: %s", (mode) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/provider/agentContextBudget.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"claudeSystemAppendBytes": 6037,
"codexAdditionalContextBytes": 7431,
"mcpToolListJsonBytes": 52145
"mcpToolListJsonBytes": 52519
}
4 changes: 4 additions & 0 deletions apps/server/src/pullRequest/PullRequestWatchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ThreadId,
type OrchestrationV2ThreadShell,
type PullRequestUnavailableError,
type PullRequestWatchUntil,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
Expand Down Expand Up @@ -69,6 +70,7 @@ export class PullRequestWatchService extends Context.Service<
readonly threadId: ThreadId;
readonly target: PullRequestTarget;
readonly watching: boolean;
readonly until?: PullRequestWatchUntil | undefined;
}) => Effect.Effect<
ResolvedTarget & { readonly watching: boolean; readonly wasWatching: boolean },
| PullRequestWatchFailedError
Expand Down Expand Up @@ -104,6 +106,7 @@ const make = Effect.gen(function* () {
readonly threadId: ThreadId;
readonly target: PullRequestTarget;
readonly watching: boolean;
readonly until?: PullRequestWatchUntil | undefined;
}) {
const { watching } = input;
const thread = yield* requireThread(input.threadId);
Expand Down Expand Up @@ -158,6 +161,7 @@ const make = Effect.gen(function* () {
number: target.number,
watching,
...(watching ? { link: { url: target.url, source: "agent" as const } } : {}),
...(watching && input.until !== undefined && { until: input.until }),
})
.pipe(
Effect.catchCause((cause) =>
Expand Down
6 changes: 4 additions & 2 deletions docs/user/source-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,10 @@ A watched thread counts as working while the pull request still has checks runni
the **Working** section, including after the agent pushes a fix. Once every check has finished,
including review bots that report as checks, the thread returns to your inbox: what happens next is
up to a person. That includes a workflow waiting for a maintainer to approve it, which runs only
once someone does. The watch stays on and still wakes the agent on new comments, failed checks or
conflicts, and a watched thread never auto-settles.
once someone does. If you ask the agent to act on the pull request itself, such as merging it once
a teammate approves, the thread stays in **Working** until the pull request merges or closes. The
watch stays on and still wakes the agent on new comments, failed checks or conflicts, and a watched
thread never auto-settles.

Cross-repository links use a project on the same host. Azure DevOps reviews require a project checked
out from the matching organization and repository.
Expand Down
Loading
Loading