diff --git a/docs/tui-rendering-pipeline.md b/docs/tui-rendering-pipeline.md index b7ae687fb..4201465a0 100644 --- a/docs/tui-rendering-pipeline.md +++ b/docs/tui-rendering-pipeline.md @@ -197,9 +197,11 @@ 6. 全量重绘判定(`fullRender(clear)`,`tui-main-screen.ts:386-428`):首帧不擦屏直接写;宽度变化必擦(换行位置全变);高度变化必擦(Termux 软键盘场景例外,否则每次键盘弹出都重放整段历史,`tui-main-screen.ts:456-460`);内容收缩超过历史最高水位且无 overlay 时擦除空行(`clearOnShrink`,`tui.ts:513-515`、`tui-main-screen.ts:465-469`)。擦屏序列是 `\x1b[2J\x1b[H\x1b[3J`(含 scrollback)。 7. 差分区间扫描(`tui-main-screen.ts:473-498`):从 `resetFrom` 扫到两帧最大行数,记 `firstChanged/lastChanged`;纯追加时区间锁在旧长度处(`appendStart` 快速路径)。区间经过 Kitty 图像块时向外扩展,避免从图像块中间重画(`tui-main-screen.ts:284-305`)。 8. 视口夹取(`tui-main-screen.ts:568-589`):`firstChanged` 落在上一帧视口之上且文档长度没变——只有 scrollback 副本过期,屏幕上行没动——只重绘可见部分;任何长度变化都可能让行在视口下位移,仍走全量重绘。 -9. 写入(`tui-main-screen.ts:593-680`):`\x1b[?2026h` 开同步输出 → 视口滚动对齐(必要时 `\r\n` 推进滚动)→ CUU/CUD 移到首变行 → `\r` → 逐行 `\x1b[2K` + 行内容 → 尾部清理旧多出的行 → `\x1b[?2026l`。只写首变行到末变行,单行变化(spinner)不会闪烁全屏。 +9. 写入(`tui-main-screen.ts` 的差分输出路径):`\x1b[?2026h` 开同步输出 → 视口滚动对齐(必要时 `\r\n` 推进滚动)→ CUU/CUD 移到首变行 → `\r` → 逐行 `\x1b[2K` + 行内容 → 尾部清理旧多出的行 → 恢复硬件光标位置及显示/隐藏状态 → `\x1b[?2026l`。只写首变行到末变行,单行变化(spinner)不会闪烁全屏。 10. 硬保险(`tui-main-screen.ts:651-678`):任何渲染行 `visibleWidth > width` 直接把全部行写进 `tui-crash.log`,清理终端状态后抛错——指向"组件没截断"这个根因。 -11. 更新镜像:`previousLines`、`hardwareCursorRow`、`previousViewportTop`、`previousKittyImageIds`、宽高(`tui-main-screen.ts:737-749`),并按需移动硬件光标(`positionHardwareCursor`,`tui-main-screen.ts:757-788`)。 +11. 更新镜像:`previousLines`、`hardwareCursorRow`、`previousViewportTop`、`previousKittyImageIds`、宽高。`positionHardwareCursor` 复用同一坐标计算;全量、差量和删除尾部内容的重绘均把其输出写入当前同步帧。没有内容重绘时仍可直接移动光标。 + +同步输出结束前必须恢复 IME 光标锚点,避免终端先发布光标停在底栏末尾的中间状态。`packages/tui/test/tui-render.test.ts` 在同步输出写入边界检查实际终端坐标,覆盖首帧、后台状态刷新、缩放、尾部删除,以及光标显示/隐藏和全屏对照。该测试不替代真实终端与输入法组合的手工验收。 输出统一经过 `BoundedTerminalWriter`(`tui-main-screen.ts:24-80`):按 1MiB 分块刷盘,超长串按代理对边界切开,避免整帧拼成一个超过 V8 字符串上限的大串。 @@ -252,6 +254,8 @@ alt screen 进入时(`tui-alt-screen.ts:316-318`):`\x1b[?1049h` + 关 auto 选择器与自定义面板有两种挂载方式。`/settings` 通过 `showSelector` 替换 `editorContainer` 内的编辑器;`/usage` 与 `/status` 使用 `ctx.ui.custom` 默认的 inline 模式,采用相同的输入区域和终端全宽,底栏保留在面板下方。关闭时恢复编辑器、未提交草稿与键盘焦点;异步更新和关闭回调随面板释放。 +聚焦的澄清对话框在选项页提供一个硬件光标锚点:选中箭头所在的物理行,自由输入时使用 Editor 的编辑位置。锚点随渲染计算,失焦时移除;是否接收答案仍由输入模式决定,而不是由锚点是否存在决定。 + 只有显式选择 overlay 的对话框才通过 `ui.showOverlay(component, options)` 叠加到现有内容上。overlay 的焦点恢复(被 overlay 内部组件抢走再还回)由 `TuiBase` 的状态机保证(`tui.ts:525-588`);普通命令面板不应为了展示内容而默认居中覆盖聊天记录。 ### 7.2 事件 → 组件树 → 重绘 diff --git a/packages/coding-agent/src/features/step-questionnaire.ts b/packages/coding-agent/src/features/step-questionnaire.ts index 1c972658d..1aed1ef0d 100644 --- a/packages/coding-agent/src/features/step-questionnaire.ts +++ b/packages/coding-agent/src/features/step-questionnaire.ts @@ -1,7 +1,16 @@ /** Step's single clarification tool, rendered with Pi's native TUI primitives. */ import type { AgentToolResult } from "@step-harness/agent-core"; -import { Editor, type EditorTheme, Key, matchesKey, Text, visibleWidth, wrapTextWithAnsi } from "@step-harness/pi-tui"; +import { + CURSOR_MARKER, + Editor, + type EditorTheme, + Key, + matchesKey, + Text, + visibleWidth, + wrapTextWithAnsi, +} from "@step-harness/pi-tui"; import { type Static, Type } from "typebox"; import type { ExtensionAPI, ExtensionContext, ExtensionFactory } from "../core/extensions/types.ts"; import { type StepTelemetryReporter, trackStepTelemetry } from "../step/telemetry.ts"; @@ -266,6 +275,7 @@ async function executeNativeDialog( let inputMode = questions[0]?.options.length === 0; let inputQuestionId: string | null = inputMode ? (questions[0]?.id ?? null) : null; let cachedLines: string[] | undefined; + let cachedWidth: number | undefined; const answers = new Map(); const totalTabs = questions.length + 1; const editorTheme: EditorTheme = { @@ -413,7 +423,8 @@ async function executeNativeDialog( }; const render = (width: number): string[] => { - if (cachedLines) return cachedLines; + if (cachedLines && cachedWidth === width) return cachedLines; + cachedWidth = width; const renderWidth = Math.max(1, width); const lines: string[] = []; const question = currentQuestion(); @@ -477,8 +488,9 @@ async function executeNativeDialog( lines.push(""); for (const [index, option] of options.entries()) { const selected = index === optionIndex; + const anchor = selected && editor.focused && !inputMode ? CURSOR_MARKER : ""; addWrapped( - selected ? theme.fg("accent", "> ") : " ", + selected ? theme.fg("accent", `> ${anchor}`) : " ", theme.fg(selected ? "accent" : "text", `${index + 1}. ${option.label}`), ); if (option.description) { @@ -510,10 +522,19 @@ async function executeNativeDialog( }; return { + get focused(): boolean { + return editor.focused; + }, + set focused(value: boolean) { + // The host focuses this wrapper, but the nested editor owns the answer cursor. + editor.focused = value; + cachedLines = undefined; + }, render, handleInput, invalidate: () => { cachedLines = undefined; + editor.invalidate(); }, }; }); diff --git a/packages/coding-agent/test/step-capabilities-extension.test.ts b/packages/coding-agent/test/step-capabilities-extension.test.ts index 019f3a95b..3af4a1fa5 100644 --- a/packages/coding-agent/test/step-capabilities-extension.test.ts +++ b/packages/coding-agent/test/step-capabilities-extension.test.ts @@ -2,9 +2,18 @@ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { AgentToolResult } from "@step-harness/agent-core"; +import { + type Component, + CURSOR_MARKER, + type Terminal, + TuiAltScreen, + TuiMainScreen, + visibleWidth, +} from "@step-harness/pi-tui"; import { type Model, validateToolArguments } from "@step-harness/providers"; import { expect, test } from "vitest"; import type { ExtensionAPI, ExtensionContext, ToolDefinition } from "../src/core/extensions/types.ts"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; import { createStepCapabilitiesExtension } from "../src/features/step-capabilities.ts"; import { createStepPlanExtension } from "../src/features/step-plan.ts"; import { @@ -504,3 +513,191 @@ test("plan adapter uses Step tool names and leaves run_command unrestricted", as ), ).resolves.toMatchObject({ block: true }); }); + +async function openClarification(params: Record, fullscreen = false) { + const { api, tools } = createApi(); + createStepCapabilitiesExtension({ subagent: { includeBuiltinAgents: false } })(api); + const ctx = createContext("/workspace"); + initTheme("step-blue"); + const terminal: Terminal = { + columns: 80, + rows: 40, + kittyProtocolActive: false, + start: () => {}, + stop: () => {}, + drainInput: async () => {}, + write: () => {}, + moveBy: () => {}, + hideCursor: () => {}, + showCursor: () => {}, + clearLine: () => {}, + clearFromCursor: () => {}, + clearScreen: () => {}, + setTitle: () => {}, + setProgress: () => {}, + }; + const tui = fullscreen ? new TuiAltScreen(terminal) : new TuiMainScreen(terminal); + const componentPromise = new Promise((resolveComponent, rejectComponent) => { + ctx.ui.custom = async (factory) => + new Promise((resolve, reject) => { + Promise.resolve(factory(tui, theme, new KeybindingsManager(), resolve)).then( + (component) => { + tui.addChild(component); + resolveComponent(component); + }, + (error: unknown) => { + rejectComponent(error); + reject(error); + }, + ); + }); + }); + const result = tools.get("clarify_user")!.execute("ask", params, undefined, undefined, ctx); + const component = await componentPromise; + return { + component, + tui, + result, + close: async () => { + // Escape first leaves custom-answer mode, then cancels the dialog. + component.handleInput?.("\x1b"); + component.handleInput?.("\x1b"); + tui.setFocus(null); + tui.removeChild(component); + try { + await result; + } finally { + tui.stop(); + } + }, + }; +} + +function dialogAnchor(component: Component, width = 80): { row: number; col: number } { + const lines = component.render(width); + const markerRows = lines.flatMap((line, row) => (line.includes(CURSOR_MARKER) ? [row] : [])); + expect(markerRows).toHaveLength(1); + const row = markerRows[0]!; + return { row, col: visibleWidth(lines[row]!.split(CURSOR_MARKER)[0]!) }; +} + +function answerCursor(component: Component, width = 80): { row: number; col: number } { + const anchor = dialogAnchor(component, width); + const answerLabelRow = component.render(width).findIndex((line) => line.includes("Your answer:")); + expect(answerLabelRow).toBeGreaterThanOrEqual(0); + expect(anchor.row).toBeGreaterThan(answerLabelRow); + return anchor; +} + +test.each([false, true])( + "clarify_user keeps the focused answer cursor at the typed position (fullscreen=%s)", + async (fullscreen) => { + const dialog = await openClarification({ question: "Describe the change" }, fullscreen); + try { + expect(dialog.component.render(80).join("\n")).not.toContain(CURSOR_MARKER); + dialog.tui.setFocus(dialog.component); + const start = answerCursor(dialog.component); + for (const [index, char] of [..."中文abc"].entries()) { + dialog.component.handleInput?.(char); + const cursor = answerCursor(dialog.component); + expect(cursor.row).toBe(start.row); + expect(cursor.col).toBe(start.col + visibleWidth("中文abc".slice(0, index + 1))); + } + dialog.component.handleInput?.("\x1b[D"); + expect(answerCursor(dialog.component).col).toBe(start.col + 6); + dialog.component.handleInput?.("\x7f"); + expect(answerCursor(dialog.component).col).toBe(start.col + 5); + dialog.tui.setFocus(null); + expect(dialog.component.render(80).join("\n")).not.toContain(CURSOR_MARKER); + dialog.tui.setFocus(dialog.component); + expect(answerCursor(dialog.component).col).toBe(start.col + 5); + } finally { + await dialog.close(); + } + }, +); + +test("clarify_user recomputes answer wrapping when width changes without invalidation", async () => { + const dialog = await openClarification({ question: "Describe the change" }); + try { + dialog.tui.setFocus(dialog.component); + dialog.component.handleInput?.("\x1b[200~abcdefghijklmnopqrstuvwx中文\x1b[201~"); + const wide = dialog.component.render(80); + const narrow = dialog.component.render(20); + expect(narrow).not.toBe(wide); + expect(narrow.every((line) => visibleWidth(line) <= 20)).toBe(true); + const narrowCursor = answerCursor(dialog.component, 20); + expect(narrowCursor.col).toBeLessThan(20); + expect(answerCursor(dialog.component, 80).col).toBe(29); + expect(visibleWidth(dialog.component.render(80)[0]!)).toBe(80); + } finally { + await dialog.close(); + } +}); + +test.each([false, true])( + "clarify_user anchors the selected option before Enter (fullscreen=%s)", + async (fullscreen) => { + const dialog = await openClarification( + { question: "Choose", options: [{ label: "中文选项", value: "option" }] }, + fullscreen, + ); + try { + expect(dialog.component.render(80).join("\n")).not.toContain(CURSOR_MARKER); + dialog.tui.setFocus(dialog.component); + const first = dialogAnchor(dialog.component); + expect(dialog.component.render(80)[first.row]).toContain("1. 中文选项"); + dialog.component.handleInput?.("\x1b[B"); + const next = dialogAnchor(dialog.component); + expect(next.row).toBeGreaterThan(first.row); + expect(dialog.component.render(80)[next.row]).toContain("2. Type a custom answer"); + expect(dialog.component.render(80).join("\n")).not.toContain("Your answer:"); + for (const width of [80, 20]) { + const anchor = dialogAnchor(dialog.component, width); + expect(anchor.col).toBe(2); + const line = dialog.component.render(width)[anchor.row]!; + expect(line.split(CURSOR_MARKER)[0]!.endsWith("> ")).toBe(true); + } + for (const width of [2, 1]) { + expect(dialogAnchor(dialog.component, width).col).toBe(0); + } + dialog.tui.setFocus(null); + expect(dialog.component.render(80).join("\n")).not.toContain(CURSOR_MARKER); + dialog.tui.setFocus(dialog.component); + expect(dialogAnchor(dialog.component)).toEqual(next); + } finally { + await dialog.close(); + } + }, +); + +test("clarify_user preserves a dialog anchor across navigation and freeform modes", async () => { + const dialog = await openClarification({ + questions: [ + { id: "choice", question: "Choose", options: [{ label: "Option", value: "option" }] }, + { id: "text", question: "Explain" }, + ], + }); + try { + dialog.tui.setFocus(dialog.component); + const optionAnchor = dialogAnchor(dialog.component); + expect(dialog.component.render(80)[optionAnchor.row]).toContain("1. Option"); + dialog.component.handleInput?.("2"); + answerCursor(dialog.component); + dialog.component.handleInput?.("\x1b"); + expect(dialogAnchor(dialog.component)).toEqual(optionAnchor); + dialog.component.handleInput?.("\t"); + const firstLine = answerCursor(dialog.component); + dialog.component.handleInput?.("\x1b[13;2u"); + dialog.component.handleInput?.("second line"); + const multiline = answerCursor(dialog.component); + expect(multiline.row).toBe(firstLine.row + 1); + dialog.component.handleInput?.("\t"); + expect(dialog.component.render(80).join("\n")).not.toContain(CURSOR_MARKER); + expect(dialog.component.render(80).join("\n")).not.toContain("Your answer:"); + dialog.component.handleInput?.("\x1b[Z"); + answerCursor(dialog.component); + } finally { + await dialog.close(); + } +}); diff --git a/packages/tui/src/tui-main-screen.ts b/packages/tui/src/tui-main-screen.ts index 61203cc9b..f4b672a05 100644 --- a/packages/tui/src/tui-main-screen.ts +++ b/packages/tui/src/tui-main-screen.ts @@ -402,8 +402,6 @@ export class TuiMainScreen extends TuiBase implements TUI { } output.append(line); } - output.append("\x1b[?2026l"); // End synchronized output - output.flush(); this.cursorRow = Math.max(0, newLines.length - 1); this.hardwareCursorRow = this.cursorRow; // Reset max lines when clearing, otherwise track growth @@ -414,7 +412,9 @@ export class TuiMainScreen extends TuiBase implements TUI { } const bufferLength = Math.max(height, newLines.length); this.previousViewportTop = Math.max(0, bufferLength - height); - this.positionHardwareCursor(cursorPos, newLines.length); + this.positionHardwareCursor(cursorPos, newLines.length, output); + output.append("\x1b[?2026l"); // Publish only after restoring the IME cursor. + output.flush(); this.previousLines = newLines; if (kittyScanEnabled) this.previousKittyImageIds = this.collectKittyImageIds(newLines); this.previousWidth = width; @@ -521,12 +521,16 @@ export class TuiMainScreen extends TuiBase implements TUI { if (moveBack > 0) { output.append(`\x1b[${moveBack}A`); } - output.append("\x1b[?2026l"); - output.flush(); this.cursorRow = targetRow; this.hardwareCursorRow = targetRow; + this.positionHardwareCursor(cursorPos, newLines.length, output); + output.append("\x1b[?2026l"); + output.flush(); + } else { + // Unreachable today: firstChanged >= newLines.length already implies the + // previous frame was longer. Kept so a changed guard still parks the cursor. + this.positionHardwareCursor(cursorPos, newLines.length); } - this.positionHardwareCursor(cursorPos, newLines.length); this.previousLines = newLines; if (kittyScanEnabled) this.previousKittyImageIds = this.collectKittyImageIds(newLines); this.previousWidth = width; @@ -673,10 +677,6 @@ export class TuiMainScreen extends TuiBase implements TUI { output.append(`\x1b[${extraLines}A`); } - output.append("\x1b[?2026l"); // End synchronized output - - output.flush(); - // Track cursor position for next render // cursorRow tracks end of content (for viewport calculation) // hardwareCursorRow tracks actual terminal cursor position (for movement) @@ -686,8 +686,10 @@ export class TuiMainScreen extends TuiBase implements TUI { this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length); this.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1); - // Position hardware cursor for IME - this.positionHardwareCursor(cursorPos, newLines.length); + // Keep the IME anchor in the same synchronized frame as the content. + this.positionHardwareCursor(cursorPos, newLines.length, output); + output.append("\x1b[?2026l"); + output.flush(); this.previousLines = newLines; if (kittyScanEnabled) this.previousKittyImageIds = this.collectKittyImageIds(newLines); @@ -700,9 +702,14 @@ export class TuiMainScreen extends TuiBase implements TUI { * @param cursorPos The cursor position extracted from rendered output, or null * @param totalLines Total number of rendered lines */ - private positionHardwareCursor(cursorPos: { row: number; col: number } | null, totalLines: number): void { + private positionHardwareCursor( + cursorPos: { row: number; col: number } | null, + totalLines: number, + output?: BoundedTerminalWriter, + ): void { if (!cursorPos || totalLines <= 0) { - this.terminal.hideCursor(); + if (output) output.append("\x1b[?25l"); + else this.terminal.hideCursor(); return; } @@ -722,11 +729,14 @@ export class TuiMainScreen extends TuiBase implements TUI { buffer += `\x1b[${targetCol + 1}G`; if (buffer) { - this.terminal.write(buffer); + if (output) output.append(buffer); + else this.terminal.write(buffer); } this.hardwareCursorRow = targetRow; - if (this.getShowHardwareCursor()) { + if (output) { + output.append(this.getShowHardwareCursor() ? "\x1b[?25h" : "\x1b[?25l"); + } else if (this.getShowHardwareCursor()) { this.terminal.showCursor(); } else { this.terminal.hideCursor(); diff --git a/packages/tui/test/tui-render.test.ts b/packages/tui/test/tui-render.test.ts index 4878091d0..0d971ccac 100644 --- a/packages/tui/test/tui-render.test.ts +++ b/packages/tui/test/tui-render.test.ts @@ -13,7 +13,8 @@ import { setCapabilities, setCellDimensions, } from "../src/terminal-image.ts"; -import type { Component, TUI } from "../src/tui.ts"; +import { type Component, CURSOR_MARKER, type TUI } from "../src/tui.ts"; +import { TuiAltScreen } from "../src/tui-alt-screen.ts"; import { TuiMainScreen } from "../src/tui-main-screen.ts"; import { VirtualTerminal } from "./virtual-terminal.ts"; @@ -79,6 +80,69 @@ class LoggingVirtualTerminal extends VirtualTerminal { } } +class SynchronizedCursorTerminal extends LoggingVirtualTerminal { + readonly framePositions: { x: number; y: number }[] = []; + + override write(data: string): void { + super.write(data); + if (!data.includes("\x1b[?2026l")) return; + // Observe this write before later cursor-restoration writes are processed. + const xterm = (this as unknown as { xterm: XtermTerminalType }).xterm; + xterm.write("", () => this.framePositions.push(this.getCursorPosition())); + } +} + +describe("TUI synchronized cursor positioning", () => { + for (const [mode, Renderer] of [ + ["regular", TuiMainScreen], + ["fullscreen", TuiAltScreen], + ] as const) { + for (const showCursor of [false, true]) { + it(`restores the answer cursor before publishing ${mode} frames (visible=${showCursor})`, async () => { + const terminal = new SynchronizedCursorTerminal(40, 12); + const tui = new Renderer(terminal, showCursor); + const component = new TestComponent(); + component.lines = ["Working 0", `中文${CURSOR_MARKER}abc`, "Footer 0"]; + tui.addChild(component); + tui.start(); + try { + const checkFrame = async () => { + await terminal.waitForRender(); + assert.strictEqual(terminal.framePositions.length, 1); + assert.deepStrictEqual(terminal.framePositions[0], { x: 4, y: 1 }); + assert.deepStrictEqual(terminal.getCursorPosition(), { x: 4, y: 1 }); + const visibility = showCursor ? "\x1b[?25h" : "\x1b[?25l"; + assert.ok(terminal.getWrites().endsWith(`${visibility}\x1b[?2026l`)); + terminal.framePositions.length = 0; + terminal.clearWrites(); + }; + await checkFrame(); + const initialRedraws = tui.fullRedraws; + + // Background status updates repaint below the focused answer. + component.lines = ["Working 1", `中文${CURSOR_MARKER}abc`, "Footer 1"]; + tui.requestRender(); + await checkFrame(); + assert.strictEqual(tui.fullRedraws, initialRedraws); + + terminal.resize(20, 12); + await checkFrame(); + assert.ok(tui.fullRedraws > initialRedraws); + + // Retain the focused answer while deleting trailing content. + tui.setClearOnShrink(false); + component.lines = ["Working 1", `中文${CURSOR_MARKER}abc`]; + tui.requestRender(); + await checkFrame(); + } finally { + tui.stop(); + await terminal.flush(); + } + }); + } + } +}); + async function withEnv(updates: Record, run: () => Promise): Promise { const previousValues = new Map(); for (const [key, value] of Object.entries(updates)) { @@ -177,7 +241,7 @@ describe("TUI bounded render output", () => { ); assert.strictEqual( terminal.writes.join(""), - `\x1b[?2026h${kittyLine}\r\n${kittyLine}\x1b[?2026l`, + `\x1b[?2026h${kittyLine}\r\n${kittyLine}\x1b[?25l\x1b[?2026l`, "chunking must preserve the synchronized render output", ); });