Skip to content

feat(agent): implement /clear command#3546

Open
haacked wants to merge 3 commits into
mainfrom
posthog-code/clear-command
Open

feat(agent): implement /clear command#3546
haacked wants to merge 3 commits into
mainfrom
posthog-code/clear-command

Conversation

@haacked

@haacked haacked commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Problem

Typing /clear in PostHog Code reports "Unsupported slash command". Upstream hid the SDK's own /clear because it is unreliable in this embedding, but users still want a way to drop the conversation and free up context without abandoning the task, including on cloud runs, where the append-only session log is what rehydrates the conversation on resume.

Here's an example session that shows it works.

Screenshot 2026-07-17 at 5 22 23 PM

Refs #670

Changes

  • Claude adapter (packages/agent): prompt() intercepts /clear (never forwarded to the SDK). It retires the current SDK query (reusing the refreshSession teardown, now extracted as retireQuery) and swaps in a brand-new SDK session, fresh id, no resume, under the same ACP session. The ACP session id stays stable; the new Session.sdkSessionId tracks the underlying SDK session, and hasSession accepts either id so cancel/reconnect work from both sides. /clear is advertised in the command list again.
  • Session log / cloud resume: the clear is recorded as a _posthog/conversation_cleared marker plus an updated sdk_session mapping in the append-only log. rebuildConversation (jsonl hydration + ResumeSaga) treats the marker as a boundary, and ResumeSaga.findSessionId picks up the post-clear session id, so cloud sandbox restarts and desktop cold reconnects resume the fresh SDK session with only post-clear turns.
  • UI: the transcript keeps the full history and renders a "Conversation cleared" divider (like the compaction marker); the context indicator resets to zero via a usage_update.
  • Timeout and reconnect hardening: the /clear prompt is only broadcast (and thus logged) once the new session is confirmed live, so a timeout leaves no orphaned entry in the log. A "Clearing…" status indicator mirrors the existing compaction spinner, so a slow or failed clear gives the user feedback instead of going silent, with a clearing_failed row rendering the outcome. On success, the local session file left under the original session id gets deleted so a cold reconnect can't mistake it for current and resume the pre-clear conversation. /clear also now refuses while a turn is queued, not just while one is active.

Codex sessions are unchanged, /clear is Claude-adapter only for now.

How did you test this?

  • New unit tests: adapter /clear behavior (fresh session swap, marker/mapping emission, busy/queued-turn guards, refresh-after-clear, timeout handling, stale jsonl cleanup, resumeSession id matching after a clear), command advertisement, clear-boundary handling in jsonl hydration and ResumeSaga, and the UI divider and status-row rendering (including clearing/clearing_failed).
  • Ran the full @posthog/agent suite, @posthog/ui sessions suite, and pnpm typecheck for agent/core/ui plus Biome on the touched packages.

Automatic notifications

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

Why: /clear is a frequently reached-for command that PostHog Code currently rejects; implementing it cloud-first required making the session-log rehydration clear-aware.


Created with PostHog Code

@trunk-io

trunk-io Bot commented Jul 17, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit e0d3e15.

haacked and others added 2 commits July 17, 2026 11:52
Intercept /clear in the Claude adapter instead of forwarding it to the SDK: retire the current query and swap in a brand-new SDK session (fresh id, no resume) under the same ACP session. Persist a _posthog/conversation_cleared marker plus an updated sdk_session mapping to the append-only session log, and make the rehydration paths (jsonl hydration, ResumeSaga) treat the marker as a conversation boundary so desktop reconnects and cloud resumes rebuild only the post-clear conversation. The UI renders a "Conversation cleared" divider and resets the context indicator.

Generated-By: PostHog Code
Task-Id: 7f180c25-f99b-4c96-ac84-f39c0e887a37
- Parallelize post-clear notifications instead of sequential awaits
- Use isNotification() helper instead of set-based method checking
- Remove unused variables and redundant state resets
- Improve comments for session id handling
@haacked
haacked force-pushed the posthog-code/clear-command branch from f58e90b to 30f05dc Compare July 17, 2026 21:12
Only broadcast (and thus log) the "/clear" prompt once the new session
is confirmed live, so a timeout leaves no orphaned entry in the log.
Add a "Clearing…" status indicator, mirroring the existing compaction
spinner, so a slow or failed clear gives the user feedback instead of
going silent. Delete the local session file left under the original
session id after a clear so a cold reconnect can't mistake it for
current and resume the pre-clear conversation.
@haacked
haacked marked this pull request as ready for review July 18, 2026 00:24
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