Xterm handle replacement can dispose queued viewport work
Bug
Replacing a retained terminal's live xterm handle can dispose the old terminal while xterm still has queued viewport work for that instance.
The queued callback later reaches Viewport.syncScrollArea through the disposed terminal and throws in the renderer. This is most visible during same-session handle/generation replacement after agent switching, but the lifetime bug is provider-independent.
Expected behavior
AO should detach its listeners and transport synchronously, while deferring only the old terminal object's final dispose() call until xterm's already-queued viewport work has drained.
A replacement must remain immediate from AO's routing and input-ownership perspective; this fix must not keep the old transport writable for another tick.
Proposed focused fix
- Detach AO listeners and transport immediately.
- Schedule only the final old-terminal disposal on the next macrotask.
- Cover exact active-handle replacement with real xterm in the retained-terminal Playwright suite.
The prepared patch is limited to XtermTerminal.tsx and one focused regression in terminal-viewport-retention.spec.ts.
Validation prepared
- Frontend typecheck
- E2E typecheck
- 49 focused
XtermTerminalVitest tests - Exact real-xterm Playwright handle-replacement regression
git diff --check
This is a small follow-up to the retained-terminal work in #3371.
I'd like to work on this. I opened draft PR #3804 before waiting for assignment, which did not follow CONTRIBUTING.md's expected order. I will not request review or continue upstream implementation unless maintainers assign this issue and confirm the scope. If you prefer, I will close the draft.
Hey @maplenk , thank you for raising this. Would like to connect with you to understand more about this issue. I am a core contributor in the project, could we connect on discord. Here is the link to the server and my profile: Server: https://discord.gg/JuaXT7SVh My username: illegalcall
Thanks for contributing to Agent Orchestrator.
This issue is being picked up by the current external contributor on-call pair:
- @neversettle17-101
- @illegalcall
If someone is already working on this, please continue as usual. The on-call pair is added for visibility, tracking, and support, not to take over the work. If you need help with review, direction, reproduction, or next steps, please tag @neversettle17-101 and @illegalcall here.
For faster context or live questions, you can also join the AO Discord.
Join the session here: https://discord.gg/H6ZDcUXmq
Come by if you want to see what is being built, ask questions, or just hang around with the community.
Hey @somewherelostt
The PR is already created.
https://github.com/Untrivial-ai/agent-orchestrator/pull/3804
Closing as fixed on current main by #5105. The terminal cleanup now detaches AO state synchronously but defers the old xterm object’s final dispose() to the next task, allowing queued viewport initialization work to drain; focused lifecycle coverage was added with the change.