载入中...
搜索中...
未找到
McpArchitectureTools.cpp
浏览该文件的文档.
2#include <Poco/JSON/Parser.h>
3#include <algorithm>
4#include <map>
5#include <queue>
6#include <set>
12
13namespace eve::dev {
14namespace {
15using Object = Poco::JSON::Object;
16using Array = Poco::JSON::Array;
17struct Node {
18 std::string id, kind, label, source, coverage;
19};
20struct Edge {
22};
23struct Graph {
24 std::map<std::string, Node> nodes;
25 std::vector<Edge> edges;
26 std::map<std::string, std::vector<size_t>> adjacency;
27 void node(const std::string& id, const std::string& kind, const std::string& label, const std::string& source = {},
28 const std::string& coverage = "known") {
29 nodes.emplace(id, Node{id, kind, label, source, coverage});
30 }
31 void edge(const std::string& from, const std::string& to, const std::string& relation, const std::string& evidence,
32 const std::string& fingerprint = {}) {
33 const auto index = edges.size();
34 edges.push_back({from, to, relation, evidence, fingerprint});
35 adjacency[from].push_back(index);
36 adjacency[to].push_back(index);
37 }
38};
39Graph buildGraph() {
41 for (const auto& contract : eve::script::generatedBindingContracts().snapshot()) {
42 const auto api = "api:" + contract.key();
43 const auto binding = "binding:" + contract.key();
44 graph.node(api, "api", contract.key(), {}, contract.architecture ? "mapped" : "unknown-owner");
45 graph.node(binding, "binding", contract.key(), contract.source);
46 graph.edge(api, binding, "binds", "static-binding");
47 if (contract.cppSymbol.empty()) continue;
48 const auto implementation = "implementation:" + contract.module + ":" + contract.cppSymbol;
49 graph.node(implementation, "implementation", contract.cppSymbol,
50 contract.architecture ? contract.architecture->implementationSource : std::string{});
51 graph.edge(binding, implementation, "calls", "static-direct-binding");
52 if (!contract.architecture) continue;
53 const auto& mapping = *contract.architecture;
54 const auto owner = "owner:" + mapping.stateOwner;
55 graph.node(owner, "owner", mapping.stateOwner, mapping.ownerSource);
56 graph.edge(implementation, owner, "state_owner", "reviewed-owner", mapping.inputHash);
57 const auto addAccess = [&](const std::vector<std::string>& fields, const std::string& access) {
58 for (const auto& field : fields) {
59 const auto state = "state:" + field;
60 graph.node(state, "state", field);
61 graph.edge(owner, state, "owns", "reviewed-owner", mapping.inputHash);
62 graph.edge(implementation, state, access, "reviewed-access", mapping.inputHash);
63 }
64 };
65 addAccess(mapping.writes, "writes");
66 addAccess(mapping.reads, "reads");
67 for (const auto& test : mapping.tests) {
68 const auto id = "test:" + test;
69 graph.node(id, "test", test, test);
70 graph.edge(implementation, id, "tested_by", "test-reference", mapping.inputHash);
71 }
72 }
73 return graph;
74}
75Object::Ptr nodeJson(const Node& node) {
76 Object::Ptr out = new Object();
77 out->set("id", node.id);
78 out->set("kind", node.kind);
79 out->set("label", node.label);
80 out->set("source", node.source);
81 out->set("coverage", node.coverage);
82 return out;
83}
84Object::Ptr query(std::string_view mode, Object::Ptr args) {
85 const auto seedArgument = args ? args->optValue<std::string>("seed", "") : std::string{};
86 const auto seed = seedArgument.find(':') == std::string::npos ? "api:" + seedArgument : seedArgument;
87 const int limit = args ? args->optValue<int>("maxNodes", 64) : 64;
88 const int depth = args ? args->optValue<int>("maxDepth", 6) : 6;
89 const int bytes = args ? args->optValue<int>("maxBytes", 16384) : 16384;
90 if (seedArgument.empty() || limit < 1 || limit > 256 || depth < 0 || depth > 8 || bytes < 1024 || bytes > 65536 ||
91 (mode == "eve_task_context" && (!args || args->optValue<std::string>("task", "").empty())))
92 return mcpFailureObject("invalid_query", "seed required; maxNodes 1..256, maxDepth 0..8, maxBytes 1024..65536");
93 // Generated contracts are immutable for this linked process. Retain owning
94 // evidence once; hot reload does not replace this build-generated registry.
95 static const Graph graph = buildGraph();
96 if (!graph.nodes.contains(seed))
97 return mcpFailureObject("unknown_seed", "Use api: followed by a binding key or a returned node ID");
98 const auto expectedHash = args->optValue<std::string>("expectedInputHash", "");
99 if (!expectedHash.empty()) {
100 const auto contract = eve::script::generatedBindingContracts().find(graph.nodes.at(seed).label);
101 if (!contract || !contract->architecture || contract->architecture->inputHash != expectedHash)
102 return mcpFailureObject("stale_evidence",
103 "Refresh the seed contract fingerprint before using this task context");
104 }
105 std::set<std::string> selected{seed};
106 std::queue<std::pair<std::string, int>> pending;
107 pending.emplace(seed, 0);
108 bool truncated = false;
109 while (!pending.empty()) {
110 const auto [current, level] = pending.front();
111 pending.pop();
112 for (const auto index : graph.adjacency.at(current)) {
113 const auto& edge = graph.edges[index];
114 const auto next = edge.from == current ? edge.to : edge.from;
115 if (selected.contains(next)) continue;
116 // Impact flows from writers to field consumers; owner/test nodes
117 // supply context but cannot fan out into unrelated state.
118 bool terminal = false;
119 if (mode != "eve_architecture_query") {
120 const auto& kind = graph.nodes.at(current).kind;
121 if (kind == "owner" || kind == "test") continue;
122 if (edge.relation == "reads" && edge.from == current) {
123 if (mode == "eve_architecture_impact") continue;
124 terminal = true;
125 }
126 }
127 if (level >= depth || selected.size() >= static_cast<size_t>(limit)) {
128 truncated = true;
129 continue;
130 }
131 selected.insert(next);
132 if (!terminal) pending.emplace(next, level + 1);
133 }
134 }
135 Object::Ptr result = new Object();
136 result->set("ok", true);
137 result->set("schema", "eve.architecture-graph");
138 result->set("schemaVersion", 1);
139 result->set("seed", seed);
140 result->set("mode", std::string(mode));
141 result->set("coverage", "reviewed-subset");
142 result->set("limitations",
143 "Candidate static impact; no runtime causality, dynamic keys or complete call graph. Test references "
144 "do not prove tests passed.");
145 Array::Ptr nodes = new Array(), edges = new Array(), tests = new Array(), unknowns = new Array(),
146 fingerprints = new Array(), contracts = new Array();
147 std::set<std::string> seenHashes;
148 for (const auto& id : selected) {
149 const auto& node = graph.nodes.at(id);
150 nodes->add(nodeJson(node));
151 if (node.kind == "api") {
153 if (contract) {
154 Poco::JSON::Parser parser;
155 contracts->add(parser.parse(contract->toJson()));
156 }
157 }
158 if (node.kind == "test") tests->add(node.label);
159 if (node.coverage == "unknown-owner") unknowns->add(id);
160 }
161 for (const auto& edge : graph.edges) {
162 if (!selected.contains(edge.from) || !selected.contains(edge.to)) continue;
163 Object::Ptr item = new Object();
164 item->set("from", edge.from);
165 item->set("to", edge.to);
166 item->set("relation", edge.relation);
167 item->set("evidence", edge.evidence);
168 item->set("inputHash", edge.fingerprint);
169 edges->add(item);
170 if (!edge.fingerprint.empty()) seenHashes.insert(edge.fingerprint);
171 }
172 for (const auto& hash : seenHashes) fingerprints->add(hash);
173 result->set("nodes", nodes);
174 result->set("edges", edges);
175 result->set("tests", tests);
176 result->set("unknowns", unknowns);
177 result->set("inputHashes", fingerprints);
178 result->set("contracts", contracts);
179 result->set("truncated", truncated);
180 if (mode == "eve_task_context") {
181 result->set("schema", "eve.task-context");
182 result->set("task", args->optValue<std::string>("task", ""));
183 result->set("consistency", "build-generated source evidence; not a live-world snapshot");
184 }
185 // Return a bounded, explicit failure rather than silently discarding evidence.
186 if (mcpStringify(result).size() > static_cast<size_t>(bytes))
187 return mcpFailureObject("context_budget_exceeded",
188 "Reduce maxNodes/maxDepth or increase maxBytes; no partial evidence was returned");
189 return result;
190}
191} // namespace
192bool isMcpArchitectureTool(std::string_view name) {
193 return name == "eve_architecture_query" || name == "eve_architecture_impact" || name == "eve_task_context";
194}
195std::string callMcpArchitectureTool(std::string_view name, Object::Ptr args) {
196 if (name == "eve_task_context" && args && args->has("projectDir")) return callMcpProjectIndexTool(name, args);
197 Object::Ptr payload;
198 try {
199 payload = isMcpArchitectureTool(name) ? query(name, args) : mcpFailureObject("unknown_tool");
200 } catch (const Poco::Exception&) {
201 payload = mcpFailureObject("invalid_query", "Argument types must match the tool schema");
202 }
203 return textContentResult(mcpStringify(payload), !payload->getValue<bool>("ok"));
204}
205std::string_view mcpArchitectureToolSchemas() {
206 static const std::string schemas = [] {
207 Array::Ptr tools = new Array();
208 for (const auto* name : {"eve_architecture_query", "eve_architecture_impact", "eve_task_context"}) {
209 Poco::JSON::Parser parser;
210 auto input =
211 parser
212 .parse(
213 R"JSON({"type":"object","required":["seed"],"properties":{"seed":{"type":"string"},"expectedInputHash":{"type":"string"},"maxNodes":{"type":"integer","minimum":1,"maximum":256},"maxDepth":{"type":"integer","minimum":0,"maximum":8},"maxBytes":{"type":"integer","minimum":1024,"maximum":65536}}})JSON")
214 .extract<Object::Ptr>();
215 if (std::string_view(name) == "eve_task_context") {
216 input->getArray("required")->add("task");
217 Object::Ptr task = new Object();
218 task->set("type", "string");
219 input->getObject("properties")->set("task", task);
220 for (const auto* field : {"projectDir", "expectedRevision"}) {
221 Object::Ptr value = new Object();
222 value->set("type", "string");
223 input->getObject("properties")->set(field, value);
224 }
225 Object::Ptr includeCalls = new Object();
226 includeCalls->set("type", "boolean");
227 input->getObject("properties")->set("includeCalls", includeCalls);
228 }
229 Object::Ptr tool = new Object();
230 tool->set("name", std::string(name));
231 tool->set("inputSchema", input);
232 tool->set("description",
233 "Read-only bounded architecture evidence: query graph, candidate state-access impact, or task "
234 "context. Coverage is partial; no runtime causality. Stable API seed: api:module/Class.method.");
235 tools->add(tool);
236 }
237 const auto text = mcpStringify(tools);
238 return text.substr(1, text.size() - 2);
239 }();
240 return schemas;
241}
242} // namespace eve::dev
243
244namespace eve::dev {
246} // namespace eve::dev
Value::Object payload
std::string selected
std::vector< QuestEvent > pending
std::string name
Definition ECS.cpp:154
std::vector< std::string > fields
Definition ECS.cpp:42
std::vector< CompiledTest > tests
EnvironmentMetricContract contract
EvpackChunkInput input
Definition Evpack.cpp:170
std::string to
std::string label
std::string fingerprint
std::string relation
std::vector< Edge > edges
std::string coverage
std::string evidence
std::map< std::string, std::vector< size_t > > adjacency
std::map< std::string, Node > nodes
std::string from
std::string id
Poco::JSON::Object::Ptr result
Snapshot snapshot
std::uint64_t bytes
#define EVE_MCP_TOOL_FAMILY(Id, Schemas, Handler, Payload)
Declare one family beside its implementation; CMake derives composition from compiled sources.
int level
std::shared_ptr< OceanGpuSpectrum > owner
std::map< std::string, std::vector< std::string > > graph
Definition Package.cpp:173
eve::action::ActionVfxBinding binding
std::string script
Definition PlayHost.cpp:117
Object::Ptr value
std::string hash
const RoadNode * node
std::string text
ScriptStructure out
std::string_view source
std::string kind
double current
std::string string
int limit
Definition TreeMesh.cpp:166
uint32_t seed
Definition TreeMesh.cpp:276
float size
Definition TreeMesh.cpp:158
std::uint32_t depth
std::size_t at
uint32_t index
const BindingContract * find(std::string_view key) const noexcept
Finds a binding contract.
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
I * query()
Queries .
Definition Capability.h:88
std::string callMcpProjectIndexTool(std::string_view name, Object::Ptr args)
std::string mcpStringify(const Poco::Dynamic::Var &value)
Compact single-line JSON text for a newline-framed MCP payload.
Definition McpJson.hpp:24
std::string textContentResult(const std::string &text, bool isError=false)
MCP tools/call result object carrying one text content item.
Definition McpJson.hpp:84
std::string_view mcpArchitectureToolSchemas()
Comma-free schema objects for the read-only architecture tools. @ownership Borrowed view of process-o...
std::string callMcpArchitectureTool(std::string_view name, Object::Ptr args)
Poco::JSON::Object::Ptr mcpFailureObject(const std::string &error, const std::string &hint={}, bool retryable=false, const std::vector< std::string > &nextTools={})
Structured tool failure object for agent self-correction.
Definition McpJson.hpp:63
bool isMcpArchitectureTool(std::string_view name)
Read-only architecture queries derived from the build's binding registry.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
ForceVector add(ForceVector a, ForceVector b)
Definition ForceMath.h:13
const BindingContractRegistry & generatedBindingContracts()
Process-wide generated Binding Contracts for this linked engine build.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Definition Runtime.h:120