feat(doctor): add --dry-run flag and fix registry parsing (bd-qn5, bd-a5z)

- Add --dry-run flag to preview fixes without applying changes
- Handle corrupted/empty/null-byte registry files gracefully
- Treat corrupted registry as empty instead of failing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-03 00:09:25 -08:00
parent 18191f5e54
commit cbf48672e7
3 changed files with 99 additions and 8 deletions

View File

@@ -71,6 +71,7 @@ func (r *Registry) withFileLock(fn func() error) error {
// readEntriesLocked reads all entries from the registry file.
// Caller must hold the file lock.
// bd-qn5: Handles missing, empty, or corrupted registry files gracefully.
func (r *Registry) readEntriesLocked() ([]RegistryEntry, error) {
data, err := os.ReadFile(r.path)
if err != nil {
@@ -80,9 +81,23 @@ func (r *Registry) readEntriesLocked() ([]RegistryEntry, error) {
return nil, fmt.Errorf("failed to read registry: %w", err)
}
// bd-qn5: Handle empty file or file with only whitespace/null bytes
// This can happen if the file was created but never written to, or was corrupted
trimmed := make([]byte, 0, len(data))
for _, b := range data {
if b != 0 && b != ' ' && b != '\t' && b != '\n' && b != '\r' {
trimmed = append(trimmed, b)
}
}
if len(trimmed) == 0 {
return []RegistryEntry{}, nil
}
var entries []RegistryEntry
if err := json.Unmarshal(data, &entries); err != nil {
return nil, fmt.Errorf("failed to parse registry: %w", err)
// bd-qn5: If registry is corrupted, treat as empty rather than failing
// A corrupted registry just means we'll need to rediscover daemons
return []RegistryEntry{}, nil
}
return entries, nil