railyard/apps/rithub-mcp/test/agentTokens.test.mjs
caelum0x 2a842f24a7 feat(rithub-mcp): agent-scoped tokens + the agent PR loop (wedge #2)
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>
2026-11-19 02:08:50 +03:00

86 lines
3.5 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";
// Each test gets its own throwaway data dir so parallel tests (and the
// module-level key cache) never collide with each other or with a real
// deployed data/ directory.
function freshStore() {
const dir = mkdtempSync(join(tmpdir(), "rithub-agent-tokens-"));
process.env.RITHUB_MCP_DATA_DIR = dir;
process.env.RITHUB_MCP_TOKEN_KEY = "11".repeat(32);
return dir;
}
test("mintToken returns a usable token once; lookupToken resolves its binding and decrypts the backing token", async () => {
const dir = freshStore();
try {
const mod = await import(`../src/agentTokens.mjs?t=${Date.now()}-1`);
const minted = await mod.mintToken({
repoOwner: "alice",
repoName: "demo",
scope: "pr",
ttlHours: 1,
label: "test",
backingToken: "real-rithub-token-abc",
mintedByLogin: "alice",
purpose: "manual",
});
assert.ok(minted.token.startsWith(mod.TOKEN_PREFIX));
const looked = await mod.lookupToken(minted.token);
assert.equal(looked.repoOwner, "alice");
assert.equal(looked.repoName, "demo");
assert.equal(looked.scope, "pr");
assert.equal(looked.backingToken, "real-rithub-token-abc");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("a revoked or expired token no longer resolves", async () => {
const dir = freshStore();
try {
const mod = await import(`../src/agentTokens.mjs?t=${Date.now()}-2`);
const minted = await mod.mintToken({ repoOwner: "a", repoName: "b", scope: "read", ttlHours: 1, backingToken: "x", mintedByLogin: "a" });
await mod.revokeToken(minted.id);
assert.equal(await mod.lookupToken(minted.token), null);
const minted2 = await mod.mintToken({ repoOwner: "a", repoName: "b", scope: "read", ttlHours: -1, backingToken: "x", mintedByLogin: "a" });
assert.equal(await mod.lookupToken(minted2.token), null);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("listTokensForRepo never includes the encrypted backing token", async () => {
const dir = freshStore();
try {
const mod = await import(`../src/agentTokens.mjs?t=${Date.now()}-3`);
await mod.mintToken({ repoOwner: "a", repoName: "b", scope: "full", ttlHours: 1, backingToken: "secret", mintedByLogin: "a", label: "ci" });
const tokens = await mod.listTokensForRepo("a", "b");
assert.equal(tokens.length, 1);
assert.equal(tokens[0].label, "ci");
assert.ok(!("encBackingToken" in tokens[0]));
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("revokeAllForRepo only revokes the matching purpose", async () => {
const dir = freshStore();
try {
const mod = await import(`../src/agentTokens.mjs?t=${Date.now()}-4`);
await mod.mintToken({ repoOwner: "a", repoName: "b", scope: "pr", ttlHours: 1, backingToken: "x", mintedByLogin: "a", purpose: "manual" });
await mod.mintToken({ repoOwner: "a", repoName: "b", scope: "pr", ttlHours: 1, backingToken: "x", mintedByLogin: "a", purpose: "assign-to-agent" });
const result = await mod.revokeAllForRepo("a", "b", { purpose: "assign-to-agent" });
assert.equal(result.revokedCount, 1);
const remaining = await mod.listTokensForRepo("a", "b");
assert.equal(remaining.length, 1);
assert.equal(remaining[0].purpose, "manual");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});