mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 20:01:51 +08:00
fix(chat): show the sandbox container a conversation actually uses
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.
This commit is contained in:
1 parent
8bac4960e7
commit
b18cf89b77
9 files changed
+531
-57
No files matched your search
@@ -0,0 +1,128 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
"github.com/Autumn-27/artex/internal/sandbox"
|
||||
)
|
||||
|
||||
func TestSandboxTargetForTrigger(t *testing.T) {
|
||||
task := sandboxTargetForTrigger("reporter", 5, false, 18)
|
||||
if task.Scope != sandbox.ScopeTask || task.OwnerID != "5" {
|
||||
t.Fatalf("reporter task = %+v", task)
|
||||
}
|
||||
merged := sandboxTargetForTrigger("reporter", 5, true, 19)
|
||||
if merged.Scope != sandbox.ScopeSession || merged.OwnerID != "conv-19" {
|
||||
t.Fatalf("multi-task reporter = %+v", merged)
|
||||
}
|
||||
other := sandboxTargetForTrigger("auto", 5, false, 20)
|
||||
if other.Scope != sandbox.ScopeSession || other.OwnerID != "conv-20" {
|
||||
t.Fatalf("non-reporter = %+v", other)
|
||||
}
|
||||
noTask := sandboxTargetForTrigger("reporter", 0, false, 21)
|
||||
if noTask.Scope != sandbox.ScopeSession || noTask.OwnerID != "conv-21" {
|
||||
t.Fatalf("reporter without task = %+v", noTask)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveConversationSandbox(t *testing.T) {
|
||||
savedTask := &db.Conversation{ID: 18, SandboxScope: "task", SandboxOwnerID: "5"}
|
||||
scope, owner := resolveConversationSandbox(savedTask, 9, true)
|
||||
if scope != "task" || owner != "5" {
|
||||
t.Fatalf("stored task binding must win, got %s %s", scope, owner)
|
||||
}
|
||||
|
||||
savedSession := &db.Conversation{ID: 19, SandboxScope: "session", SandboxOwnerID: "conv-19"}
|
||||
scope, owner = resolveConversationSandbox(savedSession, 5, true)
|
||||
if scope != "session" || owner != "conv-19" {
|
||||
t.Fatalf("stored session binding must win over findings, got %s %s", scope, owner)
|
||||
}
|
||||
|
||||
historical := &db.Conversation{ID: 20}
|
||||
scope, owner = resolveConversationSandbox(historical, 7, true)
|
||||
if scope != "task" || owner != "7" {
|
||||
t.Fatalf("historical finding fallback = %s %s", scope, owner)
|
||||
}
|
||||
|
||||
plain := &db.Conversation{ID: 21}
|
||||
scope, owner = resolveConversationSandbox(plain, 0, false)
|
||||
if scope != "session" || owner != "conv-21" {
|
||||
t.Fatalf("plain conversation = %s %s", scope, owner)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationListReturnsSandboxTarget(t *testing.T) {
|
||||
s, _ := newRetestServer(t)
|
||||
pg := s.m.pg
|
||||
task, err := pg.CreateTask("conversation sandbox api", "goal", nil, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reporter, err := pg.CreateConversation("reporter", "historical report", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
merged, err := pg.CreateConversation("reporter", "merged report", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plain, err := pg.CreateConversation("auto", "plain", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = pg.Exec(`DELETE FROM findings WHERE report_conversation_id IN ($1,$2)`, reporter.ID, merged.ID)
|
||||
_, _ = pg.Exec(`DELETE FROM conversations WHERE id IN ($1,$2,$3)`, reporter.ID, merged.ID, plain.ID)
|
||||
_ = pg.DeleteTask(task.ID)
|
||||
})
|
||||
|
||||
fid, err := pg.AddFinding(task.ID, 0, "xss", "name", "high", "summary", "evidence", "worker", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pg.SetFindingReportConversationID(fid, reporter.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mergedFinding, err := pg.AddFinding(task.ID, 0, "xss", "name", "high", "summary", "evidence", "worker", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pg.SetFindingReportConversationID(mergedFinding, merged.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pg.SetConversationSandbox(merged.ID, "session", "conv-"+strconv.FormatInt(merged.ID, 10)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
w := retestRequest(s.pgListConversations, http.MethodGet, 0, "")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body)
|
||||
}
|
||||
var body struct {
|
||||
Conversations []conversationListItem `json:"conversations"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := map[int64]conversationListItem{}
|
||||
for _, item := range body.Conversations {
|
||||
if item.ID == reporter.ID || item.ID == merged.ID || item.ID == plain.ID {
|
||||
found[item.ID] = item
|
||||
}
|
||||
}
|
||||
if len(found) != 3 {
|
||||
t.Fatalf("found %d conversations", len(found))
|
||||
}
|
||||
if found[reporter.ID].SandboxScope != "task" || found[reporter.ID].SandboxOwnerID != strconv.FormatInt(task.ID, 10) {
|
||||
t.Fatalf("historical reporter = %+v", found[reporter.ID].Conversation)
|
||||
}
|
||||
if found[merged.ID].SandboxScope != "session" || found[merged.ID].SandboxOwnerID != "conv-"+strconv.FormatInt(merged.ID, 10) {
|
||||
t.Fatalf("stored multi-task session = %+v", found[merged.ID].Conversation)
|
||||
}
|
||||
if found[plain.ID].SandboxScope != "session" || found[plain.ID].SandboxOwnerID != "conv-"+strconv.FormatInt(plain.ID, 10) {
|
||||
t.Fatalf("plain conversation = %+v", found[plain.ID].Conversation)
|
||||
}
|
||||
}
|
||||
+18
-14
@@ -63,6 +63,7 @@ func (s *Server) pgListConversations(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
s.fillConversationSandboxes(cs)
|
||||
items := make([]conversationListItem, 0, len(cs))
|
||||
s.chatMu.Lock()
|
||||
for _, c := range cs {
|
||||
@@ -126,6 +127,7 @@ func (s *Server) pgCreateConversation(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
s.fillConversationSandbox(c)
|
||||
writeJSON(w, 200, c)
|
||||
}
|
||||
|
||||
@@ -217,6 +219,7 @@ func (s *Server) pgRenameConversation(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, 404, "conversation not found")
|
||||
return
|
||||
}
|
||||
s.fillConversationSandbox(updated)
|
||||
writeJSON(w, 200, updated)
|
||||
}
|
||||
|
||||
@@ -677,6 +680,7 @@ func (s *Server) runConversationTurn(ctx context.Context, cancel context.CancelC
|
||||
webSearch = a.WebSearch
|
||||
}
|
||||
sessionID := s.convBusyKey(c.ID) // "conv-<id>" transcript session
|
||||
ctx = s.ensureConversationSandbox(ctx, c)
|
||||
emit := func(rec db.Activity) {
|
||||
if _, err := pg.AppendConvActivity(c.ID, rec); err != nil {
|
||||
log.Printf("[conv %d] append activity failed: %v", c.ID, err)
|
||||
@@ -982,26 +986,26 @@ func (s *Server) runTriggeredRun(item triggeredRun) {
|
||||
s.chatMu.Lock()
|
||||
s.chatBusy[busyKey] = true
|
||||
s.chatMu.Unlock()
|
||||
var preset sandbox.Target
|
||||
if mgr := sandbox.Default(); mgr != nil && mgr.Enabled() {
|
||||
if item.agentKey == "reporter" && item.taskID > 0 && !item.multiTask {
|
||||
preset = sandbox.Target{Scope: sandbox.ScopeTask, OwnerID: strconv.FormatInt(item.taskID, 10)}
|
||||
}
|
||||
target := sandboxTargetForTrigger(item.agentKey, item.taskID, item.multiTask, c.ID)
|
||||
if err := pg.SetConversationSandbox(c.ID, string(target.Scope), target.OwnerID); err != nil {
|
||||
log.Printf("[trigger] save sandbox binding for conv %d failed: %v", c.ID, err)
|
||||
} else {
|
||||
c.SandboxScope = string(target.Scope)
|
||||
c.SandboxOwnerID = target.OwnerID
|
||||
}
|
||||
if preset.Valid() {
|
||||
s.runConversationSync(c, msg, busyKey, preset)
|
||||
if mgr := sandbox.Default(); mgr != nil && mgr.Enabled() {
|
||||
if t, ok := s.m.Task(preset.OwnerID); !ok || s.resolvedTaskStatus(t) != "running" {
|
||||
mgr.RequestIdleStop(preset)
|
||||
if mgr := sandbox.Default(); mgr != nil && mgr.Enabled() && target.Valid() {
|
||||
s.runConversationSync(c, msg, busyKey, target)
|
||||
if target.Scope == sandbox.ScopeTask {
|
||||
if t, ok := s.m.Task(target.OwnerID); !ok || s.resolvedTaskStatus(t) != "running" {
|
||||
mgr.RequestIdleStop(target)
|
||||
}
|
||||
} else {
|
||||
// Triggered one-shot sessions (no bound task) do not need to sit idle for 20m.
|
||||
mgr.RequestIdleStop(target)
|
||||
}
|
||||
return
|
||||
}
|
||||
s.runConversationSync(c, msg, busyKey)
|
||||
// Triggered one-shot sessions (no bound task) do not need to sit idle for 20m.
|
||||
if mgr := sandbox.Default(); mgr != nil && mgr.Enabled() {
|
||||
mgr.RequestIdleStop(sandbox.Target{Scope: sandbox.ScopeSession, OwnerID: busyKey})
|
||||
}
|
||||
}
|
||||
|
||||
// firstLine returns a single-line, length-capped preview (shared with summaries).
|
||||
|
||||
Reference in new issue
Block a user