Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/agent/agent_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"maps"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -69,8 +70,17 @@ func outboundTurnMetadata(
return agentID, sessionKey, outboundScopeFromSessionScope(scope)
}

// stripToolUseText removes [tool_use: ...] patterns that LLMs may regurgitate
// after seeing them in seahorse FTS5 / summary contexts.
func stripToolUseText(content string) string {
// Match "[tool_use: name, args: {...}]" variants
re := regexp.MustCompile(`\[tool_use:\s*\w+,\s*args:\s*\{[^}]*}\]?\]?\s*`)
return strings.TrimSpace(re.ReplaceAllString(content, ""))
}

func outboundMessageForTurn(ts *turnState, content string) bus.OutboundMessage {
agentID, sessionKey, scope := outboundTurnMetadata(ts.agent.ID, ts.sessionKey, ts.opts.Dispatch.SessionScope)
content = stripToolUseText(content)
return bus.OutboundMessage{
Channel: ts.channel,
ChatID: ts.chatID,
Expand Down
20 changes: 13 additions & 7 deletions pkg/seahorse/short_compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sort"
"strings"
"time"

"github.com/sipeed/picoclaw/pkg/logger"
Expand Down Expand Up @@ -746,11 +747,16 @@ func (e *CompactionEngine) runCondensedLoop(ctx context.Context, convID int64) {
func formatMessagesForSummary(messages []Message) string {
var result string
for _, m := range messages {
ts := m.CreatedAt.Format("2006-01-02 15:04 MST")
content := m.Content
if content == "" && len(m.Parts) > 0 {
content = partsToReadableContent(m.Parts)
// Skip tool-only messages: tool calls and tool results are mechanical,
// not conversational content. Including them (even via partsToReadableContent)
// causes LLMs to mimic the format in their own output, leaking mock tool-call
// text to users. Only user/assistant messages with actual text content belong
// in summaries.
content := strings.TrimSpace(m.Content)
if content == "" {
continue
}
ts := m.CreatedAt.Format("2006-01-02 15:04 MST")
result += fmt.Sprintf("[%s]\n%s\n\n", ts, content)
}
return result
Expand Down Expand Up @@ -849,9 +855,9 @@ Output requirements:
func truncateSummary(messages []Message) string {
content := ""
for _, m := range messages {
c := m.Content
if c == "" && len(m.Parts) > 0 {
c = partsToReadableContent(m.Parts)
c := strings.TrimSpace(m.Content)
if c == "" {
continue // skip tool-only messages; see formatMessagesForSummary
}
content += c + "\n"
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/seahorse/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ func partsToReadableContent(parts []MessagePart) string {
case "text":
b.WriteString(p.Text)
case "tool_use":
fmt.Fprintf(&b, "[tool_use: %s, args: %s]", p.Name, p.Arguments)
fmt.Fprintf(&b, "🔧 %s", p.Name)
case "tool_result":
fmt.Fprintf(&b, "[tool_result for %s: %s]", p.ToolCallID, p.Text)
fmt.Fprintf(&b, "📋 %s", p.Text)
case "media":
fmt.Fprintf(&b, "[media: %s (%s)]", p.MediaURI, p.MimeType)
default:
Expand Down