(messages: list[Any])
| 1483 | |
| 1484 | |
| 1485 | def _latest_tool_result_context(messages: list[Any]) -> tuple[str, bool, str]: |
| 1486 | for index in range(len(messages) - 1, -1, -1): |
| 1487 | msg = messages[index] |
| 1488 | if not isinstance(msg, dict): |
| 1489 | continue |
| 1490 | if msg.get("role") == "tool": |
| 1491 | text = _risk_text(msg.get("content")) |
| 1492 | command = _command_for_tool_result( |
| 1493 | messages, |
| 1494 | before_index=index, |
| 1495 | tool_call_id=str(msg.get("tool_call_id") or ""), |
| 1496 | ) |
| 1497 | return text, bool(msg.get("is_error")) or _has_nonzero_xml_returncode(text), command |
| 1498 | content = msg.get("content") |
| 1499 | if isinstance(content, list): |
| 1500 | for block in reversed(content): |
| 1501 | if isinstance(block, dict) and block.get("type") == "tool_result": |
| 1502 | text = _risk_text(block.get("content")) |
| 1503 | command = _command_for_tool_result( |
| 1504 | messages, |
| 1505 | before_index=index, |
| 1506 | tool_call_id=str(block.get("tool_use_id") or ""), |
| 1507 | ) |
| 1508 | return ( |
| 1509 | text, |
| 1510 | bool(block.get("is_error")) or _has_nonzero_xml_returncode(text), |
| 1511 | command, |
| 1512 | ) |
| 1513 | return "", False, "" |
| 1514 | |
| 1515 | |
| 1516 | def _latest_tool_result_signal(messages: list[Any]) -> tuple[str, bool]: |
no test coverage detected