Files
yetone d7265c17e4 agy-cli 0.1.71: a new conversation takes a spare agy that is already started
A new conversation spawned `agy acp` and waited for initialize before
session/new. Now, after a conversation starts, the plugin warms one spare
agy for the same account, binary, folder, flags and environment: spawned and
initialised, with no session. The next new conversation for that key opens
its session on the spare and skips agy's start. Then another spare is warmed.

Only the process is warmed. On WSL with agy 1.5.9, measured through a
logging HTTPS proxy, spawn + initialize + 45s idle made no connection to
any host. session/new is where agy sends Code Assist calls (loadCodeAssist,
fetchAvailableModels, quota, user info, experiments), plus telemetry.
Closing an unused session writes its trajectory ACLs at Google. Sessions
also carry the conversation's own instructions. So sessions are never
opened ahead. agy reads its sign-in at session/new, so a spare picks up a
sign-in that changed after it started.

Spares are stopped when idle (IDLE_MS), first when the pool needs room,
never warmed when the pool has one slot left, at most 2 at a time, and
with the host (reapOnExit). agy also exits when its stdin closes.
AGY_BRIDGE_SPARE=0 turns them off.

TTFT of a second new conversation (test kit fake agy, initialize 1000ms,
session/new 3000ms): first 4026ms, second 3008ms; before, both ~4s. Real
agy start, measured on WSL: ~1s cold, ~0.25s warm. That is the saving. The
~3s session open (Google) and model latency remain.

Run (mote):
- test/spare.mjs on the old index.mjs (AGY_BRIDGE_TEST_INDEX): 6 of 7 red,
  only the AGY_BRIDGE_SPARE=0 case green. On the new index.mjs: 7 of 7
  green.
- `HOME=<sandbox> bun test packages/agy-cli/`: 36 pass, 0 fail.
- `bun scripts/check.mjs` ok.
The test kit defaults AGY_BRIDGE_SPARE=0 so suites that count spawns keep
their counts.

Not tried: a real signed-in account through the spare path, native
Windows agy, macOS agy.
2026-11-16 11:27:36 +08:00

149 lines
6.6 KiB
JavaScript

// A spare agy: the next new conversation takes an agy already started and
// initialised (no session), so it skips agy's start. Sessions are never opened
// ahead: agy asks Google for the account at session/new.
//
// bun test ./spare.mjs
import { test } from "node:test"
import assert from "node:assert/strict"
import { MESSAGES_URL, baseBody, createBridgeKit, readSse, sleep } from "./testkit.mjs"
const SPARE_ON = { AGY_BRIDGE_SPARE: "1" }
async function withKit(options, fn) {
const kit = await createBridgeKit({ ...options, env: { ...SPARE_ON, ...(options.env || {}) } })
kit.defaultPrompt = async (agent) => {
await agent.text("hello")
return { stopReason: "end_turn", usage: { inputTokens: 10, outputTokens: 1 } }
}
try {
return await fn(kit)
} finally {
await kit.dispose()
}
}
// Wait for a state, never a duration; the cap only bounds a hang.
async function until(what, check, capMs = 20_000) {
const started = Date.now()
while (!check()) {
if (Date.now() - started > capMs) throw new Error(`timed out waiting for ${what}`)
await sleep(5)
}
}
// Time to the first streamed text of one request.
async function ttft(send) {
const started = performance.now()
const response = await send()
assert.equal(response.status, 200, await response.clone().text())
const reader = response.body.getReader()
const decoder = new TextDecoder()
let text = ""
let first = null
for (;;) {
const { value, done } = await reader.read()
if (done) break
text += decoder.decode(value, { stream: true })
if (first === null && text.includes("content_block_delta")) first = performance.now() - started
}
return Math.round(first)
}
const sessionsOn = (kit) => kit.acpLog.filter((entry) => entry.method === "session/new").map((entry) => entry.spawn)
const conversation = (n) => baseBody({ system: `conversation ${n}`, messages: [{ role: "user", content: `open ${n}` }] })
test("a second new conversation takes the warmed agy and skips agy's start", async () => {
const INIT_MS = 1000
await withKit({ initializeDelayMs: INIT_MS, sessionNewDelayMs: 3000 }, async (kit) => {
const first = await ttft(() => kit.request(conversation(1)))
// the spare is started after the first conversation's agy and initialised on its own
await until("a spare agy initialised", () => kit.initialized === 2)
const spawnsBefore = kit.acpSpawns.length
const second = await ttft(() => kit.request(conversation(2)))
console.log(`TTFT new conversation (agy start ${INIT_MS}ms, session/new 3000ms): first ${first}ms, second ${second}ms`)
assert.equal(spawnsBefore, 2)
// conversation 2 opened its session on the spare: spawn #1, not a new one
assert.deepEqual(sessionsOn(kit).slice(0, 2), [0, 1])
// agy's start is gone from it; half the start is room for a loaded machine
assert.ok(second < first - INIT_MS / 2, `second ${second}ms, first ${first}ms`)
// and another spare is warmed for the conversation after
await until("the next spare", () => kit.acpSpawns.length === 3)
})
})
test("a spare opens no session until a conversation takes it", async () => {
await withKit({}, async (kit) => {
await readSse(await kit.request(conversation(1)))
await until("a spare agy initialised", () => kit.initialized === 2)
assert.equal(kit.acpSpawns.length, 2)
assert.deepEqual(kit.acpLog.filter((entry) => entry.spawn === 1).map((entry) => entry.method), ["initialize"])
})
})
test("a spare belongs to its account: another account starts its own agy", async () => {
await withKit({}, async (kit) => {
await readSse(await kit.request(conversation(1)))
await until("the default account's spare", () => kit.initialized === 2)
const other = await kit.plugin.auth.loader(async () => ({ type: "oauth", metadata: { agyHome: "otheracct1" } }))
await readSse(await other.fetch(MESSAGES_URL, { method: "POST", body: JSON.stringify(conversation(2)) }))
// spawn #1 is the default account's spare; the other account opened on a new agy
assert.equal(sessionsOn(kit)[1], 2)
assert.match(kit.acpSpawns[2].env.HOME ?? "", /otheracct1$/)
assert.notEqual(kit.acpSpawns[1].env.HOME, kit.acpSpawns[2].env.HOME)
// the default account's next conversation still takes its own spare
await readSse(await kit.request(conversation(3)))
assert.equal(sessionsOn(kit)[2], 1)
})
})
test("an idle spare is stopped after the idle time", async () => {
await withKit({ env: { AGY_BRIDGE_IDLE_MS: "60000" } }, async (kit) => {
await readSse(await kit.request(conversation(1)))
await until("a spare agy initialised", () => kit.initialized === 2)
const spare = kit.acpSpawns[1].child
kit.clock.advance(61_000)
// the next request sweeps idle processes before it picks one
await readSse(await kit.request(conversation(2)))
assert.equal(spare.closed, true, "the idle spare was stopped")
assert.notEqual(sessionsOn(kit)[1], 1)
})
})
test("a spare is the first to go when the pool is full", async () => {
await withKit({ env: { AGY_BRIDGE_MAX_PROCS: "3" } }, async (kit) => {
await readSse(await kit.request(conversation(1)))
await until("a spare agy initialised", () => kit.initialized === 2)
const spare = kit.acpSpawns[1].child
const other = await kit.plugin.auth.loader(async () => ({ type: "oauth", metadata: { agyHome: "otheracct1" } }))
const third = await kit.plugin.auth.loader(async () => ({ type: "oauth", metadata: { agyHome: "otheracct2" } }))
await readSse(await other.fetch(MESSAGES_URL, { method: "POST", body: JSON.stringify(conversation(2)) }))
// three agy now: two conversations and the spare, so no second spare
assert.equal(kit.acpSpawns.length, 3)
await readSse(await third.fetch(MESSAGES_URL, { method: "POST", body: JSON.stringify(conversation(3)) }))
assert.equal(spare.closed, true, "the spare made room")
// both conversations kept their agy
assert.equal(kit.acpSpawns[0].child.closed, false)
assert.equal(kit.acpSpawns[2].child.closed, false)
})
})
test("the host's exit stops a spare", async () => {
await withKit({}, async (kit) => {
await readSse(await kit.request(conversation(1)))
await until("a spare agy initialised", () => kit.initialized === 2)
kit.module.__agyTestReapOnExit()
assert.equal(kit.acpSpawns[1].child.closed, true)
})
})
test("AGY_BRIDGE_SPARE=0 starts no spare", async () => {
await withKit({ env: { AGY_BRIDGE_SPARE: "0" } }, async (kit) => {
await readSse(await kit.request(conversation(1)))
await readSse(await kit.request(conversation(2)))
await sleep(50)
assert.equal(kit.acpSpawns.length, 2)
assert.deepEqual(sessionsOn(kit), [0, 1])
})
})