Skip to content

Terminal type-ahead latency stats are sorted as strings instead of numbers #341302

Closed

Description

Does this issue occur when all extensions are disabled?: Yes (the bug is in core code and reproduces in a unit test; I have not verified it in the UI)

  • VS Code Version: 1.143.0 (main at 7e092ca)
  • OS Version: Windows 11 Pro 10.0.26300

Problem

PredictionStats.latency in src/vs/workbench/contrib/terminalContrib/typeAhead/browser/terminalTypeAheadAddon.ts sorts an array of numbers without a compare function:

const latencies = this._stats.filter(([, correct]) => correct).map(([s]) => s).sort();

Array.prototype.sort() without a compare function converts the elements to strings and sorts them lexicographically. As a result, min, median and max are wrong whenever the recorded latencies have a different number of digits.

Steps to Reproduce

I found this by reading the code, so the reproduction is a unit test rather than UI steps.

  1. Add the following test to the PredictionStats suite in src/vs/workbench/contrib/terminalContrib/typeAhead/test/browser/terminalTypeAhead.test.ts. It records latencies of 5ms, 50ms and 200ms.
test('sorts latencies numerically', () => {
	const stubs = createPredictionStubs(3);
	const clock = useFakeTimers();
	try {
		for (const s of stubs) { add.fire(s); }

		clock.tick(5);
		succeed.fire(stubs[0]);
		clock.tick(45);
		succeed.fire(stubs[1]);
		clock.tick(150);
		succeed.fire(stubs[2]);

		assert.deepStrictEqual(stats.latency, {
			count: 3,
			min: 5,
			max: 200,
			median: 50
		});
	} finally {
		clock.restore();
	}
});
  1. Run scripts\test.bat --grep "Terminal Typeahead" (or ./scripts/test.sh --grep "Terminal Typeahead").

Expected: { count: 3, min: 5, median: 50, max: 200 }

Actual: { count: 3, min: 200, median: 5, max: 50 }

The existing test creates sane data does not catch this because it only uses latencies with the same number of digits (100, 300, 500).

Impact

  • latency.median is compared against terminal.integrated.localEchoLatencyThreshold in _reevaluatePredictorStateNow to decide whether predictions are shown. With a wrong median, local echo can be turned off on a slow connection or turned on on a fast one. This only affects users who have enabled terminal.integrated.localEchoEnabled.
  • The same min / median / max values are sent in the terminalLatencyStats telemetry event, so those numbers are inaccurate.

Proposed Fix

Pass a numeric compare function: .sort((a, b) => a - b). I have a fix with the test above ready and will open a PR.

Activity

  1. added a commit that references this issue on Oct 13, 2026
    2e086c5
  2. added
    unreleased
    on Oct 13, 2026
  3. added this to the 1.143.0 milestone on Oct 13, 2026
  4. added
    insiders-released
    and removed
    unreleased
    on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

insiders-released

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions