host

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package host isolates what is specific to the agent host a mash session runs in (Claude Code today), so other hosts (Hermes, Pi) can plug in later. An Adapter says how a host's hook invocation is read, who owns a session's spool, how spooled mail is presented, and how serve's wake is delivered.

The wake is today the MCP channel notification serve already passes to the endpoint as its Deliverer; Waker is the seam through which an adapter shapes it. A future durable adapter (issue #43: explicit receive/ack MCP tools, a startup-declared durable mode) will extend this interface; it is deliberately not built here.

Index

Constants

This section is empty.

Variables

View Source
var Ancestors iter.Seq[int] = func(yield func(int) bool) {
	pid := os.Getppid()
	for i := 0; i < maxAncestors && pid > 1; i++ {
		if !yield(pid) {
			return
		}
		next, err := parentOf(pid)
		if err != nil {
			return
		}
		pid = next
	}
}

Ancestors yields the process ids above this one, nearest first, lazily (at most 10 levels, stopping at pid 1 or on an error): each level is a lookup (a /proc read, a sysctl, or a forked ps), so a caller that finds what it wants stops early. A var so tests can stub it. The hook is a descendant of the Claude process whose serve child owns the spool, normally its parent or grandparent.

Functions

func IsOtherLaunch added in v0.3.1

func IsOtherLaunch(why string) bool

IsOtherLaunch reports whether a Match refusal reason is the other-launch case.

func Known

func Known() []string

Known lists the host names Select accepts.

func Notice added in v0.3.1

func Notice(event, text string) []byte

Notice renders text as Claude Code hook additionalContext for event, in the shape Present uses.

func OwnerPID added in v0.3.1

func OwnerPID(owner string) int

OwnerPID is the Claude process pid an owner record (JSON or legacy "pid:N") names, 0 if none.

func Utf16Len

func Utf16Len(s string) int

Utf16Len exposes utf16Len to tests of other packages that assert the Claude Code cap.

func WaiterAncestor added in v0.3.1

func WaiterAncestor(ownerPID int, waiters []int) int

WaiterAncestor is the first of waiters that is among the hook's ancestors, provided the owner (ownerPID) is not itself a waiter and not among the ancestors; 0 otherwise.

Types

type Adapter

type Adapter interface {
	Name() string
	// Trigger reads a host hook invocation; ok=false means it is not a delivery point for us (do nothing).
	Trigger(stdin []byte, env func(string) string) (t Trigger, ok bool)
	// OwnerToken is what the lease-holding serve records as the spool's owner. env reads serve's environment.
	OwnerToken(env func(string) string) string
	// Owns reports whether this hook process may drain a spool recorded with owner.
	Owns(owner string, hook HookIdentity) bool
	// Match is Owns with the reason: which rule matched, or why every rule failed.
	Match(owner string, hook HookIdentity) (ok bool, why string)
	// Present renders entries for the host; shown are the entries actually rendered (only they are drained).
	Present(p Presentation, entries []spool.Entry) (out []byte, shown []spool.Entry)
	// SpoolsFromStart is true when the host takes no channel delivery (Pi never sees the channel notification): serve
	// then spools mail from its first message and acks only what a hook drained, never on a channel write.
	SpoolsFromStart() bool
	// Waker returns the delivery serve uses to wake the session, given the MCP channel notification.
	Waker(notify func(text string, meta map[string]string) error) func(text string, meta map[string]string) error
}

Adapter is how mash talks to the agent host a session runs in (Claude Code today).

func Select

func Select(name string) (Adapter, error)

Select returns the adapter for a host name: "" or "claude" is Claude Code.

type Claude

type Claude struct{}

Claude is the adapter for Claude Code: PostToolUse and UserPromptSubmit hooks, additionalContext output.

func (Claude) Match

func (Claude) Match(owner string, h HookIdentity) (bool, string)

Match decides whether the hook belongs to the session whose serve holds the lease. Any one of three rules, cheapest first, is enough; a hook that matches none is refused, and no rule adopts an unknown session:

  1. session: the hook's session id equals serve's (both non-empty), unless both launch tokens are non-empty and differ (another launch of the same session, ADR 0043);
  2. launch: both launch tokens are non-empty and equal (stable across /clear);
  3. process: serve's parent pid is among the hook's ancestors (covers /clear on the plain CLI).

Before them: a hook that descends from a serve recorded as waiting for the lease, and not from the owner, is refused as another launch (ADR 0043), tokens or not.

A legacy owner ("pid:N" text) is checked by rule 3 alone; an empty or other older owner is accepted.

func (Claude) Name

func (Claude) Name() string

func (Claude) OwnerToken

func (Claude) OwnerToken(env func(string) string) string

OwnerToken is the JSON owner record of the serve that holds the lease: its parent pid, its CLAUDE_CODE_SESSION_ID and its MASH_LAUNCH_TOKEN.

func (Claude) Owns

func (c Claude) Owns(owner string, h HookIdentity) bool

Owns reports whether the hook may drain a spool recorded with owner; see Match.

func (Claude) Present

func (Claude) Present(p Presentation, waiting []spool.Entry) ([]byte, []spool.Entry)

Present renders waiting as the hook's additionalContext.

func (Claude) SpoolsFromStart

func (Claude) SpoolsFromStart() bool

SpoolsFromStart: Claude Code takes channel events, so mail is the channel's until a hook first runs.

func (Claude) Trigger

func (Claude) Trigger(stdin []byte, env func(string) string) (Trigger, bool)

Trigger reads Claude Code's hook JSON.

func (Claude) Waker

func (Claude) Waker(notify func(text string, meta map[string]string) error) func(text string, meta map[string]string) error

Waker: Claude Code takes the wake as the MCP channel notification itself.

type HookIdentity

type HookIdentity struct {
	SessionID, LaunchToken string
	Waiters                []int
}

HookIdentity is what a hook process knows about the session it runs in: its session id (from its environment, else the hook's stdin), the launch token (MASH_LAUNCH_TOKEN) it inherited, and the pids of the serves recorded as waiting for the lease (spool.Waiters, ADR 0043).

type Presentation

type Presentation struct {
	Name, Owner           string
	Event                 string // the Trigger's event, for hosts that echo it
	BatchMsgs, BatchBytes int
}

Presentation is what Present needs to render a batch.

type Trigger

type Trigger struct {
	Event, SessionID string
	Subagent         bool
}

Trigger is a recognised host hook invocation. Subagent is set when it ran for a subagent's tool call, not the main session's: it must never drain, only show that hooks run.

Jump to

Keyboard shortcuts

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