Skip to content

Preserve entry timestamps on compaction and branch-summary replay messages - #764

Open
baixinghao wants to merge 1 commit into
huggingface:mainfrom
baixinghao:fix/summary-message-timestamps
Open

baixinghao wants to merge 1 commit into
huggingface:mainfrom
baixinghao:fix/summary-message-timestamps

Conversation

@baixinghao

Copy link
Copy Markdown

Summary

Fixes #747.

SessionState.from_entries builds a synthetic UserMessage for compaction and branch_summary entries without passing timestamp=, so UserMessage's default_factory=current_timestamp_ms fills in wall-clock "now" at replay time instead of the entry's historical entry.timestamp.

This breaks _last_applicable_provider_usage (src/tau_coding/context_window.py), which anchors token accounting on the latest assistant response's provider-reported usage but rejects any usage older than a later message. Because the replayed summary message's timestamp is always "now", every assistant usage after a compaction fails the ordering check and context accounting falls back to the crude 4-chars-per-token estimate for the rest of a compacted-and-reloaded session.

Fix

Pass timestamp=round(entry.timestamp * 1000) at both sites, mirroring the custom_message fix from #710:

  • _apply_compaction's summary_row
  • the branch_summary case in SessionState.from_entries

Verification

Reproduced the issue's script on this branch:

  • Before: summary UserMessage timestamp= 1791756578906 (wall clock), estimate_context_usage returned uses_provider_usage=False, provider_tokens=0.
  • After: UserMessage timestamp= 1700000010000 (entry time), uses_provider_usage=True, provider_tokens=999.

Added test_compaction_and_branch_summary_messages_keep_entry_timestamps covering both the compaction summary row and the branch-summary row.

uv run pytest tests/test_session.py — 42 passed. uv run ruff check / ruff format --check / mypy on the touched files — clean.

Note: this Windows environment has ~40 pre-existing test failures on a clean checkout (TUI layout, updater, file-drop path handling, etc.). I diffed the failure list before/after this change: identical, so the change introduces no regressions.

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.

Compaction/branch-summary UserMessage rows get a wall-clock timestamp, breaking provider-usage anchoring

1 participant