mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-28 10:41:50 +08:00
Merge pull request #203 from RuoJi6/RuoJi6/feat/issue-194-memory-limit
fix: limit ARTEX container and tool memory use # Conflicts: # agent/chat.go # agent/mainagent.go # agent/planner.go
This commit is contained in:
commit
9ce996ef46
36 files changed
+1869
-64
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/Autumn-27/artex/internal/toolguard"
|
||||
actool "github.com/Autumn-27/norma/tool"
|
||||
)
|
||||
|
||||
@@ -79,6 +80,17 @@ func AugmentTools(ctx context.Context, agentKey string, base []actool.CoreTool)
|
||||
return out, def, cleanup
|
||||
}
|
||||
|
||||
// Install the guarded handler before ToolResolve decorates it with DB defaults.
|
||||
func guardedDefaultTools() []actool.CoreTool {
|
||||
tools := actool.DefaultTools()
|
||||
for i, t := range tools {
|
||||
if t.Name() == "Bash" {
|
||||
tools[i] = toolguard.NewBash()
|
||||
}
|
||||
}
|
||||
return tools
|
||||
}
|
||||
|
||||
// guardPanic turns a panicking tool handler into an ordinary tool error. The
|
||||
// harness runs each tool on its own goroutine, so a panic inside a handler can't
|
||||
// be recovered by the caller that started the run — it takes the whole process
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGuardedBashPreservesDecoratedDefaults(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("POSIX shell syntax")
|
||||
}
|
||||
for _, tool := range guardedDefaultTools() {
|
||||
if tool.Name() != "Bash" {
|
||||
continue
|
||||
}
|
||||
decorated := DecorateTool(tool, "custom description", map[string]any{
|
||||
"properties": map[string]any{"timeout_ms": map[string]any{"type": "integer", "default": 50}},
|
||||
})
|
||||
res, err := decorated.Call(context.Background(), []byte(`{"command":"sleep 2"}`), nil)
|
||||
if err != nil || !res.IsError || !strings.Contains(res.Flatten(), "timed out") {
|
||||
t.Fatalf("expected timeout injected by tool decorator, got %s %v", res.Flatten(), err)
|
||||
}
|
||||
return
|
||||
}
|
||||
t.Fatal("Bash not found")
|
||||
}
|
||||
+1
-1
@@ -120,7 +120,7 @@ func (c *ChatAgent) Chat(ctx context.Context, agentKey, sessionID, message strin
|
||||
// Pure assistant: DefaultTools as the base; AugmentTools layers in the key's
|
||||
// visible skills/MCP and lets the DB tools table filter/override. DefaultTools
|
||||
// have no tools-table rows, so they always pass through.
|
||||
base := actool.DefaultTools()
|
||||
base := guardedDefaultTools()
|
||||
ctx = WithRunInfo(ctx, RunInfo{SessionID: sessionID, WorkDir: sessionWorkDir})
|
||||
tools, def, cleanup := AugmentTools(ctx, agentKey, base)
|
||||
defer cleanup()
|
||||
|
||||
+1
-1
@@ -126,7 +126,7 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, mainSeg int, as *db.
|
||||
tsx.steerWork = m.steerWork // enable steer_work tool (nil = unavailable)
|
||||
// 领域工具 + 基础默认工具集(Read/Write/Edit/MultiEdit/LS/Glob/Grep/Bash)
|
||||
// 资产覆盖度功能关闭时剔除 add_task_scope/list_untested_assets(不入 prompt)。
|
||||
base := append(tsx.DropCoverageTools(tsx.MainAgentTools()), actool.DefaultTools()...)
|
||||
base := append(tsx.DropCoverageTools(tsx.MainAgentTools()), guardedDefaultTools()...)
|
||||
// 本任务的工作目录 <workDir>/tasks/<taskID>,先建好。
|
||||
mainDir := ensureRunDir(m.workDir, taskID, 0)
|
||||
ctx = intercept.WithReviewWorkingDirectory(ctx, mainDir)
|
||||
|
||||
+1
-1
@@ -374,7 +374,7 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
|
||||
}
|
||||
// 领域工具 + 基础默认工具集(Read/Write/Edit/MultiEdit/LS/Glob/Grep/Bash)
|
||||
// 资产覆盖度功能关闭时剔除 add_task_scope/list_untested_assets(不入 prompt)。
|
||||
base := append(tsx.DropCoverageTools(tsx.PlannerTools()), actool.DefaultTools()...)
|
||||
base := append(tsx.DropCoverageTools(tsx.PlannerTools()), guardedDefaultTools()...)
|
||||
// 本任务的工作目录 <workDir>/tasks/<taskID>,先建好。
|
||||
taskDir := ensureRunDir(p.workDir, taskID, 0)
|
||||
ctx = intercept.WithReviewContext(ctx, taskDir, intercept.ReviewBackground{})
|
||||
|
||||
+9
-6
@@ -25,6 +25,7 @@ import (
|
||||
"github.com/Autumn-27/norma/compaction"
|
||||
"github.com/Autumn-27/norma/llm"
|
||||
acperm "github.com/Autumn-27/norma/permission"
|
||||
actool "github.com/Autumn-27/norma/tool"
|
||||
"github.com/Autumn-27/norma/transcript"
|
||||
)
|
||||
|
||||
@@ -522,12 +523,14 @@ func TestConnection(ctx context.Context, c Config) (time.Duration, string, error
|
||||
// "已中断/length/resume" 一团糟。给足预算让它把 OK 干净吐完(finish=stop)。
|
||||
// EscalateMaxTokens 保持 false:不因截断而抬额重试,避免 resume 循环空烧。
|
||||
reply, err := agentcore.Run(ctx, agentcore.Options{
|
||||
Provider: prov,
|
||||
SystemPrompt: []string{"你是连接测试。直接输出两个字符 OK 即可,不要思考、不要解释、不要别的。"},
|
||||
PermissionMode: acperm.ModeBypass,
|
||||
MaxTurns: 1,
|
||||
MaxTokens: 8192,
|
||||
NonStreaming: !c.Stream, // 用该 profile 的真实收发模式做连接测试
|
||||
Provider: prov,
|
||||
Tools: []actool.CoreTool{},
|
||||
DisableBackgroundTasks: true,
|
||||
SystemPrompt: []string{"你是连接测试。直接输出两个字符 OK 即可,不要思考、不要解释、不要别的。"},
|
||||
PermissionMode: acperm.ModeBypass,
|
||||
MaxTurns: 1,
|
||||
MaxTokens: 8192,
|
||||
NonStreaming: !c.Stream, // 用该 profile 的真实收发模式做连接测试
|
||||
}, "ping")
|
||||
lat := time.Since(start)
|
||||
if err != nil {
|
||||
|
||||
+8
-5
@@ -6,14 +6,17 @@ import (
|
||||
actool "github.com/Autumn-27/norma/tool"
|
||||
)
|
||||
|
||||
// newAgentSession installs the configurable WebFetch before the SDK builds the
|
||||
// registry, so every agent role gets one tool and the same proxy/TLS settings.
|
||||
// newAgentSession installs ARTEX's resource-guarded Bash tool and configurable
|
||||
// WebFetch before the SDK builds the registry, so every agent role gets the
|
||||
// same memory/timeout guards and proxy/TLS settings.
|
||||
func newAgentSession(opts agentcore.Options) *agentcore.Session {
|
||||
if opts.Tools == nil {
|
||||
opts.Tools = guardedDefaultTools()
|
||||
} else if opts.EnableWebFetch {
|
||||
opts.Tools = append([]actool.CoreTool{}, opts.Tools...)
|
||||
}
|
||||
if opts.EnableWebFetch {
|
||||
opts.EnableWebFetch = false
|
||||
if opts.Tools == nil {
|
||||
opts.Tools = actool.DefaultTools()
|
||||
}
|
||||
opts.Tools = append(opts.Tools, webfetch.New(actool.WebFetchConfig{
|
||||
Proxy: opts.WebFetchProxy,
|
||||
CACert: opts.WebFetchCACert,
|
||||
|
||||
+2
-2
@@ -148,14 +148,14 @@ func NewWorker(prov llm.Provider, model, workDir string, tx *transcript.Store, w
|
||||
return &Worker{prov: prov, model: model, workDir: workDir, tx: tx, window: window, maxTurns: maxTurns, extraTools: extra}
|
||||
}
|
||||
|
||||
// defaultToolsExcept returns actool.DefaultTools() minus the named tools (by
|
||||
// defaultToolsExcept returns guardedDefaultTools() minus the named tools (by
|
||||
// CoreTool.Name()). Used to trim SDK default tools an agent shouldn't have.
|
||||
func defaultToolsExcept(exclude ...string) []actool.CoreTool {
|
||||
drop := make(map[string]bool, len(exclude))
|
||||
for _, n := range exclude {
|
||||
drop[n] = true
|
||||
}
|
||||
all := actool.DefaultTools()
|
||||
all := guardedDefaultTools()
|
||||
out := make([]actool.CoreTool, 0, len(all))
|
||||
for _, t := range all {
|
||||
if !drop[t.Name()] {
|
||||
|
||||
Reference in new issue
Block a user