Skip to content

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

Add ShareEventsAfter for re-shares, and the subagent relationship

Claude00813194bdd1
diff --git a/pkg/asymptoteobserve/share.go b/pkg/asymptoteobserve/share.go
index 51c19c00..93559f7a 100644
--- a/pkg/asymptoteobserve/share.go
+++ b/pkg/asymptoteobserve/share.go
@@ -67,9 +67,13 @@ type ShareTraceV1 struct {
RelationshipType string `json:"relationship_type,omitempty"`
}
-// ShareRelationshipHandoff relates a trace to the session it was handed off from: the session's
-// first prompt carried the handoff marker (session.handoff).
-const ShareRelationshipHandoff = "handoff"
+// Relationship types of related_to. Handoff relates a trace to the session it was handed off from
+// (the session's first prompt carried the handoff marker, session.handoff); subagent relates a
+// subagent's session to its parent's.
+const (
+ ShareRelationshipHandoff = "handoff"
+ ShareRelationshipSubagent = "subagent"
+)
// ShareRelation is the trace a session continues, from its first session.handoff event: the source
// session's ShareExternalID and ShareRelationshipHandoff, or two empty strings. The handoff is the
@@ -165,6 +169,46 @@ type ShareOptions struct {
GeneratedAt time.Time
}
+// ShareEventsAfter is what a re-share or refresh uploads of a bundle's events, given the last event
+// beacon.sh holds (ShareTraceStateV1.LastEventID, or with no id its MaxNumber): the events after it,
+// re-sending that last event and every event from the earliest tool call it holds without a
+// completion, since a call still open on the server is the part of a trace most likely to have
+// been rewritten (a call recorded before its result arrives). Events are upserted by id, so a
+// re-send is harmless. When lastID is not among events, or neither watermark applies, every event is
+// returned.
+func ShareEventsAfter(events []TraceEventV1, lastID string, maxNumber int) []TraceEventV1 {
+ end := -1
+ switch {
+ case lastID != "":
+ for i, event := range events {
+ if event.ID == lastID {
+ end = i
+ break
+ }
+ }
+ case maxNumber > 0 && maxNumber <= len(events):
+ end = maxNumber - 1
+ }
+ if end < 0 {
+ return events
+ }
+ uploaded := events[:end+1]
+ completed := map[string]bool{}
+ for _, event := range uploaded {
+ if event.ToolCallID != "" && !traceToolInvocation(event) && traceToolCompletion(event) {
+ completed[event.ToolCallID] = true
+ }
+ }
+ start := end
+ for i, event := range uploaded {
+ if event.ToolCallID != "" && traceToolInvocation(event) && !completed[event.ToolCallID] {
+ start = i
+ break
+ }
+ }
+ return events[start:]
+}
+
// ShareProjects reports whether a shared trace carries event: false for Beacon's bookkeeping about
// the session (session.shared, session.share_requested, session.commit_linked), for Beacon's own
// telemetry and runtime status pings (heartbeats, inventory, destination health, idle/activity
diff --git a/pkg/asymptoteobserve/share_test.go b/pkg/asymptoteobserve/share_test.go
index 5098be7d..940b4481 100644
--- a/pkg/asymptoteobserve/share_test.go
+++ b/pkg/asymptoteobserve/share_test.go
@@ -1014,3 +1014,44 @@ func TestTraceBuilderNamesTheProject(t *testing.T) {
t.Fatalf("project = %+v", p)
}
}
+
+func TestShareEventsAfterResendsTheLastEventAndOpenCalls(t *testing.T) {
+ ev := func(id, action, callID string) TraceEventV1 {
+ return TraceEventV1{ID: id, Action: action, ToolCallID: callID}
+ }
+ events := []TraceEventV1{
+ ev("p1", "prompt.submitted", ""),
+ ev("c1", "tool.invoked", "a"),
+ ev("r1", "tool.completed", "a"),
+ ev("c2", "tool.invoked", "b"),
+ ev("m1", "agent.response", ""),
+ ev("r2", "tool.completed", "b"),
+ ev("m2", "agent.response", ""),
+ }
+ ids := func(in []TraceEventV1) string {
+ var out []string
+ for _, e := range in {
+ out = append(out, e.ID)
+ }
+ return strings.Join(out, ",")
+ }
+ cases := []struct {
+ last string
+ max int
+ want string
+ }{
+ {"r1", 0, "r1,c2,m1,r2,m2"},
+ {"m1", 0, "c2,m1,r2,m2"},
+ {"r2", 0, "r2,m2"},
+ {"m2", 0, "m2"},
+ {"gone", 0, "p1,c1,r1,c2,m1,r2,m2"},
+ {"", 3, "r1,c2,m1,r2,m2"},
+ {"", 0, "p1,c1,r1,c2,m1,r2,m2"},
+ {"", 99, "p1,c1,r1,c2,m1,r2,m2"},
+ }
+ for _, tc := range cases {
+ if got := ids(ShareEventsAfter(events, tc.last, tc.max)); got != tc.want {
+ t.Errorf("after %q/%d = %s, want %s", tc.last, tc.max, got, tc.want)
+ }
+ }
+}