diff --git a/packages/api/src/cli/github/real-pap.test.ts b/packages/api/src/cli/github/real-pap.test.ts index 169c0324..5e11e73c 100644 --- a/packages/api/src/cli/github/real-pap.test.ts +++ b/packages/api/src/cli/github/real-pap.test.ts @@ -130,6 +130,24 @@ describe('pap git, the real git as the person', () => { expect(logAgentRequest.mock.calls.map(([entry]) => [entry.collectionId, entry.action, entry.query, entry.outcome])).toContainEqual(['git', 'push', 'fix-login', 'allowed']); }); + test('a clone commits as the account with plain git too, its config naming it and nothing of the sign-in', async () => { + await runPap(['git', 'clone', '-q', 'https://github.com/acme/demo', 'plain-clone']); + const clone = repo('plain-clone'); + writeFileSync(join(clone, 'plain.txt'), 'plain\n'); + const plainEnv = {PATH: GIT_DIR, HOME: home, GIT_CONFIG_NOSYSTEM: '1'}; + + await git(['add', '.'], clone, plainEnv); + const committed = await git(['commit', '-q', '-m', 'plain'], clone, plainEnv); + + expect(committed).toMatchObject({status: 0, stderr: ''}); + expect((await git(['log', '-1', '--format=%an <%ae>|%cn <%ce>'], clone, plainEnv)).stdout.trim()).toBe(`${IDENTITY}|${IDENTITY}`); + const config = readFileSync(join(clone, '.git', 'config'), 'utf8'); + expect(config).toContain('name = Dana Octo'); + expect(config).not.toContain('Bearer'); + expect(config).not.toContain(papUrl); + expect(existsSync(join(home, '.gitconfig'))).toBe(false); + }); + test('without Push, the push is refused in PAP’s words and nothing reaches GitHub', async () => { await runPap(['git', 'clone', '-q', 'https://github.com/acme/demo', 'read-clone']); const clone = repo('read-clone'); diff --git a/packages/api/src/mcp/route.test.ts b/packages/api/src/mcp/route.test.ts index e7119ce9..b72bb62d 100644 --- a/packages/api/src/mcp/route.test.ts +++ b/packages/api/src/mcp/route.test.ts @@ -178,7 +178,9 @@ describe('handleMcpRoute', () => { const github = {id: 'github-1', integrationId: 'github', account: 'octo-dana', shared: false, actions: ['read'], tools: null, name: null}; listAgentConnections.mockResolvedValue(Ok({agent: {id: 'agent-1', providerId: 'claude', name: 'Claude'}, connections: [sandboxes, github]})); - expect(await instructions()).toContain('pap --help has the rest. In a sandbox, pap git and pap gh are git and gh as your GitHub connection’s account; plain git and gh are signed out.'); + expect(await instructions()).toContain( + 'pap --help has the rest. In a sandbox, pap git and pap gh are git and gh as your GitHub connection’s account; plain git and gh are signed out. A clone made with pap git commits as that account, with plain git too, so don’t set a name or email of your own.', + ); listAgentConnections.mockResolvedValue(Ok({agent: {id: 'agent-1', providerId: 'claude', name: 'Claude'}, connections: [sandboxes, {...github, actions: []}]})); expect(await instructions()).not.toContain('pap git'); diff --git a/packages/api/src/mcp/tools.ts b/packages/api/src/mcp/tools.ts index 2270aeae..fef44652 100644 --- a/packages/api/src/mcp/tools.ts +++ b/packages/api/src/mcp/tools.ts @@ -135,7 +135,9 @@ function runsCommands(connection: AgentConnection): boolean { function sandboxInstructions(connections: AgentConnection[]): string[] { const usable = connections.filter(runsCommands); const github = connections.some((connection) => connection.integrationId === GITHUB_INTEGRATION_ID && connection.actions.length > 0); - const gitAndGh = github ? ' In a sandbox, pap git and pap gh are git and gh as your GitHub connection’s account; plain git and gh are signed out.' : ''; + const gitAndGh = github + ? ' In a sandbox, pap git and pap gh are git and gh as your GitHub connection’s account; plain git and gh are signed out. A clone made with pap git commits as that account, with plain git too, so don’t set a name or email of your own.' + : ''; return usable.map( (connection) => `${connection.id} has sandboxes: computers with a shell and the internet, for running code. One is a task’s at a time and keeps its home directory, so reuse yours; install what you need under the home (npm -g and pip install --user land there), since after a long sleep what is outside it is gone. sandbox_exec takes a sandbox’s id, not its name. A command run with pap: true reaches your connections from code with the pap CLI: pap connections, pap tools , pap call ''; pap --help has the rest.${gitAndGh}`, diff --git a/packages/cli/src/git.test.ts b/packages/cli/src/git.test.ts index ee76c813..46ed2b6d 100644 --- a/packages/cli/src/git.test.ts +++ b/packages/cli/src/git.test.ts @@ -3,7 +3,7 @@ import {describe, expect, test} from 'bun:test'; import {Err, Ok} from 'ts-results-es'; import {fakeIo} from './fake-io'; -import {gitEnv, gitSubcommand} from './git'; +import {gitArgs, gitEnv, gitSubcommand} from './git'; import type {Env} from './io'; import {run} from './run'; @@ -109,6 +109,64 @@ describe('gitEnv', () => { }); }); +describe('gitArgs', () => { + const identity = {name: 'Dana Octo', email: '1+octo-dana@users.noreply.github.com'}; + const config = [ + '--config', + 'user.name=Dana Octo', + '--config', + 'user.email=1+octo-dana@users.noreply.github.com', + ]; + + test('a clone is given the identity for its own config, before the caller’s options', () => { + expect(gitArgs(['clone', 'https://github.com/a/b'], {}, identity)).toEqual([ + 'clone', + ...config, + 'https://github.com/a/b', + ]); + expect( + gitArgs( + ['-C', 'work', '-c', 'x=y', 'clone', '-q', '--config', 'user.name=Me', 'u'], + {}, + identity, + ), + ).toEqual([ + '-C', + 'work', + '-c', + 'x=y', + 'clone', + ...config, + '-q', + '--config', + 'user.name=Me', + 'u', + ]); + }); + + test('the environment’s author goes first, as gh’s for the clones it runs', () => { + expect( + gitArgs(['clone', 'u'], {GIT_AUTHOR_NAME: 'Bot', GIT_AUTHOR_EMAIL: 'bot@example.com'}, null), + ).toEqual([ + 'clone', + '--config', + 'user.name=Bot', + '--config', + 'user.email=bot@example.com', + 'u', + ]); + }); + + test('anything but a clone, or a clone with no one to commit as, is left as it is', () => { + const cases: string[][] = [['commit', '-m', 'x'], ['push'], ['--version'], ['remote', '-v']]; + for (const args of cases) { + expect(gitArgs(args, {}, identity)).toBe(args); + } + expect(gitArgs(['clone', 'u'], {}, null)).toEqual(['clone', 'u']); + expect(gitArgs(['clone', 'u'], {GIT_AUTHOR_NAME: 'Bot'}, null)).toEqual(['clone', 'u']); + }); +}); + describe('pap git', () => { const IDENTITY = { login: 'octo-dana', @@ -204,6 +262,20 @@ describe('pap git', () => { expect(spawned[0]?.args).toEqual(['-C', 'repo', 'commit', '-m', 'Fix']); }); + test('a clone writes the account into its own config, so plain git there commits as it', async () => { + const {spawned, pap} = harness(); + + await pap('git', 'clone', 'https://github.com/a/b'); + expect(spawned[0]?.args).toEqual([ + 'clone', + '--config', + 'user.name=Dana Octo', + '--config', + `user.email=${IDENTITY.email}`, + 'https://github.com/a/b', + ]); + }); + test('pull gets both, and the connection from the environment', async () => { const {spawned, requests, pap} = harness({ env: { diff --git a/packages/cli/src/git.ts b/packages/cli/src/git.ts index 40c244ba..e923097e 100644 --- a/packages/cli/src/git.ts +++ b/packages/cli/src/git.ts @@ -14,13 +14,17 @@ import {findProgram, namedConnection, SHIM_DIR_ENV} from './programs'; /** * `pap git`: the real git, run with its arguments, stdio and exit code as they are, signed in to - * GitHub as the connection's account for this one run. Nothing is written to any git config: git - * is given, through `GIT_CONFIG_COUNT` and its keys, GitHub's URLs (https and both SSH forms) - * rewritten to PAP's git relay and the command's token as a header sent to the relay alone - * (@proxy/contracts/cli-github), so whatever reaches GitHub goes through PAP, a partial clone's - * objects fetched later among it; and the identity route's author and committer, unless the - * environment sets them, or with none where PAP won't say. Only `git remote`, `-v` and `get-url` - * are left their URLs, so they still show github.com, which gh reads to know the repository. + * GitHub as the connection's account for this one run. Nothing of the sign-in is written to any + * git config: git is given, through `GIT_CONFIG_COUNT` and its keys, GitHub's URLs (https and both + * SSH forms) rewritten to PAP's git relay and the command's token as a header sent to the relay + * alone (@proxy/contracts/cli-github), so whatever reaches GitHub goes through PAP, a partial + * clone's objects fetched later among it; and the identity route's author and committer, unless + * the environment sets them, or with none where PAP won't say. Only `git remote`, `-v` and + * `get-url` are left their URLs, so they still show github.com, which gh reads to know the + * repository. The one thing written is who commits are made as, into a new clone's own config + * (`user.name` and `user.email`, through `git clone --config`): agents run git's local commands, + * `add` and `commit` among them, as plain git, which would otherwise have no identity and leave + * the agent to make one up. */ // git's options before the subcommand that take the next argument as their value. @@ -159,6 +163,33 @@ export function commitIdentity(io: Io): Promise { return fetchIdentity(io); } +/** + * git's arguments: `args` as they are, but for a clone, which is given who commits are made as for + * its own config, so plain git in it commits as the account too. The identity is the + * environment's author where it sets one, as gh's does for the clones it runs, otherwise PAP's; + * none where neither says. The caller's own `--config` comes after it, and wins. + */ +export function gitArgs(args: string[], env: Env, identity: Identity): string[] { + const command = gitSubcommand(args); + if (command?.name !== 'clone') { + return args; + } + const name = env.GIT_AUTHOR_NAME || identity?.name; + const email = env.GIT_AUTHOR_EMAIL || identity?.email; + if (!name || !email) { + return args; + } + const upToClone = args.slice(0, args.length - command.rest.length); + return [ + ...upToClone, + '--config', + `user.name=${name}`, + '--config', + `user.email=${email}`, + ...command.rest, + ]; +} + /** What git is to be run with for `args`. */ export function gitSetup(args: string[], io: Io): Promise> { return Do(async ($) => { @@ -175,7 +206,7 @@ export function realGit(io: Io): string | null { } export const GIT_NOTE = `pap git : git, signed in to GitHub as your GitHub connection's account; plain git is signed out. -Clone https://github.com//; commits are made as that account. +Clone https://github.com//; commits are made as that account, in such a clone with plain git too. With several GitHub connections: pap --connection git `; @@ -195,7 +226,11 @@ export async function gitMain(args: string[], io: Io): Promise { io.err(`pap: ${setup.error.message}\n`); return setup.error.exit; } - const ran = await io.spawn(git, args, gitEnv(io.env, setup.value)); + const ran = await io.spawn( + git, + gitArgs(args, io.env, setup.value.identity), + gitEnv(io.env, setup.value), + ); if (ran.isErr()) { io.err(`pap: could not run ${git}: ${ran.error.message}\n`); return EXIT.failed; diff --git a/packages/docs/content/connections/github.mdx b/packages/docs/content/connections/github.mdx index 579b3132..52542bc1 100644 --- a/packages/docs/content/connections/github.mdx +++ b/packages/docs/content/connections/github.mdx @@ -62,7 +62,9 @@ An agent works in a repository from a command run with `pap` turned on in one of `git@github.com:acme/demo.git`), then `pap git push` and `pap git fetch` in the clone. Commits made with `pap git commit`, and anything else of git's that makes commits or tags, are made as your GitHub account, with GitHub's noreply address for it rather than your own email, unless - the command sets `GIT_AUTHOR_NAME` and the like itself. + the command sets `GIT_AUTHOR_NAME` and the like itself. `pap git clone` writes that name and + address into the clone's own config as `user.name` and `user.email`, so commits made there + with plain git are yours too; nothing of the sign-in is written. - `pap gh` is GitHub's own command line tool, signed in as you: `pap gh pr create`, `pap gh issue list`, `pap gh api …`. The git it runs itself, as for `pap gh repo clone`, is `pap git`.