hephaestus/heph.nvim/lua/heph/init.lua
Erich Blume e3db2ac550 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>
2026-06-02 09:37:49 -07:00

62 lines
1.8 KiB
Lua

--- heph.nvim — the primary surface for hephaestus (tech-spec §8): an
--- obsidian.nvim replacement that is a thin client of the local `hephd` over
--- its unix-socket JSON-RPC. This module is the public entry point.
local config = require("heph.config")
local M = {}
--- The resolved config from the last `setup` (nil before setup).
M.config = nil
--- Configure the plugin. `opts.socket` overrides the daemon socket path;
--- `opts.keymaps = false` disables the default keymaps. Idempotent.
function M.setup(opts)
local cfg = vim.tbl_deep_extend("force", config.defaults, opts or {})
cfg.socket = config.resolve_socket(cfg.socket)
cfg.db = config.resolve_db(cfg.db)
M.config = cfg
local rpc = require("heph.rpc")
local daemon = require("heph.daemon")
rpc.setup(cfg.socket)
if cfg.autostart then
-- Plug-and-play: bring up a managed local daemon if none is serving, and
-- self-heal a dropped connection on later calls.
local ok = pcall(daemon.ensure, {
socket = cfg.socket,
db = cfg.db,
bin = cfg.bin,
autostart = true,
})
if not ok then
require("heph.util").notify(
"could not start hephd; will retry on first use",
vim.log.levels.WARN
)
end
rpc.set_respawn(function()
pcall(daemon.ensure, {
socket = cfg.socket,
db = cfg.db,
bin = cfg.bin,
autostart = true,
})
end)
else
-- Explicit architecture: connect only, never spawn over the user's daemon.
rpc.set_respawn(nil)
if not daemon.ensure({ socket = cfg.socket, autostart = false }) then
require("heph.util").notify(
"no hephd reachable at " .. cfg.socket .. " (autostart disabled)",
vim.log.levels.WARN
)
end
end
config.apply_keymaps(cfg)
return M
end
return M