diff --git a/docs/doctor-overview.md b/docs/doctor-overview.md index 498b4a3..bfd73e9 100644 --- a/docs/doctor-overview.md +++ b/docs/doctor-overview.md @@ -125,6 +125,8 @@ exists. Subsequent Holmes actions retain the permissions described above. If a final response only copies or summarizes a Kubernetes resource after a tool call, without a full diagnosis, mitigation and validation, the adapter asks once for the next read-only PVC or PV check using names in the evidence. +It handles a plain-text request for an unavailable tool the same way, and asks +for the PV after an already-completed PVC check. Only a schema-valid, single `kubectl get` call is forwarded, and a named PVC or PV must match the corresponding name in the collected mapping. If the model immediately repeats an identical Bash command, the adapter asks it diff --git a/scripts/ollama-tool-proxy.py b/scripts/ollama-tool-proxy.py index 725b0e5..4bdb2ea 100644 --- a/scripts/ollama-tool-proxy.py +++ b/scripts/ollama-tool-proxy.py @@ -110,6 +110,8 @@ def is_resource_extract(message): for name in ("diagnosis", "mitigation", "validation"))) if not isinstance(data, dict): return False + if set(data) == {"name", "arguments"} and isinstance(data.get("name"), str) and isinstance(data.get("arguments"), dict): + return True report_keys = {"diagnosis", "root_cause", "mitigation", "validation", "observed_facts"} resource_keys = {"metadata", "apiVersion", "kind", "containerPort", "volumeMounts", "image", "status"} if report_keys.intersection(data): @@ -120,6 +122,20 @@ def is_resource_extract(message): return bool(resource_keys.intersection(data)) +def last_bash_command(messages): + for message in reversed(messages): + if not isinstance(message, dict) or message.get("role") != "assistant": + continue + for call in message.get("tool_calls") or []: + function = call.get("function") or {} + if function.get("name") == "bash": + try: + return json.loads(function.get("arguments", "")).get("command", "") + except (ValueError, TypeError, AttributeError): + return "" + return "" + + def complete(request, bash_mode): """Retry an initial prose-only answer once; never fabricate a tool call.""" result = translate(query_ollama(request), request.get("tools", [])) @@ -152,9 +168,13 @@ def complete(request, bash_mode): users = [m for m in messages if isinstance(m, dict) and m.get("role") == "user" and isinstance(m.get("content"), str)] if users: retry = dict(request) + previous = last_bash_command(messages) + next_check = ("The PVC was already inspected. Check the bound PV by its actual PV name; use kubectl get pv, not get pvc. " + if re.search(r"\bget\s+(?:pvc|persistentvolumeclaim)\b", previous) else + "Check the named PVC or PV using kubectl get. ") retry["messages"] = [{"role": "system", "content": "Use the bash tool to perform one read-only Kubernetes check using real resource names."}, users[-1], - {"role": "user", "content": "Your last answer only copied a Kubernetes resource; investigation is not finished. " - "Use the actual pod, PVC and PV names in the provided context. Call bash for a single targeted kubectl get " + {"role": "user", "content": "Your last answer was not a completed investigation or a valid tool call. " + "Use the actual pod, PVC and PV names in the provided context. " + next_check + "Call bash for one targeted kubectl get " "check. In an evidence line 'pvc CLAIM phase=... pv=VOLUME', get pvc must use CLAIM; get pv must use VOLUME. " "Do not use placeholders, pipes, shell operators, or mutate resources. " "Return only a JSON tool call with command and suggested_prefixes."}] diff --git a/tests/jeannie/test_ollama_tool_call_adapter.py b/tests/jeannie/test_ollama_tool_call_adapter.py index 357b78a..cc62d5a 100644 --- a/tests/jeannie/test_ollama_tool_call_adapter.py +++ b/tests/jeannie/test_ollama_tool_call_adapter.py @@ -84,6 +84,11 @@ class ToolAdapterTests(unittest.TestCase): self.assertFalse(PROXY.is_resource_extract({"content": '{"response":"Diagnosis: disk. Mitigation: none. Validation: failed."}'})) self.assertTrue(PROXY.is_resource_extract({"content": "The pod is running an application and has a container image. " * 3})) self.assertFalse(PROXY.is_resource_extract({"content": "Diagnosis: bad pod. Mitigation: none. Validation: unavailable."})) + self.assertTrue(PROXY.is_resource_extract({"content": '```json\n{"name":"fetch_pvc_details","arguments":{"pvc_name":"n8n-data"}}\n```'})) + + def test_last_bash_command_reads_recorded_tool_arguments(self): + messages = [{"role": "assistant", "tool_calls": [{"function": {"name": "bash", "arguments": json.dumps({"command": "kubectl -n n8n get pvc n8n-data"})}}]}, {"role": "tool", "content": "ok"}] + self.assertEqual(PROXY.last_bash_command(messages), "kubectl -n n8n get pvc n8n-data") def test_retry_cannot_turn_a_plan_into_a_mutating_command(self): tools = [{"type": "function", "function": {"name": "bash", "parameters": {