Skip to content

Commit d0a33da

Browse files
Update behavioral monitor sample to use TaskResult history
1 parent f52a9a2 commit d0a33da

2 files changed

Lines changed: 22 additions & 40 deletions

File tree

python/samples/agentchat_behavioral_monitor/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,17 @@ monitor = BehavioralMonitor(
5555
min_messages=6,
5656
)
5757

58-
# Check after each conversation step
59-
result = monitor.check(agent.chat_messages[recipient])
58+
history = []
59+
60+
# Check after each public AgentChat run
61+
task_result = await assistant_agent.run(task="Use jwt and bcrypt for auth")
62+
result = monitor.observe_result(history, task_result)
6063
if result["drift_detected"]:
61-
# Re-inject context or trigger memory recall
6264
print("Drift at turn", result["turn"], "ghost:", result["ghost_terms"])
6365

64-
# Or patch an agent to auto-check after every reply
65-
monitor.patch_agent(assistant_agent)
66+
# Later runs keep extending the same external history
67+
task_result = await assistant_agent.run(task="Now add a profile endpoint")
68+
result = monitor.observe_result(history, task_result)
6669
```
6770

6871
## Parameters

python/samples/agentchat_behavioral_monitor/main.py

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77
to detect that drift by measuring Ghost Consistency Score (CCS): the fraction
88
of vocabulary from the earliest turns still present in the most recent turns.
99
10+
The integration path here stays on the public AgentChat surface:
11+
collect `TaskResult.messages` from `AssistantAgent.run()` / `run_stream()`,
12+
append them to an external history list, and score that history after each run.
13+
1014
Usage:
1115
python main.py
1216
1317
Requires:
1418
pip install autogen-agentchat
1519
16-
Optional (for richer semantic distance):
17-
pip install sentence-transformers
20+
Optional:
21+
Use with `AssistantAgent.run()` / `run_stream()` by passing the
22+
accumulated `TaskResult.messages` history into `observe_result()`.
1823
"""
1924

2025
from __future__ import annotations
2126

2227
import re
23-
import sys
2428
from collections import Counter
2529
from typing import Any, Dict, List, Optional, Sequence
2630

@@ -155,40 +159,15 @@ def check(self, messages: Sequence[Any]) -> Dict[str, Any]:
155159

156160
return result
157161

158-
def patch_agent(self, agent: Any) -> None:
162+
def observe_result(self, history: List[Any], task_result: Any) -> Dict[str, Any]:
159163
"""
160-
Monkey-patch an AssistantAgent so that every reply automatically
161-
runs a consistency check. Prints a warning when drift is detected.
164+
Append one public AgentChat task result into `history`, then score it.
165+
166+
This works with the current public `AssistantAgent.run()` /
167+
`run_stream()` API instead of monkey-patching private internals.
162168
"""
163-
original = agent.generate_reply
164-
monitor = self
165-
166-
def _patched(*args, **kwargs):
167-
reply = original(*args, **kwargs)
168-
msgs = []
169-
for attr in ("chat_messages", "_oai_messages", "messages"):
170-
candidate = getattr(agent, attr, None)
171-
if candidate is None:
172-
continue
173-
if isinstance(candidate, dict):
174-
for v in candidate.values():
175-
msgs = list(v)
176-
break
177-
elif isinstance(candidate, list):
178-
msgs = list(candidate)
179-
if msgs:
180-
break
181-
if msgs:
182-
r = monitor.check(msgs)
183-
if r["drift_detected"]:
184-
print(
185-
f"[BehavioralMonitor] \u26a0 drift at turn {r['turn']}: "
186-
f"CCS={r['ccs']:.3f}, ghost={r['ghost_terms']}",
187-
file=sys.stderr,
188-
)
189-
return reply
190-
191-
agent.generate_reply = _patched
169+
history.extend(getattr(task_result, "messages", []))
170+
return self.check(history)
192171

193172

194173
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)