beads/cmd/bd/agent_hook.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

68 lines
2.4 KiB
Go

package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Shared helpers for the agent lifecycle hook commands (codex-hook, cursor-hook).
// Each agent keeps its own event names, input/output schemas, and test stubs, but
// the prime-runner and one-shot refresh-marker mechanics are identical and live
// here to avoid divergence.
// runBdPrime shells out to `bd prime [args...]` and returns its combined output.
// The hooks exec a subprocess (rather than calling prime in process) to avoid
// re-entrant store initialization.
func runBdPrime(ctx context.Context, args ...string) (string, error) {
cmdArgs := append([]string{"prime"}, args...)
// #nosec G702 - os.Args[0] is this bd binary re-invoking itself; cmdArgs is the
// fixed "prime" subcommand plus internal flags, never attacker-controlled input.
cmd := exec.CommandContext(ctx, os.Args[0], cmdArgs...)
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("bd %s: %w: %s", strings.Join(cmdArgs, " "), err, strings.TrimSpace(string(out)))
}
return string(out), nil
}
// agentHookMarkerBaseDir returns the cache directory for one-shot
// post-compaction refresh markers for a given agent (subdir e.g. "codex-hooks").
// override redirects the location for tests.
func agentHookMarkerBaseDir(subdir, override string) string {
if override != "" {
return override
}
if dir, err := os.UserCacheDir(); err == nil && dir != "" {
return filepath.Join(dir, "beads", subdir)
}
return filepath.Join(os.TempDir(), "beads-"+subdir)
}
// agentHookMarkerPath derives a per-session, per-workspace marker file under
// base so concurrent agent sessions don't clobber each other's state. Empty
// keys fall back to stable placeholders.
func agentHookMarkerPath(base, sessionKey, workspaceKey string) string {
if sessionKey == "" {
sessionKey = "unknown-session"
}
if workspaceKey == "" {
workspaceKey = "unknown-workspace"
}
sum := sha256.Sum256([]byte(sessionKey + "\x00" + filepath.Clean(workspaceKey)))
return filepath.Join(base, hex.EncodeToString(sum[:])+".refresh")
}
// writeAgentHookMarker creates the marker directory and writes the one-shot
// refresh marker file.
func writeAgentHookMarker(path string) error {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
return os.WriteFile(path, []byte("1\n"), 0o600) // #nosec G306 -- user-private cache marker
}