--- Locate, spawn, and wait on a `hephd` daemon. Shared by optional autostart --- and by the e2e harness (so test readiness uses the same definition the --- plugin does). local uv = vim.uv or vim.loop local M = {} --- 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) local args = { "--mode", "local" } if opts.db then table.insert(args, "--db") table.insert(args, opts.db) end if opts.socket then table.insert(args, "--socket") table.insert(args, opts.socket) end local handle, pid = uv.spawn(opts.bin or "hephd", { args = args, stdio = { nil, nil, opts.stderr }, }, function(code, signal) if opts.on_exit then opts.on_exit(code, signal) end end) if not handle then error("heph: failed to spawn hephd (bin=" .. (opts.bin or "hephd") .. ")") end return { handle = handle, pid = pid } end --- Wait until `socket` both exists and accepts a real RPC (`health`). The --- existence check alone races the daemon's bind→accept, so we prove liveness --- with a round-trip on a throwaway session. Returns `true`, or `false, reason`. function M.wait_ready(socket, timeout) timeout = timeout or 5000 if not vim.wait(timeout, function() return uv.fs_stat(socket) ~= nil end, 20) then return false, "socket never appeared: " .. socket end local session = require("heph.rpc").new_session(socket) local ok = vim.wait(timeout, function() return pcall(function() session:call("health", vim.empty_dict(), { timeout = 200 }) end) end, 50) session:close() if not ok then return false, "socket present but not accepting rpc: " .. socket end return true end return M