hostdetect

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package hostdetect identifies which supported host product invoked the current hook process.

Detection is layered: a valid explicit override always wins, then ordered registry signals match environment markers, then bounded top-level payload sniffing runs. Detection fails closed: when nothing matches, the result is PlatformUnknown, never a silent default host.

Payload sniffing targets the stdin-JSON hooks wire formats. The legacy Codex notify payload (argv JSON with dashed keys such as "turn-id") is intentionally not recognized and yields PlatformUnknown; legacy notify consumers know their host statically.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Env

type Env interface {
	LookupEnv(string) (string, bool)
}

Env abstracts environment lookups so callers can inject fakes in tests.

type Platform

type Platform string

Platform identifies a supported host product.

const (
	// PlatformUnknown is the fail-closed result when no signal matches.
	PlatformUnknown Platform = ""
	// PlatformClaude identifies Claude Code.
	PlatformClaude Platform = "claude"
	// PlatformCodex identifies the Codex CLI.
	PlatformCodex Platform = "codex"
)

func Detect

func Detect(registry Registry, override string, env Env, payload []byte) (Platform, error)

Detect resolves the invoking platform.

A non-empty override must name a platform present in the registry; an unknown override is an error rather than an arbitrary Platform. Without an override, env markers are checked in registry order, then payload sniffing runs on bounded top-level JSON. When nothing matches, Detect returns PlatformUnknown with a nil error.

type Registry

type Registry []Signal

Registry is an ordered signal list evaluated tier by tier.

func DefaultRegistry

func DefaultRegistry() Registry

DefaultRegistry returns a fresh copy of the built-in signal order: Codex before Claude. Callers may mutate the returned slice freely; package state is never shared.

No env marker is registered for either platform: Codex natively exports both PLUGIN_ROOT and CLAUDE_PLUGIN_ROOT to plugin-bundled hooks for compatibility, so neither variable discriminates between hosts.

type Signal

type Signal struct {
	// Platform is the detection result when this signal matches.
	Platform Platform
	// EnvMarkers are environment variable names whose presence identifies
	// the platform. A marker must be set (present) to match; its value is
	// not inspected.
	EnvMarkers []string
	// PayloadSniff inspects only top-level keys of the decoded JSON payload.
	// It must not perform typed platform decoding.
	PayloadSniff func(map[string]any) bool
}

Signal describes how one platform is recognized. Earlier registry entries win when more than one signal matches.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL