Skip to content

vscode: terminal-adapter reconnect notices accumulate as orphaned scrollback lines; not cleared on successful reconnect #1001

Closed

Description

@amrmelsayed

Problem

When a Codev terminal's WebSocket drops and the bounded-retry kicks in (#936), scheduleReconnect() writes a yellow status line to the terminal pane on each attempt:

[Codev: Connection lost — retrying in 1s (attempt 1/6)]
[Codev: Connection lost — retrying in 2s (attempt 2/6)]
[Codev: Connection lost — retrying in 4s (attempt 3/6)]
...

These notices are each terminated by \r\n, so they stack as separate lines in the terminal buffer.

When the reconnect eventually succeeds (which is the common case after pir-991's root-cause fix, since afx tower stop && afx tower start now leaves the extension host alive and most restarts complete within a backoff window or two), the successful reconnect does not clear the notice lines. They remain in the terminal's scrollback as orphaned status messages above (or interleaved with) the resumed normal output, until the user closes and reopens the terminal pane.

Why the bug landed

The original #936 PR body described the desired behavior as overwriting the previous line in place:

Use ANSI cursor controls (\r + clear-line) to overwrite the previous line, or accept a one-line-per-attempt cadence but only when the backoff actually fires.

The implementation took the "one line per attempt" path (\r\n terminator) without committing to the in-place overwrite mechanic, and there is no symmetric "we just reconnected" handler that walks back up the notices and wipes them on success.

Source: packages/vscode/src/terminal-adapter.ts:204-207

this.writeEmitter.fire(
  `\x1b[33m[Codev: Connection lost — retrying in ${delay / 1000}s ` +
  `(attempt ${this.backoff.attempt}/${MAX_RECONNECT_ATTEMPTS})]\x1b[0m\r\n`,
);

Proposed fix (overwrite-in-place pattern)

The right shape matches the original #936 intent. Two coordinated changes:

1. Notice writes overwrite the previous notice line, not append

Replace the trailing \r\n with a leading \r\x1b[2K (carriage return + erase-entire-line) so each attempt overwrites the previous notice on the same line:

this.writeEmitter.fire(
  `\r\x1b[2K\x1b[33m[Codev: Connection lost. retrying in ${delay / 1000}s ` +
  `(attempt ${this.backoff.attempt}/${MAX_RECONNECT_ATTEMPTS})]\x1b[0m`,
);

(Also dropping the em dash from the message text per project convention.)

2. Wipe the notice line on successful reconnect

Inside the WebSocket onopen (or the equivalent point where the reconnect resolves successfully), emit one more \r\x1b[2K to clear the line entirely before normal output resumes:

// Inside the connection-success handler in terminal-adapter.ts
private onReconnectSuccess(): void {
  if (this.hadReconnectNotice) {
    this.writeEmitter.fire('\r\x1b[2K');
    this.hadReconnectNotice = false;
  }
  // ... existing post-connect logic
}

The boolean hadReconnectNotice is set whenever scheduleReconnect writes a notice; cleared on the wipe. Avoids emitting cursor-control sequences when no notice was ever written (e.g. happy-path first connect).

Acceptance

  • During a backoff retry cycle, only one notice line is visible in the terminal at any given time. Subsequent attempts overwrite the previous notice on the same line, with the attempt counter ticking from (attempt 1/6) to (attempt 6/6) in place.
  • When a reconnect attempt succeeds before the budget is exhausted, the notice line is cleared from the terminal before normal output resumes. No orphaned [Codev: Connection lost...] line remains in the scrollback.
  • When the budget IS exhausted, the give-up notice (#939's [Codev: unable to reconnect after 6 attempts. Click here to reconnect]) remains visible. The wipe behavior only applies to in-progress retry notices, not to the give-up state.
  • The \r\x1b[2K sequence does not garble or wipe normal terminal output that arrives between retry attempts. Test by running a long-output command (e.g. yes | head -200) and simulating a reconnect mid-stream.

Test plan

  • Unit test in packages/vscode/src/__tests__/terminal-adapter.test.ts:
    • Drive a multi-attempt reconnect, capture the writeEmitter outputs, assert each retry notice contains the erase-line prefix and is the only "Connection lost" line in the cumulative output buffer.
    • Drive a successful reconnect after one notice, assert the post-success buffer contains zero Connection lost notices (notice was wiped).
    • Drive a give-up, assert the give-up notice is present and was NOT wiped.
  • Manual: run the dev host, force a quick Tower bounce (within the first 2-3 backoff intervals), confirm the terminal shows no orphaned notice lines after reconnect.

Out of scope

  • The give-up state's "Click here to reconnect" affordance from #939 (intentionally remains visible as the terminal failure state).
  • The web dashboard terminal's notice rendering (separate code path in packages/dashboard/; file a sibling issue if the same bug exists there).
  • Changing the backoff curve or attempt budget.

Related

  • #936 — terminal reconnect overhaul (the PR this is fixing a partial implementation of)
  • #939 — click-to-reconnect affordance on give-up (the post-budget state that should remain visible)
  • #991 — Tower-side root-cause fix that makes successful in-budget reconnects the common case (shipped today; raises the visibility of the unwiped-notice symptom)
  • packages/vscode/src/terminal-adapter.ts:scheduleReconnect() — where the notices are emitted

Activity

  1. added
    bug
    area/vscode
    on Jun 6, 2026
  2. self-assigned this
    on Jun 6, 2026
  3. amrmelsayed commented on Jun 6, 2026

    @amrmelsayed
    CollaboratorAuthor

    On it! Working on this with the PIR protocol (plan + dev-approval gates before PR).

  4. added 5 commits that reference this issue on Jun 6, 2026
  5. added a commit that references this issue on Jun 6, 2026
  6. added a commit that references this issue on Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/vscodebug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions