beads/cmd/bd/setup/hooks_json.go
Michael Hackner 6829d2418a
feat(cursor): bring Cursor integration to parity with Claude/Codex (#4649)
Cursor support is anemic compared to Claude Code and Codex. It ships
only a static rules file: no agent lifecycle hooks, so `bd prime`
context is never injected at session start and is silently lost after
context compaction, and it has no `bd init` auto-install, no `--global`
scope, no shared agent skill, and no `bd doctor` coverage.

This PR brings Cursor up to the same footing as the tier-1 integrations.

- `bd setup cursor` now installs agent lifecycle hooks
  (`.cursor/hooks.json`) alongside the rules file, wiring
  sessionStart/preCompact/postToolUse to a hidden `bd cursor-hook`
  command so Beads context survives compaction:
  * sessionStart injects full `bd prime` context into new sessions
  * preCompact arms a one-shot refresh marker
  * postToolUse re-injects `bd prime` once after a compaction, then
    no-ops
- `bd init` auto-installs Cursor (rules + skill + hooks) and stages
  `.cursor/`
- `bd setup cursor --global` writes `~/.cursor/hooks.json` +
  `~/.agents/skills/beads`
- Canonical rules: `.cursor/rules/beads.mdc` wraps the shared
  `recipes.Template` instead of a hand-maintained copy
- Shared agent skill (`.agents/skills/beads`), removed only once neither
  Cursor nor Codex still relies on it
- `bd doctor` reports Cursor Integration, Settings Health, and Hook
  Completeness checks with agent-mode enrichment

Shared hook mechanics (prime runner + one-shot refresh marker) are
factored into `cmd/bd/agent_hook.go` and reused by codex-hook.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-10 13:10:46 -07:00

32 lines
1,016 B
Go

package setup
import (
"encoding/json"
"strings"
)
// parseHooksJSON unmarshals an agent hooks.json body into a generic map. Empty
// or whitespace-only input yields an empty (non-nil) map with no error, matching
// how both the Codex and Cursor installers treat a missing/blank file. File I/O
// (and absent-file handling) stays with the caller so each agent keeps its own
// read mechanism.
func parseHooksJSON(data []byte) (map[string]interface{}, error) {
config := map[string]interface{}{}
if len(strings.TrimSpace(string(data))) > 0 {
if err := json.Unmarshal(data, &config); err != nil {
return nil, err
}
}
return config, nil
}
// marshalHooksJSON renders an agent hooks.json config with two-space indent and
// a trailing newline (the on-disk format shared by the Codex and Cursor
// installers).
func marshalHooksJSON(config map[string]interface{}) ([]byte, error) {
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return nil, err
}
return append(data, '\n'), nil
}