Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

5.2 KB
Raw Blame
package cmd

import (
	"fmt"
	"os"
	"strings"

	"github.com/spf13/cobra"
)

var platformFlag string

// hookCommand is the subcommand this process runs (session-start, pre-tool, ...), as cobra resolved
// it. It is how a run tells whether another hook entry runs the same command; see
// cursorRecordsThisHook.
var hookCommand string

// The endpoint settings a hook needs, as flags.
//
// They already existed as environment variables, and still do -- an already-installed hook carries
// them as an inline `VAR=value cmd` prefix and must keep working. Flags exist because that prefix is
// a POSIX shell construct: it is not valid in cmd.exe or PowerShell, so on Windows there is no way
// to put the values in the environment from inside a hook command string at all.
//
// Passing them as arguments removes the shell from the equation entirely. One argv, no quoting rules
// beyond the path itself, and the same command works whichever shell a runtime happens to invoke it
// with -- which is also a simplification on POSIX, where the only reason a shell was involved was to
// carry these three values.
var (
	logFlag    string
	configFlag string
	cliFlag    string
)

var rootCmd = &cobra.Command{
	Use:   "beacon-hooks",
	Short: "Beacon hooks for Claude Code, Codex, GitHub Copilot, Cursor, VS Code, Devin, Factory, goose, Grok, Hermes, Antigravity, DeepSeek Harness, Kimi Code, Kiro, opencode, OpenHands, Qwen Code, Muse Code, and Cline",
	Long: `Beacon hooks binary for Claude Code, Codex, GitHub Copilot, Cursor, VS Code, Devin, Factory, goose, Grok, Hermes, Antigravity, DeepSeek Harness, Kimi Code, Kiro, opencode, OpenHands, Qwen Code, Muse Code, and Cline integration.

This binary provides hook commands that are called by IDE plugin systems
to evaluate code changes for security violations.

Use --platform to specify the calling platform (default: claude).`,
}

func init() {
	rootCmd.PersistentFlags().StringVar(&platformFlag, "platform", "claude", "Platform context: claude, cline, codex, antigravity, copilot, cursor, dsh, vscode, devin, devin-cli, devin-desktop, factory, goose, grok, hermes, kimi, kiro, muse, opencode, openhands, or qwen")
	rootCmd.PersistentFlags().StringVar(&logFlag, "log", "", "Endpoint runtime log to append to (same value as BEACON_ENDPOINT_LOG)")
	rootCmd.PersistentFlags().StringVar(&configFlag, "config", "", "Endpoint config to read (same value as BEACON_ENDPOINT_CONFIG)")
	// --cli is ignored. Every hook command written before inventory became a scheduled job
	// carries it, and those commands are only rewritten by a hooks reinstall, so it must keep
	// parsing. Hidden rather than deprecated: cobra prints a deprecation notice to stderr on
	// every use, and several runtimes surface hook stderr to the person at the keyboard.
	rootCmd.PersistentFlags().StringVar(&cliFlag, "cli", "", "Ignored; kept for hook commands written by older versions (mapped to BEACON_ENDPOINT_CLI)")
	_ = rootCmd.PersistentFlags().MarkHidden("cli")
	rootCmd.PersistentPreRun = func(cmd *cobra.Command, _ []string) {
		hookCommand = cmd.Name()
		applyEndpointFlagsToEnv()
	}
}

// applyEndpointFlagsToEnv puts flag values where the rest of the binary already looks for them.
//
// Deliberately a translation into the environment rather than a config struct threaded through every
// reader. These values are read in eight places across three packages -- logging, cloudshuttle, the
// event commands -- and every one of them reads the environment today. Rewriting all of them to
// accept a parameter would be a large mechanical change whose only purpose is plumbing, and it would
// leave two ways to answer the same question during the transition. One source of truth is worth
// more here than avoiding a Setenv in a process that exists for a few milliseconds and does one job.
//
// It also means a child process inherits them, which is what the policy provider subprocess relies on.
//
// A flag beats an inherited variable. Both can be present -- an already-installed POSIX hook carries
// the env prefix, and a repair rewrites the command to use flags -- and when they disagree the flag
// is the more specific statement of intent, because something wrote it into this exact command.
func applyEndpointFlagsToEnv() {
	for _, mapping := range []struct{ value, key string }{
		{logFlag, "BEACON_ENDPOINT_LOG"},
		{configFlag, "BEACON_ENDPOINT_CONFIG"},
		{cliFlag, "BEACON_ENDPOINT_CLI"},
	} {
		if value := strings.TrimSpace(mapping.value); value != "" {
			_ = os.Setenv(mapping.key, value)
		}
	}

	// Any of them implies endpoint mode, which is the condition several readers gate on before
	// falling back to a default log path. Beacon only ever passes these when it installed the hook
	// for an endpoint, so the presence of one is the same signal BEACON_ENDPOINT_MODE=1 carried --
	// and inferring it here means the emitted command does not have to spend an argument saying so.
	if strings.TrimSpace(logFlag) == "" && strings.TrimSpace(configFlag) == "" && strings.TrimSpace(cliFlag) == "" {
		return
	}
	if os.Getenv("BEACON_ENDPOINT_MODE") == "" {
		_ = os.Setenv("BEACON_ENDPOINT_MODE", "1")
	}
}

// Execute runs the root command
func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}