From 1ae72e4d07c8f591ae7bb8ae1e7766c30c053494 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sat, 5 Dec 2026 11:08:23 +0300 Subject: [PATCH] feat(registry): add the remote worker lease and result protocol --- docs/agent-workflow/STATE.md | 2 + docs/agent-workflow/TASKS.md | 1 + docs/agent-workflow/VERIFICATION.md | 11 + docs/roadmap/22-superiority-and-gaps.md | 2 +- .../Void/Build/Registry/RegistryService.hpp | 20 ++ registry/service/RegistryService.cpp | 202 ++++++++++++++++++ tests/RegistryTests.cpp | 99 +++++++++ 7 files changed, 336 insertions(+), 1 deletion(-) diff --git a/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index c7f2a4d..738a9f7 100644 --- a/docs/agent-workflow/STATE.md +++ b/docs/agent-workflow/STATE.md @@ -336,6 +336,8 @@ Stage: VERIFIED — Plans 01–19 + Plan 20 Slices 1–2C + Track A + Track B (B - **Wave 121 (Coverage-Guided Fuzzing with Clang/libFuzzer)**: added `fuzz/fuzz_parsers.cpp` (JSON, SemVer range/strict) plus `fuzz/run.sh` (Clang 23 `-fsanitize=fuzzer,address,undefined`, seeded corpus, `VOID_INCLUDE_DIR`). First run: 1,541,020 executions in 31 s with no findings, and libFuzzer produced a recommended dictionary. TOML/lockfile/tar stay covered by the deterministic mutation harness because their implementations are not header-only. +- **Wave 122 (Remote Execution Worker Protocol)**: `vbs-registry` now exposes an in-memory worker/lease protocol on the existing CAS - `POST /api/v1/workers` (registration), `/workers//heartbeat`, `GET /workers`, `POST /api/v1/actions/lease` (exclusive lease with TTL and token) and `POST /api/v1/actions//result` (token-checked result upload that requires every referenced output blob to exist). `RegistryTests.remote_worker_registration_lease_and_result_upload` covers registration, heartbeat, lease/conflict, wrong-token rejection, result publication and the unknown-action 404. + ## Remaining future scope - **Superiority plan ([22-superiority-and-gaps.md](../roadmap/22-superiority-and-gaps.md), 2026-12-05)**: revised platform strategy - **macOS deferred**, **Windows via MinGW-w64 cross-compilation + Wine execution** (`dev cpp-gcc -p pkgsCross.mingwW64.stdenv.cc`, `dev cpp-gcc -p wine64`), native MSVC deferred; plus packaging parity (ZIP/DEB/RPM), IDE ecosystem, migration tooling, test-runner parity, benchmarks, CI runners, and remote execution in prioritized order. diff --git a/docs/agent-workflow/TASKS.md b/docs/agent-workflow/TASKS.md index 7c87894..eb37c15 100644 --- a/docs/agent-workflow/TASKS.md +++ b/docs/agent-workflow/TASKS.md @@ -124,4 +124,5 @@ Base HEAD: `fe0d1a6` | Wave 119 (VoidLibrary install rules + profiles) | install rules, `build.profiles.cpp`, verified 320-file prefix and a dpkg-valid DEB; streaming DEB writer; LICENSE issue #63 | COMPLETE & VERIFIED IN CLONE | `contrib/voidlibrary-migration/`, [Void/Library build-vbs](https://git.faumaray.com/Void/Library/src/branch/build-vbs), `src/Install/Installer.cpp`, Void/Library#63 | | Wave 120 (WiX/MSI-style packaging + VoidLibrary CI) | `--archive-format wix` WiX v3 source generator; `.gitea/workflows/vbs.yml` template on the branch | COMPLETE & VERIFIED for the generator (XML parsed, install test passes, full CTest 8/8); CI template unexecuted | `src/Install/Installer.cpp`, `src/Main.cpp`, `tests/FullV1Tests.cpp`, [Void/Library build-vbs](https://git.faumaray.com/Void/Library/src/branch/build-vbs), docs | | Wave 121 (libFuzzer fuzzing) | `fuzz/fuzz_parsers.cpp` + `fuzz/run.sh` for JSON and SemVer with Clang/libFuzzer | COMPLETE & VERIFIED (1,541,020 runs, 0 findings) | `fuzz/`, docs | +| Wave 122 (Remote worker protocol) | Registry worker registration/heartbeat, exclusive action leases with TTL/tokens, token-checked result upload with blob verification | COMPLETE & VERIFIED (GCC 16.2, pin `0002e74`; new registry E2E passes, full CTest 8/8) | `registry/include/Void/Build/Registry/RegistryService.hpp`, `registry/service/RegistryService.cpp`, `tests/RegistryTests.cpp`, docs | | Future (Plan 20 Slice 3 / Tracks C, E3 editor extensions) |"}] Live `conan` CLI verification; macOS/Windows/cross-compile; binary-variant signing and key distribution; daemon-side dependency materialization and editor plugins; `Void::Net`/`Void::Sqlite` alignment | PLANNED (unimplemented) | [21-expansion-roadmap.md](../roadmap/21-expansion-roadmap.md), [20-future-translation-platforms.md](../roadmap/20-future-translation-platforms.md) | diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index 09e3efc..62cf4a3 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -1676,6 +1676,17 @@ VOID_INCLUDE_DIR=/tmp/opencode/vl-pin/include dev cpp -c bash fuzz/run.sh 30 their implementations are not header-only. - No repo build changed, so the last full CTest run (8/8, 281.07 s at `fd9e867`) remains valid. +## Remote Execution Worker Protocol (GCC 16.2, pin `0002e74`) + +- `vbs-registry` adds `POST /api/v1/workers`, `POST /api/v1/workers//heartbeat`, + `GET /api/v1/workers`, `POST /api/v1/actions/lease`, and + `POST /api/v1/actions//result` on top of the HTTP CAS. +- `RegistryTests.remote_worker_registration_lease_and_result_upload` **passed** (391 ms): + registration returns a worker id, heartbeat and listing work, a lease returns a token and the + current record, a second lease returns 409, a wrong token returns 403, the leased token stores + the result (visible through the CAS GET), and leasing an unknown action returns 404. +- Full CTest: **8 / 8 passed** in 280.81 s. + ## Limits and Unrun Checks 1. **HTTPS git fetch**: the host shell is proxy-restricted, but the Docker container could clone `https://git.faumaray.com/Void/VoidLibrary.git`; the pinned commit `3e254055e081a104f08df68dde39711bc7813518` was fetched directly for the verification above. A separate configure against the local `/home/faumaray/Projects/Library` checkout via `VBS_VOID_SOURCE_DIR` also succeeded (development-only override, checkout unmodified). diff --git a/docs/roadmap/22-superiority-and-gaps.md b/docs/roadmap/22-superiority-and-gaps.md index d60be8b..55d4832 100644 --- a/docs/roadmap/22-superiority-and-gaps.md +++ b/docs/roadmap/22-superiority-and-gaps.md @@ -83,7 +83,7 @@ Concrete work this implies: 6. Test-runner parity. 7. Benchmarks against CMake/Meson/build2 on documented fixtures (including 10k-target scaling). 8. CI on real runners, sanitizer jobs (**sanitizer build run done**), parser fuzzing (**deterministic mutation harness and a Clang/libFuzzer target done**). -9. Remote execution workers. +9. Remote execution workers (**registry-side worker/lease/result protocol done**; a `vbs worker` client remains). ## 6. Observed baseline (2026-12-05, x86_64, GCC 16.2, Debug, `benchmarks/compare.sh 20`) diff --git a/registry/include/Void/Build/Registry/RegistryService.hpp b/registry/include/Void/Build/Registry/RegistryService.hpp index f8ca3eb..dc1e370 100644 --- a/registry/include/Void/Build/Registry/RegistryService.hpp +++ b/registry/include/Void/Build/Registry/RegistryService.hpp @@ -82,6 +82,23 @@ struct AuditEntry { std::string detail; // Always secret-redacted! }; +// Remote execution worker (in-memory; workers are ephemeral registrations). +struct WorkerRecord { + std::string worker_id; + std::string username; + std::string platform; + std::int64_t registered_epoch{0}; + std::int64_t last_seen_epoch{0}; +}; + +// Lease of one CAS action key by a worker. +struct ActionLeaseRecord { + std::string action_key; + std::string worker_id; + std::string lease_token; + std::int64_t expires_epoch{0}; +}; + struct RegistryConfig { std::filesystem::path data_dir; std::filesystem::path web_root; @@ -159,6 +176,9 @@ class RegistryService { std::map> tokens_; std::map> sessions_; std::map> packages_; + std::map> workers_; + std::map> action_leases_; // action_key -> lease + std::uint64_t worker_sequence_{0}; std::vector audits_; HttpServer server_; }; diff --git a/registry/service/RegistryService.cpp b/registry/service/RegistryService.cpp index 3ae4b6b..baa6ad6 100644 --- a/registry/service/RegistryService.cpp +++ b/registry/service/RegistryService.cpp @@ -961,6 +961,208 @@ HttpResponse RegistryService::handle_request(const HttpRequest &req) { return HttpResponse::json(201, "{\"stored\":true,\"created\":true}\n"); } + // Remote execution workers: registration and heartbeats (in-memory, ephemeral). + if (segs.size() >= 3 && segs[0] == "api" && segs[1] == "v1" && segs[2] == "workers") { + if (!principal) { + return HttpResponse::error_json(401, "unauthorized", "authentication required"); + } + if (segs.size() == 3 && req.method == "POST") { + std::string platform; + if (!req.body.empty()) { + auto parsed = Void::Json::Strict::parse(req.body); + const auto *obj = parsed ? std::get_if(&parsed->value) : nullptr; + if (!obj) { + return HttpResponse::error_json(400, "invalid_json", + "worker registration must be a JSON object"); + } + platform = get_str(*obj, "platform"); + } + WorkerRecord worker; + worker.worker_id = std::format("wrk_{}_{}", ++worker_sequence_, random_hex(6)); + worker.username = principal->username; + worker.platform = platform.substr(0, 128); + worker.registered_epoch = now_epoch(); + worker.last_seen_epoch = worker.registered_epoch; + workers_[worker.worker_id] = worker; + append_audit_locked(principal->username, "worker.register", worker.worker_id, "ok", ""); + return HttpResponse::json(201, "{\"worker_id\":" + json_quote(worker.worker_id) + + ",\"registered_epoch\":" + + std::to_string(worker.registered_epoch) + "}\n"); + } + if (segs.size() == 5 && segs[4] == "heartbeat" && req.method == "POST") { + const auto it = workers_.find(segs[3]); + if (it == workers_.end()) { + return HttpResponse::error_json(404, "not_found", "worker not registered"); + } + if (principal->role != Role::Admin && it->second.username != principal->username) { + return HttpResponse::error_json(403, "forbidden", + "cannot heartbeat another user's worker"); + } + it->second.last_seen_epoch = now_epoch(); + return HttpResponse::json(200, "{\"worker_id\":" + json_quote(it->second.worker_id) + + ",\"last_seen_epoch\":" + + std::to_string(it->second.last_seen_epoch) + "}\n"); + } + if (segs.size() == 3 && req.method == "GET") { + std::vector visible; + for (const auto &[id, worker] : workers_) { + if (principal->role == Role::Admin || worker.username == principal->username) { + visible.push_back(&worker); + } + } + return HttpResponse::json( + 200, "{\"workers\":" + json_array(visible, [](const WorkerRecord *w) { + return "{\"worker_id\":" + json_quote(w->worker_id) + + ",\"username\":" + json_quote(w->username) + + ",\"platform\":" + json_quote(w->platform) + + ",\"last_seen_epoch\":" + std::to_string(w->last_seen_epoch) + '}'; + }) + "}\n"); + } + } + + // Action leases and result upload for remote workers. + if (segs.size() >= 4 && segs[0] == "api" && segs[1] == "v1" && segs[2] == "actions") { + if (!principal) { + return HttpResponse::error_json(401, "unauthorized", "authentication required"); + } + const auto prune_expired_leases = [&] { + const auto now = now_epoch(); + for (auto it = action_leases_.begin(); it != action_leases_.end();) { + it = it->second.expires_epoch <= now ? action_leases_.erase(it) : std::next(it); + } + }; + + if (segs.size() == 4 && segs[3] == "lease" && req.method == "POST") { + auto parsed = Void::Json::Strict::parse(req.body); + const auto *obj = parsed ? std::get_if(&parsed->value) : nullptr; + if (!obj) { + return HttpResponse::error_json(400, "invalid_json", "lease body must be JSON"); + } + const std::string action_key = get_str(*obj, "action_key"); + const std::string worker_id = get_str(*obj, "worker_id"); + std::int64_t lease_seconds = 60; + if (const auto it = obj->find("lease_seconds"); it != obj->end()) { + const auto *value = std::get_if(&it->second.value); + if (!value || *value <= 0 || *value > 3600) { + return HttpResponse::error_json(400, "invalid_lease", + "lease_seconds must be 1..3600"); + } + lease_seconds = *value; + } + if (!is_sha256_hex(action_key)) { + return HttpResponse::error_json(400, "invalid_key", + "action_key must be a SHA-256 hex digest"); + } + if (!worker_id.empty() && !workers_.contains(worker_id)) { + return HttpResponse::error_json(404, "not_found", "worker not registered"); + } + const auto record_path = + action_records_dir_ / action_key.substr(0, 2) / (action_key + ".json"); + std::error_code lease_ec; + if (!std::filesystem::is_regular_file(record_path, lease_ec)) { + return HttpResponse::error_json(404, "not_found", "action record not found"); + } + prune_expired_leases(); + if (const auto it = action_leases_.find(action_key); it != action_leases_.end()) { + return HttpResponse::error_json(409, "already_leased", + "action is leased by " + it->second.worker_id + + " until " + + std::to_string(it->second.expires_epoch)); + } + ActionLeaseRecord lease; + lease.action_key = action_key; + lease.worker_id = worker_id; + lease.lease_token = random_hex(16); + lease.expires_epoch = now_epoch() + lease_seconds; + action_leases_[action_key] = lease; + auto record_text = Void::FileSystem::read_file(record_path); + if (!record_text) { + return HttpResponse::error_json(500, "storage_error", "cannot read action record"); + } + append_audit_locked(principal->username, "action.lease", action_key, "ok", worker_id); + return HttpResponse::json( + 200, "{\"action_key\":" + json_quote(action_key) + + ",\"worker_id\":" + json_quote(worker_id) + + ",\"lease_token\":" + json_quote(lease.lease_token) + + ",\"expires_epoch\":" + std::to_string(lease.expires_epoch) + + ",\"record\":" + *record_text + "}\n"); + } + + if (segs.size() == 5 && segs[4] == "result" && req.method == "POST") { + const std::string &action_key = segs[3]; + if (!is_sha256_hex(action_key)) { + return HttpResponse::error_json(400, "invalid_key", + "action_key must be a SHA-256 hex digest"); + } + prune_expired_leases(); + const auto lease_it = action_leases_.find(action_key); + if (lease_it == action_leases_.end()) { + return HttpResponse::error_json(409, "not_leased", "action has no active lease"); + } + auto parsed = Void::Json::Strict::parse(req.body); + const auto *obj = parsed ? std::get_if(&parsed->value) : nullptr; + if (!obj) { + return HttpResponse::error_json(400, "invalid_json", "result body must be JSON"); + } + if (get_str(*obj, "lease_token") != lease_it->second.lease_token) { + return HttpResponse::error_json(403, "invalid_lease_token", + "lease token does not match"); + } + const auto record_it = obj->find("record"); + if (record_it == obj->end() || + !std::get_if(&record_it->second.value)) { + return HttpResponse::error_json(400, "invalid_record", + "record must be a JSON object"); + } + const auto *record_obj = std::get_if(&record_it->second.value); + if (get_str(*record_obj, "action_key") != action_key) { + return HttpResponse::error_json(400, "invalid_record", + "record action_key does not match the lease"); + } + // Every referenced output blob must already exist in the CAS. + std::vector missing; + if (const auto outputs_it = record_obj->find("outputs"); + outputs_it != record_obj->end()) { + if (const auto *outputs = std::get_if(&outputs_it->second.value)) { + for (const auto &[path, ref] : *outputs) { + const auto *ref_obj = std::get_if(&ref.value); + const std::string sha = ref_obj ? get_str(*ref_obj, "sha256") : ""; + const auto blob = action_blobs_dir_ / sha.substr(0, 2) / sha; + std::error_code blob_ec; + if (sha.empty() || !std::filesystem::is_regular_file(blob, blob_ec)) { + missing.push_back(path); + } + } + } + } + if (!missing.empty()) { + return HttpResponse::error_json(400, "missing_blobs", + "output blobs missing for: " + + json_array(missing, json_quote)); + } + + const auto record_path = + action_records_dir_ / action_key.substr(0, 2) / (action_key + ".json"); + std::error_code result_ec; + const bool existed = std::filesystem::is_regular_file(record_path, result_ec); + std::filesystem::create_directories(record_path.parent_path(), result_ec); + auto serialized_record = Void::Json::Strict::serialize(record_it->second); + if (!serialized_record) { + return HttpResponse::error_json(500, "storage_error", + "cannot serialize result record"); + } + const std::string &record_text = *serialized_record; + auto wrote = Platform::atomic_write_file(record_path, record_text, true, 0644u); + if (!wrote) { + return HttpResponse::error_json(500, "storage_error", "cannot store result record"); + } + action_leases_.erase(lease_it); + append_audit_locked(principal->username, "action.result", action_key, "ok", + existed ? "replaced" : "stored"); + return HttpResponse::json(200, "{\"stored\":true}\n"); + } + } + auto can_read_pkg = [&](const PackageMeta &p) { if (!p.is_private) return true; diff --git a/tests/RegistryTests.cpp b/tests/RegistryTests.cpp index 6080248..d9e9375 100644 --- a/tests/RegistryTests.cpp +++ b/tests/RegistryTests.cpp @@ -1942,3 +1942,102 @@ VOID_TEST(RegistryTests, options_preflight_metadata_and_download_endpoints) { (*svc)->stop_http(); } + +// Remote execution worker protocol: registration, heartbeat, action lease, and result upload +// on top of the shared HTTP CAS. +VOID_TEST(RegistryTests, remote_worker_registration_lease_and_result_upload) { + Void::FileSystem::TempDir tmp{"vbs_worker_"}; + RegistryConfig cfg; + cfg.data_dir = tmp / "reg_data"; + cfg.web_root = std::filesystem::path{Bootstrap::source_dir} / "registry" / "web"; + cfg.bind_address = "127.0.0.1"; + cfg.port = 0; + cfg.bootstrap_admin_user = "admin"; + cfg.bootstrap_admin_password = "admin_pw"; + + auto svc = RegistryService::open(cfg); + Void::Testing::expect(svc.has_value()); + if (!svc) { + return; + } + Void::Testing::expect( + (*svc)->create_user("exec_user", "exec_pass", Role::Publisher).has_value()); + auto tok = (*svc)->create_token("exec_user", "exec", {"read", "publish"}); + Void::Testing::expect(tok.has_value()); + if (!tok) { + return; + } + Void::Testing::expect((*svc)->start_http().has_value()); + const auto port = (*svc)->bound_port(); + const std::map> auth{{"Authorization", "Bearer " + *tok}}; + + // Input blob and the initial action record live in the CAS. + const std::string input = "WORKER_INPUT_BYTES"; + const std::string input_sha = Util::sha256_hex(input); + auto blob_put = send_loopback_http(port, "PUT", "/api/v1/cas/blobs/" + input_sha, auth, input); + Void::Testing::expect(blob_put.has_value() && blob_put->status == 201); + const std::string action_key = Util::sha256_hex("worker-action-key"); + const std::string initial_record = + "{\"schema_version\":1,\"action_key\":\"" + action_key + "\",\"outputs\":{}}"; + auto record_put = + send_loopback_http(port, "PUT", "/api/v1/cas/actions/" + action_key, auth, initial_record); + Void::Testing::expect(record_put.has_value() && record_put->status == 201); + + // Worker registration + heartbeat. + auto registered = send_loopback_http(port, "POST", "/api/v1/workers", auth, + "{\"platform\":\"linux-x86_64\"}"); + Void::Testing::expect(registered.has_value() && registered->status == 201); + if (!registered) { + return; + } + const std::string worker_id = extract_json_str(registered->body, "worker_id"); + Void::Testing::expect(!worker_id.empty()); + auto heartbeat = + send_loopback_http(port, "POST", "/api/v1/workers/" + worker_id + "/heartbeat", auth, ""); + Void::Testing::expect(heartbeat.has_value() && heartbeat->status == 200); + auto workers = send_loopback_http(port, "GET", "/api/v1/workers", auth); + Void::Testing::expect(workers.has_value() && workers->status == 200); + if (workers) { + Void::Testing::expect(workers->body.find(worker_id) != std::string::npos); + } + + // Lease the action once; a concurrent lease conflicts. + const std::string lease_body = "{\"action_key\":\"" + action_key + "\",\"worker_id\":\"" + + worker_id + "\",\"lease_seconds\":60}"; + auto lease = send_loopback_http(port, "POST", "/api/v1/actions/lease", auth, lease_body); + Void::Testing::expect(lease.has_value() && lease->status == 200); + if (!lease) { + return; + } + const std::string lease_token = extract_json_str(lease->body, "lease_token"); + Void::Testing::expect(!lease_token.empty()); + Void::Testing::expect(lease->body.find("\"record\":") != std::string::npos); + auto conflict = send_loopback_http(port, "POST", "/api/v1/actions/lease", auth, lease_body); + Void::Testing::expect(conflict.has_value() && conflict->status == 409); + + // Result upload: wrong token is rejected, the leased token is accepted. + const std::string result_record = "{\"schema_version\":1,\"action_key\":\"" + action_key + + "\",\"outputs\":{\"out.o\":{\"sha256\":\"" + input_sha + + "\",\"size\":17,\"mode\":420}}}"; + auto bad_result = + send_loopback_http(port, "POST", "/api/v1/actions/" + action_key + "/result", auth, + "{\"lease_token\":\"deadbeef\",\"record\":" + result_record + "}"); + Void::Testing::expect(bad_result.has_value() && bad_result->status == 403); + auto good_result = send_loopback_http( + port, "POST", "/api/v1/actions/" + action_key + "/result", auth, + "{\"lease_token\":\"" + lease_token + "\",\"record\":" + result_record + "}"); + Void::Testing::expect(good_result.has_value() && good_result->status == 200); + auto fetched = send_loopback_http(port, "GET", "/api/v1/cas/actions/" + action_key, auth); + Void::Testing::expect(fetched.has_value() && fetched->status == 200); + if (fetched) { + Void::Testing::expect(fetched->body.find("out.o") != std::string::npos); + } + + // Leasing an unknown action is a 404. + auto unknown = send_loopback_http(port, "POST", "/api/v1/actions/lease", auth, + "{\"action_key\":\"" + std::string(64, 'a') + + "\",\"worker_id\":\"" + worker_id + "\"}"); + Void::Testing::expect(unknown.has_value() && unknown->status == 404); + + (*svc)->stop_http(); +}