Skip to content

Commit 5a5bd71

Browse files
committed
fix(azure): preserve authentication and review read failures
Respect host rate-limit pauses for cached details, retain typed MCP target failures, and avoid remembering signed-out requests as permission denials. Wrap generated Azure Markdown destinations so names containing parentheses remain valid links. Implemented and verified by GPT-6.1 Sol through Codex.
1 parent d50bbe6 commit 5a5bd71

8 files changed

Lines changed: 128 additions & 17 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,29 @@ it("keeps failure diagnostics as the cause rather than exposing them in the tool
761761
expect(failure.cause).toBe(cause);
762762
});
763763

764+
it.effect("preserves typed resolution failures for host reads and actions", () =>
765+
Effect.gen(function* () {
766+
const harness = yield* makeHarness();
767+
const input = { url: "https://example.com/private?token=secret" };
768+
const readFailure = yield* harness
769+
.call("read_pull_request_host_details", input)
770+
.pipe(Effect.flip);
771+
const actionFailure = yield* harness
772+
.call("run_pull_request_host_action", {
773+
...input,
774+
action: { type: "vote", vote: "reset" },
775+
})
776+
.pipe(Effect.flip);
777+
const guideFailure = yield* harness.call("read_pull_request_guide", input).pipe(Effect.flip);
778+
for (const failure of [readFailure, actionFailure, guideFailure]) {
779+
expect(failure).toMatchObject({ _tag: "PullRequestUrlInvalidError" });
780+
expect(failure.message).not.toContain(input.url);
781+
expect(failure).not.toHaveProperty("operation");
782+
}
783+
expect(yield* Ref.get(harness.guideRequests)).toEqual([]);
784+
}),
785+
);
786+
764787
it.effect(
765788
"routes native PR reads and writes through the matching project and requires its capability",
766789
() =>

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
} from "@t3tools/shared/threadPullRequests";
55
import {
66
CommandId,
7-
PullRequestOperationError,
87
pullRequestHostOf,
98
type OrchestrationProjectShell,
109
type OrchestrationV2ThreadShell,
@@ -316,7 +315,7 @@ const make = Effect.gen(function* () {
316315
failure: null,
317316
})),
318317
Effect.catch((error) =>
319-
Effect.succeed({ input, target: null, project: undefined, failure: error.message }),
318+
Effect.succeed({ input, target: null, project: undefined, failure: error }),
320319
),
321320
),
322321
);
@@ -343,11 +342,7 @@ const make = Effect.gen(function* () {
343342
input: Omit<PullRequestTargetInput, "threadId">,
344343
) {
345344
const [entry] = yield* locateAll([input]);
346-
if (entry?.target === null)
347-
return yield* new PullRequestOperationError({
348-
operation: "hostDetails",
349-
detail: entry.failure ?? "Could not resolve the pull request.",
350-
});
345+
if (entry?.target === null) return yield* entry.failure;
351346
if (!entry || !entry.project)
352347
return yield* new PullRequestProjectNotFoundError({
353348
repository: entry?.target?.repository ?? input.repository ?? "",
@@ -450,7 +445,7 @@ const make = Effect.gen(function* () {
450445
number: entry.input.number ?? 0,
451446
url: entry.input.url ?? "",
452447
outcome: "failed" as const,
453-
detail: entry.failure,
448+
detail: entry.failure.message,
454449
};
455450
}
456451
const identity = {
@@ -492,7 +487,7 @@ const make = Effect.gen(function* () {
492487
Effect.gen(function* () {
493488
const [entry] = yield* locateAll([input]);
494489
if (entry !== undefined && entry.target === null) {
495-
return yield* new PullRequestGuideFailedError({ detail: entry.failure ?? "" });
490+
return yield* entry.failure;
496491
}
497492
if (entry === undefined || entry.target === null || entry.project === undefined) {
498493
return yield* new PullRequestProjectNotFoundError({

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7827,6 +7827,46 @@ it.effect(
78277827
}),
78287828
);
78297829

7830+
it.effect("pauses host detail reads after a rate limit while allowing host actions", () =>
7831+
Effect.gen(function* () {
7832+
let reads = 0;
7833+
let actions = 0;
7834+
const kind = SourceControlProviderKind.make("github");
7835+
const service = yield* makeService({
7836+
projects: [project({ id: "p1", title: "web", workspaceRoot: "/w", repository: "acme/web" })],
7837+
providers: [
7838+
fakeProvider(kind, {
7839+
capabilities: { ...fakeProvider(kind).capabilities, hostActions: true },
7840+
getHostDetails: () =>
7841+
Effect.suspend(() => {
7842+
reads++;
7843+
return Effect.fail(
7844+
new PullRequestProviderError({
7845+
provider: kind,
7846+
operation: "getHostDetails",
7847+
reason: "rate-limited",
7848+
detail: "API rate limit exceeded.",
7849+
}),
7850+
);
7851+
}),
7852+
runHostAction: () =>
7853+
Effect.sync(() => {
7854+
actions++;
7855+
return { message: "Updated.", url: null };
7856+
}),
7857+
}),
7858+
],
7859+
});
7860+
const ref = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
7861+
yield* service.hostDetails(ref).pipe(Effect.flip);
7862+
const paused = yield* service.hostDetails({ ...ref, number: 2 }).pipe(Effect.flip);
7863+
expect(paused.message).toContain("paused");
7864+
expect(reads).toBe(1);
7865+
yield* service.runHostAction({ ...ref, action: { type: "follow", enabled: true } });
7866+
expect(actions).toBe(1);
7867+
}),
7868+
);
7869+
78307870
it.effect("shares host detail reads and refreshes only the affected PR or project", () =>
78317871
Effect.gen(function* () {
78327872
let reads = 0;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ function withRateLimitBackoff(
577577
: { runHostAction: interactive("runHostAction", api.runHostAction) }),
578578
...(api.getHostDetails === undefined
579579
? {}
580-
: { getHostDetails: interactive("getHostDetails", api.getHostDetails) }),
580+
: { getHostDetails: wrap("getHostDetails", api.getHostDetails) }),
581581
...(api.deleteComment === undefined
582582
? {}
583583
: { deleteComment: interactive("deleteComment", api.deleteComment) }),

‎packages/shared/src/githubMarkdownLinks.test.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22
import { azureDevOpsMarkdownLinks, githubMarkdownLinks } from "./githubMarkdownLinks.ts";
3+
import { unified } from "unified";
4+
import remarkParse from "remark-parse";
35

46
const url = "https://github.com/team/repo/pull/8";
57
describe("githubMarkdownLinks", () => {
@@ -36,18 +38,27 @@ describe("githubMarkdownLinks", () => {
3638

3739
describe("Azure DevOps references", () => {
3840
const repo = "https://dev.azure.com/acme/project/_git/repo";
41+
it("keeps unmatched parentheses in Azure link destinations", () => {
42+
const repository = "https://dev.azure.com/acme/project)/_git/repo(";
43+
const markdown = azureDevOpsMarkdownLinks("!34", repository);
44+
expect(unified().use(remarkParse).parse(markdown).children[0]).toMatchObject({
45+
children: [{ type: "link", url: `${repository}/pullrequest/34` }],
46+
});
47+
});
3948
it("distinguishes work items, pull requests, and native mentions", () => {
4049
const identity = "abcdef01-1234-5678-1234-abcdef012345";
4150
const result = azureDevOpsMarkdownLinks(`See #12, !34 and @<${identity}>.`, repo, {
4251
[identity]: "A [reviewer]",
4352
});
44-
expect(result).toContain("[#12](https://dev.azure.com/acme/project/_workitems/edit/12)");
45-
expect(result).toContain("[!34](https://dev.azure.com/acme/project/_git/repo/pullrequest/34)");
53+
expect(result).toContain("[#12](<https://dev.azure.com/acme/project/_workitems/edit/12>)");
54+
expect(result).toContain(
55+
"[!34](<https://dev.azure.com/acme/project/_git/repo/pullrequest/34>)",
56+
);
4657
expect(result).toContain("@A \\[reviewer\\]");
4758
expect(result).not.toContain("@<");
4859
const commit = "0123456789abcdef0123456789abcdef01234567";
4960
expect(azureDevOpsMarkdownLinks(`Fixed in ${commit}`, repo)).toBe(
50-
`Fixed in [0123456](${repo}/commit/${commit})`,
61+
`Fixed in [0123456](<${repo}/commit/${commit}>)`,
5162
);
5263
});
5364
it("leaves code, authored links, escaped references and HTML code unchanged", () => {

‎packages/shared/src/githubMarkdownLinks.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export function azureDevOpsMarkdownLinks(
179179
: `${project}/_workitems/edit/${reference}`;
180180
markdown =
181181
markdown.slice(0, match.index) +
182-
`[${label}](${target})` +
182+
`[${label}](<${target}>)` +
183183
markdown.slice(match.index + original.length);
184184
}
185185
return markdown;

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestProvider.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,49 @@ it.effect(
701701
}),
702702
);
703703

704+
it.effect("does not remember an authentication failure as a permission denial", () =>
705+
Effect.gen(function* () {
706+
let signedIn = false;
707+
let attempts = 0;
708+
const authentication = new AzureDevOpsCli.AzureDevOpsCliAuthenticationError({
709+
cwd: "/w",
710+
command: "az",
711+
operation: "execute",
712+
argumentCount: 3,
713+
cause: "private credential diagnostics",
714+
});
715+
const provider = yield* make.pipe(
716+
Effect.provide(
717+
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
718+
getPullRequest: () => Effect.succeed(PULL_REQUEST),
719+
readAccess: () => Effect.succeed({ credentialKey: "same-account", permissions: {} }),
720+
replyToThread: () =>
721+
Effect.suspend(() => {
722+
attempts++;
723+
return signedIn ? Effect.void : Effect.fail(authentication);
724+
}),
725+
}),
726+
),
727+
);
728+
const input = {
729+
cwd: "/w",
730+
repository: "web",
731+
host: "dev.azure.com",
732+
number: 7,
733+
threadId: "2",
734+
body: "Test",
735+
};
736+
const failure = yield* provider.replyToThread(input).pipe(Effect.flip);
737+
expect(failure.reason).toBe("unauthenticated");
738+
expect(failure.cause).toBe(authentication);
739+
expect(failure.message).not.toContain("private credential diagnostics");
740+
expect((yield* provider.getViewerPermissions(input)).deniedActions).toEqual({});
741+
signedIn = true;
742+
yield* provider.replyToThread(input);
743+
expect(attempts).toBe(2);
744+
}),
745+
);
746+
704747
it.effect("reads permissions once for the service's check and the write behind it", () =>
705748
Effect.gen(function* () {
706749
let reads = 0;

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestProvider.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,8 @@ export const make = Effect.gen(function* () {
493493
return yield* operation(input).pipe(
494494
Effect.mapError((error) => {
495495
if (
496-
error.reason === "unauthenticated" ||
497-
((isNativeRequestError(error.cause) || isReadError(error.cause)) &&
498-
error.cause.reason === "permission-denied")
496+
(isNativeRequestError(error.cause) || isReadError(error.cause)) &&
497+
error.cause.reason === "permission-denied"
499498
) {
500499
entry.denied[name] =
501500
"Your token does not allow this action. Update its access and refresh the PR.";

0 commit comments

Comments
 (0)