agy dials --ide-mcp-port by running `nc` from PATH, and on Windows the
bridge's nc was nc.cmd: cmd.exe running mcp-server.mjs, two console
programs. When agy starts a console program with a console of its own
(CREATE_NEW_CONSOLE, or CREATE_NO_WINDOW), each dial gets a window or a
conhost.exe. Each session also made its own bin dir, and a host that
exited left its parked agy's cmd/bun children and its workspace dirs
behind.
- Under Bun (magpie's runtime), nc is now a GUI-subsystem nc.exe that
Bun builds from mcp-server.mjs on the user's own machine (`bun build
--compile --windows-hide-console`, ~0.3s on the test box). A GUI exe
gets no console under any creation flags, and it still talks over the
stdio pipes agy hands it. Nothing prebuilt ships. It lives in
%TEMP%\magpie-agy-bridge-nc-<hash of runtime, Bun version and server
source>, so every session and every later host reuses one build. A dir
no host has used for a week, or a half-done build dir older than an
hour, is swept. If the build fails, or the runtime isn't Bun (e.g.
opencode.exe), nc.cmd is used as before.
- At exit on Windows one `taskkill /T /F` ends every live agy with its
tree (cmd, nc). The workspace rm retries while the just-killed
processes let go of their cwd. Only a shim dir this host made is
removed; the shared nc.exe dir stays.
Run on Windows 11 (build 26200, xiaomi-gpu), Bun 1.4.2. A fake agy (Go)
answers ACP and dials `nc 127.0.0.1 <port>` 3 times per session, with
CreationFlags none, CREATE_NO_WINDOW or CREATE_NEW_CONSOLE. 4 sessions
per run, all 12 dials answered in every run below. conhost counts are
those under the host after 4 sessions, while agy is parked:
old, none: 5 conhost; agy 4, cmd 4, bun 4; 4 workspace dirs leaked
old, nowindow: 9 conhost (+2 per session); same leak
old, newconsole: 9 conhost (+2 per session); same leak
new, none / nowindow / newconsole: 5 conhost (the parked agy's own);
agy 4, nc.exe 4, no cmd, no bun; one magpie-agy-bridge-nc-<hash>
dir, nothing else left in TEMP after exit
new, idle eviction (3s): back to 1 conhost, no agy/nc left
new, host hard-killed: no agy/nc left; the 4 workspace dirs stay (no
code runs after a hard kill; the same on old, which also leaks its
bin dir)
old, DETACHED_PROCESS: the dial never answers (agy start times out),
so that isn't what the reported agy does.
The system-wide conhost count was 1 before and after every run.
Tests: test/windows-nc.mjs (6 tests, on any OS through the kit's
platform, execPath and versions overrides): the build command, the
shared dir built once and reused by a later host, no nc.cmd, the
nc.cmd fallback, the one-run tree kill at exit and which dirs it
removes.
Run at this tree on mica (Bun 1.4.2, temp HOME): bun test
packages/agy-cli 15 pass 0 fail; bun scripts/check.mjs exit 0.
On the Windows box: 14 pass, 1 fail. The red is smoke.mjs's "no call to
the loaded tool reaches agy's validator in the old prompt". It fails the
same way at origin/main 8768126 on the same box, and is filed as #973.
Broken by hand, each red in windows-nc.mjs: never using nc.exe (2
red), a build without --windows-hide-console (2), a rebuild per host (1),
SIGKILL per child instead of the taskkill tree (1), removing the shared
dir at exit (1).
Not tried: a real agy on Windows (it isn't installed on the box, and was
not downloaded); whether a window is visible (ssh runs in session 0,
where none is, so conhosts were counted instead); the reporter's build
26300. A host killed hard can still leave an agy that ignores stdin EOF:
only a job object around the plugin host (magpie's
internal/plugin/host.go starts it with a plain cmd.Start) would end it.
Reported-by: Tericerice <Tericerice@users.noreply.github.com>
126 lines
6.4 KiB
JavaScript
126 lines
6.4 KiB
JavaScript
import { spawn } from "node:child_process"
|
|
import path from "node:path"
|
|
|
|
export function agyCandidates(env, home, platform = process.platform) {
|
|
const windows = platform === "win32"
|
|
const paths = windows ? path.win32 : path.posix
|
|
const names = windows ? ["agy.exe", "agy.cmd", "agy.bat"] : ["agy"]
|
|
const searchPath = env.PATH || (windows ? env.Path : "") || ""
|
|
const candidates = env.AGY_PATH ? [env.AGY_PATH] : []
|
|
for (const directory of searchPath.split(windows ? ";" : ":")) {
|
|
if (directory) candidates.push(...names.map(name => paths.join(directory.replace(/^"|"$/g, ""), name)))
|
|
}
|
|
const directories = windows ? [
|
|
paths.join(env.LOCALAPPDATA || paths.join(home, "AppData", "Local"), "agy", "bin"),
|
|
paths.join(env.LOCALAPPDATA || paths.join(home, "AppData", "Local"), "Programs", "Antigravity", "bin"),
|
|
paths.join(home, ".local", "bin"),
|
|
paths.join(home, ".gemini", "bin"),
|
|
] : [paths.join(home, ".local", "bin"), paths.join(home, ".gemini", "bin"), "/usr/local/bin", "/opt/homebrew/bin"]
|
|
for (const directory of directories) candidates.push(...names.map(name => paths.join(directory, name)))
|
|
return candidates
|
|
}
|
|
|
|
// The cmd.exe escaping algorithm is adapted from node-cross-spawn:
|
|
// https://github.com/moxystudio/node-cross-spawn/blob/master/lib/util/escape.js
|
|
// Copyright (c) 2018 Made With MOXY Lda
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
const cmdMeta = /([()\][%!^"`<>&|;, *?])/g
|
|
|
|
function cmdArgument(value, doubleEscape) {
|
|
let argument = String(value)
|
|
if (/[\0\r\n]/.test(argument)) throw new Error("a Windows command argument contains a line break or NUL")
|
|
argument = argument.replace(/(?=(\\+?)?)\1"/g, '$1$1\\"')
|
|
argument = argument.replace(/(?=(\\+?)?)\1$/, "$1$1")
|
|
argument = `"${argument}"`.replace(cmdMeta, "^$1")
|
|
// npm's cmd-shim adds a second cmd expansion pass when forwarding %*.
|
|
return doubleEscape ? argument.replace(cmdMeta, "^$1") : argument
|
|
}
|
|
|
|
export function agySpawnSpec(file, args, platform = process.platform, env = process.env) {
|
|
if (platform !== "win32" || !/\.(?:cmd|bat)$/i.test(file)) return { file, args, options: {} }
|
|
if (/[\0\r\n]/.test(file)) throw new Error("a Windows command path contains a line break or NUL")
|
|
const doubleEscape = /node_modules[\\/]\.bin[\\/][^\\/]+\.cmd$/i.test(file)
|
|
const command = [path.win32.normalize(file).replace(cmdMeta, "^$1"), ...args.map(arg => cmdArgument(arg, doubleEscape))].join(" ")
|
|
return {
|
|
file: env.ComSpec || env.COMSPEC || env.comspec || "cmd.exe",
|
|
args: ["/d", "/v:off", "/s", "/c", `"${command}"`],
|
|
options: { windowsVerbatimArguments: true, windowsHide: true },
|
|
}
|
|
}
|
|
|
|
// The environment an agy child runs with. A value the user set for
|
|
// AGY_CLI_DISABLE_AUTO_UPDATE (in the parent's environment or the caller's)
|
|
// is kept as written. Only when nobody set it do the plugin's children run
|
|
// with auto-update off, so a pool of ACP processes doesn't update the binary
|
|
// under each other; the user's own `agy` runs keep updating.
|
|
export function childEnv(parent = process.env, extra = {}, platform = process.platform) {
|
|
const env = {}
|
|
const names = new Map()
|
|
for (const source of [parent, extra]) {
|
|
for (const [key, value] of Object.entries(source || {})) {
|
|
if (value === undefined) continue
|
|
// Windows treats env names case-insensitively; keep the caller's PATH.
|
|
const name = platform === "win32" ? key.toUpperCase() : key
|
|
if (names.has(name)) delete env[names.get(name)]
|
|
names.set(name, key)
|
|
env[key] = value
|
|
}
|
|
}
|
|
if (![...names.keys()].some(name => name.toUpperCase() === "AGY_CLI_DISABLE_AUTO_UPDATE")) {
|
|
env.AGY_CLI_DISABLE_AUTO_UPDATE = "true"
|
|
}
|
|
return env
|
|
}
|
|
|
|
export function spawnAgy(file, args, options = {}) {
|
|
const command = agySpawnSpec(file, args)
|
|
const env = childEnv(process.env, options?.env)
|
|
return spawn(command.file, command.args, { ...options, ...command.options, windowsHide: true, env })
|
|
}
|
|
|
|
export function windowsMcpShim(executable, server) {
|
|
const quote = value => {
|
|
if (/["\0\r\n]/.test(value)) throw new Error("invalid Windows MCP shim path")
|
|
return `"${value.replace(/%/g, "%%")}"`
|
|
}
|
|
return `@echo off\r\nsetlocal DisableDelayedExpansion\r\n${quote(executable)} ${quote(server)}\r\n`
|
|
}
|
|
|
|
// agy dials --ide-mcp-port by running `nc` from PATH. On Windows a console
|
|
// program started that way can be handed a console of its own, and nc.cmd
|
|
// always runs in cmd.exe, a console program: each dial then flashes a window
|
|
// and leaves a conhost.exe behind (#1047). A GUI-subsystem nc.exe never gets
|
|
// a console, whatever creation flags agy uses, and still talks over the
|
|
// stdio pipes agy hands it. Bun builds it from mcp-server.mjs on the user's
|
|
// own machine; nothing prebuilt ships. Another runtime gets null, and the
|
|
// caller falls back to nc.cmd.
|
|
export function windowsNcBuild(runtime, server, outfile, versions = process.versions) {
|
|
if (!versions?.bun || !/^bun(?:\.exe)?$/i.test(path.win32.basename(String(runtime || "")))) return null
|
|
return { file: runtime, args: ["build", "--compile", "--windows-hide-console", server, "--outfile", outfile] }
|
|
}
|
|
|
|
// One taskkill run that ends each agy and everything it started (cmd.exe,
|
|
// nc); null when there is nothing to end.
|
|
export function windowsTreeKill(pids) {
|
|
const live = [...new Set(pids)].filter(pid => Number.isInteger(pid) && pid > 0)
|
|
if (!live.length) return null
|
|
return { file: "taskkill", args: ["/T", "/F", ...live.flatMap(pid => ["/PID", String(pid)])] }
|
|
}
|