spb/autoscholar
Public
1import { execFileSync } from "node:child_process";2import { readFileSync } from "node:fs";3import { createRequire } from "node:module";4import { arch, cpus, hostname, platform, release, totalmem, userInfo } from "node:os";5import type { NodeMetadata } from "@autoscholar/schemas";6import { WORKER_PROTOCOL_VERSION } from "@autoscholar/schemas";78const require = createRequire(import.meta.url);910function tryExec(cmd: string, args: string[]): string | undefined {11 try {12 return execFileSync(cmd, args, {13 encoding: "utf8",14 timeout: 8000,15 stdio: ["ignore", "pipe", "ignore"],16 }).trim();17 } catch {18 return undefined;19 }20}2122export function sdkVersion(): string {23 try {24 return (require("@anthropic-ai/claude-agent-sdk/package.json") as { version: string }).version;25 } catch {26 return "unknown";27 }28}2930export function workerVersion(): string {31 try {32 return (33 JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")) as { version: string }34 ).version;35 } catch {36 return "0.0.0";37 }38}3940export function machineId(): string {41 const ioreg = tryExec("/usr/sbin/ioreg", ["-rd1", "-c", "IOPlatformExpertDevice"]);42 const m = ioreg?.match(/"IOPlatformUUID" = "([^"]+)"/);43 return m?.[1] ?? `${hostname()}-${arch()}`;44}4546export function collectNodeMetadata(opts: { simulated: boolean; profiles: string[] }): NodeMetadata {47 const tools: Record<string, string> = {};48 const probe = (name: string, cmd: string, args: string[]) => {49 const v = tryExec(cmd, args);50 if (v) tools[name] = v.split("\n")[0]!.slice(0, 80);51 };52 probe("node", "node", ["--version"]);53 probe("python3", "/opt/homebrew/bin/python3", ["--version"]);54 probe("uv", "/opt/homebrew/bin/uv", ["--version"]);55 probe("tectonic", process.env.TECTONIC_BIN ?? "/opt/homebrew/bin/tectonic", ["--version"]);56 probe("pdftoppm", "/opt/homebrew/bin/pdftoppm", ["-v"]);57 probe("git", "git", ["--version"]);58 probe("R", "/opt/homebrew/bin/Rscript", ["--version"]);59 probe("julia", "/opt/homebrew/bin/julia", ["--version"]);60 tools["claude-agent-sdk"] = sdkVersion();61 const sandboxAvailable =62 platform() === "darwin"63 ? !!tryExec("/usr/bin/sandbox-exec", ["-n", "no-network", "/usr/bin/true"]) ||64 tryExec("/bin/ls", ["/usr/bin/sandbox-exec"]) === "/usr/bin/sandbox-exec"65 : false;66 return {67 machineId: machineId(),68 hostname: hostname(),69 platform: platform(),70 arch: arch(),71 cpuCores: cpus().length,72 memoryBytes: totalmem(),73 osVersion: tryExec("/usr/bin/sw_vers", ["-productVersion"]) ?? release(),74 workerVersion: workerVersion(),75 protocolVersion: WORKER_PROTOCOL_VERSION,76 sdkVersion: sdkVersion(),77 tools,78 profiles: opts.profiles,79 user: userInfo().username,80 sandboxAvailable: opts.simulated ? false : sandboxAvailable,81 ...(opts.simulated ? { simulated: true } : {}),82 } as NodeMetadata;83}8485export function loadAverage(): { load1: number; memFreeBytes: number; diskFreeBytes: number } {86 const load1 = (() => {87 try {88 const { loadavg } = require("node:os") as typeof import("node:os");89 return loadavg()[0] ?? 0;90 } catch {91 return 0;92 }93 })();94 const { freemem } = require("node:os") as typeof import("node:os");95 let diskFreeBytes = 0;96 const df = tryExec("/bin/df", ["-k", process.env.WORKER_ROOT ?? "/"]);97 const line = df?.split("\n")[1];98 if (line) diskFreeBytes = Number(line.split(/\s+/)[3] ?? 0) * 1024;99 return { load1, memFreeBytes: freemem(), diskFreeBytes };100}101