From 82b78619c89e182e50ea6896c543926142bebf1f Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Mon, 9 Nov 2026 23:04:05 -0500 Subject: [PATCH] Draw History padding apart from its label so large histories open Every History row is a control padded with spaces to the drawer's width, and each reached the engine as one text: a distinct label carrying a long run of empty words. Sixty settled entries at a tall window exhausted the engine's measured-word storage while History opened, so the click refused instead of showing the drawer (#1220). History controls now emit the label and its trailing spaces as two texts in the same element, with the same colour and attributes, so identical padding shares one measurement. The bytes, widths, styles and viewport admission are unchanged. Regressions cover opening from the control at 316x120, padded cells and fresh restoration, and a cold 60-entry journey. Co-Authored-By: Claude Opus 5.5 --- packages/cli/src/repl/layout.ts | 32 ++- .../cli/tests/repl-history-capacity.test.ts | 183 ++++++++++++++++++ packages/cli/tests/repl-journey.test.ts | 64 ++++++ 3 files changed, 272 insertions(+), 7 deletions(-) create mode 100644 packages/cli/tests/repl-history-capacity.test.ts diff --git a/packages/cli/src/repl/layout.ts b/packages/cli/src/repl/layout.ts index f3d8b5e7d..f54aeac37 100644 --- a/packages/cli/src/repl/layout.ts +++ b/packages/cli/src/repl/layout.ts @@ -965,24 +965,42 @@ function written(content: string, box: ReplBox, focused: boolean): Op[] { if (style === undefined) { return [text(content)]; } + // A History row is padded with blanks to the drawer's width. The engine + // measures each text as words and keeps every distinct one for the frame, so + // a label carrying its own padding is a long run of empty words per ordinal; + // a few dozen rows exhausted that storage before the drawer could be drawn. + // Drawn apart, identical padding is one measurement shared by every row. + const padded = box.control && style.role === "history"; + const draw = (piece: string, colour: number, attrs: number): Op[] => + (padded ? apart(piece) : [piece]).map((one) => drawn(one, colour, attrs)); const { runs } = box; const room = roomOf(box.props); if (runs === undefined || runs.length === 0 || runText(runs) !== content || room === "unknown") { const { colour, attrs } = textStyleOf(style, focused); - return [drawn(content, colour, attrs)]; + return draw(content, colour, attrs); } - return bounded(runs, room).map((run) => { + return bounded(runs, room).flatMap((run) => { if (run.token === "caret") { - return text(run.text, { - color: terminalColour(REPL_PALETTE.composerSurface), - bg: terminalColour(REPL_PALETTE.focus), - }); + return [ + text(run.text, { + color: terminalColour(REPL_PALETTE.composerSurface), + bg: terminalColour(REPL_PALETTE.focus), + }), + ]; } const { colour, attrs } = runStyleOf(run.token, style, focused); - return drawn(run.text, colour, attrs); + return draw(run.text, colour, attrs); }); } +/** A label and the spaces after it, as two texts; a text that is all one or the other stays whole. */ +function apart(content: string): readonly string[] { + const label = content.replace(/ +$/, ""); + return label.length === 0 || label.length === content.length + ? [content] + : [label, content.slice(label.length)]; +} + /** * How many columns this element gives its text. * diff --git a/packages/cli/tests/repl-history-capacity.test.ts b/packages/cli/tests/repl-history-capacity.test.ts new file mode 100644 index 000000000..3826d7856 --- /dev/null +++ b/packages/cli/tests/repl-history-capacity.test.ts @@ -0,0 +1,183 @@ +/** + * History opens over a long retained history without exhausting measurement (#1220). + * + * Every History row is a control padded with spaces to the drawer's width, and + * each one used to reach the engine as one text: its distinct label and its + * long run of blanks were measured as one string of empty words, a fresh one + * per ordinal. Sixty settled entries at a tall window filled the engine's + * measured-word storage before the drawer could be drawn, so opening History + * refused instead of showing it. The padding now reaches the engine as its own + * text beside the label, and identical blanks share one measurement. + */ + +import { describe, it } from "@executablemd/test-support/bdd"; +import { expect } from "@executablemd/test-support/expect"; +import { scoped } from "effection"; +import type { Operation } from "effection"; + +import { + initialState, + NO_AGENT, + reduceRepl, + viewFor, + withWorkspace, + workspaceOf, +} from "../src/repl/application.ts"; +import type { ReplState, ReplView } from "../src/repl/application.ts"; +import { NO_LIFECYCLE } from "../src/repl/lifecycle.ts"; +import type { ReplModel } from "../src/repl/model.ts"; +import { navigationOf } from "../src/repl/navigation.ts"; +import type { ReplTerminalSize } from "../src/repl/terminal.ts"; +import { journalModel } from "./fixtures/repl/journals.ts"; +import { usePresenter } from "./fixtures/repl/presentation.ts"; +import type { Observed, Presenter } from "./fixtures/repl/presentation.ts"; + +const TALL: ReplTerminalSize = { columns: 316, rows: 120 }; +const NARROW: ReplTerminalSize = { columns: 72, rows: 20 }; +const LIVE = { + output: "", + question: undefined, + expansion: "playing", + pausable: false, + running: false, + agent: NO_AGENT, + lifecycle: NO_LIFECYCLE, +} as const; + +/** Sixty settled plain-text entries: one hundred and twenty positions. */ +function largeHistory(): Operation { + return journalModel(Array.from({ length: 60 }, (_, at) => `Public history sample ${at + 1}.\n`)); +} + +function viewOf( + state: ReplState, + model: ReplModel, + size: ReplTerminalSize, + focused?: string, +): ReplView { + const view = viewFor(state, model, LIVE, size, focused, navigationOf(model.checkpoints, "live")); + if (!view.ok) { + throw view.error; + } + return withWorkspace(view.value, workspaceOf(model)); +} + +/** Press the frame's own `[history]` control and reduce what it asked for. */ +function* pressHistory( + presenter: Presenter, + observed: Observed, + state: ReplState, + model: ReplModel, +): Operation { + const bounds = observed.boundsOf("footer:history"); + if (bounds === undefined) { + throw new Error("this frame drew no [history] control"); + } + const dispatched = yield* presenter.tree.dispatch({ + kind: "pointer", + target: observed.committed.rendered.map.at(bounds.x, bounds.y) ?? "", + frame: observed.committed.rendered.tree, + }); + if (!dispatched.ok || dispatched.value.outcome !== "action") { + throw new Error("the [history] control asked for nothing"); + } + return reduceRepl(state, dispatched.value.action, model, LIVE).state; +} + +function markers(observed: Observed): readonly string[] { + return observed.keys.filter((key) => key.startsWith("drawer:marker:")); +} + +describe("History over a long retained history", () => { + it("H1: opens from its control at 316x120 with every admitted row placed", function* () { + const model = yield* largeHistory(); + expect(model.checkpoints.length).toBe(120); + const presenter = yield* usePresenter(TALL); + const closed = initialState("public-history-capacity"); + const first = yield* presenter.commit(viewOf(closed, model, TALL)); + const opened = yield* pressHistory(presenter, first, closed, model); + expect(opened.route.drawers.map((one) => one.kind)).toEqual(["history"]); + + const drawn = yield* presenter.commit(viewOf(opened, model, TALL)); + const again = yield* presenter.commit(viewOf(opened, model, TALL)); + const admitted = markers(drawn); + expect(admitted.length).toBeGreaterThan(20); + expect(markers(again)).toEqual(admitted); + // In recorded order, from the first position. + expect(admitted).toEqual( + model.checkpoints.slice(0, admitted.length).map((one) => `drawer:marker:${one.marker}`), + ); + // One column, one row each, one under the other, each as wide as the next. + const bounds = admitted.map((key) => drawn.boundsOf(key)); + const placed = bounds.flatMap((one) => (one === undefined ? [] : [one])); + expect(placed.length).toBe(admitted.length); + expect(new Set(placed.map((one) => `${one.x}:${one.width}:${one.height}`)).size).toBe(1); + expect(placed[0]?.height).toBe(1); + for (const [at, one] of placed.entries()) { + expect(one.y).toBe((placed[0]?.y ?? 0) + at); + } + const label = presenter.grid.textIn(placed[0] ?? { x: 0, y: 0, width: 0, height: 0 })[0] ?? ""; + expect(label.trimEnd()).toContain("1 ยท Entry 1 admitted"); + expect(presenter.renderer.engines()).toEqual({ measuring: 1, drawing: 1 }); + }); + + it("H4: a padded row keeps its label, blanks and surface, and restores fresh", function* () { + const model = yield* largeHistory(); + const closed = initialState("public-history-capacity"); + const opened = Object.freeze({ + ...closed, + route: Object.freeze({ + ...closed.route, + drawers: Object.freeze([Object.freeze({ kind: "history" as const })]), + }), + }); + const whole = (presenter: Presenter): readonly string[] => + presenter.grid.styledIn({ x: 0, y: 0, width: TALL.columns, height: TALL.rows }); + + const incremental = yield* scoped(function* () { + const presenter = yield* usePresenter(TALL); + yield* presenter.commit(viewOf(closed, model, TALL)); + const drawn = yield* presenter.commit(viewOf(opened, model, TALL)); + const [first, second] = markers(drawn); + if (first === undefined || second === undefined) { + throw new Error("History admitted fewer than two rows"); + } + // Focus moves onto the second row and back to the first. + yield* presenter.commit(viewOf(opened, model, TALL, second)); + const focused = yield* presenter.commit(viewOf(opened, model, TALL, first)); + const row = focused.boundsOf(second); + if (row === undefined) { + throw new Error("the second History row was not placed"); + } + // The label, then blank cells to the row's edge, all on the row's surface. + const cells = presenter.grid.textIn(row)[0] ?? ""; + expect(cells.length).toBe(row.width); + expect(cells.trimEnd().length).toBeLessThan(row.width - 20); + expect(cells.slice(cells.trimEnd().length).trim()).toBe(""); + const surface = presenter.grid.styleAt(row.x, row.y).background; + expect(surface).toBeDefined(); + for (let x = row.x; x < row.x + row.width; x += 1) { + expect([x, presenter.grid.styleAt(x, row.y).background]).toEqual([x, surface]); + } + // Closed, reopened, taken narrow and brought back. + yield* presenter.commit(viewOf(closed, model, TALL)); + yield* presenter.commit(viewOf(opened, model, TALL)); + presenter.resize(NARROW); + yield* presenter.commit(viewOf(opened, model, NARROW)); + presenter.resize(TALL); + const last = yield* presenter.commit(viewOf(opened, model, TALL, first)); + expect(presenter.renderer.engines()).toEqual({ measuring: 1, drawing: 1 }); + return { cells: whole(presenter), keys: last.keys.slice().sort() }; + }); + + const fresh = yield* scoped(function* () { + const presenter = yield* usePresenter(TALL); + const drawn = yield* presenter.commit(viewOf(opened, model, TALL)); + const last = yield* presenter.commit(viewOf(opened, model, TALL, markers(drawn)[0])); + return { cells: whole(presenter), keys: last.keys.slice().sort() }; + }); + + expect(incremental.cells).toEqual(fresh.cells); + expect(incremental.keys).toEqual(fresh.keys); + }); +}); diff --git a/packages/cli/tests/repl-journey.test.ts b/packages/cli/tests/repl-journey.test.ts index a266c1c55..8c86eb8ff 100644 --- a/packages/cli/tests/repl-journey.test.ts +++ b/packages/cli/tests/repl-journey.test.ts @@ -2199,6 +2199,70 @@ describe("REPL original complete History and cold catalog", () => { expect(yield* readTextFile(hot.path)).toBe(bytes); released(page.repl.terminal); }); + + it("EC3: a long cold history opens History at a tall window and reaches every position", function* () { + // Sixty settled entries at 316x120 once exhausted the engine's measured + // words the moment History was opened, before it could be drawn (#1220). + const tall = { columns: 316, rows: 120 }; + const dataRoot = yield* useTemporary("xmd-cold-long-history-"); + const hot = yield* scoped(function* () { + const page = yield* useReplPage({ dataRoot, includes: [REFERENCE_DIRECTORY], size: tall }); + for (let order = 1; order <= 60; order += 1) { + yield* runSource(page, `Public history sample ${order}.\n`); + yield* page.until(`entry ${order} settling`, (one) => + one.shows(`Ready for Entry ${order + 1}`), + ); + } + const model = yield* retainedModel(page); + yield* draftText(page, "kept draft"); + return { + expected: expectedPositions(model), + sources: model.entries.map((one) => one.source), + location: yield* leaveWithLocation(page), + path: yield* executionJournal(page), + }; + }); + expect(hot.expected.length).toBe(120); + const bytes = yield* readTextFile(hot.path); + const host = yield* useMountedHost({ dataRoot, includes: [REFERENCE_DIRECTORY] }); + yield* noNewExecution(); + const work = yield* performedWork(); + const page = yield* useReplPage({ host, location: hot.location, size: tall }); + const kept = (one: ScreenReading): boolean => one.found("kept draft", DRAFT_VALUE) === 1; + expect(kept(page.screen())).toBe(true); + + yield* openHistory(page); + expect((yield* allPositions(page, hot.expected)).reached).toEqual(hot.expected); + // The earliest prefix still in the window paging left it at. + const prefix = positionLabels(page.screen())[0] ?? ""; + expect(hot.expected.indexOf(prefix)).toBeLessThan(hot.expected.length - 1); + yield* page.press(prefix); + yield* page.until("the earlier prefix", (one) => one.shows("[live]")); + yield* closeDrawer(page); + yield* page.press("[live]"); + yield* page.until("the head", (one) => !one.shows("[live]")); + expect(kept(page.screen())).toBe(true); + + yield* openHistory(page); + yield* closeDrawer(page); + for (const size of [NARROW, tall]) { + const shown = yield* page.resize(size); + expect(shown.rows.filter((row) => row.length > size.columns)).toEqual([]); + } + expect(kept(page.screen())).toBe(true); + // Reopened at the head, the last position is in the window again. + yield* openHistory(page); + expect(positionLabels(page.screen())).toContain(hot.expected.at(-1)); + yield* closeDrawer(page); + + expect((yield* retainedModel(page)).entries.map((one) => one.source)).toEqual(hot.sources); + expect(route(yield* leaveWithLocation(page)).draft).toBe("kept draft"); + expect(work.compiles).toBe(0); + expect(work.asked).toBe(0); + expect(work.reads.filter((one) => one.endsWith(".md"))).toEqual([]); + expect(yield* readTextFile(hot.path)).toBe(bytes); + released(page.repl.terminal); + }); }); function referencePageFor(dataRoot: string): Operation { return useReplPage({ dataRoot, includes: [REFERENCE_DIRECTORY], size: WIDE });