agy-cli 0.1.96: a resumed session runs in its own folder again, so agy's cache still hits
@chet030714 on X: Hermes through agy-cli started near 70% cache hits and
fell all day. agy's prompt cache follows the folder its process runs in.
Every agy the bridge started had a new mkdtemp folder, so a session
resumed after the 10-minute idle reclaim, an LRU eviction or a restart
replayed its whole history uncached, and the longer a conversation got,
the more each resume cost.
Measured with real agy 1.5.10 over ACP in a sandbox HOME (one session,
~24K tokens a turn): every turn in one process hit (cachedReadTokens
19,682 to 197,710). A resume in the same folder: 213,520 cached. In
another folder: 0 cached, 219,198 uncached. Only the process's folder
counts, not session/resume's cwd. The same folder after 13 minutes
idle, or removed and made again at the same path, still hits.
Now a parked session records its workspace, and a resume starts agy in
that folder (made again if it was removed), never in a spare's. Only a
magpie-agy-bridge-XXXXXX folder directly in the temp folder is taken
from the state file. A stopped agy leaves its folder when another
already runs there, and a resume waits for a removal in progress.
Run (at 63c2ff93 + this diff):
- mica, HOME=$(mktemp -d): packages/agy-cli `bun test`: 41 pass, 0 fail.
- The new test (usage-settlement.mjs, LRU resume runs in the session's
folder) on the old index.mjs, by hand: fails.
- The real bridge, index.mjs loaded with real agy in a sandbox HOME, two
turns with the process reclaimed between them (AGY_BRIDGE_IDLE_MS=5000):
old code, resumed turn 0 of 32,127 cached; this change, 29,681 of
32,018 cached.
Not tried: Windows and Linux with real agy. A resume from a park entry
written before this version still starts in a new folder, once.
This commit is contained in:
@@ -555,7 +555,7 @@ export async function createBridgeKit(options = {}) {
|
||||
}
|
||||
if (args[0] === "acp") {
|
||||
const child = new FakeChild(kit, file, args)
|
||||
kit.acpSpawns.push({ file, args, env: opts.env, child })
|
||||
kit.acpSpawns.push({ file, args, env: opts.env, cwd: opts.cwd, child })
|
||||
new FakeAgy(kit, child, opts)
|
||||
return child
|
||||
}
|
||||
|
||||
@@ -950,6 +950,42 @@ test("LRU 淘汰后,补默认字段的 follow-up 仍能 resume 原 session", a
|
||||
})
|
||||
})
|
||||
|
||||
test("LRU 淘汰后 resume 的 agy 跑在原 session 的目录里(agy 的缓存跟着目录走)", async () => {
|
||||
await withKit({ env: { AGY_BRIDGE_MAX_PROCS: "1" }, sessionResume: true }, async (kit) => {
|
||||
for (const text of ["a1", "b1", "a2", "b2", "a3"]) {
|
||||
kit.prompts.push(async (agent) => {
|
||||
await agent.text(text)
|
||||
return { stopReason: "end_turn", usage: { inputTokens: 100, outputTokens: 1 } }
|
||||
})
|
||||
}
|
||||
const bodyA = baseBody({ messages: [{ role: "user", content: "conversation A" }] })
|
||||
const bodyB = baseBody({ messages: [{ role: "user", content: "conversation B" }] })
|
||||
const follow = (body, prior, reply, next) => ({
|
||||
...body,
|
||||
messages: [...prior, { role: "assistant", content: [{ type: "text", text: reply }] }, { role: "user", content: next }],
|
||||
})
|
||||
await runTurn(kit, bodyA)
|
||||
await runTurn(kit, bodyB)
|
||||
const a2 = follow(bodyA, bodyA.messages, "a1", "more A")
|
||||
await runTurn(kit, a2)
|
||||
await runTurn(kit, follow(bodyB, bodyB.messages, "b1", "more B"))
|
||||
await runTurn(kit, follow(bodyA, a2.messages, "a2", "and more A"))
|
||||
assert.equal(kit.acpSpawns.length, 5, "每次换会话都应挤掉另一个 agy")
|
||||
const resumes = kit.acpLog.filter((item) => item.method === "session/resume")
|
||||
assert.equal(resumes.length, 3, "A、B、A 都应 resume,而不是新开 session")
|
||||
const [a, b, aAgain, bAgain, aThird] = kit.acpSpawns.map((spawn) => spawn.cwd)
|
||||
assert.ok(a && b && a !== b, "两个会话应各有自己的目录")
|
||||
assert.equal(aAgain, a, "resume 的 agy 换了目录,agy 会整段不走缓存")
|
||||
assert.equal(bAgain, b)
|
||||
assert.equal(aThird, a)
|
||||
for (const item of resumes) {
|
||||
const spawn = kit.acpSpawns[item.spawn]
|
||||
assert.equal(item.params.cwd, spawn?.cwd ?? item.params.cwd)
|
||||
}
|
||||
assert.ok(fs.existsSync(path.join(a, ".agents", "agents")), "目录被删后应重新建好")
|
||||
})
|
||||
})
|
||||
|
||||
test("取消 prompt 把缓存比例校准为 0 后,下一回合工具步也不再把前缀估成读缓存", async () => {
|
||||
await withKit({}, async (kit) => {
|
||||
const cancelled = { inputTokens: 5000, cachedReadTokens: 0, outputTokens: 3 }
|
||||
|
||||
Reference in New Issue
Block a user