Skip to content

Commit 58a8c90

Browse files
feat(server): agents read, open, update, respond to, and merge PRs through MCP tools
Agents shelled out to gh and cafe for PR work (about 18,000 forge commands in 45 days across three machines), which spends the user's own quota and gives agents raw JSON to trim with --jq. Six new MCP tools route that work through PullRequestService, so it uses the server's forge sign-in (the T3 Code for GitHub app where it is signed in) and works on every forge T3 supports. - pull_request_read: summary, checks, conversation (unresolved threads by default) and diff, defaulting to the PR for the thread's branch. - pull_request_list, pull_request_update, pull_request_respond. - pull_request_create: GitManager.createPullRequest shares runPrStep's branch, upstream, existing-PR and base checks, takes the agent's title and body, and links the PR to the thread. - pull_request_merge: requires expectedHeadSha. PullRequestService refuses a merge whose head moved on every host, and GitHub also pins it atomically through expectedHeadOid. The tool list grows by 2,815 bytes. New mcp-tool-use cases pass 12/12 on Sonnet 5.5 (0/12 on main), the control stays 4/4, and the eval runner answers the new tools from each case's world. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 75b22bf commit 58a8c90

18 files changed

Lines changed: 1141 additions & 41 deletions

File tree

‎apps/server/src/git/GitManager.ts‎

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ interface SourceControlTextGenerationSettings {
118118
readonly style: SourceControlWritingStyleSettings;
119119
}
120120

121+
export interface GitCreatePullRequestResult {
122+
readonly status: "created" | "opened_existing";
123+
/** Absent when the host accepted the PR but did not list it back yet. */
124+
readonly url?: string;
125+
readonly number?: number;
126+
readonly baseBranch: string;
127+
readonly headBranch: string;
128+
readonly title: string;
129+
}
130+
121131
export class GitManager extends Context.Service<
122132
GitManager,
123133
{
@@ -153,6 +163,13 @@ export class GitManager extends Context.Service<
153163
input: GitRunStackedActionInput,
154164
options?: GitRunStackedActionOptions,
155165
) => Effect.Effect<GitRunStackedActionResult, GitManagerServiceError>;
166+
/** Opens a PR for the checkout's pushed branch, or returns the one already open. */
167+
readonly createPullRequest: (input: {
168+
readonly cwd: string;
169+
readonly title: string;
170+
readonly body: string;
171+
readonly baseBranch?: string | undefined;
172+
}) => Effect.Effect<GitCreatePullRequestResult, GitManagerServiceError>;
156173
readonly subscribePullRequestStateChanges: Effect.Effect<
157174
Stream.Stream<ThreadPullRequestKey>,
158175
never,
@@ -2312,12 +2329,17 @@ export const make = Effect.gen(function* () {
23122329
});
23132330
});
23142331

2315-
const runPrStep = Effect.fn("runPrStep")(function* (
2316-
settings: SourceControlTextGenerationSettings,
2332+
const openPrFromBranch = Effect.fn("openPrFromBranch")(function* <E>(
23172333
cwd: string,
23182334
fallbackBranch: string | null,
2319-
threadId: ThreadId | undefined,
23202335
emit: GitActionProgressEmitter,
2336+
requestedBase: string | undefined,
2337+
content: (input: {
2338+
readonly provider: SourceControlProvider.SourceControlProvider["Service"];
2339+
readonly baseBranch: string;
2340+
readonly baseRef: string;
2341+
readonly headBranch: string;
2342+
}) => Effect.Effect<{ readonly title: string; readonly body: string }, E>,
23212343
) {
23222344
const provider = yield* sourceControlProvider(cwd);
23232345
const terms = sourceControlDefinitions.get(provider.kind).changeRequest;
@@ -2355,42 +2377,14 @@ export const make = Effect.gen(function* () {
23552377
};
23562378
}
23572379

2358-
const { baseBranch, baseRef } = yield* resolvePrBase(
2359-
cwd,
2360-
branch,
2361-
details.upstreamRef,
2362-
headContext,
2363-
);
2364-
yield* emit({
2365-
kind: "phase_started",
2366-
phase: "pr",
2367-
label: `Generating ${terms.shortLabel} content...`,
2368-
});
2369-
const baseRangeRef = yield* resolveBaseRangeRef(cwd, baseRef);
2370-
const rangeContext = yield* gitCore.readRangeContext(cwd, baseRangeRef);
2371-
const policy = yield* resolveStylePolicy(cwd, settings);
2372-
const changeRequestTemplate =
2373-
settings.style.followChangeRequestTemplates && provider.readChangeRequestTemplate
2374-
? Option.getOrUndefined(
2375-
yield* provider.readChangeRequestTemplate({ cwd, treeish: baseRangeRef }),
2376-
)
2377-
: undefined;
2378-
const threadContext = yield* readThreadContext(threadId);
2379-
2380-
const generated = yield* textGeneration.generatePrContent({
2381-
cwd,
2380+
const resolved = yield* resolvePrBase(cwd, branch, details.upstreamRef, headContext);
2381+
const baseBranch = requestedBase ?? resolved.baseBranch;
2382+
const generated = yield* content({
2383+
provider,
23822384
baseBranch,
2385+
baseRef: requestedBase ?? resolved.baseRef,
23832386
headBranch: headContext.headBranch,
2384-
commitSummary: limitContext(rangeContext.commitSummary, 20_000),
2385-
diffSummary: limitContext(rangeContext.diffSummary, 20_000),
2386-
diffPatch: limitContext(rangeContext.diffPatch, 60_000),
2387-
...(changeRequestTemplate ? { changeRequestTemplate } : {}),
2388-
...(threadContext ? { threadContext } : {}),
2389-
...(policy ? { policy } : {}),
2390-
modelSelection: settings.modelSelection,
2391-
projectId: settings.projectId,
23922387
});
2393-
23942388
const bodyFile = path.join(
23952389
tempDir,
23962390
`t3code-pr-body-${process.pid}-${yield* randomUUIDv4(cwd)}.md`,
@@ -2449,6 +2443,65 @@ export const make = Effect.gen(function* () {
24492443
};
24502444
});
24512445

2446+
const runPrStep = (
2447+
settings: SourceControlTextGenerationSettings,
2448+
cwd: string,
2449+
fallbackBranch: string | null,
2450+
threadId: ThreadId | undefined,
2451+
emit: GitActionProgressEmitter,
2452+
) =>
2453+
openPrFromBranch(
2454+
cwd,
2455+
fallbackBranch,
2456+
emit,
2457+
undefined,
2458+
({ provider, baseBranch, baseRef, headBranch }) =>
2459+
Effect.gen(function* () {
2460+
const terms = sourceControlDefinitions.get(provider.kind).changeRequest;
2461+
yield* emit({
2462+
kind: "phase_started",
2463+
phase: "pr",
2464+
label: `Generating ${terms.shortLabel} content...`,
2465+
});
2466+
const baseRangeRef = yield* resolveBaseRangeRef(cwd, baseRef);
2467+
const rangeContext = yield* gitCore.readRangeContext(cwd, baseRangeRef);
2468+
const policy = yield* resolveStylePolicy(cwd, settings);
2469+
const changeRequestTemplate =
2470+
settings.style.followChangeRequestTemplates && provider.readChangeRequestTemplate
2471+
? Option.getOrUndefined(
2472+
yield* provider.readChangeRequestTemplate({ cwd, treeish: baseRangeRef }),
2473+
)
2474+
: undefined;
2475+
const threadContext = yield* readThreadContext(threadId);
2476+
2477+
return yield* textGeneration.generatePrContent({
2478+
cwd,
2479+
baseBranch,
2480+
headBranch,
2481+
commitSummary: limitContext(rangeContext.commitSummary, 20_000),
2482+
diffSummary: limitContext(rangeContext.diffSummary, 20_000),
2483+
diffPatch: limitContext(rangeContext.diffPatch, 60_000),
2484+
...(changeRequestTemplate ? { changeRequestTemplate } : {}),
2485+
...(threadContext ? { threadContext } : {}),
2486+
...(policy ? { policy } : {}),
2487+
modelSelection: settings.modelSelection,
2488+
projectId: settings.projectId,
2489+
});
2490+
}),
2491+
);
2492+
2493+
const createPullRequest: GitManager["Service"]["createPullRequest"] = Effect.fn(
2494+
"GitManager.createPullRequest",
2495+
)(function* (input) {
2496+
return yield* openPrFromBranch(
2497+
input.cwd,
2498+
null,
2499+
() => Effect.void,
2500+
input.baseBranch,
2501+
() => Effect.succeed({ title: input.title, body: input.body }),
2502+
);
2503+
});
2504+
24522505
const localStatus: GitManager["Service"]["localStatus"] = Effect.fn("localStatus")(
24532506
function* (input) {
24542507
const cacheKey = yield* normalizeStatusCacheKey(input.cwd);
@@ -3397,6 +3450,7 @@ export const make = Effect.gen(function* () {
33973450
resolvePullRequest,
33983451
preparePullRequestThread,
33993452
runStackedAction,
3453+
createPullRequest,
34003454
subscribePullRequestStateChanges: PubSub.subscribe(pullRequestStateChanges).pipe(
34013455
Effect.map((subscription) => Stream.fromSubscription(subscription)),
34023456
),

‎apps/server/src/mcp/McpHttpServer.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ import * as Sqlite from "../persistence/Sqlite.ts";
3838
import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
3939

4040
import * as DeviceService from "../device/DeviceService.ts";
41+
import * as GitManager from "../git/GitManager.ts";
4142
import * as PullRequestReviewGuideService from "../pullRequest/PullRequestReviewGuideService.ts";
43+
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
4244
import * as ServerConfig from "../config.ts";
4345
import * as ServerSettings from "../serverSettings.ts";
4446
import { advertisedJsonSchema } from "./advertisedJsonSchema.ts";
@@ -95,6 +97,8 @@ const layerPullRequestsTest = McpHttpServer.layerPullRequestsToolkit.pipe(
9597
Layer.mergeAll(
9698
Layer.mock(ProjectService.ProjectService)({}),
9799
Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}),
100+
Layer.mock(PullRequestService.PullRequestService)({}),
101+
Layer.mock(GitManager.GitManager)({}),
98102
Layer.mock(Orchestrator.OrchestratorV2)({}),
99103
Layer.mock(ProjectionStore.ProjectionStoreV2)({}),
100104
NodeServices.layer,
@@ -1331,6 +1335,8 @@ it.effect("lists pull request watch tools only to threads that are not delegated
13311335
McpToolAccessTestkit.liveThreadsLayer,
13321336
Layer.mock(ProjectService.ProjectService)({}),
13331337
Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}),
1338+
Layer.mock(PullRequestService.PullRequestService)({}),
1339+
Layer.mock(GitManager.GitManager)({}),
13341340
Layer.mock(Orchestrator.OrchestratorV2)({}),
13351341
Layer.mock(ProjectionStore.ProjectionStoreV2)({}),
13361342
NodeServices.layer,

0 commit comments

Comments
 (0)