mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
fix(cli): release stdin before handing terminal to local-mode child (#1932)
* fix(cli): release stdin before handing terminal to local-mode child hapi ships as a Bun-compiled executable, and Bun's tty.ReadStream keeps an in-flight read on fd 0 even after process.stdin.pause() (https://github.com/oven-sh/bun/issues/29126). Remote mode reads stdin (Ink UI in raw mode, for the double-space switch-to-local gesture). When switching back to local mode, spawnWithTerminalGuard only paused stdin before spawning the agent TUI with stdio:'inherit'. The parent's stale pending read kept racing the child for console input and won: after a remote -> local switch the agent TUI rendered but received no keystrokes at all — the input box was dead and even Ctrl-C never reached it. Instrumented at local spawn time: dataListeners=0, isPaused=true, but _readableState.reading=true. Fix: destroy stdin outright before spawning the local child (releaseStdinForChild) — destroy reliably stops the pending read where pause() does not — and re-create process.stdin via `new tty.ReadStream(0)` when remote mode starts again (ensureLiveStdin, called from RemoteLauncherBase.setupTerminal). hasTTY is now computed lazily since stdin may legitimately be destroyed at launcher construction time. Verified end-to-end on Windows with `hapi kimi` by injecting console input via WriteConsoleInput and checking the agent session wire log: remote -> double-space -> local -> typed message reaches the agent; switch to remote again (double-space detection works on the re-created stdin) -> local -> input works again. Direct local launch unaffected. `bun run typecheck:cli` and the cli vitest suite pass. * fix(cli): preserve piped stdin when releasing terminal for local child Addresses review feedback on #1932: releaseStdinForChild() destroyed stdin unconditionally, but destroying a piped stdin closes fd 0 and starves the child spawned with stdio:'inherit'. The Bun stale-read workaround is only needed for a TTY, so skip destroy() when stdin is not a terminal; pause() suffices for pipes. Adds regression coverage for the piped case. --------- Co-authored-by: Naitian LIU <Liunaitian@users.noreply.github.com>
This commit is contained in:
co-authored by
Naitian LIU
parent
ebca96e575
commit
db4ee82c80
@@ -2,6 +2,7 @@ import { render } from 'ink';
|
||||
import type { ReactElement } from 'react';
|
||||
import { MessageBuffer } from '@/ui/ink/messageBuffer';
|
||||
import { restoreTerminalState } from '@/ui/terminalState';
|
||||
import { ensureLiveStdin } from '@/utils/stdinLifecycle';
|
||||
import { RPC_METHODS } from '@hapi/protocol/rpcMethods';
|
||||
|
||||
export type RemoteLauncherExitReason = 'switch' | 'exit';
|
||||
@@ -40,7 +41,6 @@ type RpcHandlerManagerLike = {
|
||||
|
||||
export abstract class RemoteLauncherBase {
|
||||
protected readonly messageBuffer: MessageBuffer;
|
||||
protected readonly hasTTY: boolean;
|
||||
protected readonly logPath?: string;
|
||||
protected exitReason: RemoteLauncherExitReason | null = null;
|
||||
protected shouldExit: boolean = false;
|
||||
@@ -49,10 +49,19 @@ export abstract class RemoteLauncherBase {
|
||||
|
||||
protected constructor(logPath?: string) {
|
||||
this.logPath = logPath;
|
||||
this.hasTTY = Boolean(process.stdout.isTTY && process.stdin.isTTY);
|
||||
this.messageBuffer = new MessageBuffer();
|
||||
}
|
||||
|
||||
// Computed lazily: stdin may be destroyed while a local-mode child owns
|
||||
// the terminal (see utils/stdinLifecycle) and re-created in setupTerminal.
|
||||
protected get hasTTY(): boolean {
|
||||
return Boolean(
|
||||
process.stdout.isTTY
|
||||
&& process.stdin.isTTY
|
||||
&& !process.stdin.destroyed
|
||||
);
|
||||
}
|
||||
|
||||
protected abstract createDisplay(context: RemoteLauncherDisplayContext): ReactElement;
|
||||
|
||||
protected abstract runMainLoop(): Promise<void>;
|
||||
@@ -147,6 +156,10 @@ export abstract class RemoteLauncherBase {
|
||||
}
|
||||
|
||||
protected setupTerminal(handlers: RemoteLauncherTerminalHandlers): void {
|
||||
// Local mode destroys stdin so the child TUI gets all console input
|
||||
// (Bun keeps reading fd 0 after pause()). Re-create it before the
|
||||
// remote-mode UI reads keys again.
|
||||
ensureLiveStdin();
|
||||
if (this.hasTTY) {
|
||||
console.clear();
|
||||
this.inkInstance = render(this.createDisplay({
|
||||
|
||||
Reference in New Issue
Block a user