-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_environment.py
More file actions
45 lines (37 loc) · 2 KB
/
Copy pathagent_environment.py
File metadata and controls
45 lines (37 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Shared toy environment: a deterministic, reproducible analogue of the DGM paper's
tool-use hallucination bug (Appendix H). The agent's job is to fix `call_tool` so it
stops fabricating results instead of actually invoking tools.
This file represents the agent's OWN codebase (the thing it's allowed to edit).
"""
import random
def real_tool_implementation(tool_name: str, args: dict) -> str:
"""The 'ground truth' tool execution - not editable, simulates a real tool call."""
if tool_name == "bash":
return f"executed: {args.get('command', '')}"
elif tool_name == "search":
return f"found 3 results for: {args.get('query', '')}"
return f"ran {tool_name} with {args}"
# BUG: under certain conditions (simulating "long context"), the wrapper hallucinates
# a plausible-looking result instead of actually calling the tool.
def call_tool(tool_name: str, args: dict, context_length: int, log: list) -> str:
"""
THIS FUNCTION HAS A BUG (intentionally seeded, deterministic via context_length
threshold rather than randomness, so behavior is reproducible across runs).
When context_length > 500, instead of actually invoking the tool, this function
fabricates a plausible-sounding result and returns it as if the tool had run.
"""
if context_length > 500:
# HALLUCINATION: fabricate output without calling the real tool
fake_result = f"[fabricated] {tool_name} probably returned something reasonable"
log.append({"event": "hallucinated_call", "tool": tool_name})
return fake_result
else:
result = real_tool_implementation(tool_name, args)
log.append({"event": "real_call", "tool": tool_name})
return result
def run_scenario(tool_name: str, args: dict, context_length: int) -> dict:
"""Run one scenario and return the log + result for scoring."""
log = []
result = call_tool(tool_name, args, context_length, log)
return {"log": log, "result": result, "context_length": context_length, "tool_name": tool_name}