Repository navigation
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.
- Add the following test to the
PredictionStatssuite insrc/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();
}
});- 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.medianis compared againstterminal.integrated.localEchoLatencyThresholdin_reevaluatePredictorStateNowto 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 enabledterminal.integrated.localEchoEnabled.- The same
min/median/maxvalues are sent in theterminalLatencyStatstelemetry 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
fix: sort terminal type-ahead latencies numerically (#341303)