refactor(agent): route /clear through ContextManager.Clear for all agents

Address review feedback: the routed-agent /clear path bypassed the
public ContextManager.Clear contract via an unexported hook. Restore
the single Clear call in the command path and resolve the session's
owning agent inside the built-in implementations instead:

- legacy: Clear resolves the owning agent via agentForSession instead
  of assuming the default agent
- seahorse: drop ClearContextStore; Clear wipes the engine state and
  the owning agent's session store
- command path: persist session scope metadata before Clear so
  ownership resolves even when /clear is the session's first message
- add coverage that a custom ContextManager receives Clear for routed
  agents

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Ethan1918andClaude Fable 5 committed 2026-07-04 14:04:37 +08:00
1 parent c4fb7a2001
commit ffffb6a0cb
4 files changed
+123 -46

No files matched your search

+10 -35
View File
@@ -333,7 +333,16 @@ func (al *AgentLoop) buildCommandsRuntime(
if opts == nil {
return fmt.Errorf("process options not available")
}
return al.clearAgentSessionContext(ctx, agent, opts.SessionKey)
// /clear can arrive before any turn has persisted session scope
// metadata (runAgentLoop records it per turn), so record it here to
// let the ContextManager resolve which agent owns the session.
ensureSessionMetadata(
agent.Sessions,
opts.Dispatch.SessionKey,
opts.Dispatch.SessionScope,
opts.Dispatch.SessionAliases,
)
return al.contextManager.Clear(ctx, opts.SessionKey)
}
rt.AskSideQuestion = func(ctx context.Context, question string) (string, error) {
@@ -363,40 +372,6 @@ func (al *AgentLoop) buildCommandsRuntime(
return rt
}
type contextStoreClearer interface {
ClearContextStore(ctx context.Context, sessionKey string) error
}
func (al *AgentLoop) clearAgentSessionContext(
ctx context.Context,
agent *AgentInstance,
sessionKey string,
) error {
if agent == nil || agent.Sessions == nil {
return fmt.Errorf("sessions not initialized")
}
if al != nil && al.registry != nil && agent == al.registry.GetDefaultAgent() {
if al.contextManager != nil {
return al.contextManager.Clear(ctx, sessionKey)
}
return clearAgentSessionStore(agent, sessionKey)
}
if al != nil && al.contextManager != nil {
if clearer, ok := al.contextManager.(contextStoreClearer); ok {
if err := clearer.ClearContextStore(ctx, sessionKey); err != nil {
return err
}
}
}
return clearAgentSessionStore(agent, sessionKey)
}
func clearAgentSessionStore(agent *AgentInstance, sessionKey string) error {
agent.Sessions.SetHistory(sessionKey, []providers.Message{})
agent.Sessions.SetSummary(sessionKey, "")
return agent.Sessions.Save(sessionKey)
}
func summarizeMCPToolParameters(schema any) []commands.MCPToolParameterInfo {
schemaMap := normalizeMCPSchema(schema)
properties, ok := schemaMap["properties"].(map[string]any)