Skip to content

Commit 1ea9afa

Browse files
fix(server): GitLab URL routing keeps raw selectors and only learns working URLs
- Repository-only link_pull_request and watch_pull_request no longer read the host first: as on main, the repository is a raw selector, and only a supplied URL becomes the link's authoritative URL. - The service learns a repository's URL only after a read through it succeeds, so a failed URL cannot displace working routing. - A learned URL is part of a read's cache identity, so a read held from the raw project is not served once the route changes. AI-assisted; verified with the tests and tools listed above.
1 parent 015c57a commit 1ea9afa

6 files changed

Lines changed: 74 additions & 174 deletions

File tree

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ 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 PullRequestService from "../pullRequest/PullRequestService.ts";
4241
import * as PullRequestReviewGuideService from "../pullRequest/PullRequestReviewGuideService.ts";
4342
import * as ServerConfig from "../config.ts";
4443
import * as ServerSettings from "../serverSettings.ts";
@@ -96,7 +95,6 @@ const layerPullRequestsTest = McpHttpServer.layerPullRequestsToolkit.pipe(
9695
Layer.mergeAll(
9796
Layer.mock(ProjectService.ProjectService)({}),
9897
Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}),
99-
Layer.mock(PullRequestService.PullRequestService)({}),
10098
Layer.mock(Orchestrator.OrchestratorV2)({}),
10199
Layer.mock(ProjectionStore.ProjectionStoreV2)({}),
102100
NodeServices.layer,
@@ -1333,7 +1331,6 @@ it.effect("lists pull request watch tools only to threads that are not delegated
13331331
McpToolAccessTestkit.liveThreadsLayer,
13341332
Layer.mock(ProjectService.ProjectService)({}),
13351333
Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}),
1336-
Layer.mock(PullRequestService.PullRequestService)({}),
13371334
Layer.mock(Orchestrator.OrchestratorV2)({}),
13381335
Layer.mock(ProjectionStore.ProjectionStoreV2)({}),
13391336
NodeServices.layer,

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

Lines changed: 17 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
v2PullRequestThread,
2727
} from "../../../orchestration-v2/testkit/pullRequestFixtures.ts";
2828
import * as ProjectService from "../../../project/ProjectService.ts";
29-
import * as PullRequestService from "../../../pullRequest/PullRequestService.ts";
3029
import * as PullRequestReviewGuideService from "../../../pullRequest/PullRequestReviewGuideService.ts";
3130
import * as McpInvocationContext from "../../McpInvocationContext.ts";
3231
import * as McpToolAccess from "../../McpToolAccess.ts";
@@ -147,7 +146,6 @@ const gitlabProject = makeProject({
147146
interface HarnessOptions {
148147
readonly thread?: PullRequestTestThread | null;
149148
readonly project?: OrchestrationProjectShell | null;
150-
readonly detail?: PullRequestService.PullRequestService["Service"]["detail"];
151149
readonly reject?: (command: OrchestrationCommand) => string | null;
152150
readonly projects?: ReadonlyArray<OrchestrationProjectShell>;
153151
readonly generate?: (
@@ -164,7 +162,6 @@ const makeHarness = Effect.fn("makePullRequestsToolkitHarness")(function* (
164162
options: HarnessOptions = {},
165163
) {
166164
const commands = yield* Ref.make<ReadonlyArray<OrchestrationCommand>>([]);
167-
const detailRequests = yield* Ref.make<ReadonlyArray<PullRequestService.PullRequestRef>>([]);
168165
const guideRequests = yield* Ref.make<ReadonlyArray<PullRequestRef>>([]);
169166
const thread = options.thread === undefined ? makeThread([]) : options.thread;
170167
const project = options.project === undefined ? makeProject() : options.project;
@@ -185,12 +182,6 @@ const makeHarness = Effect.fn("makePullRequestsToolkitHarness")(function* (
185182
getShell: () => Effect.succeed(Option.fromNullishOr(project)),
186183
listShells: () => Effect.succeed(options.projects ?? (project ? [project] : [])),
187184
}),
188-
Layer.mock(PullRequestService.PullRequestService)({
189-
detail: (input) =>
190-
Ref.update(detailRequests, (recorded) => [...recorded, input]).pipe(
191-
Effect.andThen(options.detail?.(input) ?? Effect.die("Unexpected pull request read")),
192-
),
193-
}),
194185
Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({
195186
get: () => Effect.succeed(NO_GUIDE),
196187
generate: (input) =>
@@ -238,7 +229,7 @@ const makeHarness = Effect.fn("makePullRequestsToolkitHarness")(function* (
238229
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(capabilities)),
239230
Effect.provide(layerDependencies),
240231
);
241-
return { commands, detailRequests, guideRequests, call };
232+
return { commands, guideRequests, call };
242233
});
243234

244235
describe("pull request toolkit handlers", () => {
@@ -442,106 +433,23 @@ describe("pull request toolkit handlers", () => {
442433
}),
443434
);
444435

445-
it.effect("resolves repository-only GitLab links and watches to the actual MR URL", () =>
436+
it.effect("keeps a GitLab repository selector raw and carries an existing link to a watch", () =>
446437
Effect.gen(function* () {
447-
const url = "http://code.example:8924/gitlab/gitlab/team/subgroup/project/-/merge_requests/1";
448438
const target = { repository: "gitlab/team/subgroup/project", number: 1 };
449-
const harness = yield* makeHarness({
450-
project: gitlabProject,
451-
detail: () =>
452-
Effect.succeed({
453-
...target,
454-
provider: SourceControlProviderKind.make("gitlab"),
455-
capabilities: {
456-
diff: true,
457-
comment: true,
458-
actions: [],
459-
mergeMethods: [],
460-
search: false,
461-
review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
462-
reviewers: { request: false, listCandidates: false },
463-
},
464-
viewerPermissions: {
465-
actions: [],
466-
comment: true,
467-
resolve: false,
468-
verdicts: [],
469-
requestReviewers: false,
470-
},
471-
projectId: PROJECT_ID,
472-
projectTitle: "GitLab",
473-
workspaceRoot: "/workspace/project",
474-
title: "Project 2 MR",
475-
body: "",
476-
url,
477-
author: null,
478-
state: "open",
479-
isDraft: false,
480-
mergeability: "mergeable",
481-
additions: 0,
482-
deletions: 0,
483-
changedFiles: 0,
484-
headBranch: "feature",
485-
baseBranch: "main",
486-
createdAt: "2026-10-19T00:00:00.000Z",
487-
updatedAt: "2026-10-19T00:00:00.000Z",
488-
mergedAt: null,
489-
closedAt: null,
490-
reviewers: [],
491-
labels: [],
492-
checks: [],
493-
mergeCapabilities: { merge: true, squash: false, rebase: false },
494-
}),
495-
});
496-
const linked = yield* harness.call("link_pull_request", target);
497-
const watched = yield* harness.call("watch_pull_request", target);
498-
expect([linked.url, watched.url]).toEqual([url, url]);
499-
expect(yield* Ref.get(harness.detailRequests)).toEqual([
500-
{ projectId: PROJECT_ID, host: "code.example", ...target },
501-
{ projectId: PROJECT_ID, host: "code.example", ...target },
502-
]);
503-
expect(yield* Ref.get(harness.commands)).toMatchObject([
504-
{ type: "thread.pull-request.link", ...target, url, repositoryWebUrl: url },
505-
{
506-
type: "thread.pull-request.watch",
507-
...target,
508-
link: { url, repositoryWebUrl: url, source: "agent" },
509-
},
510-
]);
511-
}),
512-
);
439+
const harness = yield* makeHarness({ project: gitlabProject });
440+
yield* harness.call("link_pull_request", target);
441+
const [link] = yield* Ref.get(harness.commands);
442+
expect(link).toMatchObject({ type: "thread.pull-request.link", ...target });
443+
expect(link).not.toHaveProperty("repositoryWebUrl");
513444

514-
it.effect("requires GitLab URL resolution only when creating a link or watch", () =>
515-
Effect.gen(function* () {
516-
const harness = yield* makeHarness({
517-
project: gitlabProject,
518-
detail: () =>
519-
Effect.fail(new PullRequestOperationError({ operation: "detail", detail: "host down" })),
520-
});
521-
const target = { repository: "gitlab/team/subgroup/project", number: 1 };
522-
const linked = yield* harness.call("link_pull_request", target).pipe(Effect.result);
523-
const watched = yield* harness.call("watch_pull_request", target).pipe(Effect.result);
524-
expect(yield* Ref.get(harness.commands)).toEqual([]);
525-
expect(linked).toMatchObject({
526-
_tag: "Failure",
527-
failure: { _tag: "PullRequestLinkFailedError" },
528-
});
529-
expect(watched).toMatchObject({
530-
_tag: "Failure",
531-
failure: { _tag: "PullRequestWatchFailedError" },
532-
});
533-
534-
const url = "http://code.example:8924/gitlab/gitlab/team/subgroup/project/-/merge_requests/1";
445+
const url = "http://code.example:8924/gitlab/team/subgroup/project/-/merge_requests/1";
535446
const existing = yield* makeHarness({
536447
project: gitlabProject,
537448
thread: makeThread([
538449
makeLink(1, { ...target, host: "code.example", url, repositoryWebUrl: url }),
539450
]),
540-
detail: () =>
541-
Effect.fail(new PullRequestOperationError({ operation: "detail", detail: "host down" })),
542451
});
543-
const result = yield* existing.call("watch_pull_request", target).pipe(Effect.result);
544-
expect(yield* Ref.get(existing.detailRequests)).toEqual([]);
452+
const result = yield* existing.call("watch_pull_request", target);
545453
expect(yield* Ref.get(existing.commands)).toMatchObject([
546454
{
547455
type: "thread.pull-request.watch",
@@ -552,11 +460,11 @@ describe("pull request toolkit handlers", () => {
552460
link: { url, repositoryWebUrl: url, source: "agent" },
553461
},
554462
]);
555-
expect(result).toMatchObject({ _tag: "Success", success: { url } });
463+
expect(result.url).toBe(url);
556464
}),
557465
);
558466

559-
it.effect("builds other hosts' URLs without reading the project's GitLab", () =>
467+
it.effect("builds the URL in the project host's own shape", () =>
560468
Effect.gen(function* () {
561469
const harness = yield* makeHarness({
562470
project: makeProject({
@@ -570,6 +478,12 @@ describe("pull request toolkit handlers", () => {
570478
displayName: "group/sub/project",
571479
}),
572480
});
481+
const result = yield* harness.call("link_pull_request", {
482+
repository: "group/sub/project",
483+
number: 42,
484+
});
485+
expect(result.url).toBe("https://gitlab.com/group/sub/project/-/merge_requests/42");
486+
expect(result.host).toBe("gitlab.com");
573487
const other = yield* harness.call("link_pull_request", {
574488
host: "gitlab.example.test",
575489
repository: "team/repo",

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

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,13 @@ function projectForPullRequest(
196196
return projects.find(matches);
197197
}
198198

199+
// A supplied URL is the link's authority; a repository alone stays a raw selector, as on main.
200+
const linkOf = (target: ResolvedTarget, input: PullRequestTargetInput) =>
201+
input.url === undefined ? { url: target.url } : { url: target.url, repositoryWebUrl: target.url };
202+
199203
const make = Effect.gen(function* () {
200204
const engine = yield* Orchestrator.OrchestratorV2;
201205
const reviewGuides = yield* PullRequestReviewGuideService.PullRequestReviewGuideService;
202-
const pullRequests = yield* PullRequestService.PullRequestService;
203206

204207
const projects = yield* ProjectService.ProjectService;
205208
const random = yield* SecureRandom.SecureRandom;
@@ -262,33 +265,6 @@ const make = Effect.gen(function* () {
262265
? Effect.failCause(cause as Cause.Cause<never>)
263266
: Effect.fail(new Failure({ cause }));
264267

265-
const resolveLink = Effect.fn("PullRequestsToolkit.resolveLink")(function* (
266-
target: ResolvedTarget,
267-
input: PullRequestTargetInput,
268-
project: OrchestrationProjectShell | undefined,
269-
) {
270-
if (input.url !== undefined) {
271-
return { url: target.url, repositoryWebUrl: target.url };
272-
}
273-
// Only a URL built from the project's own GitLab remote can miss an installation prefix.
274-
const projectHost = projectHostAndProvider(project);
275-
if (
276-
project === undefined ||
277-
projectHost.kind !== "gitlab" ||
278-
projectHost.host?.toLowerCase() !== target.host
279-
) {
280-
return { url: target.url };
281-
}
282-
const detail = yield* pullRequests.detail({
283-
projectId: project.id,
284-
host: target.host,
285-
repository: target.repository,
286-
number: target.number,
287-
});
288-
const url = PullRequestService.withoutUrlCredentials(detail.url);
289-
return { url, repositoryWebUrl: url };
290-
});
291-
292268
const setWatching = Effect.fn("PullRequestsToolkit.setWatching")(function* (
293269
input: PullRequestTargetInput,
294270
watching: boolean,
@@ -311,9 +287,7 @@ const make = Effect.gen(function* () {
311287
const link = !watching
312288
? undefined
313289
: before === undefined
314-
? yield* resolveLink(target, input, project).pipe(
315-
Effect.mapError((cause) => new PullRequestWatchFailedError({ cause })),
316-
)
290+
? linkOf(target, input)
317291
: {
318292
url: before.url,
319293
...(before.repositoryWebUrl === undefined
@@ -417,9 +391,7 @@ const make = Effect.gen(function* () {
417391
);
418392
if (existing && existing.source !== "stack-dismissed")
419393
return { ...target, url: existing.url, alreadyLinked: true, ...next };
420-
const link = yield* resolveLink(target, input, project).pipe(
421-
Effect.mapError((cause) => new PullRequestLinkFailedError({ cause })),
422-
);
394+
const link = linkOf(target, input);
423395
const alreadyLinked = yield* engine
424396
.dispatch({
425397
type: "thread.pull-request.link",

‎apps/server/src/provider/AgentContextBudget.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import { ProjectCloneTracker } from "../project/ProjectCloneTracker.ts";
3535
import { ProjectService } from "../project/ProjectService.ts";
3636
import { ProjectSetupScriptRunner } from "../project/ProjectSetupScriptRunner.ts";
3737
import { PullRequestReviewGuideService } from "../pullRequest/PullRequestReviewGuideService.ts";
38-
import { PullRequestService } from "../pullRequest/PullRequestService.ts";
3938
import { ScheduledTaskService } from "../scheduledTasks/ScheduledTaskService.ts";
4039
import { SecretRequests } from "../secrets/SecretRequests.ts";
4140
import { ServerSettingsService } from "../serverSettings.ts";
@@ -69,7 +68,6 @@ const toolHandlerServices = Layer.mergeAll(
6968
Layer.mock(ProviderAdapterRegistryV2)({}),
7069
Layer.mock(ProviderRegistry)({}),
7170
Layer.mock(PullRequestReviewGuideService)({}),
72-
Layer.mock(PullRequestService)({}),
7371
Layer.mock(RemoteDelegation)({}),
7472
Layer.mock(ScheduledTaskService)({}),
7573
Layer.mock(SecretRequests)({}),

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,12 +2035,12 @@ it.effect("keeps raw GitLab selectors apart from an HTTP checkout's URL", () =>
20352035
}),
20362036
);
20372037

2038-
it.effect("reads a linked GitLab merge request through its URL when asked by repository", () =>
2038+
it.effect("reads a linked GitLab merge request through its URL once that URL has worked", () =>
20392039
Effect.gen(function* () {
20402040
const host = "gitlab.localhost";
20412041
const repository = "gitlab/team/subgroup/project";
20422042
const webUrl = `http://${host}/gitlab/team/subgroup/project/-/merge_requests/9`;
2043-
const reads: Array<Parameters<PullRequestProviderApi["getChangeRequest"]>[0]> = [];
2043+
const unreachable = `http://unreachable.${host}/gitlab/team/subgroup/project/-/merge_requests/9`;
20442044
const gitlab = yield* makeService({
20452045
projects: [
20462046
project({
@@ -2056,25 +2056,37 @@ it.effect("reads a linked GitLab merge request through its URL when asked by rep
20562056
providers: [
20572057
fakePullRequestProvider(SourceControlProviderKind.make("gitlab"), {
20582058
getChangeRequest: (input) =>
2059-
Effect.sync(() => {
2060-
reads.push(input);
2061-
return hostedChangeRequest("");
2062-
}),
2059+
input.webUrl === unreachable
2060+
? Effect.fail(
2061+
new PullRequestProviderError({
2062+
provider: SourceControlProviderKind.make("gitlab"),
2063+
operation: "getChangeRequest",
2064+
reason: "failed",
2065+
detail: "unreachable",
2066+
}),
2067+
)
2068+
: Effect.succeed({
2069+
...hostedChangeRequest(""),
2070+
number: input.number,
2071+
title: input.webUrl === undefined ? "raw project" : "linked project",
2072+
}),
20632073
}),
20642074
],
20652075
});
2066-
const reference = { projectId: "p1" as ProjectId, host, repository, number: 9 };
2067-
// The watch reactor reads with the link's URL; the detail panel asks by repository alone.
2068-
const linked = { ...reference, webUrl };
2069-
const rawSelection = { ...reference, webUrl: null };
2070-
yield* gitlab.detail(linked);
2071-
yield* gitlab.detail(reference);
2072-
yield* gitlab.detail(rawSelection);
2073-
assert.deepStrictEqual(reads, [
2074-
{ cwd: "/gitlab", host, repository, number: 9, webUrl },
2075-
{ cwd: "/gitlab", host, repository, number: 9, webUrl },
2076-
{ cwd: "/gitlab", host, repository, number: 9 },
2077-
]);
2076+
const title = (number: number, url?: string | null) =>
2077+
gitlab
2078+
.detail({ projectId: "p1" as ProjectId, host, repository, number, webUrl: url })
2079+
.pipe(Effect.map((detail) => detail.title));
2080+
2081+
assert.strictEqual(yield* title(9), "raw project");
2082+
// A URL whose read fails teaches nothing.
2083+
yield* Effect.flip(title(9, unreachable));
2084+
assert.strictEqual(yield* title(4), "raw project");
2085+
// The watch reactor reads with the link's URL; the detail panel asks by repository alone,
2086+
// and must not be served the raw project's read it held from before.
2087+
assert.strictEqual(yield* title(9, webUrl), "linked project");
2088+
assert.strictEqual(yield* title(9), "linked project");
2089+
assert.strictEqual(yield* title(9, null), "raw project");
20782090
}),
20792091
);
20802092

0 commit comments

Comments
 (0)