fix: eliminate 3 migration pitfalls in xterm-zerolag-input

Pitfall 3 (REAL BUG): setFlushed() called _render() unconditionally.
During tab switch, rendering against a stale buffer found the old
session's prompt and locked the column position via the flushed-text
column lock. After the new buffer loaded, rerender() kept the stale
column. Fix: add render parameter (default true), pass false during
tab-switch restore to defer rendering until buffer is loaded.

Pitfall 2: Tab completion "undo" required calling clearFlushed() +
resetBufferDetection() separately — easy to get wrong. Add
undoDetection() convenience method that atomically clears flushed
state and re-enables detection.

Pitfall 1: Backspace Map sync is app-specific (can't be fixed in lib),
but removeChar() return value + getFlushed() make the pattern clean.
Updated tab-switch test to demonstrate the correct pattern.

Tests: 78 total (+4 new)
- setFlushed render=false prevents stale column lock
- setFlushed render=false keeps overlay hidden
- undoDetection clears flushed + re-enables detection
- undoDetection preserves pending text

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
arkon
2026-02-22 11:16:12 +01:00
co-authored by Claude Opus 4.6
parent 36f0772c2f
commit 8dfcfe8d62
3 changed files with 82 additions and 8 deletions
@@ -271,11 +271,16 @@ export class ZerolagInputAddon implements XtermAddon {
*
* @param count - Number of characters flushed
* @param text - The actual flushed text (avoids reading stale terminal buffer)
* @param render - Whether to re-render immediately (default: `true`).
* Pass `false` when restoring flushed state during a tab/session switch
* before the new buffer has loaded — rendering against a stale buffer
* would lock the prompt column to the wrong position. Call `rerender()`
* explicitly after the buffer finishes loading.
*/
setFlushed(count: number, text: string): void {
setFlushed(count: number, text: string, render = true): void {
this._flushedOffset = count;
this._flushedText = text;
this._render();
if (render) this._render();
}
/**
@@ -347,6 +352,20 @@ export class ZerolagInputAddon implements XtermAddon {
this._bufferDetectDone = false;
}
/**
* Undo the last `detectBufferText()` call — clears flushed state and
* re-enables detection.
*
* Use case: Tab completion detection found text that matches the
* pre-tab baseline (no real completion happened). Call this to undo
* the detection so it can retry on the next flush cycle.
*/
undoDetection(): void {
this._flushedOffset = 0;
this._flushedText = '';
this._bufferDetectDone = false;
}
/**
* Suppress buffer detection until the next `clear()` or
* `resetBufferDetection()` call.