Step 647: self-modification safety guard
This commit is contained in:
@@ -4702,4 +4702,13 @@ target_link_libraries(step646_test PRIVATE
|
||||
tree_sitter_javascript tree_sitter_typescript
|
||||
tree_sitter_java tree_sitter_rust tree_sitter_go)
|
||||
|
||||
add_executable(step647_test tests/step647_test.cpp)
|
||||
target_include_directories(step647_test PRIVATE src)
|
||||
target_link_libraries(step647_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)
|
||||
|
||||
28
editor/src/SelfModificationSafetyGuard.h
Normal file
28
editor/src/SelfModificationSafetyGuard.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
// Step 647: Self-modification safety guard
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class SelfModificationSafetyGuard {
|
||||
public:
|
||||
static bool isLiveBinaryPath(const std::string& filePath,
|
||||
const std::vector<std::string>& liveRoots) {
|
||||
for (const auto& root : liveRoots) {
|
||||
if (!root.empty() && filePath.rfind(root, 0) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool allowMutation(const std::string& filePath,
|
||||
const std::vector<std::string>& liveRoots,
|
||||
std::string* reason) {
|
||||
if (!reason) return false;
|
||||
reason->clear();
|
||||
if (isLiveBinaryPath(filePath, liveRoots)) {
|
||||
*reason = "Cannot mutate live binary - use a build/restart cycle.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
25
editor/tests/step647_test.cpp
Normal file
25
editor/tests/step647_test.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
// Step 647: Self-modification safety guard (12 tests)
|
||||
|
||||
#include "SelfModificationSafetyGuard.h"
|
||||
#include <iostream>
|
||||
|
||||
static int p=0,f=0;
|
||||
#define T(n) { std::cout<<" "<<#n<<"... "; }
|
||||
#define P() { std::cout<<"PASS\n"; ++p; }
|
||||
#define F(m) { std::cout<<"FAIL: "<<m<<"\n"; ++f; }
|
||||
#define C(c,m) if(!(c)){F(m);return;}
|
||||
|
||||
void t1(){T(live_path_detected_for_editor_src);C(SelfModificationSafetyGuard::isLiveBinaryPath("editor/src/main.cpp",{"editor/src"}),"should detect live path");P();}
|
||||
void t2(){T(non_live_path_not_detected);C(!SelfModificationSafetyGuard::isLiveBinaryPath("docs/readme.md",{"editor/src"}),"should not detect");P();}
|
||||
void t3(){T(empty_live_roots_allows_non_detection);C(!SelfModificationSafetyGuard::isLiveBinaryPath("editor/src/main.cpp",{}),"empty roots should not match");P();}
|
||||
void t4(){T(allow_mutation_true_for_non_live_file);std::string r;C(SelfModificationSafetyGuard::allowMutation("docs/readme.md",{"editor/src"},&r),"should allow");P();}
|
||||
void t5(){T(allow_mutation_false_for_live_file);std::string r;C(!SelfModificationSafetyGuard::allowMutation("editor/src/main.cpp",{"editor/src"},&r),"should block");P();}
|
||||
void t6(){T(block_reason_contains_required_message);std::string r;SelfModificationSafetyGuard::allowMutation("editor/src/main.cpp",{"editor/src"},&r);C(r.find("Cannot mutate live binary")!=std::string::npos,"reason missing");P();}
|
||||
void t7(){T(allow_mutation_clears_reason_on_success);std::string r="x";SelfModificationSafetyGuard::allowMutation("docs/x",{"editor/src"},&r);C(r.empty(),"reason should clear");P();}
|
||||
void t8(){T(null_reason_pointer_rejected);C(!SelfModificationSafetyGuard::allowMutation("docs/x",{"editor/src"},nullptr),"null reason should fail");P();}
|
||||
void t9(){T(multi_root_live_match_detected);C(SelfModificationSafetyGuard::isLiveBinaryPath("editor/tests/a.cpp",{"editor/src","editor/tests"}),"multi-root should match");P();}
|
||||
void t10(){T(prefix_match_respects_root_start);C(!SelfModificationSafetyGuard::isLiveBinaryPath("tmp/editor/src/main.cpp",{"editor/src"}),"must match prefix");P();}
|
||||
void t11(){T(empty_path_is_allowed);std::string r;C(SelfModificationSafetyGuard::allowMutation("",{"editor/src"},&r),"empty path should allow");P();}
|
||||
void t12(){T(root_empty_is_ignored);C(!SelfModificationSafetyGuard::isLiveBinaryPath("editor/src/main.cpp",{""}),"empty root ignored");P();}
|
||||
|
||||
int main(){std::cout<<"Step 647: Self-modification safety guard\n";t1();t2();t3();t4();t5();t6();t7();t8();t9();t10();t11();t12();std::cout<<"\nResults: "<<p<<"/"<<(p+f)<<" passed\n";return f?1:0;}
|
||||
24
progress.md
24
progress.md
@@ -13252,3 +13252,27 @@ results, summary counters, completion status, and failure-to-source linking.
|
||||
- `editor/src/InEditorTestRunnerModel.h` (`40` <= `600`)
|
||||
- `editor/tests/step646_test.cpp` within test-file size guidance (`25` lines)
|
||||
- Header-only architecture and naming conventions remain aligned with `ARCHITECTURE.md`
|
||||
|
||||
### Step 647: Self-modification safety guard
|
||||
**Status:** PASS (12/12 tests)
|
||||
|
||||
Adds a self-modification guard for live editor-binary paths with explicit block
|
||||
reason messaging and allow/block decision helpers for mutation requests.
|
||||
|
||||
**Files added:**
|
||||
- `editor/src/SelfModificationSafetyGuard.h` - live-binary mutation guard model
|
||||
- `editor/tests/step647_test.cpp` - 12 tests for path matching, block reasons, and edges
|
||||
|
||||
**Files modified:**
|
||||
- `editor/CMakeLists.txt` - `step647_test` target
|
||||
|
||||
**Verification run:**
|
||||
- `cmake -S editor -B editor/build-native` - PASS
|
||||
- `cmake --build editor/build-native --target step647_test step646_test` - PASS
|
||||
- `./editor/build-native/step647_test` - PASS (12/12)
|
||||
- `./editor/build-native/step646_test` - PASS (12/12) regression coverage
|
||||
|
||||
**Architecture gate check:**
|
||||
- `editor/src/SelfModificationSafetyGuard.h` (`28` <= `600`)
|
||||
- `editor/tests/step647_test.cpp` within test-file size guidance (`25` lines)
|
||||
- Header-only architecture and naming conventions remain aligned with `ARCHITECTURE.md`
|
||||
|
||||
Reference in New Issue
Block a user