Skip to content

fix(ui): order live chat items by timestamp - #81016

Merged
steipete merged 6 commits into
openclaw:mainfrom
akrimm702:codex/chat-live-message-order
May 13, 2026
Merged

steipete merged 6 commits into
openclaw:mainfrom
akrimm702:codex/chat-live-message-order

Conversation

@akrimm702

@akrimm702 akrimm702 commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Fixes [Bug]: Messages not in correct order in telegram and OpenClaw Control #80759 by rendering Control chat items in visible timestamp order instead of always appending live stream/tool items after history.
  • Keeps the hidden history notice pinned before the rendered history window and leaves active reading indicators at the tail.
  • Adds regression coverage for older live tool messages and completed stream segments appearing before newer history messages.

Root Cause

buildChatItems() built all history messages first, then appended completed stream segments and live tool messages. When a live tool/stream item had an older timestamp than a later history message, the UI could show a newer assistant group above an older tool/stream group, matching the reporter screenshot.

Fix

After collecting renderable history, stream, tool, divider, and reading items, the UI now performs a stable visible-time sort before duplicate collapse and grouping. Items with the same timestamp keep their original relative order; the history-limit notice remains first; reading indicators remain last. Untimestamped history fallbacks now stay in their original relative order instead of receiving a synthetic current timestamp for sorting.

Real behavior proof

  • Behavior addressed: Control chat visible ordering for mixed history, completed stream, and live tool items; older timestamped live items must render before newer history, while untimestamped history remains a fallback after timestamped items.
  • Real setup tested: Local OpenClaw checkout on this PR branch, running the real buildChatItems() Control UI builder used by ui/src/ui/views/chat.ts.
  • Exact steps or command run after the patch:
node --import tsx - <<'EOF_NODE'
import { buildChatItems } from './ui/src/ui/chat/build-chat-items.ts';

const items = buildChatItems({
  sessionKey: 'proof',
  messages: [
    { role: 'assistant', content: [{ type: 'text', text: 'Untimestamped history fallback.' }] },
    { role: 'assistant', content: [{ type: 'text', text: 'Newer history reply.' }], timestamp: 2000 },
  ],
  toolMessages: [{ role: 'tool', content: 'Older live tool output.', timestamp: 1000 }],
  streamSegments: [{ text: 'Older streamed output.', ts: 1500 }],
  stream: null,
  streamStartedAt: null,
  showToolCalls: true,
});

console.log(JSON.stringify(items.map((item) => {
  if (item.kind === 'group') {
    return {
      kind: item.kind,
      role: item.role,
      timestamp: item.timestamp,
      messages: item.messages.map((entry) => entry.message.content),
    };
  }
  return { kind: item.kind, timestamp: item.startedAt, text: item.text };
}), null, 2));
EOF_NODE
  • Evidence after fix: Terminal output from the command above:
[
  {
    "kind": "group",
    "role": "tool",
    "timestamp": 1000,
    "messages": [
      "Older live tool output."
    ]
  },
  {
    "kind": "stream",
    "timestamp": 1500,
    "text": "Older streamed output."
  },
  {
    "kind": "group",
    "role": "assistant",
    "timestamp": 2000,
    "messages": [
      [
        {
          "type": "text",
          "text": "Newer history reply."
        }
      ],
      [
        {
          "type": "text",
          "text": "Untimestamped history fallback."
        }
      ]
    ]
  }
]
  • Observed result after fix: The actual builder output orders the older live tool item at 1000, then the completed stream at 1500, then the assistant history group at 2000; the untimestamped assistant fallback remains after the timestamped assistant message inside that group instead of sorting as a current-time item ahead of older live output.
  • Not tested: None

Regression Test Plan

  • Added orders live tool messages before newer history messages.
  • Added orders completed stream segments before newer history messages.
  • Added orders timestamped chat items before history messages without timestamps.
  • Adjusted the streamed canvas test to find the assistant group by role so canvas lifting stays asserted even when the older tool item sorts before the assistant group.

Local Validation

  • pnpm exec oxfmt --check --threads=1 CHANGELOG.md ui/src/ui/chat/build-chat-items.ts ui/src/ui/chat/build-chat-items.test.ts
  • pnpm test ui/src/ui/chat/build-chat-items.test.ts -- --reporter=verbose (20/20)
  • pnpm check:changed

Risk

Low-to-medium UI ordering risk: this intentionally changes visible ordering across history, completed streams, and tool cards. The sort is stable, keeps existing canvas lifting behavior, and has focused regression coverage for the reported mixed history/live ordering bug.

Fixes #80759

@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 12, 2026
@clawsweeper

clawsweeper Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

Summary
The branch adds stable visible-time ordering to Control chat items, regression coverage, and a changelog entry, with small maintainer refresh cleanup in gateway/plugin type-check surfaces.

Reproducibility: yes. by source inspection: current main appends stream/tool items after history, so an older live item can render below newer history. The PR body also gives after-fix builder output for the same mixed history/live case.

Real behavior proof
Sufficient (live_output): The PR body and follow-up comment include copied after-fix live output from the real Control buildChatItems path showing the corrected order.

Next step before merge
No ClawSweeper repair is appropriate: there is no narrow code finding to fix, but current-head check failures and merge state need maintainer CI or branch-refresh handling before merge.

Security
Cleared: No concrete security or supply-chain concern found; the diff does not change dependencies, workflows, secrets, permissions, package resolution, or downloaded code execution.

Review details

Best possible solution:

Land the centralized buildChatItems timestamp-ordering fix after branch and CI refresh, then track any remaining Telegram ordering problem separately if it still reproduces.

Do we have a high-confidence way to reproduce the issue?

Yes, by source inspection: current main appends stream/tool items after history, so an older live item can render below newer history. The PR body also gives after-fix builder output for the same mixed history/live case.

Is this the best way to solve the issue?

Yes. Sorting collected builder items by explicit visible timestamp before collapse/grouping is the narrow central fix, and the patch preserves the hidden-history notice, reading indicators, and canvas lifting behavior.

What I checked:

  • Current-main root cause: Current main still builds history first, then appends completed stream segments and live tool messages, and returns grouped/collapsed items without interleaving by timestamp. (ui/src/ui/chat/build-chat-items.ts:350, b55d9fa4660b)
  • Render path uses builder order: renderChat calls buildChatItems and then renders the returned chatItems in order, so builder ordering directly controls visible Control chat ordering. (ui/src/ui/views/chat.ts:952, 433bafa55bc7)
  • Live timestamps are real inputs: Live tool messages are built with timestamp: entry.startedAt, and completed stream segments carry their own ts value, matching the PR's visible-time ordering inputs. (ui/src/ui/app-tool-stream.ts:222, 433bafa55bc7)
  • PR implementation targets central builder: The PR adds rawMessageTimestamp/chatItemTimestamp/sortChatItemsByVisibleTime and applies the sort before duplicate collapse and grouping, while preserving the hidden-history notice and reading-indicator tail behavior. (ui/src/ui/chat/build-chat-items.ts:262, 5739c0b809a4)
  • Regression coverage matches the bug shape: The PR adds tests for older live tool messages, older completed stream segments, and timestamped stream items rendering before newer or untimestamped history fallbacks, plus adjusts the canvas-lifting assertion after ordering changes. (ui/src/ui/chat/build-chat-items.test.ts:275, 5739c0b809a4)
  • Real behavior proof supplied: The PR body and follow-up comment include after-fix live output from the real buildChatItems path showing a tool item at 1000, stream at 1500, and newer assistant history at 2000 in that order. (5739c0b809a4)

Likely related people:

  • steipete: Current checkout blame points the builder, render call, and tool-stream timestamp path to Peter Steinberger, and the live PR branch includes his timestamp-stabilization and lint-refresh commits. (role: recent UI chat contributor and PR branch maintainer; confidence: high; commits: 2e983e47dfdd, 8c8b8a6fe448, 5739c0b809a4; files: ui/src/ui/chat/build-chat-items.ts, ui/src/ui/chat/build-chat-items.test.ts, ui/src/ui/views/chat.ts)
  • jakepresent: The merged live Control tool-events commit says it landed work by @jakepresent, which is directly adjacent to the live tool messages this PR reorders against history. (role: original live tool-stream source contributor; confidence: medium; commits: de2ccffec166; files: ui/src/ui/app-tool-stream.ts, ui/src/ui/views/chat.ts)

Remaining risk / open question:

  • The linked report at [Bug]: Messages not in correct order in telegram and OpenClaw Control #80759 also names Telegram; this PR only changes Control UI ordering, so any Telegram-specific ordering problem remains separate if it still reproduces.
  • Current-head GitHub checks report failures with generic annotations, so CI/merge-queue state needs refresh or investigation before landing.

Codex review notes: model gpt-5.5, reasoning high; reviewed against b55d9fa4660b.

Copy link
Copy Markdown
Contributor Author

Added the requested behavior proof to the PR body.

I re-ran the actual Control chat builder path (buildChatItems) with a newer history assistant message (timestamp: 2000) and older live tool / completed stream items (timestamp: 1000). The observed output now orders the older tool/stream item before the newer assistant history group, which is the same item order consumed by ui/src/ui/views/chat.ts for rendering.

Local validation remains:

  • pnpm exec oxfmt --check --threads=1 ui/src/ui/chat/build-chat-items.ts ui/src/ui/chat/build-chat-items.test.ts
  • pnpm test ui/src/ui/chat/build-chat-items.test.ts -- --reporter=verbose (19/19)
  • pnpm check:changed

@akrimm702
akrimm702 marked this pull request as ready for review May 12, 2026 12:50
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026

@akrimm702 akrimm702 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current-main validation update.

I rechecked this against current main (2a67a7f65e26f249c607364ace5bc01fa0a0c357) and the current PR head (ee02df0db97e5d6996c1cfbec719943c6cb28291). The branch is 1 commit ahead / 950 commits behind, and GitHub metadata currently reports mergeable: false, so I verified the merge locally instead.

Local result: the PR head merges cleanly into current origin/main with no conflicts. After that merge, the PR diff is still limited to:

  • ui/src/ui/chat/build-chat-items.ts
  • ui/src/ui/chat/build-chat-items.test.ts

I did not find a new blocker or a required code adjustment. The focused fix still matches the reported Control chat ordering bug: older live tool/stream items sort before newer history, while the hidden-history notice stays first and reading indicators stay last.

Validation run on the locally merged state:

  • git diff --check origin/main...HEAD -- ui/src/ui/chat/build-chat-items.ts ui/src/ui/chat/build-chat-items.test.ts
  • pnpm exec oxfmt --check --threads=1 ui/src/ui/chat/build-chat-items.ts ui/src/ui/chat/build-chat-items.test.ts
  • pnpm test ui/src/ui/chat/build-chat-items.test.ts -- --reporter=verbose — 19/19 passed

Existing PR-head automation is also green: CI and Workflow Sanity both completed successfully on ee02df0db97e. Given the large main drift, a maintainer-side branch update or merge-queue run would still be useful to refresh GitHub’s merge/check state before landing.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 13, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 13, 2026
@openclaw-barnacle openclaw-barnacle Bot added channel: matrix Channel integration: matrix scripts Repository scripts size: M and removed size: S proof: sufficient ClawSweeper judged the real behavior proof convincing. labels May 13, 2026
@steipete
steipete merged commit b7d3b74 into openclaw:main May 13, 2026
111 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed via squash after maintainer fixups and rebase onto current main.

Proof:

  • Local: pnpm test ui/src/ui/chat/build-chat-items.test.ts src/plugins/registry.runtime-config.test.ts src/gateway/server.node-invoke-approval-bypass.test.ts -- --reporter=verbose
  • Local: pnpm run lint:extensions:bundled
  • Local: pnpm lint --threads=8
  • Local: pnpm prompt:snapshots:check
  • Testbox: pnpm check:changed passed on tbx_01krhbjcagrh67mthczqc7c7ym / run 25820314297
  • GitHub: fresh PR checks passed on c5ddc6bc2f227b70b05a472bcd5e6a023ca71582

Head before merge: c5ddc6b
Merge commit: b7d3b74

Thanks @akrimm702!

l3ocifer pushed a commit to l3ocifer/frack-openclaw that referenced this pull request May 13, 2026
* fix(ui): order live chat items by timestamp

* fix(ui): stabilize chat timestamp sorting

* test: refresh core lint fixtures

* test: refresh current main guard fixtures

* test: refresh codex prompt snapshots

* test(matrix): keep runtime helper local

---------

Co-authored-by: Peter Steinberger <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
* fix(ui): order live chat items by timestamp

* fix(ui): stabilize chat timestamp sorting

* test: refresh core lint fixtures

* test: refresh current main guard fixtures

* test: refresh codex prompt snapshots

* test(matrix): keep runtime helper local

---------

Co-authored-by: Peter Steinberger <[email protected]>
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
* fix(ui): order live chat items by timestamp

* fix(ui): stabilize chat timestamp sorting

* test: refresh core lint fixtures

* test: refresh current main guard fixtures

* test: refresh codex prompt snapshots

* test(matrix): keep runtime helper local

---------

Co-authored-by: Peter Steinberger <[email protected]>
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
* fix(ui): order live chat items by timestamp

* fix(ui): stabilize chat timestamp sorting

* test: refresh core lint fixtures

* test: refresh current main guard fixtures

* test: refresh codex prompt snapshots

* test(matrix): keep runtime helper local

---------

Co-authored-by: Peter Steinberger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui channel: matrix Channel integration: matrix gateway Gateway runtime proof: supplied External PR includes structured after-fix real behavior proof. scripts Repository scripts size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Messages not in correct order in telegram and OpenClaw Control

2 participants