Session.append_message() uses naive datetime.now() without timezone — inconsistent with UTC-aware codebase #8421

Open
opened 2026-04-13 18:45:00 +00:00 by HAL9000 · 1 comment
Owner

Metadata

  • Commit: Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.
  • Branch: main
  • SHA: 5a9aaa79edaefb1a257114f054ea87facb8efe69
  • File: src/cleveragents/domain/models/core/session.py

Background and Context

The Session.append_message() method creates SessionMessage objects using datetime.now() (naive, no timezone). The Session.updated_at field is also set using datetime.now(). Naive datetimes (without timezone info) are inconsistent with the rest of the codebase — for example, A2aEvent uses datetime.now(tz=UTC) for its timestamp. Mixing naive and timezone-aware datetimes causes TypeError when comparing them, and can produce incorrect ordering when sessions span timezone boundaries.

Current Behavior

In session.py, Session.append_message():

def append_message(self, ...) -> SessionMessage:
    ...
    message = SessionMessage(
        ...
        timestamp=datetime.now(),   # ← naive datetime, no timezone
        ...
    )
    self.messages.append(message)
    self.updated_at = datetime.now()  # ← naive datetime, no timezone
    return message

Additionally, the SessionMessage.timestamp and Session.created_at/updated_at fields use default_factory=datetime.now (naive). The Session model config has validate_assignment=True, so assigning a timezone-aware datetime to updated_at would work, but the default factory produces naive datetimes.

Expected Behavior

All datetime values in the session domain model should be UTC-aware, consistent with A2aEvent and the broader codebase convention:

from datetime import UTC, datetime

message = SessionMessage(
    ...
    timestamp=datetime.now(tz=UTC),  # ← UTC-aware
    ...
)
self.updated_at = datetime.now(tz=UTC)  # ← UTC-aware

Field defaults should also use UTC:

timestamp: datetime = Field(default_factory=lambda: datetime.now(tz=UTC))
created_at: datetime = Field(default_factory=lambda: datetime.now(tz=UTC))
updated_at: datetime = Field(default_factory=lambda: datetime.now(tz=UTC))

Acceptance Criteria

  • Session.append_message() uses datetime.now(tz=UTC) for message timestamp and updated_at
  • SessionMessage.timestamp default factory produces UTC-aware datetimes
  • Session.created_at and Session.updated_at default factories produce UTC-aware datetimes
  • No TypeError when comparing session timestamps with A2A event timestamps
  • BDD test: append_message() produces messages with timezone-aware timestamps
  • BDD test: Session.created_at is timezone-aware on construction

Subtasks

  • Update Session.append_message() to use datetime.now(tz=UTC)
  • Update SessionMessage.timestamp field default_factory to lambda: datetime.now(tz=UTC)
  • Update Session.created_at and Session.updated_at field default_factory to lambda: datetime.now(tz=UTC)
  • Verify from datetime import UTC is imported in session.py
  • Add BDD scenarios for timezone-aware session timestamps

Definition of Done

This issue is closed when all datetime values in the session domain model are UTC-aware, consistent with the rest of the codebase, and BDD tests verify timezone correctness.


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

## Metadata - **Commit**: `Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.` - **Branch**: `main` - **SHA**: `5a9aaa79edaefb1a257114f054ea87facb8efe69` - **File**: `src/cleveragents/domain/models/core/session.py` ## Background and Context The `Session.append_message()` method creates `SessionMessage` objects using `datetime.now()` (naive, no timezone). The `Session.updated_at` field is also set using `datetime.now()`. Naive datetimes (without timezone info) are inconsistent with the rest of the codebase — for example, `A2aEvent` uses `datetime.now(tz=UTC)` for its timestamp. Mixing naive and timezone-aware datetimes causes `TypeError` when comparing them, and can produce incorrect ordering when sessions span timezone boundaries. ## Current Behavior In `session.py`, `Session.append_message()`: ```python def append_message(self, ...) -> SessionMessage: ... message = SessionMessage( ... timestamp=datetime.now(), # ← naive datetime, no timezone ... ) self.messages.append(message) self.updated_at = datetime.now() # ← naive datetime, no timezone return message ``` Additionally, the `SessionMessage.timestamp` and `Session.created_at`/`updated_at` fields use `default_factory=datetime.now` (naive). The `Session` model config has `validate_assignment=True`, so assigning a timezone-aware datetime to `updated_at` would work, but the default factory produces naive datetimes. ## Expected Behavior All datetime values in the session domain model should be UTC-aware, consistent with `A2aEvent` and the broader codebase convention: ```python from datetime import UTC, datetime message = SessionMessage( ... timestamp=datetime.now(tz=UTC), # ← UTC-aware ... ) self.updated_at = datetime.now(tz=UTC) # ← UTC-aware ``` Field defaults should also use UTC: ```python timestamp: datetime = Field(default_factory=lambda: datetime.now(tz=UTC)) created_at: datetime = Field(default_factory=lambda: datetime.now(tz=UTC)) updated_at: datetime = Field(default_factory=lambda: datetime.now(tz=UTC)) ``` ## Acceptance Criteria - [ ] `Session.append_message()` uses `datetime.now(tz=UTC)` for message timestamp and `updated_at` - [ ] `SessionMessage.timestamp` default factory produces UTC-aware datetimes - [ ] `Session.created_at` and `Session.updated_at` default factories produce UTC-aware datetimes - [ ] No `TypeError` when comparing session timestamps with A2A event timestamps - [ ] BDD test: `append_message()` produces messages with timezone-aware timestamps - [ ] BDD test: `Session.created_at` is timezone-aware on construction ## Subtasks - [ ] Update `Session.append_message()` to use `datetime.now(tz=UTC)` - [ ] Update `SessionMessage.timestamp` field `default_factory` to `lambda: datetime.now(tz=UTC)` - [ ] Update `Session.created_at` and `Session.updated_at` field `default_factory` to `lambda: datetime.now(tz=UTC)` - [ ] Verify `from datetime import UTC` is imported in `session.py` - [ ] Add BDD scenarios for timezone-aware session timestamps ## Definition of Done This issue is closed when all datetime values in the session domain model are UTC-aware, consistent with the rest of the codebase, and BDD tests verify timezone correctness. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor
HAL9000 added this to the v3.6.0 milestone 2026-04-13 18:51:08 +00:00
HAL9000 modified the milestone from v3.6.0 to v3.3.0 2026-04-13 19:17:49 +00:00
Author
Owner

Verified — Naive datetime in Session.append_message() is a real but low-severity bug. UTC consistency matters for correctness. MoSCoW: Could Have — fix when convenient, assign to v3.3.0 as it touches session layer. [AUTO-OWNR-1]


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

✅ **Verified** — Naive datetime in Session.append_message() is a real but low-severity bug. UTC consistency matters for correctness. **MoSCoW: Could Have** — fix when convenient, assign to v3.3.0 as it touches session layer. [AUTO-OWNR-1] --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
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#8421
No description provided.