Skip to content

feat(runtime): RFC - interactive PTY and stdin for background Bash #775

Closed

Description

@M4n5ter
English

Background

#486 established a runtime-owned lifecycle for background ShellRuns and intentionally left PTY and interactive stdin for a follow-up. Since then, #762 / #764 have also made foreground versus background execution explicit: ordinary Bash remains foreground, while run_in_background: true creates a runtime resource that can be observed through Read(ref) and stopped through StopBackgroundTask({ ref }).

That contract works well for pipe-oriented tasks such as development servers, watchers, and benchmarks. It still cannot represent commands that require terminal semantics or later input, including:

  • REPLs, debuggers, and interactive setup commands;
  • CLIs that change behavior when stdin/stdout is not a TTY;
  • terminal programs that clear, redraw, or use an alternate screen;
  • confirmation flows that span multiple model turns;
  • commands that need resize, Enter, Ctrl-C, or Ctrl-D after startup.

Problem

Pipe mode can provide separate stdout/stderr tails, but it has no terminal screen and no runtime-owned stdin control path. Shell-level workarounds such as script, expect, tmux, or hand-written FIFOs can solve individual tasks, but they make the runtime lose unified ownership of input ordering, screen state, cleanup, redaction, and recovery.

The missing capability is not a second shell lifecycle or a prematurely generalized background-task protocol. It is a PTY-capable implementation of the existing ShellRun lifecycle, plus the smallest PTY-specific control surface.

Scenario priorities and route choice

Two scenarios are core to the first slice:

  1. line-oriented interaction such as REPLs, prompts, debuggers, and setup flows;
  2. truthful textual state after clear, redraw, cursor movement, wrapping, and alternate-screen transitions.

A human-grade terminal attachment, styling fidelity, mouse input, and complete support for every terminal extension are not core requirements.

That second scenario determines the output route. A bounded raw-byte tail is simpler, but it cannot reliably reconstruct what is currently visible after destructive terminal operations. tmux capture-pane can provide current screen state, but it introduces an external session server, splits lifecycle ownership, and does not provide a dependable native Windows/ConPTY path. An in-process parser built around node-pty and @xterm/headless keeps process, parser, persistence, redaction, and cleanup under the existing ShellRun owner on both POSIX and Windows. The accepted cost is more implementation and a fail-closed parser boundary. Resize is useful, but it is not the reason this route was selected.

A concrete resize case is a debugging TUI such as gdb -tui. At 80x24, its source, assembly, register, or command windows may be cramped or force useful context off-screen. Resizing the running PTY to 120x40 lets the program receive the terminal-size change and redraw whichever TUI windows are active, while the parser moves to the same geometry before the next snapshot. For a line-oriented REPL, resize usually only improves wrapping; for a TUI, it can change which debugging state is visible at all.

Proposed direction

The following is a low-regret initial shape for discussion, not an attempt to lock the final design in advance.

Keep Bash as the only start path

Extend the existing explicit background schema with optional PTY support:

Bash({
  command: string;
  timeout_ms?: number;
  run_in_background?: boolean;
  pty?: boolean;
})
  • pty is valid only with run_in_background: true.
  • Omitting pty preserves the existing foreground/background pipe behavior and stdout/stderr semantics.
  • PTY Bash returns the same maka://runtime/background-tasks/<id> ref shape as a pipe ShellRun.
  • Read(ref) remains the observation path and generic StopBackgroundTask({ ref }) remains the termination path.
  • The runtime does not infer PTY use from command text, duration, or likely isatty requirements.

This preserves the existing background-task resource boundary without adding PTY-specific status, wait, or stop tools.

Add a capability-specific WriteStdin tool

A candidate schema is:

WriteStdin({
  ref: string;
  input?: string;
  size?: {
    cols: number;
    rows: number;
  };
})

Proposed semantics:

  • input and/or size is required.
  • Input is sent exactly as provided; no newline is added implicitly.
  • \r can express Enter; the U+0003 and U+0004 control characters express Ctrl-C and Ctrl-D.
  • When resize and input are supplied together, resize commits before input.
  • The tool accepts only a controllable PTY ShellRun ref; pipe mode does not gain stdin as a side effect.
  • The operation is permission-gated and returns an updated, persisted ShellRun snapshot.
  • The returned snapshot is the terminal state at the parser cut immediately after the control commit. It does not wait for or causally attribute later output; use Read(ref) to observe what arrives afterward.

WriteStdin is intentionally capability-specific. Stdin and terminal resize are not operations every background task can support, while termination already belongs to generic StopBackgroundTask. Generalizing both into one background-task control schema now would spread PTY assumptions into future task types.

PTY output model

PTY output is better represented as terminal state than as raw ANSI, an append-only log, or synthetic stdout/stderr streams.

The current direction is to:

  • process PTY bytes through a real terminal parser;
  • expose bounded screen/scrollback, cursor, dimensions, status, and revision through the existing ShellRun result;
  • treat clear/redraw and alternate-screen transitions as replacement of prior visual state rather than appended history;
  • fail closed on parser failure instead of falling back to raw ANSI;
  • redact all durable or model-visible output before publication;
  • prevent terminal control sequences from triggering host clipboard, notification, title, link, window, or bell side effects;
  • keep bounded terminal state instead of introducing an unlimited transcript.

This lets Read(ref) answer the question that matters for an interactive program: what is currently visible on the terminal?

Lifecycle and ordering

The PTY path should preserve the existing ShellRun ownership boundary:

  • a durable ShellRun record exists before its ref is returned;
  • one runtime manager owns spawn, live handles, persistence, timeout, stop, shutdown, and finalization;
  • input and resize for the same ref commit in FIFO order;
  • a Read(ref) parser cut includes controls committed before that cut;
  • Stop closes later control admission, terminates the process tree, drains final output, and then persists terminal state;
  • timeout, user cancellation, session close, and host shutdown reuse the same termination lifecycle;
  • after restart, a running record without a live native handle becomes orphaned; Maka does not reattach a PID;
  • native PTYs, terminal buffers, and protocol replies have explicit resource bounds.

The exact synchronization mechanism is an implementation detail, but these observable guarantees matter. Without them, concurrent WriteStdin, Read, Stop, parser callbacks, and process exit can publish stale or contradictory snapshots.

Product scope

The first slice could target the main Desktop and TUI agents, where the runtime already owns background ShellRuns.

The human-facing projection should remain restrained:

  • the parent Bash activity remains the task's single terminal surface;
  • background revisions update that surface instead of creating another task card;
  • WriteStdin appears only as a lightweight operation summary with a bounded, redacted, control-escaped input preview;
  • runtime-resource Read results may fold into the parent Bash activity;
  • no new background-task list or task center is required for the first slice.

Headless and child-agent tool sets should omit PTY and WriteStdin until those hosts have real permission, isolation, lifecycle, and packaging contracts for them.

Security boundary

WriteStdin should not be presented as a secure password-entry channel. Input must reach the child process unchanged. Canonical audit and replay retain the exact model-generated args, including denied calls; permission governs execution, while permission prompts and human surfaces use a bounded redacted projection. Turn-level permission memory is scoped to the complete, untruncated ref + input + size side effect, so a resize or benign input approval cannot authorize unrelated input on the same PTY. Output redaction protects published terminal snapshots; it does not make raw input secret-safe.

A future human attach or masked-input flow would need a separate design for its display, permission, and audit guarantees.

Stable contract and internal limits

Limits that affect valid model calls or cross-host observable behavior belong to the stable contract: live PTY admission capacity, terminal-size bounds, and bounded returned/model-visible terminal state. Parser watermarks, collector batching, flush cadence, and similar tuning remain implementation details unless they become externally observable. Hard internal safety budgets still fail closed, but do not become tool-schema knobs solely because the implementation needs them.

Acceptance criteria

  • Bash behavior changes only when background execution and PTY are both selected explicitly.
  • PTY Bash provides a real TTY and a documented initial terminal size.
  • WriteStdin supports exact Unicode input, control characters, and full resize without implicit newline insertion.
  • A running terminal UI can receive a resize and redraw into matching child/parser geometry.
  • Resize-before-input and same-ref FIFO ordering are deterministic.
  • Read(ref) returns a bounded, redacted terminal snapshot with monotonic revision and task status.
  • Clear/redraw, soft wrapping, Unicode cell widths, cursor state, and alternate-screen transitions produce truthful screen state.
  • Stop, timeout, user cancellation, session close, and host shutdown converge on one terminal result and clean up the managed process tree.
  • Parser or persistence failure fails closed; raw ANSI is never used as a degraded result.
  • Desktop and TUI show one coherent Bash/terminal activity; WriteStdin input appears only through the bounded redacted preview.
  • Headless and child agents do not advertise controls they cannot execute.
  • Core PTY behavior is validated with real native PTY and terminal-parser integration tests rather than a fake process state machine.

Remaining review questions

  1. Is Bash({ run_in_background: true, pty: true }) plus WriteStdin the right minimal tool surface? Is there a smaller capability-specific shape that still expresses exact input and resize?

  2. Is a bounded terminal screen/scrollback snapshot the right Read(ref) representation, or should the model receive a different terminal projection?

  3. Should the first slice target both Desktop and TUI while explicitly omitting Headless and child agents?

  4. Does the selected node-pty plus @xterm/headless route fit Maka's future packaging boundary, especially for native Windows/ConPTY distribution?

  5. Is the boundary above correct between model-visible/cross-host limits and internal parser or persistence tuning?

Out of scope for the first slice

  • Human direct attach or a full embedded terminal UI
  • Secure or masked password entry
  • Pipe-mode stdin
  • Automatic PTY detection or making PTY the default
  • Headless, child-agent, automation, or remote PTY control
  • Recovering or reattaching the native process after host restart
  • Unlimited terminal transcripts or full-output artifacts
  • A new task list, task center, or shell-specific status/wait/stop tool family
简体中文

背景

#486 建立了由 runtime 持有的后台 ShellRun 生命周期,并有意把 PTY 和交互式 stdin 留到后续。此后 #762 / #764 又把前台与后台意图改为显式选择:普通 Bash 保持前台执行,run_in_background: true 创建可通过 Read(ref) 观察、通过 StopBackgroundTask({ ref }) 停止的后台 runtime resource。

这套契约已经适合开发服务器、watcher、benchmark 等 pipe 型任务,但仍无法处理需要真实终端语义或后续输入的命令,例如:

  • REPL、调试器和交互式初始化程序;
  • 会根据 stdin/stdout 是否为 TTY 改变行为的 CLI;
  • 会清屏、重绘或使用 alternate screen 的终端程序;
  • 跨多个模型 turn 的确认提示;
  • 启动后需要 resize、Enter、Ctrl-C 或 Ctrl-D 的命令。

问题

pipe 模式可以提供独立的 stdout/stderr tail,但没有终端屏幕,也没有 runtime 持有的 stdin 控制路径。script、expect、tmux、FIFO 等 shell 技巧可以解决单个任务,却会让 runtime 失去对输入顺序、屏幕状态、清理、脱敏和恢复的统一所有权。

这里缺少的不是第二套 shell 生命周期,也不是一个预先泛化的后台任务控制协议,而是现有 ShellRun 生命周期的 PTY 实现,以及 PTY 特有的最小控制面。

场景优先级与路线选择

首个范围有两个核心场景:

  1. REPL、确认提示、调试器和初始化流程等逐行交互;
  2. 清屏、重绘、光标移动、换行和 alternate-screen 切换后,仍能提供真实的文本画面。

面向人类的完整终端 attach、样式还原、鼠标输入,以及对所有终端扩展的完全支持,不属于核心要求。

第二项决定了输出路线。有界 raw-byte tail 更简单,但无法在破坏性终端操作后可靠重建“当前实际显示什么”。tmux capture-pane 能提供当前画面,却会引入外部 session server、拆分生命周期所有权,也无法为原生 Windows/ConPTY 提供可靠路径。基于 node-pty 与 @xterm/headless 的进程内 parser,则让 POSIX 与 Windows 上的进程、parser、持久化、脱敏和清理继续由现有 ShellRun 所有者统一持有。代价是更大的实现量和 fail-closed parser 边界;当前方向明确接受这项代价。resize 很有用,但不是选择这条路线的原因。

一个具体的 resize 场景是 gdb -tui 这类调试 TUI。在 80x24 下,源码、汇编、寄存器或命令窗口可能过于拥挤,或者把有用上下文挤出画面。把仍在运行的 PTY 调整到 120x40 后,程序会收到终端尺寸变化,并重绘当前启用的 TUI 窗口;parser 也会在下一次 snapshot 前切换到相同几何尺寸。对逐行 REPL 来说,resize 通常只是改善折行;对 TUI 来说,它可能直接改变哪些调试状态能够同时显示。

当前建议方向

以下只是目前看来较低悔的初始方案,主要用于讨论,并不试图预先锁定最终设计。

继续让 Bash 成为唯一启动入口

在现有显式后台 schema 上增加可选 PTY:

Bash({
  command: string;
  timeout_ms?: number;
  run_in_background?: boolean;
  pty?: boolean;
})
  • pty 仅在 run_in_background: true 时合法;
  • 省略 pty 时,现有前台/后台 pipe 行为和 stdout/stderr 语义完全不变;
  • PTY Bash 返回与 pipe ShellRun 相同的 maka://runtime/background-tasks/<id> ref;
  • 继续通过 Read(ref) 观察,通过通用 StopBackgroundTask({ ref }) 终止;
  • 不根据命令内容、运行时长或 isatty 需求自动猜测是否使用 PTY。

这样无需增加 PTY 专用的 status、wait 或 stop 工具,也不会改变已经确定的后台任务资源边界。

增加能力专属的 WriteStdin

候选 schema:

WriteStdin({
  ref: string;
  input?: string;
  size?: {
    cols: number;
    rows: number;
  };
})

建议语义:

  • input 和 size 至少提供一个;
  • input 按原样发送,不隐式追加换行;
  • \r 可表达 Enter;U+0003 与 U+0004 控制字符分别表达 Ctrl-C 和 Ctrl-D;
  • 同时提供 resize 和 input 时,先提交 resize,再提交 input;
  • 只接受仍可控制的 PTY ShellRun ref,不顺带为 pipe 模式开放 stdin;
  • 工具需要权限,并返回一次已持久化的最新 ShellRun snapshot。
  • 返回 snapshot 是 control commit 后即时 parser cut 上的终端状态;它不等待后续输出,也不把后续输出归因于本次输入。之后到达的内容通过 Read(ref) 观察。

WriteStdin 有意保持能力专属。stdin 与终端 resize 并不是所有后台任务天然共有的操作,而停止任务已经由通用 StopBackgroundTask 承担。现在就把两者强行放进一个通用 background-task control schema,反而会把 PTY 假设扩散到未来任务类型。

PTY 输出模型

PTY 输出更适合表示为终端状态,而不是 raw ANSI、追加日志或人为拆分的 stdout/stderr。

目前建议:

  • 使用真实终端 parser 处理 PTY bytes;
  • 通过现有 ShellRun result 暴露有界的 screen/scrollback、cursor、尺寸、状态和 revision;
  • 清屏、重绘和 alternate-screen 切换应替换旧画面,而不是把旧帧继续当作日志追加;
  • parser 失败时 fail closed,不回退为 raw ANSI;
  • 所有持久化或模型可见输出先完成脱敏;
  • 终端控制序列不能触发宿主剪贴板、通知、标题、链接、窗口或响铃等 side effect;
  • 不为 PTY 引入无限 transcript。

这样 Read(ref) 回答的是交互程序真正需要的问题:终端现在显示什么。

生命周期与顺序

PTY 路径应继续遵守现有 ShellRun 的所有权边界:

  • ref 返回前已经存在 durable ShellRun record;
  • 单一 runtime manager 持有 spawn、live handle、持久化、timeout、stop、shutdown 和 finalization;
  • 同一 ref 的 input/resize 按 FIFO 提交;
  • Read(ref) 观察到的 parser cut 包含该 cut 之前已提交的控制操作;
  • Stop 关闭后续控制入口,终止进程树,完成 final drain 后再持久化终态;
  • timeout、用户取消、session close 和 host shutdown 复用同一终止生命周期;
  • 重启后不重新 attach PID;缺少 live handle 的 running record 保守地转为 orphaned;
  • native PTY、终端缓冲和 protocol reply 都有明确资源上限。

具体同步机制属于实现细节,但这些可观察保证不能省略。否则并发 WriteStdin、Read、Stop、parser callback 与 process exit 很容易发布陈旧或互相矛盾的 snapshot。

产品范围

首个范围建议覆盖 main Desktop 和 TUI agent,因为这两个宿主已经持有后台 ShellRun 生命周期。

人类界面保持克制:

  • 父 Bash activity 仍是该任务唯一的终端面板;
  • 后台 revision 更新同一面板,不创建第二张任务卡;
  • WriteStdin 只显示轻量操作摘要,以及有界、脱敏并已转义控制字符的输入预览;
  • runtime-resource Read 可以折叠回父 Bash activity;
  • 不要求同时建设新的后台任务列表或 task center。

Headless 和 child agent 在拥有真实的权限、隔离、生命周期及打包契约前,不应暴露 PTY 或 WriteStdin。

安全边界

WriteStdin 不应被描述为安全密码输入通道。输入必须原样交给子进程。Canonical 审计与 replay 保留模型生成的精确参数,包括被拒绝的调用;权限决定是否执行,而权限框和人类界面使用有界脱敏投影。回合级权限记忆按完整且不截断的 ref + input + size 副作用区分,因此一次 resize 或无害输入授权不能放行同一 PTY 上无关的后续输入。输出脱敏只保护发布后的终端 snapshot,并不会让 raw input 自动变成 secret-safe。

未来若要支持人工 attach 或 masked input,需要单独设计其显示、权限和审计边界。

稳定契约与内部上限

影响模型调用是否合法或跨宿主可观察行为的限制属于稳定契约:live PTY admission 容量、终端尺寸边界,以及有界的返回/模型可见终端状态。Parser 水位、collector 批处理、刷新节奏等调优仍是实现细节,除非它们变成外部可观察行为。内部 hard safety budget 仍然 fail closed,但不会仅因为实现需要就变成 tool schema 参数。

验收标准

  • 只有显式后台执行并选择 PTY 时,Bash 行为才发生变化;
  • PTY Bash 提供真实 TTY,并使用明确的初始终端尺寸;
  • WriteStdin 支持精确 Unicode、控制字符和完整 resize,不隐式添加换行;
  • 运行中的终端 UI 能接收 resize,并按与 child/parser 一致的几何尺寸重绘;
  • resize-before-input 与同 ref FIFO 顺序可证明;
  • Read(ref) 返回有界、已脱敏、revision 单调的终端 snapshot 和任务状态;
  • 清屏/重绘、soft wrap、Unicode cell width、cursor 和 alternate screen 都能得到真实画面;
  • Stop、timeout、用户取消、session close 与 host shutdown 都收束到唯一终态并清理受管进程树;
  • parser 或持久化失败时 fail closed,永不降级为 raw ANSI;
  • Desktop 与 TUI 只显示一个连贯的 Bash/terminal activity;WriteStdin 输入只通过有界脱敏预览呈现;
  • Headless 与 child agent 不广告无法执行的控制能力;
  • 核心 PTY 行为以真实 native PTY 和 terminal parser 集成测试验证,而不是用 fake process state machine 制造覆盖率。

剩余审查问题

  1. Bash({ run_in_background: true, pty: true }) 加 WriteStdin 是否是合适的最小工具面?是否存在更小、但仍能表达精确 input 与 resize 的能力专属设计?

  2. 有界的 terminal screen/scrollback snapshot 是否是合适的 Read(ref) 表示,还是模型应看到另一种终端投影?

  3. 首个范围是否应同时覆盖 Desktop 与 TUI,并明确排除 Headless 与 child agent?

  4. 当前选择的 node-pty 与 @xterm/headless 路线是否符合 Maka 未来的打包边界,尤其是原生 Windows/ConPTY 分发?

  5. 上述模型可见/跨宿主限制与 parser、持久化内部调优之间的边界是否合适?

首个范围不包含

  • 人工直接 attach 或完整嵌入式终端 UI;
  • 安全/遮罩密码输入;
  • pipe-mode stdin;
  • 自动 PTY 检测或把 PTY 设为默认;
  • Headless、child-agent、automation 或 remote PTY control;
  • host 重启后恢复或重新 attach native process;
  • 无限 terminal transcript 或完整输出 artifact;
  • 新任务列表、task center,或 shell 专属的 status/wait/stop 工具族。

Activity

  1. Astro-Han commented on Jul 12, 2026

    @Astro-Han
    Contributor

    The direction makes sense to me (PTY as an extension of run_in_background, scope following where that path already ships, minimal WriteStdin surface). Three things I'd like to discuss.

    1. The output model: I couldn't find a scenario call or a route argument. The RFC proposes screen snapshots via an in-process parser. Looking at other agents, there are three routes, all already in production, and I couldn't tell from the RFC why it picks the parser one. It also lists REPLs, debuggers, interactive setup, and full-screen TUI (vim/htop/less) all as PTY scenarios without separating must-support from nice-to-have, which I think is the split that decides the route.

    • dumb raw bytes (codex, codex-rs/core/src/unified_exec/process_manager.rs:69-79): strips color/cursor, no parser. Looks almost loss-free if full-screen TUI is not a core scenario.
    • tmux capture-pane (OpenHands, software-agent-sdk/.../terminal/tmux_terminal.py:188-197): tmux owns terminal state, plain-text screen to the model, already in production, at the cost of a tmux dependency.
    • in-process parser (the RFC route; gemini-cli built one at gemini-cli/packages/core/src/utils/terminalSerializer.ts:4-21 but routes it to the UI only, never the model): structured screen with cursor/attributes, no external process, but fail-closed on complex programs (vim nesting, tmux) and the most code.

    On resize: I couldn't find any implementation that exposes it to the model (OpenHands fixes 256x200 and calls it an accepted tradeoff). The parser route is the only one of the three that could do resize, so I'm curious whether that's the reason to pick it, and if so whether it's worth the cost. If full-screen TUI is not core, dumb mode drops both the parser and the tmux dependency, which seems lower-regret to me, but that hinges on the scenario call. What's the scenario priority, and why the parser route over tmux?

    2. observe_for_ms — one concern. The RFC already says Read(ref) is the observation path. If WriteStdin also has an observe window, the two paths overlap on the same job, which blurs the split the RFC is careful about elsewhere. It's also the field I'd most worry gets read as a command timeout (the RFC flags this itself). The alternative is WriteStdin returns immediately and the model does an explicit Read. Curious whether the one-step convenience is worth the overlap, or whether it'd be cleaner to drop it.

    3. Dependencies and limits seem downstream of the route. Since the route isn't decided, the dependency surface isn't either: native PTY only for dumb mode, tmux (external binary) for the tmux route, a terminal-emulator dependency plus its fail-closed fallback for the parser route. Might be worth laying out the trade rather than locking one in now. On limits, my instinct is only model-observable or cross-host-consistent ones (concurrent PTY count, size bounds, output/token cap) belong in the stable contract, and internal tuning stays internal, but happy to hear how you'd draw that line.

    简体中文

    方向我看着没问题(PTY 当作 run_in_background 的延展、范围跟这条路径已经铺到哪就走哪、工具面够小)。有几个点想一起聊聊。

    1. 输出模型:我没在 RFC 里看到场景取舍和路线论证。 RFC 说要用进程内 parser 生成终端屏幕快照。我翻了几个别的 agent,其实有三条路,都已经有产品在用,但 RFC 没说为什么选 parser 这条。它还把 REPL、调试器、交互式初始化、全屏 TUI(vim/htop/less)都列成 PTY 场景,却没分哪些是核心、哪些可有可无,而这个区分我觉得正好决定该走哪条路。

    • dumb raw bytes(codex,codex-rs/core/src/unified_exec/process_manager.rs:69-79):直接去色去光标控制,不上 parser。全屏 TUI 不是核心的话,看着基本够用。
    • tmux capture-pane(OpenHands,software-agent-sdk/.../terminal/tmux_terminal.py:188-197):终端状态交给 tmux 管,喂给模型的是纯文本屏幕,线上已经在跑,代价是多一个 tmux 依赖。
    • 进程内 parser(RFC 选的这条;gemini-cli 也建过一个,gemini-cli/packages/core/src/utils/terminalSerializer.ts:4-21,但只给 UI 用、没喂给模型):拿到的是带光标和属性的结构化屏幕,不依赖外部进程,但复杂程序上会 fail-closed(vim 嵌套、tmux),代码量也最大。

    resize 单说一句:我没找到哪家把 resize 开放给模型(OpenHands 固定 256x200,注释里明说是"为避免 1000x1000 虚拟终端网格"做的取舍)。三条路里只有 parser 做得了 resize,所以想问一下,选 parser 是不是为了 resize,如果是,这个能力值不值这个代价。如果全屏 TUI 不是核心,dumb 模式连 parser 带 tmux 依赖一起省了,我看着更稳妥,但这取决于场景怎么定。想听一下场景优先级怎么排,以及为什么选 parser 而不是 tmux?

    2. observe_for_ms,提一个担心。 RFC 自己说 Read(ref) 是观察路径,那 WriteStdin 再加一个观察窗口,两条路径就重叠在同一件事上,把 RFC 别处很在意的职责边界糊掉了。而且这个字段我最担心被人当成"命令超时"来读(RFC 自己也提了这点)。另一个办法是 WriteStdin 立即返回、要输出就显式 Read 一次。想听一下,省一步的方便值不值这个重叠,还是去掉更干净。

    3. 依赖和上限感觉是跟着路线走的。 路线没定,依赖面也就没定:dumb 模式只要 native PTY;tmux 路线要多一个 tmux(外部二进制);parser 路线要终端模拟器依赖加它的 fail-closed 兜底。三条打包成本不同,是不是先把取舍摆出来、别现在就锁一条。上限那边,我的直觉是只有模型能看到、或者跨宿主要一致的(并发 PTY 数、尺寸上下限、输出/token 上限)才进稳定契约,内部调优留内部,不过这条线怎么划也想听你的想法。

  2. M4n5ter commented on Jul 12, 2026

    @M4n5ter
    MemberAuthor
    English

    @Astro-Han I missed this comment when it was posted. The same questions later resurfaced during the review of #778, so part of the discussion has already continued there, particularly in this response. I want to reconcile the two threads rather than leave this RFC comment unanswered.

    The scenario split became clearer during implementation:

    • Line-oriented interaction such as REPLs, prompts, and debuggers is core.
    • Truthful textual state after clear, redraw, cursor movement, and alternate-screen transitions is also core.
    • A human-grade terminal attachment experience, styling, mouse input, and complete fidelity for every terminal extension remain out of scope.

    That second requirement is the main reason for the parser route; resize was not the deciding factor. A bounded raw-byte tail cannot reliably answer what is currently visible after destructive terminal operations. tmux capture-pane can provide that state, but it introduces an external session server and does not fit Maka's native Windows/ConPTY path well. Using node-pty with an in-process @xterm/headless adapter keeps process, parser, persistence, and cleanup ownership within the existing ShellRun lifecycle. The tradeoff is more implementation work and a fail-closed parser boundary, which the current implementation accepts explicitly.

    I also agree with your concern about observe_for_ms. The PR review exposed an additional attribution ambiguity under concurrent calls. I have proposed simplifying the contract so WriteStdin commits at its parser cut and later observation always goes through Read(ref). That decision is still awaiting confirmation in #778, so I do not want to state it as settled in the RFC yet.

    For limits, I agree with the boundary you suggested: values that determine valid model calls or cross-host observable behavior belong to the stable contract, while parser watermarks, flush cadence, and similar implementation tuning should remain internal.

    Once the remaining contract point is settled in #778, I will make sure the RFC and implementation describe the same final behavior.

    简体中文

    @Astro-Han 这条评论发布时我漏看了。后来在 #778 的审查中又出现了相同的问题,因此部分讨论实际上已经转移到了 PR,尤其是这条回复。我想把两边的讨论对齐,而不是让这条 RFC 评论一直没有回应。

    实现过程中,场景优先级变得更清楚了:

    • REPL、确认提示和调试器等逐行交互属于核心场景;
    • 清屏、重绘、光标移动和 alternate-screen 切换后,向模型提供真实的文本画面,同样属于核心要求;
    • 面向人类的完整终端 attach、样式、鼠标输入,以及对所有终端扩展的完全还原,仍不在本次范围。

    第二项才是选择 parser 路线的主要原因,resize 并不是决定因素。有界的原始字节 tail 无法在破坏性终端操作之后可靠回答“当前屏幕上实际显示什么”。tmux capture-pane 可以提供这种状态,但会引入外部 session server,也不适合 Maka 的原生 Windows/ConPTY 路径。通过 node-pty 和进程内的 @xterm/headless adapter,进程、parser、持久化和清理仍由现有 ShellRun 生命周期统一持有。代价是实现量更大,并且需要明确的 fail-closed parser 边界;当前实现选择正面承担这项代价。

    我也认同你对 observe_for_ms 的担心。PR 审查又发现了并发调用下的归因歧义。我已经建议把契约简化为:WriteStdin 在对应 parser cut 提交并返回,后续观察统一通过 Read(ref) 完成。目前这个决定仍在等待 #778 中的确认,所以我暂时不想在 RFC 中把它写成已经确定的结论。

    关于限制,我认同你建议的边界:决定模型调用是否合法、或者影响跨宿主可观察行为的数值应进入稳定契约;parser 水位、刷新节奏等实现调优则应保留为内部细节。

    等 #778 中剩余的契约问题确定后,我会确保 RFC 与实现描述的是同一套最终行为。

  3. M4n5ter commented on Jul 12, 2026

    @M4n5ter
    MemberAuthor
    English

    @Astro-Han I have updated the RFC to incorporate the conclusions from this discussion and the later review in #778.

    • The first-slice scenario priority is now explicit: line-oriented interaction is core, and truthful textual state after clear/redraw/cursor/alternate-screen operations is also core; a human-grade terminal attachment is not.
    • The route choice is now argued rather than implied. Raw bytes cannot reconstruct current visible state, while tmux would introduce an external session server and lacks a dependable native Windows/ConPTY path. The in-process node-pty + @xterm/headless route keeps lifecycle ownership inside ShellRun. Resize is useful but was not the deciding reason.
    • observe_for_ms has been removed. WriteStdin returns the terminal state at the immediate parser cut after control commit, and later output is observed through Read(ref).
    • The RFC now separates stable model-visible/cross-host limits from parser watermarks, flush cadence, and other internal tuning. It also records the accepted canonical-audit, human-projection, and exact permission-scope boundaries from the PR review.

    The implementation and PR description now follow the same contract. The remaining questions in the RFC are limited to whether the minimal tool/host surface, terminal projection, packaging boundary, and stable-limit boundary are the right ones for Maka.

    简体中文

    @Astro-Han 我已经把这次讨论和 #778 后续审查中确定的结论同步进 RFC。

    • 首个范围的场景优先级现在已经明确:逐行交互是核心;清屏、重绘、光标移动和 alternate-screen 操作后的真实文本画面同样是核心;面向人类的完整终端 attach 则不是。
    • 路线选择现在有明确论证,而不是隐含结论。Raw bytes 无法重建当前可见画面;tmux 会引入外部 session server,也缺少可靠的原生 Windows/ConPTY 路径;进程内 node-pty + @xterm/headless 方案让生命周期所有权继续留在 ShellRun 内。Resize 很有用,但不是决定路线的原因。
    • observe_for_ms 已删除。WriteStdin 返回 control commit 后即时 parser cut 上的终端状态,之后到达的输出通过 Read(ref) 观察。
    • RFC 现在区分稳定的模型可见/跨宿主限制与 parser 水位、刷新节奏等内部调优,也记录了 PR 审查确认的 canonical 审计、人类投影和精确权限 scope 边界。

    实现与 PR 正文现在遵循同一契约。RFC 中保留的问题只涉及最小工具/宿主范围、终端投影、打包边界和稳定限制边界是否最符合 Maka。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions