Files
DeepTutor/web/tests/dev-heap-ceiling.test.ts
Bingxi Zhao (Frank) 44fa7a1552 release: v1.5.8
Memory. A v1.5.7 source deployment grew past 14 GB before V8 aborted; the
causes were independent, so this cut fixes each one and bounds what was
previously unbounded.

- The dev server was never actually capped: `next dev` renders in a worker it
  spawns, and V8 flags in argv are not inherited, so
  `node --max-old-space-size=4096 .../next dev` never reached the process
  holding the memory. Next then substituted 50% of total RAM — a 12 GB heap on
  a 24 GB machine. web/scripts/dev.mjs routes the ceiling through NODE_OPTIONS
  (which the worker inherits and Next honors), clamped to 4 GB and sized off
  the cgroup limit in a container.
- Source installs serve a production build: `deeptutor start` builds once into
  .next-deeptutor and reuses it until a fingerprint over the web/ tree, the
  version file, or the build-time public settings changes. `--dev` is the
  opt-in for HMR and keeps using .next, so neither mode invalidates the other.
- LLM clients and providers come from a bounded event-loop-local LRU pool of 2
  instead of one per turn, closed on shutdown and retired on a settings
  reload; only the selected backend's SDK is imported.
- Caches gained ceilings: knowledge-task log streams cap events and bytes per
  task with a 1h TTL and a completion tombstone, the completed-task table
  keeps 256, the LlamaIndex index cache drops 8 -> 2 with a 10-minute idle
  evict, and a book runtime is released when its queue drains.
- After a turn, a finished book job, or a completed KB task, cycles are
  collected and glibc is asked to return free pages via malloc_trim; the
  container and launcher set MALLOC_ARENA_MAX=2 and a lower trim threshold,
  both operator-overridable.
- Every uvicorn launch sets --timeout-keep-alive 300. web/proxy.ts forwards
  over Node's http.globalAgent, whose 5s idle reaper matched uvicorn's default
  exactly; both ends raced to close the same socket and the loser's request
  died with ECONNRESET ("Failed to load sessions").
- deeptutor.agents, deeptutor.core.agentic, provider_core, the document
  extractor's parser libraries and the LLM error-mapping rules resolve on
  first use. `python -m deeptutor.api.run_server` no longer reloads by
  default (DEEPTUTOR_DEV_RELOAD=1 restores it).

README prose and all ten translated READMEs now describe the production-build
default instead of dev mode. Release notes: assets/releases/ver1-5-8.md
2026-08-02 13:23:45 +08:00

87 lines
3.0 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import path from "node:path";
/**
* `next dev` renders in a worker it spawns itself, and V8 flags in argv are not
* inherited by child processes — so a `node --max-old-space-size=N .../next dev`
* script silently does nothing for the process that holds the memory, and Next
* substitutes 50% of total RAM (12GB on a 24GB machine) instead. That shipped
* for a while and ended in `Ineffective mark-compacts near heap limit`.
* scripts/dev.mjs is the fix: the ceiling goes through NODE_OPTIONS, which the
* worker inherits and Next honors. Every dev launch point must route through it
* or the override quietly comes back.
*/
// The node-test harness runs from web/ (see scripts/run-node-tests.mjs).
const webRoot = process.cwd();
const repoRoot = path.resolve(webRoot, "..");
const read = (...parts: string[]) => readFileSync(path.join(...parts), "utf8");
test("npm dev scripts route through the heap-ceiling wrapper", () => {
const scripts = JSON.parse(read(webRoot, "package.json")).scripts as Record<
string,
string
>;
for (const name of ["dev", "dev:turbo"]) {
const script = scripts[name];
assert.ok(script, `package.json is missing the ${name} script`);
assert.match(
script,
/scripts\/dev\.mjs/,
`${name} must launch via scripts/dev.mjs, got: ${script}`,
);
assert.doesNotMatch(
script,
/--max-old-space-size/,
`${name} passes the heap flag in argv, where the render worker never sees it`,
);
}
});
test("the container dev stage routes through the wrapper too", () => {
// Command lines only — prose mentions of `next dev` are not launch points.
const devFrontend = read(repoRoot, "Dockerfile")
.split("\n")
.filter((line) => !line.trimStart().startsWith("#"))
.filter(
(line) =>
line.includes("bin/next dev") || line.includes("scripts/dev.mjs"),
);
assert.ok(
devFrontend.length > 0,
"no dev frontend command found in Dockerfile",
);
for (const line of devFrontend) {
assert.match(
line,
/scripts\/dev\.mjs/,
`Dockerfile dev frontend bypasses the wrapper: ${line.trim()}`,
);
}
});
test("the wrapper caps the ceiling and respects an explicit NODE_OPTIONS", () => {
const source = read(webRoot, "scripts", "dev.mjs");
// A ceiling only helps if it is a ceiling: Next's own 50%-of-RAM rule is what
// produced the 12GB heap, so the wrapper must clamp rather than mirror it.
assert.match(source, /Math\.min\(/, "wrapper must clamp the computed size");
assert.match(
source,
/NODE_OPTIONS/,
"the ceiling has to travel via NODE_OPTIONS to reach the worker",
);
assert.match(
source,
/max\[-_\]old\[-_\]space\[-_\]size/,
"wrapper must detect an inherited ceiling instead of appending a second one",
);
assert.match(
source,
/HEAP_CEILING_MB\s*=\s*4096/,
"the fallback ceiling must preserve the original 4GB dev-server budget",
);
});