heph.nvim: plug-and-play managed daemon (autostart, self-heal, client/server guardrail)

The plugin now manages its own hephd by default (autostart = true): if nothing
is serving the socket it spawns a local daemon against the default XDG paths,
kills only what it spawned on VimLeavePre, and self-heals — rpc.call retries
once through a respawn hook when the connection drops (the prior owner releases
the DB lock on exit, so a respawn can claim it).

- daemon.ensure() connects to an already-running daemon (any mode) or spawns one
  we own; stop_spawned()/is_managed() track lifecycle.
- A server/client daemon you started is always respected (spawn only when nothing
  serves the socket). autostart = false → connect-only, warns/errors if down,
  and clears the self-heal hook so it fails loudly.
- config: autostart defaults true; new `db` option; $HEPH_SOCKET / $HEPH_DB
  fallbacks isolate a dev Neovim onto a separate daemon + DB.

e2e: managed_daemon_spec covers autostart spawn, self-heal-after-kill, and
connect-only error. 10 specs green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erich Blume 2026-06-02 09:32:32 -07:00
commit e3db2ac550
11 changed files with 277 additions and 18 deletions

View file

@ -6,6 +6,10 @@ local uv = vim.uv or vim.loop
local M = {}
-- The daemon THIS nvim spawned (nil if we connected to an existing one).
-- `{ handle, exited = { done }, socket, db, bin }`.
M._managed = nil
--- Spawn a `local`-mode hephd against `opts.db` listening on `opts.socket`.
--- `opts.bin` defaults to `hephd` on PATH. Returns `{ handle, pid }`.
function M.spawn(opts)
@ -55,4 +59,71 @@ function M.wait_ready(socket, timeout)
return true
end
--- Ensure a daemon is reachable at `opts.socket`. If one is already serving the
--- socket (any mode — local/server/client), connect to it and do NOT spawn. Else
--- if `opts.autostart`, spawn a local hephd we own (and manage its lifecycle).
--- Returns `reachable, spawned_by_us`.
function M.ensure(opts)
-- Already serving? A quick probe respects a daemon someone else started.
if M.wait_ready(opts.socket, opts.probe_ms or 400) then
return true, false
end
if not opts.autostart then
return false, false
end
local exited = { done = false }
local d = M.spawn({
bin = opts.bin,
socket = opts.socket,
db = opts.db,
on_exit = function()
exited.done = true
end,
})
local ok, reason = M.wait_ready(opts.socket, opts.ready_ms or 5000)
if not ok then
pcall(function()
if not d.handle:is_closing() then
d.handle:kill("sigterm")
end
end)
error("heph: spawned hephd but it never became ready: " .. tostring(reason))
end
M._managed = {
handle = d.handle,
exited = exited,
socket = opts.socket,
db = opts.db,
bin = opts.bin,
}
return true, true
end
--- True if this nvim currently owns a live spawned daemon.
function M.is_managed()
return M._managed ~= nil and not M._managed.exited.done
end
--- Stop the daemon this nvim spawned (no-op if we connected to an existing one).
function M.stop_spawned()
local m = M._managed
if not m then
return
end
M._managed = nil
if m.handle and not m.exited.done then
pcall(function()
m.handle:kill("sigterm")
end)
vim.wait(2000, function()
return m.exited.done
end, 20)
end
pcall(function()
if m.handle and not m.handle:is_closing() then
m.handle:close()
end
end)
end
return M