Skip to content

feat(doctor): diagnose kubeconfig validity, port collisions, and stale sockets - #121

Merged
zeekay merged 2 commits into
hanzoai:mainfrom
tranhoangtu-it:feat/doctor-kubeconfig-ports-sockets-111
Nov 29, 2026
Merged

zeekay merged 2 commits into
hanzoai:mainfrom
tranhoangtu-it:feat/doctor-kubeconfig-ports-sockets-111

Conversation

@tranhoangtu-it

Copy link
Copy Markdown
Contributor

Closes #111
/claim #111

Summary

  • Added three local host diagnostic checks to hanzo doctor in src/commands/doctor.rs:
    1. Kubeconfig validation (diagnose_kubeconfig_path): Checks ~/.kube/hanzo.yaml and commands::up::kubeconfig_path() when present, catching empty/unreadable/malformed YAML, missing clusters/contexts/users/server, and unreachable loopback endpoints, and printing exact rm -f <path> && hanzo start / hanzo start recovery commands.
    2. Default local port collisions (diagnose_port_collisions): Probes 127.0.0.1 on ports 8080 (Hanzo Cloud), 9090 (Metrics / Prometheus), 1234 (Hanzo Engine), and 8000 (IAM / Gateway), skipping ports owned by active local Hanzo services (local_engine_healthy(), local_kube_client().cloud_replicas()) and printing OS-specific one-line inspect/free commands (lsof/kill on Unix, netstat -ano | findstr :<port> on Windows).
    3. Stale runtime sockets (diagnose_runtime_sockets): Inspects hanzod.zap.sock (commands::host::data_dir()), ~/.hanzo/up/{vm,supervisor}.sock, and ~/.hanzo/vm/hanzo-vm.sock alongside companion .pid files via commands::vm::alive, flagging orphaned sockets with rm -f <socket> && hanzo start.
  • Fixed Windows debug CLI execution in src/main.rs (#[cfg(windows)] 8 MiB main-thread & tokio worker stack trampoline) and #[cfg(not(unix))] type annotation in src/commands/up/diagnose.rs.
  • Documented the expanded hanzo doctor checks and checked ports in README.md.

Reproducible Verification

$ cargo test --locked --bin hanzo commands::doctor
running 6 tests
test commands::doctor::tests::detects_cross_org_beat_drift_and_401_auth_failure ... ok
test commands::doctor::tests::summarizes_mcp_doctor_report_and_flags_missing_computer ... ok
test commands::doctor::tests::port_collision_check_reports_unowned_busy_ports_and_skips_active_hanzo_ports ... ok
test commands::doctor::tests::integrity_comparison_reads_bytes_not_filenames ... ok
test commands::doctor::tests::runtime_socket_check_detects_orphaned_vs_live_sockets ... ok
test commands::doctor::tests::kubeconfig_validation_distinguishes_malformed_stale_and_reachable ... ok
test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 492 filtered out; finished in 0.26s

$ cargo test --locked --workspace --lib
hanzo-client: 3 passed; 0 failed
hanzo-event: 8 passed; 0 failed
hanzo-identity: 27 passed; 0 failed

$ cargo test --locked --test cli agent_run_offers_an_async_path_without_changing_the_default
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 26 filtered out; finished in 0.71s

$ cargo clippy --locked --bin hanzo --tests
Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.50s (exit code 0; 0 warnings in touched files)

$ NO_COLOR=1 target/debug/hanzo doctor --offline
Hanzo installation diagnostics
...
Local 3-layer lifecycle diagnostics
WARN [infrastructure] hanzod: not installed; run `hanzo upgrade hanzod`
INFO [infrastructure] Hanzo Runtime: hanzo-vm bootstraps automatically on first `hanzo start` / `hanzo up`
INFO [infrastructure] k3s kubeconfig: C:\Users\tranh\.kube\hanzo.yaml not created yet (`hanzo start` writes it)
INFO [platform] Hanzo Cloud: stopped locally (`hanzo up` to start)
INFO [platform] Hanzo Engine: standby (`hanzo up --ai-only` or `hanzo model run <model>` to start)
OK   Local Ports: default ports 8080, 9090, 1234, 8000 are free of conflicts
INFO Authentication not checked in offline mode
OK   NVIDIA GPU: NVIDIA RTX A2000, 582.16

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-28T09:18:09.071697Z
We couldn’t complete this review. Comment @codex review to try again.
774d22f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@zeekay

zeekay commented Nov 29, 2026

Copy link
Copy Markdown
Member

Thanks for tackling #111 — the kubeconfig YAML/loopback check and the Windows 8 MiB main-thread stack fix in src/main.rs are solid. Before we can merge, please rebase onto main (v8.5.1495 / d6686fca, which already carries the src/commands/up/diagnose.rs Option<(u64, u64)> type annotation) and fix four concrete issues in src/commands/doctor.rs:

  1. Keep hanzo doctor strictly read-only (host::data_dir() mutates disk): default_socket_candidates() calls crate::commands::host::data_dir(), which runs std::fs::create_dir_all(&dir) under ${XDG_DATA_HOME}/hanzo. doctor (//! Read-only installation checks) must never create directories on disk (and hanzo.yml enforces an empty/read-only HOME gate). Compute the candidate path read-only from dirs::data_local_dir() without create_dir_all, and keep host::data_dir() private.
  2. Use the actual runtime control socket filename: default_socket_candidates() checks vm.sock, supervisor.sock, and ~/.hanzo/vm/hanzo-vm.sock, none of which exist in this codebase — the supervisor control socket is ~/.hanzo/up/control.sock (src/commands/up/control.rs:30: const SOCKET: &str = "control.sock"), paired with supervisor.pid.
  3. Fix dead-socket false OK in diagnose_runtime_sockets: if sock_live || pid_live reports "OK " even when UnixStream::connect fails with ECONNREFUSED if the PID was recycled or the listener died. On Unix, require sock_live for socket health (and bind a real UnixListener in the unit test rather than std::fs::write(&live_sock, b"")).
  4. Account for active hanzod on port 8080 & keep doctor.rs modular: In run_local_host_diagnostics(), also treat port 8080 as Hanzo-owned when host::running_pid().is_some() or daemon::probe_dev_health().await.is_some() (not only k3s cloud_replicas() > 0), and extract the new host checks into a submodule (e.g. src/commands/doctor/host.rs) so doctor.rs (971 lines here) stays well under 1,000 lines.

@tranhoangtu-it
tranhoangtu-it force-pushed the feat/doctor-kubeconfig-ports-sockets-111 branch from 774d22f to 2a38a7f Compare November 29, 2026 03:13
@tranhoangtu-it

Copy link
Copy Markdown
Contributor Author

Thanks @zeekay for the detailed review! Rebased cleanly onto origin/main (37f7df43) and resolved all four items:

  1. Strictly read-only hanzo doctor & private host::data_dir(): Reverted crate::commands::host::data_dir() back to private, updated host::running_pid() and Unix daemon::probe_no_spawn() to resolve ${XDG_DATA_HOME}/hanzo read-only without create_dir_all, and kept default_socket_candidates() completely read-only.
  2. Actual supervisor control socket: Replaced the non-existent vm.sock / supervisor.sock / hanzo-vm.sock paths with ~/.hanzo/up/control.sock paired with supervisor.pid (plus hanzod.zap.sock / hanzod.pid).
  3. Eliminated dead-socket false OK on Unix (ECONNREFUSED): diagnose_runtime_sockets() now requires UnixStream::connect to succeed on Unix (warning even when the companion pidfile points to a live PID), and unit tests bind a real UnixListener plus verify WARN after dropping the listener.
  4. Accounted for active hanzod on port 8080 & modularized host checks: Extracted local kubeconfig, port-collision, and runtime-socket diagnostics (plus unit tests) into src/commands/doctor/host.rs (doctor.rs is 492 lines, doctor/host.rs is 579 lines), and added active_hanzo_ports() marking port 8080 owned when host::running_pid().is_some() or daemon::probe_dev_health().await.is_some().

Verified on both Windows and Linux (WSL2): cargo clippy --locked --all-targets -- -D warnings (0 warnings/errors), commands::doctor (7/7 passed on both platforms), and read-only HOME (chmod 0555) hanzo doctor --offline (exit 0).

@tranhoangtu-it
tranhoangtu-it force-pushed the feat/doctor-kubeconfig-ports-sockets-111 branch from 2a38a7f to 145019d Compare November 29, 2026 16:42
@zeekay

zeekay commented Nov 29, 2026

Copy link
Copy Markdown
Member

Thanks @tranhoangtu-it for addressing every review item cleanly! Verified cargo clippy --locked --all-targets -- -D warnings and read-only HOME (chmod 0555) commands::doctor tests (7/7 passed). Merging!

@zeekay
zeekay merged commit 7f73045 into hanzoai:main Nov 29, 2026
1 check passed
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