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
Repair a half-installed share hook; name the hook setup refuses
Claude86e7f2372df8
diff --git a/cli/beacon/cmd/git_hooks.go b/cli/beacon/cmd/git_hooks.goindex 6dfde1d0..9ea9bb3c 100644--- a/cli/beacon/cmd/git_hooks.go+++ b/cli/beacon/cmd/git_hooks.go@@ -105,7 +105,16 @@ func runGitSetup(cmd *cobra.Command, _ []string) error {"Add this line to its post-commit hook:\n\n %s\n\n"+"or run `beacon git setup --hooks-path` to install into that directory anyway", err, result.Hooks.Dir, gitlink.HookCall("post-commit"))case errors.Is(err, gitlink.ErrForeignHook):- return fmt.Errorf("%w; Beacon only edits shell hooks. Call Beacon from that hook instead:\n\n %s", err, gitlink.HookCall("post-commit"))+ var foreign *gitlink.ForeignHookError+ hook := "post-commit"+ if errors.As(err, &foreign) && foreign.Hook != "" {+ hook = foreign.Hook+ }+ msg := fmt.Sprintf("%v; Beacon only edits shell hooks, so nothing was installed. Call Beacon from that %s hook instead:\n\n %s", err, hook, gitlink.HookCall(hook))+ if hook == gitlink.ShareHook {+ msg += "\n\nor run `beacon git setup` without --share-notes to link commits without sharing them"+ }+ return errors.New(msg)}return err}diff --git a/cli/beacon/cmd/git_share_test.go b/cli/beacon/cmd/git_share_test.goindex 9af2fdda..bb957081 100644--- a/cli/beacon/cmd/git_share_test.go+++ b/cli/beacon/cmd/git_share_test.go@@ -112,3 +112,31 @@ func TestGitSetupShareNotesFlag(t *testing.T) {t.Fatalf("status after off:\n%s", out)}}++func TestGitSetupShareNotesForeignPrePushMessage(t *testing.T) {+ root, _ := gitCommandTeam(t)+ hooks := filepath.Join(root, ".git", "hooks")+ if err := os.MkdirAll(hooks, 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(hooks, "pre-push"), []byte("#!/usr/bin/env python3\n"), 0o755); err != nil {+ t.Fatal(err)+ }+ _, err := runGit(t, "setup", "--cwd", root, "--share-notes")+ if err == nil {+ t.Fatal("want a refusal")+ }+ msg := err.Error()+ for _, want := range []string{"pre-push hook", `beacon git hook pre-push "$@" || true`, "without --share-notes", "nothing was installed"} {+ if !strings.Contains(msg, want) {+ t.Errorf("message lacks %q:\n%s", want, msg)+ }+ }+ if strings.Contains(msg, "hook post-commit") {+ t.Errorf("message names the wrong hook:\n%s", msg)+ }+ // Linking alone still installs.+ if _, err := runGit(t, "setup", "--cwd", root); err != nil {+ t.Fatal(err)+ }+}diff --git a/cli/beacon/internal/gitlink/hooks.go b/cli/beacon/internal/gitlink/hooks.goindex 404710b5..a77ab556 100644--- a/cli/beacon/internal/gitlink/hooks.go+++ b/cli/beacon/internal/gitlink/hooks.go@@ -44,6 +44,20 @@ var ErrHooksPathSet = errors.New("core.hooksPath is set")// be added to it safely.var ErrForeignHook = errors.New("existing hook is not a shell script")+// ForeignHookError names the hook ErrForeignHook refused, so the caller can say which line to add+// to it by hand.+type ForeignHookError struct {+ Hook string+ Path string+ Shebang string+}++func (e *ForeignHookError) Error() string {+ return fmt.Sprintf("%s: %v (%s)", e.Path, ErrForeignHook, e.Shebang)+}++func (e *ForeignHookError) Unwrap() error { return ErrForeignHook }+// HooksLocation is where git looks for this repository's hooks.type HooksLocation struct {Dir string `json:"dir"`@@ -119,12 +133,19 @@ func InstallHooks(ctx context.Context, repo Repo, opts InstallOptions) (InstallRhooks := append([]string{}, ManagedHooks...)if opts.ShareNotes != nil && *opts.ShareNotes {hooks = append(hooks, ShareHook)- } else if opts.ShareNotes == nil && hookInstalled(loc.Dir, ShareHook) {- hooks = append(hooks, ShareHook) // refresh the script along with the others+ } else if opts.ShareNotes == nil && hookPresent(loc.Dir, ShareHook) {+ // Refresh -- or repair, when only the block or only the script survived -- along with the+ // others: status says to run setup to fix a broken install, and sharing is part of it.+ hooks = append(hooks, ShareHook)}// Check every hook before writing any, so a refusal leaves the repository as it was.for _, hook := range hooks {if err := checkHookEditable(filepath.Join(loc.Dir, hook)); err != nil {+ var foreign *ForeignHookError+ if errors.As(err, &foreign) {+ foreign.Hook = hook+ return result, foreign+ }return result, fmt.Errorf("%s: %w", filepath.Join(loc.Dir, hook), err)}}@@ -162,6 +183,12 @@ func InstallHooks(ctx context.Context, repo Repo, opts InstallOptions) (InstallRreturn result, err}+// hookPresent is true when any part of Beacon's hook is there: its block, its script, or both.+func hookPresent(dir, hook string) bool {+ data, err := os.ReadFile(filepath.Join(dir, hook))+ return (err == nil && strings.Contains(string(data), hookBlockStart)) || fileExists(filepath.Join(dir, HookScriptPrefix+hook))+}+func hookInstalled(dir, hook string) bool {data, err := os.ReadFile(filepath.Join(dir, hook))return err == nil && strings.Contains(string(data), hookBlockStart) && fileExists(filepath.Join(dir, HookScriptPrefix+hook))@@ -255,7 +282,7 @@ func checkHookEditable(path string) error {return nil}if !shellShebang.MatchString(first) {- return fmt.Errorf("%w (%s)", ErrForeignHook, strings.TrimSpace(first))+ return &ForeignHookError{Path: path, Shebang: strings.TrimSpace(first)}}return nil}@@ -271,7 +298,11 @@ func hookBlock(scriptName string) string {// HookCall is the line to put in a hook Beacon cannot edit (a hook manager's, or one written in// another language) so it links commits too.func HookCall(hook string) string {- return "beacon git hook " + hook + " || true"+ if hook == "post-commit" {+ return "beacon git hook post-commit || true"+ }+ // post-rewrite and pre-push take git's arguments (and post-rewrite its stdin) along.+ return "beacon git hook " + hook + ` "$@" || true`}var hookPurpose = map[string]string{diff --git a/cli/beacon/internal/gitlink/share_test.go b/cli/beacon/internal/gitlink/share_test.goindex f4286ba3..365c0b7a 100644--- a/cli/beacon/internal/gitlink/share_test.go+++ b/cli/beacon/internal/gitlink/share_test.go@@ -2,6 +2,7 @@ package gitlinkimport ("context"+ "errors""os""path/filepath""reflect"@@ -260,3 +261,64 @@ func TestErrorSummary(t *testing.T) {t.Fatalf("got %q", got)}}++func TestPlainSetupRepairsAHalfInstalledShareHook(t *testing.T) {+ tm := newTeam(t)+ ctx := context.Background()+ bin, _ := fakeBeacon(t, filepath.Join(t.TempDir(), "bin"))+ bob := tm.bob.open()+ on := true+ if _, err := InstallHooks(ctx, bob, InstallOptions{BeaconPath: bin, ShareNotes: &on}); err != nil {+ t.Fatal(err)+ }+ loc, _ := ResolveHooks(ctx, bob)+ script := filepath.Join(loc.Dir, HookScriptPrefix+ShareHook)+ hook := filepath.Join(loc.Dir, ShareHook)+ for name, breakIt := range map[string]func(){+ "script missing": func() { _ = os.Remove(script) },+ "block missing": func() { _ = os.Remove(hook) },+ } {+ breakIt()+ if st, _ := HookStatus(ctx, bob); st.Sharing {+ t.Fatalf("%s: status still reports sharing", name)+ }+ if _, err := InstallHooks(ctx, bob, InstallOptions{BeaconPath: bin}); err != nil {+ t.Fatalf("%s: %v", name, err)+ }+ if st, _ := HookStatus(ctx, bob); !st.Sharing {+ t.Fatalf("%s: plain setup did not repair the share hook", name)+ }+ }+ // A repository that never had sharing does not gain it from a plain setup.+ other := tm.alice.open()+ if _, err := InstallHooks(ctx, other, InstallOptions{BeaconPath: bin}); err != nil {+ t.Fatal(err)+ }+ if st, _ := HookStatus(ctx, other); st.Sharing {+ t.Fatal("plain setup turned sharing on")+ }+}++func TestShareNotesRefusesAForeignPrePushAndNamesIt(t *testing.T) {+ tm := newTeam(t)+ ctx := context.Background()+ bob := tm.bob.open()+ loc, _ := ResolveHooks(ctx, bob)+ _ = os.MkdirAll(loc.Dir, 0o755)+ if err := os.WriteFile(filepath.Join(loc.Dir, "pre-push"), []byte("#!/usr/bin/env node\n"), 0o755); err != nil {+ t.Fatal(err)+ }+ on := true+ _, err := InstallHooks(ctx, bob, InstallOptions{BeaconPath: "/x/beacon", ShareNotes: &on})+ var foreign *ForeignHookError+ if !errors.As(err, &foreign) || foreign.Hook != ShareHook || !errors.Is(err, ErrForeignHook) {+ t.Fatalf("got %v", err)+ }+ // All or nothing: the core hooks were not half-installed either.+ if _, err := os.Stat(filepath.Join(loc.Dir, "beacon-post-commit")); !os.IsNotExist(err) {+ t.Fatal("a refused setup wrote post-commit")+ }+ if got := HookCall(ShareHook); got != `beacon git hook pre-push "$@" || true` {+ t.Fatalf("HookCall(pre-push) = %q", got)+ }+}