Files
loki/scripts/test/no-raw-sql-comparisons.ts
T
CatoandClaude Opus 5.5 0c6e05508e fix(ops): a site that hangs is DOWN, not up; raw sql Date comparisons fixed (#996)
loki.orangecat.ch was down ~06:00-07:53 UTC on 2026-10-01 (next-server
alive, no listening socket; Caddy 502 after 20s) and nobody was paged. The
watchdog's curl times out at 15s, prints 000 itself, and the fallback
'|| echo 000' appended another: "000000" is neither "000" nor a number, so
check() kept the site UP. Any app that hangs rather than refuses was invisible.
check() now counts UP only on a proven 2xx/3xx; test-watchdog-check.sh runs the
shipped check() against a fake curl (old code fails timeout/refused/garbage).
The deploy verifiers had the same append (failing closed) and are normalised.

Also from the same journal: closeStaleAgentTurns threw ERR_INVALID_ARG_TYPE
every hour — postgres-js cannot serialise a Date in a raw sql fragment. Same
shape in the calendar overlap, the digest previous window and two more;
all now use lt/gt/gte, and no-raw-sql-comparisons.ts keeps it that way.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-01 01:23:15 -07:00

37 lines
1.3 KiB
TypeScript

/**
* Compare columns with drizzle's lt/gt/lte/gte, never with a raw sql`a < ${b}`.
*
* postgres-js cannot serialise a JS Date inside a raw fragment: it throws
* ERR_INVALID_ARG_TYPE at query time. closeStaleAgentTurns did exactly that on
* every hourly reap (seen 2026-10-01), and the same shape sat in the calendar
* overlap and the digest's previous-window count — failing quietly, because a
* thrown query in a background job looks like "no data". The typed helpers
* map the value through the column, so a Date is always encoded correctly.
*/
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
const RAW_COMPARE = /sql`\$\{[^}]+\}\s*(<=|>=|<|>)\s*\$\{[^}]+\}`/;
const offenders: string[] = [];
function walk(dir: string) {
for (const e of readdirSync(dir, { withFileTypes: true })) {
const p = join(dir, e.name);
if (e.isDirectory()) walk(p);
else if (/\.tsx?$/.test(e.name)) {
readFileSync(p, "utf8")
.split("\n")
.forEach((line, i) => {
if (RAW_COMPARE.test(line)) offenders.push(`${p}:${i + 1}: ${line.trim()}`);
});
}
}
}
walk("src");
assert.equal(
offenders.length,
0,
`raw sql comparisons (use lt/gt/lte/gte from drizzle-orm):\n${offenders.join("\n")}`,
);
console.log("no-raw-sql-comparisons: ok");