From 3b4b04ca1008ea2a27b45a3ee5eb8b0cd7dbfd29 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Sun, 22 Nov 2026 15:45:53 +0000 Subject: [PATCH] Job log copies drop control characters and survive one bad row --- src/runner/job-log.ts | 25 ++++++++++++++++++++++--- test/job-log.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/runner/job-log.ts b/src/runner/job-log.ts index 34f0d573..e08e6a91 100644 --- a/src/runner/job-log.ts +++ b/src/runner/job-log.ts @@ -11,6 +11,9 @@ export const JOB_LOG_KEEP_BYTES = 256 * 1024; /** Chunks are dropped once the kept text passes the cap by this much, so the DELETE runs once per this many bytes, not per copy. */ const DROP_SLACK_BYTES = 64 * 1024; export const JOB_LOG_FLUSH_MS = 5_000; +/** Postgres text cannot hold NUL; the other C0 controls (bar tab, newline, carriage return) are terminal noise. */ +// eslint-disable-next-line no-control-regex +const CONTROLS = /[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g; /** The bytes of `path` from `from` (an offset, or one computed from the size) to its end; null when the file cannot be read. */ function readRange(path: string, from: number | ((size: number) => number)): { buf: Buffer; start: number; size: number } | null { @@ -81,7 +84,7 @@ export class JobLogCopy { buf = buf.subarray(0, nl + 1); } if (!buf.length) return null; - return { job_id: this.job, seq: this.#seq + 1, start, next_start: start + buf.length, chunk: scrub(buf.toString("utf8")), at: new Date(nowMs).toISOString() }; + return { job_id: this.job, seq: this.#seq + 1, start, next_start: start + buf.length, chunk: scrub(buf.toString("utf8").replace(CONTROLS, "")), at: new Date(nowMs).toISOString() }; } /** Records a written chunk; the highest seq to drop once the kept text passes the cap, else 0. */ @@ -110,8 +113,24 @@ export function copyLogs(db: Db, copies: JobLogCopy[], final = false): Promise { const n = c.next(final, nowMs); return n ? [{ copy: c, n }] : []; }); if (!chunks.length) return; - await db.run(db.k.insertInto("job_log_chunks").values(chunks.map(({ n }) => n))); - const drops = chunks.flatMap(({ copy, n }) => { const upTo = copy.wrote(n); return upTo ? [{ job: copy.job, upTo }] : []; }); + let stored = chunks; + try { + await db.run(db.k.insertInto("job_log_chunks").values(chunks.map(({ n }) => n))); + } catch (e) { + // One bad row must not block the others: write each job's chunk on its own. + if (chunks.length < 2) throw e; + stored = []; + for (const c of chunks) { + try { + await db.run(db.k.insertInto("job_log_chunks").values(c.n)); + stored.push(c); + } catch (err) { + console.error(`job log copy failed for job ${c.copy.job}: ${(err as Error).message}`); + } + } + if (!stored.length) throw e; + } + const drops = stored.flatMap(({ copy, n }) => { const upTo = copy.wrote(n); return upTo ? [{ job: copy.job, upTo }] : []; }); if (drops.length) await db.run(db.k.deleteFrom("job_log_chunks").where((eb) => eb.or(drops.map((d) => eb.and([eb("job_id", "=", d.job), eb("seq", "<=", d.upTo)]))))); }); queue = run.catch(() => { /* the caller sees the failure; the next copy reads the lines again */ }); diff --git a/test/job-log.test.ts b/test/job-log.test.ts index 951434f8..56a53775 100644 --- a/test/job-log.test.ts +++ b/test/job-log.test.ts @@ -46,3 +46,26 @@ test("a long job log keeps its end within the cap, and the job page reads it wit rmSync(dir, { recursive: true, force: true }); } }); + +test("a log chunk with a NUL byte is copied without it, and the next chunk still starts at the right offset", async () => { + const dir = mkdtempSync(join(SCRATCH, "job-log-")); + const db = await connectDb(join(dir, "jobs.db")); + try { + const path = join(dir, "job.log"); + const t = new Date().toISOString(); + const id = Number((await db.get(db.k.insertInto("jobs").values({ step: "test-echo", params: "{}", requested_by: "boss", requested_at: t, state: "running", started_at: t, log_path: path }) + .returning("id")))!.id); + const copy = await JobLogCopy.open(db, id, path); + appendFileSync(path, "a\u0000b\u0007c\td\n"); + await copyLogs(db, [copy], true); + appendFileSync(path, "second\n"); + await copyLogs(db, [copy], true); + const rows = await db.all(db.k.selectFrom("job_log_chunks").select(["chunk", "start", "next_start"]).where("job_id", "=", id).orderBy("seq")); + assert.equal(rows[0]!.chunk, "abc\td\n"); + assert.equal(Number(rows[1]!.start), Number(rows[0]!.next_start)); + assert.equal(rows[1]!.chunk, "second\n"); + } finally { + await db.close(); + rmSync(dir, { recursive: true, force: true }); + } +});