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
Read pre-push stdin only when Beacon's hook block passed a copy in
Claudecb5d7eabc503
diff --git a/cli/beacon/cmd/git_share.go b/cli/beacon/cmd/git_share.goindex 4e0f1f2a..b7d5b4f3 100644--- a/cli/beacon/cmd/git_share.go+++ b/cli/beacon/cmd/git_share.go@@ -129,10 +129,18 @@ func runGitHookPrePush(cmd *cobra.Command, args []string) error {// A hook has no terminal to ask for credentials on; git's own push has already authenticated,// and a credential helper answers the same way for this one.repo.Git = repo.Git.WithExtraEnv("GIT_TERMINAL_PROMPT=0")- // git's stdin (the refs being pushed) is read only when sessions are shared; the hook block- // hands Beacon a copy and replays the original to the rest of the repository's hook.+ // git's stdin (the refs being pushed) is read only when sessions are shared, and only when+ // Beacon's hook block passed a copy in (it replays the original to the rest of the hook).+ // Called any other way, stdin belongs to whatever runs after Beacon, so the pushed commits are+ // worked out from HEAD and the remote's tracking branches instead.if enabled, visibility := gitlink.ShareSessionsConfig(ctx, repo); enabled {- if stdin := readHookStdin(cmd.InOrStdin()); stdin != "" {+ stdin := ""+ if os.Getenv(gitlink.HookStdinEnv) == "1" {+ stdin = readHookStdin(cmd.InOrStdin())+ } else {+ stdin = gitlink.HeadPushLine(ctx, repo, args[0])+ }+ 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)diff --git a/cli/beacon/cmd/git_share_sessions_test.go b/cli/beacon/cmd/git_share_sessions_test.goindex d344ea05..bee9f62c 100644--- a/cli/beacon/cmd/git_share_sessions_test.go+++ b/cli/beacon/cmd/git_share_sessions_test.go@@ -166,3 +166,40 @@ func TestGitSetupShareSessionsRules(t *testing.T) {t.Fatalf("status:\n%s", out)}}++// Called straight from a hook manager (gitlink.HookCall), without Beacon's block to copy and+// replay stdin, the pre-push hook must leave stdin to the steps after it (`git lfs pre-push`),+// and still finds the pushed commits from HEAD and the remote's tracking branches.+func TestGitPrePushWithoutTheBlockLeavesStdinAlone(t *testing.T) {+ root, remote := gitCommandTeam(t)+ env := newShareEnv(t)+ srv := newFakeShareServer(t)+ srv.signIn(account.ScopeTracesWrite)+ prevOpts := shareOpts+ shareOpts = shareOptions{logPath: env.logPath}+ t.Cleanup(func() { shareOpts = prevOpts })+ t.Setenv(gitlink.HookStdinEnv, "")++ 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/"+shareTestSession, sha)+ if _, err := runGit(t, "setup", "--cwd", root, "--share-sessions"); err != nil {+ t.Fatal(err)+ }+ stdin := "refs/heads/main " + sha + " refs/heads/main " + old + "\n"+ reader := strings.NewReader(stdin)+ rootCmd.SetIn(reader)+ t.Cleanup(func() { rootCmd.SetIn(nil) })+ var stderr bytes.Buffer+ if _, err := runGitStderr(t, &stderr, "hook", "pre-push", "--cwd", root, "origin", remote); err != nil || stderr.Len() != 0 {+ t.Fatalf("pre-push: %v %q", err, stderr.String())+ }+ if reader.Len() != len(stdin) {+ t.Fatalf("the hook read %d bytes of stdin that the next step needs", len(stdin)-reader.Len())+ }+ want := "beacon:claude_code/" + shareTestSession + " https://beacon.sh/s/k3xabc"+ if note := mustGit(t, remote, "notes", "--ref="+gitlink.NotesRef, "show", sha); note != want {+ t.Fatalf("remote note %q, want the session shared from HEAD", note)+ }+}diff --git a/cli/beacon/internal/gitlink/hooks.go b/cli/beacon/internal/gitlink/hooks.goindex 7573a689..77e1b875 100644--- a/cli/beacon/internal/gitlink/hooks.go+++ b/cli/beacon/internal/gitlink/hooks.go@@ -20,6 +20,11 @@ const (// DisableEnv, set to 0, turns every installed Beacon git hook into a no-op for one command:// BEACON_GIT_HOOKS=0 git commit ...DisableEnv = "BEACON_GIT_HOOKS"+ // HookStdinEnv is set to 1 by Beacon's hook block when it hands Beacon a copy of the hook's+ // stdin and replays the original to the rest of the hook. Without it, `beacon git hook+ // pre-push` leaves stdin alone: called straight from a hook manager or a hook in another+ // language (HookCall), reading it would take the refs from the steps after it.+ HookStdinEnv = "BEACON_HOOK_STDIN")// ManagedHooks are the git hooks Beacon installs. post-commit links a new commit; post-rewrite@@ -305,11 +310,11 @@ func hookBlock(hook string) string {// Beacon the copy, and points the hook's own stdin at the file before the rest runs. The file// is unlinked once it is open; the descriptor keeps it readable. When it cannot be copied,// Beacon gets nothing and stdin is left alone.- feed := ` "$beacon_hook" "$@" <"$beacon_stdin"` + "\n"+ feed := ` ` + HookStdinEnv + `=1 "$beacon_hook" "$@" <"$beacon_stdin"` + "\n"if hook == ShareHook {// The refs being pushed matter to Beacon only when it shares sessions on push.feed = ` if [ "$(git config --local --get ` + ShareSessionsKey + ` 2>/dev/null)" = "true" ]; then` + "\n" +- ` "$beacon_hook" "$@" <"$beacon_stdin"` + "\n" ++ ` ` + HookStdinEnv + `=1 "$beacon_hook" "$@" <"$beacon_stdin"` + "\n" +" else\n" +` "$beacon_hook" "$@" </dev/null` + "\n" +" fi\n"diff --git a/cli/beacon/internal/gitlink/share.go b/cli/beacon/internal/gitlink/share.goindex 20ccb8f3..cc9fe106 100644--- a/cli/beacon/internal/gitlink/share.go+++ b/cli/beacon/internal/gitlink/share.go@@ -355,6 +355,22 @@ func PushedCommits(ctx context.Context, repo Repo, remote string, stdin string,return out, nil}+// HeadPushLine stands in for a pre-push hook's stdin when Beacon was not given it: one line pushing+// HEAD to a new branch, so PushedCommits returns the commits on HEAD that none of the remote's+// tracking branches has. It is "" when remote is not a configured remote (there would be nothing+// to exclude) or HEAD has no commit.+func HeadPushLine(ctx context.Context, repo Repo, remote string) string {+ if !isRemote(ctx, repo, remote) {+ return ""+ }+ head, err := repo.Git.Run(ctx, nil, "rev-parse", "--verify", "--quiet", "HEAD^{commit}")+ head = strings.TrimSpace(head)+ if err != nil || !isSHA(head) {+ return ""+ }+ return "HEAD " + head + " HEAD " + strings.Repeat("0", len(head))+}+// hasCommit reports whether sha names a commit present in this repository.func hasCommit(ctx context.Context, repo Repo, sha string) bool {_, err := repo.Git.Run(ctx, nil, "cat-file", "-e", sha+"^{commit}")