Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Re-send open tool calls and the last uploaded message on refresh

Claudefa77decadf39
diff --git a/cli/beacon/cmd/share.go b/cli/beacon/cmd/share.go
index 67c067f1..4cd8579a 100644
--- a/cli/beacon/cmd/share.go
+++ b/cli/beacon/cmd/share.go
@@ -309,17 +309,47 @@ func runShareRefresh(cmd *cobra.Command, args []string) error {
// another source, or its earlier messages changed), it returns everything: an upsert of what the
// server already has changes nothing.
func messagesAfter(messages []asymptoteobserve.ShareMessageV1, lastID string, maxOrder int) []asymptoteobserve.ShareMessageV1 {
+ end := -1
if lastID != "" {
for i, m := range messages {
if m.ID == lastID {
- return messages[i+1:]
+ end = i
+ break
+ }
+ }
+ if end < 0 {
+ return messages
+ }
+ } else if maxOrder > 0 && maxOrder <= len(messages) {
+ end = maxOrder - 1
+ } else {
+ return messages
+ }
+ // A tool call gains its command, diff or path when its completion arrives, which updates the
+ // call message in place. Re-send from the earliest call the server holds without its result,
+ // and always the last message it holds; the server upserts by id, so a re-send is harmless.
+ return messages[firstOpenCall(messages[:end+1], end):]
+}
+
+// firstOpenCall is the index of the earliest tool_call in uploaded that has no tool_result among
+// them, or fallback when every call is resolved.
+func firstOpenCall(uploaded []asymptoteobserve.ShareMessageV1, fallback int) int {
+ resolved := map[string]bool{}
+ for _, m := range uploaded {
+ for _, part := range m.Parts {
+ if result, ok := part.Content.(*asymptoteobserve.ShareToolResultContent); ok && result.CallID != "" {
+ resolved[result.CallID] = true
}
}
}
- if maxOrder > 0 && maxOrder <= len(messages) && lastID == "" {
- return messages[maxOrder:]
+ for i, m := range uploaded {
+ for _, part := range m.Parts {
+ if call, ok := part.Content.(*asymptoteobserve.ShareToolCallContent); ok && call.CallID != "" && !resolved[call.CallID] {
+ return i
+ }
+ }
}
- return messages
+ return fallback
}
func validateShareFlags(cmd *cobra.Command) error {
diff --git a/cli/beacon/cmd/share_test.go b/cli/beacon/cmd/share_test.go
index 2cc6db49..206470e1 100644
--- a/cli/beacon/cmd/share_test.go
+++ b/cli/beacon/cmd/share_test.go
@@ -365,7 +365,8 @@ func TestShareUploadsAndRecordsTheShareLocally(t *testing.T) {
t.Fatalf("re-share duplicated messages: %d", len(srv.messages))
}
- // A refresh sends only what the server does not have.
+ // A refresh sends what the server does not have, plus the call it holds without a result
+ // (the prompt and the Bash call are on the server; the call is re-sent with its result).
srv.mu.Lock()
srv.messages = srv.messages[:2]
srv.mu.Unlock()
@@ -373,7 +374,7 @@ func TestShareUploadsAndRecordsTheShareLocally(t *testing.T) {
if err != nil {
t.Fatalf("refresh: %v", err)
}
- if err := json.Unmarshal([]byte(stdout), &result); err != nil || result.Data.Uploaded != 2 || result.Data.MessageCount != 4 {
+ if err := json.Unmarshal([]byte(stdout), &result); err != nil || result.Data.Uploaded != 3 || result.Data.MessageCount != 4 {
t.Fatalf("refresh result = %s (err %v)", stdout, err)
}
}
@@ -457,8 +458,23 @@ func TestShareRefreshParsesExternalIDs(t *testing.T) {
t.Errorf("parseShareExternalID(%q) = %q %q %v, want %+v", in, h, s, ok, want)
}
}
- if got := messagesAfter([]asymptoteobserve.ShareMessageV1{{ID: "a"}, {ID: "b"}, {ID: "c"}}, "b", 2); len(got) != 1 || got[0].ID != "c" {
- t.Fatalf("messagesAfter = %+v", got)
+ if got := messagesAfter([]asymptoteobserve.ShareMessageV1{{ID: "a"}, {ID: "b"}, {ID: "c"}}, "b", 2); len(got) != 2 || got[0].ID != "b" {
+ t.Fatalf("messagesAfter should resend the last uploaded message: %+v", got)
+ }
+ // A call uploaded before its result arrived is re-sent so the completion's command and diff
+ // reach the server.
+ call := func(id, callID string) asymptoteobserve.ShareMessageV1 {
+ return asymptoteobserve.ShareMessageV1{ID: id, Parts: []asymptoteobserve.SharePartV1{{Type: asymptoteobserve.SharePartToolCall, Content: &asymptoteobserve.ShareToolCallContent{CallID: callID}}}}
+ }
+ result := func(id, callID string) asymptoteobserve.ShareMessageV1 {
+ return asymptoteobserve.ShareMessageV1{ID: id, Parts: []asymptoteobserve.SharePartV1{{Type: asymptoteobserve.SharePartToolResult, Content: &asymptoteobserve.ShareToolResultContent{CallID: callID}}}}
+ }
+ msgs := []asymptoteobserve.ShareMessageV1{call("c1", "t1"), result("r1", "t1"), call("c2", "t2"), {ID: "text"}, result("r2", "t2"), {ID: "later"}}
+ if got := messagesAfter(msgs, "text", 0); len(got) != 4 || got[0].ID != "c2" {
+ t.Fatalf("open call not re-sent: %+v", got)
+ }
+ if got := messagesAfter(msgs, "r2", 0); len(got) != 2 || got[0].ID != "r2" {
+ t.Fatalf("resolved calls should not be re-sent: %+v", got)
}
if got := messagesAfter([]asymptoteobserve.ShareMessageV1{{ID: "a"}, {ID: "b"}}, "zzz", 9); len(got) != 2 {
t.Fatalf("unknown last id should resend everything: %+v", got)