-
Notifications
You must be signed in to change notification settings - Fork 46
fix(tui): avoid stale Windows autocomplete rows #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhouyuanxinand
wants to merge
3
commits into
openpi-dev:main
Choose a base branch
from
zhouyuanxinand:codex/windows-tui-compatibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import type { | ||
| ExtensionAPI, | ||
| ExtensionContext, | ||
| } from "@earendil-works/pi-coding-agent"; | ||
|
|
||
| interface TuiSettings { | ||
| terminal?: { | ||
| clearOnShrink?: boolean; | ||
| }; | ||
| } | ||
|
|
||
| interface TuiSettingsManager { | ||
| getGlobalSettings(): TuiSettings; | ||
| getProjectSettings(): TuiSettings; | ||
| drainErrors?(): readonly unknown[]; | ||
| } | ||
|
|
||
| type TuiSettingsManagerFactory = ( | ||
| cwd: string, | ||
| ) => TuiSettingsManager | Promise<TuiSettingsManager>; | ||
|
|
||
| const WIDGET_KEY = "openpi-windows-tui-compatibility"; | ||
|
|
||
| /** | ||
| * The main-screen renderer can leave stale autocomplete rows on Windows. | ||
| * Keep the workaround limited to interactive Windows sessions so RPC/print | ||
| * users and non-Windows terminals are unaffected. | ||
| */ | ||
| export function shouldInstallWindowsTuiCompatibility( | ||
| platform: NodeJS.Platform, | ||
| mode: ExtensionContext["mode"], | ||
| ) { | ||
| return platform === "win32" && mode === "tui"; | ||
| } | ||
|
|
||
| export function shouldEnableWindowsClearOnShrink(options: { | ||
| platform: NodeJS.Platform; | ||
| mode: ExtensionContext["mode"]; | ||
| globalClearOnShrink?: boolean; | ||
| projectClearOnShrink?: boolean; | ||
| }) { | ||
| return ( | ||
| shouldInstallWindowsTuiCompatibility(options.platform, options.mode) && | ||
| options.globalClearOnShrink === undefined && | ||
| options.projectClearOnShrink === undefined | ||
| ); | ||
| } | ||
|
|
||
| function readClearOnShrinkSettings(settingsManager: TuiSettingsManager) { | ||
| const globalSettings = settingsManager.getGlobalSettings(); | ||
| const projectSettings = settingsManager.getProjectSettings(); | ||
| return { | ||
| globalClearOnShrink: globalSettings.terminal?.clearOnShrink, | ||
| projectClearOnShrink: projectSettings.terminal?.clearOnShrink, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Register the Windows renderer workaround. | ||
| * | ||
| * Pi exposes the renderer to widget factories, but not as a direct property | ||
| * on ExtensionContext. The zero-height widget lets us apply the supported | ||
| * renderer setting without replacing OpenPI's header, footer, or editor. | ||
| * | ||
| * The renderer setting is deliberately session-local. Pi's SettingsManager | ||
| * setters persist global preferences, so this compatibility extension only | ||
| * reads the existing clear-on-shrink settings and never changes tuiMode or | ||
| * terminal preferences on the user's behalf. | ||
| */ | ||
| export function registerWindowsTuiCompatibility( | ||
| pi: ExtensionAPI, | ||
| platform: NodeJS.Platform, | ||
| settingsManagerFactory?: TuiSettingsManagerFactory, | ||
| ) { | ||
| let activeUi: ExtensionContext["ui"] | undefined; | ||
|
|
||
| const cleanup = () => { | ||
| const ui = activeUi; | ||
| activeUi = undefined; | ||
| try { | ||
| ui?.setWidget(WIDGET_KEY, undefined); | ||
| } catch { | ||
| // The renderer may already be gone during shutdown. | ||
| } | ||
| }; | ||
|
|
||
| pi.on("session_start", async (_event, ctx) => { | ||
| cleanup(); | ||
| if (!shouldInstallWindowsTuiCompatibility(platform, ctx.mode)) return; | ||
|
|
||
| let enableClearOnShrink = false; | ||
| if (settingsManagerFactory) { | ||
| try { | ||
| const settingsManager = await settingsManagerFactory(ctx.cwd); | ||
| const settings = readClearOnShrinkSettings(settingsManager); | ||
| const settingsErrors = settingsManager.drainErrors?.() ?? []; | ||
| enableClearOnShrink = | ||
| settingsErrors.length === 0 && | ||
| shouldEnableWindowsClearOnShrink({ | ||
| platform, | ||
| mode: ctx.mode, | ||
| ...settings, | ||
| }); | ||
| } catch { | ||
| // Settings reads must never prevent the OpenPI session from starting. | ||
| } | ||
| } | ||
|
|
||
| activeUi = ctx.ui; | ||
| ctx.ui.setWidget( | ||
| WIDGET_KEY, | ||
| (tui) => { | ||
| // The renderer can be replaced at runtime when the user switches TUI | ||
| // modes, so apply this when the factory receives the active renderer. | ||
| if (tui.mode === "regular" && enableClearOnShrink) { | ||
| tui.setClearOnShrink(true); | ||
| } | ||
| return { | ||
| render: () => [], | ||
| invalidate() {}, | ||
| }; | ||
| }, | ||
| { placement: "belowEditor" }, | ||
| ); | ||
| }); | ||
|
|
||
| pi.on("session_shutdown", cleanup); | ||
| } | ||
|
|
||
| export default function windowsTuiCompatibility(pi: ExtensionAPI) { | ||
| registerWindowsTuiCompatibility(pi, process.platform, async (cwd) => { | ||
| const { SettingsManager } = await import("@earendil-works/pi-coding-agent"); | ||
| return SettingsManager.create(cwd); | ||
| }); | ||
| } | ||
205 changes: 205 additions & 0 deletions
205
tests/extensions/windows-tui-compatibility/index.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import type { | ||
| ExtensionAPI, | ||
| ExtensionContext, | ||
| } from "@earendil-works/pi-coding-agent"; | ||
| import type { Component, TUI } from "@earendil-works/pi-tui"; | ||
| import { | ||
| registerWindowsTuiCompatibility, | ||
| shouldEnableWindowsClearOnShrink, | ||
| shouldInstallWindowsTuiCompatibility, | ||
| } from "../../../extensions/windows-tui-compatibility/index.ts"; | ||
|
|
||
| type WidgetFactory = ( | ||
| tui: TUI, | ||
| theme: unknown, | ||
| ) => Component & { dispose?(): void }; | ||
|
|
||
| function createHarness( | ||
| platform: NodeJS.Platform, | ||
| mode: ExtensionContext["mode"] = "tui", | ||
| settingsManagerFactory?: Parameters< | ||
| typeof registerWindowsTuiCompatibility | ||
| >[2], | ||
| ) { | ||
| const hooks = new Map< | ||
| string, | ||
| (event: unknown, ctx: ExtensionContext) => unknown | ||
| >(); | ||
| let widgetFactory: WidgetFactory | undefined; | ||
| let widgetCleared = false; | ||
| const notifications: string[] = []; | ||
|
|
||
| const pi = { | ||
| on(event: string, handler: unknown) { | ||
| hooks.set( | ||
| event, | ||
| handler as (event: unknown, ctx: ExtensionContext) => unknown, | ||
| ); | ||
| }, | ||
| } as unknown as ExtensionAPI; | ||
|
|
||
| const ctx = { | ||
| cwd: "C:\\project", | ||
| mode, | ||
| hasUI: mode === "tui", | ||
| ui: { | ||
| setWidget(_key: string, content: WidgetFactory | undefined) { | ||
| if (content) widgetFactory = content; | ||
| else { | ||
| widgetFactory = undefined; | ||
| widgetCleared = true; | ||
| } | ||
| }, | ||
| notify(message: string) { | ||
| notifications.push(message); | ||
| }, | ||
| }, | ||
| } as unknown as ExtensionContext; | ||
|
|
||
| registerWindowsTuiCompatibility(pi, platform, settingsManagerFactory); | ||
|
|
||
| return { | ||
| ctx, | ||
| emit(event: string) { | ||
| return hooks.get(event)?.({}, ctx); | ||
| }, | ||
| mount(tui: TUI) { | ||
| return widgetFactory?.(tui, {}); | ||
| }, | ||
| get widgetFactory() { | ||
| return widgetFactory; | ||
| }, | ||
| get widgetCleared() { | ||
| return widgetCleared; | ||
| }, | ||
| get notifications() { | ||
| return notifications; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| test("installs only for interactive Windows sessions", () => { | ||
| assert.equal(shouldInstallWindowsTuiCompatibility("win32", "tui"), true); | ||
| assert.equal(shouldInstallWindowsTuiCompatibility("linux", "tui"), false); | ||
| assert.equal(shouldInstallWindowsTuiCompatibility("win32", "rpc"), false); | ||
|
|
||
| assert.equal( | ||
| shouldEnableWindowsClearOnShrink({ platform: "win32", mode: "tui" }), | ||
| true, | ||
| ); | ||
| assert.equal( | ||
| shouldEnableWindowsClearOnShrink({ | ||
| platform: "win32", | ||
| mode: "tui", | ||
| globalClearOnShrink: false, | ||
| }), | ||
| false, | ||
| ); | ||
| assert.equal( | ||
| shouldEnableWindowsClearOnShrink({ | ||
| platform: "win32", | ||
| mode: "tui", | ||
| globalClearOnShrink: true, | ||
| }), | ||
| false, | ||
| ); | ||
| assert.equal( | ||
| shouldEnableWindowsClearOnShrink({ | ||
| platform: "win32", | ||
| mode: "tui", | ||
| projectClearOnShrink: false, | ||
| }), | ||
| false, | ||
| ); | ||
|
|
||
| const linux = createHarness("linux"); | ||
| linux.emit("session_start"); | ||
| assert.equal(linux.widgetFactory, undefined); | ||
| }); | ||
|
|
||
| test("enables clear-on-shrink for regular TUI but not fullscreen", async () => { | ||
| const harness = createHarness("win32", "tui", async () => ({ | ||
| getGlobalSettings: () => ({}), | ||
| getProjectSettings: () => ({}), | ||
| })); | ||
| await harness.emit("session_start"); | ||
|
|
||
| const clearOnShrink: boolean[] = []; | ||
| harness.mount({ | ||
| mode: "regular", | ||
| setClearOnShrink(enabled: boolean) { | ||
| clearOnShrink.push(enabled); | ||
| }, | ||
| requestRender(force?: boolean) { | ||
| assert.equal(force, undefined); | ||
| }, | ||
| } as TUI); | ||
| assert.deepEqual(clearOnShrink, [true]); | ||
|
|
||
| clearOnShrink.length = 0; | ||
| harness.mount({ | ||
| mode: "fullscreen", | ||
| setClearOnShrink(enabled: boolean) { | ||
| clearOnShrink.push(enabled); | ||
| }, | ||
| requestRender(force?: boolean) { | ||
| assert.equal(force, undefined); | ||
| }, | ||
| } as TUI); | ||
| assert.deepEqual(clearOnShrink, []); | ||
| }); | ||
|
|
||
| test("keeps the workaround session-local and respects explicit clear-on-shrink", async () => { | ||
| const writes: string[] = []; | ||
| const explicit = createHarness("win32", "tui", async () => ({ | ||
| getGlobalSettings: () => ({ terminal: { clearOnShrink: false } }), | ||
| getProjectSettings: () => ({}), | ||
| drainErrors: () => [], | ||
| })); | ||
| await explicit.emit("session_start"); | ||
|
|
||
| const clearOnShrink: boolean[] = []; | ||
| explicit.mount({ | ||
| mode: "regular", | ||
| setClearOnShrink(enabled: boolean) { | ||
| writes.push("clear-on-shrink"); | ||
| clearOnShrink.push(enabled); | ||
| }, | ||
| requestRender() {}, | ||
| } as TUI); | ||
|
|
||
| assert.deepEqual(clearOnShrink, []); | ||
| assert.deepEqual(explicit.notifications, []); | ||
| assert.deepEqual(writes, []); | ||
| }); | ||
|
|
||
| test("fails closed when settings cannot be read", async () => { | ||
| const harness = createHarness("win32", "tui", async () => { | ||
| throw new Error("malformed settings"); | ||
| }); | ||
| await harness.emit("session_start"); | ||
|
|
||
| const clearOnShrink: boolean[] = []; | ||
| harness.mount({ | ||
| mode: "regular", | ||
| setClearOnShrink(enabled: boolean) { | ||
| clearOnShrink.push(enabled); | ||
| }, | ||
| requestRender() {}, | ||
| } as TUI); | ||
|
|
||
| assert.deepEqual(clearOnShrink, []); | ||
| }); | ||
|
|
||
| test("cleans up the compatibility widget on shutdown", () => { | ||
| const harness = createHarness("win32"); | ||
| harness.emit("session_start"); | ||
| assert.ok(harness.widgetFactory); | ||
|
|
||
| harness.emit("session_shutdown"); | ||
|
|
||
| assert.equal(harness.widgetFactory, undefined); | ||
| assert.equal(harness.widgetCleared, true); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Apply the fallback when the native renderer actually changes
On Windows, start a session in fullscreen with clearOnShrink unset, then switch to regular through native /settings. This factory initially sees fullscreen and skips the override. In the locked Pi implementation, setExtensionWidget calls the factory only once and stores its returned component; switchTuiMode copies the previous clearOnShrink (default false) and remounts existing components without invoking the factory again. Since this component has empty render/invalidate methods, the regular renderer keeps false and the stale-row workaround never activates. Please handle the real renderer lifecycle and test this supported transition rather than simulating a second factory mount.