Skip to content

Commit 24238fe

Browse files
feat(source-control): omit agent credits from PR merge messages (#16192)
1 parent 21eb9a4 commit 24238fe

14 files changed

Lines changed: 570 additions & 34 deletions

‎apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const PAGE_PROJECT_KEYS: Record<SettingsPage, readonly ProjectScopedServerSettin
5050
"new-threads": ["defaultThreadEnvMode", "worktreeSubmodules", "defaultRuntimeMode"],
5151
"source-control": [
5252
"defaultAutoPull",
53+
"removeAgentCreditsOnMerge",
5354
"newWorktreesStartFromOrigin",
5455
"branchNamingMode",
5556
"branchNamePrefix",
@@ -334,6 +335,16 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
334335
disabled={disabledFor("branchNamingMode")}
335336
onChange={write}
336337
/>
338+
<SettingsSection title="Pull requests">
339+
<SettingsSwitchRow
340+
icon="arrow.triangle.merge"
341+
label="Remove agent credits when merging"
342+
subtitle="Remove recognized agent credits from GitHub merge and squash messages, keeping human co-authors. Includes auto-merge. Excludes merge queues, stack merges, and existing commits."
343+
value={uniform("removeAgentCreditsOnMerge")}
344+
disabled={disabledFor("removeAgentCreditsOnMerge")}
345+
onValueChange={(value) => write({ removeAgentCreditsOnMerge: value })}
346+
/>
347+
</SettingsSection>
337348
<SettingsSection title="Default branch">
338349
<SettingsSwitchRow
339350
icon="arrow.down.circle"

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

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,170 @@ layer("GitHubPullRequestCli.layer", (it) => {
18311831
}),
18321832
);
18331833

1834+
it.effect.each(["merge", "enable-auto-merge"] as const)(
1835+
"removes agent credits from the proposed message for %s",
1836+
(action) =>
1837+
Effect.gen(function* () {
1838+
mockedExecute
1839+
.mockReturnValueOnce(
1840+
Effect.succeed(
1841+
output(
1842+
encodeJson({
1843+
data: {
1844+
repository: {
1845+
pullRequest: {
1846+
isMergeQueueEnabled: false,
1847+
headRefOid: "abc123",
1848+
viewerMergeBodyText:
1849+
"Details\n\nCo-authored-by: Alice <[email protected]>\nCo-authored-by: Claude <[email protected]>",
1850+
},
1851+
},
1852+
},
1853+
}),
1854+
),
1855+
),
1856+
)
1857+
.mockReturnValueOnce(Effect.succeed(output("")));
1858+
const cli = yield* GitHubPullRequestCli.GitHubPullRequestCli;
1859+
yield* cli.runPullRequestAction({
1860+
cwd: "/w",
1861+
repository: "acme/web",
1862+
host: "github.com",
1863+
number: 7,
1864+
action,
1865+
mergeMethod: "squash",
1866+
removeAgentCreditsOnMerge: true,
1867+
});
1868+
expect(callAt(0).args).toContain("method=SQUASH");
1869+
expect(callAt(1).args.slice(-2)).toEqual(["--body-file", "-"]);
1870+
expect(callAt(1).args).toContain("--match-head-commit");
1871+
expect(callAt(1).args).toContain("abc123");
1872+
expect(callAt(1).stdin).toBe("Details\n\nCo-authored-by: Alice <[email protected]>");
1873+
expect(callAt(1).args.join(" ")).not.toContain("[email protected]");
1874+
}),
1875+
);
1876+
1877+
it.effect.each([
1878+
{
1879+
description: "an unchanged message",
1880+
body: "Details\n\nCo-authored-by: Alice <[email protected]>",
1881+
queued: false,
1882+
},
1883+
{
1884+
description: "a merge queue",
1885+
body: "Co-authored-by: Claude <[email protected]>",
1886+
queued: true,
1887+
},
1888+
] as const)("keeps GitHub's default message for $description", ({ body, queued }) =>
1889+
Effect.gen(function* () {
1890+
mockedExecute
1891+
.mockReturnValueOnce(
1892+
Effect.succeed(
1893+
output(
1894+
encodeJson({
1895+
data: {
1896+
repository: {
1897+
pullRequest: {
1898+
isMergeQueueEnabled: queued,
1899+
headRefOid: "abc123",
1900+
viewerMergeBodyText: body,
1901+
},
1902+
},
1903+
},
1904+
}),
1905+
),
1906+
),
1907+
)
1908+
.mockReturnValueOnce(Effect.succeed(output("")));
1909+
const cli = yield* GitHubPullRequestCli.GitHubPullRequestCli;
1910+
yield* cli.runPullRequestAction({
1911+
cwd: "/w",
1912+
repository: "acme/web",
1913+
host: "github.com",
1914+
number: 7,
1915+
action: "merge",
1916+
removeAgentCreditsOnMerge: true,
1917+
});
1918+
expect(callAt(0).args).toContain("method=MERGE");
1919+
expect(callAt(1).args).not.toContain("--body-file");
1920+
expect(callAt(1).stdin).toBeUndefined();
1921+
}),
1922+
);
1923+
1924+
it.effect("passes an explicitly empty body when the proposed message only credits an agent", () =>
1925+
Effect.gen(function* () {
1926+
mockedExecute
1927+
.mockReturnValueOnce(
1928+
Effect.succeed(
1929+
output(
1930+
encodeJson({
1931+
data: {
1932+
repository: {
1933+
pullRequest: {
1934+
isMergeQueueEnabled: false,
1935+
headRefOid: "abc123",
1936+
viewerMergeBodyText: "Co-authored-by: Claude <[email protected]>",
1937+
},
1938+
},
1939+
},
1940+
}),
1941+
),
1942+
),
1943+
)
1944+
.mockReturnValueOnce(Effect.succeed(output("")));
1945+
const cli = yield* GitHubPullRequestCli.GitHubPullRequestCli;
1946+
yield* cli.runPullRequestAction({
1947+
cwd: "/w",
1948+
repository: "acme/web",
1949+
host: "github.com",
1950+
number: 7,
1951+
action: "merge",
1952+
removeAgentCreditsOnMerge: true,
1953+
});
1954+
expect(callAt(1).stdin).toBe("");
1955+
expect(callAt(1).args).toContain("--body-file");
1956+
}),
1957+
);
1958+
1959+
it.effect("does not fetch a message for rebase merges", () =>
1960+
Effect.gen(function* () {
1961+
mockedExecute.mockReturnValue(Effect.succeed(output("")));
1962+
const cli = yield* GitHubPullRequestCli.GitHubPullRequestCli;
1963+
yield* cli.runPullRequestAction({
1964+
cwd: "/w",
1965+
repository: "acme/web",
1966+
host: "github.com",
1967+
number: 7,
1968+
action: "merge",
1969+
mergeMethod: "rebase",
1970+
removeAgentCreditsOnMerge: true,
1971+
});
1972+
expect(mockedExecute).toHaveBeenCalledTimes(1);
1973+
expect(callAt(0).args).toContain("--rebase");
1974+
}),
1975+
);
1976+
1977+
it.effect("refuses to merge when the proposed message cannot be read", () =>
1978+
Effect.gen(function* () {
1979+
mockedExecute.mockReturnValue(
1980+
Effect.succeed(output('{"data":{"repository":{"pullRequest":null}}}')),
1981+
);
1982+
const cli = yield* GitHubPullRequestCli.GitHubPullRequestCli;
1983+
const result = yield* Effect.result(
1984+
cli.runPullRequestAction({
1985+
cwd: "/w",
1986+
repository: "acme/web",
1987+
host: "github.com",
1988+
number: 7,
1989+
action: "merge",
1990+
removeAgentCreditsOnMerge: true,
1991+
}),
1992+
);
1993+
expect(result._tag).toBe("Failure");
1994+
expect(mockedExecute).toHaveBeenCalledTimes(1);
1995+
}),
1996+
);
1997+
18341998
it.effect("arms auto-merge with the same strategy a merge would have used", () =>
18351999
Effect.gen(function* () {
18362000
mockedExecute.mockReturnValue(Effect.succeed(output("")));

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

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { removeAgentCredits } from "./mergeMessage.ts";
12
import { makeChecksRevalidator } from "./gitHubConditionalChecks.ts";
23
import { runGitHubStackAction, type GitHubStackActionError } from "./githubStackActions.ts";
34
import * as Cause from "effect/Cause";
@@ -737,6 +738,7 @@ export class GitHubPullRequestCli extends Context.Service<
737738
readonly action: PullRequestAction;
738739
readonly stackNumber?: number;
739740
readonly expectedStackHeads?: ReadonlyArray<PullRequestStackHead>;
741+
readonly removeAgentCreditsOnMerge?: boolean;
740742
readonly mergeMethod?: PullRequestMergeMethod;
741743
readonly updateMethod?: PullRequestUpdateMethod;
742744
}) => Effect.Effect<void, GitHubPullRequestCliError>;
@@ -1055,6 +1057,35 @@ function cursorVariable(cursor: string | null): readonly [string, string] {
10551057
return cursor === null ? ["-F", "cursor=null"] : ["-f", `cursor=${cursor}`];
10561058
}
10571059

1060+
const MERGE_MESSAGE_GRAPHQL_QUERY = `
1061+
query PullRequestMergeMessage($owner: String!, $name: String!, $number: Int!, $method: PullRequestMergeMethod!) {
1062+
repository(owner: $owner, name: $name) {
1063+
pullRequest(number: $number) {
1064+
isMergeQueueEnabled
1065+
headRefOid
1066+
viewerMergeBodyText(mergeType: $method)
1067+
}
1068+
}
1069+
}`;
1070+
1071+
const decodeMergeMessageResponse = Schema.decodeUnknownResult(
1072+
Schema.fromJsonString(
1073+
Schema.Struct({
1074+
data: Schema.Struct({
1075+
repository: Schema.Struct({
1076+
pullRequest: Schema.Struct({
1077+
isMergeQueueEnabled: Schema.Boolean,
1078+
headRefOid: Schema.String,
1079+
viewerMergeBodyText: Schema.String,
1080+
}),
1081+
}),
1082+
}),
1083+
}),
1084+
),
1085+
);
1086+
const decodeMergeMessage = (raw: string) =>
1087+
Result.map(decodeMergeMessageResponse(raw), (response) => response.data.repository.pullRequest);
1088+
10581089
function actionArgs(
10591090
action: PullRequestAction,
10601091
mergeMethod: PullRequestMergeMethod | undefined,
@@ -2656,12 +2687,52 @@ export const make = Effect.gen(function* () {
26562687
input.mergeMethod,
26572688
input.updateMethod,
26582689
);
2659-
return github
2660-
.execute({
2690+
return Effect.gen(function* () {
2691+
let body: string | undefined;
2692+
let expectedHead: string | undefined;
2693+
if (
2694+
input.removeAgentCreditsOnMerge === true &&
2695+
(input.action === "merge" || input.action === "enable-auto-merge") &&
2696+
input.mergeMethod !== "rebase"
2697+
) {
2698+
const { owner, name } = parseRepositorySelector(input.repository);
2699+
const message = yield* graphqlRead({
2700+
cwd: input.cwd,
2701+
host: input.host,
2702+
operation: "runPullRequestAction",
2703+
allowReserve: true,
2704+
query: MERGE_MESSAGE_GRAPHQL_QUERY,
2705+
variables: [
2706+
["-f", `owner=${owner}`],
2707+
["-f", `name=${name}`],
2708+
["-F", `number=${input.number}`],
2709+
["-f", `method=${input.mergeMethod === "squash" ? "SQUASH" : "MERGE"}`],
2710+
],
2711+
decode: decodeMergeMessage,
2712+
});
2713+
// GitHub's merge queue chooses its own message and ignores custom text.
2714+
if (!message.isMergeQueueEnabled) {
2715+
const cleaned = removeAgentCredits(message.viewerMergeBodyText);
2716+
if (cleaned !== message.viewerMergeBodyText) {
2717+
body = cleaned;
2718+
expectedHead = message.headRefOid;
2719+
}
2720+
}
2721+
}
2722+
yield* github.execute({
26612723
cwd: input.cwd,
2662-
args: ["pr", subcommand!, String(input.number), ...repositoryArgs(input), ...flags],
2663-
})
2664-
.pipe(Effect.asVoid);
2724+
args: [
2725+
"pr",
2726+
subcommand!,
2727+
String(input.number),
2728+
...repositoryArgs(input),
2729+
...flags,
2730+
...(expectedHead === undefined ? [] : ["--match-head-commit", expectedHead]),
2731+
...(body === undefined ? [] : ["--body-file", "-"]),
2732+
],
2733+
...(body === undefined ? {} : { stdin: body }),
2734+
});
2735+
});
26652736
},
26662737

26672738
commentOnPullRequest: (input) =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ export const make = Effect.gen(function* () {
584584
host: input.host,
585585
number: input.number,
586586
action: input.action,
587+
...(input.removeAgentCreditsOnMerge === true ? { removeAgentCreditsOnMerge: true } : {}),
587588
...(input.stackNumber === undefined ? {} : { stackNumber: input.stackNumber }),
588589
...(input.expectedStackHeads === undefined
589590
? {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ export interface PullRequestProviderApi {
543543
readonly action: PullRequestAction;
544544
readonly stackNumber?: number;
545545
readonly expectedStackHeads?: ReadonlyArray<PullRequestStackHead>;
546+
/** GitHub merge message cleanup; ignored by hosts without support. */
547+
readonly removeAgentCreditsOnMerge?: boolean;
546548
/** Meaningful for `merge` and `enable-auto-merge`; absent takes the host's own default. */
547549
readonly mergeMethod?: PullRequestMergeMethod;
548550
/** Only meaningful for `update-branch`; absent takes the host's own default. */

0 commit comments

Comments
 (0)