From cf06440a449f852c15e78ea1509f29b0cd562c4d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 03:55:13 +0000 Subject: [PATCH] Sonar: numeric sort for the oldest queued item, explicit migration order, linear trailing-space trim --- src/db/migrate.ts | 2 +- src/triage/knowledge-writer.ts | 9 ++++++++- src/web/routes/health.ts | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/db/migrate.ts b/src/db/migrate.ts index f3773c7..307385f 100644 --- a/src/db/migrate.ts +++ b/src/db/migrate.ts @@ -10,7 +10,7 @@ const MIGRATIONS_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), ' * once: the whole run happens inside one write transaction (BEGIN IMMEDIATE). */ export async function migrate(db: Db, log: (msg: string) => void = () => {}): Promise { - const files = (await readdir(MIGRATIONS_DIR)).filter((f) => f.endsWith('.sql')).sort(); + const files = (await readdir(MIGRATIONS_DIR)).filter((f) => f.endsWith('.sql')).sort((a, b) => Number(a > b) - Number(a < b)); const scripts = await Promise.all(files.map(async (f) => [f, await readFile(path.join(MIGRATIONS_DIR, f), 'utf8')] as const)); return db.transaction(async (tx) => { await tx.query('CREATE TABLE IF NOT EXISTS schema_migrations (name TEXT PRIMARY KEY, applied_at INTEGER NOT NULL DEFAULT (now_ms()))'); diff --git a/src/triage/knowledge-writer.ts b/src/triage/knowledge-writer.ts index df58986..f2b0ffb 100644 --- a/src/triage/knowledge-writer.ts +++ b/src/triage/knowledge-writer.ts @@ -134,6 +134,13 @@ const cut = (text: string, max: number) => { return t.length > max ? `${t.slice(0, max - 1)}…` : t; }; +/** `line` without its trailing spaces and tabs. */ +function trimEndSpaces(line: string): string { + let end = line.length; + while (end > 0 && (line[end - 1] === ' ' || line[end - 1] === '\t')) end--; + return line.slice(0, end); +} + /** A document without its badges, images, HTML and comments, cut to size. */ export function cleanDoc(text: string, max: number): string { return text @@ -142,7 +149,7 @@ export function cleanDoc(text: string, max: number): string { .replace(/\[\s*\]\([^)]*\)/g, '') .replace(/]*>/gi, '') .replace(/<\/?(p|div|span|a|br|picture|source|img|h\d|center|details|summary|sup|sub|b|i)\b[^>]*>/gi, '') - .split('\n').map((l) => l.replace(/[ \t]+$/, '')).join('\n') + .split('\n').map(trimEndSpaces).join('\n') .replace(/\n{3,}/g, '\n\n') .trim() .slice(0, max); diff --git a/src/web/routes/health.ts b/src/web/routes/health.ts index 4df40ee..9bd4ea4 100644 --- a/src/web/routes/health.ts +++ b/src/web/routes/health.ts @@ -196,7 +196,7 @@ async function gather(web: Web, ctx: WorkspaceRequestContext) { const [repos, names] = await Promise.all([indexStatus(deps.db, [...new Set(apps.map((a) => a.repo_id))]), userNames(deps.db, [dev.by])]); const github = h.running.map((p) => p.stats?.github).filter((g): g is NonNullable => Boolean(g)).sort((a, b) => b.at.localeCompare(a.at))[0] ?? null; const count = (s: string) => filing.find((f) => f.status === s)?.n ?? 0; - const oldestQueued = filing.filter((f) => f.status === 'pending' || f.status === 'processing').map((f) => f.oldest).filter((n): n is number => n !== null).sort()[0] ?? null; + const oldestQueued = filing.filter((f) => f.status === 'pending' || f.status === 'processing').map((f) => f.oldest).filter((n): n is number => n !== null).sort((a, b) => a - b)[0] ?? null; return { dev: { ...dev, byName: dev.by ? names.get(dev.by) ?? null : null }, h, samples, disk, dbBytes: dbFile + walFile, files, filing: { queued: count('pending') + count('processing'), unconfirmed: count('uncertain'), failed: count('failed'), failedDay, filedDay, oldestQueued }, triage, triageCounts, repos, cleanup, claude, github }; }