refactor(daemon): use mtime-based version detection instead of string comparison

Replace CLI version string comparison with file modification time (mtime) based detection. This provides a more reliable way to detect when the CLI binary has been updated, especially for bun-compiled executables where package.json may not be accessible.

Changes:
- Add getInstalledCliMtimeMs() utility to check CLI binary or package.json mtime
- Store startedWithCliMtimeMs in daemon state on startup
- Use mtime comparison in version check loop for daemon auto-restart
- Move version flag handling to early CLI startup before daemon initialization
- Fix machine active state merging to prefer newer activeAt timestamp

This improves daemon restart behavior when CLI is updated via package managers.
This commit is contained in:
weishu
2025-12-23 13:14:15 +08:00
parent c8f7a23f2f
commit f81e585eef
5 changed files with 54 additions and 36 deletions
+6 -19
View File
@@ -52,6 +52,11 @@ import { withBunRuntimeEnv } from './utils/bunRuntime'
return process.argv.slice(1)
})()
if (args.includes('-v') || args.includes('--version')) {
console.log(`hapi version: ${packageJson.version}`)
process.exit(0)
}
// Check if first argument is a subcommand
const subcommand = args[0]
@@ -79,10 +84,7 @@ import { withBunRuntimeEnv } from './utils/bunRuntime'
await ensureRuntimeAssets()
// If --version is passed - do not log, its likely daemon inquiring about our version
if (!args.includes('--version')) {
logger.debug('Starting hapi CLI with args: ', process.argv)
}
logger.debug('Starting hapi CLI with args: ', process.argv)
if (subcommand === 'doctor') {
// Check for clean subcommand
@@ -318,7 +320,6 @@ ${chalk.bold('To clean up runaway processes:')} Use ${chalk.cyan('hapi doctor cl
// Parse command line arguments for main command
const options: StartOptions = {}
let showHelp = false
let showVersion = false
const unknownArgs: string[] = [] // Collect unknown args to pass through to claude
for (let i = 0; i < args.length; i++) {
@@ -328,10 +329,6 @@ ${chalk.bold('To clean up runaway processes:')} Use ${chalk.cyan('hapi doctor cl
showHelp = true
// Also pass through to claude
unknownArgs.push(arg)
} else if (arg === '-v' || arg === '--version') {
showVersion = true
// Also pass through to claude (will show after our version)
unknownArgs.push(arg)
} else if (arg === '--hapi-starting-mode') {
options.startingMode = z.enum(['local', 'remote']).parse(args[++i])
} else if (arg === '--yolo') {
@@ -404,16 +401,6 @@ ${chalk.bold.cyan('Claude Code Options (from `claude --help`):')}
process.exit(0)
}
// Show version
if (showVersion) {
console.log(`hapi version: ${packageJson.version}`)
const versionOnly = args.every((value) => value === '-v' || value === '--version')
if (versionOnly) {
process.exit(0)
}
// Continue to pass --version to Claude Code when other args are present.
}
// Normal flow - auth and machine setup
await initializeToken();
await authAndSetupMachineIfNeeded();