feat(daemon): serve bounded in-process build requests over the Unix socket
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 10s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 16s

This commit is contained in:
faumaray committed 2026-12-03 18:07:10 +03:00
1 parent 85f2ae274f
commit 55272d3e6b
8 files changed
+238 -28

No files matched your search

+125 -22
View File
@@ -3,6 +3,7 @@
#include <Void/Build.hpp>
#include <Void/Build/BootstrapConfig.hpp>
#include <Void/Build/Config/Frontend.hpp>
#include <Void/Build/Execution/Scheduler.hpp>
#include <Void/Build/Graph/Graph.hpp>
#include <Void/Build/State/StateStore.hpp>
#include <Void/Build/Toolchain/Driver.hpp>
@@ -47,6 +48,7 @@ constexpr auto kDescribeEvalTimeout = std::chrono::seconds{10};
constexpr std::size_t kMaxDescribeProfileBytes = 128U;
constexpr std::size_t kMaxDescribeTargetNames = 128U;
constexpr std::size_t kMaxDescribeTargetNameBytes = 200U;
constexpr std::size_t kMaxBuildTargets = 64U;
Void::Result::Error make_error(Void::Result::ErrorKind kind, std::string message,
int system_errno = 0) {
@@ -326,39 +328,48 @@ Config::FrontendOptions make_frontend_options(const DaemonOptions &options) {
return frontend;
}
// Evaluates the project description in-process and lowers it to a bounded graph summary.
// Workspace roots are rejected explicitly: member selection belongs to the CLI.
std::string describe_project(const DaemonOptions &options, std::string_view profile_name,
std::optional<CompilerFamily> compiler_override) {
struct EvaluatedProject {
ProfileContext context;
Graph::BuildGraph graph;
};
// Evaluates the project description in-process and lowers its graph. Workspace roots are
// rejected explicitly: member selection belongs to the CLI.
Void::Result::Result<EvaluatedProject, std::string>
evaluate_project_graph(const DaemonOptions &options, std::string_view profile_name,
std::optional<CompilerFamily> compiler_override, std::string_view op) {
if (Config::is_workspace_root(options.project_root)) {
return error_response("describe: workspace roots are not supported by the daemon yet");
return std::unexpected{
std::format("{}: workspace roots are not supported by the daemon yet", op)};
}
const auto frontend = make_frontend_options(options);
auto compiled = Config::compile_descriptor(frontend);
if (!compiled) {
return error_response(std::format("describe: {}", compiled.error().format()));
return std::unexpected{std::format("{}: {}", op, compiled.error().format())};
}
auto profiles = Config::discover_profiles(*compiled, frontend);
if (!profiles) {
return error_response(std::format("describe: {}", profiles.error().format()));
return std::unexpected{std::format("{}: {}", op, profiles.error().format())};
}
ProfileContext context;
EvaluatedProject evaluated;
try {
context = profiles->resolve(profile_name, compiler_override, Toolchain::host_os_name(),
Toolchain::host_architecture_name());
evaluated.context =
profiles->resolve(profile_name, compiler_override, Toolchain::host_os_name(),
Toolchain::host_architecture_name());
} catch (const std::exception &ex) {
return error_response(std::format("describe: {}", ex.what()));
return std::unexpected{std::format("{}: {}", op, ex.what())};
}
auto description = Config::evaluate_project(*compiled, context, frontend);
auto description = Config::evaluate_project(*compiled, evaluated.context, frontend);
if (!description) {
return error_response(std::format("describe: {}", description.error().format()));
return std::unexpected{std::format("{}: {}", op, description.error().format())};
}
auto toolchain = Toolchain::discover_and_probe(context, options.build_root / ".vbs" / "probes");
auto toolchain =
Toolchain::discover_and_probe(evaluated.context, options.build_root / ".vbs" / "probes");
if (!toolchain) {
return error_response(std::format("describe: {}", toolchain.error().format()));
return std::unexpected{std::format("{}: {}", op, toolchain.error().format())};
}
Graph::GraphLowerOptions lower_options;
@@ -366,12 +377,71 @@ std::string describe_project(const DaemonOptions &options, std::string_view prof
lower_options.build_root = options.build_root;
auto graph = Graph::lower_project_graph(*description, *toolchain, lower_options);
if (!graph) {
return error_response(std::format("describe: {}", graph.error().format()));
return std::unexpected{std::format("{}: {}", op, graph.error().format())};
}
evaluated.graph = std::move(*graph);
return evaluated;
}
// Bounded build-serving request: lowers the project graph and executes it in-process against the
// daemon's warm StateStore. Dependency materialization belongs to `vbs build`/`vbs deps`.
std::string build_project(const DaemonOptions &options, State::StateStore &state,
std::string_view profile_name,
std::optional<CompilerFamily> compiler_override,
const std::vector<std::string> &selected_targets, std::size_t jobs) {
auto evaluated = evaluate_project_graph(options, profile_name, compiler_override, "build");
if (!evaluated) {
return error_response(evaluated.error());
}
Execution::ExecutionOptions exec_options;
exec_options.jobs = jobs;
exec_options.selected_targets = selected_targets;
auto report = Execution::execute_graph(evaluated->graph, state, exec_options);
if (!report) {
return error_response(std::format("build: {}", report.error().format()));
}
JsonValue::Array failed_actions;
for (const auto &result : report->results) {
if (!result.succeeded) {
JsonValue::Object failure;
failure.emplace("action", JsonValue{result.action_id});
failure.emplace("target", JsonValue{result.target_name});
failure.emplace("error", JsonValue{result.error_message});
failed_actions.emplace_back(std::move(failure));
}
}
JsonValue::Object object;
object.emplace("ok", JsonValue{report->succeeded});
object.emplace("op", JsonValue{std::string{"build"}});
object.emplace("profile", JsonValue{evaluated->context.name});
object.emplace("targets",
JsonValue{static_cast<std::int64_t>(evaluated->graph.targets.size())});
object.emplace("actions",
JsonValue{static_cast<std::int64_t>(evaluated->graph.actions.size())});
object.emplace("executed", JsonValue{static_cast<std::int64_t>(report->executed_count)});
object.emplace("cached", JsonValue{static_cast<std::int64_t>(report->cached_count)});
object.emplace("failed", JsonValue{static_cast<std::int64_t>(report->failed_count)});
object.emplace("diagnostic", JsonValue{report->diagnostic});
object.emplace("failed_actions", JsonValue{std::move(failed_actions)});
return serialize_response(JsonValue{std::move(object)});
}
// Lowers the project graph to a bounded summary without executing it.
std::string describe_project(const DaemonOptions &options, std::string_view profile_name,
std::optional<CompilerFamily> compiler_override) {
auto evaluated = evaluate_project_graph(options, profile_name, compiler_override, "describe");
if (!evaluated) {
return error_response(evaluated.error());
}
const auto &graph = evaluated->graph;
const auto &context = evaluated->context;
std::vector<std::string> target_names;
target_names.reserve(graph->targets.size());
for (const auto &target : graph->targets) {
target_names.reserve(graph.targets.size());
for (const auto &target : graph.targets) {
target_names.push_back(target.name);
}
std::ranges::sort(target_names);
@@ -395,8 +465,8 @@ std::string describe_project(const DaemonOptions &options, std::string_view prof
object.emplace("ok", JsonValue{true});
object.emplace("op", JsonValue{std::string{"describe"}});
object.emplace("profile", JsonValue{context.name});
object.emplace("targets", JsonValue{static_cast<std::int64_t>(graph->targets.size())});
object.emplace("actions", JsonValue{static_cast<std::int64_t>(graph->actions.size())});
object.emplace("targets", JsonValue{static_cast<std::int64_t>(graph.targets.size())});
object.emplace("actions", JsonValue{static_cast<std::int64_t>(graph.actions.size())});
object.emplace("target_names", JsonValue{std::move(names_json)});
if (truncated) {
object.emplace("target_names_truncated", JsonValue{true});
@@ -511,7 +581,7 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
if (*name == "status") {
return {status_response(), false};
}
if (*name == "describe") {
if (*name == "describe" || *name == "build") {
std::string profile_name{"debug"};
if (const auto entry = object->find("profile"); entry != object->end()) {
const auto *value = std::get_if<std::string>(&entry->second.value);
@@ -538,7 +608,40 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
return {error_response(ex.what()), false};
}
}
return {describe_project(options, profile_name, compiler_override), false};
if (*name == "describe") {
return {describe_project(options, profile_name, compiler_override), false};
}
std::vector<std::string> selected_targets;
if (const auto entry = object->find("targets"); entry != object->end()) {
const auto *array = std::get_if<JsonValue::Array>(&entry->second.value);
if (array == nullptr) {
return {error_response("\"targets\" must be an array of strings"), false};
}
if (array->size() > kMaxBuildTargets) {
return {error_response("\"targets\" has too many entries"), false};
}
for (const auto &item : *array) {
const auto *text = std::get_if<std::string>(&item.value);
if (text == nullptr || text->empty() ||
text->size() > kMaxDescribeTargetNameBytes) {
return {error_response("\"targets\" entries must be non-empty bounded strings"),
false};
}
selected_targets.push_back(*text);
}
}
std::size_t jobs = 0;
if (const auto entry = object->find("jobs"); entry != object->end()) {
const auto *value = std::get_if<std::int64_t>(&entry->second.value);
if (value == nullptr || *value < 0 || *value > 256) {
return {error_response("\"jobs\" must be an integer in [0, 256]"), false};
}
jobs = static_cast<std::size_t>(*value);
}
return {build_project(options, state_store, profile_name, compiler_override,
selected_targets, jobs),
false};
}
if (*name == "shutdown") {
return {ok_response("shutdown"), true};
+28 -2
View File
@@ -1580,6 +1580,8 @@ struct DaemonCmdOptions {
std::optional<std::string> build_arg;
std::optional<std::string> socket_arg;
std::string profile_arg{"debug"};
std::vector<std::string> targets;
std::string jobs_arg{"0"};
std::string max_bytes_arg{"0"};
std::string max_requests_arg{"0"};
};
@@ -1659,6 +1661,22 @@ 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 == "build") {
std::size_t jobs = 0;
const auto [ptr, ec] = std::from_chars(opts.jobs_arg.data(),
opts.jobs_arg.data() + opts.jobs_arg.size(), jobs);
if (ec != std::errc{} || ptr != opts.jobs_arg.data() + opts.jobs_arg.size() || jobs > 256) {
std::cerr << "vbs daemon build: invalid --jobs value '" << opts.jobs_arg << "'\n";
return 2;
}
if (opts.profile_arg.empty()) {
std::cerr << "vbs daemon build: --profile must not be empty\n";
return 2;
}
const std::string targets_json = Util::json_array(
opts.targets, [](const std::string &target) { return Util::json_quote(target); });
request_json = std::format(R"({{"op":"build","profile":{},"targets":{},"jobs":{}}})",
Util::json_quote(opts.profile_arg), targets_json, jobs);
} else {
std::cerr << "vbs daemon: unknown command '" << sub << "'\n";
return 2;
@@ -1666,8 +1684,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 == "describe" ? std::chrono::milliseconds{15000} : std::chrono::milliseconds{2000};
const auto request_timeout = sub == "build" ? std::chrono::milliseconds{std::chrono::hours{1}}
: sub == "describe" ? std::chrono::milliseconds{15000}
: std::chrono::milliseconds{2000};
auto response = Daemon::request(socket_path, request_json, request_timeout);
if (!response) {
std::cerr << "vbs daemon: " << response.error().message() << '\n';
@@ -2600,6 +2619,13 @@ int main(int argc, char **argv) {
configure_daemon_common(daemon_describe_cmd);
daemon_describe_cmd.option_default("--profile", "-p", "Profile to evaluate",
daemon_opts.profile_arg, "debug");
auto &daemon_build_cmd = daemon_cmd.subcommand("build", "Build a project graph via the daemon");
configure_daemon_common(daemon_build_cmd);
daemon_build_cmd
.option_default("--profile", "-p", "Profile to build", daemon_opts.profile_arg, "debug")
.option_many("--target", "-t", "Target to build", daemon_opts.targets)
.option_default("--jobs", "-j", "Maximum parallel jobs (0 = hardware concurrency)",
daemon_opts.jobs_arg, "0");
const auto parsed = root.parse_detailed(argc, argv);
if (parsed.is_help()) {