Centralize encrypted bot profiles (#14)
Centralize ordered bot profiles and encrypted credentials in `bot-agents`. - Exposes one action input: `master-key`. - Uses Gitea’s automatic job token internally; dependent repositories provide no author/reviewer/profile inputs. - Keeps encrypted Claude and reviewer credentials in `profiles.yml`; tokenless Codex uses its persisted subscription login. - Falls back only on recognized quota exhaustion, with a distinct UID/home per profile and descendant cleanup before fallback. - Separates decryption, agent execution, and reviewer posting so privileged credentials never enter an agent process. - Installs `obra/superpowers` with the standard Skills CLI; removes custom discovery code. - Adds root-level isolation, rollback, symlink, fallback, posting, YAML, and self-workflow CI coverage. Checks: formatting, lint, all entrypoint type checks, 35 tests as root, diff check, secret scan, and repository diagnostics. Reviewed-on: #14 Co-authored-by: Danny Kim <temeddix@gmail.com> Co-committed-by: Danny Kim <temeddix@gmail.com>
This commit was merged in pull request #14.
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
import { assert, assertEquals } from "jsr:@std/assert@1";
|
||||
import { parse } from "jsr:@std/yaml@1";
|
||||
|
||||
type Mapping = Record<string, unknown>;
|
||||
|
||||
const mapping = (value: unknown, at: string): Mapping => {
|
||||
assert(
|
||||
typeof value === "object" && value !== null && !Array.isArray(value),
|
||||
at,
|
||||
);
|
||||
return value as Mapping;
|
||||
};
|
||||
|
||||
const sequence = (value: unknown, at: string): Mapping[] => {
|
||||
assert(Array.isArray(value), at);
|
||||
return value.map((entry, index) => mapping(entry, `${at}[${index}]`));
|
||||
};
|
||||
|
||||
async function yaml(path: string): Promise<Mapping> {
|
||||
return mapping(parse(await Deno.readTextFile(path)), path);
|
||||
}
|
||||
|
||||
async function yamlFiles(directory = "."): Promise<string[]> {
|
||||
const result: string[] = [];
|
||||
for await (const entry of Deno.readDir(directory)) {
|
||||
if (entry.name === ".git" || entry.name === ".superpowers") continue;
|
||||
const path = directory === "." ? entry.name : `${directory}/${entry.name}`;
|
||||
if (entry.isDirectory) result.push(...await yamlFiles(path));
|
||||
if (entry.isFile && /\.ya?ml$/.test(entry.name)) result.push(path);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Deno.test("all committed YAML parses", async () => {
|
||||
const files = await yamlFiles();
|
||||
assert(files.length > 0);
|
||||
for (const file of files) await yaml(file);
|
||||
});
|
||||
|
||||
Deno.test("the action exposes only the master key and scopes it to preparation", async () => {
|
||||
const action = await yaml("action.yml");
|
||||
assertEquals(Object.keys(mapping(action.inputs, "inputs")), ["master-key"]);
|
||||
const steps = sequence(mapping(action.runs, "runs").steps, "runs.steps");
|
||||
for (const step of steps) {
|
||||
if (typeof step.run === "string") {
|
||||
assertEquals(step.run.includes("\n"), false, String(step.name));
|
||||
}
|
||||
}
|
||||
const checkout = steps.find((step) => step.uses === "actions/checkout@v4");
|
||||
const prepare = steps.find((step) => step.id === "prepare");
|
||||
const agent = steps.find((step) => step.id === "agent");
|
||||
const post = steps.find((step) => step.id === "post");
|
||||
const source = steps.find((step) => step.id === "source");
|
||||
assert(checkout && source && prepare && agent && post);
|
||||
assertEquals(
|
||||
mapping(checkout.with, "checkout.with").token,
|
||||
"${{ gitea.token }}",
|
||||
);
|
||||
assertEquals(
|
||||
mapping(prepare.env, "prepare.env").BOT_AGENTS_MASTER_KEY,
|
||||
"${{ inputs.master-key }}",
|
||||
);
|
||||
assertEquals(JSON.stringify(agent).includes("BOT_AGENTS_MASTER_KEY"), false);
|
||||
assertEquals(JSON.stringify(post).includes("BOT_AGENTS_MASTER_KEY"), false);
|
||||
assertEquals(
|
||||
mapping(agent.env, "agent.env").GITEA_TOKEN,
|
||||
"${{ gitea.token }}",
|
||||
);
|
||||
assert(String(post.if).includes("always()"));
|
||||
for (const step of [prepare, agent, post]) {
|
||||
assert(String(step.run).includes("steps.source.outputs.path"));
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("the source protection script stages immutable action files", async () => {
|
||||
const temp = await Deno.makeTempDir();
|
||||
const outputFile = `${temp}/output`;
|
||||
try {
|
||||
const output = await new Deno.Command("/bin/bash", {
|
||||
args: ["scripts/protect-source.sh"],
|
||||
env: {
|
||||
ACTION_PATH: Deno.cwd(),
|
||||
GITHUB_OUTPUT: outputFile,
|
||||
RUNNER_TEMP: temp,
|
||||
},
|
||||
stderr: "piped",
|
||||
}).output();
|
||||
assert(output.success, new TextDecoder().decode(output.stderr));
|
||||
|
||||
const path = (await Deno.readTextFile(outputFile)).trim().slice(5);
|
||||
for (
|
||||
const file of [
|
||||
"src/prepare.ts",
|
||||
"src/run.ts",
|
||||
"src/post.ts",
|
||||
"profiles.yml",
|
||||
"prompt.md",
|
||||
]
|
||||
) {
|
||||
const stat = await Deno.stat(`${path}/${file}`);
|
||||
assert(stat.isFile, file);
|
||||
assertEquals(stat.mode! & 0o022, 0, file);
|
||||
}
|
||||
} finally {
|
||||
await Deno.remove(temp, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("the repository uses its trusted main action", async () => {
|
||||
const workflow = await yaml(".gitea/workflows/agents.yml");
|
||||
const job = mapping(mapping(workflow.jobs, "jobs").agent, "jobs.agent");
|
||||
const permissions = mapping(job.permissions, "jobs.agent.permissions");
|
||||
assertEquals(permissions, {
|
||||
code: "write",
|
||||
issues: "write",
|
||||
"pull-requests": "write",
|
||||
});
|
||||
const steps = sequence(job.steps, "jobs.agent.steps");
|
||||
const local = steps.findIndex((step) => step.uses === "./");
|
||||
assertEquals(local, -1);
|
||||
const trusted = steps.findIndex((step) =>
|
||||
step.uses === "https://git.capsulizers.com/commons/bot-agents@main"
|
||||
);
|
||||
assert(trusted >= 0);
|
||||
assertEquals(
|
||||
mapping(steps[trusted].with, "trusted.with")["master-key"],
|
||||
"${{ secrets.BOT_AGENTS_MASTER_KEY }}",
|
||||
);
|
||||
assertEquals(JSON.stringify(workflow).includes("gitea.actor != 'bot'"), true);
|
||||
});
|
||||
|
||||
Deno.test("CI checks every phase and the root isolation suite", async () => {
|
||||
const workflow = await yaml(".gitea/workflows/check.yml");
|
||||
const job = mapping(mapping(workflow.jobs, "jobs").deno, "jobs.deno");
|
||||
const commands = sequence(job.steps, "jobs.deno.steps")
|
||||
.map((step) => typeof step.run === "string" ? step.run : "")
|
||||
.join("\n");
|
||||
for (const entrypoint of ["src/prepare.ts", "src/run.ts", "src/post.ts"]) {
|
||||
assert(commands.includes(entrypoint), entrypoint);
|
||||
}
|
||||
assert(commands.includes("deno test --allow-all"));
|
||||
});
|
||||
|
||||
Deno.test("uses the standard Skills installer without custom discovery", async () => {
|
||||
const runner = await Deno.readTextFile("src/run.ts");
|
||||
assert(runner.includes("skills add obra/superpowers"));
|
||||
try {
|
||||
await Deno.stat("superpowers.ts");
|
||||
assert(false, "superpowers.ts should be removed");
|
||||
} catch (error) {
|
||||
assert(error instanceof Deno.errors.NotFound);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user