agent-status-sdk 2.4.0__tar.gz → 2.4.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/PKG-INFO +12 -2
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/README.md +11 -1
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/agent_status/__init__.py +1 -1
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/agent_status/observe.py +483 -88
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/pyproject.toml +1 -1
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/tests/test_observe.py +125 -0
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/.github/workflows/publish.yml +0 -0
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/.gitignore +0 -0
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/CONTRIBUTING.md +0 -0
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/LICENSE +0 -0
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/examples/README.md +0 -0
- {agent_status_sdk-2.4.0 → agent_status_sdk-2.4.2}/tests/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: agent-status-sdk
|
|
3
|
-
Version: 2.4.
|
|
3
|
+
Version: 2.4.2
|
|
4
4
|
Summary: Fail-open wrap for live MCP and personal-agent traffic on AgentStatus.
|
|
5
5
|
Project-URL: Homepage, https://agentstatus.dev
|
|
6
6
|
Project-URL: Dashboard, https://agentstatus.dev
|
|
@@ -76,6 +76,16 @@ observe(mcp, agent_id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
|
|
|
76
76
|
mcp.run()
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
+
Write tools only: pass the pair. After `create_invoice` the wrap calls `get_invoice` on this same request with the new id. Cursor does not see that call. No map means no second call.
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
observe(
|
|
83
|
+
mcp,
|
|
84
|
+
agent_id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
|
|
85
|
+
lookup={"create_invoice": "get_invoice"},
|
|
86
|
+
)
|
|
87
|
+
```
|
|
88
|
+
|
|
79
89
|
If `AGENTSTATUS_AGENT_ID` is set, `observe(mcp)` is enough.
|
|
80
90
|
|
|
81
91
|
## Low-level MCP `Server`
|
|
@@ -138,7 +148,7 @@ def handle_thread(thread):
|
|
|
138
148
|
return call_tool("get_calendar", {"when": "tomorrow"})
|
|
139
149
|
```
|
|
140
150
|
|
|
141
|
-
That one wrap is User Traffic and the lookup. After
|
|
151
|
+
That one wrap is User Traffic and the lookup. After any job the wrap calls a list/get sibling (`create_ticket` → `list_tickets`, `book_table` → `get_calendar`). Tools the wrap has already seen in the same stem count too. You do not set a second URL. If there is no read sibling, the dashboard says we don't know. `landed=` still overrides.
|
|
142
152
|
|
|
143
153
|
`track("calendar", ...)` still works for a single job.
|
|
144
154
|
|
|
@@ -50,6 +50,16 @@ observe(mcp, agent_id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
|
|
|
50
50
|
mcp.run()
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
Write tools only: pass the pair. After `create_invoice` the wrap calls `get_invoice` on this same request with the new id. Cursor does not see that call. No map means no second call.
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
observe(
|
|
57
|
+
mcp,
|
|
58
|
+
agent_id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
|
|
59
|
+
lookup={"create_invoice": "get_invoice"},
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
53
63
|
If `AGENTSTATUS_AGENT_ID` is set, `observe(mcp)` is enough.
|
|
54
64
|
|
|
55
65
|
## Low-level MCP `Server`
|
|
@@ -112,7 +122,7 @@ def handle_thread(thread):
|
|
|
112
122
|
return call_tool("get_calendar", {"when": "tomorrow"})
|
|
113
123
|
```
|
|
114
124
|
|
|
115
|
-
That one wrap is User Traffic and the lookup. After
|
|
125
|
+
That one wrap is User Traffic and the lookup. After any job the wrap calls a list/get sibling (`create_ticket` → `list_tickets`, `book_table` → `get_calendar`). Tools the wrap has already seen in the same stem count too. You do not set a second URL. If there is no read sibling, the dashboard says we don't know. `landed=` still overrides.
|
|
116
126
|
|
|
117
127
|
`track("calendar", ...)` still works for a single job.
|
|
118
128
|
|
|
@@ -6,6 +6,7 @@ import inspect
|
|
|
6
6
|
import json
|
|
7
7
|
import logging
|
|
8
8
|
import os
|
|
9
|
+
import re
|
|
9
10
|
import sys
|
|
10
11
|
import threading
|
|
11
12
|
import time
|
|
@@ -118,6 +119,12 @@ def job_name(name: str) -> str:
|
|
|
118
119
|
return "booking"
|
|
119
120
|
if any(part in label for part in ("pay", "charge", "card", "billing")):
|
|
120
121
|
return "payments"
|
|
122
|
+
for prefix in (
|
|
123
|
+
"get_", "book_", "check_", "create_", "list_", "add_", "send_", "hold_",
|
|
124
|
+
"update_", "delete_", "search_", "fetch_",
|
|
125
|
+
):
|
|
126
|
+
if label.startswith(prefix):
|
|
127
|
+
return label[len(prefix):][:40]
|
|
121
128
|
return label.replace("get_", "").replace("book_", "").replace("check_", "")[:40]
|
|
122
129
|
|
|
123
130
|
|
|
@@ -127,9 +134,143 @@ def _excerpt(result: Any) -> str:
|
|
|
127
134
|
value = result.get(key)
|
|
128
135
|
if value:
|
|
129
136
|
return str(value)[:2000]
|
|
137
|
+
content = result.get("content")
|
|
138
|
+
if isinstance(content, list):
|
|
139
|
+
parts = [
|
|
140
|
+
str(item.get("text") or "")
|
|
141
|
+
for item in content
|
|
142
|
+
if isinstance(item, dict) and item.get("text")
|
|
143
|
+
]
|
|
144
|
+
if parts:
|
|
145
|
+
return "\n".join(parts)[:2000]
|
|
130
146
|
return str(result or "")[:2000]
|
|
131
147
|
|
|
132
148
|
|
|
149
|
+
_ID_KEYS = (
|
|
150
|
+
"id",
|
|
151
|
+
"invoice_id",
|
|
152
|
+
"event_id",
|
|
153
|
+
"ticket_id",
|
|
154
|
+
"message_id",
|
|
155
|
+
"booking_id",
|
|
156
|
+
"reservation_id",
|
|
157
|
+
"order_id",
|
|
158
|
+
"charge_id",
|
|
159
|
+
"uid",
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
_SKIP_ID_KEYS = ("agent", "user", "session", "token", "secret", "auth")
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _lookup_pairs(raw: Any) -> Dict[str, str]:
|
|
166
|
+
if not isinstance(raw, dict):
|
|
167
|
+
return {}
|
|
168
|
+
out: Dict[str, str] = {}
|
|
169
|
+
for key, value in raw.items():
|
|
170
|
+
write = str(key or "").strip()
|
|
171
|
+
read = str(value or "").strip()
|
|
172
|
+
if write and read and write != read:
|
|
173
|
+
out[write] = read
|
|
174
|
+
return out
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _id_from_result(result: Any, depth: int = 0) -> str:
|
|
178
|
+
if depth > 5 or result is None:
|
|
179
|
+
return ""
|
|
180
|
+
if isinstance(result, dict):
|
|
181
|
+
for key in _ID_KEYS:
|
|
182
|
+
if "agent" in key:
|
|
183
|
+
continue
|
|
184
|
+
value = result.get(key)
|
|
185
|
+
text = str(value or "").strip()
|
|
186
|
+
if text and " " not in text and 1 <= len(text) <= 120:
|
|
187
|
+
return text[:120]
|
|
188
|
+
if "content" in result:
|
|
189
|
+
found = _id_from_result({"text": _excerpt(result)}, depth + 1)
|
|
190
|
+
if found:
|
|
191
|
+
return found
|
|
192
|
+
for key, value in result.items():
|
|
193
|
+
if any(part in str(key).lower() for part in _SKIP_ID_KEYS):
|
|
194
|
+
continue
|
|
195
|
+
found = _id_from_result(value, depth + 1)
|
|
196
|
+
if found:
|
|
197
|
+
return found
|
|
198
|
+
return ""
|
|
199
|
+
if isinstance(result, (list, tuple)) and result:
|
|
200
|
+
return _id_from_result(result[0], depth + 1)
|
|
201
|
+
text = str(result).strip()
|
|
202
|
+
if text.startswith("{") or text.startswith("["):
|
|
203
|
+
try:
|
|
204
|
+
return _id_from_result(json.loads(text), depth + 1)
|
|
205
|
+
except Exception:
|
|
206
|
+
pass
|
|
207
|
+
for key in _ID_KEYS:
|
|
208
|
+
match = re.search(rf'"{key}"\s*:\s*"([^"]+)"', text)
|
|
209
|
+
if match:
|
|
210
|
+
return match.group(1)[:120]
|
|
211
|
+
match = re.search(rf"'{key}'\s*:\s*'([^']+)'", text)
|
|
212
|
+
if match:
|
|
213
|
+
return match.group(1)[:120]
|
|
214
|
+
return ""
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _id_shown(ident: str, blob: str) -> Optional[bool]:
|
|
218
|
+
text = (blob or "").strip().lower()
|
|
219
|
+
needle = str(ident or "").strip().lower()
|
|
220
|
+
if not needle:
|
|
221
|
+
return None
|
|
222
|
+
if "ember stub" in text:
|
|
223
|
+
return None
|
|
224
|
+
if any(marker in text for marker in _MISS_MARKERS):
|
|
225
|
+
return False
|
|
226
|
+
if not text:
|
|
227
|
+
return False
|
|
228
|
+
return needle in text
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _read_args(write: str, ident: str) -> list:
|
|
232
|
+
noun = str(write or "")
|
|
233
|
+
for prefix in ("create_", "send_", "book_", "add_", "update_", "post_"):
|
|
234
|
+
if noun.startswith(prefix):
|
|
235
|
+
noun = noun[len(prefix):]
|
|
236
|
+
break
|
|
237
|
+
seen = []
|
|
238
|
+
out = []
|
|
239
|
+
for blob in ({"id": ident}, {f"{noun}_id": ident} if noun else {}, {"invoice_id": ident}):
|
|
240
|
+
if not blob:
|
|
241
|
+
continue
|
|
242
|
+
key = tuple(blob.items())
|
|
243
|
+
if key in seen:
|
|
244
|
+
continue
|
|
245
|
+
seen.append(key)
|
|
246
|
+
out.append(blob)
|
|
247
|
+
return out
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _retarget_req(req: Any, name: str, arguments: Dict[str, Any]) -> Any:
|
|
251
|
+
if isinstance(req, dict):
|
|
252
|
+
params = dict(req.get("params") or {}) if isinstance(req.get("params"), dict) else {}
|
|
253
|
+
params["name"] = name
|
|
254
|
+
params["arguments"] = arguments
|
|
255
|
+
return {**req, "params": params}
|
|
256
|
+
params = getattr(req, "params", None)
|
|
257
|
+
if isinstance(params, dict):
|
|
258
|
+
cloned = dict(req.__dict__) if hasattr(req, "__dict__") else None
|
|
259
|
+
if cloned is None:
|
|
260
|
+
return None
|
|
261
|
+
cloned_params = dict(params)
|
|
262
|
+
cloned_params["name"] = name
|
|
263
|
+
cloned_params["arguments"] = arguments
|
|
264
|
+
cloned["params"] = cloned_params
|
|
265
|
+
return type(req)(**cloned) if callable(type(req)) else None
|
|
266
|
+
if params is not None and hasattr(req, "model_copy") and hasattr(params, "model_copy"):
|
|
267
|
+
try:
|
|
268
|
+
return req.model_copy(update={"params": params.model_copy(update={"name": name, "arguments": arguments})})
|
|
269
|
+
except Exception:
|
|
270
|
+
return None
|
|
271
|
+
return None
|
|
272
|
+
|
|
273
|
+
|
|
133
274
|
def _landed_flag(result: Any) -> Optional[bool]:
|
|
134
275
|
if isinstance(result, dict) and "landed" in result:
|
|
135
276
|
return bool(result.get("landed"))
|
|
@@ -141,13 +282,13 @@ def _landed_flag(result: Any) -> Optional[bool]:
|
|
|
141
282
|
|
|
142
283
|
_LOOKUPING = threading.local()
|
|
143
284
|
|
|
144
|
-
|
|
145
|
-
"
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
"
|
|
149
|
-
|
|
150
|
-
|
|
285
|
+
_READ_PREFIXES = (
|
|
286
|
+
"list_", "get_", "check_", "search_", "fetch_", "read_", "lookup_", "show_", "find_",
|
|
287
|
+
)
|
|
288
|
+
_WRITE_PREFIXES = (
|
|
289
|
+
"create_", "book_", "add_", "send_", "post_", "set_", "hold_", "update_",
|
|
290
|
+
"delete_", "cancel_", "put_", "make_",
|
|
291
|
+
)
|
|
151
292
|
_MISS_MARKERS = (
|
|
152
293
|
"no event",
|
|
153
294
|
"not found",
|
|
@@ -160,6 +301,9 @@ _MISS_MARKERS = (
|
|
|
160
301
|
"no reservation",
|
|
161
302
|
"not set up",
|
|
162
303
|
"nothing charged",
|
|
304
|
+
"unknown tool",
|
|
305
|
+
"no such tool",
|
|
306
|
+
"not implemented",
|
|
163
307
|
)
|
|
164
308
|
|
|
165
309
|
|
|
@@ -167,107 +311,174 @@ def _hints(arguments: Any) -> list:
|
|
|
167
311
|
if not isinstance(arguments, dict):
|
|
168
312
|
return []
|
|
169
313
|
out = []
|
|
170
|
-
for key in ("when", "date", "day", "time", "title", "who"):
|
|
314
|
+
for key in ("when", "date", "day", "time", "title", "who", "id", "name"):
|
|
171
315
|
value = str(arguments.get(key) or "").strip().lower()
|
|
172
316
|
if len(value) >= 3:
|
|
173
317
|
out.append(value)
|
|
174
318
|
return out
|
|
175
319
|
|
|
176
320
|
|
|
177
|
-
def
|
|
178
|
-
|
|
179
|
-
|
|
321
|
+
def _bare(name: str) -> str:
|
|
322
|
+
label = str(name or "").strip().lower()
|
|
323
|
+
for prefix in _WRITE_PREFIXES + _READ_PREFIXES:
|
|
324
|
+
if label.startswith(prefix):
|
|
325
|
+
return label[len(prefix):]
|
|
326
|
+
return label
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _stems(name: str) -> list:
|
|
330
|
+
bare = _bare(name)
|
|
331
|
+
out = []
|
|
332
|
+
for item in (bare, bare.split("_")[-1] if "_" in bare else ""):
|
|
333
|
+
if item and item not in out:
|
|
334
|
+
out.append(item)
|
|
335
|
+
return out
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def _is_read(name: str) -> bool:
|
|
339
|
+
label = str(name or "").strip().lower()
|
|
340
|
+
if any(label.startswith(prefix) for prefix in _WRITE_PREFIXES):
|
|
180
341
|
return False
|
|
342
|
+
return True
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def sibling_readers(name: Any, seen: Optional[set] = None) -> list:
|
|
346
|
+
"""Read tools that can confirm this job. Not a fixed job list."""
|
|
347
|
+
tool = str(name or "").strip()
|
|
348
|
+
label = tool.lower()
|
|
349
|
+
stems = _stems(tool)
|
|
350
|
+
out: list = []
|
|
351
|
+
|
|
352
|
+
def add(candidate: Any) -> None:
|
|
353
|
+
item = str(candidate or "").strip()
|
|
354
|
+
if not item or item in out:
|
|
355
|
+
return
|
|
356
|
+
if item.lower() == label and not _is_read(item):
|
|
357
|
+
return
|
|
358
|
+
if not _is_read(item):
|
|
359
|
+
return
|
|
360
|
+
out.append(item)
|
|
361
|
+
|
|
362
|
+
if _is_read(tool):
|
|
363
|
+
add(tool)
|
|
364
|
+
if any(part in label for part in ("book", "hold", "reserv", "table", "hotel", "flight")):
|
|
365
|
+
add("get_calendar")
|
|
366
|
+
add("list_events")
|
|
367
|
+
for other in list(seen or []):
|
|
368
|
+
if any(part in str(other).lower() for part in ("calendar", "event")):
|
|
369
|
+
add(other)
|
|
370
|
+
for other in list(seen or []):
|
|
371
|
+
if set(_stems(other)) & set(stems):
|
|
372
|
+
add(other)
|
|
373
|
+
for stem in stems:
|
|
374
|
+
for prefix in _READ_PREFIXES:
|
|
375
|
+
add(prefix + stem)
|
|
376
|
+
add(prefix + stem + "s")
|
|
377
|
+
if stem.endswith("s") and len(stem) > 3:
|
|
378
|
+
add(prefix + stem[:-1])
|
|
379
|
+
return out[:16]
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def _lookup_found(name: str, blob: str, hints: list, *, own: bool = False) -> Optional[bool]:
|
|
383
|
+
text = (blob or "").strip().lower()
|
|
181
384
|
if "ember stub" in text:
|
|
182
385
|
return None
|
|
183
|
-
if any(marker in text for marker in _MISS_MARKERS):
|
|
184
|
-
return False
|
|
185
|
-
if job == "booking":
|
|
186
|
-
if any(part in text for part in ("table", "reserv", "book", "hold", "dinner")):
|
|
187
|
-
return True
|
|
188
|
-
if hints and any(hint in text for hint in hints):
|
|
189
|
-
return True
|
|
190
|
-
return False
|
|
191
|
-
if job == "payments":
|
|
192
|
-
if any(part in text for part in ("set up", "charged", "card", "paid", "invoice")):
|
|
193
|
-
return True
|
|
386
|
+
if not text or any(marker in text for marker in _MISS_MARKERS):
|
|
194
387
|
return False
|
|
195
|
-
if job == "mail":
|
|
196
|
-
if any(part in text for part in ("mail", "inbox", "unread", "draft", "email")):
|
|
197
|
-
return True
|
|
198
|
-
return len(text) > 12
|
|
199
|
-
if job == "calendar":
|
|
200
|
-
if any(part in text for part in ("calendar", "event", "meeting", "standup", "am", "pm")):
|
|
201
|
-
return True
|
|
202
|
-
return len(text) > 12
|
|
203
388
|
if hints and any(hint in text for hint in hints):
|
|
204
389
|
return True
|
|
205
|
-
|
|
390
|
+
if any(stem and stem in text for stem in _stems(name)):
|
|
391
|
+
return True
|
|
392
|
+
if own and len(text) >= 8:
|
|
393
|
+
return True
|
|
394
|
+
return None
|
|
206
395
|
|
|
207
396
|
|
|
208
|
-
def lookup_after(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
397
|
+
def lookup_after(
|
|
398
|
+
fn: Callable[..., Any],
|
|
399
|
+
name: Any,
|
|
400
|
+
arguments: Any,
|
|
401
|
+
result: Any,
|
|
402
|
+
seen: Optional[set] = None,
|
|
403
|
+
) -> Optional[Dict[str, Any]]:
|
|
404
|
+
"""Re-call a read sibling. None means we could not look."""
|
|
405
|
+
tool = str(name or "")
|
|
406
|
+
readers = sibling_readers(tool, seen)
|
|
212
407
|
hints = _hints(arguments)
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
found = _lookup_found(job, blob, hints)
|
|
408
|
+
if _is_read(tool):
|
|
409
|
+
found = _lookup_found(tool, _excerpt(result), hints, own=True)
|
|
216
410
|
if found is not None:
|
|
217
|
-
return _lookup_proof(found,
|
|
411
|
+
return _lookup_proof(found, tool, looked=False)
|
|
218
412
|
setattr(_LOOKUPING, "on", True)
|
|
413
|
+
miss: Optional[Dict[str, Any]] = None
|
|
219
414
|
try:
|
|
220
|
-
for
|
|
221
|
-
|
|
222
|
-
looked = fn(tool, arguments if isinstance(arguments, dict) else {})
|
|
223
|
-
except TypeError:
|
|
224
|
-
try:
|
|
225
|
-
looked = fn(tool)
|
|
226
|
-
except Exception:
|
|
227
|
-
continue
|
|
228
|
-
except Exception:
|
|
415
|
+
for reader in readers:
|
|
416
|
+
if reader.lower() == tool.lower():
|
|
229
417
|
continue
|
|
230
|
-
|
|
231
|
-
if
|
|
418
|
+
looked = _call_reader(fn, reader, arguments)
|
|
419
|
+
if looked is _LOOKUP_MISS:
|
|
232
420
|
continue
|
|
233
|
-
|
|
421
|
+
found = _lookup_found(tool, _excerpt(looked), hints)
|
|
422
|
+
if found is True:
|
|
423
|
+
return _lookup_proof(True, reader, looked=True)
|
|
424
|
+
if found is False:
|
|
425
|
+
miss = _lookup_proof(False, reader, looked=True)
|
|
426
|
+
return miss
|
|
234
427
|
finally:
|
|
235
428
|
setattr(_LOOKUPING, "on", False)
|
|
236
|
-
return None
|
|
237
429
|
|
|
238
430
|
|
|
239
|
-
async def lookup_after_async(
|
|
240
|
-
|
|
241
|
-
|
|
431
|
+
async def lookup_after_async(
|
|
432
|
+
fn: Callable[..., Any],
|
|
433
|
+
name: Any,
|
|
434
|
+
arguments: Any,
|
|
435
|
+
result: Any,
|
|
436
|
+
seen: Optional[set] = None,
|
|
437
|
+
) -> Optional[Dict[str, Any]]:
|
|
438
|
+
tool = str(name or "")
|
|
439
|
+
readers = sibling_readers(tool, seen)
|
|
242
440
|
hints = _hints(arguments)
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
found = _lookup_found(job, blob, hints)
|
|
441
|
+
if _is_read(tool):
|
|
442
|
+
found = _lookup_found(tool, _excerpt(result), hints, own=True)
|
|
246
443
|
if found is not None:
|
|
247
|
-
return _lookup_proof(found,
|
|
444
|
+
return _lookup_proof(found, tool, looked=False)
|
|
248
445
|
setattr(_LOOKUPING, "on", True)
|
|
446
|
+
miss: Optional[Dict[str, Any]] = None
|
|
249
447
|
try:
|
|
250
|
-
for
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
except TypeError:
|
|
448
|
+
for reader in readers:
|
|
449
|
+
if reader.lower() == tool.lower():
|
|
450
|
+
continue
|
|
451
|
+
looked = _call_reader(fn, reader, arguments)
|
|
452
|
+
if inspect.isawaitable(looked):
|
|
256
453
|
try:
|
|
257
|
-
looked =
|
|
258
|
-
if inspect.isawaitable(looked):
|
|
259
|
-
looked = await looked
|
|
454
|
+
looked = await looked
|
|
260
455
|
except Exception:
|
|
261
456
|
continue
|
|
262
|
-
|
|
457
|
+
if looked is _LOOKUP_MISS:
|
|
263
458
|
continue
|
|
264
|
-
found = _lookup_found(
|
|
265
|
-
if found is
|
|
266
|
-
|
|
267
|
-
|
|
459
|
+
found = _lookup_found(tool, _excerpt(looked), hints)
|
|
460
|
+
if found is True:
|
|
461
|
+
return _lookup_proof(True, reader, looked=True)
|
|
462
|
+
if found is False:
|
|
463
|
+
miss = _lookup_proof(False, reader, looked=True)
|
|
464
|
+
return miss
|
|
268
465
|
finally:
|
|
269
466
|
setattr(_LOOKUPING, "on", False)
|
|
270
|
-
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
_LOOKUP_MISS = object()
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def _call_reader(fn: Callable[..., Any], reader: str, arguments: Any) -> Any:
|
|
473
|
+
try:
|
|
474
|
+
return fn(reader, arguments if isinstance(arguments, dict) else {})
|
|
475
|
+
except TypeError:
|
|
476
|
+
try:
|
|
477
|
+
return fn(reader)
|
|
478
|
+
except Exception:
|
|
479
|
+
return _LOOKUP_MISS
|
|
480
|
+
except Exception:
|
|
481
|
+
return _LOOKUP_MISS
|
|
271
482
|
|
|
272
483
|
|
|
273
484
|
def _lookup_proof(found: bool, tool: str, *, looked: bool) -> Dict[str, Any]:
|
|
@@ -491,7 +702,8 @@ class Agent:
|
|
|
491
702
|
*,
|
|
492
703
|
landed: Optional[Callable[[Any, Any], Any]] = None,
|
|
493
704
|
) -> Callable[..., Any]:
|
|
494
|
-
"""Wrap once. After each job the wrap looks
|
|
705
|
+
"""Wrap once. After each job the wrap looks up a list/get sibling."""
|
|
706
|
+
seen: set = set()
|
|
495
707
|
|
|
496
708
|
def apply_land(run: JobRun, name: Any, result: Any, proof: Optional[Dict[str, Any]] = None) -> None:
|
|
497
709
|
flag: Any = None
|
|
@@ -524,9 +736,10 @@ class Agent:
|
|
|
524
736
|
if result is not None:
|
|
525
737
|
run.result(_excerpt(result))
|
|
526
738
|
proof = None
|
|
739
|
+
seen.add(str(name or "").strip())
|
|
527
740
|
if landed is None and _landed_flag(result) is None:
|
|
528
741
|
try:
|
|
529
|
-
proof = await lookup_after_async(fn, name, arguments, result)
|
|
742
|
+
proof = await lookup_after_async(fn, name, arguments, result, seen)
|
|
530
743
|
except Exception:
|
|
531
744
|
proof = None
|
|
532
745
|
apply_land(run, name, result, proof)
|
|
@@ -544,9 +757,10 @@ class Agent:
|
|
|
544
757
|
if result is not None:
|
|
545
758
|
run.result(_excerpt(result))
|
|
546
759
|
proof = None
|
|
760
|
+
seen.add(str(name or "").strip())
|
|
547
761
|
if landed is None and _landed_flag(result) is None:
|
|
548
762
|
try:
|
|
549
|
-
proof = lookup_after(fn, name, arguments, result)
|
|
763
|
+
proof = lookup_after(fn, name, arguments, result, seen)
|
|
550
764
|
except Exception:
|
|
551
765
|
proof = None
|
|
552
766
|
apply_land(run, name, result, proof)
|
|
@@ -556,11 +770,14 @@ class Agent:
|
|
|
556
770
|
|
|
557
771
|
|
|
558
772
|
class Observer:
|
|
559
|
-
def __init__(self, agent_id: str = "", ingest: str = "") -> None:
|
|
773
|
+
def __init__(self, agent_id: str = "", ingest: str = "", lookup: Any = None) -> None:
|
|
560
774
|
self.agent_id = (agent_id or os.environ.get("AGENTSTATUS_AGENT_ID") or "").strip()
|
|
561
775
|
self.ingest = ingest
|
|
562
776
|
self.provider = ""
|
|
563
777
|
self.release = ""
|
|
778
|
+
self.lookup = _lookup_pairs(lookup)
|
|
779
|
+
self._handler: Optional[Callable[..., Any]] = None
|
|
780
|
+
self._named: Optional[Callable[..., Any]] = None
|
|
564
781
|
|
|
565
782
|
def note_initialize(self, req: Any) -> None:
|
|
566
783
|
params = getattr(req, "params", None)
|
|
@@ -575,7 +792,16 @@ class Observer:
|
|
|
575
792
|
self.provider = str(getattr(client, "name", "") or "")[:80]
|
|
576
793
|
self.release = str(getattr(client, "version", "") or "")[:80]
|
|
577
794
|
|
|
578
|
-
def emit_call(
|
|
795
|
+
def emit_call(
|
|
796
|
+
self,
|
|
797
|
+
req: Any,
|
|
798
|
+
err: Any,
|
|
799
|
+
duration_ms: int,
|
|
800
|
+
result: Any = None,
|
|
801
|
+
proof: Optional[Dict[str, Any]] = None,
|
|
802
|
+
) -> None:
|
|
803
|
+
if getattr(_LOOKUPING, "on", False):
|
|
804
|
+
return
|
|
579
805
|
name, arguments = _tool_name_args(req)
|
|
580
806
|
if not name:
|
|
581
807
|
return
|
|
@@ -584,7 +810,13 @@ class Observer:
|
|
|
584
810
|
message = str(getattr(err, "message", None) or err)[:800]
|
|
585
811
|
excerpt = ""
|
|
586
812
|
if result is not None and err is None:
|
|
587
|
-
excerpt = str(result)[:2000]
|
|
813
|
+
excerpt = _excerpt(result) or str(result)[:2000]
|
|
814
|
+
landed = None
|
|
815
|
+
status = str((proof or {}).get("status") or "")
|
|
816
|
+
if status == "found":
|
|
817
|
+
landed = True
|
|
818
|
+
elif status == "miss":
|
|
819
|
+
landed = False
|
|
588
820
|
report_tool_call(
|
|
589
821
|
name=name,
|
|
590
822
|
agent_id=self.agent_id,
|
|
@@ -595,37 +827,162 @@ class Observer:
|
|
|
595
827
|
release=self.release,
|
|
596
828
|
result_excerpt=excerpt,
|
|
597
829
|
ingest=self.ingest,
|
|
830
|
+
proof=proof,
|
|
831
|
+
landed=landed,
|
|
598
832
|
)
|
|
599
833
|
|
|
834
|
+
def _lookup_named(self, fn: Callable[..., Any], write: Any, result: Any) -> Optional[Dict[str, Any]]:
|
|
835
|
+
read = self.lookup.get(str(write or "").strip())
|
|
836
|
+
ident = _id_from_result(result)
|
|
837
|
+
if not read or not ident or getattr(_LOOKUPING, "on", False):
|
|
838
|
+
return None
|
|
839
|
+
setattr(_LOOKUPING, "on", True)
|
|
840
|
+
try:
|
|
841
|
+
for arguments in _read_args(str(write), ident):
|
|
842
|
+
try:
|
|
843
|
+
looked = fn(read, arguments)
|
|
844
|
+
if inspect.isawaitable(looked):
|
|
845
|
+
return None
|
|
846
|
+
except Exception:
|
|
847
|
+
continue
|
|
848
|
+
found = _id_shown(ident, _excerpt(looked))
|
|
849
|
+
if found is None:
|
|
850
|
+
continue
|
|
851
|
+
return _lookup_proof(found, read, looked=True)
|
|
852
|
+
except Exception:
|
|
853
|
+
return None
|
|
854
|
+
finally:
|
|
855
|
+
setattr(_LOOKUPING, "on", False)
|
|
856
|
+
return None
|
|
857
|
+
|
|
858
|
+
async def _lookup_named_async(self, fn: Callable[..., Any], write: Any, result: Any) -> Optional[Dict[str, Any]]:
|
|
859
|
+
read = self.lookup.get(str(write or "").strip())
|
|
860
|
+
ident = _id_from_result(result)
|
|
861
|
+
if not read or not ident or getattr(_LOOKUPING, "on", False):
|
|
862
|
+
return None
|
|
863
|
+
setattr(_LOOKUPING, "on", True)
|
|
864
|
+
try:
|
|
865
|
+
for arguments in _read_args(str(write), ident):
|
|
866
|
+
try:
|
|
867
|
+
looked = fn(read, arguments)
|
|
868
|
+
if inspect.isawaitable(looked):
|
|
869
|
+
looked = await looked
|
|
870
|
+
except Exception:
|
|
871
|
+
continue
|
|
872
|
+
found = _id_shown(ident, _excerpt(looked))
|
|
873
|
+
if found is None:
|
|
874
|
+
continue
|
|
875
|
+
return _lookup_proof(found, read, looked=True)
|
|
876
|
+
except Exception:
|
|
877
|
+
return None
|
|
878
|
+
finally:
|
|
879
|
+
setattr(_LOOKUPING, "on", False)
|
|
880
|
+
return None
|
|
881
|
+
|
|
882
|
+
def _lookup_handler(self, fn: Callable[..., Any], req: Any, result: Any) -> Optional[Dict[str, Any]]:
|
|
883
|
+
write, _ = _tool_name_args(req)
|
|
884
|
+
read = self.lookup.get(str(write or "").strip())
|
|
885
|
+
ident = _id_from_result(result)
|
|
886
|
+
if not read or not ident or getattr(_LOOKUPING, "on", False):
|
|
887
|
+
return None
|
|
888
|
+
setattr(_LOOKUPING, "on", True)
|
|
889
|
+
try:
|
|
890
|
+
for arguments in _read_args(write, ident):
|
|
891
|
+
cloned = _retarget_req(req, read, arguments)
|
|
892
|
+
if cloned is None:
|
|
893
|
+
continue
|
|
894
|
+
try:
|
|
895
|
+
looked = fn(cloned)
|
|
896
|
+
if inspect.isawaitable(looked):
|
|
897
|
+
return None
|
|
898
|
+
except Exception:
|
|
899
|
+
continue
|
|
900
|
+
found = _id_shown(ident, _excerpt(looked))
|
|
901
|
+
if found is None:
|
|
902
|
+
continue
|
|
903
|
+
return _lookup_proof(found, read, looked=True)
|
|
904
|
+
except Exception:
|
|
905
|
+
return None
|
|
906
|
+
finally:
|
|
907
|
+
setattr(_LOOKUPING, "on", False)
|
|
908
|
+
return None
|
|
909
|
+
|
|
910
|
+
async def _lookup_handler_async(self, fn: Callable[..., Any], req: Any, result: Any) -> Optional[Dict[str, Any]]:
|
|
911
|
+
write, _ = _tool_name_args(req)
|
|
912
|
+
read = self.lookup.get(str(write or "").strip())
|
|
913
|
+
ident = _id_from_result(result)
|
|
914
|
+
if not read or not ident or getattr(_LOOKUPING, "on", False):
|
|
915
|
+
return None
|
|
916
|
+
setattr(_LOOKUPING, "on", True)
|
|
917
|
+
try:
|
|
918
|
+
for arguments in _read_args(write, ident):
|
|
919
|
+
cloned = _retarget_req(req, read, arguments)
|
|
920
|
+
if cloned is None:
|
|
921
|
+
continue
|
|
922
|
+
try:
|
|
923
|
+
looked = fn(cloned)
|
|
924
|
+
if inspect.isawaitable(looked):
|
|
925
|
+
looked = await looked
|
|
926
|
+
except Exception:
|
|
927
|
+
continue
|
|
928
|
+
found = _id_shown(ident, _excerpt(looked))
|
|
929
|
+
if found is None:
|
|
930
|
+
continue
|
|
931
|
+
return _lookup_proof(found, read, looked=True)
|
|
932
|
+
except Exception:
|
|
933
|
+
return None
|
|
934
|
+
finally:
|
|
935
|
+
setattr(_LOOKUPING, "on", False)
|
|
936
|
+
return None
|
|
937
|
+
|
|
600
938
|
def wrap_handler(self, original: Callable[..., Any]) -> Callable[..., Any]:
|
|
939
|
+
self._handler = original
|
|
601
940
|
if _is_async(original):
|
|
602
941
|
async def wrapped(*args: Any, **kwargs: Any) -> Any:
|
|
942
|
+
if getattr(_LOOKUPING, "on", False):
|
|
943
|
+
return await original(*args, **kwargs)
|
|
603
944
|
started = time.monotonic()
|
|
604
945
|
err: Any = None
|
|
605
946
|
result: Any = None
|
|
947
|
+
proof: Optional[Dict[str, Any]] = None
|
|
948
|
+
req = _first_req(args, kwargs)
|
|
606
949
|
try:
|
|
607
950
|
result = await original(*args, **kwargs)
|
|
951
|
+
if result is not None:
|
|
952
|
+
try:
|
|
953
|
+
proof = await self._lookup_handler_async(original, req, result)
|
|
954
|
+
except Exception:
|
|
955
|
+
proof = None
|
|
608
956
|
return result
|
|
609
957
|
except Exception as exc:
|
|
610
958
|
err = exc
|
|
611
959
|
raise
|
|
612
960
|
finally:
|
|
613
|
-
self.emit_call(
|
|
961
|
+
self.emit_call(req, err, int((time.monotonic() - started) * 1000), result, proof)
|
|
614
962
|
|
|
615
963
|
return wrapped
|
|
616
964
|
|
|
617
965
|
def wrapped_sync(*args: Any, **kwargs: Any) -> Any:
|
|
966
|
+
if getattr(_LOOKUPING, "on", False):
|
|
967
|
+
return original(*args, **kwargs)
|
|
618
968
|
started = time.monotonic()
|
|
619
969
|
err: Any = None
|
|
620
970
|
result: Any = None
|
|
971
|
+
proof: Optional[Dict[str, Any]] = None
|
|
972
|
+
req = _first_req(args, kwargs)
|
|
621
973
|
try:
|
|
622
974
|
result = original(*args, **kwargs)
|
|
975
|
+
if result is not None:
|
|
976
|
+
try:
|
|
977
|
+
proof = self._lookup_handler(original, req, result)
|
|
978
|
+
except Exception:
|
|
979
|
+
proof = None
|
|
623
980
|
return result
|
|
624
981
|
except Exception as exc:
|
|
625
982
|
err = exc
|
|
626
983
|
raise
|
|
627
984
|
finally:
|
|
628
|
-
self.emit_call(
|
|
985
|
+
self.emit_call(req, err, int((time.monotonic() - started) * 1000), result, proof)
|
|
629
986
|
|
|
630
987
|
return wrapped_sync
|
|
631
988
|
|
|
@@ -644,51 +1001,89 @@ class Observer:
|
|
|
644
1001
|
return wrapped_sync
|
|
645
1002
|
|
|
646
1003
|
def wrap_named(self, original: Callable[..., Any]) -> Callable[..., Any]:
|
|
1004
|
+
self._named = original
|
|
647
1005
|
if _is_async(original):
|
|
648
1006
|
async def wrapped(name: Any, arguments: Any = None, *args: Any, **kwargs: Any) -> Any:
|
|
1007
|
+
if getattr(_LOOKUPING, "on", False):
|
|
1008
|
+
return await original(name, arguments, *args, **kwargs)
|
|
649
1009
|
started = time.monotonic()
|
|
650
1010
|
err: Any = None
|
|
651
1011
|
result: Any = None
|
|
1012
|
+
proof: Optional[Dict[str, Any]] = None
|
|
652
1013
|
try:
|
|
653
1014
|
result = await original(name, arguments, *args, **kwargs)
|
|
1015
|
+
if result is not None:
|
|
1016
|
+
try:
|
|
1017
|
+
proof = await self._lookup_named_async(original, name, result)
|
|
1018
|
+
except Exception:
|
|
1019
|
+
proof = None
|
|
654
1020
|
return result
|
|
655
1021
|
except Exception as exc:
|
|
656
1022
|
err = exc
|
|
657
1023
|
raise
|
|
658
1024
|
finally:
|
|
659
|
-
self.emit_call(
|
|
1025
|
+
self.emit_call(
|
|
1026
|
+
{"params": {"name": name, "arguments": arguments}},
|
|
1027
|
+
err,
|
|
1028
|
+
int((time.monotonic() - started) * 1000),
|
|
1029
|
+
result,
|
|
1030
|
+
proof,
|
|
1031
|
+
)
|
|
660
1032
|
|
|
661
1033
|
return wrapped
|
|
662
1034
|
|
|
663
1035
|
def wrapped_sync(name: Any, arguments: Any = None, *args: Any, **kwargs: Any) -> Any:
|
|
1036
|
+
if getattr(_LOOKUPING, "on", False):
|
|
1037
|
+
return original(name, arguments, *args, **kwargs)
|
|
664
1038
|
started = time.monotonic()
|
|
665
1039
|
err: Any = None
|
|
666
1040
|
result: Any = None
|
|
1041
|
+
proof: Optional[Dict[str, Any]] = None
|
|
667
1042
|
try:
|
|
668
1043
|
result = original(name, arguments, *args, **kwargs)
|
|
1044
|
+
if result is not None:
|
|
1045
|
+
try:
|
|
1046
|
+
proof = self._lookup_named(original, name, result)
|
|
1047
|
+
except Exception:
|
|
1048
|
+
proof = None
|
|
669
1049
|
return result
|
|
670
1050
|
except Exception as exc:
|
|
671
1051
|
err = exc
|
|
672
1052
|
raise
|
|
673
1053
|
finally:
|
|
674
|
-
self.emit_call(
|
|
1054
|
+
self.emit_call(
|
|
1055
|
+
{"params": {"name": name, "arguments": arguments}},
|
|
1056
|
+
err,
|
|
1057
|
+
int((time.monotonic() - started) * 1000),
|
|
1058
|
+
result,
|
|
1059
|
+
proof,
|
|
1060
|
+
)
|
|
675
1061
|
|
|
676
1062
|
return wrapped_sync
|
|
677
1063
|
|
|
678
1064
|
|
|
679
|
-
def observe(
|
|
1065
|
+
def observe(
|
|
1066
|
+
server: Any,
|
|
1067
|
+
agent_id: str = "",
|
|
1068
|
+
ingest: str = "",
|
|
1069
|
+
required: bool = False,
|
|
1070
|
+
lookup: Any = None,
|
|
1071
|
+
) -> Any:
|
|
680
1072
|
"""
|
|
681
1073
|
Wrap an MCP Server or FastMCP instance. Call before run().
|
|
682
1074
|
|
|
683
1075
|
from agent_status import observe
|
|
684
1076
|
observe(mcp, agent_id="...")
|
|
1077
|
+
observe(mcp, agent_id="...", lookup={"create_invoice": "get_invoice"})
|
|
685
1078
|
|
|
686
|
-
|
|
687
|
-
|
|
1079
|
+
lookup= is write tools only. After the write, we call the read tool on this
|
|
1080
|
+
same request with the new id. Cursor does not see that call. No map means
|
|
1081
|
+
no second call. If the wrap cannot find tools/call, the server still runs
|
|
1082
|
+
and we print a warning. Pass required=True to raise instead.
|
|
688
1083
|
"""
|
|
689
1084
|
hooked = False
|
|
690
1085
|
try:
|
|
691
|
-
obs = Observer(agent_id=agent_id, ingest=ingest)
|
|
1086
|
+
obs = Observer(agent_id=agent_id, ingest=ingest, lookup=lookup)
|
|
692
1087
|
inner = getattr(server, "_mcp_server", None) or getattr(server, "server", None) or server
|
|
693
1088
|
handlers = getattr(inner, "request_handlers", None)
|
|
694
1089
|
if isinstance(handlers, dict):
|
|
@@ -119,6 +119,40 @@ def test_wrap_lookup_miss_when_calendar_empty(monkeypatch):
|
|
|
119
119
|
assert captured[0]["proof"]["status"] == "miss"
|
|
120
120
|
|
|
121
121
|
|
|
122
|
+
def test_wrap_looks_up_new_job_sibling(monkeypatch):
|
|
123
|
+
captured = []
|
|
124
|
+
monkeypatch.setattr(observe_mod, "report_job", lambda **kwargs: captured.append(kwargs))
|
|
125
|
+
ember = Agent(agent_id="agent-1")
|
|
126
|
+
|
|
127
|
+
def call_tool(name, arguments=None):
|
|
128
|
+
if name == "create_ticket":
|
|
129
|
+
return "opened"
|
|
130
|
+
if name == "list_tickets":
|
|
131
|
+
return "TKT-9 in inbox"
|
|
132
|
+
raise KeyError(name)
|
|
133
|
+
|
|
134
|
+
wrapped = ember.wrap_tools(call_tool)
|
|
135
|
+
wrapped("create_ticket", {"title": "TKT-9"})
|
|
136
|
+
assert captured[0]["job"] == "ticket"
|
|
137
|
+
assert captured[0]["landed"] is True
|
|
138
|
+
assert "list_tickets" in captured[0]["proof"]["detail"]
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_wrap_skips_when_no_read_sibling(monkeypatch):
|
|
142
|
+
captured = []
|
|
143
|
+
monkeypatch.setattr(observe_mod, "report_job", lambda **kwargs: captured.append(kwargs))
|
|
144
|
+
ember = Agent(agent_id="agent-1")
|
|
145
|
+
|
|
146
|
+
def call_tool(name, arguments=None):
|
|
147
|
+
if name == "create_ticket":
|
|
148
|
+
return "opened"
|
|
149
|
+
raise KeyError(name)
|
|
150
|
+
|
|
151
|
+
wrapped = ember.wrap_tools(call_tool)
|
|
152
|
+
wrapped("create_ticket", {})
|
|
153
|
+
assert captured[0]["landed"] is None
|
|
154
|
+
|
|
155
|
+
|
|
122
156
|
def test_wrap_skips_lookup_on_ember_stub(monkeypatch):
|
|
123
157
|
captured = []
|
|
124
158
|
monkeypatch.setattr(observe_mod, "report_job", lambda **kwargs: captured.append(kwargs))
|
|
@@ -223,6 +257,97 @@ def test_observe_required_raises_when_unhooked():
|
|
|
223
257
|
assert "tools/call" in str(err)
|
|
224
258
|
|
|
225
259
|
|
|
260
|
+
def test_observe_lookup_uses_new_id_on_same_request(monkeypatch):
|
|
261
|
+
captured = []
|
|
262
|
+
monkeypatch.setattr(observe_mod, "report_tool_call", lambda **kwargs: captured.append(kwargs))
|
|
263
|
+
obs = Observer(agent_id="agent-1", lookup={"create_invoice": "get_invoice"})
|
|
264
|
+
seen = []
|
|
265
|
+
|
|
266
|
+
def call_tool(name, arguments=None):
|
|
267
|
+
seen.append((name, arguments))
|
|
268
|
+
if name == "create_invoice":
|
|
269
|
+
return {"id": "INV-1049"}
|
|
270
|
+
if name == "get_invoice":
|
|
271
|
+
return {"id": "INV-1049", "status": "open"}
|
|
272
|
+
raise KeyError(name)
|
|
273
|
+
|
|
274
|
+
wrapped = obs.wrap_named(call_tool)
|
|
275
|
+
assert wrapped("create_invoice", {"amount": 12}) == {"id": "INV-1049"}
|
|
276
|
+
assert seen == [
|
|
277
|
+
("create_invoice", {"amount": 12}),
|
|
278
|
+
("get_invoice", {"id": "INV-1049"}),
|
|
279
|
+
]
|
|
280
|
+
assert captured[0]["name"] == "create_invoice"
|
|
281
|
+
assert captured[0]["proof"]["shape"] == "lookup"
|
|
282
|
+
assert captured[0]["proof"]["status"] == "found"
|
|
283
|
+
assert "get_invoice" in captured[0]["proof"]["detail"]
|
|
284
|
+
assert captured[0]["landed"] is True
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def test_observe_lookup_miss_when_id_absent(monkeypatch):
|
|
288
|
+
captured = []
|
|
289
|
+
monkeypatch.setattr(observe_mod, "report_tool_call", lambda **kwargs: captured.append(kwargs))
|
|
290
|
+
obs = Observer(agent_id="agent-1", lookup={"create_invoice": "get_invoice"})
|
|
291
|
+
|
|
292
|
+
def call_tool(name, arguments=None):
|
|
293
|
+
if name == "create_invoice":
|
|
294
|
+
return {"id": "INV-1049"}
|
|
295
|
+
if name == "get_invoice":
|
|
296
|
+
return "not found"
|
|
297
|
+
raise KeyError(name)
|
|
298
|
+
|
|
299
|
+
obs.wrap_named(call_tool)("create_invoice", {})
|
|
300
|
+
assert captured[0]["proof"]["status"] == "miss"
|
|
301
|
+
assert captured[0]["landed"] is False
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def test_observe_skips_lookup_without_id(monkeypatch):
|
|
305
|
+
captured = []
|
|
306
|
+
monkeypatch.setattr(observe_mod, "report_tool_call", lambda **kwargs: captured.append(kwargs))
|
|
307
|
+
obs = Observer(agent_id="agent-1", lookup={"create_invoice": "get_invoice"})
|
|
308
|
+
seen = []
|
|
309
|
+
|
|
310
|
+
def call_tool(name, arguments=None):
|
|
311
|
+
seen.append(name)
|
|
312
|
+
return "created"
|
|
313
|
+
|
|
314
|
+
obs.wrap_named(call_tool)("create_invoice", {})
|
|
315
|
+
assert seen == ["create_invoice"]
|
|
316
|
+
assert captured[0].get("proof") is None
|
|
317
|
+
assert captured[0].get("landed") is None
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def test_observe_skips_lookup_without_map(monkeypatch):
|
|
321
|
+
captured = []
|
|
322
|
+
monkeypatch.setattr(observe_mod, "report_tool_call", lambda **kwargs: captured.append(kwargs))
|
|
323
|
+
obs = Observer(agent_id="agent-1")
|
|
324
|
+
seen = []
|
|
325
|
+
|
|
326
|
+
def call_tool(name, arguments=None):
|
|
327
|
+
seen.append(name)
|
|
328
|
+
return {"id": "INV-1049"}
|
|
329
|
+
|
|
330
|
+
obs.wrap_named(call_tool)("create_invoice", {})
|
|
331
|
+
assert seen == ["create_invoice"]
|
|
332
|
+
assert captured[0].get("landed") is None
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def test_observe_does_not_lookup_search_docs(monkeypatch):
|
|
336
|
+
captured = []
|
|
337
|
+
monkeypatch.setattr(observe_mod, "report_tool_call", lambda **kwargs: captured.append(kwargs))
|
|
338
|
+
obs = Observer(agent_id="agent-1", lookup={"create_invoice": "get_invoice"})
|
|
339
|
+
seen = []
|
|
340
|
+
|
|
341
|
+
def call_tool(name, arguments=None):
|
|
342
|
+
seen.append(name)
|
|
343
|
+
return {"id": "doc-1", "text": "refunds"}
|
|
344
|
+
|
|
345
|
+
obs.wrap_named(call_tool)("search_docs", {"q": "refunds"})
|
|
346
|
+
assert seen == ["search_docs"]
|
|
347
|
+
assert captured[0]["name"] == "search_docs"
|
|
348
|
+
assert captured[0].get("landed") is None
|
|
349
|
+
|
|
350
|
+
|
|
226
351
|
def test_track_miss_on_exception(monkeypatch):
|
|
227
352
|
captured = []
|
|
228
353
|
monkeypatch.setattr(observe_mod, "report_job", lambda **kwargs: captured.append(kwargs))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|