fix(sandbox): stage chat attachments by resolving their storage ref from the temporary document
MessageAttachment.URL is json:"-" so it never survives DB serialization, but stageSessionAttachments depended on it to read the source file, which silently dropped every attachment before it could be written into /workspace/input. Resolve the storage handle from the temporary_documents row keyed by attachment.ID (GetScoped) when the URL is missing, and pass the session tenant through staging so shared-agent sessions look up the right row.main
parent
2523dc7276
commit
3660d5f9a8
|
|
@ -171,7 +171,7 @@ func (s *sessionService) AgentQA(
|
|||
if loadErr != nil {
|
||||
return fmt.Errorf("load session attachments for sandbox staging: %w", loadErr)
|
||||
}
|
||||
stagedAttachments, err = stager.stageSessionAttachments(ctx, sessionID, agentConfig.SandboxConfigID, sessionAttachments)
|
||||
stagedAttachments, err = stager.stageSessionAttachments(ctx, sessionID, agentConfig.SandboxConfigID, req.Session.TenantID, sessionAttachments)
|
||||
if err != nil {
|
||||
return fmt.Errorf("restore session attachments into sandbox: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/application/repository"
|
||||
"github.com/Tencent/WeKnora/internal/sandbox"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
|
|
@ -62,6 +63,7 @@ type sessionAttachmentStager interface {
|
|||
) (sandbox.SessionFileStore, error)
|
||||
stageSessionAttachments(
|
||||
ctx context.Context, sessionID, agentSandboxConfigID string,
|
||||
tenantID uint64,
|
||||
attachments types.MessageAttachments,
|
||||
) ([]stagedSessionAttachment, error)
|
||||
}
|
||||
|
|
@ -100,6 +102,7 @@ func (s *agentService) stageSessionAttachments(
|
|||
ctx context.Context,
|
||||
sessionID string,
|
||||
agentSandboxConfigID string,
|
||||
tenantID uint64,
|
||||
attachments types.MessageAttachments,
|
||||
) ([]stagedSessionAttachment, error) {
|
||||
if s == nil {
|
||||
|
|
@ -116,7 +119,11 @@ func (s *agentService) stageSessionAttachments(
|
|||
return nil, fmt.Errorf("file service is unavailable for session input staging")
|
||||
}
|
||||
|
||||
attachments = deduplicateSessionAttachments(attachments)
|
||||
resolved, err := s.resolveSessionAttachmentURLs(ctx, tenantID, sessionID, attachments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
attachments = deduplicateSessionAttachments(resolved)
|
||||
existingEntries, err := store.ListSessionFiles(ctx, sessionID, sandbox.SessionInputRoot)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list staged session inputs: %w", err)
|
||||
|
|
@ -180,6 +187,45 @@ func (s *agentService) stageSessionAttachments(
|
|||
return staged, nil
|
||||
}
|
||||
|
||||
// resolveSessionAttachmentURLs fills in the storage handle for attachments
|
||||
// whose URL was not persisted on the message row. MessageAttachment.URL is
|
||||
// deliberately excluded from DB serialization (json:"-") so a cross-session
|
||||
// downloadable reference cannot leak; the temporary-document row keyed by
|
||||
// attachment.ID is the authoritative source of the storage reference.
|
||||
// Attachments that already carry a URL, or that have no temporary-document ID,
|
||||
// pass through unchanged so callers with other attachment sources keep working.
|
||||
func (s *agentService) resolveSessionAttachmentURLs(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
sessionID string,
|
||||
attachments types.MessageAttachments,
|
||||
) (types.MessageAttachments, error) {
|
||||
if len(attachments) == 0 || s == nil || s.db == nil {
|
||||
return attachments, nil
|
||||
}
|
||||
repo := repository.NewTemporaryDocumentRepository(s.db)
|
||||
out := make(types.MessageAttachments, 0, len(attachments))
|
||||
for _, attachment := range attachments {
|
||||
if strings.TrimSpace(attachment.URL) != "" || strings.TrimSpace(attachment.ID) == "" {
|
||||
out = append(out, attachment)
|
||||
continue
|
||||
}
|
||||
document, err := repo.GetScoped(ctx, tenantID, sessionID, attachment.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolve attachment %q storage reference: %w", attachment.FileName, err)
|
||||
}
|
||||
if document == nil || strings.TrimSpace(document.ResourceRef) == "" {
|
||||
// Expired or already-deleted temporary document: leave the URL
|
||||
// empty so deduplicateSessionAttachments skips it instead of
|
||||
// failing the whole staging pass.
|
||||
continue
|
||||
}
|
||||
attachment.URL = document.ResourceRef
|
||||
out = append(out, attachment)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func deduplicateSessionAttachments(attachments types.MessageAttachments) types.MessageAttachments {
|
||||
seen := make(map[string]struct{}, len(attachments))
|
||||
out := make(types.MessageAttachments, 0, len(attachments))
|
||||
|
|
|
|||
|
|
@ -7,11 +7,14 @@ import (
|
|||
"mime/multipart"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/sandbox"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type stagingFileService struct {
|
||||
|
|
@ -132,6 +135,7 @@ func TestStageSessionAttachmentsReconcilesAndSkipsExisting(t *testing.T) {
|
|||
ctx,
|
||||
"session-1",
|
||||
"cfg-remote",
|
||||
7,
|
||||
types.MessageAttachments{attachment, attachment},
|
||||
)
|
||||
|
||||
|
|
@ -144,7 +148,7 @@ func TestStageSessionAttachmentsReconcilesAndSkipsExisting(t *testing.T) {
|
|||
assert.NotContains(t, manager.files, stalePath)
|
||||
|
||||
// The second reconciliation sees the same path and size and avoids storage IO.
|
||||
_, err = service.stageSessionAttachments(ctx, "session-1", "cfg-remote", types.MessageAttachments{attachment})
|
||||
_, err = service.stageSessionAttachments(ctx, "session-1", "cfg-remote", 7, types.MessageAttachments{attachment})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, fileService.getCalls[attachment.URL])
|
||||
}
|
||||
|
|
@ -159,7 +163,7 @@ func TestStageSessionAttachmentsSkipsWhenNoFilesystemCapability(t *testing.T) {
|
|||
}
|
||||
service := &agentService{sandboxMgr: manager, fileService: &stagingFileService{}}
|
||||
|
||||
staged, err := service.stageSessionAttachments(context.Background(), "session-1", "", types.MessageAttachments{{
|
||||
staged, err := service.stageSessionAttachments(context.Background(), "session-1", "", 7, types.MessageAttachments{{
|
||||
URL: "local://tenant/file", FileName: "file.txt",
|
||||
}})
|
||||
|
||||
|
|
@ -177,3 +181,63 @@ func TestBuildSandboxAttachmentsPromptEscapesMetadata(t *testing.T) {
|
|||
assert.Contains(t, prompt, `path="/workspace/input/hash/a.txt"`)
|
||||
assert.Contains(t, prompt, "read-only inputs")
|
||||
}
|
||||
|
||||
func TestStageSessionAttachmentsResolvesURLFromTemporaryDocument(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&types.TemporaryDocument{}))
|
||||
|
||||
docID := "doc-1"
|
||||
resourceRef := "local://tenant/attachment-1"
|
||||
require.NoError(t, db.Create(&types.TemporaryDocument{
|
||||
ID: docID,
|
||||
TenantID: 7,
|
||||
SessionID: "session-1",
|
||||
ResourceRef: resourceRef,
|
||||
FileName: "report.pdf",
|
||||
FileType: ".pdf",
|
||||
FileSize: 7,
|
||||
Status: types.TemporaryDocumentStatusReady,
|
||||
ExpiresAt: time.Now().Add(time.Hour),
|
||||
}).Error)
|
||||
|
||||
// The message row persists the temporary-document ID but NOT the URL
|
||||
// (MessageAttachment.URL is json:"-"). Staging must recover it from the
|
||||
// temporary_documents table and still stage the file into the sandbox.
|
||||
attachment := types.MessageAttachment{
|
||||
ID: docID,
|
||||
FileName: "report.pdf",
|
||||
FileType: ".pdf",
|
||||
FileSize: 7,
|
||||
}
|
||||
remotePath, err := sandboxAttachmentPath(types.MessageAttachment{
|
||||
URL: resourceRef, FileName: "report.pdf",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
manager := &stagingSandboxManager{sandboxType: sandbox.SandboxTypeCube}
|
||||
fileService := &stagingFileService{
|
||||
files: map[string][]byte{resourceRef: []byte("content")},
|
||||
}
|
||||
service := &agentService{
|
||||
db: db,
|
||||
sandboxMgr: manager,
|
||||
fileService: fileService,
|
||||
sandboxResolver: stubSandboxResolver{mgr: manager},
|
||||
}
|
||||
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(7))
|
||||
|
||||
staged, err := service.stageSessionAttachments(
|
||||
ctx,
|
||||
"session-1",
|
||||
"cfg-remote",
|
||||
7,
|
||||
types.MessageAttachments{attachment},
|
||||
)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, staged, 1)
|
||||
assert.Equal(t, remotePath, staged[0].Path)
|
||||
assert.Equal(t, []string{remotePath}, manager.writes)
|
||||
assert.Equal(t, 1, fileService.getCalls[resourceRef])
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue