Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(speech): sweep orphaned part files, un-dead delete()'s part cleanup (Task 3 review)

enesea756cc04f99
diff --git a/src/core/speech/whisper-models.test.ts b/src/core/speech/whisper-models.test.ts
index 87f8a338b..ecbb16f1e 100644
--- a/src/core/speech/whisper-models.test.ts
+++ b/src/core/speech/whisper-models.test.ts
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
-import { mkdtempSync, rmSync, existsSync, readFileSync } from 'node:fs'
+import { mkdtempSync, rmSync, existsSync, readFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { WhisperModelStore } from './whisper-models'
@@ -79,4 +79,31 @@ describe('WhisperModelStore', () => {
await expect(second).resolves.toBeUndefined()
expect(await store.has('tiny')).toBe(true)
})
+
+ it('sweeps orphaned .part.<genId> files on download', async () => {
+ const store = new WhisperModelStore({ dir, fetchFn: fakeFetch(new Uint8Array(64)) })
+ // Pre-create a fake orphan from a previous crash
+ writeFileSync(join(dir, 'ggml-tiny.bin.part.deadbeef'), 'junk')
+ expect(existsSync(join(dir, 'ggml-tiny.bin.part.deadbeef'))).toBe(true)
+
+ await store.download('tiny')
+
+ // Orphan should be gone, model should exist
+ expect(existsSync(join(dir, 'ggml-tiny.bin.part.deadbeef'))).toBe(false)
+ expect(await store.has('tiny')).toBe(true)
+ })
+
+ it('delete() removes orphaned .part.<genId> files when nothing is in flight', async () => {
+ const store = new WhisperModelStore({ dir, fetchFn: fakeFetch(new Uint8Array(64)) })
+ // Pre-create the model and an orphan
+ writeFileSync(store.modelPath('tiny'), 'model')
+ writeFileSync(store.modelPath('tiny') + '.part.orphan123', 'junk')
+ expect(await store.has('tiny')).toBe(true)
+
+ await store.delete('tiny')
+
+ // Both should be gone
+ expect(await store.has('tiny')).toBe(false)
+ expect(existsSync(store.modelPath('tiny') + '.part.orphan123')).toBe(false)
+ })
})
diff --git a/src/core/speech/whisper-models.ts b/src/core/speech/whisper-models.ts
index b100506f9..cbd1f9ca5 100644
--- a/src/core/speech/whisper-models.ts
+++ b/src/core/speech/whisper-models.ts
@@ -1,12 +1,12 @@
import { createWriteStream } from 'node:fs'
-import { mkdir, rename, rm, stat } from 'node:fs/promises'
+import { mkdir, readdir, rename, rm, stat } from 'node:fs/promises'
import { join } from 'node:path'
import { Writable } from 'node:stream'
import { WHISPER_DOWNLOAD_BASE, WHISPER_MODELS, whisperModel } from '../../shared/speech'
/** Downloads and manages the local ggml whisper models. The fences here are
- * lessons already paid for on iOS: a download streams to `<file>.part` and
- * renames only on completion; delete() aborts an in-flight download and a
+ * lessons already paid for on iOS: a download streams to a per-download `<file>.part.<genId>`
+ * and renames only on completion; delete() aborts an in-flight download and a
* late chunk can never resurrect a deleted model; concurrent download() of
* the same id joins the same promise instead of racing two writers. */
export class WhisperModelStore {
@@ -49,6 +49,7 @@ export class WhisperModelStore {
const info = whisperModel(id)
if (!info) return Promise.reject(new Error(`unknown whisper model: ${id}`))
const abort = new AbortController()
+ // Math.random suffices: not a security boundary, just a scratch-name de-collider.
const genId = Math.random().toString(36).slice(2)
const promise = this.run(id, info.file, abort, genId).finally(() => {
// Only clear our own slot — a delete already removed it.
@@ -58,8 +59,26 @@ export class WhisperModelStore {
return promise
}
+ /** Remove every .part.<genId> variant for this id. Part names are
+ * per-download (see run()), so a hard crash can strand orphans that no
+ * deterministic rm would ever find — sweep by prefix instead. */
+ private async removeParts(id: string): Promise<void> {
+ const base = this.modelPath(id)
+ const prefix = `${base.split('/').pop()}.part`
+ const entries = await readdir(this.dir).catch(() => [] as string[])
+ await Promise.all(
+ entries.filter((e) => e.startsWith(prefix)).map((e) => rm(join(this.dir, e), { force: true })),
+ )
+ }
+
private async run(id: string, file: string, abort: AbortController, genId: string): Promise<void> {
await mkdir(this.dir, { recursive: true })
+ // Sweep stale .part.<*> orphans before creating the new part file. The
+ // in-flight dedupe guarantees no OTHER live writer for this id exists, so
+ // we can only ever remove abandoned fragments from prior hard crashes.
+ if (this.inFlight.get(id)?.abort === abort) {
+ await this.removeParts(id)
+ }
const partPath = this.modelPath(id) + '.part.' + genId
const res = await this.fetchFn(WHISPER_DOWNLOAD_BASE + file, { signal: abort.signal })
if (!res.ok || !res.body) throw new Error(`model download failed (${res.status})`)
@@ -102,6 +121,6 @@ export class WhisperModelStore {
// the old .part, and no completed file can exist mid-download.
if (this.inFlight.has(id)) return
await rm(this.modelPath(id), { force: true })
- await rm(this.modelPath(id) + '.part', { force: true })
+ await this.removeParts(id)
}
}