Skip to content

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

Keep re-shares and refreshes on the source the trace came from

Claude58e6210f4477
diff --git a/cli/beacon/cmd/share.go b/cli/beacon/cmd/share.go
index 4cd8579a..1f4031e8 100644
--- a/cli/beacon/cmd/share.go
+++ b/cli/beacon/cmd/share.go
@@ -208,6 +208,16 @@ func runShare(cmd *cobra.Command) error {
fmt.Fprintf(errOut, "Sharing %s session %s (%d messages)%s.\n", subject.session.Harness, subject.session.ID, len(payload.Messages), shareSourceNote(subject))
}
ctx := commandContext(cmd)
+ if state, found, err := client.GetTrace(ctx, payload.ExternalID); err != nil {
+ return err
+ } else if found {
+ if subject, payload, err = alignShareSource(subject, payload, state); err != nil {
+ return err
+ }
+ if cmd.Flags().Changed("visibility") {
+ payload.Trace.Visibility = shareOpts.visibility
+ }
+ }
trace, err := client.PutTrace(ctx, payload.ExternalID, payload.Trace)
if err != nil {
return err
@@ -277,6 +287,9 @@ func runShareRefresh(cmd *cobra.Command, args []string) error {
if !found {
return fmt.Errorf("%s is not shared yet: run `beacon share --session %s` first", payload.ExternalID, subject.session.ID)
}
+ if subject, payload, err = alignShareSource(subject, payload, state); err != nil {
+ return err
+ }
pending := messagesAfter(payload.Messages, state.LastMessageID, state.MaxOrder)
trace, err := client.PutTrace(ctx, payload.ExternalID, payload.Trace)
if err != nil {
@@ -305,9 +318,9 @@ func runShareRefresh(cmd *cobra.Command, args []string) error {
}
// messagesAfter returns the messages after the one beacon.sh reported last. With no last id it goes
-// by the order number. When the last id is not in this projection (the session was shared from
-// another source, or its earlier messages changed), it returns everything: an upsert of what the
-// server already has changes nothing.
+// by the order number. When the last id is not in this projection it returns everything; callers
+// run alignShareSource first, so that happens only for a projection from the same source whose
+// earlier messages changed, where an upsert by id changes nothing.
func messagesAfter(messages []asymptoteobserve.ShareMessageV1, lastID string, maxOrder int) []asymptoteobserve.ShareMessageV1 {
end := -1
if lastID != "" {
@@ -415,6 +428,38 @@ func shareDirectory() (string, error) {
return abs, nil
}
+// alignShareSource keeps a re-share or refresh on the source the trace was first built from. Message
+// ids come from event ids, and the same session read from its runtime's store and from Beacon's
+// runtime log has different event ids, so uploading the other source would add a second copy of
+// every message. When the server's last message is not in this projection, the other source is
+// tried; when neither has it, the upload is refused rather than duplicated.
+func alignShareSource(subject shareSubject, payload sharePayload, state *asymptoteobserve.ShareTraceStateV1) (shareSubject, sharePayload, error) {
+ if state == nil || state.LastMessageID == "" || hasShareMessage(payload.Messages, state.LastMessageID) {
+ return subject, payload, nil
+ }
+ var other shareSubject
+ if subject.fromLog {
+ other = shareSubject{session: subject.session}
+ } else if logSubject, err := resolveShareLog(subject.session); err == nil {
+ other = logSubject
+ }
+ if other.session.ID != "" {
+ if otherPayload, err := buildSharePayload(other); err == nil && hasShareMessage(otherPayload.Messages, state.LastMessageID) {
+ return other, otherPayload, nil
+ }
+ }
+ return subject, payload, fmt.Errorf("%s on beacon.sh was built from a different copy of this session than Beacon can read now (its store and the runtime log name messages differently), so uploading it would duplicate every message; delete the shared trace and share it again to start over", payload.ExternalID)
+}
+
+func hasShareMessage(messages []asymptoteobserve.ShareMessageV1, id string) bool {
+ for _, m := range messages {
+ if m.ID == id {
+ return true
+ }
+ }
+ return false
+}
+
// shareSubject is a session to share and, when it came from the runtime log, its events.
type shareSubject struct {
session handoff.Session
diff --git a/cli/beacon/cmd/share_test.go b/cli/beacon/cmd/share_test.go
index 206470e1..33cdb5c7 100644
--- a/cli/beacon/cmd/share_test.go
+++ b/cli/beacon/cmd/share_test.go
@@ -503,3 +503,35 @@ func TestShareLeavesALocalGitRemoteOut(t *testing.T) {
t.Fatalf("network remote: err=%v\n%s", err, stdout)
}
}
+
+func TestShareRefreshRefusesToDuplicateFromAnotherSource(t *testing.T) {
+ env := newShareEnv(t)
+ srv := newFakeShareServer(t)
+ srv.signIn(account.ScopeTracesWrite)
+ if _, stderr, err := runShareCommand(t, "--cwd", env.projectDir, "--log-path", env.logPath); err != nil {
+ t.Fatalf("share: %v\n%s", err, stderr)
+ }
+
+ // The store loses the session (its transcript was deleted); the runtime log still has it, as
+ // hook events whose ids differ from the store's.
+ stubHandoffSources(t, stubHandoffSource{
+ harness: handoff.HarnessClaude,
+ sessions: []handoff.Session{{
+ Harness: handoff.HarnessClaude, ID: shareTestSession, Directory: env.projectDir, UpdatedAt: time.Now(),
+ }},
+ eventsErr: handoff.ErrNotFound,
+ })
+ for _, event := range shareFixtureEvents(env.projectDir) {
+ event.Harness.CollectionMethod = schema.CollectionMethodHook
+ if _, err := writer.AppendEvent(event, writer.Options{Path: env.logPath, UserMode: true}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ _, _, err := runShareCommand(t, "refresh", "claude_code:"+shareTestSession, "--log-path", env.logPath)
+ if err == nil || !strings.Contains(err.Error(), "duplicate every message") {
+ t.Fatalf("refresh err = %v", err)
+ }
+ if len(srv.messages) != 4 {
+ t.Fatalf("server holds %d messages after a refused refresh", len(srv.messages))
+ }
+}