Files
pipelines/internal/server/chat_attachments.go
T

207 lines
6.3 KiB
Go

package server
import (
"encoding/base64"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Files survive process restarts and asynchronous harness inbox admission.
// Only unleased files older than seven days are collected, on the next upload.
const chatAttachmentRetention = 7 * 24 * time.Hour
const chatAttachmentQuota = 64 << 20
const chatAttachmentFileQuota = 512
var chatAttachmentStore = struct {
sync.Mutex
leased map[string]bool
}{leased: make(map[string]bool)}
func releaseChatAttachments(files []opencode.PromptFile) {
chatAttachmentStore.Lock()
defer chatAttachmentStore.Unlock()
for _, file := range files {
u, err := url.Parse(file.URI)
if err != nil || !chatAttachmentStore.leased[u.Path] {
continue
}
delete(chatAttachmentStore.leased, u.Path)
}
}
const (
// maxChatAttachments bounds one prompt's browser files. OpenCode receives a
// small number of local files; more is not a useful chat message.
maxChatAttachments = 8
// maxChatAttachmentBytes bounds the decoded bytes for one prompt. The browser
// sends base64 over the existing 2 MiB websocket frame, so this keeps the
// encoded body comfortably below that hard limit.
maxChatAttachmentBytes = 1 << 20
)
// prepareChatAttachments writes browser attachment bytes into a private
// durable directory and returns the file references OpenCode accepts plus the
// display parts the user turn carries. A browser File reference is not readable
// by the agent service, so the bytes must cross the websocket.
func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachment) ([]opencode.PromptFile, []protocol.ChatPart, error) {
if len(attachments) == 0 {
return nil, nil, nil
}
if len(attachments) > maxChatAttachments {
return nil, nil, fmt.Errorf("at most %d attachments are allowed", maxChatAttachments)
}
dir := os.Getenv("PIPELINES_CHAT_ATTACHMENT_DIR")
if dir == "" {
home, err := os.UserHomeDir()
if err != nil {
return nil, nil, err
}
dir = filepath.Join(home, ".pipelines-chat-attachments")
}
if !filepath.IsAbs(dir) {
return nil, nil, fmt.Errorf("chat attachment storage must be an absolute path")
}
dir = filepath.Clean(dir)
chatAttachmentStore.Lock()
defer chatAttachmentStore.Unlock()
if err := os.Mkdir(dir, 0o700); err != nil && !os.IsExist(err) {
return nil, nil, fmt.Errorf("prepare attachment directory: %w", err)
}
info, err := os.Lstat(dir)
if err != nil || !info.IsDir() || info.Mode().Perm()&0o077 != 0 {
return nil, nil, fmt.Errorf("attachment storage must be a private directory, not a symlink")
}
root, err := os.OpenRoot(dir)
if err != nil {
return nil, nil, err
}
defer root.Close()
actual, err := root.Stat(".")
if err != nil || !os.SameFile(info, actual) {
return nil, nil, fmt.Errorf("chat attachment storage changed while opening")
}
entries, err := fs.ReadDir(root.FS(), ".")
if err != nil {
return nil, nil, err
}
var stored int64
var storedFiles int
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return nil, nil, err
}
if !info.Mode().IsRegular() {
continue
}
if !chatAttachmentStore.leased[filepath.Join(dir, entry.Name())] && time.Since(info.ModTime()) > chatAttachmentRetention {
if err := root.Remove(entry.Name()); err == nil {
continue
}
}
stored += info.Size()
storedFiles++
}
var files []opencode.PromptFile
var parts []protocol.ChatPart
var created []string
cleanup := func() {
for _, path := range created {
_ = root.Remove(filepath.Base(path))
delete(chatAttachmentStore.leased, path)
}
}
total := 0
for index, attachment := range attachments {
data := strings.TrimSpace(attachment.Data)
if len(data) > base64.StdEncoding.EncodedLen(maxChatAttachmentBytes) {
cleanup()
return nil, nil, fmt.Errorf("attachments exceed the 1 MiB limit")
}
if data == "" {
cleanup()
return nil, nil, fmt.Errorf("attachment %q has no data", attachment.Name)
}
decoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
cleanup()
return nil, nil, fmt.Errorf("attachment %q is not valid base64", attachment.Name)
}
total += len(decoded)
if total > maxChatAttachmentBytes {
cleanup()
return nil, nil, fmt.Errorf("attachments exceed the %s limit", "1 MiB")
}
if stored+int64(total) > chatAttachmentQuota {
cleanup()
return nil, nil, fmt.Errorf("chat attachment storage quota exceeded (64 MiB); retry after retention cleanup")
}
if storedFiles+index+1 > chatAttachmentFileQuota {
cleanup()
return nil, nil, fmt.Errorf("chat attachment storage quota exceeded (512 files)")
}
name := safeChatAttachmentName(attachment.Name)
mime := strings.TrimSpace(attachment.Mime)
if mime == "" {
mime = "application/octet-stream"
}
path := filepath.Join(dir, fmt.Sprintf("%d-%02d-%s", time.Now().UnixNano(), index, name))
file, err := root.OpenFile(filepath.Base(path), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
cleanup()
return nil, nil, fmt.Errorf("write attachment %q: %w", name, err)
}
created = append(created, path)
chatAttachmentStore.leased[path] = true
if _, err := file.Write(decoded); err != nil {
_ = file.Close()
cleanup()
return nil, nil, fmt.Errorf("write attachment %q: %w", name, err)
}
if err := file.Sync(); err != nil {
_ = file.Close()
cleanup()
return nil, nil, fmt.Errorf("sync attachment %q: %w", name, err)
}
if err := file.Close(); err != nil {
cleanup()
return nil, nil, fmt.Errorf("close attachment %q: %w", name, err)
}
uri := (&url.URL{Scheme: "file", Path: path}).String()
files = append(files, opencode.NewPreparedPromptFile(uri, name))
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, Mime: mime, URI: uri})
}
return files, parts, nil
}
func safeChatAttachmentName(value string) string {
name := filepath.Base(strings.TrimSpace(value))
name = strings.Map(func(r rune) rune {
if r == 0 || r == '/' || r == '\\' {
return -1
}
return r
}, name)
name = strings.TrimSpace(name)
if name == "" || name == "." {
name = "attachment"
}
if len(name) > 120 {
ext := filepath.Ext(name)
if len(ext) > 16 {
ext = ""
}
name = name[:120-len(ext)] + ext
}
return name
}