agy-cli 0.1.68: bring in the retired Antigravity plugin's accounts

A user asked on X (@tongzhenbao) to move the accounts signed in on the
retired Antigravity plugin (provider "antigravity") into agy-cli in one
click. That plugin signed in with Antigravity's own Google OAuth client
(1071006060591-tmhssin2h21lcre235vtolojh4g403ep), and agy's own
`agy acp --login` link uses the same client id, so agy can take those
refresh tokens as its own: nothing is sent anywhere agy doesn't already
send its sign-in, and no other client is spoken for.

auth.import (magpie's "Import accounts from a file…") now takes:
- the old plugin's saved oauth entry, one, a list or plugin-auth.json's
  map of them (refresh "<token>|<project>", accountId = email);
- agy's own token file / keyring value (go-keyring-base64: prefix too).
A bare refresh token or another tool's export is refused, since it may be
another client's. Each account gets its own agy home with the token file
agy writes (0600), and is added only once `agy models` runs there; one agy
refuses leaves no home. An account already signed in (same email) gives
its existing key back.

Run:
- mote: packages/agy-cli `HOME=$(mktemp -d) bun test`: smoke, signin,
  cloak, install all pass; test/usage.mjs red (3 of 30, different each
  run) and red the same way on pristine origin/main e5cb9598 (8 and 9
  of 30, two runs on mote), green on ubuntu CI; filed separately.
- the new smoke checks are red on the old index.mjs ("plugin.auth.import
  is not a function").
- bun scripts/check.mjs: ok.
- real account, sandbox HOME: the owner's live old-plugin entry copied
  into a sandbox .gemini/antigravity-cli/antigravity-oauth-token;
  `SSH_CONNECTION=… agy models` listed the models and agy refreshed the
  file in the same shape (refresh token not rotated).

Not tried: the import through a running magpie with a real account
(magpie's Providers-page Move comes in a magpie release).
This commit is contained in:
yetone
2026-11-16 01:29:08 +08:00
parent e5cb9598c8
commit 9907c187b2
7 changed files with 238 additions and 2 deletions
+54
View File
@@ -90,6 +90,13 @@ if (argv[0] === "acp" && argv.includes("--login")) {
process.stderr.write('couldn\'t log in: token exchange failed: oauth2: "invalid_grant" "Bad Request"\n')
process.exit(1)
}
// an account home whose token file Google turned away (a revoked sign-in)
const homeToken = path.join(where.home, ".gemini", "antigravity-cli", "antigravity-oauth-token")
if (argv.includes("models") && where.ssh && fs.existsSync(homeToken) && fs.readFileSync(homeToken, "utf8").includes("1//revoked")) {
log({ type: "models", revoked: true })
process.stderr.write('Error: oauth2: "invalid_grant" "Token has been expired or revoked."\n')
process.exit(1)
}
if (argv.includes("models") && fs.existsSync(signedOut)) {
log({ type: "models", update: process.env.AGY_CLI_DISABLE_AUTO_UPDATE, signedOut: true })
process.stdout.write("Fetching available models...\n")
@@ -838,6 +845,53 @@ const legacyModel = "agy-cli/gemini-test-low"
}
}
{
// tongzhenbao on X: the retired Antigravity plugin's accounts move here
// in one go. Its sign-ins were made with Antigravity's own Google client,
// the one agy signs in with, so each becomes an account home whose token
// file is that sign-in, kept once agy lists its models with it.
const savedState = process.env.XDG_STATE_HOME
const xdg = path.join(stateDir, "xdg-import")
process.env.XDG_STATE_HOME = xdg
const accounts = path.join(xdg, "magpie-agy-bridge", "accounts")
try {
check("accounts can be brought in from a file", typeof plugin.auth.import === "function" && /Antigravity/.test(plugin.auth.importFrom) && !!plugin.auth.importFroms?.zh)
const old = {
antigravity: { type: "oauth", refresh: "1//moved-a|proj-a", access: "ya29.a", expires: 1700000000000, accountId: "moved-a@example.com" },
"antigravity#2": { type: "oauth", refresh: "1//revoked|proj-b", access: "", expires: 0, accountId: "revoked@example.com" },
}
const got = await plugin.auth.import([JSON.stringify(old), JSON.stringify({ type: "oauth", refresh: "1//moved-a|proj-a", accountId: "moved-a@example.com" })])
check("each account is tried once", Array.isArray(got) && got.length === 2, JSON.stringify(got))
const [moved, revoked] = got
check("a moved sign-in is kept as an account home, named by its email",
moved?.type === "success" && moved.key === `agy:${moved.metadata?.agyHome}` && moved.metadata?.email === "moved-a@example.com", JSON.stringify(moved))
const home = path.join(accounts, moved?.metadata?.agyHome ?? "none")
const file = path.join(home, ".gemini", "antigravity-cli", "antigravity-oauth-token")
const kept = JSON.parse(fs.readFileSync(file, "utf8"))
check("its token file is agy's own shape, with the plugin's refresh token and no project",
kept.token?.refresh_token === "1//moved-a" && kept.token.access_token === "ya29.a" && kept.token.expiry === new Date(1700000000000).toISOString() && kept.auth_method === "consumer" && kept.id_token_email === "moved-a@example.com", JSON.stringify(kept))
if (process.platform !== "win32") check("the token file is the user's alone", (fs.statSync(file).mode & 0o077) === 0)
const probe = logs().filter(event => event.type === "models" && event.home === home)
check("agy was asked with it in the account's home, from the file", probe.length >= 1 && probe.every(event => event.ssh === "127.0.0.1 0 127.0.0.1 0"), JSON.stringify(probe))
check("a sign-in agy can't use is turned away and leaves no home",
revoked?.type === "failed" && revoked.accountId === "revoked@example.com" && /invalid_grant/.test(revoked.error) && fs.readdirSync(accounts).length === 1, JSON.stringify(revoked))
const again = await plugin.auth.import([JSON.stringify(old.antigravity)])
check("moved twice, an account is named again, not added twice",
again?.[0]?.type === "success" && again[0].key === moved.key && fs.readdirSync(accounts).length === 1, JSON.stringify(again))
const agyOwn = JSON.stringify({ token: { access_token: "ya29.c", refresh_token: "1//own-c", expiry: "2026-01-01T00:00:00Z", token_type: "Bearer" }, auth_method: "consumer", id_token_email: "own-c@example.com" })
const fromFile = await plugin.auth.import(["go-keyring-base64:" + Buffer.from(agyOwn).toString("base64")])
check("agy's own stored sign-in is taken too", fromFile?.[0]?.type === "success" && fromFile[0].metadata?.email === "own-c@example.com", JSON.stringify(fromFile))
const other = await plugin.auth.import([JSON.stringify({ refresh_token: "1//someone-else", client_id: "x.apps.googleusercontent.com" }), "1//bare-token"])
check("a refresh token that may be another client's isn't taken", other?.type === "failed" && /no Antigravity sign-in/.test(other.error), JSON.stringify(other))
} finally {
if (savedState === undefined) delete process.env.XDG_STATE_HOME
else process.env.XDG_STATE_HOME = savedState
}
}
{
// signed out: agy's own sign-in gives the link and takes the pasted code
const signedOut = path.join(stateDir, "signed-out")
+3
View File
@@ -42,6 +42,7 @@ export const TOOL_SCHEMA_FILE = path.join(REPO_DIR, "tool-schema.mjs")
export const TOOL_BUDGET_FILE = path.join(REPO_DIR, "tool-budget.mjs")
export const INSTALL_FILE = path.join(REPO_DIR, "install.mjs")
export const ACCOUNT_FILE = path.join(REPO_DIR, "account.mjs")
export const IMPORT_FILE = path.join(REPO_DIR, "import.mjs")
export const MESSAGES_URL = "agy://local/v1/messages"
export const COUNT_TOKENS_URL = "agy://local/v1/messages/count_tokens"
export const CATALOG_TTL_MS = 5 * 60 * 1000
@@ -549,6 +550,7 @@ export async function createBridgeKit(options = {}) {
const toolBudgetUrl = pathToFileURL(TOOL_BUDGET_FILE).href
const installUrl = pathToFileURL(INSTALL_FILE).href
const accountUrl = pathToFileURL(ACCOUNT_FILE).href
const importUrl = pathToFileURL(IMPORT_FILE).href
const linker = (specifier) => {
const key = specifier.startsWith("node:") || specifier.startsWith("./") || specifier === RUNTIME_SPECIFIER
? specifier
@@ -568,6 +570,7 @@ export async function createBridgeKit(options = {}) {
else if (key === "./tool-budget.mjs") module = sourceModule(TOOL_BUDGET_FILE, "", toolBudgetUrl)
else if (key === "./install.mjs") module = sourceModule(INSTALL_FILE, "", installUrl)
else if (key === "./account.mjs") module = sourceModule(ACCOUNT_FILE, "", accountUrl)
else if (key === "./import.mjs") module = sourceModule(IMPORT_FILE, "", importUrl)
else if (key.startsWith("node:")) module = builtinModule(key, overrides[key])
else throw new Error(`testkit: 未登记的依赖 ${specifier}`)
cache.set(key, module)