Step 539: add C/C++ constructive edit adapter

This commit is contained in:
Bill
2026-02-17 09:38:11 -07:00
parent 1c798f9323
commit 1f3ee28cfb
4 changed files with 257 additions and 0 deletions

View File

@@ -3730,4 +3730,13 @@ target_link_libraries(step538_test PRIVATE
tree_sitter_javascript tree_sitter_typescript tree_sitter_javascript tree_sitter_typescript
tree_sitter_java tree_sitter_rust tree_sitter_go) tree_sitter_java tree_sitter_rust tree_sitter_go)
add_executable(step539_test tests/step539_test.cpp)
target_include_directories(step539_test PRIVATE src)
target_link_libraries(step539_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) # Step 12: Dear ImGui shell scaffolding created (main.cpp exists but not built due to dependencies)

View File

@@ -0,0 +1,90 @@
#pragma once
// Step 539: C/C++ Constructive Edit Adapter
#include <set>
#include <string>
#include <vector>
struct CppConstructiveEditRequest {
std::string language; // c or cpp
std::string constructKind; // declaration/definition/include/macro/class/member
std::string fileType; // header/source
std::string operation;
};
struct CppConstructiveEditResult {
bool supported = false;
bool allowed = false;
std::vector<std::string> legalOperations;
std::vector<std::string> diagnostics;
};
class CppConstructiveEditAdapter {
public:
static CppConstructiveEditResult evaluate(const CppConstructiveEditRequest& request) {
CppConstructiveEditResult result;
if (request.language != "c" && request.language != "cpp") {
result.diagnostics.push_back("unsupported_language");
return result;
}
result.supported = true;
result.legalOperations = legalOps(request.constructKind, request.fileType);
if (result.legalOperations.empty()) {
result.supported = false;
result.diagnostics.push_back("unsupported_construct");
return result;
}
if (!contains(result.legalOperations, request.operation)) {
result.diagnostics.push_back("operation_not_legal_for_construct");
return result;
}
auto boundary = boundaryGuard(request);
if (!boundary.empty()) {
result.diagnostics.push_back(boundary);
return result;
}
result.allowed = true;
return result;
}
private:
static std::vector<std::string> legalOps(const std::string& kind,
const std::string& fileType) {
if (kind == "declaration") return {"rename", "update", "delete", "extract"};
if (kind == "definition") return {"rename", "update", "extract", "inline"};
if (kind == "include") return {"insert", "delete", "reorder"};
if (kind == "macro") return {"rename", "update", "delete"};
if (kind == "class") return {"rename", "update", "extract"};
if (kind == "member") {
if (fileType == "header") return {"rename", "update", "extract"};
return {"rename", "update"};
}
return {};
}
static std::string boundaryGuard(const CppConstructiveEditRequest& request) {
// Header/source safety: definitions and include edits should stay in safe domains.
if (request.constructKind == "definition" && request.fileType == "header" &&
request.operation == "inline") {
return "header_source_boundary_violation";
}
if (request.constructKind == "include" && request.fileType == "source" &&
request.operation == "reorder") {
return "source_include_reorder_blocked";
}
if (request.constructKind == "macro" && request.fileType == "source" &&
request.operation == "delete") {
return "macro_delete_requires_header_review";
}
return "";
}
static bool contains(const std::vector<std::string>& values,
const std::string& value) {
return std::set<std::string>(values.begin(), values.end()).count(value) != 0;
}
};

View File

@@ -0,0 +1,126 @@
// Step 539: C/C++ Constructive Edit Adapter (12 tests)
#include "CppConstructiveEditAdapter.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 {}
static bool hasDiag(const CppConstructiveEditResult& r, const std::string& d) {
for (const auto& x : r.diagnostics) if (x == d) return true;
return false;
}
void test_cpp_declaration_rename_allowed() {
TEST(cpp_declaration_rename_allowed);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "declaration", "header", "rename"});
CHECK(r.supported && r.allowed, "declaration rename should be allowed");
PASS();
}
void test_cpp_definition_extract_allowed_in_source() {
TEST(cpp_definition_extract_allowed_in_source);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "definition", "source", "extract"});
CHECK(r.allowed, "source definition extract should be allowed");
PASS();
}
void test_include_insert_allowed() {
TEST(include_insert_allowed);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "include", "header", "insert"});
CHECK(r.allowed, "include insert should be allowed");
PASS();
}
void test_include_reorder_blocked_in_source() {
TEST(include_reorder_blocked_in_source);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "include", "source", "reorder"});
CHECK(!r.allowed, "source include reorder should be blocked");
CHECK(hasDiag(r, "source_include_reorder_blocked"), "boundary diagnostic expected");
PASS();
}
void test_macro_update_allowed() {
TEST(macro_update_allowed);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "macro", "header", "update"});
CHECK(r.allowed, "macro update should be allowed");
PASS();
}
void test_macro_delete_blocked_in_source() {
TEST(macro_delete_blocked_in_source);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "macro", "source", "delete"});
CHECK(!r.allowed, "macro delete in source should be blocked");
CHECK(hasDiag(r, "macro_delete_requires_header_review"), "macro boundary diagnostic expected");
PASS();
}
void test_member_extract_allowed_in_header() {
TEST(member_extract_allowed_in_header);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "member", "header", "extract"});
CHECK(r.allowed, "header member extract should be allowed");
PASS();
}
void test_member_extract_blocked_in_source() {
TEST(member_extract_blocked_in_source);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "member", "source", "extract"});
CHECK(!r.allowed, "source member extract should be illegal");
CHECK(hasDiag(r, "operation_not_legal_for_construct"), "illegal-op diagnostic expected");
PASS();
}
void test_definition_inline_blocked_in_header() {
TEST(definition_inline_blocked_in_header);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "definition", "header", "inline"});
CHECK(!r.allowed, "header definition inline should be blocked");
CHECK(hasDiag(r, "header_source_boundary_violation"), "boundary diagnostic expected");
PASS();
}
void test_unsupported_construct_rejected() {
TEST(unsupported_construct_rejected);
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "template_magic", "header", "rename"});
CHECK(!r.supported, "unsupported construct should not be supported");
CHECK(hasDiag(r, "unsupported_construct"), "unsupported construct diagnostic expected");
PASS();
}
void test_unsupported_language_rejected() {
TEST(unsupported_language_rejected);
auto r = CppConstructiveEditAdapter::evaluate({"typescript", "definition", "source", "update"});
CHECK(!r.supported, "unsupported language should not be supported");
CHECK(hasDiag(r, "unsupported_language"), "unsupported language diagnostic expected");
PASS();
}
void test_c_language_paths_supported() {
TEST(c_language_paths_supported);
auto r = CppConstructiveEditAdapter::evaluate({"c", "definition", "source", "update"});
CHECK(r.supported && r.allowed, "c definition update should be supported");
PASS();
}
int main() {
std::cout << "Step 539: C/C++ Constructive Edit Adapter\n";
test_cpp_declaration_rename_allowed(); // 1
test_cpp_definition_extract_allowed_in_source();// 2
test_include_insert_allowed(); // 3
test_include_reorder_blocked_in_source(); // 4
test_macro_update_allowed(); // 5
test_macro_delete_blocked_in_source(); // 6
test_member_extract_allowed_in_header(); // 7
test_member_extract_blocked_in_source(); // 8
test_definition_inline_blocked_in_header(); // 9
test_unsupported_construct_rejected(); // 10
test_unsupported_language_rejected(); // 11
test_c_language_paths_supported(); // 12
std::cout << "\nResults: " << passed << "/" << (passed + failed) << " passed\n";
return failed == 0 ? 0 : 1;
}

View File

@@ -9149,3 +9149,35 @@ telemetry into a single constrained execution surface for agent-facing editing.
- **Steps completed:** 5 - **Steps completed:** 5
- **New tests in phase plan:** 56/56 passing - **New tests in phase plan:** 56/56 passing
- **Legal-choice API + validation + telemetry integration:** PASS - **Legal-choice API + validation + telemetry integration:** PASS
### Step 539: C/C++ Constructive Edit Adapter
**Status:** PASS (12/12 tests)
Implements a C/C++ constructive edit adapter with construct-aware legal
operation mapping and header/source boundary safety checks.
**Files added:**
- `editor/src/CppConstructiveEditAdapter.h` - C/C++ adapter module:
- legal operation mappings for declarations/definitions/includes/macros/classes/members
- language support for both `c` and `cpp`
- header/source boundary guards for sensitive operations
- structured diagnostics for unsupported constructs/languages and blocked ops
- `editor/tests/step539_test.cpp` - 12 tests covering:
- legal operation paths across declarations, definitions, includes, macros, members
- boundary-safe rejections (header/source violations)
- unsupported construct/language behavior
- C language compatibility path
**Files modified:**
- `editor/CMakeLists.txt` - `step539_test` target
**Verification run:**
- `cmake -S editor -B editor/build-native` - PASS
- `cmake --build editor/build-native --target step539_test step538_test` - PASS
- `./editor/build-native/step539_test` - PASS (12/12)
- `./editor/build-native/step538_test` - PASS (8/8) regression coverage
**Architecture gate check:**
- `editor/src/CppConstructiveEditAdapter.h` within header-size limit (`90` <= `600`)
- `editor/tests/step539_test.cpp` within test-file size guidance (`126` lines)
- Header-only architecture and naming conventions remain aligned with `ARCHITECTURE.md`