Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

61.6 KB
Raw Blame
diff --git a/src/browser/CoreBrowserTerminal.ts b/src/browser/CoreBrowserTerminal.ts
index 67893b7966eb095db4459b8837de9766921f36c5..3c2ac1afaeda22da4482099702d3ca673c57f4e1 100644
--- a/src/browser/CoreBrowserTerminal.ts
+++ b/src/browser/CoreBrowserTerminal.ts
@@ -325,6 +325,9 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
   private _handleTextAreaBlur(): void {
     // Text can safely be removed on blur. Doing it earlier could interfere with
     // screen readers reading it out.
+    if (this._compositionHelper instanceof CompositionHelper) {
+      this._compositionHelper.blur();
+    }
     this.textarea!.value = '';
     this.refresh(this.buffer.y, this.buffer.y);
     if (this.coreService.decPrivateModes.sendFocus) {
@@ -425,7 +428,18 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
       this._compositionHelper!.updateCompositionElements();
     }));
     this._register(addDisposableListener(this.textarea!, 'compositionupdate', (e: CompositionEvent) => this._compositionHelper!.compositionupdate(e)));
-    this._register(addDisposableListener(this.textarea!, 'compositionend', () => this._compositionHelper!.compositionend()));
+    this._register(addDisposableListener(this.textarea!, 'compositionend', (e: CompositionEvent) => {
+      if (this._compositionHelper instanceof CompositionHelper) {
+        if (this._compositionHelper.compositionend(e)) {
+          this.textarea!.dispatchEvent(new CustomEvent(
+            'xterm-composition-transaction-accepted',
+            { bubbles: true }
+          ));
+        }
+      } else {
+        this._compositionHelper!.compositionend();
+      }
+    }));
     this._register(addDisposableListener(this.textarea!, 'input', (ev: InputEvent) => this._inputEvent(ev), true));
     this._register(this.onRender(() => this._compositionHelper!.updateCompositionElements()));
   }
@@ -552,6 +566,11 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
     this._compositionView = this._document.createElement('div');
     this._compositionView.classList.add('composition-view');
     this._compositionHelper = this._instantiationService.createInstance(CompositionHelper, this.textarea, this._compositionView);
+    this._register(toDisposable(() => {
+      if (this._compositionHelper instanceof CompositionHelper) {
+        this._compositionHelper.dispose();
+      }
+    }));
     this._helperContainer.appendChild(this._compositionView);
 
     this._mouseCoordsService = this._instantiationService.createInstance(MouseCoordsService);
@@ -1009,7 +1028,9 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
 
     this._onKey.fire({ key, domEvent: ev });
     this._showCursor();
-    this.coreService.triggerDataEvent(key, true);
+    if (!this._compositionHelper!.keypress?.(key)) {
+      this.coreService.triggerDataEvent(key, true);
+    }
 
     this._keyPressHandled = true;
 
@@ -1027,6 +1048,23 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
    * @param ev The input event to be handled.
    */
   protected _inputEvent(ev: InputEvent): boolean {
+    // Why: Chromium updates the composing selection on input, after compositionupdate.
+    if (
+      ev.inputType === 'insertCompositionText' &&
+      this._compositionHelper instanceof CompositionHelper
+    ) {
+      // Read-only and never consumes the event, so unlike insertText it stays on in screen-reader mode.
+      this._compositionHelper.observeCompositionSelection();
+    }
+    if (
+      ev.data &&
+      ev.inputType === 'insertText' &&
+      !this.optionsService.rawOptions.screenReaderMode &&
+      this._compositionHelper instanceof CompositionHelper &&
+      this._compositionHelper.input(ev.data)
+    ) {
+      return true;
+    }
     // Only support emoji IMEs when screen reader mode is disabled as the event must bubble up to
     // support reading out character input which can doubling up input characters
     // Based on these event traces: https://github.com/xtermjs/xterm.js/issues/3679
diff --git a/src/browser/Types.ts b/src/browser/Types.ts
index 497afcf535f3eaca00889525a77e15eb633ccd96..96d499b34605f860608382114c3fbdc07dc6b07f 100644
--- a/src/browser/Types.ts
+++ b/src/browser/Types.ts
@@ -41,9 +41,10 @@ export interface ICompositionHelper {
   readonly isComposing: boolean;
   compositionstart(): void;
   compositionupdate(ev: CompositionEvent): void;
-  compositionend(): void;
+  compositionend(): boolean | void;
   updateCompositionElements(dontRecurse?: boolean): void;
   keydown(ev: KeyboardEvent): boolean;
+  keypress?(text: string): boolean;
 }
 
 export interface IBrowser {
diff --git a/src/browser/input/CompositionHelper.ts b/src/browser/input/CompositionHelper.ts
index c9ec396ab66cb966d49aa63bed09cdf9cd6c4246..cfc6385982b16b8c46b1eff1bbd6a1770c942abe 100644
--- a/src/browser/input/CompositionHelper.ts
+++ b/src/browser/input/CompositionHelper.ts
@@ -1,10 +1,15 @@
+/// <reference lib="es2022.intl" />
 /**
  * Copyright (c) 2016 The xterm.js authors. All rights reserved.
  * @license MIT
  */
 
-import { IRenderService } from '../services/Services';
-import { IBufferService, ICoreService, IOptionsService } from '../../common/services/Services';
+import { IRenderService, IThemeService } from '../services/Services';
+import { IBufferService, ICoreService, IOptionsService, IUnicodeService } from '../../common/services/Services';
+import { WidthCacheFontVariantCanvas } from '../renderer/dom/WidthCache';
+import { addDisposableListener } from '../Dom';
+import { Disposable } from '../../common/Lifecycle';
+import { color } from '../../common/Color';
 import { C0 } from '../../common/data/EscapeSequences';
 
 interface IPosition {
@@ -12,6 +17,27 @@ interface IPosition {
   end: number;
 }
 
+interface IPendingComposition {
+  transactionId: number;
+  finalizerTimer?: ReturnType<typeof setTimeout>;
+  lifecycleSettled: boolean;
+  sessionEnded: boolean;
+  position: IPosition;
+  suffix: string;
+  dataAlreadySent: string;
+  compositionData: string;
+  endData: string;
+  inputData: string;
+  keypressData: string;
+  keypressMayOverlapComposition: boolean;
+  expectsPostCompositionInput: boolean;
+  nextCompositionStart?: number;
+}
+
+const XTERM_COMPOSITION_SESSION_START_EVENT = 'xterm-composition-session-start';
+const XTERM_COMPOSITION_SESSION_END_EVENT = 'xterm-composition-session-end';
+const XTERM_COMPOSITION_TRANSACTION_ACCEPTED_EVENT =
+  'xterm-composition-transaction-accepted';
 /**
  * Encapsulates the logic for handling compositionstart, compositionupdate and compositionend
  * events, displaying the in-progress composition to the UI and forwarding the final composition
@@ -24,6 +50,15 @@ export class CompositionHelper {
    */
   private _isComposing: boolean;
   public get isComposing(): boolean { return this._isComposing; }
+  public get hasPendingCompositionFinalization(): boolean {
+    return this._pendingComposition !== undefined;
+  }
+  public get _isSendingComposition(): boolean {
+    return this.hasPendingCompositionFinalization;
+  }
+  public get _pendingKeypressData(): string {
+    return this._pendingComposition?.keypressData ?? '';
+  }
 
   /**
    * The position within the input textarea's value of the current composition.
@@ -36,52 +71,146 @@ export class CompositionHelper {
    */
   private _compositionSuffix: string;
 
-  /**
-   * Whether a composition is in the process of being sent, setting this to false will cancel any
-   * in-progress composition.
-   */
-  private _isSendingComposition: boolean;
-
   /**
    * Data already sent due to keydown event.
    */
   private _dataAlreadySent: string;
 
+  private _pendingComposition?: IPendingComposition;
+
+  private _isAwaitingCompositionEnd: boolean;
+
+  private _compositionInputData: string;
+
+  private _lastCompositionData: string;
+
+  private _compositionStartValue: string;
+
+  private _compositionStartSelection: IPosition;
+
+  private _compositionHasObservedProgress: boolean;
+
+  private _canceledKey?: Pick<KeyboardEvent, 'code' | 'timeStamp'>;
+
   /**
    * The pending textarea change timer, if any.
    */
   private _textareaChangeTimer?: number;
 
+  /**
+   * Whether the IME consumed the last keydown and still owes the commit it produced. Nothing is
+   * forwarded for such a keydown, so the commit is claimed by whichever observes it first.
+   */
+  private _imeKeydownAwaitingCommit: boolean;
+
+  /**
+   * Identifies the composition transaction that owns deferred work.
+   */
+  private _compositionTransactionId: number;
+
+  /**
+   * Timers that still own deferred composition state.
+   */
+  private _compositionTimers: Set<ReturnType<typeof setTimeout>>;
+
+  private _compositionPositionTimer?: ReturnType<typeof setTimeout>;
+
+  private _compositionViewTimer?: ReturnType<typeof setTimeout>;
+
+  private _compositionEndTimer?: ReturnType<typeof setTimeout>;
+
+  private _preeditRenderer?: CompositionPreeditRenderer;
+
+  /** The preedit's own span, used to anchor the native candidate window. */
+  private _compositionPreedit?: HTMLElement;
+
+  private _compositionSelection?: IPosition;
+
+  /** The rendered row tail, set only while the cursor sits mid-line. */
+  private _compositionRemainder?: HTMLElement;
+
+  /** The insertion caret painted above the renderer cursor the composition view covers. */
+  private _compositionCaret?: HTMLElement;
+
+  // Keep the six-argument form callable: upstream's own CompositionHelper.test.ts constructs
+  // this class directly, and `.npmignore` strips src/**/*.test.ts from the published tarball, so
+  // the shipped patch has no hunk that could update that call. Dropping this overload fails the
+  // upstream build with TS2554. The theme service is therefore optional, and every color read
+  // below keeps the stock fallback that path needs.
+  constructor(
+    textarea: HTMLTextAreaElement,
+    compositionView: HTMLElement,
+    bufferService: IBufferService,
+    optionsService: IOptionsService,
+    coreService: ICoreService,
+    renderService: IRenderService
+  );
   constructor(
     private readonly _textarea: HTMLTextAreaElement,
     private readonly _compositionView: HTMLElement,
     @IBufferService private readonly _bufferService: IBufferService,
     @IOptionsService private readonly _optionsService: IOptionsService,
     @ICoreService private readonly _coreService: ICoreService,
-    @IRenderService private readonly _renderService: IRenderService
+    @IRenderService private readonly _renderService: IRenderService,
+    @IThemeService private readonly _themeService?: IThemeService,
+    @IUnicodeService private readonly _unicodeService?: IUnicodeService
   ) {
     this._isComposing = false;
-    this._isSendingComposition = false;
+    this._isAwaitingCompositionEnd = false;
     this._compositionPosition = { start: 0, end: 0 };
     this._compositionSuffix = '';
     this._dataAlreadySent = '';
+    this._compositionInputData = '';
+    this._lastCompositionData = '';
+    this._compositionStartValue = '';
+    this._compositionStartSelection = { start: 0, end: 0 };
+    this._compositionHasObservedProgress = false;
+    this._compositionTransactionId = 0;
+    this._compositionTimers = new Set();
+    this._imeKeydownAwaitingCommit = false;
   }
 
   /**
    * Handles the compositionstart event, activating the composition view.
    */
   public compositionstart(): void {
-    this._isComposing = true;
+    this._cancelDeferredTimer(this._compositionPositionTimer);
+    this._compositionPositionTimer = undefined;
+    this._cancelDeferredTimer(this._compositionViewTimer);
+    this._compositionViewTimer = undefined;
+    this._cancelDeferredTimer(this._compositionEndTimer);
+    this._compositionEndTimer = undefined;
+    if (this._textareaChangeTimer !== undefined) {
+      clearTimeout(this._textareaChangeTimer);
+      this._textareaChangeTimer = undefined;
+    }
     // It's important to use the selection here instead of textarea length to avoid conflicts with
     // screen reader mode
     const start = this._textarea.selectionStart ?? this._textarea.value.length;
     const end = this._textarea.selectionEnd ?? start;
     this._compositionPosition.start = Math.min(start, end);
     this._compositionPosition.end = Math.max(start, end);
+    this._compositionStartValue = this._textarea.value;
+    this._compositionStartSelection = { start, end };
+    this._compositionHasObservedProgress = false;
+    // A real session owns everything it commits, so no keydown is left owing one.
+    this._imeKeydownAwaitingCommit = false;
+    if (this._pendingComposition) {
+      this._pendingComposition.nextCompositionStart = this._compositionPosition.start;
+    }
+    this._compositionTransactionId++;
+    this._isComposing = true;
+    this._isAwaitingCompositionEnd = true;
     this._compositionSuffix = this._textarea.value.substring(this._compositionPosition.end);
-    this._compositionView.textContent = '';
+    this._resetCompositionView();
     this._dataAlreadySent = '';
+    this._compositionInputData = '';
+    this._lastCompositionData = '';
     this._compositionView.classList.add('active');
+    this._dispatchCompositionSessionEvent(new CustomEvent(XTERM_COMPOSITION_SESSION_START_EVENT, {
+      bubbles: true,
+      detail: { id: this._compositionTransactionId }
+    }));
   }
 
   /**
@@ -89,22 +218,95 @@ export class CompositionHelper {
    * @param ev The event.
    */
   public compositionupdate(ev: Pick<CompositionEvent, 'data'>): void {
-    // Mark text as LTR, direction=rtl is used in CSS so the end of the text is followed for long
-    // compositions
-    this._compositionView.textContent = `\u200E${ev.data}\u200E`;
+    if (ev.data && !this._isComposing) {
+      this.compositionstart();
+    }
+    this._cancelDeferredTimer(this._compositionEndTimer);
+    this._compositionEndTimer = undefined;
+    this._compositionHasObservedProgress ||= this._hasCompositionProgress();
+    if (ev.data?.length > 0) {
+      this._lastCompositionData = ev.data;
+    }
+    this._renderCompositionView(ev.data ?? '');
+    // Some IMEs resume without compositionstart; keep that inferred transaction visible until
+    // compositionend settles it. An empty update hides the overlay without ending the transaction.
+    this._compositionView.classList.toggle('active', Boolean(ev.data));
     this.updateCompositionElements();
-    setTimeout(() => {
-      const end = this._textarea.selectionEnd ?? this._textarea.value.length;
-      this._compositionPosition.end = Math.max( this._compositionPosition.start, end);
-    }, 0);
+    const transactionId = this._compositionTransactionId;
+    this._cancelDeferredTimer(this._compositionPositionTimer);
+    this._compositionPositionTimer = this._defer(() => {
+      if (this._isComposing && this._compositionTransactionId === transactionId) {
+        this._compositionHasObservedProgress ||= this._hasCompositionProgress();
+        const end = this._textarea.selectionEnd ?? this._textarea.value.length;
+        this._compositionPosition.end = Math.max(this._compositionPosition.start, end);
+      }
+    });
   }
 
   /**
    * Handles the compositionend event, hiding the composition view and sending the composition to
    * the handler.
    */
-  public compositionend(): void {
-    this._finalizeComposition(true);
+  public compositionend(ev?: Pick<CompositionEvent, 'data'>): boolean {
+    if (!this._isAwaitingCompositionEnd) {
+      return false;
+    }
+    if (!this._isComposing) {
+      const pending = this._pendingComposition;
+      if (pending?.transactionId === this._compositionTransactionId) {
+        pending.endData = ev?.data ?? '';
+        this._updatePostCompositionInputExpectation(pending);
+      }
+      return false;
+    }
+    const endData = ev?.data ?? '';
+    this._compositionHasObservedProgress ||= this._hasCompositionProgress();
+    if (!this._compositionEndBelongsToCurrentTransaction(endData)) {
+      const pending = this._pendingComposition;
+      if (pending && pending.transactionId !== this._compositionTransactionId) {
+        this._sendPendingComposition(pending);
+      }
+      this._deferCompositionEnd(endData);
+      return false;
+    }
+    this._cancelDeferredTimer(this._compositionEndTimer);
+    this._compositionEndTimer = undefined;
+    this._finalizeComposition(true, endData);
+    return true;
+  }
+
+  public blur(): void {
+    this._cancelDeferredTimer(this._compositionEndTimer);
+    this._compositionEndTimer = undefined;
+    if (this._isComposing) {
+      const end = this._textarea.selectionEnd ?? this._textarea.value.length;
+      this._compositionPosition.end = Math.max(this._compositionPosition.start, end);
+    }
+    if (this._isComposing || this.hasPendingCompositionFinalization) {
+      this._finalizeComposition(false);
+    }
+  }
+
+  public dispose(): void {
+    if (this._textareaChangeTimer !== undefined) {
+      clearTimeout(this._textareaChangeTimer);
+      this._textareaChangeTimer = undefined;
+    }
+    for (const timer of this._compositionTimers) {
+      clearTimeout(timer);
+    }
+    this._compositionTimers.clear();
+    this._compositionPositionTimer = undefined;
+    this._compositionViewTimer = undefined;
+    this._compositionEndTimer = undefined;
+    this._pendingComposition = undefined;
+    this._isAwaitingCompositionEnd = false;
+    this._isComposing = false;
+    this._compositionTransactionId++;
+    this._compositionView.classList.remove('active');
+    this._resetCompositionView();
+    this._preeditRenderer?.dispose();
+    this._preeditRenderer = undefined;
   }
 
   /**
@@ -113,10 +315,33 @@ export class CompositionHelper {
    * @returns Whether the Terminal should continue processing the keydown event.
    */
   public keydown(ev: KeyboardEvent): boolean {
-    if (this._isComposing || this._isSendingComposition) {
+    if (this._canceledKey?.code === ev.code && this._canceledKey.timeStamp === ev.timeStamp) {
+      this._canceledKey = undefined;
+      return false;
+    }
+    // An ended session's commit is accepted; only the Escape that ended it (macOS reports that
+    // keydown after compositionend, still composing) cancels it.
+    if (
+      ev.key === 'Escape' &&
+      (this._isComposing ||
+        (this.hasPendingCompositionFinalization && (ev.isComposing || ev.keyCode === 229)))
+    ) {
+      this._canceledKey = { code: ev.code, timeStamp: ev.timeStamp };
+      this._cancelComposition();
+      return false;
+    }
+    if (this._isComposing || this.hasPendingCompositionFinalization) {
+      // A key the IME swallows can also empty the preedit — backspacing over the last radical of a
+      // Cangjie composition — and some IMEs report that with no composition event at all.
+      this._deferPreeditResync(this._composedRegionLength() > 0);
       if (ev.keyCode === 20 || ev.keyCode === 229) {
         // 20 is CapsLock, 229 is Enter
         // Continue composing if the keyCode is the "composition character"
+        if (ev.keyCode === 229 && !this._isComposing) {
+          // The ended session's commit is already in the textarea unless it still awaits its echo,
+          // so this key owes its own commit if the finalizer runs before that text arrives.
+          this._imeKeydownAwaitingCommit = !this._pendingComposition?.expectsPostCompositionInput;
+        }
         return false;
       }
       if (ev.keyCode === 16 || ev.keyCode === 17 || ev.keyCode === 18) {
@@ -128,6 +353,10 @@ export class CompositionHelper {
       this._finalizeComposition(false);
     }
 
+    // Nothing is forwarded for a keydown the IME consumed, so it is left owing its commit; any
+    // other keydown either forwards its own text or produces none, and clears the debt.
+    this._imeKeydownAwaitingCommit = ev.keyCode === 229;
+
     if (ev.keyCode === 229) {
       // If the "composition character" is used but gets to this point it means a non-composition
       // character (eg. numbers and punctuation) was pressed when the IME was active.
@@ -138,6 +367,95 @@ export class CompositionHelper {
     return true;
   }
 
+  /**
+   * Defers keypress text while a composition finalizer is pending so all input is emitted once
+   * after reconciliation with the final textarea candidate.
+   */
+  public keypress(text: string): boolean {
+    // Every path below sends this key's text, so its keydown no longer owes a commit.
+    this._imeKeydownAwaitingCommit = false;
+    const pending = this._pendingComposition;
+    if (!pending) {
+      return false;
+    }
+    if (pending.keypressMayOverlapComposition) {
+      pending.keypressData += text;
+      return true;
+    }
+    if (pending.expectsPostCompositionInput && pending.keypressData.length === 0) {
+      pending.keypressData = text;
+      return true;
+    }
+    this._sendPendingComposition(pending);
+    return false;
+  }
+
+  public input(text: string): boolean {
+    if (this._isComposing) {
+      this._compositionHasObservedProgress ||= this._hasCompositionProgress();
+      this._compositionInputData += text;
+      return true;
+    }
+    const pending = this._pendingComposition;
+    if (!pending) {
+      return this._claimImeKeydownCommit(text);
+    }
+    // The pending transaction settles this text below, so no keydown still owes it.
+    this._imeKeydownAwaitingCommit = false;
+    if (pending.expectsPostCompositionInput) {
+      pending.inputData += text;
+      pending.expectsPostCompositionInput = false;
+      this._sendPendingComposition(pending);
+      return true;
+    }
+    const repeatsPendingTextareaInput =
+      text.length > 0 &&
+      this._getPendingTextareaInput(pending) === text &&
+      this._getPendingTextareaInput(pending, true) === text;
+    this._sendPendingComposition(pending);
+    if (!repeatsPendingTextareaInput) {
+      this._coreService.triggerDataEvent(text, true);
+    }
+    return true;
+  }
+
+  /** The IME's active clause is the textarea selection inside the preedit; a caret marks none. */
+  public observeCompositionSelection(): void {
+    const preedit = this._compositionPreedit;
+    const text = preedit?.textContent?.slice(1, -1) ?? '';
+    const start = this._compositionPosition.start;
+    const selectionStart = this._textarea.selectionStart - start;
+    const selectionEnd = this._textarea.selectionEnd - start;
+    this._compositionSelection =
+      this._textarea.value.substring(start, start + text.length) === text &&
+      0 <= selectionStart && selectionStart < selectionEnd && selectionEnd <= text.length
+        ? { start: selectionStart, end: selectionEnd }
+        : undefined;
+    if (preedit) {
+      this._preeditRenderer?.render(preedit, this._compositionSelection);
+    }
+  }
+
+  /**
+   * Settles the commit a keydown the IME consumed still owes. That commit normally arrives as the
+   * deferred textarea diff, but an asynchronous IME can deliver it after the diff has already run
+   * and found the textarea unchanged, and with the key still down the terminal drops the input
+   * event instead (#12099). Whichever path sees the commit first sends it and cancels the other, so
+   * an IME that commits before the diff runs still sends once.
+   */
+  private _claimImeKeydownCommit(text: string): boolean {
+    if (!this._imeKeydownAwaitingCommit) {
+      return false;
+    }
+    this._imeKeydownAwaitingCommit = false;
+    if (this._textareaChangeTimer !== undefined) {
+      clearTimeout(this._textareaChangeTimer);
+      this._textareaChangeTimer = undefined;
+    }
+    this._coreService.triggerDataEvent(text, true);
+    return true;
+  }
+
   /**
    * Finalizes the composition, resuming regular input actions. This is called when a composition
    * is ending.
@@ -146,23 +464,52 @@ export class CompositionHelper {
    *   compositionend event is triggered, such as enter, so that the composition is sent before
    *   the command is executed.
    */
-  private _finalizeComposition(waitForPropagation: boolean): void {
+  private _finalizeComposition(waitForPropagation: boolean, endData: string = ''): void {
+    const wasComposing = this._isComposing;
     this._compositionView.classList.remove('active');
+    // Cleared, not just hidden: a rendered tail left in the view is stale DOM the next composition
+    // would have to correct before its own first update lands.
+    this._resetCompositionView();
     this._isComposing = false;
+    if (waitForPropagation && !wasComposing) {
+      return;
+    }
 
     if (!waitForPropagation) {
-      // Cancel any delayed composition send requests and send the input immediately.
-      this._isSendingComposition = false;
-      const input = this._textarea.value.substring(this._compositionPosition.start, this._compositionPosition.end);
-      this._coreService.triggerDataEvent(input, true);
+      if (this._pendingComposition) {
+        this._sendPendingComposition(this._pendingComposition, true);
+      }
+      if (wasComposing) {
+        const input = this._getCompositionInput(
+          this._compositionPosition.start + this._dataAlreadySent.length,
+          this._compositionSuffix
+        );
+        this._sendCompositionInput(this._compositionTransactionId, input);
+      }
     } else {
-      // Make a deep copy of the composition position here as a new compositionstart event may
-      // fire before the setTimeout executes.
-      const currentCompositionPosition = {
-        start: this._compositionPosition.start,
-        end: this._compositionPosition.end
+      if (this._pendingComposition) {
+        this._sendPendingComposition(this._pendingComposition);
+      }
+      const pending: IPendingComposition = {
+        transactionId: this._compositionTransactionId,
+        lifecycleSettled: false,
+        sessionEnded: false,
+        position: {
+          start: this._compositionPosition.start,
+          end: this._compositionPosition.end
+        },
+        suffix: this._compositionSuffix,
+        dataAlreadySent: this._dataAlreadySent,
+        compositionData: this._lastCompositionData,
+        endData,
+        inputData: this._compositionInputData,
+        keypressData: '',
+        keypressMayOverlapComposition:
+          this._lastCompositionData.length === 0 && endData.length === 0,
+        expectsPostCompositionInput: false
       };
-      const currentCompositionSuffix = this._compositionSuffix;
+      this._updatePostCompositionInputExpectation(pending);
+      this._pendingComposition = pending;
 
       // Since composition* events happen before the changes take place in the textarea on most
       // browsers, use a setTimeout with 0ms time to allow the native compositionend event to
@@ -172,35 +519,313 @@ export class CompositionHelper {
       // - The last compositionupdate event's data property does not always accurately describe
       //   the character, a counter example being Korean where an ending consonsant can move to
       //   the following character if the following input is a vowel.
-      this._isSendingComposition = true;
-      setTimeout(() => {
-        // Ensure that the input has not already been sent
-        if (this._isSendingComposition) {
-          this._isSendingComposition = false;
-          let input;
-          // Add length of data already sent due to keydown event,
-          // otherwise input characters can be duplicated. (Issue #3191)
-          currentCompositionPosition.start += this._dataAlreadySent.length;
-          if (this._isComposing) {
-            // Use the start position of the new composition to get the string
-            // if a new composition has started.
-            input = this._textarea.value.substring(currentCompositionPosition.start, this._compositionPosition.start);
-          } else {
-            // Keep support for non-composition characters typed immediately after composition end
-            // while avoiding re-sending the trailing text that was already present
-            // before composition started.
-            const value = this._textarea.value;
-            const valueEnd = currentCompositionSuffix.length > 0 && value.endsWith(currentCompositionSuffix)
-              ? value.length - currentCompositionSuffix.length
-              : value.length;
-            input = value.substring(currentCompositionPosition.start, Math.max(currentCompositionPosition.start, valueEnd));
-          }
-          if (input.length > 0) {
-            this._coreService.triggerDataEvent(input, true);
-          }
+      pending.finalizerTimer = this._defer(() => {
+        pending.finalizerTimer = undefined;
+        if (this._compositionTransactionId === pending.transactionId) {
+          this._isAwaitingCompositionEnd = false;
+        }
+        if (this._pendingComposition === pending) {
+          this._sendPendingComposition(pending, true);
+        }
+      });
+    }
+  }
+
+  private _sendPendingComposition(
+    pending: IPendingComposition,
+    includeFollowingInput: boolean = false
+  ): void {
+    this._cancelPendingFinalizer(pending);
+    if (this._pendingComposition === pending) {
+      this._pendingComposition = undefined;
+    }
+    const textareaInput = this._getPendingTextareaInput(pending, includeFollowingInput);
+    const observedInput = this._removeAlreadySentData(
+      pending.inputData || pending.keypressData,
+      pending.dataAlreadySent
+    );
+    // Why: with no textarea, end, input, or keypress evidence the composition
+    // was cancelled (e.g. Backspace over the whole preedit); stale
+    // compositionupdate data must not be replayed as committed text.
+    const input = this._mergeTextObservations(
+      textareaInput || pending.endData || (observedInput ? pending.compositionData : ''),
+      observedInput,
+      pending.keypressMayOverlapComposition
+    );
+    this._sendCompositionInput(pending.transactionId, input, !pending.sessionEnded);
+    this._settlePendingComposition(pending);
+  }
+
+  private _cancelPendingFinalizer(pending: IPendingComposition): void {
+    if (pending.finalizerTimer === undefined) {
+      return;
+    }
+    clearTimeout(pending.finalizerTimer);
+    this._compositionTimers.delete(pending.finalizerTimer);
+    pending.finalizerTimer = undefined;
+  }
+
+  private _settlePendingComposition(pending: IPendingComposition): void {
+    if (pending.lifecycleSettled) {
+      return;
+    }
+    pending.lifecycleSettled = true;
+    this._dispatchCompositionTransactionSettled();
+  }
+
+  private _mergeTextObservations(
+    candidate: string,
+    observed: string,
+    findShortestOrder: boolean
+  ): string {
+    if (!observed || candidate.includes(observed)) {
+      return candidate;
+    }
+    if (!candidate || observed.includes(candidate)) {
+      return observed;
+    }
+    if (findShortestOrder) {
+      let candidateFirstOverlap = Math.min(candidate.length, observed.length);
+      while (
+        candidateFirstOverlap > 0 &&
+        !candidate.endsWith(observed.substring(0, candidateFirstOverlap))
+      ) {
+        candidateFirstOverlap--;
+      }
+      let observedFirstOverlap = Math.min(candidate.length, observed.length);
+      while (
+        observedFirstOverlap > 0 &&
+        !observed.endsWith(candidate.substring(0, observedFirstOverlap))
+      ) {
+        observedFirstOverlap--;
+      }
+      return candidateFirstOverlap > observedFirstOverlap
+        ? candidate + observed.substring(candidateFirstOverlap)
+        : observed + candidate.substring(observedFirstOverlap);
+    }
+    let overlap = Math.min(candidate.length, observed.length);
+    while (overlap > 0 && !candidate.endsWith(observed.substring(0, overlap))) {
+      overlap--;
+    }
+    return candidate + observed.substring(overlap);
+  }
+
+  private _updatePostCompositionInputExpectation(pending: IPendingComposition): void {
+    pending.expectsPostCompositionInput =
+      (pending.endData.length > 0 || pending.compositionData.length > 0) &&
+      pending.inputData.length === 0 &&
+      this._getPendingTextareaInput(pending).length === 0;
+  }
+
+  private _getPendingTextareaInput(
+    pending: IPendingComposition,
+    includeFollowingInput: boolean = false
+  ): string {
+    const value = this._textarea.value;
+    const start = pending.position.start + pending.dataAlreadySent.length;
+    if (pending.nextCompositionStart !== undefined) {
+      return value.substring(start, Math.max(start, pending.nextCompositionStart));
+    }
+    const suffixEnd =
+      pending.suffix.length > 0 && value.endsWith(pending.suffix)
+        ? value.length - pending.suffix.length
+        : value.length;
+    const compositionLength = (pending.endData || pending.compositionData).length;
+    const observedEnd = includeFollowingInput
+      ? suffixEnd
+      : Math.max(pending.position.end, start + compositionLength);
+    return value.substring(start, Math.max(start, Math.min(suffixEnd, observedEnd)));
+  }
+
+  private _getCompositionInput(start: number, suffix: string): string {
+    const value = this._textarea.value;
+    const valueEnd =
+      suffix.length > 0 && value.endsWith(suffix) ? value.length - suffix.length : value.length;
+    return value.substring(start, Math.max(start, valueEnd));
+  }
+
+  private _removeAlreadySentData(input: string, dataAlreadySent: string): string {
+    if (dataAlreadySent.length === 0) {
+      return input;
+    }
+    if (input.startsWith(dataAlreadySent)) {
+      return input.substring(dataAlreadySent.length);
+    }
+    return dataAlreadySent.includes(input) ? '' : input;
+  }
+
+  private _cancelComposition(): void {
+    const pending = this._pendingComposition;
+    if (
+      pending &&
+      this._isComposing &&
+      pending.transactionId !== this._compositionTransactionId
+    ) {
+      this._sendPendingComposition(pending);
+    }
+    const transactionId = this._isComposing
+      ? this._compositionTransactionId
+      : this._pendingComposition?.transactionId ?? 0;
+    const settlesPending = pending !== undefined && this._pendingComposition === pending;
+    this._pendingComposition = undefined;
+    this._isAwaitingCompositionEnd = false;
+    this._isComposing = false;
+    this._compositionView.classList.remove('active');
+    this._resetCompositionView();
+    this._textarea.value =
+      this._textarea.value.substring(0, this._compositionPosition.start) + this._compositionSuffix;
+    this._sendCompositionInput(transactionId, '');
+    if (settlesPending && pending) {
+      this._settlePendingComposition(pending);
+    }
+  }
+
+  private _sendCompositionInput(
+    transactionId: number,
+    input: string,
+    dispatchSessionEnd: boolean = true
+  ): void {
+    let prevented = false;
+    if (dispatchSessionEnd) {
+      const event = new CustomEvent(XTERM_COMPOSITION_SESSION_END_EVENT, {
+        bubbles: true,
+        cancelable: true,
+        detail: { id: transactionId, data: input }
+      });
+      this._dispatchCompositionSessionEvent(event);
+      prevented = event.defaultPrevented;
+    }
+    if (input.length > 0 && !prevented) {
+      this._coreService.triggerDataEvent(input, true);
+    }
+  }
+
+  private _endPendingCompositionSession(pending: IPendingComposition): void {
+    if (pending.sessionEnded) {
+      return;
+    }
+    pending.sessionEnded = true;
+    const input =
+      this._getPendingTextareaInput(pending) ||
+      pending.endData ||
+      pending.compositionData;
+    this._dispatchCompositionSessionEvent(new CustomEvent(
+      XTERM_COMPOSITION_SESSION_END_EVENT,
+      {
+        bubbles: true,
+        cancelable: true,
+        detail: {
+          id: pending.transactionId,
+          data: input,
+          dataPendingReconciliation: true
+        }
+      }
+    ));
+  }
+
+  private _dispatchCompositionSessionEvent(event: CustomEvent): void {
+    if (typeof this._textarea.dispatchEvent === 'function') {
+      this._textarea.dispatchEvent(event);
+    }
+  }
+
+  private _dispatchCompositionTransactionSettled(): void {
+    this._dispatchCompositionSessionEvent(new CustomEvent(
+      'xterm-composition-transaction-settled',
+      { bubbles: true }
+    ));
+  }
+
+  private _deferCompositionEnd(endData: string): void {
+    this._cancelDeferredTimer(this._compositionEndTimer);
+    const transactionId = this._compositionTransactionId;
+    const timer = this._defer(() => {
+      if (
+        this._compositionEndTimer !== timer ||
+        !this._isComposing ||
+        this._compositionTransactionId !== transactionId
+      ) {
+        return;
+      }
+      this._compositionEndTimer = undefined;
+      if (!this._compositionEndBelongsToCurrentTransaction(endData)) {
+        if (endData.length === 0 && !this._hasCompositionProgress()) {
+          this._cancelComposition();
         }
-      }, 0);
+        return;
+      }
+      this._finalizeComposition(true, endData);
+      this._dispatchCompositionSessionEvent(new CustomEvent(
+        XTERM_COMPOSITION_TRANSACTION_ACCEPTED_EVENT,
+        { bubbles: true }
+      ));
+      const pending = this._pendingComposition;
+      if (pending?.transactionId === transactionId) {
+        this._sendPendingComposition(pending, true);
+      }
+    });
+    this._compositionEndTimer = timer;
+  }
+
+  /** How much of the textarea the IME currently owns; 0 means there is no preedit left. */
+  private _composedRegionLength(): number {
+    const end = this._textarea.value.length - this._compositionSuffix.length;
+    return Math.max(0, end - this._compositionPosition.start);
+  }
+
+  /**
+   * Re-derives the preedit from the textarea once the key that changed it has settled, and treats
+   * a composition emptied that way as cancelled. Mirrors how native terminals clear a preedit on
+   * the empty-marked-text state instead of on a specific key.
+   */
+  private _deferPreeditResync(hadPreedit: boolean): void {
+    if (!hadPreedit || !this._isComposing) {
+      return;
+    }
+    const transactionId = this._compositionTransactionId;
+    this._defer(() => {
+      if (
+        this._isComposing &&
+        this._compositionTransactionId === transactionId &&
+        this._composedRegionLength() === 0
+      ) {
+        this._cancelComposition();
+      }
+    });
+  }
+
+  private _hasCompositionProgress(): boolean {
+    const start = this._textarea.selectionStart ?? this._textarea.value.length;
+    const end = this._textarea.selectionEnd ?? start;
+    return this._compositionHasObservedProgress || (
+      this._textarea.value !== this._compositionStartValue ||
+      start !== this._compositionStartSelection.start ||
+      end !== this._compositionStartSelection.end
+    );
+  }
+
+  private _compositionEndBelongsToCurrentTransaction(endData: string): boolean {
+    return (
+      this._hasCompositionProgress() ||
+      (endData.length > 0 && endData === this._lastCompositionData)
+    );
+  }
+
+  private _defer(callback: () => void): ReturnType<typeof setTimeout> {
+    const timer = setTimeout(() => {
+      this._compositionTimers.delete(timer);
+      callback();
+    }, 0);
+    this._compositionTimers.add(timer);
+    return timer;
+  }
+
+  private _cancelDeferredTimer(timer?: ReturnType<typeof setTimeout>): void {
+    if (timer === undefined) {
+      return;
     }
+    clearTimeout(timer);
+    this._compositionTimers.delete(timer);
   }
 
   /**
@@ -222,6 +847,9 @@ export class CompositionHelper {
 
         const diff = newValue.replace(oldValue, '');
 
+        if (newValue !== oldValue) {
+          this._imeKeydownAwaitingCommit = false;
+        }
         this._dataAlreadySent = diff;
 
         if (newValue.length > oldValue.length) {
@@ -236,6 +864,106 @@ export class CompositionHelper {
     }, 0);
   }
 
+  /**
+   * Renders the preedit into the view and, when the cursor sits mid-line, the rest of the row
+   * after it, so a composition reads as inserted text pushing the tail right rather than an opaque
+   * box hiding the character under the cursor. Nothing reaches the pty while composing, so those
+   * cells still hold their characters; only what the overlay shows changes.
+   */
+  private _renderCompositionView(data: string): void {
+    if (!data) {
+      this._resetCompositionView();
+      return;
+    }
+    this._compositionSelection = undefined;
+    const doc = this._compositionView.ownerDocument;
+    const preedit = doc.createElement('span');
+    preedit.className = 'xterm-composition-preedit';
+    // Underlined so the composing text stays distinguishable from the tail it pushed right.
+    preedit.style.flexShrink = '0';
+    preedit.style.textDecoration = 'underline';
+    preedit.textContent = `‎${data}‎`;
+    const caret = doc.createElement('span');
+    caret.className = 'xterm-composition-caret';
+    caret.setAttribute('aria-hidden', 'true');
+    this._compositionView.replaceChildren(preedit, caret);
+    this._compositionPreedit = preedit;
+    this._compositionCaret = caret;
+    this._compositionRemainder = undefined;
+    this._renderCompositionRemainder(this._getRowRemainderText());
+    this._styleCompositionCaret();
+  }
+
+  private _renderCompositionRemainder(data: string): void {
+    if (!data) {
+      this._compositionRemainder?.remove();
+      this._compositionRemainder = undefined;
+      return;
+    }
+    if (!this._compositionRemainder) {
+      const remainder = this._compositionView.ownerDocument.createElement('span');
+      remainder.className = 'xterm-composition-remainder';
+      // Preserve padding cells before the trailing glyphs.
+      remainder.style.whiteSpace = 'pre';
+      this._compositionView.appendChild(remainder);
+      this._compositionRemainder = remainder;
+    }
+    this._compositionRemainder.textContent = data;
+  }
+
+  /** The committed row text from the cursor rightwards — what a mid-line preedit would cover. */
+  private _getRowRemainderText(): string {
+    const buffer = this._bufferService.buffer;
+    if (!buffer.isCursorInViewport) {
+      return '';
+    }
+    const line = buffer.lines.get(buffer.ybase + buffer.y);
+    // The explicit end column keeps this off the line string cache, whose self-renewing
+    // idle-clear timer the composition path must not arm.
+    return line
+      ? line.translateToString(true, Math.min(buffer.x, this._bufferService.cols - 1), line.length)
+      : '';
+  }
+
+  private _styleCompositionCaret(): void {
+    const caret = this._compositionCaret;
+    if (!caret) {
+      return;
+    }
+    const width = Math.max(1, this._optionsService.rawOptions.cursorWidth);
+    const cellHeight = this._renderService.dimensions.css.cell.height;
+    const colors = this._themeService?.colors;
+    const cursor = colors && (
+      color.ensureContrastRatio(colors.background, colors.cursor, 3) ?? colors.cursor
+    );
+    caret.style.backgroundColor = cursor?.css ?? '#FFF';
+    caret.style.display = 'inline-block';
+    caret.style.flexShrink = '0';
+    caret.style.height = cellHeight + 'px';
+    caret.style.marginLeft = -width + 'px';
+    caret.style.verticalAlign = 'top';
+    caret.style.width = width + 'px';
+  }
+
+  private _resetCompositionView(): void {
+    this._preeditRenderer?.clear();
+    this._compositionView.textContent = '';
+    this._compositionPreedit = undefined;
+    this._compositionRemainder = undefined;
+    this._compositionCaret = undefined;
+    this._compositionView.style.display = '';
+    this._compositionView.style.justifyContent = '';
+  }
+
+  /**
+   * The theme background with any alpha dropped. The view masks the cells it draws over, so a
+   * see-through background would re-expose the very characters the rendered tail stands in for.
+   */
+  private _opaqueViewBackground(): string {
+    const background = this._themeService?.colors.background;
+    return background ? color.opaque(background).css : '#000';
+  }
+
   /**
    * Positions the composition view on top of the cursor and the textarea just below it (so the
    * IME helper dialog is positioned correctly).
@@ -243,10 +971,33 @@ export class CompositionHelper {
    *   necessary as the IME events across browsers are not consistently triggered.
    */
   public updateCompositionElements(dontRecurse?: boolean): void {
-    if (!this._isComposing) {
+    // Empty updates hide the overlay without ending the inferred transaction.
+    if (!this._compositionView.classList.contains('active')) {
       return;
     }
 
+    const colors = this._themeService?.colors;
+    this._compositionView.style.setProperty(
+      '--xterm-composition-selection-background',
+      colors?.selectionBackgroundOpaque.css ?? '#FFF'
+    );
+    this._compositionView.style.setProperty(
+      '--xterm-composition-selection-foreground',
+      colors?.selectionForeground?.css ?? colors?.foreground.css ?? '#000'
+    );
+
+    // A TUI can repaint the row under an open composition (spinners, streamed output), and this
+    // already runs on every render — so keep the rendered tail current with the buffer. A string
+    // compare adds no layout read.
+    const rowRemainder = this._getRowRemainderText();
+    if (
+      this._compositionPreedit &&
+      rowRemainder !== (this._compositionRemainder?.textContent ?? '')
+    ) {
+      this._renderCompositionRemainder(rowRemainder);
+    }
+    this._styleCompositionCaret();
+
     if (this._bufferService.buffer.isCursorInViewport) {
       const cursorX = Math.min(this._bufferService.buffer.x, this._bufferService.cols - 1);
 
@@ -254,31 +1005,176 @@ export class CompositionHelper {
       const cursorTop = this._bufferService.buffer.y * this._renderService.dimensions.css.cell.height;
       const cursorLeft = cursorX * this._renderService.dimensions.css.cell.width;
 
+      this._compositionView.style.setProperty('--xterm-composition-cell-width', this._renderService.dimensions.css.cell.width + 'px');
       this._compositionView.style.left = cursorLeft + 'px';
       this._compositionView.style.top = cursorTop + 'px';
       this._compositionView.style.height = cellHeight + 'px';
       this._compositionView.style.lineHeight = cellHeight + 'px';
       this._compositionView.style.fontFamily = this._optionsService.rawOptions.fontFamily;
       this._compositionView.style.fontSize = this._optionsService.rawOptions.fontSize + 'px';
+      if (this._compositionPreedit && this._unicodeService && typeof Intl.Segmenter !== 'undefined') {
+        this._preeditRenderer ??= new CompositionPreeditRenderer(
+          this._unicodeService, this._compositionView.ownerDocument, () => this.updateCompositionElements(true)
+        );
+        this._preeditRenderer.render(this._compositionPreedit, this._compositionSelection);
+      }
       // Limit the composition view width to the space between the cursor and
       // the terminal's right edge, preventing it from overflowing the terminal.
       const maxWidth = this._bufferService.cols * this._renderService.dimensions.css.cell.width - cursorLeft;
       this._compositionView.style.maxWidth = maxWidth + 'px';
       this._compositionView.style.overflow = 'hidden';
-      this._compositionView.style.direction = 'rtl';
-      // Sync the textarea to the exact position of the composition view so the IME knows where the
-      // text is.
-      const compositionViewBounds = this._compositionView.getBoundingClientRect();
-      this._textarea.style.left = cursorLeft + 'px';
+      const anchorBounds =
+        (this._compositionPreedit ?? this._compositionView).getBoundingClientRect();
+      const anchorLeft = cursorLeft + Math.min(0, maxWidth - anchorBounds.width);
+      const showsRemainder =
+        Boolean(this._compositionRemainder) && anchorBounds.width < maxWidth;
+      if (this._compositionRemainder) {
+        this._compositionRemainder.style.display = showsRemainder ? '' : 'none';
+      }
+      // End alignment keeps the caret visible when the preedit consumes the remaining width.
+      this._compositionView.style.direction = 'ltr';
+      this._compositionView.style.display = showsRemainder ? '' : 'flex';
+      this._compositionView.style.justifyContent = showsRemainder ? '' : 'flex-end';
+      // Themed rather than the stock #000/#FFF, so the pushed tail reads as ordinary terminal text
+      // and light themes keep contrast.
+      this._compositionView.style.background = this._opaqueViewBackground();
+      this._compositionView.style.color = this._themeService?.colors.foreground.css ?? '#FFF';
+      // Sized and placed to match the preedit, not the whole view, so the candidate window
+      // anchors to the composing text rather than the end of the rendered tail. The clamp has to
+      // be applied here and not only in Orca's terminal-ime-candidate-anchor.ts, because
+      // CoreBrowserTerminal calls this from onRender as well as from composition events, and a
+      // render can land after the last composition event that module can hear.
+      this._textarea.style.left = anchorLeft + 'px';
       this._textarea.style.top = cursorTop + 'px';
       // Ensure the text area is at least 1x1, otherwise certain IMEs may break
-      this._textarea.style.width = Math.max(compositionViewBounds.width, 1) + 'px';
-      this._textarea.style.height = Math.max(compositionViewBounds.height, 1) + 'px';
-      this._textarea.style.lineHeight = compositionViewBounds.height + 'px';
+      this._textarea.style.width = Math.max(anchorBounds.width, 1) + 'px';
+      this._textarea.style.height = Math.max(anchorBounds.height, 1) + 'px';
+      this._textarea.style.lineHeight = anchorBounds.height + 'px';
     }
 
     if (!dontRecurse) {
-      setTimeout(() => this.updateCompositionElements(true), 0);
+      this._cancelDeferredTimer(this._compositionViewTimer);
+      this._compositionViewTimer = this._defer(() => this.updateCompositionElements(true));
+    }
+  }
+}
+
+const CJK = /^[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}\uac00-\ud7a3\u3131-\u318e\uff01-\uff60]$/u;
+const NATIVE = /[\p{Mark}\p{Extended_Pictographic}]/u;
+const MAX_PREEDIT_LAYOUT_WORK = 128;
+
+class CompositionPreeditRenderer extends Disposable {
+  private readonly _segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
+  private _measure?: WidthCacheFontVariantCanvas;
+  private _preedit?: HTMLElement;
+  private _font = '';
+  private _selectionStart?: number;
+  private _selectionEnd?: number;
+
+  constructor(private readonly _unicodeService: IUnicodeService, doc: Document, refresh: () => void) {
+    super();
+    if (doc.fonts) {
+      this._register(addDisposableListener(doc.fonts, 'loadingdone', () => {
+        this._font = '';
+        if (this._preedit) {
+          refresh();
+        }
+      }));
+    }
+  }
+
+  public clear(): void {
+    this._preedit = undefined;
+    this._font = '';
+  }
+
+  public override dispose(): void {
+    this.clear();
+    this._measure = undefined;
+    super.dispose();
+  }
+
+  public render(preedit: HTMLElement, selection?: IPosition): void {
+    const doc = preedit.ownerDocument;
+    const style = doc.defaultView?.getComputedStyle(preedit);
+    if (!style) {
+      return;
+    }
+    const font = [style.fontFamily, style.fontSize, style.fontWeight, style.fontStyle, doc.defaultView?.devicePixelRatio].join('|');
+    if (
+      this._preedit === preedit &&
+      this._font === font &&
+      this._selectionStart === selection?.start &&
+      this._selectionEnd === selection?.end
+    ) {
+      return;
+    }
+    this._preedit = preedit;
+    this._font = font;
+    this._selectionStart = selection?.start;
+    this._selectionEnd = selection?.end;
+    const data = (preedit.textContent ?? '').slice(1, -1);
+    const widths = new Map<string, number>();
+    const nodes: Node[] = [doc.createTextNode('‎')];
+    let start = 0;
+    let spacing: string | undefined;
+    let selected = false;
+    let work = 0;
+    const emit = (end: number, countLayoutWork: boolean = true): void => {
+      if (end === start) {
+        return;
+      }
+      const text = data.slice(start, end);
+      if (spacing === undefined && !selected) {
+        nodes.push(doc.createTextNode(text));
+      } else {
+        const span = doc.createElement('span');
+        if (spacing !== undefined) {
+          span.style.letterSpacing = spacing;
+          span.style.fontKerning = 'none';
+          if (countLayoutWork) {
+            work++;
+          }
+        }
+        span.style.textDecoration = 'inherit';
+        if (selected) {
+          span.classList.add('xterm-composition-preedit-selected');
+          span.style.backgroundColor = 'var(--xterm-composition-selection-background)';
+          span.style.color = 'var(--xterm-composition-selection-foreground)';
+        }
+        span.textContent = text;
+        nodes.push(span);
+      }
+      start = end;
+    };
+    for (const { segment, index } of this._segmenter.segment(data)) {
+      // Why: a clause boundary inside a grapheme highlights the whole grapheme.
+      const nextSelected =
+        selection !== undefined && index < selection.end && index + segment.length > selection.start;
+      let nextSpacing: string | undefined;
+      const columns = this._unicodeService.wcwidth(segment.codePointAt(0)!);
+      // Bound cold measurements and styled runs without changing an already-corrected prefix.
+      if (work < MAX_PREEDIT_LAYOUT_WORK && CJK.test(segment) && !NATIVE.test(segment) && (columns === 2 || /^[\uff61-\uff9f]$/u.test(segment))) {
+        let width = widths.get(segment);
+        if (width === undefined) {
+          this._measure ??= new WidthCacheFontVariantCanvas();
+          this._measure.setFont(style.fontFamily, parseFloat(style.fontSize), parseFloat(style.fontWeight) || 400, style.fontStyle === 'italic');
+          width = this._measure.measure(segment);
+          widths.set(segment, width);
+          work++;
+        }
+        nextSpacing = `calc(var(--xterm-composition-cell-width) * ${columns} - ${width}px)`;
+      }
+      const spacingChanged = nextSpacing !== spacing;
+      if (spacingChanged || nextSelected !== selected) {
+        // Why: selection-only splits do not add layout or measurement work.
+        emit(index, spacingChanged);
+        spacing = nextSpacing;
+        selected = nextSelected;
+      }
     }
+    emit(data.length);
+    nodes.push(doc.createTextNode('‎'));
+    preedit.replaceChildren(...nodes);
   }
 }
diff --git a/src/browser/renderer/dom/WidthCache.ts b/src/browser/renderer/dom/WidthCache.ts
index d45938515e7a9903de0da0d61716fd42e4deea0c..17d6577e388eb2e4397338093e35373864b822a4 100644
--- a/src/browser/renderer/dom/WidthCache.ts
+++ b/src/browser/renderer/dom/WidthCache.ts
@@ -142,7 +142,7 @@ export class WidthCache implements IDisposable {
   }
 }
 
-class WidthCacheFontVariantCanvas implements IWidthCacheFontVariantCanvas {
+export class WidthCacheFontVariantCanvas implements IWidthCacheFontVariantCanvas {
   private _canvas: OffscreenCanvas | HTMLCanvasElement;
   private _ctx: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D;
 
diff --git a/src/browser/services/MouseService.ts b/src/browser/services/MouseService.ts
index 353e0581da7a95c9473d6169cd9ec548bc3bfe30..a51645fb22b922f8fde5c10730d029e14b8912cd 100644
--- a/src/browser/services/MouseService.ts
+++ b/src/browser/services/MouseService.ts
@@ -495,9 +495,9 @@ export class MouseService implements IMouseService {
    * to fulfill protocol and encoding restrictions.
    */
   private _triggerMouseEvent(e: ICoreMouseEvent): boolean {
-    // range check for col/row
-    if (e.col < 0 || e.col >= this._bufferService.cols
-      || e.row < 0 || e.row >= this._bufferService.rows) {
+    // range check for col/row; negated so NaN (touch inertia has no clientX/Y) fails too
+    if (!(e.col >= 0 && e.col < this._bufferService.cols
+      && e.row >= 0 && e.row < this._bufferService.rows)) {
       return false;
     }
 
diff --git a/src/common/InputHandler.ts b/src/common/InputHandler.ts
index 995fe7670a10f11fb9b0b42332484b038ea6c989..3df13f60209ccd097b861d94efd481988d38400f 100644
--- a/src/common/InputHandler.ts
+++ b/src/common/InputHandler.ts
@@ -2468,6 +2468,7 @@ export class InputHandler extends Disposable implements IInputHandler {
       case 58:
         attr.extended = attr.extended.clone();
         attr.extended.underlineColor = this._updateAttrColor(attr.extended.underlineColor, accu[1], accu[3], accu[4], accu[5]);
+        attr.updateExtended();
     }
 
     return advance;
diff --git a/src/common/SortedList.ts b/src/common/SortedList.ts
index 8a10076e3963e33b4a7d1e4602333eb3f4772dc9..c6dcf18b762e3c56fe22e9c2d49b8e550d96f915 100644
--- a/src/common/SortedList.ts
+++ b/src/common/SortedList.ts
@@ -22,7 +22,8 @@ export class SortedList<T> {
   private readonly _flushInsertedTask: InstanceType<typeof IdleTaskQueue>;
   private _isFlushingInserted = false;
 
-  private readonly _deletedIndices: number[] = [];
+  private readonly _deletedIndices = new Set<number>();
+  private readonly _indicesByValue = new Map<T, number | number[]>();
   private readonly _flushDeletedTask: InstanceType<typeof IdleTaskQueue>;
   private _isFlushingDeleted = false;
 
@@ -36,10 +37,11 @@ export class SortedList<T> {
 
   public clear(): void {
     this._array.length = 0;
+    this._indicesByValue.clear();
     this._insertedValues.length = 0;
     this._flushInsertedTask.clear();
     this._isFlushingInserted = false;
-    this._deletedIndices.length = 0;
+    this._deletedIndices.clear();
     this._flushDeletedTask.clear();
     this._isFlushingDeleted = false;
   }
@@ -69,6 +71,7 @@ export class SortedList<T> {
     }
 
     this._array = newArray;
+    this._rebuildIdentityIndex();
     this._insertedValues.length = 0;
   }
 
@@ -78,54 +81,60 @@ export class SortedList<T> {
     }
   }
 
+  private _rebuildIdentityIndex(): void {
+    this._indicesByValue.clear();
+    // Reverse indices let duplicate identities remove their first occurrence in O(1).
+    for (let index = this._array.length - 1; index >= 0; index--) {
+      const value = this._array[index];
+      const indices = this._indicesByValue.get(value);
+      if (indices === undefined) {
+        this._indicesByValue.set(value, index);
+      } else if (typeof indices === 'number') {
+        this._indicesByValue.set(value, [indices, index]);
+      } else {
+        indices.push(index);
+      }
+    }
+  }
+
   public delete(value: T): boolean {
     this._flushCleanupInserted();
-    if (this._array.length === 0) {
+    // Marker disposal mutates the sort key before removal; identity stays stable.
+    const indices = this._indicesByValue.get(value);
+    if (indices === undefined) {
       return false;
     }
-    const key = this._getKey(value);
-    if (key === undefined) {
+    const index = typeof indices === 'number' ? indices : indices.pop();
+    if (index === undefined) {
       return false;
     }
-    i = this._search(key);
-    if (i === -1) {
-      return false;
+    if (typeof indices === 'number' || indices.length === 0) {
+      this._indicesByValue.delete(value);
     }
-    if (this._getKey(this._array[i]) !== key) {
-      return false;
+    if (this._deletedIndices.size === 0) {
+      this._flushDeletedTask.enqueue(() => this._flushDeleted());
     }
-    do {
-      if (this._array[i] === value) {
-        if (this._deletedIndices.length === 0) {
-          this._flushDeletedTask.enqueue(() => this._flushDeleted());
-        }
-        this._deletedIndices.push(i);
-        return true;
-      }
-    } while (++i < this._array.length && this._getKey(this._array[i]) === key);
-    return false;
+    this._deletedIndices.add(index);
+    return true;
   }
 
   private _flushDeleted(): void {
     this._isFlushingDeleted = true;
-    const sortedDeletedIndices = this._deletedIndices.sort((a, b) => a - b);
-    let sortedDeletedIndicesIndex = 0;
-    const newArray = new Array(this._array.length - sortedDeletedIndices.length);
+    const newArray = new Array(this._array.length - this._deletedIndices.size);
     let newArrayIndex = 0;
     for (let i = 0; i < this._array.length; i++) {
-      if (sortedDeletedIndices[sortedDeletedIndicesIndex] === i) {
-        sortedDeletedIndicesIndex++;
-      } else {
+      if (!this._deletedIndices.has(i)) {
         newArray[newArrayIndex++] = this._array[i];
       }
     }
     this._array = newArray;
-    this._deletedIndices.length = 0;
+    this._rebuildIdentityIndex();
+    this._deletedIndices.clear();
     this._isFlushingDeleted = false;
   }
 
   private _flushCleanupDeleted(): void {
-    if (!this._isFlushingDeleted && this._deletedIndices.length > 0) {
+    if (!this._isFlushingDeleted && this._deletedIndices.size > 0) {
       this._flushDeletedTask.flush();
     }
   }
diff --git a/src/common/buffer/AttributeData.ts b/src/common/buffer/AttributeData.ts
index b37c1550887d467d8af0c46da96defa296ede197..9569755e6d6b112c4403f424b5d2f394e789ebf5 100644
--- a/src/common/buffer/AttributeData.ts
+++ b/src/common/buffer/AttributeData.ts
@@ -168,7 +168,7 @@ export class ExtendedAttrs implements IExtendedAttrs {
   }
   public set underlineColor(value: number) {
     this._ext &= ~(Attributes.CM_MASK | Attributes.RGB_MASK);
-    this._ext |= value & (Attributes.CM_MASK | Attributes.RGB_MASK);
+    this._ext |= (value === -1 ? 0 : value) & (Attributes.CM_MASK | Attributes.RGB_MASK);
   }
 
   private _urlId: number = 0;
@@ -208,6 +208,7 @@ export class ExtendedAttrs implements IExtendedAttrs {
    * that needs to be persistant in the buffer.
    */
   public isEmpty(): boolean {
-    return this.underlineStyle === UnderlineStyle.NONE && this._urlId === 0 && this.payload === undefined;
+    return this.underlineStyle === UnderlineStyle.NONE && this._urlId === 0 && this.payload === undefined
+      && (this.underlineColor & Attributes.CM_MASK) === 0;
   }
 }
diff --git a/src/common/buffer/Buffer.ts b/src/common/buffer/Buffer.ts
index e3a88bfeb4bbf79214d799adec30cba8e4dc1b49..ca7ee6b45245a2ea5a2b027e192b98cda7ea5e36 100644
--- a/src/common/buffer/Buffer.ts
+++ b/src/common/buffer/Buffer.ts
@@ -165,9 +165,9 @@ export class Buffer extends Disposable implements IBuffer {
     let dirtyMemoryLines = 0;
 
     // Increase max length if needed before adjustments to allow space to fill
-    // as required.
+    // as required. Empty buffers must also shrink before their first activation.
     const newMaxLength = this._getCorrectBufferLength(newRows);
-    if (newMaxLength > this.lines.maxLength) {
+    if (newMaxLength > this.lines.maxLength || this.lines.length === 0) {
       this.lines.maxLength = newMaxLength;
     }