Skip to content

Extract conversation history into thread-safe class with automatic trimming#21

Merged
hiyouga merged 7 commits intomainfrom
copilot/refactor-conversation-history-management
Jan 1, 2026
Merged

Extract conversation history into thread-safe class with automatic trimming#21
hiyouga merged 7 commits intomainfrom
copilot/refactor-conversation-history-management

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 1, 2026

PrettyGeminiBot's conversation history was managed inline with direct list operations, lacking thread safety and automatic memory management.

Changes

New ConversationHistory class (embedded in mini_ema/bot/pretty_gemini_bot.py)

  • Thread-safe operations via threading.Lock
  • Automatic trimming on add_messages() prevents unbounded memory growth
  • Reads configuration from PRETTY_GEMINI_BOT_HISTORY_LENGTH environment variable
  • Hardcoded to 2 messages per round (user + assistant)
  • Calculates max capacity once during initialization
  • Always trims history (no conditional checks)

Updated PrettyGeminiBot (mini_ema/bot/pretty_gemini_bot.py)

  • Replace direct list with ConversationHistory instance
  • Simplified history management with no parameters needed
  • Simplified message extraction to take last 2 messages directly

Testing Infrastructure

  • Added pytest to project dependencies
  • Added make test command to Makefile using pytest for test execution
  • Created GitHub Actions workflow (.github/workflows/tests.yml) for automated testing
  • Updated Copilot instructions with consolidated testing documentation

Usage

# Before
self.conversation_history = []
recent_history = self.conversation_history[-max_history_messages:]
self.conversation_history.append(msg1)
self.conversation_history.append(msg2)

# After
self.conversation_history = ConversationHistory()
recent_history = self.conversation_history.get_recent_messages()
self.conversation_history.add_messages([msg1, msg2])

History is automatically trimmed to max capacity (max_rounds * 2) on each write, preventing memory leaks in long-running conversations.

Testing

Run make test to execute all tests with pytest. Tests are also automatically run via GitHub Actions on push/PR to main branch.

Original prompt

把 Pretty gemini bot里面的对话历史管理单独抽离出来作为一个类,保证线程安全。同时历史只保留有用的最后N轮,超过的不要保留。只有数组[-index:]时候注意处理index为0的情况


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits January 1, 2026 13:35
Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor conversation history management into a separate class Extract conversation history into thread-safe class with automatic trimming Jan 1, 2026
Copilot AI requested a review from hiyouga January 1, 2026 13:43
…ms, always trim

Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
Copilot AI requested a review from hiyouga January 1, 2026 14:37
Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
Copilot AI requested a review from hiyouga January 1, 2026 14:48
Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
Copilot AI requested a review from hiyouga January 1, 2026 14:55
@hiyouga hiyouga marked this pull request as ready for review January 1, 2026 14:56
Copilot AI review requested due to automatic review settings January 1, 2026 14:56
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors conversation history management in PrettyGeminiBot by extracting it into a dedicated ConversationHistory class with thread-safe operations and automatic memory management. It also establishes a testing infrastructure using pytest with CI/CD integration.

Key Changes:

  • Thread-safe ConversationHistory class with automatic trimming prevents unbounded memory growth
  • Simplified history management in PrettyGeminiBot using the new class interface
  • Comprehensive test suite with pytest and automated CI/CD via GitHub Actions

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
mini_ema/bot/pretty_gemini_bot.py Adds ConversationHistory class with thread-safe operations and refactors PrettyGeminiBot to use it
tests/test_conversation_history.py Comprehensive unit tests for ConversationHistory including thread safety validation
pyproject.toml Adds pytest dependency for testing infrastructure
Makefile Adds make test command and updates help documentation
.github/workflows/tests.yml New GitHub Actions workflow for automated test execution on push/PR
.github/copilot-instructions.md Updates documentation with consolidated testing instructions

@hiyouga hiyouga merged commit eb2dce5 into main Jan 1, 2026
8 checks passed
@hiyouga hiyouga deleted the copilot/refactor-conversation-history-management branch January 1, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants