mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 06:53:19 -10:00
Compare commits
1
Commits
v0.1.13
...
box-keepalive
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad26a15efe |
@@ -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()))
|
||||
|
||||
Reference in new issue
Block a user