Step 540: add Python/TypeScript constructive edit adapter

This commit is contained in:
Bill
2026-02-17 09:41:57 -07:00
parent 1f3ee28cfb
commit 85b8a8e07b
4 changed files with 251 additions and 0 deletions

View File

@@ -3739,4 +3739,13 @@ target_link_libraries(step539_test PRIVATE
tree_sitter_javascript tree_sitter_typescript
tree_sitter_java tree_sitter_rust tree_sitter_go)
add_executable(step540_test tests/step540_test.cpp)
target_include_directories(step540_test PRIVATE src)
target_link_libraries(step540_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,84 @@
#pragma once
// Step 540: Python/TypeScript Constructive Edit Adapter
#include <set>
#include <string>
#include <vector>
struct PyTsConstructiveEditRequest {
std::string language; // python/typescript
std::string constructKind; // function/class/import/signature/module_stmt
std::string operation;
bool moduleSideEffectRisk = false;
};
struct PyTsConstructiveEditResult {
bool supported = false;
bool allowed = false;
std::vector<std::string> legalOperations;
std::vector<std::string> diagnostics;
};
class PythonTypeScriptConstructiveEditAdapter {
public:
static PyTsConstructiveEditResult evaluate(const PyTsConstructiveEditRequest& request) {
PyTsConstructiveEditResult result;
if (request.language != "python" && request.language != "typescript") {
result.diagnostics.push_back("unsupported_language");
return result;
}
result.supported = true;
result.legalOperations = legalOps(request.constructKind);
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 sideEffect = sideEffectGuard(request);
if (!sideEffect.empty()) {
result.diagnostics.push_back(sideEffect);
return result;
}
result.allowed = true;
return result;
}
private:
static std::vector<std::string> legalOps(const std::string& kind) {
if (kind == "function") return {"rename", "update", "extract", "inline"};
if (kind == "class") return {"rename", "update", "extract"};
if (kind == "import") return {"insert", "delete", "reorder"};
if (kind == "signature") return {"update", "extract"};
if (kind == "module_stmt") return {"update", "delete"};
return {};
}
static std::string sideEffectGuard(const PyTsConstructiveEditRequest& request) {
// Module-level side effects: block destructive/reorder ops when flagged risky.
if (!request.moduleSideEffectRisk) return "";
if (request.constructKind == "import" && request.operation == "reorder") {
return "module_side_effect_reorder_blocked";
}
if (request.constructKind == "module_stmt" && request.operation == "delete") {
return "module_side_effect_delete_blocked";
}
if (request.constructKind == "function" && request.operation == "inline") {
return "module_side_effect_inline_blocked";
}
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 540: Python/TypeScript Constructive Edit Adapter (12 tests)
#include "PythonTypeScriptConstructiveEditAdapter.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 PyTsConstructiveEditResult& r, const std::string& d) {
for (const auto& x : r.diagnostics) if (x == d) return true;
return false;
}
void test_python_function_rename_allowed() {
TEST(python_function_rename_allowed);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"python", "function", "rename", false});
CHECK(r.supported && r.allowed, "python function rename should be allowed");
PASS();
}
void test_python_class_extract_allowed() {
TEST(python_class_extract_allowed);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"python", "class", "extract", false});
CHECK(r.allowed, "python class extract should be allowed");
PASS();
}
void test_typescript_import_reorder_allowed_without_side_effect_risk() {
TEST(typescript_import_reorder_allowed_without_side_effect_risk);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"typescript", "import", "reorder", false});
CHECK(r.allowed, "ts import reorder should be allowed when risk false");
PASS();
}
void test_typescript_import_reorder_blocked_with_side_effect_risk() {
TEST(typescript_import_reorder_blocked_with_side_effect_risk);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"typescript", "import", "reorder", true});
CHECK(!r.allowed, "ts import reorder should be blocked when risk true");
CHECK(hasDiag(r, "module_side_effect_reorder_blocked"), "side-effect diag expected");
PASS();
}
void test_module_stmt_delete_blocked_with_side_effect_risk() {
TEST(module_stmt_delete_blocked_with_side_effect_risk);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"python", "module_stmt", "delete", true});
CHECK(!r.allowed, "module stmt delete should be blocked with risk");
CHECK(hasDiag(r, "module_side_effect_delete_blocked"), "side-effect delete diag expected");
PASS();
}
void test_function_inline_blocked_with_side_effect_risk() {
TEST(function_inline_blocked_with_side_effect_risk);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"python", "function", "inline", true});
CHECK(!r.allowed, "inline should be blocked with module risk");
CHECK(hasDiag(r, "module_side_effect_inline_blocked"), "side-effect inline diag expected");
PASS();
}
void test_signature_update_allowed() {
TEST(signature_update_allowed);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"typescript", "signature", "update", false});
CHECK(r.allowed, "signature update should be allowed");
PASS();
}
void test_signature_rename_not_legal() {
TEST(signature_rename_not_legal);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"typescript", "signature", "rename", false});
CHECK(!r.allowed, "signature rename should be illegal");
CHECK(hasDiag(r, "operation_not_legal_for_construct"), "illegal op diag expected");
PASS();
}
void test_import_delete_allowed_without_side_effect_risk() {
TEST(import_delete_allowed_without_side_effect_risk);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"python", "import", "delete", false});
CHECK(r.allowed, "import delete should be allowed when risk false");
PASS();
}
void test_unsupported_construct_rejected() {
TEST(unsupported_construct_rejected);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"python", "decorator_stack", "update", false});
CHECK(!r.supported, "unsupported construct should not be supported");
CHECK(hasDiag(r, "unsupported_construct"), "unsupported construct diag expected");
PASS();
}
void test_unsupported_language_rejected() {
TEST(unsupported_language_rejected);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"cpp", "function", "rename", false});
CHECK(!r.supported, "unsupported language should fail");
CHECK(hasDiag(r, "unsupported_language"), "unsupported language diag expected");
PASS();
}
void test_typescript_function_extract_allowed() {
TEST(typescript_function_extract_allowed);
auto r = PythonTypeScriptConstructiveEditAdapter::evaluate({"typescript", "function", "extract", false});
CHECK(r.allowed, "ts function extract should be allowed");
PASS();
}
int main() {
std::cout << "Step 540: Python/TypeScript Constructive Edit Adapter\n";
test_python_function_rename_allowed(); // 1
test_python_class_extract_allowed(); // 2
test_typescript_import_reorder_allowed_without_side_effect_risk(); // 3
test_typescript_import_reorder_blocked_with_side_effect_risk();// 4
test_module_stmt_delete_blocked_with_side_effect_risk(); // 5
test_function_inline_blocked_with_side_effect_risk(); // 6
test_signature_update_allowed(); // 7
test_signature_rename_not_legal(); // 8
test_import_delete_allowed_without_side_effect_risk(); // 9
test_unsupported_construct_rejected(); // 10
test_unsupported_language_rejected(); // 11
test_typescript_function_extract_allowed(); // 12
std::cout << "\nResults: " << passed << "/" << (passed + failed) << " passed\n";
return failed == 0 ? 0 : 1;
}

View File

@@ -9181,3 +9181,35 @@ operation mapping and header/source boundary safety checks.
- `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`
### Step 540: Python/TypeScript Constructive Edit Adapter
**Status:** PASS (12/12 tests)
Implements Python/TypeScript constructive edit adaptation with construct-aware
legal operation mappings and module-level side-effect protection guards.
**Files added:**
- `editor/src/PythonTypeScriptConstructiveEditAdapter.h` - Python/TypeScript adapter module:
- legal operation mappings for functions/classes/imports/signatures/module statements
- shared support for both `python` and `typescript`
- module side-effect risk guardrails for reorder/delete/inline-sensitive paths
- structured diagnostics for unsupported constructs/languages and illegal ops
- `editor/tests/step540_test.cpp` - 12 tests covering:
- legal operation paths across Python/TypeScript constructs
- side-effect risk blocking behavior
- illegal operation rejections for constrained constructs
- unsupported construct/language behavior
**Files modified:**
- `editor/CMakeLists.txt` - `step540_test` target
**Verification run:**
- `cmake -S editor -B editor/build-native` - PASS
- `cmake --build editor/build-native --target step540_test step539_test` - PASS
- `./editor/build-native/step540_test` - PASS (12/12)
- `./editor/build-native/step539_test` - PASS (12/12) regression coverage
**Architecture gate check:**
- `editor/src/PythonTypeScriptConstructiveEditAdapter.h` within header-size limit (`84` <= `600`)
- `editor/tests/step540_test.cpp` within test-file size guidance (`126` lines)
- Header-only architecture and naming conventions remain aligned with `ARCHITECTURE.md`