Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat: track and reconcile Antigravity image generation and transcript usage
[._.]/ Adam Eivyc1cee12fdb94
diff --git a/client/src/pages/UsagePage.jsx b/client/src/pages/UsagePage.jsxindex 2c3155f9c6..39fca16e5d 100644--- a/client/src/pages/UsagePage.jsx+++ b/client/src/pages/UsagePage.jsx@@ -41,6 +41,15 @@ function grokRepairLine(grok) {return grok.note ? `${added} ${grok.note}` : added;}+/** What Reconcile now added to the Antigravity ledger. Absent until that job finishes. */+function agyRepairLine(agy) {+ if (!agy) return '';+ const parts = [];+ if (agy.images) parts.push(`${formatCount(agy.images)} image${agy.images === 1 ? '' : 's'}`);+ const added = parts.length ? `Added ${joinAnd(parts)}.` : 'Antigravity usage was already recorded.';+ return agy.note ? `${added} ${agy.note}` : added;+}+// A subscription is one account across every federated instance, but each// instance can only read its own CLI's panel. When peers have contributed a// reading, say so on the card and name them — the meters are the freshest@@ -710,7 +719,7 @@ function InternalUsageMetrics() {Reconcile historical usage</div><p className="text-xs text-gray-500 mt-1">- Re-read saved transcripts and Grok sessions to replace older estimates and record Imagine and long-context charges. This changes recorded history but makes no provider calls.+ Re-read saved transcripts, Grok sessions, and Antigravity media to replace older estimates and record media and long-context charges. This changes recorded history but makes no provider calls.</p>{backfill?.status === 'running' && (<p className="text-xs text-port-accent mt-1" role="status">@@ -725,6 +734,9 @@ function InternalUsageMetrics() {{backfill?.grok && (backfill.status === 'complete' || backfill.status === 'error') && (<p className="text-xs text-port-success mt-1" role="status">{grokRepairLine(backfill.grok)}</p>)}+ {backfill?.agy && (backfill.status === 'complete' || backfill.status === 'error') && (+ <p className="text-xs text-port-success mt-1" role="status">{agyRepairLine(backfill.agy)}</p>+ )}{backfill?.status === 'error' && (<p className="text-xs text-port-error mt-1" role="alert">{backfill.error}</p>)}diff --git a/client/src/pages/UsagePage.test.jsx b/client/src/pages/UsagePage.test.jsxindex 0e3c673fe2..93d7d85559 100644--- a/client/src/pages/UsagePage.test.jsx+++ b/client/src/pages/UsagePage.test.jsx@@ -168,6 +168,23 @@ describe('UsagePage historical reconciliation', () => {expect(await screen.findByText('Grok usage was already recorded.')).toBeInTheDocument();});+ it('shows the Antigravity rows reconcile added', async () => {+ api.startUsageBackfill.mockResolvedValue({+ status: 'complete',+ corrected: 1,+ agy: {+ added: 4,+ images: 4,+ backedUp: true,+ note: null,+ },+ });+ render(<MemoryRouter><UsagePage /></MemoryRouter>);+ fireEvent.click(await screen.findByRole('button', { name: 'Reconcile now' }));+ expect(await screen.findByText('Corrected 1 run.')).toBeInTheDocument();+ expect(await screen.findByText('Added 4 images.')).toBeInTheDocument();+ });+it('starts only from the explicit user action and reports completion', async () => {render(<MemoryRouter><UsagePage /></MemoryRouter>);diff --git a/scripts/backfill-antigravity-usage.js b/scripts/backfill-antigravity-usage.jsnew file mode 100755index 0000000000..b597154f32--- /dev/null+++ b/scripts/backfill-antigravity-usage.js@@ -0,0 +1,66 @@+#!/usr/bin/env node+/**+ * Dry-run, or stopped-server, wrapper around the Antigravity usage repair.+ *+ * While PortOS is running, use Reconcile now on the Usage page. That path+ * writes the server's in-memory ledger, so the next save cannot drop the rows.+ * This script prints the same plan and, with --apply, calls that same repair+ * only when this process is the one that owns data/usage.json. Stop PortOS+ * first. A running server keeps the ledger in memory and will overwrite a+ * file this process saved.+ *+ * node scripts/backfill-antigravity-usage.js+ * node scripts/backfill-antigravity-usage.js --home /path/to/home --data /path/to/data+ * node scripts/backfill-antigravity-usage.js --apply+ */++import { homedir } from 'os';+import { dirname, join, resolve } from 'path';+import { fileURLToPath } from 'url';+import { PATHS } from '../server/lib/fileUtils.js';+import { applyAgyUsageRepair, planAgyUsageRepairFromDisk } from '../server/services/agyUsageBackfill.js';+import { summarizeAgyRepairAdditions } from '../server/services/agyUsageRepair.js';++const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');++function argValue(argv, name) {+ const index = argv.indexOf(name);+ return index >= 0 ? argv[index + 1] : null;+}++function printPlan(plan) {+ const counts = summarizeAgyRepairAdditions(plan.additions);+ console.log(`Antigravity usage repair: ${counts.added} addition(s).`);+ for (const item of plan.additions) {+ console.log(` ${item.day} ${item.repairId} model=${item.record.model || '(default)'} images=${item.record.images || 0}`);+ }+ console.log(`Summary: ${counts.images} image row(s).`);+ for (const note of plan.notes) console.log(`Note: ${note}`);+}++async function main() {+ const argv = process.argv.slice(2);+ const apply = argv.includes('--apply');+ const home = argValue(argv, '--home') || homedir();+ const dataDir = argValue(argv, '--data') || join(ROOT, 'data');+ if (!apply) {+ printPlan(await planAgyUsageRepairFromDisk({ home, dataDir }));+ console.log('Dry run. Reconcile now on the Usage page applies this while PortOS is running. --apply writes the ledger only after PortOS is stopped.');+ return;+ }+ if (resolve(dataDir) !== resolve(PATHS.data)) {+ console.error(`❌ --apply writes ${PATHS.data}/usage.json. Re-run without --data, or pass --data ${PATHS.data}.`);+ process.exitCode = 1;+ return;+ }+ const result = await applyAgyUsageRepair({ home, dataDir });+ console.log(`Applied ${result.added} addition(s): ${result.images} image row(s).`);+ if (result.note) console.log(`Note: ${result.note}`);+ if (result.backedUp) console.log(`Backed up the ledger to ${result.backupDir}`);+ if (result.added === 0) console.log('Nothing to apply.');+}++main().catch((err) => {+ console.error(`❌ Antigravity usage backfill failed: ${err.message}`);+ process.exitCode = 1;+});diff --git a/server/lib/README.md b/server/lib/README.mdindex def201227a..dd344b8831 100644--- a/server/lib/README.md+++ b/server/lib/README.md@@ -300,7 +300,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub| `modelPinMembership.js` | THE model-pin membership rule, and the local-daemon classification it rests on — `modelPinIsOffered(provider, model)` (may this provider be handed this model? a configured-default sentinel, an empty catalog and a local daemon's cached snapshot are all pass-throughs, so a freshly pulled Ollama model is never rejected as "not offered"; beyond those the match is exact, with two same-model-spelled-differently tolerances: agy compares on BASE ids, since `--effort` carries the tier and the spawner splits a suffixed pin into exactly that, and OpenCode's `namespace/model` form matches a bare pin, since pins are stored bare and namespaced at spawn), plus `localRuntimeKind(provider)` (the `*Backed` markers first, then the endpoint/name classifier; `orcarouter` excluded as a remote API) and `localBackendForProvider`. Split out of `localProviderRuntime.js` — which re-exports all three, so existing importers are untouched — because the browser needs this rule for a picker and must not pull that module's endpoint graph (`opencodeConfig.js` → `zod`, not a client dependency) to get it. Carries `LOCAL_RUNTIME_MANAGE_URLS` for the same reason (#7414): the route that manages each local runtime — Models → Runtimes for the servers, Models → LLMs for the weights catalog, `null` for the operator-owned compose projects — read by `LOCAL_RUNTIMES[*].manageUrl` and by the client surfaces that must name the same page. Consumed by `cliProviderRun.js`, `routes/brainSettings.js`, `services/agentProviderResolution.js`, `modelPinReconcile.js` and `client/src/utils/providerModels.js` — one answer for all five (#7327). || `modelPinReconcile.js` | Retired-model-pin AUDIT over the shared membership rule (#7315) — `providerCatalogListsModel(provider, modelId)` (is a stored pin still served? `modelPinIsOffered` from `modelPinMembership.js` verbatim, plus the one case a retirement check answers differently from a spawn-time one: nothing is pinned at all. The agy base-id and OpenCode namespace tolerances used to be layered HERE above a stricter rule, which made the audit and the three spawn-time callers disagree about the same pin; they moved down into the rule in #7327), `catalogOfferings` (the listed ids with sentinels dropped, agy collapsed to base ids for a picker), `reconcileModelPins(pins, providers)` — which pin descriptors name a model their provider no longer lists — and, since #7339, the two helpers that make a pin's provider field singular-or-plural-agnostic: `pinProviderIds(pin)` (THE coercion from `providerId` / `providerIds` to a trimmed id list, called by the audit's descriptor builder too so the two layers cannot drift) and `pinProviderNames(pin, providers)` (how a surface names those records — shared by the warnings panel and the notification card so they cannot describe one pin differently). A reviewer pin names a BINARY several records front, so it is stale only when EVERY named record stops listing the model. Every UNKNOWN case (no pin, empty catalog, unresolved provider) answers "still listed": a false "your pin is gone" tells the user to change a setting that works. Derived on read, never persisted — see `services/modelPinAudit.js`. || `reviewerProviderMatchers.js` | Which PROVIDER RECORDS front each model-taking reviewer's binary — `REVIEWER_PROVIDER_MATCHERS` (slug → record predicates, in preference order), `providersForReviewer(reviewer, providers)` (every match, de-duped, preference-ordered so `[0]` is the record whose default a picker shows) and `reviewerProviderIds`. A reviewer slug names a BINARY and PortOS ships more than one record per binary (`claude` spans `claude-code` + `claude-code-tui`, `grok` spans `grok-cli` + its TUI), so both the picker's option list and the retired-pin audit union every match: sourcing from one record makes the answer hostage to that record's staleness, and judging a pin against one produces a false retirement for a model the other still lists. Deliberately excludes Bedrock/Vertex and the `opencode-<backend>` presets (ids that resolve only under another record's environment). Imported directly by `client/src/hooks/useReviewerModelOptions.js` — one table, no mirror to drift (#7339). |-| `modelPricing.js` | Per-model API billing rates for informational cost estimates — `resolveModelRates(providerId, model)` (exact → family regex → provider default → blended fallback, with a `matched` tier; also derives cache tiers), `pricingAsOfForModel(model)`, `isFreeProvider`, `estimateCostUsd(tokensIn, tokensOut, rates, cache?)` (uncached input plus separately priced cache tokens), `estimateGrokImagineCost(model, units)` (published Imagine image / second / input-image rates; longest model id wins), `GROK_LONG_CONTEXT_PROMPT_TOKENS` (200k — the usage report adds one extra base-rate charge for that per-request subset), and baseline `PRICING_AS_OF`. Subscription allowance and local inference bills remain unknown; batch discounts and non-Grok long-context tiers are not modeled. |+| `modelPricing.js` | Per-model API billing rates for informational cost estimates — `resolveModelRates(providerId, model)` (exact → family regex → provider default → blended fallback, with a `matched` tier; also derives cache tiers), `pricingAsOfForModel(model)`, `isFreeProvider`, `estimateCostUsd(tokensIn, tokensOut, rates, cache?)` (uncached input plus separately priced cache tokens), `estimateGrokImagineCost(model, units)` (published Imagine image / second / input-image rates; longest model id wins), `estimateAgyImageCost(model, units)` (published Imagen rates for Antigravity-driven generation), `GROK_LONG_CONTEXT_PROMPT_TOKENS` (200k — the usage report adds one extra base-rate charge for that per-request subset), and baseline `PRICING_AS_OF`. Subscription allowance and local inference bills remain unknown; batch discounts and non-Grok long-context tiers are not modeled. || `chatCompletionUsage.js` | `normalizeChatCompletionUsage(usage, { model, endpoint })` splits an OpenAI-compatible chat-completions usage body. Cached tokens are removed from fresh input. xAI (`api.x.ai`, `us.api.x.ai`, or a `grok*` model when the endpoint has no host) adds `reasoning_tokens` to output; every other host keeps output as `completion_tokens` only, even for a grok-named model. A prompt at `GROK_LONG_CONTEXT_PROMPT_TOKENS` sets `longContext` for xAI only. || `usageRange.js` | `resolveUsageRange({ period, from, to })` — pure period→inclusive-YYYY-MM-DD range resolution for the usage cost report (explicit dates win; `all` unbounded; default 7d). || `subscriptionPlanTiers.js` | Which TIER of a subscription plan the user is on ("Max 20x", "Pro"), as a short free-text label per provider family — `MAX_PLAN_TIER_LENGTH` (the cap the Zod schema enforces), `normalizePlanTier` / `normalizePlanTiers` (trim, cap, and collapse empty/non-string to `null` = not recorded). Deliberately free text: tiers are vendor marketing names that change off the PortOS release cycle. Pure; persistence lives in `services/subscriptions.js`. |@@ -327,7 +327,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub| `providerRef.js` | Provider REFERENCE grammar (#7564, epic #7561) — the two spellings a `{ providerId, model, effort }` selection may name a provider by: a PRESET record id (`PRESET_ID_RE`, the slug `createProvider` mints) or a COMPOSITE `<harness>.<cli|tui|api>@<service-slug>[+<bootstrap-slug>]` (`COMPOSITE_ID_RE`; the bootstrap suffix only with a process method). `parseProviderRef(id)` → `{kind:'preset'}` / `{kind:'composite', harnessId, method, serviceSlug, bootstrapSlug}` / `null`, `isCompositeProviderId` / `isPresetProviderId`, `formatCompositeId(parts)` (throws on parts that would not parse back), `MAX_PROVIDER_REF_LENGTH`, and the two validation messages (`PROVIDER_REF_MESSAGE`, `PRESET_ONLY_MESSAGE`). The zod forms — `providerRefSchema` / `providerRefFieldSchema` (either grammar) and `presetProviderIdSchema` (preset-only, for `PUT /providers/active`, a record's `fallbackProvider`, an app's `taskTypeOverrides` pin) — live in `zodCompat.js` so `cosValidation.js` can share them. Pure and dependency-free; mirrored in `aiToolkit/internal/providerRef.js` (the toolkit may not import out) behind `providerRef.parity.test.js`. Resolution is host-side: `services/compositeProviders.js`. || `providerRouteRecipes.js` | Minting a FRESH executable route from a connection and a harness command recipe (#6369) — the reverse of `providerConnections.js`, which reads an existing record. `buildRouteRecord({harnessId, mode, providerId, name, connection})` materializes a connection's endpoint, credentials and backend marker into a DISABLED `data/providers.json` record through `PROVIDER_HARNESSES[].recipe`; `mintRouteIds` allocates readable ids keeping the `<stem>` / `<stem>-tui` pairing `providerModeGroups` recognizes; `bindingBlocker` is the pre-write refusal (no recipe, unsupported mode, a transport the backend does not declare, a credential the program requires); `CREATABLE_CONNECTION_KINDS` / `connectionKindLabel` / `connectionKindMarkers` bound the backends a route can honestly describe (`slotstream` and `vendor` deliberately excluded); `bindingProtocol` names the wire a binding needs. `materializeRoute({harness, method, serviceInstance, bootstrap?, selection?, overrides?})` (#7562) generalizes `buildRouteRecord` from a connection to a SERVICE instance: picks the harness's first compatible binding, writes base URL / credential / static env / backend markers / inline OpenCode config / Pi `--provider` per that binding plus the recipe's mode argv, stamps `harnessId` / `method` / `serviceId` / `servicePlan`, and — for a non-`api` method with a bootstrap app — the `credentialBootstrap` object `composeBootstrapSpawn` turns into `<bootstrap> run <harness> -- <args>`; throws typed errors (`HARNESS_SERVICE_INCOMPATIBLE`, `SERVICE_ENDPOINT_REQUIRED`, `SERVICE_CREDENTIAL_REQUIRED`, `SERVICE_CREDENTIAL_BOOTSTRAP_REQUIRED`) rather than minting a route that cannot run. `materializeRouteOutcome(input)` is the same body as a VERDICT (`{ record }` or `{ error }` — the typed 400 without the throw) for the composite resolver (#7564), which reads a refusal as the reason to show beside a saved selection. `routeDescribesService(profile, instance)` is the round-trip contract: fed back through `providerConnectionProfile`, a materialized record classifies onto the service it was built for. Pure: no I/O, no spawn, no provider call.| `providerRouteSettings.js` | The MODE-OVERRIDE half of a provider route (#6369) — the route-owned fields a human tunes per execution mode (`args`, `timeout`, `effort` and the `defaultModel`/`lightModel`/`mediumModel`/`heavyModel`/`ultraModel` pins), mirroring what the toolkit's `sharedModeUpdates` already declares mode-specific. `ROUTE_SETTING_FIELDS` / `ROUTE_SETTING_KEYS` / `routeSettingKeys(mode)` name the set (`args` reaches cli/tui only); `routeSettingsFor(provider)` reads it off a record with absent and cleared collapsing to `null`; `routeSettingsSchema` is the wire shape `GET /api/providers/management` publishes; `unsupportedRouteSettings` rejects a key the route's mode does not accept; and `routeSettingsRevision` is the stale-edit fingerprint — a hash of the values as they are in providers.json rather than a graph row revision, so an edit made through `/ai/edit/:providerId` is caught too. Connection-owned values and execution consent (`enabled`) are deliberately absent. Pure: no I/O.-| `providerTranscriptUsage.js` | Parsers for the session files the coding CLIs write to disk (0 tokens to read) — `parseClaudeTranscript` (`~/.claude/projects/<cwd-slug>/*.jsonl`), `parseCodexRollout` (`~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`), `parseGrokTurns`/`parseGrokChatHistory`/`decodeGrokSessionDir` (`~/.grok/sessions/<encodeURIComponent(cwd)>/<id>/`), `parseAgyTranscript`/`parseAgyHistory` (`~/.gemini/antigravity-cli/`), `parseKimiWireLog`/`parseKimiSessionIndex` (`~/.kimi-code/sessions/.../agents/<id>/wire.jsonl`, indexed by `~/.kimi-code/session_index.jsonl`), `claudeProjectSlug`, `totalTranscriptTokens`. Each de-duplicates a format hazard that otherwise inflates counts badly: Claude repeats one response across several lines sharing a `message.id`, Codex's `total_token_usage` is cumulative and repeated, grok's `turn_completed.usage` has shipped in both per-prompt and cumulative shapes (detected and delta'd, never summed raw) while its `_meta.totalTokens` is context occupancy and never billed. Antigravity writes no token fields at all, so its parser returns chars for the caller to estimate from — including `contextChars`, the prompt each model call replays (priced as cache reads; omitting it made the plan read far cheaper than it was). Grok/Antigravity sessions PortOS never launched are billed by `services/usageInteractiveScan.js`. Kimi's `usage.record` lines are already discrete per-call deltas (no cumulative-snapshot trap), but split across one `wire.jsonl` per agent — a subagent's spend is never in its parent's file. Each parser returns per-model buckets (`byModel`) plus the message keys it counted (`countedKeys`), and accepts an `exclude` set — that is what stops two overlapping PortOS runs from both billing the same messages. Tolerant of truncated (mid-write) files; consumed by `services/usageReconciler.js`. |+| `providerTranscriptUsage.js` | Parsers for the session files the coding CLIs write to disk (0 tokens to read) — `parseClaudeTranscript` (`~/.claude/projects/<cwd-slug>/*.jsonl`), `parseCodexRollout` (`~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`), `parseGrokTurns`/`parseGrokChatHistory`/`decodeGrokSessionDir` (`~/.grok/sessions/<encodeURIComponent(cwd)>/<id>/`), `parseAgyTranscript`/`parseAgyHistory` (`~/.gemini/antigravity-cli/`), `parseKimiWireLog`/`parseKimiSessionIndex` (`~/.kimi-code/sessions/.../agents/<id>/wire.jsonl`, indexed by `~/.kimi-code/session_index.jsonl`), `claudeProjectSlug`, `totalTranscriptTokens`. Each de-duplicates a format hazard that otherwise inflates counts badly: Claude repeats one response across several lines sharing a `message.id`, Codex's `total_token_usage` is cumulative and repeated, grok's `turn_completed.usage` has shipped in both per-prompt and cumulative shapes (detected and delta'd, never summed raw) while its `_meta.totalTokens` is context occupancy and never billed. Antigravity parses measured tokens (`input_tokens`, `output_tokens`, `cache_read_tokens`) from planner responses when recorded, or falls back to chars for the caller to estimate from — including `contextChars`, the prompt each model call replays (priced as cache reads; omitting it made the plan read far cheaper than it was). Grok/Antigravity sessions PortOS never launched are billed by `services/usageInteractiveScan.js`. Kimi's `usage.record` lines are already discrete per-call deltas (no cumulative-snapshot trap), but split across one `wire.jsonl` per agent — a subagent's spend is never in its parent's file. Each parser returns per-model buckets (`byModel`) plus the message keys it counted (`countedKeys`), and accepts an `exclude` set — that is what stops two overlapping PortOS runs from both billing the same messages. Tolerant of truncated (mid-write) files; consumed by `services/usageReconciler.js`. || `opencodeCatalogCache.js` | Primes the on-disk catalog `opencode models` prints from — `primeOpencodeCatalogCache()` fetches OpenCode's `api.json` with Node's fetch and atomically writes `$XDG_CACHE_HOME/opencode/models.json` (`~/.cache` when unset). OpenCode refreshes that file from a forked task whose failures it swallows (`opencode models --refresh` still prints `Models cache refreshed`) and its HTTP client has no Happy Eyeballs, so a host advertising an unreachable IPv6 default route freezes the catalog indefinitely while other machines on the same account list newer models. Refuses to fetch or write when `OPENCODE_MODELS_PATH` / a custom `OPENCODE_MODELS_URL` / `OPENCODE_DISABLE_MODELS_FETCH` means PortOS cannot be sure which file OpenCode reads, when the file is under five minutes old, or when the body did not parse as a catalog — a stale list beats an empty picker. Never throws; the caller probes either way. || `opencodeConfig.js` | OpenCode config builder — `buildOpencodeEnvVars(provider, model)` builds dynamic `OPENCODE_CONFIG_CONTENT` declaring model ids under the namespace the provider's marker selects: a local runtime (`ollama` / `mtplx` / `llama` / `vllm` / `sglang`, bare ids) or a hosted gateway from `providerGateways.js` (`vendor/model` ids kept whole). Fixes --model rejection. Also attaches the key for a key-bearing namespace, and pins `small_model` to the run model for a gateway so OpenCode's own side calls (titles, summarization) can't land on its built-in default — a billed model the operator never chose. Under a `no-tool` public-review profile it also applies `hardenOpencodeConfigForNoTool` — root `permission: deny`, an emptied tool map on every agent, `tool_call: false` on every declared model, and no MCP/plugins/share/autoupdate — which IS OpenCode's enforced tool-free recipe, since it ships no read-only argv flag (`providerVendors.js` pairs it with `run --agent` + `OPENCODE_PUBLIC_REVIEW_AGENT`). The harden step also copies `agent.build`'s generation settings onto that agent, so the stage's configured thinking effort reaches the model that actually runs. || `localProviderRuntime.js` | Which LOCAL daemon a provider talks to, and where — `LOCAL_RUNTIMES` (llama.cpp / Ollama / LM Studio / MTPLX / vLLM: label, binary, canonical base URL read from `opencodeConfig.js` rather than re-typed, manage/docs links, model-download hint), `localEndpointPort` + `isLocalInstanceHost`, and re-exports of `localBackendForProvider` / `localRuntimeKind` / `modelPinIsOffered` — all three now live in the browser-safe leaf `modelPinMembership.js` (a picker needs the pin rule and must not pull the endpoint graph below to get it), so existing importers here are untouched — `localRuntimeForProvider(provider)` → the row with the endpoint the provider ITSELF configures (`OPENCODE_CONFIG_CONTENT`'s `baseURL`, `ANTHROPIC_BASE_URL`, or `endpoint`), then the `OLLAMA_URL`/`OLLAMA_HOST`/`LM_STUDIO_URL` override the backend managers read, then the canonical default — and `null` when that resolved endpoint fails `isLocalInstanceEndpoint` (an API provider on another machine has no local daemon to check, whatever its name says) — plus `normalizeOpenAiBaseUrl`. Pure; the probing half is `services/providerReadiness.js`. Optional `setupStateDetail` overrides `providerReadiness`'s per-state prose for a runtime whose local setup is not a model cache (vLLM's is a compose project); `standbyWhenStopped` marks an installed runtime such as llama.cpp whose stopped state is intentional standby rather than incomplete setup. |diff --git a/server/lib/modelPricing.js b/server/lib/modelPricing.jsindex 3de84ee926..e46ec1b51d 100644--- a/server/lib/modelPricing.js+++ b/server/lib/modelPricing.js@@ -75,6 +75,9 @@ export const GROK_LONG_CONTEXT_PROMPT_TOKENS = 200_000;export const GROK_IMAGINE_IMAGE_MODEL = 'grok-imagine-image-2.0';export const GROK_IMAGINE_VIDEO_MODEL = 'grok-imagine-video-1.5';+/** Recommended Antigravity image model. */+export const AGY_IMAGEN_IMAGE_MODEL = 'imagen-3.0-generate-002';+/*** Headline / lowest-resolution Imagine rates (USD). Higher resolutions cost* more; PortOS does not request a resolution, so the estimate is this tier.@@ -96,6 +99,41 @@ function grokImagineRate(model) {return GROK_IMAGINE_RATES.find((row) => id === row.id || id.includes(row.id)) || null;}+/**+ * Google Imagen 3 rates (USD). Published rate: $0.030 per image.+ * https://ai.google.dev/gemini-api/docs/pricing+ */+const AGY_IMAGEN_RATES = Object.freeze([+ { id: 'imagen-3.0-generate-002', kind: 'image', perImage: 0.03, perInputImage: 0 },+ { id: 'imagen-3.0-fast-generate-001', kind: 'image', perImage: 0.03, perInputImage: 0 },+ { id: 'imagen-3.0-generate', kind: 'image', perImage: 0.03, perInputImage: 0 },+ { id: 'imagen-3.0', kind: 'image', perImage: 0.03, perInputImage: 0 },+ { id: 'imagen-3-fast', kind: 'image', perImage: 0.03, perInputImage: 0 },+ { id: 'imagen-3', kind: 'image', perImage: 0.03, perInputImage: 0 },+ { id: 'imagen-2', kind: 'image', perImage: 0.02, perInputImage: 0 },+ { id: 'imagen', kind: 'image', perImage: 0.03, perInputImage: 0 },+].sort((a, b) => b.id.length - a.id.length));++/** The Imagen rate row a model id names, or null. */+function agyImagenRate(model) {+ const id = String(model || '').trim().toLowerCase();+ if (!id.includes('imagen')) return null;+ return AGY_IMAGEN_RATES.find((row) => id === row.id || id.includes(row.id)) || null;+}++/**+ * USD for one Antigravity Imagen bucket. Token rates do not apply.+ * @param {string|null|undefined} model+ * @param {{ images?: number, inputImages?: number }} [units]+ * @returns {number}+ */+export function estimateAgyImageCost(model, units = null) {+ const rate = agyImagenRate(model);+ if (!rate) return 0;+ return (units?.images || 0) * (rate.perImage || 0)+ + (units?.inputImages || 0) * (rate.perInputImage || 0);+}+/*** USD for one Imagine bucket. Token rates do not apply; input-image fees use* the same model's published input price. Unknown ids cost nothing here so a@@ -193,6 +231,8 @@ const EXACT_RATES = {// Introductory standard rate through 2026-11-30; doubles to $4/$20 on 2026-12-01.'gemini-4-argon': [2.0, 10.0],'gemini-3.1-pro-preview': [2.0, 12.0], // ≤200k-token tier+ 'gemini-3.8-flash': [1.5, 9.0],+ 'gemini-3.6-flash': [1.5, 9.0],'gemini-3.5-flash': [1.5, 9.0],'gemini-3.1-flash-lite': [0.25, 1.5],'gemini-2.5-pro': [1.25, 10.0],@@ -371,6 +411,17 @@ export function resolveModelRates(providerId, model) {matched: id.toLowerCase() === imagine.id ? 'exact' : 'family',};}+ const imagen = agyImagenRate(id);+ if (imagen) {+ return {+ rateModel: imagen.id,+ inputPer1M: 0,+ outputPer1M: 0,+ cacheReadPer1M: 0,+ cacheWritePer1M: 0,+ matched: id.toLowerCase() === imagen.id ? 'exact' : 'family',+ };+ }if (id && EXACT_RATES[id]) {return { ...toRates(id), matched: 'exact' };}diff --git a/server/lib/modelPricing.test.js b/server/lib/modelPricing.test.jsindex a7a67677da..461a54aa6e 100644--- a/server/lib/modelPricing.test.js+++ b/server/lib/modelPricing.test.js@@ -1,6 +1,7 @@import { describe, it, expect } from 'vitest';import {resolveModelRates, isFreeProvider, isFreeModelId, estimateCostUsd, estimateGrokImagineCost,+ estimateAgyImageCost, AGY_IMAGEN_IMAGE_MODEL,pricingAsOfForModel, PRICING_AS_OF, GROK_IMAGINE_IMAGE_MODEL, GROK_IMAGINE_VIDEO_MODEL,} from './modelPricing.js';@@ -445,3 +446,26 @@ describe('Grok Imagine rates', () => {expect(estimateGrokImagineCost('grok-4.7', { images: 1 })).toBe(0);});});++describe('Antigravity Imagen rates', () => {+ it('prices the recommended Imagen 3 model at published rates ($0.03/image)', () => {+ expect(estimateAgyImageCost(AGY_IMAGEN_IMAGE_MODEL, { images: 1 })).toBeCloseTo(0.03);+ expect(estimateAgyImageCost(AGY_IMAGEN_IMAGE_MODEL, { images: 5 })).toBeCloseTo(0.15);+ expect(estimateAgyImageCost('imagen-2', { images: 1 })).toBeCloseTo(0.02);+ expect(estimateAgyImageCost('gemini-3.8-flash', { images: 1 })).toBe(0);+ });++ it('resolves Imagen model rates with zero token pricing so images are not billed as text', () => {+ const r = resolveModelRates('antigravity-cli', AGY_IMAGEN_IMAGE_MODEL);+ expect(r.rateModel).toBe(AGY_IMAGEN_IMAGE_MODEL);+ expect(r.inputPer1M).toBe(0);+ expect(r.outputPer1M).toBe(0);+ });++ it('resolves gemini-3.8-flash exact and family variants', () => {+ const exact = resolveModelRates('antigravity-cli', 'gemini-3.8-flash');+ expect(exact).toMatchObject({ rateModel: 'gemini-3.8-flash', inputPer1M: 1.5, outputPer1M: 9.0, matched: 'exact' });+ const low = resolveModelRates('antigravity-cli', 'gemini-3.8-flash-low');+ expect(low).toMatchObject({ rateModel: 'gemini-3.8-flash', inputPer1M: 1.5, outputPer1M: 9.0, matched: 'family' });+ });+});diff --git a/server/lib/providerTranscriptUsage.js b/server/lib/providerTranscriptUsage.jsindex af323c4692..dfa8d87313 100644--- a/server/lib/providerTranscriptUsage.js+++ b/server/lib/providerTranscriptUsage.js@@ -747,6 +747,10 @@ export function parseAgyTranscript(jsonlText, { from = null, to = null, excludeconst counted = [];// Chars of every step so far (windowed or not): the prompt the NEXT model call replays.let runningContext = 0;+ let tokensIn = 0;+ let tokensOut = 0;+ let cacheReadTokens = 0;+ let hasMeasuredTokens = false;for (const [index, entry] of parseJsonLines(jsonlText).entries()) {if (typeof entry.type !== 'string') continue;@@ -764,13 +768,31 @@ export function parseAgyTranscript(jsonlText, { from = null, to = null, excludecharsOut += stepChars;contextChars += replayed;messages += 1;+ if (typeof entry.input_tokens === 'number' || typeof entry.output_tokens === 'number') {+ hasMeasuredTokens = true;+ tokensIn += num(entry.input_tokens);+ tokensOut += num(entry.output_tokens);+ cacheReadTokens += num(entry.cache_read_tokens);+ }} else {charsIn += stepChars;}counted.push(key);}- return { charsIn, charsOut, contextChars, messages, countedKeys: counted, steps };+ return {+ charsIn,+ charsOut,+ contextChars,+ messages,+ countedKeys: counted,+ steps,+ tokensIn,+ tokensOut,+ cacheReadTokens,+ cacheWriteTokens: 0,+ measured: hasMeasuredTokens,+ };}/**diff --git a/server/lib/providerTranscriptUsage.test.js b/server/lib/providerTranscriptUsage.test.jsindex ed8f7070e2..b5484f7f48 100644--- a/server/lib/providerTranscriptUsage.test.js+++ b/server/lib/providerTranscriptUsage.test.js@@ -645,7 +645,7 @@ describe('decodeGrokSessionDir', () => {// Antigravity — ~/.gemini/antigravity-cli/// ----------------------------------------------------------------------------const agyStep = ({ index = 0, type = 'GENERIC', createdAt = '2026-07-01T10:00:00Z', content = '', thinking = null, toolCalls = null }) => JSON.stringify({+const agyStep = ({ index = 0, type = 'GENERIC', createdAt = '2026-07-01T10:00:00Z', content = '', thinking = null, toolCalls = null, inputTokens = null, outputTokens = null, cacheReadTokens = null }) => JSON.stringify({step_index: index,source: type === 'USER_INPUT' ? 'USER_EXPLICIT' : 'MODEL',type,@@ -653,7 +653,10 @@ const agyStep = ({ index = 0, type = 'GENERIC', createdAt = '2026-07-01T10:00:00created_at: createdAt,...(content ? { content } : {}),...(thinking ? { thinking } : {}),- ...(toolCalls ? { tool_calls: toolCalls } : {})+ ...(toolCalls ? { tool_calls: toolCalls } : {}),+ ...(typeof inputTokens === 'number' ? { input_tokens: inputTokens } : {}),+ ...(typeof outputTokens === 'number' ? { output_tokens: outputTokens } : {}),+ ...(typeof cacheReadTokens === 'number' ? { cache_read_tokens: cacheReadTokens } : {})});describe('parseAgyTranscript', () => {@@ -711,6 +714,20 @@ describe('parseAgyTranscript', () => {].join('\n');expect(parseAgyTranscript(text, { exclude: new Set(['step-0']) }).charsIn).toBe(40);});++ it('parses measured token counts when PLANNER_RESPONSE carries them', () => {+ const text = [+ agyStep({ index: 0, type: 'USER_INPUT', content: 'hello' }),+ agyStep({ index: 1, type: 'PLANNER_RESPONSE', content: 'world', inputTokens: 500, outputTokens: 100, cacheReadTokens: 250 }),+ agyStep({ index: 2, type: 'PLANNER_RESPONSE', content: 'again', inputTokens: 300, outputTokens: 80, cacheReadTokens: 600 }),+ ].join('\n');+ const parsed = parseAgyTranscript(text);+ expect(parsed.measured).toBe(true);+ expect(parsed.tokensIn).toBe(800);+ expect(parsed.tokensOut).toBe(180);+ expect(parsed.cacheReadTokens).toBe(850);+ expect(parsed.messages).toBe(2);+ });});describe('parseAgyHistory', () => {diff --git a/server/routes/usage.test.js b/server/routes/usage.test.jsindex e2bd24745b..1d8f20af3c 100644--- a/server/routes/usage.test.js+++ b/server/routes/usage.test.js@@ -414,12 +414,14 @@ describe('usage routes', () => {expect(startHistoricalUsageBackfill).toHaveBeenCalledTimes(1);});- it('POST /api/usage/backfill returns the Grok repair summary on the job', async () => {+ it('POST /api/usage/backfill returns the Grok and Antigravity repair summaries on the job', async () => {const grok = { added: 2, turns: 1, images: 1, videos: 0, backedUp: true, note: null };- startHistoricalUsageBackfill.mockResolvedValue({ status: 'complete', corrected: 1, grok });+ const agy = { added: 3, images: 3, backedUp: true, note: null };+ startHistoricalUsageBackfill.mockResolvedValue({ status: 'complete', corrected: 1, grok, agy });const started = await request(buildApp()).post('/api/usage/backfill');expect(started.status).toBe(202);expect(started.body.grok).toEqual(grok);+ expect(started.body.agy).toEqual(agy);});it('POST /api/usage/session records a session and returns its number', async () => {diff --git a/server/services/agyMediaUsage.js b/server/services/agyMediaUsage.jsnew file mode 100644index 0000000000..389d480d1b--- /dev/null+++ b/server/services/agyMediaUsage.js@@ -0,0 +1,64 @@+/**+ * Ledger writes for an accepted Antigravity Image Gen image.+ *+ * Counts go in usage.json and are priced at read time. A repair id written in+ * the same save keeps the historical backfill from adding the job again.+ * Provider lookup prefers the configured Antigravity CLI so the row sits with the+ * token usage the interactive scan already attributes there, and falls back+ * to the id `antigravity-cli` when this install has no Antigravity provider.+ */++import { familyForProvider } from '../lib/providerFamilies.js';+import { AGY_IMAGEGEN_IMAGE_MODEL } from '../lib/imageGenCapabilities.js';++function dayKey(iso) {+ const ms = Date.parse(iso || '');+ const when = Number.isFinite(ms) ? new Date(ms) : new Date();+ return when.toISOString().slice(0, 10);+}++async function agyUsageProviderId() {+ try {+ const { listProviders } = await import('./providers.js');+ const list = await listProviders();+ return agyUsageProviderIdFromList(list);+ } catch (err) {+ console.error(`❌ Antigravity usage provider lookup failed: ${err.message}`);+ return 'antigravity-cli';+ }+}++/** CLI, then TUI, then any agy family record, else the id `antigravity-cli`. */+export function agyUsageProviderIdFromList(providers) {+ const agy = (providers || []).filter((provider) => (+ provider && provider.enabled !== false && familyForProvider(provider) === 'agy'+ ));+ const preferred = agy.find((provider) => provider.type === 'cli')+ || agy.find((provider) => provider.type === 'tui')+ || agy[0];+ return preferred?.id || 'antigravity-cli';+}++async function record(day, recordData, repairId) {+ const { addDatedUsage } = await import('./usage.js');+ await addDatedUsage(day, recordData, { repairId, flush: true });+}++/**+ * One accepted gallery image. `inputImages` is the init image plus references+ * that were actually passed to the tool.+ */+export async function recordAgyImageUsage({+ jobId, createdAt, inputImages = 0, providerId = null, repairId = null, model = null,+} = {}) {+ if (!jobId) return;+ const id = repairId || `gallery:${jobId}`;+ await record(dayKey(createdAt), {+ providerId: providerId || await agyUsageProviderId(),+ model: model || AGY_IMAGEGEN_IMAGE_MODEL,+ messages: 0,+ images: 1,+ inputImages: Math.max(0, inputImages || 0),+ source: 'measured',+ }, id);+}diff --git a/server/services/agyMediaUsage.test.js b/server/services/agyMediaUsage.test.jsnew file mode 100644index 0000000000..bd424a1698--- /dev/null+++ b/server/services/agyMediaUsage.test.js@@ -0,0 +1,110 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';+import { AGY_IMAGEGEN_IMAGE_MODEL } from '../lib/imageGenCapabilities.js';+import { agyUsageProviderIdFromList, recordAgyImageUsage } from './agyMediaUsage.js';++const mockAddDatedUsage = vi.fn(async () => true);+vi.mock('./usage.js', () => ({+ addDatedUsage: (...args) => mockAddDatedUsage(...args),+}));++const mockListProviders = vi.fn(async () => []);+vi.mock('./providers.js', () => ({+ listProviders: (...args) => mockListProviders(...args),+}));++describe('agyUsageProviderIdFromList', () => {+ it('selects CLI over TUI or other types for agy family', () => {+ const providers = [+ { id: 'agy-tui', command: 'agy', type: 'tui' },+ { id: 'agy-cli', command: 'agy', type: 'cli' },+ { id: 'other', command: 'other', type: 'cli' },+ ];+ expect(agyUsageProviderIdFromList(providers)).toBe('agy-cli');+ });++ it('selects TUI if no CLI exists', () => {+ const providers = [+ { id: 'agy-tui', command: 'agy', type: 'tui' },+ ];+ expect(agyUsageProviderIdFromList(providers)).toBe('agy-tui');+ });++ it('selects first enabled agy provider if neither CLI nor TUI type is set', () => {+ const providers = [+ { id: 'agy-custom', command: 'agy', type: 'custom' },+ ];+ expect(agyUsageProviderIdFromList(providers)).toBe('agy-custom');+ });++ it('returns antigravity-cli when no agy provider is found', () => {+ expect(agyUsageProviderIdFromList([])).toBe('antigravity-cli');+ expect(agyUsageProviderIdFromList([{ id: 'openai-cli', command: 'openai', type: 'cli' }])).toBe('antigravity-cli');+ });+});++describe('recordAgyImageUsage', () => {+ beforeEach(() => {+ mockAddDatedUsage.mockClear();+ mockListProviders.mockResolvedValue([+ { id: 'antigravity', command: 'agy', type: 'cli', enabled: true },+ ]);+ });++ it('does nothing if jobId is missing', async () => {+ await recordAgyImageUsage({});+ expect(mockAddDatedUsage).not.toHaveBeenCalled();+ });++ it('records image usage with default model and gallery repairId', async () => {+ await recordAgyImageUsage({+ jobId: 'job-123',+ createdAt: '2026-10-15T12:00:00.000Z',+ inputImages: 2,+ });++ expect(mockAddDatedUsage).toHaveBeenCalledTimes(1);+ expect(mockAddDatedUsage).toHaveBeenCalledWith(+ '2026-10-15',+ {+ providerId: 'antigravity',+ model: AGY_IMAGEGEN_IMAGE_MODEL,+ messages: 0,+ images: 1,+ inputImages: 2,+ source: 'measured',+ },+ {+ repairId: 'gallery:job-123',+ flush: true,+ },+ );+ });++ it('honors custom providerId, model, and repairId', async () => {+ await recordAgyImageUsage({+ jobId: 'job-456',+ createdAt: '2026-10-16T12:00:00.000Z',+ inputImages: 0,+ providerId: 'custom-agy',+ model: 'imagen-3.0-custom',+ repairId: 'custom-repair:job-456',+ });++ expect(mockAddDatedUsage).toHaveBeenCalledTimes(1);+ expect(mockAddDatedUsage).toHaveBeenCalledWith(+ '2026-10-16',+ {+ providerId: 'custom-agy',+ model: 'imagen-3.0-custom',+ messages: 0,+ images: 1,+ inputImages: 0,+ source: 'measured',+ },+ {+ repairId: 'custom-repair:job-456',+ flush: true,+ },+ );+ });+});diff --git a/server/services/agyUsageBackfill.js b/server/services/agyUsageBackfill.jsnew file mode 100644index 0000000000..0c56edbfc9--- /dev/null+++ b/server/services/agyUsageBackfill.js@@ -0,0 +1,180 @@+/**+ * Apply the Antigravity usage repair to the server's in-memory ledger.+ *+ * Reconcile on the Usage page calls this from the historical-backfill job, on+ * the same thread that owns usage.json. Repair ids already on the ledger make+ * a repeat a no-op. The on-disk ledger is copied before the first write of a+ * batch that has something to add.+ */++import { copyFile, mkdir, readdir, readFile } from 'fs/promises';+import { homedir } from 'os';+import { join, resolve } from 'path';+import { PATHS } from '../lib/fileUtils.js';+import { familyForProvider } from '../lib/providerFamilies.js';+import { agyUsageProviderIdFromList } from './agyMediaUsage.js';+import {+ galleryAgyImageUsageEvent,+ imagineEventsFromAgyTranscript,+ planAgyUsageRepair,+ summarizeAgyRepairAdditions,+} from './agyUsageRepair.js';+import { addDatedUsage, flushUsageLedger, getUsage, loadUsage } from './usage.js';+import { listAgyConversations } from './usageReconciler.js';++const yieldToEventLoop = () => new Promise((done) => setImmediate(done));++const faultLabel = (err) => {+ if (err?.code) return String(err.code);+ return String(err?.message || err?.name || 'error').replace(/[\r\n]+/g, ' ');+};++const oneLine = (value) => String(value ?? '').replace(/[\r\n]+/g, ' ');++async function readJson(path, fallback, { strict = false, label = 'file' } = {}) {+ try {+ return JSON.parse(await readFile(path, 'utf8'));+ } catch (err) {+ if (err.code === 'ENOENT') return fallback;+ if (strict) throw err;+ console.warn(`⚠️ Antigravity usage repair skipped unreadable ${label} (${faultLabel(err)})`);+ return fallback;+ }+}++async function listNames(dir) {+ try {+ return await readdir(dir);+ } catch (err) {+ if (err.code !== 'ENOENT') {+ console.warn(`⚠️ Antigravity usage repair skipped a directory (${faultLabel(err)})`);+ }+ return [];+ }+}++async function collectGallery(dataDir) {+ const dir = join(dataDir, 'images');+ const media = [];+ for (const name of await listNames(dir)) {+ if (!name.endsWith('.metadata.json')) continue;+ try {+ const meta = await readJson(join(dir, name), null, { label: oneLine(name) });+ const event = galleryAgyImageUsageEvent(meta);+ if (event) media.push(event);+ } catch (err) {+ console.warn(`⚠️ Antigravity usage repair skipped gallery file ${oneLine(name)} (${faultLabel(err)})`);+ }+ }+ return media;+}++async function collectSessions(home) {+ const root = join(home, '.gemini', 'antigravity-cli');+ const media = [];+ let conversations;+ try {+ conversations = await listAgyConversations(root);+ } catch {+ return media;+ }+ for (const conversation of conversations) {+ try {+ const transcriptPath = join(root, 'brain', conversation.conversationId, '.system_generated', 'logs', 'transcript.jsonl');+ const text = await readFile(transcriptPath, 'utf8').catch(() => '');+ if (text) {+ const events = imagineEventsFromAgyTranscript({+ transcriptText: text,+ sessionId: conversation.conversationId,+ cwd: conversation.workspace,+ day: conversation.timestamp ? new Date(conversation.timestamp).toISOString().slice(0, 10) : null,+ });+ media.push(...events);+ }+ } catch {+ // One unreadable session must not abort the scan+ }+ await yieldToEventLoop();+ }+ return media;+}++async function providerIdFromFile(dataDir) {+ const stored = await readJson(join(dataDir, 'providers.json'), null, { label: 'providers.json' });+ const list = Array.isArray(stored)+ ? stored+ : (Array.isArray(stored?.providers) ? stored.providers : Object.values(stored?.providers || {}));+ const agy = list.filter((provider) => provider && familyForProvider(provider) === 'agy');+ return agyUsageProviderIdFromList(agy.length ? list : []);+}++/**+ * Read the gallery and brain sessions without touching the ledger.+ */+export async function planAgyUsageRepairFromDisk({+ home = homedir(),+ dataDir = PATHS.data,+ repairIds = null,+ providerId = null,+} = {}) {+ const storedIds = repairIds ?? (await readJson(join(dataDir, 'usage.json'), {}, { strict: true }))?.grokRepairIds;+ const gallery = await collectGallery(dataDir);+ const sessionMedia = await collectSessions(home);+ return planAgyUsageRepair({+ media: [...gallery, ...sessionMedia],+ repairIds: Array.isArray(storedIds) ? storedIds : [],+ providerId: providerId || await providerIdFromFile(dataDir),+ });+}++async function backupLedger(dataDir) {+ const stamp = new Date().toISOString().replace(/[:.]/g, '-');+ const dest = join(dataDir, 'backups', `agy-usage-${stamp}`);+ await mkdir(dest, { recursive: true });+ for (const name of ['usage.json', 'usage-interactive-scan.json']) {+ const source = join(dataDir, name);+ try {+ await copyFile(source, join(dest, name));+ } catch (err) {+ if (err.code !== 'ENOENT') throw err;+ }+ }+ return dest;+}++/**+ * Plan against the in-memory repair ids and add any missing rows there.+ * Writes only PATHS.data, which is the file the running server will save.+ * @returns {Promise<{ added: number, images: number, backedUp: boolean, note: string|null, backupDir: string|null }>}+ */+export async function applyAgyUsageRepair({+ home = homedir(),+ dataDir = PATHS.data,+ providers = null,+ providerId = null,+} = {}) {+ if (resolve(dataDir) !== resolve(PATHS.data)) {+ throw new Error(`Antigravity usage repair writes ${PATHS.data}/usage.json`);+ }+ await loadUsage();+ const resolvedProviderId = providerId+ || (providers ? agyUsageProviderIdFromList(providers) : await providerIdFromFile(dataDir));+ const plan = await planAgyUsageRepairFromDisk({+ home,+ dataDir,+ repairIds: getUsage().grokRepairIds || [],+ providerId: resolvedProviderId,+ });+ const note = plan.notes[0] || null;+ if (plan.additions.length === 0) {+ return { ...summarizeAgyRepairAdditions([]), backedUp: false, note, backupDir: null };+ }+ const backupDir = await backupLedger(dataDir);+ const written = [];+ for (const item of plan.additions) {+ const wrote = await addDatedUsage(item.day, item.record, { repairId: item.repairId, flush: false });+ if (wrote) written.push(item);+ }+ if (written.length > 0) await flushUsageLedger();+ return { ...summarizeAgyRepairAdditions(written), backedUp: true, note, backupDir };+}diff --git a/server/services/agyUsageBackfill.test.js b/server/services/agyUsageBackfill.test.jsnew file mode 100644index 0000000000..a46f0dbdde--- /dev/null+++ b/server/services/agyUsageBackfill.test.js@@ -0,0 +1,83 @@+import { mkdir, mkdtemp, readFile, rm, writeFile } from 'fs/promises';+import { tmpdir } from 'os';+import { join } from 'path';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++const ledger = vi.hoisted(() => ({ grokRepairIds: [] }));+const written = vi.hoisted(() => []);+const paths = vi.hoisted(() => ({ data: '' }));++vi.mock('./usage.js', () => ({+ loadUsage: vi.fn(async () => ledger),+ getUsage: vi.fn(() => ledger),+ addDatedUsage: vi.fn(async (day, record, { repairId } = {}) => {+ if (ledger.grokRepairIds.includes(repairId)) return false;+ ledger.grokRepairIds.push(repairId);+ written.push({ day, record, repairId });+ return true;+ }),+ flushUsageLedger: vi.fn(async () => {}),+}));++vi.mock('../lib/fileUtils.js', () => ({+ PATHS: paths,+}));++const { applyAgyUsageRepair, planAgyUsageRepairFromDisk } = await import('./agyUsageBackfill.js');+const { flushUsageLedger } = await import('./usage.js');++let home;+let dataDir;++beforeEach(async () => {+ home = await mkdtemp(join(tmpdir(), 'agy-repair-home-'));+ dataDir = await mkdtemp(join(tmpdir(), 'agy-repair-data-'));+ paths.data = dataDir;+ ledger.grokRepairIds = [];+ written.length = 0;+ flushUsageLedger.mockClear();++ await mkdir(join(dataDir, 'images'), { recursive: true });+ await writeFile(join(dataDir, 'usage.json'), JSON.stringify({ grokRepairIds: [] }));+ await writeFile(join(dataDir, 'images', 'job-agy-1.metadata.json'), JSON.stringify({+ id: 'job-agy-1',+ mode: 'agy',+ imageModel: 'imagen-3.0-generate-002',+ createdAt: '2026-10-05T00:00:00.000Z',+ }));+});++afterEach(async () => {+ await rm(home, { recursive: true, force: true });+ await rm(dataDir, { recursive: true, force: true });+});++describe('planAgyUsageRepairFromDisk', () => {+ it('plans gallery images without writing the ledger', async () => {+ const plan = await planAgyUsageRepairFromDisk({ home, dataDir });+ expect(plan.additions.map((item) => item.repairId)).toEqual(['gallery:job-agy-1']);+ await expect(readFile(join(dataDir, 'usage.json'), 'utf8')).resolves.toBe(JSON.stringify({ grokRepairIds: [] }));+ });+});++describe('applyAgyUsageRepair', () => {+ it('backs up the ledger once, writes the in-memory repair, and skips a repeat', async () => {+ const first = await applyAgyUsageRepair({ home, dataDir });+ expect(first).toMatchObject({ added: 1, images: 1, backedUp: true });+ expect(first.backupDir).toMatch(/agy-usage-/);+ expect(JSON.parse(await readFile(join(first.backupDir, 'usage.json'), 'utf8')).grokRepairIds).toEqual([]);+ expect(written).toHaveLength(1);+ expect(written[0].repairId).toBe('gallery:job-agy-1');+ expect(flushUsageLedger).toHaveBeenCalledTimes(1);++ const second = await applyAgyUsageRepair({ home, dataDir });+ expect(second).toMatchObject({ added: 0, backedUp: false });+ expect(written).toHaveLength(1);+ expect(flushUsageLedger).toHaveBeenCalledTimes(1);+ });++ it('refuses to apply a plan read from a directory the server will not save', async () => {+ await expect(applyAgyUsageRepair({ home, dataDir: join(dataDir, 'elsewhere') }))+ .rejects.toThrow(/usage\.json/);+ });+});diff --git a/server/services/agyUsageRepair.js b/server/services/agyUsageRepair.jsnew file mode 100644index 0000000000..91afe45bb0--- /dev/null+++ b/server/services/agyUsageRepair.js@@ -0,0 +1,116 @@+/**+ * Pure plan for repairing Antigravity image usage that was not recorded in the ledger.+ *+ * Media recovered from the gallery is added as image units.+ * Session tool calls inside a PortOS `portos-agy-scratch-` directory are+ * omitted so a gallery job is not billed twice.+ */++import { parseJsonLines } from '../lib/providerTranscriptUsage.js';+import { AGY_IMAGEGEN_IMAGE_MODEL } from '../lib/imageGenCapabilities.js';++function isPortosAgyScratch(cwd) {+ return String(cwd || '').includes('portos-agy-scratch-');+}++function dayFromIso(iso) {+ const ms = Date.parse(iso || '');+ if (!Number.isFinite(ms)) return null;+ return new Date(ms).toISOString().slice(0, 10);+}++function inputImageCount(args) {+ if (!args || typeof args !== 'object') return 0;+ const paths = args.ImagePaths || args.imagePaths || args.image_paths || args.images;+ if (Array.isArray(paths)) return paths.length;+ if (typeof paths === 'string' && paths) return 1;+ return 0;+}++/**+ * Image generation tool calls from an Antigravity transcript outside PortOS render scratch.+ */+export function imagineEventsFromAgyTranscript({+ transcriptText = '', sessionId = 'unknown', cwd = null, day = null,+} = {}) {+ if (isPortosAgyScratch(cwd)) return [];+ const events = [];+ let index = 0;+ for (const entry of parseJsonLines(transcriptText)) {+ if (entry?.type !== 'PLANNER_RESPONSE' || !Array.isArray(entry?.tool_calls)) continue;+ const entryDay = dayFromIso(entry?.created_at) || day;+ if (!entryDay) continue;+ for (const call of entry.tool_calls) {+ const name = String(call?.name || '').toLowerCase();+ if (name !== 'generate_image') continue;+ const id = `session-image:${sessionId}:${index}`;+ index += 1;+ events.push({+ id,+ day: entryDay,+ model: AGY_IMAGEGEN_IMAGE_MODEL,+ images: 1,+ inputImages: inputImageCount(call?.args),+ videoSeconds: 0,+ source: 'measured',+ });+ }+ }+ return events;+}++export function galleryAgyImageUsageEvent(meta) {+ if (!meta || meta.mode !== 'agy' || !meta.id) return null;+ const day = dayFromIso(meta.createdAt);+ if (!day) return null;+ const refs = Array.isArray(meta.referenceImageFilenames) ? meta.referenceImageFilenames.length : 0;+ return {+ id: `gallery:${meta.id}`,+ day,+ model: meta.imageModel || AGY_IMAGEGEN_IMAGE_MODEL,+ images: 1,+ inputImages: (meta.initImageFilename ? 1 : 0) + refs,+ videoSeconds: 0,+ source: 'measured',+ };+}++/**+ * @param {{ media?: object[], repairIds?: string[], providerId?: string }} input+ * @returns {{ additions: object[], notes: string[] }}+ */+export function planAgyUsageRepair({+ media = [], repairIds = [], providerId = 'antigravity-cli',+} = {}) {+ const known = new Set(repairIds.filter((id) => typeof id === 'string' && id));+ const additions = [];+ const notes = [];+ for (const item of media) {+ if (!item?.id || !item.day || known.has(item.id)) continue;+ known.add(item.id);+ additions.push({+ repairId: item.id,+ day: item.day,+ record: {+ providerId,+ model: item.model || AGY_IMAGEGEN_IMAGE_MODEL,+ messages: 0,+ images: item.images || 0,+ videoSeconds: 0,+ inputImages: item.inputImages || 0,+ source: item.source || 'measured',+ },+ });+ }+ return { additions, notes };+}++/** Counts for the reconcile status line. */+export function summarizeAgyRepairAdditions(additions = []) {+ const summary = { added: 0, images: 0 };+ for (const item of additions) {+ summary.added += 1;+ summary.images += item?.record?.images || 0;+ }+ return summary;+}diff --git a/server/services/agyUsageRepair.test.js b/server/services/agyUsageRepair.test.jsnew file mode 100644index 0000000000..321921dd5b--- /dev/null+++ b/server/services/agyUsageRepair.test.js@@ -0,0 +1,130 @@+import { describe, expect, it } from 'vitest';+import {+ galleryAgyImageUsageEvent,+ imagineEventsFromAgyTranscript,+ planAgyUsageRepair,+ summarizeAgyRepairAdditions,+} from './agyUsageRepair.js';++describe('galleryAgyImageUsageEvent', () => {+ it('returns a formatted event for valid agy gallery metadata', () => {+ const meta = {+ id: 'job-123',+ mode: 'agy',+ createdAt: '2026-10-10T15:30:00.000Z',+ imageModel: 'imagen-3.0-generate-002',+ initImageFilename: 'init.png',+ referenceImageFilenames: ['ref1.png', 'ref2.png'],+ };++ const event = galleryAgyImageUsageEvent(meta);+ expect(event).toEqual({+ id: 'gallery:job-123',+ day: '2026-10-10',+ model: 'imagen-3.0-generate-002',+ images: 1,+ inputImages: 3,+ videoSeconds: 0,+ source: 'measured',+ });+ });++ it('returns null for non-agy modes, missing id, or invalid date', () => {+ expect(galleryAgyImageUsageEvent({ id: 'job-1', mode: 'grok', createdAt: '2026-10-10T00:00:00.000Z' })).toBeNull();+ expect(galleryAgyImageUsageEvent({ mode: 'agy', createdAt: '2026-10-10T00:00:00.000Z' })).toBeNull();+ expect(galleryAgyImageUsageEvent({ id: 'job-1', mode: 'agy', createdAt: 'invalid-date' })).toBeNull();+ });+});++describe('imagineEventsFromAgyTranscript', () => {+ it('ignores tool calls when running in a portos-agy-scratch directory', () => {+ const transcriptText = JSON.stringify({+ type: 'PLANNER_RESPONSE',+ created_at: '2026-10-10T12:00:00.000Z',+ tool_calls: [{ name: 'generate_image', args: { prompt: 'a cute cat' } }],+ });++ const events = imagineEventsFromAgyTranscript({+ transcriptText,+ sessionId: 'sess-1',+ cwd: '/tmp/portos-agy-scratch-xyz',+ });+ expect(events).toEqual([]);+ });++ it('extracts generate_image tool calls from outside scratch directories', () => {+ const transcriptText = [+ JSON.stringify({+ type: 'PLANNER_RESPONSE',+ created_at: '2026-10-10T12:00:00.000Z',+ tool_calls: [+ { name: 'generate_image', args: { prompt: 'a dog', imagePaths: ['img1.png', 'img2.png'] } },+ { name: 'other_tool', args: {} },+ ],+ }),+ JSON.stringify({+ type: 'PLANNER_RESPONSE',+ created_at: '2026-10-10T12:05:00.000Z',+ tool_calls: [+ { name: 'generate_image', args: { prompt: 'another dog' } },+ ],+ }),+ ].join('\n');++ const events = imagineEventsFromAgyTranscript({+ transcriptText,+ sessionId: 'sess-2',+ cwd: '/Users/test/projects/my-app',+ });++ expect(events).toHaveLength(2);+ expect(events[0]).toMatchObject({+ id: 'session-image:sess-2:0',+ day: '2026-10-10',+ images: 1,+ inputImages: 2,+ source: 'measured',+ });+ expect(events[1]).toMatchObject({+ id: 'session-image:sess-2:1',+ day: '2026-10-10',+ images: 1,+ inputImages: 0,+ source: 'measured',+ });+ });+});++describe('planAgyUsageRepair', () => {+ it('plans additions for unrecorded media events and skips existing repairIds', () => {+ const media = [+ { id: 'gallery:job-1', day: '2026-10-10', images: 1, inputImages: 0 },+ { id: 'gallery:job-2', day: '2026-10-11', images: 1, inputImages: 1 },+ { id: 'gallery:job-1', day: '2026-10-10', images: 1, inputImages: 0 }, // duplicate in list+ ];++ const plan = planAgyUsageRepair({+ media,+ repairIds: ['gallery:job-2'],+ providerId: 'antigravity-cli',+ });++ expect(plan.additions).toHaveLength(1);+ expect(plan.additions[0]).toEqual({+ repairId: 'gallery:job-1',+ day: '2026-10-10',+ record: {+ providerId: 'antigravity-cli',+ model: 'imagen-3.0-generate-002',+ messages: 0,+ images: 1,+ videoSeconds: 0,+ inputImages: 0,+ source: 'measured',+ },+ });++ const summary = summarizeAgyRepairAdditions(plan.additions);+ expect(summary).toEqual({ added: 1, images: 1 });+ });+});diff --git a/server/services/grokUsageBackfill.js b/server/services/grokUsageBackfill.jsindex 6e3f90c62c..d63139f101 100644--- a/server/services/grokUsageBackfill.js+++ b/server/services/grokUsageBackfill.js@@ -165,7 +165,9 @@ async function collectVideos(dataDir) {async function providerIdFromFile(dataDir) {const stored = await readJson(join(dataDir, 'providers.json'), null, { label: 'providers.json' });- const list = Array.isArray(stored) ? stored : (stored?.providers || []);+ const list = Array.isArray(stored)+ ? stored+ : (Array.isArray(stored?.providers) ? stored.providers : Object.values(stored?.providers || {}));const grok = list.filter((provider) => provider && familyForProvider(provider) === 'grok');return grokUsageProviderIdFromList(grok.length ? list : []);}diff --git a/server/services/imageGen/agy.js b/server/services/imageGen/agy.jsindex 1943066cf7..01a97cf79c 100644--- a/server/services/imageGen/agy.js+++ b/server/services/imageGen/agy.js@@ -500,6 +500,18 @@ function runAgy(job, jobId, bin, args, {sidecarPath: sidecar,mode: IMAGE_GEN_MODE.AGY,});+ try {+ const inputImageCount = (meta.initImageFilename ? 1 : 0) + (Array.isArray(meta.referenceImageFilenames) ? meta.referenceImageFilenames.length : 0);+ const { recordAgyImageUsage } = await import('../agyMediaUsage.js');+ await recordAgyImageUsage({+ jobId,+ createdAt: meta.createdAt,+ inputImages: inputImageCount,+ model: meta.imageModel || AGY_IMAGEGEN_IMAGE_MODEL,+ });+ } catch (usageErr) {+ console.error(`❌ Failed to record Antigravity image usage [${jobId.slice(0, 8)}]: ${usageErr.message}`);+ }job.status = 'complete';if (activeProcs.get(jobId) === proc) activeProcs.delete(jobId);activeJobs.delete(jobId);diff --git a/server/services/imageGen/agy.test.js b/server/services/imageGen/agy.test.jsindex 76f61a2b04..60548b6ccd 100644--- a/server/services/imageGen/agy.test.js+++ b/server/services/imageGen/agy.test.js@@ -68,6 +68,9 @@ vi.mock('../../lib/maintenanceAdmission.js', async importOriginal => ({maintenance: new Proxy({}, { get: (_target, property) => fixture.admission[property] }),}));+const { recordAgyImageUsage } = vi.hoisted(() => ({ recordAgyImageUsage: vi.fn(async () => {}) }));+vi.mock('../agyMediaUsage.js', () => ({ recordAgyImageUsage }));+const agy = await import('./agy.js');const { AGY_IMAGEGEN_DEFAULT_MODEL } = await import('../../lib/imageGenCapabilities.js');const { imageGenEvents } = await import('../imageGenEvents.js');@@ -318,6 +321,9 @@ describe('agy image provider', () => {expect(sidecar.initImageFilename).toBe('source.png');expect(sidecar.inputProvenance).toBe('provider-inputs');expect(sidecar.referenceImageFilenames).toEqual(['ref-a.png', 'ref-b.png']);+ expect(recordAgyImageUsage).toHaveBeenCalledWith(expect.objectContaining({+ jobId: job.jobId, inputImages: 3,+ }));});// agy is a coding agent: when generate_image is quota-exhausted it can stilldiff --git a/server/services/usage.js b/server/services/usage.jsindex 0db680d010..7740463843 100644--- a/server/services/usage.js+++ b/server/services/usage.js@@ -1,6 +1,6 @@import { join } from 'path';import { atomicWrite, ensureDir, PATHS, readJSONFile } from '../lib/fileUtils.js';-import { resolveModelRates, isFreeProvider, isFreeModelId, estimateCostUsd, estimateGrokImagineCost, PRICING_AS_OF } from '../lib/modelPricing.js';+import { resolveModelRates, isFreeProvider, isFreeModelId, estimateCostUsd, estimateGrokImagineCost, estimateAgyImageCost, PRICING_AS_OF } from '../lib/modelPricing.js';import { familyForProvider } from '../lib/providerFamilies.js';import { roundCents } from '../lib/subscriptionSavings.js';import { POLLUTING_KEYS } from '../lib/objects.js';@@ -981,7 +981,7 @@ function usageRowCost(modelId, counts, rates, free) {cacheReadTokens: counts?.longCacheReadTokens,cacheWriteTokens: counts?.longCacheWriteTokens,}) : 0;- return tokens + premium + estimateGrokImagineCost(modelId, counts);+ return tokens + premium + estimateGrokImagineCost(modelId, counts) + estimateAgyImageCost(modelId, counts);}export function buildUsageReport(dailyActivity, { from = null, to = null, providers = [], monthlyActivity = null, totalTokens = null } = {}) {diff --git a/server/services/usageBackfill.js b/server/services/usageBackfill.jsindex a65e878843..202bc002ee 100644--- a/server/services/usageBackfill.js+++ b/server/services/usageBackfill.js@@ -3,6 +3,7 @@ import { Worker } from 'worker_threads';import { homedir } from 'os';import { atomicWrite, PATHS, readJSONFile } from '../lib/fileUtils.js';import { applyGrokUsageRepair } from './grokUsageBackfill.js';+import { applyAgyUsageRepair } from './agyUsageBackfill.js';import {applyHistoricalUsageCorrections,getReconciledUsageRunIds,@@ -18,6 +19,7 @@ const idleJob = () => ({found: 0,corrected: 0,grok: null,+ agy: null,error: null,startedAt: null,completedAt: null@@ -116,6 +118,35 @@ export async function startHistoricalUsageBackfill({return grokOnce;};+ let agyOnce = null;+ const repairAgy = () => {+ agyOnce ??= applyAgyUsageRepair({ home, providers: providerList })+ .then((result) => ({+ agy: {+ added: result.added || 0,+ images: result.images || 0,+ backedUp: Boolean(result.backedUp),+ note: result.note || null,+ },+ agyError: null,+ }))+ .catch((error) => ({ agy: null, agyError: error.message }));+ return agyOnce;+ };++ const repairMedia = async () => {+ const [repairedGrok, repairedAgy] = await Promise.all([repairGrok(), repairAgy()]);+ const errors = [+ repairedGrok.grokError && `Grok usage repair failed: ${repairedGrok.grokError}`,+ repairedAgy.agyError && `Antigravity usage repair failed: ${repairedAgy.agyError}`,+ ].filter(Boolean);+ return {+ grok: repairedGrok.grok,+ agy: repairedAgy.agy,+ error: errors.length ? errors.join('. ') : null,+ };+ };+const fail = (error) => {job = { ...job, status: 'error', error, completedAt: new Date().toISOString() };notifyUsageBackfillUpdated();@@ -125,15 +156,15 @@ export async function startHistoricalUsageBackfill({worker.on('message', (message) => {messageTail = messageTail.then(async () => {if (message?.type === 'progress') {- job = { ...job, ...message.progress, grok: job.grok ?? null };+ job = { ...job, ...message.progress, grok: job.grok ?? null, agy: job.agy ?? null };notifyUsageBackfillUpdated();return;}if (message?.type === 'error') {- const repaired = await repairGrok();- const error = [message.error || 'Backfill failed', repaired.grokError && `Grok usage repair failed: ${repaired.grokError}`]+ const repaired = await repairMedia();+ const error = [message.error || 'Backfill failed', repaired.error].filter(Boolean).join('. ');- job = { ...job, status: 'error', error, grok: repaired.grok, completedAt: new Date().toISOString() };+ job = { ...job, status: 'error', error, grok: repaired.grok, agy: repaired.agy, completedAt: new Date().toISOString() };notifyUsageBackfillUpdated();return;}@@ -144,14 +175,15 @@ export async function startHistoricalUsageBackfill({const appliedCorrections = (result.corrections || []).filter((correction) => applied.correctedRunIds.includes(correction.runId));await markRunMetadata(appliedCorrections);- const repaired = await repairGrok();- if (repaired.grokError) {+ const repaired = await repairMedia();+ if (repaired.error) {job = {...job,status: 'error',- error: `Grok usage repair failed: ${repaired.grokError}`,+ error: repaired.error,corrected: applied.corrected,- grok: null,+ grok: repaired.grok,+ agy: repaired.agy,completedAt: new Date().toISOString()};notifyUsageBackfillUpdated();@@ -165,6 +197,7 @@ export async function startHistoricalUsageBackfill({found: result.corrections?.length || 0,corrected: applied.corrected,grok: repaired.grok,+ agy: repaired.agy,completedAt: new Date().toISOString()};notifyUsageBackfillUpdated();@@ -174,10 +207,10 @@ export async function startHistoricalUsageBackfill({});worker.on('error', (error) => {messageTail = messageTail.then(async () => {- const repaired = await repairGrok();- const message = [error.message, repaired.grokError && `Grok usage repair failed: ${repaired.grokError}`]+ const repaired = await repairMedia();+ const message = [error.message, repaired.error].filter(Boolean).join('. ');- job = { ...job, status: 'error', error: message, grok: repaired.grok, completedAt: new Date().toISOString() };+ job = { ...job, status: 'error', error: message, grok: repaired.grok, agy: repaired.agy, completedAt: new Date().toISOString() };notifyUsageBackfillUpdated();}).catch((err) => fail(err.message));});diff --git a/server/services/usageInteractiveScan.js b/server/services/usageInteractiveScan.jsindex ba271af3b8..780e8ccdbe 100644--- a/server/services/usageInteractiveScan.js+++ b/server/services/usageInteractiveScan.js@@ -209,9 +209,12 @@ function parseSlice(family, entries, slice, seen) {// a later repair of turns already at or behind the watermark does not// add that premium a second time.repairIds: (parsed.longTurns || []).map((turn) => grokTurnRepairId(turn.sessionId, turn.key)),+ source: 'measured',};}- return { byModel: { [UNKNOWN_MODEL]: agyEstimatedBuckets(parseAgyTranscript(entries, { from: slice.from, to: slice.to })) }, repairIds: [] };+ const parsed = parseAgyTranscript(entries, { from: slice.from, to: slice.to });+ const buckets = agyEstimatedBuckets(parsed);+ return { byModel: { [UNKNOWN_MODEL]: buckets }, repairIds: [], source: buckets.source };}/** One session file's billed buckets per `{ day, model }` inside `[from, to]`, minus run windows. */@@ -236,7 +239,7 @@ async function parseSession(family, session, from, to, byCwd, seen) {if (totalTranscriptTokens(buckets) <= 0) continue;const repairIds = attachedIds ? [] : (parsed.repairIds || []);attachedIds = true;- results.push({ day: slice.day, model, buckets, repairIds });+ results.push({ day: slice.day, model, buckets, repairIds, source: parsed.source });}}}@@ -272,13 +275,14 @@ export const refreshInteractiveUsage = ({ home = homedir(), runsDir = PATHS.runsconst perDay = new Map();const seen = new Set();for (const session of await listSessions(family, home, from)) {- for (const { day, model, buckets, repairIds } of await parseSession(family, session, from, to, byCwd, seen)) {- if (!perDay.has(day)) perDay.set(day, { byModel: {}, repairIds: [] });+ for (const { day, model, buckets, repairIds, source } of await parseSession(family, session, from, to, byCwd, seen)) {+ if (!perDay.has(day)) perDay.set(day, { byModel: {}, repairIds: [], sources: new Set() });const dayTotals = perDay.get(day);addBuckets(dayTotals, buckets);dayTotals.byModel[model] ??= {};addBuckets(dayTotals.byModel[model], buckets);if (repairIds?.length) dayTotals.repairIds.push(...repairIds);+ if (source) dayTotals.sources.add(source);}// A first scan parses many multi-MB files back to back; let the server breathe.await yieldToEventLoop();@@ -287,8 +291,10 @@ export const refreshInteractiveUsage = ({ home = homedir(), runsDir = PATHS.runsconst corrections = [];let tokens = 0;for (const [day, totals] of perDay) {- // Antigravity writes no token counts; every other family's are the CLI's own.- const measured = { ...totals, source: family === 'agy' ? 'estimate' : 'measured', model: null };+ const source = totals.sources?.has('measured') && totals.sources?.has('estimate')+ ? 'mixed'+ : totals.sources?.values().next().value || (family === 'agy' ? 'estimate' : 'measured');+ const measured = { ...totals, source, model: null };const provider = resolveFamilyProvider(providerList, family, measured);if (!provider) continue;tokens += totalTranscriptTokens(totals);diff --git a/server/services/usageReconciler.js b/server/services/usageReconciler.jsindex 8637a5b583..9ec3dccf4c 100644--- a/server/services/usageReconciler.js+++ b/server/services/usageReconciler.js@@ -380,17 +380,18 @@ function codexDateDirs(root, fromMs, toMs) {}/**- * Token buckets for a `parseAgyTranscript` result. Antigravity writes no token- * counts, so every figure is chars/4 — but the model calls' replayed context is- * priced as cache reads (`contextChars`) rather than dropped, since dropping it- * is what made the plan read as cheaper than its subscription.+ * Token buckets for a `parseAgyTranscript` result. When the transcript carries+ * measured token counts (`input_tokens`, `output_tokens`), those counts are+ * preserved with source `measured`. Otherwise figures fall back to chars/4+ * estimates with source `estimate`.*/export const agyEstimatedBuckets = (parsed) => ({messages: parsed.messages,- tokensIn: estimateTokensFromChars(parsed.charsIn),- tokensOut: estimateTokensFromChars(parsed.charsOut),- cacheReadTokens: estimateTokensFromChars(parsed.contextChars),- cacheWriteTokens: 0+ tokensIn: parsed.measured ? parsed.tokensIn : estimateTokensFromChars(parsed.charsIn),+ tokensOut: parsed.measured ? parsed.tokensOut : estimateTokensFromChars(parsed.charsOut),+ cacheReadTokens: parsed.measured ? parsed.cacheReadTokens : estimateTokensFromChars(parsed.contextChars),+ cacheWriteTokens: 0,+ source: parsed.measured ? 'measured' : 'estimate',});const TOKEN_FIELDS = ['messages', 'tokensIn', 'tokensOut', 'cacheReadTokens', 'cacheWriteTokens'];@@ -532,7 +533,7 @@ async function readAgySessions({ home, workspacePath, from, to, fold, reserveFroreserveFrom(transcriptPath, parsed);// No model is named anywhere in the transcript — the UNKNOWN_MODEL bucket// lets the caller attribute it to the provider's own configured model.- fold({ ...estimated, models: [], byModel: { [UNKNOWN_MODEL]: { ...estimated } } }, 'estimate');+ fold({ ...estimated, models: [], byModel: { [UNKNOWN_MODEL]: { ...estimated } } }, estimated.source || 'estimate');}}