Skip to content

feat(sessions): refill composer when cancelling before the agent starts#3006

Open
MattPua wants to merge 3 commits into
mainfrom
posthog-code/refill-composer-on-early-cancel
Open

feat(sessions): refill composer when cancelling before the agent starts#3006
MattPua wants to merge 3 commits into
mainfrom
posthog-code/refill-composer-on-early-cancel

Conversation

@MattPua

@MattPua MattPua commented Jun 29, 2026

Copy link
Copy Markdown
Member

Problem

Cancelling a turn right after sending (before the agent has produced any output) loses the just-sent message. A quick "oops, wrong send" means retyping it.

Changes

CleanShot.2026-06-29.at.17.45.39.mp4
  • Cancelling before the agent starts now refills the just-sent message back into the composer so it can be edited and re-sent.
  • 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 and mentions are restored as chips (via xmlToContent), not raw XML.
  • Steered messages are excluded (a steer folds into the running turn rather than starting its own).

Implementation:

  • New pure helper hasAgentStartedTurn({ events, hasOptimisticItems }) in core owns the decision: false while still optimistic (echo not landed, no output possible), otherwise checks for agent output after the latest session/prompt. Works for local and cloud (reads only persisted store state, avoids the local-only liveTurnContent).
  • SessionService.hasAgentStartedCurrentTurn(taskId) is the thin session-aware wrapper.
  • useSessionCallbacks captures the serialized text on send and applies the queued-wins / focus gating on cancel.

Danger level: 1/5 — additive, client-side only, no backend or protocol changes; gated behind narrow conditions and covered by tests.

How did you test this?

  • pnpm --filter @posthog/core test hasAgentStartedTurn — 9 cases (optimistic guard, prior-turn isolation, each agent output kind), all passing.
  • pnpm --filter @posthog/core typecheck and pnpm --filter @posthog/ui typecheck — both clean.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code

Cancelling a turn before the agent produces any output now refills the
just-sent message back into the composer so a quick "oops" doesn't lose
it. The message stays in conversation history (it's already in the
agent's context and the persisted log). Refill is skipped once the agent
has started, and when you're not focused on that chat. Attachments and
mentions are restored as chips via xmlToContent.

Generated-By: PostHog Code
Task-Id: ef6829e7-daed-47c3-abdd-59340c9d493c
@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 4dc1401.

@MattPua MattPua marked this pull request as ready for review June 29, 2026 21:47
@greptile-apps

greptile-apps Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

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

Comment thread packages/core/src/sessions/hasAgentStartedTurn.test.ts Outdated
Comment thread packages/core/src/sessions/hasAgentStartedTurn.test.ts Outdated
…ases

Collapse the three standalone false-returning tests into one it.each and
the two turn-isolation cases into another, matching the codebase's
preference for parametrised tests. Addresses greptile review feedback.

Generated-By: PostHog Code
Task-Id: fb9502ee-29c5-41cb-8c21-649fea69ca3d
@charlesvien charlesvien added the Stamphog This will request an autostamp by stamphog on small changes label Jul 10, 2026

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The logic and architecture are correct, but this behavioral change to the session cancel flow (refilling the composer when the agent hasn't started) has no human team-member review — only bot comments and the author's own replies.

@stamphog stamphog Bot removed the Stamphog This will request an autostamp by stamphog on small changes label Jul 10, 2026
Generated-By: PostHog Code
Task-Id: 105b1780-2d0b-43b6-9f28-140a9e7cade2
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.

2 participants