Refactor agent instance initialization to consolidate component setup and remove unused code and helper functions.

This commit is contained in:
0x5487 2026-02-25 22:59:51 +08:00
parent a547cbfd85
commit 7a402c4972
4 changed files with 7 additions and 34 deletions

View file

@ -48,20 +48,6 @@ func NewAgentInstance(
model := resolveAgentModel(agentCfg, defaults)
fallbacks := resolveAgentFallbacks(agentCfg, defaults)
restrict := defaults.RestrictToWorkspace
toolsRegistry := tools.NewToolRegistry()
toolsRegistry.Register(tools.NewReadFileTool(workspace, restrict))
toolsRegistry.Register(tools.NewWriteFileTool(workspace, restrict))
toolsRegistry.Register(tools.NewListDirTool(workspace, restrict))
toolsRegistry.Register(tools.NewExecToolWithConfig(workspace, restrict, cfg))
toolsRegistry.Register(tools.NewEditFileTool(workspace, restrict))
toolsRegistry.Register(tools.NewAppendFileTool(workspace, restrict))
sessionsDir := filepath.Join(workspace, "sessions")
sessionsManager := session.NewSessionManager(sessionsDir)
contextBuilder := NewContextBuilder(workspace)
agentID := routing.DefaultAgentID
agentName := ""
var subagents *config.SubagentsConfig
@ -73,9 +59,9 @@ func NewAgentInstance(
skillsFilter = agentCfg.Skills
}
restrict = defaults.RestrictToWorkspace
restrict := defaults.RestrictToWorkspace
roContainer := isContainerReadOnlySandbox(cfg)
toolsRegistry = tools.NewToolRegistry()
toolsRegistry := tools.NewToolRegistry()
sandboxManager := sandbox.NewFromConfigWithAgent(workspace, restrict, cfg, agentID)
isSandboxAllowed := func(toolName string) bool {
@ -103,10 +89,10 @@ func NewAgentInstance(
}
}
sessionsDir = filepath.Join(workspace, "sessions")
sessionsManager = session.NewSessionManager(sessionsDir)
sessionsDir := filepath.Join(workspace, "sessions")
sessionsManager := session.NewSessionManager(sessionsDir)
contextBuilder = NewContextBuilder(workspace)
contextBuilder := NewContextBuilder(workspace)
maxIter := defaults.MaxToolIterations
if maxIter == 0 {

View file

@ -75,8 +75,8 @@ func validateBindMounts(binds []string) error {
if err := validateBindSourcePath(bind, normalized); err != nil {
return err
}
if real := tryRealpathAbsolute(normalized); real != normalized {
if err := validateBindSourcePath(bind, real); err != nil {
if resolvedPath := tryRealpathAbsolute(normalized); resolvedPath != normalized {
if err := validateBindSourcePath(bind, resolvedPath); err != nil {
return err
}
}

View file

@ -362,7 +362,3 @@ func DefaultConfig() *Config {
},
}
}
func int64Ptr(v int64) *int64 {
return &v
}

View file

@ -72,15 +72,6 @@ func (s *cronStubSandbox) ExecStream(
return &sandbox.ExecResult{Stdout: "ok", ExitCode: 0}, nil
}
type noopExecutor struct{}
func (n *noopExecutor) ProcessDirectWithChannel(
ctx context.Context,
content, sessionKey, channel, chatID string,
) (string, error) {
return "ok", nil
}
func TestCronTool_ExecuteJob_BlocksDangerousCommandViaGuard(t *testing.T) {
msgBus := bus.NewMessageBus()
sb := &cronStubSandbox{}