fix: make bd doctor --fix repair broken hook markers (GH#2344)

marker_broken state is now fixable instead of blocking. The migration
generates a write-from-template op that replaces the entire hook with
a clean shebang + managed section.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Executed-By: beads/crew/emma
Rig: beads
Role: crew
This commit is contained in:
Pablo LION 2026-03-04 16:29:39 +01:00 • committed by Steve Yegge
commit fade3b1418
3 changed files with 32 additions and 17 deletions

View file

@ -40,7 +40,7 @@ func TestFixPendingMigrations_AppliesHookMigration(t *testing.T) {
assertExistsHookMigrationFile(t, preCommitPath+".old.migrated")
}
func TestFixPendingMigrations_BrokenMarkerBlocks(t *testing.T) {
func TestFixPendingMigrations_BrokenMarkerIsRepaired(t *testing.T) {
repoDir, hooksDir := setupHookMigrationRepo(t)
preCommitPath := filepath.Join(hooksDir, "pre-commit")
@ -50,16 +50,12 @@ func TestFixPendingMigrations_BrokenMarkerBlocks(t *testing.T) {
t.Fatalf("failed to create .beads: %v", err)
}
err := fixPendingMigrations(repoDir)
if err == nil {
t.Fatal("expected fixPendingMigrations to fail for broken marker state")
}
if !strings.Contains(err.Error(), "hook migration is blocked") {
t.Fatalf("expected blocked migration error, got: %v", err)
if err := fixPendingMigrations(repoDir); err != nil {
t.Fatalf("expected fixPendingMigrations to succeed for broken marker state, got: %v", err)
}
rendered := mustReadHookMigrationFile(t, preCommitPath)
if rendered != brokenContent {
t.Fatalf("expected broken hook content to remain unchanged, got:\n%s", rendered)
if !strings.Contains(rendered, hookSectionBeginPrefix) || !strings.Contains(rendered, hookSectionEndPrefix) {
t.Fatalf("expected repaired hook to contain valid marker section, got:\n%s", rendered)
}
}

View file

@ -135,9 +135,18 @@ func buildHookMigrationExecutionPlan(plan doctor.HookMigrationPlan) hookMigratio
case "marker_managed", "unmanaged_custom", "missing_no_artifacts":
execPlan.NoopHooks = append(execPlan.NoopHooks, hook.Name)
continue
case "marker_broken", "read_error":
case "read_error":
execPlan.BlockingErrors = append(execPlan.BlockingErrors, formatHookMigrationBlockingError(hook))
continue
case "marker_broken":
// Broken markers are fixable: replace entire file with clean section
execPlan.WriteOps = append(execPlan.WriteOps, hookMigrationWriteOp{
HookName: hook.Name,
HookPath: hook.HookPath,
State: hook.State,
SourceKind: hookMigrationWriteFromTemplate,
})
continue
}
sourceKind, sourcePath, err := chooseHookMigrationWriteSource(hook)

View file

@ -109,7 +109,7 @@ func TestApplyHookMigrationExecution_CustomWithSidecarsPreservesHookBody(t *test
assertExistsHookMigrationFile(t, preCommitPath+".backup.migrated")
}
func TestApplyHookMigrationExecution_MarkerBrokenBlocksApply(t *testing.T) {
func TestApplyHookMigrationExecution_MarkerBrokenIsRepaired(t *testing.T) {
repoDir, hooksDir := setupHookMigrationRepo(t)
preCommitPath := filepath.Join(hooksDir, "pre-commit")
@ -121,17 +121,27 @@ func TestApplyHookMigrationExecution_MarkerBrokenBlocksApply(t *testing.T) {
t.Fatalf("PlanHookMigration failed: %v", err)
}
execPlan := buildHookMigrationExecutionPlan(plan)
if len(execPlan.BlockingErrors) == 0 {
t.Fatal("expected blocking errors for broken marker")
if len(execPlan.BlockingErrors) > 0 {
t.Fatalf("broken markers should not be blocking errors, got: %v", execPlan.BlockingErrors)
}
if len(execPlan.WriteOps) == 0 {
t.Fatal("expected a write op to repair the broken marker")
}
if _, err := applyHookMigrationExecution(execPlan); err == nil {
t.Fatal("expected apply to fail for broken marker state")
summary, err := applyHookMigrationExecution(execPlan)
if err != nil {
t.Fatalf("expected apply to succeed for broken marker state, got: %v", err)
}
if summary.WrittenHookCount == 0 {
t.Fatal("expected at least one hook to be written during repair")
}
rendered := mustReadHookMigrationFile(t, preCommitPath)
if rendered != brokenContent {
t.Fatalf("expected broken hook to remain unchanged after blocked apply")
if !strings.Contains(rendered, hookSectionBeginPrefix) || !strings.Contains(rendered, hookSectionEndPrefix) {
t.Fatalf("expected repaired hook to contain valid marker section, got:\n%s", rendered)
}
if !strings.Contains(rendered, "bd hooks run pre-commit") {
t.Fatalf("expected repaired hook to contain hook invocation, got:\n%s", rendered)
}
}