From 83cfaeb87a063dee48a183a9a315b0d10759d80c Mon Sep 17 00:00:00 2001 From: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com> Date: Tue, 27 Oct 2026 21:59:58 -0400 Subject: [PATCH 1/2] Fix History.truncate() leaving session start index stale Truncation dropped the oldest items without shifting session_start_index, so retained current-session entries were hidden from unbounded ranges and searches. Shift the index by the number of removed items (clamped at 0), and reuse clear() when removing all history. Fixes #1785 --- CHANGELOG.md | 2 ++ cmd2/history.py | 11 +++++++--- tests/test_history.py | 50 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a463c146d..df6f0d447 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ - History regular expressions containing range separators are now handled as regex searches. Other selectors are treated as numbers or ranges only when they match the supported syntax; strings such as URLs, `a:b`, `+1`, and `1_0` remain literal searches. + - Fixed `History.truncate()` leaving the current session's start index unchanged, which hid + retained current-session entries from unbounded ranges and searches. ## 4.3.1 (October 24, 2026) diff --git a/cmd2/history.py b/cmd2/history.py index abd168028..8856bb610 100644 --- a/cmd2/history.py +++ b/cmd2/history.py @@ -318,16 +318,21 @@ def isin(hi: HistoryItem) -> bool: def truncate(self, max_length: int) -> None: """Truncate the length of the history, dropping the oldest items if necessary. - :param max_length: the maximum length of the history, if negative, all history + The current session's start index is shifted so it still marks the first retained + current-session item. If any current-session items are dropped, all retained items + belong to the current session. + + :param max_length: the maximum length of the history, if zero or negative, all history items will be deleted :return: nothing """ if max_length <= 0: - # remove all history - del self[:] + self.clear() elif len(self) > max_length: last_element = len(self) - max_length del self[0:last_element] + # keep the session boundary pointing at the same retained items + self.session_start_index = max(0, self.session_start_index - last_element) def _build_result_dictionary( self, start: int, end: int, filter_func: Callable[[HistoryItem], bool] | None = None diff --git a/tests/test_history.py b/tests/test_history.py index 7ff3fca27..2889bf450 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -399,6 +399,56 @@ def test_history_max_length(hist) -> None: assert hist.get(2).statement.raw == "fourth" +def test_history_truncate_shifts_session_start(hist) -> None: + from cmd2.parsing import Statement + + hist.start_session() + hist.append(Statement("", raw="new-1")) + hist.append(Statement("", raw="new-2")) + + hist.truncate(3) + assert hist.session_start_index == 1 + + expected = ["new-1", "new-2"] + assert [item.raw for item in hist.span(":").values()] == expected + assert [item.raw for item in hist.str_search("new").values()] == expected + assert [item.raw for item in hist.regex_search("/new/").values()] == expected + + +@pytest.mark.parametrize( + ("max_length", "expected"), + [ + # Drops exactly the persisted items + (2, ["new-1", "new-2"]), + # Drops into the current session + (1, ["new-2"]), + ], +) +def test_history_truncate_at_or_into_current_session(hist, max_length, expected) -> None: + from cmd2.parsing import Statement + + hist.start_session() + hist.append(Statement("", raw="new-1")) + hist.append(Statement("", raw="new-2")) + + hist.truncate(max_length) + assert hist.session_start_index == 0 + assert [item.raw for item in hist.span(":").values()] == expected + + +@pytest.mark.parametrize("max_length", [0, -1]) +def test_history_truncate_all_resets_session_start(hist, max_length) -> None: + from cmd2.parsing import Statement + + hist.start_session() + hist.truncate(max_length) + assert hist.session_start_index == 0 + + hist.append(Statement("", raw="new-1")) + assert [item.raw for item in hist.span(":").values()] == ["new-1"] + assert [item.raw for item in hist.str_search("new").values()] == ["new-1"] + + def test_history_to_json(hist) -> None: assert hist_json == hist.to_json() From 3236cf879bef9365a2785d537160a6b48d93e43a Mon Sep 17 00:00:00 2001 From: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:06:14 -0400 Subject: [PATCH 2/2] Address review feedback on History.truncate() fix Assert str_search() and regex_search() in all truncate session-boundary tests, import Statement once at module level, and clarify in the changelog that only direct callers of History.truncate() were affected. --- CHANGELOG.md | 6 ++++-- tests/test_history.py | 10 ++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df6f0d447..b0ac68c3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,8 +28,10 @@ - History regular expressions containing range separators are now handled as regex searches. Other selectors are treated as numbers or ranges only when they match the supported syntax; strings such as URLs, `a:b`, `+1`, and `1_0` remain literal searches. - - Fixed `History.truncate()` leaving the current session's start index unchanged, which hid - retained current-session entries from unbounded ranges and searches. + - Fixed `History.truncate()` leaving the current session's start index unchanged. Code calling + it directly would then have retained current-session entries hidden from unbounded ranges and + searches. Interactive `history` use was unaffected, since `cmd2` only truncates when + persisting history at exit. ## 4.3.1 (October 24, 2026) diff --git a/tests/test_history.py b/tests/test_history.py index 2889bf450..369f95022 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -14,6 +14,7 @@ import cmd2 from cmd2.history import HistoryFormatError, HistoryIndexError +from cmd2.parsing import Statement from .conftest import ( normalize, @@ -400,8 +401,6 @@ def test_history_max_length(hist) -> None: def test_history_truncate_shifts_session_start(hist) -> None: - from cmd2.parsing import Statement - hist.start_session() hist.append(Statement("", raw="new-1")) hist.append(Statement("", raw="new-2")) @@ -425,8 +424,6 @@ def test_history_truncate_shifts_session_start(hist) -> None: ], ) def test_history_truncate_at_or_into_current_session(hist, max_length, expected) -> None: - from cmd2.parsing import Statement - hist.start_session() hist.append(Statement("", raw="new-1")) hist.append(Statement("", raw="new-2")) @@ -434,12 +431,12 @@ def test_history_truncate_at_or_into_current_session(hist, max_length, expected) hist.truncate(max_length) assert hist.session_start_index == 0 assert [item.raw for item in hist.span(":").values()] == expected + assert [item.raw for item in hist.str_search("new").values()] == expected + assert [item.raw for item in hist.regex_search("/new/").values()] == expected @pytest.mark.parametrize("max_length", [0, -1]) def test_history_truncate_all_resets_session_start(hist, max_length) -> None: - from cmd2.parsing import Statement - hist.start_session() hist.truncate(max_length) assert hist.session_start_index == 0 @@ -447,6 +444,7 @@ def test_history_truncate_all_resets_session_start(hist, max_length) -> None: hist.append(Statement("", raw="new-1")) assert [item.raw for item in hist.span(":").values()] == ["new-1"] assert [item.raw for item in hist.str_search("new").values()] == ["new-1"] + assert [item.raw for item in hist.regex_search("/new/").values()] == ["new-1"] def test_history_to_json(hist) -> None: