Reuse an in-memory body on a 401 instead of cloning every request up front,
and return a 401 on a stream that cannot be sent twice. Concurrent 401s share
one token refresh. Keep the token cache in a WeakMap keyed by the fetch
function, and replace Promise.withResolvers in tests so the suite runs on Node 20.
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
The loader's fetch now hardens a generateContent/streamGenerateContent
body the way magpie's gateway builds Gemini contents (codeassist.go):
- contents ending with the model's turn get a user turn after it
("Continue.", or the summary ask when the system prompt is an agent's
compaction prompt);
- contents opening with the model's turn get Gemini CLI's
"[Continuing from previous AI thoughts...]" user turn before it;
- a user turn holding a functionResponse beside the user's text is split,
the responses (and their images) first, the text in a turn of its own.
A body that needs none of it goes byte for byte; countTokens and the
model list are not read. lib/history.mjs, test/history.test.mjs.
Note: magpie's gateway already builds a plugin's @ai-sdk/google request
through buildCodeAssistSent, so those rules reach this plugin from magpie
too (yetone/magpie test TestPluginGeminiNeverAskedEndingWithModel, red
with the gateway's guard removed). This covers a body that reaches the
plugin without them: OpenCode's own @ai-sdk/google, or a magpie path that
skips the build. The reporter's exact request body is not in the issue,
so the tests use #734's conversation shape (Codex compaction after the
model's reply) and #1533's (tool output + compaction prompt in one turn).
Run on mote (mote-run): node --test test/history.test.mjs 5/5; the
package's npm run check + node --test test/*.test.mjs 105 tests, 91 pass,
0 fail (14 skipped, Windows-only); bun scripts/check.mjs exit 0.
Broken by hand: the fetch without hardenRequest fails the end-to-end
test (Vertex fake answers 400 "ending with a model turn").
Not tried: a real Vertex account (none in a sandbox here).
gcp-ai caches an authorized_user ADC's access token per account until 60 s before it expires (refreshed at Google's OAuth token endpoint, one in-flight refresh shared; gcloud mode still asks gcloud every time), retries a model call once with a fresh token on a 401, and probes the endpoint in the background: only a 401 that stands after one refresh holds later requests, for 60 s. README: the install name, token caching, and a source line. Version 0.2.0.
Reviewed three rounds (e78a89a4, e1f83cad, 3959a795). Run at 3959a795 on mote (macOS, Node 22.22.0, Bun 1.4.2, sandbox HOME):
- packages/gcp-ai `node --test test/*.test.mjs`: 100 tests, 86 pass, 0 fail, 14 skipped (no Chrome there; Windows/Linux only). `bun test packages/gcp-ai`: 86 pass, 14 skip, 0 fail. `bun scripts/check.mjs gcp-ai`: ok.
- Broken by hand at this head: dropping the probe's invalidate and second read turns 6 tests red (the stale-token test among them); dropping only the invalidate turns the stale-token test red. Earlier rounds broke the 401 retry, the cache invalidation, the expires_in rule, the account in the token key, the 401-only probe verdict and the in-flight sharing; each went red.
- The direct refresh (refresh token to https://oauth2.googleapis.com/token only) was cleared by the maintainer on the PR.
Not tried: a real Google account (none on the test machines). #713 rebases onto this.
Follow-up to #606's post-merge notes (c78f965):
- startLogin: a userinfo reply with a sub but no (or an invalid) email, or
an empty one, now stops with "The Google account email and subject could
not be confirmed after consent; no new account was recorded" instead of a
TypeError from googleIdentity?.account.toLowerCase().
- knownAccounts skips a leftover 0.1.1 staging dir (adc-pending- plus
mkdtemp's six alphanumerics, no "-"), so it is never read as an account;
an address starting with pending- (adc-pending-user-example-com) is still
found for the same-sub check.
- README: staging is .adc-pending-*, and only googleSubject and accountId
are saved at sign-in; consoleAccount, consoleSubject and
credentialAccount are read when an older record has them.
Not done: the six PNGs under test/evidence stay. The PR description says
they were removed, but the commit doesn't delete them; removing them at the
tip wouldn't shrink a clone, since history keeps them.
Run at the reviewed head fa173ba (main f9b7a1a plus this PR, Bun 1.4.4
38afee6c8, CI's version):
- Linux (WSL magpie-desktop), fresh HOME: bun test 5483 pass, 18 skip,
0 fail across 406 files; bun scripts/check.mjs ok; no bun/node process
left behind.
- macOS (mote): bun test packages/gcp-ai 70 pass, 14 skip, 0 fail; node
--test packages/gcp-ai/test/*.test.mjs 70 pass, 0 fail.
- Broken by hand: without the LEGACY_PENDING_ADC skip, the pending- test
goes red; with .account.toLowerCase() back, the missing-email test goes
red (Bun and Node both).
- Bun 1.4.4's and Node's mkdtemp(".../adc-pending-") names all match
/^adc-pending-[0-9A-Za-z]{6}$/ (200 each).
Not tried: a real Google sign-in.
kidchen94 reported four ways the credits refresh failed on one machine:
Chrome's last-used profile decided whose session a read used, a page
Chrome translated could not be parsed, a JPY billing account timed out
on the wait for a "$" row, and an ambiguous billing account could only be
fixed with the machine-wide VERTEX_BILLING_ACCOUNT.
- Chrome profile: profileDirectories (Google account -> profile
directory), profileDirectory and VERTEX_CHROME_PROFILE_DIRECTORY pass
--profile-directory to every Chrome the plugin opens: reads, the
kept browser and the sign-in window. The name is checked (no path
separators, no "."/"..", no leading "-"), and a kept browser is never
handed to a read pinned to another directory.
- Billing account: billingAccounts maps a project to its billing
account, ahead of the row's billingAccount and the environment. The
discovery error names the project, the accounts the Console showed and
the option to set.
- Currency: billingCurrencies names a billing account's currency (USD,
JPY). Yen is read with no minor unit and shown as "¥8500 JPY". A ¥ is
never taken for yen on its own, since the Console writes yuan the same
way: a ¥ table read as USD names the setting at once. Amounts need
strict comma grouping. The row wait is any cell with a digit, not "$".
- Translation: a page Chrome marked translated (translated-ltr/rtl on
<html>) and a table with non-English column names each give an error
that says so and how to turn translation off, not a timeout or
"session may be expired".
- Cache: the profile directory and the currency map join the cache key
only when set, so existing caches are kept.
- fetch_credits.mjs takes --profile-directory and --currency.
Run at this head on mote (macOS, Node 22, Chrome for Testing 1234):
node --test test/*.test.mjs: 84 tests, 81 pass, 3 skipped (the
Linux/Windows-only ones, as on origin/main's 76), 0 fail.
bun scripts/check.mjs: ok.
Broken by hand: the new tests against origin/main's lib/ and index.mjs.
The JPY and translated browser tests fail with the reporter's symptom
(21s and 41s, "Credits table was not found or is empty; the session may
be expired"), the billing test with "set VERTEX_BILLING_ACCOUNT
explicitly", the profile test with "Chrome used Default, not Profile 1",
and plugin.test.mjs fails to load (billingCurrenciesOf is new).
Not tried: a real Console (a JPY billing account, a page Chrome
translated, a real multi-profile Chrome); the fixtures are tables
served from the test. Not run on Windows or Linux. Chrome's own
translate preference is not written; translation is detected, not
prevented. Currencies other than USD and JPY are refused, not
converted.
take() opened the lock with "wx", got EEXIST, then stat'ed it in abandoned().
When the holder released between the two, stat threw ENOENT and the second
concurrent sign-in failed with it (TestConcurrentSignIns on CI). abandoned()
now returns null for a lock that is gone, and take() opens again without
removing anything, since the path may already be a new holder's.
Run on mote (bun 1.4.2, node 22), HOME=$(mktemp -d):
- packages/gcp-ai node --test: 66 pass, 0 fail; under bun test 66 pass, 10 skip.
- Broken by hand (ENOENT catch removed): the new lock test goes red.
- The concurrent sign-ins test 30 times: 30 green.
- Full bun test 5164 pass, 15 skip, 0 fail; scripts/check.mjs ok.
Not tried: the original race itself is timing-bound and was not reproduced
on demand; the unit test covers the gone-lock path directly.
An account is now identified by its Google subject (OIDC `sub`), not its email. When the email on a Google account changes, its saved ADC and Console credit cache stay matched to that account, and usage shows the current email. A sign-in for another subject that uses the same email gets a key of its own (email#hash). A new sign-in is staged in a `.adc-pending-` dir, and a lock across every managed ADC checks the subject is not registered already. Console identity is read from the page's own JSON (`extractConsoleIdentity`). No new host: userinfo was already called before.
By NGshiyu (#606), reviewed at b344aa1.
Run on the Mac mini with Bun 1.4.2, node 22 and HOME=$(mktemp -d), on b344aa1 merged with main 1a05a77:
- `node --test test/*.test.mjs` in packages/gcp-ai (with VERTEX_CHROME_PATH set to Playwright's Chromium): 75 tests. 72 pass, 0 fail, 3 skipped (Linux/Windows only).
- Whole repo `bun test` (with #599 and #596 merged in too): 4592 pass, 11 skip, 0 fail. `bun scripts/check.mjs`: exit 0.
- Broken by hand: turning off the subject branch in confirmAccount turns "Console subject matching accepts renamed emails" red (71/1).
Not tried: a real Google account whose email changed.
gcp-ai 0.1.1: on Windows, gcloud.cmd is started by its own path (CLOUDSDK_ROOT_DIR, then PATH, quoted entries included), so the batch file's %~dp0 is its own directory and gcloud finds its library. A credit whose status reads "Expiring in N days" counts as spendable until its end date.
Run at 66279e97 (macOS): node --test packages/gcp-ai/test: 44 pass, 3 skipped (Windows-only); origin/main 40 pass. bun scripts/check.mjs gcp-ai ok. The earlier review reproduced the %~dp0 cause and fix on Windows 11 with a fake gcloud.cmd.
Not tried: the new Windows-only tests on Windows at this head (the box was unreachable); the author ran them on Windows 11 with real gcloud 589.0.0.
New package gcp-ai (Yaka88): Vertex models and Google Cloud credits through the user's own gcloud ADC, with multiple accounts, a signed-in model list, and a pipe-only Chrome for the credits read.
Reviewed at 8989b58 against the 10-24 review at 4586c99. I read the whole incremental diff (index.mjs, lib/browser, lib/credentials, lib/lock, test/*). Earlier rounds had already reviewed the rest.
- Tests reaching the network: fixed. test/network.mjs replaces fetch and blocks non-local net/tls/http/dns. `bun test` (40 pass, 1 skip [Linux-only lock test], 0 fail, 106s) ran in a sandbox HOME/TMPDIR behind a dead HTTPS_PROXY, with a preload that logged non-local fetch and connect calls: 0 lines logged. Broken by hand: with the fetch stub removed from "already filed", the guard prints "Unexpected network fetch … userinfo" and bun exits 1. Restored, it exits 0.
- Chrome background traffic: lsof on the test Chrome processes (56 samples during browser.test.mjs, 5 pass) found no inet sockets at all.
- Leftover work after a test: close() now awaits settled, and every sign-in test closes in finally before restoring fetch.
- Smaller points: the lock's age ceiling now comes after the Linux /proc check; settings() is auth > options > env for every field; resolveAdcDirectory and authorizeAdc are removed.
- `bun scripts/check.mjs gcp-ai`: ok. The merge onto main 98985a2 is clean. Loaded into magpie's plugin host (origin/main 4dfd88206) with scripts/try.sh in a sandbox HOME: gcp-ai, 12 models, not signed in.
Not tried: a real Google account or gcloud sign-in, Windows, Linux (the lock test is skipped on macOS). The headless Console credits scrape still runs for every signed-in account, cached on failure, as noted in the earlier review.