Skip to content

Instantly share code, notes, and snippets.

@tonydzi
Created September 27, 2026 20:30
Show Gist options
  • Select an option

  • Save tonydzi/bb4d23bb58ec2afaa385b68cdefd1d47 to your computer and use it in GitHub Desktop.

Select an option

Save tonydzi/bb4d23bb58ec2afaa385b68cdefd1d47 to your computer and use it in GitHub Desktop.
browser-use 0.13.10: read_file content lives in <read_state> for exactly one step; only the 1000-char long_term_memory persists. A/B harness + a custom action that survives.
"""browser-use 0.13.10 — где живёт содержимое файла: <read_state> (1 шаг) vs history (навсегда).
Harness fix: history items are only appended when model_output is not None
(message_manager/service.py "Build the history item"), so we pass a real AgentOutput.
"""
import asyncio, csv, os, tempfile
from browser_use import Tools
from browser_use.agent.message_manager.service import MessageManager
from browser_use.agent.views import ActionResult, AgentBrain, AgentOutput, AgentStepInfo
from browser_use.filesystem.file_system import FileSystem
from browser_use.llm.messages import SystemMessage
d = tempfile.mkdtemp()
csv_path = os.path.join(d, "machine_readable_links_trunc.csv")
rows = [["reporting_entity_name_common", "machine_readable_page", "machine_readable_url"]]
for i in range(1, 60):
rows.append([f"Payer Number {i} Health Plan of Someplace",
f"https://example.com/mrf/page/{i}",
f"https://example.com/mrf/{i}.json"])
with open(csv_path, "w", newline="") as f:
csv.writer(f).writerows(rows)
print(f"CSV: {os.path.getsize(csv_path)} bytes, {len(rows)-1} data rows")
def mo(goal):
return AgentOutput(
thinking=None,
evaluation_previous_goal="ok",
memory="m",
next_goal=goal,
action=[],
)
def mk_mm(fs):
return MessageManager(task="read the csv then write the pairs out",
system_message=SystemMessage(content="sys"), file_system=fs)
def report(mm, label):
rs = mm.state.read_state_description or ""
hist = "\n".join(str(h) for h in mm.state.agent_history_items)
print(f" {label}")
print(f" <read_state> chars : {len(rs)}")
print(f" row 12 in <read_state> : {'Payer Number 12 ' in rs}")
print(f" row 2 in history (persistent) : {'Payer Number 2 ' in hist}")
print(f" row 12 in history (persistent) : {'Payer Number 12 ' in hist}")
async def main():
fs = FileSystem(d)
# ---------- A: the built-in read_file action ----------
sr = await fs.read_file_structured(csv_path, external_file=True)
result = sr["message"]
MAX_MEMORY_SIZE = 1000
lines, display, n = result.splitlines(), "", 0
for line in lines:
if len(display) + len(line) < MAX_MEMORY_SIZE:
display += line + "\n"; n += 1
else:
break
rem = len(lines) - n
memory = f"{display}{rem} more lines..." if rem > 0 else display
ar = ActionResult(extracted_content=result, long_term_memory=memory,
include_extracted_content_only_once=True)
print(f"\nA) built-in read_file: extracted_content={len(result)} chars, "
f"long_term_memory={len(memory)} chars, rows kept in memory="
f"{memory.count('Payer Number')} of {len(rows)-1}")
mm = mk_mm(fs)
mm._update_agent_history_description(mo("read the file"), [ar], AgentStepInfo(step_number=1, max_steps=10))
report(mm, "STEP 1 (right after read_file):")
mm._update_agent_history_description(mo("write the file"),
[ActionResult(long_term_memory="Navigated to page")],
AgentStepInfo(step_number=2, max_steps=10))
report(mm, "STEP 2 (the step where write_file runs):")
# ---------- B: a custom action that persists ----------
tools = Tools()
@tools.action("Read selected columns and a row range from a local CSV file.")
def read_csv_rows(path: str, columns: list[str], first_row: int, last_row: int):
with open(path, newline="", encoding="utf-8") as f:
data = list(csv.DictReader(f))
picked = data[first_row - 1: last_row]
text = "\n".join(" | ".join(r[c] for c in columns) for r in picked)
payload = f"{len(picked)} rows from {os.path.basename(path)}:\n{text}"
return ActionResult(extracted_content=payload, long_term_memory=payload)
r = await read_csv_rows(path=csv_path,
columns=["reporting_entity_name_common", "machine_readable_page"],
first_row=2, last_row=12)
print(f"\nB) custom read_csv_rows: long_term_memory={len(r.long_term_memory)} chars, "
f"rows={r.long_term_memory.count('Payer Number')}")
mm2 = mk_mm(fs)
mm2._update_agent_history_description(mo("read rows"), [r], AgentStepInfo(step_number=1, max_steps=10))
report(mm2, "STEP 1:")
mm2._update_agent_history_description(mo("navigate"), [ActionResult(long_term_memory="Navigated")],
AgentStepInfo(step_number=2, max_steps=10))
mm2._update_agent_history_description(mo("write"), [ActionResult(long_term_memory="Clicked")],
AgentStepInfo(step_number=3, max_steps=10))
report(mm2, "STEP 3 (two steps later):")
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment