From 94fe904d3fb63b616b21522bec03545e5c68893d Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 23 Feb 2026 11:03:00 -0500 Subject: [PATCH] feat(ui): Expose history truncation controls in the Discussion panel --- gui.py | 15 +++++++++++++++ tests/test_history_truncation.py | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tests/test_history_truncation.py diff --git a/gui.py b/gui.py index aa01be5..c0af0e4 100644 --- a/gui.py +++ b/gui.py @@ -62,6 +62,15 @@ def get_total_token_usage() -> dict: usage[k] += u.get(k, 0) or 0 return usage +def truncate_entries(entries: list[dict], max_pairs: int) -> list[dict]: + """Truncates history to the last N pairs of User/AI messages.""" + if max_pairs <= 0: + return [] + target_count = max_pairs * 2 + if len(entries) <= target_count: + return entries + return entries[-target_count:] + # ------------------------------------------------------------------ comms rendering helpers @@ -1344,6 +1353,11 @@ class App: self.disc_entries.clear() self._rebuild_disc_list() + def cb_disc_truncate(self): + self.disc_entries = truncate_entries(self.disc_entries, 2) + self._rebuild_disc_list() + self._update_status("history truncated") + def cb_disc_collapse_all(self): for i, entry in enumerate(self.disc_entries): tag = f"disc_content_{i}" @@ -1761,6 +1775,7 @@ class App: dpg.add_button(label="+ Entry", callback=self.cb_disc_append_entry) dpg.add_button(label="-All", callback=self.cb_disc_collapse_all) dpg.add_button(label="+All", callback=self.cb_disc_expand_all) + dpg.add_button(label="Truncate", callback=self.cb_disc_truncate) dpg.add_button(label="Clear All", callback=self.cb_disc_clear) dpg.add_button(label="Save", callback=self.cb_disc_save) dpg.add_checkbox( diff --git a/tests/test_history_truncation.py b/tests/test_history_truncation.py new file mode 100644 index 0000000..418f6e5 --- /dev/null +++ b/tests/test_history_truncation.py @@ -0,0 +1,22 @@ +import pytest + +def test_history_truncation(): + # A dummy test to fulfill the Red Phase for the history truncation controls. + # The new function in gui.py should be cb_disc_truncate_history or a related utility. + from project_manager import str_to_entry, entry_to_str + + entries = [ + {"role": "User", "content": "1", "collapsed": False, "ts": "10:00:00"}, + {"role": "AI", "content": "2", "collapsed": False, "ts": "10:01:00"}, + {"role": "User", "content": "3", "collapsed": False, "ts": "10:02:00"}, + {"role": "AI", "content": "4", "collapsed": False, "ts": "10:03:00"} + ] + + # We expect a new function truncate_entries(entries, max_pairs) to exist + from gui import truncate_entries + + truncated = truncate_entries(entries, max_pairs=1) + # Keeping the last pair (user + ai) + assert len(truncated) == 2 + assert truncated[0]["content"] == "3" + assert truncated[1]["content"] == "4"