database/error_pattern_repository: add failing test for in-memory-only storage data loss on restart #10332

Open
opened 2026-04-18 08:48:31 +00:00 by HAL9000 · 0 comments
Owner

Metadata

  • Commit message: test(database): add failing TDD test for ErrorPatternRepository in-memory data loss on restart
  • Branch name: test/error-pattern-repository-persistence-tdd

Background and Context

ErrorPatternRepository stores all error patterns in a plain Python dict (self._patterns) with no database persistence. Unlike LLMTraceRepository, CheckpointRepository, and other repositories in the codebase that use SQLAlchemy sessions and proper database-backed storage, ErrorPatternRepository is entirely in-memory. This means all error patterns are lost on every process restart, which is a silent data-loss bug.

A failing test should be written to document this data-loss behaviour before the fix is implemented, following the TDD approach.

Expected Behavior

Error patterns should survive process restarts. The repository should persist data to the SQLite database (similar to how LLMTraceRepository, CheckpointRepository, etc. work). A new ErrorPatternRepository instance should be able to retrieve patterns saved by a previous instance.

Acceptance Criteria

  • A failing test file tests/infrastructure/database/test_error_pattern_repository_persistence.py is created
  • The test is marked with @pytest.mark.tdd_issue, @pytest.mark.tdd_issue_1, and @pytest.mark.tdd_expected_fail
  • The test creates a pattern via repo1, instantiates a fresh repo2, and asserts repo2.get(pattern_id) is not None
  • The test currently fails because ErrorPatternRepository uses an in-memory dict with no DB backing
  • CI confirms the test fails for the right reason (in-memory dict, not DB)
  • The test is committed and the failure is documented

Subtasks

  • Create tests/infrastructure/database/test_error_pattern_repository_persistence.py with the TDD test
  • Verify the test fails with the expected assertion error (not an import error or unrelated failure)
  • Add tdd_issue and tdd_expected_fail markers to pytest.ini / pyproject.toml if not already present
  • Commit the failing test with the specified commit message

Test Specification

# tests/infrastructure/database/test_error_pattern_repository_persistence.py

import pytest
from cleveragents.infrastructure.database.error_pattern_repository import ErrorPatternRepository
from cleveragents.domain.models.core.error_pattern import ErrorPattern
from datetime import datetime

@pytest.mark.tdd_issue
@pytest.mark.tdd_issue_1
@pytest.mark.tdd_expected_fail
def test_error_pattern_repository_persists_across_instances():
    """ErrorPatternRepository must persist patterns to the database.

    Currently FAILS because ErrorPatternRepository uses an in-memory dict
    (self._patterns) with no database backing.  Creating a second instance
    returns an empty store even though patterns were saved to the first.
    """
    pattern = ErrorPattern(
        pattern_id="01HXYZ1234567890ABCDEFGHIJ",
        pattern="ImportError",
        keywords=["import", "module"],
        frequency=1,
        last_seen=datetime.utcnow(),
    )

    repo1 = ErrorPatternRepository()
    repo1.create(pattern)

    # Simulate restart by creating a new instance
    repo2 = ErrorPatternRepository()
    result = repo2.get(pattern.pattern_id)

    # This assertion FAILS because repo2 has an empty in-memory dict
    assert result is not None, (
        "ErrorPatternRepository must persist patterns to the database; "
        "a new instance should be able to retrieve previously saved patterns."
    )

Evidence

src/cleveragents/infrastructure/database/error_pattern_repository.py:

class ErrorPatternRepository:
    """In-memory CRUD + pattern-matching repository for error patterns."""

    def __init__(self) -> None:
        self._patterns: dict[str, ErrorPattern] = {}   # ← pure in-memory, no DB

All CRUD methods (create, get, update, delete, list_all, match_context) operate exclusively on self._patterns. There is no SQLAlchemy session, no model class, and no migration for an error_patterns table.

Definition of Done

  • Failing test written and committed
  • Test is marked @pytest.mark.tdd_expected_fail
  • CI confirms the test fails for the right reason (in-memory dict, not DB)

Automated by CleverAgents Bot
Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor


Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata - **Commit message:** `test(database): add failing TDD test for ErrorPatternRepository in-memory data loss on restart` - **Branch name:** `test/error-pattern-repository-persistence-tdd` ## Background and Context `ErrorPatternRepository` stores all error patterns in a plain Python dict (`self._patterns`) with no database persistence. Unlike `LLMTraceRepository`, `CheckpointRepository`, and other repositories in the codebase that use SQLAlchemy sessions and proper database-backed storage, `ErrorPatternRepository` is entirely in-memory. This means all error patterns are lost on every process restart, which is a silent data-loss bug. A failing test should be written to document this data-loss behaviour before the fix is implemented, following the TDD approach. ## Expected Behavior Error patterns should survive process restarts. The repository should persist data to the SQLite database (similar to how `LLMTraceRepository`, `CheckpointRepository`, etc. work). A new `ErrorPatternRepository` instance should be able to retrieve patterns saved by a previous instance. ## Acceptance Criteria - [ ] A failing test file `tests/infrastructure/database/test_error_pattern_repository_persistence.py` is created - [ ] The test is marked with `@pytest.mark.tdd_issue`, `@pytest.mark.tdd_issue_1`, and `@pytest.mark.tdd_expected_fail` - [ ] The test creates a pattern via `repo1`, instantiates a fresh `repo2`, and asserts `repo2.get(pattern_id)` is not `None` - [ ] The test currently **fails** because `ErrorPatternRepository` uses an in-memory dict with no DB backing - [ ] CI confirms the test fails for the right reason (in-memory dict, not DB) - [ ] The test is committed and the failure is documented ## Subtasks - [ ] Create `tests/infrastructure/database/test_error_pattern_repository_persistence.py` with the TDD test - [ ] Verify the test fails with the expected assertion error (not an import error or unrelated failure) - [ ] Add `tdd_issue` and `tdd_expected_fail` markers to `pytest.ini` / `pyproject.toml` if not already present - [ ] Commit the failing test with the specified commit message ## Test Specification ```python # tests/infrastructure/database/test_error_pattern_repository_persistence.py import pytest from cleveragents.infrastructure.database.error_pattern_repository import ErrorPatternRepository from cleveragents.domain.models.core.error_pattern import ErrorPattern from datetime import datetime @pytest.mark.tdd_issue @pytest.mark.tdd_issue_1 @pytest.mark.tdd_expected_fail def test_error_pattern_repository_persists_across_instances(): """ErrorPatternRepository must persist patterns to the database. Currently FAILS because ErrorPatternRepository uses an in-memory dict (self._patterns) with no database backing. Creating a second instance returns an empty store even though patterns were saved to the first. """ pattern = ErrorPattern( pattern_id="01HXYZ1234567890ABCDEFGHIJ", pattern="ImportError", keywords=["import", "module"], frequency=1, last_seen=datetime.utcnow(), ) repo1 = ErrorPatternRepository() repo1.create(pattern) # Simulate restart by creating a new instance repo2 = ErrorPatternRepository() result = repo2.get(pattern.pattern_id) # This assertion FAILS because repo2 has an empty in-memory dict assert result is not None, ( "ErrorPatternRepository must persist patterns to the database; " "a new instance should be able to retrieve previously saved patterns." ) ``` ## Evidence `src/cleveragents/infrastructure/database/error_pattern_repository.py`: ```python class ErrorPatternRepository: """In-memory CRUD + pattern-matching repository for error patterns.""" def __init__(self) -> None: self._patterns: dict[str, ErrorPattern] = {} # ← pure in-memory, no DB ``` All CRUD methods (`create`, `get`, `update`, `delete`, `list_all`, `match_context`) operate exclusively on `self._patterns`. There is no SQLAlchemy session, no model class, and no migration for an `error_patterns` table. ## Definition of Done - [ ] Failing test written and committed - [ ] Test is marked `@pytest.mark.tdd_expected_fail` - [ ] CI confirms the test fails for the right reason (in-memory dict, not DB) --- **Automated by CleverAgents Bot** Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10332
No description provided.