Skip to content

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

Cap pre-push sharing only on sessions this machine has

Claudeaadf40dbcf58
diff --git a/cli/beacon/cmd/git_share_sessions.go b/cli/beacon/cmd/git_share_sessions.go
index b2c0315b..e30325c3 100644
--- a/cli/beacon/cmd/git_share_sessions.go
+++ b/cli/beacon/cmd/git_share_sessions.go
@@ -167,43 +167,57 @@ func gitShareSessionsOnPush(parent context.Context, repo gitlink.Repo, remote, s
toShare = append(toShare, link)
}
}
- if len(toShare) > gitShareMaxSessions {
- toShare = toShare[:gitShareMaxSessions]
- }
var shareErr error
+ type linkedSubject struct {
+ link gitlink.Link
+ subject shareSubject
+ }
+ var subjects []linkedSubject
if len(toShare) > 0 {
+ // Resolve first, cap after: a teammate's session, or one this machine no longer has,
+ // must not use up a slot that one of this person's sessions needs. Resolving reads only
+ // the local stores, inside the upload budget.
+ sources := handoffSources(handoff.StoreDirs{})
+ for _, link := range toShare {
+ if ctx.Err() != nil || len(subjects) >= gitShareMaxSessions {
+ break
+ }
+ harness, err := handoff.ParseHarness(link.Harness)
+ if err != nil {
+ continue
+ }
+ subject, err := resolveShareID(sources, harness, link.SessionID)
+ if err != nil || subject.session.ID != link.SessionID {
+ continue // a teammate's session, or one this machine no longer has
+ }
+ subjects = append(subjects, linkedSubject{link: link, subject: subject})
+ }
+ }
+ if len(subjects) > 0 {
client, err := newShareClient()
if err != nil {
report.Skipped = "no Beacon credential"
} else {
- sources := handoffSources(handoff.StoreDirs{})
- for _, link := range toShare {
+ for _, ls := range subjects {
if ctx.Err() != nil {
break
}
- harness, err := handoff.ParseHarness(link.Harness)
- if err != nil {
- continue
- }
- subject, err := resolveShareID(sources, harness, link.SessionID)
- if err != nil || subject.session.ID != link.SessionID {
- continue // a teammate's session, or one this machine no longer has
- }
- result, _, err := gitShareUpload(ctx, client, subject, shareUploadOptions{Visibility: visibility, Record: true})
+ key := ls.link.Key()
+ result, _, err := gitShareUpload(ctx, client, ls.subject, shareUploadOptions{Visibility: visibility, Record: true})
if err != nil && result.SharedURL == "" {
if shareErr == nil {
- shareErr = fmt.Errorf("%s: %w", link.Key(), err)
+ shareErr = fmt.Errorf("%s: %w", key, err)
}
continue
}
if gitlink.ValidShareURL(result.SharedURL) {
- urls[link.Key()] = result.SharedURL
- report.Shared = append(report.Shared, link.Key())
+ urls[key] = result.SharedURL
+ report.Shared = append(report.Shared, key)
}
}
}
}
- if ctx.Err() != nil && shareErr == nil && len(report.Shared) < len(toShare) {
+ if ctx.Err() != nil && shareErr == nil && len(report.Shared) < len(subjects) {
shareErr = errors.New("ran out of time before every linked session was shared; the next push shares the rest")
}
cancelUploads()
diff --git a/cli/beacon/cmd/git_share_sessions_test.go b/cli/beacon/cmd/git_share_sessions_test.go
index f32337b3..7c7a2c66 100644
--- a/cli/beacon/cmd/git_share_sessions_test.go
+++ b/cli/beacon/cmd/git_share_sessions_test.go
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
+ "fmt"
"os"
"path/filepath"
"strings"
@@ -305,3 +306,45 @@ func TestGitShareOnPushWritesFinishedURLsWhenUploadsRunOutOfTime(t *testing.T) {
t.Fatalf("note %q, want %q", note, want)
}
}
+
+// The per-push cap counts only sessions this machine has: links to teammates' or pruned sessions
+// ahead of one's own do not use up its slots.
+func TestGitShareOnPushCapsOnlySessionsThatResolve(t *testing.T) {
+ root, _ := gitCommandTeam(t)
+ env := newShareEnv(t)
+ srv := newFakeShareServer(t)
+ srv.signIn(account.ScopeTracesWrite)
+ prevOpts := shareOpts
+ shareOpts = shareOptions{logPath: env.logPath}
+ t.Cleanup(func() { shareOpts = prevOpts })
+ var uploaded []string
+ prevUpload := gitShareUpload
+ gitShareUpload = func(ctx context.Context, _ *account.ShareClient, subject shareSubject, _ shareUploadOptions) (shareResult, shareSubject, error) {
+ uploaded = append(uploaded, subject.session.ID)
+ return shareResult{SharedURL: "https://beacon.sh/s/mine01"}, subject, nil
+ }
+ t.Cleanup(func() { gitShareUpload = prevUpload })
+
+ var lines []string
+ for i := 0; i < gitShareMaxSessions+2; i++ {
+ lines = append(lines, fmt.Sprintf("beacon:claude_code/teammate-%02d", i))
+ }
+ lines = append(lines, "beacon:claude_code/"+shareTestSession)
+ old := mustGit(t, root, "rev-parse", "HEAD")
+ mustGit(t, root, "commit", "-q", "--allow-empty", "-m", "agent work")
+ sha := mustGit(t, root, "rev-parse", "HEAD")
+ mustGit(t, root, "notes", "--ref="+gitlink.NotesRef, "add", "-m", strings.Join(lines, "\n"), sha)
+ repo, err := gitlink.OpenRepo(context.Background(), gitlink.Git{Dir: root})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := gitShareSessionsOnPush(context.Background(), repo, "origin", "refs/heads/main "+sha+" refs/heads/main "+old+"\n", ""); err != nil {
+ t.Fatal(err)
+ }
+ if len(uploaded) != 1 || uploaded[0] != shareTestSession {
+ t.Fatalf("uploaded %v, want only this machine's session", uploaded)
+ }
+ if note := mustGit(t, root, "notes", "--ref="+gitlink.NotesRef, "show", sha); !strings.Contains(note, "beacon:claude_code/"+shareTestSession+" https://beacon.sh/s/mine01") {
+ t.Fatalf("note %q lacks the URL", note)
+ }
+}