Skip to content

Terminal buffer not cleared when switching projects in web mode #26316

@lyy-pineapple

Description

@lyy-pineapple

Description

When switching between projects in the web UI, the terminal displays mixed output from the previous project's PTY session. The backend correctly creates a new PTY with the correct CWD, but the frontend terminal component retains stale persisted buffer content from the previous workspace.

Steps to Reproduce

  1. Open OpenCode web UI (opencode web)
  2. Open a terminal in Project A (e.g., a Python project directory)
  3. Run some commands (e.g., source .venv/bin/activate && python)
  4. Switch to Project B (a different directory)
  5. Open a terminal in Project B

Expected Behavior

A clean terminal showing only the new project's prompt:

user@host:/path/to/project-b$

Actual Behavior

Previous terminal content is mixed with the new prompt:

user@host:/path/to/project-b$ from-scratch$ python
Command 'python' not found...
user@host:/path/to/project-a$ source /path/to/project-a/.venv/bin/activate
python
Python 3.12.3 ...

Analysis

From server logs, the backend correctly creates a new PTY session with the correct CWD on each project switch:

INFO service=pty id=pty_xxx cmd=/bin/bash args=["-l"] cwd=/path/to/project-b creating session

The issue is in the frontend terminal context (packages/app/src/context/terminal.tsx). When switching to a different directory:

  1. The code calls trimAll() on the PREVIOUS workspace (clearing its buffers)
  2. But it does NOT clear stale buffers on the NEW workspace that were persisted from a previous session
  3. The terminal component then restores from these stale persisted buffers, causing old output to appear before the new PTY's fresh output

Environment

  • OpenCode version: 1.14.41
  • Mode: opencode web
  • OS: Linux x86_64
  • Browser: Chrome

Fix

When switching to a different directory, also call trimAll() on the new workspace to clear any stale persisted terminal buffers. See PR.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions