Skip to content

Commit cfc8b83

Browse files
committed
fix(runtime): strip ansi from codex login output
1 parent 4ae3e7e commit cfc8b83

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

packages/runtime/src/__tests__/auth-manager.spawn.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ describe('AuthManager spawn-backed flows', () => {
104104
expect(manager.getLoginStatus('codex').status).toBe('authenticated')
105105
})
106106

107+
it('strips terminal color sequences from captured Codex login details', async () => {
108+
const child = makeChild()
109+
spawnMock.mockReturnValueOnce(child)
110+
const manager = new AuthManager()
111+
const result = manager.startLogin('codex')
112+
113+
child.stdout.emit('data', Buffer.from(
114+
'Open \u001b[94mhttps://auth.openai.com/device\u001b[0m and enter \u001b[94mABCD-12345\u001b[0m',
115+
))
116+
117+
await expect(result).resolves.toEqual({
118+
name: 'codex',
119+
status: 'pending',
120+
loginUrl: 'https://auth.openai.com/device',
121+
loginCode: 'ABCD-12345',
122+
})
123+
})
124+
107125
it('returns an error when login spawn fails before printing a URL', async () => {
108126
const child = makeChild()
109127
spawnMock.mockReturnValueOnce(child)

packages/runtime/src/auth-manager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import { createLogger } from './logger.js';
1111

1212
const log = createLogger('auth-manager');
1313
const AUTH_CHECK_TIMEOUT_MS = 1500;
14+
const ANSI_ESCAPE_REGEX = /\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g;
15+
16+
function stripAnsi(value: string): string {
17+
return value.replace(ANSI_ESCAPE_REGEX, '');
18+
}
1419

1520
/** Active login session for a provider */
1621
interface LoginSession {
@@ -203,7 +208,7 @@ export class AuthManager {
203208
let resolved = false;
204209

205210
const processOutput = (data: Buffer) => {
206-
const text = data.toString();
211+
const text = stripAnsi(data.toString());
207212
log.debug('login stdout', { provider, text: text.trim() });
208213

209214
// Try to extract code

0 commit comments

Comments
 (0)