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:
1 parent
c2c239bf16
commit
7864a492c9
37 files changed
+2365
-191
No files matched your search
+90
-24
@@ -1,9 +1,28 @@
|
||||
import { asc, eq, isNull, lt, or } from "drizzle-orm";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { models, type HubState } from "@/lib/db/schema";
|
||||
import { getRepoInfo, HubError } from "./client";
|
||||
// Hub checks per model: is the repo (and the pinned commit) still there, and is there a newer
|
||||
// commit on the tracked revision? Run periodically by the worker and on "Check now". Results are
|
||||
// stored on the model; nothing local is ever changed by a check.
|
||||
|
||||
type CheckedModel = Pick<typeof models.$inferSelect, "id" | "repoId" | "pinnedSha" | "hubMissingSince">;
|
||||
import { and, asc, eq, isNull, lt, or } from "drizzle-orm";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { type HubState, modelFiles, models, type UpdateDiff } from "@/lib/db/schema";
|
||||
import { computeUpdateDiff } from "@/lib/update-diff";
|
||||
import { getRepoInfo, HubError, listFiles, SHA_RE } from "./client";
|
||||
|
||||
type CheckedModel = Pick<
|
||||
typeof models.$inferSelect,
|
||||
"id" | "repoId" | "pinnedSha" | "trackedRevision" | "hubMissingSince" | "updateDiff" | "includePatterns" | "excludePatterns"
|
||||
>;
|
||||
|
||||
const CHECKED_COLUMNS = {
|
||||
id: models.id,
|
||||
repoId: models.repoId,
|
||||
pinnedSha: models.pinnedSha,
|
||||
trackedRevision: models.trackedRevision,
|
||||
hubMissingSince: models.hubMissingSince,
|
||||
updateDiff: models.updateDiff,
|
||||
includePatterns: models.includePatterns,
|
||||
excludePatterns: models.excludePatterns,
|
||||
};
|
||||
|
||||
/** Why new downloads are impossible, or null if the Hub still has the repo at the pinned commit. */
|
||||
export function hubMissingMessage(model: { repoId: string; hubState: HubState | null }): string | null {
|
||||
@@ -16,25 +35,73 @@ export function hubMissingMessage(model: { repoId: string; hubState: HubState |
|
||||
return null;
|
||||
}
|
||||
|
||||
export type HubCheckResult = { state: HubState } | { state: null; error: string };
|
||||
export type HubCheckResult =
|
||||
| { state: HubState; remoteSha: string | null; update: UpdateDiff | null; warning: string | null }
|
||||
| { state: null; error: string };
|
||||
|
||||
const message = (err: unknown) => (err instanceof Error ? err.message : String(err));
|
||||
const isGated = (err: unknown) => err instanceof HubError && (err.code === "gated_no_token" || err.code === "gated_no_access");
|
||||
|
||||
/** Resolve a revision to its commit; null if the Hub says the revision doesn't exist. */
|
||||
async function resolve(repoId: string, revision: string): Promise<string | null> {
|
||||
try {
|
||||
return (await getRepoInfo(repoId, revision)).sha;
|
||||
} catch (err) {
|
||||
if (err instanceof HubError && err.code === "revision_not_found") return null;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async function run(model: CheckedModel): Promise<HubCheckResult> {
|
||||
// 1. Head of the tracked revision (a full SHA as revision means "pinned for good").
|
||||
let remoteSha: string | null;
|
||||
let warning: string | null = null;
|
||||
if (SHA_RE.test(model.trackedRevision)) {
|
||||
remoteSha = model.trackedRevision;
|
||||
} else {
|
||||
remoteSha = await resolve(model.repoId, model.trackedRevision);
|
||||
if (!remoteSha) warning = `Revision "${model.trackedRevision}" no longer exists on the Hub; can't check for updates.`;
|
||||
}
|
||||
|
||||
// 2. Is the pinned commit still there? (Known if it's the head.)
|
||||
const pinnedExists = remoteSha === model.pinnedSha || (await resolve(model.repoId, model.pinnedSha)) !== null;
|
||||
const state: HubState = pinnedExists ? "available" : "commit_missing";
|
||||
|
||||
// 3. Newer commit: what would change in the selection? Only recomputed when the head moved.
|
||||
let update: UpdateDiff | null = null;
|
||||
if (remoteSha && remoteSha !== model.pinnedSha) {
|
||||
update = model.updateDiff?.sha === remoteSha ? model.updateDiff : null;
|
||||
if (!update) {
|
||||
const [next, current] = await Promise.all([
|
||||
listFiles(model.repoId, remoteSha),
|
||||
db()
|
||||
.select({ path: modelFiles.path, size: modelFiles.size, blobOid: modelFiles.blobOid, lfsSha256: modelFiles.lfsSha256 })
|
||||
.from(modelFiles)
|
||||
.where(and(eq(modelFiles.modelId, model.id), eq(modelFiles.selected, true))),
|
||||
]);
|
||||
update = computeUpdateDiff(remoteSha, current, next, { include: model.includePatterns, exclude: model.excludePatterns });
|
||||
}
|
||||
}
|
||||
return { state, remoteSha, update, warning };
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the repo, and the pinned commit, still on the Hub? Stores the result on the model. Only a
|
||||
* definite answer changes `hubState`; network errors, rate limits or a bad token are kept as
|
||||
* `lastCheckError`. Never touches local files.
|
||||
* Check one model against the Hub and store the result. Only a definite answer changes the stored
|
||||
* state; network errors, rate limits or a bad token are kept as `lastCheckError`.
|
||||
*/
|
||||
export async function checkHubAvailability(model: CheckedModel): Promise<HubCheckResult> {
|
||||
export async function checkHub(model: CheckedModel): Promise<HubCheckResult> {
|
||||
let result: HubCheckResult;
|
||||
try {
|
||||
await getRepoInfo(model.repoId, model.pinnedSha);
|
||||
result = { state: "available" };
|
||||
result = await run(model);
|
||||
} catch (err) {
|
||||
if (err instanceof HubError && err.code === "repo_not_found") result = { state: "repo_missing" };
|
||||
else if (err instanceof HubError && err.code === "revision_not_found") result = { state: "commit_missing" };
|
||||
// Gated repos exist; access is a download concern, not availability.
|
||||
else if (err instanceof HubError && (err.code === "gated_no_token" || err.code === "gated_no_access")) {
|
||||
result = { state: "available" };
|
||||
} else result = { state: null, error: err instanceof Error ? err.message : String(err) };
|
||||
if (err instanceof HubError && err.code === "repo_not_found") {
|
||||
result = { state: "repo_missing", remoteSha: null, update: null, warning: null };
|
||||
} else if (isGated(err)) {
|
||||
// Gated repos exist; access is a download concern.
|
||||
result = { state: "available", remoteSha: null, update: model.updateDiff ?? null, warning: message(err) };
|
||||
} else {
|
||||
result = { state: null, error: message(err) };
|
||||
}
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
@@ -45,9 +112,10 @@ export async function checkHubAvailability(model: CheckedModel): Promise<HubChec
|
||||
? { lastCheckedAt: now, lastCheckError: result.error }
|
||||
: {
|
||||
lastCheckedAt: now,
|
||||
lastCheckError: null,
|
||||
lastCheckError: result.warning,
|
||||
hubState: result.state,
|
||||
hubMissingSince: result.state === "available" ? null : (model.hubMissingSince ?? now),
|
||||
...(result.state === "repo_missing" ? {} : { remoteSha: result.remoteSha, updateDiff: result.update }),
|
||||
},
|
||||
)
|
||||
.where(eq(models.id, model.id));
|
||||
@@ -58,7 +126,7 @@ export async function checkHubAvailability(model: CheckedModel): Promise<HubChec
|
||||
export async function modelsDueForHubCheck(intervalMs: number, limit = 50): Promise<CheckedModel[]> {
|
||||
const cutoff = new Date(Date.now() - intervalMs);
|
||||
return db()
|
||||
.select({ id: models.id, repoId: models.repoId, pinnedSha: models.pinnedSha, hubMissingSince: models.hubMissingSince })
|
||||
.select(CHECKED_COLUMNS)
|
||||
.from(models)
|
||||
.where(or(isNull(models.lastCheckedAt), lt(models.lastCheckedAt, cutoff)))
|
||||
.orderBy(asc(models.lastCheckedAt))
|
||||
@@ -66,8 +134,6 @@ export async function modelsDueForHubCheck(intervalMs: number, limit = 50): Prom
|
||||
}
|
||||
|
||||
export async function getCheckedModel(id: string): Promise<CheckedModel | undefined> {
|
||||
return db().query.models.findFirst({
|
||||
columns: { id: true, repoId: true, pinnedSha: true, hubMissingSince: true },
|
||||
where: eq(models.id, id),
|
||||
});
|
||||
const [row] = await db().select(CHECKED_COLUMNS).from(models).where(eq(models.id, id));
|
||||
return row;
|
||||
}
|
||||
Reference in new issue
Block a user