Skip to content

fix(tui): bound run state when terminal events are missing - #109492

Closed
ZengWen-DT wants to merge 2 commits into
openclaw:mainfrom
ZengWen-DT:fix/tui-bound-session-run-map
Closed

ZengWen-DT wants to merge 2 commits into
openclaw:mainfrom
ZengWen-DT:fix/tui-bound-session-run-map

Conversation

@ZengWen-DT

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where a long-running TUI session could retain streamed run state indefinitely when a run emitted deltas but its terminal event never arrived, such as after a transport interruption.

Why This Change Was Made

The stream assembler now keeps at most 200 run states and refreshes insertion order for recently active runs, matching the existing TUI run-bookkeeping ceiling. Normal final, aborted, and error cleanup is unchanged; the bounded fallback only matters when terminal cleanup is missing. The risk is limited to very old orphaned stream state being unavailable if a terminal event arrives after more than 200 other tracked runs.

User Impact

TUI sessions no longer accumulate unbounded in-memory stream state after missing terminal events, while current and recently active streamed responses continue to assemble normally.

Evidence

AI-assisted change; I reviewed the lifecycle paths and ran the following evidence locally on Linux with Node 24.18.0.

Real behavior proof used the same script against two isolated worktrees at latest upstream/main (0a19e1ed586) and the fixed head. The script imported the real exported TuiStreamAssembler, ingested 201 distinct run IDs without terminal events, refreshed the oldest active run, and inspected the production object's runtime map only to report its size. No transport or assembler logic was mocked.

Before, latest main retained all 201 entries:

{"trackedRunCount":201,"recentRun":"Recently active","evictedCandidate":"Draft 1","newestRun":"Newest"}

After, the fixed head retained 200 entries, preserved the recently active and newest runs, and evicted the oldest orphan:

{"trackedRunCount":200,"recentRun":"Recently active","evictedCandidate":"(no output)","newestRun":"Newest"}

Focused regression test:

$ node scripts/run-vitest.mjs src/tui/tui-stream-assembler.test.ts
Test Files  1 passed (1)
Tests  14 passed (14)

Static checks:

$ node scripts/run-oxlint.mjs src/tui/tui-stream-assembler.ts src/tui/tui-stream-assembler.test.ts
$ pnpm tsgo:core
$ pnpm tsgo:core:test
$ oxfmt --check src/tui/tui-stream-assembler.ts src/tui/tui-stream-assembler.test.ts
All matched files use the correct format.
$ git diff --check upstream/main..HEAD

A live Gateway disconnect was not forced; the direct production-component run exercises the exact state-retention boundary before transport-specific handling. Full repository build and test suites were not run locally and are left to CI.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P2 Normal backlog priority with limited blast radius. labels Jul 17, 2026
@clawsweeper

clawsweeper Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 17, 2026, 1:22 AM ET / 05:22 UTC.

Summary
Bounds TUI stream-assembly state to 200 recently active runs and adds regression coverage for eviction and late finalization.

PR surface: Source +17, Tests +32. Total +49 across 2 files.

Reproducibility: yes. from source and submitted live output: ingesting 201 distinct run IDs without terminal events leaves 201 retained states on main, while the PR bounds the same production component to 200.

Review metrics: none identified.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • none.

Next step before merge

  • No automated repair remains; the prior finding is fixed, so the PR only needs ordinary maintainer review and any routine base refresh required for landing.

Security
Cleared: The two-file TUI state-management patch adds no dependency, secret, permission, workflow, artifact-download, or supply-chain surface.

Review details

Best possible solution:

Land the bounded recently-active run map after routine maintainer review, retaining terminal-event cleanup and the untracked late-finalization path exactly as covered by the regression tests.

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

Yes, from source and submitted live output: ingesting 201 distinct run IDs without terminal events leaves 201 retained states on main, while the PR bounds the same production component to 200.

Is this the best way to solve the issue?

Yes. Bounding state inside TuiStreamAssembler is the narrow owner-boundary fix, and refreshing insertion order preserves active runs while matching the TUI's existing 200-run bookkeeping ceiling.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 44b79d44ae13.

Label changes

Label justifications:

  • P2: This is a focused TUI reliability fix for potentially unbounded in-memory state with limited runtime blast radius.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR provides before-and-after live output from the real exported TuiStreamAssembler showing the unbounded main behavior and the corrected eviction behavior without mocked production logic.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR provides before-and-after live output from the real exported TuiStreamAssembler showing the unbounded main behavior and the corrected eviction behavior without mocked production logic.
Evidence reviewed

PR surface:

Source +17, Tests +32. Total +49 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 28 11 +17
Tests 1 32 0 +32
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 60 11 +49

What I checked:

  • Current-main behavior: At current main, TuiStreamAssembler stores per-run streaming state in an unbounded Map and creates entries during delta ingestion; ordinary final, abort, error, drop, and session-switch cleanup do not cover terminal events that never arrive. (src/tui/tui-stream-assembler.ts:112, 44b79d44ae13)
  • Bounded implementation: The PR head refreshes insertion order for active runs, prunes only newly tracked delta state to 200 entries, and uses an untracked empty state when finalizing an already-evicted run. (src/tui/tui-stream-assembler.ts:112, 2d2ae7c77093)
  • Caller lifecycle: TUI chat deltas enter the assembler through ingestDelta, while final, aborted, and error events finalize or drop run state; separate session/finalized run maps already use a 200-entry bookkeeping ceiling. (src/tui/tui-event-handlers.ts, 44b79d44ae13)
  • Regression coverage: The added tests prove recently active and newest runs survive pruning, the oldest orphan is evicted, and late finalization of an evicted ID does not evict another tracked run. (src/tui/tui-stream-assembler.test.ts:128, 2d2ae7c77093)
  • Prior finding resolved: The first review found that finalizing an evicted ID could create and prune another tracked entry; commit 2d2ae7c changed finalization to temporary untracked state and added a targeted regression test. (src/tui/tui-stream-assembler.ts:139, 2d2ae7c77093)
  • Real behavior proof: The PR body records the same production-component script retaining 201 entries on main and 200 on the fixed head, while preserving the refreshed and newest runs and evicting the oldest orphan; no transport or assembler logic was mocked. (src/tui/tui-stream-assembler.ts:128, 2d2ae7c77093)

Likely related people:

  • steipete: Merged and refined the current TUI stream-assembler finalization behavior, including the narrow boundary-drop guard and its focused test gate. (role: merger and recent area contributor; confidence: high; commits: 5eb53d1, b01273c; files: src/tui/tui-stream-assembler.ts, src/tui/tui-stream-assembler.test.ts)
  • scz2011: Authored the merged change that established the assembler's current streamed-text finalization and boundary-preservation behavior. (role: introduced behavior; confidence: medium; commits: 82bceb4, 5eb53d1; files: src/tui/tui-stream-assembler.ts, src/tui/tui-stream-assembler.test.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.
Review history (2 earlier review cycles)
  • reviewed 2026-07-17T01:28:05.431Z sha 7b7433f :: needs changes before merge. :: [P2] Avoid pruning another run while finalizing an evicted ID
  • reviewed 2026-07-17T04:40:19.528Z sha 2d2ae7c :: needs maintainer review before merge. :: none

Use untracked temporary state when finalize() handles a run id that is
no longer tracked, so capacity pruning only happens during delta ingest.

Co-authored-by: Cursor <[email protected]>
@ZengWen-DT

Copy link
Copy Markdown
Contributor Author

Addressed ClawSweeper P2: finalize() now uses untracked temporary state for run ids that are no longer in the map, so capacity pruning only runs during ingestDelta(). Added regression test for late finalization of an evicted run id.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added 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. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jul 17, 2026
steipete added a commit that referenced this pull request Jul 27, 2026
Fix cross-client terminal history synchronization without letting stale snapshots erase in-flight responses. Bound orphaned stream assembly, preserve session and agent ownership, and cover real Gateway delivery, persistence races, reconnects, and stress bursts.

Closes #38829.

Credits prior issue investigation in #96252 and bounded-stream work in #109492.

Co-authored-by: Harjoth Khara <[email protected]>
Co-authored-by: Wynne668 <[email protected]>
@steipete

Copy link
Copy Markdown
Contributor

Thank you for identifying the unbounded TUI stream-run state. This is now fixed on main by #114443 (ac6009e), which uses the shared bounded-map helper, retains at most 200 recently active runs, handles late finals safely, and includes a 2,000-run regression. Your investigation is credited in the merged PR and you are recorded as a commit co-author. Closing this PR as superseded by the landed fix.

@steipete steipete closed this Jul 27, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 28, 2026
…114443)

Fix cross-client terminal history synchronization without letting stale snapshots erase in-flight responses. Bound orphaned stream assembly, preserve session and agent ownership, and cover real Gateway delivery, persistence races, reconnects, and stress bursts.

Closes openclaw#38829.

Credits prior issue investigation in openclaw#96252 and bounded-stream work in openclaw#109492.

Co-authored-by: Harjoth Khara <[email protected]>
Co-authored-by: Wynne668 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. 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.

2 participants