Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

20.3 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { BrowserWorkerRequest, BrowserWorkerResponse } from './browser-ai-runtime';
import { BROWSER_MODEL, BROWSER_MODELS, browserModelBaseUrl } from './model-policy';

const fake = vi.hoisted(() => ({
    count: 23,
    template: vi.fn(),
    generate: vi.fn(),
    tokenizerLoad: vi.fn(),
    modelLoad: vi.fn(),
    ngramInputs: [] as bigint[][][],
    env: { remotePathTemplate: '{model}/resolve/{revision}/', useWasmCache: true, fetch: undefined as typeof fetch | undefined, backends: { onnx: { wasm: {} } } },
}));
vi.mock('@huggingface/transformers', () => ({
    env: fake.env,
    AutoTokenizer: { from_pretrained: fake.tokenizerLoad },
    AutoModelForCausalLM: { from_pretrained: fake.modelLoad },
    InterruptableStoppingCriteria: class {
        interrupted = false;
        interrupt() { this.interrupted = true; }
        reset() { this.interrupted = false; }
    },
    TextStreamer: class {
        callback: (chunk: string) => void;
        token: (tokens?: bigint[]) => void;
        constructor(_tokenizer: unknown, options: { callback_function: (chunk: string) => void; token_callback_function: (tokens?: bigint[]) => void }) {
            this.callback = options.callback_function; this.token = options.token_callback_function;
        }
    },
    NoRepeatNGramLogitsProcessor: class {
        constructor(public no_repeat_ngram_size: number) {}
        _call(inputIds: bigint[][], logits: unknown) { fake.ngramInputs.push(inputIds); return logits; }
    },
    LogitsProcessorList: class {
        processors: unknown[] = [];
        push(item: unknown) { this.processors.push(item); }
    },
}));

let scope: { location: { href: string }; postMessage: ReturnType<typeof vi.fn>; onmessage?: (event: MessageEvent<BrowserWorkerRequest>) => Promise<void> };
let fetchMock: ReturnType<typeof vi.fn>;
const send = (request: BrowserWorkerRequest) => scope.onmessage!({ data: request } as MessageEvent<BrowserWorkerRequest>);
const replies = (): BrowserWorkerResponse[] => scope.postMessage.mock.calls.map(call => call[0]);
beforeEach(async () => {
    vi.resetModules(); vi.clearAllMocks(); fake.count = 23; fake.ngramInputs = [];
    fake.env.useWasmCache = true; // Transformers.js 4 default in browsers with Cache Storage.
    fake.env.remotePathTemplate = '{model}/resolve/{revision}/';
    delete (fake.env.backends.onnx as { webgpu?: unknown }).webgpu;
    fake.template.mockImplementation(() => ({ input_ids: { dims: [1, fake.count], data: new BigInt64Array(fake.count) }, attention_mask: { dims: [1, fake.count] } }));
    fake.tokenizerLoad.mockResolvedValue({ apply_chat_template: fake.template });
    fake.modelLoad.mockResolvedValue({ generate: fake.generate });
    fake.generate.mockImplementation(async ({ streamer }: { streamer: { callback: (chunk: string) => void } }) => { streamer.callback('A '); streamer.callback('reply.'); });
    scope = { location: { href: 'http://localhost/worker.js' }, postMessage: vi.fn() };
    fetchMock = vi.fn(async () => ({ url: browserModelBaseUrl(BROWSER_MODELS[0]) + 'config.json' }));
    fake.env.fetch = fetchMock as unknown as typeof fetch; // v4 snapshots native fetch during import.
    vi.stubGlobal('self', scope);
    vi.stubGlobal('fetch', fetchMock);
    vi.stubGlobal('navigator', { userAgent: 'CBM fixture Chrome', vendor: '', gpu: { requestAdapter: async () => ({ features: { has: () => true } }) } });
    vi.stubGlobal('caches', { open: vi.fn(async () => ({})) });
    await import('./browser-ai.worker');
});
afterEach(() => vi.unstubAllGlobals());

describe('browser generation worker', () => {
    it('marks GPU generation errors fatal and invalidates the model before any later request', async () => {
        await send({ id: 1, kind: 'prepare' });
        const detail = 'OrtRun failed: GPUBuffer mapAsync rejected because the buffer is invalid';
        fake.generate.mockRejectedValueOnce(new Error(detail));
        await send({ id: 2, kind: 'chat', messages: [{ role: 'user', content: 'Explain' }] });
        expect(replies().at(-1)).toMatchObject({ id: 2, kind: 'error', error: detail, fatal: true });
        await send({ id: 3, kind: 'chat', messages: [{ role: 'user', content: 'Retry' }] });
        expect(fake.generate).toHaveBeenCalledOnce();
        expect(replies().at(-1)).toMatchObject({ id: 3, kind: 'error', fatal: true });
        await send({ id: 4, kind: 'prepare' });
        expect(fake.modelLoad).toHaveBeenCalledOnce();
        expect(replies().at(-1)).toMatchObject({ id: 4, kind: 'error', fatal: true });
    });

    it('reports loss of the actual inference device while idle and rejects future preparation', async () => {
        let lose!: (reason: { reason: string; message: string }) => void;
        const lost = new Promise<{ reason: string; message: string }>(resolve => { lose = resolve; });
        Object.assign(fake.env.backends.onnx, { webgpu: { device: Promise.resolve({ lost }) } });
        await send({ id: 1, kind: 'prepare' });
        lose({ reason: 'unknown', message: 'GPU device disconnected' });
        await Promise.resolve(); await Promise.resolve();
        expect(replies().at(-1)).toMatchObject({ kind: 'error', fatal: true });
        expect(replies().at(-1)?.error).toContain('GPU device disconnected');
        await send({ id: 2, kind: 'prepare' });
        expect(fake.modelLoad).toHaveBeenCalledOnce();
        expect(replies().at(-1)).toMatchObject({ id: 2, kind: 'error', fatal: true });
    });

    it('invalidates a raw non-Error failure from the GPU inference call without losing its detail', async () => {
        await send({ id: 1, kind: 'prepare' });
        fake.generate.mockRejectedValueOnce('mapAsync: invalid GPUBuffer');
        await send({ id: 2, kind: 'chat', messages: [{ role: 'user', content: 'Explain' }] });
        expect(replies().at(-1)).toMatchObject({ id: 2, kind: 'error', error: 'mapAsync: invalid GPUBuffer', fatal: true });
    });

    it('uses a validated smaller output reserve for automatic explanations and rejects invalid limits', async () => {
        await send({ id: 1, kind: 'prepare' });
        fake.count = 7800;
        const messages = [{ role: 'user' as const, content: 'Explain' }];
        await send({ id: 2, kind: 'chat', messages, ...{ maxOutputTokens: 192 } });
        expect(fake.generate).toHaveBeenCalledWith(expect.objectContaining({ max_new_tokens: 192 }));
        expect(replies().at(-1)).toMatchObject({ id: 2, kind: 'answer' });
        fake.generate.mockClear();
        for (const maxOutputTokens of [0, -1, 513, 1.5, Number.NaN]) {
            await send({ id: 3, kind: 'chat', messages, ...{ maxOutputTokens } });
            expect(replies().at(-1)?.error).toContain('output token limit');
        }
        expect(fake.generate).not.toHaveBeenCalled();
    });

    it('applies repetition controls to automatic explanations and manual chat, checking n-grams against the answer only', async () => {
        await send({ id: 1, kind: 'prepare' });
        const messages = [{ role: 'user' as const, content: 'Explain' }];
        type Processor = { no_repeat_ngram_size: number; _call(ids: bigint[][], logits: unknown): unknown };
        const processor = () => (fake.generate.mock.calls.at(-1)![0] as { logits_processor: { processors: Processor[] } }).logits_processor.processors;
        await send({ id: 2, kind: 'chat', messages, generationProfile: 'automatic-explanation' });
        expect(fake.generate).toHaveBeenLastCalledWith(expect.objectContaining({ max_new_tokens: 128, do_sample: false, repetition_penalty: 1.1 }));
        expect(processor().map(item => item.no_repeat_ngram_size)).toEqual([6]);
        expect(replies().at(-1)).toMatchObject({ id: 2, kind: 'answer', output: 'A reply.' });
        await send({ id: 3, kind: 'chat', messages });
        const ordinary = fake.generate.mock.calls.at(-1)![0];
        expect(ordinary).toMatchObject({ max_new_tokens: 512, do_sample: false, repetition_penalty: 1.1 });
        // Built-in n-gram blocking would also count the prompt and garble quoted names.
        expect(ordinary).not.toHaveProperty('no_repeat_ngram_size');
        expect(processor().map(item => item.no_repeat_ngram_size)).toEqual([20]);
        const prompt = Array.from({ length: fake.count }, (_, index) => BigInt(index));
        processor()[0]._call([[...prompt, 7n, 8n, 9n]], 'logits');
        expect(fake.ngramInputs.at(-1)).toEqual([[7n, 8n, 9n]]);
    });

    it('reports whether an answer ended at the output limit, at the end of sequence, or on Stop', async () => {
        fake.modelLoad.mockResolvedValue({ generate: fake.generate, generation_config: { eos_token_id: [2, 3] } });
        await send({ id: 1, kind: 'prepare' });
        const messages = [{ role: 'user' as const, content: 'List' }];
        const emit = (tokens: number, last = 7n) => async ({ streamer }: { streamer: { callback: (chunk: string) => void; token: (tokens?: bigint[]) => void } }) => {
            for (let index = 0; index < tokens; index++) { streamer.token([index + 1 === tokens ? last : 9n]); streamer.callback('x'); }
        };
        fake.generate.mockImplementationOnce(emit(64));
        await send({ id: 2, kind: 'chat', messages, maxOutputTokens: 64 });
        expect(replies().at(-1)).toEqual({ id: 2, kind: 'answer', output: 'x'.repeat(64), stopReason: 'length' });
        fake.generate.mockImplementationOnce(emit(10));
        await send({ id: 3, kind: 'chat', messages, maxOutputTokens: 64 });
        expect(replies().at(-1)).toEqual({ id: 3, kind: 'answer', output: 'x'.repeat(10), stopReason: 'eos' });
        // The streamer also receives the end-of-sequence token; ending on the last allowed token is not a cut.
        fake.generate.mockImplementationOnce(emit(64, 3n));
        await send({ id: 4, kind: 'chat', messages, maxOutputTokens: 64 });
        expect(replies().at(-1)).toEqual({ id: 4, kind: 'answer', output: 'x'.repeat(64), stopReason: 'eos' });
    });

    it('rejects unknown profiles and oversized automatic answers without invalidating the model', async () => {
        await send({ id: 1, kind: 'prepare' });
        const messages = [{ role: 'user' as const, content: 'Explain' }];
        for (const profile of ['unknown', '', null, {}]) {
            await send({ id: 2, kind: 'chat', messages, generationProfile: profile as BrowserWorkerRequest['generationProfile'] });
            expect(replies().at(-1)).toEqual({ id: 2, kind: 'error', error: 'Invalid generation profile.' });
        }
        await send({ id: 3, kind: 'chat', messages, generationProfile: 'automatic-explanation', maxOutputTokens: 129 });
        expect(replies().at(-1)).toEqual({ id: 3, kind: 'error', error: 'Invalid output token limit. Use an integer from 1 to 128.' });
        expect(fake.generate).not.toHaveBeenCalled();
        await send({ id: 4, kind: 'chat', messages, generationProfile: 'automatic-explanation', maxOutputTokens: 64 });
        expect(fake.generate).toHaveBeenCalledOnce();
        expect(fake.generate).toHaveBeenLastCalledWith(expect.objectContaining({ max_new_tokens: 64 }));
        expect(replies().at(-1)).toMatchObject({ id: 4, kind: 'answer' });
    });

    it('reserves the automatic profile token ceiling when checking the context boundary', async () => {
        await send({ id: 1, kind: 'prepare' });
        const messages = [{ role: 'user' as const, content: 'Explain' }];
        fake.count = 8064;
        await send({ id: 2, kind: 'chat', messages, generationProfile: 'automatic-explanation' });
        expect(replies().at(-1)).toMatchObject({ id: 2, kind: 'answer' });
        fake.generate.mockClear(); fake.count = 8065;
        await send({ id: 3, kind: 'chat', messages, generationProfile: 'automatic-explanation' });
        expect(fake.generate).not.toHaveBeenCalled();
        expect(replies().at(-1)?.error).toContain('128 reserved for the answer');
    });

    it('pins the real upstream tokenizer metadata probe even when it drops the revision option', async () => {
        const selected = BROWSER_MODELS[0];
        const allowed = browserModelBaseUrl(selected) + 'tokenizer_config.json';
        vi.stubGlobal('caches', { open: vi.fn(async () => ({ match: async () => undefined, put: async () => {} })) });
        fetchMock.mockResolvedValue({ url: allowed, status: 206, headers: new Headers({ 'content-range': 'bytes 0-0/100' }) });
        fake.tokenizerLoad.mockImplementationOnce(async () => {
            // Exercise the installed v4 metadata implementation, not a duplicate URL builder.
            // @ts-expect-error Upstream exposes declarations only through its package entry point.
            const { env: upstreamEnv } = await import('../../node_modules/@huggingface/transformers/src/env.js');
            Object.assign(upstreamEnv, fake.env);
            // @ts-expect-error Upstream exposes declarations only through its package entry point.
            const { get_tokenizer_files } = await import('../../node_modules/@huggingface/transformers/src/utils/model_registry/get_tokenizer_files.js');
            const files = await get_tokenizer_files(selected.id); // This API has no revision argument.
            expect(files).toEqual(['tokenizer.json', 'tokenizer_config.json']);
            return { apply_chat_template: fake.template };
        });
        await send({ id: 1, kind: 'prepare', modelId: selected.id });
        expect(replies().at(-1)).toEqual({ id: 1, kind: 'ready' });
        expect(fetchMock).toHaveBeenCalledExactlyOnceWith(allowed, expect.objectContaining({ method: 'GET', credentials: 'omit' }));
    });

    it('routes Transformers own fetch through the pinned model download boundary', async () => {
        const selected = BROWSER_MODELS[1];
        const allowed = browserModelBaseUrl(selected) + 'config.json';
        fake.tokenizerLoad.mockImplementationOnce(async () => {
            await expect(fake.env.fetch!(allowed, { method: 'POST', body: 'private' })).rejects.toThrow('outside');
            await expect(fake.env.fetch!(browserModelBaseUrl(BROWSER_MODELS[0]) + 'config.json')).rejects.toThrow('outside');
            await fake.env.fetch!(allowed);
            return { apply_chat_template: fake.template };
        });
        await send({ id: 1, kind: 'prepare', modelId: selected.id });
        expect(replies().at(-1)).toEqual({ id: 1, kind: 'ready' });
        expect(fetchMock).toHaveBeenCalledExactlyOnceWith(allowed, expect.objectContaining({ credentials: 'omit' }));
        await expect(fake.env.fetch!(allowed)).rejects.toThrow('outside');
    });

    it('loads from the cache only, without one network request, when the chat resumes a cached model (K10, K24)', async () => {
        const allowed = browserModelBaseUrl(BROWSER_MODELS[0]) + 'config.json';
        fake.tokenizerLoad.mockImplementationOnce(async () => {
            await expect(fake.env.fetch!(allowed)).rejects.toThrow('outside');
            return { apply_chat_template: fake.template };
        });
        await send({ id: 1, kind: 'prepare', cacheOnly: true });
        expect(replies().at(-1)).toEqual({ id: 1, kind: 'ready' });
        expect(fetchMock).not.toHaveBeenCalled();
    });

    it('keeps the bundled runtime module at a direct same-origin URL under restrictive CSP', async () => {
        fake.modelLoad.mockImplementationOnce(async () => {
            const wasm = fake.env.backends.onnx.wasm as { wasmPaths: { mjs: string; wasm: string }; proxy: boolean };
            // Upstream v4 turns wasmPaths.mjs into a blob URL when useWasmCache is enabled.
            // Product CSP permits the local module, but intentionally does not permit blob scripts.
            expect(fake.env.useWasmCache).toBe(false);
            expect(new URL(wasm.wasmPaths.mjs).origin).toBe(new URL(scope.location.href).origin);
            expect(new URL(wasm.wasmPaths.wasm).origin).toBe(new URL(scope.location.href).origin);
            expect(wasm.wasmPaths.mjs).toContain('.mjs');
            expect(wasm.wasmPaths.wasm).toContain('.wasm');
            expect(wasm.proxy).toBe(false);
            return { generate: fake.generate };
        });
        await send({ id: 1, kind: 'prepare' });
        expect(replies().at(-1)).toEqual({ id: 1, kind: 'ready' });
    });

    it('requires explicit prepare and WebGPU before accessing model files', async () => {
        expect(fake.modelLoad).not.toHaveBeenCalled();
        await send({ id: 1, kind: 'chat', messages: [{ role: 'user', content: 'hello' }] });
        expect(replies().at(-1)?.error).toContain('Load a browser model');
        vi.stubGlobal('navigator', {});
        await send({ id: 2, kind: 'prepare' });
        expect(replies().at(-1)?.error).toContain('WebGPU');
        expect(fake.tokenizerLoad).not.toHaveBeenCalled();
        expect(fetchMock).not.toHaveBeenCalled();
    });

    it('uses exact template tokens for counting and generation with no truncation and output reserve', async () => {
        await send({ id: 1, kind: 'prepare' });
        const messages = [{ role: 'user' as const, content: '\t' + 'x'.repeat(7000) + '\r\n  ' }];
        fake.count = 7680;
        await send({ id: 2, kind: 'count', messages });
        expect(replies().at(-1)).toEqual({ id: 2, kind: 'count', count: 7680 });
        await send({ id: 3, kind: 'chat', messages });
        expect(fake.template).toHaveBeenLastCalledWith(messages, expect.objectContaining({ truncation: false, enable_thinking: false, add_generation_prompt: true }));
        expect(fake.generate).toHaveBeenCalledWith(expect.objectContaining({ input_ids: expect.objectContaining({ dims: [1, 7680] }), max_new_tokens: 512, do_sample: false }));
        expect(replies().filter(reply => reply.kind === 'token').map(reply => reply.output)).toEqual(['A ', 'reply.']);
        expect(replies().at(-1)).toEqual({ id: 3, kind: 'answer', output: 'A reply.', stopReason: 'eos' });
        fake.count = 7681; fake.generate.mockClear();
        await send({ id: 4, kind: 'chat', messages });
        expect(fake.generate).not.toHaveBeenCalled();
        expect(replies().at(-1)?.error).toContain('no code was truncated');
    });

    it('interrupts the active generation and reuses the loaded model for the next turn', async () => {
        await send({ id: 1, kind: 'prepare' });
        let complete!: () => void;
        let criteria!: { interrupted: boolean };
        fake.generate.mockImplementationOnce(({ streamer, stopping_criteria }: { streamer: { callback: (chunk: string) => void }; stopping_criteria: { interrupted: boolean }[] }) => {
            streamer.callback('Partial'); criteria = stopping_criteria[0];
            return new Promise<void>(resolve => { complete = resolve; });
        });
        const generating = send({ id: 2, kind: 'chat', messages: [{ role: 'user', content: 'first' }] });
        await send({ id: 1, kind: 'stop' }); expect(criteria.interrupted).toBe(false);
        await send({ id: 2, kind: 'stop' }); expect(criteria.interrupted).toBe(true);
        complete(); await generating;
        expect(replies().at(-1)).toEqual({ id: 2, kind: 'answer', output: 'Partial', stopReason: 'interrupted' });
        await send({ id: 3, kind: 'chat', messages: [{ role: 'user', content: 'second' }] });
        expect(fake.modelLoad).toHaveBeenCalledOnce();
        expect(criteria.interrupted).toBe(false);
        expect(replies().at(-1)?.output).toBe('A reply.');
    });

    it('pins model loading and permits only selected model GETs during preparation', async () => {
        const selected = BROWSER_MODELS[2];
        const allowed = browserModelBaseUrl(selected) + 'config.json';
        await expect(fetch(allowed)).rejects.toThrow('outside');
        fake.tokenizerLoad.mockImplementationOnce(async () => {
            await expect(fetch(allowed, { method: 'POST', body: 'private' })).rejects.toThrow('outside');
            await expect(fetch(browserModelBaseUrl(BROWSER_MODELS[0]) + 'config.json')).rejects.toThrow('outside');
            await fetch(allowed);
            return { apply_chat_template: fake.template };
        });
        await send({ id: 1, kind: 'prepare', modelId: selected.id });
        expect(fake.modelLoad).toHaveBeenCalledWith(selected.id, expect.objectContaining({ revision: selected.revision, dtype: 'q4f16', device: 'webgpu' }));
        expect(fetchMock).toHaveBeenCalledExactlyOnceWith(allowed, expect.objectContaining({ credentials: 'omit', cache: 'no-store' }));
        await expect(fetch(allowed)).rejects.toThrow('outside');
        expect(fake.env).toMatchObject({ allowLocalModels: false, useCustomCache: true, useBrowserCache: false });
        expect(selected.id).not.toBe(BROWSER_MODEL.id);
    });
});