Skip to content

doctor: detect stale kubeconfig, local port collisions and orphaned runtime sockets - #127

Closed
Jonesc2321601 wants to merge 2 commits into
hanzoai:mainfrom
Jonesc2321601:doctor-selfchecks
Closed

Jonesc2321601 wants to merge 2 commits into
hanzoai:mainfrom
Jonesc2321601:doctor-selfchecks

Conversation

@Jonesc2321601

Copy link
Copy Markdown

Closes #111.

What this adds

hanzo doctor now runs three extra self-checks and prints an exact one-line fix command for every one that fails:

Check Detects Fix line printed
Kubeconfig ~/.kube/hanzo.yaml missing, not valid YAML, missing apiVersion/clusters, or pointing at a 127.0.0.1:<port> API endpoint that is not listening hanzo up / hanzo k8s restart
Local ports 8080, 9090, 1234, 8000 already bound on loopback lsof -nP -iTCP:8080 -sTCP:LISTEN (POSIX) or netstat -ano | findstr :8080 (Windows)
Runtime sockets orphaned *.sock / *.pid under ~/.hanzo (recorded pid is gone, or the socket has been untouched for more than 24h) rm -f <path> / del /f <path>

The whole pass is read-only: it reads the kubeconfig, probes the four ports with TcpListener::bind(("127.0.0.1", port)), and stats the runtime directories. No cluster, no network, no mutations.

Implementation notes

  • The analysis is a pure function over a Snapshot (kubeconfig body, busy_ports, sockets), so every branch is covered by deterministic unit tests that need neither a live cluster nor a real runtime layout — exactly what the acceptance criteria ask for.
  • No new dependencies: YAML reuse serde_norway (already a dependency for -o yaml), and liveness uses libc::kill(pid, 0) on unix, falling back to age-only detection on targets without a portable signal probe.
  • A FAIL from the kubeconfig check joins the existing failures counter, so hanzo doctor still exits non-zero when the config is unusable; port and socket collisions stay WARN because they do not necessarily break the current invocation.
  • The port list is deliberately limited to the four ports named in the issue. The runtime's own LOCAL_PORT / k3s API port are expected to be busy whenever the local cloud is up, so including them would only add noise.

Reproducible build/test output

$ cargo test --locked --bin hanzo doctor::

running 9 tests
test commands::doctor::tests::integrity_comparison_reads_bytes_not_filenames ... ok
test commands::doctor::tests::summarizes_mcp_doctor_report_and_flags_missing_computer ... ok
test commands::doctor::tests::detects_cross_org_beat_drift_and_401_auth_failure ... ok
test commands::doctor::tests::kubeconfig_self_check_reports_missing_malformed_and_healthy_states ... ok
test commands::doctor::tests::kubeconfig_without_version_or_clusters_fails ... ok
test commands::doctor::tests::localhost_server_ports_reads_both_host_forms_once_each ... ok
test commands::doctor::tests::busy_local_ports_are_reported_with_a_listener_command ... ok
test commands::doctor::tests::stale_sockets_are_reported_with_a_removal_command ... ok
test commands::doctor::tests::analyze_runtime_combines_all_three_check_families ... ok

test result: ok. 9 passed; 0 failed; 0 ignored; 0 measured; 523 filtered out; finished in 0.00s
$ cargo clippy --locked --bin hanzo
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 23.46s

clippy reports no lints for the new code; the eight dead-code warnings printed on Windows (describe, host_memory_mb, host_share, disk_verdict, QUIESCE_GUEST_SCRIPT, stop_and_save_in, warm_digest, mkdir/write_bytes/checkpoint/close_stdin) are pre-existing platform-gated items in up/vm and untouched by this PR.

Sample output

Runtime self-checks: kubeconfig, local ports, stale runtime sockets
OK   Kubeconfig: /home/u/.kube/hanzo.yaml is valid and its local API endpoint is listening
WARN Local port: 8080 is already in use — find the listener with `lsof -nP -iTCP:8080 -sTCP:LISTEN` and stop it before starting the runtime
WARN Runtime socket: /home/u/.hanzo/up/vm.sock is orphaned (untouched for more than 24h) — remove it with `rm -f /home/u/.hanzo/up/vm.sock`

Test plan

  • cargo test --locked --bin hanzo doctor:: — 9/9 pass, including five new tests
  • cargo clippy --locked --bin hanzo — no new lints
  • Manual: hanzo doctor with a valid kubeconfig, a busy 8080, and an orphaned pid file prints one OK and two WARN lines with runnable commands

…untime sockets

Adds three read-only self-checks to \hanzo doctor\ (issue hanzoai#111), each printing an exact one-line fix command:

- kubeconfig: missing file, invalid YAML, absent apiVersion/clusters, or a localhost API endpoint that is not listening
- local ports: 8080, 9090, 1234, 8000 already bound on loopback
- runtime sockets: orphaned *.sock / *.pid under ~/.hanzo (dead pid, or untouched for 24h)

The analysis is a pure function over a Snapshot (kubeconfig body, busy ports, socket states) so every branch is covered by deterministic unit tests that need neither a live cluster nor a real runtime directory. No new dependencies.
@zeekay

zeekay commented Nov 29, 2026

Copy link
Copy Markdown
Member

Thanks for working on #111, @Jonesc2321601! Closing this PR in favor of #121, which has now been merged. A few specific technical notes from our review for future reference:

  1. Kubeconfig endpoint probe: snapshot_runtime() only populated busy_ports from SELF_CHECK_PORTS (8080, 9090, 1234, 8000), whereas analyze_kubeconfig checked !busy_ports.contains(port) on the k3s API port (6443), causing healthy running clusters to always report 127.0.0.1:6443, which is not listening (and referencing hanzo k8s restart, which is not a CLI subcommand).
  2. Active Hanzo service port ownership & socket liveness: Port checks need to skip 8080/1234 when owned by active local Hanzo Cloud / hanzod / Engine services, and Unix socket health needs UnixStream::connect (rather than mtime > 24h, which flags live long-running daemons as orphaned while missing freshly crashed ECONNREFUSED sockets) without calling up::up_dir() (create_dir_all violates hanzo doctor's read-only HOME gate).
  3. Line endings: src/commands/doctor.rs was rewritten with CRLF (\r\n) line endings (+887/-481).

Appreciate your effort and detailed writeup!

@zeekay zeekay closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Intern] Expand hanzo doctor diagnostics (kubeconfig, port collisions, stale VM sockets)

2 participants