extract rebuildAgentRuntime, remove nil guards on things that must not be nil

This commit is contained in:
Levi Neely 2026-08-14 08:47:59 +02:00
parent 785fe0996f
commit c61fada3e9
2 changed files with 29 additions and 38 deletions

View File

@ -508,18 +508,14 @@ func (ag *Agent) SetSessionEnv(sessionID string) {
// SetEnv stores an environment variable on the execute server.
func (ag *Agent) SetEnv(key, value string) {
if ag.runtime.ToolServer == nil {
return
}
ag.runtime.ToolServer.SetEnv(key, value)
}
// Close releases agent resources (dispatcher, execute server).
func (ag *Agent) Close() {
if ag.runtime.ToolServer == nil {
return
if ag.runtime.ToolServer != nil {
ag.runtime.ToolServer.Close()
}
ag.runtime.ToolServer.Close()
}
// SetCWD updates the agent's working directory, preamble environment section, and dispatcher.
@ -527,12 +523,9 @@ func (ag *Agent) SetCWD(dir string) {
ag.stateMu.Lock()
ag.cwd = dir
ag.stateMu.Unlock()
// Rebuild the environment section with the new cwd
if ag.runtime != nil && ag.runtime.Preamble != nil {
isGitRepo := paths.IsGitRepo(dir)
ag.envBlock = EnvironmentBlock(dir, runtime.GOOS, isGitRepo, "")
ag.runtime.Preamble.Set(SectionEnv, ag.envBlock)
}
isGitRepo := paths.IsGitRepo(dir)
ag.envBlock = EnvironmentBlock(dir, runtime.GOOS, isGitRepo, "")
ag.runtime.Preamble.Set(SectionEnv, ag.envBlock)
if ag.runtime.ToolServer != nil {
ag.runtime.ToolServer.SetCWD(dir)
}
@ -541,9 +534,6 @@ func (ag *Agent) SetCWD(dir string) {
// RefreshTools forces a refresh of the tool listing in the agent's preamble.
func (ag *Agent) RefreshTools() {
if ag.runtime == nil || ag.runtime.Preamble == nil || ag.runtime.ToolServer == nil {
return
}
if infos, err := ag.runtime.ToolServer.ListTools(); err == nil {
ag.runtime.Preamble.Set(SectionTools, RenderTools(infos))
}
@ -551,9 +541,7 @@ func (ag *Agent) RefreshTools() {
// SetToolsPreamble updates the tools section of the preamble.
func (ag *Agent) SetToolsPreamble(content string) {
if ag.runtime != nil && ag.runtime.Preamble != nil {
ag.runtime.Preamble.Set(SectionTools, content)
}
ag.runtime.Preamble.Set(SectionTools, content)
}
// CtxSz returns a human-readable context size string.

View File

@ -338,27 +338,9 @@ func (s *Session) Resume() error {
// Rebuild full runtimes for restored agents (they have stub runtimes from persist).
for _, ag := range s.agents {
cfg := agent.LoadConfig(pkgAgentsDir, ag.Profile(), os.Open)
layers := BuildPromptLayers(cfg, ag.Cwd(), s.ID, ag.ID(), "linux", false, "")
env := []string{"OLLIE_SESSION_ID=" + s.ID, "OLLIE_UNAME=" + ag.ID()}
rt := agent.BuildRuntime(cfg, ag.ToolServer(), ag.Cwd(), env, layers.SystemPrompt, layers.EnvBlock)
beName := ""
if cfg != nil {
beName = cfg.Backend
if err := rebuildAgentRuntime(ag, s.ID); err != nil {
pkgLog.Error("resume agent %s: %v", ag.ID(), err)
}
be, err := backend.NewWithName(beName)
if err != nil {
pkgLog.Error("resume agent %s: backend: %v", ag.ID(), err)
continue
}
if cfg != nil && cfg.Model != "" {
be.SetModel(cfg.Model)
}
rt.Backend = be
ag.SetRuntime(rt)
LoadAutoLoadTools(cfg, ag.ToolServer(), s.ID, ag.ID(), func(f string, a ...any) {
pkgLog.Error("resume agent %s: "+f, append([]any{ag.ID()}, a...)...)
})
}
// Start feed consumers for restored agents (context now available).
@ -370,6 +352,27 @@ func (s *Session) Resume() error {
return nil
}
// rebuildAgentRuntime rebuilds the full runtime for an agent (used on resume).
func rebuildAgentRuntime(ag *agent.Agent, sessID string) error {
cfg := agent.LoadConfig(pkgAgentsDir, ag.Profile(), os.Open)
layers := BuildPromptLayers(cfg, ag.Cwd(), sessID, ag.ID(), "linux", false, "")
env := []string{"OLLIE_SESSION_ID=" + sessID, "OLLIE_UNAME=" + ag.ID()}
rt := agent.BuildRuntime(cfg, ag.ToolServer(), ag.Cwd(), env, layers.SystemPrompt, layers.EnvBlock)
be, err := backend.NewWithName(cfg.Backend)
if err != nil {
return fmt.Errorf("backend: %w", err)
}
if cfg.Model != "" {
be.SetModel(cfg.Model)
}
rt.Backend = be
ag.SetRuntime(rt)
LoadAutoLoadTools(cfg, ag.ToolServer(), sessID, ag.ID(), func(f string, a ...any) {
pkgLog.Error("session %s agent %s: "+f, append([]any{sessID, ag.ID()}, a...)...)
})
return nil
}
// --- Tool loading ---
// LoadTool loads a tool into the tool server.