-
Notifications
You must be signed in to change notification settings - Fork 1.8k
perf(spend): parallelize loads and memoize model build #3105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
92ef8e4
36a65ce
2af83c4
5e049b4
9ee0d30
74ab7d7
866938c
d5e7c9c
70a875b
79d2596
c449182
d7e9b5e
e2df318
e6dd9fb
46e2bd7
8909791
8338abf
98bf3a7
4660c70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import Foundation | ||
|
|
||
| /// Offline Antigravity CLI store (tokscale lesson): counts local SQLite conversations | ||
| /// at `~/.gemini/antigravity-cli/conversations/*.db` without requiring a running | ||
| /// language server or OAuth. Used as a last-resort fallback when live quota | ||
| /// probes and OAuth both fail. | ||
| public enum AntigravityOfflineStore { | ||
| /// Resolve the base Gemini home directory. Mirrors tokscale's `GEMINI_CLI_HOME` | ||
| /// override: if the env var is set and non-empty, use it; otherwise `~/.gemini`. | ||
| public static func geminiHomeDirectory(home: URL, env: [String: String]) -> URL { | ||
| if let override = env["GEMINI_CLI_HOME"]?.trimmingCharacters(in: .whitespacesAndNewlines), | ||
| !override.isEmpty | ||
| { | ||
| return URL(fileURLWithPath: override, isDirectory: true) | ||
| } | ||
| // Provider-specific by design: CLI home path is a fixed external contract. | ||
| return home.appendingPathComponent(".gemini", isDirectory: true) | ||
| } | ||
|
|
||
| public static func conversationsDirectory(home: URL, env: [String: String] = [:]) -> URL { | ||
| self.geminiHomeDirectory(home: home, env: env) | ||
| .appendingPathComponent("antigravity-cli", isDirectory: true) | ||
| .appendingPathComponent("conversations", isDirectory: true) | ||
| } | ||
|
|
||
| /// Tokscale cache alternative: `~/.config/tokscale/antigravity-cache/sessions` | ||
| public static func tokscaleCacheDirectory(home: URL) -> URL { | ||
| home.appendingPathComponent(".config", isDirectory: true) | ||
| .appendingPathComponent("tokscale", isDirectory: true) | ||
| .appendingPathComponent("antigravity-cache", isDirectory: true) | ||
| .appendingPathComponent("sessions", isDirectory: true) | ||
| } | ||
|
|
||
| /// Count offline conversations (`.db` files). Cheap, no SQLite open. | ||
| public static func countConversations( | ||
| home: URL, | ||
| env: [String: String] = [:], | ||
| fileManager: FileManager = .default) -> Int | ||
| { | ||
| let primary = self.conversationsDirectory(home: home, env: env) | ||
| let primaryCount = self.countDBFiles(in: primary, fileManager: fileManager) | ||
| if primaryCount > 0 { return primaryCount } | ||
| // Fallback to tokscale JSONL cache (also counts as offline availability) | ||
| let cache = self.tokscaleCacheDirectory(home: home) | ||
| return self.countJSONLFiles(in: cache, fileManager: fileManager) | ||
| } | ||
|
|
||
| public static func hasOfflineData( | ||
| home: URL, | ||
| env: [String: String] = [:], | ||
| fileManager: FileManager = .default) -> Bool | ||
| { | ||
| self.countConversations(home: home, env: env, fileManager: fileManager) > 0 | ||
| } | ||
|
|
||
| private static func countDBFiles(in directory: URL, fileManager: FileManager) -> Int { | ||
| guard let contents = try? fileManager.contentsOfDirectory( | ||
| at: directory, | ||
| includingPropertiesForKeys: [.isRegularFileKey], | ||
| options: [.skipsHiddenFiles]) else { return 0 } | ||
| return contents.count(where: { $0.pathExtension.lowercased() == "db" }) | ||
| } | ||
|
|
||
| private static func countJSONLFiles(in directory: URL, fileManager: FileManager) -> Int { | ||
| guard let contents = try? fileManager.contentsOfDirectory( | ||
| at: directory, | ||
| includingPropertiesForKeys: [.isRegularFileKey], | ||
| options: [.skipsHiddenFiles]) else { return 0 } | ||
| return contents.count(where: { $0.pathExtension.lowercased() == "jsonl" }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,19 +178,20 @@ public enum AntigravityProviderDescriptor { | |
| let cli = AntigravityCLIHTTPSFetchStrategy() | ||
| let ide = AntigravityStatusFetchStrategy(source: .ide) | ||
| let oauth = AntigravityOAuthFetchStrategy() | ||
| let offline = AntigravityOfflineFetchStrategy() | ||
| switch context.sourceMode { | ||
| case .cli: | ||
| return [app, cli, ide] | ||
| return [app, cli, ide, offline] | ||
| case .oauth: | ||
| return [oauth] | ||
| case .auto: | ||
| if context.selectedTokenAccountID != nil || | ||
| context.env[AntigravityOAuthCredentialsStore.environmentCredentialsKey] != nil || | ||
| self.hasSharedOAuthCredentials(context: context) | ||
| { | ||
| return [app, cli, ide, oauth] | ||
| return [app, cli, ide, oauth, offline] | ||
| } | ||
| return [app, cli, ide] | ||
| return [app, cli, ide, offline] | ||
| case .web, .api: | ||
| return [] | ||
| } | ||
|
|
@@ -786,6 +787,61 @@ struct AntigravityOAuthFetchStrategy: ProviderFetchStrategy { | |
| } | ||
| } | ||
|
|
||
| /// Offline fallback (tokscale lesson): when live probes and OAuth have no data, | ||
| /// surface the local Antigravity CLI conversation count from | ||
| /// `~/.gemini/antigravity-cli/conversations/*.db` as a non-quota snapshot. | ||
| /// This keeps the menu bar from going blank on a fresh install without a running | ||
| /// server and mirrors tokscale's direct SQLite read (no RPC, no `antigravity sync`). | ||
| struct AntigravityOfflineFetchStrategy: ProviderFetchStrategy { | ||
| let id: String = "antigravity.offline" | ||
| let kind: ProviderFetchKind = .localProbe | ||
|
|
||
| func isAvailable(_ context: ProviderFetchContext) async -> Bool { | ||
| // Cheap file existence check; no SQLite open. | ||
| let homeURL = context.env["HOME"] | ||
| .flatMap { $0.isEmpty ? nil : URL(fileURLWithPath: $0, isDirectory: true) } | ||
| ?? FileManager.default.homeDirectoryForCurrentUser | ||
| return AntigravityOfflineStore.hasOfflineData(home: homeURL, env: context.env) | ||
| } | ||
|
|
||
| func fetch(_ context: ProviderFetchContext) async throws -> ProviderFetchResult { | ||
| let homeURL = context.env["HOME"] | ||
| .flatMap { $0.isEmpty ? nil : URL(fileURLWithPath: $0, isDirectory: true) } | ||
| ?? FileManager.default.homeDirectoryForCurrentUser | ||
| let count = AntigravityOfflineStore.countConversations(home: homeURL, env: context.env) | ||
| guard count > 0 else { | ||
| throw AntigravityStatusProbeError.notRunning | ||
| } | ||
| let window = RateWindow( | ||
| usedPercent: 0, | ||
| windowMinutes: nil, | ||
| resetsAt: nil, | ||
| resetDescription: nil) | ||
| let offlineWindow = NamedRateWindow( | ||
| id: "antigravity-offline-conversations", | ||
| title: "Offline · \(count) conversation" + (count == 1 ? "" : "s"), | ||
| window: window, | ||
| usageKnown: false) | ||
| let snapshot = UsageSnapshot( | ||
| primary: nil, | ||
| secondary: nil, | ||
| tertiary: nil, | ||
| extraRateWindows: [offlineWindow], | ||
| updatedAt: Date(), | ||
| identity: ProviderIdentitySnapshot( | ||
| providerID: .antigravity, | ||
| accountEmail: AntigravitySelectedAccountGuard.selectedAccountEmail(context: context), | ||
| accountOrganization: nil, | ||
| loginMethod: "offline")) | ||
|
Comment on lines
+831
to
+835
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a saved Antigravity account is selected and the live/OAuth strategies fail, this fallback counts files in the ambient Useful? React with 👍 / 👎. |
||
| return self.makeResult(usage: snapshot, sourceLabel: "offline") | ||
| } | ||
|
|
||
| func shouldFallback(on _: Error, context _: ProviderFetchContext) -> Bool { | ||
| // Offline is terminal; no further fallback. | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// Guards ambient Antigravity snapshots against the explicitly selected account. | ||
| /// | ||
| /// The local desktop probe and the ``agy`` CLI HTTPS server report whichever | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In auto mode when any OAuth credentials are detected, the offline strategy is placed after OAuth, but
AntigravityOAuthFetchStrategy.shouldFallbackalways returns false andProviderFetchPipeline.fetchimmediately returns an OAuth failure in that case. Therefore, if the app/CLI/IDE probes fail and the saved OAuth credentials are expired or the remote request fails, locally available conversation data is never tried despite this new fallback; allow OAuth failures to fall through in auto mode or place the offline strategy before OAuth.Useful? React with 👍 / 👎.