Skip to content

Your lightweight open-source personal agent.

fix(linear): acknowledge sessions before downloading unique attachments

chengyongru422033af7da6
diff --git a/nanobot/channels/linear/runtime.py b/nanobot/channels/linear/runtime.py
index ba4b727b5..5ef393b8a 100644
--- a/nanobot/channels/linear/runtime.py
+++ b/nanobot/channels/linear/runtime.py
@@ -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))
diff --git a/nanobot/channels/linear/tests/test_linear.py b/nanobot/channels/linear/tests/test_linear.py
index 2aab69394..7d4bd8d88 100644
--- a/nanobot/channels/linear/tests/test_linear.py
+++ b/nanobot/channels/linear/tests/test_linear.py
@@ -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"
+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"]
+
+
+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,