fix(agent): preserve explicit direct session keys

This commit is contained in:
qs3c 2026-03-04 17:50:26 +08:00
parent b82bb9acc0
commit 9221b1b3d7
2 changed files with 48 additions and 2 deletions

View file

@ -485,9 +485,9 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
} }
} }
// Use routed session key, but honor pre-set agent-scoped keys (for ProcessDirect/cron) // Preserve explicit session keys for direct invocations and other internal callers.
sessionKey := route.SessionKey sessionKey := route.SessionKey
if msg.SessionKey != "" && strings.HasPrefix(msg.SessionKey, "agent:") { if msg.SessionKey != "" {
sessionKey = msg.SessionKey sessionKey = msg.SessionKey
} }

View file

@ -15,6 +15,7 @@ import (
"github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/config"
"github.com/sipeed/picoclaw/pkg/media" "github.com/sipeed/picoclaw/pkg/media"
"github.com/sipeed/picoclaw/pkg/providers" "github.com/sipeed/picoclaw/pkg/providers"
"github.com/sipeed/picoclaw/pkg/routing"
"github.com/sipeed/picoclaw/pkg/tools" "github.com/sipeed/picoclaw/pkg/tools"
) )
@ -603,6 +604,51 @@ func TestAgentLoop_ContextExhaustionRetry(t *testing.T) {
} }
} }
func TestProcessDirectWithChannel_PreservesExplicitSessionKey(t *testing.T) {
al, _, _, _, cleanup := newTestAgentLoop(t)
defer cleanup()
defaultAgent := al.registry.GetDefaultAgent()
if defaultAgent == nil {
t.Fatal("No default agent found")
}
explicitSessionKey := "custom-session"
routedSessionKey := al.registry.ResolveRoute(routing.RouteInput{
Channel: "cli",
}).SessionKey
if routedSessionKey == "" {
t.Fatal("Expected routed session key to be set")
}
if routedSessionKey == explicitSessionKey {
t.Fatalf("Test requires different routed and explicit session keys, both were %q", explicitSessionKey)
}
response, err := al.ProcessDirectWithChannel(
context.Background(),
"hello from cli",
explicitSessionKey,
"cli",
"direct",
)
if err != nil {
t.Fatalf("ProcessDirectWithChannel failed: %v", err)
}
if response != "Mock response" {
t.Fatalf("Expected mock response, got %q", response)
}
explicitHistory := defaultAgent.Sessions.GetHistory(explicitSessionKey)
if len(explicitHistory) == 0 {
t.Fatal("Expected explicit session history to be written")
}
routedHistory := defaultAgent.Sessions.GetHistory(routedSessionKey)
if len(routedHistory) != 0 {
t.Fatalf("Expected routed session history to stay empty, got %d entries", len(routedHistory))
}
}
func TestTargetReasoningChannelID_AllChannels(t *testing.T) { func TestTargetReasoningChannelID_AllChannels(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "agent-test-*") tmpDir, err := os.MkdirTemp("", "agent-test-*")
if err != nil { if err != nil {