24 Commits

Author SHA1 Message Date
Steve Yegge
ba1c9d6b17 feat: show redirect info in bd prime output (bd-kblo)
When beads is redirected (e.g., in Gas Town crew clones), bd prime now
shows a notice about the redirect. This helps agents understand why
they share issues with other clones.

CLI mode shows:
> ⚠️ **Redirected**: Local .beads → /path/to/target/.beads
> You share issues with other clones using this redirect.

MCP mode shows:
**Note**: Beads redirected to /path/to/target/.beads (shared with other clones)

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:33:01 -08:00
Steve Yegge
7fb92ff78c feat: implement conditional bond type for mol bond (bd-kzda)
Conditional bonds now work as documented: "B runs only if A fails".

Implementation:
- Add DepConditionalBlocks dependency type to types.go
- Add IsFailureClose() helper to detect failure keywords in close_reason
- Update blocked cache to handle conditional-blocks:
  - B is blocked while A is open
  - B stays blocked if A closes with success
  - B becomes unblocked if A closes with failure

Failure keywords: failed, rejected, wontfix, cancelled, abandoned,
blocked, error, timeout, aborted (case-insensitive)

Updated bondProtoProto, bondProtoMol, bondMolMol to use
DepConditionalBlocks for conditional bond type.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-23 01:32:31 -08:00
Steve Yegge
e778b3f648 feat(status): add deferred status for icebox issues (bd-4jr)
Add 'deferred' as a valid issue status for issues that are deliberately
put on ice - not blocked by dependencies, just postponed for later.

Changes:
- Add StatusDeferred constant and update IsValid() validation
- Add DeferredIssues to Statistics struct with counting in both SQLite
  and memory storage
- Add 'bd defer' command to set status to deferred
- Add 'bd undefer' command to restore status to open
- Update help text across list, search, count, dep, stale, and config
- Update MCP server models and tools to accept deferred status
- Add deferred to blocker status checks (schema, cache, ready, compact)
- Add StatusDeferred to public API exports (beads.go, internal/beads)
- Add snowflake styling for deferred in dep tree and graph views

Semantics:
- deferred vs blocked: deferred is a choice, blocked is forced
- deferred vs closed: deferred will be revisited, closed is done
- Deferred issues excluded from 'bd ready' (already works since
  default filter only includes open/in_progress)
- Deferred issues still block dependents (they are not done!)
- Deferred issues visible in 'bd list' and 'bd stale'

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-20 14:24:48 -08:00
Steve Yegge
ef93b31a74 Export Transaction type for atomic multi-operation support
Add Transaction type alias to both internal/beads and public beads packages.
This allows extensions like VC to use RunInTransaction() with the proper
Transaction interface type for atomic issue creation.

bd-m73k

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-24 22:55:55 -08:00
Steve Yegge
57253f93a3 Context propagation with graceful cancellation (bd-rtp, bd-yb8, bd-2o2)
Complete implementation of signal-aware context propagation for graceful
cancellation across all commands and storage operations.

Key changes:

1. Signal-aware contexts (bd-rtp):
   - Added rootCtx/rootCancel in main.go using signal.NotifyContext()
   - Set up in PersistentPreRun, cancelled in PersistentPostRun
   - Daemon uses same pattern in runDaemonLoop()
   - Handles SIGINT/SIGTERM for graceful shutdown

2. Context propagation (bd-yb8):
   - All commands now use rootCtx instead of context.Background()
   - sqlite.New() receives context for cancellable operations
   - Database operations respect context cancellation
   - Storage layer propagates context through all queries

3. Cancellation tests (bd-2o2):
   - Added import_cancellation_test.go with comprehensive tests
   - Added export cancellation test in export_test.go
   - Tests verify database integrity after cancellation
   - All cancellation tests passing

Fixes applied during review:
   - Fixed rootCtx lifecycle (removed premature defer from PersistentPreRun)
   - Fixed test context contamination (reset rootCtx in test cleanup)
   - Fixed export tests missing context setup

Impact:
   - Pressing Ctrl+C during import/export now cancels gracefully
   - No database corruption or hanging transactions
   - Clean shutdown of all operations

Tested:
   - go build ./cmd/bd ✓
   - go test ./cmd/bd -run TestImportCancellation ✓
   - go test ./cmd/bd -run TestExportCommand ✓
   - Manual Ctrl+C testing verified

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-20 21:57:23 -05:00
Ryan
f7e80dd80c feat: add context optimization features for AI agents (#297)
* feat: add bd prime and setup commands for AI agent integration

This commit consolidates context optimization features for AI agents:

## New Commands

**bd prime** - AI-optimized workflow context injection
- Outputs ~1-2k tokens of workflow context
- Context-aware: adapts to MCP vs CLI mode
- MCP mode: minimal reminders (~500 tokens)
- CLI mode: full command reference (~1-2k tokens)
- Warns against TodoWrite tool and markdown TODOs
- Designed for SessionStart/PreCompact hooks

**bd setup claude** - Claude Code integration installer
- Installs hooks via JSON configuration (not file scripts)
- Supports --project for project-only installation
- Supports --check to verify installation
- Supports --remove to uninstall hooks
- Idempotent (safe to run multiple times)
- Merges with existing settings

**bd setup cursor** - Cursor IDE integration installer
- Creates .cursor/rules/beads.mdc with workflow rules
- Simplified implementation (just overwrites file)

## bd doctor Enhancements

- New: CheckClaude() verifies Claude Code integration
- Detects plugin, MCP server, and hooks installation
- Provides actionable fix suggestions
- Extracted legacy pattern detection to doctor/legacy.go
- Detects JSONL-only mode and warns about legacy issues.jsonl

## Core Improvements

- FindBeadsDir() utility for cross-platform .beads/ discovery
- Works in JSONL-only mode (no database required)
- Sorted noDbCommands alphabetically (one per line for easy diffs)

## Testing

- Unit tests for setup command hook manipulation
- Tests for idempotency, adding/removing hooks
- All tests passing

## Documentation

- cmd/bd/doctor/claude.md - Documents why beads doesn't use Claude Skills
- commands/prime.md - Slash command for bd prime
- Fixed G304 gosec warnings with nosec comments

## Token Efficiency

The bd prime approach reduces AI context usage dramatically:
- MCP mode: ~500 tokens (vs ~10.5k for full MCP tool scan)
- CLI mode: ~1-2k tokens
- 80-99% reduction in standing context overhead

* fix: resolve linting errors in setup utils and remove obsolete test

- Add error check for tmpFile.Close() in setup/utils.go to fix golangci-lint G104
- Remove TestCheckMultipleJSONLFiles test that referenced deleted checkMultipleJSONLFiles function

Fixes golangci-lint errcheck violations introduced in the bd prime/setup feature.
2025-11-12 10:48:36 -08:00
Steve Yegge
8f676a4850 Add public API for external extensions
Created root-level beads.go that re-exports types and functions from
internal packages. This allows external projects (like VC) to import
from github.com/steveyegge/beads without violating Go's internal
package restrictions.

Exports:
- Storage interface and NewSQLiteStorage()
- Core types (Issue, Dependency, Label, etc.)
- Filter types (IssueFilter, WorkFilter)
- All relevant constants (Status, IssueType, DependencyType, etc.)

This matches the usage shown in docs/EXTENDING.md.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-06 13:30:04 -08:00
Steve Yegge
584c266684 Reorganize project structure: move Go files to internal/beads, docs to docs/
Amp-Thread-ID: https://ampcode.com/threads/T-7a71671d-dd5c-4c7c-b557-fa427fceb04f
Co-authored-by: Amp <amp@ampcode.com>
2025-11-05 21:04:00 -08:00
Ryan Snodgrass
22311b9240 Fix symlink path resolution in findDatabaseInTree
Resolve symlinks in working directory before walking up the tree
to find .beads directory. This ensures consistent path handling
when repositories are accessed via symlinks.

Without this fix, daemon operations can fail with 'operation not
permitted' errors when trying to export to JSONL, because the
daemon resolves paths differently than the CLI commands.

Example issue: /Users/user/Code -> /Users/user/Documents/Code
The daemon would try to write to the symlinked path while the
file system expects operations on the resolved path.
2025-11-03 14:48:13 -08:00
Steve Yegge
22756509cc Refactor: Extract path canonicalization into utils.CanonicalizePath()
Extracts duplicated path canonicalization logic (filepath.Abs + EvalSymlinks)
into a reusable helper function utils.CanonicalizePath() in internal/utils/path.go.

Changes:
- Add internal/utils/path.go with CanonicalizePath() function
- Add comprehensive tests in internal/utils/path_test.go
- Replace inline canonicalization in beads.go:131-140
- Replace inline canonicalization in cmd/bd/main.go:446-454
- Replace inline canonicalization in cmd/bd/nodb.go:25-33

The new helper maintains identical behavior:
1. Converts path to absolute form via filepath.Abs
2. Resolves symlinks via filepath.EvalSymlinks
3. Falls back gracefully on errors (returns absPath if EvalSymlinks fails,
   returns original path if Abs fails)

Fixes bd-efe8

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-02 19:11:08 -08:00
Steve Yegge
6ecfd04ec8 Implement BEADS_DIR environment variable (bd-e16b)
Add BEADS_DIR as a replacement for BEADS_DB to point to the .beads
directory instead of the database file directly.

Rationale:
- With --no-db mode, there's no .db file to point to
- The .beads directory is the logical unit (contains config.yaml, db
  files, jsonl files)
- More intuitive: point to the beads directory not the database file

Implementation:
- Add BEADS_DIR environment variable support to FindDatabasePath()
- Priority order: BEADS_DIR > BEADS_DB > auto-discovery
- Maintain backward compatibility with BEADS_DB (now deprecated)
- Update --no-db mode to respect BEADS_DIR
- Update MCP integration (config.py, bd_client.py)
- Update documentation to show BEADS_DIR as preferred method

Testing:
- Backward compatibility: BEADS_DB still works
- BEADS_DIR works with regular database mode
- BEADS_DIR works with --no-db mode
- Priority: BEADS_DIR takes precedence over BEADS_DB

Follow-up issues for refactoring:
- bd-efe8: Refactor path canonicalization into helper function
- bd-c362: Extract database search logic into helper function

Closes bd-e16b

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-02 18:36:30 -08:00
Steve Yegge
537844cb11 Fix bd-36870264: Prevent nested .beads directories with path canonicalization
- Add filepath.Abs() + EvalSymlinks() to FindDatabasePath() to normalize all database paths
- Add nested .beads directory detection in setupDaemonLock() with helpful error messages
- Prevents infinite .beads/.beads/.beads/ recursion when using relative BEADS_DB paths
- All acceptance criteria passed: singleton enforcement, lock release, no recursion

Amp-Thread-ID: https://ampcode.com/threads/T-c7fc78b8-a935-48dc-8453-a1bd47a14f72
Co-authored-by: Amp <amp@ampcode.com>
2025-11-01 19:50:34 -07:00
Steve Yegge
31fcb06059 Fix daemon crash when backup/vc.db files exist
- Changed backup file filtering from checking file extension (.backup) to checking if filename contains '.backup'
- This now properly filters files like 'beads.backup-pre-hash-20251030-171258.db'
- Also exclude vc.db from database detection
- Add strings import to beads.go
- Improve error message to suggest manual removal

Fixes bd-373c
2025-10-31 21:18:08 -07:00
Steve Yegge
f24573a5f8 Enforce canonical database naming (beads.db) - bd-165
- Added CanonicalDatabaseName constant (beads.db) and LegacyDatabaseNames list
- Updated bd init to use canonical name via constant
- Added daemon validation to reject non-canonical database names
- Updated bd migrate to use canonical name constant
- Enhanced FindDatabasePath to warn when using legacy database names
- All database discovery now prefers beads.db with backward compatibility

Closes bd-165
2025-10-26 20:42:18 -07:00
Steve Yegge
881e0940a7 Add config.json support for database path configuration (bd-163)
- Create internal/configfile package for config.json handling
- bd init now creates .beads/config.json with database, version, and jsonl_export fields
- Database discovery checks config.json first, falls back to beads.db
- Update .gitignore to not ignore config.json (part of repo state)
- Update test to expect beads.db and config.json
- Backward compatible with existing beads.db-only setups
2025-10-26 18:44:27 -07:00
Steve Yegge
ae5aee279b Implement bd-162: Enforce canonical database name (beads.db)
- Changed bd init to always create beads.db instead of {prefix}.db
- Added migration logic to detect and rename old databases
- Updated findDatabaseInTree to prefer beads.db and warn on multiple .db files
- Daemon now refuses to start if multiple .db files exist (ambiguity error)
- Updated tests to expect beads.db instead of prefix-based naming
- Tested migration, ambiguity detection, and warning messages
2025-10-26 18:16:27 -07:00
Steve Yegge
ef5128fe24 Export SortPolicy type and constants
Export SortPolicy from beads package so external tools (like VC)
can use the sort policy constants without importing internal packages.

Added:
- SortPolicy type alias
- SortPolicyHybrid, SortPolicyPriority, SortPolicyOldest constants

This completes the sort policy feature for external consumers.
2025-10-25 20:20:12 -07:00
Steve Yegge
b855c444d4 Enable errcheck linter and fix all production code warnings
- Enabled errcheck linter (previously disabled)
- Set tests: false in .golangci.yml to focus on production code
- Fixed 27 errcheck warnings using Go best practices:
  * Database resources: defer func() { _ = rows.Close() }()
  * Transaction rollbacks: defer func() { _ = tx.Rollback() }()
  * Best-effort closers: _ = store.Close(), _ = client.Close()
  * File writes: proper error checking on Close()
  * Interactive input: handle EOF gracefully
  * File ops: ignore ENOENT on os.Remove()
- All tests pass
- Closes bd-58

Amp-Thread-ID: https://ampcode.com/threads/T-57c9afd3-9adf-40c2-8be7-3e493d200361
Co-authored-by: Amp <amp@ampcode.com>
2025-10-25 18:44:38 -07:00
Steve Yegge
3d979b7d9e Add warning for multiple databases in directory hierarchy (bd-75)
- Implement FindAllDatabases() to scan hierarchy for all .beads directories
- Add DatabaseInfo struct with path, beads dir, and issue count
- Add warnMultipleDatabases() with formatted warning display
- Show active database with ▶ marker and issue counts
- Add comprehensive tests for multi-database detection
- Document warning and solutions in TROUBLESHOOTING.md
- Prevent confusion and database pollution from accidental duplicates

Amp-Thread-ID: https://ampcode.com/threads/T-4941975f-2686-40d0-bc12-aabf38a05890
Co-authored-by: Amp <amp@ampcode.com>
2025-10-24 15:28:23 -07:00
Steve Yegge
c2c7eda14f Fix 15 lint errors: dupl, gosec, revive, staticcheck, unparam
Reduced golangci-lint issues from 56 to 41:

Fixed:
- dupl (2→0): Extracted parseLabelArgs helper, added nolint for cobra commands
- gosec G104 (4→0): Handle unhandled errors with _ = assignments
- gosec G302/G306 (4→0): Fixed file permissions from 0644 to 0600
- revive exported (4→0): Added proper godoc comments for all exported types
- staticcheck SA1019 (1→0): Removed deprecated netErr.Temporary() call
- staticcheck SA4003 (1→0): Removed impossible uint64 < 0 check
- unparam (8→0): Removed unused params/returns, added nolint where needed

Renamed types in compact package to avoid stuttering:
- CompactConfig → Config
- CompactResult → Result

Remaining 41 issues are documented baseline:
- gocyclo (24): High complexity in large functions
- gosec G204/G115 (17): False positives for subprocess/conversions

Closes bd-92

Amp-Thread-ID: https://ampcode.com/threads/T-1c136506-d703-4781-bcfa-eb605999545a
Co-authored-by: Amp <amp@ampcode.com>
2025-10-24 12:40:56 -07:00
Steve Yegge
9dcb86ebfb Fix lint errors: handle errors, use fmt.Fprintf, apply De Morgan's law, use switch statements
Amp-Thread-ID: https://ampcode.com/threads/T-afcf56b0-a8bc-4310-bb59-1b63e1d70c89
Co-authored-by: Amp <amp@ampcode.com>
2025-10-24 12:27:07 -07:00
Steve Yegge
5e7b3aa43a Add Beads library API for Go integration
Expose full Storage interface and all types through public beads.go API,
enabling external Go projects (like VC) to import Beads directly instead
of spawning CLI processes.

Changes:
- Expanded beads.go with all public types (Issue, Dependency, Comment, etc.)
- Added all constants (Status, IssueType, DependencyType, EventType)
- Created comprehensive integration tests (beads_integration_test.go)
- Added library usage example at examples/library-usage/
- Documented library integration in README.md

Test coverage: 96.4% on public API, 14 integration tests, all passing.

Closes bd-58, bd-59

Amp-Thread-ID: https://ampcode.com/threads/T-f0093c79-7422-45e2-b0ed-0ddfebc9ffea
Co-authored-by: Amp <amp@ampcode.com>
2025-10-22 15:49:40 -07:00
Steve Yegge
a971762b0e Remove ~/.beads fallback behavior
- Remove ~/.beads/default.db fallback from FindDatabasePath()
- Update daemon to error if no database found instead of falling back
- Update main.go to require explicit database initialization
- Add help/version/quickstart to commands that don't need database
- Add MCP client debug logging for database routing

Amp-Thread-ID: https://ampcode.com/threads/T-2b757a14-cf10-400e-a83c-30349182dd82
Co-authored-by: Amp <amp@ampcode.com>
2025-10-17 10:56:52 -07:00
Travis Cline
3b2c60d294 Better enable go extensions (#14)
* deps: run go mod tidy

* beads: Add public Go API for bd extensions

Implements a minimal public API to enable Go-based extensions without
exposing internal packages:

**New beads.go package:**
- Exports essential types: Issue, Status, IssueType, WorkFilter
- Provides status and issue type constants
- Exposes NewSQLiteStorage() as main entry point for extensions
- Includes comprehensive package documentation

**Updated EXTENDING.md:**
- Replaced internal package imports with public beads package
- Updated function calls to use new public API
- Changed sqlite.New() to beads.NewSQLiteStorage()
- Updated GetReady() to GetReadyWork() with WorkFilter

This enables clean Go-based orchestration extensions while maintaining
API stability and hiding internal implementation details.

* beads: Refine Go extensions API and documentation

Updates to the public Go API implementation following initial commit:

- Enhanced beads.go with refined extension interface
- Updated EXTENDING.md with clearer documentation
- Modified cmd/bd/main.go to support extension loading

Continues work on enabling Go-based bd extensions.

* Fix EXTENDING.md to use beads.WorkFilter instead of types.WorkFilter

The public API exports WorkFilter as beads.WorkFilter, not types.WorkFilter.
This fixes the code example to match the imports shown.

---------

Co-authored-by: Steve Yegge <steve.yegge@gmail.com>
2025-10-14 01:06:35 -07:00