Skip to content

fix(python): preserve memory and SQL message timestamps - #726

Open
Elon-Jiang wants to merge 1 commit into
2FastLabs:mainfrom
Elon-Jiang:fix/preserve-message-timestamps
Open

Elon-Jiang wants to merge 1 commit into
2FastLabs:mainfrom
Elon-Jiang:fix/preserve-message-timestamps

Conversation

@Elon-Jiang

Copy link
Copy Markdown

Issue Link

Fixes #725

Summary

Preserve caller-supplied TimestampedMessage timestamps in Python InMemoryChatStorage and SqlChatStorage, for single and batch saves. Since TimestampedMessage inherits ConversationMessage, the existing base-class checks rebuilt already-timestamped messages with the current time. This also caused merged cross-agent history to return in the wrong chronological order.

Changes

  • Copy supplied timestamp values into persisted messages; only generate timestamps for ordinary messages. Keep the existing per-message copy so later timestamp changes to the caller's input do not modify saved in-memory history.
  • Apply that decision per message in the in-memory batch conversion. Keep SQL's existing generated offsets for ordinary batch messages.
  • Add eight regressions covering single/batch chronological ordering, exact persisted values, and ordinary/timestamped inputs in either batch position. Tests use the real in-memory store and temporary SQLite files.
  • Document the two adapters' timestamp behavior in docstrings and the Python usage guide.

Scope and user experience

A newer historical message saved before an older message now appears after the older message in fetch_all_chats, according to their supplied timestamps. Ordinary messages still receive save-time timestamps. Public signatures, trimming, same-role filtering and return values are unchanged.

DynamoDB is deliberately outside this patch: #537 already modifies its save conversions and includes batch timestamp handling. No claim is made that every adapter is covered, and this PR does not replace or duplicate that work. It is independent of the summary-persistence correction in #724.

Validation

Main baseline: 729d5f52869c8280f599a9fa5cf27fc1df445602.

  • Before the fix, the two affected storage test files report 8 failed, 22 passed. Failures check the actual persisted timestamp values; the cases also verify public merged-history ordering.
  • After the fix, those files report 30 passed.
  • Final complete Python make test: 451 passed in 2.32 seconds, Python 3.12.13, using the task-isolated environment installed from the repository's test_requirements.txt. AWS credentials in that process are dummy values and metadata lookup is disabled.
  • Full production-source Ruff check: passed. Including the changed test files additionally reports three existing B017 findings in the SQL test file; the same three are present in unchanged main. They were not changed or suppressed.
  • git diff --check: passed.

No live provider, cloud database or browser calls. Local SQLite is exercised rather than mocked. Existing task-owned test dependencies were reused; no package dependency changes.

Contribution notes

All 87 open PR file lists were checked completely. All-state searches for TimestampedMessage, timestamp, timestamp/storage and timestamps/history, plus both file histories and current main, were inspected. The overlapping DynamoDB work was explicitly excluded. The root/Python contribution guides and actual Apache-2.0 license were read.

AI assistance (OpenAI Codex) was used for investigation, implementation and executed validation. No production incident or independent human review is claimed.

@cornelcroi

Copy link
Copy Markdown
Collaborator

@claude review this PR.

  1. Backward compatibility first: no removed or renamed public symbol, no breaking signature change, no behaviour change for users who don't opt in, optional dependencies stay optional. Any break is a blocker.
  2. Docs: every change needs its docs update, and new features need a usage example.
  3. Tests for the behaviour change, happy path and the edge case.
  4. Python and TypeScript parity: does the same bug or feature exist on the other side?
  5. Then correctness and the rest of the code.

Verdict first. Blockers in a numbered list, then non-blocking notes, kept separate. Plain, short words.

@claude

claude Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Claude finished @cornelcroi's task in 17s —— View job


Verdict: Approve with small notes. No blockers found.

Blockers

None.

1. Backward compatibility: OK

  • No public symbol, signature or return value changes.
  • Plain ConversationMessage input behaves as before (save-time timestamp; SQL batch keeps its base_timestamp + i).
  • Only TimestampedMessage input changes, and it now keeps its own timestamp. That is the documented intent of the type, so I count it as a bug fix.
  • No new dependencies.

2. Docs: OK

  • Docstrings on both classes and a note in python/SKILL.md.
  • No usage example. I think that is fine for a bug fix, but one short snippet would help.

3. Tests: good

  • Single and batch saves, exact stored values, and plain/timestamped mixes in both orders. SQL tests use real SQLite.

4. Python / TypeScript parity

  • TS memoryChatStorage.ts:28 always does { ...newMessage, timestamp: Date.now() }. That overwrites any supplied timestamp, so the same bug exists there.
  • TS sqlChatStorage.ts:75 always uses Date.now(). Same bug.
  • TS dynamoDbChatStorage.ts is covered by feat(storage): add content_filters to DynamoDbChatStorage #537, as the PR says.
  • Please open a follow-up issue for TS memory and SQL, or say in the PR that it is out of scope. Not a blocker.

5. Correctness: non-blocking notes

  • in_memory_chat_storage.py batch: it still checks only new_messages[0] (existing code). A list whose first item is already a TimestampedMessage is extended as is, with no copy. So the "later changes to the caller's input don't modify saved history" claim only holds when the first item is a plain message. Mixed lists starting with a TimestampedMessage are stored by reference, and plain messages in them are never converted. This was already true before the PR, but it contradicts the PR text. Either do the per-message check the PR describes, or soften the text. I would check each message.
  • sql_chat_storage.py batch: the else: timestamped_messages.append(message) branch is now never reached for TimestampedMessage. It is reached only for non-ConversationMessage objects, which is fine.
  • TimestampedMessage(timestamp=0) always becomes "now" (timestamp or int(time.time()*1000)). A caller can't store a literal 0. The else 0 fallback relies on this. It works, but a comment would help.
  • Minor: the single-message in-memory path has if isinstance(new_message, ConversationMessage) with no else. If that is false, timestamped_message is unbound (existing code).
  • Minor: the SQL test file has a newline-at-end fix and existing B017 lint findings, which the PR already disclosed.

This branch has not been deployed

No deployments
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.

Bug: Python memory and SQL storage overwrite supplied message timestamps

2 participants