Skip to content

feat: pidfile-based socket discovery (#47, phase 1)#53

Merged
Cannon07 merged 1 commit intomainfrom
feat/pidfile-socket-discovery
May 10, 2026
Merged

feat: pidfile-based socket discovery (#47, phase 1)#53
Cannon07 merged 1 commit intomainfrom
feat/pidfile-socket-discovery

Conversation

@Cannon07
Copy link
Copy Markdown
Owner

Phase 1 of #47 — replaces OS-specific socket-glob discovery with a self-registered pidfile that each nvim writes on setup().

Summary

  • Each nvim running code-preview.setup() writes ~/.local/state/code-preview/sockets/<pid> containing its RPC socket path and current cwd (two lines).
  • bin/nvim-socket.sh checks this directory first, validates the socket is responsive, applies the same match-or-parent-cwd rule as before, and falls back to the existing /var/folders / /tmp / $XDG_RUNTIME_DIR discovery if no pidfile matches.
  • :CodePreviewStatus and :checkhealth code-preview both surface pidfile state.

Why

The glob-based discovery relies on lsof, compgen, kill -0, and /proc — none of which exist natively on Windows. This is the main blocker for #46. The pidfile is computable from any platform and stores cwd directly, eliminating the lsof lookup that today returns the process's startup cwd rather than the user's current :cd.

Behavior preserved

  • Multi-instance routing via the same match-or-parent-cwd rule
  • NVIM_LISTEN_ADDRESS override (still checked first)
  • Single-instance / fallback case (legacy globs still run when no pidfile matches, so users running an older nvim alongside the upgraded plugin keep working)

Behavior improved

  • Liveness check is now socket-connect, not kill -0 — catches hung nvim with a live PID
  • cwd is read live from nvim and refreshed on DirChanged, so :cd-ing to a different project routes correctly without restarting nvim

Scope

This is phase 1 of a four-phase migration. The pidfile itself is permanent — phases 2–4 (orchestration porting to Lua) will continue to read from the same pidfile dir. nvim-socket.sh will be removed entirely in a later phase.

Out of scope here:

  • Windows-portability of the bash itself (-S test, path separators) — not worth it on a file we're deleting
  • Phase 2+ orchestration changes

Test plan

  • Pidfile written with correct socket + cwd on setup()
  • DirChanged refreshes cwd line on :cd
  • VimLeavePre removes the pidfile on :qa
  • nvim-socket.sh resolves the right socket via pidfile (single instance)
  • Multi-instance routing — two nvims at different cwds, each gets its own edits
  • Subdirectory match — cd repo/lua still resolves to nvim opened at repo/
  • Fallback to legacy globs when pidfile dir is empty/missing
  • End-to-end Claude Code edit lands in the correct nvim
  • :CodePreviewStatus shows pidfile path
  • :checkhealth code-preview shows pidfile registration status

🤖 Generated with Claude Code

Each nvim instance running setup() now self-registers its RPC socket and
cwd to ~/.local/state/code-preview/sockets/<pid>. nvim-socket.sh checks
this directory first, falling back to the existing /var/folders, /tmp,
and XDG_RUNTIME_DIR globs for nvim instances that haven't been restarted.

Why: the glob-based discovery is OS-specific (relies on lsof, compgen,
kill -0, /proc) and is the main blocker for Windows support tracked in
\#46. The pidfile is computable from any platform without those tools and
contains the cwd directly, eliminating the lsof lookup that today gives
the startup cwd rather than the user's current :cd.

Behavior preserved:
- Multi-instance routing via the same match-or-parent-cwd rule
- NVIM_LISTEN_ADDRESS override
- Single-instance / fallback case (legacy globs still run when no
  pidfile matches)

Behavior improved:
- Liveness check is socket-connect, not kill -0 — catches hung nvim
- cwd is read live from nvim, refreshed on DirChanged

This is phase 1 of a four-phase migration to Lua-based orchestration.
The pidfile itself is permanent; nvim-socket.sh will be removed in a
later phase when orchestration moves entirely into Lua.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@Cannon07 Cannon07 merged commit 54ed050 into main May 10, 2026
2 checks passed
@Cannon07 Cannon07 deleted the feat/pidfile-socket-discovery branch May 10, 2026 10:48
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.

1 participant