agy-cli 0.1.39: a new nc.exe build drops this runtime's older ones (#1097)

Each nc dir holds a whole Bun runtime (~115MB) and its key changes with
mcp-server.mjs, so every plugin update left one in %TEMP% for the 7-day
sweep (Hy1an measured it). Each dir now holds a `runtime` file naming the
runtime that built it; a build removes the same runtime's other nc dirs at
once. Another runtime's dirs (another install) and dirs from before the
marker keep the week. A running nc.exe is locked, so its dir stays.

Run: test/windows-nc.mjs 8/8 on WSL and on Windows 11 (xiaomi-gpu);
bun test in packages/agy-cli 20/20 (WSL); check.mjs agy-cli ok.
Broken by hand: 0.1.38's index.mjs fails the new test (and the two that
now expect the marker).
Not tried: a real update on Windows. Dirs 0.1.38 left have no marker, so
they still go after a week.

Reported-by: Hy1an
This commit is contained in:
yetone
2026-11-13 21:15:11 +08:00
parent 67a5baaa1f
commit 4f5d4138ad
3 changed files with 60 additions and 10 deletions
+18 -7
View File
@@ -871,6 +871,11 @@ async function ensureShimDir() {
const WINDOWS_NC_PREFIX = "magpie-agy-bridge-nc-"
const WINDOWS_NC_BUILD_PREFIX = "magpie-agy-bridge-ncbuild-"
const WINDOWS_NC_BUILD_TIMEOUT_MS = 120_000
// Each nc dir names the runtime that built it, so a build for a newer
// mcp-server.mjs drops the same runtime's older ones at once: each holds a
// whole Bun runtime (~115MB), and a week of plugin updates piles them up
// (#1097). Another runtime's dirs (another install) keep the week.
const WINDOWS_NC_RUNTIME_FILE = "runtime"
function asciiPath(value) {
return /^[\x20-\x7e]*$/.test(String(value || ""))
@@ -889,6 +894,8 @@ async function windowsNcDir() {
if (!windowsNcBuild(process.execPath, MCP_SERVER_PATH, "nc.exe")) return null
const tmp = os.tmpdir()
const source = await fs.promises.readFile(MCP_SERVER_PATH)
const runtimeKey = createHash("sha256").update(String(process.execPath)).update("\0")
.update(String(process.versions?.bun || "")).digest("hex").slice(0, 16)
const key = createHash("sha256").update(String(process.execPath)).update("\0")
.update(String(process.versions?.bun || "")).update("\0").update(source).digest("hex").slice(0, 16)
const dir = path.join(tmp, WINDOWS_NC_PREFIX + key)
@@ -920,6 +927,7 @@ async function windowsNcDir() {
const { err, code, timedOut } = await execOnce(spec.file, spec.args, { cwd: build, timeoutMs: WINDOWS_NC_BUILD_TIMEOUT_MS })
if (err || timedOut || code !== 0 || !await isFileWithBytes(path.join(build, "nc.exe"))) return null
await fs.promises.mkdir(dir, { recursive: true })
await fs.promises.writeFile(path.join(dir, WINDOWS_NC_RUNTIME_FILE), runtimeKey).catch(() => {})
try {
await fs.promises.rename(path.join(build, "nc.exe"), exe)
} catch (error) {
@@ -930,15 +938,16 @@ async function windowsNcDir() {
fs.promises.rm(build, { recursive: true, force: true }).catch(() => {})
if (copy && copy !== build) fs.promises.rm(copy, { recursive: true, force: true }).catch(() => {})
}
sweepWindowsNcDirs(tmp, path.basename(dir)).catch(() => {})
if (copyBase && copyBase !== tmp) sweepWindowsNcDirs(copyBase, path.basename(dir)).catch(() => {})
sweepWindowsNcDirs(tmp, path.basename(dir), runtimeKey).catch(() => {})
if (copyBase && copyBase !== tmp) sweepWindowsNcDirs(copyBase, path.basename(dir), runtimeKey).catch(() => {})
return dir
}
// A build no host has started for a week (an older runtime or server), or a
// build dir a killed host left half done. An nc.exe still running is locked,
// so its dir stays until a later sweep.
async function sweepWindowsNcDirs(tmp, keep) {
// A build no host has started for a week (an older runtime or server), one
// this runtime built for an older server, or a build dir a killed host left
// half done. An nc.exe still running is locked, so its dir stays until a
// later sweep.
async function sweepWindowsNcDirs(tmp, keep, runtimeKey = "") {
const now = Date.now()
for (const name of await fs.promises.readdir(tmp)) {
const shared = name.startsWith(WINDOWS_NC_PREFIX) && /^[0-9a-f]{16}$/.test(name.slice(WINDOWS_NC_PREFIX.length))
@@ -946,8 +955,10 @@ async function sweepWindowsNcDirs(tmp, keep) {
if (name === keep || (!shared && !build)) continue
const file = path.join(tmp, name)
try {
const ours = shared && runtimeKey &&
await fs.promises.readFile(path.join(file, WINDOWS_NC_RUNTIME_FILE), "utf8").then(v => v === runtimeKey, () => false)
const stat = await fs.promises.stat(shared ? path.join(file, "nc.exe") : file).catch(() => fs.promises.stat(file))
if (now - stat.mtimeMs < (shared ? 7 * 24 : 1) * 60 * 60 * 1000) continue
if (!ours && now - stat.mtimeMs < (shared ? 7 * 24 : 1) * 60 * 60 * 1000) continue
await fs.promises.rm(file, { recursive: true, force: true })
} catch {}
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-agy-cli-auth",
"version": "0.1.38",
"version": "0.1.39",
"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",
+41 -2
View File
@@ -85,7 +85,7 @@ test("Windows sessions dial a shared nc.exe built once, never nc.cmd", async ()
const dir = agyPathDir(kit, 0)
assert.match(path.basename(dir), /^magpie-agy-bridge-nc-[0-9a-f]{16}$/)
assert.equal(agyPathDir(kit, 1), dir)
assert.deepEqual(fs.readdirSync(dir), ["nc.exe"])
assert.deepEqual(fs.readdirSync(dir).sort(), ["nc.exe", "runtime"])
assert.ok(!fs.readdirSync(kit.tmp).some((name) => name.startsWith("magpie-agy-bridge-bin-") || name.startsWith("magpie-agy-bridge-ncbuild-")))
// A later host with the same runtime and server reuses the exe.
@@ -131,7 +131,7 @@ test("a runtime under a non-ASCII folder builds nc.exe from an ASCII copy", asyn
assert.deepEqual(seen, [{ file: builds[0].file, copied: "MZ fake bun" }])
const dir = agyPathDir(kit)
assert.match(path.basename(dir), /^magpie-agy-bridge-nc-[0-9a-f]{16}$/)
assert.deepEqual(fs.readdirSync(dir), ["nc.exe"])
assert.deepEqual(fs.readdirSync(dir).sort(), ["nc.exe", "runtime"])
// The copy goes with the build dir.
for (let i = 0; i < 1000 && fs.existsSync(builds[0].file); i++) await new Promise(resolve => setImmediate(resolve))
assert.equal(fs.existsSync(builds[0].file), false)
@@ -142,6 +142,45 @@ test("a runtime under a non-ASCII folder builds nc.exe from an ASCII copy", asyn
}
})
// #1097 (Hy1an): each nc dir holds a whole Bun runtime (~115MB) and its key
// changes with mcp-server.mjs, so plugin updates piled them up for a week. A
// build drops this runtime's older dirs at once; another runtime's dirs, and
// ones from before the marker, keep the week.
test("a new nc.exe build removes this runtime's older nc dirs and keeps others", async () => {
const first = await windowsKit()
let marker
try {
await turn(first, "one")
marker = fs.readFileSync(path.join(agyPathDir(first), "runtime"), "utf8")
assert.match(marker, /^[0-9a-f]{16}$/)
} finally {
await first.dispose()
}
const kit = await windowsKit()
try {
const plant = (name, runtime) => {
const dir = path.join(kit.tmp, "magpie-agy-bridge-nc-" + name)
fs.mkdirSync(dir)
fs.writeFileSync(path.join(dir, "nc.exe"), "MZ old")
if (runtime) fs.writeFileSync(path.join(dir, "runtime"), runtime)
return dir
}
const older = plant("aaaaaaaaaaaaaaaa", marker)
const foreign = plant("bbbbbbbbbbbbbbbb", "0123456789abcdef")
const legacy = plant("cccccccccccccccc", null)
await turn(kit, "two")
assert.equal(buildsOf(kit).length, 1)
const current = agyPathDir(kit)
for (let i = 0; i < 1000 && fs.existsSync(older); i++) await new Promise(resolve => setImmediate(resolve))
assert.equal(fs.existsSync(older), false, "this runtime's older nc dir stayed")
assert.ok(fs.existsSync(path.join(foreign, "nc.exe")), "another runtime's nc dir was removed")
assert.ok(fs.existsSync(path.join(legacy, "nc.exe")), "an unmarked nc dir was removed before its week")
assert.ok(fs.existsSync(path.join(current, "nc.exe")))
} finally {
await kit.dispose()
}
})
test("Windows falls back to nc.cmd when the runtime can't build nc.exe", async () => {
for (const options of [{ execPath: "C:\\opencode\\opencode.exe", versions: { ...process.versions, bun: undefined } }, { onSpawn: () => 1 }]) {
const kit = await windowsKit(options)