ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
agent: guard ToolAugment/ToolResolve hooks against concurrent access
BaaRai37f2573a535b
diff --git a/agent/assembly.go b/agent/assembly.goindex f4f8e9a..c0885d9 100644--- a/agent/assembly.go+++ b/agent/assembly.go@@ -6,6 +6,7 @@ import ("fmt""log""runtime/debug"+ "sync"actool "github.com/Autumn-27/norma/tool")@@ -23,15 +24,36 @@ type DeferredInfo struct {UnlockSkill func(skillName string)}-// ToolAugment, if set, returns the EXTRA tools an agent should see beyond its-// built-in base set — the agent's visible skills (packed into one Skill meta-tool)+// ToolAugmentFunc is the ToolAugment hook. If set, it returns the EXTRA tools+// an agent should see beyond its built-in base set — the agent's visible skills (packed into one Skill meta-tool)// and visible MCP servers (expanded to mcp__server__tool). It also returns the// DeferredInfo describing how those MCP tools are deferred/gated. The server wires// it to the PG agent_visibility table. cleanup releases any spawned MCP clients.//// When nil, agents run with only their built-in tools — behavior is unchanged// until a user assigns a skill/MCP to the agent in the UI.-var ToolAugment func(ctx context.Context, agentKey string) (extra []actool.CoreTool, def DeferredInfo, cleanup func())+type ToolAugmentFunc func(ctx context.Context, agentKey string) (extra []actool.CoreTool, def DeferredInfo, cleanup func())++// The hook is set by server wiring while restored tasks may already be+// assembling tools on other goroutines, so every access goes through the lock.+var (+ toolAugmentMu sync.RWMutex+ toolAugment ToolAugmentFunc+)++// SetToolAugment installs (or, with nil, removes) the ToolAugment hook.+func SetToolAugment(f ToolAugmentFunc) {+ toolAugmentMu.Lock()+ toolAugment = f+ toolAugmentMu.Unlock()+}++// ToolAugment returns the current ToolAugment hook, or nil when none is set.+func ToolAugment() ToolAugmentFunc {+ toolAugmentMu.RLock()+ defer toolAugmentMu.RUnlock()+ return toolAugment+}// AugmentTools returns base plus the agent's visible skill/MCP tools, the// DeferredInfo, and a cleanup func the caller must defer (closes MCP clients).@@ -42,10 +64,11 @@ func AugmentTools(ctx context.Context, agentKey string, base []actool.CoreTool)cleanup = func() {}out = base)- if ToolAugment != nil {+ // Copy each hook once under its lock, then call it outside the lock.+ if augment := ToolAugment(); augment != nil {var extra []actool.CoreToolvar cl func()- extra, def, cl = ToolAugment(ctx, agentKey)+ extra, def, cl = augment(ctx, agentKey)if cl != nil {cleanup = cl}@@ -57,8 +80,8 @@ func AugmentTools(ctx context.Context, agentKey string, base []actool.CoreTool)// agent isn't bound to (or that are disabled) and swap in overridden// descriptions/schemas + default injection. MCP/skill/host tools have no row// and pass through untouched, so deferred/unlock wiring stays consistent.- if ToolResolve != nil {- out = ToolResolve(ctx, agentKey, out)+ if resolve := ToolResolve(); resolve != nil {+ out = resolve(ctx, agentKey, out)}// Tool RAG (off by default): lexical projection of the bound list; only removes.out = applyToolRAG(ctx, agentKey, out, def.Deferred, bm25Score)diff --git a/agent/assembly_test.go b/agent/assembly_test.gonew file mode 100644index 0000000..bdaa6da--- /dev/null+++ b/agent/assembly_test.go@@ -0,0 +1,52 @@+package agent++import (+ "context"+ "sync"+ "testing"++ actool "github.com/Autumn-27/norma/tool"+)++// Server wiring installs the hooks while restored tasks may already be assembling+// tools on other goroutines. Readers and writers run concurrently here, so under+// -race any unsynchronised hook access is reported regardless of scheduling.+func TestAugmentToolsHooksConcurrentSwap(t *testing.T) {+ oldAugment, oldResolve, oldPolicy := ToolAugment(), ToolResolve(), FindingTrafficBindingEnabled()+ t.Cleanup(func() {+ SetToolAugment(oldAugment)+ SetToolResolve(oldResolve)+ SetFindingTrafficBindingEnabled(oldPolicy)+ })+ augment := func(context.Context, string) ([]actool.CoreTool, DeferredInfo, func()) {+ return nil, DeferredInfo{}, nil+ }+ resolve := func(_ context.Context, _ string, in []actool.CoreTool) []actool.CoreTool { return in }+ policy := func() bool { return false }++ const rounds = 200+ var wg sync.WaitGroup+ for range 4 {+ wg.Go(func() {+ for range rounds {+ out, _, cleanup := AugmentTools(context.Background(), "worker", nil)+ cleanup()+ if len(out) != 0 {+ t.Errorf("AugmentTools(nil base) = %d tools, want 0", len(out))+ return+ }+ }+ })+ }+ wg.Go(func() {+ for range rounds {+ SetToolAugment(augment)+ SetToolResolve(resolve)+ SetFindingTrafficBindingEnabled(policy)+ SetToolAugment(nil)+ SetToolResolve(nil)+ SetFindingTrafficBindingEnabled(nil)+ }+ })+ wg.Wait()+}diff --git a/agent/finding_recorder_test.go b/agent/finding_recorder_test.goindex aca044f..13d274c 100644--- a/agent/finding_recorder_test.go+++ b/agent/finding_recorder_test.go@@ -51,9 +51,9 @@ func TestReportFindingOptionalTrafficWithoutCapture(t *testing.T) {}func TestReportFindingAtomicContract(t *testing.T) {- old := FindingTrafficBindingEnabled- FindingTrafficBindingEnabled = func() bool { return true }- t.Cleanup(func() { FindingTrafficBindingEnabled = old })+ old := FindingTrafficBindingEnabled()+ SetFindingTrafficBindingEnabled(func() bool { return true })+ t.Cleanup(func() { SetFindingTrafficBindingEnabled(old) })d := testDB(t)defer d.Close()task, err := d.CreateTask("report contract", "fixture", nil, 0, 0)diff --git a/agent/finding_workflow.go b/agent/finding_workflow.goindex 5231da4..1428646 100644--- a/agent/finding_workflow.go+++ b/agent/finding_workflow.go@@ -3,6 +3,7 @@ package agentimport ("encoding/json""fmt"+ "sync""github.com/Autumn-27/artex/db"actool "github.com/Autumn-27/norma/tool"@@ -13,10 +14,29 @@ const findingIDGuidance = "\n\n**Finding ID conventions**: finding_id is the sta// The server supplies the persisted setting. A missing setting/host is off.// Consulted at assembly and again on writes so an already-running session// cannot keep binding after the user switches the feature off.-var FindingTrafficBindingEnabled func() bool+// Guarded by a lock for the same reason as the tool hooks.+var (+ findingTrafficBindingMu sync.RWMutex+ findingTrafficBindingHook func() bool+)++// SetFindingTrafficBindingEnabled installs (or, with nil, removes) the hook.+func SetFindingTrafficBindingEnabled(f func() bool) {+ findingTrafficBindingMu.Lock()+ findingTrafficBindingHook = f+ findingTrafficBindingMu.Unlock()+}++// FindingTrafficBindingEnabled returns the current hook, or nil when none is set.+func FindingTrafficBindingEnabled() func() bool {+ findingTrafficBindingMu.RLock()+ defer findingTrafficBindingMu.RUnlock()+ return findingTrafficBindingHook+}func findingTrafficBindingEnabled() bool {- return FindingTrafficBindingEnabled != nil && FindingTrafficBindingEnabled()+ f := FindingTrafficBindingEnabled()+ return f != nil && f()}// Applied after ToolResolve: user descriptions and prompts remain intact, whilediff --git a/agent/finding_workflow_test.go b/agent/finding_workflow_test.goindex fd2df65..c478f3d 100644--- a/agent/finding_workflow_test.go+++ b/agent/finding_workflow_test.go@@ -10,19 +10,23 @@ import ()func TestFindingWorkflowSharedAssemblyAndSwitch(t *testing.T) {- oldPolicy, oldResolve, oldAugment := FindingTrafficBindingEnabled, ToolResolve, ToolAugment- t.Cleanup(func() { FindingTrafficBindingEnabled, ToolResolve, ToolAugment = oldPolicy, oldResolve, oldAugment })- ToolAugment = nil+ oldPolicy, oldResolve, oldAugment := FindingTrafficBindingEnabled(), ToolResolve(), ToolAugment()+ t.Cleanup(func() {+ SetFindingTrafficBindingEnabled(oldPolicy)+ SetToolResolve(oldResolve)+ SetToolAugment(oldAugment)+ })+ SetToolAugment(nil)on := false- FindingTrafficBindingEnabled = func() bool { return on }+ SetFindingTrafficBindingEnabled(func() bool { return on })ts := NewToolSet(nil, "fixture")- ToolResolve = func(_ context.Context, _ string, base []actool.CoreTool) []actool.CoreTool {+ SetToolResolve(func(_ context.Context, _ string, base []actool.CoreTool) []actool.CoreTool {out := make([]actool.CoreTool, len(base))for i, tool := range base {out[i] = DecorateTool(tool, "CUSTOM DESCRIPTION", tool.InputSchema())}return out- }+ })for _, role := range []string{"worker", "planner", "mainagent", "auto", "pentest", "custom-agent"} {for _, enabled := range []bool{false, true, false} {on = enabled@@ -59,7 +63,7 @@ func TestFindingWorkflowSharedAssemblyAndSwitch(t *testing.T) {}}on = true- ToolResolve = func(context.Context, string, []actool.CoreTool) []actool.CoreTool { return nil }+ SetToolResolve(func(context.Context, string, []actool.CoreTool) []actool.CoreTool { return nil })out, def, cleanup := AugmentTools(t.Context(), "planner", []actool.CoreTool{ts.addFinding()})defer cleanup()if len(out) != 0 || def.FindingGuidance != "" {diff --git a/agent/toolcatalog.go b/agent/toolcatalog.goindex 276dbed..36fa00a 100644--- a/agent/toolcatalog.go+++ b/agent/toolcatalog.go@@ -3,6 +3,7 @@ package agentimport ("context""encoding/json"+ "sync"actool "github.com/Autumn-27/norma/tool")@@ -121,13 +122,34 @@ func BuiltinToolSeeds() []ToolSeed {return out}-// ToolResolve, if set, post-processes an agent's fully-assembled tool list against+// ToolResolveFunc is the ToolResolve hook. If set, it post-processes an agent's+// fully-assembled tool list against// the DB tools table: it drops tools not bound to this agent (or globally disabled)// and wraps the rest so the model sees the DB-overridden description/schema and// default inputs get injected. Tools with no matching DB row (MCP/skill/host tools// like traffic) pass through untouched. nil = tools unchanged. Wired in// server/assembly.go.-var ToolResolve func(ctx context.Context, agentKey string, tools []actool.CoreTool) []actool.CoreTool+type ToolResolveFunc func(ctx context.Context, agentKey string, tools []actool.CoreTool) []actool.CoreTool++// Guarded like toolAugment: wiring can run while tasks assemble tools.+var (+ toolResolveMu sync.RWMutex+ toolResolve ToolResolveFunc+)++// SetToolResolve installs (or, with nil, removes) the ToolResolve hook.+func SetToolResolve(f ToolResolveFunc) {+ toolResolveMu.Lock()+ toolResolve = f+ toolResolveMu.Unlock()+}++// ToolResolve returns the current ToolResolve hook, or nil when none is set.+func ToolResolve() ToolResolveFunc {+ toolResolveMu.RLock()+ defer toolResolveMu.RUnlock()+ return toolResolve+}// DecorateTool wraps t so Description()/InputSchema() report the DB overrides and// Call() injects scalar parameter defaults (from schema's "default" props) wheneverdiff --git a/agent/toolrag_test.go b/agent/toolrag_test.goindex 51ceaad..4c73090 100644--- a/agent/toolrag_test.go+++ b/agent/toolrag_test.go@@ -82,12 +82,17 @@ func TestToolRAGCoreKeptAndKRespected(t *testing.T) {// The projection runs after ToolResolve, so a tool the binding/enabled filter// dropped is never selected even when it is the best lexical match.func TestToolRAGNeverSelectsUnboundOrDisabled(t *testing.T) {- oldResolve, oldAugment, oldPolicy := ToolResolve, ToolAugment, FindingTrafficBindingEnabled- t.Cleanup(func() { ToolResolve, ToolAugment, FindingTrafficBindingEnabled = oldResolve, oldAugment, oldPolicy })- ToolAugment, FindingTrafficBindingEnabled = nil, nil+ oldResolve, oldAugment, oldPolicy := ToolResolve(), ToolAugment(), FindingTrafficBindingEnabled()+ t.Cleanup(func() {+ SetToolResolve(oldResolve)+ SetToolAugment(oldAugment)+ SetFindingTrafficBindingEnabled(oldPolicy)+ })+ SetToolAugment(nil)+ SetFindingTrafficBindingEnabled(nil)t.Setenv(toolRAGEnv, "on")t.Setenv(toolRAGTopKEnv, "2")- ToolResolve = func(_ context.Context, _ string, in []actool.CoreTool) []actool.CoreTool {+ SetToolResolve(func(_ context.Context, _ string, in []actool.CoreTool) []actool.CoreTool {var out []actool.CoreToolfor _, tool := range in {if tool.Name() != "sql_injection_exploit" {@@ -95,7 +100,7 @@ func TestToolRAGNeverSelectsUnboundOrDisabled(t *testing.T) {}}return out- }+ })base := append(ragCatalog(25), ragTool("sql_injection_exploit", "Exploit SQL injection in web login form", "url"))out, _, cleanup := AugmentTools(WithToolQuery(t.Context(), ragQuery), "worker", base)defer cleanup()diff --git a/server/assembly.go b/server/assembly.goindex 55fde2b..24bff8b 100644--- a/server/assembly.go+++ b/server/assembly.go@@ -29,7 +29,7 @@ const mcpUntrustedNote = "\nOutput of this tool may contain attacker-controlled// capture is on) to add to EVERY agent's base list — the DB tools table then// filters them per-agent binding. Empty/nil → no host tools this run (capture off).func wireAgentAugment(pg *db.DB, skillDir string, hostTools func() ([]actool.CoreTool, map[string][]string)) {- agent.ToolAugment = func(ctx context.Context, agentKey string) ([]actool.CoreTool, agent.DeferredInfo, func()) {+ agent.SetToolAugment(func(ctx context.Context, agentKey string) ([]actool.CoreTool, agent.DeferredInfo, func()) {a, err := pg.GetAgentByKey(agentKey)if err != nil || a == nil {return nil, agent.DeferredInfo{}, nil@@ -188,7 +188,7 @@ func wireAgentAugment(pg *db.DB, skillDir string, hostTools func() ([]actool.CorUnlockSkill: unlockSkill,}return extra, def, cleanup- }+ })}func idSet(ids []int64) map[int64]bool {@@ -222,7 +222,7 @@ func seedPrompts(pg *db.DB) {// flag and, if kept, wrapped so the model sees the DB-overridden description/schema// and default params get injected. MCP/skill/host tools have no row and pass through.func wireTools(pg *db.DB, domainReg map[string]actool.CoreTool) {- agent.FindingTrafficBindingEnabled = func() bool { return pg.GetBool(settingAgentTrafficBinding, false) }+ agent.SetFindingTrafficBindingEnabled(func() bool { return pg.GetBool(settingAgentTrafficBinding, false) })// Seed the built-in domain tools (first-insert only; DO NOTHING preserves edits).// No startup prune: rows we didn't seed are left alone so future user-defined// custom tools (system=false, added via the UI) survive restarts.@@ -248,7 +248,7 @@ func wireTools(pg *db.DB, domainReg map[string]actool.CoreTool) {// interactive_shell is on, so Bash points at shell_open for interactive programs// without ever referencing a tool that isn't injected (§14.1/§14.2).const bashInteractiveShellNote = "\n\nPrograms that need [interactive input] (msfconsole / interactive ssh login / REPLs like mysql, psql, python / password or yes/no prompts / nc reverse shells) must not use Bash (it has no stdin and would hang); open an interactive session with shell_open instead (and shell_close when done). One-shot, non-interactive commands still use Bash."- agent.ToolResolve = func(ctx context.Context, agentKey string, tools []actool.CoreTool) []actool.CoreTool {+ agent.SetToolResolve(func(ctx context.Context, agentKey string, tools []actool.CoreTool) []actool.CoreTool {rows, err := pg.ListTools()if err != nil {log.Printf("[tools] failed to read tools table, passing through with code defaults: %v", err)@@ -333,7 +333,7 @@ func wireTools(pg *db.DB, domainReg map[string]actool.CoreTool) {}}return out- }+ })}func contains(ss []string, v string) bool {diff --git a/server/assembly_test.go b/server/assembly_test.goindex 1eabeba..e25866c 100644--- a/server/assembly_test.go+++ b/server/assembly_test.go@@ -49,7 +49,7 @@ func TestAssembleVisibleSkill(t *testing.T) {}wireAgentAugment(pg, skillDir, nil)- t.Cleanup(func() { agent.ToolAugment = nil })+ t.Cleanup(func() { agent.SetToolAugment(nil) })ag, _ := pg.GetAgentByKey("planner")if ag == nil {@@ -65,7 +65,7 @@ func TestAssembleVisibleSkill(t *testing.T) {t.Cleanup(func() { _ = pg.SetAgentSkillVisibility(ag.ID, orig) })// before: skill not visible → no Skill meta-tool- extra, _, cleanup := agent.ToolAugment(context.Background(), "planner")+ extra, _, cleanup := agent.ToolAugment()(context.Background(), "planner")cleanup()if hasSkillTool(extra) {t.Fatalf("expected no Skill meta-tool before the skill is made visible")@@ -78,7 +78,7 @@ func TestAssembleVisibleSkill(t *testing.T) {t.Cleanup(func() { _ = pg.ToggleSkillVisibility(ag.ID, skillName, false) })// after: the Skill meta-tool is packed in- extra, _, cleanup = agent.ToolAugment(context.Background(), "planner")+ extra, _, cleanup = agent.ToolAugment()(context.Background(), "planner")defer cleanup()if !hasSkillTool(extra) {t.Fatalf("expected the Skill meta-tool after making the skill visible")diff --git a/server/fact_source_schema_test.go b/server/fact_source_schema_test.goindex b180ec9..a677658 100644--- a/server/fact_source_schema_test.go+++ b/server/fact_source_schema_test.go@@ -24,7 +24,7 @@ func TestRecordFactSourceSchemaUpgrade(t *testing.T) {ctx, cancel := context.WithCancel(context.Background())cancel()s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())- t.Cleanup(func() { agent.ToolResolve = nil })+ t.Cleanup(func() { agent.SetToolResolve(nil) })pg := m.pgold, err := pg.GetTool("record_fact")@@ -96,7 +96,7 @@ func TestRecordFactSourceSchemaUpgrade(t *testing.T) {// serves it to the worker.func resolvedRecordFactProps(t *testing.T) map[string]any {t.Helper()- tools := agent.ToolResolve(context.Background(), "worker", agent.NewToolSet(nil, "").WorkerTools())+ tools := agent.ToolResolve()(context.Background(), "worker", agent.NewToolSet(nil, "").WorkerTools())for _, tl := range tools {if tl.Name() == "record_fact" {props, _ := tl.InputSchema()["properties"].(map[string]any)diff --git a/server/finding_retests_test.go b/server/finding_retests_test.goindex 860b860..b5d6f39 100644--- a/server/finding_retests_test.go+++ b/server/finding_retests_test.go@@ -55,16 +55,18 @@ func newRetestServer(t *testing.T) (*Server, int64) {if err != nil {t.Fatal(err)}- oldAugment, oldResolve, oldPrompt := agent.ToolAugment, agent.ToolResolve, agent.PromptOverride- agent.ToolAugment = func(context.Context, string) ([]actool.CoreTool, agent.DeferredInfo, func()) {+ oldAugment, oldResolve, oldPrompt := agent.ToolAugment(), agent.ToolResolve(), agent.PromptOverride+ agent.SetToolAugment(func(context.Context, string) ([]actool.CoreTool, agent.DeferredInfo, func()) {return s.findingRetestTools(), agent.DeferredInfo{}, func() {}- }- agent.ToolResolve = nil+ })+ agent.SetToolResolve(nil)agent.PromptOverride = func(string) (string, bool) { return agent.RetesterDefaultPrompt, true }t.Cleanup(func() {cancel()waitRetestIdle(t, s)- agent.ToolAugment, agent.ToolResolve, agent.PromptOverride = oldAugment, oldResolve, oldPrompt+ agent.SetToolAugment(oldAugment)+ agent.SetToolResolve(oldResolve)+ agent.PromptOverride = oldPrompt_, _ = pg.Exec(`DELETE FROM conversations WHERE id IN (SELECT conversation_id FROM finding_retests WHERE finding_id=$1)`, fid)_, _ = pg.DeleteFinding(fid)pg.Close()diff --git a/server/task_scope_description_test.go b/server/task_scope_description_test.goindex d943385..2dee374 100644--- a/server/task_scope_description_test.go+++ b/server/task_scope_description_test.go@@ -23,7 +23,7 @@ func TestTaskScopeDescriptionUpgrade(t *testing.T) {ctx, cancel := context.WithCancel(context.Background())cancel()s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())- t.Cleanup(func() { agent.ToolResolve = nil })+ t.Cleanup(func() { agent.SetToolResolve(nil) })pg := m.pgvar want string@@ -84,7 +84,7 @@ func TestTaskScopeDescriptionUpgrade(t *testing.T) {// ToolResolve serves it to the planner.func resolvedTaskScopeDescription(t *testing.T) string {t.Helper()- tools := agent.ToolResolve(context.Background(), "planner", agent.NewToolSet(nil, "").PlannerTools())+ tools := agent.ToolResolve()(context.Background(), "planner", agent.NewToolSet(nil, "").PlannerTools())for _, tl := range tools {if tl.Name() == "add_task_scope" {return tl.Description()diff --git a/server/tools_wire_test.go b/server/tools_wire_test.goindex 3e00bfb..4fa1323 100644--- a/server/tools_wire_test.go+++ b/server/tools_wire_test.go@@ -33,7 +33,7 @@ func TestWireTools(t *testing.T) {defer pg.Close()wireTools(pg, nil) // nil domainReg: test only covers filter/decoration, not injection- t.Cleanup(func() { agent.ToolResolve = nil })+ t.Cleanup(func() { agent.SetToolResolve(nil) })// Prevent interactive-shell Bash decoration: if the worker agent has// interactive_shell=true in the DB, ToolResolve would append a note to// Bash's description. Disable that for this test so the undecorated@@ -52,7 +52,7 @@ func TestWireTools(t *testing.T) {base := append(ts.WorkerTools(), actool.DefaultTools()...)ctx := context.Background()- worker := names(agent.ToolResolve(ctx, "worker", base))+ worker := names(agent.ToolResolve()(ctx, "worker", base))if _, ok := worker["record_fact"]; !ok {t.Error("worker lost record_fact")}@@ -65,7 +65,7 @@ func TestWireTools(t *testing.T) {t.Error("Bash should pass through undecorated, but description changed")}// planner is not bound to record_fact → resolving a base that contains it drops it.- planner := names(agent.ToolResolve(ctx, "planner", base))+ planner := names(agent.ToolResolve()(ctx, "planner", base))if _, ok := planner["record_fact"]; ok {t.Error("planner should not get record_fact (not bound)")}@@ -85,7 +85,7 @@ func TestWireTools(t *testing.T) {t.Fatal(err)}- worker2 := names(agent.ToolResolve(ctx, "worker", base))+ worker2 := names(agent.ToolResolve()(ctx, "worker", base))got := worker2["record_fact"]if got == nil {t.Fatal("record_fact missing after edit")