Skip to content

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

Open
littlekirkycode wants to merge 2 commits intoPostHog:mainfrom
littlekirkycode:feat/clear-command
Open

feat(code): add /clear command to reset conversation history#1248
littlekirkycode wants to merge 2 commits intoPostHog:mainfrom
littlekirkycode:feat/clear-command

Conversation

@littlekirkycode
Copy link
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
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

Comment on lines +62 to +64
const { getSessionService } = await import(
"@features/sessions/service/service"
);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: no inline imports please

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your welcome, and thanks for taking the time to provide feedback! Will amend, and stay aware of this in future.

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
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().

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