diff --git a/docs-site/public/pr-screenshots/t4-log-polling-81a8.png b/docs-site/public/pr-screenshots/t4-log-polling-81a8.png new file mode 100644 index 0000000000..4dd53042e9 Binary files /dev/null and b/docs-site/public/pr-screenshots/t4-log-polling-81a8.png differ diff --git a/docs-site/src/content/docs/fr/guides/providers.md b/docs-site/src/content/docs/fr/guides/providers.md index 5de4260813..988ca03cf7 100644 --- a/docs-site/src/content/docs/fr/guides/providers.md +++ b/docs-site/src/content/docs/fr/guides/providers.md @@ -124,6 +124,9 @@ ocx logout | `cursor` | `cursor` | `https://api2.cursor.sh` | Connexion PKCE expérimentale, transport HTTP/2 en direct et découverte de modèles filtrés par compte. | | `github-copilot` | `openai-chat` | `https://api.githubcopilot.com` | Expérimental. Flux d'appareil GitHub et échange `copilot_internal` (client OAuth de VS Code). Nécessite un abonnement Copilot actif ; il ne s'agit pas d'une API tierce officielle. | +Les vérifications de quota Google Antigravity utilisent des points de terminaison Google fixes, y compris le repli vers la liste des modèles. Elles prennent en charge le DNS Fake-IP transparent pour ces destinations en conservant la vérification TLS, le refus des redirections et les contrôles des adresses privées. Une URL de base personnalisée ne modifie que les requêtes de modèles ; `NO_PROXY` conserve la politique de connexion directe. + + Après un échec définitif d'actualisation de Nous, exécutez `ocx login nous` pour vous réauthentifier. Pour les préréglages canoniques du forfait Kimi Coding (`kimi` pour la connexion au compte et `kimi-code` diff --git a/docs-site/src/content/docs/guides/providers.md b/docs-site/src/content/docs/guides/providers.md index 255c0d8dc4..05f586a816 100644 --- a/docs-site/src/content/docs/guides/providers.md +++ b/docs-site/src/content/docs/guides/providers.md @@ -125,6 +125,9 @@ ocx logout | `cursor` | `cursor` | `https://api2.cursor.sh` | Experimental PKCE login, live HTTP/2 transport with an opt-in HTTP/1.1 compatibility path, and account-filtered model discovery. | | `github-copilot` | `openai-chat` | `https://api.githubcopilot.com` | Experimental. GitHub device flow + `copilot_internal` exchange (VS Code OAuth client). Requires an active Copilot subscription; not an official third-party API. | +Google Antigravity account and provider quota probes use fixed Google accounting endpoints, including the models fallback. They support transparent Fake-IP DNS for those destinations while retaining TLS verification, redirect rejection and private-address checks. A custom provider base URL changes model requests, not quota destinations; `NO_PROXY` continues to select the direct-route policy. + + After a terminal Nous refresh failure, run `ocx login nous` to reauthenticate. For the canonical Kimi Coding Plan presets (`kimi` account login and `kimi-code` API key), diff --git a/docs-site/src/content/docs/ja/guides/providers.md b/docs-site/src/content/docs/ja/guides/providers.md index 51d018f478..33be9fc694 100644 --- a/docs-site/src/content/docs/ja/guides/providers.md +++ b/docs-site/src/content/docs/ja/guides/providers.md @@ -114,6 +114,9 @@ ocx logout | `cursor` | `cursor` | `https://api2.cursor.sh` | 実験的 PKCE ログイン、HTTP/2 トランスポート、アカウント別モデル探索をサポート。 | | `github-copilot` | `openai-chat` | `https://api.githubcopilot.com` | 実験的。GitHub デバイスフロー + `copilot_internal` 交換(VS Code OAuth クライアント)。有効な Copilot サブスクリプションが必要で、公式のサードパーティ API ではありません。 | +Google Antigravity のアカウント・プロバイダーのクォータ確認は、モデル一覧へのフォールバックも含め、固定の Google エンドポイントを使用します。その宛先では透過 Fake-IP DNS に対応し、TLS 検証、リダイレクト拒否、プライベートアドレス検査を維持します。カスタム base URL はモデル要求にのみ適用されます。`NO_PROXY` は直接接続のポリシーを維持します。 + + Nous の refresh が終端失敗した場合は、再認証に `ocx login nous` を実行してください。 正規の Kimi Coding Plan プリセット(`kimi` アカウントログインと `kimi-code` API key)では、 diff --git a/docs-site/src/content/docs/ko/guides/providers.md b/docs-site/src/content/docs/ko/guides/providers.md index 3c28be0a59..7781cc4f52 100644 --- a/docs-site/src/content/docs/ko/guides/providers.md +++ b/docs-site/src/content/docs/ko/guides/providers.md @@ -113,6 +113,9 @@ ocx logout | `cursor` | `cursor` | `https://api2.cursor.sh` | 실험적 PKCE 로그인, HTTP/2 전송, 계정별 모델 탐색을 지원합니다. | | `github-copilot` | `openai-chat` | `https://api.githubcopilot.com` | 실험적. GitHub 디바이스 플로우 + `copilot_internal` 교환(VS Code OAuth 클라이언트). 활성 Copilot 구독 필요; 공식 서드파티 API가 아닙니다. | +Google Antigravity 계정·제공자 할당량 확인은 모델 목록 폴백을 포함해 고정된 Google 회계 엔드포인트를 사용합니다. 해당 목적지의 투명 Fake-IP DNS를 지원하며 TLS 검증, 리다이렉트 거부, 사설 주소 검사는 유지합니다. 사용자 지정 base URL은 모델 요청에만 적용되며 할당량 목적지는 바꾸지 않습니다. `NO_PROXY`는 기존 직접 연결 정책을 유지합니다. + + Nous refresh가 종료 실패한 경우, `ocx login nous`로 재인증하세요. 정식 Kimi Coding Plan 프리셋(`kimi` 계정 로그인과 `kimi-code` API key)의 경우, opencodex는 diff --git a/docs-site/src/content/docs/reference/management-api.md b/docs-site/src/content/docs/reference/management-api.md index a12303cfd7..cef695b4ad 100644 --- a/docs-site/src/content/docs/reference/management-api.md +++ b/docs-site/src/content/docs/reference/management-api.md @@ -166,6 +166,14 @@ See [Combos](/guides/combos/) for target strategies, cooldowns, aliases, and rou ### Logs, usage, and storage +`GET /api/logs` accepts an optional opaque `cursor` from its previous response. The envelope preserves +`logs`, `total`, `generatedAt` and `timeZone`, and adds `cursor` and `reset`. Without a cursor it returns +the full filtered window. A valid unchanged prefix returns only appended rows; `reset: true` replaces +the client window after edits, eviction, query changes or restart. Invalid cursors return HTTP 400 with +`error.code: "invalid_cursor"`. Authentication is unchanged. The dashboard falls back to full snapshots +for older servers. This reduces response bytes for stable windows; server projection remains bounded +by the current window size. + | Method and path | Purpose | Notable errors | | --- | --- | --- | | `GET /api/logs` | Query filtered in-memory request logs | — | diff --git a/docs-site/src/content/docs/ru/guides/providers.md b/docs-site/src/content/docs/ru/guides/providers.md index b99eb88ec2..b057cf77c6 100644 --- a/docs-site/src/content/docs/ru/guides/providers.md +++ b/docs-site/src/content/docs/ru/guides/providers.md @@ -123,6 +123,9 @@ ocx logout | `cursor` | `cursor` | `https://api2.cursor.sh` | Экспериментальный PKCE-вход, живой транспорт HTTP/2 и обнаружение моделей с фильтрацией по аккаунту. | | `github-copilot` | `openai-chat` | `https://api.githubcopilot.com` | Экспериментально. Device flow GitHub + обмен `copilot_internal` (OAuth-клиент VS Code). Требуется активная подписка Copilot; это не официальный сторонний API. | +Проверки квот аккаунтов и провайдера Google Antigravity используют фиксированные адреса Google, включая резервный запрос списка моделей. Для этих адресов поддерживается прозрачный Fake-IP DNS с сохранением проверки TLS, запрета перенаправлений и проверки частных адресов. Пользовательский base URL меняет только запросы моделей; `NO_PROXY` сохраняет политику прямого подключения. + + После терминального сбоя обновления Nous выполните `ocx login nous`, чтобы пройти повторную аутентификацию. Для канонических пресетов Kimi Coding Plan (вход через аккаунт `kimi` и API-ключ `kimi-code`) diff --git a/docs-site/src/content/docs/tr/guides/providers.md b/docs-site/src/content/docs/tr/guides/providers.md index 4559133d43..c5564e74a1 100644 --- a/docs-site/src/content/docs/tr/guides/providers.md +++ b/docs-site/src/content/docs/tr/guides/providers.md @@ -138,6 +138,9 @@ ocx logout | `cursor` | `cursor` | `https://api2.cursor.sh` | Deneysel PKCE girişi, canlı HTTP/2 aktarımı ve hesap filtreli model keşfi. | | `github-copilot` | `openai-chat` | `https://api.githubcopilot.com` | Deneysel. GitHub cihaz akışı + `copilot_internal` değişimi (VS Code OAuth istemcisi). Aktif bir Copilot aboneliği gerektirir; resmi bir üçüncü taraf API değildir. | +Google Antigravity hesap ve sağlayıcı kota sorguları, model listesine geri dönüş dahil sabit Google uç noktalarını kullanır. Bu hedefler için şeffaf Fake-IP DNS desteklenirken TLS doğrulaması, yönlendirme reddi ve özel adres kontrolleri korunur. Özel base URL yalnızca model isteklerini değiştirir; `NO_PROXY` doğrudan bağlantı politikasını korur. + + Uç bir Nous yenileme hatasından sonra yeniden kimlik doğrulamak için `ocx login nous` çalıştırın. diff --git a/docs-site/src/content/docs/zh-cn/guides/providers.md b/docs-site/src/content/docs/zh-cn/guides/providers.md index a4bbab6186..3fb72a4e6a 100644 --- a/docs-site/src/content/docs/zh-cn/guides/providers.md +++ b/docs-site/src/content/docs/zh-cn/guides/providers.md @@ -104,6 +104,9 @@ ocx logout | `cursor` | `cursor` | `https://api2.cursor.sh` | 实验性 PKCE 登录、带可选 HTTP/1.1 兼容路径的 HTTP/2 传输,以及按账号筛选的模型发现。 | | `github-copilot` | `openai-chat` | `https://api.githubcopilot.com` | 实验性。GitHub 设备流 + `copilot_internal` 交换(VS Code OAuth 客户端)。需要有效的 Copilot 订阅;不是官方第三方 API。 | +Google Antigravity 账户和提供方的配额查询(包括模型列表回退)使用固定的 Google 计量端点。这些目标支持透明 Fake-IP DNS,同时保留 TLS 验证、重定向拒绝和私有地址检查。自定义 base URL 仅改变模型请求,不改变配额目标;`NO_PROXY` 仍使用直连策略。 + + Nous refresh 发生终止性失败后,请运行 `ocx login nous` 重新认证。 对于规范的 Kimi Coding Plan 预设(`kimi` 账号登录和 `kimi-code` API key),opencodex diff --git a/docs-site/src/content/docs/zh-tw/guides/providers.md b/docs-site/src/content/docs/zh-tw/guides/providers.md index 0c20867124..96cf91ccfb 100644 --- a/docs-site/src/content/docs/zh-tw/guides/providers.md +++ b/docs-site/src/content/docs/zh-tw/guides/providers.md @@ -112,6 +112,9 @@ ocx logout | `cursor` | `cursor` | `https://api2.cursor.sh` | 實驗性 PKCE 登入、即時 HTTP/2 transport 與按帳號篩選的模型探索。 | | `github-copilot` | `openai-chat` | `https://api.githubcopilot.com` | 實驗性。GitHub device flow + `copilot_internal` exchange(VS Code OAuth client)。需要有效 Copilot 訂閱;不是官方第三方 API。 | +Google Antigravity 帳戶與供應商的配額查詢(包括模型清單備援)使用固定的 Google 計量端點。這些目標支援透明 Fake-IP DNS,同時保留 TLS 驗證、重新導向拒絕與私有位址檢查。自訂 base URL 只改變模型請求,不改變配額目標;`NO_PROXY` 仍使用直連政策。 + + 終端 Nous refresh 失敗後,執行 `ocx login nous` 重新認證。 對 canonical Kimi Coding Plan preset(`kimi` 帳號登入與 `kimi-code` API key),opencodex 只會把 caller diff --git a/gui/src/pages/Logs.tsx b/gui/src/pages/Logs.tsx index 2632ae8788..03a0a45005 100644 --- a/gui/src/pages/Logs.tsx +++ b/gui/src/pages/Logs.tsx @@ -27,6 +27,7 @@ import { sanitizeLogEntryRouteDecision, validCachedRouteDecision, } from "./log-route-decision"; +import { mergeLogDelta, parseLogPollResponse } from "./log-poll"; function logsCacheKey(apiBase: string): string { return `ocx.logs.list.v1:${apiBase}`; @@ -385,11 +386,16 @@ export default function Logs({ apiBase }: { apiBase: string }) { const filterClockRef = useRef<{ key: string; anchor?: LogsClockAnchor; active: boolean; request: number; }>({ key: resourceKey, active: false, request: 0 }); + const logPollRef = useRef<{ key: string; cursor: string | null; rows: LogEntry[] }>( + { key: resourceKey, cursor: null, rows: [] }, + ); // Invalidate the old resource at commit, before passive resource-loader effects. // A late body read must not mutate this page's clock, cache or retry state. useLayoutEffect(() => { const clock = { key: resourceKey, active: true, request: 0 }; filterClockRef.current = clock; + // Cached display rows never establish a cursor, including A -> B -> A. + logPollRef.current = { key: resourceKey, cursor: null, rows: [] }; setFilterClockNow(Date.now()); return () => { clock.active = false; }; }, [resourceKey]); @@ -462,16 +468,22 @@ export default function Logs({ apiBase }: { apiBase: string }) { logRetryRef.current = retry; } if (retry.failures > 0 && Date.now() < retry.nextAttemptAt) throw retry.error; + const poll = logPollRef.current; + const cursor = poll.key === resourceKey ? poll.cursor : null; + const url = `${apiBase}/api/logs?limit=2000${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ""}`; try { - const res = await fetch(`${apiBase}/api/logs?limit=2000`, { signal }); + const res = await fetch(url, { signal }); if (!res.ok) throw new Error(`${res.status} ${res.statusText}`.trim()); - const body = await res.json() as LogEntry[] | { logs?: LogEntry[]; generatedAt?: unknown }; + const body: unknown = await res.json(); const receivedAt = performance.now(); - const raw = Array.isArray(body) ? body : (body.logs ?? []); - const next = raw.map(sanitizeLogEntryRouteDecision); + const parsed = parseLogPollResponse(body); + const incoming = parsed.rows.map(sanitizeLogEntryRouteDecision); + const next = cursor && parsed.cursor && !parsed.reset + ? mergeLogDelta(poll.rows, incoming) : incoming; // The resource-store generation guard runs only after this loader returns. // Guard these local side effects here as fetch/body readers may ignore abort. if (!isCurrent()) throw signal.reason ?? new DOMException("Obsolete log request", "AbortError"); + logPollRef.current = { key: resourceKey, cursor: parsed.cursor, rows: next }; // Reconcile when the accepted snapshot changes, using the latest user state // rather than filters captured when the request started. Persist disappearance // as All so a later ring cannot resurrect a cleared selection. @@ -488,7 +500,7 @@ export default function Logs({ apiBase }: { apiBase: string }) { if (previous.model === nextModel && previous.provider === nextProvider) return previous; return { ...previous, model: nextModel, provider: nextProvider }; }); - const sample = logsClockAnchor(Array.isArray(body) ? undefined : body.generatedAt, receivedAt); + const sample = logsClockAnchor(parsed.generatedAt, receivedAt); if (sample) clock.anchor = sample; setFilterClockNow(logsClockNow(clock.anchor, receivedAt, Date.now())); logRetryRef.current = { key: resourceKey, failures: 0, nextAttemptAt: 0, error: null }; @@ -525,6 +537,7 @@ export default function Logs({ apiBase }: { apiBase: string }) { const fetchLogs = logsResource.refresh; const retryLogs = useCallback(() => { logRetryRef.current = { key: resourceKey, failures: 0, nextAttemptAt: 0, error: null }; + logPollRef.current = { key: resourceKey, cursor: null, rows: [] }; fetchLogs({ forceLoading: true }); }, [fetchLogs, resourceKey]); diff --git a/gui/src/pages/log-poll.ts b/gui/src/pages/log-poll.ts new file mode 100644 index 0000000000..ad13d986f7 --- /dev/null +++ b/gui/src/pages/log-poll.ts @@ -0,0 +1,38 @@ +export interface ParsedLogPollResponse { + rows: T[]; + cursor: string | null; + reset: boolean; + generatedAt?: unknown; + timeZone?: string; + total?: number; +} + +/** Legacy responses replace the window; malformed cursor responses keep last-good data. */ +export function parseLogPollResponse(body: unknown): ParsedLogPollResponse { + if (Array.isArray(body)) return { rows: body as T[], cursor: null, reset: false }; + if (!body || typeof body !== "object") throw new Error("Invalid log response"); + const value = body as Record; + const hasCursor = Object.hasOwn(value, "cursor") || Object.hasOwn(value, "reset"); + if ((value.logs !== undefined && !Array.isArray(value.logs)) + || (hasCursor && (!Array.isArray(value.logs) + || typeof value.cursor !== "string" || value.cursor.length === 0 || value.cursor.length > 512 + || !/^[A-Za-z0-9_-]+$/.test(value.cursor) || typeof value.reset !== "boolean"))) { + throw new Error("Invalid log response"); + } + return { + rows: (value.logs ?? []) as T[], + cursor: hasCursor ? value.cursor as string : null, + reset: value.reset === true, + generatedAt: value.generatedAt, + ...(typeof value.timeZone === "string" ? { timeZone: value.timeZone } : {}), + ...(typeof value.total === "number" && Number.isFinite(value.total) && value.total >= 0 + ? { total: value.total } : {}), + }; +} + +/** Updates/removals arrive as resets. Preserve order and even repeated IDs in valid suffixes. */ +export function mergeLogDelta(previous: T[], incoming: readonly T[], cap = 2000): T[] { + if (incoming.length === 0 && previous.length <= cap) return previous; + const merged = [...previous, ...incoming]; + return merged.length > cap ? merged.slice(merged.length - cap) : merged; +} diff --git a/gui/tests/log-poll.test.ts b/gui/tests/log-poll.test.ts new file mode 100644 index 0000000000..d7c2559a1f --- /dev/null +++ b/gui/tests/log-poll.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, test } from "bun:test"; +import { mergeLogDelta, parseLogPollResponse } from "../src/pages/log-poll"; + +describe("log polling protocol", () => { + test("legacy arrays and envelopes replace snapshots without a cursor", () => { + const rows = [{ requestId: "a" }]; + expect(parseLogPollResponse(rows)).toEqual({ rows, cursor: null, reset: false }); + expect(parseLogPollResponse({ logs: rows, generatedAt: 123, timeZone: "UTC", total: 5 })) + .toEqual({ rows, cursor: null, reset: false, generatedAt: 123, timeZone: "UTC", total: 5 }); + expect(parseLogPollResponse({ logs: rows, generatedAt: "bad" }).generatedAt).toBe("bad"); + }); + + test("empty deltas and resets retain clock and window metadata", () => { + for (const reset of [false, true]) { + expect(parseLogPollResponse({ logs: [], cursor: "opaque-cursor", reset, generatedAt: 456, total: 2, timeZone: "UTC" })) + .toEqual({ rows: [], cursor: "opaque-cursor", reset, generatedAt: 456, total: 2, timeZone: "UTC" }); + } + }); + + test("invalid cursor envelopes fail instead of clearing accepted rows", () => { + for (const body of [null, "bad", { logs: {} }, { logs: [], cursor: null, reset: false }, + { logs: [], cursor: "", reset: false }, { logs: [], cursor: "c", reset: "false" }, + { logs: [], cursor: "c" }, { logs: [], reset: false }, { cursor: "c", reset: false }, + { logs: [], cursor: "a".repeat(513), reset: false }, { logs: [], cursor: " c ", reset: false }]) { + expect(() => parseLogPollResponse(body)).toThrow("Invalid log response"); + } + }); + + test("append preserves order and repeated IDs without mutating inputs; cap keeps newest rows", () => { + const previous = [{ requestId: "same", value: 1 }, { requestId: "other", value: 2 }]; + const incoming = [{ requestId: "same", value: 3 }]; + expect(mergeLogDelta(previous, incoming)).toEqual([...previous, ...incoming]); + expect(mergeLogDelta(previous, incoming, 2)).toEqual([previous[1], incoming[0]]); + expect(mergeLogDelta(previous, [])).toBe(previous); + expect(mergeLogDelta(previous, [], 1)).toEqual([previous[1]]); + expect(previous).toEqual([{ requestId: "same", value: 1 }, { requestId: "other", value: 2 }]); + expect(incoming).toEqual([{ requestId: "same", value: 3 }]); + }); +}); diff --git a/gui/tests/logs-auto-refresh.test.tsx b/gui/tests/logs-auto-refresh.test.tsx index 44366f146b..200b6c4f23 100644 --- a/gui/tests/logs-auto-refresh.test.tsx +++ b/gui/tests/logs-auto-refresh.test.tsx @@ -6,7 +6,7 @@ import { LanguageProvider } from "../src/i18n/provider"; import { clearClientResourceStoresForTests } from "../src/client-resource"; import Logs from "../src/pages/Logs"; -const globals = ["document", "window", "navigator", "localStorage", "IS_REACT_ACT_ENVIRONMENT", "ResizeObserver"] as const; +const globals = ["document", "window", "navigator", "localStorage", "sessionStorage", "IS_REACT_ACT_ENVIRONMENT", "ResizeObserver"] as const; let previousGlobals: Record<(typeof globals)[number], unknown>; let testWindow: Window; const originalFetch = globalThis.fetch; @@ -89,6 +89,7 @@ beforeEach(() => { window: { configurable: true, value: testWindow }, navigator: { configurable: true, value: testWindow.navigator }, localStorage: { configurable: true, value: testWindow.localStorage }, + sessionStorage: { configurable: true, value: testWindow.sessionStorage }, }); (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; installLayoutStubs(testWindow); @@ -952,6 +953,156 @@ function proxyLogEnvelope(generatedAt: unknown, logs: unknown[]) { return { generatedAt, timeZone: "UTC", total: logs.length, logs }; } +function cursorLogEnvelope(generatedAt: unknown, logs: unknown[], cursor: string, reset = false) { + return { ...proxyLogEnvelope(generatedAt, logs), cursor, reset }; +} + +test("Logs: append, empty delta, mutation reset and legacy fallback keep the complete window", async () => { + const urls: string[] = []; + let step = 0; + const responses = [ + cursorLogEnvelope(PROXY_NOW, [sampleLog], "c0"), + cursorLogEnvelope(PROXY_NOW, [], "c0"), + cursorLogEnvelope(PROXY_NOW, [updatedLog], "c1"), + cursorLogEnvelope(PROXY_NOW, [], "c1"), + cursorLogEnvelope(PROXY_NOW, [{ ...sampleLog, model: "gpt-mutated", durationMs: 987 }], "c2", true), + [updatedLog], + { logs: [sampleLog] }, + ]; + globalThis.fetch = (async input => { + const url = String(input); + if (!url.includes("/api/logs")) return jsonResponse({ timeZone: "UTC" }); + urls.push(url); + return jsonResponse(responses[step]); + }) as typeof fetch; + const { root, container } = await mountLogs(); + try { + await flushMicrotasks(); + expect(urls[0]).toBe("http://localhost/api/logs?limit=2000"); + step = 1; + await advanceSilentRefresh(); + expect(urls.at(-1)).toContain("cursor=c0"); + expect(visibleRequestIds(container)).toEqual(["req-1"]); + step = 2; + await advanceSilentRefresh(); + expect(visibleRequestIds(container)).toEqual(["req-2", "req-1"]); + await changeLogSelect(container, "Model", "gpt-test"); + step = 3; + await advanceSilentRefresh(); + expect(container.querySelector('select[aria-label="Model"]')!.value).toBe("gpt-test"); + expect(visibleRequestIds(container)).toEqual(["req-1"]); + step = 4; + await advanceSilentRefresh(); + expect(urls.at(-1)).toContain("cursor=c1"); + expect(visibleRequestIds(container)).toEqual(["req-1"]); + expectTableLoaded(container, "gpt-mutated"); + expect(container.textContent).toContain("987"); + expect(container.querySelector('select[aria-label="Model"]')!.value).toBe(""); + step = 5; + await advanceSilentRefresh(); + expect(visibleRequestIds(container)).toEqual(["req-2"]); + step = 6; + await advanceSilentRefresh(); + expect(urls.at(-1)).toBe("http://localhost/api/logs?limit=2000"); + expect(visibleRequestIds(container)).toEqual(["req-1"]); + } finally { + await act(async () => { root.unmount(); }); + } +}); + +test("Logs: an empty delta advances the proxy clock without discarding retained rows", async () => { + let step = 0; + const row = { ...sampleLog, timestamp: PROXY_NOW - 5 * 60_000 }; + globalThis.fetch = (async input => { + if (!String(input).includes("/api/logs")) return jsonResponse({ timeZone: "UTC" }); + return jsonResponse(cursorLogEnvelope(step ? PROXY_NOW + 20 * 60_000 : PROXY_NOW, step ? [] : [row], "same")); + }) as typeof fetch; + const { root, container } = await mountLogs(); + try { + await flushMicrotasks(); + await changeLogSelect(container, "Time", "15m"); + expect(visibleRequestIds(container)).toEqual(["req-1"]); + step = 1; + await advanceSilentRefresh(); + expect(visibleRequestIds(container)).toEqual([]); + await changeLogSelect(container, "Time", "all"); + expect(visibleRequestIds(container)).toEqual(["req-1"]); + expect(JSON.parse(sessionStorage.getItem("ocx.logs.list.v1:http://localhost")!)).toHaveLength(1); + } finally { + await act(async () => { root.unmount(); }); + } +}); + +test("Logs: malformed polls preserve cursor/cache, back off, and explicit retry reads a full snapshot", async () => { + let failing = false; + const urls: string[] = []; + globalThis.fetch = (async input => { + const url = String(input); + if (!url.includes("/api/logs")) return jsonResponse({ timeZone: "UTC" }); + urls.push(url); + if (failing) return jsonResponse({ logs: [], cursor: "poison", reset: "false" }); + return jsonResponse(cursorLogEnvelope(PROXY_NOW, url.includes("cursor=") ? [] : [sampleLog], "good")); + }) as typeof fetch; + const { root, container } = await mountLogs(); + try { + await flushMicrotasks(); + failing = true; + await advanceSilentRefresh(); + const count = urls.length; + await advanceSilentRefresh(); + expect(urls).toHaveLength(count); + await advanceSilentRefresh(14000); + expect(container.textContent).toContain("Could not load request logs."); + expect(visibleRequestIds(container)).toEqual(["req-1"]); + expect(urls.slice(1).every(url => url.includes("cursor=good"))).toBe(true); + expect(JSON.parse(sessionStorage.getItem("ocx.logs.list.v1:http://localhost")!)).toHaveLength(1); + failing = false; + await act(async () => { clickRetry(container); }); + await flushMicrotasks(); + expect(urls.at(-1)).toBe("http://localhost/api/logs?limit=2000"); + expectTableLoaded(container, "gpt-test"); + } finally { + await act(async () => { root.unmount(); }); + } +}); + +test("Logs: A to B to A and remount start without a cached cursor", async () => { + const urls: string[] = []; + globalThis.fetch = (async input => { + const url = String(input); + if (!url.includes("/api/logs")) return jsonResponse({ timeZone: "UTC" }); + urls.push(url); + const name = url.startsWith("http://proxy-a/") ? "a" : "b"; + return jsonResponse(cursorLogEnvelope(PROXY_NOW, url.includes("cursor=") ? [] : [ + { ...sampleLog, requestId: name }, + ], `cursor-${name}`)); + }) as typeof fetch; + const first = await mountLogs("http://proxy-a"); + try { + await flushMicrotasks(); + await advanceSilentRefresh(); + expect(urls.at(-1)).toContain("cursor=cursor-a"); + for (const name of ["b", "a"]) { + const start = urls.length; + await renderLogsAt(first.root, `http://proxy-${name}`); + await advanceSilentRefresh(); + expect(urls[start]).toBe(`http://proxy-${name}/api/logs?limit=2000`); + expect(visibleRequestIds(first.container)).toEqual([name]); + } + } finally { + await act(async () => { first.root.unmount(); }); + } + const start = urls.length; + const remount = await mountLogs("http://proxy-a"); + try { + await advanceSilentRefresh(); + expect(urls[start]).toBe("http://proxy-a/api/logs?limit=2000"); + expect(visibleRequestIds(remount.container)).toEqual(["a"]); + } finally { + await act(async () => { remount.root.unmount(); }); + } +}); + async function renderLogsAt(root: Root, apiBase: string): Promise { await act(async () => { root.render(); @@ -1104,6 +1255,7 @@ function delayedLogBody() { test("Logs: a late body from an aborted old apiBase cannot poison the new proxy clock", async () => { const late = delayedLogBody(); + const urls: string[] = []; let oldSignal: AbortSignal | undefined; let oldRequests = 0; const wall = jest.spyOn(Date, "now").mockReturnValue(PROXY_NOW + 6 * 60 * 60_000); @@ -1113,14 +1265,15 @@ test("Logs: a late body from an aborted old apiBase cannot poison the new proxy globalThis.fetch = (async (input, init) => { const url = String(input); if (!url.includes("/api/logs")) return jsonResponse({ timeZone: "UTC" }); + urls.push(url); if (url.startsWith("http://proxy-a/")) { oldRequests++; oldSignal = init?.signal ?? undefined; return late.response; } - return jsonResponse(proxyLogEnvelope(PROXY_NOW, [ + return jsonResponse(cursorLogEnvelope(PROXY_NOW, url.includes("cursor=") ? [] : [ { ...sampleLog, requestId: "proxy-b", timestamp: PROXY_NOW - 60_000 }, - ])); + ], "cursor-b")); }) as typeof fetch; let mounted: Awaited> | undefined; try { @@ -1136,9 +1289,13 @@ test("Logs: a late body from an aborted old apiBase cannot poison the new proxy await act(async () => { container.querySelector(".logs-auto-refresh input")!.click(); }); await flushMicrotasks(); expect(visibleRequestIds(container)).toEqual(["proxy-b"]); - await act(async () => { late.resolve(proxyLogEnvelope(PROXY_NOW + 12 * 60 * 60_000, [])); }); + await act(async () => { late.resolve(cursorLogEnvelope(PROXY_NOW + 12 * 60 * 60_000, [], "poison", true)); }); await flushMicrotasks(); expect(visibleRequestIds(container)).toEqual(["proxy-b"]); + expect(JSON.parse(sessionStorage.getItem("ocx.logs.list.v1:http://proxy-b")!)).toHaveLength(1); + await act(async () => { container.querySelector(".logs-auto-refresh input")!.click(); }); + await advanceSilentRefresh(); + expect(urls.at(-1)).toContain("cursor=cursor-b"); expect(container.querySelector('select[aria-label="Model"]')!.value).toBe("gpt-test"); expect(container.querySelector('select[aria-label="Provider"]')!.value).toBe("openai"); monotonic += 30_000; @@ -1158,6 +1315,7 @@ test("Logs: a late body from an aborted old apiBase cannot poison the new proxy test("Logs: aborting an in-flight refresh before pausing cannot replace the accepted clock", async () => { const late = delayedLogBody(); + const urls: string[] = []; let requests = 0; let lateSignal: AbortSignal | undefined; const wall = jest.spyOn(Date, "now").mockReturnValue(PROXY_NOW - 6 * 60 * 60_000); @@ -1166,14 +1324,15 @@ test("Logs: aborting an in-flight refresh before pausing cannot replace the acce const clock = trackFilterClock(); globalThis.fetch = (async (input, init) => { if (!String(input).includes("/api/logs")) return jsonResponse({ timeZone: "UTC" }); + urls.push(String(input)); requests++; if (requests === 2) { lateSignal = init?.signal ?? undefined; return late.response; } - return jsonResponse(proxyLogEnvelope(PROXY_NOW, [ + return jsonResponse(cursorLogEnvelope(PROXY_NOW, String(input).includes("cursor=") ? [] : [ { ...sampleLog, requestId: "current", timestamp: PROXY_NOW - 60_000 }, - ])); + ], "accepted-cursor")); }) as typeof fetch; let mounted: Awaited> | undefined; try { @@ -1187,7 +1346,7 @@ test("Logs: aborting an in-flight refresh before pausing cannot replace the acce await flushMicrotasks(); expect(lateSignal?.aborted).toBe(true); const pausedRequests = requests; - await act(async () => { late.resolve(proxyLogEnvelope(PROXY_NOW + 12 * 60 * 60_000, [])); }); + await act(async () => { late.resolve(cursorLogEnvelope(PROXY_NOW + 12 * 60 * 60_000, [], "poison", true)); }); await flushMicrotasks(); expect(visibleRequestIds(container)).toEqual(["current"]); monotonic += 30_000; @@ -1195,6 +1354,9 @@ test("Logs: aborting an in-flight refresh before pausing cannot replace the acce await flushMicrotasks(); expect(visibleRequestIds(container)).toEqual(["current"]); expect(requests).toBe(pausedRequests); + await act(async () => { container.querySelector(".logs-auto-refresh input")!.click(); }); + await advanceSilentRefresh(); + expect(urls.at(-1)).toContain("cursor=accepted-cursor"); } finally { try { if (mounted) await act(async () => { mounted!.root.unmount(); }); @@ -1244,7 +1406,7 @@ test("Logs: a pending refresh reconciles the user's latest selection rather than globalThis.fetch = (async input => { if (!String(input).includes("/api/logs")) return jsonResponse({ timeZone: "UTC" }); requests++; - return requests === 1 ? jsonResponse(original) : late.response; + return requests === 1 ? jsonResponse(cursorLogEnvelope(PROXY_NOW, original, "initial")) : late.response; }) as typeof fetch; const { root, container } = await mountLogs(); try { @@ -1258,10 +1420,10 @@ test("Logs: a pending refresh reconciles the user's latest selection rather than await changeLogSelect(container, "Status", "errors"); expect(visibleRequestIds(container)).toEqual(["b"]); await act(async () => { - late.resolve([ + late.resolve(cursorLogEnvelope(PROXY_NOW, [ { ...original[0]!, requestId: "other", model: "model-other" }, { ...original[1]!, requestId: "current", model: "MODEL-B", provider: "XAI" }, - ]); + ], "replaced", true)); }); await flushMicrotasks(); expect(container.querySelector('select[aria-label="Model"]')!.value).toBe("MODEL-B"); diff --git a/src/providers/quota.ts b/src/providers/quota.ts index bb35c7ead1..7136cd3c70 100644 --- a/src/providers/quota.ts +++ b/src/providers/quota.ts @@ -2607,11 +2607,22 @@ function parseAntigravityQuotaSummary(body: Record | null): Pro } const ANTIGRAVITY_ACCOUNT_QUOTA_BASE = "https://daily-cloudcode-pa.googleapis.com"; -let antigravityOutboundDependencies: ProviderOutboundDependencies = {}; +const ANTIGRAVITY_QUOTA_SUMMARY_URL = `${ANTIGRAVITY_ACCOUNT_QUOTA_BASE}/v1internal:retrieveUserQuotaSummary`; +const ANTIGRAVITY_QUOTA_MODELS_URL = `${ANTIGRAVITY_ACCOUNT_QUOTA_BASE}/v1internal:fetchAvailableModels`; -/** Test seam: inject resolver/pinned transport for the per-account Antigravity probe. */ +/** Only these fixed accounting destinations may use transparent Fake-IP DNS. */ +export function isCanonicalAntigravityQuotaUrl(name: string, url: string): boolean { + return name === "google-antigravity" + && (url === ANTIGRAVITY_QUOTA_SUMMARY_URL || url === ANTIGRAVITY_QUOTA_MODELS_URL); +} + +let antigravityOutboundDependencies: ProviderOutboundDependencies = { + isCanonicalUrl: isCanonicalAntigravityQuotaUrl, +}; + +/** Test seam: inject resolver/pinned transport for provider and per-account probes. */ export function setAntigravityAccountQuotaTransportForTests(dependencies: ProviderOutboundDependencies | null): void { - antigravityOutboundDependencies = dependencies ?? {}; + antigravityOutboundDependencies = { ...dependencies, isCanonicalUrl: isCanonicalAntigravityQuotaUrl }; } /** @@ -2622,7 +2633,7 @@ export function setAntigravityAccountQuotaTransportForTests(dependencies: Provid * A redirect or non-2xx yields null (unavailable), never a partial row. */ export async function fetchAntigravityUsageQuota(accessToken: string, projectId: string): Promise { - const summaryUrl = `${ANTIGRAVITY_ACCOUNT_QUOTA_BASE}/v1internal:retrieveUserQuotaSummary`; + const summaryUrl = ANTIGRAVITY_QUOTA_SUMMARY_URL; try { const summaryResponse = await providerOutboundPost("google-antigravity", { baseUrl: ANTIGRAVITY_ACCOUNT_QUOTA_BASE }, summaryUrl, { headers: { @@ -2644,7 +2655,7 @@ export async function fetchAntigravityUsageQuota(accessToken: string, projectId: // Fallback to fetchAvailableModels on error } - const url = `${ANTIGRAVITY_ACCOUNT_QUOTA_BASE}/v1internal:fetchAvailableModels`; + const url = ANTIGRAVITY_QUOTA_MODELS_URL; const response = await providerOutboundPost("google-antigravity", { baseUrl: ANTIGRAVITY_ACCOUNT_QUOTA_BASE }, url, { headers: { Accept: "application/json", @@ -2662,7 +2673,7 @@ export async function fetchAntigravityUsageQuota(accessToken: string, projectId: return { customWindows, updatedAt: Date.now() }; } -async function fetchAntigravityQuota(provider: string, config: OcxProviderConfig): Promise { +async function fetchAntigravityQuota(provider: string): Promise { const credential = getCredential("google-antigravity"); if (!credential?.projectId) return null; let accessToken: string; @@ -2671,13 +2682,12 @@ async function fetchAntigravityQuota(provider: string, config: OcxProviderConfig } catch { return null; } - const baseUrl = (config.baseUrl || ANTIGRAVITY_ACCOUNT_QUOTA_BASE).replace(/\/+$/, ""); - // The summary probe is pinned to Google's own host through the provider-outbound + // Both probes are pinned to Google's own host through the provider-outbound // transport, mirroring `fetchAntigravityUsageQuota` above: a configured `baseUrl` is a - // routing choice for requests, not a second source of Google's accounting, and this - // request carries the account bearer. - const summaryUrl = `${ANTIGRAVITY_ACCOUNT_QUOTA_BASE}/v1internal:retrieveUserQuotaSummary`; + // routing choice for requests, not a second source of Google's accounting, and these + // requests carry the account bearer. + const summaryUrl = ANTIGRAVITY_QUOTA_SUMMARY_URL; try { const summaryResponse = await providerOutboundPost("google-antigravity", { baseUrl: ANTIGRAVITY_ACCOUNT_QUOTA_BASE }, summaryUrl, { headers: { @@ -2701,8 +2711,8 @@ async function fetchAntigravityQuota(provider: string, config: OcxProviderConfig // Fallback on network/fetch error } - const response = await fetch(`${baseUrl}/v1internal:fetchAvailableModels`, { - method: "POST", + const url = ANTIGRAVITY_QUOTA_MODELS_URL; + const response = await providerOutboundPost("google-antigravity", { baseUrl: ANTIGRAVITY_ACCOUNT_QUOTA_BASE }, url, { headers: { Accept: "application/json", "Content-Type": "application/json", @@ -2711,7 +2721,8 @@ async function fetchAntigravityQuota(provider: string, config: OcxProviderConfig }, body: JSON.stringify({ project: credential.projectId }), signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), - }); + }, antigravityOutboundDependencies); + if (await providerRedirectError(response, url)) return null; if (!response.ok) return null; const customWindows = antigravityWindowsFromModels(asRecord(await readQuotaJson(response))); if (customWindows.length === 0) return null; @@ -2783,7 +2794,7 @@ async function maybeFetchProviderQuota( } if (provider.authMode === "oauth" && explicitAccountReader(name)) return await fetchExplicitCurrentQuota(name, provider, config); if (provider.authMode === "oauth" && name === "anthropic") return fetchAnthropicQuota(name); - if (provider.authMode === "oauth" && name === "google-antigravity") return fetchAntigravityQuota(name, provider); + if (provider.authMode === "oauth" && name === "google-antigravity") return await fetchAntigravityQuota(name); if (provider.authMode === "oauth" && name === "kiro") return fetchKiroQuota(name); // Passive providers (meta-muse): Meta publishes no quota endpoint, so there is no // probe to run — the row is the active account's last in-band observation. diff --git a/src/server/management/logs-usage-routes.ts b/src/server/management/logs-usage-routes.ts index dfb3c74df0..ecbd6f655d 100644 --- a/src/server/management/logs-usage-routes.ts +++ b/src/server/management/logs-usage-routes.ts @@ -66,6 +66,7 @@ import { import type { OcxClaudeCodeConfig, OcxConfig, OcxCustomModel, OcxProviderConfig } from "../../types"; import { drainAndShutdown } from "../lifecycle"; import { filterRequestLogs, filteredRequestLogCount, getRequestLogEntries, type RequestLogEntry } from "../request-log"; +import { decodeRequestLogCursor, selectRequestLogPoll } from "../request-log-cursor"; import { estimateComboCost, estimateRequestCost, normalizeCostTokens, tokensPerSecond } from "../../usage/cost"; import { userCostOverlayVersion } from "../../usage/user-cost-overlays"; import type { PersistedUsageAttempt } from "../../usage/log"; @@ -106,14 +107,20 @@ export async function handleLogsUsageRoutes(ctx: ManagementContext): Promise MAX_CURSOR_LENGTH || !/^[A-Za-z0-9_-]+$/.test(raw)) return null; + try { + const bytes = Buffer.from(raw, "base64url"); + if (bytes.toString("base64url") !== raw) return null; + const value: unknown = JSON.parse(bytes.toString("utf8")); + if (!value || typeof value !== "object" || Array.isArray(value)) return null; + const row = value as Record; + const keys = Object.keys(row).sort().join(","); + if (row.v === 1 && keys === "id,t,v" + && typeof row.t === "number" && Number.isFinite(row.t) && row.t >= 0 + && typeof row.id === "string" && row.id.length > 0 && row.id.length <= 256) { + return { v: 1, t: row.t, id: row.id }; + } + if (row.v !== 2 || keys !== "e,h,n,q,v" + || typeof row.e !== "string" || !/^[a-f0-9]{32}$/.test(row.e) + || typeof row.n !== "number" || !Number.isSafeInteger(row.n) || row.n < 0 || row.n > MAX_WINDOW_ROWS + || typeof row.q !== "string" || !/^[a-f0-9]{64}$/.test(row.q) + || typeof row.h !== "string" || !/^[a-f0-9]{64}$/.test(row.h)) return null; + return { v: 2, e: row.e, n: row.n, q: row.q, h: row.h }; + } catch { + return null; + } +} + +/** + * Compare the current projected window, not ring identities: live entries and + * display-time pricing can change without append. This saves response bytes for + * stable prefixes; DTO projection and hashing still cost O(window bytes). + * No per-client rows or history are retained. The route calls this synchronously + * after projecting the full filtered/paginated window. + */ +export function selectRequestLogPoll( + rows: readonly T[], + params: URLSearchParams, + cursor: RequestLogCursor | null, + epoch = processEpoch, +): { logs: T[]; cursor: string; reset: boolean } { + const query = new URLSearchParams(params); + query.delete("cursor"); + query.sort(); + const queryDigest = createHash("sha256").update(query.toString()).digest("hex"); + const candidate = cursor?.v === 2 && cursor.e === epoch && cursor.q === queryDigest + && cursor.n <= rows.length ? cursor : null; + const full = createHash("sha256"); + const prefix = createHash("sha256"); + for (let index = 0; index < rows.length; index++) { + // JSON escapes embedded newlines, so the delimiter frames each whole row. + const serialized = JSON.stringify(rows[index]) + "\n"; + full.update(serialized); + if (candidate && index < candidate.n) prefix.update(serialized); + } + const unchangedPrefix = candidate !== null && prefix.digest("hex") === candidate.h; + const next: SnapshotCursor = { v: 2, e: epoch, n: rows.length, q: queryDigest, h: full.digest("hex") }; + return { + logs: rows.slice(unchangedPrefix ? candidate.n : 0), + cursor: Buffer.from(JSON.stringify(next)).toString("base64url"), + reset: cursor !== null && !unchangedPrefix, + }; +} diff --git a/tests/providers/provider-account-quota.test.ts b/tests/providers/provider-account-quota.test.ts index 1855e92d66..989f55a410 100644 --- a/tests/providers/provider-account-quota.test.ts +++ b/tests/providers/provider-account-quota.test.ts @@ -15,6 +15,7 @@ import { supportsPerAccountQuota, providerOAuthAccountQuotaMode, } from "../../src/providers/quota"; +import { PROXY_ENV_KEYS } from "../../src/lib/proxy-env"; import { removeTreeWithRetry } from "../helpers/remove-tree"; const originalFetch = globalThis.fetch; @@ -681,7 +682,21 @@ describe("google-antigravity per-account quota (#1082)", () => { }); } - afterEach(() => setAntigravityAccountQuotaTransportForTests(null)); + const proxyKeys = PROXY_ENV_KEYS.flatMap(key => [key, key.toLowerCase()]); + const originalProxyEnv = Object.fromEntries(proxyKeys.map(key => [key, process.env[key]])); + const summaryUrl = "https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuotaSummary"; + const modelsUrl = "https://daily-cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels"; + + beforeEach(() => { + for (const key of proxyKeys) delete process.env[key]; + }); + afterEach(() => { + setAntigravityAccountQuotaTransportForTests(null); + for (const key of proxyKeys) { + if (originalProxyEnv[key] === undefined) delete process.env[key]; + else process.env[key] = originalProxyEnv[key]; + } + }); test("probes each account with its own bearer and project id on the fixed Google host using retrieveUserQuotaSummary", async () => { const expires = Date.now() + 60 * 60_000; @@ -748,6 +763,95 @@ describe("google-antigravity per-account quota (#1082)", () => { expect(byId[idA]!.quota!.customWindows![0]!.resetAt).toBeDefined(); }); + for (const fallback of [false, true]) { + test(`Fake-IP ${fallback ? "models fallback" : "summary"} keeps each account bearer and project separate`, async () => { + const expires = Date.now() + 3600_000; + await saveCredential("google-antigravity", { access: "agy-first", refresh: "r1", expires, projectId: "proj-first", accountId: "agy-a", email: "a@example.com" }); + await saveCredential("google-antigravity", { access: "agy-second", refresh: "r2", expires, projectId: "proj-second", accountId: "agy-b", email: "b@example.com" }); + let plainFetchCalls = 0; + globalThis.fetch = (async () => { plainFetchCalls += 1; throw new Error("unexpected raw quota fetch"); }) as typeof fetch; + const resolved: Array<{ url: string; benchmark?: boolean; private?: boolean; mihomo?: boolean }> = []; + const posted: Array<{ url: string; auth: string | null; project: string; address: string; tls?: boolean; signal: boolean }> = []; + setAntigravityAccountQuotaTransportForTests(null); + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async (url, options) => { + const policy = typeof options === "object" ? options : undefined; + resolved.push({ url, benchmark: policy?.allowBenchmarkAddresses, private: policy?.allowPrivateNetwork, mihomo: policy?.allowMihomoIpv6FakeIp }); + if (!policy?.allowBenchmarkAddresses) throw new Error("benchmark address rejected"); + return { hostname: "daily-cloudcode-pa.googleapis.com", addresses: [{ address: "198.18.56.214", family: 4 }], privateNetwork: false }; + }, + pinnedPost: async (url, pinned, body, signal, options) => { + const auth = new Headers(options?.headers).get("authorization"); + posted.push({ url, auth, project: String(JSON.parse(body).project), address: pinned.address, tls: options?.rejectUnauthorized, signal: signal instanceof AbortSignal }); + if (url === summaryUrl && fallback) return new Response(null, { status: 404 }); + const [gem, cla]: [number, number] = auth === "Bearer agy-first" ? [0.86, 0.38] : [0.97, 0.91]; + return new Response(url === summaryUrl ? antigravitySummaryBody(gem, cla) : antigravityBody(gem, cla)); + }, + }); + const rows = await fetchProviderAccountQuotas("google-antigravity"); + const urls = fallback ? [summaryUrl, modelsUrl] : [summaryUrl]; + expect(resolved).toHaveLength(urls.length * 2); + expect(posted).toHaveLength(urls.length * 2); + for (const url of urls) { + expect(resolved.filter(row => row.url === url)).toEqual([ + { url, benchmark: true, private: false, mihomo: false }, + { url, benchmark: true, private: false, mihomo: false }, + ]); + } + for (const [auth, project] of [["Bearer agy-first", "proj-first"], ["Bearer agy-second", "proj-second"]]) { + expect(posted.filter(row => row.auth === auth)).toEqual(urls.map(url => ({ url, auth, project, address: "198.18.56.214", tls: true, signal: true }))); + } + const byId = Object.fromEntries(rows.map(row => [row.accountId, row])); + expect(byId[idFor("a@example.com")]?.quota?.customWindows?.map(w => w.percent)).toEqual(fallback ? [14, 62] : [14, 14, 62, 62]); + expect(byId[idFor("b@example.com")]?.quota?.customWindows?.map(w => w.percent)).toEqual(fallback ? [3, 9] : [3, 3, 9, 9]); + expect(plainFetchCalls).toBe(0); + }); + } + + test("NO_PROXY denial preserves an unavailable account row without sending its bearer", async () => { + await saveCredential("google-antigravity", { access: "agy-first", refresh: "r1", expires: Date.now() + 3600_000, projectId: "proj-first", accountId: "agy-a", email: "a@example.com" }); + process.env.no_proxy = "daily-cloudcode-pa.googleapis.com"; + const admitted: Array = []; + let posted = 0; + let plainFetchCalls = 0; + globalThis.fetch = (async () => { plainFetchCalls += 1; throw new Error("unexpected raw quota fetch"); }) as typeof fetch; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async (_url, options) => { + const allow = typeof options === "object" ? options?.allowBenchmarkAddresses : undefined; + admitted.push(allow); + if (!allow) throw new Error("benchmark address rejected"); + return { hostname: "daily-cloudcode-pa.googleapis.com", addresses: [{ address: "198.18.56.214", family: 4 }], privateNetwork: false }; + }, + pinnedPost: async () => { posted += 1; return new Response(antigravitySummaryBody(0.5, 0.5)); }, + }); + expect(await fetchProviderAccountQuotas("google-antigravity")).toEqual([{ accountId: idFor("a@example.com"), quota: null, unavailable: true }]); + expect(admitted).toEqual([false, false]); + expect(posted).toBe(0); + expect(plainFetchCalls).toBe(0); + }); + + for (const status of [302, 307, 308, 401, 403]) { + for (const fallback of [false, true]) { + test(`account ${fallback ? "models" : "summary"} ${status} returns unavailable without following Location`, async () => { + await saveCredential("google-antigravity", { access: "agy-first", refresh: "r1", expires: Date.now() + 3600_000, projectId: "proj-first", accountId: "agy-a", email: "a@example.com" }); + const posted: string[] = []; + let plainFetchCalls = 0; + globalThis.fetch = (async () => { plainFetchCalls += 1; throw new Error("unexpected raw quota fetch"); }) as typeof fetch; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async () => ({ hostname: "daily-cloudcode-pa.googleapis.com", addresses: [{ address: "142.250.0.1", family: 4 }], privateNetwork: false }), + pinnedPost: async url => { + posted.push(url); + if (url === summaryUrl && fallback) return new Response(null, { status: 404 }); + return new Response(null, { status, headers: { location: "https://daily-cloudcode-pa.googleapis.com/redirect-target" } }); + }, + }); + expect(await fetchProviderAccountQuotas("google-antigravity")).toEqual([{ accountId: idFor("a@example.com"), quota: null, unavailable: true }]); + expect(posted).toEqual(fallback ? [summaryUrl, modelsUrl] : [summaryUrl]); + expect(plainFetchCalls).toBe(0); + }); + } + } + test("a rejected destination never receives a bearer; the row is unavailable, not 0%", async () => { const expires = Date.now() + 60 * 60_000; await saveCredential("google-antigravity", { access: "agy-first", refresh: "r1", expires, projectId: "proj-first", accountId: "agy-a", email: "a@example.com" }); diff --git a/tests/providers/provider-quota.test.ts b/tests/providers/provider-quota.test.ts index 5a02e53f28..9c12869b04 100644 --- a/tests/providers/provider-quota.test.ts +++ b/tests/providers/provider-quota.test.ts @@ -13,6 +13,7 @@ import { saveCredential } from "../../src/oauth/store"; import { clearProviderQuotaCache, fetchProviderQuotaReports, + isCanonicalAntigravityQuotaUrl, parseOllamaCloudQuota, parseXaiCreditsResponse, QUOTA_RESPONSE_MAX_BYTES, @@ -21,7 +22,10 @@ import { setProviderQuotaBeforePublishForTests, } from "../../src/providers/quota"; import type { OcxConfig } from "../../src/types"; +import { PROXY_ENV_KEYS } from "../../src/lib/proxy-env"; import { repoPath } from "../helpers/repo-root"; +const proxyKeys = PROXY_ENV_KEYS.flatMap(key => [key, key.toLowerCase()]); +const originalProxyEnv = Object.fromEntries(proxyKeys.map(key => [key, process.env[key]])); const originalFetch = globalThis.fetch; const previousOpencodexHome = process.env.OPENCODEX_HOME; const previousCodexHome = process.env.CODEX_HOME; @@ -75,6 +79,7 @@ function testConfig(): OcxConfig { } beforeEach(() => { + for (const key of proxyKeys) delete process.env[key]; opencodexHome = mkdtempSync(join(tmpdir(), "ocx-quota-")); codexHome = mkdtempSync(join(tmpdir(), "codex-quota-")); process.env.OPENCODEX_HOME = opencodexHome; @@ -90,6 +95,10 @@ beforeEach(() => { }); afterEach(() => { + for (const key of proxyKeys) { + if (originalProxyEnv[key] === undefined) delete process.env[key]; + else process.env[key] = originalProxyEnv[key]; + } globalThis.fetch = originalFetch; clearAccountQuota(); clearProviderQuotaCache(); @@ -203,15 +212,39 @@ describe("fetchProviderQuotaReports", () => { await saveCredential("google-antigravity", { access: "agy-access-secret", refresh: "agy-refresh-secret", expires: Date.now() + 3600_000, projectId: "agy-project-secret" }); await saveCredential("kimi", { access: "kimi-access-secret", refresh: "kimi-refresh-secret", expires: Date.now() + 3600_000 }); - // The Antigravity summary probe is pinned to Google's host through the provider-outbound - // transport and never touches globalThis.fetch; without this seam the test would make a - // real network request. A 404 here exercises the fetchAvailableModels fallback below. + const seen: { url: string; authorization?: string; body?: string }[] = []; + // Both Antigravity accounting requests use the pinned transport. Keep the + // summary unavailable so this fixture still exercises the models fallback. setAntigravityAccountQuotaTransportForTests({ resolveAddresses: async () => ({ hostname: "daily-cloudcode-pa.googleapis.com", addresses: [{ address: "142.250.0.1", family: 4 }], privateNetwork: false }), - pinnedPost: async () => new Response("not found", { status: 404 }), + pinnedPost: async (url, _pinned, body, _signal, options) => { + seen.push({ url, authorization: new Headers(options?.headers).get("authorization") ?? undefined, body }); + if (url === "https://daily-cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels") { + return new Response(JSON.stringify({ + models: { + "gemini-3.6-flash-medium": { + displayName: "Gemini 3.6 Flash (Medium)", + quotaInfo: { remainingFraction: 0.64, resetTime: "2026-07-05T14:00:00Z" }, + }, + "claude-sonnet-4.6": { + displayName: "Claude Sonnet", + quotaInfoByTier: { + sonnet: { remainingFraction: 0.21, resetTime: "2026-07-05T15:00:00Z" }, + }, + }, + autocomplete: { + displayName: "Autocomplete", + quotaInfo: { remainingFraction: 0.01, resetTime: "2026-07-05T16:00:00Z" }, + }, + }, + rawProject: "agy-project-secret", + rawToken: "agy-access-secret", + }), { status: 200, headers: { "content-type": "application/json" } }); + } + return new Response("not found", { status: 404 }); + }, }); - const seen: { url: string; authorization?: string; body?: string }[] = []; globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { const url = String(input); const headers = init?.headers as Record | undefined; @@ -258,28 +291,6 @@ describe("fetchProviderQuotaReports", () => { billingCycleEnd: "2026-08-01T00:00:00.000Z", }), { status: 200, headers: { "content-type": "application/json" } }); } - if (url === "https://daily-cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels") { - return new Response(JSON.stringify({ - models: { - "gemini-3.6-flash-medium": { - displayName: "Gemini 3.6 Flash (Medium)", - quotaInfo: { remainingFraction: 0.64, resetTime: "2026-07-05T14:00:00Z" }, - }, - "claude-sonnet-4.6": { - displayName: "Claude Sonnet", - quotaInfoByTier: { - sonnet: { remainingFraction: 0.21, resetTime: "2026-07-05T15:00:00Z" }, - }, - }, - autocomplete: { - displayName: "Autocomplete", - quotaInfo: { remainingFraction: 0.01, resetTime: "2026-07-05T16:00:00Z" }, - }, - }, - rawProject: "agy-project-secret", - rawToken: "agy-access-secret", - }), { status: 200, headers: { "content-type": "application/json" } }); - } if (url === "https://api.kimi.com/coding/v1/usages") { return new Response(JSON.stringify({ user: { userId: "kimi-user-secret", businessId: "kimi-business-secret" }, @@ -3078,6 +3089,205 @@ describe("fetchProviderQuotaReports", () => { expect(posted).toEqual(["https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuotaSummary"]); }); + describe("Google Antigravity canonical quota transport (#3781)", () => { + const summaryUrl = "https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuotaSummary"; + const modelsUrl = "https://daily-cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels"; + const summaryBody = JSON.stringify({ groups: [{ displayName: "Gemini", buckets: [{ window: "5h", remainingFraction: 0.6 }] }] }); + const modelsBody = JSON.stringify({ models: { gemini: { quotaInfo: { remainingFraction: 0.75 } } } }); + const publicAddress = { hostname: "daily-cloudcode-pa.googleapis.com", addresses: [{ address: "142.250.0.1", family: 4 }], privateNetwork: false }; + let plainFetchCalls: string[]; + + function config(baseUrl = "https://daily-cloudcode-pa.googleapis.com"): OcxConfig { + return { + defaultProvider: "google-antigravity", + providers: { "google-antigravity": { adapter: "google", authMode: "oauth", baseUrl, allowPrivateNetwork: true } }, + } as OcxConfig; + } + + beforeEach(async () => { + await saveCredential("google-antigravity", { + access: "agy-canonical-access", refresh: "agy-canonical-refresh", expires: Date.now() + 3600_000, projectId: "agy-canonical-project", + }); + plainFetchCalls = []; + globalThis.fetch = (async (input) => { + plainFetchCalls.push(String(input)); + throw new Error("unexpected quota-owned raw fetch"); + }) as typeof fetch; + }); + + test("canonical proof accepts only the two exact Google accounting URLs", () => { + for (const url of [summaryUrl, modelsUrl]) { + expect(isCanonicalAntigravityQuotaUrl("google-antigravity", url)).toBe(true); + expect(isCanonicalAntigravityQuotaUrl("custom", url)).toBe(false); + for (const candidate of [ + "", "not a URL", url.replace("https:", "http:"), + url.replace(".googleapis.com", ".googleapis.com.evil.example"), + url.replace("daily-cloudcode-pa", "cloudcode-pa"), + url.replace("https://", "https://user:pass@"), + url.replace(".com/", ".com:443/"), url.replace(".com/", ".com:8443/"), + url.replace("https://", "HTTPS://"), `${url}/`, `${url}/extra`, + `${url}?token=secret`, `${url}#fragment`, ` ${url}`, + url.replace("v1internal:", "v1internal%3A"), + url.replace("v1internal:", "prefix/v1internal:"), + "https://daily-cloudcode-pa.googleapis.com/v1internal:other", + "https://198.18.0.1/v1internal:fetchAvailableModels", + "https://127.0.0.1/v1internal:fetchAvailableModels", + "https://169.254.169.254/v1internal:fetchAvailableModels", + ]) expect(isCanonicalAntigravityQuotaUrl("google-antigravity", candidate)).toBe(false); + } + }); + + for (const fallback of [false, true]) { + test(`production proof survives reset for Fake-IP ${fallback ? "fallback" : "summary"}`, async () => { + const resolved: Array<{ url: string; benchmark?: boolean; private?: boolean; mihomo?: boolean }> = []; + const posted: Array<{ url: string; address: string; tls?: boolean; auth: string | null; body: string; signal: boolean }> = []; + setAntigravityAccountQuotaTransportForTests({ isCanonicalUrl: () => false }); + setAntigravityAccountQuotaTransportForTests(null); + // Resolver/pinned-only overrides must retain the production canonical proof. + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async (url, options) => { + const policy = typeof options === "object" ? options : undefined; + resolved.push({ url, benchmark: policy?.allowBenchmarkAddresses, private: policy?.allowPrivateNetwork, mihomo: policy?.allowMihomoIpv6FakeIp }); + if (!policy?.allowBenchmarkAddresses) throw new Error("benchmark address rejected"); + return { ...publicAddress, addresses: [{ address: "198.18.56.214", family: 4 }] }; + }, + pinnedPost: async (url, pinned, body, signal, options) => { + posted.push({ url, address: pinned.address, tls: options?.rejectUnauthorized, auth: new Headers(options?.headers).get("authorization"), body, signal: signal instanceof AbortSignal }); + if (url === summaryUrl && fallback) return new Response(null, { status: 404 }); + return new Response(url === summaryUrl ? summaryBody : modelsBody); + }, + }); + const result = await fetchProviderQuotaReports(config(), true); + const urls = fallback ? [summaryUrl, modelsUrl] : [summaryUrl]; + expect(resolved).toEqual(urls.map(url => ({ url, benchmark: true, private: false, mihomo: false }))); + expect(posted).toEqual(urls.map(url => ({ url, address: "198.18.56.214", tls: true, auth: "Bearer agy-canonical-access", body: JSON.stringify({ project: "agy-canonical-project" }), signal: true }))); + expect(result.reports[0]?.source).toBe(fallback ? "google-antigravity:fetchAvailableModels" : "google-antigravity:retrieveUserQuotaSummary"); + expect(result.reports[0]?.quota.customWindows).toEqual([{ label: "Gem", percent: fallback ? 25 : 40 }]); + expect(plainFetchCalls).toEqual([]); + }); + } + + for (const baseUrl of ["https://custom.example/v1", "http://127.0.0.1:1/", "https://169.254.169.254/", "https://daily-cloudcode-pa.googleapis.com.evil.example/"]) { + test(`models fallback ignores configured destination ${baseUrl}`, async () => { + const resolved: Array<{ url: string; private?: boolean }> = []; + const posted: string[] = []; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async (url, options) => { + resolved.push({ url, private: typeof options === "object" ? options?.allowPrivateNetwork : undefined }); + return publicAddress; + }, + pinnedPost: async (url) => { + posted.push(url); + return url === summaryUrl ? new Response(null, { status: 404 }) : new Response(modelsBody); + }, + }); + const result = await fetchProviderQuotaReports(config(baseUrl), true); + expect(result.reports[0]?.quota.customWindows).toEqual([{ label: "Gem", percent: 25 }]); + expect(resolved).toEqual([{ url: summaryUrl, private: false }, { url: modelsUrl, private: false }]); + expect(posted).toEqual([summaryUrl, modelsUrl]); + expect(plainFetchCalls).toEqual([]); + }); + } + + for (const noProxy of ["daily-cloudcode-pa.googleapis.com", "*"]) { + test(`NO_PROXY ${noProxy} keeps benchmark DNS blocked`, async () => { + process.env.NO_PROXY = noProxy; + const admitted: Array = []; + let posted = 0; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async (_url, options) => { + const allow = typeof options === "object" ? options?.allowBenchmarkAddresses : undefined; + admitted.push(allow); + if (!allow) throw new Error("benchmark address rejected"); + return publicAddress; + }, + pinnedPost: async () => { posted += 1; return new Response(summaryBody); }, + }); + expect((await fetchProviderQuotaReports(config(), true)).reports).toEqual([]); + expect(admitted).toEqual([false, false]); + expect(posted).toBe(0); + expect(plainFetchCalls).toEqual([]); + }); + } + + test("resolved-address policy rejection cannot escape to raw fallback fetch", async () => { + // The real classifier's mixed-address cases live in destination-policy-resolved.test.ts; + // this checks that quota cannot bypass its rejection through a second transport. + const resolved: string[] = []; + let posted = 0; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async url => { resolved.push(url); throw new Error("provider URL resolves to metadata"); }, + pinnedPost: async () => { posted += 1; return new Response(modelsBody); }, + }); + expect((await fetchProviderQuotaReports(config("https://custom.example"), true)).reports).toEqual([]); + expect(resolved).toEqual([summaryUrl, modelsUrl]); + expect(posted).toBe(0); + expect(plainFetchCalls).toEqual([]); + }); + + for (const summary of ["{}", "invalid JSON"]) { + test(`unusable summary ${summary} falls back through the fixed models transport`, async () => { + const posted: string[] = []; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async () => publicAddress, + pinnedPost: async url => { + posted.push(url); + return new Response(url === summaryUrl ? summary : modelsBody); + }, + }); + const result = await fetchProviderQuotaReports(config(), true); + expect(result.reports[0]?.quota.customWindows).toEqual([{ label: "Gem", percent: 25 }]); + expect(posted).toEqual([summaryUrl, modelsUrl]); + expect(plainFetchCalls).toEqual([]); + }); + } + + for (const status of [200, 500]) { + test(`unusable models payload with HTTP ${status} produces no fabricated quota`, async () => { + const posted: string[] = []; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async () => publicAddress, + pinnedPost: async url => { + posted.push(url); + return url === summaryUrl ? new Response(null, { status: 404 }) : new Response("invalid JSON", { status }); + }, + }); + expect((await fetchProviderQuotaReports(config(), true)).reports).toEqual([]); + expect(posted).toEqual([summaryUrl, modelsUrl]); + expect(plainFetchCalls).toEqual([]); + }); + } + + for (const status of [302, 307, 308, 401, 403]) { + test(`summary ${status} terminates without a models request`, async () => { + const posted: string[] = []; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async () => publicAddress, + pinnedPost: async url => { posted.push(url); return new Response(null, { status, headers: { location: modelsUrl } }); }, + }); + expect((await fetchProviderQuotaReports(config(), true)).reports).toEqual([]); + expect(posted).toEqual([summaryUrl]); + expect(plainFetchCalls).toEqual([]); + }); + } + + for (const status of [302, 307, 308]) { + test(`models ${status} does not follow even a same-host redirect`, async () => { + const posted: string[] = []; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async () => publicAddress, + pinnedPost: async url => { + posted.push(url); + return url === summaryUrl ? new Response(null, { status: 404 }) : new Response(null, { status, headers: { location: summaryUrl } }); + }, + }); + expect((await fetchProviderQuotaReports(config(), true)).reports).toEqual([]); + expect(posted).toEqual([summaryUrl, modelsUrl]); + expect(plainFetchCalls).toEqual([]); + }); + } + }); + test("Ollama Cloud maps 5-hour session and weekly windows from /api/usage (legacy plan)", async () => { const seen: Array<{ url: string; authorization?: string; redirect?: RequestRedirect }> = []; globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { diff --git a/tests/server/management-api-logs-metrics.test.ts b/tests/server/management-api-logs-metrics.test.ts index 76fbd8024e..f811810809 100644 --- a/tests/server/management-api-logs-metrics.test.ts +++ b/tests/server/management-api-logs-metrics.test.ts @@ -7,6 +7,7 @@ import { usageLogPath } from "../../src/usage/log"; import { addRequestLog, clearRequestLogsForTests, + evictOldestRequestLogForBudget, getRequestLogEntries, type RequestLogEntry, } from "../../src/server/request-log"; @@ -14,6 +15,30 @@ import type { OcxConfig } from "../../src/types"; import { buildRouteDecisionTrace } from "../../src/routing/trace"; import { summarizeUsage } from "../../src/usage/summary"; import { removeTreeWithRetry } from "../helpers/remove-tree"; +import { refreshUserCostOverlays } from "../../src/usage/user-cost-overlays"; + +interface LogPollEnvelope { + logs: Array>; + cursor: string; + reset: boolean; + generatedAt: number; + timeZone: string; + total: number; +} + +async function readLogPoll(query = "", cursor?: string): Promise { + const url = new URL(`http://localhost/api/logs?${query}`); + if (cursor) url.searchParams.set("cursor", cursor); + const before = Date.now(); + const response = await handleManagementAPI(new Request(url), url, config); + expect(response?.status).toBe(200); + const body = await response!.json() as LogPollEnvelope; + expect(body.generatedAt).toBeGreaterThanOrEqual(before); + expect(body.generatedAt).toBeLessThanOrEqual(Date.now()); + expect(body.timeZone).toBe(Intl.DateTimeFormat().resolvedOptions().timeZone); + expect(typeof body.cursor).toBe("string"); + return body; +} const config = { providers: [] } as unknown as OcxConfig; @@ -294,3 +319,115 @@ describe("GET /api/logs display metrics", () => { }); }); import { ManagementRequest as Request } from "../helpers/management-auth"; + + +describe("GET /api/logs snapshot polling", () => { + beforeEach(() => clearRequestLogsForTests()); + + test("poll application equals full reads across append, nested live mutation, eviction and clear", async () => { + let accepted: Array> = []; + let cursor: string | undefined; + const check = async (reset: boolean, deltaLength: number) => { + const poll = await readLogPoll("limit=2000", cursor); + expect(poll.reset).toBe(reset); + expect(poll.logs).toHaveLength(deltaLength); + accepted = !cursor || poll.reset ? poll.logs : [...accepted, ...poll.logs]; + const snapshot = await readLogPoll("limit=2000"); + expect(accepted).toEqual(snapshot.logs); + expect(poll.total).toBe(snapshot.total); + cursor = poll.cursor; + }; + await check(false, 0); + addRequestLog(baseEntry({ requestId: "older", usage: { inputTokens: 10, outputTokens: 5 } })); + await check(false, 1); + await check(false, 0); + addRequestLog(baseEntry({ requestId: "newest", firstOutputMs: 4 })); + await check(false, 1); + getRequestLogEntries()[0]!.usage!.outputTokens = 15; + await check(true, 2); + getRequestLogEntries()[1]!.status = 500; + delete getRequestLogEntries()[1]!.firstOutputMs; + await check(true, 2); + getRequestLogEntries()[0]!.attempts = [{ + ordinal: 1, provider: "anthropic", model: "claude-3-haiku-20240307", adapter: "anthropic", + status: 200, durationMs: 50, sendCount: 1, recoveryKinds: [], usageStatus: "reported", + usage: { inputTokens: 10, outputTokens: 5 }, + }]; + await check(true, 2); + getRequestLogEntries()[0]!.attempts![0]!.usage!.outputTokens = 20; + await check(true, 2); + // The newest cursor anchor survives this real memory-budget eviction. + evictOldestRequestLogForBudget(); + await check(true, 1); + clearRequestLogsForTests(); + await check(true, 0); + await check(false, 0); + }); + + test("pagination/filter changes and shifted windows reset against the full filtered snapshot", async () => { + for (const [requestId, provider] of [["a", "anthropic"], ["b", "openai"], ["c", "anthropic"]] as const) { + addRequestLog(baseEntry({ requestId, provider })); + } + let query = "provider=anthropic&limit=1&offset=1"; + const initial = await readLogPoll(query); + expect(initial.logs.map(row => row.requestId)).toEqual(["a"]); + expect(initial.total).toBe(2); + addRequestLog(baseEntry({ requestId: "d", provider: "anthropic" })); + let poll = await readLogPoll(query, initial.cursor); + expect(poll.reset).toBe(true); + expect(poll.logs).toEqual((await readLogPoll(query)).logs); + expect(poll.logs.map(row => row.requestId)).toEqual(["c"]); + expect(poll.total).toBe(3); + for (const changed of ["provider=openai&limit=1", "tail=2&limit=1", "model=absent", "status=5xx", "conversation=absent"]) { + query = changed; + poll = await readLogPoll(query, poll.cursor); + const full = await readLogPoll(query); + expect(poll.reset).toBe(true); + expect(poll.logs).toEqual(full.logs); + expect(poll.total).toBe(full.total); + } + const filtered = await readLogPoll("provider=openai"); + addRequestLog(baseEntry({ requestId: "not-in-filter", provider: "anthropic" })); + expect(await readLogPoll("provider=openai", filtered.cursor)) + .toMatchObject({ logs: [], reset: false, cursor: filtered.cursor, total: 1 }); + }); + + test("display-time cost changes reset even when raw entries are unchanged", async () => { + const priceConfig: OcxConfig = { port: 0, defaultProvider: "fixture", providers: { fixture: { + adapter: "openai-chat", baseUrl: "https://example.test/v1", models: ["fixture-model"], + modelCosts: { "fixture-model": { input: 1, output: 2, cacheRead: 0, cacheWrite: 0 } }, + } } }; + try { + refreshUserCostOverlays(priceConfig); + addRequestLog(baseEntry({ provider: "fixture", model: "fixture-model", usage: { inputTokens: 100, outputTokens: 10 } })); + const initial = await readLogPoll(); + const rawBefore = structuredClone(getRequestLogEntries()); + priceConfig.providers.fixture!.modelCosts!["fixture-model"]!.output = 20; + refreshUserCostOverlays(priceConfig); + const changed = await readLogPoll("", initial.cursor); + expect(changed.reset).toBe(true); + expect(changed.logs[0]!.displayMetrics).not.toEqual(initial.logs[0]!.displayMetrics); + expect(changed.logs).toEqual((await readLogPoll()).logs); + expect(getRequestLogEntries()).toEqual(rawBefore); + } finally { + refreshUserCostOverlays(config); + } + }); + + test("legacy cursors reset; invalid cursors return generic errors without reflecting input", async () => { + addRequestLog(baseEntry({ requestId: "private-row" })); + const legacy = Buffer.from(JSON.stringify({ v: 1, t: 1, id: "private-row" })).toString("base64url"); + const poll = await readLogPoll("provider=anthropic", legacy); + expect(poll.reset).toBe(true); + const payload = Buffer.from(poll.cursor, "base64url").toString(); + expect(payload).not.toContain("private-row"); + expect(payload).not.toContain("anthropic"); + for (const cursor of ["", "private-invalid-cursor", "x".repeat(513)]) { + const url = new URL("http://localhost/api/logs"); + url.searchParams.set("cursor", cursor); + const response = await handleManagementAPI(new Request(url), url, config); + expect(response?.status).toBe(400); + expect(await response!.json()).toEqual({ error: { code: "invalid_cursor", message: "invalid cursor" } }); + } + }); +}); diff --git a/tests/server/server-management-auth.test.ts b/tests/server/server-management-auth.test.ts index c1bc56f70a..340ad70bd3 100644 --- a/tests/server/server-management-auth.test.ts +++ b/tests/server/server-management-auth.test.ts @@ -1652,3 +1652,63 @@ describe("codex app-server restart routes ride the management gate", () => { } }); }); + + +test("log cursors remain behind management admission and origin gates", async () => { + const config = remoteConfig(); + saveConfig(config); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const server = startServer(0, { managementAuthState: state }); + const origin = server.url.origin; + const token = "ocx_session_log_cursor_test"; + state.sessions.set(token, { + serverOrigin: origin, browserOrigin: origin, csrfToken: "csrf-log-test", + expiresAt: Date.now() + 60_000, issuance: "loopback", + }); + const adminHeaders = { "x-opencodex-api-key": "admin-secret" }; + const acceptedHeaders: HeadersInit[] = [adminHeaders, { + Origin: origin, "x-opencodex-api-key": token, "x-opencodex-gui-origin": origin, + }]; + try { + const initial = await fetch(new URL("/api/logs", server.url), { headers: adminHeaders }); + expect(initial.status).toBe(200); + const body = await initial.json() as { cursor: string }; + expect(typeof body.cursor).toBe("string"); + for (const suffix of ["", `?cursor=${body.cursor}`, "?cursor=malformed"]) { + const url = new URL(`/api/logs${suffix}`, server.url); + for (const credential of [undefined, "data-secret", "wrong-admin"]) { + const response = await fetch(url, { headers: credential ? { "x-opencodex-api-key": credential } : {} }); + expect(response.status).toBe(401); + expect(await response.json()).toEqual({ error: "opencodex admin token required" }); + } + const foreign = await fetch(url, { headers: { ...adminHeaders, Origin: "https://attacker.test" } }); + expect(foreign.status).toBe(403); + await foreign.text(); + for (const headers of acceptedHeaders) { + const allowed = await fetch(url, { headers }); + expect(allowed.status).toBe(suffix.includes("malformed") ? 400 : 200); + await allowed.text(); + } + } + } finally { + await server.stop(true); + } +}, SERVER_BUDGET_MS); + +test("unavailable management authority rejects log cursors before parsing", async () => { + saveConfig(remoteConfig()); + const server = startServer(0, { managementAuthState: { available: false, reason: "fixture unavailable" } }); + try { + const legacy = Buffer.from(JSON.stringify({ v: 1, t: 1, id: "fixture" })).toString("base64url"); + for (const suffix of ["", `?cursor=${legacy}`, "?cursor=malformed"]) { + const response = await fetch(new URL(`/api/logs${suffix}`, server.url), { + headers: { "x-opencodex-api-key": "admin-secret" }, + }); + expect(response.status).toBe(503); + expect(await response.json()).toMatchObject({ error: "management API unavailable" }); + } + } finally { + await server.stop(true); + } +}, SERVER_BUDGET_MS); diff --git a/tests/usage/request-log.test.ts b/tests/usage/request-log.test.ts index 02e26e764b..1a3b4e3264 100644 --- a/tests/usage/request-log.test.ts +++ b/tests/usage/request-log.test.ts @@ -38,6 +38,7 @@ import { mkdtempSync} from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { removeTreeWithRetry } from "../helpers/remove-tree"; +import { decodeRequestLogCursor, selectRequestLogPoll } from "../../src/server/request-log-cursor"; async function* replayAdapterEvents(events: AdapterEvent[]): AsyncGenerator { for (const event of events) yield event; @@ -1808,3 +1809,97 @@ describe("request log restart hydrate", () => { } }); }); + + +describe("request log snapshot cursor", () => { + const epoch = "a".repeat(32); + const query = new URLSearchParams("limit=2000"); + const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString("base64url"); + + test("codec bounds and canonical encoding reject malformed or type-confused input", () => { + const poll = selectRequestLogPoll([], query, null, epoch); + const payload = JSON.parse(Buffer.from(poll.cursor, "base64url").toString()); + expect(decodeRequestLogCursor(poll.cursor)).toEqual(payload); + for (const raw of ["", "!", "a".repeat(513), `${poll.cursor}=`, ` ${poll.cursor}`, + encode(null), encode([]), encode({ ...payload, v: 3 }), encode({ ...payload, n: -1 }), + encode({ ...payload, n: 2001 }), encode({ ...payload, n: 0.5 }), encode({ ...payload, n: "0" }), + encode({ ...payload, h: "x".repeat(64) }), encode({ ...payload, q: null }), + encode({ ...payload, e: "short" }), encode({ ...payload, extra: true }), + encode({ v: 1, t: -1, id: "row" }), encode({ v: 1, t: 1, id: "" }), + encode({ v: 1, t: "1", id: "row" }), encode({ v: 1, t: 1, id: "x".repeat(257) })]) { + expect(decodeRequestLogCursor(raw)).toBeNull(); + } + const legacy = decodeRequestLogCursor(encode({ v: 1, t: 1, id: "row" })); + expect(legacy).toEqual({ v: 1, t: 1, id: "row" }); + expect(selectRequestLogPoll([], query, legacy, epoch).reset).toBe(true); + }); + + test("stable empty and populated snapshots produce empty deltas, appends preserve repeated IDs", () => { + const empty = selectRequestLogPoll([], query, null, epoch); + expect(empty.reset).toBe(false); + expect(selectRequestLogPoll([], query, decodeRequestLogCursor(empty.cursor), epoch)).toEqual(empty); + const rows = [log({ requestId: "same" })]; + const first = selectRequestLogPoll(rows, query, decodeRequestLogCursor(empty.cursor), epoch); + expect(first.logs).toEqual(rows); + const cursor = decodeRequestLogCursor(first.cursor); + expect(selectRequestLogPoll(rows, query, cursor, epoch)).toEqual({ ...first, logs: [] }); + rows.push(log({ requestId: "same", status: 500 })); + expect(selectRequestLogPoll(rows, query, cursor, epoch)).toMatchObject({ logs: [rows[1]], reset: false }); + }); + + test("in-place older/newest/nested changes, field removal, reorder and eviction reset the whole window", () => { + const original = [ + log({ requestId: "older", usage: { inputTokens: 10, outputTokens: 5 }, firstOutputMs: 3 }), + log({ requestId: "newest" }), + ]; + const cursor = decodeRequestLogCursor(selectRequestLogPoll(original, query, null, epoch).cursor); + const mutations: Array<(rows: RequestLogEntry[]) => void> = [ + rows => { rows[0]!.status = 500; }, + rows => { rows[1]!.durationMs = 22; }, + rows => { rows[0]!.usage!.outputTokens = 6; }, + rows => { delete rows[0]!.firstOutputMs; }, + rows => { rows[0] = log({ requestId: "replacement" }); }, + rows => { rows.reverse(); }, + rows => { rows.shift(); }, + rows => { rows.length = 0; }, + ]; + for (const mutate of mutations) { + const rows = structuredClone(original); + mutate(rows); + expect(selectRequestLogPoll(rows, query, cursor, epoch)).toMatchObject({ logs: rows, reset: true }); + } + // Same hydrated IDs and values do not make an old process cursor valid. + expect(selectRequestLogPoll(original, query, cursor, "b".repeat(32))) + .toMatchObject({ logs: original, reset: true }); + }); + + test("query identity ignores cursor and parameter ordering but binds filters and pagination", () => { + const rows = [log({ requestId: "private-row", conversationId: "private-conversation" })]; + const first = selectRequestLogPoll(rows, new URLSearchParams("provider=private-provider&limit=1"), null, epoch); + const cursor = decodeRequestLogCursor(first.cursor); + const raw = Buffer.from(first.cursor, "base64url").toString(); + for (const value of ["private-row", "private-conversation", "private-provider"]) expect(raw).not.toContain(value); + expect(selectRequestLogPoll(rows, new URLSearchParams(`limit=1&cursor=${first.cursor}&provider=private-provider`), cursor, epoch).logs) + .toEqual([]); + for (const changed of ["provider=other&limit=1", "provider=private-provider&limit=2", "provider=private-provider&limit=1&offset=1"]) { + expect(selectRequestLogPoll(rows, new URLSearchParams(changed), cursor, epoch).reset).toBe(true); + } + const duplicated = selectRequestLogPoll(rows, new URLSearchParams("provider=a&provider=b"), null, epoch); + expect(selectRequestLogPoll(rows, new URLSearchParams("provider=b&provider=a"), decodeRequestLogCursor(duplicated.cursor), epoch).reset) + .toBe(true); + }); + + test("a full-window rollover resets; a stale fingerprint cannot suppress current rows", () => { + const rows = Array.from({ length: 2000 }, (_, index) => log({ requestId: `row-${index}`, timestamp: 2000 - index })); + const initial = selectRequestLogPoll(rows, query, null, epoch); + const cursor = decodeRequestLogCursor(initial.cursor); + expect(cursor).toMatchObject({ v: 2, n: 2000 }); + rows.shift(); + rows.push(log({ requestId: "new", timestamp: 0 })); + expect(selectRequestLogPoll(rows, query, cursor, epoch)).toMatchObject({ logs: rows, reset: true }); + const payload = JSON.parse(Buffer.from(initial.cursor, "base64url").toString()); + const stale = decodeRequestLogCursor(encode({ ...payload, h: "0".repeat(64) })); + expect(stale).not.toBeNull(); + expect(selectRequestLogPoll(rows, query, stale, epoch)).toMatchObject({ logs: rows, reset: true }); + }); +});