Skip to content

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

fix(git): time share uploads with the hook's clock (#1139)

kyle-elliott-asymptote7992919910f3
diff --git a/cli/beacon/cmd/git_hooks_test.go b/cli/beacon/cmd/git_hooks_test.go
index 556839c0..e7c24dc9 100644
--- a/cli/beacon/cmd/git_hooks_test.go
+++ b/cli/beacon/cmd/git_hooks_test.go
@@ -21,11 +21,10 @@ import (
// than when the hook is wrong. Tests of the budgets themselves do not call it.
func relaxGitBudgets(t *testing.T) {
t.Helper()
- prevHook, prevShare, prevUpload := gitHookBudget, gitShareBudget, gitShareUploadBudget
+ prevHook, prevShare := gitHookBudget, gitShareBudget
gitHookBudget = time.Minute
gitShareBudget = time.Minute
- gitShareUploadBudget = gitShareBudget - gitShareNotesReserve - gitShareNoteWriteBudget
- t.Cleanup(func() { gitHookBudget, gitShareBudget, gitShareUploadBudget = prevHook, prevShare, prevUpload })
+ t.Cleanup(func() { gitHookBudget, gitShareBudget = prevHook, prevShare })
}
func writeRuntimeEvent(t *testing.T, logPath, top, session, file string) {
diff --git a/cli/beacon/cmd/git_share_sessions.go b/cli/beacon/cmd/git_share_sessions.go
index 8275843d..d7f220e5 100644
--- a/cli/beacon/cmd/git_share_sessions.go
+++ b/cli/beacon/cmd/git_share_sessions.go
@@ -46,9 +46,6 @@ const (
)
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
// gitShareLogSessions lists the sessions in the runtime log; tests replace it to count reads.
@@ -223,25 +220,25 @@ func (c *gitShareCollector) close() gitShareCollector {
//
// The budget is enforced by the clock, not by cooperation: reading the stores and the runtime log,
// building a payload and recording the share do not all watch a context. The share phase runs in a
-// goroutine that delivers each URL as its upload finishes; the hook waits for it at most
-// gitShareUploadBudget, takes the URLs collected by then and moves on, abandoning the goroutine
-// (the hook process exits soon after). Only this function writes notes, and only from what was
-// collected, so an abandoned upload never touches them; it may still append its session.shared
-// record to the runtime log, which is harmless: that upload did happen. The note writes then get a
-// fresh gitShareNoteWriteBudget, and the caller pushes the notes ref on a fresh
-// gitShareNotesReserve, so neither is starved by the uploads.
+// goroutine that delivers each URL as its upload finishes; the hook waits for it until the hook's
+// own deadline less what the note writes and the notes push need, takes the URLs collected by then
+// and moves on, abandoning the goroutine (the hook process exits soon after). Only this function
+// writes notes, and only from what was collected, so an abandoned upload never touches them; it may
+// still append its session.shared record to the runtime log, which is harmless: that upload did
+// happen. The note writes then get a fresh gitShareNoteWriteBudget, and the caller pushes the notes
+// ref on a fresh gitShareNotesReserve, so neither is starved by the uploads.
func gitShareSessionsOnPush(parent context.Context, repo gitlink.Repo, remote, stdin, visibility string) (gitPushShareReport, error) {
var report gitPushShareReport
gitAllowShareHosts()
- // The uploads end gitShareUploadBudget from now, or earlier when the hook's own deadline leaves
- // less than the note writes and the notes push need after them.
- uploadDeadline := time.Now().Add(gitShareUploadBudget)
+ // The uploads end when the hook's deadline leaves only what the note writes and the notes push
+ // need after them; that deadline is the only clock they run on.
+ var ctx context.Context
+ var cancelUploads context.CancelFunc
if d, ok := parent.Deadline(); ok {
- if latest := d.Add(-gitShareNoteWriteBudget - gitShareNotesReserve); latest.Before(uploadDeadline) {
- uploadDeadline = latest
- }
+ ctx, cancelUploads = context.WithDeadline(parent, d.Add(-gitShareNoteWriteBudget-gitShareNotesReserve))
+ } else {
+ ctx, cancelUploads = context.WithCancel(parent)
}
- ctx, cancelUploads := context.WithDeadline(parent, uploadDeadline)
defer cancelUploads()
collector := &gitShareCollector{urls: map[string]string{}}
done := make(chan struct{})
diff --git a/cli/beacon/cmd/git_share_sessions_test.go b/cli/beacon/cmd/git_share_sessions_test.go
index c3d65951..209dbdfe 100644
--- a/cli/beacon/cmd/git_share_sessions_test.go
+++ b/cli/beacon/cmd/git_share_sessions_test.go
@@ -352,6 +352,11 @@ func TestGitPrePushAbandonsAnUploadThatNeverReturns(t *testing.T) {
t.Cleanup(func() { close(release) })
prevUpload := gitShareUpload
gitShareUpload = func(ctx context.Context, _ *account.ShareClient, subject shareSubject, _ shareUploadOptions) (shareResult, shareSubject, error) {
+ // The hook's time is the manual deadline alone; a deadline here is a real timer that a
+ // slow runner can run out before either upload starts.
+ if d, ok := ctx.Deadline(); ok {
+ t.Errorf("upload of %s has a wall-clock deadline %s the hook did not set", subject.session.ID, d)
+ }
if subject.session.ID == "sess-fast" {
return shareResult{SharedURL: "https://beacon.sh/s/fast01"}, subject, nil
}