Second embedding input: recent agent state. For multi-step agent trajectories the last_user text is often identical across steps (e.g. a swebench PR description), causing embedding collisions. The tier-discriminating signal lives in the recent assistant/tool exchange — what's been t
(messages: list[dict[str, Any]], budget_chars: int = 2400)
| 82 | |
| 83 | |
| 84 | def _extract_agent_state(messages: list[dict[str, Any]], budget_chars: int = 2400) -> str: |
| 85 | """Second embedding input: recent agent state. |
| 86 | |
| 87 | For multi-step agent trajectories the last_user text is often identical across |
| 88 | steps (e.g. a swebench PR description), causing embedding collisions. The |
| 89 | tier-discriminating signal lives in the recent assistant/tool exchange — |
| 90 | what's been tried, what errors appeared, which tools are being used. We |
| 91 | embed that separately and concatenate both vectors at classifier time. |
| 92 | """ |
| 93 | recent: list[str] = [] |
| 94 | for m in reversed(messages): |
| 95 | role = m.get("role") |
| 96 | if role not in ("assistant", "tool"): |
| 97 | continue |
| 98 | content = _normalize_content(m.get("content", "")) |
| 99 | tcs = m.get("tool_calls") or [] |
| 100 | if tcs: |
| 101 | previews = [ |
| 102 | preview for tc in tcs[:4] |
| 103 | if isinstance(tc, dict) |
| 104 | for preview in [_tool_call_preview(tc)] |
| 105 | if preview |
| 106 | ] |
| 107 | if previews: |
| 108 | tool_text = "; ".join(previews) |
| 109 | content = f"{content}\n[tool_calls: {tool_text}]" if content else f"[tool_calls: {tool_text}]" |
| 110 | if content: |
| 111 | # Keep enough recent state to preserve intent across a short read / |
| 112 | # verify sequence, while still bounding the classifier input. |
| 113 | recent.append(f"[{role}] {content[:650]}") |
| 114 | if len(recent) >= 8: |
| 115 | break |
| 116 | if not recent: |
| 117 | return "" |
| 118 | return " | ".join(reversed(recent))[:budget_chars] |
| 119 | |
| 120 | |
| 121 | def _cosine_similarity(a: np.ndarray, b: np.ndarray) -> np.ndarray: |