bridle package - github.com/CarriedWorldUniverse/bridle - Go Packages

bridle

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

bridle

CI Release Go Reference License

The harness layer of the Nexus funnel/harness split. A small Go library that drives one deliberation turn of one model with one tool surface, emitting a stream of observable events and returning a structured TurnResult.

The funnel imports bridle. Aspects do not import bridle directly.

A bridle controls and directs without being the horse — sits beneath the funnel, governs what the model produces, provider-agnostic.

Status

Spec drafted, no implementation yet. See docs/2026-05-01-bridle-spec.md for the build brief.

Scope

  • One stable provider interface, N implementations: claude-api, ollama-local, openai-api.
  • register_hooks-style interception surface for §5.6 behaviors.
  • send_comms is just a tool the funnel supplies — bridle has no special case.
  • Funnel owns session JSONL; bridle proposes deltas.

Non-goals

  • Not a framework, not an agent runtime, not a session store. It does one thing: drive one turn.
  • See spec §9 for the full non-goals list.

Reference reading

PydanticAI (iter() / CallToolsNode), Eino (streaming + tool-call normalization), PI (headless JSON streaming), Strands (register_hooks), LangGraph (interrupt/checkpoint).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrModelRequired = errors.New("bridle: TurnRequest.Model is required")

ErrModelRequired is returned by RunTurn when TurnRequest.Model is empty.

View Source
var ErrToolNameCollision = errorString("bridle: tool name collision between explicit Tools and MCP-loaded tools")

ErrToolNameCollision is returned by RunTurn when a tool name appears in both TurnRequest.Tools (explicit) and the MCP-loaded tool surface.

Functions

func IsProviderErrorKind added in v0.1.1

func IsProviderErrorKind(err error, kind ProviderErrorKind) bool

IsProviderErrorKind reports whether err (or any error in its chain) is a ProviderError with the given kind.

Types

type AfterModelChunkCtx

type AfterModelChunkCtx struct {
	Chunk ModelChunk
	Step  int
}

AfterModelChunkCtx carries context passed to AfterModelChunk hooks.

type AfterToolCallCtx

type AfterToolCallCtx struct {
	Call   ToolCall
	Result ToolCallResult
	Step   int
}

AfterToolCallCtx carries context passed to AfterToolCall hooks.

type BeforeModelCallCtx

type BeforeModelCallCtx struct {
	Request TurnRequest
	Step    int
}

BeforeModelCallCtx carries context passed to BeforeModelCall hooks.

type BeforeToolCallCtx

type BeforeToolCallCtx struct {
	Call ToolCall
	Step int
}

BeforeToolCallCtx carries context passed to BeforeToolCall hooks.

type Event

type Event interface {
	// contains filtered or unexported methods
}

Event is the union type for all observable harness events.

type EventSink

type EventSink interface {
	Emit(Event)
}

EventSink receives events as the turn unfolds.

type Harness

type Harness struct {
	// contains filtered or unexported fields
}

Harness drives one deliberation turn with one provider.

func NewHarness

func NewHarness(p Provider) *Harness

NewHarness creates a Harness backed by the given provider.

func (*Harness) RegisterAfterModelChunk

func (h *Harness) RegisterAfterModelChunk(fn Hook[AfterModelChunkCtx])

RegisterAfterModelChunk adds a hook that fires on each ModelChunk event.

func (*Harness) RegisterAfterToolCall

func (h *Harness) RegisterAfterToolCall(fn Hook[AfterToolCallCtx])

RegisterAfterToolCall adds a hook that fires after each tool execution.

func (*Harness) RegisterBeforeModelCall

func (h *Harness) RegisterBeforeModelCall(fn Hook[BeforeModelCallCtx])

RegisterBeforeModelCall adds a hook that fires before each model invocation.

func (*Harness) RegisterBeforeToolCall

func (h *Harness) RegisterBeforeToolCall(fn Hook[BeforeToolCallCtx])

RegisterBeforeToolCall adds a hook that fires before each tool execution.

func (*Harness) RegisterOnStepBoundary

func (h *Harness) RegisterOnStepBoundary(fn Hook[OnStepBoundaryCtx])

RegisterOnStepBoundary adds a hook that fires between tool-call rounds.

func (*Harness) RegisterOnTurnDone

func (h *Harness) RegisterOnTurnDone(fn Hook[OnTurnDoneCtx])

RegisterOnTurnDone adds a hook that fires after the turn completes. Hooks may mutate TurnResult.SessionDelta.

func (*Harness) RunTurn

func (h *Harness) RunTurn(ctx context.Context, req TurnRequest, runner ToolRunner, sink EventSink) (result TurnResult, err error)

RunTurn drives one turn: calls the provider, executes tool calls via runner, fires hooks at documented points, and emits events to sink. Cancellation via ctx returns a partial TurnResult with StopReason=aborted. Returns ErrModelRequired if req.Model is empty.

type Hook

type Hook[T any] func(ctx context.Context, in T) (T, HookAction, error)

Hook is the generic hook signature. T is the mutable context value passed in and returned. Registration order is the execution order.

type HookAction

type HookAction int

HookAction tells the harness what to do after a hook returns.

const (
	HookContinue HookAction = iota
	HookAbort               // end the turn; partial TurnResult returned with StopReason=aborted
)

type InboxItem

type InboxItem struct {
	From    string
	Content string
	MsgID   int64
	RawJSON json.RawMessage

	// ThreadRoot is the canonical thread identity for the message
	// (linked-list root id; nexus task #226). The funnel uses it to
	// key per-thread session state so each thread gets its own
	// claude-code jsonl, preventing SessionTail bleed across threads.
	// Zero = legacy/non-chat synthetic item or pre-#226 row.
	ThreadRoot int64

	// Source identifies which trigger channel produced this item.
	// Empty defaults to "chat" (legacy / nexus-chat substrate).
	// agora-side callers set Source="tty" for operator-typed inputs,
	// allowing ReturnHandlers to branch routing (chat → bus reply,
	// tty → panel-only). Future trigger channels add new Source
	// values; consumers default-treat unknown values as "chat" for
	// backward compat.
	Source string
}

InboxItem is a comms message that arrived during the previous turn. The harness folds these into the prompt context before the first model call. Read-only from the harness's perspective.

MsgID is the chat msg_id this item was sourced from. It carries through into the prompt so the model can reference items by id when triaging ("triage(msg_id=17, decision='reply')"). Zero means the item didn't originate from a chat message — it's an internal/synthetic item the funnel injected, and the triage contract doesn't apply.

type MCPClientConfig

type MCPClientConfig struct {
	Servers []MCPServerSpec
}

MCPClientConfig describes how bridle connects to MCP servers and what tool surface the model sees from them. The funnel constructs this; bridle consumes it. Ignored by subprocess-stream providers (SupportsMCP=false).

type MCPServerSpec

type MCPServerSpec struct {
	Name      string            // local identifier, used in tool-call provenance
	Transport MCPTransport      // stdio | http_sse
	Command   []string          // argv to spawn the server (stdio only)
	URL       string            // server URL (http_sse only)
	Env       map[string]string // environment variables for spawned server (stdio only)
	Header    map[string]string // request headers (http_sse only)
}

MCPServerSpec describes a single MCP server connection.

type MCPTransport

type MCPTransport string

MCPTransport identifies the wire transport for an MCP server connection.

const (
	MCPTransportStdio   MCPTransport = "stdio"
	MCPTransportHTTPSSE MCPTransport = "http_sse"
)

type ModelChunk

type ModelChunk struct {
	Text string
}

ModelChunk carries a streamed text fragment from the model.

type NormalizedSessionEvent

type NormalizedSessionEvent struct {
	Role    SessionRole
	Content string // human-readable text representation
}

NormalizedSessionEvent is a provider-agnostic view of a SessionEvent, used when displaying or processing session history without knowing the provider's wire format.

func ParseSessionEvent

func ParseSessionEvent(e SessionEvent) (NormalizedSessionEvent, error)

ParseSessionEvent returns a normalized view of a session event. For events with RawJSON, it attempts a provider-specific parse; falls back to Content if RawJSON is absent or unrecognized.

type OnStepBoundaryCtx

type OnStepBoundaryCtx struct {
	Step int
}

OnStepBoundaryCtx carries context passed to OnStepBoundary hooks.

type OnTurnDoneCtx

type OnTurnDoneCtx struct {
	Result *TurnResult
}

OnTurnDoneCtx carries context passed to OnTurnDone hooks. Hooks may mutate SessionDelta before it is returned to the funnel.

type Provider

type Provider interface {
	Name() ProviderID
	Capabilities() ProviderCapabilities
	RunTurn(ctx context.Context, req ProviderRequest, sink EventSink) (ProviderResult, error)
}

Provider is the interface every model backend must implement. Provider-specific weirdness (streaming, wire format, tool-schema translation) stays inside the implementation; the harness sees a uniform event stream.

type ProviderCapabilities

type ProviderCapabilities struct {
	Category               ProviderCategory
	SupportsCustomTools    bool // funnel can pass arbitrary Tools via TurnRequest
	SupportsBeforeToolCall bool // BeforeToolCall hook fires
	SupportsAfterToolCall  bool // AfterToolCall hook fires
	SupportsMCP            bool // provider consumes TurnRequest.MCP (direct-api only)
}

ProviderCapabilities advertises what a provider supports so the harness and funnel can route turns correctly.

type ProviderCategory

type ProviderCategory string

ProviderCategory classifies how a provider executes tool calls.

const (
	// CategoryDirectAPI — provider talks directly to a model API; bridle owns the tool loop.
	CategoryDirectAPI ProviderCategory = "direct-api"
	// CategorySubprocessStream — provider spawns a subprocess that runs its own agentic loop
	// and emits a structured event stream. The subprocess owns tool execution.
	CategorySubprocessStream ProviderCategory = "subprocess-stream"
)

type ProviderError added in v0.1.1

type ProviderError struct {
	Kind    ProviderErrorKind
	Message string
	Err     error // underlying error (may be nil)
}

ProviderError is a classified provider-level error.

func (*ProviderError) Error added in v0.1.1

func (e *ProviderError) Error() string

func (*ProviderError) Unwrap added in v0.1.1

func (e *ProviderError) Unwrap() error

type ProviderErrorKind added in v0.1.1

type ProviderErrorKind string

ProviderErrorKind classifies a provider-level error so callers can surface a distinct diagnosis string instead of an opaque exit code.

const (
	ProviderErrorAuthFailed   ProviderErrorKind = "auth_failed"
	ProviderErrorRateLimit    ProviderErrorKind = "rate_limit"
	ProviderErrorServerError  ProviderErrorKind = "server_error"
	ProviderErrorNetworkError ProviderErrorKind = "network_error"
	ProviderErrorTimeout      ProviderErrorKind = "timeout"
	ProviderErrorTLSError     ProviderErrorKind = "tls_error"
)

type ProviderID

type ProviderID string

ProviderID identifies a model provider.

const (
	ProviderClaude    ProviderID = "claude-api"
	ProviderClaudePty ProviderID = "claude-pty"
	ProviderOllama    ProviderID = "ollama-local"
	ProviderOpenAI    ProviderID = "openai-api"
	ProviderBedrock   ProviderID = "bedrock"
	ProviderGemini    ProviderID = "gemini-api"
	ProviderGeminiCLI ProviderID = "gemini-cli"
)

type ProviderMessage

type ProviderMessage struct {
	Role       string // "user" | "assistant" | "tool_result" | "system"
	Content    string
	ToolCallID string           // links a tool_result back to the call that produced it
	ToolName   string           // function-declaration name; required for tool_result on Gemini
	ToolCalls  []ToolInvocation // tool_use blocks for assistant turns; nil on other roles
}

ProviderMessage is a single exchange entry in provider-agnostic form.

For Role == "tool_result", both ToolCallID and ToolName must be set. ToolCallID is the call instance identifier the assistant emitted (used to correlate this result with that specific invocation). ToolName is the function-declaration name that was called (e.g. "send_chat") — some providers (Gemini's FunctionResponse) require it to be present alongside the call id, because their wire format keys responses by declaration name, not by call id. Providers that key only by call id (Anthropic, OpenAI, Ollama) ignore ToolName and the field can be left empty without harm.

For Role == "assistant", ToolCalls carries the structured tool_use blocks the model emitted on this turn. Providers that send assistant history back to the model (claude, openai, gemini, bedrock) MUST reconstruct these as native tool_use blocks; sending only Content as plain text loses the tool-call structure and breaks multi-turn tool conversations on strict providers (Bedrock rejects, Anthropic and OpenAI are lenient but degrade). Content and ToolCalls can both be non-empty — text and tool_use blocks coexist in one assistant turn.

type ProviderRequest

type ProviderRequest struct {
	AspectID           string
	AppendSystemPrompt string
	Session            SessionHandle // for subprocess-stream: resume key; for direct-api: may be empty
	Messages           []ProviderMessage
	Tools              []ToolDef
	ToolChoice         string           // see TurnRequest.ToolChoice
	MCP                *MCPClientConfig // nil = no MCP tools
	MaxSteps           int
	Model              string

	// Cwd is the working directory for subprocess-style providers (see
	// TurnRequest.Cwd). Empty falls through to bridle's host cwd. Direct-
	// API providers ignore this field.
	Cwd string

	// ProviderEnv is per-turn auth/routing env (see TurnRequest.ProviderEnv).
	// Subprocess providers overlay it onto the spawned process's env;
	// direct-API providers read it as auth/base-url config. Empty/nil =
	// provider uses its own default config.
	ProviderEnv map[string]string
}

ProviderRequest is the harness-internal lowered form of TurnRequest. System prompt is assembled, session tail is flattened to the provider's message format, tools are translated to provider-specific schema, and inbox items are folded in.

type ProviderResult

type ProviderResult struct {
	FinalText    string
	ToolCalls    []ToolInvocation
	StepCount    int
	Usage        Usage
	StopReason   StopReason
	SessionDelta []SessionEvent
}

ProviderResult is the harness-internal result from one provider turn step.

type SessionEvent

type SessionEvent struct {
	Provider ProviderID  `json:"provider,omitempty"` // who produced this event
	Role     SessionRole `json:"role"`
	Content  string      `json:"content,omitempty"`
	// RawJSON carries provider-specific blocks (tool_use, tool_result, etc.)
	// that don't fit the plain content field. Valid only in conjunction with Provider.
	RawJSON json.RawMessage `json:"raw,omitempty"`
}

SessionEvent is a single entry in a session's event log. The harness consumes SessionTail on the way in and proposes SessionDelta on the way out.

type SessionHandle

type SessionHandle struct {
	ID  string // opaque to the funnel; meaningful to the provider
	New bool   // true on the first invocation for this ID; false on continuations
}

SessionHandle is an opaque reference to provider-side session state. The funnel mints handles and maps them to threads; the provider uses the ID to resume state (e.g., --resume <session-id> for subprocess-stream). For direct-api providers, Handle may be empty; state comes from SessionTail.

New tells the provider whether the funnel is initiating a fresh session for this ID (true) or asking it to continue an existing one (false). For subprocess-stream providers like claudecode that maintain their own jsonl files, this is the difference between "create with this id" vs "load existing id". Direct-api providers that derive state from SessionTail can ignore this field.

type SessionRole

type SessionRole string

SessionRole identifies who produced a session event.

const (
	RoleUser      SessionRole = "user"
	RoleAssistant SessionRole = "assistant"
	RoleTool      SessionRole = "tool"
	RoleSystem    SessionRole = "system"
)

type StepBoundary

type StepBoundary struct {
	Step int
}

StepBoundary fires between tool-call rounds. Step 1 = the first round; fires after its results are sent back to the model.

type StopReason

type StopReason string

StopReason explains why a turn ended.

const (
	StopReasonModelDone StopReason = "model_done"
	StopReasonMaxSteps  StopReason = "max_steps"
	StopReasonError     StopReason = "error"
	StopReasonAborted   StopReason = "aborted"
	// StopReasonProcessExit is set when the underlying provider process
	// exited non-zero AFTER producing parseable assistant content. The
	// returned ProviderResult carries whatever the model said before
	// the exit — callers should treat the result as truncated-but-real,
	// not discard it. Common cause: hitting an output-token cap and the
	// CLI surfacing that as a non-zero exit rather than a clean stop.
	StopReasonProcessExit StopReason = "process_exit"
)

type ToolCall

type ToolCall struct {
	ID   string
	Name string
	Args json.RawMessage
}

ToolCall is a single invocation the model requested.

type ToolCallResult

type ToolCallResult struct {
	ID     string
	Result json.RawMessage
	Err    string // non-empty if the tool runner returned an error
}

ToolCallResult fires after the tool runner returns (or errors).

type ToolCallStart

type ToolCallStart struct {
	ID   string
	Name string
	Args json.RawMessage
}

ToolCallStart fires when the model requests a tool call, before execution.

type ToolDef

type ToolDef struct {
	Name        string
	Description string
	// InputSchema is a JSON Schema object describing the expected arguments.
	InputSchema json.RawMessage
}

ToolDef describes a tool the model may call.

type ToolInvocation

type ToolInvocation struct {
	ID     string
	Name   string
	Args   json.RawMessage
	Result json.RawMessage
	Err    string
}

ToolInvocation records a single tool call the model made.

type ToolRunner

type ToolRunner interface {
	Run(ctx context.Context, call ToolCall) (json.RawMessage, error)
}

ToolRunner executes tool calls on behalf of the harness. The funnel supplies the implementation; the harness never owns tools.

type TurnDone

type TurnDone struct {
	Result TurnResult
}

TurnDone fires after the turn completes successfully.

type TurnError

type TurnError struct {
	Err   error
	Stage string // "provider", "tool", "harness-recover", etc.
}

TurnError fires when the provider or harness hits a non-recoverable error. Never panics across the harness boundary.

type TurnRequest

type TurnRequest struct {
	// Identity & framing
	AspectID           string         // who's running (cost/triage/identity attribution)
	AppendSystemPrompt string         // composed by funnel: NEXUS.md + SOUL.md + PRIMER + harness rules
	Session            SessionHandle  // opaque handle for provider-side state (subprocess-stream: resume key)
	SessionTail        []SessionEvent // recent events for direct-api providers to lower into the request

	// This turn
	UserMessage string      // the prompt that opens this turn (may be empty for autonomous)
	Inbox       []InboxItem // mid-turn comms accumulated since last turn

	// Tool surface
	Tools []ToolDef        // explicit in-process tool defs
	MCP   *MCPClientConfig // MCP-loaded tools; nil = no MCP tools this turn

	// Provider
	Provider ProviderID // claude-api | ollama-local | openai-api | claude-code
	Model    string     // REQUIRED — provider-specific model id; RunTurn returns ErrModelRequired if empty
	MaxSteps int        // hard cap on tool-call rounds; 0 = unlimited

	// ToolChoice optionally constrains how the model picks tools.
	// Empty string → provider default (typically "auto").
	// "auto" → model decides whether to call a tool.
	// "any" → model must call exactly one tool, free choice of which.
	// "none" → no tools may be called this turn (text only).
	// Any other value → name of a specific tool that must be called.
	// Not all providers honour all values; unsupported values fall back to "auto".
	ToolChoice string

	// Cwd is the working directory for subprocess-style providers
	// (currently claude-code). Empty falls through to the bridle host
	// process's cwd. Per-request rather than per-Harness because
	// different aspects sharing one Harness need distinct cwds —
	// claude-code derives its session jsonl path AND its .mcp.json
	// discovery from cwd, so two aspects with the same Harness but
	// overlapping cwds collide sessions and leak MCP identity from one
	// into the other. Direct-API providers (claude-api, ollama, openai)
	// ignore this field — they have no subprocess to anchor.
	Cwd string

	// ProviderEnv is per-call environment for the provider. Direct-API
	// providers read it as their auth/routing config (commonly
	// ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL, OPENAI_API_KEY,
	// OPENAI_BASE_URL); subprocess providers (claude-code) propagate it
	// into the spawned process's env so the same per-turn override
	// pattern applies. nil/empty = use whatever the provider already
	// has on its own (process env, --bare-style flags, etc).
	//
	// Per-call rather than per-process so a single funnel can mix
	// credentials across turns — e.g. main turn against the operator's
	// Anthropic credit pool, judge turn against a DeepSeek-via-
	// Anthropic-shape credential, eval turn against OpenAI. The
	// credential store wires this from aspects.default_*_credential
	// per task #218.
	ProviderEnv map[string]string
}

TurnRequest is the complete input for one deliberation turn.

type TurnResult

type TurnResult struct {
	FinalText    string           // model's last assistant text (may be empty for tool-only turns)
	ToolCalls    []ToolInvocation // ordered list of what the model actually did
	StepCount    int
	Usage        Usage
	StopReason   StopReason
	SessionDelta []SessionEvent // events to propose to the funnel-owned JSONL
}

TurnResult is the structured outcome of a completed turn.

type Usage

type Usage struct {
	InputTokens              int
	OutputTokens             int
	CacheReadInputTokens     int     // Anthropic prompt-cache hit count
	CacheCreationInputTokens int     // tokens written into the prompt cache this turn
	CostUSD                  float64 // provider-reported or estimated; 0 if unknown
}

Usage holds token and cost data for a turn.

InputTokens is the count of UNCACHED prompt tokens billed at full rate. CacheReadInputTokens and CacheCreationInputTokens surface claude-api's prompt-caching behavior — the former is read at a discount, the latter is the new content being added to cache. Cache fields are zero for providers that don't expose caching (or don't run a cache-eligible request).

Sum (InputTokens + CacheReadInputTokens + CacheCreationInputTokens) approximates the total prompt size the model received. Use that for context-fullness reasoning; use InputTokens alone for billing estimates of fresh content.

Directories

Path Synopsis
Package fake provides scripted test doubles for the bridle harness.
Package fake provides scripted test doubles for the bridle harness.
internal
mcpclient
Package mcpclient wraps mark3labs/mcp-go to provide the bridle-internal MCP client used by direct-api providers.
Package mcpclient wraps mark3labs/mcp-go to provide the bridle-internal MCP client used by direct-api providers.
normalize
Package normalize provides helpers for mapping provider-specific wire values to bridle's canonical string constants.
Package normalize provides helpers for mapping provider-specific wire values to bridle's canonical string constants.
version
Package version holds the build-time version string for any binaries in this repo (today: just stubfunnel; future cmd helpers wire through here).
Package version holds the build-time version string for any binaries in this repo (today: just stubfunnel; future cmd helpers wire through here).
provider
bedrock
Package bedrock implements the bridle Provider interface for AWS Bedrock using the cross-model Converse API.
Package bedrock implements the bridle Provider interface for AWS Bedrock using the cross-model Converse API.
claude
Package claude implements the bridle Provider interface for the Anthropic Claude API.
Package claude implements the bridle Provider interface for the Anthropic Claude API.
claudecode
Package claudecode implements the bridle Provider interface using the claude-code CLI in headless mode (claude -p --output-format stream-json --verbose).
Package claudecode implements the bridle Provider interface using the claude-code CLI in headless mode (claude -p --output-format stream-json --verbose).
claudepty
Package claudepty implements the bridle Provider interface by spawning acp-claude-pty as a stdio subprocess and speaking ACP to it.
Package claudepty implements the bridle Provider interface by spawning acp-claude-pty as a stdio subprocess and speaking ACP to it.
gemini
Package gemini implements the bridle Provider interface for the Google Gemini API (Gemini Developer API or Vertex AI), via google.golang.org/genai.
Package gemini implements the bridle Provider interface for the Google Gemini API (Gemini Developer API or Vertex AI), via google.golang.org/genai.
geminicli
Package geminicli implements the bridle Provider interface using the gemini CLI in headless mode (gemini -p --output-format stream-json -y).
Package geminicli implements the bridle Provider interface using the gemini CLI in headless mode (gemini -p --output-format stream-json -y).
ollama
Package ollama implements the bridle Provider interface for a local Ollama server.
Package ollama implements the bridle Provider interface for a local Ollama server.
openai
Package openai implements the bridle Provider interface for the OpenAI API.
Package openai implements the bridle Provider interface for the OpenAI API.
Package stubfunnel is a temporary validation harness for bridle v0.1/patch1.
Package stubfunnel is a temporary validation harness for bridle v0.1/patch1.

Jump to

Keyboard shortcuts

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