On Windows 11, a signed-out agy opened Google's sign-in page 235 times in one day. The usage refresh ran `agy` about every 3 minutes, and agy opens the browser through ShellExecuteW, so the BROWSER=open.cmd guard can't stop it. New signin.mjs reads agy's stored sign-in without running agy. agy keeps it through go-keyring, service "gemini" and user "antigravity": - macOS: `security find-generic-password`; - Windows: CredReadW from PowerShell, on the credential "gemini:antigravity". It reads only whether a sign-in is stored, whether it has a refresh token, and when the access token ends. The token itself never leaves the function; a hash tells one sign-in from the next. Setting GEMINI_API_KEY, GOOGLE_API_KEY, AGY_ADC_AUTH or GOOGLE_GENAI_USE_VERTEXAI means agy signs in some other way, so nothing is concluded from the keyring. AGY_BRIDGE_STORED_SIGNIN=skip turns the read off. An access token that has ended is not "needs sign-in". The owner's working agy has one that ended 11 hours ago and is renewed from its refresh token. agy counts as unable to sign in when: - no sign-in is stored; - the access token has ended and there is no refresh token; - or agy already failed on this stored sign-in: it said it needs a sign-in, or it hung with an ended token and printed nothing or sign-in talk, the way an agy waiting on its browser does. This holds until another sign-in is stored. What changes: - Usage: in those cases agy isn't run. The read says signIn "expired", and magpie shows the account's sign-in as expired. After the next good read it says "renewed", and magpie clears that. - Other usage failures wait 10 minutes before the next run, doubling up to 6 hours. Before, they ran again on every refresh. - The model listing (`agy models`) isn't run in the background in those cases either. The catalog it has stays. - Signing in from magpie skips the `agy models` probe and goes straight to magpie's own sign-in link. A finished sign-in clears what was marked dead. - A request answers 401 with X-Magpie-Sign-In: expired when no sign-in is stored. This uses the last read, so a request never waits for PowerShell. - `agy --version` hanging is never read as a sign-in wait. Run: - mote, rebased ona02d5912: `bun scripts/check.mjs` ok and `bun test` in packages/agy-cli (20 pass, 0 fail). - mote, root `HOME=<temp> bun test` at the pre-rebase head (e90d6f01base): 7588 pass, 427 fail, none in agy-cli. The reds named in the kept tail are OAuth callback-port and loopback page-service tests (mote at load ~35-40); main's own CI test job passes them, so this push's CI test job is the record for the whole suite. - win-run on xiaomi-gpu: test/signin.mjs and test/usage.mjs (21 pass, 3 skip on Windows), plus the PowerShell script for real. Over SSH, CredReadW answers 1312 (no logon session), which the script turns into "unknown". So the struct read was tried on a CREDENTIALW built in memory with a UTF-8 blob: the bytes round-trip and the sign-in parses. Broken by hand: - with the usage gate, the backoff hold and the dead mark removed, 5 usage tests go red; - with listAgyModels' gate and the request gate removed, the new catalog test goes red; - with the request gate alone removed, it goes red too. Not tried: - a real signed-out agy, by the rule against running a real agy sign-in or a browser; - CredReadW in an interactive Windows session (none was logged on); - agy on Windows at all (xiaomi-gpu has no agy). On Linux the keyring isn't read ("unknown"): agy runs as before, with the backoff, and the xdg-open shim already stops the browser there.
660 lines
29 KiB
JavaScript
660 lines
29 KiB
JavaScript
import assert from "node:assert/strict"
|
|
import childProcess, { spawnSync } from "node:child_process"
|
|
import { readFileSync } from "node:fs"
|
|
import { syncBuiltinESMExports } from "node:module"
|
|
import fs from "node:fs/promises"
|
|
import os from "node:os"
|
|
import path from "node:path"
|
|
import { test } from "node:test"
|
|
import { createUsageReader, fetchUsage, parseUsageReport } from "../usage.mjs"
|
|
import { forgetStoredSignIn } from "../signin.mjs"
|
|
|
|
const report = groups => ({ status: "SUCCESS", command: { name: "usage", data: { groups } } })
|
|
const group = (buckets, name = "Gemini Models") => ({ name, buckets })
|
|
const bucket = (overrides = {}) => ({ id: "gemini-5h", name: "Five Hour Limit Remaining", window: "5h", remaining_fraction: 0.75, ...overrides })
|
|
|
|
test("maps all groups/cadences to display-only Magpie windows, without invented identity", () => {
|
|
const result = parseUsageReport(report([
|
|
group([bucket(), bucket({ id: "gemini-weekly", window: "weekly", remaining_fraction: 0.5 })]),
|
|
group([bucket({ id: "3p-5h", remaining_fraction: 0 }), bucket({ id: "3p-weekly", window: "weekly", remaining_fraction: 1 })], "Claude and GPT models"),
|
|
group([bucket({ id: "gemini-argon-weekly", window: "weekly", remaining_fraction: 0.53 })], "Gemini 4 Argon"),
|
|
]))
|
|
assert.equal(result.windows.length, 5)
|
|
assert.deepEqual(result.windows.map(w => w.name), ["Gemini · 5 hours", "Gemini · 7 days", "Claude & GPT · 5 hours", "Claude & GPT · 7 days", "Gemini 4 Argon · 7 days"])
|
|
assert.deepEqual(result.windows.map(w => w.used), [25, 50, 100, 0, 47])
|
|
assert.deepEqual(result.windows.map(w => w.span), [18000, 604800, 18000, 604800, 604800])
|
|
assert.ok(result.windows.every(w => w.aside === true))
|
|
assert.equal(result.plan, undefined)
|
|
assert.equal(result.user, undefined)
|
|
})
|
|
|
|
test("accepts camel case and nested/oneof fractions plus ISO and epoch-second resets", () => {
|
|
const result = parseUsageReport(JSON.stringify(report([{
|
|
displayName: "Gemini Models",
|
|
buckets: [
|
|
{ bucketId: "gemini-5h", displayName: "Session", window: "5h", remaining: { remainingFraction: 0.4 }, resetTime: "2026-10-27T02:00:00+02:00" },
|
|
{ bucketId: "gemini-weekly", remaining: { case: "remainingFraction", value: 0.8 }, resetTime: "1793059200" },
|
|
],
|
|
}])))
|
|
assert.equal(result.windows[0].used, 60)
|
|
assert.equal(result.windows[0].resetsAt, "2026-10-27T00:00:00.000Z")
|
|
assert.equal(result.windows[1].used, 20)
|
|
assert.equal(result.windows[1].resetsAt, "2026-10-27T00:00:00.000Z")
|
|
assert.equal(result.windows[1].span, 604800)
|
|
})
|
|
|
|
test("unknown, invalid and disabled quotas are never reported as zero/full usage", () => {
|
|
const result = parseUsageReport(report([group([
|
|
bucket(),
|
|
...[null, undefined, "0.5", true, -0.1, 1.1, NaN, Infinity].map((remaining_fraction, i) => bucket({ id: `unknown-${i}`, remaining_fraction, reset_time: "2026-10-27T00:00:00Z" })),
|
|
bucket({ id: "disabled", disabled: true }),
|
|
bucket({ id: "other-oneof", remaining_fraction: undefined, remaining: { case: "unlimited", value: 1 } }),
|
|
])]))
|
|
assert.equal(result.windows.length, 10)
|
|
assert.ok(result.windows.slice(1).every(w => w.used === undefined && w.display === "Limits not available" && w.aside))
|
|
assert.equal(result.windows[1].resetsAt, "2026-10-27T00:00:00.000Z")
|
|
})
|
|
|
|
test("weekly-only reports stay weekly; explicit unknown cadence never becomes five hours", () => {
|
|
const result = parseUsageReport(report([group([
|
|
bucket({ id: "gemini-weekly", window: undefined, name: "Weekly Limit Remaining" }),
|
|
bucket({ id: "gemini-5h", window: "custom", name: "Special limit", reset_time: "bad" }),
|
|
bucket({ id: "other", window: undefined, name: "Other", reset_time: "2026-10-27T00:00:00Z" }),
|
|
])]))
|
|
assert.equal(result.windows[0].span, 604800)
|
|
assert.equal(result.windows[1].span, undefined)
|
|
assert.equal(result.windows[1].resetsAt, undefined)
|
|
assert.equal(result.windows[2].span, undefined)
|
|
})
|
|
|
|
test("requires a successful usage command with at least one enabled measured bucket", () => {
|
|
for (const value of [
|
|
"invalid-json", {}, { status: "ERROR", error: "secret@example.com" },
|
|
{ ...report([group([bucket()])]), command: { name: "models", data: {} } },
|
|
report([]), report([group([bucket({ disabled: true })])]),
|
|
report([group([bucket({ remaining_fraction: undefined })])]),
|
|
report([group([{ remaining_fraction: 0.5 }])]),
|
|
]) {
|
|
assert.throws(() => parseUsageReport(value), error => !error.message.includes("secret@example.com"))
|
|
}
|
|
})
|
|
|
|
// Polls until the pid is gone. The bound is generous: a SIGKILLed child is
|
|
// reaped only when the kernel and this event loop get to it, which takes
|
|
// longer on a loaded machine.
|
|
async function waitForPidExit(pid, timeoutMs = 15_000) {
|
|
const deadline = Date.now() + timeoutMs
|
|
while (Date.now() < deadline) {
|
|
try {
|
|
process.kill(pid, 0)
|
|
} catch (err) {
|
|
if (err?.code === "ESRCH") return
|
|
}
|
|
await new Promise(resolve => setTimeout(resolve, 25))
|
|
}
|
|
assert.throws(() => process.kill(pid, 0), { code: "ESRCH" })
|
|
}
|
|
|
|
// Polls until a descendant that was killed no longer runs code. A dead orphan
|
|
// can stay a zombie on Linux until its new parent reaps it.
|
|
async function waitForNotRunning(pid, timeoutMs = 15_000) {
|
|
const deadline = Date.now() + timeoutMs
|
|
let state
|
|
while (Date.now() < deadline) {
|
|
state = spawnSync("ps", ["-o", "stat=", "-p", String(pid)], { encoding: "utf8" }).stdout.trim()
|
|
if (!state || state.startsWith("Z")) return
|
|
await new Promise(resolve => setTimeout(resolve, 25))
|
|
}
|
|
assert.fail(`descendant ${pid} still runs (${state})`)
|
|
}
|
|
|
|
// Runs fetchUsage against a hanging fixture and returns the children it
|
|
// spawned. The timeout covers both children, and a loaded machine can spend
|
|
// all of it starting the version child, so the budget doubles until the
|
|
// timeout lands while the child under test runs (the report child, or with
|
|
// version: true the version child). With onTaskkill, taskkill calls are
|
|
// recorded and handed to it instead of being run.
|
|
async function timedOut(f, { version = false, onTaskkill } = {}) {
|
|
const originalSpawn = childProcess.spawn
|
|
for (let timeoutMs = 600; ; timeoutMs *= 2) {
|
|
const children = []
|
|
const kills = []
|
|
try {
|
|
childProcess.spawn = (file, args, options) => {
|
|
if (file === "taskkill" && onTaskkill) {
|
|
kills.push({ args, options })
|
|
onTaskkill(args)
|
|
return { unref() {} }
|
|
}
|
|
const child = originalSpawn(file, args, options)
|
|
children.push({ pid: child.pid })
|
|
return child
|
|
}
|
|
syncBuiltinESMExports()
|
|
await assert.rejects(fetchUsage(f.executable, { ...f.options, timeoutMs }), /timed out/)
|
|
} finally {
|
|
childProcess.spawn = originalSpawn
|
|
syncBuiltinESMExports()
|
|
}
|
|
// The version child is spawned first, the report child second.
|
|
const target = children[version ? 0 : 1]
|
|
if (target || timeoutMs >= 20_000) {
|
|
assert.ok(target, `the ${version ? "version" : "report"} child never started within ${timeoutMs}ms`)
|
|
return { children, kills, target }
|
|
}
|
|
for (const pid of await f.pids(children)) await waitForPidExit(pid)
|
|
}
|
|
}
|
|
|
|
async function fixture(t, extraEnv = {}) {
|
|
const isWin = process.platform === "win32"
|
|
const directory = await fs.mkdtemp(path.join(os.tmpdir(), "agy-usage-test-"))
|
|
t.after(async () => {
|
|
for (let i = 0; i < 6; i++) {
|
|
try { await fs.rm(directory, { recursive: true, force: true }); break }
|
|
catch { await new Promise(r => setTimeout(r, 50)) }
|
|
}
|
|
})
|
|
const script = path.join(directory, "fake.mjs")
|
|
const executable = path.join(directory, isWin ? "agy.cmd" : "agy")
|
|
const logPath = path.join(directory, "calls.jsonl")
|
|
await fs.writeFile(script, String.raw`
|
|
import { spawn, spawnSync } from "node:child_process"
|
|
import fs from "node:fs"
|
|
import path from "node:path"
|
|
const args = process.argv.slice(2)
|
|
// A slow start on a loaded machine; set TEST_START_DELAY_MS to reproduce one.
|
|
const startDelay = Number(process.env.TEST_START_DELAY_MS) || 0
|
|
if (startDelay > 0) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, startDelay)
|
|
const version = args[0] === "--version"
|
|
const guardRun = process.platform === "win32"
|
|
? spawnSync(process.env.ComSpec || "cmd.exe", ["/d", "/s", "/c", "\"" + process.env.BROWSER + "\""], { windowsHide: true })
|
|
: spawnSync(process.env.BROWSER)
|
|
const record = {
|
|
args, cwd: process.cwd(), pid: process.pid,
|
|
update: process.env.AGY_CLI_DISABLE_AUTO_UPDATE,
|
|
path: process.env.PATH, browser: process.env.BROWSER,
|
|
browserContent: (() => { try { return fs.readFileSync(process.env.BROWSER, "utf8") } catch { return null } })(),
|
|
agent: fs.readFileSync(path.join(process.cwd(), ".agents/agents/magpie-usage/agent.md"), "utf8"),
|
|
mode: fs.statSync(process.cwd()).mode & 0o777,
|
|
guard: guardRun.status,
|
|
}
|
|
fs.appendFileSync(process.env.TEST_LOG, JSON.stringify(record) + "\n")
|
|
const mode = process.env.TEST_MODE
|
|
if (mode === "version-hang" && version || mode === "hang" && !version) setInterval(() => {}, 1000)
|
|
else if (version) {
|
|
console.log(process.env.TEST_VERSION || "1.3.5")
|
|
} else if (mode === "fail") {
|
|
console.error(process.env.TEST_ERROR || "oops secret@example.com https://secret.invalid/token")
|
|
process.exitCode = 1
|
|
} else if (mode === "oversize") {
|
|
process.stdout.write("x".repeat(1024 * 1024 + 1))
|
|
} else if (mode === "combined-output") {
|
|
process.stdout.write("x".repeat(600))
|
|
process.stderr.write("y".repeat(600))
|
|
} else if (mode === "malformed") {
|
|
console.log("invalid secret@example.com")
|
|
} else if (mode === "descendant") {
|
|
const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "inherit", windowsHide: true })
|
|
fs.writeFileSync(process.env.TEST_DESCENDANT, String(child.pid))
|
|
child.unref()
|
|
console.log(process.env.TEST_REPORT)
|
|
} else {
|
|
if (mode === "delay") await new Promise(resolve => setTimeout(resolve, 150))
|
|
console.log(process.env.TEST_REPORT)
|
|
}
|
|
`)
|
|
if (isWin) {
|
|
await fs.writeFile(executable, `@echo off\r\n"${process.execPath}" "${script}" %*\r\n`)
|
|
} else {
|
|
await fs.writeFile(executable, `#!/bin/sh\nexec ${JSON.stringify(process.execPath)} ${JSON.stringify(script)} "$@"\n`, { mode: 0o700 })
|
|
}
|
|
const env = { ...process.env, TEST_LOG: logPath, TEST_REPORT: JSON.stringify(report([group([bucket()])])), TEST_DESCENDANT: path.join(directory, "descendant"), ...extraEnv }
|
|
return {
|
|
executable, env,
|
|
// agy's stored sign-in can't be read here (signin.mjs is tested on its own)
|
|
options: { env, tempRoot: directory, signIn: async () => ({ state: "unknown" }) },
|
|
async logs() { return (await fs.readFile(logPath, "utf8")).trim().split("\n").map(line => JSON.parse(line)) },
|
|
// Every pid to see gone after a kill: the spawned children, and the agy
|
|
// each one ran that got as far as logging (on Windows a grandchild under
|
|
// cmd.exe).
|
|
async pids(children) {
|
|
let logs = []
|
|
try { logs = await this.logs() } catch (error) { if (error?.code !== "ENOENT") throw error }
|
|
return [...new Set([...children.map(child => child.pid), ...logs.map(log => log.pid)])]
|
|
},
|
|
async assertClean() { assert.deepEqual((await fs.readdir(directory)).filter(name => name.startsWith("magpie-agy-usage-")), []) },
|
|
}
|
|
}
|
|
|
|
test("queries only the structured command in a private, restricted workspace and cleans up", async t => {
|
|
const f = await fixture(t, { AGY_CLI_DISABLE_AUTO_UPDATE: "false" })
|
|
const originalSpawn = childProcess.spawn
|
|
const spawns = []
|
|
let result
|
|
try {
|
|
childProcess.spawn = (file, args, options) => {
|
|
spawns.push({ file, args, options })
|
|
return originalSpawn(file, args, options)
|
|
}
|
|
syncBuiltinESMExports()
|
|
result = await fetchUsage(f.executable, f.options)
|
|
} finally {
|
|
childProcess.spawn = originalSpawn
|
|
syncBuiltinESMExports()
|
|
}
|
|
assert.equal(result.windows[0].used, 25)
|
|
const logs = await f.logs()
|
|
assert.equal(logs.length, 2)
|
|
assert.equal(spawns.length, 2)
|
|
for (const [index, actual] of spawns.entries()) {
|
|
if (actual.file === "taskkill") continue
|
|
if (process.platform !== "win32") {
|
|
assert.equal(actual.file, f.executable)
|
|
assert.deepEqual(actual.args, logs[index].args)
|
|
assert.equal(actual.options.detached, true)
|
|
} else {
|
|
assert.equal(actual.options.detached, false)
|
|
}
|
|
assert.equal(actual.options.windowsHide, true, "usage must hide both version and report child windows")
|
|
assert.deepEqual(actual.options.stdio, ["ignore", "pipe", "pipe"])
|
|
assert.equal(path.basename(actual.options.cwd), path.basename(logs[index].cwd))
|
|
assert.equal(actual.options.env.AGY_CLI_DISABLE_AUTO_UPDATE, "false", "a value the user set is passed on as written")
|
|
assert.equal(actual.options.env.TEST_LOG, f.env.TEST_LOG)
|
|
assert.notEqual(actual.options.env, f.env)
|
|
}
|
|
assert.deepEqual(logs[0].args, ["--version"])
|
|
assert.deepEqual(logs[1].args.slice(0, 6), ["-p", "/usage", "--output-format", "json", "--agent", "magpie-usage"])
|
|
assert.equal(logs[1].args[6], "--print-timeout")
|
|
assert.match(logs[1].args[7], /^\d+s$/)
|
|
for (const log of logs) {
|
|
if (process.platform !== "win32") assert.equal(log.mode, 0o700)
|
|
assert.equal(log.update, "false")
|
|
assert.ok(log.agent.includes("tools: []\nmcpServers: []"))
|
|
assert.equal(log.guard, 1)
|
|
// macOS process.cwd() canonicalizes /var to /private/var.
|
|
assert.equal(path.basename(log.path.split(path.delimiter)[0]), path.basename(log.cwd))
|
|
assert.equal(path.dirname(log.browser), log.path.split(path.delimiter)[0])
|
|
assert.equal(path.basename(log.browser), process.platform === "win32" ? "open.cmd" : "open")
|
|
await assert.rejects(fs.stat(log.cwd), { code: "ENOENT" })
|
|
}
|
|
assert.equal(f.env.AGY_CLI_DISABLE_AUTO_UPDATE, "false")
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("with nothing set, usage children run with auto-update off", async t => {
|
|
const f = await fixture(t)
|
|
const env = { ...f.env }
|
|
for (const key of Object.keys(env)) if (/^agy_cli_disable_auto_update$/i.test(key)) delete env[key]
|
|
const result = await fetchUsage(f.executable, { ...f.options, env })
|
|
assert.equal(result.windows[0].used, 25)
|
|
const logs = await f.logs()
|
|
assert.equal(logs.length, 2)
|
|
for (const log of logs) assert.equal(log.update, "true")
|
|
assert.equal("AGY_CLI_DISABLE_AUTO_UPDATE" in env, false)
|
|
})
|
|
|
|
test("simulated Windows fetches usage with hidden, attached children and a cmd browser guard", { skip: process.platform === "win32" }, async t => {
|
|
const f = await fixture(t, { PATH: "/usr/bin:/bin", Path: "C:\\Windows\\System32" })
|
|
const descriptor = Object.getOwnPropertyDescriptor(process, "platform")
|
|
const originalSpawn = childProcess.spawn
|
|
const spawns = []
|
|
let taskkills = 0
|
|
let result
|
|
try {
|
|
Object.defineProperty(process, "platform", { ...descriptor, value: "win32" })
|
|
childProcess.spawn = (file, args, options) => {
|
|
if (file === "taskkill") {
|
|
taskkills++
|
|
return { unref() {} }
|
|
}
|
|
spawns.push({ file, args, options })
|
|
return originalSpawn(file, args, options)
|
|
}
|
|
syncBuiltinESMExports()
|
|
result = await fetchUsage(f.executable, f.options)
|
|
} finally {
|
|
Object.defineProperty(process, "platform", descriptor)
|
|
childProcess.spawn = originalSpawn
|
|
syncBuiltinESMExports()
|
|
}
|
|
assert.equal(result.windows[0].used, 25)
|
|
const logs = await f.logs()
|
|
assert.equal(logs.length, 2)
|
|
assert.equal(spawns.length, 2)
|
|
assert.equal(taskkills, 0, "a clean Windows run must never launch taskkill")
|
|
for (const [index, actual] of spawns.entries()) {
|
|
assert.equal(actual.file, f.executable)
|
|
assert.deepEqual(actual.args, logs[index].args)
|
|
assert.equal(path.basename(actual.options.cwd), path.basename(logs[index].cwd))
|
|
assert.equal(actual.options.detached, false, "Windows children stay attached to the runner")
|
|
assert.equal(actual.options.windowsHide, true)
|
|
assert.equal(actual.options.env.AGY_CLI_DISABLE_AUTO_UPDATE, "true")
|
|
assert.ok(!("Path" in actual.options.env), "mixed-case Path must be folded into PATH")
|
|
assert.equal(actual.options.env.PATH, actual.options.cwd + path.delimiter + "/usr/bin:/bin")
|
|
}
|
|
for (const log of logs) {
|
|
assert.equal(log.update, "true")
|
|
assert.equal(path.basename(path.dirname(log.browser)), path.basename(log.cwd))
|
|
assert.equal(path.basename(log.browser), "open.cmd")
|
|
assert.equal(log.browserContent, "@exit /b 1\r\n")
|
|
await assert.rejects(fs.stat(log.cwd), { code: "ENOENT" })
|
|
}
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("simulated Windows timeouts kill the whole tree via taskkill /T /F /PID", { skip: process.platform === "win32" }, async t => {
|
|
const f = await fixture(t, { TEST_MODE: "hang" })
|
|
const descriptor = Object.getOwnPropertyDescriptor(process, "platform")
|
|
let run
|
|
try {
|
|
Object.defineProperty(process, "platform", { ...descriptor, value: "win32" })
|
|
run = await timedOut(f, {
|
|
// Emulate taskkill termination so the killed child releases its pipes on this POSIX host.
|
|
onTaskkill(args) { try { process.kill(Number(args[3]), "SIGKILL") } catch {} },
|
|
})
|
|
} finally {
|
|
Object.defineProperty(process, "platform", descriptor)
|
|
}
|
|
const treeKills = run.kills.filter(kill => kill.args[3] === String(run.target.pid))
|
|
assert.equal(treeKills.length, 1)
|
|
assert.deepEqual(treeKills[0].args, ["/T", "/F", "/PID", String(run.target.pid)])
|
|
assert.equal(treeKills[0].options.windowsHide, true)
|
|
// The runner must actually stop both children, not just stop waiting for them.
|
|
for (const pid of await f.pids(run.children)) await waitForPidExit(pid)
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("simulated Windows tree-kills pipe-holding descendants after the CLI parent exits", { skip: process.platform === "win32" }, async t => {
|
|
const f = await fixture(t, { TEST_MODE: "descendant" })
|
|
const descriptor = Object.getOwnPropertyDescriptor(process, "platform")
|
|
const originalSpawn = childProcess.spawn
|
|
const kills = []
|
|
let result
|
|
try {
|
|
Object.defineProperty(process, "platform", { ...descriptor, value: "win32" })
|
|
childProcess.spawn = (file, args, options) => {
|
|
if (file === "taskkill") {
|
|
kills.push({ args, options })
|
|
// Emulate /T tree termination so the inherited pipe closes on this POSIX host.
|
|
try { process.kill(Number(readFileSync(f.env.TEST_DESCENDANT, "utf8")), "SIGKILL") } catch {}
|
|
try { process.kill(Number(args[3]), "SIGKILL") } catch {}
|
|
return { unref() {} }
|
|
}
|
|
return originalSpawn(file, args, options)
|
|
}
|
|
syncBuiltinESMExports()
|
|
result = await fetchUsage(f.executable, f.options)
|
|
} finally {
|
|
Object.defineProperty(process, "platform", descriptor)
|
|
childProcess.spawn = originalSpawn
|
|
syncBuiltinESMExports()
|
|
}
|
|
assert.equal(result.windows[0].used, 25)
|
|
const logs = await f.logs()
|
|
const treeKills = kills.filter(kill => kill.args[3] === String(logs[1].pid))
|
|
assert.equal(treeKills.length, 1)
|
|
assert.deepEqual(treeKills[0].args, ["/T", "/F", "/PID", String(logs[1].pid)])
|
|
await waitForNotRunning(Number(await fs.readFile(f.env.TEST_DESCENDANT, "utf8")))
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("retries locked-directory removal (EBUSY) until it succeeds", async t => {
|
|
const f = await fixture(t)
|
|
const originalRm = fs.rm
|
|
const attempts = []
|
|
let result
|
|
try {
|
|
fs.rm = async (target, options) => {
|
|
attempts.push(target)
|
|
if (attempts.length <= 2) {
|
|
const error = new Error("EBUSY: directory is locked")
|
|
error.code = "EBUSY"
|
|
throw error
|
|
}
|
|
return originalRm(target, options)
|
|
}
|
|
syncBuiltinESMExports()
|
|
result = await fetchUsage(f.executable, f.options)
|
|
} finally {
|
|
fs.rm = originalRm
|
|
syncBuiltinESMExports()
|
|
}
|
|
assert.equal(result.windows[0].used, 25)
|
|
assert.equal(attempts.length, 3)
|
|
for (const target of attempts) assert.ok(path.basename(target).startsWith("magpie-agy-usage-"))
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("stops retrying locked-directory removal after four attempts without failing the query", async t => {
|
|
const f = await fixture(t)
|
|
const originalRm = fs.rm
|
|
let attempts = 0
|
|
try {
|
|
fs.rm = async () => {
|
|
attempts++
|
|
const error = new Error("EBUSY: directory is locked")
|
|
error.code = "EBUSY"
|
|
throw error
|
|
}
|
|
syncBuiltinESMExports()
|
|
assert.equal((await fetchUsage(f.executable, f.options)).windows[0].used, 25)
|
|
} finally {
|
|
fs.rm = originalRm
|
|
syncBuiltinESMExports()
|
|
}
|
|
assert.equal(attempts, 4)
|
|
const root = path.dirname(f.env.TEST_LOG)
|
|
for (const name of (await fs.readdir(root)).filter(name => name.startsWith("magpie-agy-usage-"))) {
|
|
await fs.rm(path.join(root, name), { recursive: true, force: true })
|
|
}
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("rejects old/unverified versions before any possible model prompt", async t => {
|
|
for (const version of ["1.1.10", "0.9.0", "garbage", "1.3", "1.3.5-beta"]) {
|
|
const f = await fixture(t, { TEST_VERSION: version })
|
|
await assert.rejects(fetchUsage(f.executable, f.options), /1\.1\.11 or later/)
|
|
assert.equal((await f.logs()).length, 1)
|
|
await f.assertClean()
|
|
}
|
|
for (const version of ["1.1.11", "1.2.0", "2.0.0"]) {
|
|
const f = await fixture(t, { TEST_VERSION: version })
|
|
assert.equal((await fetchUsage(f.executable, f.options)).windows.length, 1)
|
|
await f.assertClean()
|
|
}
|
|
})
|
|
|
|
test("only concurrent refreshes share work, and callers cannot mutate each other's data", async t => {
|
|
const f = await fixture(t, { TEST_MODE: "delay" })
|
|
const read = createUsageReader(() => f.executable, f.options)
|
|
const results = await Promise.all([read(), read(), read()])
|
|
assert.equal((await f.logs()).length, 2)
|
|
assert.equal(results[0].signIn, "kept")
|
|
results[0].windows[0].used = 99
|
|
assert.equal(results[1].windows[0].used, 25)
|
|
await read()
|
|
assert.equal((await f.logs()).length, 4)
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("reports sanitized failures and preserves the bridge sign-in; failures back off, then are retried", async t => {
|
|
for (const [message, expected, signIn] of [
|
|
["Not logged in secret@example.com https://secret.invalid/token", /sign in again/, "expired"],
|
|
["Eligibility check failed: EOF secret@example.com", /eligibility check failed on a network/, "kept"],
|
|
["not eligible secret@example.com", /not eligible/, "kept"],
|
|
["connection refused: secret@example.com https://secret.invalid", /network or proxy/, "kept"],
|
|
["unrecognized secret@example.com", /could not read usage/, "kept"],
|
|
]) {
|
|
const f = await fixture(t, { TEST_MODE: "fail", TEST_ERROR: message })
|
|
let clock = 1_000_000
|
|
const read = createUsageReader(() => f.executable, { ...f.options, now: () => clock })
|
|
const result = await read()
|
|
assert.deepEqual(result.windows, [])
|
|
assert.equal(result.signIn, signIn)
|
|
assert.match(result.error, expected)
|
|
assert.doesNotMatch(result.error, /secret|https:/)
|
|
const runs = (await f.logs()).length
|
|
f.env.TEST_MODE = ""
|
|
// held: agy isn't run again until the backoff ends
|
|
const again = await read()
|
|
assert.deepEqual(again.windows, [])
|
|
assert.equal(again.error, result.error)
|
|
assert.equal((await f.logs()).length, runs)
|
|
clock += 10 * 60 * 1000
|
|
const ok = await read()
|
|
assert.equal(ok.windows.length, 1)
|
|
assert.equal(ok.signIn, signIn === "expired" ? "renewed" : "kept")
|
|
await f.assertClean()
|
|
}
|
|
})
|
|
|
|
test("backoff doubles per failure up to six hours", async t => {
|
|
const f = await fixture(t, { TEST_MODE: "fail", TEST_ERROR: "unrecognized" })
|
|
let clock = 0
|
|
const read = createUsageReader(() => f.executable, { ...f.options, now: () => clock })
|
|
const minute = 60 * 1000
|
|
const runs = async () => (await f.logs()).filter(l => l.args[0] !== "--version").length
|
|
await read()
|
|
for (const wait of [10, 20, 40, 80, 160, 320, 360, 360]) {
|
|
const before = await runs()
|
|
clock += (wait - 1) * minute
|
|
await read()
|
|
assert.equal(await runs(), before, `run again before ${wait} minutes`)
|
|
clock += minute
|
|
await read()
|
|
assert.equal(await runs(), before + 1, `not run again after ${wait} minutes`)
|
|
}
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("never runs agy while it has no stored sign-in, or one that ended with nothing to renew it", async t => {
|
|
const f = await fixture(t)
|
|
const ended = { state: "present", fp: "a", canRenew: false, expiresAt: 1, expired: true }
|
|
for (const stored of [{ state: "missing" }, ended]) {
|
|
let reads = 0
|
|
const read = createUsageReader(() => f.executable, { ...f.options, signIn: async () => { reads++; return stored } })
|
|
for (let i = 0; i < 3; i++) {
|
|
const result = await read()
|
|
assert.deepEqual(result.windows, [])
|
|
assert.equal(result.signIn, "expired")
|
|
assert.match(result.error, /sign in again|Sign in again/i)
|
|
}
|
|
assert.equal(reads, 3)
|
|
}
|
|
await assert.rejects(f.logs(), { code: "ENOENT" })
|
|
// signing in again: agy runs, and the sign-in reads as renewed
|
|
let stored = { state: "missing" }
|
|
const read = createUsageReader(() => f.executable, { ...f.options, signIn: async () => stored })
|
|
assert.equal((await read()).signIn, "expired")
|
|
stored = { state: "present", fp: "b", canRenew: true, expiresAt: 1, expired: true }
|
|
const back = await read()
|
|
assert.equal(back.windows.length, 1)
|
|
assert.equal(back.signIn, "renewed")
|
|
assert.equal((await read()).signIn, "kept")
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("a sign-in agy failed on isn't run again in the background until another is stored", async t => {
|
|
forgetStoredSignIn({ dead: true })
|
|
t.after(() => forgetStoredSignIn({ dead: true }))
|
|
const f = await fixture(t, { TEST_MODE: "fail", TEST_ERROR: "You are not logged in. Please sign in." })
|
|
let clock = 0
|
|
let stored = { state: "present", fp: "old", canRenew: true, expiresAt: 1, expired: true }
|
|
const read = createUsageReader(() => f.executable, { ...f.options, now: () => clock, signIn: async () => stored })
|
|
assert.equal((await read()).signIn, "expired")
|
|
const runs = (await f.logs()).length
|
|
f.env.TEST_MODE = ""
|
|
clock += 7 * 60 * 60 * 1000
|
|
const held = await read()
|
|
assert.equal(held.signIn, "expired")
|
|
assert.equal((await f.logs()).length, runs)
|
|
stored = { ...stored, fp: "new", expired: false }
|
|
const back = await read()
|
|
assert.equal(back.windows.length, 1)
|
|
assert.equal(back.signIn, "renewed")
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("a run that hangs with an ended access token and says nothing marks the sign-in dead", async t => {
|
|
forgetStoredSignIn({ dead: true })
|
|
t.after(() => forgetStoredSignIn({ dead: true }))
|
|
const f = await fixture(t, { TEST_MODE: "hang" })
|
|
const stored = { state: "present", fp: "hung", canRenew: true, expiresAt: 1, expired: true }
|
|
const read = createUsageReader(() => f.executable, { ...f.options, timeoutMs: 3000, signIn: async () => stored })
|
|
const result = await read()
|
|
assert.equal(result.signIn, "expired")
|
|
const runs = (await f.logs()).length
|
|
assert.equal((await read()).signIn, "expired")
|
|
assert.equal((await f.logs()).length, runs)
|
|
// a hang with a live access token is a hang, not a sign-in
|
|
const g = await fixture(t, { TEST_MODE: "hang" })
|
|
const live = { state: "present", fp: "live", canRenew: true, expiresAt: Date.now() + 3600_000, expired: false }
|
|
const other = await createUsageReader(() => g.executable, { ...g.options, timeoutMs: 3000, signIn: async () => live })()
|
|
assert.equal(other.signIn, "kept")
|
|
await f.assertClean()
|
|
await g.assertClean()
|
|
})
|
|
|
|
test("invalid and oversized stdout/stderr reports fail within the output budget", async t => {
|
|
for (const [mode, expected, maxOutputBytes] of [
|
|
["malformed", /invalid usage report/, undefined],
|
|
["oversize", /size limit/, undefined],
|
|
["combined-output", /size limit/, 1000],
|
|
]) {
|
|
const f = await fixture(t, { TEST_MODE: mode })
|
|
await assert.rejects(fetchUsage(f.executable, { ...f.options, ...(maxOutputBytes ? { maxOutputBytes } : {}) }), expected)
|
|
await f.assertClean()
|
|
}
|
|
})
|
|
|
|
test("version/report hangs time out, terminate the process, and leave no workspace", async t => {
|
|
for (const mode of ["version-hang", "hang"]) {
|
|
const f = await fixture(t, { TEST_MODE: mode })
|
|
const run = await timedOut(f, { version: mode === "version-hang" })
|
|
// The runner must actually stop the child, not just stop waiting for it.
|
|
for (const pid of await f.pids(run.children)) await waitForPidExit(pid)
|
|
await f.assertClean()
|
|
}
|
|
})
|
|
|
|
test("cancellation stops a running report and cleans its private workspace", async t => {
|
|
const f = await fixture(t, { TEST_MODE: "hang" })
|
|
const controller = new AbortController()
|
|
const timer = setTimeout(() => controller.abort(), 600)
|
|
try {
|
|
await assert.rejects(fetchUsage(f.executable, { ...f.options, signal: controller.signal }), /cancelled/)
|
|
} finally { clearTimeout(timer) }
|
|
await f.assertClean()
|
|
const alreadyAborted = new AbortController()
|
|
alreadyAborted.abort()
|
|
await assert.rejects(fetchUsage(f.executable, { ...f.options, signal: alreadyAborted.signal }), /cancelled/)
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("terminates owned pipe-inheriting descendants even when the CLI parent already exited", async t => {
|
|
const f = await fixture(t, { TEST_MODE: "descendant" })
|
|
await fetchUsage(f.executable, f.options)
|
|
const pid = Number(await fs.readFile(f.env.TEST_DESCENDANT, "utf8"))
|
|
if (process.platform === "win32") {
|
|
await waitForPidExit(pid)
|
|
} else {
|
|
await waitForNotRunning(pid)
|
|
}
|
|
await f.assertClean()
|
|
})
|
|
|
|
test("missing executables and filesystem failures do not leak local paths", async t => {
|
|
const f = await fixture(t)
|
|
for (const executable of [null, path.join(f.env.TEST_LOG, "missing")]) {
|
|
const result = await createUsageReader(() => executable, f.options)()
|
|
assert.equal(result.windows.length, 0)
|
|
assert.ok(result.error)
|
|
assert.doesNotMatch(result.error, /agy-usage-test-/)
|
|
await f.assertClean()
|
|
}
|
|
const result = await createUsageReader(() => f.executable, { ...f.options, tempRoot: f.executable })()
|
|
assert.ok(result.error)
|
|
assert.doesNotMatch(result.error, /agy-usage-test-/)
|
|
})
|