Skip to content

fix(tui): recover assistant responses after reconnecting to replacement runs - #128221

Merged
steipete merged 1 commit into
mainfrom
codex/autoqa-tui-reconnect-replacement-20260823
Aug 23, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/autoqa-tui-reconnect-replacement-20260823

Conversation

@steipete

Copy link
Copy Markdown
Contributor
Additional instructions

MUST: Keep Allow edits from maintainers enabled for this PR so maintainers
can help update the branch when needed.

What Problem This Solves

Fixes an issue where users reconnecting the terminal interface would see a replacement or newly started assistant run remain stuck at streaming indefinitely when that run appeared while the Gateway connection was interrupted.

Why This Change Was Made

Reconnect history is the authoritative owner of the current in-flight run, but the reconnect coordinator only reconciled runs whose identifier still matched the run captured before loading that history. When history adopted a replacement run, or discovered a run after an idle disconnect, the old watchdog stayed attached to the obsolete run and silently exited without recovering the visible conversation.

The reconnect owner now reconciles the active run reported by authoritative history while retaining the original owner check for terminal outcomes. Existing session/connection-generation guards, failed or unloaded history handling, and unchanged-run behavior remain intact; no protocol, auth, configuration, storage, timeout, or public API contracts change. Production growth is two executable lines plus two required invariant-comment lines.

User Impact

After reconnecting, terminal users recover the current assistant response even when the Gateway replaced the previous run or started one while the terminal was disconnected. Existing completed, interrupted, and failed-run outcomes continue to render normally.

Evidence

  • Genuine real-terminal PTY regression before the fix: replacement run remains at streaming and times out after approximately 37 seconds waiting for PTY_RECONNECT_RECOVERED.
  • The identical real-terminal flow passes after the fix in approximately 33 seconds using the unmodified production 30-second watchdog. One focused PTY scenario concurrently covers unchanged active, replacement active, newly appeared active, interrupted, failed, and completed reconnect outcomes.
  • OPENCLAW_VITEST_FS_MODULE_CACHE_PATH=/private/tmp/openclaw-tui-reconnect-vitest-cache-20260823 node scripts/run-vitest.mjs run --config test/vitest/vitest.tui.config.ts --configLoader runner src/tui/tui-event-handlers.test.ts src/tui/tui-session-actions.test.ts src/tui/tui-session-run-coordinator.test.ts src/tui/tui.test.ts — 342 tests across four owner-boundary suites pass.
  • OPENCLAW_VITEST_FS_MODULE_CACHE_PATH=/private/tmp/openclaw-tui-reconnect-vitest-cache-20260823 node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts --configLoader runner src/tui/tui-pty-harness.e2e.test.ts -t 'reconciles active, replacement, and terminal runs after reconnect history' — real PTY reconnect flow passes.
  • node scripts/run-tsgo.mjs -p tsconfig.core.json --incremental --tsBuildInfoFile /private/tmp/openclaw-tui-reconnect-core-20260823.tsbuildinfo — production types pass.
  • node scripts/run-tsgo.mjs -b test/tsconfig/tsconfig.core.test.plugins-platform.json --builders 1 — the actual TUI test-type shard passes.
  • Focused formatting, lint, mandatory assertion-safety ratchet, independent code review, and authenticated committed-diff review all pass.

@clawsweeper

clawsweeper Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Aug 23, 2026
@steipete
steipete marked this pull request as ready for review August 23, 2026 12:48
@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 23, 2026
@clawsweeper

clawsweeper Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 23, 2026, 9:12 AM ET / 13:12 UTC.

ClawSweeper review

What this changes

The PR makes TUI reconnect recovery follow the active run restored from session history and adds unit and PTY coverage for replacement and newly appeared runs.

Regression provenance

Possible regression — probable (reproduction; reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep open for maintainer landing review. Current main still skips reconnect recovery when restored history replaces the active run; the patch repairs that owner boundary without a blocking correctness finding.

Priority: P1
Reviewed head: 0d5bfc522873aee3789cd746fed4198aa9926a87
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The focused owner-boundary repair and boundary regression coverage are solid; normal exact-head merge validation remains the practical risk reducer.
Proof confidence 🌊 off-meta tidepool Not applicable: Not applicable: repository state identifies this as a maintainer-authored PR. The body records before/after terminal PTY validation, but this read-only review did not rerun it.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Not applicable: repository state identifies this as a maintainer-authored PR. The body records before/after terminal PTY validation, but this read-only review did not rerun it.
Evidence reviewed 6 items Current-main defect: Current main only reconciles when the run captured before history loading still equals the post-load active run, so a replacement run is skipped.
Owner-boundary repair: The branch selects the history run only for an active outcome and retains the original captured run for terminal outcomes before calling reconnect recovery.
Authoritative state producer: History loading writes the in-flight run into active TUI state before returning its active outcome, making this wrapper the correct reconciliation boundary.
Findings None None.
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts --configLoader runner src/tui/tui-pty-harness.e2e.test.ts -t 'reconciles active, replacement, and terminal runs after reconnect history'

Result: FAIL (partial) — step 2 expect_output reconciles active, replacement, and terminal runs after reconnect history: expected terminal output was not visible within 30 seconds: "reconciles active, replacement, and terminal runs after reconnect history"

node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts --configLoader runner src/tui/tui-pty-harness.e2e.test.ts -t 'reconciles active, r
eplacement, and terminal runs after reconnect history'
runner@runnervm76f27:/tmp/clawsweeper-live-proof-128221-h1SelA/target$ node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts --configLoa
der runner src/tui/tui-pty-harness.e2e.test.ts -t 'reconciles active, replacement, and terminal runs after reconnect history'

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-128221-h1SelA/target

node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts --configLoader runner src/tui/tui-pty-harness.e2e.test.ts -t 'reconciles active, r
eplacement, and terminal runs after reconnect history'
 ↓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › rejects rendering oracle false positives
 ↓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › renders local ready on startup
 ↓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › renders a compact model and active thinking level in the footer
 ↓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › keeps session modes scoped while trace changes and delivery stays process-owned
 ↓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › keeps the launch thinking override active across session-level changes
 ↓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › shows startup activity while post-connect initialization is pending




































Assertions:

  • FAIL expect_output: reconciles active, replacement, and terminal runs after reconnect history

How this fits together

After a Gateway reconnect, the terminal UI reloads session history and must recover any active assistant response. The recovered run is passed to the lifecycle watchdog, which either waits for its response or renders a terminal outcome.

flowchart LR
  A[Gateway reconnect] --> B[Reload session history]
  B --> C[Identify active or terminal run]
  C --> D[Reconnect recovery]
  D --> E[Streaming watchdog]
  E --> F[Terminal conversation]
Loading

Decision needed

Question Recommendation
Should an OpenClaw maintainer land this TUI reconnect owner-boundary fix once the exact merge head is validated? Land after normal merge validation: Accept the bounded session-state risk once the exact merge head is green, since source and PTY coverage support the active-run handoff.

Why: The protected maintainer label requires explicit maintainer handling; the remaining choice is landing readiness, not product direction.

Before merge

  • Resolve merge risk (P1) - This changes post-reconnect session ownership; the focused PTY path uses a fake backend, so it does not independently prove live Gateway transport or persistence.
  • Resolve merge risk (P1) - The branch is behind main, so the normal exact-head merge gate should validate the resulting three-way merge rather than treating base drift as a source regression.
Agent review details

Security

None.

PR surface

Source +59, Tests +32. Total +91 across 5 files.

View PR surface stats
Area Files Added Removed Net
Source 3 64 5 +59
Tests 2 35 3 +32
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 99 8 +91

Review metrics

Metric Value Why it matters
Production versus tests production +5/-1 (net +4); tests/test support +94/-7 (net +87) The small production increase is the necessary active-versus-terminal ownership discriminator; the remainder is focused regression coverage.

Merge-risk options

Maintainer options:

  1. Land with the active-run guard (recommended)
    Accept the bounded session-state risk after the normal exact-head merge gate is green; the branch preserves terminal-outcome ownership while recovering replacement active runs.
  2. Pause if merge validation changes behavior
    Hold this PR if the merge result fails the focused reconnect PTY scenario or alters the restored-history ownership boundary.

Technical review

Best possible solution:

Retain the active-versus-terminal distinction at the history-to-lifecycle handoff and land through the normal exact-head merge validation path.

Do we have a high-confidence way to reproduce the issue?

Yes, source-reproducible: current main skips reconciliation when history replaces the run ID, and the committed PTY scenario exercises replacement and newly appeared runs. This read-only review did not execute the scenario.

Is this the best way to solve the issue?

Yes: the repair belongs where authoritative history has already updated active TUI state but before lifecycle recovery consumes it, rather than adding a downstream watchdog workaround.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 066176351ebd.

Labels

Label justifications:

  • P1: A reconnect can leave terminal users without recovery of the current assistant response.
  • merge-risk: 🚨 session-state: The patch changes which in-flight run owns terminal recovery after history reload.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Not applicable: repository state identifies this as a maintainer-authored PR. The body records before/after terminal PTY validation, but this read-only review did not rerun it.

Evidence

What I checked:

  • Current-main defect: Current main only reconciles when the run captured before history loading still equals the post-load active run, so a replacement run is skipped. (src/tui/tui.ts:1449, 066176351ebd)
  • Owner-boundary repair: The branch selects the history run only for an active outcome and retains the original captured run for terminal outcomes before calling reconnect recovery. (src/tui/tui.ts:1451, 0d5bfc522873)
  • Authoritative state producer: History loading writes the in-flight run into active TUI state before returning its active outcome, making this wrapper the correct reconciliation boundary. (src/tui/tui-session-actions.ts:606, 0d5bfc522873)
  • Lifecycle consumer: Reconnect recovery clears the obsolete timer and arms its watchdog for the current active run; the timeout guards on that same run ID. (src/tui/tui-run-lifecycle.ts:247, 0d5bfc522873)
  • Boundary coverage: The added unit cases cover replacement and idle-appeared runs, while the PTY helper drives both through the real runTui loop with the fake backend. (src/tui/tui-pty-reconnect-test-support.ts:53, 0d5bfc522873)
  • Feature history: Current-main blame attributes the prior predicate to Peter Steinberger; recent adjacent reconnect work includes the interrupted-run recovery commit. (src/tui/tui.ts:1449, 0b20779024f3)

Likely related people:

  • steipete: Current-main blame attributes the reconnect predicate to Peter Steinberger, and history shows repeated TUI session and reconnect work including interrupted-run recovery. (role: recent TUI feature owner; confidence: high; commits: 0b20779024f3, 302d05b98878; files: src/tui/tui.ts, src/tui/tui-session-actions.ts, src/tui/tui-run-lifecycle.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • If the merge head changes this TUI path, rerun the focused reconnect PTY scenario before landing.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-08-23T12:54:53.576Z sha 0d5bfc5 :: needs maintainer review before merge. :: none

@steipete

Copy link
Copy Markdown
Contributor Author

Maintainer landing review: the ClawSweeper rank-up move is satisfied because the native merge does not change the reviewed TUI owner boundary; if the resulting merge changes those TUI paths, the focused reconnect PTY scenario must be rerun before landing.

The reported ClawSweeper live-verification failure is an observer timeout, not a failing terminal regression. Its observer stops waiting at 30 seconds, while this test intentionally exercises the unchanged production 30-second watchdog and passes after approximately 33 seconds. The genuine focused PTY run passed on the reviewed commit and again after rebasing, covering unchanged active, replacement active, newly appeared active, interrupted, failed, and completed reconnect outcomes. All 342 owner-boundary tests and the exact-head required CI gate also pass.

This proof uses the real terminal and production TUI lifecycle with a fake backend; it does not claim live authenticated Gateway or provider transport. Production timeout, session-generation guards, connection ownership, and terminal-outcome ownership remain unchanged.

@steipete
steipete merged commit 9b9afc3 into main Aug 23, 2026
477 of 479 checks passed
@steipete
steipete deleted the codex/autoqa-tui-reconnect-replacement-20260823 branch August 23, 2026 13:42
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant