my-homelab-configs/scripts/doctor-holmes-report.py

81 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""Render recorded Holmes tool results without treating its narrative as proof."""
import json
from pathlib import Path
import sys
def tool_evidence(directory):
calls, results = {}, []
for name in ('conversation.json', 'retry-conversation.json'):
path = directory / name
if not path.is_file():
continue
try:
data = json.loads(path.read_text())
except ValueError:
continue
for message in data.get('messages', []):
for call in message.get('tool_calls') or []:
calls[call['id']] = call
if message.get('role') == 'tool':
results.append(message)
if not results:
path = directory / 'holmes-tool-trace.jsonl'
if path.is_file():
for line in path.read_text().splitlines():
try:
row = json.loads(line)
except ValueError:
continue
for call in row.get('tool_calls', []):
calls[call['id']] = call
if row.get('previous_result'):
results.append(row['previous_result'])
seen = set()
for result in results:
identifier = result.get('tool_call_id')
if identifier in seen or identifier not in calls:
continue
seen.add(identifier)
function = calls[identifier].get('function', {})
if function.get('name') != 'bash':
continue
try:
args = function.get('arguments') or {}
if isinstance(args, str):
args = json.loads(args)
command = args['command']
except (ValueError, KeyError, TypeError):
continue
text = str(result.get('content') or '')
marker = 'The tool call output follows on the next line.\n'
if marker in text:
text = text.split(marker, 1)[1]
if text.startswith('tool_call_metadata='):
try:
_, end = json.JSONDecoder().raw_decode(text[len('tool_call_metadata='):])
text = text[len('tool_call_metadata=') + end:]
except ValueError:
pass
if text.startswith(command + '\n'):
text = text[len(command) + 1:]
yield command, text
def render(directory):
print('Recorded Holmes commands and tool output:')
count = 0
for command, output in tool_evidence(directory):
print('$', command)
print(output.strip() or '(no output)')
count += 1
if not count:
print('No paired command/result evidence was saved; outcome is unconfirmed.')
print('\nModel narrative and five-whys hypotheses remain unverified in the private conversation. '
'Recorded tool results and independent checks determine the outcome.')
if __name__ == '__main__':
render(Path(sys.argv[1]))