Repo owners can now mint a token limited to ONE repo and a chosen scope (read / pr / full) instead of handing an agent their full account token -- via one MCP tool call (mint_agent_token) or the plain /agent-tokens settings page. scopeGuard.mjs enforces the fence on every request before it reaches the real Rithub API; a pr-scope token can open branches/PRs/ issues/comments but never merge, touch another repo, or mint itself a broader token (the 5 token-management tools are ownerOnly and refused outright for an agent-scoped session). enable_agent_on_repo turns on the second half: labelling any open issue "agent" makes bin/agent-worker.mjs (a separate pm2 poller) read the issue, call the Anthropic Messages API for a structured fix, commit it on a new branch, and open a pull request that closes the issue -- commenting on the issue when it starts and when it opens the PR. The worker uses the same scope-guarded client a human-held token would be, so it can't exceed the fence either. Verified end to end against production: a throwaway mail.tm user, a real one-line bug, mint/list/revoke (plus a confirmed-blocked self-escalation attempt), enable_agent_on_repo, an agent-labelled issue, one real pollOnce() run against the live Anthropic API that produced a correct one-line fix, a PR that closed the issue on merge, and the REST /agent-tokens mint/list routes -- all cleaned up after (repo deleted, user purged, token deleted, mail.tm inbox deleted). 27/27 unit tests pass. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
73 lines
3.4 KiB
JavaScript
73 lines
3.4 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
// agentTokenActions.mjs statically imports agentTokens.mjs by its bare
|
|
// specifier, so -- unlike agentTokens.test.mjs, which busts the cache on
|
|
// that exact module -- every dynamic import of agentTokenActions.mjs in
|
|
// this one test file shares a single agentTokens.mjs module instance (and
|
|
// therefore a single DATA_DIR, fixed at whichever import happens first).
|
|
// Set the env once, before anything is imported, and give each test its
|
|
// own repo name instead of its own store.
|
|
const DATA_DIR = mkdtempSync(join(tmpdir(), "rithub-agent-token-actions-"));
|
|
process.env.RITHUB_MCP_DATA_DIR = DATA_DIR;
|
|
process.env.RITHUB_MCP_TOKEN_KEY = "22".repeat(32);
|
|
|
|
const mod = await import("../src/agentTokenActions.mjs");
|
|
const agentTokens = await import("../src/agentTokens.mjs");
|
|
|
|
test.after(() => rmSync(DATA_DIR, { recursive: true, force: true }));
|
|
|
|
function fakeApi({ permissions = { push: true }, login = "alice" } = {}) {
|
|
return {
|
|
get: async (path) => {
|
|
if (path === "/user") return { login };
|
|
if (/^\/repos\/[^/]+\/[^/]+$/.test(path)) return { permissions };
|
|
throw new Error(`unexpected GET ${path}`);
|
|
},
|
|
post: async () => {
|
|
throw new Error("not used in these tests");
|
|
},
|
|
};
|
|
}
|
|
|
|
test("mintAgentToken refuses a caller without push access", async () => {
|
|
await assert.rejects(
|
|
() => mod.mintAgentToken(fakeApi({ permissions: { push: false, admin: false } }), "real-token", { owner: "alice", repo: "no-push", scope: "pr" }),
|
|
(err) => err.status === 403
|
|
);
|
|
});
|
|
|
|
test("mintAgentToken succeeds for a pusher and the token can be listed and revoked", async () => {
|
|
const api = fakeApi();
|
|
const minted = await mod.mintAgentToken(api, "real-token", { owner: "alice", repo: "mint-list-revoke", scope: "pr", label: "ci" });
|
|
assert.ok(minted.token.length > 10);
|
|
assert.equal(minted.scope, "pr");
|
|
|
|
const listed = await mod.listAgentTokens(api, { owner: "alice", repo: "mint-list-revoke" });
|
|
assert.equal(listed.length, 1);
|
|
assert.equal(listed[0].id, minted.id);
|
|
assert.ok(!("token" in listed[0]));
|
|
|
|
const revoked = await mod.revokeAgentToken(api, { id: minted.id });
|
|
assert.equal(revoked.revoked, true);
|
|
assert.equal((await mod.listAgentTokens(api, { owner: "alice", repo: "mint-list-revoke" })).length, 0);
|
|
});
|
|
|
|
test("revokeAgentToken refuses a different login than the one who minted it", async () => {
|
|
const minted = await mod.mintAgentToken(fakeApi({ login: "alice" }), "real-token", { owner: "alice", repo: "owner-check", scope: "read" });
|
|
await assert.rejects(() => mod.revokeAgentToken(fakeApi({ login: "mallory" }), { id: minted.id }), (err) => err.status === 403);
|
|
});
|
|
|
|
test("enableAgentOnRepo replaces any previous assign-to-agent token for the same repo", async () => {
|
|
const api = fakeApi();
|
|
await mod.enableAgentOnRepo(api, "real-token", { owner: "alice", repo: "assign-to-agent-repo" });
|
|
await mod.enableAgentOnRepo(api, "real-token", { owner: "alice", repo: "assign-to-agent-repo" });
|
|
const tokens = await agentTokens.listTokensForRepo("alice", "assign-to-agent-repo", { purpose: "assign-to-agent" });
|
|
assert.equal(tokens.length, 1);
|
|
const disabled = await mod.disableAgentOnRepo(api, { owner: "alice", repo: "assign-to-agent-repo" });
|
|
assert.equal(disabled.tokens_revoked, 1);
|
|
});
|