public void onTerminalStateChanged() {
        if (!myTerminalModel.isTypeAheadEnabled() || myOutOfSyncDetected) return;
        myTerminalModel.lock();
        try {
            if (myTerminalModel.isUsingAlternateBuffer()) {
                resetState();
                return;
            }
            TypeAheadTerminalModel.LineWithCursorX lineWithCursorX = myTerminalModel.getCurrentLineWithCursor();
            if (!myPredictions.isEmpty()) {
                updateLeftMostCursorPosition(lineWithCursorX.myCursorX);
                if (myClearPredictionsDebouncer != null) {
                    myClearPredictionsDebouncer.call();
                }
            }
            if (myLastSuccessfulPrediction != null && lineWithCursorX.equals(myLastSuccessfulPrediction.myPredictedLineWithCursorX)) {
                return;
            }
            ArrayList<TypeAheadPrediction> removedPredictions = new ArrayList<>();
            while (!myPredictions.isEmpty() && !lineWithCursorX.equals(myPredictions.get(0).myPredictedLineWithCursorX)) {
                removedPredictions.add(myPredictions.remove(0));
            }
            if (myPredictions.isEmpty()) {
                myOutOfSyncDetected = true;
                resetState();
            } else {
                myLastSuccessfulPrediction = myPredictions.remove(0);
                removedPredictions.add(myLastSuccessfulPrediction);
                for (TypeAheadPrediction prediction : removedPredictions) {
                    myLatencyStatistics.adjustLatency(prediction);

                    if (prediction instanceof CharacterPrediction) {
                        myIsNotPasswordPrompt = true;
                    }
                }
                applyPredictions();
            }
        } finally {
            myTerminalModel.unlock();
        }
    }
@Test
    public void testTerminateDebounceOnInvalidState() throws Exception {
        new TestRunner() {

            @Override
            void run() {
                model.insertString("a");
                manager.onTerminalStateChanged();
                boolean found = false;
                for (Action action : actions) {
                    if (action instanceof Action.TerminateDebouncer) {
                        assertFalse(found);
                        found = true;
                    }
                }
                assertTrue(found);
            }
        }.fillLatencyStats().setIsNotPasswordPrompt().run();
    }