Compare commits

...
1 Commits
Author SHA1 Message Date
shinyobjectz ad26a15efe The box server keeps a quiet stream open past Cloudflare's 100 s, and ends an agent silent both ways for 45 min
/v1/shell and /v1/run sent no headers until their command printed: raea goal check --json over Dutch's seven cuts
printed nothing for over 100 s, Cloudflare answered 524, and raea cloud's gate sent its Client sign-off stop back as
"raea goal check couldn't run". Their headers now go at once, then an empty line ({}) every 30 s, which readers skip.

An ACP socket whose caller went away without closing it (an API restart, the Sprite's proxy keeping the box's half
open) kept its agent forever: two Gemini agents sat on raea-3 for 2 h, counted as running, so box.sh latest would wait
on them. An agent that has said nothing and been sent nothing for 45 min (RAEA_CLOUD_IDLE_AGENT_MS; raea cloud ends a
turn quiet 30 min) is now ended with its process group.

Checked locally (a 32 s silent command gets {} at 30 s; an idle echo agent's socket is closed at the limit) and on
raea-3 through its public URL's stack.
2026-11-30 02:01:20 -08:00
+33 -5
View File
@@ -185,12 +185,26 @@ function files(req: IncomingMessage, res: ServerResponse, url: URL) {
res.writeHead(200, { "content-type": "application/octet-stream", "content-length": st.size }).end(readFileSync(p))
}
/** An NDJSON answer's headers sent at once, and an empty line ({}) every KEEPALIVE_MS until it ends: the box sits
* behind Cloudflare, which answers 524 when nothing comes back for 100 s, and `raea goal check --json` over seven
* cuts prints nothing until it's done (11-30, Dutch's stop at Client sign-off was sent back on it). Readers skip a
* line with no out, err or exit. */
const KEEPALIVE_MS = 30_000
function streamOpen(res: ServerResponse) {
res.writeHead(200, { "content-type": "application/x-ndjson" })
res.flushHeaders()
const beat = setInterval(() => res.write("{}\n"), KEEPALIVE_MS)
const stop = () => clearInterval(beat)
res.on("finish", stop)
res.on("close", stop)
}
async function run(req: IncomingMessage, res: ServerResponse) {
const { args, cwd } = JSON.parse((await body(req)).toString() || "{}") as { args?: unknown; cwd?: string }
if (!Array.isArray(args) || !args.every((a) => typeof a === "string")) return json(res, { error: "args must be an array of strings: raea's own arguments" }, 400)
const dir = resolve(cwd ?? ".")
mkdirSync(dir, { recursive: true })
res.writeHead(200, { "content-type": "application/x-ndjson" })
streamOpen(res)
const child = spawn(RAEA, args, { cwd: dir, env: childEnv(), stdio: ["ignore", "pipe", "pipe"] })
const line = (v: unknown) => res.write(JSON.stringify(v) + "\n")
child.stdout.setEncoding("utf8").on("data", (out: string) => line({ out }))
@@ -202,6 +216,8 @@ async function run(req: IncomingMessage, res: ServerResponse) {
}
const SHELL_DEFAULT_MS = 600_000
/** an ACP agent silent both ways this long is ended (the /v1/acp socket below) */
const IDLE_AGENT_MS = Number(process.env.RAEA_CLOUD_IDLE_AGENT_MS ?? 45 * 60_000)
const SHELL_MAX_MS = 3_600_000
async function shell(req: IncomingMessage, res: ServerResponse) {
@@ -210,7 +226,7 @@ async function shell(req: IncomingMessage, res: ServerResponse) {
const ms = typeof timeout_ms === "number" && timeout_ms > 0 ? Math.min(timeout_ms, SHELL_MAX_MS) : SHELL_DEFAULT_MS
const dir = resolve(cwd ?? ".")
mkdirSync(dir, { recursive: true })
res.writeHead(200, { "content-type": "application/x-ndjson" })
streamOpen(res)
// the shell on any OS: bash and a process group here, RAEA_CLOUD_BASH and taskkill on Windows (shell-os.ts)
const { bash, groupOpts, shellEnv, stopTree } = await import("./shell-os.ts")
// in a process group of its own, so a timeout or a caller leaving stops what the command started too
@@ -370,8 +386,19 @@ server.on("upgrade", (req, socket, head) => {
// "socket closed" ends came 114-125 s into a silence). raea cloud skips notifications it doesn't know; the agent
// never sees it
const beat = setInterval(() => ws.readyState === ws.OPEN && ws.send('{"jsonrpc":"2.0","method":"_raea/keepalive"}'), 20_000)
ws.on("close", () => (agentSockets.delete(ws), clearInterval(beat)))
child.on("exit", () => running.delete(child))
// an agent that has said nothing and been sent nothing for IDLE_AGENT_MS is ended: raea cloud ends a turn whose
// agent is quiet 30 min (acp-run.ts QUIET_TOOL_MS), and a socket whose caller went away without closing it (an API
// restart; the Sprite's proxy kept the box's half open) otherwise kept its agent forever (11-30: two Gemini agents
// on raea-3 for 2 h, counted as running, so no box update could go on)
let heard = Date.now()
const idle = setInterval(() => {
if (Date.now() - heard < IDLE_AGENT_MS) return
process.stderr.write(`raea-cloud: ${agent.id} said nothing and was sent nothing for ${Math.round(IDLE_AGENT_MS / 60_000)} min: ended\n`)
ws.terminate()
stopAll()
}, Math.min(60_000, IDLE_AGENT_MS / 2))
ws.on("close", () => (agentSockets.delete(ws), clearInterval(beat), clearInterval(idle)))
child.on("exit", () => (running.delete(child), clearInterval(idle)))
// its stderr to the box's log as before, and along the socket as _raea/stderr notifications, so raea cloud keeps
// it with the run's trace (runtrace.ts) instead of only on this box's disk (11-25: raea-1's logs died with it)
child.stderr.setEncoding("utf8").on("data", (c: string) => {
@@ -381,10 +408,11 @@ server.on("upgrade", (req, socket, head) => {
let buf = ""
// ACP messages are newline-delimited JSON: split on \n only
child.stdout.setEncoding("utf8").on("data", (c: string) => {
heard = Date.now()
buf += c
for (let i; (i = buf.indexOf("\n")) >= 0; buf = buf.slice(i + 1)) if (i > 0) ws.send(buf.slice(0, i))
})
ws.on("message", (m) => child.stdin.write(m.toString() + "\n"))
ws.on("message", (m) => ((heard = Date.now()), child.stdin.write(m.toString() + "\n")))
ws.on("close", stopAll)
child.on("error", (e) => ws.close(1011, `couldn't start ${agent.id}: ${e.message}`.slice(0, 120)))
child.on("exit", (code) => (ws.close(1011, `${agent.id} exited ${code}`), stopAll()))