~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
/**
* Direct runtime contracts for the context-overflow analytics page.
*
* The smaller context-overflow.test.js suite pins request ownership and the
* headline chart data. These tests exercise the production bootstrap and the
* user-facing controls, rendering, empty-state transitions, and chart
* callbacks that are otherwise easy to break during the FastAPI migration.
*/
const originalReadyState = Object.getOwnPropertyDescriptor(
document,
'readyState'
);
class ChartStub {
static instances = [];
constructor(ctx, config) {
this.ctx = ctx;
this.config = config;
this.data = config.data;
this.destroy = vi.fn();
ChartStub.instances.push(this);
}
}
function buildPageDom() {
document.body.innerHTML = `
<button class="ldr-time-range-btn active" data-period="30d">30D</button>
<button class="ldr-time-range-btn" data-period="7d">7D</button>
<div id="loading"></div>
<div id="content" style="display: none">
<div id="empty-no-data" style="display: none"></div>
<div id="empty-no-context-data" style="display: none"></div>
<div id="empty-no-truncation" style="display: none"></div>
<div id="warning-banner" style="display: none">
<span id="warning-rate"></span>
</div>
<div id="context-overflow-section"></div>
<input id="requests-search">
<table>
<thead>
<tr>
<th class="ldr-sortable" data-sort="model">Model</th>
<th class="ldr-sortable" data-sort="total_tokens">Tokens</th>
</tr>
</thead>
<tbody id="requests-tbody"></tbody>
</table>
<button id="pagination-prev"></button>
<span id="pagination-info"></span>
<button id="pagination-next"></button>
</div>
`;
}
function payload(overrides = {}) {
const overview = {
truncation_rate: 0,
truncated_requests: 0,
requests_with_context_data: 0,
total_requests: 1,
avg_tokens_truncated: 0,
...overrides.overview,
};
const tokenSummary = {
total_requests: 1,
...overrides.token_summary,
};
return {
status: 'success',
model_stats: [],
model_token_stats: [],
recent_truncated: [],
chart_data: [],
context_limits: [],
phase_breakdown: [],
current_context_window: null,
all_requests: [],
pagination: { page: 1, total_pages: 1 },
...overrides,
overview,
token_summary: tokenSummary,
};
}
function request(overrides = {}) {
return {
timestamp: '2026-08-01T12:00:00.000Z',
research_id: 'research-1',
model: 'openai/gpt-4o',
provider: 'openai',
research_phase: 'search',
prompt_tokens: 80,
completion_tokens: 20,
total_tokens: 100,
context_limit: 8192,
context_truncated: false,
tokens_truncated: 0,
research_query: 'Alpha question',
...overrides,
};
}
function response(body, ok = true) {
return Promise.resolve({
ok,
json: vi.fn().mockResolvedValue(body),
});
}
async function importController(fetchMock) {
globalThis.fetch = fetchMock;
await import('@js/components/context-overflow.js');
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalled());
await vi.waitFor(() => {
expect(document.getElementById('content').style.display).toBe('block');
});
return window.contextOverflowController;
}
beforeEach(() => {
vi.resetModules();
vi.restoreAllMocks();
delete window.__VITEST_TEST__;
delete window.contextOverflowController;
buildPageDom();
ChartStub.instances = [];
globalThis.Chart = ChartStub;
globalThis.URLValidator = { safeAssign: vi.fn() };
vi.spyOn(window.HTMLCanvasElement.prototype, 'getContext')
.mockReturnValue({});
Object.defineProperty(document, 'readyState', {
configurable: true,
value: 'complete',
});
});
afterAll(() => {
if (originalReadyState) {
Object.defineProperty(document, 'readyState', originalReadyState);
} else {
delete document.readyState;
}
});
it('boots the canonical endpoint and wires filtering, sorting, paging, and periods', async () => {
const alpha = request();
const beta = request({
research_id: 'research-2',
model: 'anthropic/claude-3',
provider: 'anthropic',
total_tokens: 900,
research_query: 'Beta question',
});
const fetchMock = vi.fn(url => {
const params = new URL(url, 'https://local.test').searchParams;
const page = Number(params.get('page'));
return response(payload({
all_requests: [alpha, beta],
pagination: { page, total_pages: 3 },
}));
});
await importController(fetchMock);
expect(fetchMock.mock.calls[0][0])
.toBe('/api/context-overflow?period=30d&page=1&per_page=50');
expect(document.querySelectorAll('#requests-tbody tr')).toHaveLength(2);
const search = document.getElementById('requests-search');
search.value = 'beta';
search.dispatchEvent(new Event('input'));
expect(document.querySelectorAll('#requests-tbody tr')).toHaveLength(1);
expect(document.getElementById('requests-tbody').textContent)
.toContain('Beta question');
search.value = '';
search.dispatchEvent(new Event('input'));
const tokenHeader = document.querySelector('[data-sort="total_tokens"]');
tokenHeader.click();
expect(document.querySelector('#requests-tbody tr').textContent)
.toContain('Beta question');
expect(tokenHeader.classList.contains('ldr-sort-desc')).toBe(true);
tokenHeader.click();
expect(document.querySelector('#requests-tbody tr').textContent)
.toContain('Alpha question');
expect(tokenHeader.classList.contains('ldr-sort-asc')).toBe(true);
document.getElementById('pagination-next').click();
await vi.waitFor(() => expect(fetchMock.mock.calls[1][0]).toContain('page=2'));
await vi.waitFor(() => {
expect(document.getElementById('pagination-info').textContent)
.toBe('Page 2 of 3');
});
document.getElementById('pagination-prev').click();
await vi.waitFor(() => expect(fetchMock.mock.calls[2][0]).toContain('page=1'));
document.querySelector('[data-period="7d"]').click();
await vi.waitFor(() => {
expect(fetchMock.mock.calls[3][0])
.toBe('/api/context-overflow?period=7d&page=1&per_page=50');
});
expect(document.querySelector('[data-period="7d"]')
.classList.contains('active')).toBe(true);
expect(document.querySelector('[data-period="30d"]')
.classList.contains('active')).toBe(false);
});
it('clears old rows, pagination, and provider banners when a period has no data', async () => {
const fetchMock = vi.fn()
.mockImplementationOnce(() => response(payload({
all_requests: [request()],
pagination: { page: 2, total_pages: 4 },
})))
.mockImplementationOnce(() => response(payload({
token_summary: { total_requests: 0 },
overview: { total_requests: 0 },
pagination: { page: 1, total_pages: 1 },
})));
const controller = await importController(fetchMock);
expect(document.getElementById('empty-no-context-data').style.display)
.toBe('block');
expect(document.querySelectorAll('#requests-tbody tr')).toHaveLength(1);
await controller.loadContextData('7d', 1);
expect(document.getElementById('empty-no-data').style.display).toBe('block');
expect(document.getElementById('empty-no-context-data').style.display)
.toBe('none');
expect(document.getElementById('empty-no-truncation').style.display)
.toBe('none');
expect(document.getElementById('requests-tbody').textContent)
.toContain('No request data available');
expect(document.getElementById('pagination-info').textContent)
.toBe('Page 1 of 1');
expect(document.getElementById('pagination-prev').disabled).toBe(true);
expect(document.getElementById('pagination-next').disabled).toBe(true);
});
it('renders user-controlled request fields without creating injected attributes', async () => {
const hostile = '" onmouseover="globalThis.__contextOverflowXss = true';
const hostileRequest = request({
research_id: `id/${hostile}`,
model: hostile,
provider: hostile,
research_phase: hostile,
research_query: hostile,
});
const fetchMock = vi.fn(() => response(payload({
all_requests: [hostileRequest],
})));
await importController(fetchMock);
expect(document.querySelector('[onmouseover]')).toBeNull();
expect(document.getElementById('requests-tbody').textContent)
.toContain(hostile);
const detailsLink = document.querySelector('#requests-tbody a');
expect(detailsLink.getAttribute('href'))
.toBe(`/details/${encodeURIComponent(hostileRequest.research_id)}`);
});
it('keeps chart tooltips and drill-down navigation bound to the selected point', async () => {
const chartData = [
{
timestamp: '2026-08-01T12:00:00.000Z',
research_id: 'safe/id',
original_prompt_tokens: 1000,
prompt_tokens: 900,
context_limit: 8192,
model: 'openai/gpt-4o',
provider: 'openai',
research_phase: 'search',
response_time_ms: 900,
truncated: false,
},
{
timestamp: '2026-08-02T12:00:00.000Z',
research_id: 'critical/id',
original_prompt_tokens: 7000,
prompt_tokens: 7000,
context_limit: 8192,
model: 'anthropic/claude-3',
provider: 'anthropic',
research_phase: 'synthesis',
response_time_ms: 1500,
tokens_truncated: 100,
truncated: true,
},
{
timestamp: '2026-08-03T12:00:00.000Z',
research_id: 'unknown',
prompt_tokens: null,
context_limit: 4096,
research_phase: null,
response_time_ms: null,
},
];
const fetchMock = vi.fn(() => response(payload({
overview: {
requests_with_context_data: 3,
total_requests: 3,
truncated_requests: 1,
truncation_rate: 33.3,
avg_tokens_truncated: 100,
},
token_summary: { total_requests: 3 },
model_stats: [{
model: 'anthropic/claude-3',
provider: 'anthropic',
total_requests: 2,
truncated_count: 1,
truncation_rate: 50,
avg_context_limit: 8192,
}],
model_token_stats: [{
model: 'anthropic/claude-3',
provider: 'anthropic',
min_prompt: 1000,
avg_prompt: 7000,
max_prompt: 7000,
avg_response_time_ms: 1500,
}],
recent_truncated: [{
...request({ research_id: 'critical/id' }),
tokens_truncated: 100,
}],
chart_data: chartData,
phase_breakdown: [
{ phase: 'search', count: 1, total_tokens: 1000, avg_tokens: 1000 },
{ phase: 'synthesis', count: 1, total_tokens: 7000, avg_tokens: 7000 },
],
current_context_window: 16384,
})));
await importController(fetchMock);
const context = ChartStub.instances.find(chart =>
chart.config.type === 'scatter'
&& chart.config.options.scales.x.type === 'time'
);
const critical = context.data.datasets.find(dataset =>
dataset.label.startsWith('Critical')
).data[0];
const contextLabel = context.config.options.plugins.tooltip.callbacks.label;
expect(contextLabel({ raw: critical }))
.toContain('7,000 prompt tokens / 8,192 limit (85%)');
expect(contextLabel({ raw: critical }))
.toContain('lost 100');
const unknown = context.data.datasets.find(dataset =>
dataset.label.startsWith('Caution')
).data[0];
expect(contextLabel({ raw: unknown })).toContain('prompt tokens unknown');
context.config.options.onClick(null, [{ datasetIndex: 2, index: 0 }]);
expect(URLValidator.safeAssign).toHaveBeenCalledWith(
window.location,
'href',
'/details/critical%2Fid'
);
const latency = ChartStub.instances.find(chart =>
chart.config.type === 'scatter'
&& chart.config.options.scales.x.title?.text === 'Prompt Tokens (input size)'
);
const latencyPoint = latency.data.datasets
.find(dataset => dataset.label.startsWith('Claude')).data[0];
expect(latency.config.options.plugins.tooltip.callbacks.label({
raw: latencyPoint,
})).toContain('7,000 tokens → 1.5s (85% of limit) [truncated]');
expect(latency.config.options.scales.y.ticks.callback(1500)).toBe('1.5s');
expect(latency.config.options.scales.y.ticks.callback(900)).toBe('900ms');
const criticalDatasetIndex = latency.data.datasets.findIndex(dataset =>
dataset.label.startsWith('Claude')
);
latency.config.options.onClick(null, [{
datasetIndex: criticalDatasetIndex,
index: 0,
}]);
expect(URLValidator.safeAssign).toHaveBeenLastCalledWith(
window.location,
'href',
'/details/critical%2Fid'
);
const phase = ChartStub.instances.find(chart => chart.config.type === 'bar');
const drawContext = {
save: vi.fn(),
restore: vi.fn(),
beginPath: vi.fn(),
moveTo: vi.fn(),
lineTo: vi.fn(),
stroke: vi.fn(),
fillRect: vi.fn(),
strokeRect: vi.fn(),
};
phase.config.plugins[0].afterDatasetsDraw({
ctx: drawContext,
getDatasetMeta: () => ({ data: [{ x: 20 }, { x: 100 }] }),
scales: { y: { getPixelForValue: value => 300 - value / 50 } },
});
expect(drawContext.fillRect).toHaveBeenCalledTimes(2);
expect(phase.config.options.plugins.tooltip.callbacks.label({
raw: { y: 7000, phase: 'synthesis' },
})).toBe('7,000 prompt tokens · synthesis');
});
it('shows useful no-series states and handles a non-success API payload', async () => {
const fetchMock = vi.fn()
.mockImplementationOnce(() => response(payload({
overview: {
requests_with_context_data: 1,
total_requests: 1,
truncated_requests: 0,
},
model_stats: [],
chart_data: [],
})))
.mockImplementationOnce(() => response({
status: 'error',
message: 'backend unavailable',
}));
const controller = await importController(fetchMock);
expect(document.getElementById('empty-no-truncation').style.display)
.toBe('block');
expect(document.getElementById('model-stats').textContent)
.toContain('No model data available');
expect(document.getElementById('truncated-list').textContent)
.toContain('No truncated requests found');
expect(document.getElementById('phase-breakdown-container').textContent)
.toContain('No phase data available yet');
expect(ChartStub.instances.some(chart =>
chart.config.options.plugins.title?.text === 'No context data available yet'
)).toBe(true);
expect(ChartStub.instances.some(chart =>
chart.config.options.plugins.title?.text === 'No latency data available yet'
)).toBe(true);
await controller.loadContextData('all', 1);
expect(document.getElementById('content').textContent)
.toContain('Error loading token usage data');
});