Browse Source

feat(A08-A10): Tool registry, terminal UI, integration tests

- A08: BUILTIN_TOOLS registry — 12 tools auto-resolved in from_config localTools
- A09: TerminalUI — rich-based streaming display (tokens, tools, trace)
- A10: E2E tests — Read→Edit→Verify, Write→List→Search, Bash+Git workflow
- from_config compiler now resolves built-in tool names (ReadFile, Bash, etc.)
- 45 tests all passing

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
kenny67nju 7 tháng trước cách đây
mục cha
commit
b6e177e5db

+ 78 - 0
lambdagent/builtin_tools/registry.py

@@ -0,0 +1,78 @@
+"""
+lambdagent.builtin_tools.registry — Built-in tool registry
+
+Provides BUILTIN_TOOLS dict mapping tool names to (fn, schema) pairs.
+Used by from_config compiler to resolve localTools references.
+"""
+from __future__ import annotations
+
+from lambdagent.primitives import Tool
+from lambdagent.validated_tool import ValidatedTool
+
+from .file_tools import (
+    read_file, ReadFileSchema,
+    edit_file, EditFileSchema,
+    write_file, WriteFileSchema,
+    list_files, ListFilesSchema,
+    search_content, SearchContentSchema,
+)
+from .shell_tools import (
+    run_bash, BashSchema,
+    git_status, git_diff, git_log, git_commit, git_branch,
+)
+
+
+def _make_tool(name: str, fn, schema=None, description: str = "") -> ValidatedTool:
+    """Create a ValidatedTool with optional schema."""
+    return ValidatedTool(name, fn, schema=schema, description=description)
+
+
+# ════════════════════════════════════════════════════════════
+# Master registry
+# ════════════════════════════════════════════════════════════
+
+BUILTIN_TOOLS = {
+    # File operations (A01-A05)
+    "ReadFile":       _make_tool("ReadFile",       read_file,       ReadFileSchema,    "Read file contents with line numbers"),
+    "EditFile":       _make_tool("EditFile",       edit_file,       EditFileSchema,    "Precise string replacement in files"),
+    "WriteFile":      _make_tool("WriteFile",      write_file,      WriteFileSchema,   "Create or overwrite files"),
+    "ListFiles":      _make_tool("ListFiles",      list_files,      ListFilesSchema,   "Find files by glob pattern"),
+    "SearchContent":  _make_tool("SearchContent",  search_content,  SearchContentSchema, "Search file contents with regex"),
+
+    # Shell execution (A06)
+    "Bash":           _make_tool("Bash",           run_bash,        BashSchema,        "Execute shell commands"),
+
+    # Git operations (A07)
+    "GitStatus":      _make_tool("GitStatus",      git_status,      None, "Show git status"),
+    "GitDiff":        _make_tool("GitDiff",        git_diff,        None, "Show git diff"),
+    "GitLog":         _make_tool("GitLog",         git_log,         None, "Show git log"),
+    "GitCommit":      _make_tool("GitCommit",      git_commit,      None, "Stage and commit changes"),
+    "GitBranch":      _make_tool("GitBranch",      git_branch,      None, "List/create/switch branches"),
+
+    # Base case (always available)
+    "terminate":      Tool("terminate", fn=lambda x: x),
+}
+
+
+def get_builtin_tool(name: str) -> Tool | None:
+    """Look up a built-in tool by name. Returns None if not found."""
+    return BUILTIN_TOOLS.get(name)
+
+
+def list_builtin_tools() -> list[str]:
+    """Return sorted list of all built-in tool names."""
+    return sorted(BUILTIN_TOOLS.keys())
+
+
+def resolve_tools(names: list[str]) -> dict[str, Tool]:
+    """Resolve a list of tool names to Tool instances.
+
+    Returns only tools that exist in the registry.
+    Unknown names are silently skipped.
+    """
+    result = {}
+    for name in names:
+        tool = BUILTIN_TOOLS.get(name)
+        if tool:
+            result[name] = tool
+    return result

+ 162 - 0
lambdagent/builtin_tools/terminal_ui.py

@@ -0,0 +1,162 @@
+"""
+lambdagent.builtin_tools.terminal_ui — Streaming terminal UI
+
+Renders ReAct execution with:
+  - Token-by-token LLM streaming
+  - Tool call display (collapsed/expanded)
+  - Spinner during execution
+  - Colored β-reduction trace
+"""
+from __future__ import annotations
+
+import sys
+import time
+from typing import Any
+
+
+def _has_rich() -> bool:
+    try:
+        from rich.console import Console
+        return True
+    except ImportError:
+        return False
+
+
+class TerminalUI:
+    """Streaming terminal UI for agent execution."""
+
+    def __init__(self, verbose: bool = False, color: bool = True):
+        self.verbose = verbose
+        self.color = color
+        self._use_rich = color and _has_rich()
+        self._console = None
+        self._step_count = 0
+        self._total_tokens = 0
+        self._start_time = 0
+
+        if self._use_rich:
+            from rich.console import Console
+            self._console = Console()
+
+    def start(self, agent_name: str, agent_type: str, config_path: str = ""):
+        """Display agent start info."""
+        self._start_time = time.time()
+        if self._use_rich:
+            self._console.print(f"[bold cyan]lambdagent[/] [dim]v2.0[/]")
+            self._console.print(f"  Agent: [bold]{agent_name}[/] ({agent_type})")
+            if config_path:
+                self._console.print(f"  Config: [dim]{config_path}[/]")
+            self._console.print()
+        else:
+            print(f"lambdagent v2.0")
+            print(f"  Agent: {agent_name} ({agent_type})")
+            if config_path:
+                print(f"  Config: {config_path}")
+            print()
+
+    def token(self, text: str, term_name: str = ""):
+        """Stream a single token (incremental output)."""
+        sys.stdout.write(text)
+        sys.stdout.flush()
+
+    def newline(self):
+        print()
+
+    def tool_start(self, tool_name: str, tool_input: str):
+        """Display tool call start."""
+        self._step_count += 1
+        input_preview = tool_input[:80].replace("\n", " ")
+        if self._use_rich:
+            self._console.print(
+                f"  [dim]β[{self._step_count}][/] [yellow]{tool_name}[/] "
+                f"[dim]{input_preview}...[/]",
+                end=""
+            )
+        else:
+            print(f"  β[{self._step_count}] {tool_name}: {input_preview}...", end="")
+        sys.stdout.flush()
+
+    def tool_end(self, tool_name: str, result: str, duration_ms: float):
+        """Display tool call result."""
+        result_preview = result[:60].replace("\n", " ")
+        if self._use_rich:
+            self._console.print(f" [green]({duration_ms:.0f}ms)[/] {result_preview}")
+        else:
+            print(f" ({duration_ms:.0f}ms) {result_preview}")
+
+    def tool_error(self, tool_name: str, error: str):
+        """Display tool call error."""
+        if self._use_rich:
+            self._console.print(f" [red]ERROR: {error[:80]}[/]")
+        else:
+            print(f" ERROR: {error[:80]}")
+
+    def thinking(self, step: int):
+        """Display thinking indicator."""
+        if self._use_rich:
+            self._console.print(f"  [dim]β[{step}] thinking...[/]", end="\r")
+        else:
+            print(f"  β[{step}] thinking...", end="\r")
+        sys.stdout.flush()
+
+    def step_complete(self, step: int, term_name: str, duration_ms: float, tokens: int = 0):
+        """Display step completion."""
+        self._total_tokens += tokens
+        if self.verbose:
+            if self._use_rich:
+                self._console.print(
+                    f"  [dim]β[{step}][/] [blue]{term_name}[/] "
+                    f"[dim]{duration_ms:.0f}ms, {tokens} tokens[/]"
+                )
+            else:
+                print(f"  β[{step}] {term_name} ({duration_ms:.0f}ms, {tokens} tokens)")
+
+    def result(self, output: str, trace_count: int):
+        """Display final result."""
+        elapsed = time.time() - self._start_time
+        if self._use_rich:
+            self._console.print()
+            self._console.print(output)
+            self._console.print()
+            self._console.print(
+                f"[dim]({trace_count} β-reductions, {elapsed:.1f}s, "
+                f"~{self._total_tokens} tokens)[/]"
+            )
+        else:
+            print()
+            print(output)
+            print()
+            print(f"({trace_count} β-reductions, {elapsed:.1f}s, ~{self._total_tokens} tokens)")
+
+    def error(self, message: str):
+        """Display error."""
+        if self._use_rich:
+            self._console.print(f"[bold red]ERROR:[/] {message}")
+        else:
+            print(f"ERROR: {message}", file=sys.stderr)
+
+    def trace(self, entries: list):
+        """Display full β-reduction trace."""
+        if self._use_rich:
+            self._console.print(f"\n[bold]β-reduction trace:[/]")
+            colors = {"Lam": "blue", "Tool": "yellow", "Compose": "cyan",
+                      "Loop": "magenta", "Route": "green"}
+            for i, e in enumerate(entries):
+                term = getattr(e, "term_name", str(e.get("term", "?")))
+                ms = getattr(e, "duration_ms", e.get("duration_ms", 0))
+                ttype = getattr(e, "term_type", "") if hasattr(e, "term_type") else ""
+                color = colors.get(ttype, "white")
+                inp = str(getattr(e, "input", e.get("input", "")))[:50]
+                out = str(getattr(e, "output", e.get("output", "")))[:50]
+                self._console.print(
+                    f"  β[{i}] [{color}]{term}[/] [dim]({ms:.0f}ms)[/]: "
+                    f"{inp} → {out}"
+                )
+        else:
+            print("\nβ-reduction trace:")
+            for i, e in enumerate(entries):
+                term = getattr(e, "term_name", str(e.get("term", "?")))
+                ms = getattr(e, "duration_ms", e.get("duration_ms", 0))
+                inp = str(getattr(e, "input", e.get("input", "")))[:50]
+                out = str(getattr(e, "output", e.get("output", "")))[:50]
+                print(f"  β[{i}] {term} ({ms:.0f}ms): {inp} → {out}")

+ 8 - 5
lambdagent/fromconfig/compiler.py

@@ -563,15 +563,18 @@ def _compile_tools(cfg: Dict, overrides: Dict) -> Dict[str, Tool]:
                 fn = _compile_mcp_caller(server_name, tool_name, cfg)
                 fn = _compile_mcp_caller(server_name, tool_name, cfg)
             tools[tool_name] = Tool(name=tool_name, fn=fn)
             tools[tool_name] = Tool(name=tool_name, fn=fn)
 
 
-    # Local tools
+    # Local tools — resolve built-in tools first, then custom, then placeholder
+    from lambdagent.builtin_tools.registry import get_builtin_tool
     local = mcp_cfg.get("localTools", [])
     local = mcp_cfg.get("localTools", [])
     for tool_name in local:
     for tool_name in local:
-        if tool_name == "terminate":
-            tools["terminate"] = Tool("terminate", fn=lambda x: x)
-        elif tool_name in custom_tools:
+        if tool_name in custom_tools:
             tools[tool_name] = Tool(tool_name, fn=custom_tools[tool_name])
             tools[tool_name] = Tool(tool_name, fn=custom_tools[tool_name])
         else:
         else:
-            tools[tool_name] = Tool(tool_name, fn=lambda x, tn=tool_name: f"[local:{tn}]({x})")
+            builtin = get_builtin_tool(tool_name)
+            if builtin:
+                tools[tool_name] = builtin
+            else:
+                tools[tool_name] = Tool(tool_name, fn=lambda x, tn=tool_name: f"[local:{tn}]({x})")
 
 
     # S04: Validate tool references if whitelist enforcement is enabled
     # S04: Validate tool references if whitelist enforcement is enabled
     if overrides.get("_enforce_tool_whitelist", False):
     if overrides.get("_enforce_tool_whitelist", False):

+ 137 - 0
lambdagent/tests/test_builtin_tools.py

@@ -334,5 +334,142 @@ class TestGitTools:
         assert "ERROR" in result
         assert "ERROR" in result
 
 
 
 
+# ════════════════════════════════════════════════════════════
+# A08: Tool Registry
+# ════════════════════════════════════════════════════════════
+
+class TestToolRegistry:
+    def test_all_tools_registered(self):
+        from lambdagent.builtin_tools.registry import BUILTIN_TOOLS
+        expected = {"ReadFile", "EditFile", "WriteFile", "ListFiles", "SearchContent",
+                    "Bash", "GitStatus", "GitDiff", "GitLog", "GitCommit", "GitBranch",
+                    "terminate"}
+        assert expected.issubset(set(BUILTIN_TOOLS.keys()))
+
+    def test_get_builtin_tool(self):
+        from lambdagent.builtin_tools.registry import get_builtin_tool
+        assert get_builtin_tool("ReadFile") is not None
+        assert get_builtin_tool("nonexistent") is None
+
+    def test_resolve_tools(self):
+        from lambdagent.builtin_tools.registry import resolve_tools
+        result = resolve_tools(["ReadFile", "Bash", "terminate", "fake_tool"])
+        assert "ReadFile" in result
+        assert "Bash" in result
+        assert "terminate" in result
+        assert "fake_tool" not in result
+
+    def test_from_config_integration(self):
+        """Test that from_config resolves built-in tool names."""
+        from lambdagent.builtin_tools.registry import get_builtin_tool
+        tool = get_builtin_tool("ReadFile")
+        assert tool is not None
+        # Tool should work
+        with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
+            f.write("test content\n")
+            path = f.name
+        try:
+            result = tool.apply(json.dumps({"file_path": path}))
+            assert "test content" in result
+        finally:
+            os.unlink(path)
+
+
+# ════════════════════════════════════════════════════════════
+# A09: Terminal UI
+# ════════════════════════════════════════════════════════════
+
+class TestTerminalUI:
+    def test_ui_creation(self):
+        from lambdagent.builtin_tools.terminal_ui import TerminalUI
+        ui = TerminalUI(verbose=True)
+        assert ui.verbose is True
+
+    def test_ui_start(self, capsys):
+        from lambdagent.builtin_tools.terminal_ui import TerminalUI
+        ui = TerminalUI(color=False)
+        ui.start("test-agent", "react")
+        captured = capsys.readouterr()
+        assert "test-agent" in captured.out
+
+    def test_ui_result(self, capsys):
+        from lambdagent.builtin_tools.terminal_ui import TerminalUI
+        ui = TerminalUI(color=False)
+        ui._start_time = time.time()
+        ui.result("Hello world", 3)
+        captured = capsys.readouterr()
+        assert "Hello world" in captured.out
+        assert "3 β-reductions" in captured.out
+
+    def test_ui_tool_cycle(self, capsys):
+        from lambdagent.builtin_tools.terminal_ui import TerminalUI
+        ui = TerminalUI(color=False)
+        ui.tool_start("search", "query text")
+        ui.tool_end("search", "found 5 results", 150.0)
+        captured = capsys.readouterr()
+        assert "search" in captured.out
+        assert "150ms" in captured.out
+
+
+# ════════════════════════════════════════════════════════════
+# A10: Integration — end-to-end with built-in tools
+# ════════════════════════════════════════════════════════════
+
+class TestE2EBuiltinTools:
+    def test_read_edit_verify(self):
+        """End-to-end: ReadFile → EditFile → ReadFile to verify."""
+        from lambdagent.builtin_tools.file_tools import read_file, edit_file
+        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
+            f.write("def greet():\n    return 'hello'\n")
+            path = f.name
+        try:
+            # Read
+            content = read_file({"file_path": path})
+            assert "hello" in content
+            # Edit
+            result = edit_file({"file_path": path, "old_string": "return 'hello'", "new_string": "return 'world'"})
+            assert "OK" in result
+            # Verify
+            content2 = read_file({"file_path": path})
+            assert "world" in content2
+            assert "hello" not in content2
+        finally:
+            os.unlink(path)
+
+    def test_write_list_search(self):
+        """End-to-end: WriteFile → ListFiles → SearchContent."""
+        from lambdagent.builtin_tools.file_tools import write_file, list_files, search_content
+        with tempfile.TemporaryDirectory() as d:
+            # Write
+            path = os.path.join(d, "app.py")
+            write_file({"file_path": path, "content": "def main():\n    print('hello')\n"})
+            # List
+            found = list_files({"pattern": "*.py", "path": d})
+            assert "app.py" in found
+            # Search
+            matches = search_content({"pattern": "def main", "path": d})
+            assert "main" in matches
+
+    def test_bash_git_workflow(self):
+        """End-to-end: Bash + Git in temp repo."""
+        from lambdagent.builtin_tools.shell_tools import run_bash, git_status, git_log
+        with tempfile.TemporaryDirectory() as d:
+            run_bash({"command": "git init", "working_dir": d})
+            run_bash({"command": "git config user.email 'test@test.com'", "working_dir": d})
+            run_bash({"command": "git config user.name 'Test'", "working_dir": d})
+            run_bash({"command": "echo hello > test.txt", "working_dir": d})
+            run_bash({"command": "git add . && git commit -m 'init'", "working_dir": d})
+
+            # Status should be clean
+            from lambdagent.builtin_tools.shell_tools import _set_cwd, _get_cwd
+            original_cwd = _get_cwd()
+            _set_cwd(d)
+            try:
+                status = git_status("")
+                assert "nothing to commit" in status.lower() or status.strip() == "[OK] (no output)"
+            finally:
+                _set_cwd(original_cwd)
+
+
 if __name__ == "__main__":
 if __name__ == "__main__":
     pytest.main([__file__, "-v", "--tb=short"])
     pytest.main([__file__, "-v", "--tb=short"])