Skip to content

feat(attachments): Stream large Slack files instead of storing them - #3339

Merged
thomasjiangcy merged 6 commits into
feat/stream-attachmentsfrom
feat/lazy-slack-attachments
Nov 13, 2026
Merged

thomasjiangcy merged 6 commits into
feat/stream-attachmentsfrom
feat/lazy-slack-attachments

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Stack: 3rd of 3 (#3333 → #3336 → this PR). This PR stacks on #3336 because it reuses #3336's Slack-file-id attachment ids, its streaming Slack download, the 8 MiB part writes in loadAttachment, and the 250 MiB cap. The three PRs should merge together.

Before this PR, every Slack file streamed into storage when the message arrived, even when Junior never opened it. A 200 MB recording cost download time before the turn started, plus storage and function time.

What changes

  • Ingest: a non-image Slack file over MAX_EAGER_SLACK_FILE_BYTES (25 MiB, in chat/slack/vision-context.ts) gets a metadata-only row: Slack file id (source), filename, media type, and declared size. It is never downloaded at ingest and never stored. The model gets the same loadAttachment pointer as before. Images and smaller files are stored at ingest, as before.
  • loadAttachment: every load streams the file Slack → host → sandbox. files.info returns the current URL and size, and the 250 MiB cap is checked against that size before the download. The bytes go to the sandbox in the same 8 MiB parts as feat(attachments): Stream Slack files and loadAttachment, and raise the file cap to 250 MB #3336, joined in a scratch directory, size-checked, then moved into place; a failure removes the parts. Nothing is written to storage, no sha256 is recorded, and the row does not change. The bot token stays on the host: the sandbox gets bytes only, never a Slack URL or token.
  • Dashboard: the row says "Too large to preview" and has a Download button. The button loads GET …/attachments/:id?download=true in a hidden frame, and the route streams the file Slack → host → browser with Content-Disposition: attachment. A saved file loads no document in the frame. An error response loads in the frame, not over the conversation, and the row then sends HEAD (Slack files.info only, no download) and shows the server's error inline. There is no POST route and no "Preparing download…" step.
  • Sign-in: streaming one of these files needs a signed-in viewer, even in a public conversation. Each read downloads up to 250 MiB with the bot token. Anonymous HEAD/GET get 401 and make no Slack call. Stored files keep the byte route's normal public access.
  • Deleted or unshared file: files.info file_not_found/file_deleted, a tombstone with no URL, or a 403/404/410 on the download all become " is no longer available: The Slack file was deleted, or Junior can no longer access it." loadAttachment returns it as ToolInputError; the byte route returns it as 409. The turn keeps going.
  • Shared code: readAttachmentContent (chat/attachments/download.ts) finds the bytes of one row: storage for a stored file, the source for a large Slack file. It checks the cap before open downloads anything. loadAttachment and the byte route both use it.
  • Slack code location: the download stays in core Slack modules (chat/slack/client.ts readPrivateSlackFile, chat/slack/attachments.ts readSlackAttachmentSource). The attachment core sees only a provider-neutral ReadAttachmentSource (chat/attachments/source.ts). createTools passes the Slack reader to loadAttachment only in Slack conversations. The byte route binds the conversation's Slack installation (runWithSlackInstallation, team from juniorDestinations.providerTenantId).

Security rule

Every Slack download starts from an attachment row that ingest created in that conversation. loadAttachment and the byte route look the row up by attachment id and conversation id, and the Slack file id comes only from that row. Neither accepts a raw Slack file id or URL. A row from another conversation, a raw Slack file id, and an unknown id all get the normal not-found error and cause no Slack call (tested on both paths).

Schema change

Migration 0107_attachment_deferred_download.sql makes junior_attachments.storage_key and sha256 nullable (DROP NOT NULL ×2; no table rewrite). A null storage_key means "never stored". There is no new status column and no team or installation column: loadAttachment runs inside the conversation's Slack turn, and the byte route resolves the team from the conversation's root Location. junior_attachment_uploads does not change.

Consumers of rows without an object

  • readLiveAttachment returns source and a nullable storageKey; AttachmentRecord.storageKey is string | null.
  • Byte route: streams from Slack (no range support, so players cannot seek; the dashboard shows these as file rows, not players).
  • Transcript previews: matchSlackAttachments sets pending: true (optional field on messageAttachmentSchema), and TranscriptAttachment.tsx renders the Download row.
  • Model input (readStoredBytes) treats such a row as unavailable. Only images reach this path, and images are never deferred.
  • Retention and GC: the normal 30-day GC deletes these rows; collectAttachmentGarbage deletes only keys that are not null. Purge marking does not change.
  • sendFiles and artifacts always write bytes, so they never create these rows.

Idempotency

The id comes from the Slack file id (#3336). A Slack file seen twice maps to one row, and later messages reuse it.

Also fixed

getSlackFileId used a case-insensitive regex. On real Slack URLs (/files-pri/T…-F…/name), it returned files as the file id. The Chat SDK adapter does not set fetchMetadata.fileId, so this fallback is the path production uses. With #3336's source-based ids, two different Slack files with the same name and type in one conversation would get the same attachment id. The regex is now case-sensitive. The ingest test uses a real-shaped URL and asserts the stored provider_id.

Trade-offs

  • Every load and every dashboard download hits Slack. That is the point: no storage cost. Slack rate limits and availability now apply on each read.
  • No preview and no seeking for these files in the dashboard.
  • The request holds the stream. A dashboard download of up to 250 MiB must finish inside the 300 s function limit.
  • Bytes can change: if the file was replaced in Slack, each read gets the current file, and its size comes from files.info.
  • Rollback: code from before this PR reading such a row would call storage.get(null) and fail on that one attachment. Rows with objects are not affected.

Tests

Extended existing scenarios through real wiring (MSW Slack, Postgres, versitygw):

  • slack/attachment-media-behavior: a 25 MiB + 1 B video is not fetched at ingest (no fetchData, no download, no object). A repeat gets the same id. The row has storage_key null and the real Slack file id.
  • tools/load-attachment: two loads make two files.info calls and two downloads, the bytes match, storage stays empty, and the row keeps storage_key/sha256 null. Sandbox commands contain no Slack URL or token. A file that grew past the cap is refused before download. file_not_found and a 404 download each give the tool error and write nothing. A row from another conversation, a raw Slack file id, and an unknown id give "Attachment not found" with no Slack call.
  • attachment-s3-storage: a large Slack file gets a row with no object, and the 30-day GC deletes its row next to a stored file's object and row.
  • api/conversations/attachments: the transcript shows pending: true. HEAD calls files.info only. Two GETs stream the bytes with attachment disposition and make two downloads; nothing is stored. A deleted file gives 409 with the message on HEAD and GET. A viewer without access gets 404, and an anonymous viewer of a public conversation gets 401. A row from another conversation, a raw Slack file id, and an unknown id get 404 with no Slack call.
  • Dashboard e2e conversation-attachments.spec.ts: both large rows show "Too large to preview". Download saves standup-recording.mp4 from ?download=true, the page stays on the conversation, and the saved bytes match the streamed response. The deleted file shows its error inline, and the page stays on the conversation.

pnpm typecheck and pnpm lint pass. The affected suites pass (42 tests), plus dashboard unit tests (453) and the attachment e2e (2). In the full @sentry/junior suite, 3567 passed; 2 unrelated tests (agent-invocation-limits, any-resource-event-automations) timed out under load and pass on rerun.

via @thomasjiangcy.

--

View Session [Sentry]

Non-image Slack files over 25 MiB are recorded without their bytes when the
message arrives. The first loadAttachment reads the file from Slack
(files.info, then a streamed download into storage), and later loads reuse
the stored copy. A deleted or unshared Slack file becomes a tool error.

junior_attachments.storage_key and sha256 become nullable. A null key marks
a file that has not been downloaded yet.

Also match Slack file ids in URLs case-sensitively, so the /files-pri/
segment is no longer read as the file id.

Co-Authored-By: Thomas Jiang <[email protected]>
@vercel

vercel Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 13, 2026 1:59pm UTC

Request Review

A pending Slack file row now says "Too large to preview" and has a Download
button. The button posts to a new route that runs the same first load as
loadAttachment (ensureAttachmentStored) inside the conversation's Slack
installation, then the browser downloads the stored copy from the byte
route. A deleted or unshared file shows the loadAttachment error inline.

The route is a POST so a link prefetch cannot start a 250 MB download. It
needs a signed-in viewer who can see the conversation.

Move the attachment e2e journeys to conversation-attachments.spec.ts so
conversations.spec.ts stays under the file length limit.

Co-Authored-By: Thomas Jiang <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

The save runs after the prepare request, without user activation, so a
target=_blank click can be blocked as a popup. Use a same-window download
link instead. An installed home screen app skips the automatic save and
uses the refreshed row's Download link.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

The byte route sends videos and images inline. Firefox and Safari can
ignore the download attribute then, so the same-window save replaced
the conversation with the video. The byte route now takes ?download=true
to send Content-Disposition: attachment, and the prepared save uses it.
Non-image Slack files over MAX_EAGER_SLACK_FILE_BYTES stay metadata-only
rows. Storing them cost storage for files that are rarely opened.

- loadAttachment streams them Slack -> host -> sandbox on every load, in
  the same 8 MiB parts with scratch-then-move and cleanup. The 250 MiB cap
  is checked against files.info before the download. Nothing is written
  to storage and the row does not change. The sandbox gets bytes only.
- The dashboard byte route streams them Slack -> browser with the
  conversation's Slack installation. HEAD checks the file without a
  download, so the row can show a deleted file inline. The POST prepare
  route and the Preparing download step are gone.
- Every Slack download starts from a row that the conversation-scoped
  lookup returns. A row from another conversation, a raw Slack file id,
  or an unknown id gets the normal not-found error and no Slack call.
- Remove storeDeferredAttachment, its race handling, and the pending
  version fingerprint.

Co-Authored-By: Thomas Jiang <[email protected]>
@sentry-junior sentry-junior Bot changed the title feat(attachments): Download large Slack files on first loadAttachment feat(attachments): Stream large Slack files instead of storing them Nov 13, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 3 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c701163. Configure here.

Bugbot review on #3339:

- The dashboard loads a large Slack file in a hidden frame instead of a
  same-window link. A saved file loads no document. An error response
  loads in the frame, not over the conversation, and the row then asks
  the server for the reason. This also drops the HEAD before each
  download, so a file that fails only on the Slack download still
  shows an error.
- Streaming a Slack file now needs a signed-in viewer, also in a public
  conversation, because each read downloads up to the cap with the bot
  token. Anonymous HEAD and GET get 401 and make no Slack call.
@thomasjiangcy
thomasjiangcy merged commit 3ea3251 into main Nov 13, 2026
45 of 46 checks passed
@thomasjiangcy
thomasjiangcy deleted the feat/lazy-slack-attachments branch November 13, 2026 15:07
thomasjiangcy added a commit that referenced this pull request Nov 16, 2026
Large Slack files (non-image, over 25 MiB) are recorded with
`storage_key = NULL` and were never stored (#3339). Now
the first call that needs the bytes streams the file from Slack into
attachment storage once and fills in `storage_key`, `sha256` and
`bytes`. Later reads use storage. So `transcribeAudio`
(#3489) can sign a storage URL with `createDownloadUrl`
(#3488) for every file.

**Behavior**
- `storeDeferredAttachment` (`store.ts`) streams the source through the
existing `writeAttachmentObject` path (fresh write key, retries, size
check, sha256). Then it updates the row only where `storage_key IS
NULL`, the row is not delete-requested, and the conversation is not
purged. When the update misses, it deletes our object and returns the
winner's row, or null when the row is no longer live.
- `storeAttachmentOnFirstUse` (`download.ts`) does the Slack lookup and
the size check, maps errors to `AttachmentDownloadError`, and wraps the
store in an `attachment.store` span.
- `loadAttachment` and `transcribeAudio` call it first.
`transcribeAudio` no longer refuses unstored files.
- `writeAttachmentObject` no longer retries
`AttachmentSourceUnavailableError`. A file that Slack deleted is not
transient.
- The dashboard byte route does **not** store. It keeps streaming files
from Slack until a tool stores them.

**Notes**
- No migration: `0107_attachment_deferred_download.sql` already made
`storage_key` and `sha256` nullable.
- `model-input.ts` is unchanged except for a comment. Only web uploads
reach it, and they are always stored. It reads bytes only for images up
to 5 MB, and deferred rows are non-images over 25 MiB.

**Open risk**
- Nobody has checked yet that ElevenLabs can fetch a Vercel Blob signed
URL in production.

<!-- junior-request-attribution:start -->
via @thomasjiangcy.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A71078a65e4a20ded28d6e80cfc50ba2d -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A71078a65e4a20ded28d6e80cfc50ba2d)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A71078a65e4a20ded28d6e80cfc50ba2d/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Thomas Jiang <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 9ec09043 Deployed Nov 13, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant