Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
Merge origin/dev into fix/985-admin-rate-limit-ux
Alfredo Lopez213d9f852a07
diff --cc control-api/package-lock.jsonindex 6094a5ab6,7d6bad938..17ea1b018--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@@ -1,12 -1,12 +1,12 @@@{"name": "clerum-control-api",- "version": "0.1.719",- "version": "0.1.711",++ "version": "0.1.720","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.719",- "version": "0.1.711",++ "version": "0.1.720","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --cc control-api/package.jsonindex c12af00a3,0882b95ab..a3cbb2738--- a/control-api/package.json+++ b/control-api/package.json@@@ -1,6 -1,6 +1,6 @@@{"name": "clerum-control-api",- "version": "0.1.719",- "version": "0.1.711",++ "version": "0.1.720","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --cc control-ui/app/hosts/[name]/page.tsxindex 4f67825c5,6232e93b9..fb40cb4e7--- a/control-ui/app/hosts/[name]/page.tsx+++ b/control-ui/app/hosts/[name]/page.tsx@@@ -212,12 -201,6 +211,11 @@@ export default function HostDetailsPageconst [initialLoading, setInitialLoading] = useState(true)const [editingModel, setEditingModel] = useState(false)+ const subscriptionCapabilities = useSubscriptionCapabilities({ enabled: editingModel })+ const codexEnabled =+ subscriptionCapabilities.capabilities?.providers['codex-subscription'].enabled ?? false+ const grokEnabled =+ subscriptionCapabilities.capabilities?.providers['grok-subscription'].enabled ?? false- const [llmSecretModalOpen, setLlmSecretModalOpen] = useState(false)const [showDeleteAgentConfirm, setShowDeleteAgentConfirm] = useState(false)const [deletingAgent, setDeletingAgent] = useState(false)const [deleteAgentDialogError, setDeleteAgentDialogError] = useState('')@@@ -1017,12 -922,33 +998,32 @@@return}setSecretRefDraft(parsed.name)- setConnectionRefDraft(CODEX_UNASSIGNED_CONNECTION_KEY)- setGrantCatalog(null)- setGrantCatalogError('')- if (isOauthBrokerProvider(providerDraft)) {- setProviderDraft('openai')- setModelNameDraft(resolveDefaultModel('openai', getModelOptions(allowedCatalog, 'openai')))+ // Secret → provider sync (mirrors HostWizard.handleExistingSecretChange and+ // an explicit provider switch): when the picked secret carries provider+ // credentials (the same metadata that renders the option's provider icons),+ // align the primary provider — and its default model — with it so the+ // pair stays coherent. A secret that already serves the current provider+ // keeps it (multi-provider secrets never clobber a coherent choice), and a+ // provider-less/custom secret never touches the operator's provider.+ const pickedProviders = (+ llmSecretOptions.find(option => option.value === parsed.name)?.providers ?? []+ )+ .map(provider => normalizeProvider(provider.id))+ .filter(provider => !isOauthBrokerProvider(provider))+ if (pickedProviders.length > 0) {+ setConnectionRefDraft(CODEX_UNASSIGNED_CONNECTION_KEY)- setCodexModels([])- setGrokModels([])++ setGrantCatalog(null)+ setGrantCatalogError('')+ if (!pickedProviders.includes(providerDraft)) {+ const nextProvider = pickedProviders[0]+ setProviderDraft(nextProvider)+ setModelNameDraft(+ resolveDefaultModel(+ nextProvider,+ constrainModelOptions(allowedCatalog, allowedModelsDraft, nextProvider)+ )+ )+ }}}@@@ -1528,31 -1467,10 +1542,31 @@@}onDismiss={() => void cancelModelEdit()}open={editingModel}- size="default"- title="Edit model configuration"+ title="Edit model & credentials"><div className="cu-form-stack cu-form-stack--wide">+ <SubscriptionCapabilityNotice state={subscriptionCapabilities} />+ {subscriptionInventoryLoading ? (+ <p className="cu-muted" role="status">+ Loading subscription options…+ </p>+ ) : null}+ {subscriptionInventoryError || grantCatalogError ? (+ <div className="cu-banner cu-banner--error" role="alert">+ <span>{subscriptionInventoryError || grantCatalogError}</span>+ <button+ type="button"+ className="cu-btn cu-btn--ghost cu-btn--sm"+ onClick={() => {+ inventoryAutoRetryUsedRef.current = false+ setInventoryRetryNonce(value => value + 1)+ }}+ >+ Retry+ </button>+ </div>+ ) : null}+ {renderModelCredentialFields('model-secret-editor', 'editor')}<LlmProviderConfigprovider={providerDraft}model={modelNameDraft}diff --cc control-ui/components/__tests__/HostDetailsPage.rateLimit.test.tsxindex dab6c2ea8,000000000..915e31db7mode 100644,000000..100644--- a/control-ui/components/__tests__/HostDetailsPage.rateLimit.test.tsx+++ b/control-ui/components/__tests__/HostDetailsPage.rateLimit.test.tsx@@@ -1,477 -1,0 +1,477 @@@+import React from 'react'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'+import HostDetailsPage from '../../app/hosts/[name]/page'+import * as api from '../../lib/api'+import { __resetReadRequestCacheForTests } from '../../lib/readRequestCache'+import { buildContextResource } from '../../test/fixtures/contextResource'+import { ToastProvider } from '../Toast'++const navigation = vi.hoisted(() => ({+ replace: vi.fn(),+ push: vi.fn(),+ tab: undefined as string | undefined,+ name: 'quota-agent',+}))++vi.mock('next/navigation', () => ({+ useParams: () => ({ name: navigation.name, tab: navigation.tab }),+ usePathname: () => `/agents/${navigation.name}`,+ useRouter: () => navigation,+ useSearchParams: () => new URLSearchParams(),+}))+vi.mock('next/link', () => ({+ default: ({ href, children, ...props }: React.AnchorHTMLAttributes<HTMLAnchorElement>) => (+ <a+ {...props}+ href={href}+ onClick={event => {+ event.preventDefault()+ navigation.tab = String(href).split('/').at(-1)+ navigation.replace(href)+ }}+ >+ {children}+ </a>+ ),+}))+vi.mock('../AuthContext', () => ({ useAuth: () => ({ logout: vi.fn() }) }))+vi.mock('../Sidebar', () => ({ Sidebar: () => <aside aria-label="Sidebar" /> }))+// These sections have their own identity/access suites. This regression owns+// the detail shell, its real overview/model editor, and physical metadata GETs.+vi.mock('../HostIdentityTab', () => ({+ HostIdentityTab: ({ hostName }: { hostName: string }) => (+ <section aria-label="Identity editor">{hostName}</section>+ ),+}))+vi.mock('../HostAccessTab', () => ({+ HostAccessTab: ({ hostName }: { hostName: string }) => (+ <section aria-label="Access editor">{hostName}</section>+ ),+}))+vi.mock('../../lib/api', async importOriginal => {+ const actual = await importOriginal<typeof import('../../lib/api')>()+ return {+ ...actual,+ getHostDetailBundle: vi.fn(),+ getHost: vi.fn(),+ getLlmModels: vi.fn(),+ apiSend: vi.fn(),+ }+})++const host = {+ metadata: {+ name: 'quota-agent',+ resourceVersion: 'host-rv-1',+ creationTimestamp: '2026-01-01T12:00:00Z',+ },+ spec: {+ host: 'Operations agent',+ description: 'Loaded agent state survives optional metadata throttling.',+ contextRef: 'quota-context',+ model: { provider: 'openai', name: 'gpt-4o' },+ secretRef: 'operator-credential',+ },+ status: { lifecycle: { state: 'Active' } },+}++function json(value: unknown): Response {+ return new Response(JSON.stringify(value), {+ status: 200,+ headers: { 'content-type': 'application/json' },+ })+}++function throttle(): Response {+ return new Response(+ JSON.stringify({+ error: 'Too Many Requests',+ code: 'rate_limited',+ message: 'This request limit has been reached. Try again in 12 seconds.',+ retryAfterSeconds: 12,+ }),+ {+ status: 429,+ statusText: '',+ headers: { 'content-type': 'application/json', 'retry-after': '12' },+ }+ )+}++let rerenderPage: () => void+function renderPage() {+ const element = (+ <ToastProvider>+ <HostDetailsPage />+ </ToastProvider>+ )+ const result = render(element)+ // Reflect the route segment selected through the visible Next Link in this+ // component test; the real router journey is owned by the E2E lane.+ rerenderPage = () =>+ result.rerender(+ <ToastProvider>+ <HostDetailsPage />+ </ToastProvider>+ )+ return result+}++function navigate(label: string) {+ fireEvent.click(screen.getByRole('tab', { name: label }))+ rerenderPage()+}++function assertLoadedOverview() {+ expect(screen.getByRole('heading', { name: 'Agent: Operations agent' })).toBeInTheDocument()+ expect(+ within(screen.getByRole('region', { name: 'Agent identity' })).getByText('Active')+ ).toBeInTheDocument()+ expect(screen.getByText(host.spec.description)).toBeInTheDocument()+ expect(+ within(screen.getByRole('region', { name: 'Connectors' })).getByText('documents-connector')+ ).toBeInTheDocument()+ const access = screen.getByRole('region', { name: 'Access summary' })+ expect(within(access).getByText('Test operator')).toBeInTheDocument()+ expect(within(access).getByText('Operations team')).toBeInTheDocument()+}++describe('HostDetailsPage optional subscription throttling', () => {+ let fetchMock: ReturnType<typeof vi.fn>++ beforeEach(() => {+ navigation.tab = undefined+ navigation.name = 'quota-agent'+ __resetReadRequestCacheForTests()+ api.setControlUIReadPrincipal('unit-test-admin', 'admin')+ vi.clearAllMocks()+ vi.mocked(api.getHostDetailBundle).mockResolvedValue({+ host,+ contexts: [+ buildContextResource({+ metadata: { name: 'quota-context', resourceVersion: 'context-rv-1' },+ spec: { contextId: 'quota-context', mcpServers: ['documents-connector'] },+ }),+ ],+ secrets: [{ name: 'operator-credential', keys: ['openai-api-key'] }],+ users: [],+ teams: [],+ agentUsers: [+ { id: 'operator', email: '[email protected]', displayName: 'Test operator' },+ ],+ agentTeams: [{ id: 'operations', name: 'Operations team' }],+ } as never)+ vi.mocked(api.getHost).mockResolvedValue(host)+ vi.mocked(api.getLlmModels).mockResolvedValue({+ rows: ['gpt-4o', 'gpt-4.1'].map((model, index) => ({+ id: `model-${index}`,+ provider: 'openai',+ model,+ enabled: true,+ stale: false,+ vendor: 'OpenAI',+ display_name: null,+ context_window_tokens: null,+ created_at: '',+ updated_at: '',+ })),+ })+ fetchMock = vi.fn((input: RequestInfo | URL) => {+ if (String(input).endsWith('/capabilities'))+ return Promise.resolve(+ json({+ providers: {+ 'codex-subscription': { enabled: true },+ 'grok-subscription': { enabled: true },+ },+ })+ )+ return Promise.resolve(json({ connections: [] }))+ })+ vi.stubGlobal('fetch', fetchMock)+ })++ afterEach(() => {+ cleanup()+ __resetReadRequestCacheForTests()+ vi.unstubAllGlobals()+ vi.restoreAllMocks()+ vi.useRealTimers()+ })++ it('makes no subscription GET for ordinary sections and reuses editor metadata across reopening', async () => {+ renderPage()+ await screen.findByRole('heading', { name: 'Agent: Operations agent' })+ assertLoadedOverview()+ navigate('Identity')+ expect(screen.getByRole('region', { name: 'Identity editor' })).toHaveTextContent('quota-agent')+ navigate('Access')+ expect(screen.getByRole('region', { name: 'Access editor' })).toHaveTextContent('quota-agent')+ navigate('Connectors')+ expect(screen.getByText('documents-connector')).toBeInTheDocument()+ navigate('Models & creds')+ expect(screen.getByRole('region', { name: 'LLM configuration summary' })).toHaveTextContent(+ 'gpt-4o'+ )+ expect(fetchMock).not.toHaveBeenCalled()++ fireEvent.click(screen.getByRole('button', { name: 'Edit' }))+ await waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(3))+ await waitFor(() => expect(screen.queryByText('Loading subscription options…')).toBeNull())+ expect(new Set(fetchMock.mock.calls.map(call => String(call[0]))).size).toBe(3)+ fireEvent.click(within(screen.getByRole('dialog')).getByRole('button', { name: 'Cancel' }))+ await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())+ fireEvent.click(screen.getByRole('button', { name: 'Edit' }))+ await waitFor(() => expect(screen.queryByText('Loading subscription options…')).toBeNull())+ expect(fetchMock).toHaveBeenCalledTimes(3)+ })++ it('keeps the agent, its summaries and an unsaved model draft when the real API throttle shape rejects catalogs', async () => {+ fetchMock.mockImplementation((input: RequestInfo | URL) => {+ if (String(input).endsWith('/capabilities'))+ return Promise.resolve(+ json({+ providers: {+ 'codex-subscription': { enabled: true },+ 'grok-subscription': { enabled: true },+ },+ })+ )+ return Promise.resolve(throttle())+ })+ renderPage()+ await screen.findByRole('heading', { name: 'Agent: Operations agent' })+ assertLoadedOverview()+ navigate('Models & creds')+ fireEvent.click(screen.getByRole('button', { name: 'Edit' }))- const dialog = screen.getByRole('dialog', { name: 'Edit model configuration' })++ const dialog = screen.getByRole('dialog', { name: 'Edit model & credentials' })+ const alert = await within(dialog).findByRole('alert')+ expect(alert).toHaveTextContent('Try again in 12 seconds.')+ expect(alert).not.toHaveTextContent(/^429$/)+ expect(screen.getByRole('heading', { name: 'Agent: Operations agent' })).toBeInTheDocument()++ const model = within(dialog).getByLabelText(/model/i, { selector: '#llm-primary-model' })+ fireEvent.click(model)+ fireEvent.click(screen.getByRole('option', { name: 'gpt-4.1' }))+ expect(model).toHaveTextContent('gpt-4.1')+ const requestsBeforeRetry = fetchMock.mock.calls.length+ fireEvent.click(within(alert).getByRole('button', { name: 'Retry' }))+ await act(async () => {+ await Promise.resolve()+ await Promise.resolve()+ })+ expect(fetchMock).toHaveBeenCalledTimes(requestsBeforeRetry)+ expect(model).toHaveTextContent('gpt-4.1')+ expect(api.apiSend).not.toHaveBeenCalled()++ fireEvent.click(within(dialog).getByRole('button', { name: 'Cancel' }))+ await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())+ navigate('Overview')+ assertLoadedOverview()+ navigate('Connectors')+ expect(screen.getByText('documents-connector')).toBeInTheDocument()+ navigate('Access')+ expect(screen.getByRole('region', { name: 'Access editor' })).toHaveTextContent('quota-agent')+ navigate('Identity')+ expect(screen.getByRole('region', { name: 'Identity editor' })).toHaveTextContent('quota-agent')+ expect(api.apiSend).not.toHaveBeenCalled()+ })++ it('clears the error once the background recovery rereads the inventory, without a click or another read', async () => {+ vi.useFakeTimers()+ // The server's shared subscription-read quota: two reads per twelve-second window.+ const windowMs = 12_000+ const startedAt = Date.now()+ const reads: Array<{ url: string; window: number; status: number }> = []+ fetchMock.mockImplementation((input: RequestInfo | URL) => {+ const url = String(input)+ const window = Math.floor((Date.now() - startedAt) / windowMs)+ const inWindow = reads.filter(read => read.window === window).length+ const response =+ inWindow >= 2+ ? throttle()+ : url.endsWith('/capabilities')+ ? json({+ providers: {+ 'codex-subscription': { enabled: true },+ 'grok-subscription': { enabled: true },+ },+ })+ : json({ connections: [] })+ reads.push({ url, window, status: response.status })+ return Promise.resolve(response)+ })+ const flush = () =>+ act(async () => {+ await vi.advanceTimersByTimeAsync(0)+ })+ renderPage()+ await flush()+ navigate('Models & creds')+ fireEvent.click(screen.getByRole('button', { name: 'Edit' }))+ await flush()- const dialog = screen.getByRole('dialog', { name: 'Edit model configuration' })++ const dialog = screen.getByRole('dialog', { name: 'Edit model & credentials' })+ const alert = within(dialog).getByRole('alert')+ expect(alert).toHaveTextContent('Try again in 12 seconds.')+ expect(reads.map(read => read.status)).toEqual([200, 200, 429])++ // The shared recovery reads the denied inventory once the deadline passes,+ // and the editor shows that result without a click or a read of its own.+ await act(async () => {+ await vi.advanceTimersByTimeAsync(windowMs)+ })+ await flush()+ const afterDeadline = reads.filter(read => read.window >= 1)+ expect(afterDeadline).toHaveLength(1)+ expect(afterDeadline[0]?.status).toBe(200)+ expect(within(dialog).queryByRole('alert')).toBeNull()+ expect(screen.queryByText('Loading subscription options…')).toBeNull()+ expect(api.apiSend).not.toHaveBeenCalled()+ })++ it.each(['codex-subscription', 'grok-subscription'] as const)(+ 'does not confirm retained %s models under a different connection that throttles, then recovers only that binding',+ async provider => {+ vi.useFakeTimers()+ const modelA = provider === 'codex-subscription' ? 'gpt-5.1' : 'grok-4.6'+ const modelB = provider === 'codex-subscription' ? 'gpt-5.4' : 'grok-4.7'+ vi.mocked(api.getHostDetailBundle).mockImplementation(+ async name =>+ ({+ host: {+ ...host,+ metadata: { ...host.metadata, name },+ spec: {+ ...host.spec,+ secretRef: undefined,+ model: {+ provider,+ name: modelA,+ connectionRef: name === 'quota-agent' ? 'connection-a' : 'connection-b',+ },+ },+ },+ contexts: [+ buildContextResource({+ metadata: { name: 'quota-context' },+ spec: { contextId: 'quota-context', mcpServers: ['documents-connector'] },+ }),+ ],+ secrets: [],+ users: [],+ teams: [],+ agentUsers: [],+ agentTeams: [],+ }) as never+ )+ vi.mocked(api.getLlmModels).mockResolvedValue({+ rows: [modelA, modelB].map((model, index) => ({+ id: `binding-model-${index}`,+ provider,+ model,+ enabled: true,+ stale: false,+ vendor: provider === 'codex-subscription' ? 'OpenAI' : 'xAI',+ display_name: null,+ context_window_tokens: null,+ created_at: '',+ updated_at: '',+ })),+ })+ let bReads = 0+ fetchMock.mockImplementation((input: RequestInfo | URL) => {+ const url = String(input)+ if (url.endsWith('/capabilities'))+ return Promise.resolve(+ json({+ providers: {+ 'codex-subscription': { enabled: true },+ 'grok-subscription': { enabled: true },+ },+ })+ )+ if (url.endsWith('/connection-a/models'))+ return Promise.resolve(json({ models: [{ model: modelA, enabled: true, stale: false }] }))+ if (url.endsWith('/connection-b/models')) {+ bReads += 1+ return Promise.resolve(+ bReads === 1+ ? throttle()+ : json({ models: [{ model: modelB, enabled: true, stale: false }] })+ )+ }+ return Promise.resolve(+ json({+ connections: ['a', 'b'].map(suffix => ({+ connectionKey: `connection-${suffix}`,+ displayName: `Connection ${suffix}`,+ status: 'connected',+ credentialRevision: 1,+ catalogRevision: 1,+ catalogStatus: 'ready',+ defaultModel: suffix === 'a' ? modelA : modelB,+ })),+ })+ )+ })+ const flush = () =>+ act(async () => {+ await vi.advanceTimersByTimeAsync(0)+ })+ renderPage()+ await flush()+ navigate('Models & creds')+ fireEvent.click(screen.getByRole('button', { name: 'Edit' }))+ await flush()- let dialog = screen.getByRole('dialog', { name: 'Edit model configuration' })++ let dialog = screen.getByRole('dialog', { name: 'Edit model & credentials' })+ expect(within(dialog).getByRole('button', { name: 'Save' })).toBeEnabled()+ fireEvent.click(+ within(dialog).getByLabelText('Current model', { selector: '#llm-primary-model' })+ )+ expect(screen.getByRole('option', { name: new RegExp(modelA) })).not.toHaveTextContent(+ 'out of allowlist'+ )+ fireEvent.click(+ within(dialog).getByLabelText('Current model', { selector: '#llm-primary-model' })+ )++ // Reuse the page under a new route/detail producer input. Its real HTTP+ // catalog client must fence A's loaded result as the B binding is hydrated.+ navigation.name = 'quota-agent-b'+ rerenderPage()+ await flush()- dialog = screen.getByRole('dialog', { name: 'Edit model configuration' })++ dialog = screen.getByRole('dialog', { name: 'Edit model & credentials' })+ expect(screen.getByRole('heading', { name: 'Agent: Operations agent' })).toBeInTheDocument()+ expect(within(dialog).getByRole('alert')).toHaveTextContent('Try again in 12 seconds.')+ expect(+ within(dialog).getByLabelText('Current model', { selector: '#llm-primary-model' })+ ).toHaveTextContent(modelA)+ expect(within(dialog).getByRole('button', { name: 'Save' })).toBeDisabled()+ fireEvent.click(+ within(dialog).getByLabelText('Current model', { selector: '#llm-primary-model' })+ )+ expect(screen.getByRole('option', { name: new RegExp(modelA) })).toHaveTextContent(+ 'out of allowlist'+ )+ expect(screen.queryByRole('option', { name: new RegExp(modelB) })).toBeNull()+ fireEvent.click(+ within(dialog).getByLabelText('Current model', { selector: '#llm-primary-model' })+ )+ await act(async () => {+ await vi.advanceTimersByTimeAsync(12_000)+ })+ fireEvent.click(within(dialog).getByRole('button', { name: 'Retry' }))+ await flush()+ expect(within(dialog).queryByRole('alert')).toBeNull()+ fireEvent.click(+ within(dialog).getByLabelText('Current model', { selector: '#llm-primary-model' })+ )+ expect(screen.getByRole('option', { name: new RegExp(modelB) })).not.toHaveTextContent(+ 'out of allowlist'+ )+ expect(screen.queryByRole('option', { name: new RegExp(modelA) })).toBeNull()+ expect(api.apiSend).not.toHaveBeenCalled()+ }+ )+})diff --cc control-ui/e2e/qa-recorder-admin-rate-limit-recovery.spec.tsindex 34a73300d,000000000..486d4e874mode 100644,000000..100644--- a/control-ui/e2e/qa-recorder-admin-rate-limit-recovery.spec.ts+++ b/control-ui/e2e/qa-recorder-admin-rate-limit-recovery.spec.ts@@@ -1,802 -1,0 +1,802 @@@+import {+ type Locator,+ type Page,+ type Request,+ type Response,+ expect,+ test,+} from '@playwright/test'+import { CONTROL_ROUTES } from '../app/constants/routes'+import {+ CONTROL_API_URL,+ CONTROL_UI_URL,+ adminCredentials,+ assertAllowedTarget,+ directApi,+ loginThroughUi,+ requireRecorderConfirm,+ requiredValue,+ screenshotAndLog,+ uniqueE2EName,+} from './qa-recorder-helpers'++// Task 11 source contract: run each scenario separately in the same owned,+// disposable local lane. The operator configures the real API before running:+// --grep 'normal navigation': CONTROL_API_ADMIN_SUBSCRIPTION_READ_PER_MIN >= 150+// --grep 'real 429': CONTROL_API_ADMIN_SUBSCRIPTION_READ_PER_MIN = 2+// The existing API window is a fixed calendar minute, not a configurable knob.+// Both CONTROL_API_CODEX_SUBSCRIPTION_ENABLED and+// CONTROL_API_GROK_SUBSCRIPTION_ENABLED must be true.+// Require QA_RECORDER_CONFIRM_MUTATIONS=1, QA_RECORDER_CONFIRM_OWNED_RUNTIME=1,+// explicit loopback CONTROL_UI_URL/CONTROL_API_URL, normal recorder admin login,+// E2E_AGENT_SECRET_NAME (an existing local Secret containing an OpenAI key), and+// two distinct enabled OpenAI allowlist rows: E2E_AGENT_MODEL_NAME and+// E2E_AGENT_DRAFT_MODEL_NAME. No provider execution or catalog write is needed.+// Set QA_RECORDER_ROOT to the primary checkout's ignored .local-notes/infra/runs+// directory. These declarations do not establish deployment or runtime proof.+// Each test signs in, creates its own agent through the wizard, signs out and+// signs in again through the visible UI, then selects that agent from the list.+// Only observed successful creates enter the cleanup ledger. Direct API calls+// are confined to teardown of those exact resources, never setup or assertions.++const CAPABILITIES = '/api/v1/admin/llm/providers/capabilities'+// Required fields append " *" to their accessible name, and the catalog tab+// appends its model count.+const PROVIDER_FIELD = /^Provider(?: \*)?$/+const MODEL_FIELD = /^Model(?: \*)?$/+const NAME_FIELD = /^Name(?: \*)?$/+const UNIT_FIELD = /^Unit(?: \*)?$/+const CATALOG_TAB = /^Catalog(?: \(\d+\))?$/+const INVENTORIES = [+ '/api/v1/admin/llm/providers/codex-subscription/connections',+ '/api/v1/admin/llm/providers/grok-subscription/connections',+] as const+const METADATA_REUSE_MS = 30_000++type AgentFixture = { secretName: string; modelName: string; draftModelName: string; quota: number }+type OwnedAgent = { name: string; contextNames: Set<string>; hostCreated: boolean }+type MetadataRead = { path: string; status?: number }++function apiPath(url: string): string {+ return new URL(url).pathname.replace(/^\/control-api(?=\/)/, '')+}++function escapeRegex(value: string): string {+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')+}++function isRead(response: Response, path: string): boolean {+ return response.request().method() === 'GET' && apiPath(response.url()) === path+}++function isWrite(response: Response, method: string, path: string): boolean {+ return response.request().method() === method && apiPath(response.url()) === path+}++function assertOwnedResponse(response: Response): void {+ expect(+ [new URL(CONTROL_UI_URL).origin, new URL(CONTROL_API_URL).origin],+ 'The observed API response must come from the declared owned local runtime'+ ).toContain(new URL(response.url()).origin)+}++function fixtureFor(scenario: 'normal' | 'overload'): AgentFixture {+ requireRecorderConfirm(+ 'QA_RECORDER_CONFIRM_MUTATIONS',+ 'This journey creates and deletes its own agent and context.'+ )+ requireRecorderConfirm(+ 'QA_RECORDER_CONFIRM_OWNED_RUNTIME',+ 'This journey requires a disposable local runtime owned by this lane.'+ )+ for (const [label, url] of [+ ['CONTROL_UI_URL', CONTROL_UI_URL],+ ['CONTROL_API_URL', CONTROL_API_URL],+ ]) {+ requiredValue(label, [process.env[label]])+ // The existing helper documents localhost as necessary for Playwright's+ // API cookie jar to share the secure browser cookie during owned cleanup.+ expect(new URL(url).hostname, `${label} must use the owned localhost mapping`).toBe('localhost')+ assertAllowedTarget(label, url)+ }+ const recorderRoot = requiredValue('QA_RECORDER_ROOT', [process.env.QA_RECORDER_ROOT])+ expect(recorderRoot, 'Use an absolute recording directory under the primary checkout').toMatch(+ /^\//+ )+ expect(+ recorderRoot,+ 'Keep recording evidence under the ignored primary .local-notes/infra/runs path'+ ).toMatch(/\/\.local-notes\/infra\/runs(?:\/|$)/)+ const rawQuota = requiredValue('CONTROL_API_ADMIN_SUBSCRIPTION_READ_PER_MIN', [+ process.env.CONTROL_API_ADMIN_SUBSCRIPTION_READ_PER_MIN,+ ])+ expect(+ rawQuota,+ 'Declare the real API quota as a positive integer; the test never reconfigures it'+ ).toMatch(/^[1-9]\d*$/)+ const quota = Number(rawQuota)+ expect(Number.isSafeInteger(quota)).toBe(true)+ if (scenario === 'normal')+ expect(+ quota,+ 'The normal runtime must meet the subscription read target'+ ).toBeGreaterThanOrEqual(150)+ else expect(quota, 'Configure quota 2 externally before the controlled real-429 run').toBe(2)++ const modelName = requiredValue('E2E_AGENT_MODEL_NAME', [process.env.E2E_AGENT_MODEL_NAME])+ const draftModelName = requiredValue('E2E_AGENT_DRAFT_MODEL_NAME', [+ process.env.E2E_AGENT_DRAFT_MODEL_NAME,+ ])+ expect(draftModelName, 'Draft retention needs a second enabled OpenAI model').not.toBe(modelName)+ return {+ secretName: requiredValue('E2E_AGENT_SECRET_NAME', [process.env.E2E_AGENT_SECRET_NAME]),+ modelName,+ draftModelName,+ quota,+ }+}++function observeReads(page: Page) {+ const reads = new Map<Request, MetadataRead>()+ const throttles: string[] = []+ const recordRequest = (request: Request) => {+ const path = apiPath(request.url())+ if (+ request.method() === 'GET' &&+ (path === CAPABILITIES ||+ /^\/api\/v1\/admin\/llm\/providers\/(?:codex|grok)-subscription(?:\/|$)/.test(path))+ ) {+ reads.set(request, { path })+ }+ }+ const recordResponse = (response: Response) => {+ const read = reads.get(response.request())+ if (read) read.status = response.status()+ const path = apiPath(response.url())+ if (path.startsWith('/api/v1/admin/') && response.status() === 429) throttles.push(path)+ }+ page.on('request', recordRequest)+ page.on('response', recordResponse)+ return {+ snapshot: () => Array.from(reads.values()).map(read => ({ ...read })),+ throttles,+ stop: () => {+ page.off('request', recordRequest)+ page.off('response', recordResponse)+ },+ }+}++async function signIn(page: Page): Promise<void> {+ const authenticated = page.waitForResponse(response =>+ isWrite(response, 'POST', '/api/v1/admin/auth/login')+ )+ await loginThroughUi(page, adminCredentials())+ expect((await authenticated).status(), 'Real UI login must succeed').toBe(200)+ await expect(page).not.toHaveURL(/\/login(?:\?|$)/)+ await expect(page.getByRole('button', { name: 'Log out', exact: true })).toBeVisible()+}++async function signOut(page: Page): Promise<void> {+ const signedOut = page.waitForResponse(response =>+ isWrite(response, 'POST', '/api/v1/admin/auth/logout')+ )+ await page.getByRole('button', { name: 'Log out', exact: true }).click()+ expect([200, 204]).toContain((await signedOut).status())+ // Logout keeps the current route as `?next=` so the next sign-in returns to it.+ await expect(page).toHaveURL(+ new RegExp(`${escapeRegex(CONTROL_ROUTES.login)}(?:\\?next=[^&#]+)?$`)+ )+ await expect(page.getByRole('button', { name: 'Sign in', exact: true })).toBeVisible()+}++async function openAgents(page: Page): Promise<void> {+ await page+ .getByRole('navigation', { name: 'Main sections' })+ .getByRole('link', { name: 'Agents', exact: true })+ .click()+ await expect(page).toHaveURL(new RegExp(`${escapeRegex(CONTROL_ROUTES.agents.root)}$`))+ await expect(+ page.getByText('Manage available agents and their host mappings.', { exact: true })+ ).toBeVisible()+ // Login may already land on Agents. The visible reload provides a real+ // inventory signal even when clicking its current navigation destination.+ const reload = page.getByRole('button', { name: 'Reload agents', exact: true })+ await expect(reload).toBeEnabled()+ const inventory = page.waitForResponse(response => isRead(response, '/api/v1/admin/hosts'))+ await reload.click()+ const response = await inventory+ assertOwnedResponse(response)+ expect(response.status()).toBe(200)+}++async function selectModel(page: Page, picker: Locator, model: string): Promise<void> {+ await expect(picker).toBeEnabled()+ await picker.click()+ const option = page.getByRole('option', { name: model, exact: true })+ await expect(option, `The explicit local model fixture ${model} must be enabled`).toBeVisible()+ await option.click()+ await expect(picker).toContainText(model)+ await expect(picker).toHaveAttribute('aria-expanded', 'false')+}++async function createAgent(page: Page, owned: OwnedAgent, fixture: AgentFixture): Promise<void> {+ await openAgents(page)+ await page.getByRole('button', { name: 'Create agent', exact: true }).click()+ await expect(page).toHaveURL(new RegExp(`${escapeRegex(CONTROL_ROUTES.agents.new)}$`))+ await expect(page.getByRole('heading', { name: 'Create agent', exact: true })).toBeVisible()+ // The field is required, so its accessible name carries the "*" marker.+ const agentName = page.getByLabel(/^Agent name/)+ await agentName.fill(owned.name)+ await expect(agentName).toHaveValue(owned.name)+ await page.getByRole('button', { name: 'Next', exact: true }).click()++ await expect(page.getByRole('region', { name: 'LLM configuration', exact: true })).toBeVisible()+ await page.getByRole('radio', { name: /^Use an existing LLM Secret/ }).check()+ await page.getByRole('button', { name: 'Select LLM Secret...', exact: true }).click()+ const secret = page.getByRole('option', {+ name: new RegExp(`^${escapeRegex(fixture.secretName)}(?:\\s|$)`),+ })+ await expect(+ secret,+ 'The explicit local existing Secret fixture must be available in the visible picker'+ ).toBeVisible()+ await secret.click()+ const provider = page.getByLabel('Provider', { exact: true })+ await provider.click()+ await page.getByRole('option', { name: 'OpenAI', exact: true }).click()+ await expect(provider).toContainText('OpenAI')+ // Prove both model fixtures through the picker before choosing the saved one.+ const model = page.getByLabel('Default model', { exact: true })+ await selectModel(page, model, fixture.draftModelName)+ await selectModel(page, model, fixture.modelName)+ await page.getByRole('button', { name: 'Next', exact: true }).click()+ await expect(page.getByTestId('wizard-users-list')).toBeVisible()+ await expect(page.getByTestId('wizard-empty-access-warning')).toBeVisible()+ await page.getByRole('button', { name: 'Next', exact: true }).click()+ await expect(page.getByRole('group', { name: 'Available connectors' })).toBeVisible()+ await expect(+ page.getByText('None selected. You can add connectors later from the agent detail page.', {+ exact: true,+ })+ ).toBeVisible()++ const created = page.waitForResponse(response => isWrite(response, 'POST', '/api/v1/admin/hosts'))+ await page.getByRole('button', { name: 'Create Agent', exact: true }).click()+ const response = await created+ expect([200, 201]).toContain(response.status())+ const submitted = response.request().postDataJSON()+ expect(submitted.metadata.name).toBe(owned.name)+ expect(submitted.spec.model).toEqual({ provider: 'openai', name: fixture.modelName })+ expect(submitted.spec.secretRef).toBe(fixture.secretName)+ expect(+ owned.contextNames.has(submitted.spec.contextRef),+ 'The linked context must have been created successfully by this UI run'+ ).toBe(true)+ await expect(page).toHaveURL(+ new RegExp(`${escapeRegex(CONTROL_ROUTES.agents.detail(owned.name))}(?:/overview)?$`)+ )+ await expect(+ page.getByRole('heading', { name: `Agent: ${owned.name}`, exact: true })+ ).toBeVisible()+ await expect(page.getByRole('region', { name: 'Agent identity' })).toContainText(owned.name)+ await expect(page.getByRole('region', { name: 'Configuration', exact: true })).toContainText(+ fixture.modelName+ )+}++async function withOwnedAgent(+ page: Page,+ fixture: AgentFixture,+ journey: (owned: OwnedAgent) => Promise<void>+): Promise<void> {+ const owned: OwnedAgent = {+ name: uniqueE2EName('qa-rate-limit'),+ contextNames: new Set(),+ hostCreated: false,+ }+ const trackCreate = (response: Response) => {+ if (response.request().method() !== 'POST' || ![200, 201].includes(response.status())) return+ const path = apiPath(response.url())+ if (path !== '/api/v1/admin/hosts' && path !== '/api/v1/admin/contexts') return+ const body = response.request().postDataJSON()+ const name = body?.metadata?.name+ if (path === '/api/v1/admin/hosts' && name === owned.name) owned.hostCreated = true+ if (+ path === '/api/v1/admin/contexts' &&+ typeof name === 'string' &&+ new RegExp(`^${escapeRegex(owned.name)}-\\d{5}$`).test(name)+ )+ owned.contextNames.add(name)+ }+ page.on('response', trackCreate)+ let journeyFailure: unknown+ try {+ await test.step('sign in and create a test-owned static agent through the wizard', async () => {+ await signIn(page)+ await createAgent(page, owned, fixture)+ })+ await journey(owned)+ } catch (error) {+ journeyFailure = error+ throw error+ } finally {+ page.off('response', trackCreate)+ // The ledger contains only successful creates observed in this execution.+ // A wizard compensation may have removed a context already; 404 is allowed.+ const failures: Error[] = []+ const resources = [+ ...(owned.hostCreated ? [`/api/v1/admin/hosts/${encodeURIComponent(owned.name)}`] : []),+ ...Array.from(+ owned.contextNames,+ name => `/api/v1/admin/contexts/${encodeURIComponent(name)}`+ ),+ ]+ // The journey signs out and back in, so a failure between the two leaves the+ // browser session logged out. Cleanup authenticates on its own.+ if (resources.length > 0) {+ const { status } = await directApi(page.request, 'POST', '/api/v1/admin/auth/login', {+ ...adminCredentials(),+ })+ if (status !== 200) failures.push(new Error(`Cleanup login failed: HTTP ${status}`))+ }+ for (const path of failures.length > 0 ? [] : resources) {+ try {+ const { status } = await directApi(page.request, 'DELETE', path)+ if (![200, 204, 404].includes(status))+ throw new Error(`Owned-resource cleanup failed: ${path}, HTTP ${status}`)+ } catch (error) {+ failures.push(error instanceof Error ? error : new Error('Owned-resource cleanup failed'))+ }+ }+ if (failures.length > 0) {+ throw new AggregateError(+ [...(journeyFailure === undefined ? [] : [journeyFailure]), ...failures],+ // Playwright prints only the message, so name every cause in it.+ `This run left owned resources requiring cleanup: ${failures+ .map(failure => failure.message)+ .join(+ '; '+ )}${journeyFailure instanceof Error ? `; journey: ${journeyFailure.message}` : ''}`+ )+ }+ }+}++function waitForBundle(page: Page, name: string): Promise<Response> {+ // The bundle is an observed UI read. Validate its domain payload below;+ // no request is issued from the test and no internal state is fabricated.+ return page.waitForResponse(response =>+ isRead(response, `/api/v1/admin/hosts/${encodeURIComponent(name)}/detail`)+ )+}++async function assertSavedBundle(+ response: Response,+ owned: OwnedAgent,+ fixture: AgentFixture+): Promise<void> {+ assertOwnedResponse(response)+ expect(response.status()).toBe(200)+ const bundle = await response.json()+ expect(bundle.host.metadata.name).toBe(owned.name)+ expect(bundle.host.spec.model).toEqual({ provider: 'openai', name: fixture.modelName })+ expect(bundle.host.spec.secretRef).toBe(fixture.secretName)+ expect(owned.contextNames.has(bundle.host.spec.contextRef)).toBe(true)+ expect(bundle.agentUsers).toEqual([])+ expect(bundle.agentTeams).toEqual([])+}++async function selectOwnedAgent(+ page: Page,+ owned: OwnedAgent,+ fixture: AgentFixture+): Promise<void> {+ await openAgents(page)+ const row = page.getByRole('row', { name: `Open agent ${owned.name}`, exact: true })+ await expect(row).toBeVisible()+ await expect(row).toContainText(owned.name)+ const bundle = waitForBundle(page, owned.name)+ await row.click()+ await assertSavedBundle(await bundle, owned, fixture)+ await expect(page).toHaveURL(+ new RegExp(`${escapeRegex(CONTROL_ROUTES.agents.detail(owned.name))}(?:/overview)?$`)+ )+ await expect(+ page.getByRole('heading', { name: `Agent: ${owned.name}`, exact: true })+ ).toBeVisible()+}++async function agentSections(page: Page, owned: OwnedAgent, fixture: AgentFixture): Promise<void> {+ const tabs = page.getByRole('tablist', { name: 'Agent sections' })+ for (const [label, slug] of [+ ['Overview', 'overview'],+ ['Identity', 'identity'],+ ['Access', 'access'],+ ['Connectors', 'connectors'],+ ['Models & creds', 'model'],+ ]) {+ await test.step(`open agent ${label} through the visible section tab`, async () => {+ const access =+ label === 'Access'+ ? page.waitForResponse(response =>+ isRead(response, `/api/v1/admin/agents/${encodeURIComponent(owned.name)}/users`)+ )+ : undefined+ const personalization =+ label === 'Identity'+ ? page.waitForResponse(response =>+ isRead(+ response,+ `/api/v1/admin/hosts/${encodeURIComponent(owned.name)}/personalization`+ )+ )+ : undefined+ const tab = tabs.getByRole('tab', { name: label, exact: true })+ await tab.click()+ await expect(page).toHaveURL(+ new RegExp(`${escapeRegex(CONTROL_ROUTES.agents.tab(owned.name, slug))}$`)+ )+ await expect(tab).toHaveAttribute('aria-selected', 'true')+ await expect(+ page.getByRole('heading', { name: `Agent: ${owned.name}`, exact: true })+ ).toBeVisible()+ if (label === 'Overview') {+ await expect(page.getByRole('region', { name: 'Agent identity' })).toContainText(owned.name)+ await expect(+ page.getByRole('region', { name: 'Configuration', exact: true })+ ).toContainText(fixture.modelName)+ await expect(page.getByRole('region', { name: 'Connectors', exact: true })).toContainText(+ 'No connectors attached.'+ )+ } else if (label === 'Identity') {+ const response = await personalization!+ assertOwnedResponse(response)+ expect(response.status()).toBe(200)+ const identity = page.getByRole('region', { name: 'Admin-managed identity files' })+ await expect(identity).toBeVisible()+ await expect(+ identity.getByRole('heading', { name: 'IDENTITY.md', exact: true })+ ).toBeVisible()+ await expect(identity.getByRole('alert')).toHaveCount(0)+ } else if (label === 'Access') {+ const response = await access!+ expect(response.status()).toBe(200)+ expect((await response.json()).items).toEqual([])+ await expect(+ page+ .getByRole('region', { name: 'Access', exact: true })+ .getByText('No members have access yet.', { exact: true })+ ).toBeVisible()+ } else if (label === 'Connectors') {+ await expect(+ page.getByText('Connectors available to this agent.', { exact: true })+ ).toBeVisible()+ await expect(+ page.getByRole('cell', { name: 'No connectors attached yet.', exact: true })+ ).toBeVisible()+ } else {+ await expect(+ page+ .getByRole('region', { name: 'LLM configuration summary' })+ .getByText(fixture.modelName, { exact: true })+ ).toBeVisible()+ await expect(page.getByRole('button', { name: 'Edit', exact: true })).toBeEnabled()+ }+ })+ }+}++async function assertCapabilities(response: Response, fixture: AgentFixture): Promise<void> {+ assertOwnedResponse(response)+ expect(response.status()).toBe(200)+ const headers = response.headers()+ expect(+ headers['x-ratelimit-limit'],+ 'The observed runtime limit must match the declared external configuration'+ ).toBe(String(fixture.quota))+ expect(headers['ratelimit-policy']).toBe(`${fixture.quota};w=60`)+ const body = await response.json()+ for (const provider of ['codex-subscription', 'grok-subscription']) {+ expect(+ body.providers?.[provider]?.enabled,+ `Enable both provider capabilities in the owned fixture; ${provider} is unavailable`+ ).toBe(true)+ }+}++async function firstEditor(page: Page, fixture: AgentFixture): Promise<Response[]> {+ const capabilities = page.waitForResponse(response => isRead(response, CAPABILITIES))+ const reads = [+ capabilities,+ ...INVENTORIES.map(path => page.waitForResponse(response => isRead(response, path))),+ ]+ // Attach rejection handlers to every pending wait before fixture validation.+ // A disabled capability fails clearly before waiting for absent inventories.+ const completed = Promise.allSettled(reads)+ await page.getByRole('button', { name: 'Edit', exact: true }).click()- const dialog = page.getByRole('dialog', { name: 'Edit model configuration', exact: true })++ const dialog = page.getByRole('dialog', { name: 'Edit model & credentials', exact: true })+ await expect(dialog).toBeVisible()+ await expect(dialog.getByLabel('Current model', { exact: true })).toContainText(fixture.modelName)+ await assertCapabilities(await capabilities, fixture)+ const responses = (await completed).map(result => {+ if (result.status === 'rejected') throw result.reason+ return result.value+ })+ await expect(+ dialog.getByRole('status').filter({ hasText: 'Loading subscription options…' })+ ).toHaveCount(0)+ return responses+}++async function cancelEditor(page: Page, owned: OwnedAgent, fixture: AgentFixture): Promise<void> {+ const bundle = waitForBundle(page, owned.name)+ await page- .getByRole('dialog', { name: 'Edit model configuration', exact: true })++ .getByRole('dialog', { name: 'Edit model & credentials', exact: true })+ .getByRole('button', { name: 'Cancel', exact: true })+ .click()+ await assertSavedBundle(await bundle, owned, fixture)+ await expect(- page.getByRole('dialog', { name: 'Edit model configuration', exact: true })++ page.getByRole('dialog', { name: 'Edit model & credentials', exact: true })+ ).toHaveCount(0)+ await expect(+ page+ .getByRole('region', { name: 'LLM configuration summary' })+ .getByText(fixture.modelName, { exact: true })+ ).toBeVisible()+}++async function catalogForms(page: Page, owned: OwnedAgent): Promise<void> {+ const nav = page.getByRole('navigation', { name: 'Main sections' })+ const models = page.waitForResponse(response => isRead(response, '/api/v1/admin/llm-models'))+ await nav.getByRole('link', { name: 'LLM Models', exact: true }).click()+ expect((await models).status()).toBe(200)+ await expect(page).toHaveURL(new RegExp(`${escapeRegex(CONTROL_ROUTES.llmModels.root)}$`))+ await expect(page.getByRole('tab', { name: CATALOG_TAB })).toHaveAttribute(+ 'aria-selected',+ 'true'+ )+ await page.getByRole('button', { name: 'Add model', exact: true }).click()+ await expect(page).toHaveURL(new RegExp(`${escapeRegex(CONTROL_ROUTES.llmModels.new)}$`))+ await expect(page.getByRole('heading', { name: 'Add allowed model', exact: true })).toBeVisible()+ const provider = page.getByRole('combobox', { name: PROVIDER_FIELD })+ await expect(+ provider.getByRole('option', { name: 'xAI Grok Subscription', exact: true })+ ).toHaveCount(1)+ await provider.selectOption('grok-subscription')+ await expect(provider).toHaveValue('grok-subscription')+ await page.getByLabel(MODEL_FIELD).fill(`${owned.name}-model-draft`)+ await expect(page.getByLabel(MODEL_FIELD)).toHaveValue(`${owned.name}-model-draft`)+ await page.getByRole('button', { name: 'Cancel', exact: true }).click()+ await expect(page).toHaveURL(new RegExp(`${escapeRegex(CONTROL_ROUTES.llmModels.root)}$`))++ const cost = nav.getByRole('button', { name: 'Cost & Usage', exact: true })+ if ((await cost.getAttribute('aria-expanded')) !== 'true') await cost.click()+ await expect(cost).toHaveAttribute('aria-expanded', 'true')+ const prices = page.waitForResponse(response => isRead(response, '/api/v1/admin/llm-prices'))+ await nav.getByRole('link', { name: 'LLM Prices', exact: true }).click()+ expect((await prices).status()).toBe(200)+ await expect(page).toHaveURL(new RegExp(`${escapeRegex(CONTROL_ROUTES.costAndUsage.llmPrices)}$`))+ await page.getByRole('button', { name: 'Add price', exact: true }).click()+ await expect(page).toHaveURL(+ new RegExp(`${escapeRegex(CONTROL_ROUTES.costAndUsage.newLlmPrice())}$`)+ )+ await expect(page.getByRole('heading', { name: 'Add LLM price', exact: true })).toBeVisible()+ await expect(+ page+ .getByRole('combobox', { name: PROVIDER_FIELD })+ .getByRole('option', { name: 'xAI Grok Subscription', exact: true })+ ).toHaveCount(1)+ await page.getByLabel(MODEL_FIELD).fill(`${owned.name}-price-draft`)+ await expect(page.getByLabel(MODEL_FIELD)).toHaveValue(`${owned.name}-price-draft`)+ await page.getByRole('button', { name: 'Cancel', exact: true }).click()+ await expect(page).toHaveURL(new RegExp(`${escapeRegex(CONTROL_ROUTES.costAndUsage.llmPrices)}$`))++ const budgets = page.waitForResponse(response => isRead(response, '/api/v1/admin/budgets'))+ await nav.getByRole('link', { name: 'Token Budgets', exact: true }).click()+ expect((await budgets).status()).toBe(200)+ await expect(page).toHaveURL(+ new RegExp(`${escapeRegex(CONTROL_ROUTES.costAndUsage.tokenBudgets)}$`)+ )+ await page.getByRole('button', { name: 'New budget', exact: true }).click()+ await expect(page).toHaveURL(+ new RegExp(`${escapeRegex(CONTROL_ROUTES.costAndUsage.newTokenBudget)}$`)+ )+ await expect(page.getByRole('heading', { name: 'New token budget', exact: true })).toBeVisible()+ await page.getByLabel(NAME_FIELD).fill(`${owned.name}-budget-draft`)+ await page.getByLabel(UNIT_FIELD).selectOption('tokens')+ await page+ .getByRole('combobox', { name: 'Add Provider to scope', exact: true })+ .selectOption('grok-subscription')+ await expect(+ page.getByRole('button', {+ name: 'Remove xAI Grok Subscription from Provider scope',+ exact: true,+ })+ ).toBeVisible()+ await expect(page.getByLabel(NAME_FIELD)).toHaveValue(`${owned.name}-budget-draft`)+ await page.getByRole('button', { name: 'Cancel', exact: true }).click()+ await expect(page).toHaveURL(+ new RegExp(`${escapeRegex(CONTROL_ROUTES.costAndUsage.tokenBudgets)}$`)+ )+}++test.describe('optional QA recorder: administrative rate-limit journeys', () => {+ test('normal navigation reuses subscription reads and preserves the agent', async ({+ page,+ }, testInfo) => {+ const fixture = fixtureFor('normal')+ const entireJourney = observeReads(page)+ try {+ await withOwnedAgent(page, fixture, async owned => {+ await test.step('start a fresh visible session and navigate the owned agent without optional subscription reads', async () => {+ await signOut(page)+ const coldSession = observeReads(page)+ try {+ await signIn(page)+ await selectOwnedAgent(page, owned, fixture)+ await agentSections(page, owned, fixture)+ expect(+ coldSession.snapshot(),+ 'Ordinary agent sections must issue zero optional subscription GETs'+ ).toEqual([])+ const editorOpenedAt = Date.now()+ const responses = await firstEditor(page, fixture)+ expect(responses.map(response => response.status())).toEqual([200, 200, 200])+ expect(+ coldSession+ .snapshot()+ .map(read => read.path)+ .sort()+ ).toEqual([CAPABILITIES, ...INVENTORIES].sort())+ await cancelEditor(page, owned, fixture)+ expect(+ Date.now() - editorOpenedAt,+ 'Reopen must occur within the actual 30-second reuse window'+ ).toBeLessThan(METADATA_REUSE_MS)+ await page.getByRole('button', { name: 'Edit', exact: true }).click()+ const dialog = page.getByRole('dialog', {- name: 'Edit model configuration',++ name: 'Edit model & credentials',+ exact: true,+ })+ await expect(dialog).toBeVisible()+ await expect(dialog.getByLabel('Current model', { exact: true })).toContainText(+ fixture.modelName+ )+ await expect(+ dialog.getByRole('status').filter({ hasText: 'Loading subscription options…' })+ ).toHaveCount(0)+ await expect(dialog.getByRole('alert')).toHaveCount(0)+ await cancelEditor(page, owned, fixture)+ expect(+ coldSession.snapshot(),+ 'Reopening within TTL must reuse all three reads'+ ).toHaveLength(3)+ await agentSections(page, owned, fixture)+ expect(+ coldSession.snapshot(),+ 'Repeating representative sections must not reload subscription inventories'+ ).toHaveLength(3)+ await catalogForms(page, owned)+ expect(+ coldSession.snapshot().filter(read => read.path !== CAPABILITIES),+ 'Catalog forms discover capabilities without listing either provider inventory'+ ).toHaveLength(2)+ expect(coldSession.snapshot().every(read => read.status === 200)).toBe(true)+ await screenshotAndLog(page, testInfo, 'admin-rate-limit-normal')+ } finally {+ coldSession.stop()+ }+ })+ })+ expect(+ entireJourney.throttles,+ 'The configured normal workload must not receive an administrative 429'+ ).toEqual([])+ } finally {+ entireJourney.stop()+ }+ })++ test('real 429 preserves the model draft and recovers after Retry-After', async ({+ page,+ }, testInfo) => {+ const fixture = fixtureFor('overload')+ await withOwnedAgent(page, fixture, async owned => {+ await signOut(page)+ const coldSession = observeReads(page)+ try {+ await signIn(page)+ await selectOwnedAgent(page, owned, fixture)+ await agentSections(page, owned, fixture)+ expect(+ coldSession.snapshot(),+ 'Ordinary agent sections must not spend the low subscription quota'+ ).toEqual([])+ const responses = await firstEditor(page, fixture)+ expect(responses.map(response => response.status()).sort()).toEqual([200, 200, 429])+ expect(+ coldSession+ .snapshot()+ .map(read => read.path)+ .sort()+ ).toEqual([CAPABILITIES, ...INVENTORIES].sort())+ const limited = responses.find(response => response.status() === 429)!+ const retryAfter = Number(limited.headers()['retry-after'])+ expect(+ Number.isSafeInteger(retryAfter),+ 'A real throttle must provide numeric Retry-After'+ ).toBe(true)+ expect(retryAfter).toBeGreaterThan(0)+ expect(retryAfter).toBeLessThanOrEqual(60)+ const body = await limited.json()+ expect(body.code).toBe('rate_limited')+ expect(body.retryAfterSeconds).toBe(retryAfter)+ expect(body.message).toMatch(/try again in \d+ seconds/i)+ const retryAt = Date.now() + retryAfter * 1_000- const dialog = page.getByRole('dialog', { name: 'Edit model configuration', exact: true })++ const dialog = page.getByRole('dialog', { name: 'Edit model & credentials', exact: true })+ const alert = dialog.getByRole('alert')+ await expect(alert).toContainText(/(?:try again|retry) in \d+ seconds/i)+ await expect(alert.getByRole('button', { name: 'Retry', exact: true })).toBeVisible()+ await selectModel(+ page,+ dialog.getByLabel('Current model', { exact: true }),+ fixture.draftModelName+ )+ await expect(+ page.getByRole('heading', {+ name: `Agent: ${owned.name}`,+ exact: true,+ includeHidden: true,+ })+ ).toBeVisible()+ await screenshotAndLog(page, testInfo, 'admin-rate-limit-real-429-draft')++ // Wait on the deadline supplied by the real guard, not a fixed delay.+ await expect+ .poll(() => Date.now(), {+ timeout: retryAfter * 1_000 + 10_000,+ message: 'Respect the observed server Retry-After deadline before the visible retry',+ })+ .toBeGreaterThanOrEqual(retryAt)+ // The shared background recovery may read the denied inventory before or+ // after this click; Retry reuses that result instead of forcing a second+ // read, so assert the quota invariant rather than an exact read count.+ const readsBeforeDeadline = 3+ await alert.getByRole('button', { name: 'Retry', exact: true }).click()+ await expect(dialog.getByRole('alert')).toHaveCount(0)+ await expect(+ dialog.getByRole('status').filter({ hasText: 'Loading subscription options…' })+ ).toHaveCount(0)+ await expect(dialog.getByLabel('Current model', { exact: true })).toContainText(+ fixture.draftModelName+ )+ const afterDeadline = coldSession.snapshot().slice(readsBeforeDeadline)+ expect(afterDeadline.length, 'Recovery reads the denied inventory').toBeGreaterThan(0)+ expect(+ afterDeadline.map(read => read.status),+ 'No read after the Retry-After deadline is throttled again'+ ).not.toContain(429)+ expect(+ afterDeadline.length,+ 'Recovery and the visible Retry stay within one quota window'+ ).toBeLessThanOrEqual(fixture.quota)+ expect(+ afterDeadline.every(read => read.path !== CAPABILITIES),+ 'Recovery reloads only inventories'+ ).toBe(true)+ expect(coldSession.snapshot().filter(read => read.path === CAPABILITIES)).toHaveLength(1)+ await screenshotAndLog(page, testInfo, 'admin-rate-limit-recovered-draft')+ const readsAfterRecovery = coldSession.snapshot().length+ await cancelEditor(page, owned, fixture)+ await agentSections(page, owned, fixture)+ expect(+ coldSession.snapshot(),+ 'Agent identity/access/connectors stay usable after the real throttle'+ ).toHaveLength(readsAfterRecovery)+ } finally {+ coldSession.stop()+ }+ })+ })+})