Skip to content

Private AI meeting notes that stay on your computer. No meeting bot. Free and open source for Mac, Windows and Linux. An alternative to Granola, Otter and Fireflies.

22.2 KB
Raw Blame
import { chmodSync, existsSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
import { availableParallelism, tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createWhisperServer, killStale, recoverer } from '../src/main/asr/server'
import { createTranscriber } from '../src/main/asr/transcribe'

/**
 * a stand-in for the whisper-server program: answers GET / on --host/--port once it has "loaded".
 * modes: what each launch does with a request to /inference (the last one repeats): ok, crash (exits), wedge (never answers),
 * slow (answers after 800 ms).
 */
function fakeBinary(loadMs = 0, modes: string[] = ['ok']) {
  const dir = mkdtempSync(join(tmpdir(), 'yogurt-ws-'))
  const js = join(dir, 'ws.js')
  writeFileSync(join(dir, 'modes'), modes.join('\n'))
  writeFileSync(js, `
    const fs = require('fs'), dir = ${JSON.stringify(dir)}
    fs.appendFileSync(dir + '/launches', String(process.pid) + '\\n')
    fs.writeFileSync(dir + '/args', JSON.stringify(process.argv.slice(2)))
    const n = fs.readFileSync(dir + '/launches', 'utf8').trim().split('\\n').length
    const ms = fs.readFileSync(dir + '/modes', 'utf8').split('\\n'), mode = ms[Math.min(n, ms.length) - 1]
    const a = process.argv, port = Number(a[a.indexOf('--port') + 1]), host = a[a.indexOf('--host') + 1]
    process.stderr.write('whisper_backend_init_gpu: using Vulkan0 backend\\n')
    if (mode === 'die-loading') process.exit(1)
    setTimeout(() => require('http').createServer((q, r) => {
      if (q.method !== 'POST') return r.end('ok')
      q.resume()
      if (mode === 'crash') process.exit(1)
      if (mode === 'wedge') return
      const ans = () => r.end(JSON.stringify({ detected_language: 'english', detected_language_probability: 0.99, segments: [{ start: 0, end: 1, text: ' Fine words here today' }] }))
      q.on('end', () => (mode === 'slow' ? setTimeout(ans, 800) : ans()))
    }).listen(port, host), ${loadMs})
  `)
  const bin = join(dir, 'whisper-server')
  writeFileSync(bin, `#!/bin/sh\nexec "${process.execPath}" "${js}" "$@"\n`)
  chmodSync(bin, 0o755)
  return bin
}
const launchPids = (bin: string): number[] => { try { return readFileSync(join(bin, '../launches'), 'utf8').trim().split('\n').map(Number) } catch { return [] } }
const isAlive = (pid: number) => { try { process.kill(pid, 0); return true } catch { return false } }

const open: Array<{ stop(): Promise<void> }> = []
afterEach(async () => { await Promise.all(open.splice(0).map((s) => s.stop())) })
const make = (bin: string, extra: Partial<Parameters<typeof createWhisperServer>[0]> = {}) => {
  const s = createWhisperServer({ binary: bin, model: '/m.bin', backoffMs: [0, 0, 0], ...extra })
  open.push(s)
  return s
}
const alive = async (url: string) => { try { return (await fetch(url + '/')).ok } catch { return false } }

// the stand-in servers are shell scripts, which Windows can't start
describe.skipIf(process.platform === 'win32')('whisper server', () => {
  it('starts on first use on 127.0.0.1 and answers; asking again reuses it', async () => {
    const s = make(fakeBinary(100))
    expect(s.running).toBe(false)
    const [a, b] = await Promise.all([s.url(), s.url()])
    expect(a).toBe(b)
    expect(a).toMatch(/^http:\/\/127\.0\.0\.1:\d+$/)
    expect(await alive(a)).toBe(true)
    expect(await s.url()).toBe(a)
  })

  it('turns on VAD with the bundled model when there is one, so whisper only reads the stretches with speech', async () => {
    const bin = fakeBinary()
    const args = () => JSON.parse(readFileSync(join(bin, '../args'), 'utf8')) as string[]
    const s = make(bin, { vadModel: '/vad.bin' })
    await s.url()
    expect(args()).toEqual(expect.arrayContaining(['--vad', '-vm', '/vad.bin']))
    await s.stop()
    const t = make(bin)
    await t.url()
    expect(args()).not.toContain('--vad')
  })

  it('brings up a new one when the old one was killed', async () => {
    const s = make(fakeBinary())
    const a = await s.url()
    s.kill()
    await new Promise((r) => setTimeout(r, 200))
    expect(await alive(a)).toBe(false)
    const b = await s.url()
    expect(await alive(b)).toBe(true)
  })

  it('stop ends the process', async () => {
    const s = make(fakeBinary())
    const a = await s.url()
    await s.stop()
    expect(s.running).toBe(false)
    expect(await alive(a)).toBe(false)
  })

  it('fails with a clear error when the program is missing', async () => {
    await expect(make('/nonexistent/whisper-server').url()).rejects.toThrow(/whisper-server/)
  })

  it('fails when the program exits before it answers', async () => {
    const dir = mkdtempSync(join(tmpdir(), 'yogurt-ws-'))
    const bin = join(dir, 'whisper-server')
    writeFileSync(bin, '#!/bin/sh\nexit 3\n'); chmodSync(bin, 0o755)
    await expect(make(bin).url()).rejects.toThrow(/exited/)
  })

  it('off a Mac, a server that crashes while loading (a graphics driver) starts again without the graphics card, and stays so', async () => {
    const bin = fakeBinary(0, ['die-loading', 'ok'])
    const args = () => JSON.parse(readFileSync(join(bin, '../args'), 'utf8')) as string[]
    const s = make(bin, { gpuFallback: true })
    await expect(s.url()).rejects.toThrow(/exited/)
    expect(args()).not.toContain('-ng')
    await s.url()
    expect(args()).toContain('-ng')
    s.kill()
    await new Promise((r) => setTimeout(r, 200))
    await s.url()
    expect(args()).toContain('-ng')
  })

  // #127: quitting killed a whisper-server still loading; that kill was taken for a graphics driver crash
  it('killed by Yogurt while loading (quitting) is not a graphics card failure', async () => {
    const bin = fakeBinary(1500)
    const args = () => JSON.parse(readFileSync(join(bin, '../args'), 'utf8')) as string[]
    const off = vi.fn()
    const errors = vi.spyOn(console, 'error').mockImplementation(() => {})
    const s = make(bin, { gpuFallback: true, onGpuOff: off })
    const loading = s.url()
    await vi.waitFor(() => expect(launchPids(bin)).toHaveLength(1))
    s.kill()
    await expect(loading).rejects.toThrow(/exited/)
    expect(off).not.toHaveBeenCalled()
    expect(s.cpuOnly).toBe(false)
    expect(errors.mock.calls.flat().join(' ')).not.toMatch(/without the graphics card/)
    errors.mockRestore()
    await s.url()
    expect(args()).not.toContain('-ng')
  })

  // #76: a GTX 1050 Ti under Vulkan loaded fine but never answered; three relaunches on the card, then empty transcripts
  it('off a Mac, a server that hangs on the graphics card twice starts again without it, with its relaunches back', async () => {
    const bin = fakeBinary(0, ['wedge', 'wedge', 'ok'])
    const args = () => JSON.parse(readFileSync(join(bin, '../args'), 'utf8')) as string[]
    const off = vi.fn()
    const s = make(bin, { gpuFallback: true, onGpuOff: off, backoffMs: [0, 0] })
    const first = await s.url()
    expect(args()).not.toContain('-ng')
    const second = await s.wedged(first)
    expect(args()).not.toContain('-ng')
    await s.wedged(second)
    expect(args()).toContain('-ng')
    expect(s.cpuOnly).toBe(true)
    expect(off).toHaveBeenCalledTimes(1)
    // the processor gets the meeting's relaunches afresh
    const third = await s.url()
    await expect(s.wedged(third)).resolves.toMatch(/^http/)
  })
  // #92 (Adreno), #76 (Radeon RX 9070 XT): whisper loaded on the graphics card and answered every request with no words
  it('off a Mac, a server whose graphics card hears no words in a known clip starts again without it, before any meeting uses it', async () => {
    const bin = fakeBinary()
    const args = () => JSON.parse(readFileSync(join(bin, '../args'), 'utf8')) as string[]
    const off = vi.fn()
    const checked: boolean[] = []
    const s = make(bin, { gpuFallback: true, onGpuOff: off, checkGpu: async () => { const ok = checked.length > 0; checked.push(ok); return ok } })
    const url = await s.url()
    expect(args()).toContain('-ng')
    expect(s.cpuOnly).toBe(true)
    expect(off).toHaveBeenCalledTimes(1)
    // checked once, on the graphics card; the processor isn't checked
    expect(checked).toEqual([false])
    expect(await alive(url)).toBe(true)
  })
  it('a graphics card that hears the clip keeps the graphics, and a Mac is never checked', async () => {
    const check = vi.fn(async () => true)
    const bin = fakeBinary()
    await make(bin, { gpuFallback: true, checkGpu: check }).url()
    expect(JSON.parse(readFileSync(join(bin, '../args'), 'utf8'))).not.toContain('-ng')
    expect(check).toHaveBeenCalledTimes(1)
    const mac = vi.fn(async () => false)
    await make(fakeBinary(), { checkGpu: mac }).url()
    expect(mac).not.toHaveBeenCalled()
  })

  it('a Mac keeps its graphics however often it hangs (Metal)', async () => {
    const bin = fakeBinary(0, ['wedge'])
    const s = make(bin)
    await s.wedged(await s.wedged(await s.url()))
    expect(JSON.parse(readFileSync(join(bin, '../args'), 'utf8'))).not.toContain('-ng')
  })

  it('a Mac never turns the graphics off, and one that crashes after loading keeps them', async () => {
    const mac = fakeBinary(0, ['die-loading', 'ok'])
    const s = make(mac)
    await expect(s.url()).rejects.toThrow(/exited/)
    await s.url()
    expect(JSON.parse(readFileSync(join(mac, '../args'), 'utf8'))).not.toContain('-ng')
    const bin = fakeBinary()
    const t = make(bin, { gpuFallback: true })
    await t.url()
    t.kill()
    await new Promise((r) => setTimeout(r, 200))
    await t.url()
    expect(JSON.parse(readFileSync(join(bin, '../args'), 'utf8'))).not.toContain('-ng')
  })

  it('says in the log which graphics card whisper picked', async () => {
    const info = vi.spyOn(console, 'info').mockImplementation(() => {})
    try {
      await make(fakeBinary()).url()
      await vi.waitFor(() => expect(info.mock.calls.map((a) => a.join(' '))).toContain('ASR whisper-server runs on Vulkan0'))
    } finally { info.mockRestore() }
  })

  it('says in the log which speech model it runs, and where (#107: a report could not tell)', async () => {
    const info = vi.spyOn(console, 'info').mockImplementation(() => {})
    try {
      await make(fakeBinary()).url()
      expect(info.mock.calls.map((a) => a.join(' ')).some((l) => /^ASR whisper-server starts with \S+\.bin on the graphics card if it can$/.test(l))).toBe(true)
    } finally { info.mockRestore() }
  })

  it('never asks for more threads than the computer has', async () => {
    const bin = fakeBinary()
    await make(bin).url()
    const a = JSON.parse(readFileSync(join(bin, '../args'), 'utf8')) as string[]
    expect(Number(a[a.indexOf('-t') + 1])).toBe(Math.min(4, availableParallelism()))
  })

  it('gives up when it never answers in time', async () => {
    await expect(make(fakeBinary(5000), { startTimeoutMs: 400 }).url()).rejects.toThrow(/did not start/)
  })
})

const chunk = { track: 'me' as const, startMs: 0, samples: new Float32Array(16000) }

describe.skipIf(process.platform === 'win32')('restart budget', () => {
  it('a server that dies on every request is relaunched up to 3 times in a meeting, then requests stop going to it', async () => {
    const bin = fakeBinary(0, ['crash'])
    const srv = make(bin)
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv) })
    // more tries than relaunches: a crash that races the alive check only delays one
    for (let i = 0; i < 8; i++) await expect(tr(chunk)).rejects.toThrow()
    expect(srv.launches).toBe(4)
    expect(launchPids(bin)).toHaveLength(4)
  })

  it('waits longer before each relaunch', async () => {
    const waits: number[] = []
    const srv = make(fakeBinary(0, ['crash']), { backoffMs: [10, 20, 30], sleep: async (ms) => { waits.push(ms) } })
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv) })
    for (let i = 0; i < 8; i++) await expect(tr(chunk)).rejects.toThrow()
    expect(waits).toEqual([10, 20, 30])
  })

  it('after 10 minutes without trouble, the relaunches are there again', async () => {
    let t = 0
    const srv = make(fakeBinary(0, ['crash']), { now: () => t })
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv) })
    for (let i = 0; i < 8; i++) await expect(tr(chunk)).rejects.toThrow()
    expect(srv.launches).toBe(4)
    t = 10 * 60_000 + 1
    // relaunched again
    await expect(tr(chunk)).rejects.toThrow()
    expect(srv.launches).toBeGreaterThan(4)
  })

  it('a server that dies once is relaunched and the same chunk goes through', async () => {
    const srv = make(fakeBinary(0, ['crash', 'ok']))
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv) })
    expect((await tr(chunk)).map((l) => l.text)).toEqual(['Fine words here today'])
    expect(srv.launches).toBe(2)
  })

  it('a server that is running but never answers is killed and relaunched once', async () => {
    const bin = fakeBinary(0, ['wedge', 'ok'])
    const srv = make(bin)
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv), timeoutMs: 400 })
    expect((await tr(chunk)).map((l) => l.text)).toEqual(['Fine words here today'])
    expect(srv.launches).toBe(2)
    const [first] = launchPids(bin)
    await new Promise((r) => setTimeout(r, 200))
    expect(isAlive(first)).toBe(false)
  })

  it('a server that keeps wedging is relaunched 3 times, then left alone', async () => {
    const srv = make(fakeBinary(0, ['wedge']))
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv), timeoutMs: 300 })
    for (let i = 0; i < 5; i++) await expect(tr(chunk)).rejects.toThrow()
    expect(srv.launches).toBe(4)
  })

  it('a new meeting gets a new budget', async () => {
    const srv = make(fakeBinary(0, ['crash', 'crash', 'crash', 'crash', 'crash', 'ok']))
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv) })
    for (let i = 0; i < 8; i++) await tr(chunk).catch(() => [])
    expect(srv.launches).toBe(4)
    srv.resetBudget()
    // it starts again; a crash that races the check may cost one try, the next one gets through
    let got: Awaited<ReturnType<typeof tr>> = []
    for (let i = 0; i < 3 && !got.length; i++) got = await tr(chunk).catch(() => [])
    expect(got.length).toBe(1)
    expect(srv.launches).toBeGreaterThan(4)
  })
})

// #156: large-v3-turbo on an i5-10400F took 1.7 times the audio; every timeout spent a relaunch until transcription stopped
describe.skipIf(process.platform === 'win32')('slow on the processor', () => {
  it('a request on the processor that runs out of time while the server answers is slow, not stuck: no relaunch', async () => {
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
    const srv = make(fakeBinary(0, ['slow']), { gpuFallback: true, cpuOnly: true })
    const slow = vi.fn()
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv, { slow }), timeoutMs: 300 })
    for (let i = 0; i < 4; i++) await expect(tr(chunk)).rejects.toThrow()
    expect(srv.launches).toBe(1)
    expect(slow).toHaveBeenCalledTimes(4)
    expect(srv.tooSlow).toBe(true)
    // it still answers, and a request with time enough goes through
    expect((await createTranscriber({ url: () => srv.url(), timeoutMs: 5000 })(chunk)).length).toBe(1)
    warn.mockRestore()
  })

  it('on the graphics card a timeout is still a hang', async () => {
    const srv = make(fakeBinary(0, ['slow', 'ok']), { gpuFallback: true })
    const slow = vi.fn()
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv, { slow }), timeoutMs: 300 })
    expect((await tr(chunk)).length).toBe(1)
    expect(srv.launches).toBe(2)
    expect(slow).not.toHaveBeenCalled()
  })

  it('the relaunches spent, the transcriber is told it gave up', async () => {
    const errors = vi.spyOn(console, 'error').mockImplementation(() => {})
    const srv = make(fakeBinary(0, ['crash']))
    const gaveUp = vi.fn()
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv, { gaveUp }) })
    for (let i = 0; i < 6; i++) await expect(tr(chunk)).rejects.toThrow()
    expect(gaveUp).toHaveBeenCalled()
    errors.mockRestore()
  })
})

describe('speed on the processor', () => {
  const srv = (cpuOnly: boolean) => createWhisperServer({ binary: '/none', model: '/m.bin', gpuFallback: true, cpuOnly })
  it('the timeout follows the measured speed on the processor, and stays as it was on the graphics card', () => {
    const s = srv(true)
    expect(s.timeoutMs(16000 * 15)).toBe(60_000)
    expect(s.cpuRtf).toBeNull()
    s.timed(16000 * 15, 26_800)
    expect(s.cpuRtf).toBeCloseTo(1.79, 2)
    // 15 s at 1.79 x, 4 times over: room to wait behind the other track too
    expect(s.timeoutMs(16000 * 15)).toBe(Math.round(15_000 * (26_800 / 15_000) * 4))
    const g = srv(false)
    g.timed(16000 * 15, 26_800)
    expect(g.timeoutMs(16000 * 15)).toBe(60_000)
    expect(g.cpuRtf).toBeNull()
  })
  it('too slow once two readings took longer than their audio', () => {
    const s = srv(true)
    s.timed(16000 * 10, 17_000)
    expect(s.tooSlow).toBe(false)
    s.timed(16000 * 10, 18_000)
    expect(s.tooSlow).toBe(true)
    const fast = srv(true)
    fast.timed(16000 * 10, 4000); fast.timed(16000 * 10, 5000)
    expect(fast.tooSlow).toBe(false)
  })
  it('a reading that went to an older launch (another model, before a relaunch) does not count', () => {
    const s = srv(true)
    s.timed(16000 * 10, 30_000, 'http://127.0.0.1:9')
    expect(s.cpuRtf).toBeNull()
  })
})

describe.skipIf(process.platform === 'win32')('the graphics card again', () => {
  it('a server that set the graphics card aside tries it again when asked (a new meeting, a saved recording), and says why in the log', async () => {
    const bin = fakeBinary(0, ['wedge', 'wedge', 'ok'])
    const args = () => JSON.parse(readFileSync(join(bin, '../args'), 'utf8')) as string[]
    const errors = vi.spyOn(console, 'error').mockImplementation(() => {})
    const info = vi.spyOn(console, 'info').mockImplementation(() => {})
    const s = make(bin, { gpuFallback: true, backoffMs: [0, 0] })
    await s.wedged(await s.wedged(await s.url()))
    expect(s.cpuOnly).toBe(true)
    const cpu = await s.url()
    expect(args()).toContain('-ng')
    expect(s.gpuAgain('a new meeting')).toBe(true)
    expect(s.cpuOnly).toBe(false)
    expect(info.mock.calls.map((a) => a.join(' ')).some((l) => /tries the graphics card again for a new meeting; it was set aside because it hung twice on the graphics card/.test(l))).toBe(true)
    const gpu = await s.url()
    expect(gpu).not.toBe(cpu)
    expect(args()).not.toContain('-ng')
    // starting again for the graphics card is not a relaunch after trouble: none of the meeting's are used
    expect(s.launches).toBe(4)
    // already on the graphics card: nothing to do
    expect(s.gpuAgain('a new meeting')).toBe(false)
    errors.mockRestore(); info.mockRestore()
  })

  it('a Mac never needs it', () => {
    expect(createWhisperServer({ binary: '/none', model: '/m.bin' }).gpuAgain('a new meeting')).toBe(false)
  })
})

describe.skipIf(process.platform === 'win32')('two tracks failing at once', () => {
  it('both requests wedge, their timeouts land at different times: one kill, one relaunch, both go on in the new child', async () => {
    const bin = fakeBinary(600, ['wedge', 'ok'])
    const srv = make(bin)
    const tr = createTranscriber({ url: () => srv.url(), recover: recoverer(srv), timeoutMs: 700 })
    await srv.url()
    // me times out first and relaunches (a 600 ms load); them times out while that child is still loading
    const me = tr({ ...chunk, track: 'me' })
    await new Promise((r) => setTimeout(r, 300))
    const them = tr({ ...chunk, track: 'them' })
    const [a, b] = await Promise.all([me, them])
    expect(a).toHaveLength(1)
    expect(b).toHaveLength(1)
    expect(srv.launches).toBe(2)
    expect(launchPids(bin)).toHaveLength(2)
  })

  it('a request that failed on an old child does not kill the relaunch that is already up', async () => {
    const bin = fakeBinary(0, ['wedge', 'ok'])
    const srv = make(bin)
    const old = await srv.url()
    const recover = recoverer(srv)
    await recover('timeout', old)          // track A: kills and relaunches
    const fresh = await srv.url()
    expect(fresh).not.toBe(old)
    await recover('timeout', old)          // track B, late, still holding the old address
    expect(await srv.url()).toBe(fresh)
    expect(srv.launches).toBe(2)
    expect(await srv.alive()).toBe(true)
  })
})

describe('pid file', () => {
  const rig = (names: Record<number, string | null>) => {
    const killed: number[] = []
    return { killed, deps: { name: (pid: number) => names[pid] ?? null, kill: (pid: number) => { killed.push(pid) } } }
  }
  const pidFile = () => join(mkdtempSync(join(tmpdir(), 'yogurt-pid-')), 'whisper-server.pid')

  it('kills a left-over whisper-server and removes the file', () => {
    const f = pidFile(); writeFileSync(f, '4242')
    const r = rig({ 4242: '/Applications/Yogurt.app/Contents/Resources/bin/whisper-server' })
    killStale(f, r.deps)
    expect(r.killed).toEqual([4242])
    expect(existsSync(f)).toBe(false)
  })

  it('on Windows the program is whisper-server.exe (tasklist gives the name alone)', () => {
    const f = pidFile(); writeFileSync(f, '4242')
    const r = rig({ 4242: 'whisper-server.exe' })
    killStale(f, r.deps)
    expect(r.killed).toEqual([4242])
  })

  it('leaves alone a pid that now belongs to another program, or to nothing', () => {
    const f = pidFile(); writeFileSync(f, '4242')
    const other = rig({ 4242: '/usr/bin/Safari' })
    killStale(f, other.deps)
    expect(other.killed).toEqual([])
    expect(existsSync(f)).toBe(false)
    writeFileSync(f, '77')
    const gone = rig({})
    killStale(f, gone.deps)
    expect(gone.killed).toEqual([])
  })

  it('ignores a missing or garbled file', () => {
    const r = rig({ 1: 'whisper-server' })
    killStale(join(tmpdir(), 'no-such-pid-file'), r.deps)
    const f = pidFile(); writeFileSync(f, 'not a number')
    killStale(f, r.deps)
    expect(r.killed).toEqual([])
  })

  it.skipIf(process.platform === 'win32')('the server writes its pid on launch, clears an old one first, and removes the file on a clean stop', async () => {
    const f = pidFile(); writeFileSync(f, '999999')
    const r = rig({ 999999: 'whisper-server' })
    const srv = make(fakeBinary(), { pidFile: f, lookup: r.deps })
    await srv.url()
    expect(r.killed).toEqual([999999])
    expect(Number(readFileSync(f, 'utf8'))).toBeGreaterThan(0)
    await srv.stop()
    expect(existsSync(f)).toBe(false)
  })
})