Skip to content

fix(tui): isolate replacement sessions from stale async results - #130227

Merged
steipete merged 1 commit into
mainfrom
codex/tui-terminal-owner-qa
Aug 26, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/tui-terminal-owner-qa

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where users resetting or replacing a terminal session could see prompts, side answers, picker failures, or model/session selections from the previous session reappear in the replacement. An overlapping session refresh could also resurrect the old opaque session ID, and the existing account-limit PTY check could flake because its fake Gateway emitted an invalid event.

Why This Change Was Made

Session ownership is the exact agent, session key, opaque session ID, and lifecycle generation; a key alone is not sufficient after an in-place reset. Reuse the existing session-incarnation owner for asynchronous model/session picker requests, selections, and failures; fence session-info refreshes with the canonical lifecycle generation; reject retired transcript snapshots before projection or rendering; and accept local side answers only while their locally owned run remains alive. The PTY fake Gateway now includes the sequence required by the actual Gateway chat-event contract.

User Impact

Terminal sessions no longer leak an old conversation's prompt, side answer, or error into its replacement, and stale pickers cannot silently change the replacement session. Legitimate first-session identity binding, newer authoritative session adoption, cross-client Gateway answers, and normal local /btw replies remain intact.

Evidence

  • Captured failing regressions on clean main: same-key model/session picker mutations, stale catalog/session-list openings, delayed picker failure disclosure, retired /btw disclosure, retired transcript disclosure with older/equal/missing timestamps, and stale refresh success/failure.
  • Independently reproduced the refresh race through the real session-actions owner, event handlers, ChatLog, and an already occupied history-reload coalescer.
  • node scripts/run-vitest.mjs src/tui/tui-command-handlers.test.ts 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-session-events.test.ts src/tui/tui.submit-handler.test.ts src/tui/tui-pty-harness-assertion-test-support.test.ts
  • node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts — four real-PTY files, 76 tests passed.
  • env -u OPENCLAW_TESTBOX node scripts/check-changed.mjs — all changed-file formatting, core/test typecheck, lint, max-line ratchet, dependency, architecture, database, and import-cycle gates passed.
  • Independent structured review: no accepted/actionable findings.
  • Production LOC: +33/-27 (net +6); test and fixture support: +179/-9.
  • Proof boundary: real terminal loop and fake Gateway; no live authenticated provider/channel call was needed for these deterministic TUI-owner defects. No screenshot or recording is attached because the user-visible regression is conditional cross-session transcript disclosure; the real PTY scenarios and red/green owner regressions are the safer direct evidence.

Follow-up: task-suggestion and plugin-approval asynchronous notices deserve a separate session-incarnation audit.

@clawsweeper

clawsweeper Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@steipete
steipete marked this pull request as ready for review August 26, 2026 16:36
@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Aug 26, 2026
@steipete steipete closed this Aug 26, 2026
@steipete steipete reopened this Aug 26, 2026
@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 26, 2026
@clawsweeper

clawsweeper Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 26, 2026, 1:13 PM ET / 17:13 UTC.

ClawSweeper review

What this changes

The PR prevents stale asynchronous picker, refresh, transcript, and local side-answer results from affecting a replacement terminal session.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep open: this maintainer-labeled PR makes a focused TUI session-incarnation repair and has no actionable patch defect from review; it still needs exact-head CI completion before merge.

Priority: P1
Reviewed head: fd7d59301f552bb504ad7798db740fd9847acbf5

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-boundary repair with credible regression coverage and no blocking review finding.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-authored PR includes focused real-PTY validation in its body; the external-contributor proof gate does not apply.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored PR includes focused real-PTY validation in its body; the external-contributor proof gate does not apply.
Evidence reviewed 5 items Picker ownership repair: The PR captures session key, agent, opaque session ID, and generation before picker work, then requires the captured incarnation to remain current before opening or applying a picker result.
Event dispatch and Gateway contract: The TUI dispatches Gateway session.message events to the changed handler; the Gateway producer attaches its current session snapshot, including session ID and updated timestamp, to those events.
Focused regression coverage: The added cases cover same-key replacement sessions for model/session pickers, delayed picker failure, retired local side answers, retired transcript snapshots, and in-flight refresh results.
Findings None None.
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts

Result: FAIL (failed) — step 1 run node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts: previous terminal command was still running after 30 seconds: "node scripts/run-vitest.mjs run --config test/vitest/vitest.tui-pty.config.ts"

RUN  v4.1.11 /tmp/clawsweeper-live-proof-130227-EQrVCN/target

 ✓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › rejects rendering oracle false positives 18ms
 ✓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › renders local ready on startup 1ms
 ✓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › renders a compact model and active thinking level in the footer 457ms
stdout | src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › keeps session modes scoped while trace changes and delivery stays process-owned
[behavior-evidence] tui-session-footer {"terminal":"real PTY","sourceModesVisible":true,"targetModesCleared":true,"traceTransitionVisible":true,"fixedDeliveryPr
opagated":true}

 ✓  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 4338ms
 ✓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › keeps the launch thinking override active across session-level changes 154ms
 ✓  tui-pty  src/tui/tui-pty-harness.e2e.test.ts › TUI PTY harness › shows startup activity while post-connect initialization is pending 1ms

Assertions:

  • FAIL expect_output: Test Files

How this fits together

The terminal UI receives Gateway session/chat events and asynchronous picker or refresh results, then projects them into the selected conversation. This change decides whether each result still belongs to that exact session incarnation before rendering or mutating state.

flowchart LR
  A[Gateway session and chat events] --> B[TUI event handlers]
  C[Async picker and refresh requests] --> D[Session-incarnation check]
  B --> D
  D -->|current owner| E[Chat log and session state]
  D -->|retired owner| F[Discard stale result]
  E --> G[Terminal UI]
Loading

Before merge

  • Complete next step (P2) - This protected maintainer PR needs normal exact-head CI and merge review, not an automated repair branch.
Agent review details

Security

None.

PR surface

Source +6, Tests +170. Total +176 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 4 35 29 +6
Tests 3 177 7 +170
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 212 36 +176

Review metrics

Metric Value Why it matters
Production versus test LOC production +33/-27 (net +6); tests +177/-7; fixture support +2/-2 The small production increase is directly tied to enforcing session-incarnation ownership, with most of the change providing focused regressions.

Stored data model

Persistent data-model change detected: vector/embedding metadata: src/tui/tui-event-handlers.test.ts. Confirm migration or upgrade compatibility proof before merge.

Technical review

Best possible solution:

Keep one session-incarnation invariant at the asynchronous TUI owners, with the supplied same-key replacement regressions protecting it, then merge after exact-head checks are green.

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

Yes—source inspection shows current main checked only session key and agent for these picker and refresh paths, while the PR’s same-key replacement cases exercise the missing incarnation boundary.

Is this the best way to solve the issue?

Yes—the generation and opaque-session identity checks are placed at the asynchronous producers before their UI mutations, rather than attempting to clean stale state after it is rendered.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 67a310b2c6ba.

Labels

Label changes:

  • add P1: A stale result can leak or mutate state in an active terminal conversation after a session reset or replacement.
  • add merge-risk: 🚨 session-state: The changed guards control which asynchronous events and results may update the selected terminal session.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This maintainer-authored PR includes focused real-PTY validation in its body; the external-contributor proof gate does not apply.

Label justifications:

  • P1: A stale result can leak or mutate state in an active terminal conversation after a session reset or replacement.
  • merge-risk: 🚨 session-state: The changed guards control which asynchronous events and results may update the selected terminal session.
  • 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: This maintainer-authored PR includes focused real-PTY validation in its body; the external-contributor proof gate does not apply.

Evidence

What I checked:

  • Picker ownership repair: The PR captures session key, agent, opaque session ID, and generation before picker work, then requires the captured incarnation to remain current before opening or applying a picker result. (src/tui/tui-command-handlers.ts:270, fd7d59301f55)
  • Event dispatch and Gateway contract: The TUI dispatches Gateway session.message events to the changed handler; the Gateway producer attaches its current session snapshot, including session ID and updated timestamp, to those events. (src/gateway/server-session-events.ts:317, 67a310b2c6ba)
  • Focused regression coverage: The added cases cover same-key replacement sessions for model/session pickers, delayed picker failure, retired local side answers, retired transcript snapshots, and in-flight refresh results. (src/tui/tui-event-handlers.test.ts:3438, fd7d59301f55)
  • Prior ownership work: Recent main history shows the same area’s session/run ownership repair immediately precedes this PR, with earlier picker and reset isolation work also authored by the likely area owner. (src/tui/tui-event-handlers.ts:502, 67dc60756ac3)
  • Diff hygiene: The direct current-main comparison is limited to seven TUI source, test, and fixture files, and has no whitespace errors. (src/tui/tui-session-actions.ts:335, fd7d59301f55)

Likely related people:

  • steipete: Authored the immediately preceding session/run ownership fix and the earlier picker-ownership and reset-isolation fixes in this same TUI boundary. (role: recent TUI session-ownership contributor; confidence: high; commits: 67dc60756ac3, 0e0ec1929147, 262f26355a8f; files: src/tui/tui-command-handlers.ts, src/tui/tui-event-handlers.ts, src/tui/tui-session-actions.ts)

Rank-up moves

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

  • Let exact-head required checks complete green before merge.

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.

@steipete

Copy link
Copy Markdown
Contributor Author

Maintainer validation of fd7d59301f552bb504ad7798db740fd9847acbf5:

The root cause was partial session ownership at deferred UI boundaries: a session key can survive reset while its opaque session ID and lifecycle generation change. This repair uses the existing incarnation owner for picker work, the canonical generation for refreshes, and checks event ownership before transcript projection or local side-answer display. It removes the weaker picker key-only checks and preserves first identity binding, newer authoritative snapshots, same-session out-of-order prompts, and Gateway peer answers. Production LOC is +33/-27 (net +6); tests and fixture support are +179/-9. The small production increase supplies the missing ownership checks without adding a second lifecycle or configuration surface.

Each new regression was observed failing against pre-fix source. Seven focused owner/test-support files pass; the complete fake-backend real-PTY suite passes all 76 cases; the full changed-file gate passes; and independent Codex autoreview has no actionable findings. The newer main change in #130263 affects terminal acknowledgement normalization, not these picker/refresh/event ownership decisions; a synthetic merge with current main is conflict-free.

Two review-evidence clarifications: the bot's PTY run stopped at its 30-second command budget, not a failing product assertion; the complete local PTY run finished successfully. The reported vector/embedding persistence change is a false positive on synthetic transcript identity metadata in a test. There is no storage, schema, vector, or migration change in this PR. These are real-terminal/fake-backend and owner-boundary proofs, not authenticated provider or Gateway-transport proof; no screenshot or recording is attached.

Exact-head CI run 32991074340, attempt 2 is green. Its first attempt failed because core-unit-src-security-2 was killed with SIGKILL, with no assertion failure. Only failed jobs were retried on the unchanged head; the retried lane passed without a code change. The latest ClawSweeper review has no actionable defect, and its rank-up request for exact-head green required checks is satisfied. Proceeding through the native prepare/merge checks.

@steipete
steipete merged commit 67a9b34 into main Aug 26, 2026
533 of 618 checks passed
@steipete
steipete deleted the codex/tui-terminal-owner-qa branch August 26, 2026 20:11
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 27, 2026
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: M 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