[bug] --device-auth
Summary
First time installing. Trying to do --device-auth, and i notice that i cannot use --device-auth to login. Normal login seems to be working fine on windows.
Reproduction
- codex-multi-auth login --device-auth
Expected Behavior
I guess the expected behaviour would be to keep the app running in bg until code is inserted or app is closed.
Actual Behavior
After --device-auth command, the app seems to crash:
Warning: Detected unsettled top-level await at file:///C:/Users/ursus/AppData/Roaming/npm/node_modules/codex-multi-auth/scripts/codex-multi-auth.js:57 const exitCode = await runCodexMultiAuthCli(args);
Environment
codex --version: codex-cli 0.129.0 | codex-multi-auth 2.1.8- OS: same problem on windows and linux
Compliance Confirmation
- [x ] I am using this project for personal development workflows.
- [ x] This report does not request policy bypasses or prohibited usage.
- [ x] I removed any secrets/tokens from this report.
Coding Plan
Summary
- add
keepAlive?: booleantoDeviceAuthFlowOptionsto conditionally prevent timer unref - pass
keepAlive: truefrom the cli code path inrunSignInFlow() - this fixes the "unsettled top-level await" crash without breaking library consumers who rely on unreffed timers
- add regression test to prevent future breakage
Design Choices
Design Choice 1: should we modify the default timer behavior or add an opt-in flag?
Options Considered:
- add
keepAlive?: booleantoDeviceAuthFlowOptionsand skipunref()when true - use
process.stdin.ref()in the cli caller to keep the event loop alive externally - remove
unref()entirely and let all consumers deal with it
Chosen Option: 1
Rationale: option 1: adding a keepAlive flag to the existing options interface follows the established dependency injection pattern in DeviceAuthFlowOptions and is explicit about intent without breaking existing library consumers.
💡 User Tips
Regenerate the plan with different choices with @coderabbitai <feedback>.
Implementation Steps
fix timer unref behavior for cli context
this phase adds a keepAlive option to the device-auth flow that prevents timers from being unreffed, keeping the node process alive during polling.
Task 1: add keepAlive option to DeviceAuthFlowOptions
add a keepAlive?: boolean property to the options interface and thread it through to the sleep implementation.
- update
DeviceAuthFlowOptionsinterface inlib/auth/device-auth.ts:~30to includekeepAlive?: boolean - modify
getSleep()atlib/auth/device-auth.ts:64to accept akeepAliveparameter and conditionally skip theunrefTimer()call when true - update
sleepWithAbort()similarly if it usesunrefTimer()directly
Task 2: pass keepAlive from runSignInFlow
the cli invocation path should pass keepAlive: true to ensure the process stays alive during device-auth polling.
- update
runSignInFlow()inlib/codex-manager.ts:2031whererunDeviceAuthFlow()is called to passkeepAlive: truein the options object - this ensures cli-invoked device auth keeps the process alive while library consumers retain the default unref behavior
Task 3: add regression test for keepAlive behavior
missing regression test coverage for this scenario. need to verify timers are ref'd when keepAlive: true.
- add test case in
test/device-auth.test.tsthat verifies whenkeepAlive: trueis passed, the sleep timer is not unreffed - consider testing the inverse (default behavior still unrefs) to prevent accidental regression in the other direction
- flag: this is a node-specific behavior test; may need to mock or spy on timer refs. windows edge case: ensure test runs correctly on windows ci if present
🤖 Prompt for AI agents
this phase fixes the "unsettled top-level await" crash by adding a `keepAlive`
flag to the device-auth options, threading it through the sleep/timer logic,
wiring it into the cli call site, and adding regression coverage.
**`lib/auth/device-auth.ts:~30` — extend the options interface**
- add `keepAlive?: boolean` to the `DeviceAuthFlowOptions` interface
**`lib/auth/device-auth.ts:64` — update `getSleep()`**
- accept a `keepAlive` parameter
- conditionally skip the `unrefTimer()` call when `keepAlive` is `true`
- check `sleepWithAbort()` in the same file — if it calls `unrefTimer()`
directly, apply the same conditional skip
**`lib/codex-manager.ts:2031` — wire up the cli call site**
- in `runSignInFlow()`, where `runDeviceAuthFlow()` is called, pass `keepAlive:
true` in the options object
- do not change any other call sites; library consumers must retain the default
unref behavior
**`test/device-auth.test.ts` — regression tests**
- add a test that verifies when `keepAlive: true` is passed, the sleep timer is
**not** unreffed (spy or mock on timer ref/unref)
- add a complementary test verifying default behavior still unrefs the timer, to
catch accidental regression in either direction
- flag: windows ci edge case — confirm timer ref behavior is consistent on
windows; note this explicitly in test comments if there is any uncertainty
Research
the device-auth flow lives in lib/auth/device-auth.ts and is invoked from lib/codex-manager.ts:2022 via runSignInFlow(). the entry point at scripts/codex-multi-auth.js:57 uses top-level await. the sleep implementation at lib/auth/device-auth.ts:64-66 explicitly calls unref() on all timers, which tells node the timer shouldn't keep the event loop alive. this is correct for library/background usage but breaks cli invocation where we need the process to stay alive while waiting for the user to complete browser auth.
🚀 Next Steps
🤖 All AI agent prompts combined
this phase fixes the "unsettled top-level await" crash by adding a `keepAlive`
flag to the device-auth options, threading it through the sleep/timer logic,
wiring it into the cli call site, and adding regression coverage.
**`lib/auth/device-auth.ts:~30` — extend the options interface**
- add `keepAlive?: boolean` to the `DeviceAuthFlowOptions` interface
**`lib/auth/device-auth.ts:64` — update `getSleep()`**
- accept a `keepAlive` parameter
- conditionally skip the `unrefTimer()` call when `keepAlive` is `true`
- check `sleepWithAbort()` in the same file — if it calls `unrefTimer()`
directly, apply the same conditional skip
**`lib/codex-manager.ts:2031` — wire up the cli call site**
- in `runSignInFlow()`, where `runDeviceAuthFlow()` is called, pass `keepAlive:
true` in the options object
- do not change any other call sites; library consumers must retain the default
unref behavior
**`test/device-auth.test.ts` — regression tests**
- add a test that verifies when `keepAlive: true` is passed, the sleep timer is
**not** unreffed (spy or mock on timer ref/unref)
- add a complementary test verifying default behavior still unrefs the timer, to
catch accidental regression in either direction
- flag: windows ci edge case — confirm timer ref behavior is consistent on
windows; note this explicitly in test comments if there is any uncertainty
💡 Iterate on the plan with:
@coderabbitai <feedback>
Example Feedback
- `@coderabbitai` You can skip phase 3. Add a simple unit test case for phase 2.
- `@coderabbitai` For design choice 1 go ahead with option 3 and replan.
💬 Have feedback or questions? Drop into our discord!