refactor(cli): centralize daemon errno parsing and add utility tests

What changed:
- extracted shared readNodeErrnoCode helper in local daemon process control code
- updated isProcessRunning and signalProcess to use the shared helper
- added packages/cli/tests/18-local-daemon-utils.test.ts covering resolveTcpHostFromListen cases (numeric, host:port, unix sockets, empty/non-host)

Why:
- removes duplicated low-level error-shape branching and keeps errno handling consistent in one place
- adds deterministic coverage for pure listen parsing logic without depending on daemon runtime state

Accomplishments for next agent:
- this area now has a focused utility test entry that runs fast and is independent of daemon availability
- full workspace typecheck is clean after this change

Challenges / notes for next agent:
- packages/cli/tests/03-daemon.test.ts can be environment-sensitive when a daemon is already running (baseline in this workspace did not reliably represent daemon not running)
- existing unstaged change remains in scripts/codex-refactor-loop.sh and was intentionally left untouched
This commit is contained in:
Mohamed Boudra
2026-02-14 01:26:54 +07:00
parent 193c723714
commit 80f0ca08f9
2 changed files with 53 additions and 2 deletions
@@ -169,12 +169,20 @@ function sleep(ms: number): Promise<void> {
})
}
function readNodeErrnoCode(error: unknown): string | undefined {
if (typeof error !== 'object' || error === null || !('code' in error)) {
return undefined
}
return typeof error.code === 'string' ? error.code : undefined
}
function isProcessRunning(pid: number): boolean {
try {
process.kill(pid, 0)
return true
} catch (err) {
const code = typeof err === 'object' && err && 'code' in err ? (err as { code?: string }).code : undefined
const code = readNodeErrnoCode(err)
if (code === 'EPERM') {
return true
}
@@ -187,7 +195,7 @@ function signalProcess(pid: number, signal: NodeJS.Signals): boolean {
process.kill(pid, signal)
return true
} catch (err) {
const code = typeof err === 'object' && err && 'code' in err ? (err as { code?: string }).code : undefined
const code = readNodeErrnoCode(err)
if (code === 'ESRCH') {
return false
}