Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): fit HITL drawers within terminal dimensions (#5176)

Colton Padden580417feaa56
diff --git a/.changeset/fit-hitl-terminal-drawers.md b/.changeset/fit-hitl-terminal-drawers.md
new file mode 100644
index 000000000..1400f7ffe
--- /dev/null
+++ b/.changeset/fit-hitl-terminal-drawers.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Fix repeated horizontal rules and question text accumulating in terminal scrollback during human-in-the-loop prompts. Question and approval drawers now fit small terminals, keeping the focused choice or text editor visible.
diff --git a/packages/eve/src/cli/dev/tui/question-panel.test.ts b/packages/eve/src/cli/dev/tui/question-panel.test.ts
index fa7126c50..8c4fc2526 100644
--- a/packages/eve/src/cli/dev/tui/question-panel.test.ts
+++ b/packages/eve/src/cli/dev/tui/question-panel.test.ts
@@ -96,6 +96,34 @@ describe("renderQuestionPanel", () => {
expect(rows.every((row) => !row.includes("\n"))).toBe(true);
});
+ it("keeps the selected choice and its description visible in a short viewport", () => {
+ const options = [
+ { id: "production", label: "Production", description: "Deploy to production" },
+ { id: "staging", label: "Staging", description: "Deploy to staging" },
+ { id: "preview", label: "Preview", description: "Deploy to preview\nNo production changes" },
+ ];
+ for (const [cursor, option] of options.entries()) {
+ const rows = renderQuestionPanel(
+ {
+ prompt: "Which environment should Alice use?",
+ options,
+ cursor,
+ allowFreeform: false,
+ editor: EMPTY_LINE,
+ caretVisible: false,
+ maxRows: 3,
+ },
+ theme,
+ 60,
+ ).map(stripAnsi);
+ expect(rows).toHaveLength(3);
+ expect(rows).toContain(` ${option.label}`);
+ for (const line of option.description.split("\n")) {
+ expect(rows.some((row) => row.trim() === line)).toBe(true);
+ }
+ }
+ });
+
it("clips every row to the panel width", () => {
for (const row of render({ cursor: 0 })) {
expect(row.length).toBeLessThanOrEqual(60);
diff --git a/packages/eve/src/cli/dev/tui/question-panel.ts b/packages/eve/src/cli/dev/tui/question-panel.ts
index 23ec49c35..fee946e24 100644
--- a/packages/eve/src/cli/dev/tui/question-panel.ts
+++ b/packages/eve/src/cli/dev/tui/question-panel.ts
@@ -32,6 +32,7 @@ interface QuestionPanelState {
/** The freeform row's inline editor; focused while the cursor rests on it. */
readonly editor: LineState;
readonly caretVisible: boolean;
+ readonly maxRows?: number;
}
export function renderQuestionPanel(
@@ -39,27 +40,50 @@ export function renderQuestionPanel(
theme: Theme,
width: number,
): string[] {
- const c = theme.colors;
- // The rule hugs the question — no blank row between them.
- const rows: string[] = [];
-
- // The prompt is model-authored and can span paragraphs. Embedded newlines
- // MUST split before width-wrapping: a row that secretly holds newlines
- // occupies more terminal rows than the live region accounts for, leaking a
- // duplicate frame into scrollback on every repaint.
- for (const logical of state.prompt.split(/\r?\n/u)) {
- for (const line of wrapVisibleLine(logical, Math.max(8, width - 2))) {
- rows.push(` ${c.bold(line)}`);
- }
- }
+ const rows = renderQuestionPrompt(state.prompt, theme, width);
rows.push("");
-
- rows.push(...renderQuestionChoices(state.options, state.cursor, theme));
+ let focusedRow = rows.length;
+ let focusedEndRow = focusedRow;
+ for (const [index, option] of state.options.entries()) {
+ if (index === state.cursor) focusedRow = rows.length;
+ rows.push(
+ ...renderQuestionChoice(option, index === state.cursor, theme).flatMap((row) =>
+ row.split(/\r?\n/u),
+ ),
+ );
+ if (index === state.cursor) focusedEndRow = rows.length - 1;
+ }
if (state.allowFreeform) {
+ if (state.cursor === state.options.length) focusedRow = focusedEndRow = rows.length;
rows.push(renderFreeformRow(state, theme, width));
}
- return rows.map((row) => clipVisible(row, width));
+ return fitQuestionPanel(
+ rows.map((row) => clipVisible(row, width)),
+ state.maxRows ?? rows.length,
+ focusedRow,
+ focusedEndRow,
+ );
+}
+
+export function renderQuestionPrompt(prompt: string, theme: Theme, width: number): string[] {
+ return stripTerminalControls(prompt)
+ .split(/\r?\n/u)
+ .flatMap((line) => wrapVisibleLine(line, Math.max(1, width - 2)))
+ .map((line) => ` ${theme.colors.bold(line)}`);
+}
+
+/** Window the body, keeping the focused choice or editor reachable on short terminals. */
+export function fitQuestionPanel(
+ rows: readonly string[],
+ maxRows: number,
+ focusedRow = rows.length - 1,
+ focusedEndRow = focusedRow,
+): string[] {
+ // Keep the label when one choice's description is taller than the viewport.
+ const end = Math.min(focusedEndRow, focusedRow + maxRows - 1);
+ const start = Math.max(0, Math.min(end - maxRows + 1, rows.length - maxRows));
+ return rows.slice(start, start + maxRows);
}
/** Who asked for a prompt and where it sits among the open requests, for the drawer's corner. */
@@ -75,36 +99,32 @@ export function inputContextLabel(
return parts.length === 0 ? undefined : parts.join(` ${theme.glyph.dot} `);
}
-export function renderQuestionChoices(
- options: readonly AgentTUIInputOption[],
- cursor: number,
+function renderQuestionChoice(
+ option: AgentTUIInputOption,
+ isCursor: boolean,
theme: Theme,
- forceCursor = false,
): string[] {
- return options.flatMap((option, index) => {
- const isCursor = forceCursor || cursor === index;
- const row = renderOptionRow({
- colors: theme.colors,
- glyphs: {
- pointer: theme.glyph.pointer,
- selectedPointer: theme.glyph.selectedPointer,
- success: theme.glyph.success,
- placeholder: theme.glyph.option,
- dot: theme.glyph.dot,
- warning: theme.glyph.warning,
- },
- label: option.label,
- isCursor,
- state: { kind: "available", checked: false },
- placeholder: false,
- presentation: "minimal",
- });
- const rows = [` ${row}`];
- if (option.description !== undefined && option.description.length > 0) {
- rows.push(` ${theme.colors.dim(option.description)}`);
- }
- return rows;
+ const row = renderOptionRow({
+ colors: theme.colors,
+ glyphs: {
+ pointer: theme.glyph.pointer,
+ selectedPointer: theme.glyph.selectedPointer,
+ success: theme.glyph.success,
+ placeholder: theme.glyph.option,
+ dot: theme.glyph.dot,
+ warning: theme.glyph.warning,
+ },
+ label: option.label,
+ isCursor,
+ state: { kind: "available", checked: false },
+ placeholder: false,
+ presentation: "minimal",
});
+ const rows = [` ${row}`];
+ if (option.description !== undefined && option.description.length > 0) {
+ rows.push(` ${theme.colors.dim(option.description)}`);
+ }
+ return rows;
}
function renderFreeformRow(state: QuestionPanelState, theme: Theme, width: number): string {
diff --git a/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts b/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts
index d5bb24d8f..0cad7ebd7 100644
--- a/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts
+++ b/packages/eve/src/cli/dev/tui/terminal-renderer.test.ts
@@ -34,6 +34,7 @@ import type { AgentTUISessionOptions } from "./runner.js";
import { PROMPT_COMMANDS, promptCommandsFor } from "./prompt-commands.js";
import { TerminalRenderer } from "./terminal-renderer.js";
import { MockScreen, MockUserInput } from "./test/mock-terminal.js";
+import { visibleLength } from "#cli/ui/terminal-text.js";
/** A session view over the conversation these events produce. */
function conversationOf(
@@ -962,6 +963,78 @@ describe("TerminalRenderer (inline scrollback)", () => {
renderer.shutdown();
});
+ it.each([13, 8])("keeps the focused question option inside a %i-row terminal", async (rows) => {
+ const { screen, input, renderer } = makeRenderer(60, rows);
+ renderer.renderConversation(
+ conversationOf(
+ [
+ stampTestEvent(createTurnStartedEvent({ sequence: 0, turnId: "turn_1" })),
+ stampTestEvent(
+ createMessageAppendedEvent({
+ messageDelta: "Preparing the deployment",
+ sequence: 1,
+ stepIndex: 0,
+ turnId: "turn_1",
+ }),
+ 1,
+ ),
+ ],
+ { working: true },
+ ),
+ );
+ const answer = renderer.readInputQuestion({
+ requestId: "q1",
+ prompt: "Which environment should Alice use?",
+ display: "select",
+ options: [
+ { id: "production", label: "Production", description: "Use the production environment" },
+ { id: "staging", label: "Staging", description: "Use the staging environment" },
+ { id: "preview", label: "Preview", description: "Use the preview environment" },
+ ],
+ allowFreeform: true,
+ });
+ try {
+ for (const label of ["Production", "Staging", "Preview", "Type your own answer…"]) {
+ const lines = screen.snapshot().split("\n");
+ expect(lines.length).toBeLessThanOrEqual(rows);
+ expect(lines.some((line) => line.includes(label))).toBe(true);
+ expect(lines.every((line) => visibleLength(line) <= screen.columns)).toBe(true);
+ if (label !== "Type your own answer…") input.down();
+ }
+ input.type("sandbox");
+ input.enter();
+ await expect(answer).resolves.toEqual({ text: "sandbox" });
+ } finally {
+ renderer.shutdown();
+ await answer.catch(() => {});
+ }
+ });
+
+ it.each([15, 8])(
+ "fits a wrapped free-text question and editor inside a narrow %i-row terminal",
+ async (rows) => {
+ const { screen, input, renderer } = makeRenderer(30, rows);
+ const answer = renderer.readInputQuestion({
+ requestId: "q1",
+ prompt:
+ "Which environment should Alice use for the next deployment?\nPlease enter the environment name.",
+ display: "text",
+ });
+ try {
+ input.type("staging");
+ const lines = screen.snapshot().split("\n");
+ expect(lines.length).toBeLessThanOrEqual(screen.rows);
+ expect(lines.every((line) => visibleLength(line) <= screen.columns)).toBe(true);
+ expect(screen.snapshot()).toContain("staging");
+ input.enter();
+ await expect(answer).resolves.toEqual({ text: "staging" });
+ } finally {
+ renderer.shutdown();
+ await answer.catch(() => {});
+ }
+ },
+ );
+
it("renders the question drawer with shared option rows", async () => {
const { screen, input, renderer } = makeRenderer();
@@ -1866,6 +1939,29 @@ describe("TerminalRenderer (inline scrollback)", () => {
renderer.shutdown();
});
+ it("keeps approval choices reachable with a long prompt in a small terminal", async () => {
+ const { screen, input, renderer } = makeRenderer(25, 8);
+ const approval = renderer.readToolApproval({
+ approvalId: "a1",
+ toolCallId: "c1",
+ toolName: "deploy",
+ prompt: "Approve deploying Alice's changes?\nThe staging environment is ready.",
+ input: {},
+ });
+ try {
+ input.down();
+ const lines = screen.snapshot().split("\n");
+ expect(lines.length).toBeLessThanOrEqual(screen.rows);
+ expect(lines.every((line) => visibleLength(line) <= screen.columns)).toBe(true);
+ expect(screen.snapshot()).toContain("No");
+ input.enter();
+ await expect(approval).resolves.toEqual({ approved: false, reason: "Denied by user." });
+ } finally {
+ renderer.shutdown();
+ await approval.catch(() => {});
+ }
+ });
+
it("shows where an approval sits among the open ones and who asked for it", async () => {
const { screen, input, renderer } = makeRenderer();
const approval = renderer.readToolApproval({
diff --git a/packages/eve/src/cli/dev/tui/terminal-renderer.ts b/packages/eve/src/cli/dev/tui/terminal-renderer.ts
index accd81fe6..d845c103b 100644
--- a/packages/eve/src/cli/dev/tui/terminal-renderer.ts
+++ b/packages/eve/src/cli/dev/tui/terminal-renderer.ts
@@ -146,7 +146,12 @@ import {
import type { VercelStatusSnapshot } from "./vercel-status.js";
import type { RemoteConnectionSnapshot } from "./remote-connection.js";
import { groupToolBlocksForDisplay } from "./tool-block-groups.js";
-import { inputContextLabel, renderQuestionChoices, renderQuestionPanel } from "./question-panel.js";
+import {
+ fitQuestionPanel,
+ inputContextLabel,
+ renderQuestionPanel,
+ renderQuestionPrompt,
+} from "./question-panel.js";
import { TurnClock } from "./turn-clock.js";
import { MessageQueue, renderMessageQueueRows } from "./message-queue.js";
import { formatStoredDiagnostic, presentDiagnostic } from "./diagnostic-presentation.js";
@@ -291,6 +296,8 @@ type AgentHeaderOptions = {
const caretBlinkMs = 500;
const tickMs = 90;
+// Two rules, two padding rows, and one navigation row stay outside the body viewport.
+const hitlDrawerChromeRows = 5;
// How long to wait on a lone `ESC` before treating it as the Escape key, so a
// split arrow sequence (`ESC` then `[A`) has time to reassemble first.
const escFlushMs = 30;
@@ -1109,18 +1116,22 @@ export class TerminalRenderer implements AgentTUIRenderer {
let approvalCursor = 0;
this.#hitlDrawer = (width) =>
renderTransientDrawer(
- [
- ` ${this.#theme.colors.bold(stripTerminalControls(request.prompt))}`,
- "",
- ...renderQuestionChoices(
- [
+ renderQuestionPanel(
+ {
+ prompt: request.prompt,
+ options: [
{ id: "yes", label: "Yes" },
{ id: "no", label: "No" },
],
- approvalCursor,
- this.#theme,
- ),
- ],
+ cursor: approvalCursor,
+ allowFreeform: false,
+ editor: EMPTY_LINE,
+ caretVisible: false,
+ maxRows: this.#height() - hitlDrawerChromeRows,
+ },
+ this.#theme,
+ width,
+ ),
["y yes · n no · Ctrl-C cancel"],
this.#theme,
width,
@@ -1224,6 +1235,7 @@ export class TerminalRenderer implements AgentTUIRenderer {
allowFreeform: hasFreeformRow,
editor,
caretVisible: this.#caretVisible,
+ maxRows: this.#height() - hitlDrawerChromeRows,
},
this.#theme,
width,
@@ -1249,11 +1261,14 @@ export class TerminalRenderer implements AgentTUIRenderer {
});
if (inputRows.at(-1) === "") inputRows.pop();
return renderTransientDrawer(
- [
- ` ${this.#theme.colors.bold(stripTerminalControls(question.prompt))}`,
- "",
- ...inputRows.map((row) => ` ${row}`),
- ],
+ fitQuestionPanel(
+ [
+ ...renderQuestionPrompt(question.prompt, this.#theme, width),
+ "",
+ ...inputRows.map((row) => ` ${row}`),
+ ],
+ this.#height() - hitlDrawerChromeRows,
+ ),
["Enter submit · Esc dismiss"],
this.#theme,
width,
@@ -3328,7 +3343,7 @@ export class TerminalRenderer implements AgentTUIRenderer {
const width = this.#width();
const footer = this.#footerRows(width);
const startupHeader = this.#startupHeader === undefined ? [] : this.#renderAgentHeaderRows();
- const maxBlockRows = Math.max(1, this.#height() - startupHeader.length - footer.length);
+ const maxBlockRows = Math.max(0, this.#height() - startupHeader.length - footer.length);
const committed: string[] = [];
let previous = this.#lastCommitted;
@@ -3401,7 +3416,7 @@ export class TerminalRenderer implements AgentTUIRenderer {
if (!this.#isInteractive) return;
const width = this.#width();
const footer = this.#footerRows(width);
- const maxBlockRows = Math.max(1, this.#height() - footer.length);
+ const maxBlockRows = Math.max(0, this.#height() - footer.length);
let previous = this.#lastCommitted;
const flat: string[] = [];
@@ -3551,7 +3566,9 @@ export class TerminalRenderer implements AgentTUIRenderer {
const drawer = this.#hitlDrawer(width);
const available = this.#height() - drawer.rows.length - drawer.controls.length - 1;
const activity = available >= 4 ? this.#taskPanelRows(width, available + 1) : [];
- return [...activity.slice(0, -1), ...drawer.rows, ...drawer.controls];
+ return [...activity.slice(0, -1), ...drawer.rows, ...drawer.controls].map((row) =>
+ clip(row, width),
+ );
}
const flow = this.#setupFlow;
@@ -4434,6 +4451,7 @@ function clipLiveRows(
width: number,
theme: Theme,
): string[] {
+ if (maxRows <= 0) return [];
if (rows.length <= maxRows) return [...rows];
if (maxRows <= 1) {
return [clip(hiddenRowsMarker(rows.length, theme), width)];