~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix: request JSON for browser API calls (#6933)
LearningCircuitae88d57dcc33
diff --git a/changelog.d/+json-request-negotiation.bugfix.md b/changelog.d/+json-request-negotiation.bugfix.mdnew file mode 100644index 0000000000..3798add254--- /dev/null+++ b/changelog.d/+json-request-negotiation.bugfix.md@@ -0,0 +1 @@+Send an explicit JSON response preference from the shared browser API client so expired sessions on history routes redirect users to sign in instead of failing to parse the login page.diff --git a/src/local_deep_research/web/static/js/components/details.js b/src/local_deep_research/web/static/js/components/details.jsindex f78fdfcb0d..6288ab239f 100644--- a/src/local_deep_research/web/static/js/components/details.js+++ b/src/local_deep_research/web/static/js/components/details.js@@ -275,29 +275,13 @@const detailsUrl = URLBuilder.historyDetails(researchId);SafeLogger.log('Details URL:', detailsUrl);SafeLogger.log('Full URL being fetched:', window.location.origin + detailsUrl);- const detailsResponse = await fetch(detailsUrl);- SafeLogger.log('Details response status:', detailsResponse.status);- SafeLogger.log('Details response URL:', detailsResponse.url);-- let researchDetails = null;- if (detailsResponse.ok) {- researchDetails = await detailsResponse.json();- SafeLogger.log('Research details loaded:', researchDetails);- } else {- SafeLogger.error('Failed to load research details:', detailsResponse.status);- const errorText = await detailsResponse.text();- SafeLogger.error('Error response:', errorText);- try {- const errorJson = JSON.parse(errorText);- SafeLogger.error('Error JSON:', errorJson);- if (errorJson.message) {- throw new Error(errorJson.message);- }- } catch {- // Not JSON, use original text- }- throw new Error(`Failed to load research details: ${detailsResponse.status}`);- }+ // Shared JSON client: sends Accept: application/json so an+ // expired session on this /history/* route comes back as a+ // JSON 401 that redirects to sign-in, instead of the login+ // page's HTML reaching response.json() as a SyntaxError.+ // Non-2xx rejects and lands in this function's catch.+ const researchDetails = await window.api.fetchWithErrorHandling(detailsUrl);+ SafeLogger.log('Research details loaded:', researchDetails);// Load research metricsSafeLogger.log('Fetching research metrics...');diff --git a/src/local_deep_research/web/static/js/components/logpanel.js b/src/local_deep_research/web/static/js/components/logpanel.jsindex 4b8c79aeb9..f4ade865f8 100644--- a/src/local_deep_research/web/static/js/components/logpanel.js+++ b/src/local_deep_research/web/static/js/components/logpanel.js@@ -626,19 +626,15 @@if (!researchId) return null;const generation = window._logPanelState._countRequestGen || 0;try {- const response = await fetch(URLBuilder.historyLogCount(researchId));- if (response.ok === false) {- if (response.status === 404) {- SafeLogger.log('Log count unavailable: research not found');- } else {- SafeLogger.warn(- 'Log count request failed with status:',- response.status- );- }- return null;- }- const data = await response.json();+ // Shared JSON client: sends Accept: application/json so an+ // expired session on this /history/* route comes back as a+ // JSON 401 that redirects to sign-in instead of login HTML+ // that throws out of response.json(). Every other failure+ // rejects into the catch below — the badge still degrades+ // to the rendered count with no unhandled rejection.+ const data = await window.api.fetchWithErrorHandling(+ URLBuilder.historyLogCount(researchId)+ );// Bail out if the user switched research while this fetch// was in flight. The next load (for the new research) will// refetch and overwrite totalLogs itself.diff --git a/src/local_deep_research/web/static/js/services/api.js b/src/local_deep_research/web/static/js/services/api.jsindex bc97b74350..76eaf0975f 100644--- a/src/local_deep_research/web/static/js/services/api.js+++ b/src/local_deep_research/web/static/js/services/api.js@@ -239,14 +239,20 @@ async function fetchWithErrorHandling(url, options = {}) {try {const csrfToken = getCsrfToken();+ // This wrapper parses JSON even for routes outside /api/. Advertise+ // that response type so an expired session produces a JSON 401.+ const headers = new globalThis.Headers({+ 'Accept': 'application/json',+ 'Content-Type': 'application/json',+ ...(csrfToken ? { 'X-CSRFToken': csrfToken } : {})+ });+ new globalThis.Headers(fetchOptions.headers ?? {}).forEach((value, name) => {+ headers.set(name, value);+ });const response = await fetchWithCsrfRecovery(url, {...fetchOptions,signal: controller.signal,- headers: {- 'Content-Type': 'application/json',- ...(csrfToken ? { 'X-CSRFToken': csrfToken } : {}),- ...(fetchOptions.headers || {})- }+ headers});// Expired/invalid session on an internal API call: bounce the user todiff --git a/tests/infrastructure_tests/test_csrf_api.test.js b/tests/infrastructure_tests/test_csrf_api.test.jsindex bb8c1f434d..a48f1d86c5 100644--- a/tests/infrastructure_tests/test_csrf_api.test.js+++ b/tests/infrastructure_tests/test_csrf_api.test.js@@ -147,7 +147,7 @@ describe('fetchWithErrorHandling - CSRF injection', () => {await apiModule.postJSON('/api/test', { data: 1 });const fetchCall = global.fetch.mock.calls[0];- expect(fetchCall[1].headers['X-CSRFToken']).toBe('post-token');+ expect(new globalThis.Headers(fetchCall[1].headers).get('X-CSRFToken')).toBe('post-token');expect(fetchCall[1].method).toBe('POST');});@@ -156,7 +156,7 @@ describe('fetchWithErrorHandling - CSRF injection', () => {await apiModule.deleteResearch(42);const fetchCall = global.fetch.mock.calls[0];- expect(fetchCall[1].headers['X-CSRFToken']).toBe('delete-token');+ expect(new globalThis.Headers(fetchCall[1].headers).get('X-CSRFToken')).toBe('delete-token');expect(fetchCall[1].method).toBe('DELETE');});@@ -165,15 +165,17 @@ describe('fetchWithErrorHandling - CSRF injection', () => {await fetchWithErrorHandling('/api/test');const fetchCall = global.fetch.mock.calls[0];- expect(fetchCall[1].headers['X-CSRFToken']).toBeUndefined();+ expect(new globalThis.Headers(fetchCall[1].headers).get('X-CSRFToken')).toBeNull();});- test('always sets Content-Type: application/json', async () => {+ test('defaults to JSON request and response headers', async () => {clearMetaToken();await fetchWithErrorHandling('/api/test');const fetchCall = global.fetch.mock.calls[0];- expect(fetchCall[1].headers['Content-Type']).toBe('application/json');+ const headers = new globalThis.Headers(fetchCall[1].headers);+ expect(headers.get('Content-Type')).toBe('application/json');+ expect(headers.get('Accept')).toBe('application/json');});});@@ -192,8 +194,8 @@ describe('Header merge order', () => {clearMetaToken();await fetchWithErrorHandling('/api/test');- const headers = global.fetch.mock.calls[0][1].headers;- expect(headers['Content-Type']).toBe('application/json');+ const headers = new globalThis.Headers(global.fetch.mock.calls[0][1].headers);+ expect(headers.get('Content-Type')).toBe('application/json');});test('caller Content-Type overrides default', async () => {@@ -202,8 +204,8 @@ describe('Header merge order', () => {headers: { 'Content-Type': 'text/plain' }});- const headers = global.fetch.mock.calls[0][1].headers;- expect(headers['Content-Type']).toBe('text/plain');+ const headers = new globalThis.Headers(global.fetch.mock.calls[0][1].headers);+ expect(headers.get('Content-Type')).toBe('text/plain');});test('caller X-CSRFToken overrides auto-injected one', async () => {@@ -212,8 +214,8 @@ describe('Header merge order', () => {headers: { 'X-CSRFToken': 'manual-token' }});- const headers = global.fetch.mock.calls[0][1].headers;- expect(headers['X-CSRFToken']).toBe('manual-token');+ const headers = new globalThis.Headers(global.fetch.mock.calls[0][1].headers);+ expect(headers.get('X-CSRFToken')).toBe('manual-token');});test('caller headers coexist with defaults (no key loss)', async () => {@@ -222,26 +224,28 @@ describe('Header merge order', () => {headers: { 'X-Custom': 'custom-value' }});- const headers = global.fetch.mock.calls[0][1].headers;- expect(headers['Content-Type']).toBe('application/json');- expect(headers['X-CSRFToken']).toBe('my-token');- expect(headers['X-Custom']).toBe('custom-value');+ const headers = new globalThis.Headers(global.fetch.mock.calls[0][1].headers);+ expect(headers.get('Content-Type')).toBe('application/json');+ expect(headers.get('X-CSRFToken')).toBe('my-token');+ expect(headers.get('X-Custom')).toBe('custom-value');});- test('null/undefined caller headers handled via || {} guard', async () => {+ test('null and undefined caller headers retain the defaults', async () => {setMetaToken('safe-token');// Explicitly pass headers: undefinedawait fetchWithErrorHandling('/api/test', { headers: undefined });- const headers1 = global.fetch.mock.calls[0][1].headers;- expect(headers1['Content-Type']).toBe('application/json');- expect(headers1['X-CSRFToken']).toBe('safe-token');+ const headers1 = new globalThis.Headers(global.fetch.mock.calls[0][1].headers);+ expect(headers1.get('Content-Type')).toBe('application/json');+ expect(headers1.get('Accept')).toBe('application/json');+ expect(headers1.get('X-CSRFToken')).toBe('safe-token');// Explicitly pass headers: nullawait fetchWithErrorHandling('/api/test', { headers: null });- const headers2 = global.fetch.mock.calls[1][1].headers;- expect(headers2['Content-Type']).toBe('application/json');- expect(headers2['X-CSRFToken']).toBe('safe-token');+ const headers2 = new globalThis.Headers(global.fetch.mock.calls[1][1].headers);+ expect(headers2.get('Content-Type')).toBe('application/json');+ expect(headers2.get('Accept')).toBe('application/json');+ expect(headers2.get('X-CSRFToken')).toBe('safe-token');});});@@ -259,11 +263,11 @@ describe('Token rotation / idempotency', () => {test('fresh token read on each call (no caching)', async () => {setMetaToken('token-v1');await fetchWithErrorHandling('/api/test');- expect(global.fetch.mock.calls[0][1].headers['X-CSRFToken']).toBe('token-v1');+ expect(new globalThis.Headers(global.fetch.mock.calls[0][1].headers).get('X-CSRFToken')).toBe('token-v1');setMetaToken('token-v2');await fetchWithErrorHandling('/api/test');- expect(global.fetch.mock.calls[1][1].headers['X-CSRFToken']).toBe('token-v2');+ expect(new globalThis.Headers(global.fetch.mock.calls[1][1].headers).get('X-CSRFToken')).toBe('token-v2');});test('token change between calls is reflected', async () => {@@ -273,11 +277,11 @@ describe('Token rotation / idempotency', () => {setMetaToken('second');await fetchWithErrorHandling('/api/b');- const firstHeaders = global.fetch.mock.calls[0][1].headers;- const secondHeaders = global.fetch.mock.calls[1][1].headers;+ const firstHeaders = new globalThis.Headers(global.fetch.mock.calls[0][1].headers);+ const secondHeaders = new globalThis.Headers(global.fetch.mock.calls[1][1].headers);- expect(firstHeaders['X-CSRFToken']).toBe('first');- expect(secondHeaders['X-CSRFToken']).toBe('second');+ expect(firstHeaders.get('X-CSRFToken')).toBe('first');+ expect(secondHeaders.get('X-CSRFToken')).toBe('second');});test('document.querySelector called exactly once per fetchWithErrorHandling invocation', async () => {@@ -499,7 +503,7 @@ describe('deleteResearch - regression guard', () => {await apiModule.deleteResearch(99);const fetchCall = global.fetch.mock.calls[0];- expect(fetchCall[1].headers['X-CSRFToken']).toBe('csrf-for-delete');+ expect(new globalThis.Headers(fetchCall[1].headers).get('X-CSRFToken')).toBe('csrf-for-delete');});test('correct URL constructed via URLBuilder', async () => {diff --git a/tests/js/components/context-overflow-details-caller.test.js b/tests/js/components/context-overflow-details-caller.test.jsindex c6b4066ec5..f056ecda55 100644--- a/tests/js/components/context-overflow-details-caller.test.js+++ b/tests/js/components/context-overflow-details-caller.test.js@@ -4,6 +4,7 @@*/import '@js/config/urls.js';+import '@js/services/api.js';const RESEARCH_ID = 'details-overflow-42';const CONTEXT_URL = `/api/research/${RESEARCH_ID}/context-overflow`;diff --git a/tests/js/components/details-metrics-runtime.test.js b/tests/js/components/details-metrics-runtime.test.jsindex 5391800cd5..07f729d876 100644--- a/tests/js/components/details-metrics-runtime.test.js+++ b/tests/js/components/details-metrics-runtime.test.js@@ -7,6 +7,7 @@*/import '@js/config/urls.js';+import '@js/services/api.js';const RESEARCH_ID = 'metrics-runtime-42';diff --git a/tests/js/components/details-rich-metrics-runtime.test.js b/tests/js/components/details-rich-metrics-runtime.test.jsindex 417749c08f..613b7ea813 100644--- a/tests/js/components/details-rich-metrics-runtime.test.js+++ b/tests/js/components/details-rich-metrics-runtime.test.js@@ -8,6 +8,7 @@*/import '@js/config/urls.js';+import '@js/services/api.js';const RESEARCH_ID = 'rich-metrics-3299';diff --git a/tests/js/components/history-json-client-migration.test.js b/tests/js/components/history-json-client-migration.test.jsnew file mode 100644index 0000000000..cc28c873ba--- /dev/null+++ b/tests/js/components/history-json-client-migration.test.js@@ -0,0 +1,77 @@+/**+ * Source-pinning contract for the two /history/* call sites migrated to+ * the shared JSON client (services/api.js).+ *+ * This PR's changelog promises that an expired session on the history+ * routes redirects the user to sign-in. That is only true when every+ * live history XHR negotiates JSON through fetchWithErrorHandling — a+ * bare fetch is classified as a browser, gets the login page's HTML,+ * and dies in response.json() with a SyntaxError instead.+ *+ * tests/web/test_content_negotiation_census.py proves the server side+ * for all six /history/* XHR routes and services/api.test.js proves the+ * wrapper itself; neither can see which page code actually calls them.+ * This file pins the two page-side call sites the gate found still on+ * bare fetch: the research-details loader (components/details.js) and+ * the log panel's persisted-count fetch (components/logpanel.js).+ *+ * Mirrors the source-pinning style of settings-fetch-error-handling+ * .test.js rather than booting the pages, so the pin cannot be+ * satisfied by a test-only global.+ */++import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';++const COMPONENTS_DIR = resolve(+ __dirname,+ '../../../src/local_deep_research/web/static/js/components',+);+const DETAILS_SOURCE = readFileSync(+ resolve(COMPONENTS_DIR, 'details.js'),+ 'utf8',+);+const LOGPANEL_SOURCE = readFileSync(+ resolve(COMPONENTS_DIR, 'logpanel.js'),+ 'utf8',+);++describe('history JSON client migration', () => {+ it('fetches research details through the shared JSON client', () => {+ const handledCalls = DETAILS_SOURCE.match(+ /window\.api\.fetchWithErrorHandling\(/g,+ ) || [];+ expect(DETAILS_SOURCE)+ .toContain('await window.api.fetchWithErrorHandling(detailsUrl)');+ expect(DETAILS_SOURCE).not.toContain('fetch(detailsUrl)');+ // Exactly one call site migrated. The /metrics/* bare fetches in+ // the same loader are a different route family (the census flags+ // only /history/*), so they stay on fetch.+ expect(handledCalls).toHaveLength(1);+ expect(DETAILS_SOURCE).toContain(+ 'const metricsResponse = await fetch('+ + 'URLBuilder.build(URLS.METRICS_API.RESEARCH, researchId));'+ );+ });++ it('fetches the persisted log count through the shared JSON client', () => {+ const handledCalls = LOGPANEL_SOURCE.match(+ /window\.api\.fetchWithErrorHandling\(/g,+ ) || [];+ expect(LOGPANEL_SOURCE)+ .toContain('window.api.fetchWithErrorHandling(');+ expect(LOGPANEL_SOURCE)+ .toContain('URLBuilder.historyLogCount(researchId)');+ expect(LOGPANEL_SOURCE)+ .not.toContain('fetch(URLBuilder.historyLogCount');+ // Only the log_count site migrated: the diagnostic /history/logs/+ // fetch is a sibling the gate's table left caller-less on purpose.+ expect(handledCalls).toHaveLength(1);+ // Regex literal (not a string) so the pin cannot itself trip+ // no-template-curly-in-string or no-useless-concat. The backticks+ // and ${...} placeholders are part of the matched source.+ expect(LOGPANEL_SOURCE).toMatch(+ /await fetch\(`\$\{url\}\$\{separator\}priority=diagnostic`\)/+ );+ });+});diff --git a/tests/js/components/logpanel.test.js b/tests/js/components/logpanel.test.jsindex 64c0a71572..b75cd325f2 100644--- a/tests/js/components/logpanel.test.js+++ b/tests/js/components/logpanel.test.js@@ -21,6 +21,11 @@ beforeAll(async () => {// logpanel.js destructures window.LdrLogHelpers at IIFE-time.await import('@js/utils/log-helpers.js');+ // fetchAndCacheLogCount issues /history/log_count/{id} through the+ // shared JSON client (window.api.fetchWithErrorHandling), so the+ // panel needs the real service loaded the way base.html loads it.+ await import('@js/services/api.js');+// log-helpers.js is an IIFE that only exposes its surface via// window.LdrLogHelpers — it has no module exports, so we can't// destructure from the import. Wire the helper through window@@ -1499,6 +1504,10 @@ describe('log count indicator — persisted total and Load older', () => {const fetchSpy = vi.fn((url) => {if (url.endsWith('/log_count')) {return Promise.resolve({+ // Response shape the shared JSON client reads: ok+ // responses are parsed straight from .json().+ ok: true,+ status: 200,json: () => Promise.resolve({ total_logs: totalLogs }),});}@@ -1587,7 +1596,9 @@ describe('log count indicator — persisted total and Load older', () => {it('handles a missing research count without reporting malformed data', async () => {const researchId = 'log-count-missing-research';const indicator = addLogIndicator(researchId);- const countJson = vi.fn();+ // The shared JSON client consumes the error body to build its+ // message, so the mock must return a resolving json() promise.+ const countJson = vi.fn(() => Promise.resolve({ detail: 'Research not found' }));const errorSpy = vi.spyOn(SafeLogger, 'error');globalThis.fetch = vi.fn((url) => {if (url.endsWith('/log_count')) {@@ -1607,7 +1618,7 @@ describe('log count indicator — persisted total and Load older', () => {await logPanel.loadLogs(researchId);expect(indicator.textContent).toBe('0');- expect(countJson).not.toHaveBeenCalled();+ expect(countJson).toHaveBeenCalledTimes(1);expect(errorSpy).not.toHaveBeenCalledWith('Invalid log count data received from API');@@ -1771,6 +1782,8 @@ describe('log count indicator — persisted total and Load older', () => {const fetchSpy = vi.fn((url) => {if (url.endsWith('/log_count') && url.includes(researchA)) {return Promise.resolve({+ ok: true,+ status: 200,json: () => countPromise,});}@@ -1780,6 +1793,8 @@ describe('log count indicator — persisted total and Load older', () => {});}return Promise.resolve({+ ok: true,+ status: 200,json: () => Promise.resolve({ total_logs: 100 }),});});diff --git a/tests/js/components/settings-submit-runtime.test.js b/tests/js/components/settings-submit-runtime.test.jsindex dcda33d637..0e71050744 100644--- a/tests/js/components/settings-submit-runtime.test.js+++ b/tests/js/components/settings-submit-runtime.test.js@@ -173,12 +173,12 @@ it('serializes the real form and sends the FastAPI bulk-save contract', async ()expect(url).toBe('/settings/save_all_settings');expect(options).toEqual(expect.objectContaining({method: 'POST',- headers: {- 'Content-Type': 'application/json',- 'X-CSRFToken': 'csrf-settings-submit',- },signal: expect.any(globalThis.AbortSignal),}));+ const headers = new globalThis.Headers(options.headers);+ expect(headers.get('Accept')).toBe('application/json');+ expect(headers.get('Content-Type')).toBe('application/json');+ expect(headers.get('X-CSRFToken')).toBe('csrf-settings-submit');expect(JSON.parse(options.body)).toEqual({'app.theme': 'dark','report.options': { citations: true },diff --git a/tests/js/services/api.test.js b/tests/js/services/api.test.jsindex 7ce5226daa..99db8aafb6 100644--- a/tests/js/services/api.test.js+++ b/tests/js/services/api.test.js@@ -102,6 +102,45 @@ describe('fetchWithErrorHandling — 401 handling', () => {expect(window.location.href).toBe('http://localhost/dashboard');});+ it.each(['status', 'logs', 'report', 'markdown', 'details', 'log_count'])(+ 'requests JSON for /history/%s so an expired session reaches the login handler',+ async (action) => {+ globalThis.fetch = vi.fn((url, options) => {+ const acceptsJson = new globalThis.Headers(options.headers)+ .get('Accept') === 'application/json';+ return Promise.resolve(acceptsJson+ ? new Response('{"detail":"Authentication required"}', { status: 401 })+ : new Response('<!DOCTYPE html><title>Login</title>', { status: 200 }));+ });+ await Promise.race([+ fetchWithErrorHandling(`/history/${action}/research-42`),+ new Promise(resolve => setTimeout(resolve, 50)),+ ]);+ expect(globalThis.fetch).toHaveBeenCalledOnce();+ expect(window.location.href).toBe(+ `/auth/login?next=${encodeURIComponent('/dashboard')}`+ );+ }+ );++ it.each([+ ['record', () => ({ accept: 'application/problem+json', 'x-request-source': 'history' })],+ ['Headers', () => new globalThis.Headers({ Accept: 'application/problem+json', 'X-Request-Source': 'history' })],+ ['pairs', () => [['accept', 'application/problem+json'], ['x-request-source', 'history']]],+ ])('preserves explicit response negotiation from %s headers', async (_, makeHeaders) => {+ const callerHeaders = makeHeaders();+ const before = [...new globalThis.Headers(callerHeaders)];+ globalThis.fetch = vi.fn(() => Promise.resolve(new Response('{"ok":true}')));++ await fetchWithErrorHandling('/history/status/research-42', { headers: callerHeaders });++ const sent = new globalThis.Headers(globalThis.fetch.mock.calls[0][1].headers);+ expect(sent.get('Accept')).toBe('application/problem+json');+ expect(sent.get('X-Request-Source')).toBe('history');+ expect(sent.get('Content-Type')).toBe('application/json');+ expect([...new globalThis.Headers(callerHeaders)]).toEqual(before);+ });+it('redirects to /auth/login with next= on 401 for an internal URL', async () => {globalThis.fetch = vi.fn(() =>Promise.resolve(@@ -456,7 +495,7 @@ describe('FastAPI migration route contracts', () => {const [url, options] = globalThis.fetch.mock.calls[0];expect(url).toBe('/settings/open_file_location');expect(options.method).toBe('POST');- expect(options.headers['X-CSRFToken']).toBe('csrf-migration-test');+ expect(new globalThis.Headers(options.headers).get('X-CSRFToken')).toBe('csrf-migration-test');expect(JSON.parse(options.body)).toEqual({path: '/tmp/research-report.md',});@@ -473,11 +512,10 @@ describe('FastAPI migration route contracts', () => {});const [, options] = globalThis.fetch.mock.calls[0];- expect(options.headers).toMatchObject({- 'Content-Type': 'application/json',- 'X-CSRFToken': 'csrf-refreshed-by-caller',- 'X-Request-Source': 'settings-form',- });+ const sent = new globalThis.Headers(options.headers);+ expect(sent.get('Content-Type')).toBe('application/json');+ expect(sent.get('X-CSRFToken')).toBe('csrf-refreshed-by-caller');+ expect(sent.get('X-Request-Source')).toBe('settings-form');});it.each([@@ -493,7 +531,7 @@ describe('FastAPI migration route contracts', () => {const [url, options] = globalThis.fetch.mock.calls[0];expect(url).toBe('/settings/save_all_settings');expect(options.method).toBe('POST');- expect(options.headers['X-CSRFToken']).toBe('csrf-migration-test');+ expect(new globalThis.Headers(options.headers).get('X-CSRFToken')).toBe('csrf-migration-test');expect(JSON.parse(options.body)).toEqual({ enabled: true });});@@ -567,7 +605,7 @@ describe('FastAPI migration route contracts', () => {const [url, options] = globalThis.fetch.mock.calls[0];expect(url).toBe(expectedUrl);expect(options.method).toBe('POST');- expect(options.headers['X-CSRFToken']).toBe('csrf-migration-test');+ expect(new globalThis.Headers(options.headers).get('X-CSRFToken')).toBe('csrf-migration-test');expect(JSON.parse(options.body)).toEqual(expectedBody);});@@ -578,7 +616,7 @@ describe('FastAPI migration route contracts', () => {const [url, options] = globalThis.fetch.mock.calls[0];expect(url).toBe('/api/delete/research-42');expect(options.method).toBe('DELETE');- expect(options.headers['X-CSRFToken']).toBe('csrf-migration-test');+ expect(new globalThis.Headers(options.headers).get('X-CSRFToken')).toBe('csrf-migration-test');expect(options.body).toBeUndefined();});});diff --git a/tests/theme_tests/test_accent_ink_contrast.py b/tests/theme_tests/test_accent_ink_contrast.pyindex 6f5c278266..45ff1f1e6a 100644--- a/tests/theme_tests/test_accent_ink_contrast.py+++ b/tests/theme_tests/test_accent_ink_contrast.py@@ -965,8 +965,8 @@ DYNAMIC_INLINE_STYLES = {(NUMBER,),None,),- # details.js L716: an inline ternary of two literals.- ("static/js/components/details.js", 716, "color"): (+ # details.js L700: an inline ternary of two literals.+ ("static/js/components/details.js", 700, "color"): ("item.success_status === 'success' ? 'var(--success-color)' "": 'var(--error-color)'",("var(--success-color)", "var(--error-color)"),@@ -1280,7 +1280,7 @@ def test_inline_style_scan_finds_the_known_expressions():("templates/pages/journal_quality.html", 723, "background"),("templates/pages/journal_quality.html", 831, "background"),("static/js/components/context-overflow.js", 574, "background"),- ("static/js/components/details.js", 716, "color"),+ ("static/js/components/details.js", 700, "color"),("templates/pages/metrics.html", 975, "color"),} <= set(found), sorted(found)expanded = {diff --git a/tests/web/test_content_negotiation_census.py b/tests/web/test_content_negotiation_census.pyindex 2161d05202..d2a7c304a3 100644--- a/tests/web/test_content_negotiation_census.py+++ b/tests/web/test_content_negotiation_census.py@@ -19,25 +19,14 @@ thought to name.So this file enumerates. It imports the real, fully-mounted ``app`` anddrives every registered ``GET`` route, plus it AST-sweeps every route's-handler. Four things fell out that no other suite covers:--1. **The ``/history/*`` XHR family is invisible to both mechanisms.**- ``/history/status/{id}``, ``/history/logs/{id}``, ``/history/report/- {id}``, ``/history/markdown/{id}``, ``/history/details/{id}`` and- ``/history/log_count/{id}`` return JSON and are called only by- JavaScript -- but their paths have no ``/api/`` segment, and the app's- own fetch wrapper (``static/js/services/api.js``,- ``fetchWithErrorHandling``) sets ``Content-Type`` but never ``Accept``,- so the browser sends its default ``Accept: */*``. Neither mechanism- fires, the request is classified as a browser navigation, and an- expired session produces a **302 to /auth/login that ``fetch`` follows- transparently, handing the caller a 200 text/html login page**. The- wrapper's ``response.status === 401`` branch never runs, ``response.ok``- is true, and ``await response.json()`` throws a syntax error on- ``<!DOCTYPE``. Polling stops with a parse error instead of bouncing the- user to login. Its sibling ``/api/research/{id}/status`` -- same job,- same caller, same headers -- correctly returns a JSON 401, which is- what makes this a path-shape accident rather than a design choice.+handler. The census records these negotiation contracts and remaining gaps:++1. **The ``/history/*`` XHR family requires explicit JSON negotiation.**+ Its paths have no ``/api/`` segment. The shared browser API client now+ sends ``Accept: application/json``, so expired sessions return a JSON+ 401 that its login handler can recognize. Vitest exercises the actual+ JavaScript wrapper; this census verifies the assembled server's+ response for all six routes. Browser navigations still redirect.2. **``CSRFMiddleware`` does not negotiate at all.** All three of itsrejection sites (``web/dependencies/csrf.py``) build a bare@@ -105,9 +94,7 @@ from local_deep_research.web.fastapi_app import (BROWSER_ACCEPT = ("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")-# What `fetch()` sends when the caller does not set Accept -- which is-# what static/js/services/api.js::fetchWithErrorHandling does on every-# internal call. Not a strawman: it is the app's own XHR header set.+# Native fetch's default. The shared JSON client overrides this explicitly.FETCH_DEFAULT_ACCEPT = "*/*"JSON_ACCEPT = "application/json"@@ -198,13 +185,6 @@ def fresh_limiter():# match its path shape. Every entry is reviewed; a new route landing here# is a content-negotiation bug until someone justifies it in this table.#-# Note for whoever fixes defect 1 below: of the two available fixes, only-# one keeps this census consistent. Renaming the /history/* XHR routes-# under an /api/ segment makes path shape and behaviour agree and needs no-# entry here. Special-casing "/history/" inside _is_api_request instead-# leaves six routes that answer with JSON while looking browser-shaped,-# which this table would then have to absorb -- confirmed by mutating the-# source both ways. Prefer the rename.REVIEWED_CENSUS_EXCEPTIONS = {# Deliberate: an XHR auth probe, not a navigable page. The frontend# reads {"authenticated": false} to decide whether to show a login@@ -345,7 +325,7 @@ class TestUnauthenticatedBrowserCensus:# ----------------------------------------------------------------------------# Defect 1: the /history/* XHR family is invisible to both mechanisms+# History JSON requests negotiate independently of their path shape# ---------------------------------------------------------------------------# JSON endpoints called only by JavaScript whose paths carry no /api/@@ -361,92 +341,19 @@ HISTORY_XHR_ROUTES = [class TestXhrRoutesWithoutAnApiPathSegment:- """Routes only JS calls, that neither policy mechanism recognises."""-- def test_the_frontend_fetch_wrapper_really_does_omit_accept(self):- """Pin the mechanism, so the tests below are not a strawman.-- The whole defect rests on the claim that the app's shared fetch- wrapper sends no ``Accept`` header, leaving the browser default- ``*/*``. Read it out of the shipped JavaScript rather than- asserting it in prose.- """- from pathlib import Path-- from local_deep_research.web import fastapi_app-- api_js = (- Path(fastapi_app.__file__).parent- / "static"- / "js"- / "services"- / "api.js"- )- source = api_js.read_text(encoding="utf-8")- assert "fetchWithErrorHandling" in source, (- "the shared fetch wrapper was renamed or moved; re-derive "- f"this test against {api_js}"- )- # Positive control: the header block this asserts is *absent* an- # Accept key does demonstrably exist and does set other headers.- assert "'Content-Type': 'application/json'" in source, (- "expected the wrapper to set Content-Type; if this moved, "- "the Accept assertion below is checking nothing"- )- body = source[source.index("async function fetchWithErrorHandling") :]- body = body[: body.index("\n}\n")]- assert "'Accept'" not in body and '"Accept"' not in body, (- "fetchWithErrorHandling now sets an Accept header -- if it "- "sends application/json, the xfails below should XPASS and "- "this whole defect class is fixed"- )-- @pytest.mark.parametrize("path", HISTORY_XHR_ROUTES)- def test_explicit_json_accept_is_the_only_thing_that_works(- self, client, path- ):- """Positive control for the xfail below.-- With ``Accept: application/json`` these routes answer an expired- session correctly: a JSON 401. Nothing is broken about the routes- or the handler -- the sole difference between this passing test- and the failing one below is a request header the frontend never- sends.- """- response = client.get(- _fill(path),- headers={"accept": JSON_ACCEPT},- follow_redirects=False,- )- assert response.status_code == 401- assert response.headers["content-type"].startswith("application/json")+ """The shared browser client explicitly requests JSON for these routes."""@pytest.mark.parametrize("path", HISTORY_XHR_ROUTES)- @pytest.mark.xfail(- strict=True,- reason=(- "Neither mechanism of _is_api_request fires: the path has no "- "/api/ segment, and static/js/services/api.js's "- "fetchWithErrorHandling sends the browser default "- "Accept: */*. The request is classified as a browser "- "navigation, so the 401 becomes a 302 to /auth/login that "- "fetch() follows transparently -- the caller gets 200 "- "text/html and response.json() throws on '<!DOCTYPE'. "- "Fix by giving these routes an /api/ segment or by having "- "the wrapper send Accept: application/json."- ),- )def test_an_expired_session_reaches_the_caller_as_a_401(self, client, path):"""A JSON endpoint must report auth failure *as* auth failure.- ``follow_redirects=True`` deliberately mirrors ``fetch()``, which- follows redirects with no way for the caller to opt out. What the- JavaScript observes is the end of that chain.+ Follow redirects as fetch does by default, so an accidental login+ redirect cannot look like a successful JSON response."""response = client.get(_fill(path),headers={- "accept": FETCH_DEFAULT_ACCEPT,+ "accept": JSON_ACCEPT,"content-type": "application/json",},follow_redirects=True,@@ -456,15 +363,11 @@ class TestXhrRoutesWithoutAnApiPathSegment:f"{response.headers.get('content-type')} from "f"{response.url.path} after {len(response.history)} redirects")+ assert response.headers["content-type"].startswith("application/json")+ assert response.history == []def test_the_api_shaped_sibling_gets_this_right(self, client):- """The contrast that proves it is path shape, not design.-- ``/api/research/{id}/status`` is the same polling job for the same- caller with the same headers, and it answers correctly -- so the- ``/history/*`` behaviour above is an accident of URL shape rather- than a deliberate choice about those endpoints.- """+ """An /api/ route also returns JSON with native fetch's default."""response = client.get("/api/research/probe/status",headers={