Skip to content

feat(attachments): Stream Slack files and loadAttachment, and raise the file cap to 250 MB - #3336

Merged
thomasjiangcy merged 5 commits into
feat/slack-video-attachment-pointerfrom
feat/stream-attachments
Nov 13, 2026
Merged

thomasjiangcy merged 5 commits into
feat/slack-video-attachment-pointerfrom
feat/stream-attachments

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

Stacked on #3333. Retarget to main after #3333 merges.

Attachment files no longer sit whole in function memory. Slack files stream into storage, and loadAttachment streams storage into the sandbox. With that, the file cap goes from 25 MB to 250 MB (MAX_ATTACHMENT_BYTES, renamed from MAX_INPUT_ATTACHMENT_BYTES).

Slack ingest

  • Junior checks Slack's declared size before it downloads. Over 250 MB: no download, and the model gets a note that the file is too large to open.
  • Only an image that can go to the model inline (≤ MAX_MODEL_ATTACHMENT_BYTES) still uses the adapter's buffered fetchData. Every other file streams from url_private with the bot token (streamPrivateSlackFile, same token resolution as downloadPrivateSlackFile) straight into storage.
  • The buffered path stays only for files that cannot stream: a file with in-memory data, a file with no declared size, file id, or URL, or a run with no storage. Real Slack files always have a size, a file id, and a URL.

Store: key, hash, and idempotency

  • AttachmentStorage.put takes { body: ReadableStream, bytes }. bytes must be exact, because a single S3 PUT needs content-length before the body (the payload is UNSIGNED-PAYLOAD). Vercel Blob takes the stream as is. I did not use Blob multipart, because its SDK holds up to 128 MiB of parts in memory.
  • The storage key is still picked before the write (…/attachments/<id>/<writeId>/<filename>). sha256 and the byte count come from a transform while the bytes stream, and are recorded at the end. A body longer than declared fails mid-stream. A shorter one fails the S3 PUT, or fails the check after a Blob/memory put. Then the object is deleted and no row is written.
  • Id: a file with a source (a Slack file id) gets its id from conversation, storage provider, filename, type, and the source. The hash is not part of it. So a Slack file seen again finds its live row before open() downloads anything, which matters at 250 MB. A file with no source (sendFiles, tests) is in memory already, so its id still comes from its sha256, as before. The AttachmentFile type allows a stream only when a source is set.
  • Trade-off: ids of new Slack attachments change from the old formula, so a file stored before this deploy and seen again after it is stored once more under a new id. If someone edits a Slack snippet in place, the stored copy keeps the first version.
  • A revived (purge-marked) row also updates bytes and sha256 from the new write.

loadAttachment

  • It refuses a row over 250 MB before it reads storage (ToolInputError).
  • It reads the storage stream into parts of at most 8 MiB, writes each part to <attachmentDir>.parts/NNNN, and joins them with sh -c 'cat -- "$@" > "$out"'. The paths are passed as arguments, not put into the script. Then it removes the parts and checks the final size with stat. No signed storage URL goes to the sandbox.

Web

The accept path already streams: the browser writes to storage with a signed upload, and verifyAttachmentUploads hashes the object as a stream. So the web cap goes to 250 MB too, and the dashboard copy follows. The API PUT fallback for storage without signed uploads still buffers the body. Only the memory provider uses it.

Tests

  • attachment-media-behavior: a 6 MiB Slack video arrives in 1 MiB chunks. put reads it in those same chunks, fetchData is never called, and a repeat of the same file makes no second download. A file over the cap is refused with no download. These cases call resolveUserAttachments through createJuniorRuntimeServices, because the test-architecture check blocks new agent runs in this file.
  • load-attachment: the tool now runs against a real local directory. A 20 MiB file goes over as 8/8/4 MiB writes and joins back byte for byte. A file over the cap is refused before storage.get. A short read still fails.
  • attachment-s3-storage (versitygw): a streamed put works on both providers. A sourced stream is stored, and bodies of the wrong size fail with no row.
  • The Slack MSW adapter now takes stream bodies and records private file downloads.

Not verified: a streamed put against real Vercel Blob, because I had no Blob credentials here. The SDK accepts a ReadableStream body.

via @thomasjiangcy.

--

View Session [Sentry]

…he file cap to 250 MB

Slack files that do not go to the model inline now stream from Slack into
attachment storage, and loadAttachment streams storage into the sandbox in
8 MiB parts. No path holds a whole file in function memory, so the file cap
moves from 25 MB to 250 MB. Slack files over the cap are refused before the
download.

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 6:28am UTC

Request Review

@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.

…ed loads

Ids of files without a source keep the old field order, so rows stored
before the source-based id still match. loadAttachment removes its parts
and any partial file when a load fails.

@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.

…ter' into feat/stream-attachments

Co-Authored-By: Thomas Jiang <[email protected]>
loadAttachment joins the parts in its parts directory and moves the file
into place only when it is complete. A failed reload removes only its
parts, so the copy from an earlier load stays.

@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 2 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 caaee61. Configure here.

…failed loads

A stream can be read only once, so the S3 client cannot replay it. The
attachment store now retries a failed write with a freshly opened body,
and deletes the key after the last failure. A size mismatch is not
retried. loadAttachment cancels the storage read when it stops early.
@thomasjiangcy
thomasjiangcy merged commit 39243d9 into main Nov 13, 2026
45 checks passed
@thomasjiangcy
thomasjiangcy deleted the feat/stream-attachments branch November 13, 2026 15:07
thomasjiangcy added a commit that referenced this pull request Nov 13, 2026
…ment pointer (#3333)

Slack stored video and audio attachments but then dropped them from the
prompt (`attachment.type` was not `image` or `file`), so Junior never
knew they were there. Other non-image files of 5 MB or less were pasted
into the prompt as base64, cut at 120k chars, which the model cannot
read well. And past three attachments per turn, every further file was
dropped from the prompt without a note.

Now Slack uses the same rule as web chat (#3181): up to three images go
to the model inline, and every other file, plus every image past that
cap or the 5 MB limit, gets the stored-attachment pointer for
`loadAttachment`. If storage fails, the model gets a note that the file
could not be stored, so it does not say nothing was attached. No
attachment is dropped from the prompt anymore.

Behavior change: a small text, PDF, or other non-image Slack file is no
longer inlined. The agent loads it with `loadAttachment`. With that,
nothing produces non-image bytes for the prompt, so
`encodeNonImageAttachmentForPrompt` is removed and the prompt builder
now throws on a non-image attachment that has no prompt text.

Large-file streaming (Slack ingest and `loadAttachment` both buffer the
whole file) is in #3336, stacked on this PR.

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

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3Af2734210229ae28ebcf3b681 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/local%3Aweb%3Af2734210229ae28ebcf3b681)
[[Sentry]](https://sentry.sentry.io/explore/conversations/local%3Aweb%3Af2734210229ae28ebcf3b681/?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]>
thomasjiangcy added a commit that referenced this pull request Nov 13, 2026
…3339)

**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 #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 "<name> 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.

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

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3Af2734210229ae28ebcf3b681 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/local%3Aweb%3Af2734210229ae28ebcf3b681)
[[Sentry]](https://sentry.sentry.io/explore/conversations/local%3Aweb%3Af2734210229ae28ebcf3b681/?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 — 88e7c04d 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