Delete, verify/deep verify, repair and update checks

- Delete any model (incl. failed ones); the worker removes the repo's
  files (keeps foreign files and empty-folder cleanup), then the rows.
- Verify (existence, size, hf metadata) and deep verify (SHA-256 /
  git blob SHA-1 against the Hub) with missing/mismatch/extra report.
- Repair = download of the selection; broken files are deleted first
  because hf skips files whose metadata matches without checking them.
- Hub check resolves the tracked revision and stores which selected
  files a newer commit changes/adds/removes; Update pins it (old commit
  kept in model_revisions), downloads, then removes dropped files.
- Worker runs verify/delete jobs next to downloads (2 local slots).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-10-06 01:00:55 +02:00
1 parent c2c239bf16
commit 7864a492c9
37 files changed
+2365 -191

No files matched your search

+54 -8
View File
@@ -6,6 +6,7 @@ import { hubMissingMessage } from "@/lib/hub/availability";
import {
ACTIVE_JOB_STATES,
type JobPurpose,
type JobResult,
jobFiles,
jobLogs,
jobs,
@@ -26,19 +27,31 @@ export class JobError extends Error {
const OPEN_STATES = ["queued", ...ACTIVE_JOB_STATES] as const;
const INSERT_CHUNK = 500;
/** Queue a download of all selected files of a model at its pinned SHA. */
export async function queueDownload(modelId: string, purpose: JobPurpose = "initial"): Promise<{ jobId: string }> {
/** True if the model has a queued or running job (only one job per model at a time). */
export async function hasOpenJob(modelId: string): Promise<boolean> {
const open = await db().query.jobs.findFirst({
columns: { id: true },
where: and(eq(jobs.modelId, modelId), inArray(jobs.state, [...OPEN_STATES])),
});
return !!open;
}
/**
* Queue a download of all selected files of a model at its pinned SHA. hf skips files that are
* already complete, so this also serves as resume and repair. `removePaths`: files of an older
* revision to delete once the download succeeded (updates).
*/
export async function queueDownload(
modelId: string,
purpose: JobPurpose = "initial",
options: { removePaths?: string[] } = {},
): Promise<{ jobId: string }> {
const d = db();
const model = await d.query.models.findFirst({ where: eq(models.id, modelId) });
if (!model) throw new JobError("not_found", "Model not found.", 404);
const missing = hubMissingMessage(model);
if (missing) throw new JobError("hub_missing", missing, 409);
const open = await d.query.jobs.findFirst({
columns: { id: true },
where: and(eq(jobs.modelId, modelId), inArray(jobs.state, [...OPEN_STATES])),
});
if (open) throw new JobError("busy", "This model already has a queued or running job.", 409);
if (await hasOpenJob(modelId)) throw new JobError("busy", "This model already has a queued or running job.", 409);
const files = await d
.select({ id: modelFiles.id, path: modelFiles.path, size: modelFiles.size })
@@ -57,6 +70,7 @@ export async function queueDownload(modelId: string, purpose: JobPurpose = "init
purpose,
targetSha: model.pinnedSha,
totalBytes: files.reduce((s, f) => s + f.size, 0),
result: options.removePaths?.length ? { removePaths: options.removePaths } : null,
}),
...chunk(rows, INSERT_CHUNK).map((c) => d.insert(jobFiles).values(c)),
d.update(models).set({ status: "downloading" }).where(eq(models.id, modelId)),
@@ -64,6 +78,36 @@ export async function queueDownload(modelId: string, purpose: JobPurpose = "init
return { jobId };
}
/** Queue a verify (size + hf metadata) or deep verify (hashes) of the selected files. Local only. */
export async function queueVerify(modelId: string, deep: boolean): Promise<{ jobId: string }> {
const d = db();
const model = await d.query.models.findFirst({ where: eq(models.id, modelId) });
if (!model) throw new JobError("not_found", "Model not found.", 404);
if (await hasOpenJob(modelId)) throw new JobError("busy", "This model already has a queued or running job.", 409);
const files = await d
.select({ id: modelFiles.id, path: modelFiles.path, size: modelFiles.size })
.from(modelFiles)
.where(and(eq(modelFiles.modelId, modelId), eq(modelFiles.selected, true)))
.orderBy(asc(modelFiles.path));
if (files.length === 0) throw new JobError("nothing_selected", "No files selected for this model.", 400);
const jobId = ulid();
await d.batch([
d.insert(jobs).values({
id: jobId,
modelId,
kind: deep ? "deep_verify" : "verify",
targetSha: model.pinnedSha,
totalBytes: files.reduce((s, f) => s + f.size, 0),
}),
...chunk(
files.map((f) => ({ jobId, modelFileId: f.id, path: f.path, expectedBytes: f.size })),
INSERT_CHUNK,
).map((c) => d.insert(jobFiles).values(c)),
]);
return { jobId };
}
/** Ask the worker to stop a job. Queued jobs are cancelled by the worker on its next tick. */
export async function requestCancel(jobId: string): Promise<void> {
const res = await db()
@@ -97,6 +141,7 @@ export type JobSnapshot = {
exitCode: number | null;
errorCode: string | null;
errorMessage: string | null;
result: JobResult | null;
cancelRequested: boolean;
createdAt: number;
startedAt: number | null;
@@ -128,6 +173,7 @@ const jobColumns = {
exitCode: jobs.exitCode,
errorCode: jobs.errorCode,
errorMessage: jobs.errorMessage,
result: jobs.result,
cancelRequestedAt: jobs.cancelRequestedAt,
createdAt: jobs.createdAt,
startedAt: jobs.startedAt,