@heyhuynhgiabuu/pi-oauth-antigravity 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.1.1] - 2026-09-15
9
+
10
+ ### Fixed
11
+ - **Transient throttles are no longer reported as quota exhaustion.** Only `Individual quota reached` (plan quota) maps to "Quota reached"; shared-capacity smoothing, per-minute rate limits, and generic `RESOURCE_EXHAUSTED` now report a transient throttle with the HTTP status and backend message preserved, so the host's retry classifier can retry them instead of failing the turn. Account-limit phrases inside the quoted backend text (e.g. `quota exceeded`) are neutralized so a transient throttle is not misclassified as non-retryable.
12
+ - **Transient 429s retry in place with backoff.** A throttled request is retried up to twice on the same endpoint (2s, then 4s; override via `ANTIGRAVITY_THROTTLE_BASE_DELAY_MS`) instead of hopping to the sandbox/production endpoints, which multiplied load on an already-throttled account.
13
+ - **Plan-quota 429s fail fast** without retrying or hopping endpoints, since the limit cannot clear before its reset time.
14
+ - **Backoff is abort-aware**: cancelling a turn stops the retry immediately.
15
+
8
16
  ## [0.1.0] - 2026-09-02
9
17
 
10
18
  ### Added
@@ -3,6 +3,8 @@ import { StopReason } from "../types/enums.js";
3
3
  import { ANTIGRAVITY_API, type AntigravityGenerateRequest, type AntigravityStreamOptions, type GeminiContent, type GeminiFunctionDeclaration } from "../types/types.js";
4
4
  import { type AntigravitySessionState } from "../utils/util.js";
5
5
  export { ANTIGRAVITY_API };
6
+ /** Plan quota is deterministic; everything else on a 429 is a transient throttle. */
7
+ export declare function isPlanQuotaError(status: number | undefined, text: string): boolean;
6
8
  /** Exported for unit tests. */
7
9
  export declare function convertMessages(model: Model<Api>, context: Context, runtimeModel: string): GeminiContent[];
8
10
  /**
@@ -12,6 +12,51 @@ const ANTIGRAVITY_SYSTEM_INSTRUCTION = "You are Antigravity, a powerful agentic
12
12
  "You are pair programming with a user to solve coding tasks. Be concise, practical, and tool-aware.";
13
13
  const ANTIGRAVITY_NO_PREAMBLE_INSTRUCTION = 'CRITICAL: NEVER output rule checks, formatting guidelines, constraint checklists (e.g. "No emdashes"), or your thinking/personality preambles in the final response. Output only the final response.';
14
14
  let toolCallCounter = 0;
15
+ /**
16
+ * Transient 429s (shared-capacity smoothing, per-minute rate limits, generic
17
+ * RESOURCE_EXHAUSTED) are retried on the same endpoint with backoff. Only
18
+ * "Individual quota reached" is plan quota, which cannot clear before its reset time
19
+ * and therefore fails fast instead of burning the host's retry budget.
20
+ */
21
+ const THROTTLE_MAX_RETRIES = 2;
22
+ const DEFAULT_THROTTLE_BASE_DELAY_MS = 2000;
23
+ /** Override the 429 backoff via ANTIGRAVITY_THROTTLE_BASE_DELAY_MS (ops/tests). */
24
+ function throttleBaseDelayMs() {
25
+ const raw = Number(antigravityEnv("THROTTLE_BASE_DELAY_MS"));
26
+ return Number.isFinite(raw) && raw >= 0 ? raw : DEFAULT_THROTTLE_BASE_DELAY_MS;
27
+ }
28
+ /** Abort-aware sleep so a cancelled turn never waits out a backoff. */
29
+ function sleep(ms, signal) {
30
+ return new Promise((resolve, reject) => {
31
+ if (signal?.aborted) {
32
+ reject(new Error("Request was aborted"));
33
+ return;
34
+ }
35
+ const timer = setTimeout(() => {
36
+ signal?.removeEventListener("abort", onAbort);
37
+ resolve();
38
+ }, ms);
39
+ function onAbort() {
40
+ clearTimeout(timer);
41
+ reject(new Error("Request was aborted"));
42
+ }
43
+ signal?.addEventListener("abort", onAbort, { once: true });
44
+ });
45
+ }
46
+ /** Plan quota is deterministic; everything else on a 429 is a transient throttle. */
47
+ export function isPlanQuotaError(status, text) {
48
+ return status === 429 && /Individual quota reached/i.test(text);
49
+ }
50
+ /**
51
+ * pi chooses auto-retry by matching the error text (`isRetryableAssistantError`) and
52
+ * checks account-limit phrases like "quota exceeded" BEFORE retryable ones. Google's
53
+ * transient per-minute 429 body starts with exactly that phrase, so quoting it verbatim
54
+ * would make the host skip retrying a throttle. Keep the quote readable but unmatchable.
55
+ */
56
+ const HOST_ACCOUNT_LIMIT_PHRASES = /\bquota[\s-]+exceeded\b/gi;
57
+ function quoteBackendMessage(text) {
58
+ return text.replace(HOST_ACCOUNT_LIMIT_PHRASES, "quota-limit hit");
59
+ }
15
60
  function sanitizeToolCallId(id, fallbackName) {
16
61
  const cleaned = id.replace(/[^a-zA-Z0-9_-]/g, "_");
17
62
  const capped = cleaned.slice(0, 64);
@@ -450,7 +495,8 @@ export function mapStopReason(reason) {
450
495
  }
451
496
  /** Exported for unit tests. */
452
497
  export function friendlyAntigravityError(status, text) {
453
- const msg = redactSecrets(jsonOrTextError(text)).slice(0, 500);
498
+ const full = redactSecrets(jsonOrTextError(text));
499
+ const msg = full.slice(0, 500);
454
500
  if (status === 400) {
455
501
  if (/API key not valid|API_KEY_INVALID/i.test(msg)) {
456
502
  return "Antigravity login expired or credentials are invalid. Next: run /login antigravity, then retry.";
@@ -485,13 +531,17 @@ export function friendlyAntigravityError(status, text) {
485
531
  }
486
532
  if (status === 429) {
487
533
  const wait = msg.match(/Resets? in ([^.\n]+)/i)?.[1]?.trim();
488
- if (/Individual quota reached/i.test(msg)) {
534
+ if (isPlanQuotaError(status, full)) {
489
535
  return `Quota reached. Please wait ${wait || "for reset"}. Next: switch models or try again after reset.`;
490
536
  }
537
+ // Only "Individual quota reached" is plan quota. Shared-capacity smoothing,
538
+ // per-minute rate limits, and generic RESOURCE_EXHAUSTED all contain "quota" too,
539
+ // so never tell the user their quota is gone on those.
540
+ const backendNote = msg ? ` Backend said: ${quoteBackendMessage(msg)}` : "";
491
541
  if (/quota/i.test(msg)) {
492
- return `Quota reached.${wait ? ` Please wait ${wait}.` : ""} Next: switch models or retry later.`;
542
+ return `Antigravity throttled this request (transient, not plan quota)${wait ? `; resets in ${wait}` : ""}.${backendNote}`;
493
543
  }
494
- return `Rate limited by Antigravity. Next: wait a bit and retry.${wait ? ` Reset: ${wait}.` : ""}`;
544
+ return `Rate limited by Antigravity. Next: wait a bit and retry.${wait ? ` Reset: ${wait}.` : ""}${backendNote}`;
495
545
  }
496
546
  if (status === 500) {
497
547
  return "Antigravity had an internal server error. Next: retry in a moment or switch models.";
@@ -757,7 +807,7 @@ export function streamAntigravity(model, context, options) {
757
807
  throw new Error("Request was aborted");
758
808
  if (emptyAttempt > 0) {
759
809
  const delay = 500 * 2 ** (emptyAttempt - 1);
760
- await new Promise((res) => setTimeout(res, delay));
810
+ await sleep(delay, opts.signal);
761
811
  }
762
812
  for (let candIdx = 0; candIdx < runtimeCandidates.length; candIdx++) {
763
813
  runtimeModel = runtimeCandidates[candIdx];
@@ -765,22 +815,39 @@ export function streamAntigravity(model, context, options) {
765
815
  const body = JSON.stringify(buildRequest(model, context, projectId, opts, runtimeModel, sessionState));
766
816
  for (const endpoint of endpointCandidates(sessionState.lastGoodEndpoint)) {
767
817
  setLastEndpoint(endpoint);
768
- response = await antigravityFetch(`${endpoint}/v1internal:streamGenerateContent?alt=sse`, {
769
- method: "POST",
770
- headers: requestHeaders,
771
- body,
772
- signal: opts.signal,
773
- });
774
- setLastStatus(response.status);
775
- if (response.ok) {
776
- sessionState.lastGoodEndpoint = endpoint;
777
- persistAntigravitySessions();
818
+ let throttleAttempt = 0;
819
+ for (;;) {
820
+ response = await antigravityFetch(`${endpoint}/v1internal:streamGenerateContent?alt=sse`, {
821
+ method: "POST",
822
+ headers: requestHeaders,
823
+ body,
824
+ signal: opts.signal,
825
+ });
826
+ setLastStatus(response.status);
827
+ if (response.ok) {
828
+ sessionState.lastGoodEndpoint = endpoint;
829
+ persistAntigravitySessions();
830
+ break;
831
+ }
832
+ lastText = await response.text();
833
+ // Plan quota cannot clear before its reset time: fail fast.
834
+ if (isPlanQuotaError(response.status, lastText))
835
+ break;
836
+ // Every other 429 is a transient throttle. Back off on the same endpoint
837
+ // instead of hopping regions, which multiplies load on a throttled account.
838
+ if (response.status === 429 && throttleAttempt < THROTTLE_MAX_RETRIES) {
839
+ await sleep(throttleBaseDelayMs() * 2 ** throttleAttempt, opts.signal);
840
+ throttleAttempt += 1;
841
+ continue;
842
+ }
778
843
  break;
779
844
  }
780
- lastText = await response.text();
781
- if (response.status === 429 && /Individual quota reached/i.test(lastText))
845
+ if (response.ok)
846
+ break;
847
+ // 429s were already retried above; every endpoint shares the same throttle.
848
+ if (response.status === 429)
782
849
  break;
783
- if (![403, 404, 429, 500, 502, 503, 504].includes(response.status))
850
+ if (![403, 404, 500, 502, 503, 504].includes(response.status))
784
851
  break;
785
852
  }
786
853
  if (response?.ok)
@@ -824,7 +891,10 @@ export function streamAntigravity(model, context, options) {
824
891
  }
825
892
  }
826
893
  const friendly = friendlyAntigravityError(response?.status, lastText);
827
- if (response?.status === 429 && /Quota reached\./i.test(friendly)) {
894
+ // Plan quota only clears at reset: keep the bare message so the host does not
895
+ // retry it. Transient throttles keep the status + backend text so they stay
896
+ // diagnosable and retryable.
897
+ if (isPlanQuotaError(response?.status, lastText)) {
828
898
  throw new Error(friendly);
829
899
  }
830
900
  throw new Error(`Antigravity API error (${response?.status ?? "no response"}, ${formatRequestDiagnostics({ projectId, runtimeModel })}): ${friendly}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heyhuynhgiabuu/pi-oauth-antigravity",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Google Antigravity OAuth provider extension for Pi with Gemini 3.8, prompt cache session affinity, and trajectory chaining.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -59,8 +59,9 @@
59
59
  "undici": "^8.10.0"
60
60
  },
61
61
  "devDependencies": {
62
- "@earendil-works/pi-ai": "^0.84.4",
63
- "@earendil-works/pi-coding-agent": "^0.84.4",
62
+ "@earendil-works/pi-ai": "^0.85.0",
63
+ "@earendil-works/pi-coding-agent": "^0.85.0",
64
+ "@earendil-works/pi-server": "^0.85.0",
64
65
  "@types/node": "^22.20.1",
65
66
  "tsx": "^4.20.6",
66
67
  "typescript": "^5.9.3"