Compare commits

..
Author SHA1 Message Date
shinyobjectz fb0e78fccb Cloud: a turn's end is read from the agent's own session, not the newest one on its sign-in
Agents on one sign-in share a home on the Sprite. The watcher read the home's newest transcript, so one chat's last
message ended every other turn running beside it, with nothing said: three of ten chats run two at a time ended
mid-work. The transcript is now the one of the ACP session the socket is on, read off its own messages, and nothing is
said before a session is known. Claude's Stop hook file is shared the same way: while two agents on one sign-in are
open, it is not read.
2026-12-02 00:27:41 -08:00
4 changed files with 76 additions and 32 deletions

No files matched your search

-6
View File
@@ -21,12 +21,6 @@ say packages
sudo -E apt-get update -qq
sudo -E apt-get install -y -qq --no-install-recommends ca-certificates curl git git-lfs zstd xz-utils procps ripgrep \
libasound2t64 libfontconfig1 libvulkan1 mesa-vulkan-drivers vulkan-tools > /dev/null
# what every agent reached for and installed itself at each project's start (12-02: `which ffmpeg`, a search of the
# disk, then apt, minutes in all): ffmpeg and ffprobe (the montage check reads frames with them too) and the PDF tools a
# brief or brand guide is read with
for f in ffmpeg poppler-utils; do
dpkg -s "$f" > /dev/null 2>&1 || sudo -E apt-get install -y -qq --no-install-recommends "$f" > /dev/null 2>&1 || say "no $f here"
done
# faces an ad is set in (12-01: a box had Liberation and DejaVu alone, so every cut fell back to Liberation Sans): the
# open families brands are closest to, each taken when this Ubuntu has it
for f in fonts-inter fonts-roboto fonts-open-sans fonts-lato fonts-montserrat fonts-noto-core fonts-ibm-plex fonts-bebas-neue fonts-urw-base35 fonts-liberation2; do
+24 -5
View File
@@ -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 { claudeStopHook, jobsOf, markedEnd, watchEnd, watchTurnEnd } from "./turn-end.ts"
import { claudeStopHook, jobsOf, markedEnd, sessionIn, 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"
@@ -333,6 +333,8 @@ const server = createServer(async (req, res) => {
// the WebSocket upgrade checks the token before a socket opens
const wss = new WebSocketServer({ noServer: true })
/** how many open agents share each Claude Stop hook file (turn-end.ts) */
const stopMarks = new Map<string, number>()
const agentSockets = new Set<import("ws").WebSocket>()
server.on("upgrade", (req, socket, head) => {
const url = new URL(req.url ?? "/", "http://raea-cloud")
@@ -411,22 +413,39 @@ server.on("upgrade", (req, socket, head) => {
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))
for (let i; (i = buf.indexOf("\n")) >= 0; buf = buf.slice(i + 1)) {
if (i <= 0) continue
const line = buf.slice(0, i)
if (!acpSession || !line.includes(acpSession)) acpSession = sessionIn(line) ?? acpSession
ws.send(line)
}
})
let heardFromCaller = Date.now()
ws.on("message", (m) => ((heard = heardFromCaller = Date.now()), child.stdin.write(m.toString() + "\n")))
// the ACP session this socket is on, read off its own messages: whose transcript is this turn's (turn-end.ts)
let acpSession: string | null = null
ws.on("message", (m) => {
heard = heardFromCaller = Date.now()
const line = m.toString()
if (!acpSession || !line.includes(acpSession)) acpSession = sessionIn(line) ?? acpSession
child.stdin.write(line + "\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
{
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)
if (agent.id === "antigravity-acp" && env.GEMINI_HOME) stopWatch = watchTurnEnd(env.GEMINI_HOME, () => acpSession, () => 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)
// the hook's file is the settings folder's, which every agent on this sign-in shares: while another of
// them is open here, a touch of it is either's turn, so nothing is said (the prompt's answer ends the turn)
stopMarks.set(mark, (stopMarks.get(mark) ?? 0) + 1)
const stop = watchEnd(() => (stopMarks.get(mark) === 1 ? markedEnd(mark) : null), "its Stop hook", () => heardFromCaller, tellEnd, busy)
let left = false
stopWatch = () => { stop(); if (!left) { left = true; stopMarks.set(mark, (stopMarks.get(mark) ?? 1) - 1) } }
} catch {}
}
if (stopWatch) {
+27 -18
View File
@@ -39,24 +39,24 @@ export function lastLine(file: string, most = 1 << 20): string {
return lines.at(-1) ?? ""
}
/** the transcript written to last under an Antigravity home (GEMINI_HOME), or null */
export function newestTranscript(geminiHome: string): { file: string; mtime: number } | null {
const brain = path.join(geminiHome, "antigravity-acp", "brain")
let best: { file: string; mtime: number } | null = null
let names: string[] = []
/** a session's transcript under an Antigravity home (GEMINI_HOME): its folder is named by the ACP session's id */
export function transcriptOf(geminiHome: string, session: string): string | null {
// an id is a name, never a path
if (!/^[A-Za-z0-9_-]+$/.test(session)) return null
return path.join(geminiHome, "antigravity-acp", "brain", session, ".system_generated", "logs", "transcript.jsonl")
}
/** the session an ACP message names: a caller's session/prompt or session/load, or the agent's answer to session/new */
export function sessionIn(line: string): string | null {
// cheap first: most lines (text deltas, tool updates) are the session's own and name it too, which is as good
if (!line.includes('"sessionId"')) return null
try {
names = readdirSync(brain)
const m = JSON.parse(line)
const id = m?.params?.sessionId ?? m?.result?.sessionId
return typeof id === "string" && id ? id : null
} catch {
return null
}
for (const n of names) {
const f = path.join(brain, n, ".system_generated", "logs", "transcript.jsonl")
try {
const m = statSync(f).mtimeMs
if (!best || m > best.mtime) best = { file: f, mtime: m }
} catch {}
}
return best
}
/** What an agent has running that isn't the agent itself: every process in its session (it's started detached, so
@@ -155,11 +155,20 @@ export function watchEnd(ended: () => { step: number; at: number } | null, by: s
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 {
/** watchEnd on an Antigravity agent's transcript: the one of `session()`, the ACP session this socket is on. Agents
* on one sign-in share a home, so the home's newest transcript is whichever of them wrote last: read that way
* (12-01), one chat's last message ended every other turn running beside it with nothing said (12-02: three of ten
* chats run two at a time). With no session known yet, nothing is said. */
export function watchTurnEnd(geminiHome: string, session: () => string | null, 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
const id = session()
const file = id ? transcriptOf(geminiHome, id) : null
if (!file) return null
try {
return turnEndOf(lastLine(file))
} catch {
return null
}
}
return watchEnd(ended, "its transcript", told, said, busy, everyMs)
}
+25 -3
View File
@@ -10,7 +10,7 @@ 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 { claudeStopHook, jobsOf, lastLine, markedEnd, newestTranscript, turnEndOf, watchEnd, watchTurnEnd } from "../server/turn-end.ts"
import { claudeStopHook, jobsOf, lastLine, markedEnd, sessionIn, transcriptOf, turnEndOf, watchEnd, watchTurnEnd } from "../server/turn-end.ts"
import { landedQuery, linkIn, packCredential, unpackCredential } from "../server/google-token.ts"
const L4 = `Devices:
@@ -125,12 +125,34 @@ console.log("units: ok")
const now = Date.now()
const line = (o: object) => JSON.stringify(o) + "\n"
writeFileSync(file, line({ ...done, step_index: 1, created_at: new Date(now - 60_000).toISOString() }))
assert.equal(newestTranscript(home)?.file, file)
assert.equal(transcriptOf(home, "s1"), file)
assert.equal(transcriptOf(home, "../s1"), null)
// the session a socket is on, off its own messages: the agent's answer to session/new, a caller's prompt
assert.equal(sessionIn(JSON.stringify({ jsonrpc: "2.0", id: 2, result: { sessionId: "s1" } })), "s1")
assert.equal(sessionIn(JSON.stringify({ jsonrpc: "2.0", id: 3, method: "session/prompt", params: { sessionId: "s1", prompt: [] } })), "s1")
assert.equal(sessionIn(JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: {} })), null)
assert.equal(sessionIn("not json sessionId"), null)
// another agent on the same sign-in ends its turn beside this one: its transcript is newer, and isn't this turn's
const other = path.join(home, "antigravity-acp", "brain", "s2", ".system_generated", "logs")
mkdirSync(other, { recursive: true })
{
const heard: string[] = []
let on: string | null = null
const stopOther = watchTurnEnd(home, () => on, () => now, (by) => heard.push(by), () => false, 10)
writeFileSync(path.join(other, "transcript.jsonl"), line({ ...done, step_index: 9, created_at: new Date(now + 5000).toISOString() }))
await new Promise((ok) => setTimeout(ok, 60))
// no session known yet: nothing is said, whatever the home holds
assert.deepEqual(heard, [])
on = "s1"
await new Promise((ok) => setTimeout(ok, 60))
assert.deepEqual(heard, [])
stopOther()
}
assert.equal(JSON.parse(lastLine(file)).step_index, 1)
// an end from before the agent was last told anything isn't this turn's; one after it is said once
const said: string[] = []
let busy = true
const stop = watchTurnEnd(home, () => now, (by) => said.push(by), () => busy, 10)
const stop = watchTurnEnd(home, () => "s1", () => now, (by) => said.push(by), () => busy, 10)
await new Promise((ok) => setTimeout(ok, 60))
assert.deepEqual(said, [])
writeFileSync(file, line({ ...done, step_index: 2, tool_calls: [{ name: "x" }], created_at: new Date(now + 1000).toISOString() }) + line({ ...done, step_index: 3, created_at: new Date(now + 2000).toISOString() }))