Worker: write only what changed, sample downloads less

- The models folder scan compared nothing and rewrote local_folders on every
  run (delete all, insert all, each folder's file list included), every 2
  minutes by default: ~1 MB of SQLite writes per scan with three large import
  folders. It now inserts new folders, updates changed ones and deletes gone
  ones; unchanged rows (compared by counts, commit and the stored JSON text)
  aren't written.
- Download progress and the speed limit share one ProgressSampler. A file found
  complete isn't stat'ed or its hf metadata re-read again during the run, and
  with every file complete the partial files aren't listed. Before, every
  sample looked at every file, 5 times a second with a speed limit set. The
  check after hf exits still looks at every file.
- A benchmark's phase is written when its step changes, not for every
  progress line BetterBench prints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
This commit is contained in:
Claude committed 2026-11-01 21:01:11 +00:00
1 parent d10a491042
commit d58bff3437
8 files changed
+260 -8

No files matched your search

+8 -4
View File
@@ -33,6 +33,7 @@ import { XetCounter } from "./hf-progress";
import { JobLog } from "./job-log";
import { log } from "./log";
import { settleModelStatus } from "./model-status";
import { ProgressSampler } from "./progress-sampler";
import type { Runner } from "./runner";
import { SpeedMeter } from "./speed";
import { Throttle } from "./throttle";
@@ -319,7 +320,9 @@ export class DownloadRunner implements Runner {
);
child.on("close", (code, signal) => done({ code, signal }));
running.add(this);
const throttleTimer = this.throttle(dir, expected);
// One sampler for progress and the speed limit; it skips files it already found complete.
const sampler = new ProgressSampler(dir, expected);
const throttleTimer = this.throttle(sampler);
// Progress from bytes on disk, written to the DB once per second.
const xetBytes = new XetCounter();
@@ -331,7 +334,7 @@ export class DownloadRunner implements Runner {
if (busy) return;
busy = true;
try {
const progress = steadyProgress(last, await this.sample(dir, expected));
const progress = steadyProgress(last, await sampler.sample());
last = progress;
const onDisk = progress.reduce((s, p) => s + p.done, 0);
const doneBytes = Math.min(this.job.totalBytes, Math.max(onDisk, alreadyDone + xetBytes.bytes));
@@ -351,7 +354,7 @@ export class DownloadRunner implements Runner {
* Enforce Settings → Downloads → Speed limit by pausing hf while it is ahead of the budget
* (see Throttle). The limit is re-read every few seconds, so changes apply to running downloads.
*/
private throttle(dir: string, expected: ExpectedFile[]): NodeJS.Timeout {
private throttle(sampler: ProgressSampler): NodeJS.Timeout {
let total: number | null = null;
let readAt = 0;
let bucket: Throttle | null = null;
@@ -383,7 +386,7 @@ export class DownloadRunner implements Runner {
const limit = total / Math.max(1, running.size);
if (!bucket) bucket = new Throttle(limit);
bucket.limit = limit;
const bytes = (await this.sample(dir, expected)).reduce((s, p) => s + p.done, 0);
const bytes = (await sampler.sample()).reduce((s, p) => s + p.done, 0);
if (bucket.update(Date.now(), bytes)) this.pause();
else this.resume();
} catch (err) {
@@ -406,6 +409,7 @@ export class DownloadRunner implements Runner {
if (this.child.exitCode === null) this.child.kill("SIGCONT");
}
/** Every file, looked at fresh (before hf starts and after it exits). */
private async sample(dir: string, expected: ExpectedFile[]) {
const incomplete = await listIncomplete(dir);
return Promise.all(expected.map((f) => bytesOnDisk(dir, f, incomplete)));