feat(daemon): add editor-extension watch poll protocol
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 7s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 1m19s

This commit is contained in:
faumaray committed 2026-12-03 20:45:09 +03:00
1 parent e15e2cb2b4
commit c7a6bad3c0
9 files changed
+122 -14

No files matched your search

+48 -9
View File
@@ -57,6 +57,7 @@ constexpr std::size_t kMaxDescribeProfileBytes = 128U;
constexpr std::size_t kMaxDescribeTargetNames = 128U;
constexpr std::size_t kMaxDescribeTargetNameBytes = 200U;
constexpr std::size_t kMaxBuildTargets = 64U;
constexpr std::size_t kMaxPendingChanges = 4096U;
Void::Result::Error make_error(Void::Result::ErrorKind kind, std::string message,
int system_errno = 0) {
@@ -769,6 +770,7 @@ struct DaemonServer::Impl {
[[nodiscard]] DaemonStatus current_status() const;
[[nodiscard]] std::string status_response() const;
DispatchResult dispatch(std::string_view line);
void record_watch_batch(const Watch::WatchBatch &batch);
void close_socket() noexcept {
if (listen_fd >= 0) {
@@ -798,6 +800,7 @@ struct DaemonServer::Impl {
std::uint64_t watch_events{0};
std::string last_event_path;
std::uint64_t sequence{0};
std::vector<std::filesystem::path> pending_changes;
};
DaemonStatus DaemonServer::Impl::current_status() const {
@@ -813,6 +816,28 @@ DaemonStatus DaemonServer::Impl::current_status() const {
return status;
}
void DaemonServer::Impl::record_watch_batch(const Watch::WatchBatch &batch) {
if (batch.overflowed || batch.rescan_required) {
static_cast<void>(watcher.rescan_watches());
++sequence;
}
if (batch.changed_paths.empty()) {
return;
}
watch_events += batch.changed_paths.size();
last_event_path = batch.changed_paths.back().string();
++sequence;
for (const auto &path : batch.changed_paths) {
pending_changes.push_back(path);
}
if (pending_changes.size() > kMaxPendingChanges) {
pending_changes.erase(
pending_changes.begin(),
pending_changes.begin() +
static_cast<std::ptrdiff_t>(pending_changes.size() - kMaxPendingChanges));
}
}
std::string DaemonServer::Impl::status_response() const {
const DaemonStatus status = current_status();
JsonValue::Object object;
@@ -854,6 +879,28 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
if (*name == "status") {
return {status_response(), false};
}
if (*name == "watch_poll") {
auto batch =
watcher.poll_events(std::chrono::milliseconds{0}, std::chrono::milliseconds{0});
if (!batch) {
return {error_response(std::format("watch_poll: {}", batch.error().format())), false};
}
record_watch_batch(*batch);
JsonValue::Array changed;
for (const auto &path : pending_changes) {
changed.emplace_back(path.string());
}
pending_changes.clear();
JsonValue::Object object;
object.emplace("ok", JsonValue{true});
object.emplace("op", JsonValue{std::string{"watch_poll"}});
object.emplace("sequence", JsonValue{static_cast<std::int64_t>(sequence)});
object.emplace("watch_events", JsonValue{static_cast<std::int64_t>(watch_events)});
object.emplace("changed", JsonValue{std::move(changed)});
object.emplace("overflowed", JsonValue{batch->overflowed});
object.emplace("rescan_required", JsonValue{batch->rescan_required});
return {serialize_response(JsonValue{std::move(object)}), false};
}
if (*name == "describe" || *name == "build" || *name == "ide_query" ||
*name == "ide_generate" || *name == "compdb_export") {
std::string profile_name{"debug"};
@@ -1195,15 +1242,7 @@ Void::Result::Result<void, Void::Result::Error> DaemonServer::run(std::stop_toke
// Watcher failures are surfaced through the next scan; the daemon stays alive.
continue;
}
if (batch->overflowed || batch->rescan_required) {
static_cast<void>(impl.watcher.rescan_watches());
++impl.sequence;
}
if (!batch->changed_paths.empty()) {
impl.watch_events += batch->changed_paths.size();
impl.last_event_path = batch->changed_paths.back().string();
++impl.sequence;
}
impl.record_watch_batch(*batch);
}
impl.close_socket();
return {};
+11 -2
View File
@@ -1445,6 +1445,8 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
}
request_json =
std::format(R"({{"op":"describe","profile":{}}})", Util::json_quote(opts.profile_arg));
} else if (sub == "watch_poll") {
request_json = R"({"op":"watch_poll"})";
} else if (sub == "ide_generate") {
if (opts.profile_arg.empty()) {
std::cerr << "vbs daemon ide generate: --profile must not be empty\n";
@@ -1492,8 +1494,9 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
// Describe evaluates the whole project graph (descriptor compile, toolchain probe, lowering),
// which can exceed the 2 s default; the daemon bounds evaluation at 10 s.
const auto request_timeout =
sub == "build" ? std::chrono::milliseconds{std::chrono::hours{1}}
: sub == "describe" ? std::chrono::milliseconds{15000}
sub == "build" ? std::chrono::milliseconds{std::chrono::hours{1}}
: sub == "watch_poll" ? std::chrono::milliseconds{5000}
: sub == "describe" ? std::chrono::milliseconds{15000}
: (sub == "ide_query" || sub == "ide_generate" || sub == "compdb_export")
? std::chrono::milliseconds{30000}
: std::chrono::milliseconds{2000};
@@ -2453,6 +2456,9 @@ int main(int argc, char **argv) {
configure_daemon_common(daemon_ide_query_cmd);
daemon_ide_query_cmd.option_default("--profile", "-p", "Profile to query",
daemon_opts.profile_arg, "debug");
auto &daemon_ide_poll_cmd = daemon_ide_cmd.subcommand(
"poll", "Poll pending project file changes (editor extension protocol)");
configure_daemon_common(daemon_ide_poll_cmd);
auto &daemon_ide_generate_cmd =
daemon_ide_cmd.subcommand("generate", "Write editor configuration files");
configure_daemon_common(daemon_ide_generate_cmd);
@@ -2560,6 +2566,9 @@ int main(int argc, char **argv) {
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
parsed.matched_command_path[2] == "generate") {
daemon_sub = "ide_generate";
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
parsed.matched_command_path[2] == "poll") {
daemon_sub = "watch_poll";
} else if (daemon_sub == "compdb" && parsed.matched_command_path.size() > 2 &&
parsed.matched_command_path[2] == "export") {
daemon_sub = "compdb_export";