From f1cf277847175999811426dd1683de06d905dbc8 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sun, 6 Dec 2026 20:26:57 +0300 Subject: [PATCH] feat(schemas): versioned JSON contracts for metadata, describe and explain (G6) - schemas/ publishes JSON Schemas (draft 2020-12, additionalProperties false) for `vbs metadata`, `vbs describe` and `vbs explain --format json`, plus README documenting the versioning rule: any emitted-shape change bumps schema_version and adds a new -vN file; older schemas stay. - `vbs describe` now emits a StrictJson graph summary with schema_version, project/build roots, profile/compiler/os/arch, target and dependency counts and names. - `vbs explain --format json` carries schema_version. - Tests include a schema subset validator (types, required, enum, additionalProperties, items, minimum) and json_schemas_validate_metadata_describe_and_explain validates the real documents against the files, then proves the validator rejects a missing required key, a wrong enum value and an unexpected property. Verified: the schema test, vbs_core_tests and the CLI contract suite pass; clang-format clean. --- docs/agent-workflow/STATE.md | 2 + docs/agent-workflow/TASKS.md | 1 + docs/roadmap/24-superiority-tracks.md | 8 + schemas/README.md | 29 +++ schemas/describe-v1.schema.json | 34 +++ schemas/explain-v1.schema.json | 51 ++++ schemas/metadata-v1.schema.json | 153 ++++++++++++ src/Main.cpp | 34 ++- tests/FullV1Tests.cpp | 340 ++++++++++++++++++++++++++ 9 files changed, 648 insertions(+), 4 deletions(-) create mode 100644 schemas/README.md create mode 100644 schemas/describe-v1.schema.json create mode 100644 schemas/explain-v1.schema.json create mode 100644 schemas/metadata-v1.schema.json diff --git a/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index 6fbb75d..38b98c8 100644 --- a/docs/agent-workflow/STATE.md +++ b/docs/agent-workflow/STATE.md @@ -401,6 +401,8 @@ Stage: VERIFIED — Plans 01–19 + Plan 20 Slices 1–2C + Track A + Track B (B ## Remaining future scope +- **Wave 153 (Versioned JSON contracts, G6 start)**: `schemas/` publishes JSON Schemas for `vbs metadata` (`metadata-v1.schema.json`), `vbs describe` (`describe-v1.schema.json`) and `vbs explain --format json` (`explain-v1.schema.json`), all with `additionalProperties: false`; `schemas/README.md` states the versioning rule (a shape change bumps `schema_version` and adds a new `-vN` file, older schemas stay). `vbs describe` now emits a StrictJson graph summary (`schema_version`, roots, profile/compiler/os/arch, counts, target/dependency names) and `vbs explain --format json` carries `schema_version`. `FullV1Tests.json_schemas_validate_metadata_describe_and_explain` validates the emitted documents against the files and proves the validator rejects missing required keys, wrong enums and unexpected properties. + - **Wave 152 (Test depth and fuzzing, G8)**: `vbs doctest` extracts fenced C++ blocks from `///`, `//!` and Doxygen comment blocks, compiles each as the body of `main()` with `#line` mapping (so errors point at the header), links optional `--source` files and reports block pass/fail. The test runner gained `--shard I/N` (fixture-aware clustering, greedy balancing by the previous run's recorded timings in `/.vbs/test_timings.json`), `--rerun-failed` (reads `/.vbs/last_failed_tests.json` and re-runs only the failed closure), `--known-flaky FILE` (flaky failures are reported but do not fail the run) and coverage gates `--coverage-min PCT` / `--coverage-diff BASE` (lcov summary comparison). `vbs fuzz` builds a libFuzzer target with clang (`-fsanitize=fuzzer,address,undefined`), runs it over a corpus and reports crash artifacts. Verified by `doctest_extracts_compiles_and_runs_blocks`, `test_sharding_partitions_tests_and_records_timings`, `test_rerun_failed_and_known_flaky`, `coverage_gates_enforce_minimum_and_baseline` and `fuzz_builds_targets_and_reports_crashes`. - **Wave 151 (Profiles and toolchain pinning, G3)**: `Dependency::profile(name, DependencyProfileOptions)` stores per-profile compile/link options, defines, LTO and sanitizers; the native provider applies the matching override to the dependency's own `ProfileContext` and includes the overrides in the provider input hash, so changing them invalidates cached dependency builds. `vbs-toolchain.toml` pins compiler, minimum version, C/C++ standards and sysroot (with a `strict` field); it is loaded once per build and checked after every toolchain probe, warning on drift or failing with `--locked-toolchain` (or `strict = true`). `vbs profile list|show|diff` lists declared profiles with parents, prints one resolved profile, or reports the fields that differ between two. Verified by `FullV1Tests.dependency_profile_overrides_apply_to_native_builds`, `toolchain_pin_detects_and_enforces_drift` and `profile_commands_list_show_and_diff`. diff --git a/docs/agent-workflow/TASKS.md b/docs/agent-workflow/TASKS.md index b09d656..a066c3a 100644 --- a/docs/agent-workflow/TASKS.md +++ b/docs/agent-workflow/TASKS.md @@ -152,4 +152,5 @@ Base HEAD: `fe0d1a6` | Wave 150 (Feature flags) | `Project::feature`, `Target::require_feature`, `Dependency::when`, additive unification, `--features`/`--no-default-features`, `VBS_FEATURE_*` defines, feature-aware action keys, optional dependency filtering, `explain --show-features`, metadata feature list | COMPLETE & VERIFIED (GCC 16.2; `features_unify_defines_and_invalidate_action_keys` and `optional_dependencies_follow_feature_selection` pass; CLI contracts pass) | `include/Void/Build.hpp`, `sdk/Build.cpp`, `include/Void/Build/Graph/Graph.hpp`, `src/Graph/Graph.cpp`, `src/Main.cpp`, `tests/{FullV1Tests,CliContracts}`, docs | | Wave 151 (Profiles + toolchain pinning) | `Dependency::profile` overrides for native dependency builds (hash-aware), `vbs-toolchain.toml` pin + drift detection (`--locked-toolchain`, `strict`), `vbs profile list\|show\|diff` | COMPLETE & VERIFIED (GCC 16.2; the three G3 tests pass; CLI contracts pass) | `include/Void/Build.hpp`, `sdk/Build.cpp`, `src/Graph/Graph.cpp`, `src/Provider/Provider.cpp`, `include/Void/Build/Toolchain/Pin.hpp`, `src/Toolchain/Pin.cpp`, `src/Main.cpp`, `tests/{FullV1Tests,CliContracts}`, docs | | Wave 152 (Test depth + fuzzing) | `vbs doctest` (fenced blocks, `#line` diagnostics, `--source`), `--shard I/N` + timings, `--rerun-failed`, `--known-flaky`, `--coverage-min`/`--coverage-diff`, `vbs fuzz` (libFuzzer) | COMPLETE & VERIFIED (GCC 16.2; doctest/sharding/rerun/coverage tests pass; fuzz test passes under clang 23; CLI contracts pass) | `include/Void/Build/Test/Doctest.hpp`, `src/Test/Doctest.cpp`, `include/Void/Build/Test/Fuzz.hpp`, `src/Test/Fuzz.cpp`, `include/Void/Build/Report/Coverage.hpp`, `src/Report/Coverage.cpp`, `src/Main.cpp`, `tests/{FullV1Tests,CliContracts}`, docs | +| Wave 153 (Versioned JSON contracts) | `schemas/{metadata,describe,explain}-v1.schema.json` + versioning rule, `vbs describe` StrictJson graph summary, `vbs explain --format json` schema_version, schema validation test with negative cases | COMPLETE & VERIFIED (GCC 16.2; `json_schemas_validate_metadata_describe_and_explain` passes; vbs_core_tests and CLI contracts pass) | `schemas/`, `src/Main.cpp`, `tests/FullV1Tests.cpp`, docs | | Future (Plans 23–24) | Gap closure and superiority tracks | PLANNED (see the plans for per-track acceptance gates) | [23-remaining-upgrade-plan.md](../roadmap/23-remaining-upgrade-plan.md), [24-superiority-tracks.md](../roadmap/24-superiority-tracks.md) | diff --git a/docs/roadmap/24-superiority-tracks.md b/docs/roadmap/24-superiority-tracks.md index 2a24e62..06375c1 100644 --- a/docs/roadmap/24-superiority-tracks.md +++ b/docs/roadmap/24-superiority-tracks.md @@ -324,6 +324,14 @@ policy violation exits non-zero in CI. ### G6 — Machine-readable everything and tooling protocols *(M)* +**Status: partially implemented (Wave 153).** Done: item 1's `vbs metadata` workspace +document (targets, dependencies, features, lockfile, roots) and item 2's published +`schemas/{metadata,describe,explain}-v1.schema.json` with the versioning rule and a +compatibility test (`FullV1Tests.json_schemas_validate_metadata_describe_and_explain`); +`vbs describe` is now a StrictJson graph summary with `schema_version`. Pending: `graph`, +`trace`, `bep`, `doctor` and daemon schemas, the DAP server, the LSP-status request and the +plugin protocol. + **Beats:** Cargo (`metadata`), CMake (File API), Meson (introspect) — VBS can expose a single, versioned, cross-cutting model. diff --git a/schemas/README.md b/schemas/README.md new file mode 100644 index 0000000..aca15d8 --- /dev/null +++ b/schemas/README.md @@ -0,0 +1,29 @@ +# JSON schemas + +Versioned JSON Schemas for the machine-readable `vbs` surfaces: + +| Command | Schema | +| --- | --- | +| `vbs metadata` | [metadata-v1.schema.json](metadata-v1.schema.json) | +| `vbs describe` | [describe-v1.schema.json](describe-v1.schema.json) | +| `vbs explain --format json` | [explain-v1.schema.json](explain-v1.schema.json) | + +## Versioning rule + +- The schema file name and the document's `schema_version` integer always match (`-vN` ⇔ + `"schema_version": N`). +- **Any change to the emitted shape bumps the version and adds a new `-vN` file.** That includes + adding, removing, renaming or retyping a field. The previous schema stays for consumers that + read older documents. +- Consumers must ignore fields they do not know; producers must not rely on that for breaking + changes. +- The repository tests validate the emitted documents against these files + (`FullV1Tests.json_schemas_validate_metadata_describe_and_explain`), so a code change that + alters a document without updating its schema fails the suite. + +## Adding a new version + +1. Emit the new `schema_version` from the command. +2. Copy the previous schema to `-vN+1.schema.json` and update it. +3. Add the new document to the schema validation test. +4. Keep the older schema file until the next major release. diff --git a/schemas/describe-v1.schema.json b/schemas/describe-v1.schema.json new file mode 100644 index 0000000..5aa231d --- /dev/null +++ b/schemas/describe-v1.schema.json @@ -0,0 +1,34 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://void.build/schemas/describe-v1.schema.json", + "title": "VoidBuildSystem graph summary (schema_version 1)", + "description": "Emitted by `vbs describe`. Any shape change bumps schema_version and adds a new schema file.", + "type": "object", + "required": [ + "schema_version", + "profile", + "compiler", + "os", + "architecture", + "project_root", + "build_root", + "targets", + "dependencies", + "target_names", + "dependency_names" + ], + "additionalProperties": false, + "properties": { + "schema_version": { "type": "integer", "enum": [1] }, + "profile": { "type": "string" }, + "compiler": { "type": "string" }, + "os": { "type": "string" }, + "architecture": { "type": "string" }, + "project_root": { "type": "string" }, + "build_root": { "type": "string" }, + "targets": { "type": "integer", "minimum": 0 }, + "dependencies": { "type": "integer", "minimum": 0 }, + "target_names": { "type": "array", "items": { "type": "string" } }, + "dependency_names": { "type": "array", "items": { "type": "string" } } + } +} diff --git a/schemas/explain-v1.schema.json b/schemas/explain-v1.schema.json new file mode 100644 index 0000000..877ae40 --- /dev/null +++ b/schemas/explain-v1.schema.json @@ -0,0 +1,51 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://void.build/schemas/explain-v1.schema.json", + "title": "VoidBuildSystem action explanation (schema_version 1)", + "description": "Emitted by `vbs explain --format json`. Any shape change bumps schema_version and adds a new schema file.", + "type": "object", + "required": ["schema_version", "profile", "total", "up_to_date", "would_run", "actions"], + "additionalProperties": false, + "properties": { + "schema_version": { "type": "integer", "enum": [1] }, + "profile": { "type": "string" }, + "total": { "type": "integer", "minimum": 0 }, + "up_to_date": { "type": "integer", "minimum": 0 }, + "would_run": { "type": "integer", "minimum": 0 }, + "actions": { + "type": "array", + "items": { + "type": "object", + "required": [ + "id", + "target", + "label", + "action_key", + "cacheable", + "would_run", + "restorable", + "remote_hit", + "reason", + "changed_inputs", + "changed_outputs", + "command" + ], + "additionalProperties": false, + "properties": { + "id": { "type": "string" }, + "target": { "type": "string" }, + "label": { "type": "string" }, + "action_key": { "type": "string" }, + "cacheable": { "type": "boolean" }, + "would_run": { "type": "boolean" }, + "restorable": { "type": "boolean" }, + "remote_hit": { "type": "boolean" }, + "reason": { "type": "string" }, + "changed_inputs": { "type": "array", "items": { "type": "string" } }, + "changed_outputs": { "type": "array", "items": { "type": "string" } }, + "command": { "type": "string" } + } + } + } + } +} diff --git a/schemas/metadata-v1.schema.json b/schemas/metadata-v1.schema.json new file mode 100644 index 0000000..1b6470b --- /dev/null +++ b/schemas/metadata-v1.schema.json @@ -0,0 +1,153 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://void.build/schemas/metadata-v1.schema.json", + "title": "VoidBuildSystem workspace metadata (schema_version 1)", + "description": "Emitted by `vbs metadata`. Any shape change bumps schema_version and adds a new schema file.", + "type": "object", + "required": [ + "schema_version", + "project_root", + "build_root", + "profile", + "compiler", + "os", + "architecture", + "features", + "targets", + "dependencies", + "lockfile" + ], + "additionalProperties": false, + "properties": { + "schema_version": { "type": "integer", "enum": [1] }, + "project_root": { "type": "string" }, + "build_root": { "type": "string" }, + "profile": { "type": "string" }, + "compiler": { "type": "string" }, + "os": { "type": "string" }, + "architecture": { "type": "string" }, + "features": { + "type": "array", + "items": { + "type": "object", + "required": ["name", "default_on", "description", "enabled"], + "additionalProperties": false, + "properties": { + "name": { "type": "string" }, + "default_on": { "type": "boolean" }, + "description": { "type": "string" }, + "enabled": { "type": "boolean" } + } + } + }, + "targets": { + "type": "array", + "items": { + "type": "object", + "required": [ + "name", + "kind", + "sources", + "include_directories", + "links", + "labels", + "required_features", + "install" + ], + "additionalProperties": false, + "properties": { + "name": { "type": "string" }, + "kind": { + "type": "string", + "enum": [ + "executable", + "static_library", + "shared_library", + "header_only_library", + "object_library", + "custom_action", + "test" + ] + }, + "sources": { "type": "array", "items": { "type": "string" } }, + "include_directories": { + "type": "array", + "items": { + "type": "object", + "required": ["path", "visibility"], + "additionalProperties": false, + "properties": { + "path": { "type": "string" }, + "visibility": { "type": "string", "enum": ["public", "private"] } + } + } + }, + "links": { + "type": "array", + "items": { + "type": "object", + "required": ["target", "dependency", "visibility"], + "additionalProperties": false, + "properties": { + "target": { "type": "string" }, + "dependency": { "type": "string" }, + "visibility": { "type": "string", "enum": ["public", "private"] } + } + } + }, + "labels": { "type": "array", "items": { "type": "string" } }, + "required_features": { "type": "array", "items": { "type": "string" } }, + "install": { + "type": ["object", "null"], + "required": ["destination", "component"], + "additionalProperties": false, + "properties": { + "destination": { "type": "string" }, + "component": { "type": "string" } + } + } + } + } + }, + "dependencies": { + "type": "array", + "items": { + "type": "object", + "required": [ + "name", + "source", + "package", + "version", + "builder", + "registry", + "features", + "build_dependency" + ], + "additionalProperties": false, + "properties": { + "name": { "type": "string" }, + "source": { + "type": "string", + "enum": ["git", "package", "path", "archive", "conan", "pkgconfig", "unspecified"] + }, + "package": { "type": "string" }, + "version": { "type": "string" }, + "builder": { "type": "string" }, + "registry": { "type": "string" }, + "features": { "type": "array", "items": { "type": "string" } }, + "build_dependency": { "type": "boolean" } + } + } + }, + "lockfile": { + "type": "object", + "required": ["path", "exists"], + "additionalProperties": false, + "properties": { + "path": { "type": "string" }, + "exists": { "type": "boolean" }, + "revision": { "type": "string" } + } + } + } +} diff --git a/src/Main.cpp b/src/Main.cpp index 2c68b40..fb60afc 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1446,7 +1446,7 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) { }; if (opts.explain_format == "json") { std::string json = std::format( - "{{\"profile\":\"{}\",\"total\":{},\"up_to_date\":{},\"would_run\":{},\"actions\":[", + "{{\"schema_version\":1,\"profile\":\"{}\",\"total\":{},\"up_to_date\":{},\"would_run\":{},\"actions\":[", json_escape(prof_name), explained->total, explained->up_to_date, explained->would_run); for (std::size_t i = 0; i < explained->actions.size(); ++i) { @@ -2601,9 +2601,35 @@ int execute_describe_command(const DescribeCmdOptions &opts) { std::cerr << "vbs describe: " << desc.error().message() << '\n'; return 1; } - std::cout << "{\"profile\":" << Util::json_quote(desc->profile.name) - << ",\"targets\":" << desc->targets.size() - << ",\"dependencies\":" << desc->dependencies.size() << "}\n"; + using JsonValue = Void::Json::Strict::Value; + JsonValue::Object root; + root["schema_version"] = JsonValue{std::int64_t{1}}; + root["profile"] = JsonValue{desc->profile.name}; + root["compiler"] = JsonValue{std::string{compiler_name(desc->profile.compiler)}}; + root["os"] = JsonValue{desc->profile.os}; + root["architecture"] = JsonValue{desc->profile.architecture}; + root["project_root"] = JsonValue{std::filesystem::absolute(project_root).string()}; + root["build_root"] = JsonValue{std::filesystem::absolute(build_root).string()}; + root["targets"] = JsonValue{static_cast(desc->targets.size())}; + root["dependencies"] = JsonValue{static_cast(desc->dependencies.size())}; + JsonValue::Array target_names; + for (const auto &target : desc->targets) { + target_names.push_back(JsonValue{target.name}); + } + root["target_names"] = JsonValue{std::move(target_names)}; + JsonValue::Array dependency_names; + for (const auto &dependency : desc->dependencies) { + dependency_names.push_back(JsonValue{dependency.name}); + } + root["dependency_names"] = JsonValue{std::move(dependency_names)}; + JsonValue document; + document.value = std::move(root); + auto serialized = Void::Json::Strict::serialize(document); + if (!serialized) { + std::cerr << "vbs describe: cannot serialize the graph summary\n"; + return 1; + } + std::cout << *serialized << '\n'; return 0; } catch (const std::exception &ex) { std::cerr << "vbs describe: " << ex.what() << '\n'; diff --git a/tests/FullV1Tests.cpp b/tests/FullV1Tests.cpp index ef41c5c..255eb54 100644 --- a/tests/FullV1Tests.cpp +++ b/tests/FullV1Tests.cpp @@ -121,6 +121,195 @@ std::filesystem::path locate_vbs_binary() { } // namespace +namespace { + +using JsonValue = Void::Json::Strict::Value; + +const JsonValue::Object *as_json_object(const JsonValue &value) { + return std::get_if(&value.value); +} + +const JsonValue::Array *as_json_array(const JsonValue &value) { + return std::get_if(&value.value); +} + +bool json_type_matches(const JsonValue &value, std::string_view type) { + if (type == "object") { + return std::holds_alternative(value.value); + } + if (type == "array") { + return std::holds_alternative(value.value); + } + if (type == "string") { + return std::holds_alternative(value.value); + } + if (type == "boolean") { + return std::holds_alternative(value.value); + } + if (type == "null") { + return std::holds_alternative(value.value); + } + if (type == "integer") { + return std::holds_alternative(value.value); + } + if (type == "number") { + return std::holds_alternative(value.value) || + std::holds_alternative(value.value); + } + return false; +} + +bool validate_against_schema(const JsonValue &value, const JsonValue &schema, + const std::string &path, std::string &error); + +bool validate_object_against_schema(const JsonValue::Object &object, const JsonValue::Object &schema, + const std::string &path, std::string &error) { + if (const auto required = schema.find("required"); required != schema.end()) { + if (const auto *keys = as_json_array(required->second); keys != nullptr) { + for (const auto &key_value : *keys) { + const auto *key = std::get_if(&key_value.value); + if (key != nullptr && !object.contains(*key)) { + error = path + ": missing required property '" + *key + "'"; + return false; + } + } + } + } + const auto properties_it = schema.find("properties"); + const auto *properties = properties_it == schema.end() + ? nullptr + : as_json_object(properties_it->second); + for (const auto &[key, member] : object) { + if (properties != nullptr) { + if (const auto property = properties->find(key); property != properties->end()) { + if (!validate_against_schema(member, property->second, path + "." + key, error)) { + return false; + } + continue; + } + } + if (const auto additional = schema.find("additionalProperties"); + additional != schema.end()) { + if (const auto *allowed = std::get_if(&additional->second.value); + allowed != nullptr && !*allowed) { + error = path + ": unexpected property '" + key + "'"; + return false; + } + } + } + return true; +} + +bool validate_array_against_schema(const JsonValue::Array &array, const JsonValue::Object &schema, + const std::string &path, std::string &error) { + if (const auto items = schema.find("items"); items != schema.end()) { + for (std::size_t i = 0; i < array.size(); ++i) { + if (!validate_against_schema(array[i], items->second, + path + "[" + std::to_string(i) + "]", error)) { + return false; + } + } + } + if (const auto minimum = schema.find("minItems"); minimum != schema.end()) { + if (const auto *limit = std::get_if(&minimum->second.value); + limit != nullptr && array.size() < static_cast(*limit)) { + error = path + ": too few items"; + return false; + } + } + return true; +} + +bool validate_against_schema(const JsonValue &value, const JsonValue &schema, + const std::string &path, std::string &error) { + const auto *schema_object = as_json_object(schema); + if (schema_object == nullptr) { + error = path + ": schema is not an object"; + return false; + } + if (const auto type = schema_object->find("type"); type != schema_object->end()) { + bool matched = false; + if (const auto *single = std::get_if(&type->second.value); single != nullptr) { + matched = json_type_matches(value, *single); + } else if (const auto *multiple = as_json_array(type->second); multiple != nullptr) { + for (const auto &entry : *multiple) { + const auto *name = std::get_if(&entry.value); + if (name != nullptr && json_type_matches(value, *name)) { + matched = true; + break; + } + } + } + if (!matched) { + error = path + ": value does not match the declared type"; + return false; + } + } + if (const auto enumeration = schema_object->find("enum"); + enumeration != schema_object->end()) { + const auto *allowed = as_json_array(enumeration->second); + bool found = false; + if (allowed != nullptr) { + auto serialized_value = Void::Json::Strict::serialize(value); + for (const auto &entry : *allowed) { + auto serialized_entry = Void::Json::Strict::serialize(entry); + if (serialized_value && serialized_entry && *serialized_value == *serialized_entry) { + found = true; + break; + } + } + } + if (!found) { + error = path + ": value is not in the schema enum"; + return false; + } + } + if (const auto *object = as_json_object(value); object != nullptr) { + if (!validate_object_against_schema(*object, *schema_object, path, error)) { + return false; + } + } + if (const auto *array = as_json_array(value); array != nullptr) { + if (!validate_array_against_schema(*array, *schema_object, path, error)) { + return false; + } + } + if (const auto minimum = schema_object->find("minimum"); minimum != schema_object->end()) { + double actual = 0.0; + if (const auto *integer = std::get_if(&value.value); integer != nullptr) { + actual = static_cast(*integer); + } else if (const auto *real = std::get_if(&value.value); real != nullptr) { + actual = *real; + } else { + return true; + } + if (const auto *limit = std::get_if(&minimum->second.value); + limit != nullptr && actual < static_cast(*limit)) { + error = path + ": value is below the schema minimum"; + return false; + } + } + return true; +} + +std::filesystem::path locate_repository_root() { + std::error_code ec; + auto directory = locate_vbs_binary().parent_path(); + for (int depth = 0; depth < 8 && !directory.empty(); ++depth) { + if (std::filesystem::is_regular_file(directory / "schemas" / "metadata-v1.schema.json", + ec)) { + return directory; + } + if (!directory.has_parent_path()) { + break; + } + directory = directory.parent_path(); + } + return {}; +} + +} // namespace + VOID_TEST(FullV1Tests, workspace_members_shared_build_and_member_selection) { const auto vbs_bin = locate_vbs_binary(); Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin)); @@ -5646,3 +5835,154 @@ void build(Void::Build::Project &p) { (tmp / "cov4").string(), "--coverage-diff", low_baseline.string()}); Void::Testing::expect(kept.has_value() && kept->succeeded()); } + +// === G6: versioned JSON schemas for metadata / describe / explain === +VOID_TEST(FullV1Tests, json_schemas_validate_metadata_describe_and_explain) { + const auto vbs_bin = locate_vbs_binary(); + Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin)); + if (vbs_bin.empty() || !std::filesystem::exists(vbs_bin)) { + return; + } + const auto repository_root = locate_repository_root(); + Void::Testing::require(!repository_root.empty()); + const auto load_schema = [&](std::string_view name) { + auto text = Void::FileSystem::read_file(repository_root / "schemas" / name); + Void::Testing::require(text.has_value()); + auto parsed = Void::Json::Strict::parse(*text); + Void::Testing::require(parsed.has_value()); + return *parsed; + }; + const auto metadata_schema = load_schema("metadata-v1.schema.json"); + const auto describe_schema = load_schema("describe-v1.schema.json"); + const auto explain_schema = load_schema("explain-v1.schema.json"); + + Void::FileSystem::TempDir tmp{"vbs_schemas_"}; + const auto proj = tmp / "proj"; + write_file(proj / "build.cpp", R"cpp(#include +void build(Void::Build::Project &p) { + p.feature("ssl", true); + auto lib = p.static_library("demo") + .cxx_standard(Void::Build::CxxStandard::Cxx26) + .sources({"src/demo.cpp"}); + p.executable("app") + .cxx_standard(Void::Build::CxxStandard::Cxx26) + .sources({"src/main.cpp"}) + .link(lib); + p.test("check") + .cxx_standard(Void::Build::CxxStandard::Cxx26) + .sources({"tests/check.cpp"}); +} +)cpp"); + write_file(proj / "src" / "demo.cpp", "int demo() { return 1; }\n"); + write_file(proj / "src" / "main.cpp", "int demo();\nint main() { return demo() == 1 ? 0 : 1; }\n"); + write_file(proj / "tests" / "check.cpp", "int main() { return 0; }\n"); + + auto run_vbs = [&](std::vector args) { + Platform::ProcessRequest req; + req.executable = vbs_bin; + req.arguments = std::move(args); + req.timeout = std::chrono::seconds{600}; + return Platform::run_supervised(req); + }; + const auto parse_document = [](const Platform::ProcessResult &result) { + auto parsed = Void::Json::Strict::parse(result.stdout_data); + Void::Testing::require(parsed.has_value()); + return *parsed; + }; + const auto schema_version = [](const JsonValue &document) -> std::int64_t { + const auto *object = as_json_object(document); + Void::Testing::require(object != nullptr); + const auto *version = std::get_if(&object->at("schema_version").value); + Void::Testing::require(version != nullptr); + return *version; + }; + + auto metadata = run_vbs({"metadata", "--project", proj.string(), "--build-dir", + (tmp / "build").string()}); + Void::Testing::expect(metadata.has_value() && metadata->succeeded()); + Void::Testing::require(metadata.has_value()); + const auto metadata_document = parse_document(*metadata); + { + std::string error; + const bool valid = validate_against_schema(metadata_document, metadata_schema, "$", + error); + if (!valid) { + std::cerr << "metadata schema: " << error << "\n" << metadata->stdout_data; + } + Void::Testing::expect(valid); + } + Void::Testing::expect_eq(schema_version(metadata_document), 1LL); + + auto describe = run_vbs({"describe", "--project", proj.string(), "--build-dir", + (tmp / "build").string()}); + Void::Testing::expect(describe.has_value() && describe->succeeded()); + Void::Testing::require(describe.has_value()); + const auto describe_document = parse_document(*describe); + { + std::string error; + const bool valid = validate_against_schema(describe_document, describe_schema, "$", + error); + if (!valid) { + std::cerr << "describe schema: " << error << "\n" << describe->stdout_data; + } + Void::Testing::expect(valid); + } + Void::Testing::expect_eq(schema_version(describe_document), 1LL); + + auto explained = run_vbs({"explain", "--project", proj.string(), "--build-dir", + (tmp / "build").string(), "--format", "json"}); + Void::Testing::expect(explained.has_value() && explained->succeeded()); + Void::Testing::require(explained.has_value()); + const auto explain_document = parse_document(*explained); + { + std::string error; + const bool valid = + validate_against_schema(explain_document, explain_schema, "$", error); + if (!valid) { + std::cerr << "explain schema: " << error << "\n" << explained->stdout_data; + } + Void::Testing::expect(valid); + } + Void::Testing::expect_eq(schema_version(explain_document), 1LL); + if (const auto *object = as_json_object(explain_document); object != nullptr) { + const auto *actions = as_json_array(object->at("actions")); + Void::Testing::require(actions != nullptr); + Void::Testing::expect(!actions->empty()); + } + + // Negative cases: the validator must actually reject drift. + auto missing_required = metadata_document; + if (auto *object = std::get_if(&missing_required.value); object != nullptr) { + object->erase("targets"); + std::string error; + Void::Testing::expect( + !validate_against_schema(missing_required, metadata_schema, "$", error)); + Void::Testing::expect(error.find("missing required property 'targets'") != + std::string::npos); + } + auto wrong_enum = metadata_document; + if (auto *object = std::get_if(&wrong_enum.value); object != nullptr) { + auto targets = object->find("targets"); + if (targets != object->end()) { + if (auto *array = std::get_if(&targets->second.value); + array != nullptr && !array->empty()) { + if (auto *target = std::get_if(&(*array)[0].value); + target != nullptr) { + (*target)["kind"] = JsonValue{std::string{"bogus"}}; + } + } + } + std::string error; + Void::Testing::expect(!validate_against_schema(wrong_enum, metadata_schema, "$", error)); + Void::Testing::expect(error.find("enum") != std::string::npos); + } + auto extra_property = metadata_document; + if (auto *object = std::get_if(&extra_property.value); object != nullptr) { + (*object)["surprise"] = JsonValue{std::string{"drift"}}; + std::string error; + Void::Testing::expect( + !validate_against_schema(extra_property, metadata_schema, "$", error)); + Void::Testing::expect(error.find("unexpected property 'surprise'") != std::string::npos); + } +} +