Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(studio): distinguish unavailable metrics from measured zeros (#5220)
Xinmin Zeng4f46362ee22e
diff --git a/web-studio/src/i18n/locales/en/resources.ts b/web-studio/src/i18n/locales/en/resources.tsindex 541405d01..50f059a4d 100644--- a/web-studio/src/i18n/locales/en/resources.ts+++ b/web-studio/src/i18n/locales/en/resources.ts@@ -7,8 +7,8 @@ const resources = {requestLogs: {accessRequired: {description:- 'Current connection has no admin/root role. Configure an API key with Console Usage/Audit access in Connection & Identity.',- title: 'Admin access required',+ 'Connection identity is unresolved. Check the server address, identity, and authentication settings in Connection Settings.',+ title: 'Connection identity required',},clear: 'Clear',description:@@ -62,9 +62,9 @@ const resources = {reset: 'Reset',searchPlaceholder: 'Filter method, path, or status',scope: {- currentIdentity: 'Current scope: Current API key identity',+ currentIdentity: 'Current scope: Current connection identity',currentIdentityWithName:- 'Current scope: Current API key identity ({{identity}})',+ 'Current scope: Current connection identity ({{identity}})',},status: {error: 'ERR',diff --git a/web-studio/src/i18n/locales/en/workspace.ts b/web-studio/src/i18n/locales/en/workspace.tsindex e0d08f808..68c16c5e2 100644--- a/web-studio/src/i18n/locales/en/workspace.ts+++ b/web-studio/src/i18n/locales/en/workspace.ts@@ -1089,7 +1089,7 @@ const workspace = {usageDisabled:'Usage/Audit is not initialized, so live usage stats are unavailable.',usageAccessRequired:- 'Current connection has no admin/root role. Configure an API key with Console Usage/Audit access in Connection & Identity.',+ 'Connection identity is unresolved. Check the server address, identity, and authentication settings in Connection Settings.',},} as constdiff --git a/web-studio/src/i18n/locales/zh-CN/resources.ts b/web-studio/src/i18n/locales/zh-CN/resources.tsindex 60a3cf3ec..72b91f959 100644--- a/web-studio/src/i18n/locales/zh-CN/resources.ts+++ b/web-studio/src/i18n/locales/zh-CN/resources.ts@@ -7,8 +7,8 @@ const resources = {requestLogs: {accessRequired: {description:- '当前连接没有管理员或 Root 权限,无法显示请求日志。请在“连接设置”中配置具备控制台用量和审计权限的 API 密钥。',- title: '需要管理员权限',+ '当前连接身份尚未确认。请在“连接设置”中检查服务地址、身份和认证配置。',+ title: '需要确认连接身份',},clear: '清空',description:@@ -60,9 +60,8 @@ const resources = {reset: '重置',searchPlaceholder: '筛选方法、路径或状态码',scope: {- currentIdentity: '当前范围:当前 API 密钥对应的身份',- currentIdentityWithName:- '当前范围:当前 API 密钥对应的身份({{identity}})',+ currentIdentity: '当前范围:当前连接身份',+ currentIdentityWithName: '当前范围:当前连接身份({{identity}})',},status: {error: '错误',diff --git a/web-studio/src/i18n/locales/zh-CN/workspace.ts b/web-studio/src/i18n/locales/zh-CN/workspace.tsindex f46f8b4d8..7c2bf0f7b 100644--- a/web-studio/src/i18n/locales/zh-CN/workspace.ts+++ b/web-studio/src/i18n/locales/zh-CN/workspace.ts@@ -1044,7 +1044,7 @@ const workspace = {},usageDisabled: '用量和审计功能尚未初始化,暂无实时统计。',usageAccessRequired:- '当前连接没有管理员或 Root 权限,无法显示用量和审计数据。请在“连接设置”中配置具备控制台用量和审计权限的 API 密钥。',+ '当前连接身份尚未确认。请在“连接设置”中检查服务地址、身份和认证配置。',},} as constdiff --git a/web-studio/src/routes/home/-components/metric-panels.tsx b/web-studio/src/routes/home/-components/metric-panels.tsxindex b83ad0b26..8434a402f 100644--- a/web-studio/src/routes/home/-components/metric-panels.tsx+++ b/web-studio/src/routes/home/-components/metric-panels.tsx@@ -115,7 +115,7 @@ function MetricPanel({)}</div>- {children ? (+ {children && !isLoading && !isError ? (<div className="mt-4 grid grid-cols-[repeat(auto-fit,minmax(140px,1fr))] gap-x-5 gap-y-1">{children}</div>@@ -147,7 +147,9 @@ export function ContextDataPanel({isError={isError}isLoading={isLoading}title={t('contextData.title')}- value={isError ? t('requestFailed') : formatNumber(total)}+ value={+ isError ? t('requestFailed') : disabled ? '—' : formatNumber(total)+ }>{disabled ? (<p className="text-xs text-muted-foreground">{disabledMessage}</p>@@ -194,7 +196,9 @@ export function TodayTokensPanel({isError={isError}isLoading={isLoading}title={t('todayTokens.title')}- value={isError ? t('requestFailed') : formatNumber(total)}+ value={+ isError ? t('requestFailed') : disabled ? '—' : formatNumber(total)+ }>{disabled ? (<p className="text-xs text-muted-foreground">{disabledMessage}</p>@@ -241,7 +245,9 @@ export function TodayRetrievalsPanel({isError={isError}isLoading={isLoading}title={t('todayRetrievals.title')}- value={isError ? t('requestFailed') : formatNumber(total)}+ value={+ isError ? t('requestFailed') : disabled ? '—' : formatNumber(total)+ }>{disabled ? (<p className="text-xs text-muted-foreground">{disabledMessage}</p>diff --git a/web-studio/src/routes/home/route.test.tsx b/web-studio/src/routes/home/route.test.tsxnew file mode 100644index 000000000..de6dff032--- /dev/null+++ b/web-studio/src/routes/home/route.test.tsx@@ -0,0 +1,164 @@+// @vitest-environment jsdom+import type { ComponentType } from 'react'+import {+ act,+ cleanup,+ render,+ screen,+ waitFor,+ within,+} from '@testing-library/react'+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'+import { afterEach, beforeEach, expect, it, vi } from 'vitest'++import { Route } from './route'++const state = vi.hoisted(() => ({+ role: 'user',+ summary: vi.fn(),+ tokens: vi.fn(),+ commits: vi.fn(),+}))+vi.mock('#/hooks/use-app-connection', () => ({+ useAppConnection: () => ({+ connection: {+ baseUrl: 'http://localhost:1933',+ accountId: 'account-a',+ userId: 'alice',+ apiKey: '',+ adminApiKey: '',+ },+ connectionRole: state.role,+ isConnectionRoleLoading: false,+ }),+}))+vi.mock('./-lib/api', () => ({+ fetchConsoleDashboardSummary: state.summary,+ fetchConsoleTokenSeries: state.tokens,+ fetchConsoleContextCommits: state.commits,+}))+vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}))+beforeEach(() => {+ state.role = 'user'+ state.summary.mockReset().mockResolvedValue({+ context_counts: { total: 0 },+ today_tokens: { total: 0 },+ today_retrievals: { total: 0 },+ })+ state.tokens.mockReset().mockResolvedValue({ items: [] })+ state.commits.mockReset().mockResolvedValue({ items: [] })+ vi.stubGlobal('matchMedia', () => ({ matches: true }))+})+afterEach(() => {+ cleanup()+ vi.unstubAllGlobals()+})++async function renderHome() {+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ })+ const Home = Route.options.component as ComponentType & {+ preload?: () => Promise<unknown>+ }+ await Home.preload?.()+ const view = render(+ <QueryClientProvider client={client}>+ <Home />+ </QueryClientProvider>,+ )+ return () => {+ view.unmount()+ client.clear()+ }+}++it.each(['unknown', 'disabled'])(+ 'shows unavailable values for %s data',+ async (reason) => {+ if (reason === 'unknown') state.role = 'unknown'+ else state.summary.mockResolvedValue({ enabled: false })+ const close = await renderHome()+ try {+ await waitFor(() => expect(screen.getAllByText('—')).toHaveLength(3), {+ timeout: 2000,+ })+ if (reason === 'unknown') expect(state.summary).not.toHaveBeenCalled()+ } finally {+ close()+ }+ },+)++it('renders successful zero measurements after the response completes', async () => {+ const zero = {+ context_counts: { total: 0 },+ today_tokens: { total: 0 },+ today_retrievals: { total: 0 },+ }+ let complete!: (value: typeof zero) => void+ state.summary.mockImplementation(+ () =>+ new Promise<typeof zero>((resolve) => {+ complete = resolve+ }),+ )+ const close = await renderHome()+ try {+ await waitFor(() => expect(state.summary).toHaveBeenCalled())+ expect(screen.queryAllByText('0')).toHaveLength(0)+ await act(async () => {+ complete(zero)+ })+ await screen.findByText('contextData.files')+ for (const title of [+ 'contextData.title',+ 'todayTokens.title',+ 'todayRetrievals.title',+ ]) {+ const panel = screen+ .getByRole('heading', { name: title })+ .closest('section')!+ expect(+ within(panel).getByText('0', { selector: '.text-4xl span' }),+ ).toBeTruthy()+ expect(within(panel).queryByText('—')).toBeNull()+ }+ } finally {+ close()+ }+})++it('uses each series result when the summary is disabled', async () => {+ state.summary.mockResolvedValue({ enabled: false })+ const close = await renderHome()+ try {+ await waitFor(+ () => expect(screen.getByText('tokenTrend.empty')).toBeTruthy(),+ { timeout: 2000 },+ )+ expect(screen.getByText('contextCommits.empty')).toBeTruthy()+ } finally {+ close()+ }+})++it.each(['loading', 'error'])(+ 'withholds metric detail values during %s',+ async (stateName) => {+ if (stateName === 'loading')+ state.summary.mockImplementation(() => new Promise(() => {}))+ else state.summary.mockRejectedValue(new Error('Request failed'))+ const close = await renderHome()+ try {+ await waitFor(() => expect(state.summary).toHaveBeenCalled())+ if (stateName === 'error') await screen.findAllByText('requestFailed')+ expect(screen.queryByText('contextData.files')).toBeNull()+ expect(screen.queryAllByText('0')).toHaveLength(0)+ } finally {+ close()+ }+ },+)diff --git a/web-studio/src/routes/home/route.tsx b/web-studio/src/routes/home/route.tsxindex 0841ae0c0..2ba0cc575 100644--- a/web-studio/src/routes/home/route.tsx+++ b/web-studio/src/routes/home/route.tsx@@ -83,15 +83,18 @@ function HomePage() {})const summary = dashboard.data- const missingPrivilegedRole =+ const identityUnavailable =!isConnectionRoleLoading && connectionRole === 'unknown'- const metricsUnavailable = missingPrivilegedRole || isDisabledPayload(summary)- const unavailableMessage = missingPrivilegedRole+ const metricsUnavailable = identityUnavailable || isDisabledPayload(summary)+ const unavailableMessage = identityUnavailable? t('usageAccessRequired'): t('usageDisabled')- const isMetricsLoading = isConnectionRoleLoading || dashboard.isLoading- const isSeriesLoading = isConnectionRoleLoading || tokenSeries.isLoading- const isCommitsLoading = isConnectionRoleLoading || contextCommits.isLoading+ const isMetricsLoading =+ isConnectionRoleLoading || (canQueryMetrics && dashboard.isPending)+ const isSeriesLoading =+ isConnectionRoleLoading || (canQueryMetrics && tokenSeries.isPending)+ const isCommitsLoading =+ isConnectionRoleLoading || (canQueryMetrics && contextCommits.isPending)return (<div className="flex flex-col gap-5 pb-8">@@ -124,7 +127,7 @@ function HomePage() {<TokenTrendPaneldata={tokenSeries.data}- disabled={metricsUnavailable}+ disabled={identityUnavailable}disabledMessage={unavailableMessage}isError={tokenSeries.isError}isLoading={isSeriesLoading}@@ -133,7 +136,7 @@ function HomePage() {<ContextCommitsPaneldata={contextCommits.data}- disabled={metricsUnavailable}+ disabled={identityUnavailable}disabledMessage={unavailableMessage}isError={contextCommits.isError}isLoading={isCommitsLoading}diff --git a/web-studio/src/routes/request-logs/route.test.tsx b/web-studio/src/routes/request-logs/route.test.tsxnew file mode 100644index 000000000..9d77c44c7--- /dev/null+++ b/web-studio/src/routes/request-logs/route.test.tsx@@ -0,0 +1,71 @@+// @vitest-environment jsdom+import type { ComponentType } from 'react'+import { cleanup, render, screen, waitFor } from '@testing-library/react'+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'+import { afterEach, expect, it, vi } from 'vitest'++import { Route } from './route'+import type * as AuditApi from './-lib/api'++const { fetchLogs } = vi.hoisted(() => ({ fetchLogs: vi.fn() }))+vi.mock('#/hooks/use-app-connection', () => ({+ useAppConnection: () => ({+ connection: {+ baseUrl: 'http://localhost:1933',+ accountId: 'account-a',+ userId: 'alice',+ apiKey: '',+ adminApiKey: '',+ },+ connectionRole: 'user',+ isConnectionRoleLoading: false,+ }),+}))+vi.mock('./-lib/api', async (importOriginal) => ({+ ...(await importOriginal<typeof AuditApi>()),+ fetchAuditLogs: fetchLogs,+}))+vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}))+afterEach(cleanup)++it.each(['loading', 'disabled', 'error', 'zero'])(+ 'renders audit metrics for %s',+ async (state) => {+ fetchLogs.mockReset()+ if (state === 'loading')+ fetchLogs.mockImplementation(() => new Promise(() => {}))+ if (state === 'disabled') fetchLogs.mockResolvedValue({ enabled: false })+ if (state === 'error')+ fetchLogs.mockRejectedValue(new Error('Request failed'))+ if (state === 'zero')+ fetchLogs.mockResolvedValue({ items: [], total: 0, success_rate: 0 })+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ })+ const Logs = Route.options.component as ComponentType & {+ preload?: () => Promise<unknown>+ }+ await Logs.preload?.()+ const view = render(+ <QueryClientProvider client={client}>+ <Logs />+ </QueryClientProvider>,+ )+ try {+ if (state === 'disabled') await screen.findByText('disabled.title')+ if (state === 'error') await screen.findByText('error.title')+ if (state === 'zero') await screen.findByText('empty.title')+ if (state === 'zero') {+ await waitFor(() => expect(screen.getByText('0%')).toBeTruthy())+ expect(screen.queryByText('—')).toBeNull()+ } else {+ await waitFor(() => expect(screen.getAllByText('—')).toHaveLength(2))+ }+ } finally {+ view.unmount()+ client.clear()+ }+ },+)diff --git a/web-studio/src/routes/request-logs/route.tsx b/web-studio/src/routes/request-logs/route.tsxindex 1f90c5e44..514cada13 100644--- a/web-studio/src/routes/request-logs/route.tsx+++ b/web-studio/src/routes/request-logs/route.tsx@@ -54,6 +54,8 @@ function RequestLogsRoute() {const logs = zeroResult ? [] : (audit.data?.items ?? [])const disabled = audit.data?.enabled === false+ const hasMetrics =+ zeroResult || (!isConnectionRoleLoading && audit.isSuccess && !disabled)const total = zeroResult ? 0 : (audit.data?.total ?? 0)const pageCount = Math.max(1, Math.ceil(total / pageSize))const identityParts = [connection.accountId, connection.userId].filter(@@ -110,12 +112,16 @@ function RequestLogsRoute() {<div className="grid gap-3 md:grid-cols-2"><MetricCardlabel={t('metrics.total')}- value={total >= 1000 ? '999+' : total}+ value={hasMetrics ? (total >= 1000 ? '999+' : total) : '—'}icon={<ActivityIcon className="size-4" />}/><MetricCardlabel={t('metrics.successRate')}- value={formatPercent(zeroResult ? 0 : audit.data?.success_rate)}+ value={+ hasMetrics+ ? formatPercent(zeroResult ? 0 : audit.data?.success_rate)+ : '—'+ }icon={<BarChart3Icon className="size-4" />}/></div>@@ -127,7 +133,9 @@ function RequestLogsRoute() {filters={filters}isError={audit.isError}isFetching={audit.isFetching}- isLoading={isConnectionRoleLoading || audit.isLoading}+ isLoading={+ isConnectionRoleLoading || (!zeroResult && audit.isPending)+ }logs={logs}onDraftFiltersChange={setDraftFilters}onLogTypeChange={handleLogTypeChange}