Skip to content

feat(code): add /clear command to reset conversation history#1248

Merged
jonathanlab merged 3 commits intoPostHog:mainfrom
littlekirkycode:feat/clear-command
Mar 31, 2026
Merged

feat(code): add /clear command to reset conversation history#1248
jonathanlab merged 3 commits intoPostHog:mainfrom
littlekirkycode:feat/clear-command

Conversation

@littlekirkycode
Copy link
Copy Markdown
Contributor

@littlekirkycode littlekirkycode commented Mar 15, 2026

Screenshot 2026-03-15 at 15 48 43

Summary

Adds a /clear slash command that wipes conversation history and
reconnects with a fresh session. Useful when context is cluttered or the
agent is stuck in a loop.

Implementation

Client-side command in the message editor — delegates to
SessionService.resetSession():

{
  name: "clear",
  description: "Clear conversation history and start fresh",
  async execute(_args, ctx) {
    const { getSessionService } = await
import("@features/sessions/service/service");
    await getSessionService().resetSession(ctx.taskId, ctx.repoPath);
    toast.success("Conversation cleared");
  },
}

resetSession() calls reconnectInPlace with a new clearHistory
flag. When true, log replay is skipped entirely — the session reconnects
with empty event history instead of replaying from the log file:

const prefetchedLogs = clearHistory
  ? { rawEntries: [], sessionId: undefined, adapter: undefined }
  : await this.fetchSessionLogs(logUrl, taskRunId);

SDK's built-in /clear is suppressed — it doesn't work in our ACP
context, so it's added to UNSUPPORTED_COMMANDS and replaced by our own.

Changes

File What
commands.ts (message-editor) New /clear command definition
commands.ts (session) Add "clear" to UNSUPPORTED_COMMANDS
service.ts resetSession() passes clearHistory=true, reconnectInPlace skips log fetch when clearing

Test plan

  • Type /clear in message editor → conversation history is cleared
  • Session reconnects with clean state (no old messages replayed)
  • Toast confirms "Conversation cleared"
  • /clear appears in slash command autocomplete

Adds /clear slash command that resets conversation history and starts a
fresh session. Suppresses the SDK's built-in /clear (which doesn't work
in our context) and replaces it with our own implementation.
resetSession() now clears UI history by skipping log replay on reconnect.
Copy link
Copy Markdown
Contributor

@jonathanlab jonathanlab left a comment

Choose a reason for hiding this comment

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

Thanks for taking a stab at this! Clearing this way will only clear the in memory logs, but the actual S3 logs will not be reset.
This means that if you run /clear, and then reload the application, you'll see your old logs. (Attachment shows an example of this. I ran /clear after each message, then restarted PostHog Code)

We'll have to create a new task run (Each task run has an associated S3 log) or maybe filter for logs after the /clear command. We already have comparable functionality for this in createNewLocalSession.

image

Remove inline import in commands.ts. Rewrite resetSession() to create a new task run via createNewLocalSession() instead of clearing in-memory logs, ensuring old messages don't reappear on app restart.
@littlekirkycode
Copy link
Copy Markdown
Contributor Author

Both changes applied — removed the inline import and rewrote resetSession() to tear down the old session and create a fresh task run via the suggested createNewLocalSession(), so old messages won't come back after a restart.

Also rebased #1249 on top of this as its clear-and-continue flow goes through resetSession().

@jonathanlab
Copy link
Copy Markdown
Contributor

Note: This PR does not offer support for /clear in cloud runs, but that's an architectural issue on our side, will merge for now since it works fine locally.

@jonathanlab jonathanlab merged commit fefe98c into PostHog:main Mar 31, 2026
15 checks passed
@jonathanlab
Copy link
Copy Markdown
Contributor

heads up, I reverted this PR after merging it. Upon closer look, you modified resetSession which would reconnectInPlace. Reset session is meant to be used for reconnecting to the same task run. I'd prefer we actually create a separate function for this instead of modifying existing behavior because the New session button depends on resetSession (shown to the user when a chat runs into an issue)

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