Skip to content

Commit c6aca3e

Browse files
fix(website-browser): ensure download ended_at is set when hashing fails (#521)
Co-authored-by: Edgar Arakelyan <edgar.arakelyan@scale.com>
1 parent fc54178 commit c6aca3e

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎packages/agentenv-protocol/src/agentenv_protocol/website_browser/control.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,11 @@ def _on_Browser_downloadProgress(self, params: dict, session: Optional[str]) ->
713713
download["ended_at"] = _now()
714714

715715
async def _hashed(self, download: dict, path: Path) -> None:
716-
size, sha256 = await asyncio.to_thread(_size_and_sha256, path)
717-
download.update(size=size, sha256=sha256, ended_at=_now())
716+
try:
717+
size, sha256 = await asyncio.to_thread(_size_and_sha256, path)
718+
download.update(size=size, sha256=sha256)
719+
finally:
720+
download["ended_at"] = _now()
718721

719722
def _spawn(self, work: Awaitable) -> None:
720723
task = asyncio.ensure_future(work)

‎packages/agentenv-protocol/src/agentenv_protocol/website_browser/shapes.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class ClosedTab(BaseModel):
9797

9898

9999
class Download(BaseModel):
100-
"""A download into ``~/Downloads``; completed, with where it landed, its size and its sha256."""
100+
"""A download into ``~/Downloads``; when completed, with where it landed, and its size and sha256 when available."""
101101
guid: str
102102
url: str
103103
filename: str

‎packages/agentenv-protocol/tests/test_website_browser.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,27 @@ async def test_its_control_records_a_completed_download_with_its_size_and_sha256
11501150
assert done["sha256"] == hashlib.sha256(b"%PDF-1.7 report").hexdigest()
11511151

11521152

1153+
@pytest.mark.asyncio
1154+
async def test_its_control_records_ended_at_when_download_hashing_fails(tmp_path):
1155+
devtools = StubDevTools()
1156+
control = BrowserControl(devtools, "agent")
1157+
missing_file = tmp_path / "nonexistent.pdf"
1158+
1159+
devtools.send("Browser.downloadWillBegin", {"guid": "g2", "url": "https://a.test/missing.pdf",
1160+
"suggestedFilename": "missing.pdf"})
1161+
devtools.send("Browser.downloadProgress", {"guid": "g2", "state": "completed",
1162+
"filePath": str(missing_file)})
1163+
for _ in range(int(WAIT_S / POLL_S)):
1164+
if "ended_at" in control.downloads.get("g2", {}):
1165+
break
1166+
await asyncio.sleep(POLL_S)
1167+
1168+
done = control.downloads["g2"]
1169+
assert done["state"] == "completed"
1170+
assert "ended_at" in done
1171+
assert "sha256" not in done
1172+
1173+
11531174
@pytest.mark.asyncio
11541175
async def test_its_control_says_the_tab_whose_page_has_focus_is_active_else_the_first_visible_one(tmp_path):
11551176
states = {"s-a": ["hidden", False], "s-b": ["visible", False], "s-c": ["visible", True]}

0 commit comments

Comments
 (0)