Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(agentic-ide): keep a pane's terminal negotiation when its replay is cut

Ruben Lütkea9b6a1099787
diff --git a/jarvis/agentic_ide/transcript.py b/jarvis/agentic_ide/transcript.py
index eca551cdfa..62735c3f9d 100644
--- a/jarvis/agentic_ide/transcript.py
+++ b/jarvis/agentic_ide/transcript.py
@@ -22,6 +22,7 @@ box border by a column; and a two-column TUI layout still reads left-to-right
per row, exactly as it looks on screen. Both are irrelevant for answering "what
is this agent working on?", and both keep the module dependency-free.
"""
+
from __future__ import annotations
import re
@@ -49,6 +50,9 @@ _ANY_ESC_RE = re.compile(
# split may have fallen either side of it.
_CSI_TAIL_RE = re.compile(r"\[?[0-?]*[ -/]*[@-~]")
+#: ``ESC [ ? <params> h|l`` — how a CLI turns terminal features on and off.
+_PRIVATE_MODE_RE = re.compile(r"\x1b\[\?([0-9;]+)([hl])")
+
_DECORATION_CHARS = set("─│┌┐└┘├┤┬┴┼━┃╭╮╯╰═║╔╗╚╝╠╣╦╩╬▀▄█▌▐░▒▓▔▁·.-_=*#~ ")
_SPINNER_CHARS = set("|/\\-◐◓◑◒✻✽✢·✳✶⣿")
@@ -186,6 +190,25 @@ class ReplayBuffer:
Whoever replays a truncated buffer therefore has to ask the agent for a
fresh paint afterwards; :meth:`SessionRegistry.attach` does it with a
window-size change, the one request every TUI answers with a full redraw.
+
+ **The modes are kept even when the bytes that set them are gone.** A CLI
+ negotiates what kind of terminal it is talking to exactly ONCE, in its first
+ few hundred bytes — measured against Claude Code 2.1.220: ``ESC[?1049h``
+ (take the whole screen) and ``ESC[?1000h ESC[?1002h ESC[?1003h ESC[?1006h``
+ (send me the mouse). Those bytes fall out of a 128 KB tail within minutes of
+ an agent working, and every viewer that re-joined afterwards built a
+ terminal that did not know any of it: xterm stayed on the normal buffer and
+ kept the wheel to itself, while the agent went on believing it owned both.
+ A wheel turn then scrolled the viewer's own stale buffer — which the agent
+ overwrote on its next repaint, since it addresses the screen absolutely — so
+ scrolling a Claude Code pane did nothing at all, in the app, forever, while
+ a Codex pane (which negotiates neither) was unaffected. Reported four times
+ over as "the scrollbar only works in Codex"; the scrollbar was the symptom.
+
+ So every private mode the agent has set is remembered as it goes past, and a
+ truncated replay is prefixed with the ones still in force. Cheap — a regex
+ over each chunk, a dict of at most a dozen entries — and it puts the viewer
+ in the state the agent thinks it is in, which is the whole job of a replay.
"""
limit: int = REPLAY_LIMIT_CHARS
@@ -197,10 +220,15 @@ class ReplayBuffer:
#: Did the last dropped chunk stop inside an escape sequence? Then the rest
#: of that sequence is at the front of what remains.
_open_escape: bool = field(default=False, init=False)
+ #: Every private mode seen, in the order it was first negotiated, with the
+ #: value it was last given. Insertion order matters: a screen switch has to
+ #: be replayed before what was drawn on that screen.
+ _modes: dict[str, bool] = field(default_factory=dict, init=False)
def feed(self, chunk: str) -> None:
if not chunk:
return
+ self._note_modes(chunk)
self._chunks.append(chunk)
self._size += len(chunk)
while self._size > self.limit and len(self._chunks) > 1:
@@ -209,6 +237,23 @@ class ReplayBuffer:
self.truncated = True
self._open_escape = _ends_mid_escape(dropped)
+ def _note_modes(self, chunk: str) -> None:
+ """Remember what the agent has told the terminal it wants.
+
+ ``ESC[?1000;1006h`` sets several at once, so each parameter is recorded
+ on its own. Modes are kept whichever way they were set — a mode the
+ agent turned OFF is restored as off, because several of them (wraparound,
+ the cursor) are ON in a terminal that was just built.
+ """
+ for params, action in _PRIVATE_MODE_RE.findall(chunk):
+ for param in params.split(";"):
+ if param:
+ self._modes[param] = action == "h"
+
+ def _mode_prologue(self) -> str:
+ """The negotiation a truncated replay lost, re-stated in order."""
+ return "".join(f"\x1b[?{mode}{'h' if on else 'l'}" for mode, on in self._modes.items())
+
def text(self) -> str:
"""Everything kept, oldest first — ready to be written to a terminal.
@@ -218,6 +263,11 @@ class ReplayBuffer:
in the middle of the agent's interface — so that remnant is skipped, and
an attribute reset makes the starting colour deterministic rather than
inherited from bytes that are gone.
+
+ The repair also re-states the modes the agent negotiated (see the class
+ docstring): they are set once at startup and are the first thing a long
+ tail loses, and a viewer that never heard them is a terminal the agent
+ cannot reach with the mouse.
"""
text = "".join(self._chunks)
if not self.truncated or not text:
@@ -226,7 +276,7 @@ class ReplayBuffer:
remnant = _CSI_TAIL_RE.match(text)
if remnant is not None:
text = text[remnant.end() :]
- return "\x1b[0m" + text
+ return self._mode_prologue() + "\x1b[0m" + text
def clear(self) -> None:
self._chunks.clear()
diff --git a/tests/unit/agentic_ide/test_transcript.py b/tests/unit/agentic_ide/test_transcript.py
index 544d3fe9e5..f050c1f2a9 100644
--- a/tests/unit/agentic_ide/test_transcript.py
+++ b/tests/unit/agentic_ide/test_transcript.py
@@ -212,3 +212,62 @@ def test_a_truncated_replay_alone_cannot_rebuild_the_screen() -> None:
"followed by a repaint rather than trusted on its own"
)
assert buffer.truncated is True
+
+
+def test_a_truncated_replay_still_says_who_owns_the_screen() -> None:
+ """The bug behind four rounds of "scrolling is broken in Claude Code panes".
+
+ A CLI negotiates what kind of terminal it is talking to exactly once, in its
+ first few hundred bytes. Measured against Claude Code 2.1.220: take the
+ whole screen (``?1049h``) and send me the mouse (``?1000/1002/1003/1006h``).
+ Those bytes leave a 128 KB tail within minutes, and every viewer that
+ re-joined afterwards got a terminal that had never heard them — so it kept
+ the wheel to itself and scrolled its own stale buffer, which the agent
+ overwrote on its next repaint. Nothing moved, ever, in the app.
+ """
+ start = "\x1b[?1049h\x1b[?1000h\x1b[?1002h\x1b[?1003h\x1b[?1006h\x1b[?25lhello"
+ buffer = ReplayBuffer(limit=64)
+ buffer.feed(start)
+ buffer.feed("x" * 200)
+
+ replay = buffer.text()
+
+ assert buffer.truncated is True
+ for mode in ("?1049h", "?1000h", "?1002h", "?1003h", "?1006h", "?25l"):
+ assert f"\x1b[{mode}" in replay, f"{mode} must survive the truncation"
+ # And before the content, or the screen switch would wipe what it restored.
+ assert replay.index("\x1b[?1049h") < replay.index("x" * 20)
+
+
+def test_a_mode_the_agent_turned_off_comes_back_off() -> None:
+ """Several private modes are ON in a terminal that was just built.
+
+ Wraparound and the cursor among them — restoring only what was switched on
+ would hand the viewer a cursor the agent had deliberately hidden.
+ """
+ buffer = ReplayBuffer(limit=32)
+ buffer.feed("\x1b[?7h\x1b[?25h\x1b[?7l\x1b[?25l")
+ buffer.feed("y" * 100)
+
+ replay = buffer.text()
+
+ assert "\x1b[?7l" in replay and "\x1b[?25l" in replay
+ assert "\x1b[?7h" not in replay and "\x1b[?25h" not in replay
+
+
+def test_an_untruncated_replay_is_still_verbatim() -> None:
+ """Nothing is prepended while the original negotiation is still in there."""
+ stream = "\x1b[?1049h\x1b[?1006hhello"
+ buffer = ReplayBuffer(limit=1024)
+ buffer.feed(stream)
+
+ assert buffer.text() == stream
+
+
+def test_a_replay_forgets_the_modes_when_it_is_cleared() -> None:
+ buffer = ReplayBuffer(limit=32)
+ buffer.feed("\x1b[?1049h" + "z" * 100)
+ buffer.clear()
+ buffer.feed("fresh")
+
+ assert buffer.text() == "fresh"