fix(preview): anonymize form previews on a worker, trace slow ones to their project

A user's backtracking term made each anonymize-form preview block the API's
event loop for ~1.2 s and fail with a 500. One per keystroke, 45 of them
froze every request for about a minute (12:59 UTC, 2026-11-30).

- Previews run on their own worker pool (1 worker by default). A preview
  that runs past its deadline (3 s) terminates its worker and answers
  422 anonymization_timeout; the form explains the term is too slow.
- One preview per user: a newer edit, or the client leaving, stops the
  older preview (409 preview_superseded, logged at info).
- Slow (>=500 ms) or failed previews log an anonymization_preview record
  with the project's repoId, input bytes and term count.
- Heartbeats carry the interval's worst event-loop delay (loopMaxMs,
  loopP99Ms); a delay of 1 s or more logs event_loop_blocked at warn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-30 13:17:35 +00:00
1 parent 049195439a
commit fc14b2c3ae
16 files changed
+385 -73

No files matched your search

+3 -1
View File
@@ -5,7 +5,8 @@ const { AnonymizeTransformer: Anonymize } = require("../src/core/anonymize-utils
const router = require("../src/server/routes/anonymize-preview").default;
const handler = router.stack.find(layer => layer.route?.methods.post).route.stack[0].handle;
describe("POST /api/anonymize-preview preservation", () => {
describe("POST /api/anonymize-preview preservation", function () {
this.timeout(20000); // The first preview starts its worker.
const content = "Jane Smith https://example.org ![Jane Smith](plot.png)";
const options = { terms: ["Jane Smith", "Smith"], image: false, link: false };
function request(body) {
@@ -13,6 +14,7 @@ describe("POST /api/anonymize-preview preservation", () => {
res.status = code => { res.statusCode = code; return res; };
res.set = (key, value) => { res.headers[key] = value; return res; };
res.json = body => { res.body = body; return res; };
res.once = () => res;
return handler({ body }, res).then(() => res);
}
for (const [filePath, preserveFiles] of [
+53
View File
@@ -0,0 +1,53 @@
const { expect } = require("chai");
require("ts-node/register/transpile-only");
const { monitorEventLoopDelay } = require("node:perf_hooks");
const { setTimeout } = require("node:timers");
const { anonymizePreview } = require("../src/core/preview-anonymizer");
const SLOW_TERM = "a+a+a+a+a+a+b(?=x)";
describe("anonymize-form preview off the main thread", function () {
this.timeout(20000);
it("anonymizes on a worker", async () => {
const [out] = await anonymizePreview("user-ok", ["Alice wrote this"], { terms: ["Alice"] });
expect(out).to.match(/^\S+ wrote this$/);
expect(out).to.not.include("Alice");
});
it("answers a backtracking term with a coded timeout and keeps the event loop free", async () => {
const loop = monitorEventLoopDelay({ resolution: 10 });
loop.enable();
let failure;
try { await anonymizePreview("user-slow", ["a".repeat(250)], { terms: [SLOW_TERM] }); }
catch (error) { failure = error; }
loop.disable();
expect(failure && failure.message).to.equal("anonymization_timeout");
expect(failure.httpStatus).to.equal(422);
// The regex ran for about a second on the worker; the main loop stayed responsive.
expect(loop.max / 1e6).to.be.lessThan(300);
// The pool recovers after the stalled worker is replaced.
const [out] = await anonymizePreview("user-slow", ["Bob"], { terms: ["Bob"] });
expect(out).to.not.include("Bob");
});
it("keeps one preview per user: a newer edit supersedes the older one", async () => {
const older = anonymizePreview("user-typing", ["a".repeat(250)], { terms: [SLOW_TERM] }).then(() => null, error => error);
const newer = anonymizePreview("user-typing", ["Carol"], { terms: ["Carol"] });
const [olderResult, [newerOut]] = await Promise.all([older, newer]);
expect(olderResult && olderResult.message).to.equal("preview_superseded");
expect(olderResult.httpStatus).to.equal(409);
expect(newerOut).to.not.include("Carol");
});
it("stops a preview whose client went away", async () => {
const controller = new AbortController();
const pending = anonymizePreview("user-left", ["a".repeat(250)], { terms: [SLOW_TERM] }, controller.signal)
.then(() => null, error => error);
setTimeout(() => controller.abort(), 50);
const started = Date.now();
const error = await pending;
expect(error && error.message).to.equal("preview_superseded");
expect(Date.now() - started).to.be.lessThan(800);
});
});