claude bridge: Claude Desktop's own annotations are not the user's words (#5403)
Claude Desktop's Claude Code appends notes of its own after tool
results: the scaling note a wide Read gets ("[Image: original 2200x680,
displayed at 2000x618. Multiply coordinates by 1.10 …]"), the Skill
tool's "Base directory for this skill: …" and ToolSearch's "Tool
loaded.". The bridge took each for words the user sent mid-turn, so
the run was told "The user sent a new message while you were working:"
with the note, and the next turn's history marked it [user message].
The model answered messages nobody wrote (#5403, miroku9: about 30
such notes in 13 sessions, two answered).
They join agentNotice, the table #2060 built for hook notices and
token counts, so saidWithResults leaves them out and renderParts
closes the result without the [user message] mark. Wide images read
in parallel bring one note each; miroku9's inner-session queue shows
them joined by blank lines, so the image branch matches one or more
notes (with "Tool loaded.") separated by whitespace, both as one text
and as separate parts. A user's own words after a note still go on.
Patch from ggbdpq; the parallel-notes case and its test are added
here, after miroku9's follow-up on the issue.
Run (mote-run, go test -tags nogui ./internal/gateway/):
- TestDesktopAnnotations*, TestParallelImageNotesAreAgentNotices,
TestAgentNotice*: with claude_subscription.go at origin/main all four
new tests FAIL ("… was taken for the user's words", "[user message]"
before the note); with the fix ok.
- With the patch's single-note regex, TestParallelImageNotes FAILs on
the joined text only (the separate-parts case passes).
- -run 'Notice|MidTurn|SaidWith|RenderParts|UserWords|Claude': ok 74.8s.
- gofmt -l clean; GOOS=linux go vet -tags nogui and GOOS=windows go vet
of internal/gateway clean.
Not tried: a live Claude Desktop + inner Claude Code pairing; the note
shapes are the reporter's jsonl verbatim. userWords (routing's view of
a user text) is unchanged: these notes only follow tool results, where
routing doesn't start a turn. A new kind of note from a later Claude
Code needs its own line here.
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// #5403 (miroku9): Claude Desktop appends its own annotations to what the
|
||||
// run reads as tool results — the image-scaling note a >2000px read gets,
|
||||
// a skill's base directory, ToolSearch's "Tool loaded." — and magpie took
|
||||
// each for words the user sent mid-turn: the model was told "The user sent
|
||||
// a new message while you were working: [Image: original 2200x680 …]"
|
||||
// though the user had sent nothing, and answered the phantom message. The
|
||||
// annotations are the agent's own (the image itself is already in the
|
||||
// result), so they are agent notices, and go where the rest go.
|
||||
func TestDesktopAnnotationsAreAgentNotices(t *testing.T) {
|
||||
for _, s := range []string{
|
||||
"[Image: original 2200x680, displayed at 2000x618. Multiply coordinates by 1.10 to map to original image.]",
|
||||
"Base directory for this skill: /home/user/.claude/skills/pdf",
|
||||
"Tool loaded.",
|
||||
} {
|
||||
if !agentNotice(s) {
|
||||
t.Errorf("%q was taken for the user's words", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// and they neither reach the run as a mid-turn message, nor split a tool
|
||||
// result off as the user's own words.
|
||||
func TestDesktopAnnotationsStayWithTheResult(t *testing.T) {
|
||||
note := "[Image: original 2200x680, displayed at 2000x618. Multiply coordinates by 1.10 to map to original image.]"
|
||||
msgs := []Message{
|
||||
{Role: "assistant", Parts: []Part{{Kind: ToolCall, CallID: "c1", Name: "Read"}}},
|
||||
{Role: "user", Parts: []Part{
|
||||
{Kind: ToolResult, CallID: "c1", Text: "the png's bytes as text"},
|
||||
{Kind: Text, Text: note},
|
||||
}},
|
||||
}
|
||||
if said := saidWithResults(msgs); said != "" {
|
||||
t.Errorf("the annotation went to the run as the user's: %q", said)
|
||||
}
|
||||
var sb strings.Builder
|
||||
renderParts(nil, &sb, msgs[len(msgs)-1].Parts, nil)
|
||||
if got := sb.String(); !strings.Contains(got, "[end of tool result id=c1]") || strings.Contains(got, "[user message]") {
|
||||
t.Errorf("the result should close with the annotation after it, not as the user's words; got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// a real mid-turn word still rides along, annotations or not.
|
||||
func TestDesktopAnnotationsLeaveTheUsersWords(t *testing.T) {
|
||||
msgs := []Message{
|
||||
{Role: "assistant", Parts: []Part{{Kind: ToolCall, CallID: "c1", Name: "Read"}}},
|
||||
{Role: "user", Parts: []Part{
|
||||
{Kind: ToolResult, CallID: "c1", Text: "the png's bytes as text"},
|
||||
{Kind: Text, Text: "[Image: original 2200x680, displayed at 2000x618. Multiply coordinates by 1.10 to map to original image.]"},
|
||||
{Kind: Text, Text: "now zoom into the top left corner please"},
|
||||
}},
|
||||
}
|
||||
said := saidWithResults(msgs)
|
||||
if said != "now zoom into the top left corner please" {
|
||||
t.Errorf("said %q, want the user's own words only", said)
|
||||
}
|
||||
}
|
||||
|
||||
// #5403 (miroku9): wide images read in parallel bring a note each, and
|
||||
// they arrive as separate parts or as one text joined by blank lines
|
||||
// (verbatim from the inner session's queue); neither is the user's.
|
||||
func TestParallelImageNotesAreAgentNotices(t *testing.T) {
|
||||
note := "[Image: original 2400x1350, displayed at 2000x1125. Multiply coordinates by 1.20 to map to original image.]"
|
||||
joined := note + "\n\n\n" + note + "\n\n\n" + note
|
||||
if !agentNotice(joined) {
|
||||
t.Errorf("three joined notes were taken for the user's words")
|
||||
}
|
||||
if agentNotice(note + "\n\n\nwhy is the second one blurry?") {
|
||||
t.Errorf("the user's words after a note were taken for the agent's")
|
||||
}
|
||||
for name, parts := range map[string][]Part{
|
||||
"one text": {{Kind: Text, Text: joined}},
|
||||
"parts": {{Kind: Text, Text: note}, {Kind: Text, Text: note}, {Kind: Text, Text: note}},
|
||||
} {
|
||||
msgs := []Message{
|
||||
{Role: "assistant", Parts: []Part{
|
||||
{Kind: ToolCall, CallID: "c1", Name: "Read"},
|
||||
{Kind: ToolCall, CallID: "c2", Name: "Read"},
|
||||
{Kind: ToolCall, CallID: "c3", Name: "Read"},
|
||||
}},
|
||||
{Role: "user", Parts: append([]Part{
|
||||
{Kind: ToolResult, CallID: "c1", Text: "a"},
|
||||
{Kind: ToolResult, CallID: "c2", Text: "b"},
|
||||
{Kind: ToolResult, CallID: "c3", Text: "c"},
|
||||
}, parts...)},
|
||||
}
|
||||
if said := saidWithResults(msgs); said != "" {
|
||||
t.Errorf("%s: the notes went to the run as the user's: %q", name, said)
|
||||
}
|
||||
var sb strings.Builder
|
||||
renderParts(nil, &sb, msgs[1].Parts, nil)
|
||||
if strings.Contains(sb.String(), "[user message]") {
|
||||
t.Errorf("%s: the notes were marked as the user's: %q", name, sb.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5262,6 +5262,14 @@ var (
|
||||
tokenCount = regexp.MustCompile(`^(?:<total_tokens>[^<]*</total_tokens>|Token usage: \S+; \S+ remaining)$`)
|
||||
// reminderTag is a <system-reminder> tag, open or close.
|
||||
reminderTag = regexp.MustCompile(`</?system-reminder(?:\s[^>]*)?>`)
|
||||
// desktopNote is an annotation Claude Desktop (its Claude Code) puts
|
||||
// beside what the run reads, telling it about the picture it scaled
|
||||
// down, where a skill's files live, or that a tool of ToolSearch's
|
||||
// was loaded: the agent's own words about its own work (#5403), never
|
||||
// the user's — the picture itself rides in the tool result. Wide
|
||||
// images read in parallel bring one note each, which may come as one
|
||||
// text joined by blank lines: every note in it is the agent's.
|
||||
desktopNote = regexp.MustCompile(`(?s)^(?:(?:\[Image: original \d+x\d+, displayed at \d+x\d+\. Multiply coordinates by [\d.]+ to map to original image\.\]|Tool loaded\.)\s*)+$|^Base directory for this skill: `)
|
||||
)
|
||||
|
||||
// midTurnSaid is the user's words in each message they sent mid-turn
|
||||
@@ -5289,7 +5297,7 @@ func midTurnSaid(text string) []string {
|
||||
// token count, or a notice userWords knows (injected).
|
||||
func agentNotice(text string) bool {
|
||||
t := strings.TrimSpace(reminderTag.ReplaceAllString(text, ""))
|
||||
if t == "" || hookNotice.MatchString(t) || tokenCount.MatchString(t) {
|
||||
if t == "" || hookNotice.MatchString(t) || tokenCount.MatchString(t) || desktopNote.MatchString(t) {
|
||||
return true
|
||||
}
|
||||
for _, lead := range injected {
|
||||
|
||||
Reference in New Issue
Block a user