Fix two daemon bugs: race condition on deletion (bd-155) and wrong project context (bd-156)

- bd-155: Add pre-import flush in daemon sync cycle to prevent deleted issues from being re-imported
- bd-155: Reduce default daemon polling interval from 5 minutes to 10 seconds
- bd-156: Fix hardcoded 'vc.db' - now properly detects existing .db files in local .beads/ directory
This commit is contained in:
Steve Yegge
2025-10-26 17:09:20 -07:00
parent b5a2734f78
commit e657931186
3 changed files with 52 additions and 18 deletions

View File

@@ -29,7 +29,7 @@ var daemonCmd = &cobra.Command{
Long: `Run a background daemon that automatically syncs issues with git remote.
The daemon will:
- Poll for changes at configurable intervals (default: 5 minutes)
- Poll for changes at configurable intervals (default: 10 seconds)
- Export pending database changes to JSONL
- Auto-commit changes if --auto-commit flag set
- Auto-push commits if --auto-push flag set
@@ -172,7 +172,7 @@ Use --health to check daemon health and metrics.`,
}
func init() {
daemonCmd.Flags().Duration("interval", 5*time.Minute, "Sync check interval")
daemonCmd.Flags().Duration("interval", 10*time.Second, "Sync check interval")
daemonCmd.Flags().Bool("auto-commit", false, "Automatically commit changes")
daemonCmd.Flags().Bool("auto-push", false, "Automatically push commits")
daemonCmd.Flags().Bool("stop", false, "Stop running daemon")
@@ -969,16 +969,24 @@ func createSyncFunc(ctx context.Context, store storage.Storage, autoCommit, auto
}
if err := gitPull(syncCtx); err != nil {
log.log("Pull failed: %v", err)
return
log.log("Pull failed: %v", err)
return
}
log.log("Pulled from remote")
if err := importToJSONLWithStore(syncCtx, store, jsonlPath); err != nil {
log.log("Import failed: %v", err)
return
}
log.log("Imported from JSONL")
// Flush any pending dirty issues to JSONL BEFORE importing
// This prevents the race condition where deletions get re-imported (bd-155)
if err := exportToJSONLWithStore(syncCtx, store, jsonlPath); err != nil {
log.log("Pre-import flush failed: %v", err)
return
}
log.log("Flushed pending changes before import")
if err := importToJSONLWithStore(syncCtx, store, jsonlPath); err != nil {
log.log("Import failed: %v", err)
return
}
log.log("Imported from JSONL")
if autoPush && autoCommit {
if err := gitPush(syncCtx); err != nil {

View File

@@ -147,22 +147,36 @@ var rootCmd = &cobra.Command{
// Special case for import: if we found a database but there's a local .beads/
// directory without a database, prefer creating a local database
if cmd.Name() == cmdImport && localBeadsDir != "" {
if _, err := os.Stat(localBeadsDir); err == nil {
// Check if found database is NOT in the local .beads/ directory
if !strings.HasPrefix(dbPath, localBeadsDir+string(filepath.Separator)) {
// Use local .beads/vc.db instead for import
dbPath = filepath.Join(localBeadsDir, "vc.db")
if _, err := os.Stat(localBeadsDir); err == nil {
// Check if found database is NOT in the local .beads/ directory
if !strings.HasPrefix(dbPath, localBeadsDir+string(filepath.Separator)) {
// Look for existing .db file in local .beads/ directory
matches, _ := filepath.Glob(filepath.Join(localBeadsDir, "*.db"))
if len(matches) > 0 {
dbPath = matches[0]
} else {
// No database exists yet - will be created by import
// Use generic name that will be renamed after prefix detection
dbPath = filepath.Join(localBeadsDir, "bd.db")
}
}
}
}
} else {
// For import command, allow creating database if .beads/ directory exists
if cmd.Name() == cmdImport && localBeadsDir != "" {
if _, err := os.Stat(localBeadsDir); err == nil {
// For import command, allow creating database if .beads/ directory exists
if cmd.Name() == cmdImport && localBeadsDir != "" {
if _, err := os.Stat(localBeadsDir); err == nil {
// Look for existing .db file in local .beads/ directory
matches, _ := filepath.Glob(filepath.Join(localBeadsDir, "*.db"))
if len(matches) > 0 {
dbPath = matches[0]
} else {
// .beads/ directory exists - set dbPath for import to create
dbPath = filepath.Join(localBeadsDir, "vc.db")
// Use generic name that will be renamed after prefix detection
dbPath = filepath.Join(localBeadsDir, "bd.db")
}
}
}
// If dbPath still not set, error out
if dbPath == "" {