Skip to content

Commit 0fc829c

Browse files
axisrowclaude
andcommitted
fix(terminal): restore walk parity and BSD-safe ps format per verification
- ps: use two -o flags instead of the comma list; FreeBSD's parser treats everything after the first '=' as one header, which collapsed the combined form into a single bogus column and dropped the probe to the tput fallback. Still one spawn per ancestor. - walk: start at process.ppid and read each generation's TTY before terminating on its PPID, restoring the old two-spawn walk's generation coverage (1..8) and keeping a dead-end PPID from hiding a valid width on the same ps row. Known narrowing vs the old sequence: if the combined lookup fails entirely, the walk stops (the old two-call flow could still continue on the separately obtained PPID). Co-Authored-By: Claude Code <[email protected]>
1 parent 1177422 commit 0fc829c

2 files changed

Lines changed: 50 additions & 24 deletions

File tree

‎src/utils/__tests__/terminal.test.ts‎

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('terminal utils', () => {
6666
it('returns width from the immediate parent tty when available', () => {
6767
pinPosixPlatform();
6868
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
69-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
69+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
7070
return '1234 ttys001\n';
7171
}
7272

@@ -79,23 +79,23 @@ describe('terminal utils', () => {
7979

8080
expect(getTerminalWidth()).toBe(120);
8181
expect(mockExecFileSync.mock.calls.map(([file, args]) => `${file as string} ${(args as string[]).join(' ')}`)).toEqual([
82-
`ps -o ppid=,tty= -p ${process.pid}`,
82+
`ps -o ppid= -o tty= -p ${process.ppid}`,
8383
'stty -F /dev/ttys001 size'
8484
]);
8585
});
8686

8787
it('walks ancestor processes until it finds a valid tty', () => {
8888
pinPosixPlatform();
8989
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
90-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
90+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
9191
return '1234 ?\n';
9292
}
9393

94-
if (file === 'ps' && args.join(' ') === '-o ppid=,tty= -p 1234') {
94+
if (file === 'ps' && args.join(' ') === '-o ppid= -o tty= -p 1234') {
9595
return '5678 ??\n';
9696
}
9797

98-
if (file === 'ps' && args.join(' ') === '-o ppid=,tty= -p 5678') {
98+
if (file === 'ps' && args.join(' ') === '-o ppid= -o tty= -p 5678') {
9999
return '1 ttys009\n';
100100
}
101101

@@ -109,11 +109,30 @@ describe('terminal utils', () => {
109109
expect(getTerminalWidth()).toBe(104);
110110
});
111111

112+
it('reads the current generation TTY even when its PPID ends the walk', () => {
113+
pinPosixPlatform();
114+
// A PPID of 0 terminates the walk after this row, not before it:
115+
// the parent's own width must still win over the tput fallback.
116+
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
117+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
118+
return '0 ttys001\n';
119+
}
120+
121+
if (file === 'stty' && args.join(' ') === '-F /dev/ttys001 size') {
122+
return '24 120\n';
123+
}
124+
125+
throw new Error(`Unexpected command: ${file} ${args.join(' ')}`);
126+
});
127+
128+
expect(getTerminalWidth()).toBe(120);
129+
});
130+
112131
it('falls back through stty variants when the first form returns no value', () => {
113132
pinPosixPlatform();
114133
// Simulates BSD/macOS, where `stty -F` exits with an error; `stty -f` succeeds.
115134
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
116-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
135+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
117136
return '1234 ttys003\n';
118137
}
119138

@@ -144,11 +163,11 @@ describe('terminal utils', () => {
144163
it('returns null when ancestor and fallback probes all fail', () => {
145164
pinPosixPlatform();
146165
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
147-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
166+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
148167
return '1234 ttys001\n';
149168
}
150169

151-
if (file === 'ps' && args.join(' ') === '-o ppid=,tty= -p 1234') {
170+
if (file === 'ps' && args.join(' ') === '-o ppid= -o tty= -p 1234') {
152171
return '0 ttys001\n';
153172
}
154173

@@ -169,15 +188,15 @@ describe('terminal utils', () => {
169188
it('detects availability when an ancestor tty probe succeeds', () => {
170189
pinPosixPlatform();
171190
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
172-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
191+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
173192
return '1234 ?\n';
174193
}
175194

176-
if (file === 'ps' && args.join(' ') === '-o ppid=,tty= -p 1234') {
195+
if (file === 'ps' && args.join(' ') === '-o ppid= -o tty= -p 1234') {
177196
return '5678 ??\n';
178197
}
179198

180-
if (file === 'ps' && args.join(' ') === '-o ppid=,tty= -p 5678') {
199+
if (file === 'ps' && args.join(' ') === '-o ppid= -o tty= -p 5678') {
181200
return '1 ttys010\n';
182201
}
183202

@@ -211,7 +230,7 @@ describe('terminal utils', () => {
211230
process.env.CCSTATUSLINE_WIDTH = '0';
212231

213232
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
214-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
233+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
215234
return '1234 ttys001\n';
216235
}
217236

@@ -230,7 +249,7 @@ describe('terminal utils', () => {
230249
process.env.CCSTATUSLINE_WIDTH = 'wide';
231250

232251
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
233-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
252+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
234253
return '1234 ttys001\n';
235254
}
236255

@@ -264,7 +283,7 @@ describe('terminal utils', () => {
264283
it('probes only once across repeated calls when a width is found', () => {
265284
pinPosixPlatform();
266285
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
267-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
286+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
268287
return '1234 ttys001\n';
269288
}
270289

@@ -280,7 +299,7 @@ describe('terminal utils', () => {
280299
expect(canDetectTerminalWidth()).toBe(true);
281300

282301
const ppidProbes = mockExecFileSync.mock.calls.filter(
283-
call => call[0] === 'ps' && Array.isArray(call[1]) && (call[1])[1] === 'ppid=,tty='
302+
call => call[0] === 'ps' && Array.isArray(call[1]) && (call[1])[1] === 'ppid='
284303
);
285304
expect(ppidProbes).toHaveLength(1);
286305
});
@@ -300,7 +319,7 @@ describe('terminal utils', () => {
300319
expect(canDetectTerminalWidth()).toBe(false);
301320

302321
const ppidProbes = mockExecFileSync.mock.calls.filter(
303-
call => call[0] === 'ps' && Array.isArray(call[1]) && (call[1])[1] === 'ppid=,tty='
322+
call => call[0] === 'ps' && Array.isArray(call[1]) && (call[1])[1] === 'ppid='
304323
);
305324
expect(ppidProbes).toHaveLength(1);
306325
});
@@ -401,7 +420,7 @@ describe('terminal utils', () => {
401420
it('never persists a discovered numeric width to the L2 cache', () => {
402421
pinPosixPlatform();
403422
mockExecFileSync.mockImplementation((file: string, args: string[]) => {
404-
if (file === 'ps' && args.join(' ') === `-o ppid=,tty= -p ${process.pid}`) {
423+
if (file === 'ps' && args.join(' ') === `-o ppid= -o tty= -p ${process.ppid}`) {
405424
return '1234 ttys001\n';
406425
}
407426

‎src/utils/terminal.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,26 @@ function probeTerminalWidth(): number | null {
5454

5555
// Claude Code can spawn ccstatusline with piped stdio, leaving the immediate
5656
// parent process without a controlling TTY. Walk up a few ancestors until we
57-
// find the shell process that owns the real PTY.
58-
let pid = process.pid;
57+
// find the shell process that owns the real PTY. process.ppid starts the walk
58+
// at generation 1 for free, matching the old two-spawn walk's generation
59+
// coverage (1..8).
60+
let pid = process.ppid;
5961
for (let depth = 0; depth < 8; depth += 1) {
6062
const ancestor = getProcessAncestorInfo(pid);
61-
if (ancestor.parentPid === null) {
62-
break;
63-
}
6463

64+
// Try this generation's TTY even when the PPID looks terminal: a PPID of
65+
// 0 must not hide a perfectly good width on the same ps row.
6566
if (ancestor.tty !== null) {
6667
const width = getWidthForTTY(ancestor.tty);
6768
if (width !== null) {
6869
return width;
6970
}
7071
}
7172

73+
if (ancestor.parentPid === null) {
74+
break;
75+
}
76+
7277
pid = ancestor.parentPid;
7378
}
7479

@@ -107,9 +112,11 @@ function getProcessAncestorInfo(pid: number): ProcessAncestorInfo {
107112
// and the controlling TTY. The probe re-runs per render on macOS (numeric
108113
// widths are deliberately not persisted across processes), and under
109114
// several concurrent sessions each saved subprocess is saved per repaint
110-
// (see #397).
115+
// (see #397). Two -o flags rather than one comma list: FreeBSD's parser
116+
// treats everything after the first '=' as a single header, so the comma
117+
// form collapses into one bogus column.
111118
try {
112-
const output = execFileSync('ps', ['-o', 'ppid=,tty=', '-p', String(pid)], {
119+
const output = execFileSync('ps', ['-o', 'ppid=', '-o', 'tty=', '-p', String(pid)], {
113120
encoding: 'utf8',
114121
stdio: ['pipe', 'pipe', 'ignore'],
115122
windowsHide: true

0 commit comments

Comments
 (0)