feat: 动态获取 Antigravity User-Agent 版本号#2535
Conversation
Fetch the latest version from the antigravity auto-updater releases endpoint and cache it for 6 hours. Falls back to 1.21.9 if the API is unreachable or returns unexpected data.
There was a problem hiding this comment.
Code Review
This pull request introduces a dynamic version fetching mechanism for the 'antigravity' service, replacing hardcoded User-Agent strings with a centralized utility in a new misc package. The implementation includes a caching layer with a 6-hour TTL and a fallback version. Feedback highlights several critical improvements: moving the network request outside of the write lock to avoid performance bottlenecks, implementing io.LimitReader to prevent potential memory exhaustion from large API responses, and reducing the cache duration for failed fetches to ensure quicker recovery from transient network issues. Additionally, it is recommended to remove the now-obsolete defaultAntigravityAgent constant.
| antigravityVersionMu.Lock() | ||
| defer antigravityVersionMu.Unlock() | ||
|
|
||
| // Double-check after acquiring write lock. | ||
| if cachedAntigravityVersion != "" && time.Now().Before(antigravityVersionExpiry) { | ||
| return cachedAntigravityVersion | ||
| } | ||
|
|
||
| version := fetchAntigravityLatestVersion() | ||
| cachedAntigravityVersion = version | ||
| antigravityVersionExpiry = time.Now().Add(antigravityVersionCacheTTL) | ||
| return version |
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "sync" | ||
| "time" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| ) |
There was a problem hiding this comment.
| version := fetchAntigravityLatestVersion() | ||
| cachedAntigravityVersion = version | ||
| antigravityVersionExpiry = time.Now().Add(antigravityVersionCacheTTL) |
| } | ||
|
|
||
| var releases []antigravityRelease | ||
| if err := json.NewDecoder(resp.Body).Decode(&releases); err != nil { |
There was a problem hiding this comment.
| antigravityClientID = "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com" | ||
| antigravityClientSecret = "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" | ||
| defaultAntigravityAgent = "antigravity/1.21.9 darwin/arm64" | ||
| defaultAntigravityAgent = "antigravity/1.21.9 darwin/arm64" // fallback only; overridden at runtime by misc.AntigravityUserAgent() |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8d5e470e1f
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| cachedAntigravityVersion = version | ||
| antigravityVersionExpiry = time.Now().Add(antigravityVersionCacheTTL) |
There was a problem hiding this comment.
Keep last cached version when refresh falls back
When the cache expires, any transient failure in fetchAntigravityLatestVersion() returns the fallback 1.21.9, and this code unconditionally stores that fallback and resets the 6-hour TTL. That discards a previously valid fetched version and can pin all requests to an older UA for hours after a brief updater outage, which risks avoidable request failures if upstream starts rejecting older versions. Only update the cache when the refresh succeeds, otherwise keep the last known good version.
Useful? React with 👍 / 👎.
| return cachedAntigravityVersion | ||
| } | ||
|
|
||
| version := fetchAntigravityLatestVersion() |
There was a problem hiding this comment.
Move network fetch out of the version cache lock
AntigravityLatestVersion() calls fetchAntigravityLatestVersion() while holding the write lock, and that fetch can block on network I/O up to the 10-second timeout. Because this function is used by resolveUserAgent() on the request path, concurrent requests can stall behind the lock at cold start and each cache refresh window. Refreshing outside the critical section (or serving stale while refreshing) avoids periodic head-of-line latency spikes.
Useful? React with 👍 / 👎.
1.主要改动 (What)
internal/misc/antigravity_version.go):AntigravityLatestVersion():通过 HTTP 请求访问antigravity-auto-updater接口。sync.RWMutex保证并发安全,避免频繁请求 API。1.21.9。internal/runtime/executor/antigravity_executor.go):resolveUserAgent函数,由原来的直接返回常量改为调用misc.AntigravityUserAgent()。defaultAntigravityAgent标记为仅作为 Fallback 使用。2.测试验证 (Testing)
1.21.9。