Skip to content

feat: revalidate cached chat history with cursor deltas - #125606

Merged
steipete merged 5 commits into
mainfrom
steipete/chat-history-cursor-delta
Aug 18, 2026
Merged

steipete merged 5 commits into
mainfrom
steipete/chat-history-cursor-delta

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Revalidating a cached session transcript refetched the full approximately 100-message tail even when the client was already current. That cost 5–16 KB on small sessions and hundreds of KB on real sessions because the history API had no forward catch-up direction.

Why This Change Was Made

This adds Codex-app-server-style cursor catch-up to the existing chat.history and chat.startup methods rather than introducing another RPC. Tail pages now return an opaque deltaCursor, reusing the raw-delta codec's generation and last-sequence fence. Cursor requests return a closed union with kind: "delta" or kind: "reset".

Delta messages are projected through the same shared payload builder as live session.message broadcasts, so their wire payloads are byte-identical. The Control UI replays catch-up messages through the same reducer used by live events, persists the cursor with the session snapshot, and re-warms prefetched sessions with cursor requests. A generation change after compaction or reset, an unknown cursor, or a client more than 200 projected events behind returns reset; the client then performs today's normal cursor-less tail fetch instead of surfacing an error. This is an additive protocol change and old clients are unaffected.

User Impact

Cached-session revalidation and background prefetch shrink from full tail pages to approximately 1 KB deltas, or approximately 200 bytes when already current. Recently used sessions stay warm at a fraction of the traffic. CLI-imported sessions and cursor resets degrade to the existing full-tail flow.

The v1 tradeoff is deliberate: clients do not loop through more than 200 missed events, so far-behind cursors reset. A pane's cold open also still performs a full fetch under the existing network-wins rule. Looping and cold-open reuse are named follow-up opportunities.

Evidence

Focused post-rebase suites:

  • Gateway cursor suite: 16 tests, including delta/live payload equivalence cases and the deterministic WAL-race regression.
  • Control UI cursor suite: 4 tests.
  • Control UI prefetch suite: 6 tests.
  • Control UI snapshot-store suite: 11 tests.
  • Gateway protocol schema suite: 5 tests.
  • pnpm ui:build passed, including Control UI performance budgets.
  • Fresh whole-branch autoreview reported no accepted/actionable findings.

Live verification used an isolated dev Gateway and captured WebSocket frames. The rig showed a first-touch full fetch followed by cursor-based reopens; cold-reload prefetch converged after CLI-injected turns; compaction produced reset and recovered through a cursor-less refetch; and delta responses were approximately 1 KB versus 5–16 KB full tail pages, with already-current responses approximately 200 bytes.

That live pass found two bugs which were fixed failing-test-first: an append-miss stub could clobber a persisted snapshot (a pre-existing bug class tracked in #125128), and a WAL race could advance the delta cursor past the last projected event. The final tests cover both repairs in their owning boundaries.

There is no visual UI change: rendered transcripts are identical. This is wire-level transcript revalidation, so the relevant evidence is captured frames, live convergence/reset behavior, and focused tests rather than screenshots.

@clawsweeper

clawsweeper Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@steipete steipete self-assigned this Aug 18, 2026
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation app: web-ui App: web-ui gateway Gateway runtime labels Aug 18, 2026
@steipete
steipete marked this pull request as ready for review August 18, 2026 04:51
@openclaw-barnacle openclaw-barnacle Bot added size: XL maintainer Maintainer-authored PR labels Aug 18, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 18, 2026
@clawsweeper

clawsweeper Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 18, 2026, 3:38 AM ET / 07:38 UTC.

ClawSweeper review

What this changes

Adds cursor-based delta responses to chat history requests and uses them to revalidate cached Control UI transcripts without refetching a full tail page.

Merge readiness

⚠️ Ready for maintainer review - 4 items remain

Keep open. This fresh, maintainer-authored feature is not on current main; the inspected Gateway, protocol, and Control UI paths are coherent, but its persisted transcript-state risk warrants a rebase and exact-head validation before landing.

Priority: P2
Reviewed head: fd513e7c30e4f8c79ec72b6243aa99b699b8ed36
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The central implementation and boundary-level tests are strong; rebasing and exact-head validation remain the material merge-risk reducer.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-authored PR is outside the external-contributor proof gate; its body nevertheless describes an isolated Gateway WebSocket run covering delta, reset, and recovery.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored PR is outside the external-contributor proof gate; its body nevertheless describes an isolated Gateway WebSocket run covering delta, reset, and recovery.
Evidence reviewed 5 items Gateway owner boundary: The cursor branch rejects unsupported transcript sources, reads a bounded display delta, and returns a closed delta/reset result while retaining the non-cursor history response.
Replay boundary: The Control UI first handles reset through the existing full-tail request, then applies delta envelopes through the pane-owned session-message reducer and persists the returned cursor.
Regression coverage: The Gateway suite covers a WAL interleaving fence, visible-tail equivalence for plain, tool, heartbeat, and hidden events, reset boundaries, and bounded delta limits.
Findings None None.
Security None None.

How this fits together

The Control UI stores recent chat snapshots, then asks the Gateway to revalidate them. The Gateway reads the SQLite transcript projection and returns replayable message deltas or a reset that returns the client to the existing full-history fetch.

flowchart LR
  A[Cached chat snapshot] --> B[Control UI history request]
  B --> C[Gateway transcript projection]
  C --> D{Cursor valid and bounded?}
  D -->|Yes| E[Replayable message deltas]
  D -->|No| F[Reset response]
  E --> G[Shared chat reducer]
  F --> H[Full tail history fetch]
  G --> I[Updated cached snapshot]
  H --> I
Loading

Decision needed

Question Recommendation
Should this session-state feature be rebased onto current main and receive exact-head Gateway and Control UI validation before merge? Rebase and validate exact head: Rebase onto current main and rerun the focused cursor, snapshot-cache, and protocol checks before landing.

Why: The change persists transcript cursors and replays durable messages across cache, Gateway, and UI boundaries; maintainer judgment is needed on the final merged upgrade surface.

Before merge

  • Resolve merge risk (P1) - The branch is behind current main while changing persisted transcript cache and replay semantics; rebase and exact-head Gateway/UI validation are needed before merge.
  • Resolve merge risk (P1) - The local promisor checkout could not materialize every base-diff object, so the final merged diff should be rechecked after rebase.
  • Complete next step (P2) - Maintainer should review the rebased exact-head session-state proof; no narrow automated repair is identified.
Agent review details

Security

None.

PR surface

Source +626, Tests +886, Docs +1, Other +83. Total +1596 across 31 files.

View PR surface stats
Area Files Added Removed Net
Source 19 853 227 +626
Tests 9 897 11 +886
Docs 1 1 0 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 2 87 4 +83
Total 31 1838 242 +1596

Review metrics

Metric Value Why it matters
Production versus test delta production +853/-227; tests +897/-11; generated Swift +83; docs +1; baseline +4/-4 The feature adds a substantial cross-boundary protocol and cache path, balanced by focused Gateway and Control UI regression coverage.
Protocol surface 1 optional cursor parameter; 3 additive delta/reset schemas; 1 persisted snapshot field Existing clients retain the cursor-less history response, while current clients gain an explicit bounded recovery contract.

Stored data model

Persistent data-model change detected: persistent cache schema: ui/src/pages/chat/chat-gateway.test.ts, persistent cache schema: ui/src/pages/chat/chat-history-cursor.test.ts, persistent cache schema: ui/src/pages/chat/session-message-cache.test.ts, persistent cache schema: ui/src/pages/chat/session-message-cache.ts, persistent cache schema: ui/src/pages/chat/session-prefetch.test.ts, persistent cache schema: ui/src/pages/chat/session-prefetch.ts, and 22 more. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Rebase and validate exact head (recommended)
    Rebase onto current main and verify cursor replay, reset recovery, and persisted snapshot updates against the final merge result.
  2. Pause for an invariant split
    Pause the PR if the rebase reveals a transcript-identity or cache-ownership conflict that cannot be resolved without a separate design decision.

Technical review

Best possible solution:

Land the additive cursor protocol only after its reset-to-tail fallback and replayed transcript state remain correct on a rebased current-main head.

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

Not applicable as a bug report. The PR body provides a concrete isolated-Gateway/WebSocket verification route, and the inspected tests exercise cursor, reset, and replay behavior.

Is this the best way to solve the issue?

Yes, provisionally: the additive cursor plus reset-to-existing-tail-fetch path reuses the canonical history and reducer flows; final confidence requires the rebased exact-head checks.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 9ad1c0c68204.

Labels

Label justifications:

  • P2: This is a bounded cached-transcript performance feature with limited but meaningful user-facing impact.
  • merge-risk: 🚨 session-state: Merging changes persisted transcript cursors and replay ordering, so an upgrade mistake could stale or mis-associate cached chat state.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This maintainer-authored PR is outside the external-contributor proof gate; its body nevertheless describes an isolated Gateway WebSocket run covering delta, reset, and recovery.

Evidence

What I checked:

Likely related people:

  • steipete: Authored the cursor implementation and the prior merged persisted-session-snapshot feature that this PR incrementally revalidates. (role: feature owner; confidence: high; commits: fd513e7c30e4, 0e7f8c9ce963; files: src/gateway/server-methods/chat-history-handler.ts, ui/src/pages/chat/session-message-cache.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Rebase onto current main and rerun the focused Gateway cursor, Control UI cursor, prefetch, snapshot-store, and protocol suites.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (2 earlier review cycles)
  • reviewed 2026-08-18T04:58:51.489Z sha 7503260 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-18T05:07:21.594Z sha 7503260 :: needs maintainer review before merge. :: none

@openclaw-barnacle openclaw-barnacle Bot added the agents Agent runtime and tooling label Aug 18, 2026
@steipete

steipete commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Land-ready at fd513e7c30e4f8c79ec72b6243aa99b699b8ed36.

ClawSweeper rank-up move:

  • Rebase skipped: this branch was rebased onto current main before PR creation, remains conflict-free, and repository landing policy treats later behind-main drift as advisory. Native prepare validated the strict current-main merge tree.
  • No actionable ClawSweeper finding was skipped.

Proof completed:

  • Exact-head CI run 32110459485 is green, including generated Swift / Apple lanes.
  • pnpm check:architecture
  • node scripts/run-vitest.mjs ui/src/pages/chat/chat-history-cursor.test.ts ui/src/pages/chat/chat-gateway.test.ts ui/src/pages/chat/chat-send.test.ts (472 passed)
  • node scripts/run-vitest.mjs src/agents/agent-bundle-mcp-runtime.test.ts (94 passed)
  • Core, core-test, and UI typechecks; dead-export scans; formatting; Swift/core/scripts lint
  • Final local autoreview: clean, no accepted/actionable findings
  • OPENCLAW_TESTBOX=1 scripts/pr prepare-run 125606: passed with gates_mode=hosted_exact_or_recent_parent; prep head equals PR head. No separate Testbox run ID was created because exact-head hosted CI was reusable.

Known local-only gap: the macOS tooling Vitest shard hit its documented 900-second no-output timeout on this loaded host and was stopped during the automatic retry. Exact-head CI is the platform authority and passed the corresponding lanes.

@steipete
steipete merged commit 079a874 into main Aug 18, 2026
226 checks passed
@steipete
steipete deleted the steipete/chat-history-cursor-delta branch August 18, 2026 07:55
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 20, 2026
)

* feat(gateway): add cursor catch-up to chat history

* feat(ui): revalidate cached transcripts via history cursor

* fix(ui): stop append-miss stubs clobbering persisted snapshots

* fix(gateway): fence history delta cursor to projected events

* fix: repair cursor delta landing gates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling app: web-ui App: web-ui docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XL status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant