BUG-HUNT: [error-handling] Missing Argument Validation in LLMTraceRepository Constructor #2988

Open
opened 2026-04-05 03:16:12 +00:00 by freemo · 1 comment
Owner

Metadata

  • Branch: bugfix/m3-llm-trace-repo-arg-validation
  • Commit Message: fix(error-handling): validate session_factory is callable in LLMTraceRepository.__init__
  • Milestone: v3.3.0
  • Parent Epic: #362

Bug Report: [error-handling] — Missing Argument Validation in LLMTraceRepository Constructor

Severity Assessment

  • Impact: A TypeError will be raised when a repository method is called, which is less clear than a ValueError in the constructor. This violates the fail-fast principle.
  • Likelihood: Low. This would only occur if the dependency injection container is misconfigured.
  • Priority: Low

Location

  • File: src/cleveragents/infrastructure/database/llm_trace_repository.py
  • Function/Class: LLMTraceRepository.__init__
  • Lines: 36–43

Description

The __init__ method of LLMTraceRepository checks if session_factory is None, but it does not validate that the provided argument is a callable. This can lead to a TypeError when a repository method is called, which is less informative than a ValueError during object construction.

Evidence

    def __init__(
        self,
        session_factory: Callable[[], Session],
    ) -> None:
        """Initialise with a callable returning a SQLAlchemy Session."""
        if session_factory is None:
            raise ValueError("session_factory must not be None")
        self._sf = session_factory

Expected Behavior

The constructor should validate that session_factory is a callable and raise a TypeError or ValueError if it is not.

Actual Behavior

The constructor accepts non-callable arguments, leading to a TypeError later when the session factory is invoked.

Suggested Fix

Add a callable() check to the constructor:

        if not callable(session_factory):
            raise TypeError("session_factory must be a callable")

Category

error-handling

Subtasks

  • Add callable() guard to LLMTraceRepository.__init__ raising TypeError for non-callable session_factory
  • Update or add unit tests covering the new validation path
  • Verify coverage >= 97% via nox -s coverage_report
  • Run nox (all default sessions), fix any errors

Definition of Done

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass
  • Coverage >= 97%

Automated by CleverAgents Bot
Supervisor: Bug Hunting | Agent: ca-new-issue-creator

## Metadata - **Branch**: `bugfix/m3-llm-trace-repo-arg-validation` - **Commit Message**: `fix(error-handling): validate session_factory is callable in LLMTraceRepository.__init__` - **Milestone**: v3.3.0 - **Parent Epic**: #362 ## Bug Report: [error-handling] — Missing Argument Validation in LLMTraceRepository Constructor ### Severity Assessment - **Impact**: A `TypeError` will be raised when a repository method is called, which is less clear than a `ValueError` in the constructor. This violates the fail-fast principle. - **Likelihood**: Low. This would only occur if the dependency injection container is misconfigured. - **Priority**: Low ### Location - **File**: `src/cleveragents/infrastructure/database/llm_trace_repository.py` - **Function/Class**: `LLMTraceRepository.__init__` - **Lines**: 36–43 ### Description The `__init__` method of `LLMTraceRepository` checks if `session_factory` is `None`, but it does not validate that the provided argument is a callable. This can lead to a `TypeError` when a repository method is called, which is less informative than a `ValueError` during object construction. ### Evidence ```python def __init__( self, session_factory: Callable[[], Session], ) -> None: """Initialise with a callable returning a SQLAlchemy Session.""" if session_factory is None: raise ValueError("session_factory must not be None") self._sf = session_factory ``` ### Expected Behavior The constructor should validate that `session_factory` is a callable and raise a `TypeError` or `ValueError` if it is not. ### Actual Behavior The constructor accepts non-callable arguments, leading to a `TypeError` later when the session factory is invoked. ### Suggested Fix Add a `callable()` check to the constructor: ```python if not callable(session_factory): raise TypeError("session_factory must be a callable") ``` ### Category error-handling ## Subtasks - [ ] Add `callable()` guard to `LLMTraceRepository.__init__` raising `TypeError` for non-callable `session_factory` - [ ] Update or add unit tests covering the new validation path - [ ] Verify coverage >= 97% via `nox -s coverage_report` - [ ] Run `nox` (all default sessions), fix any errors ## Definition of Done - [ ] All subtasks above are completed and checked off. - [ ] A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation. - [ ] The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - [ ] The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - All nox stages pass - Coverage >= 97% --- **Automated by CleverAgents Bot** Supervisor: Bug Hunting | Agent: ca-new-issue-creator
freemo added this to the v3.3.0 milestone 2026-04-05 03:17:02 +00:00
Author
Owner

Issue triaged by project owner:

  • State: Verified
  • Priority: Confirmed
  • MoSCoW: Should Have

Valid finding verified during batch triage.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: ca-project-owner

Issue triaged by project owner: - **State**: Verified - **Priority**: Confirmed - **MoSCoW**: Should Have Valid finding verified during batch triage. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: ca-project-owner
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.

Blocks
#362 Epic: Security & Safety Hardening
cleveragents/cleveragents-core
Reference
cleveragents/cleveragents-core#2988
No description provided.