mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 21:03:47 -10:00
Compare commits
1
Commits
claude-turn-end
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06a48730d1 |
No files matched your search
+17
-10
@@ -34,7 +34,7 @@
|
||||
// Every route but /healthz needs RAEA_CLOUD_TOKEN: `Authorization: Bearer TOKEN`, or ?token= (a browser's WebSocket
|
||||
// can't set headers). The agent and raea get the container's environment without the token or the state keys.
|
||||
|
||||
import { jobsOf, watchTurnEnd } from "./turn-end.ts"
|
||||
import { claudeStopHook, jobsOf, markedEnd, watchEnd, watchTurnEnd } from "./turn-end.ts"
|
||||
import { execFileSync, spawn } from "node:child_process"
|
||||
import { timingSafeEqual } from "node:crypto"
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, symlinkSync, writeFileSync } from "node:fs"
|
||||
@@ -417,15 +417,22 @@ server.on("upgrade", (req, socket, head) => {
|
||||
ws.on("message", (m) => ((heard = heardFromCaller = Date.now()), child.stdin.write(m.toString() + "\n")))
|
||||
// the turn's end from the agent's own transcript, said along the socket (turn-end.ts): raea cloud closes the turn
|
||||
// on it without waiting for an answer to session/prompt that may not come
|
||||
if (agent.id === "antigravity-acp" && env.GEMINI_HOME) {
|
||||
const stopWatch = watchTurnEnd(
|
||||
env.GEMINI_HOME,
|
||||
() => heardFromCaller,
|
||||
(by) => ws.readyState === ws.OPEN && ws.send(JSON.stringify({ jsonrpc: "2.0", method: "_raea/turn_end", params: { by } })),
|
||||
() => !!child.pid && jobsOf(child.pid, path.dirname(agent.command), WORK).length > 0,
|
||||
)
|
||||
ws.on("close", stopWatch)
|
||||
child.on("exit", stopWatch)
|
||||
{
|
||||
const tellEnd = (by: string) => ws.readyState === ws.OPEN && ws.send(JSON.stringify({ jsonrpc: "2.0", method: "_raea/turn_end", params: { by } }))
|
||||
const busy = () => !!child.pid && jobsOf(child.pid, path.dirname(agent.command), WORK).length > 0
|
||||
let stopWatch: (() => void) | undefined
|
||||
if (agent.id === "antigravity-acp" && env.GEMINI_HOME) stopWatch = watchTurnEnd(env.GEMINI_HOME, () => heardFromCaller, tellEnd, busy)
|
||||
else if (agent.id === "claude-acp" && own && env.CLAUDE_CONFIG_DIR) {
|
||||
// Claude says it itself, through a Stop hook in the socket's own settings (never the box's own home's)
|
||||
try {
|
||||
const mark = claudeStopHook(env.CLAUDE_CONFIG_DIR)
|
||||
stopWatch = watchEnd(() => markedEnd(mark), "its Stop hook", () => heardFromCaller, tellEnd, busy)
|
||||
} catch {}
|
||||
}
|
||||
if (stopWatch) {
|
||||
ws.on("close", stopWatch)
|
||||
child.on("exit", stopWatch)
|
||||
}
|
||||
}
|
||||
ws.on("close", stopAll)
|
||||
child.on("error", (e) => ws.close(1011, `couldn't start ${agent.id}: ${e.message}`.slice(0, 120)))
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
//
|
||||
// Antigravity keeps a transcript of each session, a JSON line a step. A model step that finished and called no tool is
|
||||
// the end of its turn only when nothing it started still runs: it ends a step the same way to wait on a render it left
|
||||
// in the background (123 of 131 such steps in one StreetTalk run), and is woken when that finishes. So both are read:
|
||||
// in the background (123 of 131 such steps in one run), and is woken when that finishes. So both are read:
|
||||
// the transcript's last line, and the processes that are the agent's own.
|
||||
import { closeSync, openSync, readdirSync, readFileSync, readlinkSync, readSync, statSync } from "node:fs"
|
||||
import { closeSync, openSync, readdirSync, readFileSync, readlinkSync, readSync, statSync, writeFileSync } from "node:fs"
|
||||
import path from "node:path"
|
||||
|
||||
/** the step a transcript line ends a turn at, and when it was made (ms); null for any other line */
|
||||
@@ -105,23 +105,61 @@ export function jobsOf(pid: number, agentDir: string, work: string, proc = "/pro
|
||||
.map((p) => p.pid)
|
||||
}
|
||||
|
||||
/** Watches an Antigravity agent's transcript and calls `said` once for each turn that ends after `told()` (when the
|
||||
* agent was last sent anything: a turn that ended before its newest prompt isn't this one's), once `busy()` has been
|
||||
/** Claude Code's own word that its turn ended: a Stop hook in the agent's settings (CLAUDE_CONFIG_DIR/settings.json)
|
||||
* that touches a file here. Other settings and hooks already there are kept. Returns the file. */
|
||||
export function claudeStopHook(configDir: string): string {
|
||||
const mark = path.join(configDir, ".raea-turn-end")
|
||||
const file = path.join(configDir, "settings.json")
|
||||
let settings: any = {}
|
||||
try {
|
||||
settings = JSON.parse(readFileSync(file, "utf8"))
|
||||
} catch {}
|
||||
if (!settings || typeof settings !== "object" || Array.isArray(settings)) settings = {}
|
||||
const command = `touch '${mark.replace(/'/g, "'\\''")}'`
|
||||
const hooks = (settings.hooks ??= {})
|
||||
const stop: any[] = Array.isArray(hooks.Stop) ? hooks.Stop : (hooks.Stop = [])
|
||||
if (!stop.some((g) => Array.isArray(g?.hooks) && g.hooks.some((h: any) => h?.command === command))) {
|
||||
stop.push({ hooks: [{ type: "command", command }] })
|
||||
writeFileSync(file, `${JSON.stringify(settings, null, 2)}\n`)
|
||||
}
|
||||
return mark
|
||||
}
|
||||
|
||||
/** when a Stop hook's file was last touched, as a turn's end; null before the first */
|
||||
export function markedEnd(mark: string): { step: number; at: number } | null {
|
||||
try {
|
||||
const at = statSync(mark).mtimeMs
|
||||
return { step: at, at }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** Calls `said` once for each turn's end that `ended` reads (an agent's own record of it) after `told()` (when the
|
||||
* agent was last sent anything: an end from before its newest prompt isn't this turn's), once `busy()` has been
|
||||
* false two looks running. Returns its stop. */
|
||||
export function watchTurnEnd(geminiHome: string, told: () => number, said: (by: string) => void, busy: () => boolean = () => false, everyMs = 1000): () => void {
|
||||
export function watchEnd(ended: () => { step: number; at: number } | null, by: string, told: () => number, said: (by: string) => void, busy: () => boolean = () => false, everyMs = 1000): () => void {
|
||||
let seen = -1
|
||||
let idle = 0
|
||||
const t = setInterval(() => {
|
||||
try {
|
||||
const tr = newestTranscript(geminiHome)
|
||||
const end = tr ? turnEndOf(lastLine(tr.file)) : null
|
||||
// a second's slack: the transcript's times are whole seconds
|
||||
const end = ended()
|
||||
// a second's slack: a transcript's times are whole seconds
|
||||
if (!end || end.step === seen || end.at < told() - 1000 || busy()) return void (idle = 0)
|
||||
if (++idle < 2) return
|
||||
seen = end.step
|
||||
said("its transcript, and nothing of its own running")
|
||||
said(`${by}, and nothing of its own running`)
|
||||
} catch {}
|
||||
}, everyMs)
|
||||
t.unref?.()
|
||||
return () => clearInterval(t)
|
||||
}
|
||||
|
||||
/** watchEnd on an Antigravity agent's transcript */
|
||||
export function watchTurnEnd(geminiHome: string, told: () => number, said: (by: string) => void, busy: () => boolean = () => false, everyMs = 1000): () => void {
|
||||
const ended = () => {
|
||||
const tr = newestTranscript(geminiHome)
|
||||
return tr ? turnEndOf(lastLine(tr.file)) : null
|
||||
}
|
||||
return watchEnd(ended, "its transcript", told, said, busy, everyMs)
|
||||
}
|
||||
+21
-2
@@ -3,13 +3,14 @@
|
||||
// paths read on the worker)
|
||||
|
||||
import assert from "node:assert/strict"
|
||||
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"
|
||||
import { execSync } from "node:child_process"
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { parseSummary, pick } from "../server/gpu.ts"
|
||||
import { plan, projectRoot } from "../server/offload.ts"
|
||||
import { shellEnv } from "../server/shell-os.ts"
|
||||
import { jobsOf, lastLine, newestTranscript, turnEndOf, watchTurnEnd } from "../server/turn-end.ts"
|
||||
import { claudeStopHook, jobsOf, lastLine, markedEnd, newestTranscript, turnEndOf, watchEnd, watchTurnEnd } from "../server/turn-end.ts"
|
||||
import { landedQuery, linkIn, packCredential, unpackCredential } from "../server/google-token.ts"
|
||||
|
||||
const L4 = `Devices:
|
||||
@@ -157,6 +158,24 @@ console.log("units: ok")
|
||||
put(200, 101, 100, "/opt/raea/bin/raea", "raea render edit.tsx")
|
||||
put(300, 1, 300, "/usr/bin/ffmpeg", "ffmpeg -i a.mp4", "/srv/work/threads/r1/out")
|
||||
assert.deepEqual(jobsOf(100, "/opt/raea-cloud/agents/antigravity-acp", "/srv/work", proc).sort(), [200, 300])
|
||||
// Claude's Stop hook: added to the settings there once, what was there kept; its file's touch is the turn's end
|
||||
const cfg = path.join(home, "claude")
|
||||
mkdirSync(cfg, { recursive: true })
|
||||
writeFileSync(path.join(cfg, "settings.json"), JSON.stringify({ model: "opus", hooks: { Stop: [{ hooks: [{ type: "command", command: "true" }] }] } }))
|
||||
const mark = claudeStopHook(cfg)
|
||||
claudeStopHook(cfg)
|
||||
const set = JSON.parse(readFileSync(path.join(cfg, "settings.json"), "utf8"))
|
||||
assert.equal(set.model, "opus")
|
||||
assert.deepEqual(set.hooks.Stop.map((g: any) => g.hooks[0].command), ["true", `touch '${mark}'`])
|
||||
assert.equal(markedEnd(mark), null)
|
||||
const heard: string[] = []
|
||||
const stop2 = watchEnd(() => markedEnd(mark), "its Stop hook", () => Date.now() - 500, (by) => heard.push(by), () => false, 10)
|
||||
await new Promise((ok) => setTimeout(ok, 40))
|
||||
assert.deepEqual(heard, [])
|
||||
execSync(set.hooks.Stop[1].hooks[0].command)
|
||||
await new Promise((ok) => setTimeout(ok, 80))
|
||||
stop2()
|
||||
assert.deepEqual(heard, ["its Stop hook, and nothing of its own running"])
|
||||
rmSync(home, { recursive: true, force: true })
|
||||
console.log("ok turn-end")
|
||||
}
|
||||
Reference in new issue
Block a user