From dec2ff6735914d7fe5395e38a62dd3adc57aa0f9 Mon Sep 17 00:00:00 2001 From: Brian O'Kelley Date: Wed, 30 Sep 2026 20:40:54 +0000 Subject: [PATCH] fix(registry): show public agents when the session cookie is stale Since #7462, optionalAuth returns 401 for a present-but-unusable wos-session cookie instead of treating the caller as anonymous. The Agents page swallowed that error and rendered an empty registry. Retry the public list once with credentials omitted on 401, and show an error instead of an empty grid for any other failure. Co-Authored-By: Claude Opus 5.5 (1M context) --- server/public/agents.html | 35 ++++++++++++++----- .../unit/public-agent-registry-ui.test.ts | 29 ++++++++++++++- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/server/public/agents.html b/server/public/agents.html index d01e5abc229..a39929a2d36 100644 --- a/server/public/agents.html +++ b/server/public/agents.html @@ -529,6 +529,7 @@

let allAgents = []; let filters = { type: 'all', status: 'all', compliance: 'all', search: '' }; let isLoading = true; + let loadError = null; function escapeHtml(str) { if (!str) return ''; @@ -546,6 +547,22 @@

} } + // The registry list is public. A stale or unusable session cookie makes the + // server answer 401 instead of treating the caller as anonymous, so retry + // without credentials rather than rendering an empty registry. + async function fetchRegistryAgents(query) { + const url = `/api/registry/agents?${query}`; + let response = await fetch(url); + if (response.status === 401) { + response = await fetch(url, { credentials: 'omit' }); + } + const data = await response.json(); + if (!response.ok || !Array.isArray(data.agents)) { + throw new Error(data.error || `Request failed (${response.status})`); + } + return data.agents; + } + function agentToolCount(agent) { return Number(agent?.capabilities?.tools_count ?? agent?.health?.tools_count) || 0; } @@ -568,9 +585,7 @@

async function loadAllAgents() { try { - const response = await fetch('/api/registry/agents?health=true&properties=true&capabilities=true&compliance=true'); - const data = await response.json(); - const agents = data.agents || data; + const agents = await fetchRegistryAgents('health=true&properties=true&capabilities=true&compliance=true'); allAgents = agents.map(agent => { const hasPropertyError = agent.propertiesError && agent.propertiesError.length > 0; @@ -592,6 +607,7 @@

renderAgents(); } catch (error) { console.error('Failed to load agents:', error); + loadError = error; isLoading = false; renderAgents(); } @@ -633,6 +649,11 @@

return; } + if (loadError) { + container.innerHTML = `
Failed to load agents: ${escapeHtml(loadError.message)}. Try reloading the page.
`; + return; + } + const filtered = allAgents.filter(agent => { if (filters.type !== 'all' && agent.type !== filters.type) return false; if (filters.status !== 'all') { @@ -884,9 +905,7 @@

const [type, ...nameParts] = agentId.split('/'); const name = nameParts.join('/'); - const capsResp = await fetch('/api/registry/agents?health=true&capabilities=true&properties=true&compliance=true'); - const capsData = await capsResp.json(); - const allAgents = capsData.agents || []; + const allAgents = await fetchRegistryAgents('health=true&capabilities=true&properties=true&compliance=true'); const agentWithCaps = allAgents.find(a => { @@ -1136,9 +1155,7 @@

Agent information

async function fetchTools(agent) { const section = document.getElementById('tools-section'); try { - const response = await fetch('/api/registry/agents?health=true&capabilities=true'); - const data = await response.json(); - const allAgents = data.agents || data; + const allAgents = await fetchRegistryAgents('health=true&capabilities=true'); const agentWithCaps = allAgents.find(a => a.url === agent.url); const toolCount = agentToolCount(agentWithCaps); diff --git a/server/tests/unit/public-agent-registry-ui.test.ts b/server/tests/unit/public-agent-registry-ui.test.ts index 57ac423279f..fe0f7c527af 100644 --- a/server/tests/unit/public-agent-registry-ui.test.ts +++ b/server/tests/unit/public-agent-registry-ui.test.ts @@ -1,5 +1,5 @@ import { readFileSync } from 'node:fs'; -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; const agentsHtml = readFileSync(new URL('../../public/agents.html', import.meta.url), 'utf8'); @@ -34,4 +34,31 @@ describe('public agent registry UI', () => { expect(agentsHtml).not.toContain('href="${escapeHtml(p.verification_url)}"'); expect(agentsHtml).toContain('escapeHtml(String(params.min_width || 0))'); }); + + describe('registry list fetch', () => { + const source = agentsHtml.match(/async function fetchRegistryAgents\(query\) \{[\s\S]*?\n \}\n/)?.[0]; + const load = (fetchImpl: typeof fetch) => + new Function('fetch', `${source}; return fetchRegistryAgents;`)(fetchImpl) as (query: string) => Promise; + const json = (status: number, body: unknown) => + new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); + + it('retries anonymously when a stale session cookie is rejected', async () => { + expect(source).toBeDefined(); + const fetchMock = vi.fn() + .mockResolvedValueOnce(json(401, { error: 'Invalid session', login_url: '/auth/login' })) + .mockResolvedValueOnce(json(200, { agents: [{ url: 'https://agent.example' }], count: 1 })); + + await expect(load(fetchMock)('health=true')).resolves.toEqual([{ url: 'https://agent.example' }]); + expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/registry/agents?health=true'); + expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/registry/agents?health=true', { credentials: 'omit' }); + }); + + it('surfaces failures instead of rendering an empty registry', async () => { + const fetchMock = vi.fn().mockResolvedValue(json(503, { error: 'Authorization service temporarily unavailable' })); + + await expect(load(fetchMock)('health=true')).rejects.toThrow('Authorization service temporarily unavailable'); + expect(fetchMock).toHaveBeenCalledTimes(1); + expect(agentsHtml).toContain('Failed to load agents: ${escapeHtml(loadError.message)}'); + }); + }); });