The "ADC setup reads gcloud's complete consent URL ..." test's
writesThenFails fake started the ADC write on one 5ms timer (void, not
awaited) and exited on another 40ms timer. beginAdcLogin stats the ADC
file on a non-zero exit to tell "saved, then the quota probe failed"
from a failed consent, so when the write took more than ~35ms on a busy
machine the exit was read before the file existed and the test saw
"gcloud could not set up ADC". Real gcloud has written the file before
it exits, so the plugin is right and the race was the test's: the fake
now emits exit only once its write has resolved.
Run on mote (Bun 1.4.2, TMPDIR and HOME inside the run dir):
- reproduced: the write's timer moved 5ms -> 60ms on the old test: red
("gcloud could not set up ADC") every run.
- fixed, with a 100ms sleep injected into fixture.setAdc: green.
- broken by hand (the fake's exit no longer awaits the write), same
100ms injection: red. Injection removed afterwards.
- bun test packages/gcp-ai: 95 pass, 14 skip, 0 fail.
- bun scripts/check.mjs gcp-ai: ok.
- repo-root bun test: 6867 pass, 0 fail (499 files).
Only a test changed, so no version bump.
Fixes#835
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>
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.
48a5f21's "the usage hook takes the billing, currency and profile maps"
test signed in with an accountId, so each of its usage calls first asked
gcloud for a token. On the publish run's ubuntu runner that took past
bun's 5s per-test limit, the test job went red, and gcp-ai 0.1.5 was
not published. The test now has no account, so nothing is asked before
the options are read and refused; it checks the same errors.
Run at this head on mote: `HOME=$(mktemp -d) bun test` (as publish.yml
runs it): 5195 pass, 0 fail, the test in 1.14ms. node --test
packages/gcp-ai/test/*.test.mjs: 84 tests, 81 pass, 3 skipped, 0 fail.
bun scripts/check.mjs: ok.
Not tried: the ubuntu runner itself; the first publish run on it will
show it.
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.
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.