Prefer xterm visible line for WebCRT command audit on Enter.

PTY stdout/stdin carry backspace and edit noise during typing; record only the final row the operator sees when pressing Enter.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-09-02 11:13:41 +08:00
parent 6ea14e6d5e
commit b35e56afb0
4 changed files with 42 additions and 18 deletions

View file

@ -502,6 +502,21 @@ def normalize_audit_line(line: str) -> str:
return s.replace("\r", "").rstrip()
def finalize_audit_line(line: str) -> str:
"""Apply echoed backspaces then normalize (PTY stdout fallback only)."""
s = _strip_ansi(str(line or ""))
out: list[str] = []
for ch in s:
if ch in ("\b", "\x7f"):
if out:
out.pop()
continue
if ord(ch) < 32 and ch != "\t":
continue
out.append(ch)
return normalize_audit_line("".join(out))
def _strip_ansi(text: str) -> str:
return re.sub(r"\x1b\[[0-9;?]*[A-Za-z]|\x1b\].*?\x07|\x1b.", "", str(text or ""))
@ -537,11 +552,11 @@ def extract_last_prompt_command(text: str) -> str | None:
tail = s.rsplit("\r", 1)[-1]
tail_line = tail.split("\n")[-1].rstrip()
if _is_prompt_command_line(tail_line):
return tail_line[:512]
return finalize_audit_line(tail_line)[:512]
for frag in reversed(re.split(r"[\r\n]+", s)):
line = frag.strip()
if line and _is_prompt_command_line(line):
return line[:512]
return finalize_audit_line(line)[:512]
return None

View file

@ -372,13 +372,14 @@ class WebcrtSession:
if "\r" in text or "\n" in text:
from .webcrt_channel import extract_last_prompt_command, normalize_audit_line
cmd = extract_last_prompt_command(self._stdout_tail) or self._last_prompt_line
if not cmd and audit_line:
# Ground truth: xterm visible row at Enter (frontend). PTY stdout/stdin
# still carry intermediate backspaces / tab redraws — do not prefer those.
if audit_line and str(audit_line).strip():
cmd = normalize_audit_line(audit_line)
if cmd and str(cmd).strip():
lines = [cmd]
else:
lines = buf_lines
raw = extract_last_prompt_command(self._stdout_tail) or self._last_prompt_line
cmd = raw if raw else (buf_lines[-1] if buf_lines else None)
lines = [cmd] if cmd and str(cmd).strip() else []
self._last_prompt_line = ""
else:
lines = []