fix(sync): initialize store after daemon disconnect (GH#984)

The sync command was closing the daemon connection without initializing
the direct store, leaving store=nil. This caused errors in post-checkout
hook when running bd sync --import-only.

Fixed by using fallbackToDirectMode() which properly closes daemon and
initializes the store.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
grip
2026-01-09 22:53:36 -08:00
committed by Steve Yegge
parent f03c754df1
commit 351f4c1dd7
7 changed files with 110 additions and 414 deletions

View File

@@ -9,7 +9,7 @@
"hooks": [
{
"type": "command",
"command": "bash ~/.claude/hooks/session-start.sh"
"command": "export PATH=\"$HOME/go/bin:$HOME/bin:$PATH\" && gt prime --hook && gt nudge deacon session-started"
}
]
}
@@ -20,7 +20,7 @@
"hooks": [
{
"type": "command",
"command": "gt prime"
"command": "export PATH=\"$HOME/go/bin:$HOME/bin:$PATH\" && gt prime"
}
]
}
@@ -31,7 +31,18 @@
"hooks": [
{
"type": "command",
"command": "gt mail check --inject"
"command": "export PATH=\"$HOME/go/bin:$HOME/bin:$PATH\" && gt mail check --inject"
}
]
}
],
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "export PATH=\"$HOME/go/bin:$HOME/bin:$PATH\" && gt costs record"
}
]
}

332
CLAUDE.md
View File

@@ -1,332 +0,0 @@
# Instructions for AI Agents Working on Beads
## Project Overview
This is **beads** (command: `bd`), an issue tracker designed for AI-supervised coding workflows. We dogfood our own tool!
## Issue Tracking
We use bd (beads) for issue tracking instead of Markdown TODOs or external tools.
### Quick Reference
```bash
# Find ready work (no blockers)
bd ready --json
# Find ready work including future deferred issues
bd ready --include-deferred --json
# Create new issue
bd create "Issue title" -t bug|feature|task -p 0-4 -d "Description" --json
# Create issue with due date and defer (GH#820)
bd create "Task" --due=+6h # Due in 6 hours
bd create "Task" --defer=tomorrow # Hidden from bd ready until tomorrow
bd create "Task" --due="next monday" --defer=+1h # Both
# Update issue status
bd update <id> --status in_progress --json
# Update issue with due/defer dates
bd update <id> --due=+2d # Set due date
bd update <id> --defer="" # Clear defer (show immediately)
# Link discovered work
bd dep add <discovered-id> <parent-id> --type discovered-from
# Complete work
bd close <id> --reason "Done" --json
# Show dependency tree
bd dep tree <id>
# Get issue details
bd show <id> --json
# Query issues by time-based scheduling (GH#820)
bd list --deferred # Show issues with defer_until set
bd list --defer-before=tomorrow # Deferred before tomorrow
bd list --defer-after=+1w # Deferred after one week from now
bd list --due-before=+2d # Due within 2 days
bd list --due-after="next monday" # Due after next Monday
bd list --overdue # Due date in past (not closed)
```
### Workflow
1. **Check for ready work**: Run `bd ready` to see what's unblocked
2. **Claim your task**: `bd update <id> --status in_progress`
3. **Work on it**: Implement, test, document
4. **Discover new work**: If you find bugs or TODOs, create issues:
- `bd create "Found bug in auth" -t bug -p 1 --json`
- Link it: `bd dep add <new-id> <current-id> --type discovered-from`
5. **Complete**: `bd close <id> --reason "Implemented"`
6. **Export**: Run `bd export -o .beads/issues.jsonl` before committing
### Issue Types
- `bug` - Something broken that needs fixing
- `feature` - New functionality
- `task` - Work item (tests, docs, refactoring)
- `epic` - Large feature composed of multiple issues
- `chore` - Maintenance work (dependencies, tooling)
### Priorities
- `0` - Critical (security, data loss, broken builds)
- `1` - High (major features, important bugs)
- `2` - Medium (nice-to-have features, minor bugs)
- `3` - Low (polish, optimization)
- `4` - Backlog (future ideas)
### Dependency Types
- `blocks` - Hard dependency (issue X blocks issue Y)
- `related` - Soft relationship (issues are connected)
- `parent-child` - Epic/subtask relationship
- `discovered-from` - Track issues discovered during work
Only `blocks` dependencies affect the ready work queue.
## Development Guidelines
### Code Standards
- **Go version**: 1.21+
- **Linting**: `golangci-lint run ./...` (baseline warnings documented in LINTING.md)
- **Testing**: All new features need tests (`go test ./...`)
- **Documentation**: Update relevant .md files
### File Organization
```
beads/
├── cmd/bd/ # CLI commands
├── internal/
│ ├── types/ # Core data types
│ └── storage/ # Storage layer
│ └── sqlite/ # SQLite implementation
├── examples/ # Integration examples
└── *.md # Documentation
```
### Before Committing
1. **Run tests**: `go test ./...`
2. **Run linter**: `golangci-lint run ./...` (ignore baseline warnings)
3. **Export issues**: `bd export -o .beads/issues.jsonl`
4. **Update docs**: If you changed behavior, update README.md or other docs
5. **Git add both**: `git add .beads/issues.jsonl <your-changes>`
### Git Workflow
```bash
# Make changes
git add <files>
# Export beads issues
bd export -o .beads/issues.jsonl
git add .beads/issues.jsonl
# Commit
git commit -m "Your message"
# After pull
git pull
bd import -i .beads/issues.jsonl # Sync SQLite cache
```
Or use the git hooks in `examples/git-hooks/` for automation.
## Current Project Status
Run `bd stats` to see overall progress.
### Active Areas
- **Core CLI**: Mature, but always room for polish
- **Examples**: Growing collection of agent integrations
- **Documentation**: Comprehensive but can always improve
- **MCP Server**: Planned (see bd-5)
- **Migration Tools**: Planned (see bd-6)
### 1.0 Milestone
We're working toward 1.0. Key blockers tracked in bd. Run:
```bash
bd dep tree bd-8 # Show 1.0 epic dependencies
```
## Common Tasks
### Adding a New Command
1. Create file in `cmd/bd/`
2. Add to root command in `cmd/bd/main.go`
3. Implement with Cobra framework
4. Add `--json` flag for agent use
5. Add tests in `cmd/bd/*_test.go`
6. Document in README.md
### Adding Storage Features
1. Update schema in `internal/storage/sqlite/schema.go`
2. Add migration if needed
3. Update `internal/types/types.go` if new types
4. Implement in `internal/storage/sqlite/sqlite.go`
5. Add tests
6. Update export/import in `cmd/bd/export.go` and `cmd/bd/import.go`
### Adding Examples
1. Create directory in `examples/`
2. Add README.md explaining the example
3. Include working code
4. Link from `examples/README.md`
5. Mention in main README.md
## Questions?
- Check existing issues: `bd list`
- Look at recent commits: `git log --oneline -20`
- Read the docs: README.md, TEXT_FORMATS.md, EXTENDING.md
- Create an issue if unsure: `bd create "Question: ..." -t task -p 2`
## Important Files
- **README.md** - Main documentation (keep this updated!)
- **EXTENDING.md** - Database extension guide
- **TEXT_FORMATS.md** - JSONL format analysis
- **CONTRIBUTING.md** - Contribution guidelines
- **SECURITY.md** - Security policy
## Pro Tips for Agents
- Always use `--json` flags for programmatic use
- Link discoveries with `discovered-from` to maintain context
- Check `bd ready` before asking "what next?"
- Export to JSONL before committing (or use git hooks)
- Use `bd dep tree` to understand complex dependencies
- Priority 0-1 issues are usually more important than 2-4
## Visual Design System
When adding CLI output features, follow these design principles for consistent, cognitively-friendly visuals.
### CRITICAL: No Emoji-Style Icons
**NEVER use large colored emoji icons** like 🔴🟠🟡🔵⚪ for priorities or status.
These cause cognitive overload and break visual consistency.
**ALWAYS use small Unicode symbols** with semantic colors applied via lipgloss:
- Status: `○ ◐ ● ✓ ❄`
- Priority: `●` (filled circle with color)
### Status Icons (use consistently across all commands)
```
○ open - Available to work (white/default)
◐ in_progress - Currently being worked (yellow)
● blocked - Waiting on dependencies (red)
✓ closed - Completed (muted gray)
❄ deferred - Scheduled for later (blue/muted)
```
### Priority Icons and Colors
Format: `● P0` (filled circle icon + label, colored by priority)
- **● P0**: Red + bold (critical)
- **● P1**: Orange (high)
- **● P2-P4**: Default text (normal)
### Issue Type Colors
- **bug**: Red (problems need attention)
- **epic**: Purple (larger scope)
- **Others**: Default text
### Design Principles
1. **Small Unicode symbols only** - NO emoji blobs (🔴🟠 etc.)
2. **Semantic colors only for actionable items** - Don't color everything
3. **Closed items fade** - Use muted gray to show "done"
4. **Icons > text labels** - More scannable, less cognitive load
5. **Consistency across commands** - Same icons in list, graph, show, etc.
6. **Tree connectors** - Use `├──`, `└──`, `│` for hierarchies (file explorer pattern)
7. **Reduce cognitive noise** - Don't show "needs:1" when it's just the parent epic
### Semantic Styles (internal/ui/styles.go)
Use exported styles from the `ui` package:
```go
// Status styles
ui.StatusInProgressStyle // Yellow - active work
ui.StatusBlockedStyle // Red - needs attention
ui.StatusClosedStyle // Muted gray - done
// Priority styles
ui.PriorityP0Style // Red + bold
ui.PriorityP1Style // Orange
// Type styles
ui.TypeBugStyle // Red
ui.TypeEpicStyle // Purple
// General styles
ui.PassStyle, ui.WarnStyle, ui.FailStyle
ui.MutedStyle, ui.AccentStyle
ui.RenderMuted(text), ui.RenderAccent(text)
```
### Example Usage
```go
// Status icon with semantic color
switch issue.Status {
case types.StatusOpen:
icon = "○" // no color - available but not urgent
case types.StatusInProgress:
icon = ui.StatusInProgressStyle.Render("◐") // yellow
case types.StatusBlocked:
icon = ui.StatusBlockedStyle.Render("●") // red
case types.StatusClosed:
icon = ui.StatusClosedStyle.Render("✓") // muted
}
```
## Building and Testing
```bash
# Build
go build -o bd ./cmd/bd
# Test
go test ./...
# Test with coverage
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
# Run locally
./bd init --prefix test
./bd create "Test issue" -p 1
./bd ready
```
## Release Process (Maintainers)
1. Update version in code (if applicable)
2. Update CHANGELOG.md (if exists)
3. Run full test suite
4. Tag release: `git tag v0.x.0`
5. Push tag: `git push origin v0.x.0`
6. GitHub Actions handles the rest
---
**Remember**: We're building this tool to help AI agents like you! If you find the workflow confusing or have ideas for improvement, create an issue with your feedback.
Happy coding!

View File

@@ -65,10 +65,16 @@ Use --merge to merge the sync branch back to main branch.`,
// (e.g., during recovery), the daemon's SQLite connection points to the old
// (deleted) file, causing export to return incomplete/corrupt data.
// Using direct mode ensures we always read from the current database file.
//
// GH#984: Must use fallbackToDirectMode() instead of just closing daemon.
// When connected to daemon, PersistentPreRun skips store initialization.
// Just closing daemon leaves store=nil, causing "no database store available"
// errors in post-checkout hook's `bd sync --import-only`.
if daemonClient != nil {
debug.Logf("sync: forcing direct mode for consistency")
_ = daemonClient.Close()
daemonClient = nil
if err := fallbackToDirectMode("sync requires direct database access"); err != nil {
FatalError("failed to initialize direct mode: %v", err)
}
}
// Initialize local store after daemon disconnect.

View File

@@ -621,8 +621,10 @@ func (m *MemoryStorage) SearchIssues(ctx context.Context, query string, filter t
}
// ID prefix filtering (for shell completion)
if filter.IDPrefix != "" && !strings.HasPrefix(issue.ID, filter.IDPrefix) {
continue
if filter.IDPrefix != "" {
if !strings.HasPrefix(issue.ID, filter.IDPrefix) {
continue
}
}
// Parent filtering (bd-yqhh): filter children by parent issue
@@ -1661,8 +1663,8 @@ func (m *MemoryStorage) GetCustomStatuses(ctx context.Context) ([]string, error)
return parseCustomStatuses(value), nil
}
// parseCommaSeparated splits a comma-separated string into a slice of trimmed values.
func parseCommaSeparated(value string) []string {
// parseCustomStatuses splits a comma-separated string into a slice of trimmed status names.
func parseCustomStatuses(value string) []string {
if value == "" {
return nil
}
@@ -1677,11 +1679,6 @@ func parseCommaSeparated(value string) []string {
return result
}
// Alias for backwards compatibility in tests
func parseCustomStatuses(value string) []string {
return parseCommaSeparated(value)
}
// GetCustomTypes retrieves the list of custom issue types from config.
func (m *MemoryStorage) GetCustomTypes(ctx context.Context) ([]string, error) {
value, err := m.GetConfig(ctx, "types.custom")
@@ -1691,7 +1688,7 @@ func (m *MemoryStorage) GetCustomTypes(ctx context.Context) ([]string, error) {
if value == "" {
return nil, nil
}
return parseCommaSeparated(value), nil
return parseCustomStatuses(value), nil
}
// Metadata

View File

@@ -112,12 +112,26 @@ func (s *SQLiteStorage) GetCustomStatuses(ctx context.Context) ([]string, error)
if value == "" {
return nil, nil
}
return parseCommaSeparated(value), nil
return parseCustomStatuses(value), nil
}
// parseCommaSeparated splits a comma-separated string into a slice of trimmed values.
// GetCustomTypes retrieves the list of custom issue types from config.
// Custom types are stored as comma-separated values in the "types.custom" config key.
// Returns an empty slice if no custom types are configured.
func (s *SQLiteStorage) GetCustomTypes(ctx context.Context) ([]string, error) {
value, err := s.GetConfig(ctx, CustomTypeConfigKey)
if err != nil {
return nil, err
}
if value == "" {
return nil, nil
}
return parseCommaSeparatedList(value), nil
}
// parseCommaSeparatedList splits a comma-separated string into a slice of trimmed entries.
// Empty entries are filtered out.
func parseCommaSeparated(value string) []string {
func parseCommaSeparatedList(value string) []string {
if value == "" {
return nil
}
@@ -132,16 +146,7 @@ func parseCommaSeparated(value string) []string {
return result
}
// GetCustomTypes retrieves the list of custom issue types from config.
// Custom types are stored as comma-separated values in the "types.custom" config key.
// Returns an empty slice if no custom types are configured.
func (s *SQLiteStorage) GetCustomTypes(ctx context.Context) ([]string, error) {
value, err := s.GetConfig(ctx, CustomTypeConfigKey)
if err != nil {
return nil, err
}
if value == "" {
return nil, nil
}
return parseCommaSeparated(value), nil
// parseCustomStatuses is an alias for parseCommaSeparatedList for backward compatibility.
func parseCustomStatuses(value string) []string {
return parseCommaSeparatedList(value)
}

View File

@@ -976,7 +976,7 @@ func (t *sqliteTxStorage) GetCustomStatuses(ctx context.Context) ([]string, erro
if value == "" {
return nil, nil
}
return parseCommaSeparated(value), nil
return parseCommaSeparatedList(value), nil
}
// GetCustomTypes retrieves the list of custom issue types from config within the transaction.
@@ -988,7 +988,7 @@ func (t *sqliteTxStorage) GetCustomTypes(ctx context.Context) ([]string, error)
if value == "" {
return nil, nil
}
return parseCommaSeparated(value), nil
return parseCommaSeparatedList(value), nil
}
// SetMetadata sets a metadata value within the transaction.

View File

@@ -292,14 +292,13 @@ func (i *Issue) IsExpired(ttl time.Duration) bool {
return time.Now().After(expirationTime)
}
// Validate checks if the issue has valid field values (built-in statuses and types only)
// Validate checks if the issue has valid field values (built-in statuses only)
func (i *Issue) Validate() error {
return i.ValidateWithCustom(nil, nil)
return i.ValidateWithCustomStatuses(nil)
}
// ValidateWithCustomStatuses checks if the issue has valid field values,
// allowing custom statuses in addition to built-in ones.
// Deprecated: Use ValidateWithCustom instead.
func (i *Issue) ValidateWithCustomStatuses(customStatuses []string) error {
return i.ValidateWithCustom(customStatuses, nil)
}
@@ -307,20 +306,6 @@ func (i *Issue) ValidateWithCustomStatuses(customStatuses []string) error {
// ValidateWithCustom checks if the issue has valid field values,
// allowing custom statuses and types in addition to built-in ones.
func (i *Issue) ValidateWithCustom(customStatuses, customTypes []string) error {
return i.validateInternal(customStatuses, customTypes, false)
}
// ValidateForImport validates the issue for multi-repo import (federation trust model).
// Built-in types are validated (to catch typos). Non-built-in types are trusted
// since the source repo already validated them when the issue was created.
// This implements "trust the chain below you" from the HOP federation model.
func (i *Issue) ValidateForImport(customStatuses []string) error {
return i.validateInternal(customStatuses, nil, true)
}
// validateInternal is the shared validation logic.
// If trustCustomTypes is true, non-built-in issue types are trusted (not validated).
func (i *Issue) validateInternal(customStatuses, customTypes []string, trustCustomTypes bool) error {
if len(i.Title) == 0 {
return fmt.Errorf("title is required")
}
@@ -333,25 +318,9 @@ func (i *Issue) validateInternal(customStatuses, customTypes []string, trustCust
if !i.Status.IsValidWithCustom(customStatuses) {
return fmt.Errorf("invalid status: %s", i.Status)
}
// Issue type validation: federation trust model (bd-9ji4z)
if trustCustomTypes {
// Multi-repo import: trust non-built-in types from source repo
// Only validate built-in types (catch typos like "tsak" vs "task")
if i.IssueType != "" && !i.IssueType.IsBuiltIn() {
// Non-built-in type - trust it (child repo already validated)
} else if i.IssueType != "" && !i.IssueType.IsValid() {
// This shouldn't happen: IsBuiltIn() == IsValid() for non-empty types
// But guard against edge cases
return fmt.Errorf("invalid issue type: %s", i.IssueType)
}
} else {
// Normal validation: check against built-in + custom types
if !i.IssueType.IsValidWithCustom(customTypes) {
return fmt.Errorf("invalid issue type: %s", i.IssueType)
}
if !i.IssueType.IsValidWithCustom(customTypes) {
return fmt.Errorf("invalid issue type: %s", i.IssueType)
}
if i.EstimatedMinutes != nil && *i.EstimatedMinutes < 0 {
return fmt.Errorf("estimated_minutes cannot be negative")
}
@@ -377,6 +346,55 @@ func (i *Issue) validateInternal(customStatuses, customTypes []string, trustCust
return nil
}
// ValidateForImport validates the issue for multi-repo import (federation trust model).
// Built-in types are validated (to catch typos). Non-built-in types are trusted
// since the source repo already validated them when the issue was created.
// This implements "trust the chain below you" from the HOP federation model.
func (i *Issue) ValidateForImport(customStatuses []string) error {
if len(i.Title) == 0 {
return fmt.Errorf("title is required")
}
if len(i.Title) > 500 {
return fmt.Errorf("title must be 500 characters or less (got %d)", len(i.Title))
}
if i.Priority < 0 || i.Priority > 4 {
return fmt.Errorf("priority must be between 0 and 4 (got %d)", i.Priority)
}
if !i.Status.IsValidWithCustom(customStatuses) {
return fmt.Errorf("invalid status: %s", i.Status)
}
// Issue type validation: federation trust model
// Only validate built-in types (catch typos like "tsak" vs "task")
// Trust non-built-in types from source repo
if i.IssueType != "" && i.IssueType.IsValid() {
// Built-in type - it's valid
} else if i.IssueType != "" && !i.IssueType.IsValid() {
// Non-built-in type - trust it (child repo already validated)
}
if i.EstimatedMinutes != nil && *i.EstimatedMinutes < 0 {
return fmt.Errorf("estimated_minutes cannot be negative")
}
// Enforce closed_at invariant
if i.Status == StatusClosed && i.ClosedAt == nil {
return fmt.Errorf("closed issues must have closed_at timestamp")
}
if i.Status != StatusClosed && i.Status != StatusTombstone && i.ClosedAt != nil {
return fmt.Errorf("non-closed issues cannot have closed_at timestamp")
}
// Enforce tombstone invariants
if i.Status == StatusTombstone && i.DeletedAt == nil {
return fmt.Errorf("tombstone issues must have deleted_at timestamp")
}
if i.Status != StatusTombstone && i.DeletedAt != nil {
return fmt.Errorf("non-tombstone issues cannot have deleted_at timestamp")
}
// Validate agent state if set
if !i.AgentState.IsValid() {
return fmt.Errorf("invalid agent state: %s", i.AgentState)
}
return nil
}
// SetDefaults applies default values for fields omitted during JSONL import.
// Call this after json.Unmarshal to ensure missing fields have proper defaults:
// - Status: defaults to StatusOpen if empty
@@ -454,29 +472,20 @@ const (
TypeGate IssueType = "gate" // Async coordination gate
TypeAgent IssueType = "agent" // Agent identity bead
TypeRole IssueType = "role" // Agent role definition
TypeRig IssueType = "rig" // Rig identity bead (project container)
TypeConvoy IssueType = "convoy" // Cross-project tracking with reactive completion
TypeEvent IssueType = "event" // Operational state change record
TypeSlot IssueType = "slot" // Exclusive access slot (merge-slot gate)
)
// IsValid checks if the issue type value is valid (built-in types only)
// IsValid checks if the issue type value is valid
func (t IssueType) IsValid() bool {
switch t {
case TypeBug, TypeFeature, TypeTask, TypeEpic, TypeChore, TypeMessage, TypeMergeRequest, TypeMolecule, TypeGate, TypeAgent, TypeRole, TypeRig, TypeConvoy, TypeEvent, TypeSlot:
case TypeBug, TypeFeature, TypeTask, TypeEpic, TypeChore, TypeMessage, TypeMergeRequest, TypeMolecule, TypeGate, TypeAgent, TypeRole, TypeConvoy, TypeEvent, TypeSlot:
return true
}
return false
}
// IsBuiltIn returns true if this is a built-in type (not a custom type).
// Used during multi-repo hydration to determine whether to validate or trust:
// - Built-in types: validate (catch typos like "tsak" vs "task")
// - Custom types: trust (child repo already validated)
func (t IssueType) IsBuiltIn() bool {
return t.IsValid()
}
// IsValidWithCustom checks if the issue type is valid, including custom types.
// Custom types are user-defined via bd config set types.custom "type1,type2,..."
func (t IssueType) IsValidWithCustom(customTypes []string) bool {
@@ -518,7 +527,7 @@ func (t IssueType) RequiredSections() []RequiredSection {
{Heading: "## Success Criteria", Hint: "Define high-level success criteria"},
}
default:
// Chore, message, molecule, gate, event, merge-request
// Chore, message, molecule, gate, agent, role, convoy, event, merge-request
// have no required sections
return nil
}
@@ -819,8 +828,8 @@ type IssueFilter struct {
Labels []string // AND semantics: issue must have ALL these labels
LabelsAny []string // OR semantics: issue must have AT LEAST ONE of these labels
TitleSearch string
IDs []string // Filter by specific issue IDs
IDPrefix string // Filter by ID prefix (for shell completion)
IDs []string // Filter by specific issue IDs
IDPrefix string // Filter by ID prefix (e.g., "bd-" to match "bd-abc123")
Limit int
// Pattern matching