fix(terminal-panel): count a prompt printed before startup began as shell output

startup() only accepted output it observed after it began, so a shell
whose prompt reached the screen inside openForAgent and then stayed quiet
waited until startupTimeoutMs, failed terminal_open and closed the new
tab. A non-empty screen at the start now counts as output; the revision
alone is not used, because an attachment's state change also advances
it. The startup-failure test now models a shell that never prints with an
empty screen.
This commit is contained in:
Yichen Jiang committed 2026-11-05 14:29:38 +08:00
1 parent 95b955bd41
commit 13285ef097
2 files changed
+27 -1

No files matched your search

@@ -36,7 +36,9 @@ export async function startup(terminal: PanelTerminal, options: StartupOptions,
const startedAt = now()
const deadline = startedAt + options.startupTimeoutMs
let revision = terminal.revision
let lastChange: number | undefined
// A screen printed before startup began counts as output; the revision alone also advances on attachment state.
const printed = terminal.read({ offset: 0, count: Number.MAX_SAFE_INTEGER, maxBytes: options.maxReadBytes }).text !== ''
let lastChange = printed ? startedAt : undefined
let lastProbe = -Infinity
for (;;) {
signal.throwIfAborted()
@@ -62,6 +62,7 @@ describe('spawn', () => {
it('closes the terminal when startup fails and reports a failed cleanup with the cause', async () => {
const failed = backend()
failed.terminal.screen = ['']
const spawning = failed.backend.spawn({ sessionId: TerminalSessionId('pty-1'), owner: agentFor(new Context(), 'session'), type: 'shell' })
const rejected = expect(spawning).rejects.toThrow('did not reach its prompt within 5000 ms')
await vi.advanceTimersByTimeAsync(5000)
@@ -122,6 +123,29 @@ describe('startup', () => {
expect(motd).toBe('$loading nvm\n$')
})
it('counts a prompt printed before startup began as output', async () => {
const terminal = new FakeTerminal()
const ready = startup(terminal, options, new AbortController().signal)
await vi.advanceTimersByTimeAsync(300)
expect(await ready).toBe('$')
expect(terminal.inspectForeground).toHaveBeenCalledOnce()
})
it('waits for output on an empty screen even when attachment state advances the revision', async () => {
const terminal = new FakeTerminal()
terminal.screen = ['']
terminal.update({ cols: 120 })
const ready = startup(terminal, options, new AbortController().signal)
let motd: string | undefined
void ready.then((value) => { motd = value })
await vi.advanceTimersByTimeAsync(2000)
expect(motd).toBeUndefined()
expect(terminal.inspectForeground).not.toHaveBeenCalled()
terminal.output('$')
await vi.advanceTimersByTimeAsync(300)
expect(motd).toBe('$')
})
it('accepts silence alone when the foreground cannot be observed', async () => {
const terminal = new FakeTerminal()
terminal.foreground = undefined