Skip to content

History list mutations other than truncate() leave session_start_index stale #1788

Closed

Description

@tleonhardt

Follow-up from #1787 (fix for #1785).

History subclasses list and stores the current session boundary as a positional index, session_start_index. PR #1787 makes History.truncate() shift that index when it drops old items. Every other way of removing or inserting earlier items still leaves it pointing at the wrong place, including del hist[i], del hist[a:b], pop(), remove(), insert(), and slice assignment.

from cmd2.history import History, HistoryItem
from cmd2.parsing import Statement

hist = History([HistoryItem(Statement("", raw=f"old-{i}")) for i in range(1, 5)])
hist.start_session()  # session_start_index == 4
hist.append(Statement("", raw="new-1"))
hist.append(Statement("", raw="new-2"))

del hist[:2]
print(hist.session_start_index)                         # 4, should be 2
print([e.raw for e in hist.span(":").values()])         # [], expected ['new-1', 'new-2']

Unbounded ranges, str_search(), regex_search(), and the --all check in do_history all read the stale boundary.

Possible approaches:

  • Override the mutating list methods to adjust session_start_index.
  • Store the boundary in a form that doesn't depend on position, such as a count measured from the end, a reference to the first current-session HistoryItem, or a per-item "current session" flag.

The second approach is more robust, but session_start_index is a public attribute that tests and apps set directly, so compatibility needs some thought.

Activity

  1. mayankdhingra02 commented on Oct 30, 2026

    @mayankdhingra02
    Contributor

    I’d like to work on this. I’m checking how removals and insertions affect the session boundary and preparing regression tests for the list mutation methods and slice assignment, while preserving the public session_start_index interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions