perf: 桥变化才写、stdin 优先、stream 50ms 轮询

1. 桥写放大:旧代码每 ~50ms 轮询都无条件重写 state 和帧
   文件(约 40 次原子 temp+replace/s 走 runsc gofer,
   泵持久返回同一帧、updated_at 每轮都变,字节其实不变)。
   现帧字节不变不写;state 只在 url/title/started/holder/
   streaming 变化时写,或 0.5s 心跳一次(判死阈值 30s,
   绰绰有余);静态页写量从 ~40次/s 降到 ~2次/s。
2. MCP 主循环 stdin 优先:旧循环先 drain 再读 stdin,
   每次工具调用都先付一轮文件 IO。现 pending 请求先处理,
   空闲轮询 drain,忙时 200ms 保底 drain 一次防饿死。
3. stream 轮询 200ms → 50ms,画面延迟下限随之下降。
附回归测试:无变化轮询不重写、帧变化立即写、holder
变化绕过节流立即写。
This commit is contained in:
host committed 2026-10-30 13:36:24 +08:00
1 parent e9370d7291
commit 910168126d
4 files changed
+93 -21

No files matched your search

+10 -1
View File
@@ -430,14 +430,23 @@ def main():
if _get_bridge(browser) is not None:
browser.set_frame_sink(_frame_queue())
reader = _LineReader(sys.stdin.fileno())
last_drain = 0.0
while True:
_drain_remote(browser)
lines, drained = reader.poll()
for line in lines:
line = line.strip()
if not line:
continue
_handle_line(line, browser)
# Pending requests go first: a tool call used to pay a
# full drain round of file I/O before executing. Drain
# on idle iterations as before, plus a 200ms floor while
# busy so a request burst can never starve remote input
# (takeover/click) or the state heartbeat for long.
now = time.time()
if not lines or now - last_drain >= 0.2:
_drain_remote(browser)
last_drain = now
if drained:
break
if not lines: