载入中...
搜索中...
未找到
McpTaskProfile.cpp
浏览该文件的文档.
2
4
5#include <cstdlib>
6#include <mutex>
7#include <unordered_set>
8
9namespace eve::dev {
10namespace {
11
12std::mutex g_mu;
13std::string g_processProfile;
14std::string g_sessionProfile;
15McpFilterStats g_stats;
16bool g_processLoaded = false;
17
18void ensureProcessLoadedUnlocked() {
19 if (g_processLoaded) return;
20 if (const char* raw = std::getenv("EVE_MCP_PROFILE")) {
21 g_processProfile = raw;
22 }
23 g_processLoaded = true;
24}
25
26bool isKnownTaskKind(std::string_view kind) {
27 return kind == "content" || kind == "script" || kind == "verify" || kind == "fix" || kind == "workshop";
28}
29
30bool isKnownProfile(std::string_view profile) {
31 return profile == "content.scene" || profile == "content.map" || profile == "script.bind" ||
32 profile == "verify.play" || profile == "fix.compile" || profile == "workshop.rpg";
33}
34
35std::string defaultProfileForKind(std::string_view kind) {
36 if (kind == "content") return "content.scene";
37 if (kind == "script") return "script.bind";
38 if (kind == "fix") return "fix.compile";
39 if (kind == "workshop") return "workshop.rpg";
40 return "verify.play";
41}
42
43std::string taskKindForProfile(std::string_view profile) {
44 if (profile.starts_with("content.")) return "content";
45 if (profile.starts_with("script.")) return "script";
46 if (profile.starts_with("fix.")) return "fix";
47 if (profile.starts_with("workshop.")) return "workshop";
48 return "verify";
49}
50
51bool startsWith(std::string_view value, std::string_view prefix) {
52 return value.size() >= prefix.size() && value.compare(0, prefix.size(), prefix) == 0;
53}
54
55bool equalsAny(std::string_view value, std::initializer_list<const char*> names) {
56 for (const char* name : names) {
57 if (value == name) return true;
58 }
59 return false;
60}
61
62bool isDangerEvalTool(std::string_view tool) { return tool == "eve_eval" || tool == "eve_run_script"; }
63
64bool isAgentSessionTool(std::string_view tool) { return startsWith(tool, "eve_agent_"); }
65
66bool isCoreDiscoverTool(std::string_view tool) {
67 return equalsAny(tool, {"eve_status", "eve_game_world", "eve_ai_log", "eve_ai_note", "eve_crash_report",
68 "eve_console_read", "eve_console_write", "eve_console_clear"}) ||
69 startsWith(tool, "eve_asset_kit_");
70}
71
72bool tagsAllow(std::string_view tool, const std::unordered_set<std::string>& tags) {
73 for (const auto& tag : mcpToolTags(tool)) {
74 if (tags.count(tag) != 0) return true;
75 }
76 return false;
77}
78
79} // namespace
80
81bool mcpResolveTaskProfile(std::string_view taskKind, std::string_view profile, std::string* outProfile,
82 std::string* outTaskKind, std::string* errorCode, std::string* errorMessage) {
83 std::string kind = std::string(taskKind);
84 std::string prof = std::string(profile);
85 if (kind.empty() && prof.empty()) {
86 kind = "verify";
87 prof = "verify.play";
88 } else if (kind.empty()) {
89 if (!isKnownProfile(prof)) {
90 if (errorCode) *errorCode = "invalid-profile";
91 if (errorMessage) *errorMessage = "profile must be a known task profile id";
92 return false;
93 }
94 kind = taskKindForProfile(prof);
95 } else if (prof.empty()) {
96 if (!isKnownTaskKind(kind)) {
97 if (errorCode) *errorCode = "invalid-task-kind";
98 if (errorMessage) *errorMessage = "taskKind must be content, script, verify, fix, or workshop";
99 return false;
100 }
101 prof = defaultProfileForKind(kind);
102 } else {
103 if (!isKnownTaskKind(kind)) {
104 if (errorCode) *errorCode = "invalid-task-kind";
105 if (errorMessage) *errorMessage = "taskKind must be content, script, verify, fix, or workshop";
106 return false;
107 }
108 if (!isKnownProfile(prof)) {
109 if (errorCode) *errorCode = "invalid-profile";
110 if (errorMessage) *errorMessage = "profile must be a known task profile id";
111 return false;
112 }
113 if (taskKindForProfile(prof) != kind) {
114 if (errorCode) *errorCode = "profile-task-mismatch";
115 if (errorMessage) *errorMessage = "profile does not belong to the given taskKind";
116 return false;
117 }
118 }
119 if (outProfile) *outProfile = std::move(prof);
120 if (outTaskKind) *outTaskKind = std::move(kind);
121 return true;
122}
123
124std::vector<std::string> mcpProfileToolsetTags(std::string_view profile) {
125 if (profile == "content.scene") return {"agent", "core", "scene"};
126 if (profile == "content.map") return {"agent", "core", "editor"};
127 if (profile == "script.bind") return {"agent", "core", "debug"};
128 if (profile == "verify.play") return {"agent", "core", "play", "input"};
129 if (profile == "fix.compile") return {"agent", "core", "debug"};
130 if (profile == "workshop.rpg") return {"agent", "core", "play", "input", "editor", "scene"};
131 return {};
132}
133
134std::string mcpProfileToolsetEnv(std::string_view profile) {
135 const auto tags = mcpProfileToolsetTags(profile);
136 std::string out;
137 for (const auto& tag : tags) {
138 if (!out.empty()) out += ',';
139 out += tag;
140 }
141 return out;
142}
143
144bool mcpProfileAllowsTool(std::string_view profile, std::string_view tool) {
145 if (profile.empty()) return true;
146 if (!isKnownProfile(profile)) return false;
147 // Session / agent tools stay reachable so the Agent can escalate, status, and complete.
148 // Asset kits are read-only pixel contracts; available on every profile before art gen.
149 if (isAgentSessionTool(tool) || tool == "eve_status" || tool == "eve_game_world" ||
150 startsWith(tool, "eve_asset_kit_") || tool == "eve_scene_search" || startsWith(tool, "eve_scene_search_"))
151 return true;
152
153 if (isDangerEvalTool(tool)) {
154 // Only the fix profile may touch eval/run_script; still not valid required evidence.
155 return profile == "fix.compile";
156 }
157 // Stable facade: never leak into content/verify via shared scene/play tags.
158 if (tool == "eve_workshop") return profile == "workshop.rpg";
159 // Recipes carry the discovery tag too; it must not grant mutation to
160 // verification or binding profiles through their shared agent toolset.
161 if (tool == "eve_recipe_apply") return profile == "content.scene" || profile == "workshop.rpg";
162
163 const auto tagList = mcpProfileToolsetTags(profile);
164 std::unordered_set<std::string> tags(tagList.begin(), tagList.end());
165
166 if (profile == "script.bind") {
167 if (equalsAny(tool, {"eve_api_search", "eve_api_get", "eve_console_read", "eve_console_write",
168 "eve_console_clear", "eve_error_slice", "eve_crash_report"}))
169 return true;
170 // Keep debug observations, but not breakpoints / snapshots / eval (eval already rejected).
171 if (equalsAny(tool, {"eve_stack", "eve_locals", "eve_list_breakpoints"})) return true;
172 return false;
173 }
174
175 if (profile == "verify.play") {
176 if (equalsAny(tool, {"eve_screenshot", "eve_screenshot_image", "eve_render_status", "eve_console_read"}))
177 return true;
178 return tagsAllow(tool, tags);
179 }
180
181 if (profile == "fix.compile") {
182 if (isCoreDiscoverTool(tool) || tool == "eve_restart" || tool == "eve_error_slice" ||
183 tool == "eve_api_search" || tool == "eve_api_get")
184 return true;
185 return tagsAllow(tool, tags);
186 }
187
188 if (profile == "content.scene") {
189 if (startsWith(tool, "eve_procgen_") || startsWith(tool, "eve_recipe_") ||
190 startsWith(tool, "eve_asset_kit_") || tool == "eve_scene_info" || tool == "eve_scene_director_status")
191 return true;
192 return tagsAllow(tool, tags);
193 }
194
195 if (profile == "content.map") {
196 if (tool == "eve_console_read") return true;
197 return tagsAllow(tool, tags);
198 }
199
200 if (profile == "workshop.rpg") {
201 if (tool == "eve_workshop" || tool == "eve_screenshot" || tool == "eve_screenshot_image" ||
202 tool == "eve_console_read" || tool == "eve_api_search" || tool == "eve_api_get")
203 return true;
204 return tagsAllow(tool, tags);
205 }
206
207 return tagsAllow(tool, tags);
208}
209
210std::optional<McpProfileEscalate> mcpProfileEscalateHint(std::string_view profile, std::string_view tool) {
211 if (profile.empty() || mcpProfileAllowsTool(profile, tool)) return std::nullopt;
212
214 hint.errorCode = "profile-denied";
215
216 if (isDangerEvalTool(tool) && profile != "fix.compile") {
217 hint.to = "fix.compile";
218 hint.reason = "eval and run_script are limited to fix.compile and cannot satisfy required criteria";
219 return hint;
220 }
221 if ((startsWith(tool, "eve_scene_") || startsWith(tool, "eve_camera_") || startsWith(tool, "eve_procgen_") ||
222 startsWith(tool, "eve_recipe_") || tool == "eve_whitebox_playtest") &&
223 (profile == "verify.play" || profile == "script.bind" || profile == "fix.compile")) {
224 hint.to = "content.scene";
225 hint.reason = "scene, recipe, or procgen mutation requires content.scene";
226 return hint;
227 }
228 if (tool == "eve_workshop" && profile != "workshop.rpg") {
229 hint.to = "workshop.rpg";
230 hint.reason = "workshop facade is limited to workshop.rpg";
231 return hint;
232 }
233 if ((startsWith(tool, "eve_editor_") || startsWith(tool, "eve_host_")) &&
234 (profile == "verify.play" || profile == "script.bind" || profile == "content.scene")) {
235 hint.to = profile == "content.scene" ? "content.map" : "workshop.rpg";
236 hint.reason = "editor/host tools require content.map or workshop.rpg";
237 return hint;
238 }
239 if ((tool == "eve_play" || tool == "eve_gameplay" || startsWith(tool, "eve_input_") ||
240 startsWith(tool, "eve_ui_")) &&
241 (profile == "content.scene" || profile == "content.map" || profile == "script.bind" ||
242 profile == "fix.compile")) {
243 hint.to = "verify.play";
244 hint.reason = "play/observe verification requires verify.play";
245 return hint;
246 }
247 if ((tool == "eve_api_search" || tool == "eve_api_get") &&
248 (profile == "content.scene" || profile == "content.map" || profile == "verify.play")) {
249 hint.to = "script.bind";
250 hint.reason = "binding-contract lookup is opened by script.bind";
251 return hint;
252 }
253 if ((tool == "eve_restart" || tool == "eve_error_slice") && profile != "fix.compile") {
254 hint.to = "fix.compile";
255 hint.reason = "compile/runtime repair tools require fix.compile";
256 return hint;
257 }
258 return std::nullopt;
259}
260
261void mcpSetProcessProfile(std::string_view profile) {
262 std::lock_guard lock(g_mu);
263 g_processProfile = std::string(profile);
264 g_processLoaded = true;
265}
266
267std::string mcpProcessProfile() {
268 std::lock_guard lock(g_mu);
269 ensureProcessLoadedUnlocked();
270 return g_processProfile;
271}
272
273void mcpSetSessionProfile(std::string_view profile) {
274 std::lock_guard lock(g_mu);
275 g_sessionProfile = std::string(profile);
276}
277
278std::string mcpSessionProfile() {
279 std::lock_guard lock(g_mu);
280 return g_sessionProfile;
281}
282
283void mcpFilterNoteCall(bool allowed, std::size_t resultBytes, bool offeredEscalate) {
284 std::lock_guard lock(g_mu);
285 ++g_stats.toolCalls;
286 if (!allowed) ++g_stats.toolsFiltered;
287 if (offeredEscalate) ++g_stats.escalateCount;
288 g_stats.approxResultBytes += static_cast<std::uint64_t>(resultBytes);
289}
290
292 std::lock_guard lock(g_mu);
293 return g_stats;
294}
295
297 std::lock_guard lock(g_mu);
298 g_stats = {};
299}
300
301} // namespace eve::dev
std::string name
Definition ECS.cpp:154
double raw
Named Agent task profiles that narrow the MCP tool surface.
std::unordered_set< std::string > tags
Toolset / read-only / exclude filtering for the embedded MCP server.
const std::string * tag
Object::Ptr value
ScriptStructure out
std::string kind
bool mcpResolveTaskProfile(std::string_view taskKind, std::string_view profile, std::string *outProfile, std::string *outTaskKind, std::string *errorCode, std::string *errorMessage)
Resolve taskKind + optional profile into a concrete profile id.
std::string mcpProcessProfile()
Current process-level profile; empty when unset.
std::vector< std::string > mcpProfileToolsetTags(std::string_view profile)
Tag list advertised for EVE_MCP_TOOLSET when only a named profile is chosen.
std::vector< std::string > mcpToolTags(std::string_view name)
Classify a built-in or script tool into toolset tags.
std::string mcpProfileToolsetEnv(std::string_view profile)
Comma-joined toolset tags for env forwarding (eve-mcp --profile=).
void mcpSetProcessProfile(std::string_view profile)
Install or clear the process-level profile (EVE_MCP_PROFILE mirror for tests).
McpFilterStats mcpFilterStats()
Snapshot of proxy metrics since process start / last test reset.
void mcpFilterStatsResetForTest()
Test helper: zero proxy metrics.
bool mcpProfileAllowsTool(std::string_view profile, std::string_view tool)
Whether tool is allowed under a concrete named profile (empty = no profile gate).
void mcpFilterNoteCall(bool allowed, std::size_t resultBytes, bool offeredEscalate)
Record a tools/call attempt for proxy metrics.
std::string mcpSessionProfile()
Current session-level profile; empty when no session overlay.
std::optional< McpProfileEscalate > mcpProfileEscalateHint(std::string_view profile, std::string_view tool)
Suggest an escalate edge when tool is denied by profile.
void mcpSetSessionProfile(std::string_view profile)
Install or clear the session-level profile overlay.
Proxy counters for profile A/B comparisons (not billing truth).
Suggested profile change when a call is rejected by the active profile.