Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/ghidra_deep_agent/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
MONGODB_VECTOR_COLLECTION collection name (default ``re_knowledge``)
"""

import ast
import os
import re
import sys
from collections import Counter
from collections.abc import Mapping
from datetime import UTC, datetime
from typing import Any
from typing import Annotated, Any

from langchain_core.callbacks import Callbacks
from langchain_core.documents import Document
Expand All @@ -29,6 +30,7 @@
from langchain_core.tools.retriever import create_retriever_tool
from langchain_mongodb import MongoDBAtlasVectorSearch
from langchain_mongodb.embeddings import AutoEmbeddings
from pydantic import BeforeValidator
from pymongo.errors import PyMongoError

from ghidra_deep_agent.mongo_util import get_mongo_client, mongo_write_with_retry
Expand Down Expand Up @@ -57,6 +59,30 @@
SUMMARY_SCAN_CAP = 1000


def _coerce_tags(value: Any) -> Any:
"""Accept tags sent as one string, e.g. ``"['crypto', 'loop']"`` or ``"a, b"``.

GLM sometimes serializes the list into a string; strict validation then
rejected the whole call and the agent had to resend it.
"""
if not isinstance(value, str):
return value
text = value.strip()
if text.startswith("["):
try:
parsed = ast.literal_eval(text)
except (ValueError, SyntaxError):
parsed = None
if isinstance(parsed, list):
return [str(t) for t in parsed]
return [t.strip().strip("'\"") for t in text.strip("[]").split(",") if t.strip()]


# A tool's ``tags`` argument: a list of strings, or one string the model
# serialized it into (see ``_coerce_tags``).
Tags = Annotated[list[str], BeforeValidator(_coerce_tags)]


# Fields every listing tool renders from. Kept as one projection because the
# tools differ in how they *format* a finding, not in which fields they need —
# four near-identical copies of this dict is how they drifted before.
Expand Down Expand Up @@ -321,7 +347,7 @@ def save_knowledge(
address: str = "",
function_name: str = "",
confidence: str = "medium",
tags: list[str] | None = None,
tags: Tags | None = None,
) -> str:
"""Save a NEW reverse engineering finding to the long-term knowledge base.

Expand Down Expand Up @@ -368,7 +394,7 @@ def update_knowledge(
address: str,
function_name: str | None = None,
confidence: str | None = None,
tags: list[str] | None = None,
tags: Tags | None = None,
) -> str:
"""Update metadata on EXISTING knowledge entries for a given address.

Expand Down Expand Up @@ -414,7 +440,7 @@ def update_knowledge(
)

@tool
def query_by_address(address: str, tags: list[str] | None = None) -> str:
def query_by_address(address: str, tags: Tags | None = None) -> str:
"""Retrieve all stored findings for a given address or address prefix.

Use this before working on a specific function or data location to surface
Expand Down Expand Up @@ -443,7 +469,7 @@ def query_by_address(address: str, tags: list[str] | None = None) -> str:
return f"{len(docs)} finding(s):\n" + "\n".join(lines) + _cap_note(docs)

@tool
def query_by_category(category: str, tags: list[str] | None = None) -> str:
def query_by_category(category: str, tags: Tags | None = None) -> str:
"""Retrieve all stored findings of a given category.

Useful for reviewing every known function, every hypothesis, or every rename
Expand Down Expand Up @@ -500,7 +526,7 @@ def get_knowledge_summary() -> str:
return _render_knowledge_summary(docs, binary_name, total)

@tool
def list_all_knowledge(tags: list[str] | None = None) -> str:
def list_all_knowledge(tags: Tags | None = None) -> str:
"""List a summary of every entry in the knowledge base for the current binary.

Call this at the start of a session to orient yourself — see which
Expand Down Expand Up @@ -540,7 +566,7 @@ def list_all_knowledge(tags: list[str] | None = None) -> str:
)

@tool
def query_by_tags(tags: list[str]) -> str:
def query_by_tags(tags: Tags) -> str:
"""Retrieve all stored findings that match any of the given tags.

Use this to surface everything related to a theme across the whole binary,
Expand Down
12 changes: 9 additions & 3 deletions src/ghidra_deep_agent/tui/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,19 @@ def handle_event(
)
app.post_message(ToolCountChanged(1))

elif kind == "on_tool_end":
# A tool that raises (e.g. its arguments fail validation) gets
# on_tool_error *instead of* on_tool_end; without closing it here its node
# spun "in progress" forever and the active-tool count never came back down.
elif kind in ("on_tool_end", "on_tool_error"):
run.active_tool_runs.discard(run_id)
if run_id in run.hidden_tool_runs:
run.hidden_tool_runs.discard(run_id)
return
output = event.get("data", {}).get("output")
error = bool(event.get("data", {}).get("error"))
data = event.get("data", {})
if kind == "on_tool_error":
output, error = data.get("error"), True
else:
output, error = data.get("output"), bool(data.get("error"))
meta = run.subagent_meta.pop(run_id, None)
if meta is not None:
# `task` is a local tool that can never return an async submission
Expand Down
27 changes: 27 additions & 0 deletions tests/test_knowledge_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import asyncio
import re

import pytest
from langchain_core.callbacks import CallbackManagerForRetrieverRun
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever
from langchain_core.tools import tool
from pymongo.errors import OperationFailure

from ghidra_deep_agent.knowledge import (
QUERY_RESULT_CAP,
Tags,
_address_prefix_filter,
_cap_note,
_safe_retriever_tool,
Expand Down Expand Up @@ -100,3 +103,27 @@ def test_retriever_tool_reports_mongo_errors() -> None:
):
assert result.startswith("Warning: semantic search is unavailable")
assert "NOT_STARTED" in result


# ── tags coercion ─────────────────────────────────────────────────────────────


@tool
def _tagged(tags: Tags | None = None) -> str:
"""Echo the validated tags."""
return repr(tags)


@pytest.mark.parametrize(
("sent", "expected"),
[
("['state-block', 'cff']", ["state-block", "cff"]),
('["vgtrace"]', ["vgtrace"]),
("crypto, loop", ["crypto", "loop"]),
(["already", "a-list"], ["already", "a-list"]),
(None, None),
],
)
def test_tags_sent_as_a_string_become_a_list(sent: object, expected: object) -> None:
"""GLM sometimes serializes the list; strict validation rejected the call."""
assert _tagged.invoke({"tags": sent}) == repr(expected)
45 changes: 45 additions & 0 deletions tests/test_tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,51 @@ async def run() -> None:
asyncio.run(run())


def test_tool_error_closes_the_node() -> None:
"""A tool that raises gets on_tool_error instead of on_tool_end; its node
must still close (as failed) and the active-tool count come back down."""

async def run() -> None:
app = _make_app()
async with app.run_test() as pilot:
activity = app.query_one(ActivityTree)
response = app.query_one(ResponseLog)
thinking = app.query_one(ThinkingPanel)

def emit(event: dict[str, Any]) -> None:
handle_event(app, event, activity, response, thinking)

emit(
{
"event": "on_tool_start",
"run_id": "save",
"name": "save_knowledge",
"metadata": {},
"parent_ids": [],
"data": {"input": {"content": "x"}},
}
)
await pilot.pause()
assert app.query_one(StatusBar).active_tools == 1

emit(
{
"event": "on_tool_error",
"run_id": "save",
"metadata": {},
"data": {"error": ValueError("tags: Input should be a valid list")},
}
)
await pilot.pause()
assert app.run_state.active_tool_runs == set()
assert app.query_one(StatusBar).active_tools == 0
node = activity._run_map["save"][0]
assert "✗" in str(node.label)
assert "valid list" in str(node.children[0].label)

asyncio.run(run())


def test_nested_tool_calls_are_hidden() -> None:
"""A tool invoked from inside another tool's body (recover_prototypes →
scripts) is suppressed entirely — no tree row, no deferred-async leak."""
Expand Down