fix(server): stream Ollama model pulls so long downloads finish

A non-streaming /api/pull holds the response headers until the download
ends, and Node fetch gives up on headers after five minutes, cancelling
every larger model pull. Request stream:true and read the NDJSON progress,
failing on an in-stream error or a missing success status (UF-008).
This commit is contained in:
shumeiko.it
2026-10-15 18:13:34 +02:00
parent 15e5bbe3f6
commit 3e1a188c03
2 changed files with 107 additions and 2 deletions

View File

@@ -732,6 +732,70 @@ describe('local-inference route — verified model installation', () => {
}
});
it('streams the pull so downloads longer than the fetch header timeout survive', async () => {
// With stream:false Ollama sends no response headers until the download
// finishes, and Node's fetch gives up after 300 s without headers, which
// cancelled every pull longer than five minutes.
const fetchMock = vi.fn(async (input: string | URL | Request) => {
const url = String(input);
if (url.endsWith('/api/pull')) {
return new Response([
JSON.stringify({ status: 'pulling manifest' }),
JSON.stringify({ status: 'pulling sha256:abc', total: 100, completed: 50 }),
JSON.stringify({ status: 'success' }),
'',
].join('\n'), { status: 200, headers: { 'content-type': 'application/x-ndjson' } });
}
return new Response(JSON.stringify({ response: 'OK', done: true }), { status: 200 });
});
vi.stubGlobal('fetch', fetchMock);
const server = await buildPullServer(['qwen3:1.7b']);
try {
const response = await server.inject({
method: 'POST',
url: '/api/local-inference/pull',
payload: { model: 'qwen3:1.7b' },
});
expect(JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body))).toMatchObject({
name: 'qwen3:1.7b',
stream: true,
});
expect(response.statusCode).toBe(200);
expect(response.json()).toMatchObject({
ok: true,
model: 'qwen3:1.7b',
status: { status: 'success' },
verifiedGeneration: true,
});
} finally {
await server.close();
vi.unstubAllGlobals();
}
});
it('fails the pull when Ollama reports an error inside the progress stream', async () => {
const fetchMock = vi.fn().mockResolvedValueOnce(new Response([
JSON.stringify({ status: 'pulling manifest' }),
JSON.stringify({ error: 'pull model manifest: file does not exist' }),
'',
].join('\n'), { status: 200, headers: { 'content-type': 'application/x-ndjson' } }));
vi.stubGlobal('fetch', fetchMock);
const server = await buildPullServer([]);
try {
const response = await server.inject({
method: 'POST',
url: '/api/local-inference/pull',
payload: { model: 'qwen3:1.7b' },
});
expect(response.statusCode).toBe(502);
expect(response.json().error).toContain('pull model manifest: file does not exist');
expect(fetchMock).toHaveBeenCalledTimes(1);
} finally {
await server.close();
vi.unstubAllGlobals();
}
});
it('canonicalizes the bare digest advertised by the live Ollama tag API', async () => {
const wireDigest = 'B'.repeat(64);
const digest = `sha256:${'b'.repeat(64)}`;