Skip to content

Fix History.truncate() leaving session start index stale - #1787

Merged
tleonhardt merged 2 commits into
mainfrom
fix-history-truncate-session-start
Oct 29, 2026
Merged

tleonhardt merged 2 commits into
mainfrom
fix-history-truncate-session-start

Conversation

@tleonhardt

Copy link
Copy Markdown
Member

Closes #1785

Problem

History.truncate() dropped the oldest items without adjusting session_start_index. Retained current-session
entries were then hidden from unbounded ranges (span(":")), str_search(), and regex_search().

Fix

  • When dropping the oldest items, shift session_start_index back by the number removed, clamped at 0. If the cut
    reaches into the current session, every retained item counts as current-session.
  • When max_length <= 0, call clear(), which already resets the session boundary.
  • Updated the docstring: it now says zero also deletes everything, and describes how the session boundary shifts.

Tests

  • Truncating only persisted items: the boundary shifts, and span and both searches return the session items (the
    issue's reproduction).
  • Truncating exactly at the session boundary, and into the current session.
  • Truncating everything (0 and -1): the boundary resets to 0, and items appended afterward are visible.

make check and make test pass locally.

Note

In cmd2 itself, truncate() is only called from _persist_history at exit, so this mainly affects code that calls
History.truncate() directly.

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
@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.75%. Comparing base (1e68913) to head (3236cf8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1787   +/-   ##
=======================================
  Coverage   99.75%   99.75%           
=======================================
  Files          23       23           
  Lines        6502     6503    +1     
=======================================
+ Hits         6486     6487    +1     
  Misses         16       16           
Flag Coverage Δ
unittests 99.75% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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.
@tleonhardt
tleonhardt merged commit 57c3a66 into main Oct 29, 2026
32 checks passed
@tleonhardt
tleonhardt deleted the fix-history-truncate-session-start branch October 29, 2026 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

History.truncate() hides retained current-session entries

1 participant