import fs from "node:fs"; import os from "node:os"; import { agent } from "@crosscode/contracts"; import { generateEd25519, generateX25519 } from "@crosscode/contracts/signing"; import { configFile, ed25519File, loadConfig, saveConfig, writePrivate, x25519File } from "./config"; import { hubClient, postUnsigned } from "./hub"; import { Agent } from "./loop"; import { AGENT_VERSION } from "./stats"; /** * crosscode-agent (AGENT-1.0-PLAN §3) * crosscode-agent join --hub https://hub.crosscode.co.za --token [--central] * crosscode-agent run what systemd runs (crosscode-agent.service) * crosscode-agent version */ const USAGE = `crosscode-agent ${AGENT_VERSION} crosscode-agent join --hub --token [--central] [--force] Join this server to the Hub with a one-time token (Hub → Servers → Add server). Makes the server's key pairs; the private keys never leave it. crosscode-agent run poll the Hub for jobs and send stats (run by systemd) crosscode-agent version `; function flag(args: string[], name: string): string | undefined { const i = args.indexOf(name); return i >= 0 ? args[i + 1] : undefined; } export async function join(args: string[]): Promise { const hub = flag(args, "--hub"); const token = flag(args, "--token"); const mode: agent.AgentMode = args.includes("--central") ? "central" : "church"; if (!hub || !/^https?:\/\//.test(hub)) throw new Error("--hub is required (https://hub.crosscode.co.za)."); if (!token) throw new Error("--token is required (Hub → Servers → Add server)."); if (fs.existsSync(configFile()) && !args.includes("--force")) { throw new Error(`This server has already joined (${configFile()}). Use --force to join again (the Hub then sees a new server).`); } const ed = generateEd25519(); const x = generateX25519(); const reply = agent.joinResponseSchema.parse(await postUnsigned(hub, "/api/agent/v1/join", { agentApiVersion: agent.AGENT_API_VERSION, token, mode, hostname: os.hostname().slice(0, 253), agentVersion: AGENT_VERSION, publicKeys: { ed25519: ed.publicKey, x25519: x.publicKey }, })); writePrivate(ed25519File(), ed.privateKeyPem); writePrivate(x25519File(), x.privateKeyPem); saveConfig({ hubUrl: hub, serverId: reply.serverId, mode, hubJobKey: reply.hubJobKey, pollSeconds: reply.pollSeconds, statsSeconds: reply.statsSeconds, joinedAt: new Date().toISOString() }); console.log(`Joined the Hub as ${reply.serverId} (${mode}). Start the agent: systemctl enable --now crosscode-agent`); } export async function main(argv = process.argv.slice(2)): Promise { const [command, ...args] = argv; switch (command) { case "version": case "--version": console.log(AGENT_VERSION); return; case "join": await join(args); return; case "run": { if (process.getuid && process.getuid() !== 0 && !process.env.CROSSCODE_AGENT_ALLOW_NON_ROOT) { throw new Error("Run the agent as root (it runs crosscode, which needs root)."); } const { config, keys } = loadConfig(); const a = new Agent({ config, keys, hub: hubClient(config, keys) }); for (const sig of ["SIGTERM", "SIGINT"] as const) process.on(sig, () => { a.stop(); setTimeout(() => process.exit(0), 1000).unref(); }); await a.start(); return; } default: process.stdout.write(USAGE); if (command && command !== "help" && command !== "--help") process.exitCode = 1; } } if (require.main === module) { main().catch((e) => { console.error(`crosscode-agent: ${(e as Error).message}`); process.exit(1); }); }