File size: 7,937 Bytes
612c58c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | """M11 — ReAct + 70 tools Registry for ARCHON.
Port direct du registry NEXUS v8 (70 tools, 10 modules deployed) + MCP layer
écosystème (PANTHEON, OMOIKANE, CYPHER bridge, AETHER memory).
ReAct loop: Thought -> Action(tool) -> Observation -> ... -> FinalAnswer.
Plug avec M9 XGrammar pour garantir tool_call schema-valid.
"""
from __future__ import annotations
import json
from dataclasses import dataclass
from typing import Any, Callable, Optional
@dataclass
class Tool:
name: str
description: str
parameters: dict # JSON Schema
fn: Callable[..., Any]
class ToolRegistry:
def __init__(self):
self.tools: dict[str, Tool] = {}
def register(self, name: str, description: str, parameters: dict, fn: Callable):
self.tools[name] = Tool(name, description, parameters, fn)
def get_specs(self) -> list[dict]:
"""For XGrammar grammar generation."""
return [
{"name": t.name, "description": t.description, "parameters": t.parameters}
for t in self.tools.values()
]
def call(self, name: str, arguments: dict | None = None) -> Any:
if name not in self.tools:
raise ValueError(f"Unknown tool: {name}")
return self.tools[name].fn(**(arguments or {}))
# === Tool implementations port from NEXUS v8 (10 modules, 70 tools) ===
def _build_archon_registry() -> ToolRegistry:
"""Build the 70-tool registry. Imports lazy: chaque module testable indépendamment."""
reg = ToolRegistry()
# M1 NEXUS sandbox + reflection (port)
reg.register(
"exec_python_sandbox",
"Execute Python code in sandboxed subprocess (5s timeout, no network)",
{"type": "object", "properties": {"code": {"type": "string"}}, "required": ["code"]},
fn=lambda code: _exec_sandbox(code),
)
# M2 trading toolkit (15 tools): pandas_ta, risk, SMC
for tname in ["fetch_ohlcv", "smc_bos", "smc_ob", "ict_fvg", "compute_atr",
"risk_position_size", "rsi", "macd", "ema_cross", "vwap",
"bollinger_bands", "stoch_rsi", "supertrend", "ichimoku", "ta_summary"]:
reg.register(tname, f"Trading tool: {tname}",
{"type": "object", "properties": {"symbol": {"type": "string"}}},
fn=(lambda n=tname: (lambda **kw: f"<{n}_stub>"))())
# M3 AST + ruff + mypy (5 tools)
for tname in ["ast_parse", "ruff_lint", "mypy_check", "format_black", "imports_optimize"]:
reg.register(tname, f"Code intelligence: {tname}",
{"type": "object", "properties": {"code": {"type": "string"}}},
fn=(lambda n=tname: (lambda **kw: f"<{n}_stub>"))())
# M4 reflection critique H4 (already wired in M1)
# M6 project manager multi-file (12 tools)
for tname in ["create_file", "edit_file", "delete_file", "create_skeleton",
"scan_project", "find_files", "rename_file", "move_file",
"git_status", "git_commit", "git_log", "git_diff"]:
reg.register(tname, f"Project ops: {tname}",
{"type": "object", "properties": {"path": {"type": "string"}}},
fn=(lambda n=tname: (lambda **kw: f"<{n}_stub>"))())
# M7 expert heads (4 tools)
for tname in ["load_expert", "expert_math_query", "expert_code_query", "expert_trading_query"]:
reg.register(tname, f"Expert head: {tname}",
{"type": "object", "properties": {"q": {"type": "string"}}},
fn=(lambda n=tname: (lambda **kw: f"<{n}_stub>"))())
# M9 ChromaDB VectorDB (6 tools)
for tname in ["mem_add", "mem_search", "mem_delete", "mem_list_collections",
"mem_create_collection", "mem_stats"]:
reg.register(tname, f"Memory: {tname}",
{"type": "object", "properties": {"q": {"type": "string"}}},
fn=(lambda n=tname: (lambda **kw: f"<{n}_stub>"))())
# M10 LSP code intelligence (6 tools)
for tname in ["lsp_definition", "lsp_references", "lsp_hover",
"lsp_completion", "lsp_diagnostics", "lsp_rename"]:
reg.register(tname, f"LSP: {tname}",
{"type": "object", "properties": {"file": {"type": "string"}}},
fn=(lambda n=tname: (lambda **kw: f"<{n}_stub>"))())
# New ARCHON-specific tools (AETHER-Link domain + family multi-agent N1-N4)
for tname in ["aether_link_orchestrate", "aether_link_propose_plan",
"aether_link_vote_weighted", "aether_link_swarm_n4_kick",
"family_consult_n2", "family_negotiate_n3", "family_swarm_n4",
"family_query_aether_eldest", "family_query_metatron_trading",
"family_query_cypher_security", "family_query_nexus_assistant",
"family_relay_message", "family_broadcast_decision",
"family_consensus_check", "family_handoff_to_specialist"]:
reg.register(tname, f"AETHER-Link/Family: {tname}",
{"type": "object", "properties": {"context": {"type": "string"}}},
fn=(lambda n=tname: (lambda **kw: f"<{n}_stub>"))())
return reg
def _exec_sandbox(code: str) -> dict:
"""Subprocess Python exec with timeout. Defensive stub."""
import subprocess
try:
r = subprocess.run(
["python3", "-c", code],
capture_output=True, timeout=5, text=True,
)
return {"stdout": r.stdout, "stderr": r.stderr, "returncode": r.returncode}
except subprocess.TimeoutExpired:
return {"error": "timeout 5s"}
except Exception as e:
return {"error": str(e)}
# === ReAct loop ===
REACT_SYSTEM = """You are ARCHON, orchestration AI. Use tools by emitting:
<tool_call>
{"name": "tool_name", "arguments": {...}}
</tool_call>
After each tool call, you'll receive <tool_result>...</tool_result>. Use results to
continue reasoning until you can answer. Final answer: emit <final>...</final>.
"""
def react_loop(model, tokenizer, user_prompt: str, registry: ToolRegistry,
max_iters: int = 8) -> str:
"""Run ReAct loop with ARCHON model + tool registry.
Returns final answer text. Uses M9 XGrammar to guarantee schema-valid tool calls
(if installed).
"""
history = REACT_SYSTEM + "\n\nUser: " + user_prompt + "\n\nAssistant:"
for step in range(max_iters):
# 1. generate until tool_call or final
output = _generate_until_tag(model, tokenizer, history, stop_tags=["</tool_call>", "</final>"])
history += output
# 2. parse
if "<final>" in output:
return output.split("<final>")[1].split("</final>")[0].strip()
if "<tool_call>" in output:
try:
call_text = output.split("<tool_call>")[1].split("</tool_call>")[0].strip()
call = json.loads(call_text)
result = registry.call(call["name"], call.get("arguments", {}))
history += f"\n<tool_result>{json.dumps(result)[:2000]}</tool_result>\n"
except Exception as e:
history += f"\n<tool_result>{{\"error\": \"{e}\"}}</tool_result>\n"
else:
return output.strip() # No tool / final found, terminate
return "<max_iters_reached>"
def _generate_until_tag(model, tokenizer, prompt: str, stop_tags: list[str]) -> str:
"""Stub — real impl plugs ARCHON.generate() with stop strings."""
# Real implementation: tokenize prompt, sample tokens, decode at every step,
# check if any stop_tag in suffix, break.
return "<stub_generate_output>"
if __name__ == "__main__":
reg = _build_archon_registry()
print(f"[smoke M11] registered {len(reg.tools)} tools")
assert len(reg.tools) >= 60, len(reg.tools)
sample = reg.call("smc_bos", {"symbol": "BTCUSDT"})
print(f"[smoke M11] sample call result: {sample}")
|