Skip to content

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

Write finished uploads' URLs into notes even when the upload budget runs out

Claude75713c1f8a32
diff --git a/cli/beacon/cmd/git_share.go b/cli/beacon/cmd/git_share.go
index a291f1d7..dbd736e6 100644
--- a/cli/beacon/cmd/git_share.go
+++ b/cli/beacon/cmd/git_share.go
@@ -142,13 +142,12 @@ func runGitHookPrePush(cmd *cobra.Command, args []string) error {
fmt.Fprintln(cmd.ErrOrStderr(), "beacon: sessions are shared on push only through the hook `beacon git setup` installs; this push shares commit links only")
}
if stdin != "" {
- // Sharing sessions gets the budget less what pushing the notes ref needs, so the links
- // (with whatever URLs it recorded) still reach the remote.
- shareCtx, cancelShare := context.WithTimeout(ctx, gitShareBudget-gitShareNotesReserve)
- if _, err := gitShareSessionsOnPush(shareCtx, repo, args[0], stdin, visibility); err != nil && shareCtx.Err() == nil {
+ // Sharing sessions splits the budget itself (uploads, then writing their URLs into the
+ // notes) and leaves gitShareNotesReserve for pushing the notes ref, so the links, with
+ // whatever URLs it recorded, still reach the remote.
+ if _, err := gitShareSessionsOnPush(ctx, repo, args[0], stdin, visibility); err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "beacon: linked sessions were not all shared: %s\n", gitlink.ErrorSummary(err))
}
- cancelShare()
}
}
if _, err := gitlink.PushNotes(ctx, repo, args[0]); err != nil {
diff --git a/cli/beacon/cmd/git_share_sessions.go b/cli/beacon/cmd/git_share_sessions.go
index 86e760a5..b2c0315b 100644
--- a/cli/beacon/cmd/git_share_sessions.go
+++ b/cli/beacon/cmd/git_share_sessions.go
@@ -5,6 +5,7 @@ import (
"bytes"
"context"
"encoding/json"
+ "errors"
"fmt"
"io"
"net/url"
@@ -38,6 +39,17 @@ const (
gitShareMaxCommits = 100
// gitShareNotesReserve is the part of the pre-push budget kept for pushing the notes ref.
gitShareNotesReserve = 3 * time.Second
+ // gitShareNoteWriteBudget is the part kept, after the uploads, for writing the URLs they
+ // returned into the local notes. It is local work, so a second is plenty.
+ gitShareNoteWriteBudget = time.Second
+)
+
+var (
+ // gitShareUploadBudget bounds finding and uploading the sessions to share during a push: the
+ // pre-push budget less what writing their URLs into the notes and pushing the notes ref need.
+ gitShareUploadBudget = gitShareBudget - gitShareNotesReserve - gitShareNoteWriteBudget
+ // gitShareUpload is shareUpload; tests replace it.
+ gitShareUpload = shareUpload
)
// gitAllowShareHosts admits the configured Beacon service's host besides beacon.sh.
@@ -115,9 +127,13 @@ type gitPushShareReport struct {
// session already shared from this machine only has its URL written. Without a credential it does
// nothing and says nothing. ctx bounds it; every error is returned for one line on stderr, never
// to fail the push.
-func gitShareSessionsOnPush(ctx context.Context, repo gitlink.Repo, remote, stdin, visibility string) (gitPushShareReport, error) {
+func gitShareSessionsOnPush(parent context.Context, repo gitlink.Repo, remote, stdin, visibility string) (gitPushShareReport, error) {
var report gitPushShareReport
gitAllowShareHosts()
+ // The uploads get their own deadline, so running out of it still leaves time to write the URLs
+ // of the uploads that did finish; those run on a short context of their own.
+ ctx, cancelUploads := context.WithTimeout(parent, gitShareUploadBudget)
+ defer cancelUploads()
commits, err := gitlink.PushedCommits(ctx, repo, remote, stdin, gitShareMaxCommits)
if err != nil {
return report, err
@@ -173,7 +189,7 @@ func gitShareSessionsOnPush(ctx context.Context, repo gitlink.Repo, remote, stdi
if err != nil || subject.session.ID != link.SessionID {
continue // a teammate's session, or one this machine no longer has
}
- result, _, err := shareUpload(ctx, client, subject, shareUploadOptions{Visibility: visibility, Record: true})
+ result, _, err := gitShareUpload(ctx, client, subject, shareUploadOptions{Visibility: visibility, Record: true})
if err != nil && result.SharedURL == "" {
if shareErr == nil {
shareErr = fmt.Errorf("%s: %w", link.Key(), err)
@@ -187,8 +203,14 @@ func gitShareSessionsOnPush(ctx context.Context, repo gitlink.Repo, remote, stdi
}
}
}
+ if ctx.Err() != nil && shareErr == nil && len(report.Shared) < len(toShare) {
+ shareErr = errors.New("ran out of time before every linked session was shared; the next push shares the rest")
+ }
+ cancelUploads()
+ noteCtx, cancelNotes := context.WithTimeout(parent, gitShareNoteWriteBudget)
+ defer cancelNotes()
for sha := range notes {
- changed, err := gitlink.UpdateNote(ctx, repo.Git, sha, func(note string) string { return gitlink.SetLinkURLs(note, urls) })
+ changed, err := gitlink.UpdateNote(noteCtx, repo.Git, sha, func(note string) string { return gitlink.SetLinkURLs(note, urls) })
if err != nil {
if shareErr == nil {
shareErr = err
diff --git a/cli/beacon/cmd/git_share_sessions_test.go b/cli/beacon/cmd/git_share_sessions_test.go
index 4aa28529..f32337b3 100644
--- a/cli/beacon/cmd/git_share_sessions_test.go
+++ b/cli/beacon/cmd/git_share_sessions_test.go
@@ -2,6 +2,7 @@ package cmd
import (
"bytes"
+ "context"
"encoding/json"
"os"
"path/filepath"
@@ -12,6 +13,7 @@ import (
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/account"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/gitlink"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/handoff"
)
func writeSharedEvent(t *testing.T, logPath, harness, session, url string) {
@@ -251,3 +253,55 @@ func TestGitPrePushKeepsAnExistingTracesTitle(t *testing.T) {
t.Fatalf("trace title %q visibility %q, want the ones set earlier", srv.trace.Title, srv.trace.Visibility)
}
}
+
+// When the uploads run out of their deadline, the URLs of the uploads that finished are still
+// written into the notes: the note updates run on their own short context.
+func TestGitShareOnPushWritesFinishedURLsWhenUploadsRunOutOfTime(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 })
+ stubHandoffSources(t, stubHandoffSource{
+ harness: handoff.HarnessClaude,
+ sessions: []handoff.Session{
+ {Harness: handoff.HarnessClaude, ID: "sess-fast", Directory: env.projectDir, UpdatedAt: time.Now()},
+ {Harness: handoff.HarnessClaude, ID: "sess-slow", Directory: env.projectDir, UpdatedAt: time.Now()},
+ },
+ events: shareFixtureEvents(env.projectDir),
+ })
+ prevUpload, prevBudget := gitShareUpload, gitShareUploadBudget
+ gitShareUploadBudget = 300 * time.Millisecond
+ gitShareUpload = func(ctx context.Context, _ *account.ShareClient, subject shareSubject, _ shareUploadOptions) (shareResult, shareSubject, error) {
+ if subject.session.ID == "sess-fast" {
+ return shareResult{SharedURL: "https://beacon.sh/s/fast01"}, subject, nil
+ }
+ <-ctx.Done() // the second upload uses up the deadline
+ return shareResult{}, subject, ctx.Err()
+ }
+ t.Cleanup(func() { gitShareUpload, gitShareUploadBudget = prevUpload, prevBudget })
+
+ 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", "beacon:claude_code/sess-fast\nbeacon:claude_code/sess-slow", sha)
+ repo, err := gitlink.OpenRepo(context.Background(), gitlink.Git{Dir: root})
+ if err != nil {
+ t.Fatal(err)
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), gitShareBudget)
+ defer cancel()
+ report, err := gitShareSessionsOnPush(ctx, repo, "origin", "refs/heads/main "+sha+" refs/heads/main "+old+"\n", "")
+ if err == nil {
+ t.Fatal("running out of time was not reported")
+ }
+ if report.Noted != 1 {
+ t.Fatalf("report %+v, want the note updated", report)
+ }
+ want := "beacon:claude_code/sess-fast https://beacon.sh/s/fast01\nbeacon:claude_code/sess-slow"
+ if note := mustGit(t, root, "notes", "--ref="+gitlink.NotesRef, "show", sha); note != want {
+ t.Fatalf("note %q, want %q", note, want)
+ }
+}