fix(chat): settle authoritative turns and make capabilities explicit
This commit is contained in:
@@ -3,16 +3,41 @@ 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.
|
||||
@@ -24,7 +49,7 @@ const (
|
||||
)
|
||||
|
||||
// prepareChatAttachments writes browser attachment bytes into a private
|
||||
// temporary directory and returns the file references OpenCode accepts plus the
|
||||
// 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) {
|
||||
@@ -34,21 +59,74 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
|
||||
if len(attachments) > maxChatAttachments {
|
||||
return nil, nil, fmt.Errorf("at most %d attachments are allowed", maxChatAttachments)
|
||||
}
|
||||
dir := filepath.Join(os.TempDir(), "pipelines-chat-attachments", safeChatAttachmentSegment(sessionID))
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
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 {
|
||||
_ = os.Remove(path)
|
||||
_ = 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)
|
||||
@@ -63,27 +141,41 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
|
||||
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 := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
||||
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)
|
||||
}
|
||||
created = append(created, path)
|
||||
uri := (&url.URL{Scheme: "file", Path: path}).String()
|
||||
files = append(files, opencode.PromptFile{URI: uri, Name: name})
|
||||
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, Mime: mime, URI: uri})
|
||||
@@ -91,25 +183,6 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
|
||||
return files, parts, nil
|
||||
}
|
||||
|
||||
func safeChatAttachmentSegment(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
value = strings.Map(func(r rune) rune {
|
||||
switch {
|
||||
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '_', r == '-':
|
||||
return r
|
||||
default:
|
||||
return '_'
|
||||
}
|
||||
}, value)
|
||||
if value == "" {
|
||||
return "session"
|
||||
}
|
||||
if len(value) > 80 {
|
||||
value = value[:80]
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func safeChatAttachmentName(value string) string {
|
||||
name := filepath.Base(strings.TrimSpace(value))
|
||||
name = strings.Map(func(r rune) rune {
|
||||
|
||||
Reference in New Issue
Block a user