From 2e77e646c808e8315a07b2fb50b16e1a960931e1 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 17:29:02 +0800 Subject: [PATCH] agy-cli 0.1.36: the last model list agy gave is served after a restart (#1086) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a restart, models() handed out the static Gemini-only fallback until agy answered again. offeredFilter also hid every non-Gemini model until a session or probe told it what agy offers. So the first requests after a restart saw no Claude models. The bridge now keeps the last list agy gave, and the set it offered, in models.json beside the session state file. The file has one entry per agy path, is written under the state lock through temp-and-rename (mode 0600), and is trusted for 14 days. A half-written or invalid file, another agy path, or a stale entry all give the static fallback, as before. A saved list is not treated as an answer from this agy: the offered probe still waits for a real listing. Run (WSL magpie-desktop, Bun 1.4.2, Node 22): - bun test in packages/agy-cli: 20 pass. - node --test test/catalog.mjs: 24 pass. - bun scripts/check.mjs agy-cli: ok. Windows (xiaomi-gpu): node --test test/catalog.mjs 12 times, 11 green. The one red is the existing wall-clock test "#1064 登录检查不为探测等太久", filed as #1089. I haven't shown whether it is red on origin/main too. Broken by hand: readSavedCatalog returning null turns the new #1086 test red; skipping the offered restore turns it red too. Not tried: real agy on any OS; a restart with a real signed-in agy. Reported-by: yinbing-666 --- packages/agy-cli/README.en.md | 2 + packages/agy-cli/README.md | 3 +- packages/agy-cli/index.mjs | 79 ++++++++++++++++++++++++++++++- packages/agy-cli/package.json | 2 +- packages/agy-cli/test/catalog.mjs | 46 ++++++++++++++++++ 5 files changed, 128 insertions(+), 4 deletions(-) diff --git a/packages/agy-cli/README.en.md b/packages/agy-cli/README.en.md index 0dafee4..44b8afa 100644 --- a/packages/agy-cli/README.en.md +++ b/packages/agy-cli/README.en.md @@ -68,6 +68,8 @@ MCP server. The control port listens on loopback only, with a token per process. this client (agy may keep Claude and GPT-OSS for clients it recognises) is left out. To list models, the plugin opens one session with `agy acp` (no prompt is sent) and reads what it offers; until it has, only Gemini is listed. + The last list agy gave and offered is kept in `models.json` beside the session + state file, so after a restart that list is served while agy is asked again. Models and thinking levels can be switched mid-conversation without losing history. - **Streaming with thought summaries**: summaries come back as `thinking` blocks. diff --git a/packages/agy-cli/README.md b/packages/agy-cli/README.md index d0e5f5a..8a05b45 100644 --- a/packages/agy-cli/README.md +++ b/packages/agy-cli/README.md @@ -50,7 +50,8 @@ agy 连接 `--ide-mcp-port` 时会运行 `nc 127.0.0.1 <端口>`。插件在 agy - **agy 开放的模型**:模型列表以 ACP 会话里 agy 给出的模型选项为准。`agy models` 列出的 模型如果没有在会话里向本客户端开放(agy 可能只向它认可的客户端开放 Claude、GPT-OSS), 就不会出现在列表里。列模型时插件会先用 `agy acp` 开一个会话(不发 prompt)问 agy 开放了哪些, - 问到之前只列 Gemini。模型和思考等级可以在会话中切换, + 问到之前只列 Gemini。上次 agy 列出并开放的模型存在会话状态文件旁边的 `models.json`, + 重启后先列它,同时再问一次 agy。模型和思考等级可以在会话中切换, 历史保留。 - **流式输出与思考摘要**:思考摘要以 `thinking` 块返回,设置 `AGY_BRIDGE_THINKING=0` 可关闭。 - **图片输入**:支持内联 base64 的 PNG、JPEG、GIF、WebP。需要 agy 1.5.0 或更新版本, diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index 3279330..302cf7d 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -313,6 +313,7 @@ function noteOfferedModels(configOptions, models) { if (key === acpOfferedKey) return acpOfferedModels = names acpOfferedKey = key + if (cachedModelCatalog?.agyPath) saveCatalog(cachedModelCatalog.agyPath, { offered: [...names].map(([id, name]) => ({ id, name })) }) } // A model the session offers that `agy models` did not list is served as is. @@ -502,17 +503,90 @@ function fallbackModelCatalog(agyPath = null) { return { agyPath, list: FALLBACK_MODELS, catalog: null, offeredKey: null, isFallback: true, fetchedAt: 0, lastAttemptAt: null } } +// The last list `agy models` gave, kept beside sessions.json per agy +// executable (#1086): after a restart the host lists it at once, instead of +// the Gemini-only fallback until agy answers again. It is served as stale +// (fetchedAt 0), so the first models() call still asks agy. +const SAVED_CATALOG_MAX_AGE_MS = 14 * 24 * 60 * 60 * 1000 + +function modelCatalogFile() { + return path.join(path.dirname(sessionStateFile()), "models.json") +} + +function validModelRow(row) { + return row && typeof row.id === "string" && /^\S+$/.test(row.id) && typeof row.name === "string" && + (row.effort === undefined || EFFORT_ORDER.includes(row.effort)) +} + +function readSavedCatalog(agyPath) { + if (!agyPath) return null + try { + const data = JSON.parse(fs.readFileSync(modelCatalogFile(), "utf8")) + const entry = data?.version === 1 && data.byAgy && Object.hasOwn(data.byAgy, agyPath) ? data.byAgy[agyPath] : null + if (!entry || !Number.isFinite(entry.at) || Date.now() - entry.at > SAVED_CATALOG_MAX_AGE_MS) return null + // A half-written or hand-edited file means "unknown": the fallback serves. + if (!Array.isArray(entry.list) || !entry.list.length || !entry.list.every(validModelRow)) return null + const offered = Array.isArray(entry.offered) && entry.offered.length && + entry.offered.every(row => validModelRow(row) && row.effort === undefined) + ? entry.offered + : null + return { list: entry.list.map(savedRow), offered: offered && offered.map(({ id, name }) => ({ id, name })) } + } catch { + return null + } +} + +function savedRow({ id, name, effort }) { + return effort ? { id, name, effort } : { id, name } +} + +// saveCatalog merges what changed (the listing, or what agy acp offers) into +// the agy's entry. +function saveCatalog(agyPath, change) { + if (!agyPath) return + const file = modelCatalogFile() + try { + withStateLock(file, () => { + let byAgy = {} + try { + const data = JSON.parse(fs.readFileSync(file, "utf8")) + if (data?.version === 1 && data.byAgy && typeof data.byAgy === "object") byAgy = data.byAgy + } catch {} + const entry = { ...(byAgy[agyPath] || {}), at: Date.now() } + if (change.list) entry.list = change.list.map(savedRow) + if (change.offered) entry.offered = change.offered + if (!Array.isArray(entry.list)) return + byAgy[agyPath] = entry + const temp = `${file}.${process.pid}.${randomUUID().slice(0, 8)}.tmp` + fs.writeFileSync(temp, JSON.stringify({ version: 1, byAgy }) + "\n", { mode: 0o600 }) + fs.renameSync(temp, file) + }) + } catch {} +} + +function startingModelCatalog(agyPath) { + const saved = readSavedCatalog(agyPath) + if (!saved) return fallbackModelCatalog(agyPath) + // What agy acp offered last time, until a session of this run says. + if (saved.offered && !acpOfferedModels) { + acpOfferedModels = new Map(saved.offered.map(({ id, name }) => [id, name])) + acpOfferedKey = [...acpOfferedModels.keys()].sort().join("\n") + } + return { agyPath, list: saved.list, catalog: null, offeredKey: null, isFallback: false, fetchedAt: 0, lastAttemptAt: null } +} + // useAgyPath starts over when the executable changes: an old executable's // dynamic additions and offer must not route to, or list for, a new one. function useAgyPath(agyPath) { if (cachedModelCatalog.agyPath === agyPath) return // The first agy this process finds is not a change of one. if (cachedModelCatalog.agyPath !== null) forgetOffered() - cachedModelCatalog = fallbackModelCatalog(agyPath) + cachedModelCatalog = startingModelCatalog(agyPath) modelCatalogRefreshPromise = null } function storeListedCatalog(agyPath, list, lastAttemptAt) { + saveCatalog(agyPath, { list }) cachedModelCatalog = { agyPath, list, @@ -583,7 +657,8 @@ async function loadModelCatalog(agyPath, { probeOffered = false } = {}) { } // `agy models` answered, so agy is signed in: learn what its ACP server // offers this client, waiting for it as briefly as for the listing. - const probe = probeOffered && !cachedModelCatalog.isFallback ? probeOfferedOnce(agyPath) : null + // A saved list (fetchedAt 0) is not an answer from this agy yet. + const probe = probeOffered && !cachedModelCatalog.isFallback && cachedModelCatalog.fetchedAt > 0 ? probeOfferedOnce(agyPath) : null if (probe) await briefly(probe, MODEL_CATALOG_FAST_WAIT_MS) return catalogOf(cachedModelCatalog) } diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 65e0015..be445fc 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-agy-cli-auth", - "version": "0.1.35", + "version": "0.1.36", "description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI", "type": "module", "main": "./opencode.mjs", diff --git a/packages/agy-cli/test/catalog.mjs b/packages/agy-cli/test/catalog.mjs index 73e4720..58a9f2d 100644 --- a/packages/agy-cli/test/catalog.mjs +++ b/packages/agy-cli/test/catalog.mjs @@ -7,6 +7,9 @@ import { test } from "node:test" import assert from "node:assert/strict" import { spawnSync } from "node:child_process" +import fs from "node:fs" +import os from "node:os" +import path from "node:path" import { BASE_MODEL_ROWS, CATALOG_TTL_MS, baseBody, createBridgeKit, modelsOutput, readSse, sleep, tick, } from "./testkit.mjs" @@ -227,6 +230,49 @@ test("#1064 慢 agy:第一次 models() 给静态目录,后台热身后的第 }) }) +// #1086(yinbing-666):重启后在 agy 再次回答前,也该列出上次的完整目录,而不是只有 Gemini。 +test("#1086 重启后第一次 models() 就给上次 agy 列出并开放的目录,坏文件和别的 agy 仍给静态目录", async () => { + const shared = fs.mkdtempSync(path.join(os.tmpdir(), "agy-catalog-saved-")) + try { + const agy = path.join(shared, process.platform === "win32" ? "agy.exe" : "agy") + fs.writeFileSync(agy, "#!/bin/sh\nexit 99\n", { mode: 0o755 }) + const env = { XDG_STATE_HOME: path.join(shared, "state") } + await withKit({ ...SLOW_AGY, agyPath: agy, env }, async (kit) => { + assertFallbackPresent(await kit.models()) + await warmed(kit) + assertClaudeListed(await kit.models(), "第一次运行") + }) + const file = path.join(shared, "state", "magpie-agy-bridge", "models.json") + const saved = fs.readFileSync(file, "utf8") + assert.match(saved, /claude-sonnet-5-5-high/) + + // 重启:agy 还是慢,第一次 models() 就该有 Claude;探测等 agy models 回答后才起。 + await withKit({ ...SLOW_AGY, agyPath: agy, env }, async (kit) => { + const { value: first, ms } = await timed(() => kit.models()) + assert.ok(ms < FAST_BUDGET_MS, `重启后首次目录耗时 ${Math.round(ms)}ms`) + assertClaudeListed(first, "重启后第一次") + assert.equal(kit.acpSpawns.length, 0, "agy models 回答前不该起探测会话") + assert.equal(kit.modelsSpawns.length, 1, "保存的目录也要再问一次 agy") + await warmed(kit) + }) + + // 另一个 agy 可执行文件不用这份目录。 + await withKit({ ...SLOW_AGY, env }, async (kit) => { + assertFallbackPresent(await kit.models()) + await warmed(kit) + }) + + // 半截文件:当作不知道,给静态目录。 + fs.writeFileSync(file, saved.slice(0, Math.floor(saved.length / 2))) + await withKit({ ...SLOW_AGY, agyPath: agy, env }, async (kit) => { + assertFallbackPresent(await kit.models()) + await warmed(kit) + }) + } finally { + fs.rmSync(shared, { recursive: true, force: true }) + } +}) + test("#1064 慢 agy:已登录时 authorize 读到的列表和探测就是目录,紧接着的 models() 不再是静态目录", async () => { await withKit(SLOW_AGY, async (kit) => { const { value: started, ms } = await timed(() => kit.plugin.auth.methods[0].authorize())