diff --git a/cmd/bd/doctor_fix_pending_migrations_test.go b/cmd/bd/doctor_fix_pending_migrations_test.go index 6158cc27a..903823abd 100644 --- a/cmd/bd/doctor_fix_pending_migrations_test.go +++ b/cmd/bd/doctor_fix_pending_migrations_test.go @@ -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) } } diff --git a/cmd/bd/migrate_hooks_apply.go b/cmd/bd/migrate_hooks_apply.go index 479286031..1ff19178c 100644 --- a/cmd/bd/migrate_hooks_apply.go +++ b/cmd/bd/migrate_hooks_apply.go @@ -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) diff --git a/cmd/bd/migrate_hooks_apply_test.go b/cmd/bd/migrate_hooks_apply_test.go index 8dfacb68b..6f87ddf62 100644 --- a/cmd/bd/migrate_hooks_apply_test.go +++ b/cmd/bd/migrate_hooks_apply_test.go @@ -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) } }