Step 542: add cross-language consistency gate

This commit is contained in:
Bill
2026-02-17 09:44:27 -07:00
parent 3f00e8dcc8
commit cf4b01b283
4 changed files with 283 additions and 0 deletions

View File

@@ -3757,4 +3757,13 @@ target_link_libraries(step541_test PRIVATE
tree_sitter_javascript tree_sitter_typescript
tree_sitter_java tree_sitter_rust tree_sitter_go)
add_executable(step542_test tests/step542_test.cpp)
target_include_directories(step542_test PRIVATE src)
target_link_libraries(step542_test PRIVATE
nlohmann_json::nlohmann_json
unofficial::tree-sitter::tree-sitter
tree_sitter_python tree_sitter_cpp tree_sitter_elisp
tree_sitter_javascript tree_sitter_typescript
tree_sitter_java tree_sitter_rust tree_sitter_go)
# Step 12: Dear ImGui shell scaffolding created (main.cpp exists but not built due to dependencies)

View File

@@ -0,0 +1,108 @@
#pragma once
// Step 542: Cross-Language Consistency Gate
#include <string>
#include <vector>
#include "CppConstructiveEditAdapter.h"
#include "PythonTypeScriptConstructiveEditAdapter.h"
#include "RustGoConstructiveEditAdapter.h"
struct CrossLanguageRequest {
std::string language;
std::string constructKind;
std::string operation;
bool moduleSideEffectRisk = false;
bool borrowOrLifetimeRisk = false;
bool concurrencyRisk = false;
std::string fileType = "source";
};
struct CrossLanguageResult {
bool supported = false;
bool allowed = false;
std::string standardizedFailure = "";
std::string fallbackAction = "none";
std::vector<std::string> diagnostics;
};
class CrossLanguageConsistencyGate {
public:
static CrossLanguageResult evaluate(const CrossLanguageRequest& request) {
CrossLanguageResult out;
if (request.language == "c" || request.language == "cpp") {
auto r = CppConstructiveEditAdapter::evaluate(
{request.language, request.constructKind, request.fileType, request.operation});
return normalize(r.supported, r.allowed, r.diagnostics);
}
if (request.language == "python" || request.language == "typescript") {
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate(
{request.language, request.constructKind, request.operation,
request.moduleSideEffectRisk});
return normalize(r.supported, r.allowed, r.diagnostics);
}
if (request.language == "rust" || request.language == "go") {
auto r = RustGoConstructiveEditAdapter::evaluate(
{request.language, request.constructKind, request.operation,
request.borrowOrLifetimeRisk, request.concurrencyRisk});
return normalize(r.supported, r.allowed, r.diagnostics);
}
out.diagnostics = {"unsupported_language"};
out.standardizedFailure = "unsupported_language";
out.fallbackAction = "fallback_to_readonly";
return out;
}
private:
static CrossLanguageResult normalize(bool supported,
bool allowed,
const std::vector<std::string>& diagnostics) {
CrossLanguageResult out;
out.supported = supported;
out.allowed = allowed;
out.diagnostics = diagnostics;
if (supported && allowed) {
out.standardizedFailure = "";
out.fallbackAction = "none";
return out;
}
const std::string primary = diagnostics.empty() ? "unknown_failure" : diagnostics.front();
out.standardizedFailure = canonical(primary);
if (out.standardizedFailure == "unsupported_language" ||
out.standardizedFailure == "unsupported_construct") {
out.fallbackAction = "fallback_to_readonly";
} else if (out.standardizedFailure == "operation_not_legal_for_construct") {
out.fallbackAction = "suggest_legal_ops";
} else if (out.standardizedFailure == "safety_guard_blocked") {
out.fallbackAction = "escalate_with_safety_context";
} else {
out.fallbackAction = "retry_with_constraints";
}
return out;
}
static std::string canonical(const std::string& diag) {
if (diag == "unsupported_language") return "unsupported_language";
if (diag == "unsupported_construct") return "unsupported_construct";
if (diag == "operation_not_legal_for_construct") return "operation_not_legal_for_construct";
if (diag.find("boundary") != std::string::npos ||
diag.find("side_effect") != std::string::npos ||
diag.find("lifetime") != std::string::npos ||
diag.find("concurrency") != std::string::npos ||
diag.find("borrow") != std::string::npos ||
diag.find("review") != std::string::npos) {
return "safety_guard_blocked";
}
return "constraint_failure";
}
};

View File

@@ -0,0 +1,132 @@
// Step 542: Cross-Language Consistency Gate (12 tests)
#include "CrossLanguageConsistencyGate.h"
#include <iostream>
static int passed = 0, failed = 0;
#define TEST(name) { std::cout << " " << #name << "... "; }
#define PASS() { std::cout << "PASS\n"; ++passed; }
#define FAIL(msg) { std::cout << "FAIL: " << msg << "\n"; ++failed; }
#define CHECK(cond, msg) if (!(cond)) { FAIL(msg); return; } else {}
void test_cpp_allowed_path_normalizes_to_no_failure() {
TEST(cpp_allowed_path_normalizes_to_no_failure);
auto r = CrossLanguageConsistencyGate::evaluate({"cpp", "declaration", "rename", false, false, false, "header"});
CHECK(r.supported && r.allowed, "cpp allowed path should pass");
CHECK(r.standardizedFailure.empty(), "no failure expected");
CHECK(r.fallbackAction == "none", "no fallback expected");
PASS();
}
void test_python_side_effect_block_maps_to_safety_guard_blocked() {
TEST(python_side_effect_block_maps_to_safety_guard_blocked);
auto r = CrossLanguageConsistencyGate::evaluate({"python", "module_stmt", "delete", true});
CHECK(!r.allowed, "python side effect block expected");
CHECK(r.standardizedFailure == "safety_guard_blocked", "safety mapping expected");
CHECK(r.fallbackAction == "escalate_with_safety_context", "fallback mismatch");
PASS();
}
void test_rust_lifetime_block_maps_to_safety_guard_blocked() {
TEST(rust_lifetime_block_maps_to_safety_guard_blocked);
auto r = CrossLanguageConsistencyGate::evaluate({"rust", "borrow_region", "extract", false, true, false});
CHECK(!r.allowed, "rust lifetime block expected");
CHECK(r.standardizedFailure == "safety_guard_blocked", "safety mapping expected");
PASS();
}
void test_go_concurrency_block_maps_to_safety_guard_blocked() {
TEST(go_concurrency_block_maps_to_safety_guard_blocked);
auto r = CrossLanguageConsistencyGate::evaluate({"go", "channel", "extract", false, false, true});
CHECK(!r.allowed, "go concurrency block expected");
CHECK(r.standardizedFailure == "safety_guard_blocked", "safety mapping expected");
PASS();
}
void test_illegal_operation_maps_to_suggest_legal_ops_fallback() {
TEST(illegal_operation_maps_to_suggest_legal_ops_fallback);
auto r = CrossLanguageConsistencyGate::evaluate({"typescript", "signature", "rename"});
CHECK(!r.allowed, "illegal op expected");
CHECK(r.standardizedFailure == "operation_not_legal_for_construct", "operation mapping expected");
CHECK(r.fallbackAction == "suggest_legal_ops", "fallback mismatch");
PASS();
}
void test_unsupported_construct_maps_to_readonly_fallback() {
TEST(unsupported_construct_maps_to_readonly_fallback);
auto r = CrossLanguageConsistencyGate::evaluate({"cpp", "template_magic", "rename"});
CHECK(!r.supported, "unsupported construct should fail support");
CHECK(r.standardizedFailure == "unsupported_construct", "construct mapping expected");
CHECK(r.fallbackAction == "fallback_to_readonly", "fallback mismatch");
PASS();
}
void test_unsupported_language_maps_to_readonly_fallback() {
TEST(unsupported_language_maps_to_readonly_fallback);
auto r = CrossLanguageConsistencyGate::evaluate({"haskell", "function", "rename"});
CHECK(!r.supported, "unsupported language should fail support");
CHECK(r.standardizedFailure == "unsupported_language", "language mapping expected");
CHECK(r.fallbackAction == "fallback_to_readonly", "fallback mismatch");
PASS();
}
void test_python_allowed_path_is_consistent() {
TEST(python_allowed_path_is_consistent);
auto r = CrossLanguageConsistencyGate::evaluate({"python", "function", "rename"});
CHECK(r.supported && r.allowed, "python allowed path expected");
CHECK(r.fallbackAction == "none", "allowed path fallback should be none");
PASS();
}
void test_rust_allowed_path_is_consistent() {
TEST(rust_allowed_path_is_consistent);
auto r = CrossLanguageConsistencyGate::evaluate({"rust", "function", "rename"});
CHECK(r.supported && r.allowed, "rust allowed path expected");
PASS();
}
void test_cpp_boundary_block_maps_to_safety_guard_blocked() {
TEST(cpp_boundary_block_maps_to_safety_guard_blocked);
auto r = CrossLanguageConsistencyGate::evaluate({"cpp", "definition", "inline", false, false, false, "header"});
CHECK(!r.allowed, "cpp boundary block expected");
CHECK(r.standardizedFailure == "safety_guard_blocked", "safety mapping expected");
PASS();
}
void test_unknown_diag_maps_to_constraint_failure_fallback_retry() {
TEST(unknown_diag_maps_to_constraint_failure_fallback_retry);
auto r = CrossLanguageConsistencyGate::evaluate({"cpp", "member", "extract", false, false, false, "source"});
CHECK(!r.allowed, "member extract in source should fail");
CHECK(r.standardizedFailure == "operation_not_legal_for_construct", "expected canonical op failure");
CHECK(r.fallbackAction == "suggest_legal_ops", "expected legal op fallback");
PASS();
}
void test_diagnostics_are_preserved_for_downstream_use() {
TEST(diagnostics_are_preserved_for_downstream_use);
auto r = CrossLanguageConsistencyGate::evaluate({"go", "channel", "extract", false, false, true});
CHECK(!r.diagnostics.empty(), "raw diagnostics should be preserved");
CHECK(r.diagnostics[0] == "go_concurrency_extract_blocked", "expected raw diagnostic");
PASS();
}
int main() {
std::cout << "Step 542: Cross-Language Consistency Gate\n";
test_cpp_allowed_path_normalizes_to_no_failure(); // 1
test_python_side_effect_block_maps_to_safety_guard_blocked(); // 2
test_rust_lifetime_block_maps_to_safety_guard_blocked(); // 3
test_go_concurrency_block_maps_to_safety_guard_blocked(); // 4
test_illegal_operation_maps_to_suggest_legal_ops_fallback(); // 5
test_unsupported_construct_maps_to_readonly_fallback(); // 6
test_unsupported_language_maps_to_readonly_fallback(); // 7
test_python_allowed_path_is_consistent(); // 8
test_rust_allowed_path_is_consistent(); // 9
test_cpp_boundary_block_maps_to_safety_guard_blocked(); // 10
test_unknown_diag_maps_to_constraint_failure_fallback_retry(); // 11
test_diagnostics_are_preserved_for_downstream_use(); // 12
std::cout << "\nResults: " << passed << "/" << (passed + failed) << " passed\n";
return failed == 0 ? 0 : 1;
}

View File

@@ -9247,3 +9247,37 @@ concurrency-sensitive guardrails for high-risk transform paths.
- `editor/src/RustGoConstructiveEditAdapter.h` within header-size limit (`91` <= `600`)
- `editor/tests/step541_test.cpp` within test-file size guidance (`127` lines)
- Header-only architecture and naming conventions remain aligned with `ARCHITECTURE.md`
### Step 542: Cross-Language Consistency Gate
**Status:** PASS (12/12 tests)
Implements a cross-language consistency gate that normalizes adapter outcomes,
standardizes failure categories, and applies consistent fallback behavior across
C/C++, Python/TypeScript, and Rust/Go adapters.
**Files added:**
- `editor/src/CrossLanguageConsistencyGate.h` - consistency gate module:
- routes language-specific requests to the appropriate constructive adapter
- normalizes adapter diagnostics into canonical failure categories
- assigns standardized fallback actions (`readonly`, `suggest_legal_ops`, `escalate`, `retry`)
- preserves raw adapter diagnostics for downstream handling
- `editor/tests/step542_test.cpp` - 12 tests covering:
- allowed-path normalization across languages
- safety-guard failure normalization for side-effect/lifetime/concurrency blocks
- illegal-operation and unsupported construct/language normalization
- fallback action consistency by canonical failure type
- diagnostic preservation behavior
**Files modified:**
- `editor/CMakeLists.txt` - `step542_test` target
**Verification run:**
- `cmake -S editor -B editor/build-native` - PASS
- `cmake --build editor/build-native --target step542_test step541_test` - PASS
- `./editor/build-native/step542_test` - PASS (12/12)
- `./editor/build-native/step541_test` - PASS (12/12) regression coverage
**Architecture gate check:**
- `editor/src/CrossLanguageConsistencyGate.h` within header-size limit (`108` <= `600`)
- `editor/tests/step542_test.cpp` within test-file size guidance (`132` lines)
- Header-only architecture and naming conventions remain aligned with `ARCHITECTURE.md`