diff --git a/sdk/cloud/server/main.ts b/sdk/cloud/server/main.ts index 99f25a45..70a14b89 100644 --- a/sdk/cloud/server/main.ts +++ b/sdk/cloud/server/main.ts @@ -35,7 +35,7 @@ // can't set headers). The agent and raea get the container's environment without the token or the state keys. import { claudeStopHook, jobsOf, markedEnd, sessionIn, watchEnd, watchTurnEnd } from "./turn-end.ts" -import { execFileSync, spawn } from "node:child_process" +import { execFile, spawn } from "node:child_process" import { timingSafeEqual } from "node:crypto" import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, symlinkSync, writeFileSync } from "node:fs" import { createServer, type IncomingMessage, type ServerResponse } from "node:http" @@ -144,14 +144,16 @@ const withoutKeys = (env: NodeJS.ProcessEnv) => { for (const k of [...AGENT_KEYS, "GOOGLE_API_KEY", "GOOGLE_APPLICATION_CREDENTIALS"]) delete out[k] return out } -const raeaVersion = () => { - try { - version ??= execFileSync(RAEA, ["--version"], { encoding: "utf8", timeout: 20_000, env: childEnv() }).trim() - } catch { - version = "unknown" - } - return version -} +/** raea's version, asked once as the server starts and never on a request: asked synchronously, on a Sprite whose + * disk was cold it held every request (health included) for 6 s, and up to its 20 s timeout after the restarts on + * 12-02, which also left it "unknown" for good. "reading" until it answers */ +const raeaVersion = () => version ?? "reading" +const versionKnown = new Promise((resolve) => + execFile(RAEA, ["--version"], { encoding: "utf8", timeout: 120_000, env: childEnv() }, (e, out) => { + version = e ? "unknown" : out.trim() + resolve() + }), +) function authorized(req: IncomingMessage, url: URL): boolean { const given = /^Bearer (.+)$/.exec(req.headers.authorization ?? "")?.[1] ?? url.searchParams.get("token") ?? "" @@ -252,6 +254,8 @@ const server = createServer(async (req, res) => { if (!authorized(req, url)) return json(res, { error: "unauthorized" }, 401) try { if (url.pathname === "/v1/health" && req.method === "GET") { + // a moment for raea's version on a fresh start, without holding anything else up + if (version === undefined) await Promise.race([versionKnown, new Promise((r) => setTimeout(r, 3000))]) const def = (() => { try { return spawnFor().id @@ -467,7 +471,8 @@ if (existsSync("/proc/meminfo") && process.env.RAEA_CLOUD_MEMGUARD !== "0") process.stderr.write(line + "\n") for (const ws of agentSockets) if (ws.readyState === ws.OPEN) ws.send(JSON.stringify({ jsonrpc: "2.0", method: "_raea/stderr", params: { text: line + "\n" } })) }) -server.listen(PORT, () => { +server.listen(PORT, async () => { + await versionKnown const d = device(childEnv()) const gpu = process.env.RAEA_CLOUD_GPU_URL console.log(