Files
plugins/packages/fingerprint/src/store.ts
T
KaranocaVe 00f4d84b8f fingerprint 0.1.0: a page that checks whether a model answers like the one it claims to be (#516)
Adds the Fingerprint page plugin (packages/fingerprint): it checks whether a model behind the local magpie gateway answers like the model it claims to be.

Reviewed at 531ca11f4 (KaranocaVe). Run at that head merged with main 848497c, through mote-run on the Mac mini with Bun 1.4.2:
- bun run rebuild from the committed bun.lock (frozen, --ignore-scripts): typecheck, 30/30 tests, build; diff -r against the committed dist/ is empty.
- HOME=$(mktemp -d) bun test: 3838 pass, 0 fail. bun scripts/check.mjs: exit 0.
- Offline preload refusing every non-loopback fetch/socket: 30/30, nothing refused.
- Broken by hand, each red: ensureDetector() in bootstrap, dropping ttl: Infinity, validating the job after the download.

Maintainer decision: the reference data comes from two third-party hosts (lm.ikale.io, cdn.jsdmirror.com), only on an explicit Download or a valid job, and both are named on the page, in registry.json and the READMEs. It is data used locally to rank candidates and is sent nowhere; the @latest fallback has no pinned hash, so a bad snapshot would rank wrongly, which the page states.

Not on npm yet (E404): magpie installs it from the dist branch until an owner publishes the first version.
2026-10-31 02:00:33 +08:00

55 lines
1.9 KiB
TypeScript

import { mkdir, readdir, readFile, rename, writeFile, unlink } from 'node:fs/promises';
import { join } from 'node:path';
import type { Job } from './types';
export class Store {
readonly jobs = new Map<string, Job>();
private writing = Promise.resolve();
constructor(readonly dir: string) {}
async load() {
await mkdir(this.dir, { recursive: true, mode: 0o700 });
for (const name of await readdir(this.dir)) {
if (!/^[\w-]+\.json$/.test(name)) continue;
try {
const job = JSON.parse(await readFile(join(this.dir, name), 'utf8')) as Job;
if (!job.id || !Array.isArray(job.results) || name !== `${job.id}.json`) continue;
if (job.state === 'running' || job.state === 'queued') {
job.state = 'interrupted';
job.error = 'The plugin service restarted before this job finished.';
job.finishedAt = new Date().toISOString();
for (const r of job.results)
if (r.state === 'running' || r.state === 'queued') {
r.state = 'cancelled';
r.analysis = null;
}
await this.save(job);
}
this.jobs.set(job.id, job);
} catch {
/* Ignore damaged files; never replace them with invented results. */
}
}
}
list() {
return [...this.jobs.values()].sort((a, b) => b.createdAt.localeCompare(a.createdAt));
}
save(job: Job) {
const contents = JSON.stringify(job);
const path = join(this.dir, `${job.id}.json`);
const next = this.writing.then(async () => {
const temp = `${path}.tmp`;
await writeFile(temp, contents, { mode: 0o600 });
await rename(temp, path);
});
this.writing = next.catch(() => {});
return next;
}
async remove(id: string) {
await this.writing;
await unlink(join(this.dir, `${id}.json`));
this.jobs.delete(id);
}
async flush() {
await this.writing;
}
}