@@ -426,7 +426,7 @@ func ensureServer(cmd *cobra.Command, hostURL *url.URL) error {
426426// duration of "spawn + readiness probe" and released before the caller
427427// resumes its normal lifetime.
428428func spawnAndWaitReady(cmd *cobra.Command, hostURL *url.URL) error {
429− chDir, err := perHostServerDir()
429+ chDir, err := perHostServerDir(hostURL)
430430 if err != nil {
431431 return err
432432 }
@@ -435,7 +435,7 @@ func spawnAndWaitReady(cmd *cobra.Command, hostURL *url.URL) error {
435435 // If the lock itself is unavailable, fall back to the
436436 // unsynchronized path rather than blocking the user.
437437 slog.Warn("Failed to acquire spawn lock, proceeding without single-flight", "error", err)
438− if err := startDetachedServer(cmd); err != nil {
438+ if err := startDetachedServer(cmd, hostURL); err != nil {
439439 return err
440440 }
441441 return waitForServerReady(cmd.Context(), hostURL)
@@ -452,7 +452,7 @@ func spawnAndWaitReady(cmd *cobra.Command, hostURL *url.URL) error {
452452 return nil
453453 }
454454
455− if err := startDetachedServer(cmd); err != nil {
455+ if err := startDetachedServer(cmd, hostURL); err != nil {
456456 return err
457457 }
458458 return waitForServerReady(cmd.Context(), hostURL)
@@ -469,17 +469,25 @@ func quickHealthProbe(ctx context.Context, hostURL *url.URL) error {
469469}
470470
471471// perHostServerDir returns (and creates) the cache directory used for
472−// per-host server state (logs, start.lock, etc.). It mirrors the path
473−// computed in startDetachedServer so both code paths stay in sync.
474−func perHostServerDir() (string, error) {
475− safeClientHost := safeNameRegexp.ReplaceAllString(clientHost, "_")
476− chDir := filepath.Join(config.GlobalCacheDir(), "server-"+safeClientHost)
472+// per-host server state (logs, start.lock, etc.). The path is derived
473+// from the parsed host URL rather than the global flag so the same key
474+// is computed regardless of where the host came from.
475+func perHostServerDir(hostURL *url.URL) (string, error) {
476+ chDir := filepath.Join(config.GlobalCacheDir(), "server-"+safeHostName(hostURL))
477477 if err := os.MkdirAll(chDir, 0o700); err != nil {
478478 return "", fmt.Errorf("failed to create server working directory: %v", err)
479479 }
480480 return chDir, nil
481481}
482482
483+// safeHostName returns a filesystem-safe identifier for hostURL,
484+// suitable for use as a directory name. It mirrors the input shape of
485+// the --host flag so client and server compute the same key.
486+func safeHostName(hostURL *url.URL) string {
487+ return safeNameRegexp.ReplaceAllString(
488+ hostURL.Scheme+"://"+hostURL.Host+hostURL.Path, "_")
489+}
490+
483491// serverReadyTimeout returns the total budget for the readiness probe.
484492// Overridable via CRUSH_SERVER_READY_TIMEOUT (parsed as a Go duration).
485493func serverReadyTimeout() time.Duration {
@@ -631,13 +639,13 @@ func restartIfStale(cmd *cobra.Command, hostURL *url.URL) (restarted bool, err e
631639
632640var safeNameRegexp = regexp.MustCompile(`[^a-zA-Z0-9._-]`)
633641
634−func startDetachedServer(cmd *cobra.Command) error {
642+func startDetachedServer(cmd *cobra.Command, hostURL *url.URL) error {
635643 exe, err := os.Executable()
636644 if err != nil {
637645 return fmt.Errorf("failed to get executable path: %v", err)
638646 }
639647
640− chDir, err := perHostServerDir()
648+ chDir, err := perHostServerDir(hostURL)
641649 if err != nil {
642650 return err
643651 }