Terminals: flow-controlled output, immediate echo, sliced emulator writes
Machine terminals no longer skip output for live viewers. Each connection's subscription is a cursor into the 1 MiB retained tail, drained by the connection's writer as soon as the PTY reader writes (no 50 ms tick; a streaming frame gathers for at most 2 ms). A subscription opened with flow: true gets window (256 KiB) in its reply and acknowledges drawn output with terminal_ack; while one is full the reader stops reading the PTY, so the program waits. A subscription full for 3 s without acking stops holding the terminal and later sees a gap; a gone connection drops its subscriptions. Clients that never ask for flow keep the old pacing (newest 64 KiB per 50 ms). terminal_open/terminal_output replies precede the frames that follow them, which start exactly at the reply's output_end. Chat shell runs share the buffer code: output is pushed as it arrives, oldest first, while the chat's watchers keep short queues; 256 KiB unpushed stops the reader, and watchers behind for 3 s get the old pacing. The app asks for flow, takes offsets from the server (frame end, reply output_end) instead of re-encoded text length, acks per quarter window, and writes replays and bursts into the emulator in slices that yield to the event loop (ChatShellTerminalFeed, shared with chat shells). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -35,6 +35,14 @@ type terminalProcess struct {
|
||||
// refused: the login never asked for a password; the terminal is
|
||||
// forgotten without being listed.
|
||||
refused bool
|
||||
// views are the connections' subscriptions to the output
|
||||
// (terminal_flow.go); room is signalled when one may have room again,
|
||||
// exited is closed once the attachment exited, and outDone is set once
|
||||
// its stream is fully read.
|
||||
views map[*terminalView]struct{}
|
||||
room chan struct{}
|
||||
exited chan struct{}
|
||||
outDone bool
|
||||
}
|
||||
|
||||
func newTerminalProcess(record protocol.TerminalRecord) *terminalProcess {
|
||||
@@ -46,6 +54,9 @@ func newTerminalProcess(record protocol.TerminalRecord) *terminalProcess {
|
||||
out: newChatShellOutput(protocol.TerminalRetained),
|
||||
input: make(chan []byte, protocol.ChatShellInputQueue),
|
||||
done: make(chan struct{}),
|
||||
views: map[*terminalView]struct{}{},
|
||||
room: make(chan struct{}, 1),
|
||||
exited: make(chan struct{}),
|
||||
|
||||
loginChanged: make(chan struct{}, 1),
|
||||
}
|
||||
@@ -70,16 +81,40 @@ func (p *terminalProcess) running() bool {
|
||||
func (p *terminalProcess) snapshot(output bool, after int64) protocol.TerminalRecord {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
return p.snapshotLocked(output, after)
|
||||
}
|
||||
|
||||
// snapshotLocked is snapshot with p.mu held. The output ends at OutputEnd:
|
||||
// an incomplete trailing character waits for the next frame.
|
||||
func (p *terminalProcess) snapshotLocked(output bool, after int64) protocol.TerminalRecord {
|
||||
record := p.record
|
||||
record.OutputEnd = p.out.total
|
||||
if output {
|
||||
text, start := p.out.tail(protocol.TerminalRetained)
|
||||
if after > start {
|
||||
skip := min(after-start, int64(len(text)))
|
||||
cut := runeStartAfter([]byte(text), int(skip))
|
||||
text, start = text[cut:], start+int64(cut)
|
||||
from := max(after, p.out.start)
|
||||
text, start, end, ok := p.out.chunk(from, p.out.total, protocol.TerminalRetained, p.outDone)
|
||||
if !ok {
|
||||
// Nothing new (or only part of a character): the reply ends
|
||||
// where the frames that follow it start.
|
||||
from = min(from, p.out.total)
|
||||
text, start, end = "", from, from
|
||||
}
|
||||
record.Output, record.OutputStart, record.OutputEnd = text, start, end
|
||||
}
|
||||
return record
|
||||
}
|
||||
|
||||
// readFor is the record with its output from after on, read for
|
||||
// subscription v (when set): v's frames continue where the output ends,
|
||||
// once the reply is written (v.release).
|
||||
func (p *terminalProcess) readFor(after int64, v *terminalView) protocol.TerminalRecord {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
record := p.snapshotLocked(true, after)
|
||||
if v != nil && !v.gone {
|
||||
v.resetLocked(record.OutputEnd)
|
||||
if v.flow {
|
||||
record.Window = protocol.TerminalFlowWindow
|
||||
}
|
||||
record.Output, record.OutputStart = text, start
|
||||
}
|
||||
return record
|
||||
}
|
||||
@@ -108,10 +143,12 @@ func (p *terminalProcess) startAttachment(cmd *exec.Cmd) (*os.File, error) {
|
||||
func (p *terminalProcess) read(terminal *os.File, titled func(string)) {
|
||||
buffer := make([]byte, 32<<10)
|
||||
for {
|
||||
p.awaitRoom()
|
||||
n, err := terminal.Read(buffer)
|
||||
if n > 0 {
|
||||
p.mu.Lock()
|
||||
p.out.write(buffer[:n])
|
||||
p.pokeViewsLocked()
|
||||
if p.login.feed(buffer[:n]) {
|
||||
select {
|
||||
case p.loginChanged <- struct{}{}:
|
||||
@@ -166,6 +203,7 @@ func (p *terminalProcess) signal(sig syscall.Signal) {
|
||||
// kills its group and closes its terminal. It returns the exit error.
|
||||
func (p *terminalProcess) waitAttachment(cmd *exec.Cmd, terminal *os.File, readDone <-chan struct{}) error {
|
||||
err := cmd.Wait()
|
||||
close(p.exited)
|
||||
_ = terminal.SetReadDeadline(time.Now().Add(chatShellDrain))
|
||||
select {
|
||||
case <-readDone:
|
||||
|
||||
Reference in New Issue
Block a user