Files
DeepTutor/web/tests/chat-outline.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

177 lines
5.4 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
buildChatOutline,
plainTextPreview,
turnAnchorKey,
} from "../lib/chat-outline";
type Msg = Parameters<typeof buildChatOutline>[0][number];
function conversation(): Msg[] {
return [
{
id: 1,
role: "user",
content: "What is gradient descent?",
parentMessageId: null,
},
{
id: 2,
role: "assistant",
content: "It **walks** downhill.",
parentMessageId: 1,
},
{ id: 3, role: "user", content: "And momentum?", parentMessageId: 2 },
{
id: 4,
role: "assistant",
content: "It smooths the path.",
parentMessageId: 3,
},
];
}
test("one entry per user turn, numbered and paired with its reply", () => {
const outline = buildChatOutline(conversation(), {});
assert.equal(outline.length, 2);
assert.deepEqual(
outline.map((e) => [e.ordinal, e.title, e.reply]),
[
[1, "What is gradient descent?", "It walks downhill."],
[2, "And momentum?", "It smooths the path."],
],
);
});
test("entry keys match the anchor the bubble renders", () => {
const messages = conversation();
const outline = buildChatOutline(messages, {});
// The bubble receives its index within the visible path; the outline
// must key off the same pair so the rail can find the DOM node.
assert.equal(outline[0].key, turnAnchorKey(messages[0], 0));
assert.equal(outline[1].key, turnAnchorKey(messages[2], 2));
});
test("id-less rows fall back to a positional key", () => {
assert.equal(turnAnchorKey({ id: 7 }, 3), "m7");
assert.equal(turnAnchorKey({ id: -1700000 }, 3), "m-1700000");
assert.equal(turnAnchorKey({}, 3), "i3");
});
test("only the selected edit branch earns ticks", () => {
const messages: Msg[] = [
{ id: 1, role: "user", content: "first draft", parentMessageId: null },
{ id: 2, role: "assistant", content: "draft reply", parentMessageId: 1 },
{ id: 3, role: "user", content: "edited draft", parentMessageId: null },
{ id: 4, role: "assistant", content: "edited reply", parentMessageId: 3 },
];
// Default: latest sibling wins.
assert.deepEqual(
buildChatOutline(messages, {}).map((e) => e.title),
["edited draft"],
);
// Switching the branch reshapes the rail with the transcript.
assert.deepEqual(
buildChatOutline(messages, { null: 1 }).map((e) => e.title),
["first draft"],
);
});
test("backend-synthesised user rows are not navigable", () => {
const messages: Msg[] = [
{ id: 1, role: "user", content: "Quiz me", parentMessageId: null },
{ id: 2, role: "assistant", content: "Here you go.", parentMessageId: 1 },
{
id: 3,
role: "user",
content: "[Quiz Performance] 3/5 correct",
parentMessageId: 2,
},
{ id: 4, role: "assistant", content: "Review these.", parentMessageId: 3 },
];
assert.deepEqual(
buildChatOutline(messages, {}).map((e) => e.title),
["Quiz me"],
);
});
test("system grounding rows never break the question/reply pairing", () => {
const messages: Msg[] = [
{ id: 1, role: "user", content: "Explain entropy", parentMessageId: null },
{ id: 2, role: "system", content: "context blob", parentMessageId: 1 },
{
id: 3,
role: "assistant",
content: "Disorder, measured.",
parentMessageId: 2,
},
];
const outline = buildChatOutline(messages, {});
assert.equal(outline[0].reply, "Disorder, measured.");
});
test("a content-free planning turn does not hide the real answer", () => {
const messages: Msg[] = [
{
id: 1,
role: "user",
content: "Research LLM routing",
parentMessageId: null,
},
{ id: 2, role: "assistant", content: "", parentMessageId: 1 },
{
id: 3,
role: "assistant",
content: "Here is the report.",
parentMessageId: 2,
},
];
assert.equal(buildChatOutline(messages, {})[0].reply, "Here is the report.");
});
test("weights are relative to the longest question in the session", () => {
const messages: Msg[] = [
{ id: 1, role: "user", content: "hi", parentMessageId: null },
{ id: 2, role: "assistant", content: "hello", parentMessageId: 1 },
{ id: 3, role: "user", content: "x".repeat(120), parentMessageId: 2 },
];
const [short, long] = buildChatOutline(messages, {});
assert.equal(long.weight, 1);
assert.ok(short.weight > 0 && short.weight < 0.3);
});
test("empty session yields no ticks", () => {
assert.deepEqual(buildChatOutline([], {}), []);
});
test("preview flattens markdown to one readable line", () => {
assert.equal(
plainTextPreview("# Title\n\nSome **bold** and `code` text."),
"Title Some bold and code text.",
);
assert.equal(
plainTextPreview("See [the docs](https://example.com/a?b=c) for more."),
"See the docs for more.",
);
assert.equal(plainTextPreview("![diagram](a.png)\n\nAfter."), "After.");
assert.equal(
plainTextPreview("Before\n```py\nprint('x')\n```\nAfter"),
"Before After",
);
assert.equal(
plainTextPreview("- one\n- two\n\n> quoted\n\n---\n\n1. three"),
"one two quoted three",
);
});
test("preview survives an unterminated code fence mid-stream", () => {
assert.equal(plainTextPreview("Here:\n```py\nprint('x')"), "Here:");
});
test("preview truncates on a character budget with an ellipsis", () => {
const long = plainTextPreview("word ".repeat(80), 40);
assert.ok(long.length <= 41, long);
assert.ok(long.endsWith("…"));
});