MCPcopy Create free account
hub / github.com/Ishabdullah/Codey-v2 / execute_tool

Function execute_tool

core/agent.py:258–354  ·  view source on GitHub ↗

Execute a tool call with learning integration. Learns from: - File operations (for preference learning) - Errors (for error database) - Strategy effectiveness (for adaptive recovery)

(tool_dict)

Source from the content-addressed store, hash-verified

256
257
258def execute_tool(tool_dict):
259 """
260 Execute a tool call with learning integration.
261
262 Learns from:
263 - File operations (for preference learning)
264 - Errors (for error database)
265 - Strategy effectiveness (for adaptive recovery)
266 """
267 name = tool_dict.get("name", "")
268 args = tool_dict.get("args", {})
269 learning = _get_learning()
270
271 if name not in TOOLS:
272 return "[ERROR] Unknown tool: " + name
273
274 start_time = time.time()
275
276 try:
277 # For write_file: read old content for diff display BEFORE the write,
278 # show the display panel immediately after, then release old_content.
279 # This avoids holding both old and new content for the entire function.
280 _is_write = name == "write_file"
281 _is_patch = name == "patch_file"
282 old_content = None
283 if _is_write:
284 from pathlib import Path as _P
285 p = _P(args.get("path", ""))
286 if p.exists():
287 try: old_content = p.read_text()
288 except: pass
289
290 result = TOOLS[name](args)
291 duration = time.time() - start_time
292
293 # Display IMMEDIATELY after write — then release old_content so GC
294 # can reclaim it before linting/learning/memory-loading pile on.
295 try:
296 if _is_write:
297 show_file_write(args.get("path",""), args.get("content",""), old_content)
298 del old_content # release ~10-50KB before next steps
299 elif _is_patch:
300 show_patch(args.get("path",""), args.get("old_str",""), args.get("new_str",""))
301 elif name == "shell":
302 is_err = is_error(result, "shell")
303 show_shell(args.get("command",""), result, error=is_err)
304 elif name != "read_file":
305 show_tool_generic(name, args, result)
306 except Exception:
307 pass # display failure must not mask a successful tool result
308 old_content = None # ensure released even if display was skipped
309
310 # ── Post-write lint (replaces pre-write syntax check + post-write lint)
311 # Single pass — the linter reads from disk (no extra content copy).
312 if (_is_write or _is_patch) and not result.startswith("[ERROR]"):
313 _lpath = args.get("path", "")
314 if _lpath.endswith(".py"):
315 try:

Callers 1

run_agentFunction · 0.85

Calls 12

show_file_writeFunction · 0.90
show_patchFunction · 0.90
show_shellFunction · 0.90
show_tool_genericFunction · 0.90
run_linterFunction · 0.90
format_issuesFunction · 0.90
_get_learningFunction · 0.85
is_errorFunction · 0.85
existsMethod · 0.80
getMethod · 0.45
log_actionMethod · 0.45
record_errorMethod · 0.45

Tested by

no test coverage detected