Repository navigation
Closed
Labels
Description
Summary
Enable file exchange between Slack users and Trinity agents.
Requirements
Slack → Agent (inbound) — Phase 1
When a user uploads a file in a Slack message, make it accessible to the agent.
Implemented (POC):
- Parse
filesarray from Slack event payloads (DM, @mention, thread reply) - Download files via Slack API using bot token (
files:readscope required) - Images (
image/*): embed as base64 data URI in prompt — Claude vision processes them directly - Other files (text, CSV, JSON, PDF, etc.): copy into agent container via Docker
put_archiveAPI, addReadto allowed tools - File info appended to context prompt as
[Uploaded files]block - Per-session upload directory (
uploads/{session_id}/) with cleanup after execution
Agent → Slack (outbound) — Phase 2
- When an agent response includes generated files (images, PDFs, documents), attach them to the Slack message
- Use Slack
files.uploadAPI or Block Kit file attachments - Respect file size limits
Technical Findings (from POC)
- Slack
url_private_downloadrequiresfiles:readOAuth scope — without it, returns HTML login page instead of file bytes - Claude Code crashes with no stdout when
--allowedToolsincludesReadand the agent reads a PNG file (API returns 400 "Could not process image"). Process becomes zombie, pipe reader hangs indefinitely. Workaround: pass images as base64 inline instead of copying to container. - Agent server runs single uvicorn worker — zombie Claude processes from failed runs can block subsequent executions for minutes. Restart clears the state.
- Base64 images consume context window (~1.33x file size). Need size caps.
Improvements Needed (before production)
Security
- File path traversal —
f.namefrom Slack could be../../.env. Sanitize: strip path separators, reject.., use basename only. - Base64 prompt injection — Crafted images could contain prompt injection in EXIF/metadata. Low risk (Claude processes as vision), but worth noting.
- Download size validation — Verify downloaded bytes match expected size from Slack metadata. Already have
max_sizeon download, but double-check. - Upload directory isolation —
--allowedTools Readgrants Read to ALL files, not just uploads. Public Slack users could read.env, credentials. Consider restricted Read path or sandboxed directory. - File upload rate limiting — Current rate limiter counts messages only. Add per-user file upload limit (e.g., 5 files/minute) to prevent disk/bandwidth exhaustion.
- Filename collisions — Two users uploading
report.csvsimultaneously overwrite each other. Fix: per-session subdirectory (implemented in POC). - Uploaded file cleanup — Files accumulate forever. Fix: delete per-session directory after execution completes (implemented in POC).
Architecture
- Base64 prompt size — 5MB image = 6.7MB base64 in prompt. Add size tiers: small images inline, large images described by filename only.
- Multiple images cap — 5 screenshots × 3MB = 20MB prompt. Add max total inline size (e.g., 10MB combined), fall back to description for excess.
- PDF handling — PDFs are binary, Claude can't
Readthem meaningfully. Consider converting to text (pdftotext) or treating as base64 for small ones. - Move file download to adapter — Router currently calls
slack_service.download_filedirectly. Should be onChannelAdapterso Telegram/Discord implement their own download. Router receives bytes. ChannelResponsefile support — For outbound (agent → Slack),ChannelResponseneeds afilesfield. Not in scope for inbound but needed for Phase 2.- Allowed tools escalation — Adding
Readfor text files is privilege escalation for public Slack users. Consider agent-server-level restricted Read that only allows/home/developer/uploads/*.
Reliability
- Slack URL expiry —
url_private_downloadURLs expire. Download immediately on event receive, not lazily. Current POC does this correctly. - Docker put_archive failure — If container restarts between file copy and execution, file is lost. Unlikely but handle gracefully.
- Agent server zombie fix — Claude crashes without flushing stdout → pipe reader hangs. Agent server should poll
process.poll()to detect early exit. Pre-existing bug, not file-specific.
Testing
- Unit tests — File extraction from Slack events, filename sanitization, size limits, image vs non-image routing.
- Integration test — Mock Slack event with file → verify file in container → verify prompt includes file reference.
Depends On
- Channel Adapter Abstraction (Channel Adapter Abstraction #63) ✅
- Slack Adapter Hardening (Slack Adapter Hardening #64) ✅
Files Changed (POC)
src/backend/adapters/base.py—FileAttachmentmodel,filesfield onNormalizedMessagesrc/backend/adapters/slack_adapter.py— Extract files from Slack events in all 3 parse methodssrc/backend/adapters/message_router.py— Download, copy/embed, context injection, cleanupsrc/backend/services/slack_service.py—download_file()method,files:readOAuth scopesrc/backend/services/docker_utils.py—container_put_archive()async wrapper
Future Improvements
PDF Support
Currently PDFs are rejected with a user-friendly message. Future options:
- Convert PDF to text via
pdftotextin the agent container, then pass as readable file - For small PDFs, embed as base64 and rely on Claude vision
- For large PDFs, extract text and inject into context prompt
File Lifecycle After Execution
Files are deleted from the agent container immediately after execution completes. This is acceptable because:
- Claude Code's session context retains the file content from when it was read
- The public chat session history includes previous messages, so follow-up questions have context
- Re-uploading is trivial if the agent needs to re-read the raw file
Edge case: user says "read line 50" in a follow-up — agent would need the file on disk again. Acceptable trade-off vs disk accumulation.
Activity
feat: Bidirectional file sharing — Slack to agent (inbound) (#222)
docs: Add requirements, feature flow for Slack file sharing (#222)
feat: Slack inbound file sharing — images via vision, text files via …
feat: Slack inbound file sharing — images via vision, text files via …
feat(channels): file upload Phase 2 — workspace delivery hardening (#487
docs(planning): #408 dissolution gated on #428, not #306 (re-audit) (#…
Release: v0.5.0 (#608)