mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 20:01:51 +08:00
A reporter chat bound to one task ran commands in that task's sandbox, while the chat page kept querying the empty session container. Save the task or session target on the conversation and point the badge, uploads, and later turns at it.
129 lines
4.1 KiB
Go
129 lines
4.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/artex/internal/sandbox"
|
|
)
|
|
|
|
// sandboxTargetForTrigger matches the container a triggered run actually executes
|
|
// in. A reporter bound to one task reuses that task's sandbox. Merged multi-task
|
|
// runs and every other agent keep a session container.
|
|
func sandboxTargetForTrigger(agentKey string, taskID int64, multiTask bool, convID int64) sandbox.Target {
|
|
if agentKey == "reporter" && taskID > 0 && !multiTask {
|
|
return sandbox.Target{Scope: sandbox.ScopeTask, OwnerID: strconv.FormatInt(taskID, 10)}
|
|
}
|
|
return sandbox.Target{Scope: sandbox.ScopeSession, OwnerID: "conv-" + strconv.FormatInt(convID, 10)}
|
|
}
|
|
|
|
func storedConversationSandbox(c *db.Conversation) (string, string, bool) {
|
|
if c == nil {
|
|
return "", "", false
|
|
}
|
|
scope := strings.TrimSpace(c.SandboxScope)
|
|
owner := strings.TrimSpace(c.SandboxOwnerID)
|
|
if (scope == string(sandbox.ScopeTask) || scope == string(sandbox.ScopeSession)) && owner != "" {
|
|
return scope, owner, true
|
|
}
|
|
return "", "", false
|
|
}
|
|
|
|
// resolveConversationSandbox prefers a binding already saved on the conversation.
|
|
// fallbackTaskID is used only when nothing is saved and every linked finding
|
|
// belongs to that one task. hasFallback distinguishes "no row" from task 0.
|
|
func resolveConversationSandbox(c *db.Conversation, fallbackTaskID int64, hasFallback bool) (string, string) {
|
|
if scope, owner, ok := storedConversationSandbox(c); ok {
|
|
return scope, owner
|
|
}
|
|
if c == nil {
|
|
return "", ""
|
|
}
|
|
if hasFallback && fallbackTaskID > 0 {
|
|
return string(sandbox.ScopeTask), strconv.FormatInt(fallbackTaskID, 10)
|
|
}
|
|
return string(sandbox.ScopeSession), "conv-" + strconv.FormatInt(c.ID, 10)
|
|
}
|
|
|
|
// fillConversationSandboxes writes the sandbox the chat badge should query.
|
|
// Saved bindings win. Older reporter threads that never stored one fall back to
|
|
// the single task that owns their findings.
|
|
func (s *Server) fillConversationSandboxes(cs []*db.Conversation) {
|
|
if len(cs) == 0 || s == nil || s.m == nil || s.m.pg == nil {
|
|
return
|
|
}
|
|
pending := make([]int64, 0, len(cs))
|
|
for _, c := range cs {
|
|
if _, _, ok := storedConversationSandbox(c); !ok && c != nil {
|
|
pending = append(pending, c.ID)
|
|
}
|
|
}
|
|
fallbacks := map[int64]int64{}
|
|
if len(pending) > 0 {
|
|
got, err := s.m.pg.ReportTaskSandboxFallbacks(pending)
|
|
if err != nil {
|
|
log.Printf("[conv] sandbox fallback: %v", err)
|
|
} else if got != nil {
|
|
fallbacks = got
|
|
}
|
|
}
|
|
for _, c := range cs {
|
|
if c == nil {
|
|
continue
|
|
}
|
|
taskID, ok := fallbacks[c.ID]
|
|
scope, owner := resolveConversationSandbox(c, taskID, ok)
|
|
c.SandboxScope = scope
|
|
c.SandboxOwnerID = owner
|
|
}
|
|
}
|
|
|
|
func (s *Server) fillConversationSandbox(c *db.Conversation) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
s.fillConversationSandboxes([]*db.Conversation{c})
|
|
}
|
|
|
|
// ensureConversationSandbox binds a live turn to the conversation's sandbox.
|
|
// A trigger may already have placed the target on ctx; that value wins so the
|
|
// turn cannot drift back to a session container. Local mode leaves the context
|
|
// alone because there is no container to select.
|
|
func (s *Server) ensureConversationSandbox(ctx context.Context, c *db.Conversation) context.Context {
|
|
if mgr := sandbox.Default(); mgr == nil || !mgr.Enabled() {
|
|
return ctx
|
|
}
|
|
if preset, ok := sandbox.TargetFrom(ctx); ok && preset.Valid() {
|
|
return ctx
|
|
}
|
|
if c == nil || s == nil || s.m == nil || s.m.pg == nil {
|
|
return ctx
|
|
}
|
|
scope, owner, stored := storedConversationSandbox(c)
|
|
if !stored {
|
|
fallbacks, err := s.m.pg.ReportTaskSandboxFallbacks([]int64{c.ID})
|
|
if err != nil {
|
|
log.Printf("[conv %d] sandbox fallback: %v", c.ID, err)
|
|
fallbacks = nil
|
|
}
|
|
taskID, ok := fallbacks[c.ID]
|
|
scope, owner = resolveConversationSandbox(c, taskID, ok)
|
|
if err == nil && scope != "" && owner != "" {
|
|
if saveErr := s.m.pg.SetConversationSandbox(c.ID, scope, owner); saveErr != nil {
|
|
log.Printf("[conv %d] save sandbox binding: %v", c.ID, saveErr)
|
|
} else {
|
|
c.SandboxScope = scope
|
|
c.SandboxOwnerID = owner
|
|
}
|
|
}
|
|
}
|
|
target := sandbox.Target{Scope: sandbox.Scope(scope), OwnerID: owner}
|
|
if !target.Valid() {
|
|
return ctx
|
|
}
|
|
return sandbox.WithTarget(ctx, target)
|
|
}
|