The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
test(git): stop git hook tests racing wall-clock budgets on slow runners (#859)
kyle-elliott-asymptotefdcbf96b3c84
diff --git a/cli/beacon/cmd/git_hooks_test.go b/cli/beacon/cmd/git_hooks_test.goindex 03f512ec..5b4676e0 100644--- a/cli/beacon/cmd/git_hooks_test.go+++ b/cli/beacon/cmd/git_hooks_test.go@@ -14,6 +14,19 @@ import ("github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv")+// relaxGitBudgets lifts the git hooks' wall-clock budgets for a test of what a hook writes. A hook+// spawns git a dozen or more times, and on a loaded Windows runner each spawn can cost a few hundred+// milliseconds, so under the production budgets such a test fails when the runner is slow rather+// 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+ gitHookBudget = time.Minute+ gitShareBudget = time.Minute+ gitShareUploadBudget = gitShareBudget - gitShareNotesReserve - gitShareNoteWriteBudget+ t.Cleanup(func() { gitHookBudget, gitShareBudget, gitShareUploadBudget = prevHook, prevShare, prevUpload })+}+func writeRuntimeEvent(t *testing.T, logPath, top, session, file string) {t.Helper()event := schema.NewEvent(schema.NewEventOptions{Action: "file.modified", Harness: schema.HarnessInfo{Name: "cursor"}})@@ -32,6 +45,7 @@ func writeRuntimeEvent(t *testing.T, logPath, top, session, file string) {}func TestGitHookPostCommitLinksHEAD(t *testing.T) {+ relaxGitBudgets(t)root := gitCommandRepo(t)top := mustGit(t, root, "rev-parse", "--show-toplevel")logPath := filepath.Join(t.TempDir(), "runtime.jsonl")@@ -64,6 +78,7 @@ func TestGitHookPostCommitIsQuietOutsideARepositoryAndWhenDisabled(t *testing.T)}func TestGitHookPostCommitSkipsReplays(t *testing.T) {+ relaxGitBudgets(t)root := gitCommandRepo(t)top := mustGit(t, root, "rev-parse", "--show-toplevel")logPath := filepath.Join(t.TempDir(), "runtime.jsonl")@@ -135,6 +150,7 @@ func TestGitSetupExplainsCoreHooksPath(t *testing.T) {}func TestGitHookPostRewriteNormalizesRewrittenNotes(t *testing.T) {+ relaxGitBudgets(t)root := gitCommandRepo(t)mustGit(t, root, "commit", "-q", "--allow-empty", "-m", "c")sha := mustGit(t, root, "rev-parse", "HEAD")@@ -207,6 +223,7 @@ func TestGitLinkRecordsOneEventPerLink(t *testing.T) {}func TestGitHookPostCommitRecordsTheLinkEvent(t *testing.T) {+ relaxGitBudgets(t)root := gitCommandRepo(t)top := mustGit(t, root, "rev-parse", "--show-toplevel")logPath := filepath.Join(t.TempDir(), "runtime.jsonl")@@ -228,6 +245,7 @@ func TestGitHookPostCommitRecordsTheLinkEvent(t *testing.T) {// configured host before it reads notes, and merging keeps a link's line with trailing fields over// the bare one cat_sort_uniq sorts first.func TestGitHookPostRewriteKeepsSelfHostedShareURLs(t *testing.T) {+ relaxGitBudgets(t)t.Setenv(account.BaseURLEnv, "https://beacon.rewrite-test.example")testenv.SetHome(t, t.TempDir())root := gitCommandRepo(t)diff --git a/cli/beacon/cmd/git_share_sessions_test.go b/cli/beacon/cmd/git_share_sessions_test.goindex d70f899f..61829871 100644--- a/cli/beacon/cmd/git_share_sessions_test.go+++ b/cli/beacon/cmd/git_share_sessions_test.go@@ -34,6 +34,7 @@ func writeSharedEvent(t *testing.T, logPath, harness, session, url string) {}func TestGitHookPostCommitWritesAKnownShareURL(t *testing.T) {+ relaxGitBudgets(t)root := gitCommandRepo(t)top := mustGit(t, root, "rev-parse", "--show-toplevel")logPath := filepath.Join(t.TempDir(), "runtime.jsonl")@@ -66,6 +67,7 @@ func TestGitHookPostCommitWritesAKnownShareURL(t *testing.T) {}func TestGitPrePushSharesLinkedSessions(t *testing.T) {+ relaxGitBudgets(t)t.Setenv(gitlink.HookStdinEnv, "1") // as Beacon's hook block runs itroot, remote := gitCommandTeam(t)env := newShareEnv(t)@@ -219,6 +221,7 @@ func TestGitPrePushWithoutTheBlockLeavesStdinAloneAndSharesNothing(t *testing.T)// Pre-push sharing to a trace that already exists keeps the title set on it earlier (with --title// or a rename on beacon.sh) and its visibility.func TestGitPrePushKeepsAnExistingTracesTitle(t *testing.T) {+ relaxGitBudgets(t)root, remote := gitCommandTeam(t)env := newShareEnv(t)srv := newFakeShareServer(t)@@ -255,9 +258,11 @@ func TestGitPrePushKeepsAnExistingTracesTitle(t *testing.T) {}}-// 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.+// When the uploads' context ends before they finish, the URLs of the uploads that finished are+// still written into the notes: the note updates run on their own context. The slow upload ends the+// hook's context itself, so the test does not depend on how long reading the notes and stores takes.func TestGitShareOnPushWritesFinishedURLsWhenUploadsRunOutOfTime(t *testing.T) {+ relaxGitBudgets(t)root, _ := gitCommandTeam(t)env := newShareEnv(t)srv := newFakeShareServer(t)@@ -273,16 +278,18 @@ func TestGitShareOnPushWritesFinishedURLsWhenUploadsRunOutOfTime(t *testing.T) {},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) {+ ctx, cancel := context.WithTimeout(context.Background(), gitShareBudget)+ defer cancel()+ prevUpload := gitShareUpload+ gitShareUpload = func(uploadCtx 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()+ cancel() // the second upload outlasts the hook+ <-uploadCtx.Done()+ return shareResult{}, subject, uploadCtx.Err()}- t.Cleanup(func() { gitShareUpload, gitShareUploadBudget = prevUpload, prevBudget })+ t.Cleanup(func() { gitShareUpload = prevUpload })old := mustGit(t, root, "rev-parse", "HEAD")mustGit(t, root, "commit", "-q", "--allow-empty", "-m", "agent work")@@ -292,8 +299,6 @@ func TestGitShareOnPushWritesFinishedURLsWhenUploadsRunOutOfTime(t *testing.T) {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")@@ -311,6 +316,7 @@ func TestGitShareOnPushWritesFinishedURLsWhenUploadsRunOutOfTime(t *testing.T) {// ahead of one's own do not use up its slots. Each runtime's store is listed once per push and the// runtime log read once, however many links there are.func TestGitShareOnPushCapsOnlySessionsThatResolve(t *testing.T) {+ relaxGitBudgets(t)root, _ := gitCommandTeam(t)env := newShareEnv(t)srv := newFakeShareServer(t)@@ -353,7 +359,10 @@ func TestGitShareOnPushCapsOnlySessionsThatResolve(t *testing.T) {if err != nil {t.Fatal(err)}- if _, err := gitShareSessionsOnPush(context.Background(), repo, "origin", "refs/heads/main "+sha+" refs/heads/main "+old+"\n", ""); err != nil {+ // The hook's deadline, as runGitHookPrePush passes it: the note writes get what it leaves.+ ctx, cancel := context.WithTimeout(context.Background(), gitShareBudget)+ defer cancel()+ if _, err := gitShareSessionsOnPush(ctx, repo, "origin", "refs/heads/main "+sha+" refs/heads/main "+old+"\n", ""); err != nil {t.Fatal(err)}if len(uploaded) != 1 || uploaded[0] != shareTestSession {@@ -372,7 +381,9 @@ func TestGitShareOnPushCapsOnlySessionsThatResolve(t *testing.T) {// The share phase is held to the clock: an upload that never returns, and ignores its context, does// not hold up the push. The notes still get the URLs of the uploads that finished, the notes ref is-// pushed with its full reserve, and the hook returns within its budget.+// pushed, and the hook returns within its production budget. The upload budget is short against+// that budget but long enough for a slow runner to read the notes and stores before it, so the fast+// upload lands.func TestGitPrePushAbandonsAnUploadThatNeverReturns(t *testing.T) {t.Setenv(gitlink.HookStdinEnv, "1")root, remote := gitCommandTeam(t)@@ -393,7 +404,7 @@ func TestGitPrePushAbandonsAnUploadThatNeverReturns(t *testing.T) {release := make(chan struct{})t.Cleanup(func() { close(release) })prevUpload, prevBudget := gitShareUpload, gitShareUploadBudget- gitShareUploadBudget = 300 * time.Millisecond+ gitShareUploadBudget = 3 * time.SecondgitShareUpload = 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@@ -417,7 +428,7 @@ func TestGitPrePushAbandonsAnUploadThatNeverReturns(t *testing.T) {if _, err := runGitStderr(t, &stderr, "hook", "pre-push", "--cwd", root, "origin", remote); err != nil {t.Fatalf("pre-push: %v", err)}- if elapsed := time.Since(start); elapsed > gitShareUploadBudget+gitShareNoteWriteBudget+gitShareNotesReserve {+ if elapsed := time.Since(start); elapsed > gitShareBudget {t.Fatalf("pre-push took %s, past its budget", elapsed)}if !strings.Contains(stderr.String(), "ran out of time") {