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.
This commit is contained in:
278
cmd/bd/setup/claude_test.go
Normal file
278
cmd/bd/setup/claude_test.go
Normal file
@@ -0,0 +1,278 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAddHookCommand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
existingHooks map[string]interface{}
|
||||
event string
|
||||
command string
|
||||
wantAdded bool
|
||||
}{
|
||||
{
|
||||
name: "add hook to empty hooks",
|
||||
existingHooks: make(map[string]interface{}),
|
||||
event: "SessionStart",
|
||||
command: "bd prime",
|
||||
wantAdded: true,
|
||||
},
|
||||
{
|
||||
name: "hook already exists",
|
||||
existingHooks: map[string]interface{}{
|
||||
"SessionStart": []interface{}{
|
||||
map[string]interface{}{
|
||||
"matcher": "",
|
||||
"hooks": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "command",
|
||||
"command": "bd prime",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
event: "SessionStart",
|
||||
command: "bd prime",
|
||||
wantAdded: false,
|
||||
},
|
||||
{
|
||||
name: "add second hook alongside existing",
|
||||
existingHooks: map[string]interface{}{
|
||||
"SessionStart": []interface{}{
|
||||
map[string]interface{}{
|
||||
"matcher": "",
|
||||
"hooks": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "command",
|
||||
"command": "other command",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
event: "SessionStart",
|
||||
command: "bd prime",
|
||||
wantAdded: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := addHookCommand(tt.existingHooks, tt.event, tt.command)
|
||||
if got != tt.wantAdded {
|
||||
t.Errorf("addHookCommand() = %v, want %v", got, tt.wantAdded)
|
||||
}
|
||||
|
||||
// Verify hook exists in structure
|
||||
eventHooks, ok := tt.existingHooks[tt.event].([]interface{})
|
||||
if !ok {
|
||||
t.Fatal("Event hooks not found")
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, hook := range eventHooks {
|
||||
hookMap := hook.(map[string]interface{})
|
||||
commands := hookMap["hooks"].([]interface{})
|
||||
for _, cmd := range commands {
|
||||
cmdMap := cmd.(map[string]interface{})
|
||||
if cmdMap["command"] == tt.command {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Errorf("Hook command %q not found in event %q", tt.command, tt.event)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveHookCommand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
existingHooks map[string]interface{}
|
||||
event string
|
||||
command string
|
||||
wantRemaining int
|
||||
}{
|
||||
{
|
||||
name: "remove only hook",
|
||||
existingHooks: map[string]interface{}{
|
||||
"SessionStart": []interface{}{
|
||||
map[string]interface{}{
|
||||
"matcher": "",
|
||||
"hooks": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "command",
|
||||
"command": "bd prime",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
event: "SessionStart",
|
||||
command: "bd prime",
|
||||
wantRemaining: 0,
|
||||
},
|
||||
{
|
||||
name: "remove one of multiple hooks",
|
||||
existingHooks: map[string]interface{}{
|
||||
"SessionStart": []interface{}{
|
||||
map[string]interface{}{
|
||||
"matcher": "",
|
||||
"hooks": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "command",
|
||||
"command": "other command",
|
||||
},
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"matcher": "",
|
||||
"hooks": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "command",
|
||||
"command": "bd prime",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
event: "SessionStart",
|
||||
command: "bd prime",
|
||||
wantRemaining: 1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
removeHookCommand(tt.existingHooks, tt.event, tt.command)
|
||||
|
||||
eventHooks, ok := tt.existingHooks[tt.event].([]interface{})
|
||||
if !ok && tt.wantRemaining > 0 {
|
||||
t.Fatal("Event hooks not found")
|
||||
}
|
||||
|
||||
if len(eventHooks) != tt.wantRemaining {
|
||||
t.Errorf("Expected %d remaining hooks, got %d", tt.wantRemaining, len(eventHooks))
|
||||
}
|
||||
|
||||
// Verify target hook is actually gone
|
||||
for _, hook := range eventHooks {
|
||||
hookMap := hook.(map[string]interface{})
|
||||
commands := hookMap["hooks"].([]interface{})
|
||||
for _, cmd := range commands {
|
||||
cmdMap := cmd.(map[string]interface{})
|
||||
if cmdMap["command"] == tt.command {
|
||||
t.Errorf("Hook command %q still present after removal", tt.command)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasBeadsHooks(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
settingsData map[string]interface{}
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "has bd prime hook",
|
||||
settingsData: map[string]interface{}{
|
||||
"hooks": map[string]interface{}{
|
||||
"SessionStart": []interface{}{
|
||||
map[string]interface{}{
|
||||
"matcher": "",
|
||||
"hooks": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "command",
|
||||
"command": "bd prime",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "no hooks",
|
||||
settingsData: map[string]interface{}{},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "has other hooks but not bd prime",
|
||||
settingsData: map[string]interface{}{
|
||||
"hooks": map[string]interface{}{
|
||||
"SessionStart": []interface{}{
|
||||
map[string]interface{}{
|
||||
"matcher": "",
|
||||
"hooks": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "command",
|
||||
"command": "other command",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
settingsPath := filepath.Join(tmpDir, "settings.json")
|
||||
|
||||
data, err := json.Marshal(tt.settingsData)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal test data: %v", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(settingsPath, data, 0644); err != nil {
|
||||
t.Fatalf("Failed to write test file: %v", err)
|
||||
}
|
||||
|
||||
got := hasBeadsHooks(settingsPath)
|
||||
if got != tt.want {
|
||||
t.Errorf("hasBeadsHooks() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdempotency(t *testing.T) {
|
||||
// Test that running addHookCommand twice doesn't duplicate hooks
|
||||
hooks := make(map[string]interface{})
|
||||
|
||||
// First add
|
||||
added1 := addHookCommand(hooks, "SessionStart", "bd prime")
|
||||
if !added1 {
|
||||
t.Error("First call should have added the hook")
|
||||
}
|
||||
|
||||
// Second add (should detect existing)
|
||||
added2 := addHookCommand(hooks, "SessionStart", "bd prime")
|
||||
if added2 {
|
||||
t.Error("Second call should have detected existing hook")
|
||||
}
|
||||
|
||||
// Verify only one hook exists
|
||||
eventHooks := hooks["SessionStart"].([]interface{})
|
||||
if len(eventHooks) != 1 {
|
||||
t.Errorf("Expected 1 hook, got %d", len(eventHooks))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user