Skip to content

fix(agents): emit chat event timestamps in milliseconds (#9867)#10243

Open
aniruddh909 wants to merge 1 commit into
mudler:masterfrom
aniruddh909:fix/agent-chat-timestamp-ms
Open

fix(agents): emit chat event timestamps in milliseconds (#9867)#10243
aniruddh909 wants to merge 1 commit into
mudler:masterfrom
aniruddh909:fix/agent-chat-timestamp-ms

Conversation

@aniruddh909

Copy link
Copy Markdown

What

Fixes the broken agent chat timestamps reported in #9867 — replies in the web UI render as "Invalid Timestamp", "12:00 AM", or the same time for every message.

Root cause

The agent chat SSE json_message event is produced by two paths that disagreed on the timestamp unit, and the React UI assumed a third interpretation:

Source Unit
core/services/agents/dispatcher.go (local dispatcher) UnixMilli()
core/services/agents/events.go PublishEvent (NATS EventBridge) UnixNano()
core/http/react-ui/src/pages/AgentChat.jsx value / 1e6 (i.e. assumed nanoseconds)

In the common single-node case the local dispatcher sends milliseconds, the UI divides by 1e6, and the result collapses to a near-epoch date → "12:00 AM" / "Invalid Timestamp". Nanoseconds are also the wrong thing to send over JSON, since UnixNano() (~1.7e18) exceeds JavaScript's Number.MAX_SAFE_INTEGER.

Change

Standardize on Unix milliseconds — the JS-native unit for new Date(ts) and within the safe-integer range:

  • events.go: PublishEvent now uses time.Now().UnixMilli() (matching the local dispatcher), with a comment documenting the unit on both the call site and the AgentEvent.Timestamp field.
  • AgentChat.jsx: consume data.timestamp directly instead of dividing by 1e6.
  • events_test.go: new regression test asserting the published timestamp falls within a millisecond [before, after] window (it fails against the old UnixNano behavior).

Testing

  • go test ./core/services/agents/ passes (40 specs); the new test fails when reverted to UnixNano and passes with UnixMilli, confirming it guards the regression.
  • The frontend change is a one-line unit fix verified by inspection; I did not run the Playwright e2e suite locally.

Scope note

#9867 also mentions chat history not being included in context. That is by design — conversation history is stored client-side (browser localStorage), as documented in events.go. This PR addresses only the timestamp defect.

Agent chat replies rendered a broken timestamp in the web UI
("Invalid Timestamp" / "12:00 AM", identical for every reply) because
the SSE timestamp unit was inconsistent across producers.

EventBridge.PublishEvent emitted Unix nanoseconds while the local
dispatcher (dispatcher.go) already emitted Unix milliseconds, and the
React UI fed the value straight into `new Date(ts)` after dividing by
1e6. Nanoseconds also overflow JS's safe-integer range (~1.7e18).

Standardize on Unix milliseconds: switch PublishEvent to UnixMilli and
drop the /1e6 conversion in AgentChat.jsx so both SSE paths agree and
match the React UI's expectation. Add a regression test asserting the
published timestamp is in milliseconds.
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.

1 participant