fix(linear): acknowledge sessions before downloading unique attachments

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-23 18:29:52 +08:00
1 parent b4b7d340cf
commit 422033af7d
2 files changed
+60 -7

No files matched your search

+6 -6
View File
@@ -403,11 +403,6 @@ class LinearChannel(BaseChannel):
return
media: list[str] = []
if self.is_allowed(sender_id):
media, attachment_warnings = await self._download_prompt_media(
organization_id, content, delivery_id
)
if attachment_warnings:
content = "\n\n".join((content, *attachment_warnings))
await self._create_activity(
agent_session_id,
metadata,
@@ -415,6 +410,11 @@ class LinearChannel(BaseChannel):
key="accepted",
ephemeral=True,
)
media, attachment_warnings = await self._download_prompt_media(
organization_id, content, delivery_id
)
if attachment_warnings:
content = "\n\n".join((content, *attachment_warnings))
await self._handle_message(
sender_id=sender_id,
chat_id=agent_session_id,
@@ -467,7 +467,7 @@ class LinearChannel(BaseChannel):
remaining_bytes -= len(body)
name = _download_filename(url, labeled.get(url, ""), content_type, index)
destination = get_media_dir("linear") / safe_filename(
f"{delivery_id}_{name}"
f"{delivery_id}_{index + 1}_{name}"
)
await asyncio.to_thread(destination.write_bytes, body)
media.append(str(destination))
+54 -1
View File
@@ -793,10 +793,63 @@ async def test_created_session_downloads_private_linear_attachments(
inbound = await channel.bus.consume_inbound()
assert client.downloads == ["https://uploads.linear.app/private/image-1"]
assert len(inbound.media) == 1
assert Path(inbound.media[0]).name == "delivery-media_checkout.png"
assert Path(inbound.media[0]).name == "delivery-media_1_checkout.png"
assert Path(inbound.media[0]).read_bytes() == b"image"
@pytest.mark.asyncio
async def test_same_named_attachments_keep_distinct_contents(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
) -> None:
channel, client = _runtime(tmp_path)
monkeypatch.setattr(linear_runtime, "get_media_dir", lambda _channel: tmp_path)
monkeypatch.setattr(client, "download_file", AsyncMock(side_effect=[
(b"first image", "image/png"), (b"second image", "image/png"),
]))
payload = _agent_webhook()
payload["promptContext"] = (
"Compare ![image.png](https://uploads.linear.app/private/one) "
"with ![image.png](https://uploads.linear.app/private/two)"
)
await channel._process_webhook("same-names", payload) # pyright: ignore[reportPrivateUsage]
inbound = await channel.bus.consume_inbound()
assert len(set(inbound.media)) == 2
assert [Path(path).read_bytes() for path in inbound.media] == [b"first image", b"second image"]
@pytest.mark.asyncio
async def test_start_activity_precedes_blocked_attachment_download(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
) -> None:
channel, client = _runtime(tmp_path)
monkeypatch.setattr(linear_runtime, "get_media_dir", lambda _channel: tmp_path)
download_started = asyncio.Event()
release_download = asyncio.Event()
async def download(*_args: object, **_kwargs: object) -> tuple[bytes, str]:
download_started.set()
await release_download.wait()
return b"image", "image/png"
monkeypatch.setattr(client, "download_file", download)
payload = _agent_webhook()
payload["promptContext"] = "Review https://uploads.linear.app/private/image"
task = asyncio.create_task(
channel._process_webhook("slow-download", payload) # pyright: ignore[reportPrivateUsage]
)
try:
await asyncio.wait_for(download_started.wait(), timeout=5)
assert client.activities[0]["content"] == {"type": "thought", "body": "Starting…"}
assert channel.bus.inbound.empty()
finally:
release_download.set()
await asyncio.wait_for(task, timeout=5)
assert len((await channel.bus.consume_inbound()).media) == 1
@pytest.mark.asyncio
async def test_unpaired_session_does_not_download_private_attachments(
tmp_path: Path,