77// polling fd 0. The only clean fix is to let Ink tear down stdin via its own internal
88// handleSetRawMode(false) path (which calls stdin.unref()), then re-add it afterward.
99//
10+ // Targets ink 6.x. Every patch looks up exact source snippets and exits with an error if they are missing, so a
11+ // future ink upgrade fails loudly here instead of silently breaking the PTY handoff.
12+ //
1013// What the patch adds:
11- // App.js — suspendStdin() calls handleSetRawMode(false) to fully release stdin;
12- // resumeStdin() calls handleSetRawMode(true) to restore it.
13- // ink.js — suspendStdin()/resumeStdin() methods that delegate to the App instance.
14+ // App.js — registers suspend/resume closures on a `codifyStdinControl` prop. suspend fully releases stdin
15+ // (raw mode off, readable listener removed, unref); resume restores it.
16+ // ink.js — passes `codifyStdinControl` to App and exposes suspendStdin()/resumeStdin() plus
17+ // pauseRendering()/resumeRendering() for stopping the render loop during the handoff.
1418// render.js — includes suspendStdin/resumeStdin in the return value of render().
1519// reconciler.js — clears the cached <Static> node when it's unmounted (see the reconciler section below).
1620
@@ -57,60 +61,68 @@ if (!existsSync(APP_JS)) {
5761}
5862
5963// ── Patch App.js ─────────────────────────────────────────────────────────────
64+ // Since ink 6, App is a function component, so there is no class instance to call methods on. Instead, ink.js
65+ // (patched below) passes a mutable `codifyStdinControl` object as a prop, and App assigns suspend/resume closures
66+ // onto it on every render. The closures reuse App's own refs, so they always see the current listener state.
6067let appContent = await fs . readFile ( APP_JS , 'utf8' ) ;
6168
6269if ( ! appContent . includes ( 'CODIFY_INK_PATCH' ) ) {
63- // Remove any orphaned fragments from previous partial patch attempts
64- appContent = appContent . replace ( / \n s t d i n \. s e t R a w M o d e \( f a l s e \) ; \n s t d i n \. r e m o v e L i s t e n e r \( ' r e a d a b l e ' , t h i s \. h a n d l e R e a d a b l e \) ; \n s t d i n \. u n r e f \( \) ; \n \} \n \} ; \n r e s u m e S t d i n [ \s \S ] * ?\} ; \n (? = \/ \/ C O D I F Y _ I N K _ P A T C H ) / , '\n' ) ;
65-
66- // Also patch handleSetRawMode to snapshot kState before addListener('readable')
67- const SNAPSHOT_SEARCH = 'stdin.addListener(\'readable\', this.handleReadable);' ;
68- const snapshotIdx = appContent . indexOf ( SNAPSHOT_SEARCH ) ;
69- if ( snapshotIdx !== - 1 ) {
70- const SNAPSHOT_PATCH = `const _ks = Object.getOwnPropertySymbols(stdin._readableState)[0];
71- if (_ks !== undefined) { this._kStateBeforeReadable = stdin._readableState[_ks]; }
72- ` ;
73- appContent = appContent . slice ( 0 , snapshotIdx ) + SNAPSHOT_PATCH + appContent . slice ( snapshotIdx ) ;
74- }
75-
76- // Insert suspendStdin/resumeStdin just before the closing brace of the class
77- const SEARCH = 'findPreviousFocusable = (state) => {' ;
78- const idx = appContent . indexOf ( SEARCH ) ;
79- if ( idx === - 1 ) {
80- console . error ( 'ERROR: Could not find insertion point in ink App.js.' ) ;
70+ const SIGNATURE_SEARCH = 'setCursorPosition, }) {' ;
71+ const SNAPSHOT_SEARCH = 'stdin.addListener(\'readable\', handleReadable);' ;
72+ const INSERT_SEARCH = '// Focus navigation helpers' ;
73+ if (
74+ ! appContent . includes ( SIGNATURE_SEARCH )
75+ || ! appContent . includes ( SNAPSHOT_SEARCH )
76+ || ! appContent . includes ( INSERT_SEARCH )
77+ ) {
78+ console . error ( 'ERROR: Could not find insertion points in ink App.js.' ) ;
8179 process . exit ( 1 ) ;
8280 }
8381
82+ // Accept the control object as a prop
83+ appContent = appContent . replace ( SIGNATURE_SEARCH , 'setCursorPosition, codifyStdinControl, }) {' ) ;
84+
85+ // Snapshot stdin's internal kState bits right before ink adds its 'readable' listener (see suspend below)
86+ appContent = appContent . replace ( SNAPSHOT_SEARCH , `if (codifyStdinControl) {
87+ const _ks = Object.getOwnPropertySymbols(stdin._readableState)[0];
88+ if (_ks !== undefined) { codifyStdinControl.kStateBeforeReadable = stdin._readableState[_ks]; }
89+ }
90+ ${ SNAPSHOT_SEARCH } ` ) ;
91+
8492 const PATCH = `// CODIFY_INK_PATCH — suspendStdin/resumeStdin
85- _kStateBeforeReadable = undefined;
86- suspendStdin = () => {
87- if (this.isRawModeSupported() && this.rawModeEnabledCount > 0) {
88- const { stdin } = this.props;
93+ if (codifyStdinControl) {
94+ codifyStdinControl.suspend = () => {
95+ if (!isRawModeSupported || rawModeEnabledCount.current === 0) {
96+ return;
97+ }
8998 stdin.setRawMode(false);
90- stdin.removeListener('readable', this.handleReadable );
99+ detachReadableListener( );
91100 stdin.unref();
101+ inputParserRef.current.reset();
102+ clearPendingInputFlush();
92103 // In Node 24, removeListener does not clear internal kState bits set by
93104 // addListener('readable'), so isPaused() stays true and resume() won't
94105 // switch to flowing mode. Restore the kState value from before Ink added
95106 // its readable listener to fully undo the listener registration.
96107 const kState = Object.getOwnPropertySymbols(stdin._readableState)[0];
97- if (kState !== undefined && this._kStateBeforeReadable !== undefined) {
98- stdin._readableState[kState] = this._kStateBeforeReadable;
108+ if (kState !== undefined && codifyStdinControl.kStateBeforeReadable !== undefined) {
109+ stdin._readableState[kState] = codifyStdinControl.kStateBeforeReadable;
110+ }
111+ };
112+ codifyStdinControl.resume = () => {
113+ if (!isRawModeSupported || rawModeEnabledCount.current === 0) {
114+ return;
99115 }
100- }
101- };
102- resumeStdin = () => {
103- if (this.isRawModeSupported() && this.rawModeEnabledCount > 0) {
104- const { stdin } = this.props;
105116 stdin.ref();
106117 stdin.setRawMode(true);
107118 stdin.setEncoding('utf8');
108- stdin.addListener('readable', this.handleReadable);
109- }
110- };
119+ readableListenerRef.current = handleReadable;
120+ stdin.addListener('readable', handleReadable);
121+ };
122+ }
111123 ` ;
112124
113- appContent = appContent . slice ( 0 , idx ) + PATCH + appContent . slice ( idx ) ;
125+ appContent = appContent . replace ( INSERT_SEARCH , PATCH + INSERT_SEARCH ) ;
114126 await fs . writeFile ( APP_JS , appContent , 'utf8' ) ;
115127 console . log ( 'Patched ink App.js' ) ;
116128} else {
@@ -120,51 +132,45 @@ if (!appContent.includes('CODIFY_INK_PATCH')) {
120132// ── Patch ink.js ─────────────────────────────────────────────────────────────
121133let inkContent = await fs . readFile ( INK_JS , 'utf8' ) ;
122134
123- if ( ! inkContent . includes ( 'suspendStdin' ) ) {
124- // Add suspendStdin/resumeStdin methods that reach into the React tree via the
125- // container's current fiber to call the App component's methods.
126- // Simpler approach: store the App ref. But since we don't have a ref, we access
127- // the fiber's stateNode. Add methods to the Ink class that call into the container.
128- const SEARCH = 'async waitUntilExit() {' ;
129- const idx = inkContent . indexOf ( SEARCH ) ;
130- if ( idx === - 1 ) {
131- console . error ( 'ERROR: Could not find insertion point in ink ink.js.' ) ;
135+ if ( ! inkContent . includes ( 'CODIFY_INK_PATCH' ) ) {
136+ const METHODS_SEARCH = 'async waitUntilExit() {' ;
137+ const APP_PROPS_SEARCH = 'onExit: this.handleAppExit }' ;
138+ if ( ! inkContent . includes ( METHODS_SEARCH ) || ! inkContent . includes ( APP_PROPS_SEARCH ) ) {
139+ console . error ( 'ERROR: Could not find insertion points in ink ink.js.' ) ;
132140 process . exit ( 1 ) ;
133141 }
134142
135- const PATCH = `suspendStdin() {
136- // Walk the fiber tree to find the App component instance and call suspendStdin
137- let fiber = this.container.current;
138- while (fiber) {
139- if (fiber.stateNode && typeof fiber.stateNode.suspendStdin === 'function') {
140- fiber.stateNode.suspendStdin();
141- return;
142- }
143- fiber = fiber.child;
144- }
143+ // Hand App the control object it registers suspend/resume on
144+ inkContent = inkContent . replace (
145+ APP_PROPS_SEARCH ,
146+ 'onExit: this.handleAppExit, codifyStdinControl: this.codifyStdinControl }'
147+ ) ;
148+
149+ const PATCH = `// CODIFY_INK_PATCH — stdin + render loop handoff
150+ codifyStdinControl = {};
151+ suspendStdin() {
152+ this.codifyStdinControl.suspend?.();
145153 }
146154 resumeStdin() {
147- let fiber = this.container.current;
148- while (fiber) {
149- if (fiber.stateNode && typeof fiber.stateNode.resumeStdin === 'function') {
150- fiber.stateNode.resumeStdin();
151- return;
152- }
153- fiber = fiber.child;
154- }
155+ this.codifyStdinControl.resume?.();
155156 }
156157 pauseRendering() {
157158 // Temporarily stop all stdout writes without tearing down the React tree.
159+ // Cancel pending throttled writes so nothing lands on the terminal after the pause.
160+ this.throttledOnRender?.cancel?.();
161+ this.throttledLog?.cancel?.();
158162 this.isUnmounted = true;
159163 this.log.clear();
160164 }
161165 resumeRendering() {
162166 this.isUnmounted = false;
167+ // The log was cleared on pause, so force the next frame to be written even if it is unchanged.
168+ this.lastOutput = '';
163169 this.onRender();
164170 }
165171 ` ;
166172
167- inkContent = inkContent . slice ( 0 , idx ) + PATCH + inkContent . slice ( idx ) ;
173+ inkContent = inkContent . replace ( METHODS_SEARCH , PATCH + METHODS_SEARCH ) ;
168174 await fs . writeFile ( INK_JS , inkContent , 'utf8' ) ;
169175 console . log ( 'Patched ink ink.js' ) ;
170176} else {
0 commit comments