Skip to content

Refill composer when cancelling before the agent starts#2880

Draft
MattPua wants to merge 1 commit into
mainfrom
posthog-code/refill-cancelled-message
Draft

Refill composer when cancelling before the agent starts#2880
MattPua wants to merge 1 commit into
mainfrom
posthog-code/refill-cancelled-message

Conversation

@MattPua

@MattPua MattPua commented Jun 23, 2026

Copy link
Copy Markdown
Member

What

  • When a turn is cancelled before the agent has produced any output, the just-sent message is refilled into the composer so a quick "oops" doesn't lose it.
  • The message stays in conversation history — it's already committed to the agent's context and the persisted log, so removing it would desync the UI from what the model actually sees.
  • Refill is skipped once the agent has started working, and when you're not focused on that chat.
  • Attachments/mentions are restored as chips (via xmlToContent).

How

  • New core method SessionService.hasAgentStartedCurrentTurn(taskId) owns the decision:
    • returns false while the prompt is still optimistic (echo not landed → no output possible → refill safe),
    • otherwise checks for agent output after the latest session/prompt, reusing the existing classifyTurnEventKind.
  • Works for both local and cloud (reads store data both populate; avoids the local-only liveTurnContent).
  • useSessionCallbacks just captures the serialized text and applies the queued-wins / focus gating.

Test plan

  • Instant cancel (Stop and Escape) before output → message refilled + stays in history
  • Cancel after agent emits text/tool call → no refill
  • Queued message present → queued restore wins
  • Steer send → no refill
  • Cancel while viewing a different task → that composer untouched
  • Local and cloud behave the same

Created with PostHog Code

Restore the just-sent message into the composer when a turn is cancelled
before the agent has produced any output, so a quick "oops" doesn't lose
the text. The message stays in history (it's already in the agent's
context). Core owns the "did the agent start?" decision so the optimistic
window is handled correctly for both local and cloud.

Generated-By: PostHog Code
Task-Id: 88f22fa1-dfa2-4fae-905f-26d327c1a133
@github-actions

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 85e1a5d.

@greptile-apps

greptile-apps Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(sessions): refill composer when can..." | Re-trigger Greptile

Comment on lines 83 to +86
markActivity(taskId);
await sessionService.sendPrompt(taskId, text, {
steer: messagingMode === "steer",
});

const view = getAppViewSnapshot();
const isViewingTask =
view?.type === "task-detail" && view?.taskId === taskId;
if (isViewingTask) {
const steer = messagingMode === "steer";
// A steer folds into the running turn rather than starting its own, so it

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.

P2 lastSentTextRef populated before sendPrompt resolves

lastSentTextRef.current is assigned before the await, so it is set even when sendPrompt ultimately rejects (e.g., network error). On a brand-new task with no previous events, hasAgentStartedCurrentTurn returns false, so a subsequent cancel would refill the failed-send's text. For existing sessions the refill is suppressed because the backwards-scan sees a prior agent event and returns true, which limits the blast radius. The current behaviour is arguably fine (user gets back what they tried to send), but it is worth confirming this is intentional rather than accidental — moving the assignment to after the await and inside the try block would make the intent explicit.

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