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:
+103
@@ -0,0 +1,103 @@
|
||||
import { join } from "jsr:@std/path@1";
|
||||
import { parseCredentials } from "./credentials.ts";
|
||||
import type { BotProfile } from "./profiles.ts";
|
||||
|
||||
const FIRST_PROFILE_UID = 20_000;
|
||||
|
||||
export type RuntimeProfile = Pick<BotProfile, "type" | "model"> & {
|
||||
index: number;
|
||||
uid: number;
|
||||
gid: number;
|
||||
home: string;
|
||||
tokenFile: string | null;
|
||||
};
|
||||
|
||||
export type RuntimeManifest = {
|
||||
runtimeDir: string;
|
||||
manifestPath: string;
|
||||
reviewerFile: string;
|
||||
outcomeFile: string;
|
||||
profiles: RuntimeProfile[];
|
||||
};
|
||||
|
||||
export type RuntimeOutcome =
|
||||
| {
|
||||
ok: true;
|
||||
body: string;
|
||||
reviewPath: string;
|
||||
anchorsPath: string;
|
||||
}
|
||||
| { ok: false };
|
||||
|
||||
async function writePrivate(
|
||||
path: string,
|
||||
value: string,
|
||||
uid: number,
|
||||
gid: number,
|
||||
): Promise<void> {
|
||||
await Deno.writeTextFile(path, value, { mode: 0o600 });
|
||||
await Deno.chmod(path, 0o600);
|
||||
await Deno.chown(path, uid, gid);
|
||||
}
|
||||
|
||||
export async function prepareRuntime(
|
||||
actionPath: string,
|
||||
runtimeRoot: string,
|
||||
masterKey: string,
|
||||
): Promise<RuntimeManifest> {
|
||||
const credentials = await parseCredentials(
|
||||
await Deno.readTextFile(join(actionPath, "profiles.yml")),
|
||||
masterKey,
|
||||
);
|
||||
const runtimeDir = await Deno.makeTempDir({
|
||||
dir: runtimeRoot,
|
||||
prefix: "bot-agents-",
|
||||
});
|
||||
try {
|
||||
await Deno.chmod(runtimeDir, 0o711);
|
||||
const reviewerFile = join(runtimeDir, "reviewer-token");
|
||||
await writePrivate(reviewerFile, credentials.reviewerToken, 0, 0);
|
||||
|
||||
const profiles: RuntimeProfile[] = [];
|
||||
for (const [index, profile] of credentials.profiles.entries()) {
|
||||
const uid = FIRST_PROFILE_UID + index;
|
||||
const home = join(runtimeDir, `profile-${index}`);
|
||||
await Deno.mkdir(home, { mode: 0o700 });
|
||||
await Deno.chmod(home, 0o700);
|
||||
await Deno.chown(home, uid, uid);
|
||||
const tokenFile = profile.token ? join(home, "token") : null;
|
||||
if (tokenFile !== null) {
|
||||
await writePrivate(tokenFile, profile.token!, uid, uid);
|
||||
}
|
||||
profiles.push({
|
||||
index,
|
||||
type: profile.type,
|
||||
model: profile.model,
|
||||
uid,
|
||||
gid: uid,
|
||||
home,
|
||||
tokenFile,
|
||||
});
|
||||
}
|
||||
|
||||
const manifestPath = join(runtimeDir, "manifest.json");
|
||||
const manifest: RuntimeManifest = {
|
||||
runtimeDir,
|
||||
manifestPath,
|
||||
reviewerFile,
|
||||
outcomeFile: join(runtimeDir, "outcome.json"),
|
||||
profiles,
|
||||
};
|
||||
await writePrivate(manifestPath, JSON.stringify(manifest), 0, 0);
|
||||
return manifest;
|
||||
} catch (error) {
|
||||
await Deno.remove(runtimeDir, { recursive: true }).catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function readRuntimeManifest(
|
||||
path: string,
|
||||
): Promise<RuntimeManifest> {
|
||||
return JSON.parse(await Deno.readTextFile(path));
|
||||
}
|
||||
Reference in New Issue
Block a user