← Back to PRs

#12358: fix: Sessions format timestamps in sessions_history using userTimezone

by xialonglee open 2026-02-09 05:25 View on GitHub →
agents size: S
# fix: Sessions format timestamps in sessions_history using userTimezone ## Summary fix #12221 ## What Changed - **Modified `sessions-history-tool.ts`**: - Added `formatZonedTimestamp` import from `auto-reply/envelope.js` - Added `resolveUserTimezone` import from `date-time.js` - Updated `sanitizeHistoryMessage` function to accept an optional `userTimezone` parameter - Added logic to format timestamps when `userTimezone` is configured, preserving the original `timestamp` field and adding a new `timestampFormatted` field - Updated the tool execution to resolve user timezone from config and pass it to `sanitizeHistoryMessage` - **Added comprehensive tests**: - Test case for timezone formatting when `userTimezone` is configured (Asia/Shanghai timezone) - Test case to ensure `timestampFormatted` is not added when `userTimezone` is not configured - Both test cases verify that original timestamps are preserved ## Why Previously, timestamps in session history were only provided as raw Unix timestamps, making it difficult for users to quickly understand when messages were sent in their local timezone. This change improves UX by providing human-readable, timezone-aware formatted timestamps while maintaining backward compatibility. ## Testing ✅ All tests pass: - `pnpm test src/agents/openclaw-tools.sessions.test.ts` - 12 tests passed - Tests cover both scenarios: with and without timezone configuration - Tests verify timezone conversion accuracy (UTC to Asia/Shanghai) ## Files Changed - `src/agents/tools/sessions-history-tool.ts` (+23 lines, -9 lines) - `src/agents/openclaw-tools.sessions.test.ts` (+121 lines, -9 lines) ## Checklist - [x] Test locally with your OpenClaw instance - [x] Run tests: `pnpm test src/agents/openclaw-tools.sessions.test.ts` ✅ - [x] Keep PRs focused (one thing per PR) ✅ - [x] Describe what & why ✅ <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR updates the `sessions_history` tool to optionally add a human-readable `timestampFormatted` field to each history message when `agents.defaults.userTimezone` is explicitly configured. It does this by threading an optional `userTimezone` into `sanitizeHistoryMessage` and using the shared `formatZonedTimestamp` helper. The accompanying tests extend the sessions tools test suite by mocking config (now safely via `structuredClone` to avoid cross-test mutation) and asserting both behaviors: formatted timestamps when a timezone is configured, and no `timestampFormatted` field when it is not. <h3>Confidence Score: 5/5</h3> - This PR is safe to merge with minimal risk. - Changes are small and well-scoped: timestamp formatting is gated on an explicitly configured timezone and uses existing shared formatting utilities. Tests cover both configured and unconfigured scenarios, and the config mock was adjusted to avoid shared mutable state across tests. - No files require special attention <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs