diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index ae1671717..a16fc5cec 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -155,11 +155,8 @@ func NewAgentLoop( // Initialize fork-specific fields (stats, sessions, orchestration, gcLoop). al.initLoopExt(cfg, registry, len(enableStats) > 0 && enableStats[0]) - // Register shared tools to all agents. - registerSharedTools(cfg, msgBus, registry, provider) - - // Register fork-specific orchestration tools (needs al for reporter injection). - al.registerAllOrchestrationTools(cfg, registry, provider, msgBus) + // Register shared tools to all agents (needs al for orchestration reporter). + registerSharedTools(cfg, msgBus, registry, provider, al) return al } @@ -170,6 +167,7 @@ func registerSharedTools( msgBus *bus.MessageBus, registry *AgentRegistry, provider providers.LLMProvider, + al *AgentLoop, ) { for _, agentID := range registry.ListAgentIDs() { agent, ok := registry.GetAgent(agentID) @@ -299,6 +297,9 @@ func registerSharedTools( } } + // Orchestration tools (spawn, subagent, answer, review_plan) + registerOrchestrationTools(cfg, agent, agentID, registry, provider, msgBus, al) + // Update context builder with the complete tools registry agent.ContextBuilder.SetToolsRegistry(agent.Tools) } @@ -642,8 +643,7 @@ func (al *AgentLoop) ReloadProviderAndConfig( } // Ensure shared tools are re-registered on the new registry - registerSharedTools(cfg, al.bus, registry, provider) - al.registerAllOrchestrationTools(cfg, registry, provider, al.bus) + registerSharedTools(cfg, al.bus, registry, provider, al) // Atomically swap the config and registry under write lock // This ensures readers see a consistent pair @@ -1094,10 +1094,6 @@ func (al *AgentLoop) withTelegramThread(channel, chatID string, threadID int) st return fmt.Sprintf("%s/%d", baseChatID, threadID) } -func (al *AgentLoop) runAgentLoop(ctx context.Context, agent *AgentInstance, opts processOptions) (string, error) { - return al.runAgentLoopImpl(ctx, agent, opts) -} - func (al *AgentLoop) targetReasoningChannelID(channelName string) (chatID string) { if al.channelManager == nil { return "" diff --git a/pkg/agent/loop_ext.go b/pkg/agent/loop_ext.go index 46807cbb0..4ef4aaecf 100644 --- a/pkg/agent/loop_ext.go +++ b/pkg/agent/loop_ext.go @@ -107,24 +107,6 @@ func (al *AgentLoop) SetHeartbeatThreadUpdater(fn func(int)) { al.onHeartbeatThreadUpdate = fn } -// registerAllOrchestrationTools iterates all agents and registers orchestration -// tools for those with subagents enabled. Called from NewAgentLoop after -// registerSharedTools, keeping the upstream function signature clean. -func (al *AgentLoop) registerAllOrchestrationTools( - cfg *config.Config, - registry *AgentRegistry, - provider providers.LLMProvider, - msgBus *bus.MessageBus, -) { - for _, agentID := range registry.ListAgentIDs() { - agent, ok := registry.GetAgent(agentID) - if !ok { - continue - } - registerOrchestrationTools(cfg, agent, agentID, registry, provider, msgBus, al) - } -} - // registerOrchestrationTools registers spawn, subagent, answer, and review_plan // tools for agents with orchestration enabled. func registerOrchestrationTools( diff --git a/pkg/agent/loop_run.go b/pkg/agent/loop_run.go index 99bfc2ea3..8e0a82620 100644 --- a/pkg/agent/loop_run.go +++ b/pkg/agent/loop_run.go @@ -19,9 +19,8 @@ import ( "github.com/sipeed/picoclaw/pkg/utils" ) -// runAgentLoopImpl is the full implementation of runAgentLoop, extracted -// to keep loop.go minimal and reduce upstream merge conflicts. -func (al *AgentLoop) runAgentLoopImpl(ctx context.Context, agent *AgentInstance, opts processOptions) (string, error) { +// runAgentLoop is the main message processing loop for a single agent session. +func (al *AgentLoop) runAgentLoop(ctx context.Context, agent *AgentInstance, opts processOptions) (string, error) { // -1. Acquire per-session lock to prevent concurrent access on the same session if !al.acquireSessionLock(ctx, opts.SessionKey) {