Preserve original message timestamps through fork/compress/branch#28840
Open
yitang wants to merge 2 commits into
Open
Preserve original message timestamps through fork/compress/branch#28840yitang wants to merge 2 commits into
yitang wants to merge 2 commits into
Conversation
messages.timestamp is always time.time() at INSERT time. When messages are
copied between sessions during /branch, compression, or /retry, their
original timestamps are silently discarded.
This change adds an optional timestamp parameter to append_message() and
reads msg.get('timestamp') in replace_messages(), then forwards it from
every caller that has the field available.
All changes are backward compatible: callers that do not pass timestamp
get identical time.time() behaviour.
6fed2bd to
dbcdc01
Compare
… stronger fork test
This was referenced May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Preserve original message timestamps through fork/compress/branch
Summary
messages.timestampis alwaystime.time()at INSERT time. When messages arecopied between sessions during
/branch, compression, or/retry, theiroriginal timestamps are silently discarded. This change adds an optional
timestampparameter toappend_message()and readsmsg.get("timestamp")in
replace_messages(), then forwards it from every caller that has the fieldavailable.
All changes are backward compatible: callers that don't pass
timestampgetidentical
time.time()behaviour.Changes
hermes_state.pyappend_message(): added optionaltimestamp: float = Noneparam; uses it instead oftime.time()when set.replace_messages(): readsmsg.get("timestamp")per message, falls back totime.time()run_agent.pytimestamp=msg.get("timestamp")in_flush_messages_to_session_db()gateway/run.pytimestamp=msg.get("timestamp")in/branchhandlercli.pytimestamp=msg.get("timestamp")in branch handlertui_gateway/server.pytimestamp=msg.get("timestamp")in branch handlergateway/session.pytimestamp=message.get("timestamp")inappend_to_transcript()gateway/mirror.pytimestamp=message.get("timestamp")in_append_to_sqlite()tests/test_hermes_state.pyTestTimestampPreservationwith 8 testsAll 8 files: each caller forwards
msg.get("timestamp")toappend_message()or
replace_messages(). The core fix inhermes_state.py(accept + store thesupplied timestamp) makes every caller work correctly — both the ones modified here
and existing ones using
replace_messages()with timestamp-bearing dicts.Backward compatibility
timestamp=None(default) →time.time()as before.timestampis keyword-onlywith default.
replace_messages()falls back tobase_ts = time.time()when a messagedict has no
timestampkey.Testing
8 new tests in
tests/test_hermes_state.py::TestTimestampPreservation:test_append_message_with_explicit_timestamptest_append_message_multiple_timestampstest_append_message_without_timestamp_defaultstime.time()fallbacktest_append_message_mixed_timestampstest_replace_messages_preserves_timestampsreplace_messagestest_replace_messages_fallback_when_no_timestamptest_replace_messages_mixed_timestampstest_fork_chain_preserves_timestamps/branchcopy preserves original timestampsImplementation notes
The root cause was narrow:
append_message()never accepted a caller-supplied timestamp → 1 param + 1 conditionalreplace_messages()never read timestamps from dicts → 3 lines changedThe 6 callers (branch handlers, mirror, flush, transcript) all needed a 1-line
forwarding change. Existing callers using
replace_messages()withtimestamp-bearing dicts (
rewrite_transcript, ACP persist) worked automaticallyonce the core fix was in place.