mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
* fix(terminal): avoid keyword highlight flicker * fix: address Codex review on PR #2951 * fix: address Codex review on PR #2951 * perf(terminal): defer highlight catch-up under load * perf(terminal): bound deferred highlight work * fix(terminal): bound highlight catch-up backlog * fix(terminal): mirror history wipes in highlighter * fix(ci): load throughput baseline from PR base * fix(terminal): preserve highlight rebuild ordering * fix(terminal): drain highlights before exit capture * fix(terminal): preserve line state across clear * perf(terminal): slice history serialization * fix(terminal): preserve parser state on recolor * fix(terminal): preserve protocol state on recolor * fix(terminal): retain live state during recolor * fix(terminal): close recolor state gaps * fix(terminal): retain saved colors across recolor * fix(terminal): serialize recolor side effects * fix(terminal): color keywords in place without history rebuild The previous approach rewrote the stream and replayed a second terminal, which forced private xterm state snapshots and still risked flicker-adjacent reset work. Writes now stay pristine and only dirty cells get a new foreground. Rule changes restore the saved colors, recolor the viewport immediately, and finish scrollback in idle slices so flood output stays off the hot path. Related to #2784 Related to #2879 * fix(terminal): keep in-place keyword coloring correct under load Enter no longer restyles the previous row, and write/catch-up positions now follow xterm markers so saturated scrollback cannot skip a just-written match. Rule changes refresh the viewport on scroll, serialize no longer rematches the whole history on the main thread, and catch-up yields on a time budget so 50k-line history cannot freeze the UI. Related to #2784 Related to #2879 * perf(terminal): keep flood and scroll off the highlight hot path Pinned log tails were rematching the whole viewport on every output-driven scroll, and every write allocated a marker. Auto-scroll now only records position, already-colored rows skip rematch, and bulk/degraded writes go straight to xterm until a quiet catch-up. Related to #2784 Related to #2879 * perf(terminal): merge flood writes on a 16ms burst window Electerm keeps log tails smooth by coalescing attach writes for one frame after a flush. Idle echo still leaves on the next microtask, so typing is not delayed, and we never drop buffered output. Related to #2784 Related to #2879 * fix(terminal): keep keyword originals across append, clear, and alt screen Same-line appends no longer throw away saved colors just because the line fingerprint changed. Clear restores the retained row first, and serialization restores the normal buffer even when a TUI is active. Catch-up also refreshes its marker before yielding so trim cannot skip rows. Related to #2784 Related to #2879 * fix(ci): stop resetting highlight catch-up on every flood write The throughput gate was comparing cell-color wrapping to raw xterm and failing at ~18% on DOM CI. Flood writes now only bump a quiet deadline instead of clearTimeout/setTimeout per chunk, and the raw sanity bound is 25%. The product check remains 10% versus main. Related to #2784 Related to #2879 * fix(terminal): rematch recycled rows and cap wrapped catch-up Quiet catch-up now treats a recycled BufferLine as stale when its saved highlight colors are gone, so repeated keywords on a full scrollback still color. Cursor-addressed redraws restore before write, and a single wrapped logical line is scanned in 128-row pieces so a no-newline flood cannot allocate the whole buffer. Related to #2784 Related to #2879 * fix(terminal): accept plugin #RRGGBBAA highlight colors Plugin decorations allow 8-digit hex. Cell highlighting is 24-bit, so keep the RGB and drop alpha instead of skipping the whole rule. * fix(terminal): color cells for partial grapheme matches A match that ends inside a combining cell maps both endpoints to the same xterm column. Color that cell instead of building an empty range. * fix(terminal): rematch flood rows after empty-line stamps A hot-path \r\n write stamped the following empty cursor row as current. The next flood chunk reused that BufferLine with ERROR text, and quiet catch-up skipped it. Fingerprint the stamped content so filled or recycled identical rows are recolored. * perf(terminal): avoid flood-path registerMarker after trim Once a catch-up range exists, later bypassed writes only keep the earliest bound. A disposed marker collapses to 0 without allocating a new one on every saturated-scrollback chunk. * fix(terminal): recolor the row retained by clear xterm clear keeps the cursor row after we restore originals. Recolor that surviving line so enabled rules still apply until the next write. * perf(terminal): stamp no-match rows without cell snapshots Catch-up only needs a generation and fingerprint when a line has no keyword. Allocate typed-array originals when a cell is actually colored. * perf(terminal): treat pinned-bottom recycle as output scroll Once scrollback is full, baseY and viewportY stay put while rows recycle. That was treated as a user scroll, so every flood write rematched the visible area. Keep pinned-to-bottom updates off the rematch path. * perf(terminal): keep quiet catch-up to a single viewport paint Catch-up already refreshes visible slices. A trailing recolorVisible() caused a second settle paint and tripped the atomic-repaint gate. --------- Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
343 lines
12 KiB
TypeScript
343 lines
12 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { buildTerminalContextSnapshotText } from "../../domain/terminalContextRead.ts";
|
|
|
|
import {
|
|
applyAuthoritativeHibernateSnapshot,
|
|
isTerminalAlternateScreenActive,
|
|
nudgeAlternateScreenRedraw,
|
|
readTerminalHibernateContext,
|
|
refreshTerminalViewport,
|
|
resolveHibernateSerializeOptions,
|
|
resolveTerminalSnapshotCapture,
|
|
serializeTerminalForHibernate,
|
|
} from "./terminalHibernateRuntime.ts";
|
|
|
|
test("authoritative empty snapshots clear every hibernated copy", () => {
|
|
const ref = <T>(current: T) => ({ current });
|
|
const refs = {
|
|
snapshot: ref("old snapshot"),
|
|
viewportSnapshot: ref("old viewport"),
|
|
scrollbackSnapshot: ref("old scrollback"),
|
|
contextSnapshot: ref("old context"),
|
|
contextViewportSnapshot: ref("old context viewport"),
|
|
contextScrollbackSnapshot: ref("old context scrollback"),
|
|
pendingBuffer: ref("old pending"),
|
|
alternateScreen: ref(true),
|
|
};
|
|
|
|
applyAuthoritativeHibernateSnapshot(refs, "", {
|
|
contextSnapshot: "",
|
|
contextViewportSnapshot: "",
|
|
contextScrollbackSnapshot: "",
|
|
alternateScreen: false,
|
|
});
|
|
|
|
assert.deepEqual(Object.fromEntries(
|
|
Object.entries(refs).map(([key, value]) => [key, value.current]),
|
|
), {
|
|
snapshot: "",
|
|
viewportSnapshot: "",
|
|
scrollbackSnapshot: "",
|
|
contextSnapshot: "",
|
|
contextViewportSnapshot: "",
|
|
contextScrollbackSnapshot: "",
|
|
pendingBuffer: "",
|
|
alternateScreen: false,
|
|
});
|
|
});
|
|
|
|
test("authoritative non-empty snapshots keep readable hibernated context", () => {
|
|
const ref = <T>(current: T) => ({ current });
|
|
const refs = {
|
|
snapshot: ref(""),
|
|
viewportSnapshot: ref(""),
|
|
scrollbackSnapshot: ref("old scrollback"),
|
|
contextSnapshot: ref("old context"),
|
|
contextViewportSnapshot: ref("old context viewport"),
|
|
contextScrollbackSnapshot: ref("old context scrollback"),
|
|
pendingBuffer: ref("old pending"),
|
|
alternateScreen: ref(true),
|
|
};
|
|
const serialized = "\x1b[31mleft\x1b[4Cright\x1b[0m";
|
|
const context = {
|
|
contextSnapshot: "history\nleft right",
|
|
contextViewportSnapshot: "left right",
|
|
contextScrollbackSnapshot: "history",
|
|
alternateScreen: true,
|
|
};
|
|
|
|
applyAuthoritativeHibernateSnapshot(refs, serialized, context);
|
|
|
|
assert.equal(refs.snapshot.current, serialized);
|
|
assert.equal(refs.viewportSnapshot.current, serialized);
|
|
assert.equal(refs.scrollbackSnapshot.current, "");
|
|
assert.equal(refs.contextSnapshot.current, context.contextSnapshot);
|
|
assert.equal(refs.contextViewportSnapshot.current, context.contextViewportSnapshot);
|
|
assert.equal(refs.contextScrollbackSnapshot.current, context.contextScrollbackSnapshot);
|
|
assert.equal(refs.pendingBuffer.current, "");
|
|
assert.equal(refs.alternateScreen.current, true);
|
|
});
|
|
|
|
const createFakeTerm = (bufferType: "normal" | "alternate", rows = 24, length = 30) => ({
|
|
rows,
|
|
cols: 80,
|
|
buffer: {
|
|
active: {
|
|
type: bufferType,
|
|
length,
|
|
getLine(index: number) {
|
|
return {
|
|
translateToString: () => `screen-${index}`,
|
|
};
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
test("resolveHibernateSerializeOptions keeps alt buffer and modes for full-screen apps", () => {
|
|
const term = createFakeTerm("alternate");
|
|
assert.equal(isTerminalAlternateScreenActive(term as never), true);
|
|
assert.deepEqual(resolveHibernateSerializeOptions(term as never), {
|
|
excludeAltBuffer: false,
|
|
excludeModes: false,
|
|
alternateScreen: true,
|
|
});
|
|
});
|
|
|
|
test("resolveHibernateSerializeOptions excludes alt buffer on the normal screen", () => {
|
|
const term = createFakeTerm("normal");
|
|
assert.equal(isTerminalAlternateScreenActive(term as never), false);
|
|
assert.deepEqual(resolveHibernateSerializeOptions(term as never), {
|
|
excludeAltBuffer: true,
|
|
excludeModes: true,
|
|
alternateScreen: false,
|
|
});
|
|
});
|
|
|
|
test("readTerminalHibernateContext uses rendered buffer text instead of replay control sequences", () => {
|
|
const term = createFakeTerm("normal", 3, 5);
|
|
const context = readTerminalHibernateContext(term as never);
|
|
assert.equal(context.contextScrollbackSnapshot, "screen-0\nscreen-1");
|
|
assert.equal(context.contextViewportSnapshot, "screen-2\nscreen-3\nscreen-4");
|
|
assert.equal(context.contextSnapshot, "screen-0\nscreen-1\nscreen-2\nscreen-3\nscreen-4");
|
|
});
|
|
|
|
test("readTerminalHibernateContext preserves a scrolled viewport separately from full context", () => {
|
|
const term = createFakeTerm("normal", 3, 7);
|
|
Object.assign(term.buffer.active, { viewportY: 2 });
|
|
const context = readTerminalHibernateContext(term as never);
|
|
assert.equal(context.contextSnapshot, Array.from({ length: 7 }, (_, index) => `screen-${index}`).join("\n"));
|
|
assert.equal(context.contextScrollbackSnapshot, "screen-0\nscreen-1");
|
|
assert.equal(context.contextViewportSnapshot, "screen-2\nscreen-3\nscreen-4");
|
|
assert.equal(context.alternateScreen, false);
|
|
});
|
|
|
|
test("readTerminalHibernateContext marks alternate-screen viewport context", () => {
|
|
const term = createFakeTerm("alternate", 3, 3);
|
|
assert.deepEqual(readTerminalHibernateContext(term as never), {
|
|
contextSnapshot: "screen-0\nscreen-1\nscreen-2",
|
|
contextViewportSnapshot: "screen-0\nscreen-1\nscreen-2",
|
|
contextScrollbackSnapshot: "",
|
|
alternateScreen: true,
|
|
});
|
|
});
|
|
|
|
test("readTerminalHibernateContext treats an all-empty buffer as no readable context", () => {
|
|
const term = createFakeTerm("normal", 3, 3);
|
|
Object.assign(term.buffer.active, {
|
|
getLine() {
|
|
return { translateToString: () => "" };
|
|
},
|
|
});
|
|
assert.deepEqual(readTerminalHibernateContext(term as never), {
|
|
contextSnapshot: "",
|
|
contextViewportSnapshot: "",
|
|
contextScrollbackSnapshot: "",
|
|
alternateScreen: false,
|
|
});
|
|
});
|
|
|
|
test("readTerminalHibernateContext preserves blank scrollback line boundaries", () => {
|
|
const lines = ["", "", "v1", "v2", "v3"];
|
|
const term = createFakeTerm("normal", 3, lines.length);
|
|
Object.assign(term.buffer.active, {
|
|
viewportY: 2,
|
|
getLine(index: number) {
|
|
return { translateToString: () => lines[index] ?? "" };
|
|
},
|
|
});
|
|
const context = readTerminalHibernateContext(term as never);
|
|
assert.equal(context.contextSnapshot, "\n\nv1\nv2\nv3");
|
|
assert.equal(context.contextScrollbackSnapshot, "\n");
|
|
assert.equal(context.contextViewportSnapshot, "v1\nv2\nv3");
|
|
assert.deepEqual(buildTerminalContextSnapshotText({
|
|
scrollbackText: context.contextScrollbackSnapshot,
|
|
viewportText: context.contextViewportSnapshot,
|
|
pendingText: "",
|
|
}), {
|
|
fullText: "\n\nv1\nv2\nv3",
|
|
viewportStartLine: 2,
|
|
viewportEndLine: 4,
|
|
});
|
|
});
|
|
|
|
test("readTerminalHibernateContext preserves an empty viewport after readable history", () => {
|
|
const lines = ["history", "", "", ""];
|
|
const term = createFakeTerm("normal", 3, lines.length);
|
|
Object.assign(term.buffer.active, {
|
|
viewportY: 1,
|
|
getLine(index: number) {
|
|
return { translateToString: () => lines[index] ?? "" };
|
|
},
|
|
});
|
|
const context = readTerminalHibernateContext(term as never);
|
|
assert.equal(context.contextSnapshot, "history\n\n\n");
|
|
assert.equal(context.contextScrollbackSnapshot, "history");
|
|
assert.equal(context.contextViewportSnapshot, "\n\n");
|
|
assert.equal(buildTerminalContextSnapshotText({
|
|
scrollbackText: context.contextScrollbackSnapshot,
|
|
viewportText: context.contextViewportSnapshot,
|
|
pendingText: "",
|
|
}).viewportStartLine, 1);
|
|
});
|
|
|
|
test("resolveTerminalSnapshotCapture falls back for missing serializers and preserves alternate mode", () => {
|
|
const context = {
|
|
contextSnapshot: "old\nscreen",
|
|
contextViewportSnapshot: "screen",
|
|
contextScrollbackSnapshot: "old",
|
|
alternateScreen: true,
|
|
};
|
|
assert.deepEqual(resolveTerminalSnapshotCapture(undefined, context), {
|
|
snapshot: "\x1b[?1049h\x1b[Hscreen",
|
|
context,
|
|
});
|
|
assert.equal(resolveTerminalSnapshotCapture("", context).snapshot, "");
|
|
});
|
|
|
|
test("refreshTerminalViewport skips refresh when the terminal has no rows", () => {
|
|
const term = {
|
|
rows: 0,
|
|
refresh: () => {
|
|
throw new Error("refresh should not be called");
|
|
},
|
|
};
|
|
refreshTerminalViewport(term as never);
|
|
});
|
|
|
|
test("refreshTerminalViewport refreshes the full viewport", () => {
|
|
let refreshed: [number, number] | null = null;
|
|
const term = {
|
|
rows: 24,
|
|
refresh: (start: number, end: number) => {
|
|
refreshed = [start, end];
|
|
},
|
|
};
|
|
refreshTerminalViewport(term as never);
|
|
assert.deepEqual(refreshed, [0, 23]);
|
|
});
|
|
|
|
test("alternate-screen redraw never forces xterm parser flushing through resize", () => {
|
|
const refreshed: Array<[number, number]> = [];
|
|
const term = {
|
|
cols: 80,
|
|
rows: 24,
|
|
refresh: (start: number, end: number) => refreshed.push([start, end]),
|
|
resize: () => {
|
|
throw new Error("redraw must not resize while a TUI frame may still be pending");
|
|
},
|
|
};
|
|
|
|
nudgeAlternateScreenRedraw(term as never);
|
|
|
|
assert.deepEqual(refreshed, [[0, 23]]);
|
|
});
|
|
|
|
test("serializeTerminalForHibernate preserves alternate screen when serialize throws", async () => {
|
|
const term = createFakeTerm("alternate");
|
|
const serializeAddon = {
|
|
serialize: () => {
|
|
throw new Error("serialize failed");
|
|
},
|
|
};
|
|
const result = await serializeTerminalForHibernate(term as never, serializeAddon as never);
|
|
assert.equal(result.snapshot, "");
|
|
assert.equal(result.viewportSnapshot, "");
|
|
assert.equal(result.scrollbackSnapshot, "");
|
|
assert.equal(result.contextViewportSnapshot?.startsWith("screen-0\nscreen-1"), true);
|
|
assert.equal(result.alternateScreen, true);
|
|
});
|
|
|
|
test("serializeTerminalForHibernate requests viewport-only range on alternate screen", async () => {
|
|
let capturedOptions: Record<string, unknown> | undefined;
|
|
const term = createFakeTerm("alternate", 24);
|
|
const serializeAddon = {
|
|
serialize: (options?: Record<string, unknown>) => {
|
|
capturedOptions = options;
|
|
return "alt-viewport";
|
|
},
|
|
};
|
|
const result = await serializeTerminalForHibernate(term as never, serializeAddon as never);
|
|
assert.equal(result.snapshot, "alt-viewport");
|
|
assert.equal(result.contextViewportSnapshot, Array.from({ length: 24 }, (_, index) => `screen-${index}`).join("\n"));
|
|
assert.deepEqual(capturedOptions?.range, { start: 0, end: 23 });
|
|
});
|
|
|
|
test("serializeTerminalForHibernate keeps visual capture when context buffer reading fails", async () => {
|
|
const term = createFakeTerm("normal", 3, 3);
|
|
Object.assign(term.buffer.active, {
|
|
getLine() {
|
|
throw new Error("buffer unavailable");
|
|
},
|
|
});
|
|
const result = await serializeTerminalForHibernate(term as never, {
|
|
serialize: () => "serialized",
|
|
} as never);
|
|
assert.equal(result.snapshot, "serialized");
|
|
assert.equal(result.contextSnapshot, "");
|
|
assert.equal(result.contextViewportSnapshot, "");
|
|
assert.equal(result.contextScrollbackSnapshot, "");
|
|
});
|
|
|
|
test("serializeTerminalForHibernate preserves plain text context for normal screen", async () => {
|
|
const capturedRanges: unknown[] = [];
|
|
const term = createFakeTerm("normal", 3, 5);
|
|
const serializeAddon = {
|
|
serialize: (options?: Record<string, unknown>) => {
|
|
capturedRanges.push(options?.range);
|
|
return "serialized";
|
|
},
|
|
};
|
|
const result = await serializeTerminalForHibernate(term as never, serializeAddon as never);
|
|
assert.equal(result.viewportSnapshot, "serialized");
|
|
assert.equal(result.scrollbackSnapshot, "serialized");
|
|
assert.equal(result.contextScrollbackSnapshot, "screen-0\nscreen-1");
|
|
assert.equal(result.contextViewportSnapshot, "screen-2\nscreen-3\nscreen-4");
|
|
assert.equal(result.contextSnapshot, "screen-0\nscreen-1\nscreen-2\nscreen-3\nscreen-4");
|
|
assert.deepEqual(capturedRanges, [
|
|
{ start: 2, end: 4 },
|
|
{ start: 0, end: 1 },
|
|
undefined,
|
|
]);
|
|
});
|
|
|
|
test("serializeTerminalForHibernate waits for deferred history preparation", async () => {
|
|
const term = createFakeTerm("normal", 3, 5);
|
|
const events: string[] = [];
|
|
const result = await serializeTerminalForHibernate(term as never, {
|
|
serialize: () => {
|
|
events.push("serialize");
|
|
return "serialized";
|
|
},
|
|
} as never, {
|
|
prepare: async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
events.push("prepare");
|
|
},
|
|
});
|
|
|
|
assert.equal(result.snapshot, "serialized");
|
|
assert.deepEqual(events.slice(0, 2), ["prepare", "serialize"]);
|
|
});
|