Skip to content

feat(conversation): implement optimistic message handling for real-ti… - #710

Merged
pikann merged 5 commits into
masterfrom
feature/enhance-agent-conversation-ux
Nov 6, 2026
Merged

pikann merged 5 commits into
masterfrom
feature/enhance-agent-conversation-ux

Conversation

@pikann

@pikann pikann commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes three problems with continuing an existing conversation:

  1. Message appeared ~10s late. After sending, only a dot showed until the server persisted the turn's user_message event.
  2. Floating chat boxes went stale. They showed only "thinking" until you opened the conversation page.
  3. Conversation page stopped updating until a reload.

Changes

  • Optimistic user message (use-optimistic-user-message.ts): the sent message is overlaid on the event list immediately and the thread is treated as running, so the "thinking" indicator shows right away. The overlay is dropped when the real user_message event (same text, past the index seen at send time) arrives, so it never renders twice. It is also dropped if the send fails or the "agent is busy" dialog is cancelled, and it follows the reply if it starts a new conversation.
  • Floating chats (ai-chat-float.tsx, ai-chat-float-global.tsx): they read a plain events query, but realtime persisted events only land in the live tail cache, so the query stayed stale until a status change. Both now use useConversationEventWindow, plus the optimistic message. Note they now load only the newest page (200 events) and have no "load older" control.
  • Conversation page (conversation-view.tsx, use-conversation-event-window.ts):
    • Sending now calls jumpToLatest(). Previously a reader who had scrolled up kept following off and all new events, including their own message, were withheld.
    • The event window refetches once when there is a hole between the paged events and the first live event (a dropped realtime message), which nothing else repaired.

Testing

  • tsc -b, biome, and vitest run src/components/projects pass (426 tests), plus 2 new tests for the optimistic hook.
  • Not yet verified manually in a browser. The gap-healing refetch is a defensive guard; the scroll/following case is the more likely cause of the stale page.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important

Two behavior bugs to fix before merge: an optimistic message with surrounding whitespace never clears (the server trims the persisted text, the client does not), and the floats' new-conversation branch never scopes the overlay to the conversation it was sent in, so starting a fresh conversation can inherit the old pending message.

Reviewed changes

  • Optimistic overlay — new useOptimisticUserMessage hook shows the sender's message and the running state immediately, then drops the overlay when the matching persisted user_message arrives (or on send failure).
  • Send flow — conversation-view.tsx now jumps to the latest event on send and re-scopes the overlay when the reply lands in a new conversation; both floating panels get the same hook.
  • Floating panels — switch from the plain events query to useConversationEventWindow, so persisted events arrive through the live tail cache instead of going stale until a status change.
  • Window gap healing — useConversationEventWindow refetches once per distinct hole between the paged window and the first live event.

ℹ️ The new gap-healing refetch has no test

use-conversation-event-window.test.tsx covers paging, live merge, and pruning, but nothing exercises the new firstLiveIndex > lastPagedIndex + 1 branch or its healedGapRef dedupe. A case that signals an event at lastPagedIndex + 2 and asserts exactly one conversationEventWindowKey invalidation would lock in the intended "one refetch per distinct hole" behavior.

Technical details
# Missing test for the gap-healing effect

## Affected sites
- `apps/web/src/components/projects/agents/use-conversation-event-window.ts:136-145` — the new effect.
- `apps/web/src/components/projects/agents/use-conversation-event-window.test.tsx` — existing `signal`-driven harness.

## Required outcome
- A boundary gap (e.g. `signal` an event at `lastPagedIndex + 2`) triggers exactly one refetch.
- The same gap does not refetch again on the next render.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

This run reviewed the follow-up commit on top of the prior pullfrog review.

  • Whitespace normalization — begin now trims the message before storing it, so the optimistic bubble matches the server's TrimSpace-persisted echo; a padded-text test was added.
  • Float new-conversation scoping — both floating panels now call optimistic.follow(...) when a send starts a brand-new conversation, so the overlay is scoped and cleared when the user opens a new conversation.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important

The 60s optimistic-message TTL can drop a message that is queued and still waiting to start, so the sender's own message disappears from the transcript while the conversation stays queued.

Reviewed changes

This run reviewed the delta since 81aebbd (0909d686).

  • Added busy-dialog hooks — useAgentBusyPrompt.send now takes optional onAsk/onResume; conversation-view hides the overlay while the dialog is open and restores it after queue/force.
  • Normalized echo matching — both the optimistic text and the persisted event are whitespace-collapsed before comparison; begin reads the latest index through a ref.
  • Bounded the overlay — a 60s TTL drops an overlay whose echo never matches; the synthetic event uses a fractional event_index.
  • Keyed gap healing by conversation — healedGapRef now includes conversationId.
  • Added tests — TTL expiry, cross-conversation follow, foreign-conversation clearing, and inner-whitespace echo matching.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important

The TTL hold is released as soon as the conversation leaves queued, but the persisted user_message does not exist yet at that point, so the overlay can still disappear before the echo arrives.

Reviewed changes

This run reviewed the delta since 0909d686 (c03b373f).

  • Held the safety-net TTL while queued — useOptimisticUserMessage now takes a holdTimeout flag that suspends the 60s expiry, and all three call sites pass conversation?.status === "queued".
  • Covered the hold in tests — a new case advances two TTLs while held, asserts the overlay survives, then clears one tick after release.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

This run reviewed the delta since c03b373 (dd9fb18): the optimistic-message safety-net TTL now starts when the hold lifts.

  • Restarted the TTL when the hold lifts — useOptimisticUserMessage arms a full 60s timeout only once holdTimeout goes false, so a long queue wait no longer leaves the timer already expired at the queued → running transition, and the overlay survives until the echo is persisted.
  • Extended the hold test — the queued case now asserts the overlay survives TTL - 1 after release and clears one tick later.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann
pikann merged commit 92ca114 into master Nov 6, 2026
2 checks passed
@pikann
pikann deleted the feature/enhance-agent-conversation-ux branch November 6, 2026 08:09
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