mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-06 21:11:50 +08:00
feat(sandbox): isolate worker/chat execution in Docker containers with lifecycle UI
- Add Docker sandbox manager, non-root sandbox image, compose isolation, and host firewall script (#217) - Route Bash, custom commands/scripts, stdio MCP, chat sessions, MainAgent chat, and task-scoped reporter screenshots into sandbox containers with per-task browser concurrency gate - Enforce proxy SSRF guard against loopback, metadata, internal services, and admin port - Add dedicated /system/sandbox management page, /chat session sandbox control, and task detail sandbox popover with progressive stats loading
This commit is contained in:
1 parent
6bbeac588d
commit
9c43e694a3
44 files changed
+7086
-91
No files matched your search
+44
-9
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/Autumn-27/artex/db"
|
||||
"github.com/Autumn-27/artex/intercept"
|
||||
"github.com/Autumn-27/artex/intercept/jev"
|
||||
"github.com/Autumn-27/artex/internal/sandbox"
|
||||
"github.com/Autumn-27/artex/llmpool"
|
||||
"github.com/Autumn-27/artex/llmrec"
|
||||
"github.com/Autumn-27/artex/report"
|
||||
@@ -169,6 +170,7 @@ type triggeredRun struct {
|
||||
taskDesc string // 任务描述(任务级,同任务相同);合并时只渲染一次
|
||||
taskGoal string // 任务目标(任务级,同任务相同);合并时只渲染一次
|
||||
mergeable bool // true for finding/goal event triggers (merge by taskID)
|
||||
multiTask bool // true when mergeAllRuns combined events from multiple distinct tasks
|
||||
}
|
||||
|
||||
func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDir string) *Server {
|
||||
@@ -282,9 +284,6 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
|
||||
// 漏洞 IM 推送投递引擎。与 Scheduler 并列但独立:推送的实时性要求(3s)
|
||||
// 与触发器的业务节奏不同,且两者失败互不牵连——推送卡住不该影响 agent 触发。
|
||||
go newNotifier(s).Run(s.ctx)
|
||||
// Fill the tool cache for any enabled MCP that has none yet (notably the
|
||||
// seeded browser MCP on first run). Async so it never blocks startup.
|
||||
go s.discoverEmptyMCPsOnStartup()
|
||||
logSink.SetDB(ctx, m.pg) // restore last 100 log rows and enable async persistence
|
||||
}
|
||||
// precedence: persisted DB config > env.
|
||||
@@ -304,6 +303,12 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
|
||||
log.Printf("[engine] no LLM provider configured — engine idle until set via /api/llm or env")
|
||||
}
|
||||
s.restoreTaskRuntimes()
|
||||
s.initSandbox()
|
||||
if m.pg != nil {
|
||||
// Fill the tool cache for any enabled MCP (notably browser MCP) AFTER
|
||||
// initSandbox so stdio discovery runs inside the sandbox container.
|
||||
go s.discoverEmptyMCPsOnStartup()
|
||||
}
|
||||
s.ensureProxySupervisor()
|
||||
s.proxyPoolWG.Add(1)
|
||||
go func() { defer s.proxyPoolWG.Done(); s.runProxyPoolSupervisor(s.ctx) }()
|
||||
@@ -323,13 +328,17 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
|
||||
return s
|
||||
}
|
||||
|
||||
// Close waits for proxy pool supervision to stop before Manager closes the DB.
|
||||
// Close waits for proxy pool supervision to stop and stops running sandboxes before DB close.
|
||||
func (s *Server) Close() {
|
||||
if s != nil {
|
||||
s.proxyPoolWG.Wait()
|
||||
if mgr := sandbox.Default(); mgr != nil && mgr.Enabled() {
|
||||
sctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
mgr.Shutdown(sctx)
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Restored deadline and worker loops must inherit the same context as new tasks,
|
||||
// including the side-question checkpoint publisher installed during startup.
|
||||
func (s *Server) restoreTaskRuntimes() {
|
||||
@@ -1167,6 +1176,20 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("PUT /api/intercept/judge", s.interceptSetJudgeConfig)
|
||||
mux.HandleFunc("GET /api/intercept/judge/usage", s.interceptJudgeUsage) // 兜底审批累计 token 用量
|
||||
|
||||
// 沙箱容器隔离状态、配置与启停管理
|
||||
mux.HandleFunc("GET /api/sandbox/status", s.sandboxStatus)
|
||||
mux.HandleFunc("PUT /api/sandbox/settings", s.updateSandboxSettings)
|
||||
mux.HandleFunc("POST /api/sandbox/prune", s.pruneSandboxContainers)
|
||||
mux.HandleFunc("GET /api/sandbox/containers/{scope}/{id}", s.getSandboxContainer)
|
||||
mux.HandleFunc("POST /api/sandbox/containers/{scope}/{id}/start", s.startSandboxContainer)
|
||||
mux.HandleFunc("POST /api/sandbox/containers/{scope}/{id}/stop", s.stopSandboxContainer)
|
||||
mux.HandleFunc("POST /api/sandbox/containers/{scope}/{id}/recreate", s.recreateSandboxContainer)
|
||||
mux.HandleFunc("DELETE /api/sandbox/containers/{scope}/{id}", s.removeSandboxContainer)
|
||||
mux.HandleFunc("GET /api/tasks/{id}/sandbox", s.getTaskSandbox)
|
||||
mux.HandleFunc("POST /api/tasks/{id}/sandbox/start", s.startTaskSandbox)
|
||||
mux.HandleFunc("POST /api/tasks/{id}/sandbox/stop", s.stopTaskSandbox)
|
||||
mux.HandleFunc("POST /api/tasks/{id}/sandbox/recreate", s.recreateTaskSandbox)
|
||||
|
||||
// /api/* goes through CORS + JWT; everything else is served by the embedded
|
||||
// frontend (no JWT required; the optional Basic Auth gate wraps both). With the
|
||||
// no-embed build the webui handler just 404s (run `next dev` separately).
|
||||
@@ -1180,9 +1203,8 @@ func (s *Server) Handler() http.Handler {
|
||||
top := http.NewServeMux()
|
||||
top.HandleFunc(mcpEndpointPath, s.serveMCP)
|
||||
top.Handle("/", s.requireBasicAuth(root))
|
||||
return top
|
||||
return s.blockSandboxAdminAccess(top)
|
||||
}
|
||||
|
||||
// --- handlers ---
|
||||
|
||||
// health requires the optional Basic Auth gate but no admin JWT, and exposes no application fingerprints.
|
||||
@@ -1882,6 +1904,13 @@ func (s *Server) createTaskFromRequest(req createTaskReq) (*Task, *taskCreateErr
|
||||
}
|
||||
return nil, &taskCreateError{500, err.Error()}
|
||||
}
|
||||
if newDesc, newGoal, copied := transferDraftUploadsToTask(s.m.dir, t.ID, req.Description, req.Goal); copied > 0 {
|
||||
req.Description, req.Goal = newDesc, newGoal
|
||||
t.Description, t.Goal = newDesc, newGoal
|
||||
if id, e := strconv.ParseInt(t.ID, 10, 64); e == nil && s.m.pg != nil {
|
||||
_, _ = s.m.pg.Exec(`UPDATE tasks SET description=$2, goal=$3 WHERE id=$1`, id, newDesc, newGoal)
|
||||
}
|
||||
}
|
||||
log.Printf("[task] 新建任务 #%s «%s» 目标: %s", t.ID, req.Description, req.Goal)
|
||||
// 共享的建后流程(seed + 种子意图 + 后台目标分解 + engine.Run),与 spawn_task 复用同一段。
|
||||
// launchTask 内部异步,不阻塞 UI —— 目标分解在后台可见地进行。
|
||||
@@ -4616,12 +4645,18 @@ func (s *Server) chat(w http.ResponseWriter, r *http.Request) {
|
||||
// 把上传附件的【绝对路径】清单拼进发给 agent 的消息,它据此用 Read/Bash 打开文件。
|
||||
// taskDir = agent 的工作目录(CWD),与 chatUpload 落盘、ensureRunDir 一致。
|
||||
taskDir := filepath.Join(s.m.dir, "tasks", t.ID)
|
||||
agentMsg := composeAgentMessage(agentMessage, req.Attachments, taskDir)
|
||||
attachDir := taskDir
|
||||
if mgr := sandbox.Default(); mgr != nil && mgr.Enabled() {
|
||||
attachDir = sandbox.ContainerWorkDir
|
||||
}
|
||||
agentMsg := composeAgentMessage(agentMessage, req.Attachments, attachDir)
|
||||
s.engine.BeginLLMCall(t.ID)
|
||||
_, err := ma.Chat(ctx, maTaskID, mainSeg, s.m.Assets(), t.Store, t.Goal, agentMsg, emit, t.Notify, resume, t.NotifyGoal, t.NotifyHint)
|
||||
s.engine.EndLLMCall(t.ID)
|
||||
if mgr := sandbox.Default(); mgr != nil && mgr.Enabled() && s.resolvedTaskStatus(t) != "running" {
|
||||
mgr.RequestIdleStop(sandbox.Target{Scope: sandbox.ScopeTask, OwnerID: t.ID})
|
||||
}
|
||||
if err != nil && ctx.Err() == nil {
|
||||
s.engine.emitActivity(t, db.Activity{Worker: "mainagent", Kind: "text", IsError: true, Summary: "(主 Agent 出错:" + err.Error() + ")", MainSeg: segPtr})
|
||||
}
|
||||
}()
|
||||
writeJSON(w, 202, map[string]any{"status": "accepted", "mode": "llm"})
|
||||
|
||||
Reference in new issue
Block a user