diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c6a7a20e..e6bb0aee1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -812,6 +812,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 error envelope on stdout under `--json`. A batch that reads at least one of its ids still exits 0. Both the direct and the proxied-server routes changed. +- **`bd init --quiet` no longer prints "Removed SessionStart hook" or "Removed + PreCompact hook"** when the project's `.claude/settings.json` (or the legacy + `.claude/settings.local.json`) still carries the bare `bd prime` hooks an + older bd registered. The hooks are still migrated to the SessionStart + `bd prime --hook-json` hook; the removal lines now go to the installer's own + output, which `--quiet` discards, instead of straight to stdout. - **Time-bound filters and the keyset cursor compare the same instant on every engine, whatever zone they are given in.** The storage filter builder binds every time bound (`created_*`, `updated_*`, `closed_*`, `started_*`, diff --git a/cmd/bd/init_embedded_test.go b/cmd/bd/init_embedded_test.go index dea6ce141..858082849 100644 --- a/cmd/bd/init_embedded_test.go +++ b/cmd/bd/init_embedded_test.go @@ -482,6 +482,39 @@ func TestEmbeddedInitABasic(t *testing.T) { t.Errorf("issue_prefix: got %q, want %q", val, "alpha") } }) + + // The project's .claude/settings.json still carries the bare `bd prime` + // SessionStart and PreCompact hooks an older bd registered. init's Claude + // install migrates them, and --quiet drops the migration's "Removed + // hook" lines like the rest of the installers' progress. + t.Run("quiet_legacy_claude_hooks", func(t *testing.T) { + dir := t.TempDir() + initGitRepoAt(t, dir) + settingsPath := filepath.Join(dir, ".claude", "settings.json") + if err := os.MkdirAll(filepath.Dir(settingsPath), 0o755); err != nil { + t.Fatalf("mkdir .claude: %v", err) + } + legacyHook := `[{"matcher": "", "hooks": [{"type": "command", "command": "bd prime"}]}]` + legacy := `{"hooks": {"SessionStart": ` + legacyHook + `, "PreCompact": ` + legacyHook + `}}` + if err := os.WriteFile(settingsPath, []byte(legacy), 0o644); err != nil { + t.Fatalf("seed settings.json: %v", err) + } + args := []string{"--prefix", "legacyhooks"} + if database, seeded := seededInitDatabase(args); seeded { + seedEmbeddedSchema(t, dir, database) + } + + if out := runBDInit(t, bd, dir, args...); out != "" { + t.Errorf("bd init --quiet should print nothing, got:\n%s", out) + } + data, err := os.ReadFile(settingsPath) + if err != nil { + t.Fatalf("read settings.json: %v", err) + } + if got := string(data); !strings.Contains(got, `"bd prime --hook-json"`) || strings.Contains(got, `"bd prime"`) || strings.Contains(got, "PreCompact") { + t.Errorf("legacy hooks not migrated to the SessionStart --hook-json hook:\n%s", got) + } + }) } func TestEmbeddedInitAForkAutoContributor(t *testing.T) { diff --git a/cmd/bd/setup/claude.go b/cmd/bd/setup/claude.go index ed9e25266..c04fd2caf 100644 --- a/cmd/bd/setup/claude.go +++ b/cmd/bd/setup/claude.go @@ -289,11 +289,11 @@ func installClaude(env claudeEnv, global bool, stealth bool) error { if legacy == command { continue } - removeHookCommand(hooks, "SessionStart", legacy) - removeHookCommand(hooks, "PreCompact", legacy) + removeHookCommand(env.stdout, hooks, "SessionStart", legacy) + removeHookCommand(env.stdout, hooks, "PreCompact", legacy) } - removeHookCommand(hooks, "PreCompact", "bd prime --hook-json") - removeHookCommand(hooks, "PreCompact", "bd prime --stealth --hook-json") + removeHookCommand(env.stdout, hooks, "PreCompact", "bd prime --hook-json") + removeHookCommand(env.stdout, hooks, "PreCompact", "bd prime --stealth --hook-json") // GH#3192: Skip writing hooks if the beads plugin is already providing them, // so project-level hooks don't fire bd prime twice per session. @@ -326,8 +326,8 @@ func installClaude(env claudeEnv, global bool, stealth bool) error { if json.Unmarshal(legacyData, &legacySettings) == nil { if legacyHooks, ok := legacySettings["hooks"].(map[string]interface{}); ok { for _, v := range []string{"bd prime", "bd prime --stealth", "bd prime --hook-json", "bd prime --stealth --hook-json"} { - removeHookCommand(legacyHooks, "SessionStart", v) - removeHookCommand(legacyHooks, "PreCompact", v) + removeHookCommand(env.stdout, legacyHooks, "SessionStart", v) + removeHookCommand(env.stdout, legacyHooks, "PreCompact", v) } if migrated, marshalErr := marshalSettings(legacySettings); marshalErr == nil { if writeErr := writeSettingsIfChanged(env, legacyPath, migrated); writeErr == nil { @@ -498,8 +498,8 @@ func removeClaude(env claudeEnv, global bool) error { _, _ = fmt.Fprintln(env.stdout, "No hooks found") } else { for _, v := range []string{"bd prime", "bd prime --stealth", "bd prime --hook-json", "bd prime --stealth --hook-json"} { - removeHookCommand(hooks, "SessionStart", v) - removeHookCommand(hooks, "PreCompact", v) + removeHookCommand(env.stdout, hooks, "SessionStart", v) + removeHookCommand(env.stdout, hooks, "PreCompact", v) } data, err = marshalSettings(settings) @@ -523,8 +523,8 @@ func removeClaude(env claudeEnv, global bool) error { if json.Unmarshal(legacyData, &legacySettings) == nil { if legacyHooks, ok := legacySettings["hooks"].(map[string]interface{}); ok { for _, v := range []string{"bd prime", "bd prime --stealth", "bd prime --hook-json", "bd prime --stealth --hook-json"} { - removeHookCommand(legacyHooks, "SessionStart", v) - removeHookCommand(legacyHooks, "PreCompact", v) + removeHookCommand(env.stdout, legacyHooks, "SessionStart", v) + removeHookCommand(env.stdout, legacyHooks, "PreCompact", v) } if migrated, marshalErr := marshalSettings(legacySettings); marshalErr == nil { _ = writeSettingsIfChanged(env, legacyPath, migrated) @@ -605,8 +605,10 @@ func addHookCommand(hooks map[string]interface{}, event, command string) bool { // removeHookCommand removes a specific command from an event's hook entries. // Only the matching command object is removed; sibling commands in the same // hook entry are preserved. A hook entry is dropped only when its command list -// becomes empty after filtering. -func removeHookCommand(hooks map[string]interface{}, event, command string) { +// becomes empty after filtering. Each removal is reported to w, the caller's +// stdout: bd init --quiet hands the installers a discarding writer, so a +// print to the process stdout here would escape it. +func removeHookCommand(w io.Writer, hooks map[string]interface{}, event, command string) { eventHooks, ok := hooks[event].([]interface{}) if !ok { return @@ -644,7 +646,7 @@ func removeHookCommand(hooks map[string]interface{}, event, command string) { } if removed { - fmt.Printf("✓ Removed %s hook\n", event) + _, _ = fmt.Fprintf(w, "✓ Removed %s hook\n", event) } // Drop the hook entry only when it has no commands left. diff --git a/cmd/bd/setup/claude_artifacts_golden_test.go b/cmd/bd/setup/claude_artifacts_golden_test.go index 2e0c2ac74..1d35a33c1 100644 --- a/cmd/bd/setup/claude_artifacts_golden_test.go +++ b/cmd/bd/setup/claude_artifacts_golden_test.go @@ -356,10 +356,9 @@ func TestClaudePluginManifestHookEvents(t *testing.T) { } // runClaudeArtifactStep runs one step with env.stdout and the process stdout -// sent to the same file, so the transcript keeps the order a user sees: the -// hook helpers (addHookCommand, removeHookCommand) print with fmt.Printf -// rather than to env.stdout. Swapping os.Stdout is why these tests must not -// run in parallel. +// sent to the same file, so the transcript keeps the order a user sees: +// addHookCommand prints with fmt.Printf rather than to env.stdout. Swapping +// os.Stdout is why these tests must not run in parallel. func runClaudeArtifactStep(t *testing.T, env claudeEnv, run func(claudeEnv) error) (stdout, stderr string, err error) { t.Helper() capture, createErr := os.CreateTemp(t.TempDir(), "stdout-*") diff --git a/cmd/bd/setup/claude_test.go b/cmd/bd/setup/claude_test.go index 4d1e178e1..bcb565a15 100644 --- a/cmd/bd/setup/claude_test.go +++ b/cmd/bd/setup/claude_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "io" "os" "path/filepath" "strings" @@ -271,7 +272,7 @@ func TestRemoveHookCommand(t *testing.T) { }, }, } - removeHookCommand(hooks, "SessionStart", "bd prime") + removeHookCommand(io.Discard, hooks, "SessionStart", "bd prime") entries, ok := hooks["SessionStart"].([]interface{}) if !ok || len(entries) != 1 { @@ -289,7 +290,7 @@ func TestRemoveHookCommand(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - removeHookCommand(tt.existingHooks, tt.event, tt.command) + removeHookCommand(io.Discard, tt.existingHooks, tt.event, tt.command) eventHooks, ok := tt.existingHooks[tt.event].([]interface{}) if !ok && tt.wantRemaining > 0 { @@ -332,7 +333,7 @@ func TestRemoveHookCommandNoNull(t *testing.T) { }, } - removeHookCommand(hooks, "SessionStart", "bd prime") + removeHookCommand(io.Discard, hooks, "SessionStart", "bd prime") // Key should be deleted, not set to null or empty array if _, exists := hooks["SessionStart"]; exists { @@ -349,6 +350,79 @@ func TestRemoveHookCommandNoNull(t *testing.T) { } } +// bd init --quiet hands the Claude installer QuietProjectInstallOutput. When +// the project's settings still carry the bare `bd prime` hooks an older bd +// registered, the migration sweep's "Removed hook" lines must go to +// that output like the rest of the installer's progress: removeHookCommand +// printed them straight to the process stdout, so a quiet init still wrote +// them. +func TestClaudeLegacyHookRemovalReportsThroughGivenOutput(t *testing.T) { + legacyHook := func() []interface{} { + return []interface{}{map[string]interface{}{ + "matcher": "", + "hooks": []interface{}{map[string]interface{}{"type": "command", "command": "bd prime"}}, + }} + } + for _, tc := range []struct { + name string + path func(projectDir string) string + }{ + {name: "settings.json", path: projectSettingsPath}, + {name: "legacy settings.local.json", path: legacyProjectSettingsPath}, + } { + t.Run(tc.name, func(t *testing.T) { + env, _, _ := newClaudeTestEnv(t) + stubClaudeEnvProvider(t, env, nil) + writeSettings(t, tc.path(env.projectDir), map[string]interface{}{ + "hooks": map[string]interface{}{"SessionStart": legacyHook(), "PreCompact": legacyHook()}, + }) + + var out, errOut bytes.Buffer + processOut := captureProcessStdout(t, func() { + if err := InstallClaudeProjectTo(false, ProjectInstallOutput{Stdout: &out, Stderr: &errOut}); err != nil { + t.Fatalf("InstallClaudeProjectTo: %v", err) + } + }) + if processOut != "" { + t.Errorf("Claude installer wrote to the process stdout instead of the given one: %q", processOut) + } + for _, want := range []string{"✓ Removed SessionStart hook\n", "✓ Removed PreCompact hook\n"} { + if !strings.Contains(out.String(), want) { + t.Errorf("given stdout is missing %q:\n%s", want, out.String()) + } + } + if errOut.Len() != 0 { + t.Errorf("install wrote errors: %q", errOut.String()) + } + }) + } +} + +// captureProcessStdout runs fn with the process stdout sent to a temp file and +// returns what was written there. Swapping os.Stdout is why the tests in this +// package must not run in parallel. +func captureProcessStdout(t *testing.T, fn func()) string { + t.Helper() + capture, err := os.CreateTemp(t.TempDir(), "process-stdout-*") + if err != nil { + t.Fatalf("create stdout capture: %v", err) + } + orig := os.Stdout + os.Stdout = capture + func() { + defer func() { os.Stdout = orig }() + fn() + }() + if err := capture.Close(); err != nil { + t.Fatalf("close stdout capture: %v", err) + } + data, err := os.ReadFile(capture.Name()) + if err != nil { + t.Fatalf("read stdout capture: %v", err) + } + return string(data) +} + // TestInstallClaudeCleanupNullHooks verifies that install cleans up existing null values. // GH#955: null values left by previous buggy removal cause Claude Code to fail. func TestInstallClaudeCleanupNullHooks(t *testing.T) { diff --git a/cmd/bd/setup/gemini.go b/cmd/bd/setup/gemini.go index d4442cfb7..aa5c47273 100644 --- a/cmd/bd/setup/gemini.go +++ b/cmd/bd/setup/gemini.go @@ -129,15 +129,15 @@ func installGemini(env geminiEnv, project bool, stealth bool) error { if legacy == command { continue // never remove the variant we're about to add } - removeHookCommand(hooks, "SessionStart", legacy) - removeHookCommand(hooks, "PreCompress", legacy) + removeHookCommand(env.stdout, hooks, "SessionStart", legacy) + removeHookCommand(env.stdout, hooks, "PreCompress", legacy) } // Also clear any --hook-json registration from PreCompress. Beads previously // registered bd prime on PreCompress before we discovered that Gemini's // PreCompress event is advisory-only and cannot inject additionalContext // into the model regardless of output format. Re-running setup migrates cleanly. - removeHookCommand(hooks, "PreCompress", "bd prime --hook-json") - removeHookCommand(hooks, "PreCompress", "bd prime --stealth --hook-json") + removeHookCommand(env.stdout, hooks, "PreCompress", "bd prime --hook-json") + removeHookCommand(env.stdout, hooks, "PreCompress", "bd prime --stealth --hook-json") if addHookCommand(hooks, "SessionStart", command) { _, _ = fmt.Fprintln(env.stdout, "✓ Registered SessionStart hook") @@ -246,8 +246,8 @@ func removeGemini(env geminiEnv, project bool) error { "bd prime --stealth --hook-json", } for _, cmd := range variants { - removeHookCommand(hooks, "SessionStart", cmd) - removeHookCommand(hooks, "PreCompress", cmd) + removeHookCommand(env.stdout, hooks, "SessionStart", cmd) + removeHookCommand(env.stdout, hooks, "PreCompress", cmd) } data, err = json.MarshalIndent(settings, "", " ")