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

+77
View File
@@ -0,0 +1,77 @@
// The models-folder scan, on a temporary folder and SQLite database.
// Runs with `--conditions=react-server` (see package.json).
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { after, before, describe, it } from "node:test";
const dir = mkdtempSync(join(tmpdir(), "kaname-scan-"));
process.env.DATABASE_URL = `file:${join(dir, "test.db").replace(/\\/g, "/")}`;
const modelsDir = join(dir, "models");
process.env.MODELS_DIR = modelsDir;
let scan: typeof import("./scan");
let D: typeof import("@/lib/db/client");
let S: typeof import("@/lib/db/schema");
before(async () => {
const { runMigrations } = await import("@/lib/db/migrate");
await runMigrations();
scan = await import("./scan");
D = await import("@/lib/db/client");
S = await import("@/lib/db/schema");
});
after(() => {
try {
rmSync(dir, { recursive: true, force: true });
} catch {}
});
function put(path: string, bytes = 3) {
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, "x".repeat(bytes));
}
const tick = () => new Promise((r) => setTimeout(r, 5));
async function stored() {
const rows = await D.db().select().from(S.localFolders);
return Object.fromEntries(rows.map((r) => [r.path, r]));
}
describe("models folder scan", () => {
it("writes only new or changed folders and removes the ones that are gone", async () => {
put(join(modelsDir, "org", "a", "model.gguf"));
put(join(modelsDir, "org", "b", "shards", "x.safetensors"));
assert.equal(await scan.scanModelsDir(modelsDir), 2);
const first = await stored();
await tick();
assert.equal(await scan.scanModelsDir(modelsDir), 2);
const second = await stored();
assert.equal(second["org/a"].scannedAt.getTime(), first["org/a"].scannedAt.getTime(), "unchanged: not written");
assert.equal(second["org/b"].scannedAt.getTime(), first["org/b"].scannedAt.getTime(), "unchanged: not written");
await tick();
put(join(modelsDir, "org", "a", "extra.json"), 10);
rmSync(join(modelsDir, "org", "b"), { recursive: true });
assert.equal(await scan.scanModelsDir(modelsDir), 1);
const third = await stored();
assert.deepEqual(Object.keys(third), ["org/a"]);
assert.equal(third["org/a"].fileCount, 2);
assert.deepEqual(third["org/a"].files.map((f) => f.path), ["extra.json", "model.gguf"]);
assert.ok(third["org/a"].scannedAt.getTime() > first["org/a"].scannedAt.getTime(), "changed: written");
});
it("compares the file list, not only the counts", () => {
const at = new Date();
const found = { path: "o/m", files: [{ path: "b.gguf", size: 2 }], fileCount: 1, totalBytes: 2, commit: null, scannedAt: at };
const same = { path: "o/m", filesJson: JSON.stringify(found.files), fileCount: 1, totalBytes: 2, commit: null };
assert.deepEqual(scan.folderChanges([same], [found]), { insert: [], update: [], remove: [] });
const renamed = { ...same, filesJson: JSON.stringify([{ path: "a.gguf", size: 2 }]) };
assert.deepEqual(scan.folderChanges([renamed], [found]).update, [found]);
});
});