Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(llamacpp): join the thread cache dir with the provider path's separator
Akarshan Biswas2632834b8a5b
diff --git a/extensions/llamacpp-extension/src/preset.test.ts b/extensions/llamacpp-extension/src/preset.test.tsindex 8bdbe7877..9cb106e15 100644--- a/extensions/llamacpp-extension/src/preset.test.ts+++ b/extensions/llamacpp-extension/src/preset.test.ts@@ -810,3 +810,53 @@ describe('generatePreset spec type', () => {)})})++describe('threadCacheDir separator handling', () => {+ const WIN_EXT = '\\\\?\\C:\\Users\\u\\AppData\\Roaming\\Jan-nightly\\data\\llamacpp'++ it('joins a POSIX path with a forward slash', () => {+ expect(threadCacheDir('/p')).toBe('/p/thread-cache')+ expect(threadCacheDir('/home/u/.local/share/Jan/data/llamacpp')).toBe(+ '/home/u/.local/share/Jan/data/llamacpp/thread-cache'+ )+ })++ it('joins a Windows extended path with a backslash', () => {+ // Win32 normalizes `/` to `\` for ordinary paths but not for `\\?\` ones,+ // where a `/` is a literal name character -- so create_dir_all fails with+ // ERROR_INVALID_NAME (os error 123) and the worker exits 7 before serving.+ expect(threadCacheDir(WIN_EXT)).toBe(`${WIN_EXT}\\thread-cache`)+ expect(threadCacheDir(WIN_EXT)).not.toContain('/')+ })++ it('joins a plain Windows path with a backslash', () => {+ expect(threadCacheDir('C:\\Users\\u\\llamacpp')).toBe(+ 'C:\\Users\\u\\llamacpp\\thread-cache'+ )+ })++ it('does not read a backslash in a POSIX name as a Windows separator', () => {+ // `\` is a legal filename character on Linux, so the separator follows the+ // path's root rather than whether a backslash appears anywhere in it.+ expect(threadCacheDir('/home/u/od\\d/llamacpp')).toBe(+ '/home/u/od\\d/llamacpp/thread-cache'+ )+ })++ it('does not double an existing trailing separator', () => {+ expect(threadCacheDir('/p/')).toBe('/p/thread-cache')+ expect(threadCacheDir('C:\\p\\')).toBe('C:\\p\\thread-cache')+ })++ it('is the one definition the preset and the erase path both use', async () => {+ // index.ts's forgetThreadCache passes threadCacheDir(providerPath) as+ // cacheDir, and llama.cpp joins state file names onto slot-save-path, so a+ // preset that derived the directory differently would erase from a+ // directory the worker never wrote to.+ await generatePreset(WIN_EXT, 'C:\\jan', CONFIG)+ const ini = Object.entries(writtenFiles).find(([p]) =>+ p.endsWith('router.preset.ini')+ )?.[1] as string+ expect(ini).toContain(`slot-save-path = ${threadCacheDir(WIN_EXT)}`)+ })+})diff --git a/extensions/llamacpp-extension/src/preset.ts b/extensions/llamacpp-extension/src/preset.tsindex 006728e78..142471894 100644--- a/extensions/llamacpp-extension/src/preset.ts+++ b/extensions/llamacpp-extension/src/preset.ts@@ -87,7 +87,22 @@ export const DEFAULT_EMBEDDING_UBATCH = 2048* definition.*/export function threadCacheDir(providerPath: string): string {- return `${providerPath}/thread-cache`+ const sep = isWindowsPath(providerPath) ? '\\' : '/'+ return `${providerPath.replace(/[\\/]+$/, '')}${sep}thread-cache`+}++/**+ * Whether a path is rooted the Windows way: a drive letter, a UNC share, or an+ * extended `\\?\` prefix.+ *+ * Tested on the root rather than on "contains a backslash" because `\` is a+ * legal filename character on Linux. Win32 normalizes `/` to `\` for the first+ * two, but *not* for an extended path, where a `/` is a literal name character+ * -- `create_dir_all` then fails with ERROR_INVALID_NAME and the worker exits+ * before it serves anything.+ */+function isWindowsPath(p: string): boolean {+ return /^(?:[A-Za-z]:|\\\\)/.test(p)}// Fallback context size when the user hasn't set one, to avoid loading adiff --git a/extensions/llamacpp-extension/src/test/index.test.ts b/extensions/llamacpp-extension/src/test/index.test.tsindex 4f6affab0..e7439e83e 100644--- a/extensions/llamacpp-extension/src/test/index.test.ts+++ b/extensions/llamacpp-extension/src/test/index.test.ts@@ -41,6 +41,7 @@ vi.mock('@janhq/tauri-plugin-llamacpp-api', async () => {models_max: 1,}),engineDevices: vi.fn().mockResolvedValue([]),+ eraseThreadSlotState: vi.fn().mockResolvedValue(0),}})@@ -1270,3 +1271,44 @@ describe('embedding readiness during the first-run fetch', () => {expect(report.error).toContain('no embedder')})})++describe('forgetThreadCache', () => {+ let extension: llamacpp_extension++ beforeEach(() => {+ vi.clearAllMocks()+ extension = new llamacpp_extension('http://localhost', 'llamacpp')+ })++ afterEach(() => {+ vi.restoreAllMocks()+ })++ // The worker resolves state file names against slot-save-path, so erasing+ // has to name the very directory the preset named. On Windows the provider+ // path is extended (`\\?\C:\...`), where a `/` is a literal name character+ // rather than a separator.+ it('erases from the directory the preset names, on a Windows provider path', async () => {+ const { getJanDataFolderPath, joinPath } = await import('@janhq/core')+ const { eraseThreadSlotState } = await import(+ '@janhq/tauri-plugin-llamacpp-api'+ )+ const { threadCacheDir } = await import('../preset')+ const providerPath = '\\\\?\\C:\\Users\\u\\AppData\\Roaming\\Jan-nightly\\data\\llamacpp'++ vi.mocked(getJanDataFolderPath).mockResolvedValue(+ '\\\\?\\C:\\Users\\u\\AppData\\Roaming\\Jan-nightly\\data'+ )+ vi.mocked(joinPath).mockResolvedValue(providerPath)++ await extension.forgetThreadCache('t1')++ expect(eraseThreadSlotState).toHaveBeenCalledWith({+ threadId: 't1',+ cacheDir: threadCacheDir(providerPath),+ })+ expect(+ vi.mocked(eraseThreadSlotState).mock.calls[0][0].cacheDir+ ).not.toContain('/')+ })+})